0.9.4.12:
[sbcl.git] / src / code / mips-vm.lisp
1 (in-package "SB!VM")
2
3 \f
4 (define-alien-type os-context-t (struct os-context-t-struct))
5
6 \f
7 ;;;; MACHINE-TYPE and MACHINE-VERSION
8
9 (defun machine-type ()
10   "Returns a string describing the type of the local machine."
11   "MIPS")
12
13 ;;; support for CL:MACHINE-VERSION defined OAOO elsewhere
14 (defun get-machine-version ()
15   #!+little-endian "little-endian"
16   #!-little-endian "big-endian")
17
18 \f
19 ;;;; FIXUP-CODE-OBJECT
20
21 (defun fixup-code-object (code offset value kind)
22   (unless (zerop (rem offset n-word-bytes))
23     (error "Unaligned instruction?  offset=#x~X." offset))
24   (sb!sys:without-gcing
25    (let ((sap (truly-the system-area-pointer
26                          (%primitive sb!c::code-instructions code))))
27      (ecase kind
28        (:jump
29         (aver (zerop (ash value -28)))
30         (setf (ldb (byte 26 0) (sap-ref-32 sap offset))
31               (ash value -2)))
32        (:lui
33         (setf (sap-ref-16 sap
34                           #!+little-endian offset
35                           #!-little-endian (+ offset 2))
36               (+ (ash value -16)
37                  (if (logbitp 15 value) 1 0))))
38        (:addi
39         (setf (sap-ref-16 sap
40                           #!+little-endian offset
41                           #!-little-endian (+ offset 2))
42               (ldb (byte 16 0) value)))))))
43
44 \f
45 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-int)
46   (context (* os-context-t)))
47
48 (defun context-pc (context)
49   (declare (type (alien (* os-context-t)) context))
50   ;; KLUDGE: this sucks, and furthermore will break on either of (a)
51   ;; porting back to IRIX or (b) running on proper 64-bit support.
52   ;; Linux on the MIPS defines its registers in the sigcontext as
53   ;; 64-bit quantities ("unsigned long long"), presumably to be
54   ;; binary-compatible with 64-bit mode.  Since there appears not to
55   ;; be ALIEN support for 64-bit return values, we have to do the
56   ;; hacky pointer arithmetic thing.  -- CSR, 2002-09-01
57   (int-sap (deref (context-pc-addr context)
58                   #!-little-endian 1
59                   #!+little-endian 0)))
60
61 (define-alien-routine ("os_context_register_addr" context-register-addr)
62   (* unsigned-int)
63   (context (* os-context-t))
64   (index int))
65
66 (define-alien-routine ("os_context_bd_cause" context-bd-cause-int)
67     (unsigned 32)
68   (context (* os-context-t)))
69
70 ;;; FIXME: Should this and CONTEXT-PC be INLINE to reduce consing?
71 ;;; (Are they used in anything time-critical, or just the debugger?)
72 (defun context-register (context index)
73   (declare (type (alien (* os-context-t)) context))
74   (deref (context-register-addr context index)
75          #!-little-endian 1
76          #!+little-endian 0))
77
78 (defun %set-context-register (context index new)
79   (declare (type (alien (* os-context-t)) context))
80   (setf (deref (context-register-addr context index)
81                #!-little-endian 1
82                #!+little-endian 0)
83         new))
84
85 #!+linux
86 ;;; For now.
87 (defun context-floating-point-modes (context)
88   (declare (ignore context))
89   (warn "stub CONTEXT-FLOATING-POINT-MODES")
90   0)
91
92 ;;;; Internal-error-arguments.
93
94 ;;; INTERNAL-ERROR-ARGUMENTS -- interface.
95 ;;;
96 ;;; Given the sigcontext, extract the internal error arguments from the
97 ;;; instruction stream.
98 ;;;
99 (defun internal-error-args (context)
100   (declare (type (alien (* os-context-t)) context))
101   (/show0 "entering INTERNAL-ERROR-ARGS, CONTEXT=..")
102   (/hexstr context)
103   (let ((pc (context-pc context))
104         (cause (context-bd-cause-int context)))
105     (declare (type system-area-pointer pc))
106     ;; KLUDGE: This exposure of the branch delay mechanism hurts.
107     (when (logbitp 31 cause)
108       (setf pc (sap+ pc 4)))
109     (args-for-unimp-inst pc)))
110
111 (defun args-for-unimp-inst (pc)
112   (declare (type system-area-pointer pc))
113   (let* ((length (sap-ref-8 pc 4))
114          (vector (make-array length :element-type '(unsigned-byte 8))))
115     (declare (type (unsigned-byte 8) length)
116              (type (simple-array (unsigned-byte 8) (*)) vector))
117     (copy-ub8-from-system-area pc 5 vector 0 length)
118     (let* ((index 0)
119            (error-number (sb!c:read-var-integer vector index)))
120       (collect ((sc-offsets))
121                (loop
122                 (when (>= index length)
123                   (return))
124                 (sc-offsets (sb!c:read-var-integer vector index)))
125                (values error-number (sc-offsets))))))