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 (sb!kernel: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))
117 (declaim (inline context-register))
118 (defun context-register (context index)
119 (declare (type (alien (* os-context-t)) context))
120 (let ((addr (context-register-addr context index)))
121 (declare (type (alien (* unsigned)) addr))
124 (defun %set-context-register (context index new)
125 (declare (type (alien (* os-context-t)) context))
126 (let ((addr (context-register-addr context index)))
127 (declare (type (alien (* unsigned)) addr))
128 (setf (deref addr) new)))
130 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
131 ;;; register. FORMAT is the type of float to return.
133 ;;; As of sbcl-0.6.7, there is no working code which calls this code,
134 ;;; so it's stubbed out. Someday, in order to make the debugger work
135 ;;; better, it may be necessary to unstubify it.
136 (defun context-float-register (context index format)
137 (declare (ignore context index))
138 (warn "stub CONTEXT-FLOAT-REGISTER")
140 (defun %set-context-float-register (context index format new-value)
141 (declare (ignore context index))
142 (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
143 (coerce new-value format))
145 ;;; Given a signal context, return the floating point modes word in
146 ;;; the same format as returned by FLOATING-POINT-MODES.
148 (defun context-floating-point-modes (context)
149 (declare (ignore context)) ; stub!
150 (warn "stub CONTEXT-FLOATING-POINT-MODES")
153 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
154 (sb!alien:unsigned 32)
155 (context (* os-context-t)))
157 (define-alien-routine
158 ("arch_get_fp_modes" floating-point-modes) (sb!alien:unsigned 32))
160 (define-alien-routine
161 ("arch_set_fp_modes" %floating-point-modes-setter) void (fp (sb!alien:unsigned 32)))
163 (defun (setf floating-point-modes) (val) (%floating-point-modes-setter val))
166 ;;;; INTERNAL-ERROR-ARGS
168 ;;; Given a (POSIX) signal context, extract the internal error
169 ;;; arguments from the instruction stream.
170 (defun internal-error-args (context)
171 (declare (type (alien (* os-context-t)) context))
172 (/show0 "entering INTERNAL-ERROR-ARGS, CONTEXT=..")
174 (let ((pc (context-pc context)))
175 (declare (type system-area-pointer pc))
177 ;; using INT3 the pc is .. INT3 <here> code length bytes...
178 (let* ((length (sap-ref-8 pc 1))
179 (vector (make-array length :element-type '(unsigned-byte 8))))
180 (declare (type (unsigned-byte 8) length)
181 (type (simple-array (unsigned-byte 8) (*)) vector))
182 (/show0 "LENGTH,VECTOR,ERROR-NUMBER=..")
185 (copy-ub8-from-system-area pc 2 vector 0 length)
187 (error-number (sb!c:read-var-integer vector index)))
188 (/hexstr error-number)
189 (collect ((sc-offsets))
193 (when (>= index length)
195 (let ((sc-offset (sb!c:read-var-integer vector index)))
196 (/show0 "SC-OFFSET=..")
198 (sc-offsets sc-offset)))
199 (values error-number (sc-offsets)))))))
202 ;;; the current alien stack pointer; saved/restored for non-local exits
203 (defvar *alien-stack*)