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 eax-offset :to (:result 0)) eax)
32 (:results (result :scs (unsigned-reg)))
33 (:result-types positive-fixnum)
36 (inst and al-tn lowtag-mask)
37 (inst cmp al-tn other-pointer-lowtag)
38 (inst jmp :e other-ptr)
39 (inst cmp al-tn fun-pointer-lowtag)
40 (inst jmp :e function-ptr)
42 ;; Pick off structures and list pointers.
47 (inst and al-tn fixnum-tag-mask)
50 ;; must be an other immediate
55 (load-type al-tn object (- fun-pointer-lowtag))
59 (load-type al-tn object (- other-pointer-lowtag))
62 (inst movzx result al-tn)))
64 (define-vop (fun-subtype)
65 (:translate fun-subtype)
67 (:args (function :scs (descriptor-reg)))
68 (:temporary (:sc byte-reg :from (:eval 0) :to (:eval 1)) temp)
69 (:results (result :scs (unsigned-reg)))
70 (:result-types positive-fixnum)
72 (load-type temp function (- fun-pointer-lowtag))
73 (inst movzx result temp)))
75 (define-vop (set-fun-subtype)
76 (:translate (setf fun-subtype))
78 (:args (type :scs (unsigned-reg) :target eax)
79 (function :scs (descriptor-reg)))
80 (:arg-types positive-fixnum *)
81 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
82 :to (:result 0) :target result)
84 (:results (result :scs (unsigned-reg)))
85 (:result-types positive-fixnum)
88 (storeb al-tn function 0 fun-pointer-lowtag)
91 (define-vop (get-header-data)
92 (:translate get-header-data)
94 (:args (x :scs (descriptor-reg)))
95 (:results (res :scs (unsigned-reg)))
96 (:result-types positive-fixnum)
98 (loadw res x 0 other-pointer-lowtag)
99 (inst shr res n-widetag-bits)))
101 (define-vop (get-closure-length)
102 (:translate get-closure-length)
104 (:args (x :scs (descriptor-reg)))
105 (:results (res :scs (unsigned-reg)))
106 (:result-types positive-fixnum)
108 (loadw res x 0 fun-pointer-lowtag)
109 (inst shr res n-widetag-bits)))
111 (define-vop (set-header-data)
112 (:translate set-header-data)
114 (:args (x :scs (descriptor-reg) :target res :to (:result 0))
115 (data :scs (any-reg) :target eax))
116 (:arg-types * positive-fixnum)
117 (:results (res :scs (descriptor-reg)))
118 (:temporary (:sc unsigned-reg :offset eax-offset
119 :from (:argument 1) :to (:result 0)) eax)
122 (inst shl eax (- n-widetag-bits 2))
123 (inst mov al-tn (make-ea :byte :base x :disp (- other-pointer-lowtag)))
124 (storew eax x 0 other-pointer-lowtag)
127 (define-vop (pointer-hash)
128 (:translate pointer-hash)
129 (:args (ptr :scs (any-reg descriptor-reg) :target res))
130 (:results (res :scs (any-reg descriptor-reg)))
134 ;; Mask the lowtag, and shift the whole address into a positive
136 (inst and res (lognot lowtag-mask))
141 (define-vop (dynamic-space-free-pointer)
142 (:results (int :scs (sap-reg)))
143 (:result-types system-area-pointer)
144 (:translate dynamic-space-free-pointer)
147 (load-symbol-value int *allocation-pointer*)))
149 (define-vop (binding-stack-pointer-sap)
150 (:results (int :scs (sap-reg)))
151 (:result-types system-area-pointer)
152 (:translate binding-stack-pointer-sap)
155 (load-binding-stack-pointer int)))
157 (defknown (setf binding-stack-pointer-sap)
158 (system-area-pointer) system-area-pointer ())
160 (define-vop (set-binding-stack-pointer-sap)
161 (:args (new-value :scs (sap-reg) :target int))
162 (:arg-types system-area-pointer)
163 (:results (int :scs (sap-reg)))
164 (:result-types system-area-pointer)
165 (:translate (setf binding-stack-pointer-sap))
168 (store-binding-stack-pointer new-value)
169 (move int new-value)))
171 (define-vop (control-stack-pointer-sap)
172 (:results (int :scs (sap-reg)))
173 (:result-types system-area-pointer)
174 (:translate control-stack-pointer-sap)
179 ;;;; code object frobbing
181 (define-vop (code-instructions)
182 (:translate code-instructions)
184 (:args (code :scs (descriptor-reg) :to (:result 0)))
185 (:results (sap :scs (sap-reg) :from (:argument 0)))
186 (:result-types system-area-pointer)
188 (loadw sap code 0 other-pointer-lowtag)
189 (inst shr sap n-widetag-bits)
190 (inst lea sap (make-ea :byte :base code :index sap :scale 4
191 :disp (- other-pointer-lowtag)))))
193 (define-vop (compute-fun)
194 (:args (code :scs (descriptor-reg) :to (:result 0))
195 (offset :scs (signed-reg unsigned-reg) :to (:result 0)))
196 (:arg-types * positive-fixnum)
197 (:results (func :scs (descriptor-reg) :from (:argument 0)))
199 (loadw func code 0 other-pointer-lowtag)
200 (inst shr func n-widetag-bits)
202 (make-ea :byte :base offset :index func :scale 4
203 :disp (- fun-pointer-lowtag other-pointer-lowtag)))
204 (inst add func code)))
206 (define-vop (%simple-fun-self)
208 (:translate %simple-fun-self)
209 (:args (function :scs (descriptor-reg)))
210 (:results (result :scs (descriptor-reg)))
212 (loadw result function simple-fun-self-slot fun-pointer-lowtag)
214 (make-ea :byte :base result
215 :disp (- fun-pointer-lowtag
216 (* simple-fun-code-offset n-word-bytes))))))
218 ;;; The closure function slot is a pointer to raw code on X86 instead
219 ;;; of a pointer to the code function object itself. This VOP is used
220 ;;; to reference the function object given the closure object.
221 (define-source-transform %closure-fun (closure)
222 `(%simple-fun-self ,closure))
224 (define-vop (%set-fun-self)
226 (:translate (setf %simple-fun-self))
227 (:args (new-self :scs (descriptor-reg) :target result :to :result)
228 (function :scs (descriptor-reg) :to :result))
229 (:temporary (:sc any-reg :from (:argument 0) :to :result) temp)
230 (:results (result :scs (descriptor-reg)))
232 (inst lea temp (make-ea-for-object-slot new-self simple-fun-code-offset
234 (storew temp function simple-fun-self-slot fun-pointer-lowtag)
235 (move result new-self)))
237 ;;;; other miscellaneous VOPs
239 (defknown sb!unix::receive-pending-interrupt () (values))
240 (define-vop (sb!unix::receive-pending-interrupt)
242 (:translate sb!unix::receive-pending-interrupt)
244 (inst break pending-interrupt-trap)))
247 (define-vop (insert-safepoint)
249 (:translate sb!kernel::gc-safepoint)
254 (defknown current-thread-offset-sap ((unsigned-byte 32))
255 system-area-pointer (flushable))
258 (define-vop (current-thread-offset-sap)
259 (:results (sap :scs (sap-reg)))
260 (:result-types system-area-pointer)
261 (:translate current-thread-offset-sap)
262 (:args (n :scs (unsigned-reg)
263 #!+win32 #!+win32 :to :save
264 #!-win32 #!-win32 :target sap))
265 (:arg-types unsigned-num)
270 ;; Note that SAP conflicts with N in this case, hence the reader
271 ;; conditionals above.
272 (inst mov sap (make-ea :dword :disp +win32-tib-arbitrary-field-offset+) :fs)
273 (inst mov sap (make-ea :dword :base sap :disp 0 :index n :scale 4)))
275 (inst mov sap (make-ea :dword :disp 0 :index n :scale 4) :fs)))
279 (inst break halt-trap)))
281 (defknown float-wait () (values))
282 (define-vop (float-wait)
284 (:translate float-wait)
286 (:save-p :compute-only)
288 (note-next-instruction vop :internal-error)
293 ;;; the RDTSC instruction (present on Pentium processors and
294 ;;; successors) allows you to access the time-stamp counter, a 64-bit
295 ;;; model-specific register that counts executed cycles. The
296 ;;; instruction returns the low cycle count in EAX and high cycle
299 ;;; In order to obtain more significant results on out-of-order
300 ;;; processors (such as the Pentium II and later), we issue a
301 ;;; serializing CPUID instruction before reading the cycle counter.
302 ;;; This instruction is used for its side effect of emptying the
303 ;;; processor pipeline, to ensure that the RDTSC instruction is
304 ;;; executed once all pending instructions have been completed.
305 ;;; CPUID writes to EBX and ECX in addition to EAX and EDX, so
306 ;;; they need to be added as temporaries.
308 ;;; Note that cache effects mean that the cycle count can vary for
309 ;;; different executions of the same code (it counts cycles, not
310 ;;; retired instructions). Furthermore, the results are per-processor
311 ;;; and not per-process, so are unreliable on multiprocessor machines
312 ;;; where processes can migrate between processors.
314 ;;; This method of obtaining a cycle count has the advantage of being
315 ;;; very fast (around 20 cycles), and of not requiring a system call.
316 ;;; However, you need to know your processor's clock speed to translate
317 ;;; this into real execution time.
319 ;;; FIXME: This about the WITH-CYCLE-COUNTER interface a bit, and then
320 ;;; perhaps export it from SB-SYS.
322 (defknown %read-cycle-counter () (values (unsigned-byte 32) (unsigned-byte 32)) ())
324 (define-vop (%read-cycle-counter)
326 (:translate %read-cycle-counter)
327 (:temporary (:sc unsigned-reg :offset eax-offset :target lo) eax)
328 (:temporary (:sc unsigned-reg :offset edx-offset :target hi) edx)
329 (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
330 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
332 (:results (hi :scs (unsigned-reg))
333 (lo :scs (unsigned-reg)))
334 (:result-types unsigned-num unsigned-num)
337 ;; Intel docs seem quite consistent on only using CPUID before RDTSC,
338 ;; not both before and after. Go figure.
344 (defmacro with-cycle-counter (&body body)
345 "Returns the primary value of BODY as the primary value, and the
346 number of CPU cycles elapsed as secondary value. EXPERIMENTAL."
347 (with-unique-names (hi0 hi1 lo0 lo1)
348 `(multiple-value-bind (,hi0 ,lo0) (%read-cycle-counter)
349 (values (locally ,@body)
350 (multiple-value-bind (,hi1 ,lo1) (%read-cycle-counter)
351 (+ (ash (- ,hi1 ,hi0) 32)
355 (define-vop (count-me)
356 (:args (count-vector :scs (descriptor-reg)))
359 (inst inc (make-ea-for-vector-data count-vector :offset index))))
361 ;;;; Memory barrier support
363 #!+memory-barrier-vops
364 (define-vop (%compiler-barrier)
366 (:translate %compiler-barrier)
369 #!+memory-barrier-vops
370 (define-vop (%memory-barrier)
372 (:translate %memory-barrier)
374 (inst add (make-ea :dword :base esp-tn) 0 :lock)))
376 #!+memory-barrier-vops
377 (define-vop (%read-barrier)
379 (:translate %read-barrier)
382 #!+memory-barrier-vops
383 (define-vop (%write-barrier)
385 (:translate %write-barrier)
388 #!+memory-barrier-vops
389 (define-vop (%data-dependency-barrier)
391 (:translate %data-dependency-barrier)
395 (:translate spin-loop-hint)