1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 28 /* All Rights Reserved */ 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/sysmacros.h> 35 #include <sys/signal.h> 36 #include <sys/systm.h> 37 #include <sys/user.h> 38 #include <sys/mman.h> 39 #include <sys/class.h> 40 #include <sys/proc.h> 41 #include <sys/procfs.h> 42 #include <sys/buf.h> 43 #include <sys/kmem.h> 44 #include <sys/cred.h> 45 #include <sys/archsystm.h> 46 #include <sys/vmparam.h> 47 #include <sys/prsystm.h> 48 #include <sys/reboot.h> 49 #include <sys/uadmin.h> 50 #include <sys/vfs.h> 51 #include <sys/vnode.h> 52 #include <sys/file.h> 53 #include <sys/session.h> 54 #include <sys/ucontext.h> 55 #include <sys/dnlc.h> 56 #include <sys/var.h> 57 #include <sys/cmn_err.h> 58 #include <sys/debugreg.h> 59 #include <sys/thread.h> 60 #include <sys/vtrace.h> 61 #include <sys/consdev.h> 62 #include <sys/psw.h> 63 #include <sys/regset.h> 64 #include <sys/privregs.h> 65 #include <sys/stack.h> 66 #include <sys/swap.h> 67 #include <vm/hat.h> 68 #include <vm/anon.h> 69 #include <vm/as.h> 70 #include <vm/page.h> 71 #include <vm/seg.h> 72 #include <vm/seg_kmem.h> 73 #include <vm/seg_map.h> 74 #include <vm/seg_vn.h> 75 #include <sys/exec.h> 76 #include <sys/acct.h> 77 #include <sys/core.h> 78 #include <sys/corectl.h> 79 #include <sys/modctl.h> 80 #include <sys/tuneable.h> 81 #include <c2/audit.h> 82 #include <sys/bootconf.h> 83 #include <sys/brand.h> 84 #include <sys/dumphdr.h> 85 #include <sys/promif.h> 86 #include <sys/systeminfo.h> 87 #include <sys/kdi.h> 88 #include <sys/contract_impl.h> 89 #include <sys/x86_archext.h> 90 #include <sys/segments.h> 91 #include <sys/ontrap.h> 92 93 /* 94 * Compare the version of boot that boot says it is against 95 * the version of boot the kernel expects. 96 */ 97 int 98 check_boot_version(int boots_version) 99 { 100 if (boots_version == BO_VERSION) 101 return (0); 102 103 prom_printf("Wrong boot interface - kernel needs v%d found v%d\n", 104 BO_VERSION, boots_version); 105 prom_panic("halting"); 106 /*NOTREACHED*/ 107 } 108 109 /* 110 * Process the physical installed list for boot. 111 * Finds: 112 * 1) the pfn of the highest installed physical page, 113 * 2) the number of pages installed 114 * 3) the number of distinct contiguous regions these pages fall into. 115 */ 116 void 117 installed_top_size( 118 struct memlist *list, /* pointer to start of installed list */ 119 pfn_t *high_pfn, /* return ptr for top value */ 120 pgcnt_t *pgcnt, /* return ptr for sum of installed pages */ 121 int *ranges) /* return ptr for the count of contig. ranges */ 122 { 123 pfn_t top = 0; 124 pgcnt_t sumpages = 0; 125 pfn_t highp; /* high page in a chunk */ 126 int cnt = 0; 127 128 for (; list; list = list->next) { 129 ++cnt; 130 highp = (list->address + list->size - 1) >> PAGESHIFT; 131 if (top < highp) 132 top = highp; 133 sumpages += btop(list->size); 134 } 135 136 *high_pfn = top; 137 *pgcnt = sumpages; 138 *ranges = cnt; 139 } 140 141 /* 142 * Copy in a memory list from boot to kernel, with a filter function 143 * to remove pages. The filter function can increase the address and/or 144 * decrease the size to filter out pages. It will also align addresses and 145 * sizes to PAGESIZE. 146 */ 147 void 148 copy_memlist_filter( 149 struct memlist *src, 150 struct memlist **dstp, 151 void (*filter)(uint64_t *, uint64_t *)) 152 { 153 struct memlist *dst, *prev; 154 uint64_t addr; 155 uint64_t size; 156 uint64_t eaddr; 157 158 dst = *dstp; 159 prev = dst; 160 161 /* 162 * Move through the memlist applying a filter against 163 * each range of memory. Note that we may apply the 164 * filter multiple times against each memlist entry. 165 */ 166 for (; src; src = src->next) { 167 addr = P2ROUNDUP(src->address, PAGESIZE); 168 eaddr = P2ALIGN(src->address + src->size, PAGESIZE); 169 while (addr < eaddr) { 170 size = eaddr - addr; 171 if (filter != NULL) 172 filter(&addr, &size); 173 if (size == 0) 174 break; 175 dst->address = addr; 176 dst->size = size; 177 dst->next = 0; 178 if (prev == dst) { 179 dst->prev = 0; 180 dst++; 181 } else { 182 dst->prev = prev; 183 prev->next = dst; 184 dst++; 185 prev++; 186 } 187 addr += size; 188 } 189 } 190 191 *dstp = dst; 192 } 193 194 /* 195 * Kernel setup code, called from startup(). 196 */ 197 void 198 kern_setup1(void) 199 { 200 proc_t *pp; 201 202 pp = &p0; 203 204 proc_sched = pp; 205 206 /* 207 * Initialize process 0 data structures 208 */ 209 pp->p_stat = SRUN; 210 pp->p_flag = SSYS; 211 212 pp->p_pidp = &pid0; 213 pp->p_pgidp = &pid0; 214 pp->p_sessp = &session0; 215 pp->p_tlist = &t0; 216 pid0.pid_pglink = pp; 217 pid0.pid_pgtail = pp; 218 219 /* 220 * XXX - we asssume that the u-area is zeroed out except for 221 * ttolwp(curthread)->lwp_regs. 222 */ 223 PTOU(curproc)->u_cmask = (mode_t)CMASK; 224 225 thread_init(); /* init thread_free list */ 226 pid_init(); /* initialize pid (proc) table */ 227 contract_init(); /* initialize contracts */ 228 229 init_pages_pp_maximum(); 230 } 231 232 /* 233 * Load a procedure into a thread. 234 */ 235 void 236 thread_load(kthread_t *t, void (*start)(), caddr_t arg, size_t len) 237 { 238 caddr_t sp; 239 size_t framesz; 240 caddr_t argp; 241 long *p; 242 extern void thread_start(); 243 244 /* 245 * Push a "c" call frame onto the stack to represent 246 * the caller of "start". 247 */ 248 sp = t->t_stk; 249 ASSERT(((uintptr_t)t->t_stk & (STACK_ENTRY_ALIGN - 1)) == 0); 250 if (len != 0) { 251 /* 252 * the object that arg points at is copied into the 253 * caller's frame. 254 */ 255 framesz = SA(len); 256 sp -= framesz; 257 ASSERT(sp > t->t_stkbase); 258 argp = sp + SA(MINFRAME); 259 bcopy(arg, argp, len); 260 arg = argp; 261 } 262 /* 263 * Set up arguments (arg and len) on the caller's stack frame. 264 */ 265 p = (long *)sp; 266 267 *--p = 0; /* fake call */ 268 *--p = 0; /* null frame pointer terminates stack trace */ 269 *--p = (long)len; 270 *--p = (intptr_t)arg; 271 *--p = (intptr_t)start; 272 273 /* 274 * initialize thread to resume at thread_start() which will 275 * turn around and invoke (*start)(arg, len). 276 */ 277 t->t_pc = (uintptr_t)thread_start; 278 t->t_sp = (uintptr_t)p; 279 280 ASSERT((t->t_sp & (STACK_ENTRY_ALIGN - 1)) == 0); 281 } 282 283 /* 284 * load user registers into lwp. 285 */ 286 /*ARGSUSED2*/ 287 void 288 lwp_load(klwp_t *lwp, gregset_t grp, uintptr_t thrptr) 289 { 290 struct regs *rp = lwptoregs(lwp); 291 292 setgregs(lwp, grp); 293 rp->r_ps = PSL_USER; 294 295 /* 296 * For 64-bit lwps, we allow null %fs selector value, and null 297 * %gs selector to point anywhere in the address space using 298 * %fsbase and %gsbase behind the scenes. libc uses %fs to point 299 * at the ulwp_t structure. 300 * 301 * For 32-bit lwps, libc wedges its lwp thread pointer into the 302 * ucontext ESP slot (which is otherwise irrelevant to setting a 303 * ucontext) and LWPGS_SEL value into gregs[REG_GS]. This is so 304 * syslwp_create() can atomically setup %gs. 305 * 306 * See setup_context() in libc. 307 */ 308 #ifdef _SYSCALL32_IMPL 309 if (lwp_getdatamodel(lwp) == DATAMODEL_ILP32) { 310 if (grp[REG_GS] == LWPGS_SEL) 311 (void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr); 312 } else { 313 /* 314 * See lwp_setprivate in kernel and setup_context in libc. 315 * 316 * Currently libc constructs a ucontext from whole cloth for 317 * every new (not main) lwp created. For 64 bit processes 318 * %fsbase is directly set to point to current thread pointer. 319 * In the past (solaris 10) %fs was also set LWPFS_SEL to 320 * indicate %fsbase. Now we use the null GDT selector for 321 * this purpose. LWP[FS|GS]_SEL are only intended for 32 bit 322 * processes. To ease transition we support older libcs in 323 * the newer kernel by forcing %fs or %gs selector to null 324 * by calling lwp_setprivate if LWP[FS|GS]_SEL is passed in 325 * the ucontext. This is should be ripped out at some future 326 * date. Another fix would be for libc to do a getcontext 327 * and inherit the null %fs/%gs from the current context but 328 * that means an extra system call and could hurt performance. 329 */ 330 if (grp[REG_FS] == 0x1bb) /* hard code legacy LWPFS_SEL */ 331 (void) lwp_setprivate(lwp, _LWP_FSBASE, 332 (uintptr_t)grp[REG_FSBASE]); 333 334 if (grp[REG_GS] == 0x1c3) /* hard code legacy LWPGS_SEL */ 335 (void) lwp_setprivate(lwp, _LWP_GSBASE, 336 (uintptr_t)grp[REG_GSBASE]); 337 } 338 #else 339 if (grp[GS] == LWPGS_SEL) 340 (void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr); 341 #endif 342 343 lwp->lwp_eosys = JUSTRETURN; 344 lwptot(lwp)->t_post_sys = 1; 345 } 346 347 /* 348 * set syscall()'s return values for a lwp. 349 */ 350 void 351 lwp_setrval(klwp_t *lwp, int v1, int v2) 352 { 353 lwptoregs(lwp)->r_ps &= ~PS_C; 354 lwptoregs(lwp)->r_r0 = v1; 355 lwptoregs(lwp)->r_r1 = v2; 356 } 357 358 /* 359 * set syscall()'s return values for a lwp. 360 */ 361 void 362 lwp_setsp(klwp_t *lwp, caddr_t sp) 363 { 364 lwptoregs(lwp)->r_sp = (intptr_t)sp; 365 } 366 367 /* 368 * Copy regs from parent to child. 369 */ 370 void 371 lwp_forkregs(klwp_t *lwp, klwp_t *clwp) 372 { 373 #if defined(__amd64) 374 struct pcb *pcb = &clwp->lwp_pcb; 375 struct regs *rp = lwptoregs(lwp); 376 377 if ((pcb->pcb_flags & RUPDATE_PENDING) == 0) { 378 pcb->pcb_ds = rp->r_ds; 379 pcb->pcb_es = rp->r_es; 380 pcb->pcb_fs = rp->r_fs; 381 pcb->pcb_gs = rp->r_gs; 382 pcb->pcb_flags |= RUPDATE_PENDING; 383 lwptot(clwp)->t_post_sys = 1; 384 } 385 ASSERT(lwptot(clwp)->t_post_sys); 386 #endif 387 388 bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct regs)); 389 } 390 391 /* 392 * This function is currently unused on x86. 393 */ 394 /*ARGSUSED*/ 395 void 396 lwp_freeregs(klwp_t *lwp, int isexec) 397 {} 398 399 /* 400 * This function is currently unused on x86. 401 */ 402 void 403 lwp_pcb_exit(void) 404 {} 405 406 /* 407 * Lwp context ops for segment registers. 408 */ 409 410 /* 411 * Every time we come into the kernel (syscall, interrupt or trap 412 * but not fast-traps) we capture the current values of the user's 413 * segment registers into the lwp's reg structure. This includes 414 * lcall for i386 generic system call support since it is handled 415 * as a segment-not-present trap. 416 * 417 * Here we save the current values from the lwp regs into the pcb 418 * and set the RUPDATE_PENDING bit to tell the rest of the kernel 419 * that the pcb copy of the segment registers is the current one. 420 * This ensures the lwp's next trip to user land via update_sregs. 421 * Finally we set t_post_sys to ensure that no system call fast-path's 422 * its way out of the kernel via sysret. 423 * 424 * (This means that we need to have interrupts disabled when we test 425 * t->t_post_sys in the syscall handlers; if the test fails, we need 426 * to keep interrupts disabled until we return to userland so we can't 427 * be switched away.) 428 * 429 * As a result of all this, we don't really have to do a whole lot if 430 * the thread is just mucking about in the kernel, switching on and 431 * off the cpu for whatever reason it feels like. And yet we still 432 * preserve fast syscalls, cause if we -don't- get descheduled, 433 * we never come here either. 434 */ 435 436 #define VALID_LWP_DESC(udp) ((udp)->usd_type == SDT_MEMRWA && \ 437 (udp)->usd_p == 1 && (udp)->usd_dpl == SEL_UPL) 438 439 void 440 lwp_segregs_save(klwp_t *lwp) 441 { 442 #if defined(__amd64) 443 pcb_t *pcb = &lwp->lwp_pcb; 444 struct regs *rp; 445 446 ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc)); 447 ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc)); 448 449 if ((pcb->pcb_flags & RUPDATE_PENDING) == 0) { 450 rp = lwptoregs(lwp); 451 452 /* 453 * If there's no update already pending, capture the current 454 * %ds/%es/%fs/%gs values from lwp's regs in case the user 455 * changed them; %fsbase and %gsbase are privileged so the 456 * kernel versions of these registers in pcb_fsbase and 457 * pcb_gsbase are always up-to-date. 458 */ 459 pcb->pcb_ds = rp->r_ds; 460 pcb->pcb_es = rp->r_es; 461 pcb->pcb_fs = rp->r_fs; 462 pcb->pcb_gs = rp->r_gs; 463 pcb->pcb_flags |= RUPDATE_PENDING; 464 lwp->lwp_thread->t_post_sys = 1; 465 } 466 #endif /* __amd64 */ 467 468 ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPFS], &lwp->lwp_pcb.pcb_fsdesc, 469 sizeof (lwp->lwp_pcb.pcb_fsdesc)) == 0); 470 ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPGS], &lwp->lwp_pcb.pcb_gsdesc, 471 sizeof (lwp->lwp_pcb.pcb_gsdesc)) == 0); 472 } 473 474 #if defined(__amd64) 475 476 /* 477 * Update the segment registers with new values from the pcb 478 * 479 * We have to do this carefully, and in the following order, 480 * in case any of the selectors points at a bogus descriptor. 481 * If they do, we'll catch trap with on_trap and return 1. 482 * returns 0 on success. 483 * 484 * This is particularly tricky for %gs. 485 * This routine must be executed under a cli. 486 */ 487 int 488 update_sregs(struct regs *rp, klwp_t *lwp) 489 { 490 pcb_t *pcb = &lwp->lwp_pcb; 491 ulong_t kgsbase; 492 on_trap_data_t otd; 493 int rc = 0; 494 495 if (!on_trap(&otd, OT_SEGMENT_ACCESS)) { 496 497 kgsbase = (ulong_t)CPU; 498 __set_gs(pcb->pcb_gs); 499 500 /* 501 * If __set_gs fails it's because the new %gs is a bad %gs, 502 * we'll be taking a trap but with the original %gs and %gsbase 503 * undamaged (i.e. pointing at curcpu). 504 * 505 * We've just mucked up the kernel's gsbase. Oops. In 506 * particular we can't take any traps at all. Make the newly 507 * computed gsbase be the hidden gs via __swapgs , and fix 508 * the kernel's gsbase back again. Later, when we return to 509 * userland we'll swapgs again restoring gsbase just loaded 510 * above. 511 */ 512 __swapgs(); 513 rp->r_gs = pcb->pcb_gs; 514 515 /* 516 * restore kernel's gsbase 517 */ 518 wrmsr(MSR_AMD_GSBASE, kgsbase); 519 520 /* 521 * Only override the descriptor base address if 522 * r_gs == LWPGS_SEL or if r_gs == NULL. A note on 523 * NULL descriptors -- 32-bit programs take faults 524 * if they deference NULL descriptors; however, 525 * when 64-bit programs load them into %fs or %gs, 526 * they DONT fault -- only the base address remains 527 * whatever it was from the last load. Urk. 528 * 529 * XXX - note that lwp_setprivate now sets %fs/%gs to the 530 * null selector for 64 bit processes. Whereas before 531 * %fs/%gs were set to LWP(FS|GS)_SEL regardless of 532 * the process's data model. For now we check for both 533 * values so that the kernel can also support the older 534 * libc. This should be ripped out at some point in the 535 * future. 536 */ 537 if (pcb->pcb_gs == LWPGS_SEL || pcb->pcb_gs == 0) 538 wrmsr(MSR_AMD_KGSBASE, pcb->pcb_gsbase); 539 540 __set_ds(pcb->pcb_ds); 541 rp->r_ds = pcb->pcb_ds; 542 543 __set_es(pcb->pcb_es); 544 rp->r_es = pcb->pcb_es; 545 546 __set_fs(pcb->pcb_fs); 547 rp->r_fs = pcb->pcb_fs; 548 549 /* 550 * Same as for %gs 551 */ 552 if (pcb->pcb_fs == LWPFS_SEL || pcb->pcb_fs == 0) 553 wrmsr(MSR_AMD_FSBASE, pcb->pcb_fsbase); 554 555 } else { 556 cli(); 557 rc = 1; 558 } 559 no_trap(); 560 return (rc); 561 } 562 #endif /* __amd64 */ 563 564 #ifdef _SYSCALL32_IMPL 565 566 /* 567 * Make it impossible for a process to change its data model. 568 * We do this by toggling the present bits for the 32 and 569 * 64-bit user code descriptors. That way if a user lwp attempts 570 * to change its data model (by using the wrong code descriptor in 571 * %cs) it will fault immediately. This also allows us to simplify 572 * assertions and checks in the kernel. 573 */ 574 static void 575 gdt_ucode_model(model_t model) 576 { 577 cpu_t *cpu; 578 579 kpreempt_disable(); 580 cpu = CPU; 581 if (model == DATAMODEL_NATIVE) { 582 cpu->cpu_gdt[GDT_UCODE].usd_p = 1; 583 cpu->cpu_gdt[GDT_U32CODE].usd_p = 0; 584 } else { 585 cpu->cpu_gdt[GDT_U32CODE].usd_p = 1; 586 cpu->cpu_gdt[GDT_UCODE].usd_p = 0; 587 } 588 kpreempt_enable(); 589 } 590 591 #endif /* _SYSCALL32_IMPL */ 592 593 /* 594 * Restore lwp private fs and gs segment descriptors 595 * on current cpu's GDT. 596 */ 597 static void 598 lwp_segregs_restore(klwp_t *lwp) 599 { 600 pcb_t *pcb = &lwp->lwp_pcb; 601 cpu_t *cpu = CPU; 602 603 ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc)); 604 ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc)); 605 606 #ifdef _SYSCALL32_IMPL 607 gdt_ucode_model(DATAMODEL_NATIVE); 608 #endif 609 610 cpu->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc; 611 cpu->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc; 612 613 } 614 615 #ifdef _SYSCALL32_IMPL 616 617 static void 618 lwp_segregs_restore32(klwp_t *lwp) 619 { 620 /*LINTED*/ 621 cpu_t *cpu = CPU; 622 pcb_t *pcb = &lwp->lwp_pcb; 623 624 ASSERT(VALID_LWP_DESC(&lwp->lwp_pcb.pcb_fsdesc)); 625 ASSERT(VALID_LWP_DESC(&lwp->lwp_pcb.pcb_gsdesc)); 626 627 gdt_ucode_model(DATAMODEL_ILP32); 628 cpu->cpu_gdt[GDT_LWPFS] = pcb->pcb_fsdesc; 629 cpu->cpu_gdt[GDT_LWPGS] = pcb->pcb_gsdesc; 630 } 631 632 #endif /* _SYSCALL32_IMPL */ 633 634 /* 635 * If this is a process in a branded zone, then we want it to use the brand 636 * syscall entry points instead of the standard Solaris entry points. This 637 * routine must be called when a new lwp is created within a branded zone 638 * or when an existing lwp moves into a branded zone via a zone_enter() 639 * operation. 640 */ 641 void 642 lwp_attach_brand_hdlrs(klwp_t *lwp) 643 { 644 kthread_t *t = lwptot(lwp); 645 646 ASSERT(PROC_IS_BRANDED(lwptoproc(lwp))); 647 ASSERT(removectx(t, NULL, brand_interpositioning_disable, 648 brand_interpositioning_enable, NULL, NULL, NULL, NULL) == 0); 649 650 installctx(t, NULL, brand_interpositioning_disable, 651 brand_interpositioning_enable, NULL, NULL, NULL, NULL); 652 653 if (t == curthread) { 654 kpreempt_disable(); 655 brand_interpositioning_enable(); 656 kpreempt_enable(); 657 } 658 } 659 660 /* 661 * Add any lwp-associated context handlers to the lwp at the beginning 662 * of the lwp's useful life. 663 * 664 * All paths which create lwp's invoke lwp_create(); lwp_create() 665 * invokes lwp_stk_init() which initializes the stack, sets up 666 * lwp_regs, and invokes this routine. 667 * 668 * All paths which destroy lwp's invoke lwp_exit() to rip the lwp 669 * apart and put it on 'lwp_deathrow'; if the lwp is destroyed it 670 * ends up in thread_free() which invokes freectx(t, 0) before 671 * invoking lwp_stk_fini(). When the lwp is recycled from death 672 * row, lwp_stk_fini() is invoked, then thread_free(), and thus 673 * freectx(t, 0) as before. 674 * 675 * In the case of exec, the surviving lwp is thoroughly scrubbed 676 * clean; exec invokes freectx(t, 1) to destroy associated contexts. 677 * On the way back to the new image, it invokes setregs() which 678 * in turn invokes this routine. 679 */ 680 void 681 lwp_installctx(klwp_t *lwp) 682 { 683 kthread_t *t = lwptot(lwp); 684 int thisthread = t == curthread; 685 #ifdef _SYSCALL32_IMPL 686 void (*restop)(klwp_t *) = lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ? 687 lwp_segregs_restore : lwp_segregs_restore32; 688 #else 689 void (*restop)(klwp_t *) = lwp_segregs_restore; 690 #endif 691 692 /* 693 * Install the basic lwp context handlers on each lwp. 694 * 695 * On the amd64 kernel, the context handlers are responsible for 696 * virtualizing %ds, %es, %fs, and %gs to the lwp. The register 697 * values are only ever changed via sys_rtt when the 698 * RUPDATE_PENDING bit is set. Only sys_rtt gets to clear the bit. 699 * 700 * On the i386 kernel, the context handlers are responsible for 701 * virtualizing %gs/%fs to the lwp by updating the per-cpu GDTs 702 */ 703 ASSERT(removectx(t, lwp, lwp_segregs_save, restop, 704 NULL, NULL, NULL, NULL) == 0); 705 if (thisthread) 706 kpreempt_disable(); 707 installctx(t, lwp, lwp_segregs_save, restop, 708 NULL, NULL, NULL, NULL); 709 if (thisthread) { 710 /* 711 * Since we're the right thread, set the values in the GDT 712 */ 713 restop(lwp); 714 kpreempt_enable(); 715 } 716 717 /* 718 * If we have sysenter/sysexit instructions enabled, we need 719 * to ensure that the hardware mechanism is kept up-to-date with the 720 * lwp's kernel stack pointer across context switches. 721 * 722 * sep_save zeros the sysenter stack pointer msr; sep_restore sets 723 * it to the lwp's kernel stack pointer (kstktop). 724 */ 725 if (x86_feature & X86_SEP) { 726 #if defined(__amd64) 727 caddr_t kstktop = (caddr_t)lwp->lwp_regs; 728 #elif defined(__i386) 729 caddr_t kstktop = ((caddr_t)lwp->lwp_regs - MINFRAME) + 730 SA(sizeof (struct regs) + MINFRAME); 731 #endif 732 ASSERT(removectx(t, kstktop, 733 sep_save, sep_restore, NULL, NULL, NULL, NULL) == 0); 734 735 if (thisthread) 736 kpreempt_disable(); 737 installctx(t, kstktop, 738 sep_save, sep_restore, NULL, NULL, NULL, NULL); 739 if (thisthread) { 740 /* 741 * We're the right thread, so set the stack pointer 742 * for the first sysenter instruction to use 743 */ 744 sep_restore(kstktop); 745 kpreempt_enable(); 746 } 747 } 748 749 if (PROC_IS_BRANDED(ttoproc(t))) 750 lwp_attach_brand_hdlrs(lwp); 751 } 752 753 /* 754 * Clear registers on exec(2). 755 */ 756 void 757 setregs(uarg_t *args) 758 { 759 struct regs *rp; 760 kthread_t *t = curthread; 761 klwp_t *lwp = ttolwp(t); 762 pcb_t *pcb = &lwp->lwp_pcb; 763 greg_t sp; 764 765 /* 766 * Initialize user registers 767 */ 768 (void) save_syscall_args(); /* copy args from registers first */ 769 rp = lwptoregs(lwp); 770 sp = rp->r_sp; 771 bzero(rp, sizeof (*rp)); 772 773 rp->r_ss = UDS_SEL; 774 rp->r_sp = sp; 775 rp->r_pc = args->entry; 776 rp->r_ps = PSL_USER; 777 778 #if defined(__amd64) 779 780 pcb->pcb_fs = pcb->pcb_gs = 0; 781 pcb->pcb_fsbase = pcb->pcb_gsbase = 0; 782 783 if (ttoproc(t)->p_model == DATAMODEL_NATIVE) { 784 785 rp->r_cs = UCS_SEL; 786 787 /* 788 * Only allow 64-bit user code descriptor to be present. 789 */ 790 gdt_ucode_model(DATAMODEL_NATIVE); 791 792 /* 793 * Arrange that the virtualized %fs and %gs GDT descriptors 794 * have a well-defined initial state (present, ring 3 795 * and of type data). 796 */ 797 pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc; 798 799 /* 800 * thrptr is either NULL or a value used by DTrace. 801 * 64-bit processes use %fs as their "thread" register. 802 */ 803 if (args->thrptr) 804 (void) lwp_setprivate(lwp, _LWP_FSBASE, args->thrptr); 805 806 } else { 807 808 rp->r_cs = U32CS_SEL; 809 rp->r_ds = rp->r_es = UDS_SEL; 810 811 /* 812 * only allow 32-bit user code selector to be present. 813 */ 814 gdt_ucode_model(DATAMODEL_ILP32); 815 816 pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc; 817 818 /* 819 * thrptr is either NULL or a value used by DTrace. 820 * 32-bit processes use %gs as their "thread" register. 821 */ 822 if (args->thrptr) 823 (void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr); 824 825 } 826 827 pcb->pcb_ds = rp->r_ds; 828 pcb->pcb_es = rp->r_es; 829 pcb->pcb_flags |= RUPDATE_PENDING; 830 831 #elif defined(__i386) 832 833 rp->r_cs = UCS_SEL; 834 rp->r_ds = rp->r_es = UDS_SEL; 835 836 /* 837 * Arrange that the virtualized %fs and %gs GDT descriptors 838 * have a well-defined initial state (present, ring 3 839 * and of type data). 840 */ 841 pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc; 842 843 /* 844 * For %gs we need to reset LWP_GSBASE in pcb and the 845 * per-cpu GDT descriptor. thrptr is either NULL 846 * or a value used by DTrace. 847 */ 848 if (args->thrptr) 849 (void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr); 850 #endif 851 852 lwp->lwp_eosys = JUSTRETURN; 853 t->t_post_sys = 1; 854 855 /* 856 * Here we initialize minimal fpu state. 857 * The rest is done at the first floating 858 * point instruction that a process executes. 859 */ 860 pcb->pcb_fpu.fpu_flags = 0; 861 862 /* 863 * Add the lwp context handlers that virtualize segment registers, 864 * and/or system call stacks etc. 865 */ 866 lwp_installctx(lwp); 867 } 868 869 user_desc_t * 870 cpu_get_gdt(void) 871 { 872 return (CPU->cpu_gdt); 873 } 874 875 876 #if !defined(lwp_getdatamodel) 877 878 /* 879 * Return the datamodel of the given lwp. 880 */ 881 /*ARGSUSED*/ 882 model_t 883 lwp_getdatamodel(klwp_t *lwp) 884 { 885 return (lwp->lwp_procp->p_model); 886 } 887 888 #endif /* !lwp_getdatamodel */ 889 890 #if !defined(get_udatamodel) 891 892 model_t 893 get_udatamodel(void) 894 { 895 return (curproc->p_model); 896 } 897 898 #endif /* !get_udatamodel */ 899