2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include "lint.h"
2N/A#include "thr_uberdata.h"
2N/A#include <procfs.h>
2N/A#include <setjmp.h>
2N/A#include <sys/fsr.h>
2N/A#include "sigjmp_struct.h"
2N/A
2N/Aextern int getlwpstatus(thread_t, lwpstatus_t *);
2N/Aextern int putlwpregs(thread_t, prgregset_t);
2N/A
2N/A/* ARGSUSED2 */
2N/Avoid *
2N/Asetup_top_frame(void *stk, size_t stksize, ulwp_t *ulwp)
2N/A{
2N/A uintptr_t stack;
2N/A char frame[SA(MINFRAME)];
2N/A
2N/A /*
2N/A * Top-of-stack must be rounded down to STACK_ALIGN and
2N/A * there must be a minimum frame for the register window.
2N/A */
2N/A stack = (((uintptr_t)stk + stksize) & ~(STACK_ALIGN - 1)) -
2N/A SA(MINFRAME);
2N/A
2N/A /*
2N/A * This will return NULL if the kernel cannot allocate
2N/A * a page for the top page of the stack. This will cause
2N/A * thr_create(), pthread_create() or pthread_attr_setstack()
2N/A * to fail, passing the problem up to the application.
2N/A */
2N/A (void) memset(frame, 0, sizeof (frame));
2N/A if (uucopy(frame, (void *)stack, sizeof (frame)) == 0)
2N/A return ((void *)stack);
2N/A return (NULL);
2N/A}
2N/A
2N/Aint
2N/Asetup_context(ucontext_t *ucp, void *(*func)(ulwp_t *),
2N/A ulwp_t *ulwp, caddr_t stk, size_t stksize)
2N/A{
2N/A uintptr_t stack;
2N/A
2N/A /* clear the context */
2N/A (void) memset(ucp, 0, sizeof (*ucp));
2N/A
2N/A /*
2N/A * Clear the top stack frame.
2N/A * If this fails, pass the problem up to the application.
2N/A */
2N/A if ((stack = (uintptr_t)setup_top_frame(stk, stksize, ulwp)) == NULL)
2N/A return (ENOMEM);
2N/A
2N/A /* fill in registers of interest */
2N/A ucp->uc_flags |= UC_CPU;
2N/A ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
2N/A ucp->uc_mcontext.gregs[REG_nPC] = (greg_t)func + 4;
2N/A ucp->uc_mcontext.gregs[REG_O0] = (greg_t)ulwp;
2N/A ucp->uc_mcontext.gregs[REG_SP] = (greg_t)(stack - STACK_BIAS);
2N/A ucp->uc_mcontext.gregs[REG_O7] = (greg_t)_lwp_start;
2N/A ucp->uc_mcontext.gregs[REG_G7] = (greg_t)ulwp;
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Machine-dependent startup code for a newly-created thread.
2N/A */
2N/Avoid *
2N/A_thrp_setup(ulwp_t *self)
2N/A{
2N/A extern void _setfsr(greg_t *);
2N/A
2N/A if (self->ul_fpuenv.fpu_en)
2N/A _setfsr(&self->ul_fpuenv.fsr);
2N/A
2N/A self->ul_ustack.ss_sp = (void *)(self->ul_stktop - self->ul_stksiz);
2N/A self->ul_ustack.ss_size = self->ul_stksiz;
2N/A self->ul_ustack.ss_flags = 0;
2N/A (void) setustack(&self->ul_ustack);
2N/A
2N/A update_sched(self);
2N/A tls_setup();
2N/A
2N/A /* signals have been deferred until now */
2N/A sigon(self);
2N/A
2N/A if (self->ul_cancel_pending == 2 && !self->ul_cancel_disabled)
2N/A return (NULL); /* cancelled by pthread_create() */
2N/A return (self->ul_startpc(self->ul_startarg));
2N/A}
2N/A
2N/Avoid
2N/A_fpinherit(ulwp_t *ulwp)
2N/A{
2N/A extern void _getfsr(greg_t *);
2N/A int fpu_enabled;
2N/A
2N/A#ifdef __sparcv9
2N/A extern greg_t _getfprs();
2N/A fpu_enabled = _getfprs() & FPRS_FEF;
2N/A#else
2N/A extern psw_t _getpsr();
2N/A fpu_enabled = _getpsr() & PSR_EF;
2N/A#endif /* __sparcv9 */
2N/A
2N/A if (fpu_enabled) {
2N/A _getfsr(&ulwp->ul_fpuenv.fsr);
2N/A ulwp->ul_fpuenv.fpu_en = 1;
2N/A } else {
2N/A ulwp->ul_fpuenv.fpu_en = 0;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Agetgregs(ulwp_t *ulwp, gregset_t rs)
2N/A{
2N/A lwpstatus_t status;
2N/A
2N/A if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
2N/A rs[REG_PC] = status.pr_reg[R_PC];
2N/A rs[REG_O6] = status.pr_reg[R_O6];
2N/A rs[REG_O7] = status.pr_reg[R_O7];
2N/A rs[REG_G1] = status.pr_reg[R_G1];
2N/A rs[REG_G2] = status.pr_reg[R_G2];
2N/A rs[REG_G3] = status.pr_reg[R_G3];
2N/A rs[REG_G4] = status.pr_reg[R_G4];
2N/A } else {
2N/A rs[REG_PC] = 0;
2N/A rs[REG_O6] = 0;
2N/A rs[REG_O7] = 0;
2N/A rs[REG_G1] = 0;
2N/A rs[REG_G2] = 0;
2N/A rs[REG_G3] = 0;
2N/A rs[REG_G4] = 0;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Asetgregs(ulwp_t *ulwp, gregset_t rs)
2N/A{
2N/A lwpstatus_t status;
2N/A
2N/A if (getlwpstatus(ulwp->ul_lwpid, &status) == 0) {
2N/A status.pr_reg[R_PC] = rs[REG_PC];
2N/A status.pr_reg[R_O6] = rs[REG_O6];
2N/A status.pr_reg[R_O7] = rs[REG_O7];
2N/A status.pr_reg[R_G1] = rs[REG_G1];
2N/A status.pr_reg[R_G2] = rs[REG_G2];
2N/A status.pr_reg[R_G3] = rs[REG_G3];
2N/A status.pr_reg[R_G4] = rs[REG_G4];
2N/A (void) putlwpregs(ulwp->ul_lwpid, status.pr_reg);
2N/A }
2N/A}
2N/A
2N/Aint
2N/A__csigsetjmp(sigjmp_buf env, int savemask)
2N/A{
2N/A sigjmp_struct_t *bp = (sigjmp_struct_t *)env;
2N/A ulwp_t *self = curthread;
2N/A
2N/A /*
2N/A * bp->sjs_sp, bp->sjs_pc, bp->sjs_fp and bp->sjs_i7 are already set.
2N/A * Also, if we are running in 64-bit mode (__sparcv9),
2N/A * then bp->sjs_asi and bp->sjs_fprs are already set.
2N/A */
2N/A bp->sjs_flags = JB_FRAMEPTR;
2N/A bp->sjs_uclink = self->ul_siglink;
2N/A if (self->ul_ustack.ss_flags & SS_ONSTACK)
2N/A bp->sjs_stack = self->ul_ustack;
2N/A else {
2N/A bp->sjs_stack.ss_sp =
2N/A (void *)(self->ul_stktop - self->ul_stksiz);
2N/A bp->sjs_stack.ss_size = self->ul_stksiz;
2N/A bp->sjs_stack.ss_flags = 0;
2N/A }
2N/A if (savemask) {
2N/A bp->sjs_flags |= JB_SAVEMASK;
2N/A enter_critical(self);
2N/A bp->sjs_sigmask = self->ul_sigmask;
2N/A exit_critical(self);
2N/A }
2N/A
2N/A return (0);
2N/A}