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$");
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#include <stdlib.h>
36#include <string.h>
37
38struct fdesc {
39	uint64_t ip;
40	uint64_t gp;
41};
42
43typedef void (*func_t)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
44    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, func_t func, uint64_t *args)
57{
58
59	(*func)(args[0], args[1], args[2], args[3], args[4], args[5], args[6],
60	    args[7]);
61	if (ucp->uc_link == NULL)
62		exit(0);
63	setcontext((const ucontext_t *)ucp->uc_link);
64	/* should never get here */
65	abort();
66	/* NOTREACHED */
67}
68
69__weak_reference(__makecontext, makecontext);
70
71void
72__makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
73{
74	uint64_t *args, *bsp;
75	va_list ap;
76	int i;
77
78	/*
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).
83	 */
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)
88		abort();
89
90	/*
91	 * Copy the arguments of function 'func' onto the (memory) stack.
92	 * Always take up space for 8 arguments.
93	 */
94	va_start(ap, argc);
95	args = (uint64_t*)(ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size) - 8;
96	i = 0;
97	while (i < argc)
98		args[i++] = va_arg(ap, uint64_t);
99	while (i < 8)
100		args[i++] = 0;
101	va_end(ap);
102
103	/*
104	 * Push (spill) the arguments of the context wrapper onto the register
105	 * stack. They get loaded by the RSE on a context switch.
106	 */
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);
111
112	/*
113	 * Setup the MD portion of the context.
114	 */
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;
123}
124