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