signalcontext.c revision 256281
1129199Scognet/* 2137550Scognet * Copyright (c) 2004 Marcel Moolenaar, Peter Grehan 3142591Scognet * All rights reserved. 4161105Scognet * 5239268Sgonzo * Redistribution and use in source and binary forms, with or without 6238189Simp * modification, are permitted provided that the following conditions 7147115Scognet * are met: 8239268Sgonzo * 9239268Sgonzo * 1. Redistributions of source code must retain the above copyright 10153277Scognet * notice, this list of conditions and the following disclaimer. 11239268Sgonzo * 2. Redistributions in binary form must reproduce the above copyright 12239268Sgonzo * notice, this list of conditions and the following disclaimer in the 13244480Sgonzo * documentation and/or other materials provided with the distribution. 14244480Sgonzo * 15239268Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16266058Sian * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17239268Sgonzo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18239268Sgonzo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19239268Sgonzo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20135671Scognet * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21135671Scognet * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22170583Scognet * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23135671Scognet * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24170583Scognet * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25164427Ssam */ 26186352Ssam 27179595Sbenno#include <sys/cdefs.h> 28166820Scognet__FBSDID("$FreeBSD: stable/10/lib/libc/powerpc/gen/signalcontext.c 132399 2004-07-19 12:08:03Z grehan $"); 29239268Sgonzo 30239268Sgonzo#include <sys/param.h> 31237042Simp#include <sys/ucontext.h> 32189651Ssam#include <signal.h> 33135671Scognet#include <stdlib.h> 34135671Scognet#include <strings.h> 35237044Simp 36166820Scognettypedef void (*handler_t)(uint32_t, uint32_t, uint32_t); 37239268Sgonzo 38158531Scognet/* Prototypes */ 39250634Sgberstatic void ctx_wrapper(ucontext_t *ucp, handler_t func, uint32_t sig, 40234004Sstas uint32_t sig_si, uint32_t sig_uc); 41239268Sgonzo 42183840Sraj__weak_reference(__signalcontext, signalcontext); 43239268Sgonzo 44239268Sgonzoint 45183840Sraj__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func) 46239268Sgonzo{ 47183840Sraj siginfo_t *sig_si; 48239268Sgonzo ucontext_t *sig_uc; 49239268Sgonzo uint32_t sp; 50239268Sgonzo 51239268Sgonzo /* Bail out if we don't have a valid ucontext pointer. */ 52135671Scognet if (ucp == NULL) 53135671Scognet abort(); 54157567Scognet 55159322Scognet /* 56239268Sgonzo * Build a 16-byte-aligned signal frame 57213510Scognet */ 58213510Scognet sp = (ucp->uc_mcontext.mc_gpr[1] - sizeof(ucontext_t)) & ~15UL; 59213510Scognet sig_uc = (ucontext_t *)sp; 60239696Sgonzo bcopy(ucp, sig_uc, sizeof(*sig_uc)); 61239696Sgonzo sp = (sp - sizeof(siginfo_t)) & ~15UL; 62239696Sgonzo sig_si = (siginfo_t *)sp; 63236535Simp bzero(sig_si, sizeof(*sig_si)); 64254461Sandrew sig_si->si_signo = sig; 65 66 /* 67 * Subtract 8 bytes from stack to allow for frameptr 68 */ 69 sp -= 2*sizeof(uint32_t); 70 sp &= ~15UL; 71 72 /* 73 * Setup the ucontext of the signal handler. 74 */ 75 bzero(&ucp->uc_mcontext, sizeof(ucp->uc_mcontext)); 76 ucp->uc_link = sig_uc; 77 sigdelset(&ucp->uc_sigmask, sig); 78 79 ucp->uc_mcontext.mc_vers = _MC_VERSION; 80 ucp->uc_mcontext.mc_len = sizeof(struct __mcontext); 81 ucp->uc_mcontext.mc_srr0 = (uint32_t) ctx_wrapper; 82 ucp->uc_mcontext.mc_gpr[1] = (uint32_t) sp; 83 ucp->uc_mcontext.mc_gpr[3] = (uint32_t) func; 84 ucp->uc_mcontext.mc_gpr[4] = (uint32_t) sig; 85 ucp->uc_mcontext.mc_gpr[5] = (uint32_t) sig_si; 86 ucp->uc_mcontext.mc_gpr[6] = (uint32_t) sig_uc; 87 88 return (0); 89} 90 91static void 92ctx_wrapper(ucontext_t *ucp, handler_t func, uint32_t sig, uint32_t sig_si, 93 uint32_t sig_uc) 94{ 95 96 (*func)(sig, sig_si, sig_uc); 97 if (ucp->uc_link == NULL) 98 exit(0); 99 setcontext((const ucontext_t *)ucp->uc_link); 100 /* should never get here */ 101 abort(); 102 /* NOTREACHED */ 103} 104