forked from froggey/Mezzano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.lisp
332 lines (306 loc) · 14.5 KB
/
runtime.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
;;;; Copyright (c) 2011-2016 Henry Harrington <[email protected]>
;;;; This code is licensed under the MIT license.
(in-package :mezzano.runtime)
(defun sys.int::%%unwind-to (target-special-stack-pointer)
(declare (sys.int::suppress-ssp-checking))
(loop (when (eq target-special-stack-pointer (sys.int::%%special-stack-pointer))
(return))
(assert (sys.int::%%special-stack-pointer))
(etypecase (svref (sys.int::%%special-stack-pointer) 1)
(symbol
(sys.int::%%unbind))
(simple-vector
(sys.int::%%disestablish-block-or-tagbody))
(function
(sys.int::%%disestablish-unwind-protect)))))
(defvar *active-catch-handlers*)
(defun sys.int::%catch (tag fn)
;; Catch is used in low levelish code, so must avoid allocation.
(let ((vec (sys.c::make-dx-simple-vector 3)))
(setf (svref vec 0) *active-catch-handlers*
(svref vec 1) tag
(svref vec 2) (flet ((exit-fn (values)
(return-from sys.int::%catch (values-list values))))
(declare (dynamic-extent (function exit-fn)))
#'exit-fn))
(let ((*active-catch-handlers* vec))
(funcall fn))))
(defun sys.int::%throw (tag values)
;; Note! The VALUES list has dynamic extent!
;; This is fine, as the exit function calls VALUES-LIST on it before unwinding.
(do ((current *active-catch-handlers* (svref current 0)))
((not current)
(error 'sys.int::bad-catch-tag-error :tag tag))
(when (eq (svref current 1) tag)
(funcall (svref current 2) values))))
(defun sys.int::%coerce-to-callable (object)
(etypecase object
(function object)
(symbol
;; Fast-path for symbols.
(let ((fref (sys.int::%object-ref-t object sys.int::+symbol-function+)))
(when (not fref)
(return-from sys.int::%coerce-to-callable
(fdefinition object)))
(let ((fn (sys.int::%object-ref-t fref sys.int::+fref-function+)))
(if (sys.int::%undefined-function-p fn)
(fdefinition object)
fn))))))
(in-package :sys.int)
(defun return-address-to-function (return-address)
"Convert a return address to a function pointer.
Dangerous! The return address must be kept live as a return address on a
thread's stack if this function is called from normal code."
;; Return address must be within the pinned or wired area.
(assert (< return-address sys.int::*pinned-area-bump*))
;; Walk backwards looking for an object header with a function type and
;; an appropriate entry point.
(loop
with address = (logand return-address -16)
;; Be careful when reading to avoid bignums.
for potential-header-type = (ldb (byte +object-type-size+ +object-type-shift+)
(memref-unsigned-byte-8 address 0))
do
(when (and
;; Closures never contain code.
(or (eql potential-header-type +object-tag-function+)
(eql potential-header-type +object-tag-funcallable-instance+))
;; Check entry point halves individually, avoiding bignums.
;; Currently the entry point of every non-closure function
;; points to the base-address + 16.
(eql (logand (+ address 16) #xFFFFFFFF)
(memref-unsigned-byte-32 (+ address 8) 0))
(eql (logand (ash (+ address 16) -32) #xFFFFFFFF)
(memref-unsigned-byte-32 (+ address 12) 0)))
(return (values (%%assemble-value address sys.int::+tag-object+)
(- return-address address))))
(decf address 16)))
(defun map-function-gc-metadata (function function-to-inspect)
"Call FUNCTION with every GC metadata entry in FUNCTION-TO-INSPECT.
Arguments to FUNCTION:
start-offset
framep
interruptp
pushed-values
pushed-values-register
layout-address
layout-length
multiple-values
incoming-arguments
block-or-tagbody-thunk
extra-registers
restart"
(check-type function function)
(let* ((fn-address (logand (lisp-object-address function-to-inspect) -16))
(header-data (%object-header-data function-to-inspect))
(mc-size (* (ldb (byte +function-machine-code-size+
+function-machine-code-position+)
header-data)
16))
(n-constants (ldb (byte +function-constant-pool-size+
+function-constant-pool-position+)
header-data))
;; Address of GC metadata & the length.
(address (+ fn-address mc-size (* n-constants 8)))
(length (ldb (byte +function-gc-metadata-size+
+function-gc-metadata-position+)
header-data))
;; Position within the metadata.
(position 0))
(flet ((consume (&optional (errorp t))
(when (>= position length)
(when errorp
(mezzano.supervisor:panic "Corrupt GC info in function " function-to-inspect))
(return-from map-function-gc-metadata))
(prog1 (memref-unsigned-byte-8 address position)
(incf position))))
(declare (dynamic-extent #'consume))
(loop (let ((start-offset-in-function 0)
flags-and-pvr
mv-and-ia
(pv 0)
(n-layout-bits 0)
layout-address)
;; Read first byte of address, this is where we can terminate.
(let ((byte (consume nil))
(offset 0))
(setf start-offset-in-function (ldb (byte 7 0) byte)
offset 7)
(when (logtest byte #x80)
;; Read remaining bytes.
(loop (let ((byte (consume)))
(setf (ldb (byte 7 offset) start-offset-in-function)
(ldb (byte 7 0) byte))
(incf offset 7)
(unless (logtest byte #x80)
(return))))))
;; Read flag/pvr byte
(setf flags-and-pvr (consume))
;; Read mv-and-ia
(setf mv-and-ia (consume))
;; Read vs32 pv.
(let ((shift 0))
(loop
(let ((b (consume)))
(when (not (logtest b #x80))
(setf pv (logior pv (ash (logand b #x3F) shift)))
(when (logtest b #x40)
(setf pv (- pv)))
(return))
(setf pv (logior pv (ash (logand b #x7F) shift)))
(incf shift 7))))
;; Read vu32 n-layout bits.
(let ((shift 0))
(loop
(let ((b (consume)))
(setf n-layout-bits (logior n-layout-bits (ash (logand b #x7F) shift)))
(when (not (logtest b #x80))
(return))
(incf shift 7))))
(setf layout-address (+ address position))
;; Consume layout bits.
(incf position (ceiling n-layout-bits 8))
;; Decode this entry and do something else.
(funcall function
;; Start offset in the function.
start-offset-in-function
;; Frame/no-frame.
(logtest flags-and-pvr #b00001)
;; Interrupt.
(logtest flags-and-pvr #b00010)
;; Pushed-values.
pv
;; Pushed-values-register.
(if (logtest flags-and-pvr #b10000)
:rcx
nil)
;; Layout-address. Fixnum pointer to virtual memory
;; the inspected function must remain live to keep
;; this valid.
layout-address
;; Number of bits in the layout.
n-layout-bits
;; Multiple-values.
(if (eql (ldb (byte 4 0) mv-and-ia) 15)
nil
(ldb (byte 4 0) mv-and-ia))
;; Incoming-arguments.
(if (logtest flags-and-pvr #b1000)
(if (eql (ldb (byte 4 4) mv-and-ia) 15)
:rcx
(ldb (byte 4 4) mv-and-ia))
nil)
;; Block-or-tagbody-thunk.
(if (logtest flags-and-pvr #b0100)
:rax
nil)
;; Extra-registers.
(case (ldb (byte 2 6) flags-and-pvr)
(0 nil)
(1 :rax)
(2 :rax-rcx)
(3 :rax-rcx-rdx))
;; Restart
(logtest flags-and-pvr #b10000000)))))))
(declaim (inline memref-t (setf memref-t)))
(defun memref-t (base &optional (index 0))
(%memref-t base index))
(defun (setf memref-t) (value base &optional (index 0))
(setf (%memref-t base index) value))
(declaim (inline memref-unsigned-byte-8 (setf memref-unsigned-byte-8)))
(defun memref-unsigned-byte-8 (base &optional (index 0))
(%memref-unsigned-byte-8 base index))
(defun (setf memref-unsigned-byte-8) (value base &optional (index 0))
(setf (%memref-unsigned-byte-8 base index) value))
(declaim (inline memref-unsigned-byte-16 (setf memref-unsigned-byte-16)))
(defun memref-unsigned-byte-16 (base &optional (index 0))
(%memref-unsigned-byte-16 base index))
(defun (setf memref-unsigned-byte-16) (value base &optional (index 0))
(setf (%memref-unsigned-byte-16 base index) value))
(declaim (inline memref-unsigned-byte-32 (setf memref-unsigned-byte-32)))
(defun memref-unsigned-byte-32 (base &optional (index 0))
(%memref-unsigned-byte-32 base index))
(defun (setf memref-unsigned-byte-32) (value base &optional (index 0))
(setf (%memref-unsigned-byte-32 base index) value))
(declaim (inline memref-unsigned-byte-64 (setf memref-unsigned-byte-64)))
(defun memref-unsigned-byte-64 (base &optional (index 0))
(%memref-unsigned-byte-64 base index))
(defun (setf memref-unsigned-byte-64) (value base &optional (index 0))
(setf (%memref-unsigned-byte-64 base index) value))
(declaim (inline memref-signed-byte-8 (setf memref-signed-byte-8)))
(defun memref-signed-byte-8 (base &optional (index 0))
(%memref-signed-byte-8 base index))
(defun (setf memref-signed-byte-8) (value base &optional (index 0))
(setf (%memref-signed-byte-8 base index) value))
(declaim (inline memref-signed-byte-16 (setf memref-signed-byte-16)))
(defun memref-signed-byte-16 (base &optional (index 0))
(%memref-signed-byte-16 base index))
(defun (setf memref-signed-byte-16) (value base &optional (index 0))
(setf (%memref-signed-byte-16 base index) value))
(declaim (inline memref-signed-byte-32 (setf memref-signed-byte-32)))
(defun memref-signed-byte-32 (base &optional (index 0))
(%memref-signed-byte-32 base index))
(defun (setf memref-signed-byte-32) (value base &optional (index 0))
(setf (%memref-signed-byte-32 base index) value))
(declaim (inline memref-signed-byte-64 (setf memref-signed-byte-64)))
(defun memref-signed-byte-64 (base &optional (index 0))
(%memref-signed-byte-64 base index))
(defun (setf memref-signed-byte-64) (value base &optional (index 0))
(setf (%memref-signed-byte-64 base index) value))
(declaim (inline %object-ref-unsigned-byte-8 (setf %object-ref-unsigned-byte-8)))
(defun %object-ref-unsigned-byte-8 (object index)
(%%object-ref-unsigned-byte-8 object index))
(defun (setf %object-ref-unsigned-byte-8) (value object index)
(check-type value (unsigned-byte 8))
(setf (%%object-ref-unsigned-byte-8 object index) value))
(declaim (inline %object-ref-unsigned-byte-16 (setf %object-ref-unsigned-byte-16)))
(defun %object-ref-unsigned-byte-16 (object index)
(%%object-ref-unsigned-byte-16 object index))
(defun (setf %object-ref-unsigned-byte-16) (value object index)
(check-type value (unsigned-byte 16))
(setf (%%object-ref-unsigned-byte-16 object index) value))
(declaim (inline %object-ref-unsigned-byte-32 (setf %object-ref-unsigned-byte-32)))
(defun %object-ref-unsigned-byte-32 (object index)
(%%object-ref-unsigned-byte-32 object index))
(defun (setf %object-ref-unsigned-byte-32) (value object index)
(check-type value (unsigned-byte 32))
(setf (%%object-ref-unsigned-byte-32 object index) value))
(declaim (inline %object-ref-signed-byte-8 (setf %object-ref-signed-byte-8)))
(defun %object-ref-signed-byte-8 (object index)
(%%object-ref-signed-byte-8 object index))
(defun (setf %object-ref-signed-byte-8) (value object index)
(check-type value (signed-byte 8))
(setf (%%object-ref-signed-byte-8 object index) value))
(declaim (inline %object-ref-signed-byte-16 (setf %object-ref-signed-byte-16)))
(defun %object-ref-signed-byte-16 (object index)
(%%object-ref-signed-byte-16 object index))
(defun (setf %object-ref-signed-byte-16) (value object index)
(check-type value (signed-byte 16))
(setf (%%object-ref-signed-byte-16 object index) value))
(declaim (inline %object-ref-signed-byte-32 (setf %object-ref-signed-byte-32)))
(defun %object-ref-signed-byte-32 (object index)
(%%object-ref-signed-byte-32 object index))
(defun (setf %object-ref-signed-byte-32) (value object index)
(check-type value (signed-byte 32))
(setf (%%object-ref-signed-byte-32 object index) value))
(declaim (inline %object-ref-single-float (setf %object-ref-single-float)))
(defun %object-ref-single-float (object index)
(%%object-ref-single-float object index))
(defun (setf %object-ref-single-float) (value object index)
(check-type value single-float)
(setf (%%object-ref-single-float object index) value)
value)
(declaim (inline %%object-ref-single-float (setf %%object-ref-single-float)))
(defun %%object-ref-single-float (object index)
(%integer-as-single-float (%%object-ref-unsigned-byte-32 object index)))
(defun (setf %%object-ref-single-float) (value object index)
(setf (%%object-ref-unsigned-byte-32 object index)
(%single-float-as-integer value))
value)
(declaim (inline %object-ref-double-float (setf %object-ref-double-float)))
(defun %object-ref-double-float (object index)
(%integer-as-double-float (%object-ref-unsigned-byte-64 object index)))
(defun (setf %object-ref-double-float) (value object index)
(setf (%object-ref-unsigned-byte-64 object index)
(%double-float-as-integer value))
value)