xref: /onnv-gate/usr/src/uts/common/os/subr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/sysmacros.h>
35*0Sstevel@tonic-gate #include <sys/param.h>
36*0Sstevel@tonic-gate #include <sys/vmparam.h>
37*0Sstevel@tonic-gate #include <sys/systm.h>
38*0Sstevel@tonic-gate #include <sys/cred.h>
39*0Sstevel@tonic-gate #include <sys/user.h>
40*0Sstevel@tonic-gate #include <sys/proc.h>
41*0Sstevel@tonic-gate #include <sys/conf.h>
42*0Sstevel@tonic-gate #include <sys/tuneable.h>
43*0Sstevel@tonic-gate #include <sys/cpuvar.h>
44*0Sstevel@tonic-gate #include <sys/archsystm.h>
45*0Sstevel@tonic-gate #include <sys/vmem.h>
46*0Sstevel@tonic-gate #include <vm/seg_kmem.h>
47*0Sstevel@tonic-gate #include <sys/errno.h>
48*0Sstevel@tonic-gate #include <sys/cmn_err.h>
49*0Sstevel@tonic-gate #include <sys/debug.h>
50*0Sstevel@tonic-gate #include <sys/atomic.h>
51*0Sstevel@tonic-gate #include <sys/model.h>
52*0Sstevel@tonic-gate #include <sys/kmem.h>
53*0Sstevel@tonic-gate #include <sys/memlist.h>
54*0Sstevel@tonic-gate #include <sys/autoconf.h>
55*0Sstevel@tonic-gate #include <sys/ontrap.h>
56*0Sstevel@tonic-gate #include <sys/utsname.h>
57*0Sstevel@tonic-gate #include <sys/zone.h>
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #ifdef __sparc
60*0Sstevel@tonic-gate #include <sys/membar.h>
61*0Sstevel@tonic-gate #endif
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate  * Routine which sets a user error; placed in
65*0Sstevel@tonic-gate  * illegal entries in the bdevsw and cdevsw tables.
66*0Sstevel@tonic-gate  */
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate int
69*0Sstevel@tonic-gate nodev()
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 	return (curthread->t_lwp ?
72*0Sstevel@tonic-gate 	    ttolwp(curthread)->lwp_error = ENXIO : ENXIO);
73*0Sstevel@tonic-gate }
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /*
76*0Sstevel@tonic-gate  * Null routine; placed in insignificant entries
77*0Sstevel@tonic-gate  * in the bdevsw and cdevsw tables.
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate int
81*0Sstevel@tonic-gate nulldev()
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	return (0);
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate static kmutex_t udevlock;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate  * Generate an unused major device number.
90*0Sstevel@tonic-gate  */
91*0Sstevel@tonic-gate major_t
92*0Sstevel@tonic-gate getudev()
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	static major_t next = 0;
95*0Sstevel@tonic-gate 	major_t ret;
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	/*
98*0Sstevel@tonic-gate 	 * Ensure that we start allocating major numbers above the 'devcnt'
99*0Sstevel@tonic-gate 	 * count.  The only limit we place on the number is that it should be a
100*0Sstevel@tonic-gate 	 * legal 32-bit SVR4 major number and be greater than or equal to devcnt
101*0Sstevel@tonic-gate 	 * in the current system).
102*0Sstevel@tonic-gate 	 */
103*0Sstevel@tonic-gate 	mutex_enter(&udevlock);
104*0Sstevel@tonic-gate 	if (next == 0)
105*0Sstevel@tonic-gate 		next = devcnt;
106*0Sstevel@tonic-gate 	if (next <= L_MAXMAJ32 && next >= devcnt)
107*0Sstevel@tonic-gate 		ret = next++;
108*0Sstevel@tonic-gate 	else {
109*0Sstevel@tonic-gate 		/*
110*0Sstevel@tonic-gate 		 * If we fail to allocate a major number because devcnt has
111*0Sstevel@tonic-gate 		 * reached L_MAXMAJ32, we may be the victim of a sparsely
112*0Sstevel@tonic-gate 		 * populated devnames array.  We scan the array backwards
113*0Sstevel@tonic-gate 		 * looking for an empty slot;  if we find one, mark it as
114*0Sstevel@tonic-gate 		 * DN_GETUDEV so it doesn't get taken by subsequent consumers
115*0Sstevel@tonic-gate 		 * users of the devnames array, and issue a warning.
116*0Sstevel@tonic-gate 		 * It is vital for this routine to take drastic measures to
117*0Sstevel@tonic-gate 		 * succeed, since the kernel really needs it to boot.
118*0Sstevel@tonic-gate 		 */
119*0Sstevel@tonic-gate 		int i;
120*0Sstevel@tonic-gate 		for (i = devcnt - 1; i >= 0; i--) {
121*0Sstevel@tonic-gate 			LOCK_DEV_OPS(&devnamesp[i].dn_lock);
122*0Sstevel@tonic-gate 			if (devnamesp[i].dn_name == NULL &&
123*0Sstevel@tonic-gate 			    ((devnamesp[i].dn_flags & DN_TAKEN_GETUDEV) == 0))
124*0Sstevel@tonic-gate 				break;
125*0Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&devnamesp[i].dn_lock);
126*0Sstevel@tonic-gate 		}
127*0Sstevel@tonic-gate 		if (i != -1) {
128*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "Reusing device major number %d.", i);
129*0Sstevel@tonic-gate 			ASSERT(i >= 0 && i < devcnt);
130*0Sstevel@tonic-gate 			devnamesp[i].dn_flags |= DN_TAKEN_GETUDEV;
131*0Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&devnamesp[i].dn_lock);
132*0Sstevel@tonic-gate 			ret = (major_t)i;
133*0Sstevel@tonic-gate 		} else {
134*0Sstevel@tonic-gate 			ret = (major_t)-1;
135*0Sstevel@tonic-gate 		}
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 	mutex_exit(&udevlock);
138*0Sstevel@tonic-gate 	return (ret);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate /*
143*0Sstevel@tonic-gate  * Compress 'long' device number encoding to 32-bit device number
144*0Sstevel@tonic-gate  * encoding.  If it won't fit, we return failure, but set the
145*0Sstevel@tonic-gate  * device number to 32-bit NODEV for the sake of our callers.
146*0Sstevel@tonic-gate  */
147*0Sstevel@tonic-gate int
148*0Sstevel@tonic-gate cmpldev(dev32_t *dst, dev_t dev)
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate #if defined(_LP64)
151*0Sstevel@tonic-gate 	if (dev == NODEV) {
152*0Sstevel@tonic-gate 		*dst = NODEV32;
153*0Sstevel@tonic-gate 	} else {
154*0Sstevel@tonic-gate 		major_t major = dev >> L_BITSMINOR;
155*0Sstevel@tonic-gate 		minor_t minor = dev & L_MAXMIN;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 		if (major > L_MAXMAJ32 || minor > L_MAXMIN32) {
158*0Sstevel@tonic-gate 			*dst = NODEV32;
159*0Sstevel@tonic-gate 			return (0);
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 		*dst = (dev32_t)((major << L_BITSMINOR32) | minor);
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate #else
165*0Sstevel@tonic-gate 	*dst = (dev32_t)dev;
166*0Sstevel@tonic-gate #endif
167*0Sstevel@tonic-gate 	return (1);
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate /*
171*0Sstevel@tonic-gate  * Expand 32-bit dev_t's to long dev_t's.  Expansion always "fits"
172*0Sstevel@tonic-gate  * into the return type, but we're careful to expand NODEV explicitly.
173*0Sstevel@tonic-gate  */
174*0Sstevel@tonic-gate dev_t
175*0Sstevel@tonic-gate expldev(dev32_t dev32)
176*0Sstevel@tonic-gate {
177*0Sstevel@tonic-gate #ifdef _LP64
178*0Sstevel@tonic-gate 	if (dev32 == NODEV32)
179*0Sstevel@tonic-gate 		return (NODEV);
180*0Sstevel@tonic-gate 	return (makedevice((dev32 >> L_BITSMINOR32) & L_MAXMAJ32,
181*0Sstevel@tonic-gate 	    dev32 & L_MAXMIN32));
182*0Sstevel@tonic-gate #else
183*0Sstevel@tonic-gate 	return ((dev_t)dev32);
184*0Sstevel@tonic-gate #endif
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate #ifndef _LP64
188*0Sstevel@tonic-gate /*
189*0Sstevel@tonic-gate  * Keep these entry points for 32-bit systems but enforce the use
190*0Sstevel@tonic-gate  * of MIN/MAX macros on 64-bit systems.  The DDI header files already
191*0Sstevel@tonic-gate  * define min/max as macros so drivers shouldn't need these functions.
192*0Sstevel@tonic-gate  */
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate int
195*0Sstevel@tonic-gate min(int a, int b)
196*0Sstevel@tonic-gate {
197*0Sstevel@tonic-gate 	return (a < b ? a : b);
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate int
201*0Sstevel@tonic-gate max(int a, int b)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	return (a > b ? a : b);
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate uint_t
207*0Sstevel@tonic-gate umin(uint_t a, uint_t b)
208*0Sstevel@tonic-gate {
209*0Sstevel@tonic-gate 	return (a < b ? a : b);
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate uint_t
213*0Sstevel@tonic-gate umax(uint_t a, uint_t b)
214*0Sstevel@tonic-gate {
215*0Sstevel@tonic-gate 	return (a > b ? a : b);
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate #endif /* !_LP64 */
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate /*
221*0Sstevel@tonic-gate  * Return bit position of least significant bit set in mask,
222*0Sstevel@tonic-gate  * starting numbering from 1.
223*0Sstevel@tonic-gate  */
224*0Sstevel@tonic-gate int
225*0Sstevel@tonic-gate ffs(long mask)
226*0Sstevel@tonic-gate {
227*0Sstevel@tonic-gate 	int i;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	if (mask == 0)
230*0Sstevel@tonic-gate 		return (0);
231*0Sstevel@tonic-gate 	for (i = 1; i <= NBBY * sizeof (mask); i++) {
232*0Sstevel@tonic-gate 		if (mask & 1)
233*0Sstevel@tonic-gate 			return (i);
234*0Sstevel@tonic-gate 		mask >>= 1;
235*0Sstevel@tonic-gate 	}
236*0Sstevel@tonic-gate 	return (0);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate /*
240*0Sstevel@tonic-gate  * Parse suboptions from a string.
241*0Sstevel@tonic-gate  * Same as getsubopt(3C).
242*0Sstevel@tonic-gate  */
243*0Sstevel@tonic-gate int
244*0Sstevel@tonic-gate getsubopt(char **optionsp, char * const *tokens, char **valuep)
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate 	char *s = *optionsp, *p;
247*0Sstevel@tonic-gate 	int i;
248*0Sstevel@tonic-gate 	size_t optlen;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	*valuep = NULL;
251*0Sstevel@tonic-gate 	if (*s == '\0')
252*0Sstevel@tonic-gate 		return (-1);
253*0Sstevel@tonic-gate 	p = strchr(s, ',');		/* find next option */
254*0Sstevel@tonic-gate 	if (p == NULL) {
255*0Sstevel@tonic-gate 		p = s + strlen(s);
256*0Sstevel@tonic-gate 	} else {
257*0Sstevel@tonic-gate 		*p++ = '\0';		/* mark end and point to next */
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 	*optionsp = p;			/* point to next option */
260*0Sstevel@tonic-gate 	p = strchr(s, '=');		/* find value */
261*0Sstevel@tonic-gate 	if (p == NULL) {
262*0Sstevel@tonic-gate 		optlen = strlen(s);
263*0Sstevel@tonic-gate 		*valuep = NULL;
264*0Sstevel@tonic-gate 	} else {
265*0Sstevel@tonic-gate 		optlen = p - s;
266*0Sstevel@tonic-gate 		*valuep = ++p;
267*0Sstevel@tonic-gate 	}
268*0Sstevel@tonic-gate 	for (i = 0; tokens[i] != NULL; i++) {
269*0Sstevel@tonic-gate 		if ((optlen == strlen(tokens[i])) &&
270*0Sstevel@tonic-gate 		    (strncmp(s, tokens[i], optlen) == 0))
271*0Sstevel@tonic-gate 			return (i);
272*0Sstevel@tonic-gate 	}
273*0Sstevel@tonic-gate 	/* no match, point value at option and return error */
274*0Sstevel@tonic-gate 	*valuep = s;
275*0Sstevel@tonic-gate 	return (-1);
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate /*
279*0Sstevel@tonic-gate  * Append the suboption string 'opt' starting at the position 'str'
280*0Sstevel@tonic-gate  * within the buffer defined by 'buf' and 'len'. If 'buf' is not null,
281*0Sstevel@tonic-gate  * a comma is appended first.
282*0Sstevel@tonic-gate  * Return a pointer to the end of the resulting string (the null byte).
283*0Sstevel@tonic-gate  * Return NULL if there isn't enough space left to append 'opt'.
284*0Sstevel@tonic-gate  */
285*0Sstevel@tonic-gate char *
286*0Sstevel@tonic-gate append_subopt(const char *buf, size_t len, char *str, const char *opt)
287*0Sstevel@tonic-gate {
288*0Sstevel@tonic-gate 	size_t l = strlen(opt);
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	/*
291*0Sstevel@tonic-gate 	 * Include a ',' if this is not the first option.
292*0Sstevel@tonic-gate 	 * Include space for the null byte.
293*0Sstevel@tonic-gate 	 */
294*0Sstevel@tonic-gate 	if (strlen(buf) + (buf[0] != '\0') + l + 1 > len)
295*0Sstevel@tonic-gate 		return (NULL);
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	if (buf[0] != '\0')
298*0Sstevel@tonic-gate 		*str++ = ',';
299*0Sstevel@tonic-gate 	(void) strcpy(str, opt);
300*0Sstevel@tonic-gate 	return (str + l);
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate /*
304*0Sstevel@tonic-gate  * Tables to convert a single byte to/from binary-coded decimal (BCD).
305*0Sstevel@tonic-gate  */
306*0Sstevel@tonic-gate uchar_t byte_to_bcd[256] = {
307*0Sstevel@tonic-gate 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
308*0Sstevel@tonic-gate 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
309*0Sstevel@tonic-gate 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
310*0Sstevel@tonic-gate 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
311*0Sstevel@tonic-gate 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
312*0Sstevel@tonic-gate 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
313*0Sstevel@tonic-gate 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
314*0Sstevel@tonic-gate 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
315*0Sstevel@tonic-gate 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
316*0Sstevel@tonic-gate 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
317*0Sstevel@tonic-gate };
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate uchar_t bcd_to_byte[256] = {		/* CSTYLED */
320*0Sstevel@tonic-gate 	 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  0,  0,  0,  0,  0,  0,
321*0Sstevel@tonic-gate 	10, 11, 12, 13, 14, 15, 16, 17, 18, 19,  0,  0,  0,  0,  0,  0,
322*0Sstevel@tonic-gate 	20, 21, 22, 23, 24, 25, 26, 27, 28, 29,  0,  0,  0,  0,  0,  0,
323*0Sstevel@tonic-gate 	30, 31, 32, 33, 34, 35, 36, 37, 38, 39,  0,  0,  0,  0,  0,  0,
324*0Sstevel@tonic-gate 	40, 41, 42, 43, 44, 45, 46, 47, 48, 49,  0,  0,  0,  0,  0,  0,
325*0Sstevel@tonic-gate 	50, 51, 52, 53, 54, 55, 56, 57, 58, 59,  0,  0,  0,  0,  0,  0,
326*0Sstevel@tonic-gate 	60, 61, 62, 63, 64, 65, 66, 67, 68, 69,  0,  0,  0,  0,  0,  0,
327*0Sstevel@tonic-gate 	70, 71, 72, 73, 74, 75, 76, 77, 78, 79,  0,  0,  0,  0,  0,  0,
328*0Sstevel@tonic-gate 	80, 81, 82, 83, 84, 85, 86, 87, 88, 89,  0,  0,  0,  0,  0,  0,
329*0Sstevel@tonic-gate 	90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
330*0Sstevel@tonic-gate };
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate /*
333*0Sstevel@tonic-gate  * Hot-patch a single instruction in the kernel's text.
334*0Sstevel@tonic-gate  * If you want to patch multiple instructions you must
335*0Sstevel@tonic-gate  * arrange to do it so that all intermediate stages are
336*0Sstevel@tonic-gate  * sane -- we don't stop other cpus while doing this.
337*0Sstevel@tonic-gate  * Size must be 1, 2, or 4 bytes with iaddr aligned accordingly.
338*0Sstevel@tonic-gate  */
339*0Sstevel@tonic-gate void
340*0Sstevel@tonic-gate hot_patch_kernel_text(caddr_t iaddr, uint32_t new_instr, uint_t size)
341*0Sstevel@tonic-gate {
342*0Sstevel@tonic-gate 	caddr_t vaddr;
343*0Sstevel@tonic-gate 	page_t **ppp;
344*0Sstevel@tonic-gate 	uintptr_t off = (uintptr_t)iaddr & PAGEOFFSET;
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	vaddr = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	(void) as_pagelock(&kas, &ppp, iaddr - off, PAGESIZE, S_WRITE);
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	hat_devload(kas.a_hat, vaddr, PAGESIZE,
351*0Sstevel@tonic-gate 	    hat_getpfnum(kas.a_hat, iaddr - off),
352*0Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE, HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	switch (size) {
355*0Sstevel@tonic-gate 	case 1:
356*0Sstevel@tonic-gate 		*(uint8_t *)(vaddr + off) = new_instr;
357*0Sstevel@tonic-gate 		break;
358*0Sstevel@tonic-gate 	case 2:
359*0Sstevel@tonic-gate 		*(uint16_t *)(vaddr + off) = new_instr;
360*0Sstevel@tonic-gate 		break;
361*0Sstevel@tonic-gate 	case 4:
362*0Sstevel@tonic-gate 		*(uint32_t *)(vaddr + off) = new_instr;
363*0Sstevel@tonic-gate 		break;
364*0Sstevel@tonic-gate 	default:
365*0Sstevel@tonic-gate 		panic("illegal hot-patch");
366*0Sstevel@tonic-gate 	}
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	membar_enter();
369*0Sstevel@tonic-gate 	sync_icache(vaddr + off, size);
370*0Sstevel@tonic-gate 	sync_icache(iaddr, size);
371*0Sstevel@tonic-gate 	as_pageunlock(&kas, ppp, iaddr - off, PAGESIZE, S_WRITE);
372*0Sstevel@tonic-gate 	hat_unload(kas.a_hat, vaddr, PAGESIZE, HAT_UNLOAD_UNLOCK);
373*0Sstevel@tonic-gate 	vmem_free(heap_arena, vaddr, PAGESIZE);
374*0Sstevel@tonic-gate }
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate /*
377*0Sstevel@tonic-gate  * Routine to report an attempt to execute non-executable data.  If the
378*0Sstevel@tonic-gate  * address executed lies in the stack, explicitly say so.
379*0Sstevel@tonic-gate  */
380*0Sstevel@tonic-gate void
381*0Sstevel@tonic-gate report_stack_exec(proc_t *p, caddr_t addr)
382*0Sstevel@tonic-gate {
383*0Sstevel@tonic-gate 	if (!noexec_user_stack_log)
384*0Sstevel@tonic-gate 		return;
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	if (addr < p->p_usrstack && addr >= (p->p_usrstack - p->p_stksize)) {
387*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s[%d] attempt to execute code "
388*0Sstevel@tonic-gate 		    "on stack by uid %d", p->p_user.u_comm,
389*0Sstevel@tonic-gate 		    p->p_pid, crgetruid(p->p_cred));
390*0Sstevel@tonic-gate 	} else {
391*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s[%d] attempt to execute non-executable "
392*0Sstevel@tonic-gate 		    "data at 0x%p by uid %d", p->p_user.u_comm,
393*0Sstevel@tonic-gate 		    p->p_pid, (void *) addr, crgetruid(p->p_cred));
394*0Sstevel@tonic-gate 	}
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	delay(hz / 50);
397*0Sstevel@tonic-gate }
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate /*
400*0Sstevel@tonic-gate  * Determine whether the address range [addr, addr + len) is in memlist mp.
401*0Sstevel@tonic-gate  */
402*0Sstevel@tonic-gate int
403*0Sstevel@tonic-gate address_in_memlist(struct memlist *mp, uint64_t addr, size_t len)
404*0Sstevel@tonic-gate {
405*0Sstevel@tonic-gate 	while (mp != 0)	 {
406*0Sstevel@tonic-gate 		if ((addr >= mp->address) &&
407*0Sstevel@tonic-gate 		    (addr + len <= mp->address + mp->size))
408*0Sstevel@tonic-gate 			return (1);	 /* TRUE */
409*0Sstevel@tonic-gate 		mp = mp->next;
410*0Sstevel@tonic-gate 	}
411*0Sstevel@tonic-gate 	return (0);	/* FALSE */
412*0Sstevel@tonic-gate }
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate /*
415*0Sstevel@tonic-gate  * Pop the topmost element from the t_ontrap stack, removing the current set of
416*0Sstevel@tonic-gate  * on_trap() protections.  Refer to <sys/ontrap.h> for more info.  If the
417*0Sstevel@tonic-gate  * stack is already empty, no_trap() just returns.
418*0Sstevel@tonic-gate  */
419*0Sstevel@tonic-gate void
420*0Sstevel@tonic-gate no_trap(void)
421*0Sstevel@tonic-gate {
422*0Sstevel@tonic-gate 	if (curthread->t_ontrap != NULL) {
423*0Sstevel@tonic-gate #ifdef __sparc
424*0Sstevel@tonic-gate 		membar_sync(); /* deferred error barrier (see sparcv9_subr.s) */
425*0Sstevel@tonic-gate #endif
426*0Sstevel@tonic-gate 		curthread->t_ontrap = curthread->t_ontrap->ot_prev;
427*0Sstevel@tonic-gate 	}
428*0Sstevel@tonic-gate }
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate /*
431*0Sstevel@tonic-gate  * Return utsname.nodename outside a zone, or the zone name within.
432*0Sstevel@tonic-gate  */
433*0Sstevel@tonic-gate char *
434*0Sstevel@tonic-gate uts_nodename(void)
435*0Sstevel@tonic-gate {
436*0Sstevel@tonic-gate 	if (curproc == NULL)
437*0Sstevel@tonic-gate 		return (utsname.nodename);
438*0Sstevel@tonic-gate 	return (curproc->p_zone->zone_nodename);
439*0Sstevel@tonic-gate }
440