2 * Copyright (c) 2003 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/ucontext.h>
32 #include <machine/fpu.h>
43 typedef void (*func_t)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
44 uint64_t, uint64_t, uint64_t);
46 static __inline uint64_t *
47 spill(uint64_t *bsp, uint64_t arg)
50 if (((intptr_t)bsp & 0x1ff) == 0x1f8)
56 ctx_wrapper(ucontext_t *ucp, func_t func, uint64_t *args)
59 (*func)(args[0], args[1], args[2], args[3], args[4], args[5], args[6],
61 if (ucp->uc_link == NULL)
63 setcontext((const ucontext_t *)ucp->uc_link);
64 /* should never get here */
69 __weak_reference(__makecontext, makecontext);
72 __makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
79 * Drop the ball completely if something's not right. We only
80 * support general registers as arguments and not more than 8
81 * of them. Things get hairy if we need to support FP registers
82 * (alignment issues) or more than 8 arguments (stack based).
84 if (argc < 0 || argc > 8 || ucp == NULL ||
85 ucp->uc_stack.ss_sp == NULL || (ucp->uc_stack.ss_size & 15) ||
86 ((intptr_t)ucp->uc_stack.ss_sp & 15) ||
87 ucp->uc_stack.ss_size < MINSIGSTKSZ)
91 * Copy the arguments of function 'func' onto the (memory) stack.
92 * Always take up space for 8 arguments.
95 args = (uint64_t*)(ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size) - 8;
98 args[i++] = va_arg(ap, uint64_t);
104 * Push (spill) the arguments of the context wrapper onto the register
105 * stack. They get loaded by the RSE on a context switch.
107 bsp = (uint64_t*)ucp->uc_stack.ss_sp;
108 bsp = spill(bsp, (intptr_t)ucp);
109 bsp = spill(bsp, (intptr_t)func);
110 bsp = spill(bsp, (intptr_t)args);
113 * Setup the MD portion of the context.
115 memset(&ucp->uc_mcontext, 0, sizeof(ucp->uc_mcontext));
116 ucp->uc_mcontext.mc_special.sp = (intptr_t)args - 16;
117 ucp->uc_mcontext.mc_special.bspstore = (intptr_t)bsp;
118 ucp->uc_mcontext.mc_special.pfs = (3 << 7) | 3;
119 ucp->uc_mcontext.mc_special.rsc = 0xf;
120 ucp->uc_mcontext.mc_special.rp = ((struct fdesc*)ctx_wrapper)->ip;
121 ucp->uc_mcontext.mc_special.gp = ((struct fdesc*)ctx_wrapper)->gp;
122 ucp->uc_mcontext.mc_special.fpsr = IA64_FPSR_DEFAULT;