1 ;;;; X86-64-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
))
39 (defun machine-type ()
40 "Return a string describing the type of the local machine."
43 ;;;; :CODE-OBJECT fixups
45 ;;; This gets called by LOAD to resolve newly positioned objects
46 ;;; with things (like code instructions) that have to refer to them.
47 (defun fixup-code-object (code offset fixup kind
&optional flavor
)
48 (declare (type index offset
) (ignorable flavor
))
50 (let ((sap (code-instructions code
)))
53 ;; Word at sap + offset contains a value to be replaced by
54 ;; adding that value to fixup.
55 (setf (sap-ref-64 sap offset
) (+ fixup
(sap-ref-64 sap offset
))))
57 ;; Word at sap + offset contains a value to be replaced by
58 ;; adding that value to fixup.
59 (setf (sap-ref-32 sap offset
) (+ fixup
(signed-sap-ref-32 sap offset
))))
61 ;; Fixup is the actual address wanted.
62 ;; Replace word with value to add to that loc to get there.
63 ;; In the #!-immobile-code case, there's nothing to assert.
64 ;; Relative fixups pretty much can't happen.
66 (unless (<= immobile-space-start
(get-lisp-obj-address code
) immobile-space-end
)
67 (error "Can't compute fixup relative to movable object ~S" code
))
68 (setf (signed-sap-ref-32 sap offset
)
71 ;; JMP/CALL are relative to the next instruction,
72 ;; so add 4 bytes for the size of the displacement itself.
74 (the (unsigned-byte 64) (+ (sap-int sap
) offset
4))))))))))
75 ;; An absolute fixup is stored in the code header if it
76 ;; references an immobile-space (but not static-space) object.
77 ;; This needn't be inside WITHOUT-GCING, because code fixups will point
78 ;; only to objects that don't move except during save-lisp-and-die.
79 ;; So there is no race with GC here.
81 (when (eq flavor
:immobile-object
)
82 (let ((fixups (sb!vm
::%code-fixups code
)))
83 ;; Sanctifying the code component will compact these into a bignum.
84 (setf (sb!vm
::%code-fixups code
)
85 (cons offset
(if (eql fixups
0) nil fixups
)))))
88 (defun sanctify-for-execution (code)
89 (let ((fixups (sb!vm
::%code-fixups code
)))
91 (setf (sb!vm
::%code-fixups code
) (sb!c
::pack-code-fixup-locs fixups
))))
94 ;;;; low-level signal context access functions
96 ;;;; Note: In CMU CL, similar functions were hardwired to access
97 ;;;; BSD-style sigcontext structures defined as alien objects. Our
98 ;;;; approach is different in two ways:
99 ;;;; 1. We use POSIX SA_SIGACTION-style signals, so our context is
100 ;;;; whatever the void pointer in the sigaction handler dereferences
101 ;;;; to, not necessarily a sigcontext.
102 ;;;; 2. We don't try to maintain alien definitions of the context
103 ;;;; structure at Lisp level, but instead call alien C functions
104 ;;;; which take care of access for us. (Since the C functions can
105 ;;;; be defined in terms of system standard header files, they
106 ;;;; should be easier to maintain; and since Lisp code uses signal
107 ;;;; contexts only in interactive or exception code (like the debugger
108 ;;;; and internal error handling) the extra runtime cost should be
111 (declaim (inline context-pc-addr
))
112 (define-alien-routine ("os_context_pc_addr" context-pc-addr
) (* unsigned
)
113 ;; (Note: Just as in CONTEXT-REGISTER-ADDR, we intentionally use an
114 ;; 'unsigned *' interpretation for the 32-bit word passed to us by
115 ;; the C code, even though the C code may think it's an 'int *'.)
116 (context (* os-context-t
)))
118 (declaim (inline context-pc
))
119 (defun context-pc (context)
120 (declare (type (alien (* os-context-t
)) context
))
121 (let ((addr (context-pc-addr context
)))
122 (declare (type (alien (* unsigned
)) addr
))
123 (int-sap (deref addr
))))
125 (declaim (inline context-register-addr
))
126 (define-alien-routine ("os_context_register_addr" context-register-addr
)
128 ;; (Note the mismatch here between the 'int *' value that the C code
129 ;; may think it's giving us and the 'unsigned *' value that we
130 ;; receive. It's intentional: the C header files may think of
131 ;; register values as signed, but the CMU CL code tends to think of
132 ;; register values as unsigned, and might get bewildered if we ask
133 ;; it to work with signed values.)
134 (context (* os-context-t
))
137 #!+(or darwin linux win32
)
138 (define-alien-routine ("os_context_float_register_addr" context-float-register-addr
)
139 (* unsigned
) (context (* os-context-t
)) (index int
))
141 (declaim (inline context-register
))
142 (defun context-register (context index
)
143 (declare (type (alien (* os-context-t
)) context
))
144 (let ((addr (context-register-addr context index
)))
145 (declare (type (alien (* unsigned
)) addr
))
148 (defun %set-context-register
(context index new
)
149 (declare (type (alien (* os-context-t
)) context
))
150 (let ((addr (context-register-addr context index
)))
151 (declare (type (alien (* unsigned
)) addr
))
152 (setf (deref addr
) new
)))
154 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
155 ;;; register. FORMAT is the type of float to return.
157 (defun context-float-register (context index format
)
158 (declare (ignorable context index
))
159 #!-
(or darwin linux win32
)
161 (warn "stub CONTEXT-FLOAT-REGISTER")
163 #!+(or darwin linux win32
)
164 (let ((sap (alien-sap (context-float-register-addr context index
))))
167 (sap-ref-single sap
0))
169 (sap-ref-double sap
0))
170 (complex-single-float
171 (complex (sap-ref-single sap
0)
172 (sap-ref-single sap
4)))
173 (complex-double-float
174 (complex (sap-ref-double sap
0)
175 (sap-ref-double sap
8))))))
177 (defun %set-context-float-register
(context index format value
)
178 (declare (ignorable context index format
))
181 (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
184 (let ((sap (alien-sap (context-float-register-addr context index
))))
187 (setf (sap-ref-single sap
0) value
))
189 (setf (sap-ref-double sap
0) value
))
190 (complex-single-float
192 (declare (type (complex single-float
) value
))
193 (setf (sap-ref-single sap
0) (realpart value
)
194 (sap-ref-single sap
4) (imagpart value
))))
195 (complex-double-float
197 (declare (type (complex double-float
) value
))
198 (setf (sap-ref-double sap
0) (realpart value
)
199 (sap-ref-double sap
8) (imagpart value
)))))))
201 ;;; Given a signal context, return the floating point modes word in
202 ;;; the same format as returned by FLOATING-POINT-MODES.
204 (defun context-floating-point-modes (context)
205 (declare (ignore context
)) ; stub!
206 (warn "stub CONTEXT-FLOATING-POINT-MODES")
209 (define-alien-routine ("os_context_fp_control" context-floating-point-modes
)
211 (context (* os-context-t
)))
213 (define-alien-routine
214 ("arch_get_fp_modes" floating-point-modes
) (unsigned 32))
216 (define-alien-routine
217 ("arch_set_fp_modes" %floating-point-modes-setter
) void
(fp (unsigned 32)))
219 (defun (setf floating-point-modes
) (val) (%floating-point-modes-setter val
))
222 ;;;; INTERNAL-ERROR-ARGS
224 ;;; Given a (POSIX) signal context, extract the internal error
225 ;;; arguments from the instruction stream.
226 (defun internal-error-args (context)
227 (declare (type (alien (* os-context-t
)) context
))
228 (/show0
"entering INTERNAL-ERROR-ARGS, CONTEXT=..")
230 (let* ((pc (context-pc context
))
231 (trap-number (sap-ref-8 pc
0)))
232 (declare (type system-area-pointer pc
))
234 ;; using INT3 the pc is .. INT3 <here> code length bytes...
235 (if (= trap-number invalid-arg-count-trap
)
236 (values #.
(error-number-or-lose 'invalid-arg-count-error
)
238 (let ((error-number (sap-ref-8 pc
1)))
240 (sb!kernel
::decode-internal-error-args
(sap+ pc
2) error-number
))))))
243 ;;; the current alien stack pointer; saved/restored for non-local exits
244 (defvar *alien-stack-pointer
*)