Debian Codegraph @

rpython/jit/backend/riscv/callbuilder.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 from rpython.jit.backend.llsupport import llerrno
4 from rpython.jit.backend.llsupport.callbuilder import AbstractCallBuilder
5 from rpython.jit.backend.llsupport.jump import remap_frame_layout
6 from rpython.jit.backend.riscv import registers as r
7 from rpython.jit.backend.riscv.arch import (
8 ABI_STACK_ALIGN, FLEN, INST_SIZE, XLEN)
9 from rpython.jit.backend.riscv.codebuilder import OverwritingBuilder
10 from rpython.jit.backend.riscv.instructions import (
11 AMO_ACQUIRE, AMO_RELEASE)
12 from rpython.jit.metainterp.history import FLOAT
13 from rpython.rlib.objectmodel import we_are_translated
14 from rpython.rtyper.lltypesystem import rffi
15
16
17 class RISCVCallBuilder(AbstractCallBuilder):
18 def __init__(self, assembler, fnloc, arglocs, resloc, restype, ressize):
19 AbstractCallBuilder.__init__(self, assembler, fnloc, arglocs,
20 resloc, restype, ressize)
21 self.current_sp = 0
22
23 def prepare_arguments(self):
24 arglocs = self.arglocs
25
26 non_float_locs = []
27 non_float_regs = []
28 float_locs = []
29 float_regs = []
30 stack_locs = []
31
32 free_regs = [r.x17, r.x16, r.x15, r.x14, r.x13, r.x12, r.x11, r.x10]
33 free_float_regs = [r.f17, r.f16, r.f15, r.f14, r.f13, r.f12, r.f11,
34 r.f10]
35
36 # Collect argument registers.
37 stack_adj_offset = 0
38 for arg in arglocs:
39 if arg.type == FLOAT:
40 if free_float_regs:
41 float_locs.append(arg)
42 float_regs.append(free_float_regs.pop())
43 elif free_regs:
44 # If float registers are exhausted but integer registers
45 # are still available, use integer registers.
46 non_float_locs.append(arg)
47 non_float_regs.append(free_regs.pop())
48 else:
49 stack_adj_offset += FLEN
50 stack_locs.append(arg)
51 else:
52 if free_regs:
53 non_float_locs.append(arg)
54 non_float_regs.append(free_regs.pop())
55 else:
56 stack_adj_offset += XLEN
57 stack_locs.append(arg)
58
59 if stack_locs:
60 # Adjust the stack pointer.
61 stack_adj_offset = ((stack_adj_offset + ABI_STACK_ALIGN - 1)
62 // ABI_STACK_ALIGN * ABI_STACK_ALIGN)
63 assert stack_adj_offset <= 2**11, 'too many arguments'
64 self.mc.ADDI(r.sp.value, r.sp.value, -stack_adj_offset)
65 self.current_sp = stack_adj_offset
66
67 # Spill argument values to the stack offset.
68 sp_offset = 0
69 for loc in stack_locs:
70 self.asm.mov_loc_to_raw_stack(loc, sp_offset)
71 sp_offset += FLEN if loc.type == FLOAT else XLEN
72
73 # Assign the callee function address to the `ra` register.
74 #
75 # Note: In the RISC-V backend, the `ra` (`x1`) register is not an
76 # allocatable register, thus it is preserved between
77 # `remap_frame_layout` calls.
78 if self.fnloc.is_core_reg():
79 self.mc.MV(r.ra.value, self.fnloc.value)
80 elif self.fnloc.is_imm():
81 self.mc.load_int_imm(r.ra.value, self.fnloc.value)
82 else:
83 assert self.fnloc.is_stack()
84 self.mc.load_int(r.ra.value, r.jfp.value, self.fnloc.value)
85 self.fnloc = r.ra
86
87 # Move augment values to argument registers.
88 scratch_core_reg = r.x31
89 scratch_fp_reg = r.f31
90
91 remap_frame_layout(self.asm, non_float_locs, non_float_regs,
92 scratch_core_reg)
93 if float_locs:
94 remap_frame_layout(self.asm, float_locs, float_regs,
95 scratch_fp_reg)
96
97 def push_gcmap(self):
98 noregs = self.asm.cpu.gc_ll_descr.is_shadow_stack()
99 gcmap = self.asm._regalloc.get_gcmap([r.x10], noregs=noregs)
100 self.asm.push_gcmap(self.mc, gcmap)
101
102 def pop_gcmap(self):
103 scratch_reg = r.x12 # caller-saved scratch reg other than ra, x31
104 self.asm._reload_frame_if_necessary(self.mc, tmplocs=[scratch_reg])
105 self.asm.pop_gcmap(self.mc)
106
107 def emit_raw_call(self):
108 assert self.fnloc is r.ra
109 self.mc.JALR(self.fnloc.value, self.fnloc.value, 0)
110
111 def restore_stack_pointer(self):
112 if self.current_sp == 0:
113 return
114 self.mc.ADDI(r.sp.value, r.sp.value, self.current_sp)
115 self.current_sp = 0
116
117 def load_result(self):
118 resloc = self.resloc
119 if self.restype == 'S':
120 assert False, 'unimplemented'
121 elif self.restype == 'L':
122 assert False, 'unimplemented'
123 if resloc is not None and resloc.is_core_reg():
124 self._ensure_result_bit_extension(resloc, self.ressize,
125 self.ressign)
126
127 def _ensure_result_bit_extension(self, resloc, size, signed):
128 if size == XLEN:
129 return
130 assert XLEN == 8, 'implementation below assumes 64-bit backend'
131 if size == 4:
132 if signed:
133 self.mc.SLLI(resloc.value, resloc.value, 32)
134 self.mc.SRAI(resloc.value, resloc.value, 32)
135 else:
136 self.mc.SLLI(resloc.value, resloc.value, 32)
137 self.mc.SRLI(resloc.value, resloc.value, 32)
138 elif size == 2:
139 if signed:
140 self.mc.SLLI(resloc.value, resloc.value, 48)
141 self.mc.SRAI(resloc.value, resloc.value, 48)
142 else:
143 self.mc.SLLI(resloc.value, resloc.value, 48)
144 self.mc.SRLI(resloc.value, resloc.value, 48)
145 elif size == 1:
146 if not signed:
147 self.mc.ANDI(resloc.value, resloc.value, 0xFF)
148 else:
149 self.mc.SLLI(resloc.value, resloc.value, 56)
150 self.mc.SRAI(resloc.value, resloc.value, 56)
151
152 def call_releasegil_addr_and_move_real_arguments(self, fastgil):
153 assert self.is_call_release_gil
154 assert not self.asm._is_asmgcc()
155
156 # `r.thread_id` holds our thread identifier.
157 # `r.shadow_old` holds the old value of the shadow stack pointer, which
158 # we save here for later comparison.
159
160 scratch_reg = r.x31
161
162 gcrootmap = self.asm.cpu.gc_ll_descr.gcrootmap
163 if gcrootmap:
164 rst = gcrootmap.get_root_stack_top_addr()
165 self.mc.load_int_imm(scratch_reg.value, rst)
166 self.mc.load_int(r.shadow_old.value, scratch_reg.value, 0)
167
168 # Change `rpy_fastgil` to 0 (it should be non-zero right now) and save
169 # the old value of `rpy_fastgil` into `r.thread_id`.
170 self.mc.load_int_imm(scratch_reg.value, fastgil)
171 self.mc.load_int(r.thread_id.value, scratch_reg.value, 0)
172
173 # atomic_store_int(0, &rpy_fastgil, mo_release)
174 self.mc.atomic_swap_int(r.x0.value, r.x0.value, scratch_reg.value,
175 AMO_RELEASE)
176
177 if not we_are_translated():
178 # For testing, we should not access the jfp register any more.
179 self.mc.ADDI(r.jfp.value, r.jfp.value, 1)
180
181 def write_real_errno(self, save_err):
182 # Use caller-saved registers as scratch registers.
183 #
184 # Note: Skip x10-x17 registers because they contain the arguments to
185 # the future call.
186 tls_reg = r.x31
187 addr_reg = r.x30
188 scratch_reg = r.x29
189
190 if save_err & rffi.RFFI_READSAVED_ERRNO:
191 # Just before a call, read `*_errno` and write it into the real
192 # `errno`.
193
194 if save_err & rffi.RFFI_ALT_ERRNO:
195 rpy_errno = llerrno.get_alt_errno_offset(self.asm.cpu)
196 else:
197 rpy_errno = llerrno.get_rpy_errno_offset(self.asm.cpu)
198
199 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
200
201 # TODO: Replace saved_threadlocal_addr with RISCV architecture `tp`
202 # (thread pointer) register.
203 self.mc.load_int(tls_reg.value, r.sp.value,
204 self.asm.saved_threadlocal_addr + self.current_sp)
205 self.mc.load_int_from_base_plus_offset(addr_reg.value,
206 tls_reg.value, p_errno)
207 self.mc.load_rffi_int_from_base_plus_offset(scratch_reg.value,
208 tls_reg.value,
209 rpy_errno)
210 self.mc.store_rffi_int(scratch_reg.value, addr_reg.value, 0)
211 elif save_err & rffi.RFFI_ZERO_ERRNO_BEFORE:
212 # Same, but write zero.
213 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
214 self.mc.load_int(tls_reg.value, r.sp.value,
215 self.asm.saved_threadlocal_addr + self.current_sp)
216 self.mc.load_int_from_base_plus_offset(addr_reg.value,
217 tls_reg.value, p_errno)
218 self.mc.store_rffi_int(r.x0.value, addr_reg.value, 0)
219
220 def read_real_errno(self, save_err):
221 if save_err & rffi.RFFI_SAVE_ERRNO:
222 # Just after a call, read the real `errno` and save a copy of
223 # it inside our thread-local `*_errno`.
224
225 # Use caller-saved registers as scratch registers.
226 tls_reg = r.x30
227 scratch_reg = r.x31
228 scratch2_reg = r.x29
229
230 if save_err & rffi.RFFI_ALT_ERRNO:
231 rpy_errno = llerrno.get_alt_errno_offset(self.asm.cpu)
232 else:
233 rpy_errno = llerrno.get_rpy_errno_offset(self.asm.cpu)
234
235 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
236
237 self.mc.load_int(tls_reg.value, r.sp.value,
238 self.asm.saved_threadlocal_addr)
239 self.mc.load_int_from_base_plus_offset(scratch_reg.value,
240 tls_reg.value, p_errno)
241 self.mc.load_rffi_int(scratch_reg.value, scratch_reg.value, 0)
242 self.mc.store_rffi_int_to_base_plus_offset(scratch_reg.value,
243 tls_reg.value,
244 rpy_errno,
245 tmp=scratch2_reg.value)
246
247 def move_real_result_and_call_reacqgil_addr(self, fastgil):
248 # Try to reacquire the lock. The following two values are saved across
249 # the call and are still alive now:
250 #
251 # r.thread_id # our thread ident
252 # r.shadow_old # old value of the shadowstack pointer
253
254 # Scratch registers (these must be caller-saved registers)
255 scratch_reg = r.x31
256 rpy_fastgil_adr_reg = r.x30
257 old_fastgil_reg = r.x29
258
259 # Load the address of rpy_fastgil.
260 self.mc.load_int_imm(rpy_fastgil_adr_reg.value, fastgil)
261
262 # Compare-and-swap rpy_fastgil:
263 #
264 # atomic_compare_exchange_strong(old=0, new=r.thread_id,
265 # addr=&rpy_fastgil)
266 self.mc.load_reserve_int(old_fastgil_reg.value,
267 rpy_fastgil_adr_reg.value,
268 AMO_ACQUIRE | AMO_RELEASE)
269 self.mc.BNEZ(old_fastgil_reg.value, 12)
270 self.mc.store_conditional_int(scratch_reg.value, r.thread_id.value,
271 rpy_fastgil_adr_reg.value,
272 AMO_ACQUIRE | AMO_RELEASE)
273 self.mc.BNEZ(scratch_reg.value, -12) # Re-try for spurious SC failure.
274
275 # Now, the `old_fastgil_reg` keeps the old value of the lock, and if
276 # `old_fastgil_reg == 0` then the lock now contains `r.thread_id`.
277
278 # Patch Location:
279 # - boehm: `BEQZ old_fastgil_reg, end`
280 # - shadowstack: `BNEZ old_fastgil_reg, reacqgil_slowpath`
281 b1_location = self.mc.get_relative_pos()
282 self.mc.EBREAK()
283
284 gcrootmap = self.asm.cpu.gc_ll_descr.gcrootmap
285 if gcrootmap:
286 # When doing a call_release_gil with shadowstack, there is the risk
287 # that the `rpy_fastgil` was free but the current shadowstack can
288 # be the one of a different thread. So here we check if the
289 # shadowstack pointer is still the same as before we released the
290 # GIL (saved in `r.shadow_old`), and if not, we fall back to
291 # `reacqgil_addr`.
292 rst = gcrootmap.get_root_stack_top_addr()
293 self.mc.load_int_imm(scratch_reg.value, rst)
294 self.mc.load_int(scratch_reg.value, scratch_reg.value, 0)
295
296 # Patch Location: `BEQ scratch_reg, r.shadow_old, end`
297 b3_location = self.mc.get_relative_pos()
298 self.mc.EBREAK()
299
300 # Revert the rpy_fastgil acquired above, so that the general
301 # `self.asm.reacqgil_addr` below can acquire it again.
302 #
303 # atomic_store_int(0, &rpy_fastgil, mo_release)
304 self.mc.atomic_swap_int(r.x0.value, r.x0.value,
305 rpy_fastgil_adr_reg.value, AMO_RELEASE)
306
307 # Patch the b1_location above.
308 pmc = OverwritingBuilder(self.mc, b1_location, INST_SIZE)
309 pmc.BNEZ(old_fastgil_reg.value,
310 self.mc.get_relative_pos() - b1_location)
311
312 open_location = b3_location
313 else:
314 open_location = b1_location
315
316 # LABEL[reacqgil_slowpath]:
317 #
318 # Save the result value across `reacqgil`.
319 saved_res = r.thread_id # Reuse `r.thread_id` to save things
320 reg = self.resloc
321 if reg is not None:
322 if reg.is_core_reg():
323 self.mc.MV(saved_res.value, reg.value)
324 elif reg.is_fp_reg():
325 assert XLEN == FLEN
326 self.mc.FMV_X_D(saved_res.value, reg.value)
327
328 # Call the `reacqgil` function.
329 self.mc.load_int_imm(r.ra.value, self.asm.reacqgil_addr)
330 self.mc.JALR(r.ra.value, r.ra.value, 0)
331
332 # Restore the saved register
333 if reg is not None:
334 if reg.is_core_reg():
335 self.mc.MV(reg.value, saved_res.value)
336 elif reg.is_fp_reg():
337 assert XLEN == FLEN
338 self.mc.FMV_D_X(reg.value, saved_res.value)
339
340 # LABEL[end]:
341 #
342 # Patch the `open_location` jump above:
343 pmc = OverwritingBuilder(self.mc, open_location, INST_SIZE)
344 offset = self.mc.get_relative_pos() - open_location
345 if gcrootmap:
346 pmc.BEQ(scratch_reg.value, r.shadow_old.value, offset)
347 else:
348 pmc.BEQZ(old_fastgil_reg.value, offset)
349
350 if not we_are_translated():
351 # For testing, now we can access the jfp register again.
352 self.mc.ADDI(r.jfp.value, r.jfp.value, -1)