2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / x86_64 / makecontext.c
blob615dede522dbed4d7c097a7403c9bf7a4f11c8b2
1 /* Create new context.
2 Copyright (C) 2002, 2004, 2005, 2008 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Andreas Jaeger <aj@suse.de>, 2002.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <sysdep.h>
22 #include <stdarg.h>
23 #include <stdint.h>
24 #include <ucontext.h>
26 #include "ucontext_i.h"
28 /* This implementation can handle any ARGC value but only
29 normal integer parameters.
30 makecontext sets up a stack and the registers for the
31 user context. The stack looks like this:
32 +-----------------------+
33 | next context |
34 +-----------------------+
35 | parameter 7-n |
36 +-----------------------+
37 | trampoline address |
38 %rsp -> +-----------------------+
40 The registers are set up like this:
41 %rdi,%rsi,%rdx,%rcx,%r8,%r9: parameter 1 to 6
42 %rbx : address of next context
43 %rsp : stack pointer.
46 /* XXX: This implementation currently only handles integer arguments.
47 To handle long int and pointer arguments the va_arg arguments needs
48 to be changed to long and also the stdlib/tst-setcontext.c file needs
49 to be changed to pass long arguments to makecontext. */
52 void
53 __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
55 extern void __start_context (void);
56 unsigned long int *sp, idx_uc_link;
57 va_list ap;
58 int i;
60 /* Generate room on stack for parameter if needed and uc_link. */
61 sp = (unsigned long int *) ((uintptr_t) ucp->uc_stack.ss_sp
62 + ucp->uc_stack.ss_size);
63 sp -= (argc > 6 ? argc - 6 : 0) + 1;
64 /* Align stack and make space for trampoline address. */
65 sp = (unsigned long int *) ((((uintptr_t) sp) & -16L) - 8);
67 idx_uc_link = (argc > 6 ? argc - 6 : 0) + 1;
69 /* Setup context ucp. */
70 /* Address to jump to. */
71 ucp->uc_mcontext.gregs[REG_RIP] = (long int) func;
72 /* Setup rbx.*/
73 ucp->uc_mcontext.gregs[REG_RBX] = (long int) &sp[idx_uc_link];
74 ucp->uc_mcontext.gregs[REG_RSP] = (long int) sp;
76 /* Setup stack. */
77 sp[0] = (unsigned long int) &__start_context;
78 sp[idx_uc_link] = (unsigned long int) ucp->uc_link;
80 va_start (ap, argc);
81 /* Handle arguments.
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. */
90 for (i = 0; i < argc; ++i)
91 switch (i)
93 case 0:
94 ucp->uc_mcontext.gregs[REG_RDI] = va_arg (ap, long int);
95 break;
96 case 1:
97 ucp->uc_mcontext.gregs[REG_RSI] = va_arg (ap, long int);
98 break;
99 case 2:
100 ucp->uc_mcontext.gregs[REG_RDX] = va_arg (ap, long int);
101 break;
102 case 3:
103 ucp->uc_mcontext.gregs[REG_RCX] = va_arg (ap, long int);
104 break;
105 case 4:
106 ucp->uc_mcontext.gregs[REG_R8] = va_arg (ap, long int);
107 break;
108 case 5:
109 ucp->uc_mcontext.gregs[REG_R9] = va_arg (ap, long int);
110 break;
111 default:
112 /* Put value on stack. */
113 sp[i - 5] = va_arg (ap, unsigned long int);
114 break;
116 va_end (ap);
121 weak_alias (__makecontext, makecontext)