Debian Codegraph @

rpython/jit/backend/aarch64/callbuilder.py

raw symbols SCIP
1
2 from rpython.jit.backend.llsupport.callbuilder import AbstractCallBuilder
3 from rpython.jit.backend.aarch64.arch import WORD
4 from rpython.jit.metainterp.history import INT, FLOAT, REF
5 from rpython.jit.backend.aarch64 import registers as r
6 from rpython.jit.backend.arm import conditions as c
7 from rpython.jit.backend.aarch64.jump import remap_frame_layout # we use arm algo
8 from rpython.jit.backend.llsupport import llerrno
9 from rpython.jit.backend.aarch64.codebuilder import OverwritingBuilder
10
11 from rpython.rlib.objectmodel import we_are_translated
12 from rpython.rtyper.lltypesystem import rffi
13
14 class Aarch64CallBuilder(AbstractCallBuilder):
15 def __init__(self, assembler, fnloc, arglocs,
16 resloc=r.x0, restype=INT, ressize=WORD, ressigned=True):
17 AbstractCallBuilder.__init__(self, assembler, fnloc, arglocs,
18 resloc, restype, ressize)
19 self.current_sp = 0
20
21 def prepare_arguments(self):
22 arglocs = self.arglocs
23 non_float_locs = []
24 non_float_regs = []
25 float_locs = []
26 float_regs = []
27 stack_locs = []
28 free_regs = [r.x7, r.x6, r.x5, r.x4, r.x3, r.x2, r.x1, r.x0]
29 free_float_regs = [r.d7, r.d6, r.d5, r.d4, r.d3, r.d2, r.d1, r.d0]
30 for arg in arglocs:
31 if arg.type == FLOAT:
32 if free_float_regs:
33 float_locs.append(arg)
34 float_regs.append(free_float_regs.pop())
35 else:
36 stack_locs.append(arg)
37 else:
38 if free_regs:
39 non_float_locs.append(arg)
40 non_float_regs.append(free_regs.pop())
41 else:
42 stack_locs.append(arg)
43
44 if stack_locs:
45 adj = len(stack_locs) + (len(stack_locs) & 1)
46 self.mc.SUB_ri(r.sp.value, r.sp.value, adj * WORD)
47 self.current_sp = adj * WORD
48 c = 0
49 for loc in stack_locs:
50 self.asm.mov_loc_to_raw_stack(loc, c)
51 c += WORD
52
53 move_back = False
54 if not self.fnloc.is_imm():
55 if self.fnloc.is_core_reg():
56 self.mc.MOV_rr(r.ip1.value, self.fnloc.value)
57 else:
58 assert self.fnloc.is_stack()
59 self.mc.LDR_ri(r.ip1.value, r.fp.value, self.fnloc.value)
60 self.fnloc = r.x8
61 move_back = True
62
63 remap_frame_layout(self.asm, non_float_locs, non_float_regs, r.ip0)
64 if float_locs:
65 remap_frame_layout(self.asm, float_locs, float_regs, r.d8)
66
67 if move_back:
68 self.mc.MOV_rr(r.x8.value, r.ip1.value)
69
70 def push_gcmap(self):
71 noregs = self.asm.cpu.gc_ll_descr.is_shadow_stack()
72 gcmap = self.asm._regalloc.get_gcmap([r.x0], noregs=noregs)
73 self.asm.push_gcmap(self.mc, gcmap)
74
75 def pop_gcmap(self):
76 self.asm._reload_frame_if_necessary(self.mc)
77 self.asm.pop_gcmap(self.mc)
78
79 def emit_raw_call(self):
80 #the actual call
81 if self.fnloc.is_imm():
82 self.mc.BL(self.fnloc.value)
83 return
84 if self.fnloc.is_stack():
85 assert False, "we should never be here"
86 else:
87 assert self.fnloc.is_core_reg()
88 assert self.fnloc is r.x8
89 self.mc.BLR_r(self.fnloc.value)
90
91 def restore_stack_pointer(self):
92 assert self.current_sp & 1 == 0 # always adjusted to 16 bytes
93 if self.current_sp == 0:
94 return
95 self.mc.ADD_ri(r.sp.value, r.sp.value, self.current_sp)
96 self.current_sp = 0
97
98 def load_result(self):
99 resloc = self.resloc
100 if self.restype == 'S':
101 assert False, "not supported yet"
102 XXX
103 self.mc.VMOV_sc(resloc.value, r.s0.value)
104 elif self.restype == 'L':
105 assert False, "not possible on 64bit backend"
106 YYY
107 assert resloc.is_vfp_reg()
108 self.mc.FMDRR(resloc.value, r.r0.value, r.r1.value)
109 # ensure the result is wellformed and stored in the correct location
110 if resloc is not None and resloc.is_core_reg():
111 self._ensure_result_bit_extension(resloc,
112 self.ressize, self.ressign)
113
114 def _ensure_result_bit_extension(self, resloc, size, signed):
115 if size == WORD:
116 return
117 if size == 4:
118 if not signed: # unsigned int
119 self.mc.LSL_ri(resloc.value, resloc.value, 32)
120 self.mc.LSR_ri(resloc.value, resloc.value, 32)
121 else: # signed int
122 self.mc.LSL_ri(resloc.value, resloc.value, 32)
123 self.mc.ASR_ri(resloc.value, resloc.value, 32)
124 elif size == 2:
125 if not signed:
126 self.mc.LSL_ri(resloc.value, resloc.value, 48)
127 self.mc.LSR_ri(resloc.value, resloc.value, 48)
128 else:
129 self.mc.LSL_ri(resloc.value, resloc.value, 48)
130 self.mc.ASR_ri(resloc.value, resloc.value, 48)
131 elif size == 1:
132 if not signed: # unsigned char
133 self.mc.AND_ri(resloc.value, resloc.value, 0xFF)
134 else:
135 self.mc.LSL_ri(resloc.value, resloc.value, 56)
136 self.mc.ASR_ri(resloc.value, resloc.value, 56)
137
138 def call_releasegil_addr_and_move_real_arguments(self, fastgil):
139 assert self.is_call_release_gil
140 assert not self.asm._is_asmgcc()
141 RTHREADID = r.x19 # our thread ident
142 RSHADOWOLD = r.x20 # old value of the shadowstack pointer,
143 # which we save here for later comparison
144
145 gcrootmap = self.asm.cpu.gc_ll_descr.gcrootmap
146 if gcrootmap:
147 rst = gcrootmap.get_root_stack_top_addr()
148 self.mc.gen_load_int(r.ip1.value, rst)
149 self.mc.LDR_ri(RSHADOWOLD.value, r.ip1.value, 0)
150
151 # change 'rpy_fastgil' to 0 (it should be non-zero right now)
152 # and save the old value of 'rpy_fastgil' into RTHREADID
153 self.mc.gen_load_int(r.ip1.value, fastgil)
154 self.mc.LDR_ri(RTHREADID.value, r.ip1.value, 0)
155 self.mc.STLR(r.xzr.value, r.ip1.value)
156
157 if not we_are_translated(): # for testing: we should not access
158 self.mc.ADD_ri(r.fp.value, r.fp.value, 1) # fp any more
159
160 def write_real_errno(self, save_err):
161 if save_err & rffi.RFFI_READSAVED_ERRNO:
162 # Just before a call, read '*_errno' and write it into the
163 # real 'errno'. The x0-x7 registers contain arguments to the
164 # future call;
165 # the x8-x10 registers contain various stuff. XXX what?
166 # We still have x11 and up.
167 if save_err & rffi.RFFI_ALT_ERRNO:
168 rpy_errno = llerrno.get_alt_errno_offset(self.asm.cpu)
169 else:
170 rpy_errno = llerrno.get_rpy_errno_offset(self.asm.cpu)
171 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
172 self.mc.LDR_ri(r.x11.value, r.sp.value,
173 self.asm.saved_threadlocal_addr + self.current_sp)
174 self.mc.LDR_ri(r.ip0.value, r.x11.value, p_errno)
175 self.mc.LDR_ri(r.x11.value, r.x11.value, rpy_errno)
176 self.mc.STR_ri(r.x11.value, r.ip0.value, 0)
177 elif save_err & rffi.RFFI_ZERO_ERRNO_BEFORE:
178 # Same, but write zero.
179 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
180 self.mc.LDR_ri(r.x11.value, r.sp.value,
181 self.asm.saved_threadlocal_addr + self.current_sp)
182 self.mc.LDR_ri(r.ip0.value, r.x11.value, p_errno)
183 self.mc.MOVZ_r_u16(r.x11.value, 0, 0)
184 self.mc.STR_ri(r.x11.value, r.ip0.value, 0)
185
186 def read_real_errno(self, save_err):
187 if save_err & rffi.RFFI_SAVE_ERRNO:
188 # Just after a call, read the real 'errno' and save a copy of
189 # it inside our thread-local '*_errno'. Registers x11 and up
190 # are unused here, and registers x2-x3 never contain anything
191 # after the call.
192 if save_err & rffi.RFFI_ALT_ERRNO:
193 rpy_errno = llerrno.get_alt_errno_offset(self.asm.cpu)
194 else:
195 rpy_errno = llerrno.get_rpy_errno_offset(self.asm.cpu)
196 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
197 self.mc.LDR_ri(r.x3.value, r.sp.value,
198 self.asm.saved_threadlocal_addr)
199 self.mc.LDR_ri(r.ip0.value, r.x3.value, p_errno)
200 self.mc.LDR_ri(r.ip0.value, r.ip0.value, 0)
201 self.mc.STR_ri(r.ip0.value, r.x3.value, rpy_errno)
202
203 def move_real_result_and_call_reacqgil_addr(self, fastgil):
204 # try to reacquire the lock. The following two values are saved
205 # across the call and are still alive now:
206 RTHREADID = r.x19 # our thread ident
207 RSHADOWOLD = r.x20 # old value of the shadowstack pointer
208
209 RPYFASTGIL = r.ip2 # &rpy_fastgil, loaded now:
210 self.mc.gen_load_int(RPYFASTGIL.value, fastgil)
211
212 # this comes from gcc compiling this code:
213 # __sync_bool_compare_and_swap(&rpy_fastgil, old=0, new=RTHREADID);
214 self.mc.LDXR(r.x1.value, RPYFASTGIL.value)
215 self.mc.CBNZ(r.x1.value, +12)
216 self.mc.STLXR(r.x3.value, RTHREADID.value, RPYFASTGIL.value)
217 self.mc.CBNZ_w(r.x3.value, -12)
218 self.mc.DMB_ISH()
219 # now x1 is the old value of the lock, and if x1 == 0 then the lock
220 # now contains RTHREADID
221
222 b1_location = self.mc.currpos()
223 self.mc.BRK() # boehm: patched with a CBZ (jump if x1 == 0)
224 # shadowstack: patched with CBNZ instead
225
226 gcrootmap = self.asm.cpu.gc_ll_descr.gcrootmap
227 if gcrootmap:
228 # When doing a call_release_gil with shadowstack, there
229 # is the risk that the 'rpy_fastgil' was free but the
230 # current shadowstack can be the one of a different
231 # thread. So here we check if the shadowstack pointer
232 # is still the same as before we released the GIL (saved
233 # in 'x20'), and if not, we fall back to 'reacqgil_addr'.
234 rst = gcrootmap.get_root_stack_top_addr()
235 self.mc.gen_load_int(r.ip1.value, rst)
236 self.mc.LDR_ri(r.ip0.value, r.ip1.value, 0) # new shadowstack
237 self.mc.CMP_rr(r.ip0.value, RSHADOWOLD.value)
238 b3_location = self.mc.currpos()
239 self.mc.BRK() # B.eq forward
240
241 # revert the rpy_fastgil acquired above, so that the
242 # general 'reacqgil_addr' below can acquire it again...
243 self.mc.STR_ri(r.xzr.value, RPYFASTGIL.value, 0)
244
245 # patch the b1_location above, with "CBNZ here"
246 pmc = OverwritingBuilder(self.mc, b1_location, WORD)
247 pmc.CBNZ(r.x1.value, self.mc.currpos() - b1_location)
248
249 open_location = b3_location
250 else:
251 open_location = b1_location
252
253 # Yes, we need to call the reacqgil() function.
254 # save the result we just got
255 RSAVEDRES = RTHREADID # can reuse this reg here to save things
256 reg = self.resloc
257 if reg is not None:
258 if reg.is_core_reg():
259 self.mc.MOV_rr(RSAVEDRES.value, reg.value)
260 elif reg.is_vfp_reg():
261 self.mc.SUB_ri(r.sp.value, r.sp.value, 2 * WORD)
262 self.mc.STR_di(reg.value, r.sp.value, 0)
263
264 # call the function
265 self.mc.BL(self.asm.reacqgil_addr)
266
267 # restore the saved register
268 if reg is not None:
269 if reg.is_core_reg():
270 self.mc.MOV_rr(reg.value, RSAVEDRES.value)
271 elif reg.is_vfp_reg():
272 self.mc.LDR_di(reg.value, r.sp.value, 0)
273 self.mc.ADD_ri(r.sp.value, r.sp.value, 2 * WORD)
274
275 # now patch the still-open jump above:
276 # boehm: patch b1_location with a CBZ(x1)
277 # shadowstack: patch b3_location with BEQ
278 pmc = OverwritingBuilder(self.mc, open_location, WORD)
279 offset = self.mc.currpos() - open_location
280 if gcrootmap:
281 pmc.B_ofs_cond(offset, c.EQ)
282 else:
283 pmc.CBZ(r.x1.value, offset)
284
285 if not we_are_translated(): # for testing: now we can access
286 self.mc.SUB_ri(r.fp.value, r.fp.value, 1) # fp again
287
288 def get_result_locs(self):
289 if self.resloc is None:
290 return [], []
291 if self.resloc.is_vfp_reg():
292 if self.restype == 'L': # long long
293 return [r.r0], []
294 else:
295 return [], [r.d0]
296 assert self.resloc.is_core_reg()
297 return [r.r0], []