17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5d4204c8raf * Common Development and Distribution License (the "License").
6d4204c8raf * 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 */
21d4204c8raf
227c478bdstevel@tonic-gate/*
233cd8a1aRoger A. Faulkner * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#include "lint.h"
277c478bdstevel@tonic-gate#include "thr_uberdata.h"
287c478bdstevel@tonic-gate#include <procfs.h>
297c478bdstevel@tonic-gate#include <setjmp.h>
307c478bdstevel@tonic-gate#include <sys/fsr.h>
317c478bdstevel@tonic-gate#include "sigjmp_struct.h"
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gateextern int getlwpstatus(thread_t, lwpstatus_t *);
347c478bdstevel@tonic-gateextern int putlwpregs(thread_t, prgregset_t);
357c478bdstevel@tonic-gate
36373d25aRoger A. Faulkner/* ARGSUSED2 */
37373d25aRoger A. Faulknervoid *
38373d25aRoger A. Faulknersetup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
397c478bdstevel@tonic-gate{
40373d25aRoger A. Faulkner	uintptr_t stack;
41373d25aRoger A. Faulkner	char frame[SA(MINFRAME)];
42373d25aRoger A. Faulkner
437c478bdstevel@tonic-gate	/*
447c478bdstevel@tonic-gate	 * Top-of-stack must be rounded down to STACK_ALIGN and
457c478bdstevel@tonic-gate	 * there must be a minimum frame for the register window.
467c478bdstevel@tonic-gate	 */
47373d25aRoger A. Faulkner	stack = (((uintptr_t)stk + stksize) & ~(STACK_ALIGN - 1)) -
487c478bdstevel@tonic-gate	    SA(MINFRAME);
497c478bdstevel@tonic-gate
50373d25aRoger A. Faulkner	/*
51373d25aRoger A. Faulkner	 * This will return NULL if the kernel cannot allocate
52373d25aRoger A. Faulkner	 * a page for the top page of the stack.  This will cause
53373d25aRoger A. Faulkner	 * thr_create(), pthread_create() or pthread_attr_setstack()
54373d25aRoger A. Faulkner	 * to fail, passing the problem up to the application.
55373d25aRoger A. Faulkner	 */
56373d25aRoger A. Faulkner	(void) memset(frame, 0, sizeof (frame));
57373d25aRoger A. Faulkner	if (uucopy(frame, (void *)stack, sizeof (frame)) == 0)
58373d25aRoger A. Faulkner		return ((void *)stack);
59373d25aRoger A. Faulkner	return (NULL);
60373d25aRoger A. Faulkner}
61373d25aRoger A. Faulkner
62373d25aRoger A. Faulknerint
63373d25aRoger A. Faulknersetup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
647484d76Toomas Soome    ulwp_t *ulwp, caddr_t stk, size_t stksize)
65373d25aRoger A. Faulkner{
66373d25aRoger A. Faulkner	uintptr_t stack;
67373d25aRoger A. Faulkner
68373d25aRoger A. Faulkner	/* clear the context */
698cd4554raf	(void) memset(ucp, 0, sizeof (*ucp));
70373d25aRoger A. Faulkner
71373d25aRoger A. Faulkner	/*
72373d25aRoger A. Faulkner	 * Clear the top stack frame.
73373d25aRoger A. Faulkner	 * If this fails, pass the problem up to the application.
74373d25aRoger A. Faulkner	 */
757484d76Toomas Soome	stack = (uintptr_t)setup_top_frame(stk, stksize, ulwp);
767484d76Toomas Soome	if (stack == (uintptr_t)NULL)
77373d25aRoger A. Faulkner		return (ENOMEM);
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate	/* fill in registers of interest */
807c478bdstevel@tonic-gate	ucp->uc_flags |= UC_CPU;
817c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
827c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[REG_nPC] = (greg_t)func + 4;
837c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[REG_O0] = (greg_t)ulwp;
847c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[REG_SP] = (greg_t)(stack - STACK_BIAS);
857c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[REG_O7] = (greg_t)_lwp_start;
867c478bdstevel@tonic-gate	ucp->uc_mcontext.gregs[REG_G7] = (greg_t)ulwp;
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gate	return (0);
897c478bdstevel@tonic-gate}
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate/*
927c478bdstevel@tonic-gate * Machine-dependent startup code for a newly-created thread.
937c478bdstevel@tonic-gate */
947c478bdstevel@tonic-gatevoid *
957257d1braf_thrp_setup(ulwp_t *self)
967c478bdstevel@tonic-gate{
977c478bdstevel@tonic-gate	extern void _setfsr(greg_t *);
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gate	if (self->ul_fpuenv.fpu_en)
1007c478bdstevel@tonic-gate		_setfsr(&self->ul_fpuenv.fsr);
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
1037c478bdstevel@tonic-gate	self->ul_ustack.ss_size = self->ul_stksiz;
1047c478bdstevel@tonic-gate	self->ul_ustack.ss_flags = 0;
1058cd4554raf	(void) setustack(&self->ul_ustack);
106d4204c8raf
107d4204c8raf	update_sched(self);
1087c478bdstevel@tonic-gate	tls_setup();
1097c478bdstevel@tonic-gate
1107c478bdstevel@tonic-gate	/* signals have been deferred until now */
1117c478bdstevel@tonic-gate	sigon(self);
1127c478bdstevel@tonic-gate
113d4204c8raf	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
114d4204c8raf		return (NULL);	/* cancelled by pthread_create() */
1157c478bdstevel@tonic-gate	return (self->ul_startpc(self->ul_startarg));
1167c478bdstevel@tonic-gate}
1177c478bdstevel@tonic-gate
1187c478bdstevel@tonic-gatevoid
1197c478bdstevel@tonic-gate_fpinherit(ulwp_t *ulwp)
1207c478bdstevel@tonic-gate{
1217c478bdstevel@tonic-gate	extern void _getfsr(greg_t *);
1227c478bdstevel@tonic-gate	int fpu_enabled;
1237c478bdstevel@tonic-gate
1247c478bdstevel@tonic-gate#ifdef __sparcv9
1257c478bdstevel@tonic-gate	extern greg_t _getfprs();
1267c478bdstevel@tonic-gate	fpu_enabled = _getfprs() & FPRS_FEF;
1277c478bdstevel@tonic-gate#else
1287c478bdstevel@tonic-gate	extern psw_t _getpsr();
1297c478bdstevel@tonic-gate	fpu_enabled = _getpsr() & PSR_EF;
1307c478bdstevel@tonic-gate#endif /* __sparcv9 */
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate	if (fpu_enabled) {
1337c478bdstevel@tonic-gate		_getfsr(&ulwp->ul_fpuenv.fsr);
1347c478bdstevel@tonic-gate		ulwp->ul_fpuenv.fpu_en = 1;
1357c478bdstevel@tonic-gate	} else {
1367c478bdstevel@tonic-gate		ulwp->ul_fpuenv.fpu_en = 0;
1377c478bdstevel@tonic-gate	}
1387c478bdstevel@tonic-gate}
1397c478bdstevel@tonic-gate
1407c478bdstevel@tonic-gatevoid
1417c478bdstevel@tonic-gategetgregs(ulwp_t *ulwp, gregset_t rs)
1427c478bdstevel@tonic-gate{
1437c478bdstevel@tonic-gate	lwpstatus_t status;
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1467c478bdstevel@tonic-gate		rs[REG_PC] = status.pr_reg[R_PC];
1477c478bdstevel@tonic-gate		rs[REG_O6] = status.pr_reg[R_O6];
1487c478bdstevel@tonic-gate		rs[REG_O7] = status.pr_reg[R_O7];
1497c478bdstevel@tonic-gate		rs[REG_G1] = status.pr_reg[R_G1];
1507c478bdstevel@tonic-gate		rs[REG_G2] = status.pr_reg[R_G2];
1517c478bdstevel@tonic-gate		rs[REG_G3] = status.pr_reg[R_G3];
1527c478bdstevel@tonic-gate		rs[REG_G4] = status.pr_reg[R_G4];
1537c478bdstevel@tonic-gate	} else {
1547c478bdstevel@tonic-gate		rs[REG_PC] = 0;
1557c478bdstevel@tonic-gate		rs[REG_O6] = 0;
1567c478bdstevel@tonic-gate		rs[REG_O7] = 0;
1577c478bdstevel@tonic-gate		rs[REG_G1] = 0;
1587c478bdstevel@tonic-gate		rs[REG_G2] = 0;
1597c478bdstevel@tonic-gate		rs[REG_G3] = 0;
1607c478bdstevel@tonic-gate		rs[REG_G4] = 0;
1617c478bdstevel@tonic-gate	}
1627c478bdstevel@tonic-gate}
1637c478bdstevel@tonic-gate
1647c478bdstevel@tonic-gatevoid
1657c478bdstevel@tonic-gatesetgregs(ulwp_t *ulwp, gregset_t rs)
1667c478bdstevel@tonic-gate{
1677c478bdstevel@tonic-gate	lwpstatus_t status;
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gate	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1707c478bdstevel@tonic-gate		status.pr_reg[R_PC] = rs[REG_PC];
1717c478bdstevel@tonic-gate		status.pr_reg[R_O6] = rs[REG_O6];
1727c478bdstevel@tonic-gate		status.pr_reg[R_O7] = rs[REG_O7];
1737c478bdstevel@tonic-gate		status.pr_reg[R_G1] = rs[REG_G1];
1747c478bdstevel@tonic-gate		status.pr_reg[R_G2] = rs[REG_G2];
1757c478bdstevel@tonic-gate		status.pr_reg[R_G3] = rs[REG_G3];
1767c478bdstevel@tonic-gate		status.pr_reg[R_G4] = rs[REG_G4];
1777c478bdstevel@tonic-gate		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
1787c478bdstevel@tonic-gate	}
1797c478bdstevel@tonic-gate}
1807c478bdstevel@tonic-gate
1817c478bdstevel@tonic-gateint
1827c478bdstevel@tonic-gate__csigsetjmp(sigjmp_buf env, int savemask)
1837c478bdstevel@tonic-gate{
1847c478bdstevel@tonic-gate	sigjmp_struct_t *bp = (sigjmp_struct_t *)env;
1857c478bdstevel@tonic-gate	ulwp_t *self = curthread;
1867c478bdstevel@tonic-gate
1877c478bdstevel@tonic-gate	/*
1887c478bdstevel@tonic-gate	 * bp->sjs_sp, bp->sjs_pc, bp->sjs_fp and bp->sjs_i7 are already set.
1893cd8a1aRoger A. Faulkner	 * Also, if we are running in 64-bit mode (__sparcv9),
1903cd8a1aRoger A. Faulkner	 * then bp->sjs_asi and bp->sjs_fprs are already set.
1917c478bdstevel@tonic-gate	 */
1927c478bdstevel@tonic-gate	bp->sjs_flags = JB_FRAMEPTR;
1937c478bdstevel@tonic-gate	bp->sjs_uclink = self->ul_siglink;
1947c478bdstevel@tonic-gate	if (self->ul_ustack.ss_flags & SS_ONSTACK)
1957c478bdstevel@tonic-gate		bp->sjs_stack = self->ul_ustack;
1967c478bdstevel@tonic-gate	else {
1977c478bdstevel@tonic-gate		bp->sjs_stack.ss_sp =
198d4204c8raf		    (void *)(self->ul_stktop - self->ul_stksiz);
1997c478bdstevel@tonic-gate		bp->sjs_stack.ss_size = self->ul_stksiz;
2007c478bdstevel@tonic-gate		bp->sjs_stack.ss_flags = 0;
2017c478bdstevel@tonic-gate	}
2027c478bdstevel@tonic-gate	if (savemask) {
2037c478bdstevel@tonic-gate		bp->sjs_flags |= JB_SAVEMASK;
2047c478bdstevel@tonic-gate		enter_critical(self);
2057c478bdstevel@tonic-gate		bp->sjs_sigmask = self->ul_sigmask;
2067c478bdstevel@tonic-gate		exit_critical(self);
2077c478bdstevel@tonic-gate	}
2087c478bdstevel@tonic-gate
2097c478bdstevel@tonic-gate	return (0);
2107c478bdstevel@tonic-gate}
211