xref: /onnv-gate/usr/src/lib/libc/i386/threads/machdep.c (revision 7657:59d4b80e7a88)
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  */
216247Sraf 
220Sstevel@tonic-gate /*
236247Sraf  * 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 #include "thr_uberdata.h"
280Sstevel@tonic-gate #include <procfs.h>
290Sstevel@tonic-gate #include <ucontext.h>
300Sstevel@tonic-gate #include <setjmp.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
330Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
340Sstevel@tonic-gate 
35*7657SRoger.Faulkner@Sun.COM void *
36*7657SRoger.Faulkner@Sun.COM setup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
37*7657SRoger.Faulkner@Sun.COM {
38*7657SRoger.Faulkner@Sun.COM 	uint32_t *stack;
39*7657SRoger.Faulkner@Sun.COM 	struct {
40*7657SRoger.Faulkner@Sun.COM 		uint32_t	rpc;
41*7657SRoger.Faulkner@Sun.COM 		uint32_t	arg;
42*7657SRoger.Faulkner@Sun.COM 		uint32_t	fp;
43*7657SRoger.Faulkner@Sun.COM 		uint32_t	pc;
44*7657SRoger.Faulkner@Sun.COM 	} frame;
45*7657SRoger.Faulkner@Sun.COM 
46*7657SRoger.Faulkner@Sun.COM 	/*
47*7657SRoger.Faulkner@Sun.COM 	 * Top-of-stack must be rounded down to STACK_ALIGN and
48*7657SRoger.Faulkner@Sun.COM 	 * there must be a minimum frame.
49*7657SRoger.Faulkner@Sun.COM 	 */
50*7657SRoger.Faulkner@Sun.COM 	stack = (uint32_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
51*7657SRoger.Faulkner@Sun.COM 
52*7657SRoger.Faulkner@Sun.COM 	/*
53*7657SRoger.Faulkner@Sun.COM 	 * This will return NULL if the kernel cannot allocate
54*7657SRoger.Faulkner@Sun.COM 	 * a page for the top page of the stack.  This will cause
55*7657SRoger.Faulkner@Sun.COM 	 * thr_create(), pthread_create() or pthread_attr_setstack()
56*7657SRoger.Faulkner@Sun.COM 	 * to fail, passing the problem up to the application.
57*7657SRoger.Faulkner@Sun.COM 	 */
58*7657SRoger.Faulkner@Sun.COM 	stack -= 4;
59*7657SRoger.Faulkner@Sun.COM 	frame.pc = 0;
60*7657SRoger.Faulkner@Sun.COM 	frame.fp = 0;
61*7657SRoger.Faulkner@Sun.COM 	frame.arg = (uint32_t)ulwp;
62*7657SRoger.Faulkner@Sun.COM 	frame.rpc = (uint32_t)_lwp_start;
63*7657SRoger.Faulkner@Sun.COM 	if (uucopy(&frame, (void *)stack, sizeof (frame)) == 0)
64*7657SRoger.Faulkner@Sun.COM 		return (stack);
65*7657SRoger.Faulkner@Sun.COM 	return (NULL);
66*7657SRoger.Faulkner@Sun.COM }
67*7657SRoger.Faulkner@Sun.COM 
680Sstevel@tonic-gate int
690Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
700Sstevel@tonic-gate 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	static int initialized;
730Sstevel@tonic-gate 	static greg_t fs, es, ds, cs, ss;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	uint32_t *stack;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	if (!initialized) {
780Sstevel@tonic-gate 		ucontext_t uc;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 		/* do this once to load the segment registers */
810Sstevel@tonic-gate 		uc.uc_flags = UC_CPU;
826515Sraf 		(void) __getcontext(&uc);
830Sstevel@tonic-gate 		fs = uc.uc_mcontext.gregs[FS];
840Sstevel@tonic-gate 		es = uc.uc_mcontext.gregs[ES];
850Sstevel@tonic-gate 		ds = uc.uc_mcontext.gregs[DS];
860Sstevel@tonic-gate 		cs = uc.uc_mcontext.gregs[CS];
870Sstevel@tonic-gate 		ss = uc.uc_mcontext.gregs[SS];
880Sstevel@tonic-gate 		initialized = 1;
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 	/* clear the context and set the segment registers */
916515Sraf 	(void) memset(ucp, 0, sizeof (*ucp));
920Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
930Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
940Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
950Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
960Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/*
990Sstevel@tonic-gate 	 * Yuck.
1000Sstevel@tonic-gate 	 * Use unused kernel pointer field in ucontext
1010Sstevel@tonic-gate 	 * to pass down self pointer and set %gs selector
1020Sstevel@tonic-gate 	 * value so __lwp_create() can setup %gs atomically.
1030Sstevel@tonic-gate 	 * Without this we would need to block all signals
1046812Sraf 	 * and directly call ___lwp_private() in _thrp_setup
1050Sstevel@tonic-gate 	 * on the other side of __lwp_create().
1060Sstevel@tonic-gate 	 */
1070Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = (greg_t)ulwp;
1083446Smrj 	ucp->uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
1090Sstevel@tonic-gate 
110*7657SRoger.Faulkner@Sun.COM 	/*
111*7657SRoger.Faulkner@Sun.COM 	 * Setup the top stack frame.
112*7657SRoger.Faulkner@Sun.COM 	 * If this fails, pass the problem up to the application.
113*7657SRoger.Faulkner@Sun.COM 	 */
114*7657SRoger.Faulkner@Sun.COM 	if ((stack = setup_top_frame(stk, stksize, ulwp)) == NULL)
115*7657SRoger.Faulkner@Sun.COM 		return (ENOMEM);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/* fill in registers of interest */
1180Sstevel@tonic-gate 	ucp->uc_flags |= UC_CPU;
1190Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
1200Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = (greg_t)stack;
121*7657SRoger.Faulkner@Sun.COM 	ucp->uc_mcontext.gregs[EBP] = (greg_t)(stack + 2);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	return (0);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * Machine-dependent startup code for a newly-created thread.
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate void *
1306812Sraf _thrp_setup(ulwp_t *self)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
1330Sstevel@tonic-gate 	self->ul_ustack.ss_size = self->ul_stksiz;
1340Sstevel@tonic-gate 	self->ul_ustack.ss_flags = 0;
1356515Sraf 	(void) setustack(&self->ul_ustack);
1360Sstevel@tonic-gate 
1376247Sraf 	update_sched(self);
1380Sstevel@tonic-gate 	tls_setup();
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	/* signals have been deferred until now */
1410Sstevel@tonic-gate 	sigon(self);
1420Sstevel@tonic-gate 
1436247Sraf 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
1446247Sraf 		return (NULL);	/* cancelled by pthread_create() */
1450Sstevel@tonic-gate 	return (self->ul_startpc(self->ul_startarg));
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate void
1490Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	ulwp->ul_fpuenv.ftag = 0xffffffff;
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate void
1550Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	lwpstatus_t status;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1600Sstevel@tonic-gate 		rs[EIP] = status.pr_reg[EIP];
1610Sstevel@tonic-gate 		rs[EDI] = status.pr_reg[EDI];
1620Sstevel@tonic-gate 		rs[ESI] = status.pr_reg[ESI];
1630Sstevel@tonic-gate 		rs[EBP] = status.pr_reg[EBP];
1640Sstevel@tonic-gate 		rs[EBX] = status.pr_reg[EBX];
1650Sstevel@tonic-gate 		rs[UESP] = status.pr_reg[UESP];
1660Sstevel@tonic-gate 	} else {
1670Sstevel@tonic-gate 		rs[EIP] = 0;
1680Sstevel@tonic-gate 		rs[EDI] = 0;
1690Sstevel@tonic-gate 		rs[ESI] = 0;
1700Sstevel@tonic-gate 		rs[EBP] = 0;
1710Sstevel@tonic-gate 		rs[EBX] = 0;
1720Sstevel@tonic-gate 		rs[UESP] = 0;
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate void
1770Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	lwpstatus_t status;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1820Sstevel@tonic-gate 		status.pr_reg[EIP] = rs[EIP];
1830Sstevel@tonic-gate 		status.pr_reg[EDI] = rs[EDI];
1840Sstevel@tonic-gate 		status.pr_reg[ESI] = rs[ESI];
1850Sstevel@tonic-gate 		status.pr_reg[EBP] = rs[EBP];
1860Sstevel@tonic-gate 		status.pr_reg[EBX] = rs[EBX];
1870Sstevel@tonic-gate 		status.pr_reg[UESP] = rs[UESP];
1880Sstevel@tonic-gate 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate int
1930Sstevel@tonic-gate __csigsetjmp(greg_t cs, greg_t ss, greg_t gs,
1940Sstevel@tonic-gate 	greg_t fs, greg_t es, greg_t ds,
1950Sstevel@tonic-gate 	greg_t edi, greg_t esi, greg_t ebp, greg_t esp,
1960Sstevel@tonic-gate 	greg_t ebx, greg_t edx, greg_t ecx, greg_t eax, greg_t eip,
1970Sstevel@tonic-gate 	sigjmp_buf env, int savemask)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	ucontext_t *ucp = (ucontext_t *)env;
2000Sstevel@tonic-gate 	ulwp_t *self = curthread;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	ucp->uc_link = self->ul_siglink;
2030Sstevel@tonic-gate 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
2040Sstevel@tonic-gate 		ucp->uc_stack = self->ul_ustack;
2050Sstevel@tonic-gate 	else {
2060Sstevel@tonic-gate 		ucp->uc_stack.ss_sp =
2076247Sraf 		    (void *)(self->ul_stktop - self->ul_stksiz);
2080Sstevel@tonic-gate 		ucp->uc_stack.ss_size = self->ul_stksiz;
2090Sstevel@tonic-gate 		ucp->uc_stack.ss_flags = 0;
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 	ucp->uc_flags = UC_STACK | UC_CPU;
2120Sstevel@tonic-gate 	if (savemask) {
2130Sstevel@tonic-gate 		ucp->uc_flags |= UC_SIGMASK;
2140Sstevel@tonic-gate 		enter_critical(self);
2150Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
2160Sstevel@tonic-gate 		exit_critical(self);
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[GS] = gs;
2190Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
2200Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
2210Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
2220Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDI] = edi;
2230Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESI] = esi;
2240Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBP] = ebp;
2250Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = esp + 4;
2260Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBX] = ebx;
2270Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDX] = edx;
2280Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ECX] = ecx;
2290Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EAX] = eax;
2300Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[TRAPNO] = 0;
2310Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ERR] = 0;
2320Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = eip;
2330Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
2340Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EFL] = 0;
2350Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = esp + 4;
2360Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	return (0);
2390Sstevel@tonic-gate }
240