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  */
21d4204c85Sraf 
227c478bd9Sstevel@tonic-gate /*
23d4204c85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
307c478bd9Sstevel@tonic-gate #include <procfs.h>
317c478bd9Sstevel@tonic-gate #include <ucontext.h>
327c478bd9Sstevel@tonic-gate #include <setjmp.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
357c478bd9Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate int
387c478bd9Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
397c478bd9Sstevel@tonic-gate 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate 	static int initialized;
427c478bd9Sstevel@tonic-gate 	static greg_t fs, es, ds, cs, ss;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	uint32_t *stack;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	if (!initialized) {
477c478bd9Sstevel@tonic-gate 		ucontext_t uc;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 		/* do this once to load the segment registers */
507c478bd9Sstevel@tonic-gate 		uc.uc_flags = UC_CPU;
51*8cd45542Sraf 		(void) __getcontext(&uc);
527c478bd9Sstevel@tonic-gate 		fs = uc.uc_mcontext.gregs[FS];
537c478bd9Sstevel@tonic-gate 		es = uc.uc_mcontext.gregs[ES];
547c478bd9Sstevel@tonic-gate 		ds = uc.uc_mcontext.gregs[DS];
557c478bd9Sstevel@tonic-gate 		cs = uc.uc_mcontext.gregs[CS];
567c478bd9Sstevel@tonic-gate 		ss = uc.uc_mcontext.gregs[SS];
577c478bd9Sstevel@tonic-gate 		initialized = 1;
587c478bd9Sstevel@tonic-gate 	}
597c478bd9Sstevel@tonic-gate 	/* clear the context and set the segment registers */
60*8cd45542Sraf 	(void) memset(ucp, 0, sizeof (*ucp));
617c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
627c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
637c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
647c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
657c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	/*
687c478bd9Sstevel@tonic-gate 	 * Yuck.
697c478bd9Sstevel@tonic-gate 	 * Use unused kernel pointer field in ucontext
707c478bd9Sstevel@tonic-gate 	 * to pass down self pointer and set %gs selector
717c478bd9Sstevel@tonic-gate 	 * value so __lwp_create() can setup %gs atomically.
727c478bd9Sstevel@tonic-gate 	 * Without this we would need to block all signals
737c478bd9Sstevel@tonic-gate 	 * and directly call __lwp_setprivate() in _thr_setup
747c478bd9Sstevel@tonic-gate 	 * on the other side of __lwp_create().
757c478bd9Sstevel@tonic-gate 	 */
767c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = (greg_t)ulwp;
77ae115bc7Smrj 	ucp->uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/* top-of-stack must be rounded down to STACK_ALIGN */
807c478bd9Sstevel@tonic-gate 	stack = (uint32_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/* set up top stack frame */
837c478bd9Sstevel@tonic-gate 	*--stack = 0;
847c478bd9Sstevel@tonic-gate 	*--stack = 0;
857c478bd9Sstevel@tonic-gate 	*--stack = (uint32_t)ulwp;
867c478bd9Sstevel@tonic-gate 	*--stack = (uint32_t)_lwp_start;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/* fill in registers of interest */
897c478bd9Sstevel@tonic-gate 	ucp->uc_flags |= UC_CPU;
907c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
917c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = (greg_t)stack;
927c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBP] = (greg_t)(stack+2);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	return (0);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * Machine-dependent startup code for a newly-created thread.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate void *
1017c478bd9Sstevel@tonic-gate _thr_setup(ulwp_t *self)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
1047c478bd9Sstevel@tonic-gate 	self->ul_ustack.ss_size = self->ul_stksiz;
1057c478bd9Sstevel@tonic-gate 	self->ul_ustack.ss_flags = 0;
106*8cd45542Sraf 	(void) setustack(&self->ul_ustack);
1077c478bd9Sstevel@tonic-gate 
108d4204c85Sraf 	update_sched(self);
1097c478bd9Sstevel@tonic-gate 	tls_setup();
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	/* signals have been deferred until now */
1127c478bd9Sstevel@tonic-gate 	sigon(self);
1137c478bd9Sstevel@tonic-gate 
114d4204c85Sraf 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
115d4204c85Sraf 		return (NULL);	/* cancelled by pthread_create() */
1167c478bd9Sstevel@tonic-gate 	return (self->ul_startpc(self->ul_startarg));
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate void
1207c478bd9Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	ulwp->ul_fpuenv.ftag = 0xffffffff;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate void
1267c478bd9Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	lwpstatus_t status;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1317c478bd9Sstevel@tonic-gate 		rs[EIP] = status.pr_reg[EIP];
1327c478bd9Sstevel@tonic-gate 		rs[EDI] = status.pr_reg[EDI];
1337c478bd9Sstevel@tonic-gate 		rs[ESI] = status.pr_reg[ESI];
1347c478bd9Sstevel@tonic-gate 		rs[EBP] = status.pr_reg[EBP];
1357c478bd9Sstevel@tonic-gate 		rs[EBX] = status.pr_reg[EBX];
1367c478bd9Sstevel@tonic-gate 		rs[UESP] = status.pr_reg[UESP];
1377c478bd9Sstevel@tonic-gate 	} else {
1387c478bd9Sstevel@tonic-gate 		rs[EIP] = 0;
1397c478bd9Sstevel@tonic-gate 		rs[EDI] = 0;
1407c478bd9Sstevel@tonic-gate 		rs[ESI] = 0;
1417c478bd9Sstevel@tonic-gate 		rs[EBP] = 0;
1427c478bd9Sstevel@tonic-gate 		rs[EBX] = 0;
1437c478bd9Sstevel@tonic-gate 		rs[UESP] = 0;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate void
1487c478bd9Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	lwpstatus_t status;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1537c478bd9Sstevel@tonic-gate 		status.pr_reg[EIP] = rs[EIP];
1547c478bd9Sstevel@tonic-gate 		status.pr_reg[EDI] = rs[EDI];
1557c478bd9Sstevel@tonic-gate 		status.pr_reg[ESI] = rs[ESI];
1567c478bd9Sstevel@tonic-gate 		status.pr_reg[EBP] = rs[EBP];
1577c478bd9Sstevel@tonic-gate 		status.pr_reg[EBX] = rs[EBX];
1587c478bd9Sstevel@tonic-gate 		status.pr_reg[UESP] = rs[UESP];
1597c478bd9Sstevel@tonic-gate 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate int
1647c478bd9Sstevel@tonic-gate __csigsetjmp(greg_t cs, greg_t ss, greg_t gs,
1657c478bd9Sstevel@tonic-gate 	greg_t fs, greg_t es, greg_t ds,
1667c478bd9Sstevel@tonic-gate 	greg_t edi, greg_t esi, greg_t ebp, greg_t esp,
1677c478bd9Sstevel@tonic-gate 	greg_t ebx, greg_t edx, greg_t ecx, greg_t eax, greg_t eip,
1687c478bd9Sstevel@tonic-gate 	sigjmp_buf env, int savemask)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	ucontext_t *ucp = (ucontext_t *)env;
1717c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	ucp->uc_link = self->ul_siglink;
1747c478bd9Sstevel@tonic-gate 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
1757c478bd9Sstevel@tonic-gate 		ucp->uc_stack = self->ul_ustack;
1767c478bd9Sstevel@tonic-gate 	else {
1777c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_sp =
178d4204c85Sraf 		    (void *)(self->ul_stktop - self->ul_stksiz);
1797c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_size = self->ul_stksiz;
1807c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_flags = 0;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 	ucp->uc_flags = UC_STACK | UC_CPU;
1837c478bd9Sstevel@tonic-gate 	if (savemask) {
1847c478bd9Sstevel@tonic-gate 		ucp->uc_flags |= UC_SIGMASK;
1857c478bd9Sstevel@tonic-gate 		enter_critical(self);
1867c478bd9Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
1877c478bd9Sstevel@tonic-gate 		exit_critical(self);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[GS] = gs;
1907c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[FS] = fs;
1917c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ES] = es;
1927c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[DS] = ds;
1937c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDI] = edi;
1947c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESI] = esi;
1957c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBP] = ebp;
1967c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ESP] = esp + 4;
1977c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EBX] = ebx;
1987c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDX] = edx;
1997c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ECX] = ecx;
2007c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EAX] = eax;
2017c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[TRAPNO] = 0;
2027c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[ERR] = 0;
2037c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = eip;
2047c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[CS] = cs;
2057c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EFL] = 0;
2067c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = esp + 4;
2077c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[SS] = ss;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	return (0);
2107c478bd9Sstevel@tonic-gate }
211