-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathswitch_x86_64_gcc.h
More file actions
114 lines (94 loc) · 3.47 KB
/
Copy pathswitch_x86_64_gcc.h
File metadata and controls
114 lines (94 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* This is the System V ABI for x86-64.
* It is used on linux and other similar systems (other than
* windows).
* https://wiki.osdev.org/System_V_ABI and
* https://www.uclibc.org/docs/psABI-x86_64.pdf
*/
/* clang cannot perform inline assembly using specific __attr__
* instructions, and so it may use a base pointer and other
* things. We must force it to use the pre-build assembler
*/
#if !defined(STACKMAN_ASSEMBLY_SRC)
#if defined (__clang__) || ! STACKMAN_INLINE_ASM
#define STACKMAN_ASSEMBLY_SRC "platforms/switch_x86_64_gcc.S"
#endif
#endif
#ifndef STACKMAN_HAVE_CALL
#define STACKMAN_HAVE_CALL 1
#define STACKMAN_STACK_ALIGN 16
#endif
#if defined(STACKMAN_SWITCH_IMPL)
#if !__ASSEMBLER__ && !defined(STACKMAN_ASSEMBLY_SRC)
/* inline assembly */
#include <stddef.h>
#include "../stackman_switch.h"
/*
* define registers to save.
* x87 control word is callee saved and certain bits of the MXCSR
* too, so we save both manually.
*/
#define PRESERVE "rbx", "r12", "r13", "r14", "r15", "rbp"
/* Need the optimization level to avoid storing 'stack_pointer'
* and other locals
* on the stack which would cause the wrong value to be sent to
* the second callback call (it would be read from stack).
* We also enforce no frame pointer, so that locals are not
* accessessed using rbp (base pointer), and push rbp ourselves.
* stack protection code may be generated. This is normally safe,
* but can be forcefully disabled using "no-stack-protector" option.
*/
#define OPTIMIZE "O", "omit-frame-pointer", "no-stack-protector"
__attribute__((optimize(OPTIMIZE)))
STACKMAN_LINKAGE_SWITCH_INASM
void *STACKMAN_SWITCH_INASM_NAME(stackman_cb_t callback, void *context)
{
void *stack_pointer, *stack_pointer2;
ptrdiff_t diff;
/* assembly to save non-volatile registers, including x87 and mmx */
int mxcsr; short x87cw;
__asm__ volatile (
"fstcw %[cw]\n\t"
"stmxcsr %[sr]\n\t"
: [cw] "=m" (x87cw), [sr] "=m" (mxcsr) : : PRESERVE);
/* sp = get stack pointer from assembly code */
__asm__ ("movq %%rsp, %[result]" : [result] "=r" (stack_pointer));
stack_pointer2 = callback(context, STACKMAN_OP_SAVE, stack_pointer);
diff = stack_pointer2 - stack_pointer;
/* set stack pointer from sp using assembly */
__asm__ ("movq %[result], %%rsp" :: [result] "r" (stack_pointer2));
__asm__ ("addq %[arg], %%rbp" :: [arg] "r" (diff));
stack_pointer = callback(context, STACKMAN_OP_RESTORE, stack_pointer2);
/* restore non-volatile registers from stack */
__asm__ volatile (
"ldmxcsr %[sr]\n\t"
"fldcw %[cw]\n\t"
: : [cw] "m" (x87cw), [sr] "m" (mxcsr));
return stack_pointer;
}
/*
* Similar, but we want the frame pointer so that a debugger
* can follow the stack
*/
#define OPTIMIZE_CALL "O", "no-omit-frame-pointer"
__attribute__((optimize(OPTIMIZE_CALL)))
STACKMAN_LINKAGE_SWITCH
void *stackman_call(stackman_cb_t callback, void *context, void *stack_pointer)
{
void *old_sp, *result;
/* sp = store stack pointer in rbx */
__asm__ ("movq %%rsp, %%rbx" : : : "rbx");
__asm__ ("movq %%rsp, %[sp]" : [sp] "=r" (old_sp));
/* set stack pointer from provided using assembly */
if (stack_pointer != 0)
__asm__ ("movq %[sp], %%rsp" :: [sp] "r" (stack_pointer));
result = callback(context, STACKMAN_OP_CALL, old_sp);
/* restore stack pointer */
__asm__ ("movq %%rbx, %%rsp" :::);
return result;
}
#endif
#if __ASSEMBLER__ && defined(STACKMAN_ASSEMBLY_SRC)
/* pre-generated assembly code */
#include STACKMAN_ASSEMBLY_SRC
#endif
#endif /* STACKMAN_SWITCH_IMPL */