55c655c2d516b62c6fedf65e954ef53c90b36277
[sbcl.git] / src / code / sparc-vm.lisp
1 ;;;; SPARC-specific runtime 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 (in-package "SB!VM")
12 \f
13 ;;; See x86-vm.lisp for a description of this.
14 (define-alien-type os-context-t (struct os-context-t-struct))
15 \f
16 ;;;; MACHINE-TYPE and MACHINE-VERSION
17
18 (defun machine-type ()
19   "Returns a string describing the type of the local machine."
20   "SPARC")
21
22 ;;; support for CL:MACHINE-VERSION defined OAOO elsewhere
23 (defun get-machine-version ()
24   nil)
25 \f
26 (defun fixup-code-object (code offset fixup kind)
27   (declare (type index offset))
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 sb!kernel::code-instructions code))))
33      (ecase kind
34        (:call
35         (error "Can't deal with CALL fixups, yet."))
36        (:sethi
37         (setf (ldb (byte 22 0) (sap-ref-32 sap offset))
38               (ldb (byte 22 10) fixup)))
39        (:add
40         (setf (ldb (byte 10 0) (sap-ref-32 sap offset))
41               (ldb (byte 10 0) fixup)))))))
42
43 \f
44 ;;;; "Sigcontext" access functions, cut & pasted from alpha-vm.lisp.
45 ;;;;
46 ;;;; See also x86-vm for commentary on signed vs unsigned.
47
48 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-int)
49   (context (* os-context-t)))
50
51 (defun context-pc (context)
52   (declare (type (alien (* os-context-t)) context))
53   (int-sap (deref (context-pc-addr context))))
54
55 (define-alien-routine ("os_context_register_addr" context-register-addr)
56   (* unsigned-int)
57   (context (* os-context-t))
58   (index int))
59
60 ;;; FIXME: Should this and CONTEXT-PC be INLINE to reduce consing?
61 ;;; (Are they used in anything time-critical, or just the debugger?)
62 (defun context-register (context index)
63   (declare (type (alien (* os-context-t)) context))
64   (deref (context-register-addr context index)))
65
66 (defun %set-context-register (context index new)
67 (declare (type (alien (* os-context-t)) context))
68 (setf (deref (context-register-addr context index))
69       new))
70
71 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
72 ;;; register. FORMAT is the type of float to return.
73
74 ;;; FIXME: Whether COERCE actually knows how to make a float out of a
75 ;;; long is another question. This stuff still needs testing.
76 #+nil
77 (define-alien-routine ("os_context_float_register_addr" context-float-register-addr)
78   (* long)
79   (context (* os-context-t))
80   (index int))
81 #+nil
82 (defun context-float-register (context index format)
83   (declare (type (alien (* os-context-t)) context))
84   (coerce (deref (context-float-register-addr context index)) format))
85 #+nil
86 (defun %set-context-float-register (context index format new)
87   (declare (type (alien (* os-context-t)) context))
88   (setf (deref (context-float-register-addr context index))
89         (coerce new format)))
90
91 ;;; Given a signal context, return the floating point modes word in
92 ;;; the same format as returned by FLOATING-POINT-MODES.
93
94 ;;; Under SunOS, we have a straightforward implementation in C:
95 #!+sunos
96 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
97     (sb!alien:unsigned 32)
98   (context (* os-context-t)))
99
100 ;;; Under Linux, we have to contend with utterly broken signal handling.
101 #!+linux
102 (defun context-floating-point-modes (context)
103   (declare (ignore context))
104   (warn "stub CONTEXT-FLOATING-POINT-MODES")
105   0)
106 \f
107 ;;;; INTERNAL-ERROR-ARGS.
108
109 ;;; Given a (POSIX) signal context, extract the internal error
110 ;;; arguments from the instruction stream.  This is e.g.
111 ;;; 4       23      254     240     2       0       0       0
112 ;;; |       ~~~~~~~~~~~~~~~~~~~~~~~~~
113 ;;; length         data              (everything is an octet)
114 ;;;  (pc)
115 (defun internal-error-args (context)
116   (declare (type (alien (* os-context-t)) context))
117   (sb!int::/show0 "entering INTERNAL-ERROR-ARGS")
118   (let* ((pc (context-pc context))
119          (bad-inst (sap-ref-32 pc 0))
120          (op (ldb (byte 2 30) bad-inst))
121          (op2 (ldb (byte 3 22) bad-inst))
122          (op3 (ldb (byte 6 19) bad-inst)))
123     (declare (type system-area-pointer pc))
124     (cond ((and (= op #b00) (= op2 #b000))
125            (args-for-unimp-inst context))
126           ((and (= op #b10) (= (ldb (byte 4 2) op3) #b1000))
127            (args-for-tagged-add-inst context bad-inst))
128           ((and (= op #b10) (= op3 #b111010))
129            (args-for-tcc-inst bad-inst))
130           (t
131            (values #.(error-number-or-lose 'unknown-error) nil)))))
132
133 (defun args-for-unimp-inst (context)
134   (declare (type (alien (* os-context-t)) context))
135   (let* ((pc (context-pc context))
136          (length (sap-ref-8 pc 4))
137          (vector (make-array length :element-type '(unsigned-byte 8))))
138     (declare (type system-area-pointer pc)
139              (type (unsigned-byte 8) length)
140              (type (simple-array (unsigned-byte 8) (*)) vector))
141     (copy-ub8-from-system-area pc 5 vector 0 length)
142     (let* ((index 0)
143            (error-number (sb!c:read-var-integer vector index)))
144       (collect ((sc-offsets))
145                (loop
146                 (when (>= index length)
147                   (return))
148                 (sc-offsets (sb!c:read-var-integer vector index)))
149                (values error-number (sc-offsets))))))
150
151 (defun args-for-tagged-add-inst (context bad-inst)
152   (declare (type (alien (* os-context-t)) context))
153   (let* ((rs1 (ldb (byte 5 14) bad-inst))
154          (op1 (sb!kernel:make-lisp-obj (context-register context rs1))))
155     (if (fixnump op1)
156         (if (zerop (ldb (byte 1 13) bad-inst))
157             (let* ((rs2 (ldb (byte 5 0) bad-inst))
158                    (op2 (sb!kernel:make-lisp-obj (context-register context rs2))))
159               (if (fixnump op2)
160                   (values #.(error-number-or-lose 'unknown-error) nil)
161                   (values #.(error-number-or-lose 'object-not-fixnum-error)
162                           (list (sb!c::make-sc-offset
163                                  descriptor-reg-sc-number
164                                  rs2)))))
165             (values #.(error-number-or-lose 'unknown-error) nil))
166         (values #.(error-number-or-lose 'object-not-fixnum-error)
167                 (list (sb!c::make-sc-offset descriptor-reg-sc-number
168                                             rs1))))))
169
170 (defun args-for-tcc-inst (bad-inst)
171   (let* ((trap-number (ldb (byte 8 0) bad-inst))
172          (reg (ldb (byte 5 8) bad-inst)))
173     (values (case trap-number
174               (#.object-not-list-trap
175                #.(error-number-or-lose 'object-not-list-error))
176               (#.object-not-instance-trap
177                #.(error-number-or-lose 'object-not-instance-error))
178               (t
179                #.(error-number-or-lose 'unknown-error)))
180             (list (sb!c::make-sc-offset descriptor-reg-sc-number reg)))))
181