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 ()
41 "Return a string describing the type of the local machine."
44 ;;;; :CODE-OBJECT fixups
46 ;;; This gets called by LOAD to resolve newly positioned objects
47 ;;; with things (like code instructions) that have to refer to them.
48 (defun fixup-code-object (code offset fixup kind
)
49 (declare (type index offset
))
51 (let ((sap (truly-the system-area-pointer
52 (code-instructions code
))))
53 (unless (member kind
'(:absolute
:absolute64
:relative
))
54 (error "Unknown code-object-fixup kind ~S." kind
))
57 ;; Word at sap + offset contains a value to be replaced by
58 ;; adding that value to fixup.
59 (setf (sap-ref-64 sap offset
) (+ fixup
(sap-ref-64 sap offset
))))
61 ;; Word at sap + offset contains a value to be replaced by
62 ;; adding that value to fixup.
63 (setf (sap-ref-32 sap offset
) (+ fixup
(sap-ref-32 sap offset
))))
65 ;; Fixup is the actual address wanted.
66 ;; Replace word with value to add to that loc to get there.
67 (let* ((loc-sap (+ (sap-int sap
) offset
))
68 (rel-val (- fixup loc-sap
(/ n-word-bytes
2))))
69 (declare (type (unsigned-byte 64) loc-sap
)
70 (type (signed-byte 32) rel-val
))
71 (setf (signed-sap-ref-32 sap offset
) rel-val
))))))
74 ;;;; low-level signal context access functions
76 ;;;; Note: In CMU CL, similar functions were hardwired to access
77 ;;;; BSD-style sigcontext structures defined as alien objects. Our
78 ;;;; approach is different in two ways:
79 ;;;; 1. We use POSIX SA_SIGACTION-style signals, so our context is
80 ;;;; whatever the void pointer in the sigaction handler dereferences
81 ;;;; to, not necessarily a sigcontext.
82 ;;;; 2. We don't try to maintain alien definitions of the context
83 ;;;; structure at Lisp level, but instead call alien C functions
84 ;;;; which take care of access for us. (Since the C functions can
85 ;;;; be defined in terms of system standard header files, they
86 ;;;; should be easier to maintain; and since Lisp code uses signal
87 ;;;; contexts only in interactive or exception code (like the debugger
88 ;;;; and internal error handling) the extra runtime cost should be
91 (declaim (inline context-pc-addr
))
92 (define-alien-routine ("os_context_pc_addr" context-pc-addr
) (* unsigned
)
93 ;; (Note: Just as in CONTEXT-REGISTER-ADDR, we intentionally use an
94 ;; 'unsigned *' interpretation for the 32-bit word passed to us by
95 ;; the C code, even though the C code may think it's an 'int *'.)
96 (context (* os-context-t
)))
98 (declaim (inline context-pc
))
99 (defun context-pc (context)
100 (declare (type (alien (* os-context-t
)) context
))
101 (let ((addr (context-pc-addr context
)))
102 (declare (type (alien (* unsigned
)) addr
))
103 (int-sap (deref addr
))))
105 (declaim (inline context-register-addr
))
106 (define-alien-routine ("os_context_register_addr" context-register-addr
)
108 ;; (Note the mismatch here between the 'int *' value that the C code
109 ;; may think it's giving us and the 'unsigned *' value that we
110 ;; receive. It's intentional: the C header files may think of
111 ;; register values as signed, but the CMU CL code tends to think of
112 ;; register values as unsigned, and might get bewildered if we ask
113 ;; it to work with signed values.)
114 (context (* os-context-t
))
118 (define-alien-routine ("os_context_float_register_addr" context-float-register-addr
)
119 (* unsigned
) (context (* os-context-t
)) (index int
))
121 (declaim (inline context-register
))
122 (defun context-register (context index
)
123 (declare (type (alien (* os-context-t
)) context
))
124 (let ((addr (context-register-addr context index
)))
125 (declare (type (alien (* unsigned
)) addr
))
128 (defun %set-context-register
(context index new
)
129 (declare (type (alien (* os-context-t
)) context
))
130 (let ((addr (context-register-addr context index
)))
131 (declare (type (alien (* unsigned
)) addr
))
132 (setf (deref addr
) new
)))
134 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
135 ;;; register. FORMAT is the type of float to return.
137 (defun context-float-register (context index format
)
138 (declare (ignorable context index
))
141 (warn "stub CONTEXT-FLOAT-REGISTER")
144 (let ((sap (alien-sap (context-float-register-addr context index
))))
147 (sap-ref-single sap
0))
149 (sap-ref-double sap
0))
150 (complex-single-float
151 (complex (sap-ref-single sap
0)
152 (sap-ref-single sap
4)))
153 (complex-double-float
154 (complex (sap-ref-double sap
0)
155 (sap-ref-double sap
8))))))
157 (defun %set-context-float-register
(context index format value
)
158 (declare (ignorable context index format
))
161 (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
164 (let ((sap (alien-sap (context-float-register-addr context index
))))
167 (setf (sap-ref-single sap
0) value
))
169 (setf (sap-ref-double sap
0) value
))
170 (complex-single-float
172 (declare (type (complex single-float
) value
))
173 (setf (sap-ref-single sap
0) (realpart value
)
174 (sap-ref-single sap
4) (imagpart value
))))
175 (complex-double-float
177 (declare (type (complex double-float
) value
))
178 (setf (sap-ref-double sap
0) (realpart value
)
179 (sap-ref-double sap
8) (imagpart value
)))))))
181 ;;; Given a signal context, return the floating point modes word in
182 ;;; the same format as returned by FLOATING-POINT-MODES.
184 (defun context-floating-point-modes (context)
185 (declare (ignore context
)) ; stub!
186 (warn "stub CONTEXT-FLOATING-POINT-MODES")
189 (define-alien-routine ("os_context_fp_control" context-floating-point-modes
)
191 (context (* os-context-t
)))
193 (define-alien-routine
194 ("arch_get_fp_modes" floating-point-modes
) (unsigned 32))
196 (define-alien-routine
197 ("arch_set_fp_modes" %floating-point-modes-setter
) void
(fp (unsigned 32)))
199 (defun (setf floating-point-modes
) (val) (%floating-point-modes-setter val
))
202 ;;;; INTERNAL-ERROR-ARGS
204 ;;; Given a (POSIX) signal context, extract the internal error
205 ;;; arguments from the instruction stream.
206 (defun internal-error-args (context)
207 (declare (type (alien (* os-context-t
)) context
))
208 (/show0
"entering INTERNAL-ERROR-ARGS, CONTEXT=..")
210 (let* ((pc (context-pc context
))
211 (trap-number (sap-ref-8 pc
0) ))
212 (declare (type system-area-pointer pc
))
214 ;; using INT3 the pc is .. INT3 <here> code length bytes...
215 (if (= trap-number invalid-arg-count-trap
)
216 (values #.
(error-number-or-lose 'invalid-arg-count-error
)
218 (let* ((length (sap-ref-8 pc
1))
219 (vector (make-array length
:element-type
'(unsigned-byte 8))))
220 (declare (type (unsigned-byte 8) length
)
221 (type (simple-array (unsigned-byte 8) (*)) vector
))
222 (/show0
"LENGTH,VECTOR,ERROR-NUMBER=..")
225 (copy-ub8-from-system-area pc
2 vector
0 length
)
227 (error-number (sb!c
:read-var-integer vector index
)))
228 (/hexstr error-number
)
229 (collect ((sc-offsets))
233 (when (>= index length
)
235 (let ((sc-offset (sb!c
:read-var-integer vector index
)))
236 (/show0
"SC-OFFSET=..")
238 (sc-offsets sc-offset
)))
239 (values error-number
(sc-offsets))))))))
242 ;;; the current alien stack pointer; saved/restored for non-local exits
243 (defvar *alien-stack-pointer
*)