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))
142 (define-vop (dynamic-space-free-pointer)
143 (:results (int :scs (sap-reg)))
144 (:result-types system-area-pointer)
145 (:translate dynamic-space-free-pointer)
148 (load-symbol-value int *allocation-pointer*)))
150 (define-vop (binding-stack-pointer-sap)
151 (:results (int :scs (sap-reg)))
152 (:result-types system-area-pointer)
153 (:translate binding-stack-pointer-sap)
156 (load-binding-stack-pointer int)))
158 (defknown (setf binding-stack-pointer-sap)
159 (system-area-pointer) system-area-pointer ())
161 (define-vop (set-binding-stack-pointer-sap)
162 (:args (new-value :scs (sap-reg) :target int))
163 (:arg-types system-area-pointer)
164 (:results (int :scs (sap-reg)))
165 (:result-types system-area-pointer)
166 (:translate (setf binding-stack-pointer-sap))
169 (store-binding-stack-pointer new-value)
170 (move int new-value)))
172 (define-vop (control-stack-pointer-sap)
173 (:results (int :scs (sap-reg)))
174 (:result-types system-area-pointer)
175 (:translate control-stack-pointer-sap)
180 ;;;; code object frobbing
182 (define-vop (code-instructions)
183 (:translate code-instructions)
185 (:args (code :scs (descriptor-reg) :to (:result 0)))
186 (:results (sap :scs (sap-reg) :from (:argument 0)))
187 (:result-types system-area-pointer)
189 (loadw sap code 0 other-pointer-lowtag)
190 (inst shr sap n-widetag-bits)
191 (inst lea sap (make-ea :byte :base code :index sap
193 :disp (- other-pointer-lowtag)))))
195 (define-vop (compute-fun)
196 (:args (code :scs (descriptor-reg) :to (:result 0))
197 (offset :scs (signed-reg unsigned-reg) :to (:result 0)))
198 (:arg-types * positive-fixnum)
199 (:results (func :scs (descriptor-reg) :from (:argument 0)))
201 (loadw func code 0 other-pointer-lowtag)
202 (inst shr func n-widetag-bits)
204 (make-ea :byte :base offset :index func
206 :disp (- fun-pointer-lowtag other-pointer-lowtag)))
207 (inst add func code)))
209 (define-vop (%simple-fun-self)
211 (:translate %simple-fun-self)
212 (:args (function :scs (descriptor-reg)))
213 (:results (result :scs (descriptor-reg)))
215 (loadw result function simple-fun-self-slot fun-pointer-lowtag)
217 (make-ea :byte :base result
218 :disp (- fun-pointer-lowtag
219 (* simple-fun-code-offset n-word-bytes))))))
221 ;;; The closure function slot is a pointer to raw code on X86 instead
222 ;;; of a pointer to the code function object itself. This VOP is used
223 ;;; to reference the function object given the closure object.
224 (define-source-transform %closure-fun (closure)
225 `(%simple-fun-self ,closure))
227 (define-vop (%set-fun-self)
229 (:translate (setf %simple-fun-self))
230 (:args (new-self :scs (descriptor-reg) :target result :to :result)
231 (function :scs (descriptor-reg) :to :result))
232 (:temporary (:sc any-reg :from (:argument 0) :to :result) temp)
233 (:results (result :scs (descriptor-reg)))
236 (make-ea :byte :base new-self
237 :disp (- (ash simple-fun-code-offset word-shift)
238 fun-pointer-lowtag)))
239 (storew temp function simple-fun-self-slot fun-pointer-lowtag)
240 (move result new-self)))
242 ;;;; other miscellaneous VOPs
244 (defknown sb!unix::receive-pending-interrupt () (values))
245 (define-vop (sb!unix::receive-pending-interrupt)
247 (:translate sb!unix::receive-pending-interrupt)
249 (inst break pending-interrupt-trap)))
252 (define-vop (insert-safepoint)
254 (:translate sb!kernel::gc-safepoint)
259 (defknown current-thread-offset-sap ((unsigned-byte 64))
260 system-area-pointer (flushable))
263 (define-vop (current-thread-offset-sap)
264 (:results (sap :scs (sap-reg)))
265 (:result-types system-area-pointer)
266 (:translate current-thread-offset-sap)
267 (:args (n :scs (unsigned-reg) :target sap))
268 (:arg-types unsigned-num)
272 (make-ea :qword :base thread-base-tn :disp 0 :index n :scale 8))))
276 (inst break halt-trap)))
278 (defknown float-wait () (values))
279 (define-vop (float-wait)
281 (:translate float-wait)
283 (:save-p :compute-only)
285 (note-next-instruction vop :internal-error)
290 ;;; the RDTSC instruction (present on Pentium processors and
291 ;;; successors) allows you to access the time-stamp counter, a 64-bit
292 ;;; model-specific register that counts executed cycles. The
293 ;;; instruction returns the low cycle count in EAX and high cycle
296 ;;; In order to obtain more significant results on out-of-order
297 ;;; processors (such as the Pentium II and later), we issue a
298 ;;; serializing CPUID instruction before and after reading the cycle
299 ;;; counter. This instruction is used for its side effect of emptying
300 ;;; the processor pipeline, to ensure that the RDTSC instruction is
301 ;;; executed once all pending instructions have been completed and
302 ;;; before any others. CPUID writes to EBX and ECX in addition to EAX
303 ;;; and EDX, so they need to be added as temporaries.
305 ;;; Note that cache effects mean that the cycle count can vary for
306 ;;; different executions of the same code (it counts cycles, not
307 ;;; retired instructions). Furthermore, the results are per-processor
308 ;;; and not per-process, so are unreliable on multiprocessor machines
309 ;;; where processes can migrate between processors.
311 ;;; This method of obtaining a cycle count has the advantage of being
312 ;;; very fast (around 20 cycles), and of not requiring a system call.
313 ;;; However, you need to know your processor's clock speed to translate
314 ;;; this into real execution time.
316 ;;; FIXME: This about the WITH-CYCLE-COUNTER interface a bit, and then
317 ;;; perhaps export it from SB-SYS.
319 (defknown %read-cycle-counter () (values (unsigned-byte 32) (unsigned-byte 32)) ())
321 (define-vop (%read-cycle-counter)
323 (:translate %read-cycle-counter)
324 (:temporary (:sc unsigned-reg :offset eax-offset :target lo) eax)
325 (:temporary (:sc unsigned-reg :offset edx-offset :target hi) edx)
326 (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
327 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
329 (:results (hi :scs (unsigned-reg))
330 (lo :scs (unsigned-reg)))
331 (:result-types unsigned-num unsigned-num)
334 ;; Intel docs seem quite consistent on only using CPUID before RDTSC,
335 ;; not both before and after. Go figure.
341 (defmacro with-cycle-counter (&body body)
342 "Returns the primary value of BODY as the primary value, and the
343 number of CPU cycles elapsed as secondary value. EXPERIMENTAL."
344 (with-unique-names (hi0 hi1 lo0 lo1)
345 `(multiple-value-bind (,hi0 ,lo0) (%read-cycle-counter)
346 (values (locally ,@body)
347 (multiple-value-bind (,hi1 ,lo1) (%read-cycle-counter)
348 (+ (ash (- ,hi1 ,hi0) 32)
352 (define-vop (count-me)
353 (:args (count-vector :scs (descriptor-reg)))
356 (inst inc (make-ea :qword :base count-vector
357 :disp (- (* (+ vector-data-offset index) n-word-bytes)
358 other-pointer-lowtag)))))
360 ;;;; Memory barrier support
362 #!+memory-barrier-vops
363 (define-vop (%compiler-barrier)
365 (:translate %compiler-barrier)
368 #!+memory-barrier-vops
369 (define-vop (%memory-barrier)
371 (:translate %memory-barrier)
375 #!+memory-barrier-vops
376 (define-vop (%read-barrier)
378 (:translate %read-barrier)
381 #!+memory-barrier-vops
382 (define-vop (%write-barrier)
384 (:translate %write-barrier)
387 #!+memory-barrier-vops
388 (define-vop (%data-dependency-barrier)
390 (:translate %data-dependency-barrier)
394 (:translate spin-loop-hint)