ae57c4c1f37e2e762b5a349663f7de1fbf2bb029
[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-thread
263 (defknown current-thread-offset-sap ((unsigned-byte 64))
264   system-area-pointer (flushable))
265
266 #!+sb-thread
267 (define-vop (current-thread-offset-sap)
268   (:results (sap :scs (sap-reg)))
269   (:result-types system-area-pointer)
270   (:translate current-thread-offset-sap)
271   (:args (n :scs (unsigned-reg) :target sap))
272   (:arg-types unsigned-num)
273   (:policy :fast-safe)
274   (:generator 2
275     (inst mov sap
276           (make-ea :qword :base thread-base-tn :disp 0 :index n :scale 8))))
277
278 (define-vop (halt)
279   (:generator 1
280     (inst break halt-trap)))
281
282 (defknown float-wait () (values))
283 (define-vop (float-wait)
284   (:policy :fast-safe)
285   (:translate float-wait)
286   (:vop-var vop)
287   (:save-p :compute-only)
288   (:generator 1
289     (note-next-instruction vop :internal-error)
290     (inst wait)))
291 \f
292 ;;;; Miscellany
293
294 ;;; the RDTSC instruction (present on Pentium processors and
295 ;;; successors) allows you to access the time-stamp counter, a 64-bit
296 ;;; model-specific register that counts executed cycles. The
297 ;;; instruction returns the low cycle count in EAX and high cycle
298 ;;; count in EDX.
299 ;;;
300 ;;; In order to obtain more significant results on out-of-order
301 ;;; processors (such as the Pentium II and later), we issue a
302 ;;; serializing CPUID instruction before and after reading the cycle
303 ;;; counter. This instruction is used for its side effect of emptying
304 ;;; the processor pipeline, to ensure that the RDTSC instruction is
305 ;;; executed once all pending instructions have been completed and
306 ;;; before any others. CPUID writes to EBX and ECX in addition to EAX
307 ;;; and EDX, so they need to be added as temporaries.
308 ;;;
309 ;;; Note that cache effects mean that the cycle count can vary for
310 ;;; different executions of the same code (it counts cycles, not
311 ;;; retired instructions). Furthermore, the results are per-processor
312 ;;; and not per-process, so are unreliable on multiprocessor machines
313 ;;; where processes can migrate between processors.
314 ;;;
315 ;;; This method of obtaining a cycle count has the advantage of being
316 ;;; very fast (around 20 cycles), and of not requiring a system call.
317 ;;; However, you need to know your processor's clock speed to translate
318 ;;; this into real execution time.
319 ;;;
320 ;;; FIXME: This about the WITH-CYCLE-COUNTER interface a bit, and then
321 ;;; perhaps export it from SB-SYS.
322
323 (defknown %read-cycle-counter () (values (unsigned-byte 32) (unsigned-byte 32)) ())
324
325 (define-vop (%read-cycle-counter)
326   (:policy :fast-safe)
327   (:translate %read-cycle-counter)
328   (:temporary (:sc unsigned-reg :offset eax-offset :target lo) eax)
329   (:temporary (:sc unsigned-reg :offset edx-offset :target hi) edx)
330   (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
331   (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
332   (:ignore ebx ecx)
333   (:results (hi :scs (unsigned-reg))
334             (lo :scs (unsigned-reg)))
335   (:result-types unsigned-num unsigned-num)
336   (:generator 5
337      (zeroize eax)
338      ;; Intel docs seem quite consistent on only using CPUID before RDTSC,
339      ;; not both before and after. Go figure.
340      (inst cpuid)
341      (inst rdtsc)
342      (move lo eax)
343      (move hi edx)))
344
345 (defmacro with-cycle-counter (&body body)
346   "Returns the primary value of BODY as the primary value, and the
347 number of CPU cycles elapsed as secondary value. EXPERIMENTAL."
348   (with-unique-names (hi0 hi1 lo0 lo1)
349     `(multiple-value-bind (,hi0 ,lo0) (%read-cycle-counter)
350        (values (locally ,@body)
351                (multiple-value-bind (,hi1 ,lo1) (%read-cycle-counter)
352                  (+ (ash (- ,hi1 ,hi0) 32)
353                     (- ,lo1 ,lo0)))))))
354
355 #!+sb-dyncount
356 (define-vop (count-me)
357   (:args (count-vector :scs (descriptor-reg)))
358   (:info index)
359   (:generator 0
360     (inst inc (make-ea :qword :base count-vector
361                        :disp (- (* (+ vector-data-offset index) n-word-bytes)
362                                 other-pointer-lowtag)))))
363 \f
364 ;;;; Memory barrier support
365
366 #!+memory-barrier-vops
367 (define-vop (%compiler-barrier)
368   (:policy :fast-safe)
369   (:translate %compiler-barrier)
370   (:generator 3))
371
372 #!+memory-barrier-vops
373 (define-vop (%memory-barrier)
374   (:policy :fast-safe)
375   (:translate %memory-barrier)
376   (:generator 3
377     (inst mfence)))
378
379 #!+memory-barrier-vops
380 (define-vop (%read-barrier)
381   (:policy :fast-safe)
382   (:translate %read-barrier)
383   (:generator 3))
384
385 #!+memory-barrier-vops
386 (define-vop (%write-barrier)
387   (:policy :fast-safe)
388   (:translate %write-barrier)
389   (:generator 3))
390
391 #!+memory-barrier-vops
392 (define-vop (%data-dependency-barrier)
393   (:policy :fast-safe)
394   (:translate %data-dependency-barrier)
395   (:generator 3))
396
397 (define-vop (pause)
398   (:translate spin-loop-hint)
399   (:policy :fast-safe)
400   (:generator 0
401     (inst pause)))