Add safepoint mechanism
[sbcl.git] / src / compiler / x86-64 / system.lisp
1 ;;;; x86 VM definitions of various system hacking operations
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; type frobbing VOPs
15
16 (define-vop (lowtag-of)
17   (:translate lowtag-of)
18   (:policy :fast-safe)
19   (:args (object :scs (any-reg descriptor-reg control-stack)
20                  :target result))
21   (:results (result :scs (unsigned-reg)))
22   (:result-types positive-fixnum)
23   (:generator 1
24     (move result object)
25     (inst and result lowtag-mask)))
26
27 (define-vop (widetag-of)
28   (:translate widetag-of)
29   (:policy :fast-safe)
30   (:args (object :scs (descriptor-reg)))
31   (:temporary (:sc unsigned-reg :offset rax-offset :target result
32                    :to (:result 0)) rax)
33   (:results (result :scs (unsigned-reg)))
34   (:result-types positive-fixnum)
35   (:generator 6
36     (inst movzx rax (reg-in-size object :byte))
37     (inst and al-tn lowtag-mask)
38     (inst cmp al-tn other-pointer-lowtag)
39     (inst jmp :e OTHER-PTR)
40     (inst cmp al-tn fun-pointer-lowtag)
41     (inst jmp :e FUNCTION-PTR)
42
43     ;; Pick off fixnums.
44     (inst test al-tn fixnum-tag-mask)
45     (inst jmp :e DONE)
46
47     ;; Pick off structures and list pointers.
48     (inst test al-tn 2)
49     (inst jmp :ne DONE)
50
51     ;; must be an other immediate
52     (inst movzx rax (reg-in-size object :byte))
53     (inst jmp DONE)
54
55     FUNCTION-PTR
56     (load-type rax object (- fun-pointer-lowtag))
57     (inst jmp DONE)
58
59     OTHER-PTR
60     (load-type rax object (- other-pointer-lowtag))
61
62     DONE
63     (move result rax)))
64 \f
65 (define-vop (fun-subtype)
66   (:translate fun-subtype)
67   (:policy :fast-safe)
68   (:args (function :scs (descriptor-reg)))
69   (:results (result :scs (unsigned-reg)))
70   (:result-types positive-fixnum)
71   (:generator 6
72     (load-type result function (- fun-pointer-lowtag))))
73
74 (define-vop (set-fun-subtype)
75   (:translate (setf fun-subtype))
76   (:policy :fast-safe)
77   (:args (type :scs (unsigned-reg) :target eax)
78          (function :scs (descriptor-reg)))
79   (:arg-types positive-fixnum *)
80   (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 0)
81                    :to (:result 0) :target result)
82               eax)
83   (:results (result :scs (unsigned-reg)))
84   (:result-types positive-fixnum)
85   (:generator 6
86     (move eax type)
87     (inst mov
88           (make-ea :byte :base function :disp (- fun-pointer-lowtag))
89           al-tn)
90     (move result eax)))
91
92 (define-vop (get-header-data)
93   (:translate get-header-data)
94   (:policy :fast-safe)
95   (:args (x :scs (descriptor-reg)))
96   (:results (res :scs (unsigned-reg)))
97   (:result-types positive-fixnum)
98   (:generator 6
99     (loadw res x 0 other-pointer-lowtag)
100     (inst shr res n-widetag-bits)))
101
102 (define-vop (get-closure-length)
103   (:translate get-closure-length)
104   (:policy :fast-safe)
105   (:args (x :scs (descriptor-reg)))
106   (:results (res :scs (unsigned-reg)))
107   (:result-types positive-fixnum)
108   (:generator 6
109     (loadw res x 0 fun-pointer-lowtag)
110     (inst shr res n-widetag-bits)))
111
112 (define-vop (set-header-data)
113   (:translate set-header-data)
114   (:policy :fast-safe)
115   (:args (x :scs (descriptor-reg) :target res :to (:result 0))
116          (data :scs (any-reg) :target eax))
117   (:arg-types * positive-fixnum)
118   (:results (res :scs (descriptor-reg)))
119   (:temporary (:sc unsigned-reg :offset eax-offset
120                    :from (:argument 1) :to (:result 0)) eax)
121   (:generator 6
122     (move eax data)
123     (inst shl eax (- n-widetag-bits n-fixnum-tag-bits))
124     (inst mov al-tn (make-ea :byte :base x :disp (- other-pointer-lowtag)))
125     (storew eax x 0 other-pointer-lowtag)
126     (move res x)))
127 \f
128 (define-vop (pointer-hash)
129   (:translate pointer-hash)
130   (:args (ptr :scs (any-reg descriptor-reg) :target res))
131   (:results (res :scs (any-reg descriptor-reg)))
132   (:policy :fast-safe)
133   (:generator 1
134     (move res ptr)
135     ;; Mask the lowtag, and shift the whole address into a positive
136     ;; fixnum.
137     (inst and res (lognot lowtag-mask))
138     (inst shr res 1)))
139
140 (define-vop (make-other-immediate-type)
141   (:args (val :scs (any-reg descriptor-reg) :target res)
142          (type :scs (unsigned-reg immediate)))
143   (:results (res :scs (any-reg descriptor-reg) :from (:argument 0)))
144   (:generator 2
145     (move res val)
146     (inst shl res (- n-widetag-bits n-fixnum-tag-bits))
147     (inst or res (sc-case type
148                    (unsigned-reg type)
149                    (immediate (tn-value type))))))
150 \f
151 ;;;; allocation
152
153 (define-vop (dynamic-space-free-pointer)
154   (:results (int :scs (sap-reg)))
155   (:result-types system-area-pointer)
156   (:translate dynamic-space-free-pointer)
157   (:policy :fast-safe)
158   (:generator 1
159     (load-symbol-value int *allocation-pointer*)))
160
161 (define-vop (binding-stack-pointer-sap)
162   (:results (int :scs (sap-reg)))
163   (:result-types system-area-pointer)
164   (:translate binding-stack-pointer-sap)
165   (:policy :fast-safe)
166   (:generator 1
167     (load-binding-stack-pointer int)))
168
169 (defknown (setf binding-stack-pointer-sap)
170     (system-area-pointer) system-area-pointer ())
171
172 (define-vop (set-binding-stack-pointer-sap)
173   (:args (new-value :scs (sap-reg) :target int))
174   (:arg-types system-area-pointer)
175   (:results (int :scs (sap-reg)))
176   (:result-types system-area-pointer)
177   (:translate (setf binding-stack-pointer-sap))
178   (:policy :fast-safe)
179   (:generator 1
180     (store-binding-stack-pointer new-value)
181     (move int new-value)))
182
183 (define-vop (control-stack-pointer-sap)
184   (:results (int :scs (sap-reg)))
185   (:result-types system-area-pointer)
186   (:translate control-stack-pointer-sap)
187   (:policy :fast-safe)
188   (:generator 1
189     (move int rsp-tn)))
190 \f
191 ;;;; code object frobbing
192
193 (define-vop (code-instructions)
194   (:translate code-instructions)
195   (:policy :fast-safe)
196   (:args (code :scs (descriptor-reg) :to (:result 0)))
197   (:results (sap :scs (sap-reg) :from (:argument 0)))
198   (:result-types system-area-pointer)
199   (:generator 10
200     (loadw sap code 0 other-pointer-lowtag)
201     (inst shr sap n-widetag-bits)
202     (inst lea sap (make-ea :byte :base code :index sap
203                            :scale n-word-bytes
204                            :disp (- other-pointer-lowtag)))))
205
206 (define-vop (compute-fun)
207   (:args (code :scs (descriptor-reg) :to (:result 0))
208          (offset :scs (signed-reg unsigned-reg) :to (:result 0)))
209   (:arg-types * positive-fixnum)
210   (:results (func :scs (descriptor-reg) :from (:argument 0)))
211   (:generator 10
212     (loadw func code 0 other-pointer-lowtag)
213     (inst shr func n-widetag-bits)
214     (inst lea func
215           (make-ea :byte :base offset :index func
216                    :scale n-word-bytes
217                    :disp (- fun-pointer-lowtag other-pointer-lowtag)))
218     (inst add func code)))
219
220 (define-vop (%simple-fun-self)
221   (:policy :fast-safe)
222   (:translate %simple-fun-self)
223   (:args (function :scs (descriptor-reg)))
224   (:results (result :scs (descriptor-reg)))
225   (:generator 3
226     (loadw result function simple-fun-self-slot fun-pointer-lowtag)
227     (inst lea result
228           (make-ea :byte :base result
229                    :disp (- fun-pointer-lowtag
230                             (* simple-fun-code-offset n-word-bytes))))))
231
232 ;;; The closure function slot is a pointer to raw code on X86 instead
233 ;;; of a pointer to the code function object itself. This VOP is used
234 ;;; to reference the function object given the closure object.
235 (define-source-transform %closure-fun (closure)
236   `(%simple-fun-self ,closure))
237
238 (define-vop (%set-fun-self)
239   (:policy :fast-safe)
240   (:translate (setf %simple-fun-self))
241   (:args (new-self :scs (descriptor-reg) :target result :to :result)
242          (function :scs (descriptor-reg) :to :result))
243   (:temporary (:sc any-reg :from (:argument 0) :to :result) temp)
244   (:results (result :scs (descriptor-reg)))
245   (:generator 3
246     (inst lea temp
247           (make-ea :byte :base new-self
248                    :disp (- (ash simple-fun-code-offset word-shift)
249                             fun-pointer-lowtag)))
250     (storew temp function simple-fun-self-slot fun-pointer-lowtag)
251     (move result new-self)))
252 \f
253 ;;;; other miscellaneous VOPs
254
255 (defknown sb!unix::receive-pending-interrupt () (values))
256 (define-vop (sb!unix::receive-pending-interrupt)
257   (:policy :fast-safe)
258   (:translate sb!unix::receive-pending-interrupt)
259   (:generator 1
260     (inst break pending-interrupt-trap)))
261
262 #!+sb-safepoint
263 (define-vop (insert-safepoint)
264   (:policy :fast-safe)
265   (:translate sb!kernel::gc-safepoint)
266   (:generator 0
267     (emit-safepoint)))
268
269 #!+sb-thread
270 (defknown current-thread-offset-sap ((unsigned-byte 64))
271   system-area-pointer (flushable))
272
273 #!+sb-thread
274 (define-vop (current-thread-offset-sap)
275   (:results (sap :scs (sap-reg)))
276   (:result-types system-area-pointer)
277   (:translate current-thread-offset-sap)
278   (:args (n :scs (unsigned-reg) :target sap))
279   (:arg-types unsigned-num)
280   (:policy :fast-safe)
281   (:generator 2
282     (inst mov sap
283           (make-ea :qword :base thread-base-tn :disp 0 :index n :scale 8))))
284
285 (define-vop (halt)
286   (:generator 1
287     (inst break halt-trap)))
288
289 (defknown float-wait () (values))
290 (define-vop (float-wait)
291   (:policy :fast-safe)
292   (:translate float-wait)
293   (:vop-var vop)
294   (:save-p :compute-only)
295   (:generator 1
296     (note-next-instruction vop :internal-error)
297     (inst wait)))
298 \f
299 ;;;; Miscellany
300
301 ;;; the RDTSC instruction (present on Pentium processors and
302 ;;; successors) allows you to access the time-stamp counter, a 64-bit
303 ;;; model-specific register that counts executed cycles. The
304 ;;; instruction returns the low cycle count in EAX and high cycle
305 ;;; count in EDX.
306 ;;;
307 ;;; In order to obtain more significant results on out-of-order
308 ;;; processors (such as the Pentium II and later), we issue a
309 ;;; serializing CPUID instruction before and after reading the cycle
310 ;;; counter. This instruction is used for its side effect of emptying
311 ;;; the processor pipeline, to ensure that the RDTSC instruction is
312 ;;; executed once all pending instructions have been completed and
313 ;;; before any others. CPUID writes to EBX and ECX in addition to EAX
314 ;;; and EDX, so they need to be added as temporaries.
315 ;;;
316 ;;; Note that cache effects mean that the cycle count can vary for
317 ;;; different executions of the same code (it counts cycles, not
318 ;;; retired instructions). Furthermore, the results are per-processor
319 ;;; and not per-process, so are unreliable on multiprocessor machines
320 ;;; where processes can migrate between processors.
321 ;;;
322 ;;; This method of obtaining a cycle count has the advantage of being
323 ;;; very fast (around 20 cycles), and of not requiring a system call.
324 ;;; However, you need to know your processor's clock speed to translate
325 ;;; this into real execution time.
326 ;;;
327 ;;; FIXME: This about the WITH-CYCLE-COUNTER interface a bit, and then
328 ;;; perhaps export it from SB-SYS.
329
330 (defknown %read-cycle-counter () (values (unsigned-byte 32) (unsigned-byte 32)) ())
331
332 (define-vop (%read-cycle-counter)
333   (:policy :fast-safe)
334   (:translate %read-cycle-counter)
335   (:temporary (:sc unsigned-reg :offset eax-offset :target lo) eax)
336   (:temporary (:sc unsigned-reg :offset edx-offset :target hi) edx)
337   (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
338   (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
339   (:ignore ebx ecx)
340   (:results (hi :scs (unsigned-reg))
341             (lo :scs (unsigned-reg)))
342   (:result-types unsigned-num unsigned-num)
343   (:generator 5
344      (zeroize eax)
345      ;; Intel docs seem quite consistent on only using CPUID before RDTSC,
346      ;; not both before and after. Go figure.
347      (inst cpuid)
348      (inst rdtsc)
349      (move lo eax)
350      (move hi edx)))
351
352 (defmacro with-cycle-counter (&body body)
353   "Returns the primary value of BODY as the primary value, and the
354 number of CPU cycles elapsed as secondary value. EXPERIMENTAL."
355   (with-unique-names (hi0 hi1 lo0 lo1)
356     `(multiple-value-bind (,hi0 ,lo0) (%read-cycle-counter)
357        (values (locally ,@body)
358                (multiple-value-bind (,hi1 ,lo1) (%read-cycle-counter)
359                  (+ (ash (- ,hi1 ,hi0) 32)
360                     (- ,lo1 ,lo0)))))))
361
362 #!+sb-dyncount
363 (define-vop (count-me)
364   (:args (count-vector :scs (descriptor-reg)))
365   (:info index)
366   (:generator 0
367     (inst inc (make-ea :qword :base count-vector
368                        :disp (- (* (+ vector-data-offset index) n-word-bytes)
369                                 other-pointer-lowtag)))))
370 \f
371 ;;;; Memory barrier support
372
373 #!+memory-barrier-vops
374 (define-vop (%compiler-barrier)
375   (:policy :fast-safe)
376   (:translate %compiler-barrier)
377   (:generator 3))
378
379 #!+memory-barrier-vops
380 (define-vop (%memory-barrier)
381   (:policy :fast-safe)
382   (:translate %memory-barrier)
383   (:generator 3
384     (inst mfence)))
385
386 #!+memory-barrier-vops
387 (define-vop (%read-barrier)
388   (:policy :fast-safe)
389   (:translate %read-barrier)
390   (:generator 3))
391
392 #!+memory-barrier-vops
393 (define-vop (%write-barrier)
394   (:policy :fast-safe)
395   (:translate %write-barrier)
396   (:generator 3))
397
398 #!+memory-barrier-vops
399 (define-vop (%data-dependency-barrier)
400   (:policy :fast-safe)
401   (:translate %data-dependency-barrier)
402   (:generator 3))
403
404 (define-vop (pause)
405   (:translate spin-loop-hint)
406   (:policy :fast-safe)
407   (:generator 0
408     (inst pause)))