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
5ae115bc7Smrj  * Common Development and Distribution License (the "License").
6ae115bc7Smrj  * 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  */
227c478bd9Sstevel@tonic-gate /*
23e2c5185aSChristopher Kiick  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
277c478bd9Sstevel@tonic-gate #include <procfs.h>
287c478bd9Sstevel@tonic-gate #include <ucontext.h>
297c478bd9Sstevel@tonic-gate #include <setjmp.h>
31ebe15f48SRoger A. Faulkner /*
32ceef08daSJosef 'Jeff' Sipek  * The stack needs to be 16-byte aligned with a 4-byte bias.  See comment in
33ceef08daSJosef 'Jeff' Sipek  * lib/libc/i386/gen/makectxt.c.
34ceef08daSJosef 'Jeff' Sipek  *
35ceef08daSJosef 'Jeff' Sipek  * Note: If you change it, you need to change it in the following files as
36ceef08daSJosef 'Jeff' Sipek  * well:
37ceef08daSJosef 'Jeff' Sipek  *
38ceef08daSJosef 'Jeff' Sipek  *  - lib/libc/i386/gen/makectxt.c
39*3e76f9d6SRichard Lowe  *  - lib/crt/i386/crti.s
40*3e76f9d6SRichard Lowe  *  - lib/crt/i386/crt1.s
41ebe15f48SRoger A. Faulkner  */
42ebe15f48SRoger A. Faulkner #undef	STACK_ALIGN
43ebe15f48SRoger A. Faulkner #define	STACK_ALIGN	16
44ebe15f48SRoger A. Faulkner 
457c478bd9Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
467c478bd9Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
48373d25a2SRoger A. Faulkner void *
setup_top_frame(void * stk,size_t stksize,ulwp_t * ulwp)49373d25a2SRoger A. Faulkner setup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
50373d25a2SRoger A. Faulkner {
51373d25a2SRoger A. Faulkner 	uint32_t *stack;
52373d25a2SRoger A. Faulkner 	struct {
53373d25a2SRoger A. Faulkner 		uint32_t	rpc;
54373d25a2SRoger A. Faulkner 		uint32_t	arg;
55ebe15f48SRoger A. Faulkner 		uint32_t	pad;
56373d25a2SRoger A. Faulkner 		uint32_t	fp;
57373d25a2SRoger A. Faulkner 		uint32_t	pc;
58373d25a2SRoger A. Faulkner 	} frame;
59373d25a2SRoger A. Faulkner 
60373d25a2SRoger A. Faulkner 	/*
61373d25a2SRoger A. Faulkner 	 * Top-of-stack must be rounded down to STACK_ALIGN and
62ebe15f48SRoger A. Faulkner 	 * there must be a minimum frame.  Note: 'frame' is not a true
63ebe15f48SRoger A. Faulkner 	 * stack frame (see <sys/frame.h>) but a construction made here to
64ebe15f48SRoger A. Faulkner 	 * make it look like _lwp_start called the thread start function
65ebe15f48SRoger A. Faulkner 	 * with a 16-byte aligned stack pointer (the address of frame.arg
66ebe15f48SRoger A. Faulkner 	 * is the address that muet be aligned on a 16-byte boundary).
67373d25a2SRoger A. Faulkner 	 */
68373d25a2SRoger A. Faulkner 	stack = (uint32_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
69373d25a2SRoger A. Faulkner 
70373d25a2SRoger A. Faulkner 	/*
71373d25a2SRoger A. Faulkner 	 * This will return NULL if the kernel cannot allocate
72373d25a2SRoger A. Faulkner 	 * a page for the top page of the stack.  This will cause
73373d25a2SRoger A. Faulkner 	 * thr_create(), pthread_create() or pthread_attr_setstack()
74373d25a2SRoger A. Faulkner 	 * to fail, passing the problem up to the application.
75373d25a2SRoger A. Faulkner 	 */
76ebe15f48SRoger A. Faulkner 	stack -= 5;	/* make the address of frame.arg be 16-byte aligned */
77373d25a2SRoger A. Faulkner 	frame.pc = 0;
78ebe15f48SRoger A. Faulkner 	frame.fp = 0;	/* initial address for %ebp (see EBP below) */
79ebe15f48SRoger A. Faulkner 	frame.pad = 0;
80373d25a2SRoger A. Faulkner 	frame.arg = (uint32_t)ulwp;
81373d25a2SRoger A. Faulkner 	frame.rpc = (uint32_t)_lwp_start;
82373d25a2SRoger A. Faulkner 	if (uucopy(&frame, (void *)stack, sizeof (frame)) == 0)
83373d25a2SRoger A. Faulkner 		return (stack);
84373d25a2SRoger A. Faulkner 	return (NULL);
85373d25a2SRoger A. Faulkner }
86373d25a2SRoger A. Faulkner 
877c478bd9Sstevel@tonic-gate int
setup_context(ucontext_t * ucp,void * (* func)(ulwp_t *),ulwp_t * ulwp,caddr_t stk,size_t stksize)887c478bd9Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
89*3e76f9d6SRichard Lowe     ulwp_t *ulwp, caddr_t stk, size_t stksize)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	static int initialized;
927c478bd9Sstevel@tonic-gate 	static greg_t fs, es, ds, cs, ss;
947c478bd9Sstevel@tonic-gate 	uint32_t *stack;
967c478bd9Sstevel@tonic-gate 	if (!initialized) {
977c478bd9Sstevel@tonic-gate 		ucontext_t uc;
997c478bd9Sstevel@tonic-gate 		/* do this once to load the segment registers */
1007c478bd9Sstevel@tonic-gate 		uc.uc_flags = UC_CPU;
1018cd45542Sraf 		(void) __getcontext(&uc);
1027c478bd9Sstevel@tonic-gate 		fs = uc.uc_mcontext.gregs[FS];
1037c478bd9Sstevel@tonic-gate 		es = uc.uc_mcontext.gregs[ES];
1047c478bd9Sstevel@tonic-gate 		ds = uc.uc_mcontext.gregs[DS];
1057c478bd9Sstevel@tonic-gate 		cs = uc.uc_mcontext.gregs[CS];
1067c478bd9Sstevel@tonic-gate 		ss = uc.uc_mcontext.gregs[SS];
1077c478bd9Sstevel@tonic-gate 		initialized = 1;
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 	/* clear the context and set the segment registers */
1108cd45542Sraf 	(void) memset(ucp, 0, sizeof (*ucp));
1117c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
1127c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
1137c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
1147c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
1157c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * Yuck.
1197c478bd9Sstevel@tonic-gate 	 * Use unused kernel pointer field in ucontext
1207c478bd9Sstevel@tonic-gate 	 * to pass down self pointer and set %gs selector
1217c478bd9Sstevel@tonic-gate 	 * value so __lwp_create() can setup %gs atomically.
1227c478bd9Sstevel@tonic-gate 	 * Without this we would need to block all signals
1237257d1b4Sraf 	 * and directly call ___lwp_private() in _thrp_setup
1247c478bd9Sstevel@tonic-gate 	 * on the other side of __lwp_create().
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = (greg_t)ulwp;
127ae115bc7Smrj 	ucp->uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
129373d25a2SRoger A. Faulkner 	/*
130373d25a2SRoger A. Faulkner 	 * Setup the top stack frame.
131373d25a2SRoger A. Faulkner 	 * If this fails, pass the problem up to the application.
132373d25a2SRoger A. Faulkner 	 */
133373d25a2SRoger A. Faulkner 	if ((stack = setup_top_frame(stk, stksize, ulwp)) == NULL)
134373d25a2SRoger A. Faulkner 		return (ENOMEM);
1367c478bd9Sstevel@tonic-gate 	/* fill in registers of interest */
1377c478bd9Sstevel@tonic-gate 	ucp->uc_flags |= UC_CPU;
1387c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
1397c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = (greg_t)stack;
140ebe15f48SRoger A. Faulkner 	ucp->uc_mcontext.gregs[EBP] = (greg_t)(stack + 3);
1427c478bd9Sstevel@tonic-gate 	return (0);
1437c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Machine-dependent startup code for a newly-created thread.
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate void *
_thrp_setup(ulwp_t * self)1497257d1b4Sraf _thrp_setup(ulwp_t *self)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
1527c478bd9Sstevel@tonic-gate 	self->ul_ustack.ss_size = self->ul_stksiz;
1537c478bd9Sstevel@tonic-gate 	self->ul_ustack.ss_flags = 0;
1548cd45542Sraf 	(void) setustack(&self->ul_ustack);
156d4204c85Sraf 	update_sched(self);
1577c478bd9Sstevel@tonic-gate 	tls_setup();
1597c478bd9Sstevel@tonic-gate 	/* signals have been deferred until now */
1607c478bd9Sstevel@tonic-gate 	sigon(self);
162d4204c85Sraf 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
163d4204c85Sraf 		return (NULL);	/* cancelled by pthread_create() */
1647c478bd9Sstevel@tonic-gate 	return (self->ul_startpc(self->ul_startarg));
1657c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate void
_fpinherit(ulwp_t * ulwp)1687c478bd9Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	ulwp->ul_fpuenv.ftag = 0xffffffff;
1717c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate void
getgregs(ulwp_t * ulwp,gregset_t rs)1747c478bd9Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	lwpstatus_t status;
1787c478bd9Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1797c478bd9Sstevel@tonic-gate 		rs[EIP] = status.pr_reg[EIP];
1807c478bd9Sstevel@tonic-gate 		rs[EDI] = status.pr_reg[EDI];
1817c478bd9Sstevel@tonic-gate 		rs[ESI] = status.pr_reg[ESI];
1827c478bd9Sstevel@tonic-gate 		rs[EBP] = status.pr_reg[EBP];
1837c478bd9Sstevel@tonic-gate 		rs[EBX] = status.pr_reg[EBX];
1847c478bd9Sstevel@tonic-gate 		rs[UESP] = status.pr_reg[UESP];
1857c478bd9Sstevel@tonic-gate 	} else {
1867c478bd9Sstevel@tonic-gate 		rs[EIP] = 0;
1877c478bd9Sstevel@tonic-gate 		rs[EDI] = 0;
1887c478bd9Sstevel@tonic-gate 		rs[ESI] = 0;
1897c478bd9Sstevel@tonic-gate 		rs[EBP] = 0;
1907c478bd9Sstevel@tonic-gate 		rs[EBX] = 0;
1917c478bd9Sstevel@tonic-gate 		rs[UESP] = 0;
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate void
setgregs(ulwp_t * ulwp,gregset_t rs)1967c478bd9Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	lwpstatus_t status;
2007c478bd9Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
2017c478bd9Sstevel@tonic-gate 		status.pr_reg[EIP] = rs[EIP];
2027c478bd9Sstevel@tonic-gate 		status.pr_reg[EDI] = rs[EDI];
2037c478bd9Sstevel@tonic-gate 		status.pr_reg[ESI] = rs[ESI];
2047c478bd9Sstevel@tonic-gate 		status.pr_reg[EBP] = rs[EBP];
2057c478bd9Sstevel@tonic-gate 		status.pr_reg[EBX] = rs[EBX];
2067c478bd9Sstevel@tonic-gate 		status.pr_reg[UESP] = rs[UESP];
2077c478bd9Sstevel@tonic-gate 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate int
__csigsetjmp(greg_t cs,greg_t ss,greg_t gs,greg_t fs,greg_t es,greg_t ds,greg_t edi,greg_t esi,greg_t ebp,greg_t esp,greg_t ebx,greg_t edx,greg_t ecx,greg_t eax,greg_t eip,sigjmp_buf env,int savemask)2127c478bd9Sstevel@tonic-gate __csigsetjmp(greg_t cs, greg_t ss, greg_t gs,
213*3e76f9d6SRichard Lowe     greg_t fs, greg_t es, greg_t ds,
214*3e76f9d6SRichard Lowe     greg_t edi, greg_t esi, greg_t ebp, greg_t esp,
215*3e76f9d6SRichard Lowe     greg_t ebx, greg_t edx, greg_t ecx, greg_t eax, greg_t eip,
216*3e76f9d6SRichard Lowe     sigjmp_buf env, int savemask)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	ucontext_t *ucp = (ucontext_t *)env;
2197c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
2217c478bd9Sstevel@tonic-gate 	ucp->uc_link = self->ul_siglink;
2227c478bd9Sstevel@tonic-gate 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
2237c478bd9Sstevel@tonic-gate 		ucp->uc_stack = self->ul_ustack;
2247c478bd9Sstevel@tonic-gate 	else {
2257c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_sp =
226d4204c85Sraf 		    (void *)(self->ul_stktop - self->ul_stksiz);
2277c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_size = self->ul_stksiz;
2287c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_flags = 0;
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	ucp->uc_flags = UC_STACK | UC_CPU;
2317c478bd9Sstevel@tonic-gate 	if (savemask) {
2327c478bd9Sstevel@tonic-gate 		ucp->uc_flags |= UC_SIGMASK;
2337c478bd9Sstevel@tonic-gate 		enter_critical(self);
2347c478bd9Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
2357c478bd9Sstevel@tonic-gate 		exit_critical(self);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[GS] = gs;
2387c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
2397c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
2407c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
2417c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDI] = edi;
2427c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESI] = esi;
2437c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBP] = ebp;
2447c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = esp + 4;
2457c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBX] = ebx;
2467c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDX] = edx;
2477c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ECX] = ecx;
2487c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EAX] = eax;
2497c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[TRAPNO] = 0;
2507c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ERR] = 0;
2517c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = eip;
2527c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
2537c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EFL] = 0;
2547c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = esp + 4;
2557c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
2577c478bd9Sstevel@tonic-gate 	return (0);
2587c478bd9Sstevel@tonic-gate }
259e2c5185aSChristopher Kiick 
260e2c5185aSChristopher Kiick void
smt_pause(void)261e2c5185aSChristopher Kiick smt_pause(void)
262e2c5185aSChristopher Kiick {
263e2c5185aSChristopher Kiick 	SMT_PAUSE();
264e2c5185aSChristopher Kiick }