xref: /onnv-gate/usr/src/lib/libc/sparc/threads/machdep.c (revision 7657:59d4b80e7a88)
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
56247Sraf  * Common Development and Distribution License (the "License").
66247Sraf  * 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 /*
236247Sraf  * Copyright 2008 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 "lint.h"
280Sstevel@tonic-gate #include "thr_uberdata.h"
290Sstevel@tonic-gate #include <procfs.h>
300Sstevel@tonic-gate #include <setjmp.h>
310Sstevel@tonic-gate #include <sys/fsr.h>
320Sstevel@tonic-gate #include "sigjmp_struct.h"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
350Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
360Sstevel@tonic-gate 
37*7657SRoger.Faulkner@Sun.COM /* ARGSUSED2 */
38*7657SRoger.Faulkner@Sun.COM void *
39*7657SRoger.Faulkner@Sun.COM setup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
40*7657SRoger.Faulkner@Sun.COM {
41*7657SRoger.Faulkner@Sun.COM 	uintptr_t stack;
42*7657SRoger.Faulkner@Sun.COM 	char frame[SA(MINFRAME)];
43*7657SRoger.Faulkner@Sun.COM 
44*7657SRoger.Faulkner@Sun.COM 	/*
45*7657SRoger.Faulkner@Sun.COM 	 * Top-of-stack must be rounded down to STACK_ALIGN and
46*7657SRoger.Faulkner@Sun.COM 	 * there must be a minimum frame for the register window.
47*7657SRoger.Faulkner@Sun.COM 	 */
48*7657SRoger.Faulkner@Sun.COM 	stack = (((uintptr_t)stk + stksize) & ~(STACK_ALIGN - 1)) -
49*7657SRoger.Faulkner@Sun.COM 	    SA(MINFRAME);
50*7657SRoger.Faulkner@Sun.COM 
51*7657SRoger.Faulkner@Sun.COM 	/*
52*7657SRoger.Faulkner@Sun.COM 	 * This will return NULL if the kernel cannot allocate
53*7657SRoger.Faulkner@Sun.COM 	 * a page for the top page of the stack.  This will cause
54*7657SRoger.Faulkner@Sun.COM 	 * thr_create(), pthread_create() or pthread_attr_setstack()
55*7657SRoger.Faulkner@Sun.COM 	 * to fail, passing the problem up to the application.
56*7657SRoger.Faulkner@Sun.COM 	 */
57*7657SRoger.Faulkner@Sun.COM 	(void) memset(frame, 0, sizeof (frame));
58*7657SRoger.Faulkner@Sun.COM 	if (uucopy(frame, (void *)stack, sizeof (frame)) == 0)
59*7657SRoger.Faulkner@Sun.COM 		return ((void *)stack);
60*7657SRoger.Faulkner@Sun.COM 	return (NULL);
61*7657SRoger.Faulkner@Sun.COM }
62*7657SRoger.Faulkner@Sun.COM 
630Sstevel@tonic-gate int
640Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
650Sstevel@tonic-gate 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
660Sstevel@tonic-gate {
67*7657SRoger.Faulkner@Sun.COM 	uintptr_t stack;
68*7657SRoger.Faulkner@Sun.COM 
69*7657SRoger.Faulkner@Sun.COM 	/* clear the context */
70*7657SRoger.Faulkner@Sun.COM 	(void) memset(ucp, 0, sizeof (*ucp));
71*7657SRoger.Faulkner@Sun.COM 
720Sstevel@tonic-gate 	/*
73*7657SRoger.Faulkner@Sun.COM 	 * Clear the top stack frame.
74*7657SRoger.Faulkner@Sun.COM 	 * If this fails, pass the problem up to the application.
750Sstevel@tonic-gate 	 */
76*7657SRoger.Faulkner@Sun.COM 	if ((stack = (uintptr_t)setup_top_frame(stk, stksize, ulwp)) == NULL)
77*7657SRoger.Faulkner@Sun.COM 		return (ENOMEM);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	/* fill in registers of interest */
800Sstevel@tonic-gate 	ucp->uc_flags |= UC_CPU;
810Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
820Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_nPC] = (greg_t)func + 4;
830Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_O0] = (greg_t)ulwp;
840Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_SP] = (greg_t)(stack - STACK_BIAS);
850Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_O7] = (greg_t)_lwp_start;
860Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_G7] = (greg_t)ulwp;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	return (0);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * Machine-dependent startup code for a newly-created thread.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate void *
956812Sraf _thrp_setup(ulwp_t *self)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	extern void _setfsr(greg_t *);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if (self->ul_fpuenv.fpu_en)
1000Sstevel@tonic-gate 		_setfsr(&self->ul_fpuenv.fsr);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
1030Sstevel@tonic-gate 	self->ul_ustack.ss_size = self->ul_stksiz;
1040Sstevel@tonic-gate 	self->ul_ustack.ss_flags = 0;
1056515Sraf 	(void) setustack(&self->ul_ustack);
1066247Sraf 
1076247Sraf 	update_sched(self);
1080Sstevel@tonic-gate 	tls_setup();
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	/* signals have been deferred until now */
1110Sstevel@tonic-gate 	sigon(self);
1120Sstevel@tonic-gate 
1136247Sraf 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
1146247Sraf 		return (NULL);	/* cancelled by pthread_create() */
1150Sstevel@tonic-gate 	return (self->ul_startpc(self->ul_startarg));
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate void
1190Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	extern void _getfsr(greg_t *);
1220Sstevel@tonic-gate 	int fpu_enabled;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate #ifdef __sparcv9
1250Sstevel@tonic-gate 	extern greg_t _getfprs();
1260Sstevel@tonic-gate 	fpu_enabled = _getfprs() & FPRS_FEF;
1270Sstevel@tonic-gate #else
1280Sstevel@tonic-gate 	extern psw_t _getpsr();
1290Sstevel@tonic-gate 	fpu_enabled = _getpsr() & PSR_EF;
1300Sstevel@tonic-gate #endif /* __sparcv9 */
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (fpu_enabled) {
1330Sstevel@tonic-gate 		_getfsr(&ulwp->ul_fpuenv.fsr);
1340Sstevel@tonic-gate 		ulwp->ul_fpuenv.fpu_en = 1;
1350Sstevel@tonic-gate 	} else {
1360Sstevel@tonic-gate 		ulwp->ul_fpuenv.fpu_en = 0;
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate void
1410Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	lwpstatus_t status;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1460Sstevel@tonic-gate 		rs[REG_PC] = status.pr_reg[R_PC];
1470Sstevel@tonic-gate 		rs[REG_O6] = status.pr_reg[R_O6];
1480Sstevel@tonic-gate 		rs[REG_O7] = status.pr_reg[R_O7];
1490Sstevel@tonic-gate 		rs[REG_G1] = status.pr_reg[R_G1];
1500Sstevel@tonic-gate 		rs[REG_G2] = status.pr_reg[R_G2];
1510Sstevel@tonic-gate 		rs[REG_G3] = status.pr_reg[R_G3];
1520Sstevel@tonic-gate 		rs[REG_G4] = status.pr_reg[R_G4];
1530Sstevel@tonic-gate 	} else {
1540Sstevel@tonic-gate 		rs[REG_PC] = 0;
1550Sstevel@tonic-gate 		rs[REG_O6] = 0;
1560Sstevel@tonic-gate 		rs[REG_O7] = 0;
1570Sstevel@tonic-gate 		rs[REG_G1] = 0;
1580Sstevel@tonic-gate 		rs[REG_G2] = 0;
1590Sstevel@tonic-gate 		rs[REG_G3] = 0;
1600Sstevel@tonic-gate 		rs[REG_G4] = 0;
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate void
1650Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	lwpstatus_t status;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1700Sstevel@tonic-gate 		status.pr_reg[R_PC] = rs[REG_PC];
1710Sstevel@tonic-gate 		status.pr_reg[R_O6] = rs[REG_O6];
1720Sstevel@tonic-gate 		status.pr_reg[R_O7] = rs[REG_O7];
1730Sstevel@tonic-gate 		status.pr_reg[R_G1] = rs[REG_G1];
1740Sstevel@tonic-gate 		status.pr_reg[R_G2] = rs[REG_G2];
1750Sstevel@tonic-gate 		status.pr_reg[R_G3] = rs[REG_G3];
1760Sstevel@tonic-gate 		status.pr_reg[R_G4] = rs[REG_G4];
1770Sstevel@tonic-gate 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate int
1820Sstevel@tonic-gate __csigsetjmp(sigjmp_buf env, int savemask)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	sigjmp_struct_t *bp = (sigjmp_struct_t *)env;
1850Sstevel@tonic-gate 	ulwp_t *self = curthread;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	/*
1880Sstevel@tonic-gate 	 * bp->sjs_sp, bp->sjs_pc, bp->sjs_fp and bp->sjs_i7 are already set.
1890Sstevel@tonic-gate 	 */
1900Sstevel@tonic-gate 	bp->sjs_flags = JB_FRAMEPTR;
1910Sstevel@tonic-gate 	bp->sjs_uclink = self->ul_siglink;
1920Sstevel@tonic-gate 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
1930Sstevel@tonic-gate 		bp->sjs_stack = self->ul_ustack;
1940Sstevel@tonic-gate 	else {
1950Sstevel@tonic-gate 		bp->sjs_stack.ss_sp =
1966247Sraf 		    (void *)(self->ul_stktop - self->ul_stksiz);
1970Sstevel@tonic-gate 		bp->sjs_stack.ss_size = self->ul_stksiz;
1980Sstevel@tonic-gate 		bp->sjs_stack.ss_flags = 0;
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 	if (savemask) {
2010Sstevel@tonic-gate 		bp->sjs_flags |= JB_SAVEMASK;
2020Sstevel@tonic-gate 		enter_critical(self);
2030Sstevel@tonic-gate 		bp->sjs_sigmask = self->ul_sigmask;
2040Sstevel@tonic-gate 		exit_critical(self);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	return (0);
2080Sstevel@tonic-gate }
209