29e5647d6963c5ed412772ed729b834f7a56a295
[lttng-ust.git] / libringbuffer / rseq-x86.h
1 /*
2 * rseq-x86.h
3 *
4 * (C) Copyright 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #ifdef __x86_64__
26
27 #define has_fast_acquire_release() 1
28 #define has_single_copy_load_64() 1
29
30 /*
31 * The __rseq_table section can be used by debuggers to better handle
32 * single-stepping through the restartable critical sections.
33 */
34 #define RSEQ_FINISH_ASM(_target_final, _to_write_final, _start_value, \
35 _failure, _spec_store, _spec_input, \
36 _final_store, _final_input, _extra_clobber, \
37 _setup, _teardown, _scratch) \
38 do { \
39 _scratch \
40 __asm__ __volatile__ goto ( \
41 ".pushsection __rseq_table, \"aw\"\n\t" \
42 ".balign 32\n\t" \
43 "3:\n\t" \
44 ".quad 1f, 2f, 4f, 0x0\n\t" \
45 ".popsection\n\t" \
46 "1:\n\t" \
47 _setup \
48 RSEQ_INJECT_ASM(1) \
49 "movq $3b, %[rseq_cs]\n\t" \
50 RSEQ_INJECT_ASM(2) \
51 "cmpl %[start_event_counter], %[current_event_counter]\n\t" \
52 "jnz 4f\n\t" \
53 RSEQ_INJECT_ASM(3) \
54 _spec_store \
55 _final_store \
56 "2:\n\t" \
57 RSEQ_INJECT_ASM(5) \
58 "movq $0, %[rseq_cs]\n\t" \
59 _teardown \
60 ".pushsection __rseq_failure, \"a\"\n\t" \
61 "4:\n\t" \
62 "movq $0, %[rseq_cs]\n\t" \
63 _teardown \
64 "jmp %l[failure]\n\t" \
65 ".popsection\n\t" \
66 : /* no outputs */ \
67 : [start_event_counter]"r"((_start_value).event_counter), \
68 [current_event_counter]"m"((_start_value).rseqp->u.e.event_counter), \
69 [rseq_cs]"m"((_start_value).rseqp->rseq_cs) \
70 _spec_input \
71 _final_input \
72 RSEQ_INJECT_INPUT \
73 : "memory", "cc" \
74 _extra_clobber \
75 RSEQ_INJECT_CLOBBER \
76 : _failure \
77 ); \
78 } while (0)
79
80 #define RSEQ_FINISH_FINAL_STORE_ASM() \
81 "movq %[to_write_final], %[target_final]\n\t"
82
83 /* x86-64 is TSO */
84 #define RSEQ_FINISH_FINAL_STORE_RELEASE_ASM() \
85 RSEQ_FINISH_FINAL_STORE_ASM()
86
87 #define RSEQ_FINISH_FINAL_STORE_INPUT(_target_final, _to_write_final) \
88 , [to_write_final]"r"(_to_write_final), \
89 [target_final]"m"(*(_target_final))
90
91 #define RSEQ_FINISH_SPECULATIVE_STORE_ASM() \
92 "movq %[to_write_spec], %[target_spec]\n\t" \
93 RSEQ_INJECT_ASM(4)
94
95 #define RSEQ_FINISH_SPECULATIVE_STORE_INPUT(_target_spec, _to_write_spec) \
96 , [to_write_spec]"r"(_to_write_spec), \
97 [target_spec]"m"(*(_target_spec))
98
99 /* TODO: implement a faster memcpy. */
100 #define RSEQ_FINISH_MEMCPY_STORE_ASM() \
101 "test %[len_memcpy], %[len_memcpy]\n\t" \
102 "jz 333f\n\t" \
103 "222:\n\t" \
104 "movb (%[to_write_memcpy]), %%al\n\t" \
105 "movb %%al, (%[target_memcpy])\n\t" \
106 "inc %[to_write_memcpy]\n\t" \
107 "inc %[target_memcpy]\n\t" \
108 "dec %[len_memcpy]\n\t" \
109 "jnz 222b\n\t" \
110 "333:\n\t" \
111 RSEQ_INJECT_ASM(4)
112
113 #define RSEQ_FINISH_MEMCPY_STORE_INPUT(_target_memcpy, _to_write_memcpy, _len_memcpy) \
114 , [to_write_memcpy]"r"(_to_write_memcpy), \
115 [target_memcpy]"r"(_target_memcpy), \
116 [len_memcpy]"r"(_len_memcpy), \
117 [rseq_scratch0]"m"(rseq_scratch[0]), \
118 [rseq_scratch1]"m"(rseq_scratch[1]), \
119 [rseq_scratch2]"m"(rseq_scratch[2])
120
121 #define RSEQ_FINISH_MEMCPY_CLOBBER() \
122 , "rax"
123
124 #define RSEQ_FINISH_MEMCPY_SCRATCH() \
125 uint64_t rseq_scratch[3];
126
127 /*
128 * We need to save and restore those input registers so they can be
129 * modified within the assembly.
130 */
131 #define RSEQ_FINISH_MEMCPY_SETUP() \
132 "movq %[to_write_memcpy], %[rseq_scratch0]\n\t" \
133 "movq %[target_memcpy], %[rseq_scratch1]\n\t" \
134 "movq %[len_memcpy], %[rseq_scratch2]\n\t"
135
136 #define RSEQ_FINISH_MEMCPY_TEARDOWN() \
137 "movq %[rseq_scratch2], %[len_memcpy]\n\t" \
138 "movq %[rseq_scratch1], %[target_memcpy]\n\t" \
139 "movq %[rseq_scratch0], %[to_write_memcpy]\n\t"
140
141 #elif __i386__
142
143 #define has_fast_acquire_release() 0
144 #define has_single_copy_load_64() 0
145
146 /*
147 * Use eax as scratch register and take memory operands as input to
148 * lessen register pressure. Especially needed when compiling
149 * do_rseq_memcpy() in O0.
150 */
151 #define RSEQ_FINISH_ASM(_target_final, _to_write_final, _start_value, \
152 _failure, _spec_store, _spec_input, \
153 _final_store, _final_input, _extra_clobber, \
154 _setup, _teardown, _scratch) \
155 do { \
156 _scratch \
157 __asm__ __volatile__ goto ( \
158 ".pushsection __rseq_table, \"aw\"\n\t" \
159 ".balign 32\n\t" \
160 "3:\n\t" \
161 ".long 1f, 0x0, 2f, 0x0, 4f, 0x0, 0x0, 0x0\n\t" \
162 ".popsection\n\t" \
163 "1:\n\t" \
164 _setup \
165 RSEQ_INJECT_ASM(1) \
166 "movl $3b, %[rseq_cs]\n\t" \
167 RSEQ_INJECT_ASM(2) \
168 "movl %[start_event_counter], %%eax\n\t" \
169 "cmpl %%eax, %[current_event_counter]\n\t" \
170 "jnz 4f\n\t" \
171 RSEQ_INJECT_ASM(3) \
172 _spec_store \
173 _final_store \
174 "2:\n\t" \
175 RSEQ_INJECT_ASM(5) \
176 "movl $0, %[rseq_cs]\n\t" \
177 _teardown \
178 ".pushsection __rseq_failure, \"a\"\n\t" \
179 "4:\n\t" \
180 "movl $0, %[rseq_cs]\n\t" \
181 _teardown \
182 "jmp %l[failure]\n\t" \
183 ".popsection\n\t" \
184 : /* no outputs */ \
185 : [start_event_counter]"m"((_start_value).event_counter), \
186 [current_event_counter]"m"((_start_value).rseqp->u.e.event_counter), \
187 [rseq_cs]"m"((_start_value).rseqp->rseq_cs) \
188 _spec_input \
189 _final_input \
190 RSEQ_INJECT_INPUT \
191 : "memory", "cc", "eax" \
192 _extra_clobber \
193 RSEQ_INJECT_CLOBBER \
194 : _failure \
195 ); \
196 } while (0)
197
198 #define RSEQ_FINISH_FINAL_STORE_ASM() \
199 "movl %[to_write_final], %%eax\n\t" \
200 "movl %%eax, %[target_final]\n\t"
201
202 #define RSEQ_FINISH_FINAL_STORE_RELEASE_ASM() \
203 "lock; addl $0,0(%%esp)\n\t" \
204 RSEQ_FINISH_FINAL_STORE_ASM()
205
206 #define RSEQ_FINISH_FINAL_STORE_INPUT(_target_final, _to_write_final) \
207 , [to_write_final]"m"(_to_write_final), \
208 [target_final]"m"(*(_target_final))
209
210 #define RSEQ_FINISH_SPECULATIVE_STORE_ASM() \
211 "movl %[to_write_spec], %%eax\n\t" \
212 "movl %%eax, %[target_spec]\n\t" \
213 RSEQ_INJECT_ASM(4)
214
215 #define RSEQ_FINISH_SPECULATIVE_STORE_INPUT(_target_spec, _to_write_spec) \
216 , [to_write_spec]"m"(_to_write_spec), \
217 [target_spec]"m"(*(_target_spec))
218
219 /* TODO: implement a faster memcpy. */
220 #define RSEQ_FINISH_MEMCPY_STORE_ASM() \
221 "movl %[len_memcpy], %%eax\n\t" \
222 "test %%eax, %%eax\n\t" \
223 "jz 333f\n\t" \
224 "222:\n\t" \
225 "movb (%[to_write_memcpy]), %%al\n\t" \
226 "movb %%al, (%[target_memcpy])\n\t" \
227 "inc %[to_write_memcpy]\n\t" \
228 "inc %[target_memcpy]\n\t" \
229 "decl %[rseq_scratch2]\n\t" \
230 "jnz 222b\n\t" \
231 "333:\n\t" \
232 RSEQ_INJECT_ASM(4)
233
234 #define RSEQ_FINISH_MEMCPY_STORE_INPUT(_target_memcpy, _to_write_memcpy, _len_memcpy) \
235 , [to_write_memcpy]"r"(_to_write_memcpy), \
236 [target_memcpy]"r"(_target_memcpy), \
237 [len_memcpy]"m"(_len_memcpy), \
238 [rseq_scratch0]"m"(rseq_scratch[0]), \
239 [rseq_scratch1]"m"(rseq_scratch[1]), \
240 [rseq_scratch2]"m"(rseq_scratch[2])
241
242 #define RSEQ_FINISH_MEMCPY_CLOBBER()
243
244 #define RSEQ_FINISH_MEMCPY_SCRATCH() \
245 uint32_t rseq_scratch[3];
246
247 /*
248 * We need to save and restore those input registers so they can be
249 * modified within the assembly.
250 */
251 #define RSEQ_FINISH_MEMCPY_SETUP() \
252 "movl %[to_write_memcpy], %[rseq_scratch0]\n\t" \
253 "movl %[target_memcpy], %[rseq_scratch1]\n\t" \
254 "movl %[len_memcpy], %%eax\n\t" \
255 "movl %%eax, %[rseq_scratch2]\n\t"
256
257 #define RSEQ_FINISH_MEMCPY_TEARDOWN() \
258 "movl %[rseq_scratch1], %[target_memcpy]\n\t" \
259 "movl %[rseq_scratch0], %[to_write_memcpy]\n\t"
260
261 #endif
This page took 0.034345 seconds and 4 git commands to generate.