xref: /onnv-gate/usr/src/lib/libc/i386/threads/machdep.c (revision 10607:da3cc66100c3)
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 /*
23*10607SRoger.Faulkner@Sun.COM  * Copyright 2009 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 
32*10607SRoger.Faulkner@Sun.COM /*
33*10607SRoger.Faulkner@Sun.COM  * The i386 ABI says that the stack pointer need be only 4-byte aligned
34*10607SRoger.Faulkner@Sun.COM  * before a function call (STACK_ALIGN == 4).  We use a 16-byte stack
35*10607SRoger.Faulkner@Sun.COM  * alignment for the benefit of floating point code compiled using sse2.
36*10607SRoger.Faulkner@Sun.COM  * Even though the i386 ABI doesn't require it, both cc and gcc
37*10607SRoger.Faulkner@Sun.COM  * assume this alignment on entry to a function and maintain it
38*10607SRoger.Faulkner@Sun.COM  * for calls made from that function.  If the stack is initially
39*10607SRoger.Faulkner@Sun.COM  * aligned on a 16-byte boundary, it will continue to be so aligned.
40*10607SRoger.Faulkner@Sun.COM  * If it is not initially so aligned, it will never become so aligned.
41*10607SRoger.Faulkner@Sun.COM  */
42*10607SRoger.Faulkner@Sun.COM #undef	STACK_ALIGN
43*10607SRoger.Faulkner@Sun.COM #define	STACK_ALIGN	16
44*10607SRoger.Faulkner@Sun.COM 
450Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
460Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
470Sstevel@tonic-gate 
487657SRoger.Faulkner@Sun.COM void *
497657SRoger.Faulkner@Sun.COM setup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
507657SRoger.Faulkner@Sun.COM {
517657SRoger.Faulkner@Sun.COM 	uint32_t *stack;
527657SRoger.Faulkner@Sun.COM 	struct {
537657SRoger.Faulkner@Sun.COM 		uint32_t	rpc;
547657SRoger.Faulkner@Sun.COM 		uint32_t	arg;
55*10607SRoger.Faulkner@Sun.COM 		uint32_t	pad;
567657SRoger.Faulkner@Sun.COM 		uint32_t	fp;
577657SRoger.Faulkner@Sun.COM 		uint32_t	pc;
587657SRoger.Faulkner@Sun.COM 	} frame;
597657SRoger.Faulkner@Sun.COM 
607657SRoger.Faulkner@Sun.COM 	/*
617657SRoger.Faulkner@Sun.COM 	 * Top-of-stack must be rounded down to STACK_ALIGN and
62*10607SRoger.Faulkner@Sun.COM 	 * there must be a minimum frame.  Note: 'frame' is not a true
63*10607SRoger.Faulkner@Sun.COM 	 * stack frame (see <sys/frame.h>) but a construction made here to
64*10607SRoger.Faulkner@Sun.COM 	 * make it look like _lwp_start called the thread start function
65*10607SRoger.Faulkner@Sun.COM 	 * with a 16-byte aligned stack pointer (the address of frame.arg
66*10607SRoger.Faulkner@Sun.COM 	 * is the address that muet be aligned on a 16-byte boundary).
677657SRoger.Faulkner@Sun.COM 	 */
687657SRoger.Faulkner@Sun.COM 	stack = (uint32_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
697657SRoger.Faulkner@Sun.COM 
707657SRoger.Faulkner@Sun.COM 	/*
717657SRoger.Faulkner@Sun.COM 	 * This will return NULL if the kernel cannot allocate
727657SRoger.Faulkner@Sun.COM 	 * a page for the top page of the stack.  This will cause
737657SRoger.Faulkner@Sun.COM 	 * thr_create(), pthread_create() or pthread_attr_setstack()
747657SRoger.Faulkner@Sun.COM 	 * to fail, passing the problem up to the application.
757657SRoger.Faulkner@Sun.COM 	 */
76*10607SRoger.Faulkner@Sun.COM 	stack -= 5;	/* make the address of frame.arg be 16-byte aligned */
777657SRoger.Faulkner@Sun.COM 	frame.pc = 0;
78*10607SRoger.Faulkner@Sun.COM 	frame.fp = 0;	/* initial address for %ebp (see EBP below) */
79*10607SRoger.Faulkner@Sun.COM 	frame.pad = 0;
807657SRoger.Faulkner@Sun.COM 	frame.arg = (uint32_t)ulwp;
817657SRoger.Faulkner@Sun.COM 	frame.rpc = (uint32_t)_lwp_start;
827657SRoger.Faulkner@Sun.COM 	if (uucopy(&frame, (void *)stack, sizeof (frame)) == 0)
837657SRoger.Faulkner@Sun.COM 		return (stack);
847657SRoger.Faulkner@Sun.COM 	return (NULL);
857657SRoger.Faulkner@Sun.COM }
867657SRoger.Faulkner@Sun.COM 
870Sstevel@tonic-gate int
880Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
890Sstevel@tonic-gate 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	static int initialized;
920Sstevel@tonic-gate 	static greg_t fs, es, ds, cs, ss;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	uint32_t *stack;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (!initialized) {
970Sstevel@tonic-gate 		ucontext_t uc;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 		/* do this once to load the segment registers */
1000Sstevel@tonic-gate 		uc.uc_flags = UC_CPU;
1016515Sraf 		(void) __getcontext(&uc);
1020Sstevel@tonic-gate 		fs = uc.uc_mcontext.gregs[FS];
1030Sstevel@tonic-gate 		es = uc.uc_mcontext.gregs[ES];
1040Sstevel@tonic-gate 		ds = uc.uc_mcontext.gregs[DS];
1050Sstevel@tonic-gate 		cs = uc.uc_mcontext.gregs[CS];
1060Sstevel@tonic-gate 		ss = uc.uc_mcontext.gregs[SS];
1070Sstevel@tonic-gate 		initialized = 1;
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 	/* clear the context and set the segment registers */
1106515Sraf 	(void) memset(ucp, 0, sizeof (*ucp));
1110Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
1120Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
1130Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
1140Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
1150Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/*
1180Sstevel@tonic-gate 	 * Yuck.
1190Sstevel@tonic-gate 	 * Use unused kernel pointer field in ucontext
1200Sstevel@tonic-gate 	 * to pass down self pointer and set %gs selector
1210Sstevel@tonic-gate 	 * value so __lwp_create() can setup %gs atomically.
1220Sstevel@tonic-gate 	 * Without this we would need to block all signals
1236812Sraf 	 * and directly call ___lwp_private() in _thrp_setup
1240Sstevel@tonic-gate 	 * on the other side of __lwp_create().
1250Sstevel@tonic-gate 	 */
1260Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = (greg_t)ulwp;
1273446Smrj 	ucp->uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
1280Sstevel@tonic-gate 
1297657SRoger.Faulkner@Sun.COM 	/*
1307657SRoger.Faulkner@Sun.COM 	 * Setup the top stack frame.
1317657SRoger.Faulkner@Sun.COM 	 * If this fails, pass the problem up to the application.
1327657SRoger.Faulkner@Sun.COM 	 */
1337657SRoger.Faulkner@Sun.COM 	if ((stack = setup_top_frame(stk, stksize, ulwp)) == NULL)
1347657SRoger.Faulkner@Sun.COM 		return (ENOMEM);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/* fill in registers of interest */
1370Sstevel@tonic-gate 	ucp->uc_flags |= UC_CPU;
1380Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
1390Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = (greg_t)stack;
140*10607SRoger.Faulkner@Sun.COM 	ucp->uc_mcontext.gregs[EBP] = (greg_t)(stack + 3);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	return (0);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate  * Machine-dependent startup code for a newly-created thread.
1470Sstevel@tonic-gate  */
1480Sstevel@tonic-gate void *
1496812Sraf _thrp_setup(ulwp_t *self)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
1520Sstevel@tonic-gate 	self->ul_ustack.ss_size = self->ul_stksiz;
1530Sstevel@tonic-gate 	self->ul_ustack.ss_flags = 0;
1546515Sraf 	(void) setustack(&self->ul_ustack);
1550Sstevel@tonic-gate 
1566247Sraf 	update_sched(self);
1570Sstevel@tonic-gate 	tls_setup();
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/* signals have been deferred until now */
1600Sstevel@tonic-gate 	sigon(self);
1610Sstevel@tonic-gate 
1626247Sraf 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
1636247Sraf 		return (NULL);	/* cancelled by pthread_create() */
1640Sstevel@tonic-gate 	return (self->ul_startpc(self->ul_startarg));
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate void
1680Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	ulwp->ul_fpuenv.ftag = 0xffffffff;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate void
1740Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	lwpstatus_t status;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1790Sstevel@tonic-gate 		rs[EIP] = status.pr_reg[EIP];
1800Sstevel@tonic-gate 		rs[EDI] = status.pr_reg[EDI];
1810Sstevel@tonic-gate 		rs[ESI] = status.pr_reg[ESI];
1820Sstevel@tonic-gate 		rs[EBP] = status.pr_reg[EBP];
1830Sstevel@tonic-gate 		rs[EBX] = status.pr_reg[EBX];
1840Sstevel@tonic-gate 		rs[UESP] = status.pr_reg[UESP];
1850Sstevel@tonic-gate 	} else {
1860Sstevel@tonic-gate 		rs[EIP] = 0;
1870Sstevel@tonic-gate 		rs[EDI] = 0;
1880Sstevel@tonic-gate 		rs[ESI] = 0;
1890Sstevel@tonic-gate 		rs[EBP] = 0;
1900Sstevel@tonic-gate 		rs[EBX] = 0;
1910Sstevel@tonic-gate 		rs[UESP] = 0;
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate void
1960Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	lwpstatus_t status;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
2010Sstevel@tonic-gate 		status.pr_reg[EIP] = rs[EIP];
2020Sstevel@tonic-gate 		status.pr_reg[EDI] = rs[EDI];
2030Sstevel@tonic-gate 		status.pr_reg[ESI] = rs[ESI];
2040Sstevel@tonic-gate 		status.pr_reg[EBP] = rs[EBP];
2050Sstevel@tonic-gate 		status.pr_reg[EBX] = rs[EBX];
2060Sstevel@tonic-gate 		status.pr_reg[UESP] = rs[UESP];
2070Sstevel@tonic-gate 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate int
2120Sstevel@tonic-gate __csigsetjmp(greg_t cs, greg_t ss, greg_t gs,
2130Sstevel@tonic-gate 	greg_t fs, greg_t es, greg_t ds,
2140Sstevel@tonic-gate 	greg_t edi, greg_t esi, greg_t ebp, greg_t esp,
2150Sstevel@tonic-gate 	greg_t ebx, greg_t edx, greg_t ecx, greg_t eax, greg_t eip,
2160Sstevel@tonic-gate 	sigjmp_buf env, int savemask)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	ucontext_t *ucp = (ucontext_t *)env;
2190Sstevel@tonic-gate 	ulwp_t *self = curthread;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	ucp->uc_link = self->ul_siglink;
2220Sstevel@tonic-gate 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
2230Sstevel@tonic-gate 		ucp->uc_stack = self->ul_ustack;
2240Sstevel@tonic-gate 	else {
2250Sstevel@tonic-gate 		ucp->uc_stack.ss_sp =
2266247Sraf 		    (void *)(self->ul_stktop - self->ul_stksiz);
2270Sstevel@tonic-gate 		ucp->uc_stack.ss_size = self->ul_stksiz;
2280Sstevel@tonic-gate 		ucp->uc_stack.ss_flags = 0;
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 	ucp->uc_flags = UC_STACK | UC_CPU;
2310Sstevel@tonic-gate 	if (savemask) {
2320Sstevel@tonic-gate 		ucp->uc_flags |= UC_SIGMASK;
2330Sstevel@tonic-gate 		enter_critical(self);
2340Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
2350Sstevel@tonic-gate 		exit_critical(self);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[GS] = gs;
2380Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
2390Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
2400Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
2410Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDI] = edi;
2420Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESI] = esi;
2430Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBP] = ebp;
2440Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = esp + 4;
2450Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBX] = ebx;
2460Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDX] = edx;
2470Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ECX] = ecx;
2480Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EAX] = eax;
2490Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[TRAPNO] = 0;
2500Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ERR] = 0;
2510Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = eip;
2520Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
2530Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EFL] = 0;
2540Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = esp + 4;
2550Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	return (0);
2580Sstevel@tonic-gate }
259