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 (def-alien-type os-context-t (struct os-context-t-struct))
37 ;;;; MACHINE-TYPE and MACHINE-VERSION
39 (defun machine-type ()
41 "Returns a string describing the type of the local machine."
44 (defun machine-version ()
46 "Returns 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 ;;; This gets called by LOAD to resolve newly positioned objects
56 ;;; with things (like code instructions) that have to refer to them.
58 ;;; Add a fixup offset to the vector of fixup offsets for the given
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
67 (when sb!impl::*enable-dynamic-space-code*
69 (let ((fixups (code-header-ref code code-constants-offset)))
70 (cond ((typep fixups '(simple-array (unsigned-byte 32) (*)))
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)
78 (unless (or (eq (get-type fixups)
79 sb!vm:unbound-marker-type)
81 (format t "** Init. code FU = ~S~%" fixups)) ; FIXME
82 (setf (code-header-ref code code-constants-offset)
83 (make-specializable-array
85 :element-type '(unsigned-byte 32)
86 :initial-element offset))))))))
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)
92 #+nil (const-start-addr (+ obj-start-addr (* 5 4)))
93 (code-start-addr (sb!sys:sap-int (sb!kernel:code-instructions
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))
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)))
108 ;; Fixup is the actual address wanted.
110 ;; Record relative fixups that point outside the code
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))))))
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.
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
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) (*)))
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)
141 ;; FIXME: This doesn't look like production code, and
142 ;; should be a fatal error, not just a print.
143 (unless (or (eq (get-type fixups)
144 sb!vm:unbound-marker-type)
146 (%primitive print "** Init. code FU"))
147 (setf (code-header-ref code sb!vm:code-constants-offset)
148 (make-specializable-array
150 :element-type '(unsigned-byte 32)
151 :initial-element offset)))))))
152 (let* ((sap (truly-the system-area-pointer
153 (sb!kernel:code-instructions code)))
155 ;; FIXME: looks like (LOGANDC2 foo typebits)
156 (logand (sb!kernel:get-lisp-obj-address code) #xfffffff8))
157 (code-start-addr (sb!sys:sap-int (sb!kernel:code-instructions
159 (ncode-words (sb!kernel:code-header-ref code 1))
160 (code-end-addr (+ code-start-addr (* ncode-words 4))))
163 ;; Record absolute fixups that point within the code object.
164 (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
165 (add-load-time-code-fixup code offset)))
167 ;; Record relative fixups that point outside the code object.
168 (when (or (< fixup obj-start-addr) (> fixup code-end-addr))
169 (add-load-time-code-fixup code offset)))))))
171 ;;;; low-level signal context access functions
173 ;;;; Note: In CMU CL, similar functions were hardwired to access
174 ;;;; BSD-style sigcontext structures defined as alien objects. Our
175 ;;;; approach is different in two ways:
176 ;;;; 1. We use POSIX SA_SIGACTION-style signals, so our context is
177 ;;;; whatever the void pointer in the sigaction handler dereferences
178 ;;;; to, not necessarily a sigcontext.
179 ;;;; 2. We don't try to maintain alien definitions of the context
180 ;;;; structure at Lisp level, but instead call alien C functions
181 ;;;; which take care of access for us. (Since the C functions can
182 ;;;; be defined in terms of system standard header files, they
183 ;;;; should be easier to maintain; and since Lisp code uses signal
184 ;;;; contexts only in interactive or exception code (like the debugger
185 ;;;; and internal error handling) the extra runtime cost should be
188 (def-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-int)
189 ;; (Note: Just as in CONTEXT-REGISTER-ADDR, we intentionally use an
190 ;; 'unsigned *' interpretation for the 32-bit word passed to us by
191 ;; the C code, even though the C code may think it's an 'int *'.)
192 (context (* os-context-t)))
194 (defun context-pc (context)
195 (declare (type (alien (* os-context-t)) context))
196 (int-sap (deref (context-pc-addr context))))
198 (def-alien-routine ("os_context_register_addr" context-register-addr)
200 ;; (Note the mismatch here between the 'int *' value that the C code
201 ;; may think it's giving us and the 'unsigned *' value that we
202 ;; receive. It's intentional: the C header files may think of
203 ;; register values as signed, but the CMU CL code tends to think of
204 ;; register values as unsigned, and might get bewildered if we ask
205 ;; it to work with signed values.)
206 (context (* os-context-t))
209 ;;; FIXME: Should this and CONTEXT-PC be INLINE to reduce consing?
210 ;;; (Are they used in anything time-critical, or just the debugger?)
211 (defun context-register (context index)
212 (declare (type (alien (* os-context-t)) context))
213 (deref (context-register-addr context index)))
215 (defun %set-context-register (context index new)
216 (declare (type (alien (* os-context-t)) context))
217 (setf (deref (context-register-addr context index))
220 ;;; Like CONTEXT-REGISTER, but returns the value of a float register.
221 ;;; FORMAT is the type of float to return.
223 ;;; As of sbcl-0.6.7, there is no working code which calls this code,
224 ;;; so it's stubbed out. Someday, in order to make the debugger work
225 ;;; better, it may be necessary to unstubify it.
226 (defun context-float-register (context index format)
227 (declare (ignore context index))
228 (warn "stub CONTEXT-FLOAT-REGISTER")
230 (defun %set-context-float-register (context index format new-value)
231 (declare (ignore context index))
232 (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
233 (coerce new-value format))
235 ;;; Given a signal context, return the floating point modes word in
236 ;;; the same format as returned by FLOATING-POINT-MODES.
237 (defun context-floating-point-modes (context)
238 ;; FIXME: As of sbcl-0.6.7 and the big rewrite of signal handling for
239 ;; POSIXness and (at the Lisp level) opaque signal contexts,
240 ;; this is stubified. It needs to be rewritten as an
242 (warn "stub CONTEXT-FLOATING-POINT-MODES")
244 ;; old code for Linux:
246 (let ((cw (slot (deref (slot context 'fpstate) 0) 'cw))
247 (sw (slot (deref (slot context 'fpstate) 0) 'sw)))
248 ;;(format t "cw = ~4X~%sw = ~4X~%" cw sw)
249 ;; NOT TESTED -- Clear sticky bits to clear interrupt condition.
250 (setf (slot (deref (slot context 'fpstate) 0) 'sw) (logandc2 sw #x3f))
251 ;;(format t "new sw = ~X~%" (slot (deref (slot context 'fpstate) 0) 'sw))
252 ;; Simulate floating-point-modes VOP.
253 (logior (ash (logand sw #xffff) 16) (logxor (logand cw #xffff) #x3f)))
257 ;;;; INTERNAL-ERROR-ARGUMENTS
259 ;;; Given a (POSIX) signal context, extract the internal error
260 ;;; arguments from the instruction stream.
261 (defun internal-error-arguments (context)
262 (declare (type (alien (* os-context-t)) context))
263 (/show0 "entering INTERNAL-ERROR-ARGUMENTS, CONTEXT=..")
264 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr context))
265 (let ((pc (context-pc context)))
266 (declare (type system-area-pointer pc))
267 ;; using INT3 the pc is .. INT3 <here> code length bytes...
268 (let* ((length (sap-ref-8 pc 1))
269 (vector (make-array length :element-type '(unsigned-byte 8))))
270 (declare (type (unsigned-byte 8) length)
271 (type (simple-array (unsigned-byte 8) (*)) vector))
272 (/show0 "LENGTH,VECTOR,ERROR-NUMBER=..")
273 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr length))
274 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr vector))
275 (copy-from-system-area pc (* sb!vm:byte-bits 2)
276 vector (* sb!vm:word-bits
277 sb!vm:vector-data-offset)
278 (* length sb!vm:byte-bits))
280 (error-number (sb!c::read-var-integer vector index)))
281 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr error-number))
282 (collect ((sc-offsets))
285 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr index))
286 (when (>= index length)
288 (let ((sc-offset (sb!c::read-var-integer vector index)))
289 (/show0 "SC-OFFSET=..")
290 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr sc-offset))
291 (sc-offsets sc-offset)))
292 (values error-number (sc-offsets)))))))
294 ;;; Do whatever is necessary to make the given code component
295 ;;; executable. (This is a no-op on the x86.)
296 (defun sanctify-for-execution (component)
297 (declare (ignore component))
302 ;;; This is used in error.lisp to insure that floating-point exceptions
303 ;;; are properly trapped. The compiler translates this to a VOP.
309 ;;; These are used by the FP MOVE-FROM-{SINGLE|DOUBLE} VOPs rather than the
310 ;;; i387 load constant instructions to avoid consing in some cases. Note these
311 ;;; are initialized by GENESIS as they are needed early.
312 (defvar *fp-constant-0s0*)
313 (defvar *fp-constant-1s0*)
314 (defvar *fp-constant-0d0*)
315 (defvar *fp-constant-1d0*)
316 ;;; The long-float constants.
317 (defvar *fp-constant-0l0*)
318 (defvar *fp-constant-1l0*)
319 (defvar *fp-constant-pi*)
320 (defvar *fp-constant-l2t*)
321 (defvar *fp-constant-l2e*)
322 (defvar *fp-constant-lg2*)
323 (defvar *fp-constant-ln2*)
325 ;;; Enable/disable scavenging of the read-only space.
326 (defvar *scavenge-read-only-space* nil)
327 ;;; FIXME: should be *SCAVENGE-READ-ONLY-SPACE-P*
329 ;;; The current alien stack pointer; saved/restored for non-local exits.
330 (defvar *alien-stack*)
332 (defun sb!kernel::%instance-set-conditional (object slot test-value new-value)
333 (declare (type instance object)
336 "Atomically compare object's slot value to test-value and if EQ store
337 new-value in the slot. The original value of the slot is returned."
338 (sb!kernel::%instance-set-conditional object slot test-value new-value))
340 ;;; Support for the MT19937 random number generator. The update
341 ;;; function is implemented as an assembly routine. This definition is
342 ;;; transformed to a call to the assembly routine allowing its use in byte
344 (defun random-mt19937 (state)
345 (declare (type (simple-array (unsigned-byte 32) (627)) state))
346 (random-mt19937 state))