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