9c1e08cc733b917352ffbaeee39f5c2a05118120
[sbcl.git] / src / code / x86-vm.lisp
1 ;;;; X86-specific runtime stuff
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 ;;;; OS-CONTEXT-T
15
16 ;;; a POSIX signal context, i.e. the type passed as the third 
17 ;;; argument to an SA_SIGACTION-style signal handler
18 ;;;
19 ;;; The real type does have slots, but at Lisp level, we never
20 ;;; access them, or care about the size of the object. Instead, we
21 ;;; always refer to these objects by pointers handed to us by the C
22 ;;; runtime library, and ask the runtime library any time we need
23 ;;; information about the contents of one of these objects. Thus, it
24 ;;; works to represent this as an object with no slots.
25 ;;;
26 ;;; KLUDGE: It would be nice to have a type definition analogous to
27 ;;; C's "struct os_context_t;", for an incompletely specified object
28 ;;; which can only be referred to by reference, but I don't know how
29 ;;; to do that in the FFI, so instead we just this bogus no-slots
30 ;;; representation. -- WHN 20000730
31 ;;;
32 ;;; FIXME: Since SBCL, unlike CMU CL, uses this as an opaque type,
33 ;;; it's no longer architecture-dependent, and probably belongs in
34 ;;; some other package, perhaps SB-KERNEL.
35 (def-alien-type os-context-t (struct os-context-t-struct))
36 \f
37 ;;;; MACHINE-TYPE and MACHINE-VERSION
38
39 (defun machine-type ()
40   #!+sb-doc
41   "Returns a string describing the type of the local machine."
42   "X86")
43
44 (defun machine-version ()
45   #!+sb-doc
46   "Returns a string describing the version of the local machine."
47   "X86")
48 \f
49 ;;;; :CODE-OBJECT fixups
50
51 ;;; a counter to measure the storage overhead of these fixups
52 (defvar *num-fixups* 0)
53 ;;; FIXME: When the system runs, it'd be interesting to see what this is.
54
55 ;;; This gets called by LOAD to resolve newly positioned objects
56 ;;; with things (like code instructions) that have to refer to them.
57 ;;;
58 ;;; Add a fixup offset to the vector of fixup offsets for the given
59 ;;; code object.
60 (defun fixup-code-object (code offset fixup kind)
61   (declare (type index offset))
62   (flet ((add-fixup (code offset)
63            ;; Although this could check for and ignore fixups for code
64            ;; objects in the read-only and static spaces, this should
65            ;; only be the case when *enable-dynamic-space-code* is
66            ;; True.
67            (when sb!impl::*enable-dynamic-space-code*
68              (incf *num-fixups*)
69              (let ((fixups (code-header-ref code code-constants-offset)))
70                (cond ((typep fixups '(simple-array (unsigned-byte 32) (*)))
71                       (let ((new-fixups
72                              (adjust-array fixups (1+ (length fixups))
73                                            :element-type '(unsigned-byte 32))))
74                         (setf (aref new-fixups (length fixups)) offset)
75                         (setf (code-header-ref code code-constants-offset)
76                               new-fixups)))
77                      (t
78                       (unless (or (eq (get-type fixups)
79                                       sb!vm:unbound-marker-type)
80                                   (zerop fixups))
81                         (format t "** Init. code FU = ~S~%" fixups)) ; FIXME
82                       (setf (code-header-ref code code-constants-offset)
83                             (make-specializable-array
84                              1
85                              :element-type '(unsigned-byte 32)
86                              :initial-element offset))))))))
87     (sb!sys:without-gcing
88      (let* ((sap (truly-the system-area-pointer
89                             (sb!kernel:code-instructions code)))
90             (obj-start-addr (logand (sb!kernel:get-lisp-obj-address code)
91                                     #xfffffff8))
92             #+nil (const-start-addr (+ obj-start-addr (* 5 4)))
93             (code-start-addr (sb!sys:sap-int (sb!kernel:code-instructions
94                                               code)))
95             (ncode-words (sb!kernel:code-header-ref code 1))
96             (code-end-addr (+ code-start-addr (* ncode-words 4))))
97        (unless (member kind '(:absolute :relative))
98          (error "Unknown code-object-fixup kind ~S." kind))
99        (ecase kind
100          (:absolute
101           ;; Word at sap + offset contains a value to be replaced by
102           ;; adding that value to fixup.
103           (setf (sap-ref-32 sap offset) (+ fixup (sap-ref-32 sap offset)))
104           ;; Record absolute fixups that point within the code object.
105           (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
106             (add-fixup code offset)))
107          (:relative
108           ;; Fixup is the actual address wanted.
109           ;;
110           ;; Record relative fixups that point outside the code
111           ;; object.
112           (when (or (< fixup obj-start-addr) (> fixup code-end-addr))
113             (add-fixup code offset))
114           ;; Replace word with value to add to that loc to get there.
115           (let* ((loc-sap (+ (sap-int sap) offset))
116                  (rel-val (- fixup loc-sap 4)))
117             (declare (type (unsigned-byte 32) loc-sap)
118                      (type (signed-byte 32) rel-val))
119             (setf (signed-sap-ref-32 sap offset) rel-val))))))
120     nil))
121
122 ;;; Add a code fixup to a code object generated by GENESIS. The fixup has
123 ;;; already been applied, it's just a matter of placing the fixup in the code's
124 ;;; fixup vector if necessary.
125 ;;;
126 ;;; KLUDGE: I'd like a good explanation of why this has to be done at
127 ;;; load time instead of in GENESIS. It's probably simple, I just haven't
128 ;;; figured it out, or found it written down anywhere. -- WHN 19990908
129 #!+gencgc
130 (defun !do-load-time-code-fixup (code offset fixup kind)
131   (flet ((add-load-time-code-fixup (code offset)
132            (let ((fixups (code-header-ref code sb!vm:code-constants-offset)))
133              (cond ((typep fixups '(simple-array (unsigned-byte 32) (*)))
134                     (let ((new-fixups
135                            (adjust-array fixups (1+ (length fixups))
136                                          :element-type '(unsigned-byte 32))))
137                       (setf (aref new-fixups (length fixups)) offset)
138                       (setf (code-header-ref code sb!vm:code-constants-offset)
139                             new-fixups)))
140                    (t
141                     (unless (or (eq (get-type fixups)
142                                     sb!vm:unbound-marker-type)
143                                 (zerop fixups))
144                       (sb!impl::!cold-lose "Argh! can't process fixup"))
145                     (setf (code-header-ref code sb!vm:code-constants-offset)
146                           (make-specializable-array
147                            1
148                            :element-type '(unsigned-byte 32)
149                            :initial-element offset)))))))
150     (let* ((sap (truly-the system-area-pointer
151                            (sb!kernel:code-instructions code)))
152            (obj-start-addr
153             ;; FIXME: looks like (LOGANDC2 foo typebits)
154             (logand (sb!kernel:get-lisp-obj-address code) #xfffffff8))
155            (code-start-addr (sb!sys:sap-int (sb!kernel:code-instructions
156                                              code)))
157            (ncode-words (sb!kernel:code-header-ref code 1))
158          (code-end-addr (+ code-start-addr (* ncode-words 4))))
159       (ecase kind
160         (:absolute
161          ;; Record absolute fixups that point within the code object.
162          (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
163            (add-load-time-code-fixup code offset)))
164         (:relative
165          ;; Record relative fixups that point outside the code object.
166          (when (or (< fixup obj-start-addr) (> fixup code-end-addr))
167            (add-load-time-code-fixup code offset)))))))
168 \f
169 ;;;; low-level signal context access functions
170 ;;;;
171 ;;;; Note: In CMU CL, similar functions were hardwired to access
172 ;;;; BSD-style sigcontext structures defined as alien objects. Our
173 ;;;; approach is different in two ways:
174 ;;;;   1. We use POSIX SA_SIGACTION-style signals, so our context is
175 ;;;;      whatever the void pointer in the sigaction handler dereferences
176 ;;;;      to, not necessarily a sigcontext.
177 ;;;;   2. We don't try to maintain alien definitions of the context
178 ;;;;      structure at Lisp level, but instead call alien C functions
179 ;;;;      which take care of access for us. (Since the C functions can
180 ;;;;      be defined in terms of system standard header files, they
181 ;;;;      should be easier to maintain; and since Lisp code uses signal
182 ;;;;      contexts only in interactive or exception code (like the debugger
183 ;;;;      and internal error handling) the extra runtime cost should be
184 ;;;;      negligible.
185
186 (def-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-int)
187   ;; (Note: Just as in CONTEXT-REGISTER-ADDR, we intentionally use an
188   ;; 'unsigned *' interpretation for the 32-bit word passed to us by
189   ;; the C code, even though the C code may think it's an 'int *'.)
190   (context (* os-context-t)))
191
192 (defun context-pc (context)
193   (declare (type (alien (* os-context-t)) context))
194   (int-sap (deref (context-pc-addr context))))
195
196 (def-alien-routine ("os_context_register_addr" context-register-addr)
197   (* unsigned-int)
198   ;; (Note the mismatch here between the 'int *' value that the C code
199   ;; may think it's giving us and the 'unsigned *' value that we
200   ;; receive. It's intentional: the C header files may think of
201   ;; register values as signed, but the CMU CL code tends to think of
202   ;; register values as unsigned, and might get bewildered if we ask
203   ;; it to work with signed values.)
204   (context (* os-context-t))
205   (index int))
206
207 ;;; FIXME: Should this and CONTEXT-PC be INLINE to reduce consing?
208 ;;; (Are they used in anything time-critical, or just the debugger?)
209 (defun context-register (context index)
210   (declare (type (alien (* os-context-t)) context))
211   (deref (context-register-addr context index)))
212
213 (defun %set-context-register (context index new)
214 (declare (type (alien (* os-context-t)) context))
215 (setf (deref (context-register-addr context index))
216       new))
217
218 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
219 ;;; register. FORMAT is the type of float to return.
220 ;;;
221 ;;; As of sbcl-0.6.7, there is no working code which calls this code,
222 ;;; so it's stubbed out. Someday, in order to make the debugger work
223 ;;; better, it may be necessary to unstubify it.
224 (defun context-float-register (context index format)
225   (declare (ignore context index))
226   (warn "stub CONTEXT-FLOAT-REGISTER")
227   (coerce 0.0 format))
228 (defun %set-context-float-register (context index format new-value)
229   (declare (ignore context index))
230   (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
231   (coerce new-value format))
232
233 ;;; Given a signal context, return the floating point modes word in
234 ;;; the same format as returned by FLOATING-POINT-MODES.
235 (defun context-floating-point-modes (context)
236   ;; FIXME: As of sbcl-0.6.7 and the big rewrite of signal handling for
237   ;; POSIXness and (at the Lisp level) opaque signal contexts,
238   ;; this is stubified. It needs to be rewritten as an
239   ;; alien function.
240   (declare (ignore context)) ; stub!
241   (warn "stub CONTEXT-FLOATING-POINT-MODES")
242
243   ;; old code for Linux:
244   #+nil
245   (let ((cw (slot (deref (slot context 'fpstate) 0) 'cw))
246         (sw (slot (deref (slot context 'fpstate) 0) 'sw)))
247     ;;(format t "cw = ~4X~%sw = ~4X~%" cw sw)
248     ;; NOT TESTED -- Clear sticky bits to clear interrupt condition.
249     (setf (slot (deref (slot context 'fpstate) 0) 'sw) (logandc2 sw #x3f))
250     ;;(format t "new sw = ~X~%" (slot (deref (slot context 'fpstate) 0) 'sw))
251     ;; Simulate floating-point-modes VOP.
252     (logior (ash (logand sw #xffff) 16) (logxor (logand cw #xffff) #x3f)))
253
254   0)
255 \f
256 ;;;; INTERNAL-ERROR-ARGUMENTS
257
258 ;;; Given a (POSIX) signal context, extract the internal error
259 ;;; arguments from the instruction stream.
260 (defun internal-error-arguments (context)
261   (declare (type (alien (* os-context-t)) context))
262   (/show0 "entering INTERNAL-ERROR-ARGUMENTS, CONTEXT=..")
263   (/hexstr context)
264   (let ((pc (context-pc context)))
265     (declare (type system-area-pointer pc))
266     ;; using INT3 the pc is .. INT3 <here> code length bytes...
267     (let* ((length (sap-ref-8 pc 1))
268            (vector (make-array length :element-type '(unsigned-byte 8))))
269       (declare (type (unsigned-byte 8) length)
270                (type (simple-array (unsigned-byte 8) (*)) vector))
271       (/show0 "LENGTH,VECTOR,ERROR-NUMBER=..")
272       (/hexstr length)
273       (/hexstr vector)
274       (copy-from-system-area pc (* sb!vm:byte-bits 2)
275                              vector (* sb!vm:word-bits
276                                        sb!vm:vector-data-offset)
277                              (* length sb!vm:byte-bits))
278       (let* ((index 0)
279              (error-number (sb!c::read-var-integer vector index)))
280         (/hexstr error-number)
281         (collect ((sc-offsets))
282           (loop
283            (/show0 "INDEX=..")
284            (/hexstr index)
285            (when (>= index length)
286              (return))
287            (let ((sc-offset (sb!c::read-var-integer vector index)))
288              (/show0 "SC-OFFSET=..")
289              (/hexstr sc-offset)
290              (sc-offsets sc-offset)))
291           (values error-number (sc-offsets)))))))
292 \f
293 ;;; Do whatever is necessary to make the given code component
294 ;;; executable. (This is a no-op on the x86.)
295 (defun sanctify-for-execution (component)
296   (declare (ignore component))
297   nil)
298
299 ;;; This is used in error.lisp to insure that floating-point exceptions
300 ;;; are properly trapped. The compiler translates this to a VOP.
301 (defun float-wait ()
302   (float-wait))
303
304 ;;; float constants
305 ;;;
306 ;;; These are used by the FP MOVE-FROM-{SINGLE|DOUBLE} VOPs rather
307 ;;; than the i387 load constant instructions to avoid consing in some
308 ;;; cases. Note these are initialized by GENESIS as they are needed
309 ;;; early.
310 (defvar *fp-constant-0s0*)
311 (defvar *fp-constant-1s0*)
312 (defvar *fp-constant-0d0*)
313 (defvar *fp-constant-1d0*)
314 ;;; The long-float constants.
315 (defvar *fp-constant-0l0*)
316 (defvar *fp-constant-1l0*)
317 (defvar *fp-constant-pi*)
318 (defvar *fp-constant-l2t*)
319 (defvar *fp-constant-l2e*)
320 (defvar *fp-constant-lg2*)
321 (defvar *fp-constant-ln2*)
322
323 ;;; Enable/disable scavenging of the read-only space.
324 (defvar *scavenge-read-only-space* nil)
325 ;;; FIXME: should be *SCAVENGE-READ-ONLY-SPACE-P*
326
327 ;;; The current alien stack pointer; saved/restored for non-local exits.
328 (defvar *alien-stack*)
329
330 (defun sb!kernel::%instance-set-conditional (object slot test-value new-value)
331   (declare (type instance object)
332            (type index slot))
333   #!+sb-doc
334   "Atomically compare object's slot value to test-value and if EQ store
335    new-value in the slot. The original value of the slot is returned."
336   (sb!kernel::%instance-set-conditional object slot test-value new-value))
337
338 ;;; Support for the MT19937 random number generator. The update
339 ;;; function is implemented as an assembly routine. This definition is
340 ;;; transformed to a call to the assembly routine allowing its use in byte
341 ;;; compiled code.
342 (defun random-mt19937 (state)
343   (declare (type (simple-array (unsigned-byte 32) (627)) state))
344   (random-mt19937 state))