1 ;;;; x86 VM definitions of various system hacking operations
3 ;;;; This software is part of the SBCL system. See the README file for
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.
14 ;;;; type frobbing VOPs
16 (define-vop (lowtag-of)
17 (:translate lowtag-of)
19 (:args (object :scs (any-reg descriptor-reg control-stack)
21 (:results (result :scs (unsigned-reg)))
22 (:result-types positive-fixnum)
25 (inst and result lowtag-mask)))
27 (define-vop (widetag-of)
28 (:translate widetag-of)
30 (:args (object :scs (descriptor-reg)))
31 (:temporary (:sc unsigned-reg :offset rax-offset :target result
33 (:results (result :scs (unsigned-reg)))
34 (:result-types positive-fixnum)
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)
44 (inst test al-tn fixnum-tag-mask)
47 ;; Pick off structures and list pointers.
51 ;; must be an other immediate
52 (inst movzx rax (reg-in-size object :byte))
56 (load-type rax object (- fun-pointer-lowtag))
60 (load-type rax object (- other-pointer-lowtag))
65 (define-vop (fun-subtype)
66 (:translate fun-subtype)
68 (:args (function :scs (descriptor-reg)))
69 (:results (result :scs (unsigned-reg)))
70 (:result-types positive-fixnum)
72 (load-type result function (- fun-pointer-lowtag))))
74 (define-vop (set-fun-subtype)
75 (:translate (setf fun-subtype))
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)
83 (:results (result :scs (unsigned-reg)))
84 (:result-types positive-fixnum)
88 (make-ea :byte :base function :disp (- fun-pointer-lowtag))
92 (define-vop (get-header-data)
93 (:translate get-header-data)
95 (:args (x :scs (descriptor-reg)))
96 (:results (res :scs (unsigned-reg)))
97 (:result-types positive-fixnum)
99 (loadw res x 0 other-pointer-lowtag)
100 (inst shr res n-widetag-bits)))
102 (define-vop (get-closure-length)
103 (:translate get-closure-length)
105 (:args (x :scs (descriptor-reg)))
106 (:results (res :scs (unsigned-reg)))
107 (:result-types positive-fixnum)
109 (loadw res x 0 fun-pointer-lowtag)
110 (inst shr res n-widetag-bits)))
112 (define-vop (set-header-data)
113 (:translate set-header-data)
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)
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)
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)))
135 ;; Mask the lowtag, and shift the whole address into a positive
137 (inst and res (lognot lowtag-mask))
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)))
146 (inst shl res (- n-widetag-bits n-fixnum-tag-bits))
147 (inst or res (sc-case type
149 (immediate (tn-value type))))))
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)
159 (load-symbol-value int *allocation-pointer*)))
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)
167 (load-binding-stack-pointer int)))
169 (defknown (setf binding-stack-pointer-sap)
170 (system-area-pointer) system-area-pointer ())
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))
180 (store-binding-stack-pointer new-value)
181 (move int new-value)))
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)
191 ;;;; code object frobbing
193 (define-vop (code-instructions)
194 (:translate code-instructions)
196 (:args (code :scs (descriptor-reg) :to (:result 0)))
197 (:results (sap :scs (sap-reg) :from (:argument 0)))
198 (:result-types system-area-pointer)
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
204 :disp (- other-pointer-lowtag)))))
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)))
212 (loadw func code 0 other-pointer-lowtag)
213 (inst shr func n-widetag-bits)
215 (make-ea :byte :base offset :index func
217 :disp (- fun-pointer-lowtag other-pointer-lowtag)))
218 (inst add func code)))
220 (define-vop (%simple-fun-self)
222 (:translate %simple-fun-self)
223 (:args (function :scs (descriptor-reg)))
224 (:results (result :scs (descriptor-reg)))
226 (loadw result function simple-fun-self-slot fun-pointer-lowtag)
228 (make-ea :byte :base result
229 :disp (- fun-pointer-lowtag
230 (* simple-fun-code-offset n-word-bytes))))))
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))
238 (define-vop (%set-fun-self)
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)))
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)))
253 ;;;; other miscellaneous VOPs
255 (defknown sb!unix::receive-pending-interrupt () (values))
256 (define-vop (sb!unix::receive-pending-interrupt)
258 (:translate sb!unix::receive-pending-interrupt)
260 (inst break pending-interrupt-trap)))
263 (define-vop (insert-safepoint)
265 (:translate sb!kernel::gc-safepoint)
270 (defknown current-thread-offset-sap ((unsigned-byte 64))
271 system-area-pointer (flushable))
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)
283 (make-ea :qword :base thread-base-tn :disp 0 :index n :scale 8))))
287 (inst break halt-trap)))
289 (defknown float-wait () (values))
290 (define-vop (float-wait)
292 (:translate float-wait)
294 (:save-p :compute-only)
296 (note-next-instruction vop :internal-error)
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
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.
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.
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.
327 ;;; FIXME: This about the WITH-CYCLE-COUNTER interface a bit, and then
328 ;;; perhaps export it from SB-SYS.
330 (defknown %read-cycle-counter () (values (unsigned-byte 32) (unsigned-byte 32)) ())
332 (define-vop (%read-cycle-counter)
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)
340 (:results (hi :scs (unsigned-reg))
341 (lo :scs (unsigned-reg)))
342 (:result-types unsigned-num unsigned-num)
345 ;; Intel docs seem quite consistent on only using CPUID before RDTSC,
346 ;; not both before and after. Go figure.
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)
363 (define-vop (count-me)
364 (:args (count-vector :scs (descriptor-reg)))
367 (inst inc (make-ea :qword :base count-vector
368 :disp (- (* (+ vector-data-offset index) n-word-bytes)
369 other-pointer-lowtag)))))
371 ;;;; Memory barrier support
373 #!+memory-barrier-vops
374 (define-vop (%compiler-barrier)
376 (:translate %compiler-barrier)
379 #!+memory-barrier-vops
380 (define-vop (%memory-barrier)
382 (:translate %memory-barrier)
386 #!+memory-barrier-vops
387 (define-vop (%read-barrier)
389 (:translate %read-barrier)
392 #!+memory-barrier-vops
393 (define-vop (%write-barrier)
395 (:translate %write-barrier)
398 #!+memory-barrier-vops
399 (define-vop (%data-dependency-barrier)
401 (:translate %data-dependency-barrier)
405 (:translate spin-loop-hint)