17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
569bb4bb4Scarlsonj * Common Development and Distribution License (the "License").
669bb4bb4Scarlsonj * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*56f33205SJonathan Adams * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/cred.h>
357c478bd9Sstevel@tonic-gate #include <sys/user.h>
367c478bd9Sstevel@tonic-gate #include <sys/proc.h>
377c478bd9Sstevel@tonic-gate #include <sys/conf.h>
387c478bd9Sstevel@tonic-gate #include <sys/tuneable.h>
397c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
407c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
417c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
427c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
437c478bd9Sstevel@tonic-gate #include <sys/errno.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
477c478bd9Sstevel@tonic-gate #include <sys/model.h>
487c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
497c478bd9Sstevel@tonic-gate #include <sys/memlist.h>
507c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
517c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
527c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
537c478bd9Sstevel@tonic-gate #include <sys/zone.h>
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #ifdef __sparc
567c478bd9Sstevel@tonic-gate #include <sys/membar.h>
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate * Routine which sets a user error; placed in
617c478bd9Sstevel@tonic-gate * illegal entries in the bdevsw and cdevsw tables.
627c478bd9Sstevel@tonic-gate */
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate int
nodev()657c478bd9Sstevel@tonic-gate nodev()
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate return (curthread->t_lwp ?
687c478bd9Sstevel@tonic-gate ttolwp(curthread)->lwp_error = ENXIO : ENXIO);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate * Null routine; placed in insignificant entries
737c478bd9Sstevel@tonic-gate * in the bdevsw and cdevsw tables.
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate int
nulldev()777c478bd9Sstevel@tonic-gate nulldev()
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate return (0);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate static kmutex_t udevlock;
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate * Generate an unused major device number.
867c478bd9Sstevel@tonic-gate */
877c478bd9Sstevel@tonic-gate major_t
getudev()887c478bd9Sstevel@tonic-gate getudev()
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate static major_t next = 0;
917c478bd9Sstevel@tonic-gate major_t ret;
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate * Ensure that we start allocating major numbers above the 'devcnt'
957c478bd9Sstevel@tonic-gate * count. The only limit we place on the number is that it should be a
967c478bd9Sstevel@tonic-gate * legal 32-bit SVR4 major number and be greater than or equal to devcnt
977c478bd9Sstevel@tonic-gate * in the current system).
987c478bd9Sstevel@tonic-gate */
997c478bd9Sstevel@tonic-gate mutex_enter(&udevlock);
1007c478bd9Sstevel@tonic-gate if (next == 0)
1017c478bd9Sstevel@tonic-gate next = devcnt;
1027c478bd9Sstevel@tonic-gate if (next <= L_MAXMAJ32 && next >= devcnt)
1037c478bd9Sstevel@tonic-gate ret = next++;
1047c478bd9Sstevel@tonic-gate else {
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate * If we fail to allocate a major number because devcnt has
1077c478bd9Sstevel@tonic-gate * reached L_MAXMAJ32, we may be the victim of a sparsely
1087c478bd9Sstevel@tonic-gate * populated devnames array. We scan the array backwards
1097c478bd9Sstevel@tonic-gate * looking for an empty slot; if we find one, mark it as
1107c478bd9Sstevel@tonic-gate * DN_GETUDEV so it doesn't get taken by subsequent consumers
1117c478bd9Sstevel@tonic-gate * users of the devnames array, and issue a warning.
1127c478bd9Sstevel@tonic-gate * It is vital for this routine to take drastic measures to
1137c478bd9Sstevel@tonic-gate * succeed, since the kernel really needs it to boot.
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate int i;
1167c478bd9Sstevel@tonic-gate for (i = devcnt - 1; i >= 0; i--) {
1177c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&devnamesp[i].dn_lock);
1187c478bd9Sstevel@tonic-gate if (devnamesp[i].dn_name == NULL &&
1197c478bd9Sstevel@tonic-gate ((devnamesp[i].dn_flags & DN_TAKEN_GETUDEV) == 0))
1207c478bd9Sstevel@tonic-gate break;
1217c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&devnamesp[i].dn_lock);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate if (i != -1) {
1247c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "Reusing device major number %d.", i);
1257c478bd9Sstevel@tonic-gate ASSERT(i >= 0 && i < devcnt);
1267c478bd9Sstevel@tonic-gate devnamesp[i].dn_flags |= DN_TAKEN_GETUDEV;
1277c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&devnamesp[i].dn_lock);
1287c478bd9Sstevel@tonic-gate ret = (major_t)i;
1297c478bd9Sstevel@tonic-gate } else {
130a204de77Scth ret = DDI_MAJOR_T_NONE;
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate mutex_exit(&udevlock);
1347c478bd9Sstevel@tonic-gate return (ret);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * Compress 'long' device number encoding to 32-bit device number
1407c478bd9Sstevel@tonic-gate * encoding. If it won't fit, we return failure, but set the
1417c478bd9Sstevel@tonic-gate * device number to 32-bit NODEV for the sake of our callers.
1427c478bd9Sstevel@tonic-gate */
1437c478bd9Sstevel@tonic-gate int
cmpldev(dev32_t * dst,dev_t dev)1447c478bd9Sstevel@tonic-gate cmpldev(dev32_t *dst, dev_t dev)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate #if defined(_LP64)
1477c478bd9Sstevel@tonic-gate if (dev == NODEV) {
1487c478bd9Sstevel@tonic-gate *dst = NODEV32;
1497c478bd9Sstevel@tonic-gate } else {
1507c478bd9Sstevel@tonic-gate major_t major = dev >> L_BITSMINOR;
1517c478bd9Sstevel@tonic-gate minor_t minor = dev & L_MAXMIN;
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate if (major > L_MAXMAJ32 || minor > L_MAXMIN32) {
1547c478bd9Sstevel@tonic-gate *dst = NODEV32;
1557c478bd9Sstevel@tonic-gate return (0);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate *dst = (dev32_t)((major << L_BITSMINOR32) | minor);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate #else
1617c478bd9Sstevel@tonic-gate *dst = (dev32_t)dev;
1627c478bd9Sstevel@tonic-gate #endif
1637c478bd9Sstevel@tonic-gate return (1);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate * Expand 32-bit dev_t's to long dev_t's. Expansion always "fits"
1687c478bd9Sstevel@tonic-gate * into the return type, but we're careful to expand NODEV explicitly.
1697c478bd9Sstevel@tonic-gate */
1707c478bd9Sstevel@tonic-gate dev_t
expldev(dev32_t dev32)1717c478bd9Sstevel@tonic-gate expldev(dev32_t dev32)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate #ifdef _LP64
1747c478bd9Sstevel@tonic-gate if (dev32 == NODEV32)
1757c478bd9Sstevel@tonic-gate return (NODEV);
1767c478bd9Sstevel@tonic-gate return (makedevice((dev32 >> L_BITSMINOR32) & L_MAXMAJ32,
1777c478bd9Sstevel@tonic-gate dev32 & L_MAXMIN32));
1787c478bd9Sstevel@tonic-gate #else
1797c478bd9Sstevel@tonic-gate return ((dev_t)dev32);
1807c478bd9Sstevel@tonic-gate #endif
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate #ifndef _LP64
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate * Keep these entry points for 32-bit systems but enforce the use
1867c478bd9Sstevel@tonic-gate * of MIN/MAX macros on 64-bit systems. The DDI header files already
1877c478bd9Sstevel@tonic-gate * define min/max as macros so drivers shouldn't need these functions.
1887c478bd9Sstevel@tonic-gate */
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate int
min(int a,int b)1917c478bd9Sstevel@tonic-gate min(int a, int b)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate return (a < b ? a : b);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate int
max(int a,int b)1977c478bd9Sstevel@tonic-gate max(int a, int b)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate return (a > b ? a : b);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate uint_t
umin(uint_t a,uint_t b)2037c478bd9Sstevel@tonic-gate umin(uint_t a, uint_t b)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate return (a < b ? a : b);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate uint_t
umax(uint_t a,uint_t b)2097c478bd9Sstevel@tonic-gate umax(uint_t a, uint_t b)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate return (a > b ? a : b);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate #endif /* !_LP64 */
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate * Parse suboptions from a string.
2187c478bd9Sstevel@tonic-gate * Same as getsubopt(3C).
2197c478bd9Sstevel@tonic-gate */
2207c478bd9Sstevel@tonic-gate int
getsubopt(char ** optionsp,char * const * tokens,char ** valuep)2217c478bd9Sstevel@tonic-gate getsubopt(char **optionsp, char * const *tokens, char **valuep)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate char *s = *optionsp, *p;
2247c478bd9Sstevel@tonic-gate int i;
2257c478bd9Sstevel@tonic-gate size_t optlen;
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate *valuep = NULL;
2287c478bd9Sstevel@tonic-gate if (*s == '\0')
2297c478bd9Sstevel@tonic-gate return (-1);
2307c478bd9Sstevel@tonic-gate p = strchr(s, ','); /* find next option */
2317c478bd9Sstevel@tonic-gate if (p == NULL) {
2327c478bd9Sstevel@tonic-gate p = s + strlen(s);
2337c478bd9Sstevel@tonic-gate } else {
2347c478bd9Sstevel@tonic-gate *p++ = '\0'; /* mark end and point to next */
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate *optionsp = p; /* point to next option */
2377c478bd9Sstevel@tonic-gate p = strchr(s, '='); /* find value */
2387c478bd9Sstevel@tonic-gate if (p == NULL) {
2397c478bd9Sstevel@tonic-gate optlen = strlen(s);
2407c478bd9Sstevel@tonic-gate *valuep = NULL;
2417c478bd9Sstevel@tonic-gate } else {
2427c478bd9Sstevel@tonic-gate optlen = p - s;
2437c478bd9Sstevel@tonic-gate *valuep = ++p;
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate for (i = 0; tokens[i] != NULL; i++) {
2467c478bd9Sstevel@tonic-gate if ((optlen == strlen(tokens[i])) &&
2477c478bd9Sstevel@tonic-gate (strncmp(s, tokens[i], optlen) == 0))
2487c478bd9Sstevel@tonic-gate return (i);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate /* no match, point value at option and return error */
2517c478bd9Sstevel@tonic-gate *valuep = s;
2527c478bd9Sstevel@tonic-gate return (-1);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate * Append the suboption string 'opt' starting at the position 'str'
2577c478bd9Sstevel@tonic-gate * within the buffer defined by 'buf' and 'len'. If 'buf' is not null,
2587c478bd9Sstevel@tonic-gate * a comma is appended first.
2597c478bd9Sstevel@tonic-gate * Return a pointer to the end of the resulting string (the null byte).
2607c478bd9Sstevel@tonic-gate * Return NULL if there isn't enough space left to append 'opt'.
2617c478bd9Sstevel@tonic-gate */
2627c478bd9Sstevel@tonic-gate char *
append_subopt(const char * buf,size_t len,char * str,const char * opt)2637c478bd9Sstevel@tonic-gate append_subopt(const char *buf, size_t len, char *str, const char *opt)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate size_t l = strlen(opt);
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate * Include a ',' if this is not the first option.
2697c478bd9Sstevel@tonic-gate * Include space for the null byte.
2707c478bd9Sstevel@tonic-gate */
2717c478bd9Sstevel@tonic-gate if (strlen(buf) + (buf[0] != '\0') + l + 1 > len)
2727c478bd9Sstevel@tonic-gate return (NULL);
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate if (buf[0] != '\0')
2757c478bd9Sstevel@tonic-gate *str++ = ',';
2767c478bd9Sstevel@tonic-gate (void) strcpy(str, opt);
2777c478bd9Sstevel@tonic-gate return (str + l);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate * Tables to convert a single byte to/from binary-coded decimal (BCD).
2827c478bd9Sstevel@tonic-gate */
2837c478bd9Sstevel@tonic-gate uchar_t byte_to_bcd[256] = {
2847c478bd9Sstevel@tonic-gate 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2857c478bd9Sstevel@tonic-gate 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
2867c478bd9Sstevel@tonic-gate 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
2877c478bd9Sstevel@tonic-gate 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
2887c478bd9Sstevel@tonic-gate 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
2897c478bd9Sstevel@tonic-gate 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
2907c478bd9Sstevel@tonic-gate 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
2917c478bd9Sstevel@tonic-gate 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
2927c478bd9Sstevel@tonic-gate 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
2937c478bd9Sstevel@tonic-gate 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
2947c478bd9Sstevel@tonic-gate };
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate uchar_t bcd_to_byte[256] = { /* CSTYLED */
2977c478bd9Sstevel@tonic-gate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
2987c478bd9Sstevel@tonic-gate 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0,
2997c478bd9Sstevel@tonic-gate 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 0, 0, 0, 0, 0, 0,
3007c478bd9Sstevel@tonic-gate 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 0, 0, 0, 0, 0, 0,
3017c478bd9Sstevel@tonic-gate 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 0, 0, 0, 0, 0, 0,
3027c478bd9Sstevel@tonic-gate 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 0, 0, 0, 0, 0, 0,
3037c478bd9Sstevel@tonic-gate 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 0, 0, 0, 0, 0, 0,
3047c478bd9Sstevel@tonic-gate 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 0, 0, 0, 0, 0, 0,
3057c478bd9Sstevel@tonic-gate 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 0, 0, 0, 0, 0, 0,
3067c478bd9Sstevel@tonic-gate 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
3077c478bd9Sstevel@tonic-gate };
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate * Hot-patch a single instruction in the kernel's text.
3117c478bd9Sstevel@tonic-gate * If you want to patch multiple instructions you must
3127c478bd9Sstevel@tonic-gate * arrange to do it so that all intermediate stages are
3137c478bd9Sstevel@tonic-gate * sane -- we don't stop other cpus while doing this.
3147c478bd9Sstevel@tonic-gate * Size must be 1, 2, or 4 bytes with iaddr aligned accordingly.
3157c478bd9Sstevel@tonic-gate */
3167c478bd9Sstevel@tonic-gate void
hot_patch_kernel_text(caddr_t iaddr,uint32_t new_instr,uint_t size)3177c478bd9Sstevel@tonic-gate hot_patch_kernel_text(caddr_t iaddr, uint32_t new_instr, uint_t size)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate caddr_t vaddr;
3207c478bd9Sstevel@tonic-gate page_t **ppp;
3217c478bd9Sstevel@tonic-gate uintptr_t off = (uintptr_t)iaddr & PAGEOFFSET;
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate vaddr = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate (void) as_pagelock(&kas, &ppp, iaddr - off, PAGESIZE, S_WRITE);
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate hat_devload(kas.a_hat, vaddr, PAGESIZE,
3287c478bd9Sstevel@tonic-gate hat_getpfnum(kas.a_hat, iaddr - off),
3297c478bd9Sstevel@tonic-gate PROT_READ | PROT_WRITE, HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate switch (size) {
3327c478bd9Sstevel@tonic-gate case 1:
3337c478bd9Sstevel@tonic-gate *(uint8_t *)(vaddr + off) = new_instr;
3347c478bd9Sstevel@tonic-gate break;
3357c478bd9Sstevel@tonic-gate case 2:
3367c478bd9Sstevel@tonic-gate *(uint16_t *)(vaddr + off) = new_instr;
3377c478bd9Sstevel@tonic-gate break;
3387c478bd9Sstevel@tonic-gate case 4:
3397c478bd9Sstevel@tonic-gate *(uint32_t *)(vaddr + off) = new_instr;
3407c478bd9Sstevel@tonic-gate break;
3417c478bd9Sstevel@tonic-gate default:
3427c478bd9Sstevel@tonic-gate panic("illegal hot-patch");
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate membar_enter();
3467c478bd9Sstevel@tonic-gate sync_icache(vaddr + off, size);
3477c478bd9Sstevel@tonic-gate sync_icache(iaddr, size);
3487c478bd9Sstevel@tonic-gate as_pageunlock(&kas, ppp, iaddr - off, PAGESIZE, S_WRITE);
3497c478bd9Sstevel@tonic-gate hat_unload(kas.a_hat, vaddr, PAGESIZE, HAT_UNLOAD_UNLOCK);
3507c478bd9Sstevel@tonic-gate vmem_free(heap_arena, vaddr, PAGESIZE);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate * Routine to report an attempt to execute non-executable data. If the
3557c478bd9Sstevel@tonic-gate * address executed lies in the stack, explicitly say so.
3567c478bd9Sstevel@tonic-gate */
3577c478bd9Sstevel@tonic-gate void
report_stack_exec(proc_t * p,caddr_t addr)3587c478bd9Sstevel@tonic-gate report_stack_exec(proc_t *p, caddr_t addr)
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate if (!noexec_user_stack_log)
3617c478bd9Sstevel@tonic-gate return;
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate if (addr < p->p_usrstack && addr >= (p->p_usrstack - p->p_stksize)) {
3647c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "%s[%d] attempt to execute code "
3657c478bd9Sstevel@tonic-gate "on stack by uid %d", p->p_user.u_comm,
3667c478bd9Sstevel@tonic-gate p->p_pid, crgetruid(p->p_cred));
3677c478bd9Sstevel@tonic-gate } else {
3687c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "%s[%d] attempt to execute non-executable "
3697c478bd9Sstevel@tonic-gate "data at 0x%p by uid %d", p->p_user.u_comm,
3707c478bd9Sstevel@tonic-gate p->p_pid, (void *) addr, crgetruid(p->p_cred));
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate delay(hz / 50);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate /*
3777c478bd9Sstevel@tonic-gate * Determine whether the address range [addr, addr + len) is in memlist mp.
3787c478bd9Sstevel@tonic-gate */
3797c478bd9Sstevel@tonic-gate int
address_in_memlist(struct memlist * mp,uint64_t addr,size_t len)3807c478bd9Sstevel@tonic-gate address_in_memlist(struct memlist *mp, uint64_t addr, size_t len)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate while (mp != 0) {
383*56f33205SJonathan Adams if ((addr >= mp->ml_address) &&
384*56f33205SJonathan Adams (addr + len <= mp->ml_address + mp->ml_size))
3857c478bd9Sstevel@tonic-gate return (1); /* TRUE */
386*56f33205SJonathan Adams mp = mp->ml_next;
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate return (0); /* FALSE */
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate * Pop the topmost element from the t_ontrap stack, removing the current set of
3937c478bd9Sstevel@tonic-gate * on_trap() protections. Refer to <sys/ontrap.h> for more info. If the
3947c478bd9Sstevel@tonic-gate * stack is already empty, no_trap() just returns.
3957c478bd9Sstevel@tonic-gate */
3967c478bd9Sstevel@tonic-gate void
no_trap(void)3977c478bd9Sstevel@tonic-gate no_trap(void)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate if (curthread->t_ontrap != NULL) {
4007c478bd9Sstevel@tonic-gate #ifdef __sparc
4017c478bd9Sstevel@tonic-gate membar_sync(); /* deferred error barrier (see sparcv9_subr.s) */
4027c478bd9Sstevel@tonic-gate #endif
4037c478bd9Sstevel@tonic-gate curthread->t_ontrap = curthread->t_ontrap->ot_prev;
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate * Return utsname.nodename outside a zone, or the zone name within.
4097c478bd9Sstevel@tonic-gate */
4107c478bd9Sstevel@tonic-gate char *
uts_nodename(void)4117c478bd9Sstevel@tonic-gate uts_nodename(void)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate if (curproc == NULL)
4147c478bd9Sstevel@tonic-gate return (utsname.nodename);
4157c478bd9Sstevel@tonic-gate return (curproc->p_zone->zone_nodename);
4167c478bd9Sstevel@tonic-gate }
417