0.pre7.136:
[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 \f
14 (defvar *number-of-signals* 64)
15 (defvar *bits-per-word* 64)
16
17 ;;; See x86-vm.lisp for a description of this.
18 (define-alien-type os-context-t (struct os-context-t-struct))
19 \f
20 ;;;; MACHINE-TYPE and MACHINE-VERSION
21
22 (defun machine-type ()
23   "Return a string describing the type of the local machine."
24   "Alpha")
25 (defun machine-version ()
26   "Return a string describing the version of the local machine."
27   "Alpha")
28 \f
29 (defun fixup-code-object (code offset value kind)
30   (unless (zerop (rem offset n-word-bytes))
31     (error "Unaligned instruction?  offset=#x~X." offset))
32   (sb!sys:without-gcing
33    (let ((sap (truly-the system-area-pointer
34                          (%primitive sb!kernel::code-instructions code))))
35      (ecase kind
36        (:jmp-hint
37         (assert (zerop (ldb (byte 2 0) value)))
38         #+nil
39         (setf (sap-ref-16 sap offset)
40               (logior (sap-ref-16 sap offset)
41                       (ldb (byte 14 0) (ash value -2)))))
42        (:bits-63-48
43         (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
44                (value (if (logbitp 31 value) (+ value (ash 1 32)) value))
45                (value (if (logbitp 47 value) (+ value (ash 1 48)) value)))
46           (setf (sap-ref-8 sap offset) (ldb (byte 8 48) value))
47           (setf (sap-ref-8 sap (1+ offset)) (ldb (byte 8 56) value))))
48        (:bits-47-32
49         (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
50                (value (if (logbitp 31 value) (+ value (ash 1 32)) value)))
51           (setf (sap-ref-8 sap offset) (ldb (byte 8 32) value))
52           (setf (sap-ref-8 sap (1+ offset)) (ldb (byte 8 40) value))))
53        (:ldah
54         (let ((value (if (logbitp 15 value) (+ value (ash 1 16)) value)))
55           (setf (sap-ref-8 sap offset) (ldb (byte 8 16) value))
56           (setf (sap-ref-8 sap (1+ offset)) (ldb (byte 8 24) value))))
57        (:lda
58         (setf (sap-ref-8 sap offset) (ldb (byte 8 0) value))
59         (setf (sap-ref-8 sap (1+ offset)) (ldb (byte 8 8) value)))))))
60 \f
61 ;;;; "sigcontext" access functions, cut & pasted from x86-vm.lisp then
62 ;;;; hacked for types.
63 ;;;;
64 ;;;; KLUDGE: The alpha has 64-bit registers, so these potentially
65 ;;;; return 64 bit numbers (which means bignums ... ew) We think that
66 ;;;; 99 times of 100 (i.e. unless something is badly wrong) we'll get
67 ;;;; answers that fit in 32 bits anyway. Which probably won't help us
68 ;;;; stop passing bignums around as the compiler can't prove they fit
69 ;;;; in 32 bits. But maybe the stuff it does on x86 to unbox 32-bit
70 ;;;; constants happens magically for 64-bit constants here. Just
71 ;;;; maybe. -- Dan Barlow, ca. 2001-05-05
72 ;;;;
73 ;;;; See also x86-vm for commentary on signed vs unsigned.
74
75 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-long)
76   (context (* os-context-t)))
77
78 (defun context-pc (context)
79   (declare (type (alien (* os-context-t)) context))
80   (int-sap (deref (context-pc-addr context))))
81
82 (define-alien-routine ("os_context_register_addr" context-register-addr)
83   (* unsigned-long)
84   (context (* os-context-t))
85   (index int))
86
87 ;;; FIXME: Should this and CONTEXT-PC be INLINE to reduce consing?
88 ;;; (Are they used in anything time-critical, or just the debugger?)
89 (defun context-register (context index)
90   (declare (type (alien (* os-context-t)) context))
91   (deref (the (alien (* unsigned-long))
92            (context-register-addr context index))))
93
94 (defun %set-context-register (context index new)
95   (declare (type (alien (* os-context-t)) context))
96   (setf (deref (the (alien (* unsigned-long))
97                  (context-register-addr context index)))
98         new))
99
100 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
101 ;;; register. FORMAT is the type of float to return.
102
103 ;;; FIXME: Whether COERCE actually knows how to make a float out of a
104 ;;; long is another question. This stuff still needs testing.
105 (define-alien-routine ("os_context_fpregister_addr"
106                        context-float-register-addr)
107   (* long)
108   (context (* os-context-t))
109   (index int))
110 (defun context-float-register (context index format)
111   (declare (type (alien (* os-context-t)) context))
112   (coerce (deref (context-float-register-addr context index)) format))
113 (defun %set-context-float-register (context index format new)
114   (declare (type (alien (* os-context-t)) context))
115   (setf (deref (context-float-register-addr context index))
116         (coerce new format)))
117
118 ;;; Given a signal context, return the floating point modes word in
119 ;;; the same format as returned by FLOATING-POINT-MODES.
120 (defun context-floating-point-modes (context)
121   ;; FIXME: As of sbcl-0.6.7 and the big rewrite of signal handling for
122   ;; POSIXness and (at the Lisp level) opaque signal contexts,
123   ;; this is stubified. It needs to be rewritten as an
124   ;; alien function.
125   (warn "stub CONTEXT-FLOATING-POINT-MODES")
126
127   ;; old code for Linux:
128   #+nil
129   (let ((cw (slot (deref (slot context 'fpstate) 0) 'cw))
130         (sw (slot (deref (slot context 'fpstate) 0) 'sw)))
131     ;;(format t "cw = ~4X~%sw = ~4X~%" cw sw)
132     ;; NOT TESTED -- Clear sticky bits to clear interrupt condition.
133     (setf (slot (deref (slot context 'fpstate) 0) 'sw) (logandc2 sw #x3f))
134     ;;(format t "new sw = ~X~%" (slot (deref (slot context 'fpstate) 0) 'sw))
135     ;; Simulate floating-point-modes VOP.
136     (logior (ash (logand sw #xffff) 16) (logxor (logand cw #xffff) #x3f)))
137
138   0)
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-from-system-area pc (* n-byte-bits 5)
161                              vector (* n-word-bits vector-data-offset)
162                              (* length n-byte-bits))
163       (let* ((index 0)
164              (error-number (sb!c::read-var-integer vector index)))
165         (collect ((sc-offsets))
166                  (loop
167                   (when (>= index length)
168                     (return))
169                   (sc-offsets (sb!c::read-var-integer vector index)))
170                  (values error-number (sc-offsets)))))))
171 \f
172 ;;; The loader uses this to convert alien names to the form they
173 ;;; occure in the symbol table (for example, prepending an
174 ;;; underscore). 
175 (defun extern-alien-name (name)
176   (declare (type simple-base-string name))
177   ;; On the Alpha we don't do anything.
178   name)
179 \f
180 ;;;; Do whatever is necessary to make the given code component
181 ;;;; executable.
182 ;;;;
183 ;;;; XXX do we really not have to flush caches or something here? I
184 ;;;; need an architecture manual
185 (defun sanctify-for-execution (component)
186   (declare (ignore component))
187   nil)