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>
35 typedef void (*func_t)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
39 static void makectx_wrapper(ucontext_t *ucp, func_t func, uint64_t *args);
41 __weak_reference(__makecontext, makecontext);
44 __makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
51 /* A valid context is required. */
52 if ((ucp == NULL) || (ucp->uc_mcontext.mc_len != sizeof(mcontext_t)))
54 else if ((argc < 0) || (argc > 6) || (ucp->uc_stack.ss_sp == NULL) ||
55 (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
57 * This should really return -1 with errno set to ENOMEM
58 * or something, but the spec says that makecontext is
59 * a void function. At least make sure that the context
60 * isn't valid so it can't be used without an error.
62 ucp->uc_mcontext.mc_len = 0;
66 /* Align the stack to 16 bytes. */
67 sp = (uint64_t *)(ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
68 sp = (uint64_t *)((uint64_t)sp & ~15UL);
70 /* Allocate space for a maximum of 6 arguments on the stack. */
74 * Account for arguments on stack and do the funky C entry alignment.
75 * This means that we need an 8-byte-odd alignment since the ABI expects
76 * the return address to be pushed, thus breaking the 16 byte alignment.
80 /* Add the arguments: */
82 for (i = 0; i < argc; i++)
83 args[i] = va_arg(ap, uint64_t);
85 for (i = argc; i < 6; i++)
88 ucp->uc_mcontext.mc_rdi = (register_t)ucp;
89 ucp->uc_mcontext.mc_rsi = (register_t)start;
90 ucp->uc_mcontext.mc_rdx = (register_t)args;
91 ucp->uc_mcontext.mc_rbp = 0;
92 ucp->uc_mcontext.mc_rbx = (register_t)sp;
93 ucp->uc_mcontext.mc_rsp = (register_t)sp;
94 ucp->uc_mcontext.mc_rip = (register_t)makectx_wrapper;
98 makectx_wrapper(ucontext_t *ucp, func_t func, uint64_t *args)
100 (*func)(args[0], args[1], args[2], args[3], args[4], args[5]);
101 if (ucp->uc_link == NULL)
103 setcontext((const ucontext_t *)ucp->uc_link);
104 /* should never get here */