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