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) 1990, 1991 UNIX System Laboratories, Inc. */ 28*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 29*0Sstevel@tonic-gate /* All Rights Reserved */ 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/param.h> 35*0Sstevel@tonic-gate #include <sys/sysmacros.h> 36*0Sstevel@tonic-gate #include <sys/signal.h> 37*0Sstevel@tonic-gate #include <sys/systm.h> 38*0Sstevel@tonic-gate #include <sys/user.h> 39*0Sstevel@tonic-gate #include <sys/mman.h> 40*0Sstevel@tonic-gate #include <sys/class.h> 41*0Sstevel@tonic-gate #include <sys/proc.h> 42*0Sstevel@tonic-gate #include <sys/procfs.h> 43*0Sstevel@tonic-gate #include <sys/buf.h> 44*0Sstevel@tonic-gate #include <sys/kmem.h> 45*0Sstevel@tonic-gate #include <sys/cred.h> 46*0Sstevel@tonic-gate #include <sys/archsystm.h> 47*0Sstevel@tonic-gate #include <sys/vmparam.h> 48*0Sstevel@tonic-gate #include <sys/prsystm.h> 49*0Sstevel@tonic-gate #include <sys/reboot.h> 50*0Sstevel@tonic-gate #include <sys/uadmin.h> 51*0Sstevel@tonic-gate #include <sys/vfs.h> 52*0Sstevel@tonic-gate #include <sys/vnode.h> 53*0Sstevel@tonic-gate #include <sys/file.h> 54*0Sstevel@tonic-gate #include <sys/session.h> 55*0Sstevel@tonic-gate #include <sys/ucontext.h> 56*0Sstevel@tonic-gate #include <sys/dnlc.h> 57*0Sstevel@tonic-gate #include <sys/var.h> 58*0Sstevel@tonic-gate #include <sys/cmn_err.h> 59*0Sstevel@tonic-gate #include <sys/debugreg.h> 60*0Sstevel@tonic-gate #include <sys/thread.h> 61*0Sstevel@tonic-gate #include <sys/vtrace.h> 62*0Sstevel@tonic-gate #include <sys/consdev.h> 63*0Sstevel@tonic-gate #include <sys/psw.h> 64*0Sstevel@tonic-gate #include <sys/regset.h> 65*0Sstevel@tonic-gate #include <sys/privregs.h> 66*0Sstevel@tonic-gate #include <sys/stack.h> 67*0Sstevel@tonic-gate #include <sys/swap.h> 68*0Sstevel@tonic-gate #include <vm/hat.h> 69*0Sstevel@tonic-gate #include <vm/anon.h> 70*0Sstevel@tonic-gate #include <vm/as.h> 71*0Sstevel@tonic-gate #include <vm/page.h> 72*0Sstevel@tonic-gate #include <vm/seg.h> 73*0Sstevel@tonic-gate #include <vm/seg_kmem.h> 74*0Sstevel@tonic-gate #include <vm/seg_map.h> 75*0Sstevel@tonic-gate #include <vm/seg_vn.h> 76*0Sstevel@tonic-gate #include <sys/exec.h> 77*0Sstevel@tonic-gate #include <sys/acct.h> 78*0Sstevel@tonic-gate #include <sys/core.h> 79*0Sstevel@tonic-gate #include <sys/corectl.h> 80*0Sstevel@tonic-gate #include <sys/modctl.h> 81*0Sstevel@tonic-gate #include <sys/tuneable.h> 82*0Sstevel@tonic-gate #include <c2/audit.h> 83*0Sstevel@tonic-gate #include <sys/bootconf.h> 84*0Sstevel@tonic-gate #include <sys/dumphdr.h> 85*0Sstevel@tonic-gate #include <sys/promif.h> 86*0Sstevel@tonic-gate #include <sys/systeminfo.h> 87*0Sstevel@tonic-gate #include <sys/kdi.h> 88*0Sstevel@tonic-gate #include <sys/contract_impl.h> 89*0Sstevel@tonic-gate #include <sys/x86_archext.h> 90*0Sstevel@tonic-gate #include <sys/segments.h> 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate /* 93*0Sstevel@tonic-gate * Compare the version of boot that boot says it is against 94*0Sstevel@tonic-gate * the version of boot the kernel expects. 95*0Sstevel@tonic-gate */ 96*0Sstevel@tonic-gate int 97*0Sstevel@tonic-gate check_boot_version(int boots_version) 98*0Sstevel@tonic-gate { 99*0Sstevel@tonic-gate if (boots_version == BO_VERSION) 100*0Sstevel@tonic-gate return (0); 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate prom_printf("Wrong boot interface - kernel needs v%d found v%d\n", 103*0Sstevel@tonic-gate BO_VERSION, boots_version); 104*0Sstevel@tonic-gate prom_panic("halting"); 105*0Sstevel@tonic-gate /*NOTREACHED*/ 106*0Sstevel@tonic-gate } 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate /* 109*0Sstevel@tonic-gate * Process the physical installed list for boot. 110*0Sstevel@tonic-gate * Finds: 111*0Sstevel@tonic-gate * 1) the pfn of the highest installed physical page, 112*0Sstevel@tonic-gate * 2) the number of pages installed 113*0Sstevel@tonic-gate * 3) the number of distinct contiguous regions these pages fall into. 114*0Sstevel@tonic-gate */ 115*0Sstevel@tonic-gate void 116*0Sstevel@tonic-gate installed_top_size( 117*0Sstevel@tonic-gate struct memlist *list, /* pointer to start of installed list */ 118*0Sstevel@tonic-gate pfn_t *high_pfn, /* return ptr for top value */ 119*0Sstevel@tonic-gate pgcnt_t *pgcnt, /* return ptr for sum of installed pages */ 120*0Sstevel@tonic-gate int *ranges) /* return ptr for the count of contig. ranges */ 121*0Sstevel@tonic-gate { 122*0Sstevel@tonic-gate pfn_t top = 0; 123*0Sstevel@tonic-gate pgcnt_t sumpages = 0; 124*0Sstevel@tonic-gate pfn_t highp; /* high page in a chunk */ 125*0Sstevel@tonic-gate int cnt = 0; 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate for (; list; list = list->next) { 128*0Sstevel@tonic-gate ++cnt; 129*0Sstevel@tonic-gate highp = (list->address + list->size - 1) >> PAGESHIFT; 130*0Sstevel@tonic-gate if (top < highp) 131*0Sstevel@tonic-gate top = highp; 132*0Sstevel@tonic-gate sumpages += btop(list->size); 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate *high_pfn = top; 136*0Sstevel@tonic-gate *pgcnt = sumpages; 137*0Sstevel@tonic-gate *ranges = cnt; 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate /* 141*0Sstevel@tonic-gate * Copy in a memory list from boot to kernel, with a filter function 142*0Sstevel@tonic-gate * to remove pages. The filter function can increase the address and/or 143*0Sstevel@tonic-gate * decrease the size to filter out pages. 144*0Sstevel@tonic-gate */ 145*0Sstevel@tonic-gate void 146*0Sstevel@tonic-gate copy_memlist_filter( 147*0Sstevel@tonic-gate struct memlist *src, 148*0Sstevel@tonic-gate struct memlist **dstp, 149*0Sstevel@tonic-gate void (*filter)(uint64_t *, uint64_t *)) 150*0Sstevel@tonic-gate { 151*0Sstevel@tonic-gate struct memlist *dst, *prev; 152*0Sstevel@tonic-gate uint64_t addr; 153*0Sstevel@tonic-gate uint64_t size; 154*0Sstevel@tonic-gate uint64_t eaddr; 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate dst = *dstp; 157*0Sstevel@tonic-gate prev = dst; 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate /* 160*0Sstevel@tonic-gate * Move through the memlist applying a filter against 161*0Sstevel@tonic-gate * each range of memory. Note that we may apply the 162*0Sstevel@tonic-gate * filter multiple times against each memlist entry. 163*0Sstevel@tonic-gate */ 164*0Sstevel@tonic-gate for (; src; src = src->next) { 165*0Sstevel@tonic-gate addr = src->address; 166*0Sstevel@tonic-gate eaddr = addr + src->size; 167*0Sstevel@tonic-gate while (addr < eaddr) { 168*0Sstevel@tonic-gate size = eaddr - addr; 169*0Sstevel@tonic-gate if (filter != NULL) 170*0Sstevel@tonic-gate filter(&addr, &size); 171*0Sstevel@tonic-gate if (size == 0) 172*0Sstevel@tonic-gate break; 173*0Sstevel@tonic-gate dst->address = addr; 174*0Sstevel@tonic-gate dst->size = size; 175*0Sstevel@tonic-gate dst->next = 0; 176*0Sstevel@tonic-gate if (prev == dst) { 177*0Sstevel@tonic-gate dst->prev = 0; 178*0Sstevel@tonic-gate dst++; 179*0Sstevel@tonic-gate } else { 180*0Sstevel@tonic-gate dst->prev = prev; 181*0Sstevel@tonic-gate prev->next = dst; 182*0Sstevel@tonic-gate dst++; 183*0Sstevel@tonic-gate prev++; 184*0Sstevel@tonic-gate } 185*0Sstevel@tonic-gate addr += size; 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate } 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate *dstp = dst; 190*0Sstevel@tonic-gate } 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate /* 193*0Sstevel@tonic-gate * Kernel setup code, called from startup(). 194*0Sstevel@tonic-gate */ 195*0Sstevel@tonic-gate void 196*0Sstevel@tonic-gate kern_setup1(void) 197*0Sstevel@tonic-gate { 198*0Sstevel@tonic-gate proc_t *pp; 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate pp = &p0; 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate proc_sched = pp; 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate /* 205*0Sstevel@tonic-gate * Initialize process 0 data structures 206*0Sstevel@tonic-gate */ 207*0Sstevel@tonic-gate pp->p_stat = SRUN; 208*0Sstevel@tonic-gate pp->p_flag = SSYS; 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate pp->p_pidp = &pid0; 211*0Sstevel@tonic-gate pp->p_pgidp = &pid0; 212*0Sstevel@tonic-gate pp->p_sessp = &session0; 213*0Sstevel@tonic-gate pp->p_tlist = &t0; 214*0Sstevel@tonic-gate pid0.pid_pglink = pp; 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate /* 217*0Sstevel@tonic-gate * XXX - we asssume that the u-area is zeroed out except for 218*0Sstevel@tonic-gate * ttolwp(curthread)->lwp_regs. 219*0Sstevel@tonic-gate */ 220*0Sstevel@tonic-gate u.u_cmask = (mode_t)CMASK; 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate thread_init(); /* init thread_free list */ 223*0Sstevel@tonic-gate pid_init(); /* initialize pid (proc) table */ 224*0Sstevel@tonic-gate contract_init(); /* initialize contracts */ 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate init_pages_pp_maximum(); 227*0Sstevel@tonic-gate } 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate /* 230*0Sstevel@tonic-gate * Load a procedure into a thread. 231*0Sstevel@tonic-gate */ 232*0Sstevel@tonic-gate void 233*0Sstevel@tonic-gate thread_load(kthread_t *t, void (*start)(), caddr_t arg, size_t len) 234*0Sstevel@tonic-gate { 235*0Sstevel@tonic-gate caddr_t sp; 236*0Sstevel@tonic-gate size_t framesz; 237*0Sstevel@tonic-gate caddr_t argp; 238*0Sstevel@tonic-gate long *p; 239*0Sstevel@tonic-gate extern void thread_start(); 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate /* 242*0Sstevel@tonic-gate * Push a "c" call frame onto the stack to represent 243*0Sstevel@tonic-gate * the caller of "start". 244*0Sstevel@tonic-gate */ 245*0Sstevel@tonic-gate sp = t->t_stk; 246*0Sstevel@tonic-gate ASSERT(((uintptr_t)t->t_stk & (STACK_ENTRY_ALIGN - 1)) == 0); 247*0Sstevel@tonic-gate if (len != 0) { 248*0Sstevel@tonic-gate /* 249*0Sstevel@tonic-gate * the object that arg points at is copied into the 250*0Sstevel@tonic-gate * caller's frame. 251*0Sstevel@tonic-gate */ 252*0Sstevel@tonic-gate framesz = SA(len); 253*0Sstevel@tonic-gate sp -= framesz; 254*0Sstevel@tonic-gate ASSERT(sp > t->t_stkbase); 255*0Sstevel@tonic-gate argp = sp + SA(MINFRAME); 256*0Sstevel@tonic-gate bcopy(arg, argp, len); 257*0Sstevel@tonic-gate arg = argp; 258*0Sstevel@tonic-gate } 259*0Sstevel@tonic-gate /* 260*0Sstevel@tonic-gate * Set up arguments (arg and len) on the caller's stack frame. 261*0Sstevel@tonic-gate */ 262*0Sstevel@tonic-gate p = (long *)sp; 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate *--p = 0; /* fake call */ 265*0Sstevel@tonic-gate *--p = 0; /* null frame pointer terminates stack trace */ 266*0Sstevel@tonic-gate *--p = (long)len; 267*0Sstevel@tonic-gate *--p = (intptr_t)arg; 268*0Sstevel@tonic-gate *--p = (intptr_t)start; 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gate /* 271*0Sstevel@tonic-gate * initialize thread to resume at thread_start() which will 272*0Sstevel@tonic-gate * turn around and invoke (*start)(arg, len). 273*0Sstevel@tonic-gate */ 274*0Sstevel@tonic-gate t->t_pc = (uintptr_t)thread_start; 275*0Sstevel@tonic-gate t->t_sp = (uintptr_t)p; 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate ASSERT((t->t_sp & (STACK_ENTRY_ALIGN - 1)) == 0); 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate /* 281*0Sstevel@tonic-gate * load user registers into lwp. 282*0Sstevel@tonic-gate */ 283*0Sstevel@tonic-gate /*ARGSUSED2*/ 284*0Sstevel@tonic-gate void 285*0Sstevel@tonic-gate lwp_load(klwp_t *lwp, gregset_t grp, uintptr_t thrptr) 286*0Sstevel@tonic-gate { 287*0Sstevel@tonic-gate struct regs *rp = lwptoregs(lwp); 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate setgregs(lwp, grp); 290*0Sstevel@tonic-gate rp->r_ps = PSL_USER; 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate /* 293*0Sstevel@tonic-gate * For 64-bit lwps, we allow one magic %fs selector value, and one 294*0Sstevel@tonic-gate * magic %gs selector to point anywhere in the address space using 295*0Sstevel@tonic-gate * %fsbase and %gsbase behind the scenes. libc uses %fs to point 296*0Sstevel@tonic-gate * at the ulwp_t structure. 297*0Sstevel@tonic-gate * 298*0Sstevel@tonic-gate * For 32-bit lwps, libc wedges its lwp thread pointer into the 299*0Sstevel@tonic-gate * ucontext ESP slot (which is otherwise irrelevant to setting a 300*0Sstevel@tonic-gate * ucontext) and LWPGS_SEL value into gregs[REG_GS]. This is so 301*0Sstevel@tonic-gate * syslwp_create() can atomically setup %gs. 302*0Sstevel@tonic-gate * 303*0Sstevel@tonic-gate * See setup_context() in libc. 304*0Sstevel@tonic-gate */ 305*0Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 306*0Sstevel@tonic-gate if (lwp_getdatamodel(lwp) == DATAMODEL_ILP32) { 307*0Sstevel@tonic-gate if (grp[REG_GS] == LWPGS_SEL) 308*0Sstevel@tonic-gate (void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr); 309*0Sstevel@tonic-gate } 310*0Sstevel@tonic-gate #else 311*0Sstevel@tonic-gate if (grp[GS] == LWPGS_SEL) 312*0Sstevel@tonic-gate (void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr); 313*0Sstevel@tonic-gate #endif 314*0Sstevel@tonic-gate 315*0Sstevel@tonic-gate lwp->lwp_eosys = JUSTRETURN; 316*0Sstevel@tonic-gate lwptot(lwp)->t_post_sys = 1; 317*0Sstevel@tonic-gate } 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate /* 320*0Sstevel@tonic-gate * set syscall()'s return values for a lwp. 321*0Sstevel@tonic-gate */ 322*0Sstevel@tonic-gate void 323*0Sstevel@tonic-gate lwp_setrval(klwp_t *lwp, int v1, int v2) 324*0Sstevel@tonic-gate { 325*0Sstevel@tonic-gate lwptoregs(lwp)->r_ps &= ~PS_C; 326*0Sstevel@tonic-gate lwptoregs(lwp)->r_r0 = v1; 327*0Sstevel@tonic-gate lwptoregs(lwp)->r_r1 = v2; 328*0Sstevel@tonic-gate } 329*0Sstevel@tonic-gate 330*0Sstevel@tonic-gate /* 331*0Sstevel@tonic-gate * set syscall()'s return values for a lwp. 332*0Sstevel@tonic-gate */ 333*0Sstevel@tonic-gate void 334*0Sstevel@tonic-gate lwp_setsp(klwp_t *lwp, caddr_t sp) 335*0Sstevel@tonic-gate { 336*0Sstevel@tonic-gate lwptoregs(lwp)->r_sp = (intptr_t)sp; 337*0Sstevel@tonic-gate } 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate /* 340*0Sstevel@tonic-gate * Copy regs from parent to child. 341*0Sstevel@tonic-gate */ 342*0Sstevel@tonic-gate void 343*0Sstevel@tonic-gate lwp_forkregs(klwp_t *lwp, klwp_t *clwp) 344*0Sstevel@tonic-gate { 345*0Sstevel@tonic-gate #if defined(__amd64) 346*0Sstevel@tonic-gate clwp->lwp_pcb.pcb_flags |= RUPDATE_PENDING; 347*0Sstevel@tonic-gate lwptot(clwp)->t_post_sys = 1; 348*0Sstevel@tonic-gate #endif 349*0Sstevel@tonic-gate bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct regs)); 350*0Sstevel@tonic-gate } 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate /* 353*0Sstevel@tonic-gate * This function is currently unused on x86. 354*0Sstevel@tonic-gate */ 355*0Sstevel@tonic-gate /*ARGSUSED*/ 356*0Sstevel@tonic-gate void 357*0Sstevel@tonic-gate lwp_freeregs(klwp_t *lwp, int isexec) 358*0Sstevel@tonic-gate {} 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate /* 361*0Sstevel@tonic-gate * This function is currently unused on x86. 362*0Sstevel@tonic-gate */ 363*0Sstevel@tonic-gate void 364*0Sstevel@tonic-gate lwp_pcb_exit(void) 365*0Sstevel@tonic-gate {} 366*0Sstevel@tonic-gate 367*0Sstevel@tonic-gate /* 368*0Sstevel@tonic-gate * Lwp context ops for segment registers. 369*0Sstevel@tonic-gate */ 370*0Sstevel@tonic-gate 371*0Sstevel@tonic-gate /* 372*0Sstevel@tonic-gate * Every time we come into the kernel (syscall, interrupt or trap 373*0Sstevel@tonic-gate * but not fast-traps) we capture the current values of the user's 374*0Sstevel@tonic-gate * segment registers into the lwp's reg structure. This includes 375*0Sstevel@tonic-gate * lcall for i386 generic system call support since it is handled 376*0Sstevel@tonic-gate * as a segment-not-present trap. 377*0Sstevel@tonic-gate * 378*0Sstevel@tonic-gate * Here we save the current values from the lwp regs into the pcb 379*0Sstevel@tonic-gate * and set the RUPDATE_PENDING bit to tell the rest of the kernel 380*0Sstevel@tonic-gate * that the pcb copy of the segment registers is the current one. 381*0Sstevel@tonic-gate * This ensures the lwp's next trip to user land via update_sregs. 382*0Sstevel@tonic-gate * Finally we set t_post_sys to ensure that no system call fast-path's 383*0Sstevel@tonic-gate * its way out of the kernel via sysret. 384*0Sstevel@tonic-gate * 385*0Sstevel@tonic-gate * (This means that we need to have interrupts disabled when we test 386*0Sstevel@tonic-gate * t->t_post_sys in the syscall handlers; if the test fails, we need 387*0Sstevel@tonic-gate * to keep interrupts disabled until we return to userland so we can't 388*0Sstevel@tonic-gate * be switched away.) 389*0Sstevel@tonic-gate * 390*0Sstevel@tonic-gate * As a result of all this, we don't really have to do a whole lot if 391*0Sstevel@tonic-gate * the thread is just mucking about in the kernel, switching on and 392*0Sstevel@tonic-gate * off the cpu for whatever reason it feels like. And yet we still 393*0Sstevel@tonic-gate * preserve fast syscalls, cause if we -don't- get descheduled, 394*0Sstevel@tonic-gate * we never come here either. 395*0Sstevel@tonic-gate */ 396*0Sstevel@tonic-gate 397*0Sstevel@tonic-gate #define VALID_LWP_DESC(udp) ((udp)->usd_type == SDT_MEMRWA && \ 398*0Sstevel@tonic-gate (udp)->usd_p == 1 && (udp)->usd_dpl == SEL_UPL) 399*0Sstevel@tonic-gate 400*0Sstevel@tonic-gate void 401*0Sstevel@tonic-gate lwp_segregs_save(klwp_t *lwp) 402*0Sstevel@tonic-gate { 403*0Sstevel@tonic-gate #if defined(__amd64) 404*0Sstevel@tonic-gate pcb_t *pcb = &lwp->lwp_pcb; 405*0Sstevel@tonic-gate struct regs *rp; 406*0Sstevel@tonic-gate 407*0Sstevel@tonic-gate ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc)); 408*0Sstevel@tonic-gate ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc)); 409*0Sstevel@tonic-gate 410*0Sstevel@tonic-gate if ((pcb->pcb_flags & RUPDATE_PENDING) == 0) { 411*0Sstevel@tonic-gate rp = lwptoregs(lwp); 412*0Sstevel@tonic-gate 413*0Sstevel@tonic-gate /* 414*0Sstevel@tonic-gate * If there's no update already pending, capture the current 415*0Sstevel@tonic-gate * %ds/%es/%fs/%gs values from lwp's regs in case the user 416*0Sstevel@tonic-gate * changed them; %fsbase and %gsbase are privileged so the 417*0Sstevel@tonic-gate * kernel versions of these registers in pcb_fsbase and 418*0Sstevel@tonic-gate * pcb_gsbase are always up-to-date. 419*0Sstevel@tonic-gate */ 420*0Sstevel@tonic-gate pcb->pcb_ds = rp->r_ds; 421*0Sstevel@tonic-gate pcb->pcb_es = rp->r_es; 422*0Sstevel@tonic-gate pcb->pcb_fs = rp->r_fs; 423*0Sstevel@tonic-gate pcb->pcb_gs = rp->r_gs; 424*0Sstevel@tonic-gate pcb->pcb_flags |= RUPDATE_PENDING; 425*0Sstevel@tonic-gate lwp->lwp_thread->t_post_sys = 1; 426*0Sstevel@tonic-gate } 427*0Sstevel@tonic-gate #endif /* __amd64 */ 428*0Sstevel@tonic-gate 429*0Sstevel@tonic-gate ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPFS], &lwp->lwp_pcb.pcb_fsdesc, 430*0Sstevel@tonic-gate sizeof (lwp->lwp_pcb.pcb_fsdesc)) == 0); 431*0Sstevel@tonic-gate ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPGS], &lwp->lwp_pcb.pcb_gsdesc, 432*0Sstevel@tonic-gate sizeof (lwp->lwp_pcb.pcb_gsdesc)) == 0); 433*0Sstevel@tonic-gate } 434*0Sstevel@tonic-gate 435*0Sstevel@tonic-gate /* 436*0Sstevel@tonic-gate * Restore lwp private fs and gs segment descriptors 437*0Sstevel@tonic-gate * on current cpu's GDT. 438*0Sstevel@tonic-gate */ 439*0Sstevel@tonic-gate static void 440*0Sstevel@tonic-gate lwp_segregs_restore(klwp_t *lwp) 441*0Sstevel@tonic-gate { 442*0Sstevel@tonic-gate cpu_t *cpu = CPU; 443*0Sstevel@tonic-gate pcb_t *pcb = &lwp->lwp_pcb; 444*0Sstevel@tonic-gate 445*0Sstevel@tonic-gate ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc)); 446*0Sstevel@tonic-gate ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc)); 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc; 449*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc; 450*0Sstevel@tonic-gate 451*0Sstevel@tonic-gate #if defined(__amd64) 452*0Sstevel@tonic-gate /* 453*0Sstevel@tonic-gate * Make it impossible for a process to change its data model. 454*0Sstevel@tonic-gate * We do this by toggling the present bits for the 32 and 455*0Sstevel@tonic-gate * 64-bit user code descriptors. That way if a user lwp attempts 456*0Sstevel@tonic-gate * to change its data model (by using the wrong code descriptor in 457*0Sstevel@tonic-gate * %cs) it will fault immediately. This also allows us to simplify 458*0Sstevel@tonic-gate * assertions and checks in the kernel. 459*0Sstevel@tonic-gate */ 460*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_UCODE].usd_p = 1; 461*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_U32CODE].usd_p = 0; 462*0Sstevel@tonic-gate #endif /* __amd64 */ 463*0Sstevel@tonic-gate } 464*0Sstevel@tonic-gate 465*0Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 466*0Sstevel@tonic-gate 467*0Sstevel@tonic-gate static void 468*0Sstevel@tonic-gate lwp_segregs_restore32(klwp_t *lwp) 469*0Sstevel@tonic-gate { 470*0Sstevel@tonic-gate cpu_t *cpu = CPU; 471*0Sstevel@tonic-gate pcb_t *pcb = &lwp->lwp_pcb; 472*0Sstevel@tonic-gate 473*0Sstevel@tonic-gate ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc)); 474*0Sstevel@tonic-gate ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc)); 475*0Sstevel@tonic-gate 476*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc; 477*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc; 478*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_UCODE].usd_p = 0; 479*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_U32CODE].usd_p = 1; 480*0Sstevel@tonic-gate } 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 483*0Sstevel@tonic-gate 484*0Sstevel@tonic-gate /* 485*0Sstevel@tonic-gate * Add any lwp-associated context handlers to the lwp at the beginning 486*0Sstevel@tonic-gate * of the lwp's useful life. 487*0Sstevel@tonic-gate * 488*0Sstevel@tonic-gate * All paths which create lwp's invoke lwp_create(); lwp_create() 489*0Sstevel@tonic-gate * invokes lwp_stk_init() which initializes the stack, sets up 490*0Sstevel@tonic-gate * lwp_regs, and invokes this routine. 491*0Sstevel@tonic-gate * 492*0Sstevel@tonic-gate * All paths which destroy lwp's invoke lwp_exit() to rip the lwp 493*0Sstevel@tonic-gate * apart and put it on 'lwp_deathrow'; if the lwp is destroyed it 494*0Sstevel@tonic-gate * ends up in thread_free() which invokes freectx(t, 0) before 495*0Sstevel@tonic-gate * invoking lwp_stk_fini(). When the lwp is recycled from death 496*0Sstevel@tonic-gate * row, lwp_stk_fini() is invoked, then thread_free(), and thus 497*0Sstevel@tonic-gate * freectx(t, 0) as before. 498*0Sstevel@tonic-gate * 499*0Sstevel@tonic-gate * In the case of exec, the surviving lwp is thoroughly scrubbed 500*0Sstevel@tonic-gate * clean; exec invokes freectx(t, 1) to destroy associated contexts. 501*0Sstevel@tonic-gate * On the way back to the new image, it invokes setregs() which 502*0Sstevel@tonic-gate * in turn invokes this routine. 503*0Sstevel@tonic-gate */ 504*0Sstevel@tonic-gate void 505*0Sstevel@tonic-gate lwp_installctx(klwp_t *lwp) 506*0Sstevel@tonic-gate { 507*0Sstevel@tonic-gate kthread_t *t = lwptot(lwp); 508*0Sstevel@tonic-gate int thisthread = t == curthread; 509*0Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 510*0Sstevel@tonic-gate void (*restop)(klwp_t *) = lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ? 511*0Sstevel@tonic-gate lwp_segregs_restore : lwp_segregs_restore32; 512*0Sstevel@tonic-gate #else 513*0Sstevel@tonic-gate void (*restop)(klwp_t *) = lwp_segregs_restore; 514*0Sstevel@tonic-gate #endif 515*0Sstevel@tonic-gate 516*0Sstevel@tonic-gate /* 517*0Sstevel@tonic-gate * Install the basic lwp context handlers on each lwp. 518*0Sstevel@tonic-gate * 519*0Sstevel@tonic-gate * On the amd64 kernel, the context handlers are responsible for 520*0Sstevel@tonic-gate * virtualizing %ds, %es, %fs, and %gs to the lwp. The register 521*0Sstevel@tonic-gate * values are only ever changed via sys_rtt when the 522*0Sstevel@tonic-gate * RUPDATE_PENDING bit is set. Only sys_rtt gets to clear the bit. 523*0Sstevel@tonic-gate * 524*0Sstevel@tonic-gate * On the i386 kernel, the context handlers are responsible for 525*0Sstevel@tonic-gate * virtualizing %gs/%fs to the lwp by updating the per-cpu GDTs 526*0Sstevel@tonic-gate */ 527*0Sstevel@tonic-gate ASSERT(removectx(t, lwp, lwp_segregs_save, restop, 528*0Sstevel@tonic-gate NULL, NULL, NULL, NULL) == 0); 529*0Sstevel@tonic-gate if (thisthread) 530*0Sstevel@tonic-gate kpreempt_disable(); 531*0Sstevel@tonic-gate installctx(t, lwp, lwp_segregs_save, restop, 532*0Sstevel@tonic-gate NULL, NULL, NULL, NULL); 533*0Sstevel@tonic-gate if (thisthread) { 534*0Sstevel@tonic-gate /* 535*0Sstevel@tonic-gate * Since we're the right thread, set the values in the GDT 536*0Sstevel@tonic-gate */ 537*0Sstevel@tonic-gate restop(lwp); 538*0Sstevel@tonic-gate kpreempt_enable(); 539*0Sstevel@tonic-gate } 540*0Sstevel@tonic-gate 541*0Sstevel@tonic-gate /* 542*0Sstevel@tonic-gate * If we have sysenter/sysexit instructions enabled, we need 543*0Sstevel@tonic-gate * to ensure that the hardware mechanism is kept up-to-date with the 544*0Sstevel@tonic-gate * lwp's kernel stack pointer across context switches. 545*0Sstevel@tonic-gate * 546*0Sstevel@tonic-gate * sep_save zeros the sysenter stack pointer msr; sep_restore sets 547*0Sstevel@tonic-gate * it to the lwp's kernel stack pointer (kstktop). 548*0Sstevel@tonic-gate */ 549*0Sstevel@tonic-gate if (x86_feature & X86_SEP) { 550*0Sstevel@tonic-gate #if defined(__amd64) 551*0Sstevel@tonic-gate caddr_t kstktop = (caddr_t)lwp->lwp_regs; 552*0Sstevel@tonic-gate #elif defined(__i386) 553*0Sstevel@tonic-gate caddr_t kstktop = ((caddr_t)lwp->lwp_regs - MINFRAME) + 554*0Sstevel@tonic-gate SA(sizeof (struct regs) + MINFRAME); 555*0Sstevel@tonic-gate #endif 556*0Sstevel@tonic-gate ASSERT(removectx(t, kstktop, 557*0Sstevel@tonic-gate sep_save, sep_restore, NULL, NULL, NULL, NULL) == 0); 558*0Sstevel@tonic-gate 559*0Sstevel@tonic-gate if (thisthread) 560*0Sstevel@tonic-gate kpreempt_disable(); 561*0Sstevel@tonic-gate installctx(t, kstktop, 562*0Sstevel@tonic-gate sep_save, sep_restore, NULL, NULL, NULL, NULL); 563*0Sstevel@tonic-gate if (thisthread) { 564*0Sstevel@tonic-gate /* 565*0Sstevel@tonic-gate * We're the right thread, so set the stack pointer 566*0Sstevel@tonic-gate * for the first sysenter instruction to use 567*0Sstevel@tonic-gate */ 568*0Sstevel@tonic-gate sep_restore(kstktop); 569*0Sstevel@tonic-gate kpreempt_enable(); 570*0Sstevel@tonic-gate } 571*0Sstevel@tonic-gate } 572*0Sstevel@tonic-gate } 573*0Sstevel@tonic-gate 574*0Sstevel@tonic-gate /* 575*0Sstevel@tonic-gate * Clear registers on exec(2). 576*0Sstevel@tonic-gate */ 577*0Sstevel@tonic-gate void 578*0Sstevel@tonic-gate setregs(uarg_t *args) 579*0Sstevel@tonic-gate { 580*0Sstevel@tonic-gate struct regs *rp; 581*0Sstevel@tonic-gate kthread_t *t = curthread; 582*0Sstevel@tonic-gate klwp_t *lwp = ttolwp(t); 583*0Sstevel@tonic-gate pcb_t *pcb = &lwp->lwp_pcb; 584*0Sstevel@tonic-gate greg_t sp; 585*0Sstevel@tonic-gate 586*0Sstevel@tonic-gate /* 587*0Sstevel@tonic-gate * Initialize user registers 588*0Sstevel@tonic-gate */ 589*0Sstevel@tonic-gate (void) save_syscall_args(); /* copy args from registers first */ 590*0Sstevel@tonic-gate rp = lwptoregs(lwp); 591*0Sstevel@tonic-gate sp = rp->r_sp; 592*0Sstevel@tonic-gate bzero(rp, sizeof (*rp)); 593*0Sstevel@tonic-gate 594*0Sstevel@tonic-gate rp->r_ss = UDS_SEL; 595*0Sstevel@tonic-gate rp->r_sp = sp; 596*0Sstevel@tonic-gate rp->r_pc = args->entry; 597*0Sstevel@tonic-gate rp->r_ps = PSL_USER; 598*0Sstevel@tonic-gate 599*0Sstevel@tonic-gate #if defined(__amd64) 600*0Sstevel@tonic-gate 601*0Sstevel@tonic-gate pcb->pcb_fs = pcb->pcb_gs = 0; 602*0Sstevel@tonic-gate pcb->pcb_fsbase = pcb->pcb_gsbase = 0; 603*0Sstevel@tonic-gate 604*0Sstevel@tonic-gate if (ttoproc(t)->p_model == DATAMODEL_NATIVE) { 605*0Sstevel@tonic-gate cpu_t *cpu; 606*0Sstevel@tonic-gate 607*0Sstevel@tonic-gate rp->r_cs = UCS_SEL; 608*0Sstevel@tonic-gate 609*0Sstevel@tonic-gate /* 610*0Sstevel@tonic-gate * Only allow 64-bit user code descriptor to be present. 611*0Sstevel@tonic-gate */ 612*0Sstevel@tonic-gate kpreempt_disable(); 613*0Sstevel@tonic-gate cpu = CPU; 614*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_UCODE].usd_p = 1; 615*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_U32CODE].usd_p = 0; 616*0Sstevel@tonic-gate kpreempt_enable(); 617*0Sstevel@tonic-gate 618*0Sstevel@tonic-gate /* 619*0Sstevel@tonic-gate * Arrange that the virtualized %fs and %gs GDT descriptors 620*0Sstevel@tonic-gate * have a well-defined initial state (present, ring 3 621*0Sstevel@tonic-gate * and of type data). 622*0Sstevel@tonic-gate */ 623*0Sstevel@tonic-gate pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc; 624*0Sstevel@tonic-gate 625*0Sstevel@tonic-gate /* 626*0Sstevel@tonic-gate * thrptr is either NULL or a value used by DTrace. 627*0Sstevel@tonic-gate * 64-bit processes use %fs as their "thread" register. 628*0Sstevel@tonic-gate */ 629*0Sstevel@tonic-gate if (args->thrptr) 630*0Sstevel@tonic-gate (void) lwp_setprivate(lwp, _LWP_FSBASE, args->thrptr); 631*0Sstevel@tonic-gate 632*0Sstevel@tonic-gate } else { 633*0Sstevel@tonic-gate cpu_t *cpu; 634*0Sstevel@tonic-gate 635*0Sstevel@tonic-gate rp->r_cs = U32CS_SEL; 636*0Sstevel@tonic-gate rp->r_ds = rp->r_es = UDS_SEL; 637*0Sstevel@tonic-gate 638*0Sstevel@tonic-gate /* 639*0Sstevel@tonic-gate * only allow 32-bit user code selector to be present. 640*0Sstevel@tonic-gate */ 641*0Sstevel@tonic-gate kpreempt_disable(); 642*0Sstevel@tonic-gate cpu = CPU; 643*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_UCODE].usd_p = 0; 644*0Sstevel@tonic-gate cpu->cpu_gdt[GDT_U32CODE].usd_p = 1; 645*0Sstevel@tonic-gate kpreempt_enable(); 646*0Sstevel@tonic-gate 647*0Sstevel@tonic-gate pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc; 648*0Sstevel@tonic-gate 649*0Sstevel@tonic-gate /* 650*0Sstevel@tonic-gate * thrptr is either NULL or a value used by DTrace. 651*0Sstevel@tonic-gate * 32-bit processes use %gs as their "thread" register. 652*0Sstevel@tonic-gate */ 653*0Sstevel@tonic-gate if (args->thrptr) 654*0Sstevel@tonic-gate (void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr); 655*0Sstevel@tonic-gate 656*0Sstevel@tonic-gate } 657*0Sstevel@tonic-gate 658*0Sstevel@tonic-gate 659*0Sstevel@tonic-gate pcb->pcb_ds = rp->r_ds; 660*0Sstevel@tonic-gate pcb->pcb_es = rp->r_es; 661*0Sstevel@tonic-gate pcb->pcb_flags |= RUPDATE_PENDING; 662*0Sstevel@tonic-gate 663*0Sstevel@tonic-gate #elif defined(__i386) 664*0Sstevel@tonic-gate 665*0Sstevel@tonic-gate rp->r_cs = UCS_SEL; 666*0Sstevel@tonic-gate rp->r_ds = rp->r_es = UDS_SEL; 667*0Sstevel@tonic-gate 668*0Sstevel@tonic-gate /* 669*0Sstevel@tonic-gate * Arrange that the virtualized %fs and %gs GDT descriptors 670*0Sstevel@tonic-gate * have a well-defined initial state (present, ring 3 671*0Sstevel@tonic-gate * and of type data). 672*0Sstevel@tonic-gate */ 673*0Sstevel@tonic-gate pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc; 674*0Sstevel@tonic-gate 675*0Sstevel@tonic-gate 676*0Sstevel@tonic-gate /* 677*0Sstevel@tonic-gate * For %gs we need to reset LWP_GSBASE in pcb and the 678*0Sstevel@tonic-gate * per-cpu GDT descriptor. thrptr is either NULL 679*0Sstevel@tonic-gate * or a value used by DTrace. 680*0Sstevel@tonic-gate */ 681*0Sstevel@tonic-gate if (args->thrptr) 682*0Sstevel@tonic-gate (void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr); 683*0Sstevel@tonic-gate #endif 684*0Sstevel@tonic-gate 685*0Sstevel@tonic-gate lwp->lwp_eosys = JUSTRETURN; 686*0Sstevel@tonic-gate t->t_post_sys = 1; 687*0Sstevel@tonic-gate 688*0Sstevel@tonic-gate /* 689*0Sstevel@tonic-gate * Here we initialize minimal fpu state. 690*0Sstevel@tonic-gate * The rest is done at the first floating 691*0Sstevel@tonic-gate * point instruction that a process executes. 692*0Sstevel@tonic-gate */ 693*0Sstevel@tonic-gate pcb->pcb_fpu.fpu_flags = 0; 694*0Sstevel@tonic-gate 695*0Sstevel@tonic-gate /* 696*0Sstevel@tonic-gate * Add the lwp context handlers that virtualize segment registers, 697*0Sstevel@tonic-gate * and/or system call stacks etc. 698*0Sstevel@tonic-gate */ 699*0Sstevel@tonic-gate lwp_installctx(lwp); 700*0Sstevel@tonic-gate } 701*0Sstevel@tonic-gate 702*0Sstevel@tonic-gate #if !defined(lwp_getdatamodel) 703*0Sstevel@tonic-gate 704*0Sstevel@tonic-gate /* 705*0Sstevel@tonic-gate * Return the datamodel of the given lwp. 706*0Sstevel@tonic-gate */ 707*0Sstevel@tonic-gate /*ARGSUSED*/ 708*0Sstevel@tonic-gate model_t 709*0Sstevel@tonic-gate lwp_getdatamodel(klwp_t *lwp) 710*0Sstevel@tonic-gate { 711*0Sstevel@tonic-gate return (lwp->lwp_procp->p_model); 712*0Sstevel@tonic-gate } 713*0Sstevel@tonic-gate 714*0Sstevel@tonic-gate #endif /* !lwp_getdatamodel */ 715*0Sstevel@tonic-gate 716*0Sstevel@tonic-gate #if !defined(get_udatamodel) 717*0Sstevel@tonic-gate 718*0Sstevel@tonic-gate model_t 719*0Sstevel@tonic-gate get_udatamodel(void) 720*0Sstevel@tonic-gate { 721*0Sstevel@tonic-gate return (curproc->p_model); 722*0Sstevel@tonic-gate } 723*0Sstevel@tonic-gate 724*0Sstevel@tonic-gate #endif /* !get_udatamodel */ 725