makecontext.c revision 115518
1/* 2 * Copyright (c) 2003 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 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/makecontext.c 115518 2003-05-31 19:42:51Z marcel $"); 29 30#include <sys/types.h> 31#include <sys/ucontext.h> 32#include <machine/fpu.h> 33#include <stdarg.h> 34#include <stdio.h> 35 36struct fdesc { 37 uint64_t ip; 38 uint64_t gp; 39}; 40 41typedef void (*func_t)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, 42 uint64_t, uint64_t, uint64_t); 43 44static __inline uint64_t * 45spill(uint64_t *bsp, uint64_t arg) 46{ 47 *bsp++ = arg; 48 if (((intptr_t)bsp & 0x1ff) == 0x1f8) 49 *bsp++ = 0; 50 return (bsp); 51} 52 53static void 54ctx_wrapper(ucontext_t *ucp, func_t func, uint64_t *args) 55{ 56 57 (*func)(args[0], args[1], args[2], args[3], args[4], args[5], args[6], 58 args[7]); 59 if (ucp->uc_link == NULL) 60 exit(0); 61 setcontext((const ucontext_t *)ucp->uc_link); 62 /* should never get here */ 63 abort(); 64 /* NOTREACHED */ 65} 66 67__weak_reference(__makecontext, makecontext); 68 69void 70__makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) 71{ 72 uint64_t *args, *bsp; 73 va_list ap; 74 int i; 75 76 /* 77 * Drop the ball completely if something's not right. We only 78 * support general registers as arguments and not more than 8 79 * of them. Things get hairy if we need to support FP registers 80 * (alignment issues) or more than 8 arguments (stack based). 81 */ 82 if (argc < 0 || argc > 8 || ucp == NULL || 83 ucp->uc_stack.ss_sp == NULL || (ucp->uc_stack.ss_size & 15) || 84 ((intptr_t)ucp->uc_stack.ss_sp & 15) || 85 ucp->uc_stack.ss_size < MINSIGSTKSZ) 86 abort(); 87 88 /* 89 * Copy the arguments of function 'func' onto the (memory) stack. 90 * Always take up space for 8 arguments. 91 */ 92 va_start(ap, argc); 93 args = (uint64_t*)(ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size) - 8; 94 i = 0; 95 while (i < argc) 96 args[i++] = va_arg(ap, uint64_t); 97 while (i < 8) 98 args[i++] = 0; 99 va_end(ap); 100 101 /* 102 * Push (spill) the arguments of the context wrapper onto the register 103 * stack. They get loaded by the RSE on a context switch. 104 */ 105 bsp = (uint64_t*)ucp->uc_stack.ss_sp; 106 bsp = spill(bsp, (intptr_t)ucp); 107 bsp = spill(bsp, (intptr_t)func); 108 bsp = spill(bsp, (intptr_t)args); 109 110 /* 111 * Setup the MD portion of the context. 112 */ 113 memset(&ucp->uc_mcontext, 0, sizeof(ucp->uc_mcontext)); 114 ucp->uc_mcontext.mc_special.sp = (intptr_t)args - 16; 115 ucp->uc_mcontext.mc_special.bspstore = (intptr_t)bsp; 116 ucp->uc_mcontext.mc_special.pfs = 3; 117 ucp->uc_mcontext.mc_special.rsc = 0xf; 118 ucp->uc_mcontext.mc_special.rp = ((struct fdesc*)ctx_wrapper)->ip; 119 ucp->uc_mcontext.mc_special.rp = ((struct fdesc*)ctx_wrapper)->gp; 120 ucp->uc_mcontext.mc_special.fpsr = IA64_FPSR_DEFAULT; 121} 122