signalcontext.c revision 124191
172017Scg/* 272017Scg * Copyright (c) 2003 Marcel Moolenaar 374761Speter * All rights reserved. 472017Scg * 574761Speter * Redistribution and use in source and binary forms, with or without 6110498Snyan * modification, are permitted provided that the following conditions 774761Speter * are met: 874761Speter * 972017Scg * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/lib/libc/ia64/gen/signalcontext.c 124191 2004-01-06 19:40:28Z nectar $"); 29 30#include <sys/types.h> 31#include <sys/ucontext.h> 32#include <machine/fpu.h> 33#include <signal.h> 34#include <stdio.h> 35#include <stdlib.h> 36#include <string.h> 37#include <strings.h> 38 39struct fdesc { 40 uint64_t ip; 41 uint64_t gp; 42}; 43 44typedef void (*handler_t)(uint64_t, uint64_t, uint64_t); 45 46static __inline uint64_t * 47spill(uint64_t *bsp, uint64_t arg) 48{ 49 *bsp++ = arg; 50 if (((intptr_t)bsp & 0x1ff) == 0x1f8) 51 *bsp++ = 0; 52 return (bsp); 53} 54 55static void 56ctx_wrapper(ucontext_t *ucp, handler_t func, uint64_t *args) 57{ 58 59 (*func)(args[0], args[1], args[2]); 60 if (ucp->uc_link == NULL) 61 exit(0); 62 setcontext((const ucontext_t *)ucp->uc_link); 63 /* should never get here */ 64 abort(); 65 /* NOTREACHED */ 66} 67 68__weak_reference(__signalcontext, signalcontext); 69 70int 71__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func) 72{ 73 uint64_t *args, *bsp; 74 siginfo_t *sig_si; 75 ucontext_t *sig_uc; 76 uint64_t sp; 77 78 /* Bail out if we don't have a valid ucontext pointer. */ 79 if (ucp == NULL) 80 abort(); 81 82 /* 83 * Build a signal frame and copy the arguments of signal handler 84 * 'func' onto the (memory) stack. We only need 3 arguments, but 85 * we create room for 4 so that we are 16-byte aligned. 86 */ 87 sp = (ucp->uc_mcontext.mc_special.sp - sizeof(ucontext_t)) & ~15UL; 88 sig_uc = (ucontext_t*)sp; 89 bcopy(ucp, sig_uc, sizeof(*sig_uc)); 90 sp = (sp - sizeof(siginfo_t)) & ~15UL; 91 sig_si = (siginfo_t*)sp; 92 bzero(sig_si, sizeof(*sig_si)); 93 sig_si->si_signo = sig; 94 sp -= 4 * sizeof(uint64_t); 95 args = (uint64_t*)sp; 96 args[0] = sig; 97 args[1] = (intptr_t)sig_si; 98 args[2] = (intptr_t)sig_uc; 99 100 /* 101 * Push (spill) the arguments of the context wrapper onto the register 102 * stack. They get loaded by the RSE on a context switch. 103 */ 104 bsp = (uint64_t*)ucp->uc_mcontext.mc_special.bspstore; 105 bsp = spill(bsp, (intptr_t)ucp); 106 bsp = spill(bsp, (intptr_t)func); 107 bsp = spill(bsp, (intptr_t)args); 108 109 /* 110 * Setup the ucontext of the signal handler. 111 */ 112 memset(&ucp->uc_mcontext, 0, sizeof(ucp->uc_mcontext)); 113 ucp->uc_link = sig_uc; 114 sigdelset(&ucp->uc_sigmask, sig); 115 ucp->uc_mcontext.mc_special.sp = (intptr_t)args - 16; 116 ucp->uc_mcontext.mc_special.bspstore = (intptr_t)bsp; 117 ucp->uc_mcontext.mc_special.pfs = (3 << 7) | 3; 118 ucp->uc_mcontext.mc_special.rsc = 0xf; 119 ucp->uc_mcontext.mc_special.rp = ((struct fdesc*)ctx_wrapper)->ip; 120 ucp->uc_mcontext.mc_special.gp = ((struct fdesc*)ctx_wrapper)->gp; 121 ucp->uc_mcontext.mc_special.fpsr = IA64_FPSR_DEFAULT; 122 return (0); 123} 124