]>
Commit | Line | Data |
---|---|---|
ba1e2fe6 | 1 | /* Create new context. |
f7a9f785 | 2 | Copyright (C) 2002-2016 Free Software Foundation, Inc. |
ba1e2fe6 AJ |
3 | This file is part of the GNU C Library. |
4 | Contributed by Andreas Jaeger <aj@suse.de>, 2002. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
ba1e2fe6 AJ |
19 | |
20 | #include <sysdep.h> | |
21 | #include <stdarg.h> | |
3cc4a097 | 22 | #include <stdint.h> |
ba1e2fe6 AJ |
23 | #include <ucontext.h> |
24 | ||
25 | #include "ucontext_i.h" | |
26 | ||
27 | /* This implementation can handle any ARGC value but only | |
28 | normal integer parameters. | |
29 | makecontext sets up a stack and the registers for the | |
30 | user context. The stack looks like this: | |
31 | +-----------------------+ | |
32 | | next context | | |
33 | +-----------------------+ | |
34 | | parameter 7-n | | |
35 | +-----------------------+ | |
36 | | trampoline address | | |
37 | %rsp -> +-----------------------+ | |
38 | ||
39 | The registers are set up like this: | |
40 | %rdi,%rsi,%rdx,%rcx,%r8,%r9: parameter 1 to 6 | |
41 | %rbx : address of next context | |
42 | %rsp : stack pointer. | |
43 | */ | |
44 | ||
45 | /* XXX: This implementation currently only handles integer arguments. | |
46 | To handle long int and pointer arguments the va_arg arguments needs | |
47 | to be changed to long and also the stdlib/tst-setcontext.c file needs | |
48 | to be changed to pass long arguments to makecontext. */ | |
49 | ||
50 | ||
51 | void | |
52 | __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...) | |
53 | { | |
54 | extern void __start_context (void); | |
6b6cd74b L |
55 | greg_t *sp; |
56 | unsigned int idx_uc_link; | |
ba1e2fe6 AJ |
57 | va_list ap; |
58 | int i; | |
59 | ||
60 | /* Generate room on stack for parameter if needed and uc_link. */ | |
6b6cd74b L |
61 | sp = (greg_t *) ((uintptr_t) ucp->uc_stack.ss_sp |
62 | + ucp->uc_stack.ss_size); | |
c2df18a9 | 63 | sp -= (argc > 6 ? argc - 6 : 0) + 1; |
9f19140a | 64 | /* Align stack and make space for trampoline address. */ |
6b6cd74b | 65 | sp = (greg_t *) ((((uintptr_t) sp) & -16L) - 8); |
ba1e2fe6 | 66 | |
c2df18a9 | 67 | idx_uc_link = (argc > 6 ? argc - 6 : 0) + 1; |
ba1e2fe6 AJ |
68 | |
69 | /* Setup context ucp. */ | |
70 | /* Address to jump to. */ | |
6b6cd74b | 71 | ucp->uc_mcontext.gregs[REG_RIP] = (uintptr_t) func; |
ba1e2fe6 | 72 | /* Setup rbx.*/ |
6b6cd74b L |
73 | ucp->uc_mcontext.gregs[REG_RBX] = (uintptr_t) &sp[idx_uc_link]; |
74 | ucp->uc_mcontext.gregs[REG_RSP] = (uintptr_t) sp; | |
ba1e2fe6 AJ |
75 | |
76 | /* Setup stack. */ | |
6b6cd74b L |
77 | sp[0] = (uintptr_t) &__start_context; |
78 | sp[idx_uc_link] = (uintptr_t) ucp->uc_link; | |
ba1e2fe6 AJ |
79 | |
80 | va_start (ap, argc); | |
6c30d38f UD |
81 | /* Handle arguments. |
82 | ||
83 | The standard says the parameters must all be int values. This is | |
84 | an historic accident and would be done differently today. For | |
85 | x86-64 all integer values are passed as 64-bit values and | |
86 | therefore extending the API to copy 64-bit values instead of | |
87 | 32-bit ints makes sense. It does not break existing | |
88 | functionality and it does not violate the standard which says | |
89 | that passing non-int values means undefined behavior. */ | |
ba1e2fe6 AJ |
90 | for (i = 0; i < argc; ++i) |
91 | switch (i) | |
92 | { | |
93 | case 0: | |
6b6cd74b | 94 | ucp->uc_mcontext.gregs[REG_RDI] = va_arg (ap, greg_t); |
ba1e2fe6 AJ |
95 | break; |
96 | case 1: | |
6b6cd74b | 97 | ucp->uc_mcontext.gregs[REG_RSI] = va_arg (ap, greg_t); |
ba1e2fe6 AJ |
98 | break; |
99 | case 2: | |
6b6cd74b | 100 | ucp->uc_mcontext.gregs[REG_RDX] = va_arg (ap, greg_t); |
ba1e2fe6 AJ |
101 | break; |
102 | case 3: | |
6b6cd74b | 103 | ucp->uc_mcontext.gregs[REG_RCX] = va_arg (ap, greg_t); |
ba1e2fe6 AJ |
104 | break; |
105 | case 4: | |
6b6cd74b | 106 | ucp->uc_mcontext.gregs[REG_R8] = va_arg (ap, greg_t); |
ba1e2fe6 AJ |
107 | break; |
108 | case 5: | |
6b6cd74b | 109 | ucp->uc_mcontext.gregs[REG_R9] = va_arg (ap, greg_t); |
ba1e2fe6 AJ |
110 | break; |
111 | default: | |
112 | /* Put value on stack. */ | |
6b6cd74b | 113 | sp[i - 5] = va_arg (ap, greg_t); |
ba1e2fe6 AJ |
114 | break; |
115 | } | |
116 | va_end (ap); | |
117 | ||
118 | } | |
119 | ||
120 | ||
1ab18a5b | 121 | weak_alias (__makecontext, makecontext) |