xref: /onnv-gate/usr/src/lib/libc/sparc/threads/machdep.c (revision 12061:36363ca8fb06)
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 /*
23*12061SRoger.Faulkner@Oracle.COM  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include "lint.h"
270Sstevel@tonic-gate #include "thr_uberdata.h"
280Sstevel@tonic-gate #include <procfs.h>
290Sstevel@tonic-gate #include <setjmp.h>
300Sstevel@tonic-gate #include <sys/fsr.h>
310Sstevel@tonic-gate #include "sigjmp_struct.h"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate extern int getlwpstatus(thread_t, lwpstatus_t *);
340Sstevel@tonic-gate extern int putlwpregs(thread_t, prgregset_t);
350Sstevel@tonic-gate 
367657SRoger.Faulkner@Sun.COM /* ARGSUSED2 */
377657SRoger.Faulkner@Sun.COM void *
setup_top_frame(void * stk,size_t stksize,ulwp_t * ulwp)387657SRoger.Faulkner@Sun.COM setup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
397657SRoger.Faulkner@Sun.COM {
407657SRoger.Faulkner@Sun.COM 	uintptr_t stack;
417657SRoger.Faulkner@Sun.COM 	char frame[SA(MINFRAME)];
427657SRoger.Faulkner@Sun.COM 
437657SRoger.Faulkner@Sun.COM 	/*
447657SRoger.Faulkner@Sun.COM 	 * Top-of-stack must be rounded down to STACK_ALIGN and
457657SRoger.Faulkner@Sun.COM 	 * there must be a minimum frame for the register window.
467657SRoger.Faulkner@Sun.COM 	 */
477657SRoger.Faulkner@Sun.COM 	stack = (((uintptr_t)stk + stksize) & ~(STACK_ALIGN - 1)) -
487657SRoger.Faulkner@Sun.COM 	    SA(MINFRAME);
497657SRoger.Faulkner@Sun.COM 
507657SRoger.Faulkner@Sun.COM 	/*
517657SRoger.Faulkner@Sun.COM 	 * This will return NULL if the kernel cannot allocate
527657SRoger.Faulkner@Sun.COM 	 * a page for the top page of the stack.  This will cause
537657SRoger.Faulkner@Sun.COM 	 * thr_create(), pthread_create() or pthread_attr_setstack()
547657SRoger.Faulkner@Sun.COM 	 * to fail, passing the problem up to the application.
557657SRoger.Faulkner@Sun.COM 	 */
567657SRoger.Faulkner@Sun.COM 	(void) memset(frame, 0, sizeof (frame));
577657SRoger.Faulkner@Sun.COM 	if (uucopy(frame, (void *)stack, sizeof (frame)) == 0)
587657SRoger.Faulkner@Sun.COM 		return ((void *)stack);
597657SRoger.Faulkner@Sun.COM 	return (NULL);
607657SRoger.Faulkner@Sun.COM }
617657SRoger.Faulkner@Sun.COM 
620Sstevel@tonic-gate int
setup_context(ucontext_t * ucp,void * (* func)(ulwp_t *),ulwp_t * ulwp,caddr_t stk,size_t stksize)630Sstevel@tonic-gate setup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
640Sstevel@tonic-gate 	ulwp_t *ulwp, caddr_t stk, size_t stksize)
650Sstevel@tonic-gate {
667657SRoger.Faulkner@Sun.COM 	uintptr_t stack;
677657SRoger.Faulkner@Sun.COM 
687657SRoger.Faulkner@Sun.COM 	/* clear the context */
697657SRoger.Faulkner@Sun.COM 	(void) memset(ucp, 0, sizeof (*ucp));
707657SRoger.Faulkner@Sun.COM 
710Sstevel@tonic-gate 	/*
727657SRoger.Faulkner@Sun.COM 	 * Clear the top stack frame.
737657SRoger.Faulkner@Sun.COM 	 * If this fails, pass the problem up to the application.
740Sstevel@tonic-gate 	 */
757657SRoger.Faulkner@Sun.COM 	if ((stack = (uintptr_t)setup_top_frame(stk, stksize, ulwp)) == NULL)
767657SRoger.Faulkner@Sun.COM 		return (ENOMEM);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	/* fill in registers of interest */
790Sstevel@tonic-gate 	ucp->uc_flags |= UC_CPU;
800Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
810Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_nPC] = (greg_t)func + 4;
820Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_O0] = (greg_t)ulwp;
830Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_SP] = (greg_t)(stack - STACK_BIAS);
840Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_O7] = (greg_t)_lwp_start;
850Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_G7] = (greg_t)ulwp;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	return (0);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * Machine-dependent startup code for a newly-created thread.
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate void *
_thrp_setup(ulwp_t * self)946812Sraf _thrp_setup(ulwp_t *self)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	extern void _setfsr(greg_t *);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (self->ul_fpuenv.fpu_en)
990Sstevel@tonic-gate 		_setfsr(&self->ul_fpuenv.fsr);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
1020Sstevel@tonic-gate 	self->ul_ustack.ss_size = self->ul_stksiz;
1030Sstevel@tonic-gate 	self->ul_ustack.ss_flags = 0;
1046515Sraf 	(void) setustack(&self->ul_ustack);
1056247Sraf 
1066247Sraf 	update_sched(self);
1070Sstevel@tonic-gate 	tls_setup();
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	/* signals have been deferred until now */
1100Sstevel@tonic-gate 	sigon(self);
1110Sstevel@tonic-gate 
1126247Sraf 	if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
1136247Sraf 		return (NULL);	/* cancelled by pthread_create() */
1140Sstevel@tonic-gate 	return (self->ul_startpc(self->ul_startarg));
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate void
_fpinherit(ulwp_t * ulwp)1180Sstevel@tonic-gate _fpinherit(ulwp_t *ulwp)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	extern void _getfsr(greg_t *);
1210Sstevel@tonic-gate 	int fpu_enabled;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #ifdef __sparcv9
1240Sstevel@tonic-gate 	extern greg_t _getfprs();
1250Sstevel@tonic-gate 	fpu_enabled = _getfprs() & FPRS_FEF;
1260Sstevel@tonic-gate #else
1270Sstevel@tonic-gate 	extern psw_t _getpsr();
1280Sstevel@tonic-gate 	fpu_enabled = _getpsr() & PSR_EF;
1290Sstevel@tonic-gate #endif /* __sparcv9 */
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	if (fpu_enabled) {
1320Sstevel@tonic-gate 		_getfsr(&ulwp->ul_fpuenv.fsr);
1330Sstevel@tonic-gate 		ulwp->ul_fpuenv.fpu_en = 1;
1340Sstevel@tonic-gate 	} else {
1350Sstevel@tonic-gate 		ulwp->ul_fpuenv.fpu_en = 0;
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate void
getgregs(ulwp_t * ulwp,gregset_t rs)1400Sstevel@tonic-gate getgregs(ulwp_t *ulwp, gregset_t rs)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	lwpstatus_t status;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1450Sstevel@tonic-gate 		rs[REG_PC] = status.pr_reg[R_PC];
1460Sstevel@tonic-gate 		rs[REG_O6] = status.pr_reg[R_O6];
1470Sstevel@tonic-gate 		rs[REG_O7] = status.pr_reg[R_O7];
1480Sstevel@tonic-gate 		rs[REG_G1] = status.pr_reg[R_G1];
1490Sstevel@tonic-gate 		rs[REG_G2] = status.pr_reg[R_G2];
1500Sstevel@tonic-gate 		rs[REG_G3] = status.pr_reg[R_G3];
1510Sstevel@tonic-gate 		rs[REG_G4] = status.pr_reg[R_G4];
1520Sstevel@tonic-gate 	} else {
1530Sstevel@tonic-gate 		rs[REG_PC] = 0;
1540Sstevel@tonic-gate 		rs[REG_O6] = 0;
1550Sstevel@tonic-gate 		rs[REG_O7] = 0;
1560Sstevel@tonic-gate 		rs[REG_G1] = 0;
1570Sstevel@tonic-gate 		rs[REG_G2] = 0;
1580Sstevel@tonic-gate 		rs[REG_G3] = 0;
1590Sstevel@tonic-gate 		rs[REG_G4] = 0;
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate void
setgregs(ulwp_t * ulwp,gregset_t rs)1640Sstevel@tonic-gate setgregs(ulwp_t *ulwp, gregset_t rs)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	lwpstatus_t status;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
1690Sstevel@tonic-gate 		status.pr_reg[R_PC] = rs[REG_PC];
1700Sstevel@tonic-gate 		status.pr_reg[R_O6] = rs[REG_O6];
1710Sstevel@tonic-gate 		status.pr_reg[R_O7] = rs[REG_O7];
1720Sstevel@tonic-gate 		status.pr_reg[R_G1] = rs[REG_G1];
1730Sstevel@tonic-gate 		status.pr_reg[R_G2] = rs[REG_G2];
1740Sstevel@tonic-gate 		status.pr_reg[R_G3] = rs[REG_G3];
1750Sstevel@tonic-gate 		status.pr_reg[R_G4] = rs[REG_G4];
1760Sstevel@tonic-gate 		(void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate int
__csigsetjmp(sigjmp_buf env,int savemask)1810Sstevel@tonic-gate __csigsetjmp(sigjmp_buf env, int savemask)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	sigjmp_struct_t *bp = (sigjmp_struct_t *)env;
1840Sstevel@tonic-gate 	ulwp_t *self = curthread;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * bp->sjs_sp, bp->sjs_pc, bp->sjs_fp and bp->sjs_i7 are already set.
188*12061SRoger.Faulkner@Oracle.COM 	 * Also, if we are running in 64-bit mode (__sparcv9),
189*12061SRoger.Faulkner@Oracle.COM 	 * then bp->sjs_asi and bp->sjs_fprs are already set.
1900Sstevel@tonic-gate 	 */
1910Sstevel@tonic-gate 	bp->sjs_flags = JB_FRAMEPTR;
1920Sstevel@tonic-gate 	bp->sjs_uclink = self->ul_siglink;
1930Sstevel@tonic-gate 	if (self->ul_ustack.ss_flags & SS_ONSTACK)
1940Sstevel@tonic-gate 		bp->sjs_stack = self->ul_ustack;
1950Sstevel@tonic-gate 	else {
1960Sstevel@tonic-gate 		bp->sjs_stack.ss_sp =
1976247Sraf 		    (void *)(self->ul_stktop - self->ul_stksiz);
1980Sstevel@tonic-gate 		bp->sjs_stack.ss_size = self->ul_stksiz;
1990Sstevel@tonic-gate 		bp->sjs_stack.ss_flags = 0;
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	if (savemask) {
2020Sstevel@tonic-gate 		bp->sjs_flags |= JB_SAVEMASK;
2030Sstevel@tonic-gate 		enter_critical(self);
2040Sstevel@tonic-gate 		bp->sjs_sigmask = self->ul_sigmask;
2050Sstevel@tonic-gate 		exit_critical(self);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	return (0);
2090Sstevel@tonic-gate }
210