1 | /* Create new context. |
2 | Copyright (C) 2002-2024 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <sysdep.h> |
20 | #include <stdarg.h> |
21 | #include <stdint.h> |
22 | #include <ucontext.h> |
23 | #if SHSTK_ENABLED |
24 | # include <pthread.h> |
25 | # include <libc-pointer-arith.h> |
26 | # include <sys/prctl.h> |
27 | # include <allocate-shadow-stack.h> |
28 | #endif |
29 | |
30 | #include "ucontext_i.h" |
31 | |
32 | /* This implementation can handle any ARGC value but only |
33 | normal integer parameters. |
34 | makecontext sets up a stack and the registers for the |
35 | user context. The stack looks like this: |
36 | +-----------------------+ |
37 | | next context | |
38 | +-----------------------+ |
39 | | parameter 7-n | |
40 | +-----------------------+ |
41 | | trampoline address | |
42 | %rsp -> +-----------------------+ |
43 | |
44 | The registers are set up like this: |
45 | %rdi,%rsi,%rdx,%rcx,%r8,%r9: parameter 1 to 6 |
46 | %rbx : address of next context |
47 | %rsp : stack pointer. |
48 | */ |
49 | |
50 | /* XXX: This implementation currently only handles integer arguments. |
51 | To handle long int and pointer arguments the va_arg arguments needs |
52 | to be changed to long and also the stdlib/tst-setcontext.c file needs |
53 | to be changed to pass long arguments to makecontext. */ |
54 | |
55 | |
56 | void |
57 | __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...) |
58 | { |
59 | extern void __start_context (void) attribute_hidden; |
60 | extern void __push___start_context (ucontext_t *) |
61 | attribute_hidden; |
62 | greg_t *sp; |
63 | unsigned int idx_uc_link; |
64 | va_list ap; |
65 | int i; |
66 | |
67 | /* Generate room on stack for parameter if needed and uc_link. */ |
68 | sp = (greg_t *) ((uintptr_t) ucp->uc_stack.ss_sp |
69 | + ucp->uc_stack.ss_size); |
70 | sp -= (argc > 6 ? argc - 6 : 0) + 1; |
71 | /* Align stack and make space for trampoline address. */ |
72 | sp = (greg_t *) ((((uintptr_t) sp) & -16L) - 8); |
73 | |
74 | idx_uc_link = (argc > 6 ? argc - 6 : 0) + 1; |
75 | |
76 | /* Setup context ucp. */ |
77 | /* Address to jump to. */ |
78 | ucp->uc_mcontext.gregs[REG_RIP] = (uintptr_t) func; |
79 | /* Setup rbx.*/ |
80 | ucp->uc_mcontext.gregs[REG_RBX] = (uintptr_t) &sp[idx_uc_link]; |
81 | ucp->uc_mcontext.gregs[REG_RSP] = (uintptr_t) sp; |
82 | |
83 | /* Setup stack. */ |
84 | #if SHSTK_ENABLED |
85 | struct pthread *self = THREAD_SELF; |
86 | unsigned int feature_1 = THREAD_GETMEM (self, header.feature_1); |
87 | /* NB: We must check feature_1 before accessing __ssp since caller |
88 | may be compiled against ucontext_t without __ssp. */ |
89 | if ((feature_1 & X86_FEATURE_1_SHSTK) != 0) |
90 | { |
91 | /* Shadow stack is enabled. We need to allocate a new shadow |
92 | stack. NB: |
93 | ucp->__ssp[0]: The new shadow stack pointer. |
94 | ucp->__ssp[1]: The base address of the new shadow stack. |
95 | ucp->__ssp[2]: The size of the new shadow stack. |
96 | */ |
97 | long int ret |
98 | = __allocate_shadow_stack (((uintptr_t) sp |
99 | - (uintptr_t) ucp->uc_stack.ss_sp), |
100 | &ucp->__ssp[1]); |
101 | if (ret != 0) |
102 | { |
103 | /* FIXME: What should we do? */ |
104 | abort (); |
105 | } |
106 | |
107 | ucp->__ssp[0] = ucp->__ssp[1] + ucp->__ssp[2] - 8; |
108 | /* Call __push___start_context to push __start_context onto the new |
109 | stack as well as the new shadow stack. */ |
110 | __push___start_context (ucp); |
111 | } |
112 | else |
113 | #endif |
114 | sp[0] = (uintptr_t) &__start_context; |
115 | sp[idx_uc_link] = (uintptr_t) ucp->uc_link; |
116 | |
117 | va_start (ap, argc); |
118 | /* Handle arguments. |
119 | |
120 | The standard says the parameters must all be int values. This is |
121 | an historic accident and would be done differently today. For |
122 | x86-64 all integer values are passed as 64-bit values and |
123 | therefore extending the API to copy 64-bit values instead of |
124 | 32-bit ints makes sense. It does not break existing |
125 | functionality and it does not violate the standard which says |
126 | that passing non-int values means undefined behavior. */ |
127 | for (i = 0; i < argc; ++i) |
128 | switch (i) |
129 | { |
130 | case 0: |
131 | ucp->uc_mcontext.gregs[REG_RDI] = va_arg (ap, greg_t); |
132 | break; |
133 | case 1: |
134 | ucp->uc_mcontext.gregs[REG_RSI] = va_arg (ap, greg_t); |
135 | break; |
136 | case 2: |
137 | ucp->uc_mcontext.gregs[REG_RDX] = va_arg (ap, greg_t); |
138 | break; |
139 | case 3: |
140 | ucp->uc_mcontext.gregs[REG_RCX] = va_arg (ap, greg_t); |
141 | break; |
142 | case 4: |
143 | ucp->uc_mcontext.gregs[REG_R8] = va_arg (ap, greg_t); |
144 | break; |
145 | case 5: |
146 | ucp->uc_mcontext.gregs[REG_R9] = va_arg (ap, greg_t); |
147 | break; |
148 | default: |
149 | /* Put value on stack. */ |
150 | sp[i - 5] = va_arg (ap, greg_t); |
151 | break; |
152 | } |
153 | va_end (ap); |
154 | |
155 | } |
156 | |
157 | |
158 | weak_alias (__makecontext, makecontext) |
159 | |