0.8.21.5:
[sbcl.git] / src / code / alpha-vm.lisp
1 ;;;; Alpha-specific implementation stuff
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!VM")
13
14 ;;; See x86-vm.lisp for a description of this.
15 (define-alien-type os-context-t (struct os-context-t-struct))
16 \f
17 ;;;; MACHINE-TYPE and MACHINE-VERSION
18
19 (defun machine-type ()
20   "Return a string describing the type of the local machine."
21   "Alpha")
22
23 ;;; support for CL:MACHINE-VERSION defined OAOO elsewhere
24 (defun get-machine-version ()
25   nil)
26 \f
27 (defun fixup-code-object (code offset value kind)
28   (unless (zerop (rem offset n-word-bytes))
29     (error "Unaligned instruction?  offset=#x~X." offset))
30   (sb!sys:without-gcing
31    (let ((sap (truly-the system-area-pointer
32                          (%primitive code-instructions code))))
33      (ecase kind
34        (:jmp-hint
35         (assert (zerop (ldb (byte 2 0) value)))
36         #+nil
37         (setf (sap-ref-16 sap offset)
38               (logior (sap-ref-16 sap offset)
39                       (ldb (byte 14 0) (ash value -2)))))
40        (:bits-63-48
41         (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
42                (value (if (logbitp 31 value) (+ value (ash 1 32)) value))
43                (value (if (logbitp 47 value) (+ value (ash 1 48)) value)))
44           (setf (sap-ref-8 sap offset) (ldb (byte 8 48) value))
45           (setf (sap-ref-8 sap (1+ offset)) (ldb (byte 8 56) value))))
46        (:bits-47-32
47         (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
48                (value (if (logbitp 31 value) (+ value (ash 1 32)) value)))
49           (setf (sap-ref-8 sap offset) (ldb (byte 8 32) value))
50           (setf (sap-ref-8 sap (1+ offset)) (ldb (byte 8 40) value))))
51        (:ldah
52         (let ((value (if (logbitp 15 value) (+ value (ash 1 16)) value)))
53           (setf (sap-ref-8 sap offset) (ldb (byte 8 16) value))
54           (setf (sap-ref-8 sap (1+ offset)) (ldb (byte 8 24) value))))
55        (:lda
56         (setf (sap-ref-8 sap offset) (ldb (byte 8 0) value))
57         (setf (sap-ref-8 sap (1+ offset)) (ldb (byte 8 8) value)))))))
58 \f
59 ;;;; "sigcontext" access functions, cut & pasted from x86-vm.lisp then
60 ;;;; hacked for types.
61 ;;;;
62 ;;;; KLUDGE: The alpha has 64-bit registers, so these potentially
63 ;;;; return 64 bit numbers (which means bignums ... ew) We think that
64 ;;;; 99 times of 100 (i.e. unless something is badly wrong) we'll get
65 ;;;; answers that fit in 32 bits anyway. Which probably won't help us
66 ;;;; stop passing bignums around as the compiler can't prove they fit
67 ;;;; in 32 bits. But maybe the stuff it does on x86 to unbox 32-bit
68 ;;;; constants happens magically for 64-bit constants here. Just
69 ;;;; maybe. -- Dan Barlow, ca. 2001-05-05
70 ;;;;
71 ;;;; See also x86-vm for commentary on signed vs unsigned.
72
73 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-long)
74   (context (* os-context-t)))
75
76 (defun context-pc (context)
77   (declare (type (alien (* os-context-t)) context))
78   (int-sap (deref (context-pc-addr context))))
79
80 (define-alien-routine ("os_context_register_addr" context-register-addr)
81   (* unsigned-long)
82   (context (* os-context-t))
83   (index int))
84
85 ;;; FIXME: Should this and CONTEXT-PC be INLINE to reduce consing?
86 ;;; (Are they used in anything time-critical, or just the debugger?)
87 (defun context-register (context index)
88   (declare (type (alien (* os-context-t)) context))
89   (deref (the (alien (* unsigned-long))
90            (context-register-addr context index))))
91
92 (defun %set-context-register (context index new)
93   (declare (type (alien (* os-context-t)) context))
94   (setf (deref (the (alien (* unsigned-long))
95                  (context-register-addr context index)))
96         new))
97
98 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
99 ;;; register. FORMAT is the type of float to return.
100
101 ;;; FIXME: Whether COERCE actually knows how to make a float out of a
102 ;;; long is another question. This stuff still needs testing.
103 (define-alien-routine ("os_context_float_register_addr"
104                        context-float-register-addr)
105   (* long)
106   (context (* os-context-t))
107   (index int))
108 (defun context-float-register (context index format)
109   (declare (type (alien (* os-context-t)) context))
110   (coerce (deref (context-float-register-addr context index)) format))
111 (defun %set-context-float-register (context index format new)
112   (declare (type (alien (* os-context-t)) context))
113   (setf (deref (context-float-register-addr context index))
114         (coerce new format)))
115
116 ;;; This sets the software fp_control word, which is not the same
117 ;;; thing as the hardware fpcr.  We have to do this so that OS FPU
118 ;;; completion works properly
119
120 ;;; Note that this means we can't set rounding modes; we'd have to do
121 ;;; that separately.  That said, almost everybody seems to agree that
122 ;;; changing the rounding mode is rarely a good idea, because it upsets
123 ;;; libm functions.  So adding that is not a priority.  Sorry.
124 ;;; -dan 2001.02.06
125
126 (define-alien-routine
127     ("arch_get_fp_control" floating-point-modes) (sb!alien:unsigned 64))
128
129 (define-alien-routine
130     ("arch_set_fp_control" %floating-point-modes-setter) void (fp (sb!alien:unsigned 64)))
131
132 (defun (setf floating-point-modes) (val) (%floating-point-modes-setter val))
133
134 ;;; Given a signal context, return the floating point modes word in
135 ;;; the same format as returned by FLOATING-POINT-MODES.
136 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
137     (sb!alien:unsigned 64) (context (* os-context-t)))
138
139 \f
140 ;;;; INTERNAL-ERROR-ARGS
141
142 ;;; Given a (POSIX) signal context, extract the internal error
143 ;;; arguments from the instruction stream.  This is e.g.
144 ;;; 4       23      254     240     2       0       0       0 
145 ;;; |       ~~~~~~~~~~~~~~~~~~~~~~~~~
146 ;;; length         data              (everything is an octet)
147 ;;;  (pc)
148 ;;; (example from undefined_tramp: "(gdb) x/40ub 0x10148" for yourself
149 ;;; to replicate)
150 (defun internal-error-args (context)
151   (declare (type (alien (* os-context-t)) context))
152   (let ((pc (context-pc context)))
153     (declare (type system-area-pointer pc))
154     ;; pc is a SAP pointing at - or actually, shortly after -
155     ;; the instruction that got us into this mess in the first place
156     (let* ((length (sap-ref-8 pc 4))
157            (vector (make-array length :element-type '(unsigned-byte 8))))
158       (declare (type (unsigned-byte 8) length)
159                (type (simple-array (unsigned-byte 8) (*)) vector))
160       (copy-ub8-from-system-area pc 5 vector 0 length)
161       (let* ((index 0)
162              (error-number (sb!c:read-var-integer vector index)))
163         (collect ((sc-offsets))
164                  (loop
165                   (when (>= index length)
166                     (return))
167                   (sc-offsets (sb!c:read-var-integer vector index)))
168                  (values error-number (sc-offsets)))))))
169