17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*7257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
307c478bd9Sstevel@tonic-gate #include <sys/stack.h>
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * Initialization of the main stack is performed in libc_init().
34*7257d1b4Sraf * Initialization of thread stacks is performed in _thrp_setup().
357c478bd9Sstevel@tonic-gate */
367c478bd9Sstevel@tonic-gate
37*7257d1b4Sraf #pragma weak _stack_getbounds = stack_getbounds
387c478bd9Sstevel@tonic-gate int
stack_getbounds(stack_t * sp)39*7257d1b4Sraf stack_getbounds(stack_t *sp)
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate *sp = curthread->ul_ustack;
427c478bd9Sstevel@tonic-gate return (0);
437c478bd9Sstevel@tonic-gate }
447c478bd9Sstevel@tonic-gate
45*7257d1b4Sraf #pragma weak _stack_setbounds = stack_setbounds
467c478bd9Sstevel@tonic-gate int
stack_setbounds(const stack_t * sp)47*7257d1b4Sraf stack_setbounds(const stack_t *sp)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate if (sp == NULL || sp->ss_sp == NULL ||
527c478bd9Sstevel@tonic-gate (uintptr_t)sp->ss_sp != SA((uintptr_t)sp->ss_sp) ||
537c478bd9Sstevel@tonic-gate sp->ss_flags != 0 || sp->ss_size < MINSIGSTKSZ ||
547c478bd9Sstevel@tonic-gate (uintptr_t)sp->ss_size != SA((uintptr_t)sp->ss_size)) {
557c478bd9Sstevel@tonic-gate errno = EINVAL;
567c478bd9Sstevel@tonic-gate return (-1);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate sigoff(self);
607c478bd9Sstevel@tonic-gate self->ul_ustack = *sp;
617c478bd9Sstevel@tonic-gate sigon(self);
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate return (0);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * Returns a boolean value:
687c478bd9Sstevel@tonic-gate * 1 addr is within the bounds of the current stack
697c478bd9Sstevel@tonic-gate * 0 addr is outside of the bounds of the current stack
707c478bd9Sstevel@tonic-gate * Note that addr is an unbiased value.
717c478bd9Sstevel@tonic-gate */
72*7257d1b4Sraf #pragma weak _stack_inbounds = stack_inbounds
737c478bd9Sstevel@tonic-gate int
stack_inbounds(void * addr)74*7257d1b4Sraf stack_inbounds(void *addr)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate stack_t *ustackp = &curthread->ul_ustack;
777c478bd9Sstevel@tonic-gate uintptr_t base = (uintptr_t)ustackp->ss_sp;
787c478bd9Sstevel@tonic-gate size_t size = ustackp->ss_size;
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate return ((uintptr_t)addr >= base && (uintptr_t)addr < base + size);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
83*7257d1b4Sraf #pragma weak _stack_violation = stack_violation
847c478bd9Sstevel@tonic-gate int
stack_violation(int sig,const siginfo_t * sip,const ucontext_t * ucp)85*7257d1b4Sraf stack_violation(int sig, const siginfo_t *sip, const ucontext_t *ucp)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate uintptr_t addr;
887c478bd9Sstevel@tonic-gate uintptr_t base;
897c478bd9Sstevel@tonic-gate size_t size;
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate if ((sig != SIGSEGV && sig != SIGBUS) ||
927c478bd9Sstevel@tonic-gate sip == NULL || ucp == NULL || SI_FROMUSER(sip))
937c478bd9Sstevel@tonic-gate return (0);
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate * ucp has the correct view of the stack when the signal was raised.
977c478bd9Sstevel@tonic-gate */
987c478bd9Sstevel@tonic-gate base = (uintptr_t)ucp->uc_stack.ss_sp;
997c478bd9Sstevel@tonic-gate size = ucp->uc_stack.ss_size;
1007c478bd9Sstevel@tonic-gate #if defined(__sparc)
1017c478bd9Sstevel@tonic-gate addr = ucp->uc_mcontext.gregs[REG_SP] + STACK_BIAS;
1027c478bd9Sstevel@tonic-gate #elif defined(__amd64) || defined(__i386)
1037c478bd9Sstevel@tonic-gate addr = ucp->uc_mcontext.gregs[REG_SP];
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate * If the faulted address is just below the stack pointer we
1067c478bd9Sstevel@tonic-gate * might be looking at a push instruction that caused the fault
1077c478bd9Sstevel@tonic-gate * (the largest amount a push instruction can decrement the
1087c478bd9Sstevel@tonic-gate * stack pointer by is 32). In that case, use the faulted
1097c478bd9Sstevel@tonic-gate * address in our computation rather than the stack pointer.
1107c478bd9Sstevel@tonic-gate */
1117c478bd9Sstevel@tonic-gate if (addr - (uintptr_t)sip->si_addr < 32)
1127c478bd9Sstevel@tonic-gate addr = (uintptr_t)sip->si_addr;
1137c478bd9Sstevel@tonic-gate #else
1147c478bd9Sstevel@tonic-gate #error "none of __sparc, __amd64, __i386 is defined"
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate return (!(addr >= base && addr < base + size));
1177c478bd9Sstevel@tonic-gate }
118