10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*3446Smrj * Common Development and Distribution License (the "License"). 6*3446Smrj * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*3446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include "thr_uberdata.h" 290Sstevel@tonic-gate #include <procfs.h> 300Sstevel@tonic-gate #include <ucontext.h> 310Sstevel@tonic-gate #include <setjmp.h> 320Sstevel@tonic-gate 330Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *); 340Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t); 350Sstevel@tonic-gate 360Sstevel@tonic-gate int 370Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *), 380Sstevel@tonic-gate ulwp_t *ulwp, caddr_t stk, size_t stksize) 390Sstevel@tonic-gate { 400Sstevel@tonic-gate static int initialized; 410Sstevel@tonic-gate static greg_t fs, es, ds, cs, ss; 420Sstevel@tonic-gate 430Sstevel@tonic-gate uint32_t *stack; 440Sstevel@tonic-gate 450Sstevel@tonic-gate if (!initialized) { 460Sstevel@tonic-gate ucontext_t uc; 470Sstevel@tonic-gate 480Sstevel@tonic-gate /* do this once to load the segment registers */ 490Sstevel@tonic-gate uc.uc_flags = UC_CPU; 500Sstevel@tonic-gate (void) __getcontext_syscall(&uc); 510Sstevel@tonic-gate fs = uc.uc_mcontext.gregs[FS]; 520Sstevel@tonic-gate es = uc.uc_mcontext.gregs[ES]; 530Sstevel@tonic-gate ds = uc.uc_mcontext.gregs[DS]; 540Sstevel@tonic-gate cs = uc.uc_mcontext.gregs[CS]; 550Sstevel@tonic-gate ss = uc.uc_mcontext.gregs[SS]; 560Sstevel@tonic-gate initialized = 1; 570Sstevel@tonic-gate } 580Sstevel@tonic-gate /* clear the context and set the segment registers */ 590Sstevel@tonic-gate (void) _memset(ucp, 0, sizeof (*ucp)); 600Sstevel@tonic-gate ucp->uc_mcontext.gregs[FS] = fs; 610Sstevel@tonic-gate ucp->uc_mcontext.gregs[ES] = es; 620Sstevel@tonic-gate ucp->uc_mcontext.gregs[DS] = ds; 630Sstevel@tonic-gate ucp->uc_mcontext.gregs[CS] = cs; 640Sstevel@tonic-gate ucp->uc_mcontext.gregs[SS] = ss; 650Sstevel@tonic-gate 660Sstevel@tonic-gate /* 670Sstevel@tonic-gate * Yuck. 680Sstevel@tonic-gate * Use unused kernel pointer field in ucontext 690Sstevel@tonic-gate * to pass down self pointer and set %gs selector 700Sstevel@tonic-gate * value so __lwp_create() can setup %gs atomically. 710Sstevel@tonic-gate * Without this we would need to block all signals 720Sstevel@tonic-gate * and directly call __lwp_setprivate() in _thr_setup 730Sstevel@tonic-gate * on the other side of __lwp_create(). 740Sstevel@tonic-gate */ 750Sstevel@tonic-gate ucp->uc_mcontext.gregs[ESP] = (greg_t)ulwp; 76*3446Smrj ucp->uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL; 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* top-of-stack must be rounded down to STACK_ALIGN */ 790Sstevel@tonic-gate stack = (uint32_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1)); 800Sstevel@tonic-gate 810Sstevel@tonic-gate /* set up top stack frame */ 820Sstevel@tonic-gate *--stack = 0; 830Sstevel@tonic-gate *--stack = 0; 840Sstevel@tonic-gate *--stack = (uint32_t)ulwp; 850Sstevel@tonic-gate *--stack = (uint32_t)_lwp_start; 860Sstevel@tonic-gate 870Sstevel@tonic-gate /* fill in registers of interest */ 880Sstevel@tonic-gate ucp->uc_flags |= UC_CPU; 890Sstevel@tonic-gate ucp->uc_mcontext.gregs[EIP] = (greg_t)func; 900Sstevel@tonic-gate ucp->uc_mcontext.gregs[UESP] = (greg_t)stack; 910Sstevel@tonic-gate ucp->uc_mcontext.gregs[EBP] = (greg_t)(stack+2); 920Sstevel@tonic-gate 930Sstevel@tonic-gate return (0); 940Sstevel@tonic-gate } 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * Machine-dependent startup code for a newly-created thread. 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate void * 1000Sstevel@tonic-gate _thr_setup(ulwp_t *self) 1010Sstevel@tonic-gate { 1020Sstevel@tonic-gate self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz); 1030Sstevel@tonic-gate self->ul_ustack.ss_size = self->ul_stksiz; 1040Sstevel@tonic-gate self->ul_ustack.ss_flags = 0; 1050Sstevel@tonic-gate (void) _private_setustack(&self->ul_ustack); 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate tls_setup(); 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* signals have been deferred until now */ 1100Sstevel@tonic-gate sigon(self); 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate return (self->ul_startpc(self->ul_startarg)); 1130Sstevel@tonic-gate } 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate void 1160Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp) 1170Sstevel@tonic-gate { 1180Sstevel@tonic-gate ulwp->ul_fpuenv.ftag = 0xffffffff; 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate void 1220Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs) 1230Sstevel@tonic-gate { 1240Sstevel@tonic-gate lwpstatus_t status; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) { 1270Sstevel@tonic-gate rs[EIP] = status.pr_reg[EIP]; 1280Sstevel@tonic-gate rs[EDI] = status.pr_reg[EDI]; 1290Sstevel@tonic-gate rs[ESI] = status.pr_reg[ESI]; 1300Sstevel@tonic-gate rs[EBP] = status.pr_reg[EBP]; 1310Sstevel@tonic-gate rs[EBX] = status.pr_reg[EBX]; 1320Sstevel@tonic-gate rs[UESP] = status.pr_reg[UESP]; 1330Sstevel@tonic-gate } else { 1340Sstevel@tonic-gate rs[EIP] = 0; 1350Sstevel@tonic-gate rs[EDI] = 0; 1360Sstevel@tonic-gate rs[ESI] = 0; 1370Sstevel@tonic-gate rs[EBP] = 0; 1380Sstevel@tonic-gate rs[EBX] = 0; 1390Sstevel@tonic-gate rs[UESP] = 0; 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate void 1440Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs) 1450Sstevel@tonic-gate { 1460Sstevel@tonic-gate lwpstatus_t status; 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) { 1490Sstevel@tonic-gate status.pr_reg[EIP] = rs[EIP]; 1500Sstevel@tonic-gate status.pr_reg[EDI] = rs[EDI]; 1510Sstevel@tonic-gate status.pr_reg[ESI] = rs[ESI]; 1520Sstevel@tonic-gate status.pr_reg[EBP] = rs[EBP]; 1530Sstevel@tonic-gate status.pr_reg[EBX] = rs[EBX]; 1540Sstevel@tonic-gate status.pr_reg[UESP] = rs[UESP]; 1550Sstevel@tonic-gate (void) putlwpregs(ulwp->ul_lwpid, status.pr_reg); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate int 1600Sstevel@tonic-gate __csigsetjmp(greg_t cs, greg_t ss, greg_t gs, 1610Sstevel@tonic-gate greg_t fs, greg_t es, greg_t ds, 1620Sstevel@tonic-gate greg_t edi, greg_t esi, greg_t ebp, greg_t esp, 1630Sstevel@tonic-gate greg_t ebx, greg_t edx, greg_t ecx, greg_t eax, greg_t eip, 1640Sstevel@tonic-gate sigjmp_buf env, int savemask) 1650Sstevel@tonic-gate { 1660Sstevel@tonic-gate ucontext_t *ucp = (ucontext_t *)env; 1670Sstevel@tonic-gate ulwp_t *self = curthread; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate ucp->uc_link = self->ul_siglink; 1700Sstevel@tonic-gate if (self->ul_ustack.ss_flags & SS_ONSTACK) 1710Sstevel@tonic-gate ucp->uc_stack = self->ul_ustack; 1720Sstevel@tonic-gate else { 1730Sstevel@tonic-gate ucp->uc_stack.ss_sp = 1740Sstevel@tonic-gate (void *)(self->ul_stktop - self->ul_stksiz); 1750Sstevel@tonic-gate ucp->uc_stack.ss_size = self->ul_stksiz; 1760Sstevel@tonic-gate ucp->uc_stack.ss_flags = 0; 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate ucp->uc_flags = UC_STACK | UC_CPU; 1790Sstevel@tonic-gate if (savemask) { 1800Sstevel@tonic-gate ucp->uc_flags |= UC_SIGMASK; 1810Sstevel@tonic-gate enter_critical(self); 1820Sstevel@tonic-gate ucp->uc_sigmask = self->ul_sigmask; 1830Sstevel@tonic-gate exit_critical(self); 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate ucp->uc_mcontext.gregs[GS] = gs; 1860Sstevel@tonic-gate ucp->uc_mcontext.gregs[FS] = fs; 1870Sstevel@tonic-gate ucp->uc_mcontext.gregs[ES] = es; 1880Sstevel@tonic-gate ucp->uc_mcontext.gregs[DS] = ds; 1890Sstevel@tonic-gate ucp->uc_mcontext.gregs[EDI] = edi; 1900Sstevel@tonic-gate ucp->uc_mcontext.gregs[ESI] = esi; 1910Sstevel@tonic-gate ucp->uc_mcontext.gregs[EBP] = ebp; 1920Sstevel@tonic-gate ucp->uc_mcontext.gregs[ESP] = esp + 4; 1930Sstevel@tonic-gate ucp->uc_mcontext.gregs[EBX] = ebx; 1940Sstevel@tonic-gate ucp->uc_mcontext.gregs[EDX] = edx; 1950Sstevel@tonic-gate ucp->uc_mcontext.gregs[ECX] = ecx; 1960Sstevel@tonic-gate ucp->uc_mcontext.gregs[EAX] = eax; 1970Sstevel@tonic-gate ucp->uc_mcontext.gregs[TRAPNO] = 0; 1980Sstevel@tonic-gate ucp->uc_mcontext.gregs[ERR] = 0; 1990Sstevel@tonic-gate ucp->uc_mcontext.gregs[EIP] = eip; 2000Sstevel@tonic-gate ucp->uc_mcontext.gregs[CS] = cs; 2010Sstevel@tonic-gate ucp->uc_mcontext.gregs[EFL] = 0; 2020Sstevel@tonic-gate ucp->uc_mcontext.gregs[UESP] = esp + 4; 2030Sstevel@tonic-gate ucp->uc_mcontext.gregs[SS] = ss; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate return (0); 2060Sstevel@tonic-gate } 207