1 ;;;; X86-specific runtime stuff
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.
16 ;;; a POSIX signal context, i.e. the type passed as the third
17 ;;; argument to an SA_SIGACTION-style signal handler
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.
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
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))
37 ;;;; MACHINE-TYPE and MACHINE-VERSION
39 (defun machine-type ()
41 "Return a string describing the type of the local machine."
44 (defun machine-version ()
46 "Return a string describing the version of the local machine."
49 ;;;; :CODE-OBJECT fixups
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.
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))))
62 ;;; This gets called by LOAD to resolve newly positioned objects
63 ;;; with things (like code instructions) that have to refer to them.
65 ;;; Add a fixup offset to the vector of fixup offsets for the given
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.)
76 (let ((fixups (code-header-ref code code-constants-offset)))
77 (cond ((typep fixups '(simple-array (unsigned-byte 32) (*)))
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)
84 (unless (or (eq (widetag-of fixups)
85 unbound-marker-widetag)
87 (format t "** Init. code FU = ~S~%" fixups)) ; FIXME
88 (setf (code-header-ref code code-constants-offset)
91 :element-type '(unsigned-byte 32)
92 :initial-element offset)))))))
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)
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
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))
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)))
115 ;; Fixup is the actual address wanted.
117 ;; Record relative fixups that point outside the code
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))))))
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.
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
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) (*)))
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)
147 (unless (or (eq (widetag-of fixups)
148 unbound-marker-widetag)
150 (sb!impl::!cold-lose "Argh! can't process fixup"))
151 (setf (code-header-ref code code-constants-offset)
154 :element-type '(unsigned-byte 32)
155 :initial-element offset)))))))
156 (let* ((sap (truly-the system-area-pointer
157 (sb!kernel:code-instructions code)))
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
163 (ncode-words (sb!kernel:code-header-ref code 1))
164 (code-end-addr (+ code-start-addr (* ncode-words n-word-bytes))))
167 ;; Record absolute fixups that point within the code object.
168 (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
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)))))))
175 ;;;; low-level signal context access functions
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
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)))
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))))
204 (define-alien-routine ("os_context_register_addr" context-register-addr)
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))
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))
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)))
227 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
228 ;;; register. FORMAT is the type of float to return.
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")
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))
242 ;;; Given a signal context, return the floating point modes word in
243 ;;; the same format as returned by FLOATING-POINT-MODES.
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
250 (declare (ignore context)) ; stub!
251 (warn "stub CONTEXT-FLOATING-POINT-MODES")
253 ;; old code for Linux:
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)))
267 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
268 (sb!alien:unsigned 32)
269 (context (* os-context-t)))
271 ;;;; INTERNAL-ERROR-ARGS
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=..")
279 (let ((pc (context-pc context)))
280 (declare (type system-area-pointer 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=..")
290 (copy-from-system-area pc (* n-byte-bits 2)
291 vector (* n-word-bits vector-data-offset)
292 (* length n-byte-bits))
294 (error-number (sb!c:read-var-integer vector index)))
295 (/hexstr error-number)
296 (collect ((sc-offsets))
300 (when (>= index length)
302 (let ((sc-offset (sb!c:read-var-integer vector index)))
303 (/show0 "SC-OFFSET=..")
305 (sc-offsets sc-offset)))
306 (values error-number (sc-offsets)))))))
308 ;;; This is used in error.lisp to insure that floating-point exceptions
309 ;;; are properly trapped. The compiler translates this to a VOP.
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
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*)
332 ;;; the current alien stack pointer; saved/restored for non-local exits
333 (defvar *alien-stack*)
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))