2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
25 # include <libc-pointer-arith.h>
26 # include <sys/prctl.h>
27 # include <allocate-shadow-stack.h>
30 #include "ucontext_i.h"
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 +-----------------------+
38 +-----------------------+
40 +-----------------------+
41 | trampoline address |
42 %rsp -> +-----------------------+
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
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. */
57 __makecontext (ucontext_t
*ucp
, void (*func
) (void), int argc
, ...)
59 extern void __start_context (void) attribute_hidden
;
60 extern void __push___start_context (ucontext_t
*)
63 unsigned int idx_uc_link
;
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);
74 idx_uc_link
= (argc
> 6 ? argc
- 6 : 0) + 1;
76 /* Setup context ucp. */
77 /* Address to jump to. */
78 ucp
->uc_mcontext
.gregs
[REG_RIP
] = (uintptr_t) func
;
80 ucp
->uc_mcontext
.gregs
[REG_RBX
] = (uintptr_t) &sp
[idx_uc_link
];
81 ucp
->uc_mcontext
.gregs
[REG_RSP
] = (uintptr_t) sp
;
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)
91 /* Shadow stack is enabled. We need to allocate a new shadow
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.
98 = __allocate_shadow_stack (((uintptr_t) sp
99 - (uintptr_t) ucp
->uc_stack
.ss_sp
),
103 /* FIXME: What should we do? */
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
);
114 sp
[0] = (uintptr_t) &__start_context
;
115 sp
[idx_uc_link
] = (uintptr_t) ucp
->uc_link
;
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
)
131 ucp
->uc_mcontext
.gregs
[REG_RDI
] = va_arg (ap
, greg_t
);
134 ucp
->uc_mcontext
.gregs
[REG_RSI
] = va_arg (ap
, greg_t
);
137 ucp
->uc_mcontext
.gregs
[REG_RDX
] = va_arg (ap
, greg_t
);
140 ucp
->uc_mcontext
.gregs
[REG_RCX
] = va_arg (ap
, greg_t
);
143 ucp
->uc_mcontext
.gregs
[REG_R8
] = va_arg (ap
, greg_t
);
146 ucp
->uc_mcontext
.gregs
[REG_R9
] = va_arg (ap
, greg_t
);
149 /* Put value on stack. */
150 sp
[i
- 5] = va_arg (ap
, greg_t
);
158 weak_alias (__makecontext
, makecontext
)