xref: /freebsd/lib/libc/amd64/gen/makecontext.c (revision 09a53ad8f1318c5daae6cfb19d97f4f6459f0013)
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 <stdarg.h>
33 #include <stdlib.h>
34 
35 typedef void (*func_t)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
36     uint64_t);
37 
38 /* Prototypes */
39 static void makectx_wrapper(ucontext_t *ucp, func_t func, uint64_t *args);
40 
41 __weak_reference(__makecontext, makecontext);
42 
43 void
44 __makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
45 {
46 	uint64_t *args;
47 	uint64_t *sp;
48 	va_list ap;
49 	int i;
50 
51 	/* A valid context is required. */
52 	if ((ucp == NULL) || (ucp->uc_mcontext.mc_len != sizeof(mcontext_t)))
53 		return;
54 	else if ((argc < 0) || (argc > 6) || (ucp->uc_stack.ss_sp == NULL) ||
55 	    (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
56 		/*
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.
61 		 */
62 		ucp->uc_mcontext.mc_len = 0;
63 		return;
64 	}
65 
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);
69 
70 	/* Allocate space for a maximum of 6 arguments on the stack. */
71 	args = sp - 6;
72 
73 	/*
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.
77 	 */
78 	sp -= 7;
79 
80 	/* Add the arguments: */
81 	va_start(ap, argc);
82 	for (i = 0; i < argc; i++)
83 		args[i] = va_arg(ap, uint64_t);
84 	va_end(ap);
85 	for (i = argc; i < 6; i++)
86 		args[i] = 0;
87 
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;
95 }
96 
97 static void
98 makectx_wrapper(ucontext_t *ucp, func_t func, uint64_t *args)
99 {
100 	(*func)(args[0], args[1], args[2], args[3], args[4], args[5]);
101 	if (ucp->uc_link == NULL)
102 		exit(0);
103 	setcontext((const ucontext_t *)ucp->uc_link);
104 	/* should never get here */
105 	abort();
106 	/* NOTREACHED */
107 }
108