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