[NTOS]: Implement MmDeleteTeb, VADs are now deleted/freed on thread exit as well...
[reactos.git] / reactos / lib / sdk / crt / setjmp / amd64 / setjmp.s
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * PURPOSE: Implementation of _setjmp/longjmp
5 * FILE: lib/sdk/crt/setjmp/amd64/setjmp.s
6 * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include <reactos/asm.h>
12 #include <ndk/amd64/asm.h>
13
14 #define JUMP_BUFFER_Frame 0x00
15 #define JUMP_BUFFER_Rbx 0x08
16 #define JUMP_BUFFER_Rsp 0x10
17 #define JUMP_BUFFER_Rbp 0x18
18 #define JUMP_BUFFER_Rsi 0x20
19 #define JUMP_BUFFER_Rdi 0x28
20 #define JUMP_BUFFER_R12 0x30
21 #define JUMP_BUFFER_R13 0x38
22 #define JUMP_BUFFER_R14 0x40
23 #define JUMP_BUFFER_R15 0x48
24 #define JUMP_BUFFER_Rip 0x50
25 #define JUMP_BUFFER_Spare 0x58
26 #define JUMP_BUFFER_Xmm6 0x60
27 #define JUMP_BUFFER_Xmm7 0x70
28 #define JUMP_BUFFER_Xmm8 0x80
29 #define JUMP_BUFFER_Xmm9 0x90
30 #define JUMP_BUFFER_Xmm10 0xa0
31 #define JUMP_BUFFER_Xmm11 0xb0
32 #define JUMP_BUFFER_Xmm12 0xc0
33 #define JUMP_BUFFER_Xmm13 0xd0
34 #define JUMP_BUFFER_Xmm14 0xe0
35 #define JUMP_BUFFER_Xmm15 0xf0
36
37
38 /* FUNCTIONS ******************************************************************/
39
40 /*
41 * int _setjmp(jmp_buf env);
42 *
43 * Parameters: <rcx> - jmp_buf env
44 * Returns: 0
45 * Notes: Sets up the jmp_buf
46 */
47 PUBLIC _setjmp
48 .proc _setjmp
49 /* Load rsp as it was before the call into rax */
50 lea rax, [rsp + 8]
51 /* Load return address into r8 */
52 mov r8, [rsp]
53 mov qword ptr [rcx + JUMP_BUFFER_Frame], 0
54 mov [rcx + JUMP_BUFFER_Rbx], rbx
55 mov [rcx + JUMP_BUFFER_Rbp], rbp
56 mov [rcx + JUMP_BUFFER_Rsi], rsi
57 mov [rcx + JUMP_BUFFER_Rdi], rdi
58 mov [rcx + JUMP_BUFFER_R12], r12
59 mov [rcx + JUMP_BUFFER_R13], r13
60 mov [rcx + JUMP_BUFFER_R14], r14
61 mov [rcx + JUMP_BUFFER_R15], r15
62 mov [rcx + JUMP_BUFFER_Rsp], rax
63 mov [rcx + JUMP_BUFFER_Rip], r8
64 movdqa [rcx + JUMP_BUFFER_Xmm6], xmm6
65 movdqa [rcx + JUMP_BUFFER_Xmm7], xmm7
66 movdqa [rcx + JUMP_BUFFER_Xmm8], xmm8
67 movdqa [rcx + JUMP_BUFFER_Xmm9], xmm9
68 movdqa [rcx + JUMP_BUFFER_Xmm10], xmm10
69 movdqa [rcx + JUMP_BUFFER_Xmm11], xmm11
70 movdqa [rcx + JUMP_BUFFER_Xmm12], xmm12
71 movdqa [rcx + JUMP_BUFFER_Xmm13], xmm13
72 movdqa [rcx + JUMP_BUFFER_Xmm14], xmm14
73 movdqa [rcx + JUMP_BUFFER_Xmm15], xmm15
74 xor rax, rax
75 ret
76 .endp setjmp
77
78 /*
79 * int _setjmpex(jmp_buf _Buf,void *_Ctx);
80 *
81 * Parameters: <rcx> - jmp_buf env
82 * <rdx> - frame
83 * Returns: 0
84 * Notes: Sets up the jmp_buf
85 */
86 PUBLIC _setjmpex
87 .proc _setjmpex
88 /* Load rsp as it was before the call into rax */
89 lea rax, [rsp + 8]
90 /* Load return address into r8 */
91 mov r8, [rsp]
92 mov [rcx + JUMP_BUFFER_Frame], rdx
93 mov [rcx + JUMP_BUFFER_Rbx], rbx
94 mov [rcx + JUMP_BUFFER_Rbp], rbp
95 mov [rcx + JUMP_BUFFER_Rsi], rsi
96 mov [rcx + JUMP_BUFFER_Rdi], rdi
97 mov [rcx + JUMP_BUFFER_R12], r12
98 mov [rcx + JUMP_BUFFER_R13], r13
99 mov [rcx + JUMP_BUFFER_R14], r14
100 mov [rcx + JUMP_BUFFER_R15], r15
101 mov [rcx + JUMP_BUFFER_Rsp], rax
102 mov [rcx + JUMP_BUFFER_Rip], r8
103 movdqa [rcx + JUMP_BUFFER_Xmm6], xmm6
104 movdqa [rcx + JUMP_BUFFER_Xmm7], xmm7
105 movdqa [rcx + JUMP_BUFFER_Xmm8], xmm8
106 movdqa [rcx + JUMP_BUFFER_Xmm9], xmm9
107 movdqa [rcx + JUMP_BUFFER_Xmm10], xmm10
108 movdqa [rcx + JUMP_BUFFER_Xmm11], xmm11
109 movdqa [rcx + JUMP_BUFFER_Xmm12], xmm12
110 movdqa [rcx + JUMP_BUFFER_Xmm13], xmm13
111 movdqa [rcx + JUMP_BUFFER_Xmm14], xmm14
112 movdqa [rcx + JUMP_BUFFER_Xmm15], xmm15
113 xor rax, rax
114 ret
115 .endp setjmpex
116
117
118 /*
119 * void longjmp(jmp_buf env, int value);
120 *
121 * Parameters: <rcx> - jmp_buf setup by _setjmp
122 * <rdx> - int value to return
123 * Returns: Doesn't return
124 * Notes: Non-local goto
125 */
126 PUBLIC longjmp
127 .proc longjmp
128
129 // FIXME: handle frame
130
131 mov rbx, [rcx + JUMP_BUFFER_Rbx]
132 mov rbp, [rcx + JUMP_BUFFER_Rbp]
133 mov rsi, [rcx + JUMP_BUFFER_Rsi]
134 mov rdi, [rcx + JUMP_BUFFER_Rdi]
135 mov r12, [rcx + JUMP_BUFFER_R12]
136 mov r13, [rcx + JUMP_BUFFER_R13]
137 mov r14, [rcx + JUMP_BUFFER_R14]
138 mov r15, [rcx + JUMP_BUFFER_R15]
139 mov rsp, [rcx + JUMP_BUFFER_Rsp]
140 mov r8, [rcx + JUMP_BUFFER_Rip]
141 movdqa xmm6, [rcx + JUMP_BUFFER_Xmm6]
142 movdqa xmm7, [rcx + JUMP_BUFFER_Xmm7]
143 movdqa xmm8, [rcx + JUMP_BUFFER_Xmm8]
144 movdqa xmm9, [rcx + JUMP_BUFFER_Xmm9]
145 movdqa xmm10, [rcx + JUMP_BUFFER_Xmm10]
146 movdqa xmm11, [rcx + JUMP_BUFFER_Xmm11]
147 movdqa xmm12, [rcx + JUMP_BUFFER_Xmm12]
148 movdqa xmm13, [rcx + JUMP_BUFFER_Xmm13]
149 movdqa xmm14, [rcx + JUMP_BUFFER_Xmm14]
150 movdqa xmm15, [rcx + JUMP_BUFFER_Xmm15]
151
152 /* return param2 or 1 if it was 0 */
153 mov rax, rdx
154 test rax, rax
155 jnz 2f
156 inc rax
157 2: jmp r8
158 .endp longjmp