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 59acbbeafSnn35248 * Common Development and Distribution License (the "License"). 69acbbeafSnn35248 * 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*7417cfdeSKuriakose Kuruvilla * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/param.h> 317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 327c478bd9Sstevel@tonic-gate #include <sys/signal.h> 337c478bd9Sstevel@tonic-gate #include <sys/systm.h> 347c478bd9Sstevel@tonic-gate #include <sys/user.h> 357c478bd9Sstevel@tonic-gate #include <sys/mman.h> 367c478bd9Sstevel@tonic-gate #include <sys/class.h> 377c478bd9Sstevel@tonic-gate #include <sys/proc.h> 387c478bd9Sstevel@tonic-gate #include <sys/procfs.h> 397c478bd9Sstevel@tonic-gate #include <sys/buf.h> 407c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 417c478bd9Sstevel@tonic-gate #include <sys/cred.h> 427c478bd9Sstevel@tonic-gate #include <sys/archsystm.h> 437c478bd9Sstevel@tonic-gate #include <sys/vmparam.h> 447c478bd9Sstevel@tonic-gate #include <sys/prsystm.h> 457c478bd9Sstevel@tonic-gate #include <sys/reboot.h> 467c478bd9Sstevel@tonic-gate #include <sys/uadmin.h> 477c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 487c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 497c478bd9Sstevel@tonic-gate #include <sys/file.h> 507c478bd9Sstevel@tonic-gate #include <sys/session.h> 517c478bd9Sstevel@tonic-gate #include <sys/ucontext.h> 527c478bd9Sstevel@tonic-gate #include <sys/dnlc.h> 537c478bd9Sstevel@tonic-gate #include <sys/var.h> 547c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 557c478bd9Sstevel@tonic-gate #include <sys/debugreg.h> 567c478bd9Sstevel@tonic-gate #include <sys/thread.h> 577c478bd9Sstevel@tonic-gate #include <sys/vtrace.h> 587c478bd9Sstevel@tonic-gate #include <sys/consdev.h> 597c478bd9Sstevel@tonic-gate #include <sys/psw.h> 607c478bd9Sstevel@tonic-gate #include <sys/regset.h> 617c478bd9Sstevel@tonic-gate #include <sys/privregs.h> 626e541dddSmrj #include <sys/cpu.h> 637c478bd9Sstevel@tonic-gate #include <sys/stack.h> 647c478bd9Sstevel@tonic-gate #include <sys/swap.h> 657c478bd9Sstevel@tonic-gate #include <vm/hat.h> 667c478bd9Sstevel@tonic-gate #include <vm/anon.h> 677c478bd9Sstevel@tonic-gate #include <vm/as.h> 687c478bd9Sstevel@tonic-gate #include <vm/page.h> 697c478bd9Sstevel@tonic-gate #include <vm/seg.h> 707c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h> 717c478bd9Sstevel@tonic-gate #include <vm/seg_map.h> 727c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h> 737c478bd9Sstevel@tonic-gate #include <sys/exec.h> 747c478bd9Sstevel@tonic-gate #include <sys/acct.h> 757c478bd9Sstevel@tonic-gate #include <sys/core.h> 767c478bd9Sstevel@tonic-gate #include <sys/corectl.h> 777c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 787c478bd9Sstevel@tonic-gate #include <sys/tuneable.h> 797c478bd9Sstevel@tonic-gate #include <c2/audit.h> 807c478bd9Sstevel@tonic-gate #include <sys/bootconf.h> 819acbbeafSnn35248 #include <sys/brand.h> 827c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h> 837c478bd9Sstevel@tonic-gate #include <sys/promif.h> 847c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 857c478bd9Sstevel@tonic-gate #include <sys/kdi.h> 867c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h> 877c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h> 887c478bd9Sstevel@tonic-gate #include <sys/segments.h> 89ae115bc7Smrj #include <sys/ontrap.h> 90843e1988Sjohnlev #include <sys/cpu.h> 91843e1988Sjohnlev #ifdef __xpv 92843e1988Sjohnlev #include <sys/hypervisor.h> 93843e1988Sjohnlev #endif 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * Compare the version of boot that boot says it is against 977c478bd9Sstevel@tonic-gate * the version of boot the kernel expects. 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate int 1007c478bd9Sstevel@tonic-gate check_boot_version(int boots_version) 1017c478bd9Sstevel@tonic-gate { 1027c478bd9Sstevel@tonic-gate if (boots_version == BO_VERSION) 1037c478bd9Sstevel@tonic-gate return (0); 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate prom_printf("Wrong boot interface - kernel needs v%d found v%d\n", 1067c478bd9Sstevel@tonic-gate BO_VERSION, boots_version); 1077c478bd9Sstevel@tonic-gate prom_panic("halting"); 1087c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate /* 1127c478bd9Sstevel@tonic-gate * Process the physical installed list for boot. 1137c478bd9Sstevel@tonic-gate * Finds: 1147c478bd9Sstevel@tonic-gate * 1) the pfn of the highest installed physical page, 1157c478bd9Sstevel@tonic-gate * 2) the number of pages installed 1167c478bd9Sstevel@tonic-gate * 3) the number of distinct contiguous regions these pages fall into. 117a3114836SGerry Liu * 4) the number of contiguous memory ranges 1187c478bd9Sstevel@tonic-gate */ 1197c478bd9Sstevel@tonic-gate void 120a3114836SGerry Liu installed_top_size_ex( 1217c478bd9Sstevel@tonic-gate struct memlist *list, /* pointer to start of installed list */ 1227c478bd9Sstevel@tonic-gate pfn_t *high_pfn, /* return ptr for top value */ 1237c478bd9Sstevel@tonic-gate pgcnt_t *pgcnt, /* return ptr for sum of installed pages */ 1247c478bd9Sstevel@tonic-gate int *ranges) /* return ptr for the count of contig. ranges */ 1257c478bd9Sstevel@tonic-gate { 1267c478bd9Sstevel@tonic-gate pfn_t top = 0; 1277c478bd9Sstevel@tonic-gate pgcnt_t sumpages = 0; 1287c478bd9Sstevel@tonic-gate pfn_t highp; /* high page in a chunk */ 1297c478bd9Sstevel@tonic-gate int cnt = 0; 1307c478bd9Sstevel@tonic-gate 13156f33205SJonathan Adams for (; list; list = list->ml_next) { 1327c478bd9Sstevel@tonic-gate ++cnt; 13356f33205SJonathan Adams highp = (list->ml_address + list->ml_size - 1) >> PAGESHIFT; 1347c478bd9Sstevel@tonic-gate if (top < highp) 1357c478bd9Sstevel@tonic-gate top = highp; 13656f33205SJonathan Adams sumpages += btop(list->ml_size); 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate *high_pfn = top; 1407c478bd9Sstevel@tonic-gate *pgcnt = sumpages; 1417c478bd9Sstevel@tonic-gate *ranges = cnt; 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 144a3114836SGerry Liu void 145a3114836SGerry Liu installed_top_size( 146a3114836SGerry Liu struct memlist *list, /* pointer to start of installed list */ 147a3114836SGerry Liu pfn_t *high_pfn, /* return ptr for top value */ 148a3114836SGerry Liu pgcnt_t *pgcnt) /* return ptr for sum of installed pages */ 149a3114836SGerry Liu { 150a3114836SGerry Liu int ranges; 151a3114836SGerry Liu 152a3114836SGerry Liu installed_top_size_ex(list, high_pfn, pgcnt, &ranges); 153a3114836SGerry Liu } 154a3114836SGerry Liu 155a3114836SGerry Liu void 156a3114836SGerry Liu phys_install_has_changed(void) 157a3114836SGerry Liu {} 158a3114836SGerry Liu 1597c478bd9Sstevel@tonic-gate /* 1607c478bd9Sstevel@tonic-gate * Copy in a memory list from boot to kernel, with a filter function 1617c478bd9Sstevel@tonic-gate * to remove pages. The filter function can increase the address and/or 162607702b6Smec * decrease the size to filter out pages. It will also align addresses and 163607702b6Smec * sizes to PAGESIZE. 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate void 1667c478bd9Sstevel@tonic-gate copy_memlist_filter( 1677c478bd9Sstevel@tonic-gate struct memlist *src, 1687c478bd9Sstevel@tonic-gate struct memlist **dstp, 1697c478bd9Sstevel@tonic-gate void (*filter)(uint64_t *, uint64_t *)) 1707c478bd9Sstevel@tonic-gate { 1717c478bd9Sstevel@tonic-gate struct memlist *dst, *prev; 1727c478bd9Sstevel@tonic-gate uint64_t addr; 1737c478bd9Sstevel@tonic-gate uint64_t size; 1747c478bd9Sstevel@tonic-gate uint64_t eaddr; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate dst = *dstp; 1777c478bd9Sstevel@tonic-gate prev = dst; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate /* 1807c478bd9Sstevel@tonic-gate * Move through the memlist applying a filter against 1817c478bd9Sstevel@tonic-gate * each range of memory. Note that we may apply the 1827c478bd9Sstevel@tonic-gate * filter multiple times against each memlist entry. 1837c478bd9Sstevel@tonic-gate */ 18456f33205SJonathan Adams for (; src; src = src->ml_next) { 18556f33205SJonathan Adams addr = P2ROUNDUP(src->ml_address, PAGESIZE); 18656f33205SJonathan Adams eaddr = P2ALIGN(src->ml_address + src->ml_size, PAGESIZE); 1877c478bd9Sstevel@tonic-gate while (addr < eaddr) { 1887c478bd9Sstevel@tonic-gate size = eaddr - addr; 1897c478bd9Sstevel@tonic-gate if (filter != NULL) 1907c478bd9Sstevel@tonic-gate filter(&addr, &size); 1917c478bd9Sstevel@tonic-gate if (size == 0) 1927c478bd9Sstevel@tonic-gate break; 19356f33205SJonathan Adams dst->ml_address = addr; 19456f33205SJonathan Adams dst->ml_size = size; 19556f33205SJonathan Adams dst->ml_next = 0; 1967c478bd9Sstevel@tonic-gate if (prev == dst) { 19756f33205SJonathan Adams dst->ml_prev = 0; 1987c478bd9Sstevel@tonic-gate dst++; 1997c478bd9Sstevel@tonic-gate } else { 20056f33205SJonathan Adams dst->ml_prev = prev; 20156f33205SJonathan Adams prev->ml_next = dst; 2027c478bd9Sstevel@tonic-gate dst++; 2037c478bd9Sstevel@tonic-gate prev++; 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate addr += size; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate *dstp = dst; 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate /* 2137c478bd9Sstevel@tonic-gate * Kernel setup code, called from startup(). 2147c478bd9Sstevel@tonic-gate */ 2157c478bd9Sstevel@tonic-gate void 2167c478bd9Sstevel@tonic-gate kern_setup1(void) 2177c478bd9Sstevel@tonic-gate { 2187c478bd9Sstevel@tonic-gate proc_t *pp; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate pp = &p0; 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate proc_sched = pp; 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate /* 2257c478bd9Sstevel@tonic-gate * Initialize process 0 data structures 2267c478bd9Sstevel@tonic-gate */ 2277c478bd9Sstevel@tonic-gate pp->p_stat = SRUN; 2287c478bd9Sstevel@tonic-gate pp->p_flag = SSYS; 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate pp->p_pidp = &pid0; 2317c478bd9Sstevel@tonic-gate pp->p_pgidp = &pid0; 2327c478bd9Sstevel@tonic-gate pp->p_sessp = &session0; 2337c478bd9Sstevel@tonic-gate pp->p_tlist = &t0; 2347c478bd9Sstevel@tonic-gate pid0.pid_pglink = pp; 235e44bd21cSsusans pid0.pid_pgtail = pp; 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate /* 2387c478bd9Sstevel@tonic-gate * XXX - we asssume that the u-area is zeroed out except for 2397c478bd9Sstevel@tonic-gate * ttolwp(curthread)->lwp_regs. 2407c478bd9Sstevel@tonic-gate */ 241ae115bc7Smrj PTOU(curproc)->u_cmask = (mode_t)CMASK; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate thread_init(); /* init thread_free list */ 2447c478bd9Sstevel@tonic-gate pid_init(); /* initialize pid (proc) table */ 2457c478bd9Sstevel@tonic-gate contract_init(); /* initialize contracts */ 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate init_pages_pp_maximum(); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate /* 2517c478bd9Sstevel@tonic-gate * Load a procedure into a thread. 2527c478bd9Sstevel@tonic-gate */ 2537c478bd9Sstevel@tonic-gate void 2547c478bd9Sstevel@tonic-gate thread_load(kthread_t *t, void (*start)(), caddr_t arg, size_t len) 2557c478bd9Sstevel@tonic-gate { 2567c478bd9Sstevel@tonic-gate caddr_t sp; 2577c478bd9Sstevel@tonic-gate size_t framesz; 2587c478bd9Sstevel@tonic-gate caddr_t argp; 2597c478bd9Sstevel@tonic-gate long *p; 2607c478bd9Sstevel@tonic-gate extern void thread_start(); 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate /* 2637c478bd9Sstevel@tonic-gate * Push a "c" call frame onto the stack to represent 2647c478bd9Sstevel@tonic-gate * the caller of "start". 2657c478bd9Sstevel@tonic-gate */ 2667c478bd9Sstevel@tonic-gate sp = t->t_stk; 2677c478bd9Sstevel@tonic-gate ASSERT(((uintptr_t)t->t_stk & (STACK_ENTRY_ALIGN - 1)) == 0); 2687c478bd9Sstevel@tonic-gate if (len != 0) { 2697c478bd9Sstevel@tonic-gate /* 2707c478bd9Sstevel@tonic-gate * the object that arg points at is copied into the 2717c478bd9Sstevel@tonic-gate * caller's frame. 2727c478bd9Sstevel@tonic-gate */ 2737c478bd9Sstevel@tonic-gate framesz = SA(len); 2747c478bd9Sstevel@tonic-gate sp -= framesz; 2757c478bd9Sstevel@tonic-gate ASSERT(sp > t->t_stkbase); 2767c478bd9Sstevel@tonic-gate argp = sp + SA(MINFRAME); 2777c478bd9Sstevel@tonic-gate bcopy(arg, argp, len); 2787c478bd9Sstevel@tonic-gate arg = argp; 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate /* 2817c478bd9Sstevel@tonic-gate * Set up arguments (arg and len) on the caller's stack frame. 2827c478bd9Sstevel@tonic-gate */ 2837c478bd9Sstevel@tonic-gate p = (long *)sp; 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate *--p = 0; /* fake call */ 2867c478bd9Sstevel@tonic-gate *--p = 0; /* null frame pointer terminates stack trace */ 2877c478bd9Sstevel@tonic-gate *--p = (long)len; 2887c478bd9Sstevel@tonic-gate *--p = (intptr_t)arg; 2897c478bd9Sstevel@tonic-gate *--p = (intptr_t)start; 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate /* 2927c478bd9Sstevel@tonic-gate * initialize thread to resume at thread_start() which will 2937c478bd9Sstevel@tonic-gate * turn around and invoke (*start)(arg, len). 2947c478bd9Sstevel@tonic-gate */ 2957c478bd9Sstevel@tonic-gate t->t_pc = (uintptr_t)thread_start; 2967c478bd9Sstevel@tonic-gate t->t_sp = (uintptr_t)p; 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate ASSERT((t->t_sp & (STACK_ENTRY_ALIGN - 1)) == 0); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate /* 3027c478bd9Sstevel@tonic-gate * load user registers into lwp. 3037c478bd9Sstevel@tonic-gate */ 3047c478bd9Sstevel@tonic-gate /*ARGSUSED2*/ 3057c478bd9Sstevel@tonic-gate void 3067c478bd9Sstevel@tonic-gate lwp_load(klwp_t *lwp, gregset_t grp, uintptr_t thrptr) 3077c478bd9Sstevel@tonic-gate { 3087c478bd9Sstevel@tonic-gate struct regs *rp = lwptoregs(lwp); 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate setgregs(lwp, grp); 3117c478bd9Sstevel@tonic-gate rp->r_ps = PSL_USER; 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate /* 314843e1988Sjohnlev * For 64-bit lwps, we allow one magic %fs selector value, and one 315843e1988Sjohnlev * magic %gs selector to point anywhere in the address space using 3167c478bd9Sstevel@tonic-gate * %fsbase and %gsbase behind the scenes. libc uses %fs to point 3177c478bd9Sstevel@tonic-gate * at the ulwp_t structure. 3187c478bd9Sstevel@tonic-gate * 3197c478bd9Sstevel@tonic-gate * For 32-bit lwps, libc wedges its lwp thread pointer into the 3207c478bd9Sstevel@tonic-gate * ucontext ESP slot (which is otherwise irrelevant to setting a 3217c478bd9Sstevel@tonic-gate * ucontext) and LWPGS_SEL value into gregs[REG_GS]. This is so 3227c478bd9Sstevel@tonic-gate * syslwp_create() can atomically setup %gs. 3237c478bd9Sstevel@tonic-gate * 3247c478bd9Sstevel@tonic-gate * See setup_context() in libc. 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 3277c478bd9Sstevel@tonic-gate if (lwp_getdatamodel(lwp) == DATAMODEL_ILP32) { 3287c478bd9Sstevel@tonic-gate if (grp[REG_GS] == LWPGS_SEL) 3297c478bd9Sstevel@tonic-gate (void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr); 330ae115bc7Smrj } else { 331ae115bc7Smrj /* 332ae115bc7Smrj * See lwp_setprivate in kernel and setup_context in libc. 333ae115bc7Smrj * 334ae115bc7Smrj * Currently libc constructs a ucontext from whole cloth for 335ae115bc7Smrj * every new (not main) lwp created. For 64 bit processes 336ae115bc7Smrj * %fsbase is directly set to point to current thread pointer. 337ae115bc7Smrj * In the past (solaris 10) %fs was also set LWPFS_SEL to 338ae115bc7Smrj * indicate %fsbase. Now we use the null GDT selector for 339ae115bc7Smrj * this purpose. LWP[FS|GS]_SEL are only intended for 32 bit 340ae115bc7Smrj * processes. To ease transition we support older libcs in 341ae115bc7Smrj * the newer kernel by forcing %fs or %gs selector to null 342ae115bc7Smrj * by calling lwp_setprivate if LWP[FS|GS]_SEL is passed in 343ae115bc7Smrj * the ucontext. This is should be ripped out at some future 344ae115bc7Smrj * date. Another fix would be for libc to do a getcontext 345ae115bc7Smrj * and inherit the null %fs/%gs from the current context but 346ae115bc7Smrj * that means an extra system call and could hurt performance. 347ae115bc7Smrj */ 348ae115bc7Smrj if (grp[REG_FS] == 0x1bb) /* hard code legacy LWPFS_SEL */ 349ae115bc7Smrj (void) lwp_setprivate(lwp, _LWP_FSBASE, 350ae115bc7Smrj (uintptr_t)grp[REG_FSBASE]); 351ae115bc7Smrj 352ae115bc7Smrj if (grp[REG_GS] == 0x1c3) /* hard code legacy LWPGS_SEL */ 353ae115bc7Smrj (void) lwp_setprivate(lwp, _LWP_GSBASE, 354ae115bc7Smrj (uintptr_t)grp[REG_GSBASE]); 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate #else 3577c478bd9Sstevel@tonic-gate if (grp[GS] == LWPGS_SEL) 3587c478bd9Sstevel@tonic-gate (void) lwp_setprivate(lwp, _LWP_GSBASE, thrptr); 3597c478bd9Sstevel@tonic-gate #endif 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate lwp->lwp_eosys = JUSTRETURN; 3627c478bd9Sstevel@tonic-gate lwptot(lwp)->t_post_sys = 1; 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate /* 3667c478bd9Sstevel@tonic-gate * set syscall()'s return values for a lwp. 3677c478bd9Sstevel@tonic-gate */ 3687c478bd9Sstevel@tonic-gate void 3697c478bd9Sstevel@tonic-gate lwp_setrval(klwp_t *lwp, int v1, int v2) 3707c478bd9Sstevel@tonic-gate { 3717c478bd9Sstevel@tonic-gate lwptoregs(lwp)->r_ps &= ~PS_C; 3727c478bd9Sstevel@tonic-gate lwptoregs(lwp)->r_r0 = v1; 3737c478bd9Sstevel@tonic-gate lwptoregs(lwp)->r_r1 = v2; 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate /* 3777c478bd9Sstevel@tonic-gate * set syscall()'s return values for a lwp. 3787c478bd9Sstevel@tonic-gate */ 3797c478bd9Sstevel@tonic-gate void 3807c478bd9Sstevel@tonic-gate lwp_setsp(klwp_t *lwp, caddr_t sp) 3817c478bd9Sstevel@tonic-gate { 3827c478bd9Sstevel@tonic-gate lwptoregs(lwp)->r_sp = (intptr_t)sp; 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * Copy regs from parent to child. 3877c478bd9Sstevel@tonic-gate */ 3887c478bd9Sstevel@tonic-gate void 3897c478bd9Sstevel@tonic-gate lwp_forkregs(klwp_t *lwp, klwp_t *clwp) 3907c478bd9Sstevel@tonic-gate { 3917c478bd9Sstevel@tonic-gate #if defined(__amd64) 3929acbbeafSnn35248 struct pcb *pcb = &clwp->lwp_pcb; 3939acbbeafSnn35248 struct regs *rp = lwptoregs(lwp); 3949acbbeafSnn35248 3957712e92cSsudheer if (pcb->pcb_rupdate == 0) { 3969acbbeafSnn35248 pcb->pcb_ds = rp->r_ds; 3979acbbeafSnn35248 pcb->pcb_es = rp->r_es; 3989acbbeafSnn35248 pcb->pcb_fs = rp->r_fs; 3999acbbeafSnn35248 pcb->pcb_gs = rp->r_gs; 4007712e92cSsudheer pcb->pcb_rupdate = 1; 4017c478bd9Sstevel@tonic-gate lwptot(clwp)->t_post_sys = 1; 4029acbbeafSnn35248 } 4039acbbeafSnn35248 ASSERT(lwptot(clwp)->t_post_sys); 4047c478bd9Sstevel@tonic-gate #endif 4059acbbeafSnn35248 4067c478bd9Sstevel@tonic-gate bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct regs)); 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate /* 4107c478bd9Sstevel@tonic-gate * This function is currently unused on x86. 4117c478bd9Sstevel@tonic-gate */ 4127c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4137c478bd9Sstevel@tonic-gate void 4147c478bd9Sstevel@tonic-gate lwp_freeregs(klwp_t *lwp, int isexec) 4157c478bd9Sstevel@tonic-gate {} 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate /* 4187c478bd9Sstevel@tonic-gate * This function is currently unused on x86. 4197c478bd9Sstevel@tonic-gate */ 4207c478bd9Sstevel@tonic-gate void 4217c478bd9Sstevel@tonic-gate lwp_pcb_exit(void) 4227c478bd9Sstevel@tonic-gate {} 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate /* 4257c478bd9Sstevel@tonic-gate * Lwp context ops for segment registers. 4267c478bd9Sstevel@tonic-gate */ 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate /* 4297c478bd9Sstevel@tonic-gate * Every time we come into the kernel (syscall, interrupt or trap 4307c478bd9Sstevel@tonic-gate * but not fast-traps) we capture the current values of the user's 4317c478bd9Sstevel@tonic-gate * segment registers into the lwp's reg structure. This includes 4327c478bd9Sstevel@tonic-gate * lcall for i386 generic system call support since it is handled 4337c478bd9Sstevel@tonic-gate * as a segment-not-present trap. 4347c478bd9Sstevel@tonic-gate * 4357c478bd9Sstevel@tonic-gate * Here we save the current values from the lwp regs into the pcb 4367712e92cSsudheer * and set pcb->pcb_rupdate to 1 to tell the rest of the kernel 4377c478bd9Sstevel@tonic-gate * that the pcb copy of the segment registers is the current one. 4387c478bd9Sstevel@tonic-gate * This ensures the lwp's next trip to user land via update_sregs. 4397c478bd9Sstevel@tonic-gate * Finally we set t_post_sys to ensure that no system call fast-path's 4407c478bd9Sstevel@tonic-gate * its way out of the kernel via sysret. 4417c478bd9Sstevel@tonic-gate * 4427c478bd9Sstevel@tonic-gate * (This means that we need to have interrupts disabled when we test 4437c478bd9Sstevel@tonic-gate * t->t_post_sys in the syscall handlers; if the test fails, we need 4447c478bd9Sstevel@tonic-gate * to keep interrupts disabled until we return to userland so we can't 4457c478bd9Sstevel@tonic-gate * be switched away.) 4467c478bd9Sstevel@tonic-gate * 4477c478bd9Sstevel@tonic-gate * As a result of all this, we don't really have to do a whole lot if 4487c478bd9Sstevel@tonic-gate * the thread is just mucking about in the kernel, switching on and 4497c478bd9Sstevel@tonic-gate * off the cpu for whatever reason it feels like. And yet we still 4507c478bd9Sstevel@tonic-gate * preserve fast syscalls, cause if we -don't- get descheduled, 4517c478bd9Sstevel@tonic-gate * we never come here either. 4527c478bd9Sstevel@tonic-gate */ 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate #define VALID_LWP_DESC(udp) ((udp)->usd_type == SDT_MEMRWA && \ 4557c478bd9Sstevel@tonic-gate (udp)->usd_p == 1 && (udp)->usd_dpl == SEL_UPL) 4567c478bd9Sstevel@tonic-gate 457843e1988Sjohnlev /*ARGSUSED*/ 4587c478bd9Sstevel@tonic-gate void 4597c478bd9Sstevel@tonic-gate lwp_segregs_save(klwp_t *lwp) 4607c478bd9Sstevel@tonic-gate { 4617c478bd9Sstevel@tonic-gate #if defined(__amd64) 4627c478bd9Sstevel@tonic-gate pcb_t *pcb = &lwp->lwp_pcb; 4637c478bd9Sstevel@tonic-gate struct regs *rp; 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc)); 4667c478bd9Sstevel@tonic-gate ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc)); 4677c478bd9Sstevel@tonic-gate 4687712e92cSsudheer if (pcb->pcb_rupdate == 0) { 4697c478bd9Sstevel@tonic-gate rp = lwptoregs(lwp); 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate /* 4727c478bd9Sstevel@tonic-gate * If there's no update already pending, capture the current 4737c478bd9Sstevel@tonic-gate * %ds/%es/%fs/%gs values from lwp's regs in case the user 4747c478bd9Sstevel@tonic-gate * changed them; %fsbase and %gsbase are privileged so the 4757c478bd9Sstevel@tonic-gate * kernel versions of these registers in pcb_fsbase and 4767c478bd9Sstevel@tonic-gate * pcb_gsbase are always up-to-date. 4777c478bd9Sstevel@tonic-gate */ 4787c478bd9Sstevel@tonic-gate pcb->pcb_ds = rp->r_ds; 4797c478bd9Sstevel@tonic-gate pcb->pcb_es = rp->r_es; 4807c478bd9Sstevel@tonic-gate pcb->pcb_fs = rp->r_fs; 4817c478bd9Sstevel@tonic-gate pcb->pcb_gs = rp->r_gs; 4827712e92cSsudheer pcb->pcb_rupdate = 1; 4837c478bd9Sstevel@tonic-gate lwp->lwp_thread->t_post_sys = 1; 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate #endif /* __amd64 */ 4867c478bd9Sstevel@tonic-gate 487843e1988Sjohnlev #if !defined(__xpv) /* XXPV not sure if we can re-read gdt? */ 4887c478bd9Sstevel@tonic-gate ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPFS], &lwp->lwp_pcb.pcb_fsdesc, 4897c478bd9Sstevel@tonic-gate sizeof (lwp->lwp_pcb.pcb_fsdesc)) == 0); 4907c478bd9Sstevel@tonic-gate ASSERT(bcmp(&CPU->cpu_gdt[GDT_LWPGS], &lwp->lwp_pcb.pcb_gsdesc, 4917c478bd9Sstevel@tonic-gate sizeof (lwp->lwp_pcb.pcb_gsdesc)) == 0); 492843e1988Sjohnlev #endif 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate #if defined(__amd64) 496ae115bc7Smrj 497ae115bc7Smrj /* 498843e1988Sjohnlev * Update the segment registers with new values from the pcb. 499ae115bc7Smrj * 500ae115bc7Smrj * We have to do this carefully, and in the following order, 501ae115bc7Smrj * in case any of the selectors points at a bogus descriptor. 502ae115bc7Smrj * If they do, we'll catch trap with on_trap and return 1. 503ae115bc7Smrj * returns 0 on success. 504ae115bc7Smrj * 505ae115bc7Smrj * This is particularly tricky for %gs. 506ae115bc7Smrj * This routine must be executed under a cli. 507ae115bc7Smrj */ 508ae115bc7Smrj int 509ae115bc7Smrj update_sregs(struct regs *rp, klwp_t *lwp) 510ae115bc7Smrj { 511ae115bc7Smrj pcb_t *pcb = &lwp->lwp_pcb; 512ae115bc7Smrj ulong_t kgsbase; 513ae115bc7Smrj on_trap_data_t otd; 514ae115bc7Smrj int rc = 0; 515ae115bc7Smrj 516ae115bc7Smrj if (!on_trap(&otd, OT_SEGMENT_ACCESS)) { 517ae115bc7Smrj 518843e1988Sjohnlev #if defined(__xpv) 519843e1988Sjohnlev /* 520843e1988Sjohnlev * On the hyervisor this is easy. The hypercall below will 521843e1988Sjohnlev * swapgs and load %gs with the user selector. If the user 522843e1988Sjohnlev * selector is bad the hypervisor will catch the fault and 523843e1988Sjohnlev * load %gs with the null selector instead. Either way the 524843e1988Sjohnlev * kernel's gsbase is not damaged. 525843e1988Sjohnlev */ 526843e1988Sjohnlev kgsbase = (ulong_t)CPU; 527843e1988Sjohnlev if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, 528843e1988Sjohnlev pcb->pcb_gs) != 0) { 529843e1988Sjohnlev no_trap(); 530843e1988Sjohnlev return (1); 531843e1988Sjohnlev } 532843e1988Sjohnlev 533843e1988Sjohnlev rp->r_gs = pcb->pcb_gs; 534843e1988Sjohnlev ASSERT((cpu_t *)kgsbase == CPU); 535843e1988Sjohnlev 536843e1988Sjohnlev #else /* __xpv */ 537843e1988Sjohnlev 538843e1988Sjohnlev /* 539843e1988Sjohnlev * A little more complicated running native. 540843e1988Sjohnlev */ 541ae115bc7Smrj kgsbase = (ulong_t)CPU; 542ae115bc7Smrj __set_gs(pcb->pcb_gs); 543ae115bc7Smrj 544ae115bc7Smrj /* 545ae115bc7Smrj * If __set_gs fails it's because the new %gs is a bad %gs, 546ae115bc7Smrj * we'll be taking a trap but with the original %gs and %gsbase 547ae115bc7Smrj * undamaged (i.e. pointing at curcpu). 548ae115bc7Smrj * 549ae115bc7Smrj * We've just mucked up the kernel's gsbase. Oops. In 550ae115bc7Smrj * particular we can't take any traps at all. Make the newly 551ae115bc7Smrj * computed gsbase be the hidden gs via __swapgs, and fix 552ae115bc7Smrj * the kernel's gsbase back again. Later, when we return to 553ae115bc7Smrj * userland we'll swapgs again restoring gsbase just loaded 554ae115bc7Smrj * above. 555ae115bc7Smrj */ 556ae115bc7Smrj __swapgs(); 557ae115bc7Smrj rp->r_gs = pcb->pcb_gs; 558ae115bc7Smrj 559ae115bc7Smrj /* 560ae115bc7Smrj * restore kernel's gsbase 561ae115bc7Smrj */ 562ae115bc7Smrj wrmsr(MSR_AMD_GSBASE, kgsbase); 563ae115bc7Smrj 564843e1988Sjohnlev #endif /* __xpv */ 565843e1988Sjohnlev 566ae115bc7Smrj /* 567ae115bc7Smrj * Only override the descriptor base address if 568ae115bc7Smrj * r_gs == LWPGS_SEL or if r_gs == NULL. A note on 569ae115bc7Smrj * NULL descriptors -- 32-bit programs take faults 570ae115bc7Smrj * if they deference NULL descriptors; however, 571ae115bc7Smrj * when 64-bit programs load them into %fs or %gs, 572ae115bc7Smrj * they DONT fault -- only the base address remains 573ae115bc7Smrj * whatever it was from the last load. Urk. 574ae115bc7Smrj * 575ae115bc7Smrj * XXX - note that lwp_setprivate now sets %fs/%gs to the 576ae115bc7Smrj * null selector for 64 bit processes. Whereas before 577ae115bc7Smrj * %fs/%gs were set to LWP(FS|GS)_SEL regardless of 578ae115bc7Smrj * the process's data model. For now we check for both 579ae115bc7Smrj * values so that the kernel can also support the older 580ae115bc7Smrj * libc. This should be ripped out at some point in the 581ae115bc7Smrj * future. 582ae115bc7Smrj */ 583843e1988Sjohnlev if (pcb->pcb_gs == LWPGS_SEL || pcb->pcb_gs == 0) { 584843e1988Sjohnlev #if defined(__xpv) 585843e1988Sjohnlev if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER, 586843e1988Sjohnlev pcb->pcb_gsbase)) { 587843e1988Sjohnlev no_trap(); 588843e1988Sjohnlev return (1); 589843e1988Sjohnlev } 590843e1988Sjohnlev #else 591ae115bc7Smrj wrmsr(MSR_AMD_KGSBASE, pcb->pcb_gsbase); 592843e1988Sjohnlev #endif 593843e1988Sjohnlev } 594ae115bc7Smrj 595ae115bc7Smrj __set_ds(pcb->pcb_ds); 596ae115bc7Smrj rp->r_ds = pcb->pcb_ds; 597ae115bc7Smrj 598ae115bc7Smrj __set_es(pcb->pcb_es); 599ae115bc7Smrj rp->r_es = pcb->pcb_es; 600ae115bc7Smrj 601ae115bc7Smrj __set_fs(pcb->pcb_fs); 602ae115bc7Smrj rp->r_fs = pcb->pcb_fs; 603ae115bc7Smrj 604ae115bc7Smrj /* 605ae115bc7Smrj * Same as for %gs 606ae115bc7Smrj */ 607843e1988Sjohnlev if (pcb->pcb_fs == LWPFS_SEL || pcb->pcb_fs == 0) { 608843e1988Sjohnlev #if defined(__xpv) 609843e1988Sjohnlev if (HYPERVISOR_set_segment_base(SEGBASE_FS, 610843e1988Sjohnlev pcb->pcb_fsbase)) { 611843e1988Sjohnlev no_trap(); 612843e1988Sjohnlev return (1); 613843e1988Sjohnlev } 614843e1988Sjohnlev #else 615ae115bc7Smrj wrmsr(MSR_AMD_FSBASE, pcb->pcb_fsbase); 616843e1988Sjohnlev #endif 617843e1988Sjohnlev } 618ae115bc7Smrj 619ae115bc7Smrj } else { 620ae115bc7Smrj cli(); 621ae115bc7Smrj rc = 1; 622ae115bc7Smrj } 623ae115bc7Smrj no_trap(); 624ae115bc7Smrj return (rc); 625ae115bc7Smrj } 626843e1988Sjohnlev 627843e1988Sjohnlev /* 628843e1988Sjohnlev * Make sure any stale selectors are cleared from the segment registers 629843e1988Sjohnlev * by putting KDS_SEL (the kernel's default %ds gdt selector) into them. 630843e1988Sjohnlev * This is necessary because the kernel itself does not use %es, %fs, nor 631843e1988Sjohnlev * %ds. (%cs and %ss are necessary, and are set up by the kernel - along with 632843e1988Sjohnlev * %gs - to point to the current cpu struct.) If we enter kmdb while in the 633843e1988Sjohnlev * kernel and resume with a stale ldt or brandz selector sitting there in a 634843e1988Sjohnlev * segment register, kmdb will #gp fault if the stale selector points to, 635843e1988Sjohnlev * for example, an ldt in the context of another process. 636843e1988Sjohnlev * 637843e1988Sjohnlev * WARNING: Intel and AMD chips behave differently when storing 638843e1988Sjohnlev * the null selector into %fs and %gs while in long mode. On AMD 639843e1988Sjohnlev * chips fsbase and gsbase are not cleared. But on Intel chips, storing 640843e1988Sjohnlev * a null selector into %fs or %gs has the side effect of clearing 641843e1988Sjohnlev * fsbase or gsbase. For that reason we use KDS_SEL, which has 642843e1988Sjohnlev * consistent behavor between AMD and Intel. 643843e1988Sjohnlev * 644843e1988Sjohnlev * Caller responsible for preventing cpu migration. 645843e1988Sjohnlev */ 646843e1988Sjohnlev void 647843e1988Sjohnlev reset_sregs(void) 648843e1988Sjohnlev { 649843e1988Sjohnlev ulong_t kgsbase = (ulong_t)CPU; 650843e1988Sjohnlev 651843e1988Sjohnlev ASSERT(curthread->t_preempt != 0 || getpil() >= DISP_LEVEL); 652843e1988Sjohnlev 653843e1988Sjohnlev cli(); 654843e1988Sjohnlev __set_gs(KGS_SEL); 655843e1988Sjohnlev 656843e1988Sjohnlev /* 657843e1988Sjohnlev * restore kernel gsbase 658843e1988Sjohnlev */ 659843e1988Sjohnlev #if defined(__xpv) 660843e1988Sjohnlev xen_set_segment_base(SEGBASE_GS_KERNEL, kgsbase); 661843e1988Sjohnlev #else 662843e1988Sjohnlev wrmsr(MSR_AMD_GSBASE, kgsbase); 663843e1988Sjohnlev #endif 664843e1988Sjohnlev 665843e1988Sjohnlev sti(); 666843e1988Sjohnlev 667843e1988Sjohnlev __set_ds(KDS_SEL); 668843e1988Sjohnlev __set_es(0 | SEL_KPL); /* selector RPL not ring 0 on hypervisor */ 669843e1988Sjohnlev __set_fs(KFS_SEL); 670843e1988Sjohnlev } 671843e1988Sjohnlev 672ae115bc7Smrj #endif /* __amd64 */ 673ae115bc7Smrj 674ae115bc7Smrj #ifdef _SYSCALL32_IMPL 675ae115bc7Smrj 6767c478bd9Sstevel@tonic-gate /* 6777c478bd9Sstevel@tonic-gate * Make it impossible for a process to change its data model. 6787c478bd9Sstevel@tonic-gate * We do this by toggling the present bits for the 32 and 6797c478bd9Sstevel@tonic-gate * 64-bit user code descriptors. That way if a user lwp attempts 6807c478bd9Sstevel@tonic-gate * to change its data model (by using the wrong code descriptor in 6817c478bd9Sstevel@tonic-gate * %cs) it will fault immediately. This also allows us to simplify 6827c478bd9Sstevel@tonic-gate * assertions and checks in the kernel. 6837c478bd9Sstevel@tonic-gate */ 684843e1988Sjohnlev 685ae115bc7Smrj static void 686ae115bc7Smrj gdt_ucode_model(model_t model) 687ae115bc7Smrj { 688ae115bc7Smrj kpreempt_disable(); 689ae115bc7Smrj if (model == DATAMODEL_NATIVE) { 690843e1988Sjohnlev gdt_update_usegd(GDT_UCODE, &ucs_on); 691843e1988Sjohnlev gdt_update_usegd(GDT_U32CODE, &ucs32_off); 692ae115bc7Smrj } else { 693843e1988Sjohnlev gdt_update_usegd(GDT_U32CODE, &ucs32_on); 694843e1988Sjohnlev gdt_update_usegd(GDT_UCODE, &ucs_off); 695ae115bc7Smrj } 696ae115bc7Smrj kpreempt_enable(); 697ae115bc7Smrj } 698ae115bc7Smrj 699ae115bc7Smrj #endif /* _SYSCALL32_IMPL */ 700ae115bc7Smrj 701ae115bc7Smrj /* 702ae115bc7Smrj * Restore lwp private fs and gs segment descriptors 703ae115bc7Smrj * on current cpu's GDT. 704ae115bc7Smrj */ 705ae115bc7Smrj static void 706ae115bc7Smrj lwp_segregs_restore(klwp_t *lwp) 707ae115bc7Smrj { 708ae115bc7Smrj pcb_t *pcb = &lwp->lwp_pcb; 709ae115bc7Smrj 710ae115bc7Smrj ASSERT(VALID_LWP_DESC(&pcb->pcb_fsdesc)); 711ae115bc7Smrj ASSERT(VALID_LWP_DESC(&pcb->pcb_gsdesc)); 712ae115bc7Smrj 713ae115bc7Smrj #ifdef _SYSCALL32_IMPL 714ae115bc7Smrj gdt_ucode_model(DATAMODEL_NATIVE); 715ae115bc7Smrj #endif 716ae115bc7Smrj 717843e1988Sjohnlev gdt_update_usegd(GDT_LWPFS, &pcb->pcb_fsdesc); 718843e1988Sjohnlev gdt_update_usegd(GDT_LWPGS, &pcb->pcb_gsdesc); 719ae115bc7Smrj 7207c478bd9Sstevel@tonic-gate } 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate static void 7257c478bd9Sstevel@tonic-gate lwp_segregs_restore32(klwp_t *lwp) 7267c478bd9Sstevel@tonic-gate { 727ae115bc7Smrj /*LINTED*/ 7287c478bd9Sstevel@tonic-gate cpu_t *cpu = CPU; 7297c478bd9Sstevel@tonic-gate pcb_t *pcb = &lwp->lwp_pcb; 7307c478bd9Sstevel@tonic-gate 731ae115bc7Smrj ASSERT(VALID_LWP_DESC(&lwp->lwp_pcb.pcb_fsdesc)); 732ae115bc7Smrj ASSERT(VALID_LWP_DESC(&lwp->lwp_pcb.pcb_gsdesc)); 7337c478bd9Sstevel@tonic-gate 734ae115bc7Smrj gdt_ucode_model(DATAMODEL_ILP32); 735843e1988Sjohnlev gdt_update_usegd(GDT_LWPFS, &pcb->pcb_fsdesc); 736843e1988Sjohnlev gdt_update_usegd(GDT_LWPGS, &pcb->pcb_gsdesc); 7377c478bd9Sstevel@tonic-gate } 7387c478bd9Sstevel@tonic-gate 7397c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate /* 7429acbbeafSnn35248 * If this is a process in a branded zone, then we want it to use the brand 7439acbbeafSnn35248 * syscall entry points instead of the standard Solaris entry points. This 7449acbbeafSnn35248 * routine must be called when a new lwp is created within a branded zone 7459acbbeafSnn35248 * or when an existing lwp moves into a branded zone via a zone_enter() 7469acbbeafSnn35248 * operation. 7479acbbeafSnn35248 */ 7489acbbeafSnn35248 void 7499acbbeafSnn35248 lwp_attach_brand_hdlrs(klwp_t *lwp) 7509acbbeafSnn35248 { 7519acbbeafSnn35248 kthread_t *t = lwptot(lwp); 7529acbbeafSnn35248 7539acbbeafSnn35248 ASSERT(PROC_IS_BRANDED(lwptoproc(lwp))); 754fd9e7635Sedp 7559acbbeafSnn35248 ASSERT(removectx(t, NULL, brand_interpositioning_disable, 75612afeb84Ssp92102 brand_interpositioning_enable, NULL, NULL, 75712afeb84Ssp92102 brand_interpositioning_disable, NULL) == 0); 7589acbbeafSnn35248 installctx(t, NULL, brand_interpositioning_disable, 75912afeb84Ssp92102 brand_interpositioning_enable, NULL, NULL, 76012afeb84Ssp92102 brand_interpositioning_disable, NULL); 7619acbbeafSnn35248 7629acbbeafSnn35248 if (t == curthread) { 7639acbbeafSnn35248 kpreempt_disable(); 7649acbbeafSnn35248 brand_interpositioning_enable(); 7659acbbeafSnn35248 kpreempt_enable(); 7669acbbeafSnn35248 } 7679acbbeafSnn35248 } 7689acbbeafSnn35248 7699acbbeafSnn35248 /* 770fd9e7635Sedp * If this is a process in a branded zone, then we want it to disable the 771fd9e7635Sedp * brand syscall entry points. This routine must be called when the last 772fd9e7635Sedp * lwp in a process is exiting in proc_exit(). 773fd9e7635Sedp */ 774fd9e7635Sedp void 775fd9e7635Sedp lwp_detach_brand_hdlrs(klwp_t *lwp) 776fd9e7635Sedp { 777fd9e7635Sedp kthread_t *t = lwptot(lwp); 778fd9e7635Sedp 779fd9e7635Sedp ASSERT(PROC_IS_BRANDED(lwptoproc(lwp))); 780fd9e7635Sedp if (t == curthread) 781fd9e7635Sedp kpreempt_disable(); 782fd9e7635Sedp 783fd9e7635Sedp /* Remove the original context handlers */ 784fd9e7635Sedp VERIFY(removectx(t, NULL, brand_interpositioning_disable, 785fd9e7635Sedp brand_interpositioning_enable, NULL, NULL, 786fd9e7635Sedp brand_interpositioning_disable, NULL) != 0); 787fd9e7635Sedp 788fd9e7635Sedp if (t == curthread) { 789fd9e7635Sedp /* Cleanup our MSR and IDT entries. */ 790fd9e7635Sedp brand_interpositioning_disable(); 791fd9e7635Sedp kpreempt_enable(); 792fd9e7635Sedp } 793fd9e7635Sedp } 794fd9e7635Sedp 795fd9e7635Sedp /* 7967c478bd9Sstevel@tonic-gate * Add any lwp-associated context handlers to the lwp at the beginning 7977c478bd9Sstevel@tonic-gate * of the lwp's useful life. 7987c478bd9Sstevel@tonic-gate * 7997c478bd9Sstevel@tonic-gate * All paths which create lwp's invoke lwp_create(); lwp_create() 8007c478bd9Sstevel@tonic-gate * invokes lwp_stk_init() which initializes the stack, sets up 8017c478bd9Sstevel@tonic-gate * lwp_regs, and invokes this routine. 8027c478bd9Sstevel@tonic-gate * 8037c478bd9Sstevel@tonic-gate * All paths which destroy lwp's invoke lwp_exit() to rip the lwp 8047c478bd9Sstevel@tonic-gate * apart and put it on 'lwp_deathrow'; if the lwp is destroyed it 8057c478bd9Sstevel@tonic-gate * ends up in thread_free() which invokes freectx(t, 0) before 8067c478bd9Sstevel@tonic-gate * invoking lwp_stk_fini(). When the lwp is recycled from death 8077c478bd9Sstevel@tonic-gate * row, lwp_stk_fini() is invoked, then thread_free(), and thus 8087c478bd9Sstevel@tonic-gate * freectx(t, 0) as before. 8097c478bd9Sstevel@tonic-gate * 8107c478bd9Sstevel@tonic-gate * In the case of exec, the surviving lwp is thoroughly scrubbed 8117c478bd9Sstevel@tonic-gate * clean; exec invokes freectx(t, 1) to destroy associated contexts. 8127c478bd9Sstevel@tonic-gate * On the way back to the new image, it invokes setregs() which 8137c478bd9Sstevel@tonic-gate * in turn invokes this routine. 8147c478bd9Sstevel@tonic-gate */ 8157c478bd9Sstevel@tonic-gate void 8167c478bd9Sstevel@tonic-gate lwp_installctx(klwp_t *lwp) 8177c478bd9Sstevel@tonic-gate { 8187c478bd9Sstevel@tonic-gate kthread_t *t = lwptot(lwp); 8197c478bd9Sstevel@tonic-gate int thisthread = t == curthread; 8207c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 8217c478bd9Sstevel@tonic-gate void (*restop)(klwp_t *) = lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ? 8227c478bd9Sstevel@tonic-gate lwp_segregs_restore : lwp_segregs_restore32; 8237c478bd9Sstevel@tonic-gate #else 8247c478bd9Sstevel@tonic-gate void (*restop)(klwp_t *) = lwp_segregs_restore; 8257c478bd9Sstevel@tonic-gate #endif 8267c478bd9Sstevel@tonic-gate 8277c478bd9Sstevel@tonic-gate /* 8287c478bd9Sstevel@tonic-gate * Install the basic lwp context handlers on each lwp. 8297c478bd9Sstevel@tonic-gate * 8307c478bd9Sstevel@tonic-gate * On the amd64 kernel, the context handlers are responsible for 8317c478bd9Sstevel@tonic-gate * virtualizing %ds, %es, %fs, and %gs to the lwp. The register 8327c478bd9Sstevel@tonic-gate * values are only ever changed via sys_rtt when the 8337712e92cSsudheer * pcb->pcb_rupdate == 1. Only sys_rtt gets to clear the bit. 8347c478bd9Sstevel@tonic-gate * 8357c478bd9Sstevel@tonic-gate * On the i386 kernel, the context handlers are responsible for 8367c478bd9Sstevel@tonic-gate * virtualizing %gs/%fs to the lwp by updating the per-cpu GDTs 8377c478bd9Sstevel@tonic-gate */ 8387c478bd9Sstevel@tonic-gate ASSERT(removectx(t, lwp, lwp_segregs_save, restop, 8397c478bd9Sstevel@tonic-gate NULL, NULL, NULL, NULL) == 0); 8407c478bd9Sstevel@tonic-gate if (thisthread) 8417c478bd9Sstevel@tonic-gate kpreempt_disable(); 8427c478bd9Sstevel@tonic-gate installctx(t, lwp, lwp_segregs_save, restop, 8437c478bd9Sstevel@tonic-gate NULL, NULL, NULL, NULL); 8447c478bd9Sstevel@tonic-gate if (thisthread) { 8457c478bd9Sstevel@tonic-gate /* 8467c478bd9Sstevel@tonic-gate * Since we're the right thread, set the values in the GDT 8477c478bd9Sstevel@tonic-gate */ 8487c478bd9Sstevel@tonic-gate restop(lwp); 8497c478bd9Sstevel@tonic-gate kpreempt_enable(); 8507c478bd9Sstevel@tonic-gate } 8517c478bd9Sstevel@tonic-gate 8527c478bd9Sstevel@tonic-gate /* 8537c478bd9Sstevel@tonic-gate * If we have sysenter/sysexit instructions enabled, we need 8547c478bd9Sstevel@tonic-gate * to ensure that the hardware mechanism is kept up-to-date with the 8557c478bd9Sstevel@tonic-gate * lwp's kernel stack pointer across context switches. 8567c478bd9Sstevel@tonic-gate * 8577c478bd9Sstevel@tonic-gate * sep_save zeros the sysenter stack pointer msr; sep_restore sets 8587c478bd9Sstevel@tonic-gate * it to the lwp's kernel stack pointer (kstktop). 8597c478bd9Sstevel@tonic-gate */ 860*7417cfdeSKuriakose Kuruvilla if (is_x86_feature(x86_featureset, X86FSET_SEP)) { 8617c478bd9Sstevel@tonic-gate #if defined(__amd64) 8627c478bd9Sstevel@tonic-gate caddr_t kstktop = (caddr_t)lwp->lwp_regs; 8637c478bd9Sstevel@tonic-gate #elif defined(__i386) 8647c478bd9Sstevel@tonic-gate caddr_t kstktop = ((caddr_t)lwp->lwp_regs - MINFRAME) + 8657c478bd9Sstevel@tonic-gate SA(sizeof (struct regs) + MINFRAME); 8667c478bd9Sstevel@tonic-gate #endif 8677c478bd9Sstevel@tonic-gate ASSERT(removectx(t, kstktop, 8687c478bd9Sstevel@tonic-gate sep_save, sep_restore, NULL, NULL, NULL, NULL) == 0); 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate if (thisthread) 8717c478bd9Sstevel@tonic-gate kpreempt_disable(); 8727c478bd9Sstevel@tonic-gate installctx(t, kstktop, 8737c478bd9Sstevel@tonic-gate sep_save, sep_restore, NULL, NULL, NULL, NULL); 8747c478bd9Sstevel@tonic-gate if (thisthread) { 8757c478bd9Sstevel@tonic-gate /* 8767c478bd9Sstevel@tonic-gate * We're the right thread, so set the stack pointer 8777c478bd9Sstevel@tonic-gate * for the first sysenter instruction to use 8787c478bd9Sstevel@tonic-gate */ 8797c478bd9Sstevel@tonic-gate sep_restore(kstktop); 8807c478bd9Sstevel@tonic-gate kpreempt_enable(); 8817c478bd9Sstevel@tonic-gate } 8827c478bd9Sstevel@tonic-gate } 8839acbbeafSnn35248 8849acbbeafSnn35248 if (PROC_IS_BRANDED(ttoproc(t))) 8859acbbeafSnn35248 lwp_attach_brand_hdlrs(lwp); 8867c478bd9Sstevel@tonic-gate } 8877c478bd9Sstevel@tonic-gate 8887c478bd9Sstevel@tonic-gate /* 8897c478bd9Sstevel@tonic-gate * Clear registers on exec(2). 8907c478bd9Sstevel@tonic-gate */ 8917c478bd9Sstevel@tonic-gate void 8927c478bd9Sstevel@tonic-gate setregs(uarg_t *args) 8937c478bd9Sstevel@tonic-gate { 8947c478bd9Sstevel@tonic-gate struct regs *rp; 8957c478bd9Sstevel@tonic-gate kthread_t *t = curthread; 8967c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(t); 8977c478bd9Sstevel@tonic-gate pcb_t *pcb = &lwp->lwp_pcb; 8987c478bd9Sstevel@tonic-gate greg_t sp; 8997c478bd9Sstevel@tonic-gate 9007c478bd9Sstevel@tonic-gate /* 9017c478bd9Sstevel@tonic-gate * Initialize user registers 9027c478bd9Sstevel@tonic-gate */ 9037c478bd9Sstevel@tonic-gate (void) save_syscall_args(); /* copy args from registers first */ 9047c478bd9Sstevel@tonic-gate rp = lwptoregs(lwp); 9057c478bd9Sstevel@tonic-gate sp = rp->r_sp; 9067c478bd9Sstevel@tonic-gate bzero(rp, sizeof (*rp)); 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate rp->r_ss = UDS_SEL; 9097c478bd9Sstevel@tonic-gate rp->r_sp = sp; 9107c478bd9Sstevel@tonic-gate rp->r_pc = args->entry; 9117c478bd9Sstevel@tonic-gate rp->r_ps = PSL_USER; 9127c478bd9Sstevel@tonic-gate 9137c478bd9Sstevel@tonic-gate #if defined(__amd64) 9147c478bd9Sstevel@tonic-gate 9157c478bd9Sstevel@tonic-gate pcb->pcb_fs = pcb->pcb_gs = 0; 9167c478bd9Sstevel@tonic-gate pcb->pcb_fsbase = pcb->pcb_gsbase = 0; 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate if (ttoproc(t)->p_model == DATAMODEL_NATIVE) { 9197c478bd9Sstevel@tonic-gate 9207c478bd9Sstevel@tonic-gate rp->r_cs = UCS_SEL; 9217c478bd9Sstevel@tonic-gate 9227c478bd9Sstevel@tonic-gate /* 9237c478bd9Sstevel@tonic-gate * Only allow 64-bit user code descriptor to be present. 9247c478bd9Sstevel@tonic-gate */ 925ae115bc7Smrj gdt_ucode_model(DATAMODEL_NATIVE); 9267c478bd9Sstevel@tonic-gate 9277c478bd9Sstevel@tonic-gate /* 9287c478bd9Sstevel@tonic-gate * Arrange that the virtualized %fs and %gs GDT descriptors 9297c478bd9Sstevel@tonic-gate * have a well-defined initial state (present, ring 3 9307c478bd9Sstevel@tonic-gate * and of type data). 9317c478bd9Sstevel@tonic-gate */ 9327c478bd9Sstevel@tonic-gate pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc; 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate /* 9357c478bd9Sstevel@tonic-gate * thrptr is either NULL or a value used by DTrace. 9367c478bd9Sstevel@tonic-gate * 64-bit processes use %fs as their "thread" register. 9377c478bd9Sstevel@tonic-gate */ 9387c478bd9Sstevel@tonic-gate if (args->thrptr) 9397c478bd9Sstevel@tonic-gate (void) lwp_setprivate(lwp, _LWP_FSBASE, args->thrptr); 9407c478bd9Sstevel@tonic-gate 9417c478bd9Sstevel@tonic-gate } else { 9427c478bd9Sstevel@tonic-gate 9437c478bd9Sstevel@tonic-gate rp->r_cs = U32CS_SEL; 9447c478bd9Sstevel@tonic-gate rp->r_ds = rp->r_es = UDS_SEL; 9457c478bd9Sstevel@tonic-gate 9467c478bd9Sstevel@tonic-gate /* 9477c478bd9Sstevel@tonic-gate * only allow 32-bit user code selector to be present. 9487c478bd9Sstevel@tonic-gate */ 949ae115bc7Smrj gdt_ucode_model(DATAMODEL_ILP32); 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc; 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate /* 9547c478bd9Sstevel@tonic-gate * thrptr is either NULL or a value used by DTrace. 9557c478bd9Sstevel@tonic-gate * 32-bit processes use %gs as their "thread" register. 9567c478bd9Sstevel@tonic-gate */ 9577c478bd9Sstevel@tonic-gate if (args->thrptr) 9587c478bd9Sstevel@tonic-gate (void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr); 9597c478bd9Sstevel@tonic-gate 9607c478bd9Sstevel@tonic-gate } 9617c478bd9Sstevel@tonic-gate 9627c478bd9Sstevel@tonic-gate pcb->pcb_ds = rp->r_ds; 9637c478bd9Sstevel@tonic-gate pcb->pcb_es = rp->r_es; 9647712e92cSsudheer pcb->pcb_rupdate = 1; 9657c478bd9Sstevel@tonic-gate 9667c478bd9Sstevel@tonic-gate #elif defined(__i386) 9677c478bd9Sstevel@tonic-gate 9687c478bd9Sstevel@tonic-gate rp->r_cs = UCS_SEL; 9697c478bd9Sstevel@tonic-gate rp->r_ds = rp->r_es = UDS_SEL; 9707c478bd9Sstevel@tonic-gate 9717c478bd9Sstevel@tonic-gate /* 9727c478bd9Sstevel@tonic-gate * Arrange that the virtualized %fs and %gs GDT descriptors 9737c478bd9Sstevel@tonic-gate * have a well-defined initial state (present, ring 3 9747c478bd9Sstevel@tonic-gate * and of type data). 9757c478bd9Sstevel@tonic-gate */ 9767c478bd9Sstevel@tonic-gate pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc; 9777c478bd9Sstevel@tonic-gate 9787c478bd9Sstevel@tonic-gate /* 9797c478bd9Sstevel@tonic-gate * For %gs we need to reset LWP_GSBASE in pcb and the 9807c478bd9Sstevel@tonic-gate * per-cpu GDT descriptor. thrptr is either NULL 9817c478bd9Sstevel@tonic-gate * or a value used by DTrace. 9827c478bd9Sstevel@tonic-gate */ 9837c478bd9Sstevel@tonic-gate if (args->thrptr) 9847c478bd9Sstevel@tonic-gate (void) lwp_setprivate(lwp, _LWP_GSBASE, args->thrptr); 9857c478bd9Sstevel@tonic-gate #endif 9867c478bd9Sstevel@tonic-gate 9877c478bd9Sstevel@tonic-gate lwp->lwp_eosys = JUSTRETURN; 9887c478bd9Sstevel@tonic-gate t->t_post_sys = 1; 9897c478bd9Sstevel@tonic-gate 9907c478bd9Sstevel@tonic-gate /* 9917c478bd9Sstevel@tonic-gate * Here we initialize minimal fpu state. 9927c478bd9Sstevel@tonic-gate * The rest is done at the first floating 9937c478bd9Sstevel@tonic-gate * point instruction that a process executes. 9947c478bd9Sstevel@tonic-gate */ 9957c478bd9Sstevel@tonic-gate pcb->pcb_fpu.fpu_flags = 0; 9967c478bd9Sstevel@tonic-gate 9977c478bd9Sstevel@tonic-gate /* 9987c478bd9Sstevel@tonic-gate * Add the lwp context handlers that virtualize segment registers, 9997c478bd9Sstevel@tonic-gate * and/or system call stacks etc. 10007c478bd9Sstevel@tonic-gate */ 10017c478bd9Sstevel@tonic-gate lwp_installctx(lwp); 10027c478bd9Sstevel@tonic-gate } 10037c478bd9Sstevel@tonic-gate 10049acbbeafSnn35248 user_desc_t * 10059acbbeafSnn35248 cpu_get_gdt(void) 10069acbbeafSnn35248 { 10079acbbeafSnn35248 return (CPU->cpu_gdt); 10089acbbeafSnn35248 } 10099acbbeafSnn35248 10109acbbeafSnn35248 10117c478bd9Sstevel@tonic-gate #if !defined(lwp_getdatamodel) 10127c478bd9Sstevel@tonic-gate 10137c478bd9Sstevel@tonic-gate /* 10147c478bd9Sstevel@tonic-gate * Return the datamodel of the given lwp. 10157c478bd9Sstevel@tonic-gate */ 10167c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 10177c478bd9Sstevel@tonic-gate model_t 10187c478bd9Sstevel@tonic-gate lwp_getdatamodel(klwp_t *lwp) 10197c478bd9Sstevel@tonic-gate { 10207c478bd9Sstevel@tonic-gate return (lwp->lwp_procp->p_model); 10217c478bd9Sstevel@tonic-gate } 10227c478bd9Sstevel@tonic-gate 10237c478bd9Sstevel@tonic-gate #endif /* !lwp_getdatamodel */ 10247c478bd9Sstevel@tonic-gate 10257c478bd9Sstevel@tonic-gate #if !defined(get_udatamodel) 10267c478bd9Sstevel@tonic-gate 10277c478bd9Sstevel@tonic-gate model_t 10287c478bd9Sstevel@tonic-gate get_udatamodel(void) 10297c478bd9Sstevel@tonic-gate { 10307c478bd9Sstevel@tonic-gate return (curproc->p_model); 10317c478bd9Sstevel@tonic-gate } 10327c478bd9Sstevel@tonic-gate 10337c478bd9Sstevel@tonic-gate #endif /* !get_udatamodel */ 1034