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