xref: /onnv-gate/usr/src/lib/libc/i386/threads/machdep.c (revision 6247:ad4c702ff226)
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
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * 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  */
21*6247Sraf 
220Sstevel@tonic-gate /*
23*6247Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "thr_uberdata.h"
300Sstevel@tonic-gate #include <procfs.h>
310Sstevel@tonic-gate #include <ucontext.h>
320Sstevel@tonic-gate #include <setjmp.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
350Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
360Sstevel@tonic-gate 
370Sstevel@tonic-gate int
380Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
390Sstevel@tonic-gate 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate 	static int initialized;
420Sstevel@tonic-gate 	static greg_t fs, es, ds, cs, ss;
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 	uint32_t *stack;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	if (!initialized) {
470Sstevel@tonic-gate 		ucontext_t uc;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 		/* do this once to load the segment registers */
500Sstevel@tonic-gate 		uc.uc_flags = UC_CPU;
510Sstevel@tonic-gate 		(void) __getcontext_syscall(&uc);
520Sstevel@tonic-gate 		fs = uc.uc_mcontext.gregs[FS];
530Sstevel@tonic-gate 		es = uc.uc_mcontext.gregs[ES];
540Sstevel@tonic-gate 		ds = uc.uc_mcontext.gregs[DS];
550Sstevel@tonic-gate 		cs = uc.uc_mcontext.gregs[CS];
560Sstevel@tonic-gate 		ss = uc.uc_mcontext.gregs[SS];
570Sstevel@tonic-gate 		initialized = 1;
580Sstevel@tonic-gate 	}
590Sstevel@tonic-gate 	/* clear the context and set the segment registers */
600Sstevel@tonic-gate 	(void) _memset(ucp, 0, sizeof (*ucp));
610Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
620Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
630Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
640Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
650Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	/*
680Sstevel@tonic-gate 	 * Yuck.
690Sstevel@tonic-gate 	 * Use unused kernel pointer field in ucontext
700Sstevel@tonic-gate 	 * to pass down self pointer and set %gs selector
710Sstevel@tonic-gate 	 * value so __lwp_create() can setup %gs atomically.
720Sstevel@tonic-gate 	 * Without this we would need to block all signals
730Sstevel@tonic-gate 	 * and directly call __lwp_setprivate() in _thr_setup
740Sstevel@tonic-gate 	 * on the other side of __lwp_create().
750Sstevel@tonic-gate 	 */
760Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = (greg_t)ulwp;
773446Smrj 	ucp->uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	/* top-of-stack must be rounded down to STACK_ALIGN */
800Sstevel@tonic-gate 	stack = (uint32_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	/* set up top stack frame */
830Sstevel@tonic-gate 	*--stack = 0;
840Sstevel@tonic-gate 	*--stack = 0;
850Sstevel@tonic-gate 	*--stack = (uint32_t)ulwp;
860Sstevel@tonic-gate 	*--stack = (uint32_t)_lwp_start;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	/* fill in registers of interest */
890Sstevel@tonic-gate 	ucp->uc_flags |= UC_CPU;
900Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
910Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = (greg_t)stack;
920Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBP] = (greg_t)(stack+2);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	return (0);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * Machine-dependent startup code for a newly-created thread.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate void *
1010Sstevel@tonic-gate _thr_setup(ulwp_t *self)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
1040Sstevel@tonic-gate 	self->ul_ustack.ss_size = self->ul_stksiz;
1050Sstevel@tonic-gate 	self->ul_ustack.ss_flags = 0;
1060Sstevel@tonic-gate 	(void) _private_setustack(&self->ul_ustack);
1070Sstevel@tonic-gate 
108*6247Sraf 	update_sched(self);
1090Sstevel@tonic-gate 	tls_setup();
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	/* signals have been deferred until now */
1120Sstevel@tonic-gate 	sigon(self);
1130Sstevel@tonic-gate 
114*6247Sraf 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
115*6247Sraf 		return (NULL);	/* cancelled by pthread_create() */
1160Sstevel@tonic-gate 	return (self->ul_startpc(self->ul_startarg));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate void
1200Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	ulwp->ul_fpuenv.ftag = 0xffffffff;
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate void
1260Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	lwpstatus_t status;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1310Sstevel@tonic-gate 		rs[EIP] = status.pr_reg[EIP];
1320Sstevel@tonic-gate 		rs[EDI] = status.pr_reg[EDI];
1330Sstevel@tonic-gate 		rs[ESI] = status.pr_reg[ESI];
1340Sstevel@tonic-gate 		rs[EBP] = status.pr_reg[EBP];
1350Sstevel@tonic-gate 		rs[EBX] = status.pr_reg[EBX];
1360Sstevel@tonic-gate 		rs[UESP] = status.pr_reg[UESP];
1370Sstevel@tonic-gate 	} else {
1380Sstevel@tonic-gate 		rs[EIP] = 0;
1390Sstevel@tonic-gate 		rs[EDI] = 0;
1400Sstevel@tonic-gate 		rs[ESI] = 0;
1410Sstevel@tonic-gate 		rs[EBP] = 0;
1420Sstevel@tonic-gate 		rs[EBX] = 0;
1430Sstevel@tonic-gate 		rs[UESP] = 0;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate void
1480Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	lwpstatus_t status;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1530Sstevel@tonic-gate 		status.pr_reg[EIP] = rs[EIP];
1540Sstevel@tonic-gate 		status.pr_reg[EDI] = rs[EDI];
1550Sstevel@tonic-gate 		status.pr_reg[ESI] = rs[ESI];
1560Sstevel@tonic-gate 		status.pr_reg[EBP] = rs[EBP];
1570Sstevel@tonic-gate 		status.pr_reg[EBX] = rs[EBX];
1580Sstevel@tonic-gate 		status.pr_reg[UESP] = rs[UESP];
1590Sstevel@tonic-gate 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate int
1640Sstevel@tonic-gate __csigsetjmp(greg_t cs, greg_t ss, greg_t gs,
1650Sstevel@tonic-gate 	greg_t fs, greg_t es, greg_t ds,
1660Sstevel@tonic-gate 	greg_t edi, greg_t esi, greg_t ebp, greg_t esp,
1670Sstevel@tonic-gate 	greg_t ebx, greg_t edx, greg_t ecx, greg_t eax, greg_t eip,
1680Sstevel@tonic-gate 	sigjmp_buf env, int savemask)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	ucontext_t *ucp = (ucontext_t *)env;
1710Sstevel@tonic-gate 	ulwp_t *self = curthread;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	ucp->uc_link = self->ul_siglink;
1740Sstevel@tonic-gate 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
1750Sstevel@tonic-gate 		ucp->uc_stack = self->ul_ustack;
1760Sstevel@tonic-gate 	else {
1770Sstevel@tonic-gate 		ucp->uc_stack.ss_sp =
178*6247Sraf 		    (void *)(self->ul_stktop - self->ul_stksiz);
1790Sstevel@tonic-gate 		ucp->uc_stack.ss_size = self->ul_stksiz;
1800Sstevel@tonic-gate 		ucp->uc_stack.ss_flags = 0;
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 	ucp->uc_flags = UC_STACK | UC_CPU;
1830Sstevel@tonic-gate 	if (savemask) {
1840Sstevel@tonic-gate 		ucp->uc_flags |= UC_SIGMASK;
1850Sstevel@tonic-gate 		enter_critical(self);
1860Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
1870Sstevel@tonic-gate 		exit_critical(self);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[GS] = gs;
1900Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
1910Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
1920Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
1930Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDI] = edi;
1940Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESI] = esi;
1950Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBP] = ebp;
1960Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = esp + 4;
1970Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBX] = ebx;
1980Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDX] = edx;
1990Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ECX] = ecx;
2000Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EAX] = eax;
2010Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[TRAPNO] = 0;
2020Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ERR] = 0;
2030Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = eip;
2040Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
2050Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EFL] = 0;
2060Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = esp + 4;
2070Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	return (0);
2100Sstevel@tonic-gate }
211