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