Debian Codegraph @

rpython/jit/backend/ppc/callbuilder.py

raw symbols SCIP
1 from rpython.jit.backend.ppc.arch import IS_PPC_64, WORD, PARAM_SAVE_AREA_OFFSET
2 from rpython.jit.backend.ppc.arch import THREADLOCAL_ADDR_OFFSET
3 import rpython.jit.backend.ppc.register as r
4 from rpython.jit.metainterp.history import INT, FLOAT
5 from rpython.jit.backend.llsupport.callbuilder import AbstractCallBuilder
6 from rpython.jit.backend.ppc.jump import remap_frame_layout
7 from rpython.rlib.objectmodel import we_are_translated
8 from rpython.jit.backend.llsupport import llerrno
9 from rpython.rtyper.lltypesystem import rffi
10
11
12 def follow_jump(addr):
13 # xxx implement me
14 return addr
15
16
17 class CallBuilder(AbstractCallBuilder):
18 GPR_ARGS = [r.r3, r.r4, r.r5, r.r6, r.r7, r.r8, r.r9, r.r10]
19 FPR_ARGS = r.MANAGED_FP_REGS
20 assert FPR_ARGS == [r.f1, r.f2, r.f3, r.f4, r.f5, r.f6, r.f7,
21 r.f8, r.f9, r.f10, r.f11, r.f12, r.f13]
22 RSHADOWPTR = r.RCS1
23 RFASTGILPTR = r.RCS2
24 RSHADOWOLD = r.RCS3
25 RTHREADID = r.RCS4 # loaded from the old value of rpy_fastgil
26
27 ressign = True
28
29 def __init__(self, assembler, fnloc, arglocs, resloc):
30 AbstractCallBuilder.__init__(self, assembler, fnloc, arglocs,
31 resloc, restype=INT, ressize=WORD)
32
33 def prepare_arguments(self):
34 assert IS_PPC_64
35 self.subtracted_to_sp = 0
36
37 # Prepare arguments. Note that this follows the convention where
38 # a prototype is in scope, and doesn't take "..." arguments. If
39 # you were to call a C function with a "..." argument with cffi,
40 # it would not go there but instead via libffi. If you pretend
41 # instead that it takes fixed arguments, then it would arrive here
42 # but the convention is bogus for floating-point arguments. (And,
43 # to add to the mess, at least CPython's ctypes cannot be used
44 # to call a "..." function with floating-point arguments. As I
45 # guess that it's a problem with libffi, it means PyPy inherits
46 # the same problem.)
47 arglocs = self.arglocs
48 num_args = len(arglocs)
49
50 non_float_locs = []
51 non_float_regs = []
52 float_locs = []
53 for i in range(min(num_args, 8)):
54 if arglocs[i].type != FLOAT:
55 non_float_locs.append(arglocs[i])
56 non_float_regs.append(self.GPR_ARGS[i])
57 else:
58 float_locs.append(arglocs[i])
59 # now 'non_float_locs' and 'float_locs' together contain the
60 # locations of the first 8 arguments
61
62 if num_args > 8:
63 # We need to make a larger PPC stack frame, as shown on the
64 # picture in arch.py. It needs to be 48 bytes + 8 * num_args.
65 # The new SP back chain location should point to the top of
66 # the whole stack frame, i.e. jumping over both the existing
67 # fixed-sise part and the new variable-sized part.
68 base = PARAM_SAVE_AREA_OFFSET
69 varsize = base + 8 * num_args
70 varsize = (varsize + 15) & ~15 # align
71 self.mc.load(r.SCRATCH2.value, r.SP.value, 0) # SP back chain
72 self.mc.store_update(r.SCRATCH2.value, r.SP.value, -varsize)
73 self.subtracted_to_sp = varsize
74
75 # In this variable-sized part, only the arguments from the 8th
76 # one need to be written, starting at SP + 112
77 for n in range(8, num_args):
78 loc = arglocs[n]
79 if loc.type != FLOAT:
80 # after the 8th argument, a non-float location is
81 # always stored in the stack
82 if loc.is_reg():
83 src = loc
84 else:
85 src = r.r2
86 self.asm.regalloc_mov(loc, src)
87 self.mc.std(src.value, r.SP.value, base + 8 * n)
88 else:
89 # the first 13 floating-point arguments are all passed
90 # in the registers f1 to f13, independently on their
91 # index in the complete list of arguments
92 if len(float_locs) < len(self.FPR_ARGS):
93 float_locs.append(loc)
94 else:
95 if loc.is_fp_reg():
96 src = loc
97 else:
98 src = r.FP_SCRATCH
99 self.asm.regalloc_mov(loc, src)
100 self.mc.stfd(src.value, r.SP.value, base + 8 * n)
101
102 # We must also copy fnloc into FNREG
103 non_float_locs.append(self.fnloc)
104 non_float_regs.append(self.mc.RAW_CALL_REG)
105
106 if float_locs:
107 assert len(float_locs) <= len(self.FPR_ARGS)
108 remap_frame_layout(self.asm, float_locs,
109 self.FPR_ARGS[:len(float_locs)],
110 r.FP_SCRATCH)
111
112 remap_frame_layout(self.asm, non_float_locs, non_float_regs,
113 r.SCRATCH)
114
115
116 def push_gcmap(self):
117 # we push *now* the gcmap, describing the status of GC registers
118 # after the rearrangements done just before, ignoring the return
119 # value r3, if necessary
120 assert not self.is_call_release_gil
121 noregs = self.asm.cpu.gc_ll_descr.is_shadow_stack()
122 gcmap = self.asm._regalloc.get_gcmap([r.r3], noregs=noregs)
123 self.asm.push_gcmap(self.mc, gcmap, store=True)
124
125 def pop_gcmap(self):
126 ssreg = None
127 gcrootmap = self.asm.cpu.gc_ll_descr.gcrootmap
128 if gcrootmap:
129 if gcrootmap.is_shadow_stack and self.is_call_release_gil:
130 # in this mode, RSHADOWOLD happens to contain the shadowstack
131 # top at this point, so reuse it instead of loading it again
132 ssreg = self.RSHADOWOLD
133 self.asm._reload_frame_if_necessary(self.mc, shadowstack_reg=ssreg)
134
135 def emit_raw_call(self):
136 self.mc.raw_call()
137
138 def restore_stack_pointer(self):
139 if self.subtracted_to_sp != 0:
140 self.mc.addi(r.SP.value, r.SP.value, self.subtracted_to_sp)
141
142 def load_result(self):
143 assert (self.resloc is None or
144 self.resloc is r.r3 or
145 self.resloc is r.f1)
146 ressize = self.ressize
147 if ressize == 1:
148 assert self.resloc is r.r3
149 if self.ressign:
150 self.mc.extsb(r.r3.value, r.r3.value)
151 else:
152 self.mc.rldicl(r.r3.value, r.r3.value, 0, 56)
153 elif ressize == 2:
154 assert self.resloc is r.r3
155 if self.ressign:
156 self.mc.extsh(r.r3.value, r.r3.value)
157 else:
158 self.mc.rldicl(r.r3.value, r.r3.value, 0, 48)
159 elif ressize == 4:
160 assert self.resloc is r.r3
161 if self.ressign:
162 self.mc.extsw(r.r3.value, r.r3.value)
163 else:
164 self.mc.rldicl(r.r3.value, r.r3.value, 0, 32)
165
166 def call_releasegil_addr_and_move_real_arguments(self, fastgil):
167 assert self.is_call_release_gil
168 RSHADOWPTR = self.RSHADOWPTR
169 RFASTGILPTR = self.RFASTGILPTR
170 RSHADOWOLD = self.RSHADOWOLD
171 RTHREADID = self.RTHREADID
172 #
173 # Save this thread's shadowstack pointer into r29, for later comparison
174 gcrootmap = self.asm.cpu.gc_ll_descr.gcrootmap
175 if gcrootmap:
176 if gcrootmap.is_shadow_stack:
177 rst = gcrootmap.get_root_stack_top_addr()
178 self.mc.load_imm(RSHADOWPTR, rst)
179 self.mc.load(RSHADOWOLD.value, RSHADOWPTR.value, 0)
180 #
181 # change 'rpy_fastgil' to 0 (it should be non-zero right now)
182 self.mc.load_imm(RFASTGILPTR, fastgil)
183 self.mc.ld(RTHREADID.value, RFASTGILPTR.value, 0)
184 self.mc.li(r.r0.value, 0)
185 self.mc.lwsync()
186 self.mc.std(r.r0.value, RFASTGILPTR.value, 0)
187 #
188 if not we_are_translated(): # for testing: we should not access
189 self.mc.addi(r.SPP.value, r.SPP.value, 1) # r31 any more
190
191
192 def move_real_result_and_call_reacqgil_addr(self, fastgil):
193 from rpython.jit.backend.ppc.codebuilder import OverwritingBuilder
194
195 # try to reacquire the lock. The following registers are still
196 # valid from before the call:
197 RSHADOWPTR = self.RSHADOWPTR # r30: &root_stack_top
198 RFASTGILPTR = self.RFASTGILPTR # r29: &fastgil
199 RSHADOWOLD = self.RSHADOWOLD # r28: previous val of root_stack_top
200 RTHREADID = self.RTHREADID # r27: my thread id
201
202 # Equivalent of
203 # r10 = __sync_val_compare_and_swap(&rpy_fastgil, 0, thread_id);
204 self.mc.sync()
205 retry_label = self.mc.currpos()
206 self.mc.ldarx(r.r10.value, 0, RFASTGILPTR.value) # load the lock value
207 self.mc.cmpdi(0, r.r10.value, 0)
208 self.mc.bc(6, 2, +12)
209 self.mc.stdcxx(RTHREADID.value, 0, RFASTGILPTR.value) #try to claim lock
210 self.mc.bc(6, 2, retry_label - self.mc.currpos()) # retry if failed
211 self.mc.isync()
212
213 b1_location = self.mc.currpos()
214 self.mc.trap() # boehm: patched with a BEQ: jump if r10 is zero
215 # shadowstack: patched with BNE instead
216
217 if self.asm.cpu.gc_ll_descr.gcrootmap:
218 # When doing a call_release_gil with shadowstack, there
219 # is the risk that the 'rpy_fastgil' was free but the
220 # current shadowstack can be the one of a different
221 # thread. So here we check if the shadowstack pointer
222 # is still the same as before we released the GIL (saved
223 # in RSHADOWOLD), and if not, we fall back to 'reacqgil_addr'.
224 self.mc.load(r.r9.value, RSHADOWPTR.value, 0)
225 self.mc.cmpd(0, r.r9.value, RSHADOWOLD.value)
226 bne_location = b1_location
227 b1_location = self.mc.currpos()
228 self.mc.trap()
229
230 # revert the rpy_fastgil acquired above, so that the
231 # general 'reacqgil_addr' below can acquire it again...
232 # (here, r10 is conveniently zero)
233 self.mc.std(r.r10.value, RFASTGILPTR.value, 0)
234
235 pmc = OverwritingBuilder(self.mc, bne_location, 1)
236 pmc.bne(self.mc.currpos() - bne_location)
237 pmc.overwrite()
238 #
239 # Yes, we need to call the reacqgil() function.
240 # save the result we just got
241 RSAVEDRES = RFASTGILPTR # can reuse this reg here
242 reg = self.resloc
243 if reg is not None:
244 if reg.is_core_reg():
245 self.mc.mr(RSAVEDRES.value, reg.value)
246 elif reg.is_fp_reg():
247 self.mc.stfd(reg.value, r.SP.value,
248 PARAM_SAVE_AREA_OFFSET + 7 * WORD)
249 self.mc.load_imm(self.mc.RAW_CALL_REG, self.asm.reacqgil_addr)
250 self.mc.raw_call()
251 if reg is not None:
252 if reg.is_core_reg():
253 self.mc.mr(reg.value, RSAVEDRES.value)
254 elif reg.is_fp_reg():
255 self.mc.lfd(reg.value, r.SP.value,
256 PARAM_SAVE_AREA_OFFSET + 7 * WORD)
257
258 # replace b1_location with BEQ(here)
259 pmc = OverwritingBuilder(self.mc, b1_location, 1)
260 pmc.beq(self.mc.currpos() - b1_location)
261 pmc.overwrite()
262
263 if not we_are_translated(): # for testing: now we can access
264 self.mc.addi(r.SPP.value, r.SPP.value, -1) # r31 again
265
266
267 def write_real_errno(self, save_err):
268 if save_err & rffi.RFFI_READSAVED_ERRNO:
269 # Just before a call, read '*_errno' and write it into the
270 # real 'errno'. A lot of registers are free here, notably
271 # r11 and r0.
272 if save_err & rffi.RFFI_ALT_ERRNO:
273 rpy_errno = llerrno.get_alt_errno_offset(self.asm.cpu)
274 else:
275 rpy_errno = llerrno.get_rpy_errno_offset(self.asm.cpu)
276 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
277 self.mc.ld(r.r11.value, r.SP.value,
278 THREADLOCAL_ADDR_OFFSET + self.subtracted_to_sp)
279 self.mc.lwz(r.r0.value, r.r11.value, rpy_errno)
280 self.mc.ld(r.r11.value, r.r11.value, p_errno)
281 self.mc.stw(r.r0.value, r.r11.value, 0)
282 elif save_err & rffi.RFFI_ZERO_ERRNO_BEFORE:
283 # Same, but write zero.
284 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
285 self.mc.ld(r.r11.value, r.SP.value,
286 THREADLOCAL_ADDR_OFFSET + self.subtracted_to_sp)
287 self.mc.ld(r.r11.value, r.r11.value, p_errno)
288 self.mc.li(r.r0.value, 0)
289 self.mc.stw(r.r0.value, r.r11.value, 0)
290
291 def read_real_errno(self, save_err):
292 if save_err & rffi.RFFI_SAVE_ERRNO:
293 # Just after a call, read the real 'errno' and save a copy of
294 # it inside our thread-local '*_errno'. Registers r4-r10
295 # never contain anything after the call.
296 if save_err & rffi.RFFI_ALT_ERRNO:
297 rpy_errno = llerrno.get_alt_errno_offset(self.asm.cpu)
298 else:
299 rpy_errno = llerrno.get_rpy_errno_offset(self.asm.cpu)
300 p_errno = llerrno.get_p_errno_offset(self.asm.cpu)
301 self.mc.ld(r.r9.value, r.SP.value, THREADLOCAL_ADDR_OFFSET)
302 self.mc.ld(r.r10.value, r.r9.value, p_errno)
303 self.mc.lwz(r.r10.value, r.r10.value, 0)
304 self.mc.stw(r.r10.value, r.r9.value, rpy_errno)