27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5ae115bcmrj * Common Development and Distribution License (the "License").
6ae115bcmrj * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
23e2c5185Christopher Kiick * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate#include "thr_uberdata.h"
277c478bdstevel@tonic-gate#include <procfs.h>
287c478bdstevel@tonic-gate#include <ucontext.h>
297c478bdstevel@tonic-gate#include <setjmp.h>
31ebe15f4Roger A. Faulkner/*
32ceef08dJosef 'Jeff' Sipek * The stack needs to be 16-byte aligned with a 4-byte bias.  See comment in
33ceef08dJosef 'Jeff' Sipek * lib/libc/i386/gen/makectxt.c.
34ceef08dJosef 'Jeff' Sipek *
35ceef08dJosef 'Jeff' Sipek * Note: If you change it, you need to change it in the following files as
36ceef08dJosef 'Jeff' Sipek * well:
37ceef08dJosef 'Jeff' Sipek *
38ceef08dJosef 'Jeff' Sipek *  - lib/libc/i386/gen/makectxt.c
393e76f9dRichard Lowe *  - lib/crt/i386/crti.s
403e76f9dRichard Lowe *  - lib/crt/i386/crt1.s
41ebe15f4Roger A. Faulkner */
42ebe15f4Roger A. Faulkner#undef	STACK_ALIGN
43ebe15f4Roger A. Faulkner#define	STACK_ALIGN	16
44ebe15f4Roger A. Faulkner
457c478bdstevel@tonic-gateextern int getlwpstatus(thread_t, lwpstatus_t *);
467c478bdstevel@tonic-gateextern int putlwpregs(thread_t, prgregset_t);
48373d25aRoger A. Faulknervoid *
49373d25aRoger A. Faulknersetup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
50373d25aRoger A. Faulkner{
51373d25aRoger A. Faulkner	uint32_t *stack;
52373d25aRoger A. Faulkner	struct {
53373d25aRoger A. Faulkner		uint32_t	rpc;
54373d25aRoger A. Faulkner		uint32_t	arg;
55ebe15f4Roger A. Faulkner		uint32_t	pad;
56373d25aRoger A. Faulkner		uint32_t	fp;
57373d25aRoger A. Faulkner		uint32_t	pc;
58373d25aRoger A. Faulkner	} frame;
59373d25aRoger A. Faulkner
60373d25aRoger A. Faulkner	/*
61373d25aRoger A. Faulkner	 * Top-of-stack must be rounded down to STACK_ALIGN and
62ebe15f4Roger A. Faulkner	 * there must be a minimum frame.  Note: 'frame' is not a true
63ebe15f4Roger A. Faulkner	 * stack frame (see <sys/frame.h>) but a construction made here to
64ebe15f4Roger A. Faulkner	 * make it look like _lwp_start called the thread start function
65ebe15f4Roger A. Faulkner	 * with a 16-byte aligned stack pointer (the address of frame.arg
66ebe15f4Roger A. Faulkner	 * is the address that muet be aligned on a 16-byte boundary).
67373d25aRoger A. Faulkner	 */
68373d25aRoger A. Faulkner	stack = (uint32_t *)(((uintptr_t)stk + stksize) & ~(STACK_ALIGN-1));
69373d25aRoger A. Faulkner
70373d25aRoger A. Faulkner	/*
71373d25aRoger A. Faulkner	 * This will return NULL if the kernel cannot allocate
72373d25aRoger A. Faulkner	 * a page for the top page of the stack.  This will cause
73373d25aRoger A. Faulkner	 * thr_create(), pthread_create() or pthread_attr_setstack()
74373d25aRoger A. Faulkner	 * to fail, passing the problem up to the application.
75373d25aRoger A. Faulkner	 */
76ebe15f4Roger A. Faulkner	stack -= 5;	/* make the address of frame.arg be 16-byte aligned */
77373d25aRoger A. Faulkner	frame.pc = 0;
78ebe15f4Roger A. Faulkner	frame.fp = 0;	/* initial address for %ebp (see EBP below) */
79ebe15f4Roger A. Faulkner	frame.pad = 0;
80373d25aRoger A. Faulkner	frame.arg = (uint32_t)ulwp;
81373d25aRoger A. Faulkner	frame.rpc = (uint32_t)_lwp_start;
82373d25aRoger A. Faulkner	if (uucopy(&frame, (void *)stack, sizeof (frame)) == 0)
83373d25aRoger A. Faulkner		return (stack);
84373d25aRoger A. Faulkner	return (NULL);
85373d25aRoger A. Faulkner}
86373d25aRoger A. Faulkner
887c478bdstevel@tonic-gatesetup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
893e76f9dRichard Lowe    ulwp_t *ulwp, caddr_t stk, size_t stksize)
917c478bdstevel@tonic-gate	static int initialized;
927c478bdstevel@tonic-gate	static greg_t fs, es, ds, cs, ss;
947c478bdstevel@tonic-gate	uint32_t *stack;
967c478bdstevel@tonic-gate	if (!initialized) {
977c478bdstevel@tonic-gate		ucontext_t uc;
997c478bdstevel@tonic-gate		/* do this once to load the segment registers */
1007c478bdstevel@tonic-gate		uc.uc_flags = UC_CPU;
1018cd4554raf		(void) __getcontext(&uc);
1027c478bdstevel@tonic-gate		fs = uc.uc_mcontext.gregs[FS];
1037c478bdstevel@tonic-gate		es = uc.uc_mcontext.gregs[ES];
1047c478bdstevel@tonic-gate		ds = uc.uc_mcontext.gregs[DS];
1057c478bdstevel@tonic-gate		cs = uc.uc_mcontext.gregs[CS];
1067c478bdstevel@tonic-gate		ss = uc.uc_mcontext.gregs[SS];
1077c478bdstevel@tonic-gate		initialized = 1;
1087c478bdstevel@tonic-gate	}
1097c478bdstevel@tonic-gate	/* clear the context and set the segment registers */
1108cd4554raf	(void) memset(ucp, 0, sizeof (*ucp));
1117c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[FS] = fs;
1127c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[ES] = es;
1137c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[DS] = ds;
1147c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[CS] = cs;
1157c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[SS] = ss;
1177c478bdstevel@tonic-gate	/*
1187c478bdstevel@tonic-gate	 * Yuck.
1197c478bdstevel@tonic-gate	 * Use unused kernel pointer field in ucontext
1207c478bdstevel@tonic-gate	 * to pass down self pointer and set %gs selector
1217c478bdstevel@tonic-gate	 * value so __lwp_create() can setup %gs atomically.
1227c478bdstevel@tonic-gate	 * Without this we would need to block all signals
1237257d1braf	 * and directly call ___lwp_private() in _thrp_setup
1247c478bdstevel@tonic-gate	 * on the other side of __lwp_create().
1257c478bdstevel@tonic-gate	 */
1267c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[ESP] = (greg_t)ulwp;
127ae115bcmrj	ucp->uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
129373d25aRoger A. Faulkner	/*
130373d25aRoger A. Faulkner	 * Setup the top stack frame.
131373d25aRoger A. Faulkner	 * If this fails, pass the problem up to the application.
132373d25aRoger A. Faulkner	 */
133373d25aRoger A. Faulkner	if ((stack = setup_top_frame(stk, stksize, ulwp)) == NULL)
134373d25aRoger A. Faulkner		return (ENOMEM);
1367c478bdstevel@tonic-gate	/* fill in registers of interest */
1377c478bdstevel@tonic-gate	ucp->uc_flags |= UC_CPU;
1387c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[EIP] = (greg_t)func;
1397c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[UESP] = (greg_t)stack;
140ebe15f4Roger A. Faulkner	ucp->uc_mcontext.gregs[EBP] = (greg_t)(stack + 3);
1427c478bdstevel@tonic-gate	return (0);
1467c478bdstevel@tonic-gate * Machine-dependent startup code for a newly-created thread.
1477c478bdstevel@tonic-gate */
1487c478bdstevel@tonic-gatevoid *
1497257d1braf_thrp_setup(ulwp_t *self)
1517c478bdstevel@tonic-gate	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
1527c478bdstevel@tonic-gate	self->ul_ustack.ss_size = self->ul_stksiz;
1537c478bdstevel@tonic-gate	self->ul_ustack.ss_flags = 0;
1548cd4554raf	(void) setustack(&self->ul_ustack);
156d4204c8raf	update_sched(self);
1577c478bdstevel@tonic-gate	tls_setup();
1597c478bdstevel@tonic-gate	/* signals have been deferred until now */
1607c478bdstevel@tonic-gate	sigon(self);
162d4204c8raf	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
163d4204c8raf		return (NULL);	/* cancelled by pthread_create() */
1647c478bdstevel@tonic-gate	return (self->ul_startpc(self->ul_startarg));
1687c478bdstevel@tonic-gate_fpinherit(ulwp_t *ulwp)
1707c478bdstevel@tonic-gate	ulwp->ul_fpuenv.ftag = 0xffffffff;
1747c478bdstevel@tonic-gategetgregs(ulwp_t *ulwp, gregset_t rs)
1767c478bdstevel@tonic-gate	lwpstatus_t status;
1787c478bdstevel@tonic-gate	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1797c478bdstevel@tonic-gate		rs[EIP] = status.pr_reg[EIP];
1807c478bdstevel@tonic-gate		rs[EDI] = status.pr_reg[EDI];
1817c478bdstevel@tonic-gate		rs[ESI] = status.pr_reg[ESI];
1827c478bdstevel@tonic-gate		rs[EBP] = status.pr_reg[EBP];
1837c478bdstevel@tonic-gate		rs[EBX] = status.pr_reg[EBX];
1847c478bdstevel@tonic-gate		rs[UESP] = status.pr_reg[UESP];
1857c478bdstevel@tonic-gate	} else {
1867c478bdstevel@tonic-gate		rs[EIP] = 0;
1877c478bdstevel@tonic-gate		rs[EDI] = 0;
1887c478bdstevel@tonic-gate		rs[ESI] = 0;
1897c478bdstevel@tonic-gate		rs[EBP] = 0;
1907c478bdstevel@tonic-gate		rs[EBX] = 0;
1917c478bdstevel@tonic-gate		rs[UESP] = 0;
1927c478bdstevel@tonic-gate	}
1967c478bdstevel@tonic-gatesetgregs(ulwp_t *ulwp, gregset_t rs)
1987c478bdstevel@tonic-gate	lwpstatus_t status;
2007c478bdstevel@tonic-gate	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
2017c478bdstevel@tonic-gate		status.pr_reg[EIP] = rs[EIP];
2027c478bdstevel@tonic-gate		status.pr_reg[EDI] = rs[EDI];
2037c478bdstevel@tonic-gate		status.pr_reg[ESI] = rs[ESI];
2047c478bdstevel@tonic-gate		status.pr_reg[EBP] = rs[EBP];
2057c478bdstevel@tonic-gate		status.pr_reg[EBX] = rs[EBX];
2067c478bdstevel@tonic-gate		status.pr_reg[UESP] = rs[UESP];
2077c478bdstevel@tonic-gate		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
2087c478bdstevel@tonic-gate	}
2127c478bdstevel@tonic-gate__csigsetjmp(greg_t cs, greg_t ss, greg_t gs,
2133e76f9dRichard Lowe    greg_t fs, greg_t es, greg_t ds,
2143e76f9dRichard Lowe    greg_t edi, greg_t esi, greg_t ebp, greg_t esp,
2153e76f9dRichard Lowe    greg_t ebx, greg_t edx, greg_t ecx, greg_t eax, greg_t eip,
2163e76f9dRichard Lowe    sigjmp_buf env, int savemask)
2187c478bdstevel@tonic-gate	ucontext_t *ucp = (ucontext_t *)env;
2197c478bdstevel@tonic-gate	ulwp_t *self = curthread;
2217c478bdstevel@tonic-gate	ucp->uc_link = self->ul_siglink;
2227c478bdstevel@tonic-gate	if (self->ul_ustack.ss_flags & SS_ONSTACK)
2237c478bdstevel@tonic-gate		ucp->uc_stack = self->ul_ustack;
2247c478bdstevel@tonic-gate	else {
2257c478bdstevel@tonic-gate		ucp->uc_stack.ss_sp =
226d4204c8raf		    (void *)(self->ul_stktop - self->ul_stksiz);
2277c478bdstevel@tonic-gate		ucp->uc_stack.ss_size = self->ul_stksiz;
2287c478bdstevel@tonic-gate		ucp->uc_stack.ss_flags = 0;
2297c478bdstevel@tonic-gate	}
2307c478bdstevel@tonic-gate	ucp->uc_flags = UC_STACK | UC_CPU;
2317c478bdstevel@tonic-gate	if (savemask) {
2327c478bdstevel@tonic-gate		ucp->uc_flags |= UC_SIGMASK;
2337c478bdstevel@tonic-gate		enter_critical(self);
2347c478bdstevel@tonic-gate		ucp->uc_sigmask = self->ul_sigmask;
2357c478bdstevel@tonic-gate		exit_critical(self);
2367c478bdstevel@tonic-gate	}
2377c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[GS] = gs;
2387c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[FS] = fs;
2397c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[ES] = es;
2407c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[DS] = ds;
2417c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[EDI] = edi;
2427c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[ESI] = esi;
2437c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[EBP] = ebp;
2447c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[ESP] = esp + 4;
2457c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[EBX] = ebx;
2467c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[EDX] = edx;
2477c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[ECX] = ecx;
2487c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[EAX] = eax;
2497c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[TRAPNO] = 0;
2507c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[ERR] = 0;
2517c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[EIP] = eip;
2527c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[CS] = cs;
2537c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[EFL] = 0;
2547c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[UESP] = esp + 4;
2557c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[SS] = ss;
2577c478bdstevel@tonic-gate	return (0);
259e2c5185Christopher Kiick
260e2c5185Christopher Kiickvoid
261e2c5185Christopher Kiicksmt_pause(void)
262e2c5185Christopher Kiick{
263e2c5185Christopher Kiick	SMT_PAUSE();
264e2c5185Christopher Kiick}