xref: /onnv-gate/usr/src/uts/common/os/subr.c (revision 11474:857f9db4ef05)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52546Scarlsonj  * Common Development and Distribution License (the "License").
62546Scarlsonj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11474SJonathan.Adams@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/vmparam.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/cred.h>
350Sstevel@tonic-gate #include <sys/user.h>
360Sstevel@tonic-gate #include <sys/proc.h>
370Sstevel@tonic-gate #include <sys/conf.h>
380Sstevel@tonic-gate #include <sys/tuneable.h>
390Sstevel@tonic-gate #include <sys/cpuvar.h>
400Sstevel@tonic-gate #include <sys/archsystm.h>
410Sstevel@tonic-gate #include <sys/vmem.h>
420Sstevel@tonic-gate #include <vm/seg_kmem.h>
430Sstevel@tonic-gate #include <sys/errno.h>
440Sstevel@tonic-gate #include <sys/cmn_err.h>
450Sstevel@tonic-gate #include <sys/debug.h>
460Sstevel@tonic-gate #include <sys/atomic.h>
470Sstevel@tonic-gate #include <sys/model.h>
480Sstevel@tonic-gate #include <sys/kmem.h>
490Sstevel@tonic-gate #include <sys/memlist.h>
500Sstevel@tonic-gate #include <sys/autoconf.h>
510Sstevel@tonic-gate #include <sys/ontrap.h>
520Sstevel@tonic-gate #include <sys/utsname.h>
530Sstevel@tonic-gate #include <sys/zone.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #ifdef __sparc
560Sstevel@tonic-gate #include <sys/membar.h>
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Routine which sets a user error; placed in
610Sstevel@tonic-gate  * illegal entries in the bdevsw and cdevsw tables.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate int
nodev()650Sstevel@tonic-gate nodev()
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	return (curthread->t_lwp ?
680Sstevel@tonic-gate 	    ttolwp(curthread)->lwp_error = ENXIO : ENXIO);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * Null routine; placed in insignificant entries
730Sstevel@tonic-gate  * in the bdevsw and cdevsw tables.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate int
nulldev()770Sstevel@tonic-gate nulldev()
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	return (0);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static kmutex_t udevlock;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Generate an unused major device number.
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate major_t
getudev()880Sstevel@tonic-gate getudev()
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	static major_t next = 0;
910Sstevel@tonic-gate 	major_t ret;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/*
940Sstevel@tonic-gate 	 * Ensure that we start allocating major numbers above the 'devcnt'
950Sstevel@tonic-gate 	 * count.  The only limit we place on the number is that it should be a
960Sstevel@tonic-gate 	 * legal 32-bit SVR4 major number and be greater than or equal to devcnt
970Sstevel@tonic-gate 	 * in the current system).
980Sstevel@tonic-gate 	 */
990Sstevel@tonic-gate 	mutex_enter(&udevlock);
1000Sstevel@tonic-gate 	if (next == 0)
1010Sstevel@tonic-gate 		next = devcnt;
1020Sstevel@tonic-gate 	if (next <= L_MAXMAJ32 && next >= devcnt)
1030Sstevel@tonic-gate 		ret = next++;
1040Sstevel@tonic-gate 	else {
1050Sstevel@tonic-gate 		/*
1060Sstevel@tonic-gate 		 * If we fail to allocate a major number because devcnt has
1070Sstevel@tonic-gate 		 * reached L_MAXMAJ32, we may be the victim of a sparsely
1080Sstevel@tonic-gate 		 * populated devnames array.  We scan the array backwards
1090Sstevel@tonic-gate 		 * looking for an empty slot;  if we find one, mark it as
1100Sstevel@tonic-gate 		 * DN_GETUDEV so it doesn't get taken by subsequent consumers
1110Sstevel@tonic-gate 		 * users of the devnames array, and issue a warning.
1120Sstevel@tonic-gate 		 * It is vital for this routine to take drastic measures to
1130Sstevel@tonic-gate 		 * succeed, since the kernel really needs it to boot.
1140Sstevel@tonic-gate 		 */
1150Sstevel@tonic-gate 		int i;
1160Sstevel@tonic-gate 		for (i = devcnt - 1; i >= 0; i--) {
1170Sstevel@tonic-gate 			LOCK_DEV_OPS(&devnamesp[i].dn_lock);
1180Sstevel@tonic-gate 			if (devnamesp[i].dn_name == NULL &&
1190Sstevel@tonic-gate 			    ((devnamesp[i].dn_flags & DN_TAKEN_GETUDEV) == 0))
1200Sstevel@tonic-gate 				break;
1210Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&devnamesp[i].dn_lock);
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 		if (i != -1) {
1240Sstevel@tonic-gate 			cmn_err(CE_WARN, "Reusing device major number %d.", i);
1250Sstevel@tonic-gate 			ASSERT(i >= 0 && i < devcnt);
1260Sstevel@tonic-gate 			devnamesp[i].dn_flags |= DN_TAKEN_GETUDEV;
1270Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&devnamesp[i].dn_lock);
1280Sstevel@tonic-gate 			ret = (major_t)i;
1290Sstevel@tonic-gate 		} else {
1307009Scth 			ret = DDI_MAJOR_T_NONE;
1310Sstevel@tonic-gate 		}
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	mutex_exit(&udevlock);
1340Sstevel@tonic-gate 	return (ret);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * Compress 'long' device number encoding to 32-bit device number
1400Sstevel@tonic-gate  * encoding.  If it won't fit, we return failure, but set the
1410Sstevel@tonic-gate  * device number to 32-bit NODEV for the sake of our callers.
1420Sstevel@tonic-gate  */
1430Sstevel@tonic-gate int
cmpldev(dev32_t * dst,dev_t dev)1440Sstevel@tonic-gate cmpldev(dev32_t *dst, dev_t dev)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate #if defined(_LP64)
1470Sstevel@tonic-gate 	if (dev == NODEV) {
1480Sstevel@tonic-gate 		*dst = NODEV32;
1490Sstevel@tonic-gate 	} else {
1500Sstevel@tonic-gate 		major_t major = dev >> L_BITSMINOR;
1510Sstevel@tonic-gate 		minor_t minor = dev & L_MAXMIN;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 		if (major > L_MAXMAJ32 || minor > L_MAXMIN32) {
1540Sstevel@tonic-gate 			*dst = NODEV32;
1550Sstevel@tonic-gate 			return (0);
1560Sstevel@tonic-gate 		}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		*dst = (dev32_t)((major << L_BITSMINOR32) | minor);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate #else
1610Sstevel@tonic-gate 	*dst = (dev32_t)dev;
1620Sstevel@tonic-gate #endif
1630Sstevel@tonic-gate 	return (1);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * Expand 32-bit dev_t's to long dev_t's.  Expansion always "fits"
1680Sstevel@tonic-gate  * into the return type, but we're careful to expand NODEV explicitly.
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate dev_t
expldev(dev32_t dev32)1710Sstevel@tonic-gate expldev(dev32_t dev32)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate #ifdef _LP64
1740Sstevel@tonic-gate 	if (dev32 == NODEV32)
1750Sstevel@tonic-gate 		return (NODEV);
1760Sstevel@tonic-gate 	return (makedevice((dev32 >> L_BITSMINOR32) & L_MAXMAJ32,
1770Sstevel@tonic-gate 	    dev32 & L_MAXMIN32));
1780Sstevel@tonic-gate #else
1790Sstevel@tonic-gate 	return ((dev_t)dev32);
1800Sstevel@tonic-gate #endif
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate #ifndef _LP64
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * Keep these entry points for 32-bit systems but enforce the use
1860Sstevel@tonic-gate  * of MIN/MAX macros on 64-bit systems.  The DDI header files already
1870Sstevel@tonic-gate  * define min/max as macros so drivers shouldn't need these functions.
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate int
min(int a,int b)1910Sstevel@tonic-gate min(int a, int b)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	return (a < b ? a : b);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate int
max(int a,int b)1970Sstevel@tonic-gate max(int a, int b)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	return (a > b ? a : b);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate uint_t
umin(uint_t a,uint_t b)2030Sstevel@tonic-gate umin(uint_t a, uint_t b)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	return (a < b ? a : b);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate uint_t
umax(uint_t a,uint_t b)2090Sstevel@tonic-gate umax(uint_t a, uint_t b)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	return (a > b ? a : b);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate #endif /* !_LP64 */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate  * Parse suboptions from a string.
2180Sstevel@tonic-gate  * Same as getsubopt(3C).
2190Sstevel@tonic-gate  */
2200Sstevel@tonic-gate int
getsubopt(char ** optionsp,char * const * tokens,char ** valuep)2210Sstevel@tonic-gate getsubopt(char **optionsp, char * const *tokens, char **valuep)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	char *s = *optionsp, *p;
2240Sstevel@tonic-gate 	int i;
2250Sstevel@tonic-gate 	size_t optlen;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	*valuep = NULL;
2280Sstevel@tonic-gate 	if (*s == '\0')
2290Sstevel@tonic-gate 		return (-1);
2300Sstevel@tonic-gate 	p = strchr(s, ',');		/* find next option */
2310Sstevel@tonic-gate 	if (p == NULL) {
2320Sstevel@tonic-gate 		p = s + strlen(s);
2330Sstevel@tonic-gate 	} else {
2340Sstevel@tonic-gate 		*p++ = '\0';		/* mark end and point to next */
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 	*optionsp = p;			/* point to next option */
2370Sstevel@tonic-gate 	p = strchr(s, '=');		/* find value */
2380Sstevel@tonic-gate 	if (p == NULL) {
2390Sstevel@tonic-gate 		optlen = strlen(s);
2400Sstevel@tonic-gate 		*valuep = NULL;
2410Sstevel@tonic-gate 	} else {
2420Sstevel@tonic-gate 		optlen = p - s;
2430Sstevel@tonic-gate 		*valuep = ++p;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 	for (i = 0; tokens[i] != NULL; i++) {
2460Sstevel@tonic-gate 		if ((optlen == strlen(tokens[i])) &&
2470Sstevel@tonic-gate 		    (strncmp(s, tokens[i], optlen) == 0))
2480Sstevel@tonic-gate 			return (i);
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 	/* no match, point value at option and return error */
2510Sstevel@tonic-gate 	*valuep = s;
2520Sstevel@tonic-gate 	return (-1);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate  * Append the suboption string 'opt' starting at the position 'str'
2570Sstevel@tonic-gate  * within the buffer defined by 'buf' and 'len'. If 'buf' is not null,
2580Sstevel@tonic-gate  * a comma is appended first.
2590Sstevel@tonic-gate  * Return a pointer to the end of the resulting string (the null byte).
2600Sstevel@tonic-gate  * Return NULL if there isn't enough space left to append 'opt'.
2610Sstevel@tonic-gate  */
2620Sstevel@tonic-gate char *
append_subopt(const char * buf,size_t len,char * str,const char * opt)2630Sstevel@tonic-gate append_subopt(const char *buf, size_t len, char *str, const char *opt)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	size_t l = strlen(opt);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	/*
2680Sstevel@tonic-gate 	 * Include a ',' if this is not the first option.
2690Sstevel@tonic-gate 	 * Include space for the null byte.
2700Sstevel@tonic-gate 	 */
2710Sstevel@tonic-gate 	if (strlen(buf) + (buf[0] != '\0') + l + 1 > len)
2720Sstevel@tonic-gate 		return (NULL);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	if (buf[0] != '\0')
2750Sstevel@tonic-gate 		*str++ = ',';
2760Sstevel@tonic-gate 	(void) strcpy(str, opt);
2770Sstevel@tonic-gate 	return (str + l);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * Tables to convert a single byte to/from binary-coded decimal (BCD).
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate uchar_t byte_to_bcd[256] = {
2840Sstevel@tonic-gate 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2850Sstevel@tonic-gate 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
2860Sstevel@tonic-gate 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
2870Sstevel@tonic-gate 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
2880Sstevel@tonic-gate 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
2890Sstevel@tonic-gate 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
2900Sstevel@tonic-gate 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
2910Sstevel@tonic-gate 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
2920Sstevel@tonic-gate 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
2930Sstevel@tonic-gate 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
2940Sstevel@tonic-gate };
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate uchar_t bcd_to_byte[256] = {		/* CSTYLED */
2970Sstevel@tonic-gate 	 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  0,  0,  0,  0,  0,  0,
2980Sstevel@tonic-gate 	10, 11, 12, 13, 14, 15, 16, 17, 18, 19,  0,  0,  0,  0,  0,  0,
2990Sstevel@tonic-gate 	20, 21, 22, 23, 24, 25, 26, 27, 28, 29,  0,  0,  0,  0,  0,  0,
3000Sstevel@tonic-gate 	30, 31, 32, 33, 34, 35, 36, 37, 38, 39,  0,  0,  0,  0,  0,  0,
3010Sstevel@tonic-gate 	40, 41, 42, 43, 44, 45, 46, 47, 48, 49,  0,  0,  0,  0,  0,  0,
3020Sstevel@tonic-gate 	50, 51, 52, 53, 54, 55, 56, 57, 58, 59,  0,  0,  0,  0,  0,  0,
3030Sstevel@tonic-gate 	60, 61, 62, 63, 64, 65, 66, 67, 68, 69,  0,  0,  0,  0,  0,  0,
3040Sstevel@tonic-gate 	70, 71, 72, 73, 74, 75, 76, 77, 78, 79,  0,  0,  0,  0,  0,  0,
3050Sstevel@tonic-gate 	80, 81, 82, 83, 84, 85, 86, 87, 88, 89,  0,  0,  0,  0,  0,  0,
3060Sstevel@tonic-gate 	90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
3070Sstevel@tonic-gate };
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate  * Hot-patch a single instruction in the kernel's text.
3110Sstevel@tonic-gate  * If you want to patch multiple instructions you must
3120Sstevel@tonic-gate  * arrange to do it so that all intermediate stages are
3130Sstevel@tonic-gate  * sane -- we don't stop other cpus while doing this.
3140Sstevel@tonic-gate  * Size must be 1, 2, or 4 bytes with iaddr aligned accordingly.
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate void
hot_patch_kernel_text(caddr_t iaddr,uint32_t new_instr,uint_t size)3170Sstevel@tonic-gate hot_patch_kernel_text(caddr_t iaddr, uint32_t new_instr, uint_t size)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	caddr_t vaddr;
3200Sstevel@tonic-gate 	page_t **ppp;
3210Sstevel@tonic-gate 	uintptr_t off = (uintptr_t)iaddr & PAGEOFFSET;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	(void) as_pagelock(&kas, &ppp, iaddr - off, PAGESIZE, S_WRITE);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	hat_devload(kas.a_hat, vaddr, PAGESIZE,
3280Sstevel@tonic-gate 	    hat_getpfnum(kas.a_hat, iaddr - off),
3290Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE, HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	switch (size) {
3320Sstevel@tonic-gate 	case 1:
3330Sstevel@tonic-gate 		*(uint8_t *)(vaddr + off) = new_instr;
3340Sstevel@tonic-gate 		break;
3350Sstevel@tonic-gate 	case 2:
3360Sstevel@tonic-gate 		*(uint16_t *)(vaddr + off) = new_instr;
3370Sstevel@tonic-gate 		break;
3380Sstevel@tonic-gate 	case 4:
3390Sstevel@tonic-gate 		*(uint32_t *)(vaddr + off) = new_instr;
3400Sstevel@tonic-gate 		break;
3410Sstevel@tonic-gate 	default:
3420Sstevel@tonic-gate 		panic("illegal hot-patch");
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	membar_enter();
3460Sstevel@tonic-gate 	sync_icache(vaddr + off, size);
3470Sstevel@tonic-gate 	sync_icache(iaddr, size);
3480Sstevel@tonic-gate 	as_pageunlock(&kas, ppp, iaddr - off, PAGESIZE, S_WRITE);
3490Sstevel@tonic-gate 	hat_unload(kas.a_hat, vaddr, PAGESIZE, HAT_UNLOAD_UNLOCK);
3500Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, PAGESIZE);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate  * Routine to report an attempt to execute non-executable data.  If the
3550Sstevel@tonic-gate  * address executed lies in the stack, explicitly say so.
3560Sstevel@tonic-gate  */
3570Sstevel@tonic-gate void
report_stack_exec(proc_t * p,caddr_t addr)3580Sstevel@tonic-gate report_stack_exec(proc_t *p, caddr_t addr)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	if (!noexec_user_stack_log)
3610Sstevel@tonic-gate 		return;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	if (addr < p->p_usrstack && addr >= (p->p_usrstack - p->p_stksize)) {
3640Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s[%d] attempt to execute code "
3650Sstevel@tonic-gate 		    "on stack by uid %d", p->p_user.u_comm,
3660Sstevel@tonic-gate 		    p->p_pid, crgetruid(p->p_cred));
3670Sstevel@tonic-gate 	} else {
3680Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s[%d] attempt to execute non-executable "
3690Sstevel@tonic-gate 		    "data at 0x%p by uid %d", p->p_user.u_comm,
3700Sstevel@tonic-gate 		    p->p_pid, (void *) addr, crgetruid(p->p_cred));
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	delay(hz / 50);
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate /*
3770Sstevel@tonic-gate  * Determine whether the address range [addr, addr + len) is in memlist mp.
3780Sstevel@tonic-gate  */
3790Sstevel@tonic-gate int
address_in_memlist(struct memlist * mp,uint64_t addr,size_t len)3800Sstevel@tonic-gate address_in_memlist(struct memlist *mp, uint64_t addr, size_t len)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	while (mp != 0)	 {
383*11474SJonathan.Adams@Sun.COM 		if ((addr >= mp->ml_address) &&
384*11474SJonathan.Adams@Sun.COM 		    (addr + len <= mp->ml_address + mp->ml_size))
3850Sstevel@tonic-gate 			return (1);	 /* TRUE */
386*11474SJonathan.Adams@Sun.COM 		mp = mp->ml_next;
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 	return (0);	/* FALSE */
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate  * Pop the topmost element from the t_ontrap stack, removing the current set of
3930Sstevel@tonic-gate  * on_trap() protections.  Refer to <sys/ontrap.h> for more info.  If the
3940Sstevel@tonic-gate  * stack is already empty, no_trap() just returns.
3950Sstevel@tonic-gate  */
3960Sstevel@tonic-gate void
no_trap(void)3970Sstevel@tonic-gate no_trap(void)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	if (curthread->t_ontrap != NULL) {
4000Sstevel@tonic-gate #ifdef __sparc
4010Sstevel@tonic-gate 		membar_sync(); /* deferred error barrier (see sparcv9_subr.s) */
4020Sstevel@tonic-gate #endif
4030Sstevel@tonic-gate 		curthread->t_ontrap = curthread->t_ontrap->ot_prev;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate  * Return utsname.nodename outside a zone, or the zone name within.
4090Sstevel@tonic-gate  */
4100Sstevel@tonic-gate char *
uts_nodename(void)4110Sstevel@tonic-gate uts_nodename(void)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	if (curproc == NULL)
4140Sstevel@tonic-gate 		return (utsname.nodename);
4150Sstevel@tonic-gate 	return (curproc->p_zone->zone_nodename);
4160Sstevel@tonic-gate }
417