loongarch_vcpu 0.5.5

LoongArch VCPU implementation for ArceOS Hypervisor
Documentation
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
.section .text

.macro SAVE_GUEST_REGS
    st.d    $r0,  $sp, 0
    st.d    $r1,  $sp, 8
    st.d    $r2,  $sp, 16
    csrrd   $t0, 0x502
    st.d    $t0, $sp, 24
    st.d    $r4,  $sp, 32
    st.d    $r5,  $sp, 40
    st.d    $r6,  $sp, 48
    st.d    $r7,  $sp, 56
    st.d    $r8,  $sp, 64
    st.d    $r9,  $sp, 72
    st.d    $r10, $sp, 80
    st.d    $r11, $sp, 88
    st.d    $r12, $sp, 96
    st.d    $r13, $sp, 104
    st.d    $r14, $sp, 112
    st.d    $r15, $sp, 120
    st.d    $r16, $sp, 128
    st.d    $r17, $sp, 136
    st.d    $r18, $sp, 144
    st.d    $r19, $sp, 152
    st.d    $r20, $sp, 160
    st.d    $r21, $sp, 168
    st.d    $r22, $sp, 176
    st.d    $r23, $sp, 184
    st.d    $r24, $sp, 192
    st.d    $r25, $sp, 200
    st.d    $r26, $sp, 208
    st.d    $r27, $sp, 216
    st.d    $r28, $sp, 224
    st.d    $r29, $sp, 232
    st.d    $r30, $sp, 240
    st.d    $r31, $sp, 248

    csrrd   $t0, 0x6
    st.d    $t0, $sp, 256
    gcsrrd  $t0, 0x0
    st.d    $t0, $sp, 264
    gcsrrd  $t0, 0x1
    st.d    $t0, $sp, 272
    gcsrrd  $t0, 0x2
    st.d    $t0, $sp, 280
    gcsrrd  $t0, 0x3
    st.d    $t0, $sp, 288
    gcsrrd  $t0, 0x4
    st.d    $t0, $sp, 296
    gcsrrd  $t0, 0x5
    st.d    $t0, $sp, 304
    gcsrrd  $t0, 0x6
    st.d    $t0, $sp, 312
    gcsrrd  $t0, 0x7
    st.d    $t0, $sp, 320
    gcsrrd  $t0, 0x8
    st.d    $t0, $sp, 328
    gcsrrd  $t0, 0xc
    st.d    $t0, $sp, 336
    gcsrrd  $t0, 0x10
    st.d    $t0, $sp, 344
    gcsrrd  $t0, 0x11
    st.d    $t0, $sp, 352
    gcsrrd  $t0, 0x12
    st.d    $t0, $sp, 360
    gcsrrd  $t0, 0x13
    st.d    $t0, $sp, 368
    gcsrrd  $t0, 0x18
    st.d    $t0, $sp, 376
    gcsrrd  $t0, 0x19
    st.d    $t0, $sp, 384
    gcsrrd  $t0, 0x1a
    st.d    $t0, $sp, 392
    gcsrrd  $t0, 0x1b
    st.d    $t0, $sp, 400
    gcsrrd  $t0, 0x1c
    st.d    $t0, $sp, 408
    gcsrrd  $t0, 0x1d
    st.d    $t0, $sp, 416
    gcsrrd  $t0, 0x1e
    st.d    $t0, $sp, 424
    gcsrrd  $t0, 0x1f
    st.d    $t0, $sp, 432
    gcsrrd  $t0, 0x20
    st.d    $t0, $sp, 440
    gcsrrd  $t0, 0x21
    st.d    $t0, $sp, 448
    gcsrrd  $t0, 0x22
    st.d    $t0, $sp, 456
    gcsrrd  $t0, 0x23
    st.d    $t0, $sp, 464
    gcsrrd  $t0, 0x30
    st.d    $t0, $sp, 472
    gcsrrd  $t0, 0x31
    st.d    $t0, $sp, 480
    gcsrrd  $t0, 0x32
    st.d    $t0, $sp, 488
    gcsrrd  $t0, 0x33
    st.d    $t0, $sp, 496
    gcsrrd  $t0, 0x34
    st.d    $t0, $sp, 504
    gcsrrd  $t0, 0x35
    st.d    $t0, $sp, 512
    gcsrrd  $t0, 0x36
    st.d    $t0, $sp, 520
    gcsrrd  $t0, 0x37
    st.d    $t0, $sp, 528
    gcsrrd  $t0, 0x38
    st.d    $t0, $sp, 536
    gcsrrd  $t0, 0x39
    st.d    $t0, $sp, 544
    gcsrrd  $t0, 0x3a
    st.d    $t0, $sp, 552
    gcsrrd  $t0, 0x3b
    st.d    $t0, $sp, 560
    gcsrrd  $t0, 0x3c
    st.d    $t0, $sp, 568
    gcsrrd  $t0, 0x3d
    st.d    $t0, $sp, 576
    gcsrrd  $t0, 0x3e
    st.d    $t0, $sp, 584
    gcsrrd  $t0, 0x3f
    st.d    $t0, $sp, 592
    gcsrrd  $t0, 0x40
    st.d    $t0, $sp, 600
    gcsrrd  $t0, 0x41
    st.d    $t0, $sp, 608
    gcsrrd  $t0, 0x42
    st.d    $t0, $sp, 616
    gcsrrd  $t0, 0x43
    st.d    $t0, $sp, 624
    gcsrrd  $t0, 0x44
    st.d    $t0, $sp, 632
    gcsrrd  $t0, 0x60
    st.d    $t0, $sp, 640
    gcsrrd  $t0, 0x88
    st.d    $t0, $sp, 648
    gcsrrd  $t0, 0x89
    st.d    $t0, $sp, 656
    gcsrrd  $t0, 0x8a
    st.d    $t0, $sp, 664
    gcsrrd  $t0, 0x8b
    st.d    $t0, $sp, 672
    gcsrrd  $t0, 0x8c
    st.d    $t0, $sp, 680
    gcsrrd  $t0, 0x8d
    st.d    $t0, $sp, 688
    gcsrrd  $t0, 0x8e
    st.d    $t0, $sp, 696
    gcsrrd  $t0, 0x8f
    st.d    $t0, $sp, 704
    gcsrrd  $t0, 0x180
    st.d    $t0, $sp, 712
    gcsrrd  $t0, 0x181
    st.d    $t0, $sp, 720
    gcsrrd  $t0, 0x182
    st.d    $t0, $sp, 728
    gcsrrd  $t0, 0x183
    st.d    $t0, $sp, 736

    csrrd   $t0, 0x5
    st.d    $t0, $sp, 744
    csrrd   $t0, 0x6
    st.d    $t0, $sp, 752
    csrrd   $t0, 0x7
    st.d    $t0, $sp, 760
    csrrd   $t0, 0x8
    st.d    $t0, $sp, 768
    csrrd   $t0, 0x89
    st.d    $t0, $sp, 776
    csrrd   $t0, 0x8a
    st.d    $t0, $sp, 784
.endm

.macro RESTORE_GUEST_REGS
    ld.d    $t0, $sp, 24
    csrwr   $t0, 0x502

    ld.d    $t0, $sp, 256
    csrwr   $t0, 0x6
    ld.d    $t0, $sp, 264
    gcsrwr  $t0, 0x0
    ld.d    $t0, $sp, 272
    gcsrwr  $t0, 0x1
    ld.d    $t0, $sp, 280
    gcsrwr  $t0, 0x2
    ld.d    $t0, $sp, 288
    gcsrwr  $t0, 0x3
    ld.d    $t0, $sp, 296
    gcsrwr  $t0, 0x4
    ld.d    $t0, $sp, 304
    gcsrwr  $t0, 0x5
    ld.d    $t0, $sp, 312
    gcsrwr  $t0, 0x6
    ld.d    $t0, $sp, 320
    gcsrwr  $t0, 0x7
    ld.d    $t0, $sp, 328
    gcsrwr  $t0, 0x8
    ld.d    $t0, $sp, 336
    gcsrwr  $t0, 0xc
    ld.d    $t0, $sp, 344
    gcsrwr  $t0, 0x10
    ld.d    $t0, $sp, 352
    gcsrwr  $t0, 0x11
    ld.d    $t0, $sp, 360
    gcsrwr  $t0, 0x12
    ld.d    $t0, $sp, 368
    gcsrwr  $t0, 0x13
    ld.d    $t0, $sp, 376
    gcsrwr  $t0, 0x18
    ld.d    $t0, $sp, 384
    gcsrwr  $t0, 0x19
    ld.d    $t0, $sp, 392
    gcsrwr  $t0, 0x1a
    ld.d    $t0, $sp, 400
    gcsrwr  $t0, 0x1b
    ld.d    $t0, $sp, 408
    gcsrwr  $t0, 0x1c
    ld.d    $t0, $sp, 416
    gcsrwr  $t0, 0x1d
    ld.d    $t0, $sp, 424
    gcsrwr  $t0, 0x1e
    ld.d    $t0, $sp, 432
    gcsrwr  $t0, 0x1f
    ld.d    $t0, $sp, 440
    gcsrwr  $t0, 0x20
    ld.d    $t0, $sp, 448
    gcsrwr  $t0, 0x21
    ld.d    $t0, $sp, 456
    gcsrwr  $t0, 0x22
    ld.d    $t0, $sp, 464
    gcsrwr  $t0, 0x23
    ld.d    $t0, $sp, 472
    gcsrwr  $t0, 0x30
    ld.d    $t0, $sp, 480
    gcsrwr  $t0, 0x31
    ld.d    $t0, $sp, 488
    gcsrwr  $t0, 0x32
    ld.d    $t0, $sp, 496
    gcsrwr  $t0, 0x33
    ld.d    $t0, $sp, 504
    gcsrwr  $t0, 0x34
    ld.d    $t0, $sp, 512
    gcsrwr  $t0, 0x35
    ld.d    $t0, $sp, 520
    gcsrwr  $t0, 0x36
    ld.d    $t0, $sp, 528
    gcsrwr  $t0, 0x37
    ld.d    $t0, $sp, 536
    gcsrwr  $t0, 0x38
    ld.d    $t0, $sp, 544
    gcsrwr  $t0, 0x39
    ld.d    $t0, $sp, 552
    gcsrwr  $t0, 0x3a
    ld.d    $t0, $sp, 560
    gcsrwr  $t0, 0x3b
    ld.d    $t0, $sp, 568
    gcsrwr  $t0, 0x3c
    ld.d    $t0, $sp, 576
    gcsrwr  $t0, 0x3d
    ld.d    $t0, $sp, 584
    gcsrwr  $t0, 0x3e
    ld.d    $t0, $sp, 592
    gcsrwr  $t0, 0x3f
    ld.d    $t0, $sp, 600
    gcsrwr  $t0, 0x40
    ld.d    $t0, $sp, 608
    gcsrwr  $t0, 0x41
    ld.d    $t0, $sp, 616
    gcsrwr  $t0, 0x42
    ld.d    $t0, $sp, 624
    gcsrwr  $t0, 0x43
    ld.d    $t0, $sp, 632
    gcsrwr  $t0, 0x44
    ld.d    $t0, $sp, 640
    gcsrwr  $t0, 0x60
    ld.d    $t0, $sp, 648
    gcsrwr  $t0, 0x88
    ld.d    $t0, $sp, 656
    gcsrwr  $t0, 0x89
    ld.d    $t0, $sp, 664
    gcsrwr  $t0, 0x8a
    ld.d    $t0, $sp, 672
    gcsrwr  $t0, 0x8b
    ld.d    $t0, $sp, 680
    gcsrwr  $t0, 0x8c
    ld.d    $t0, $sp, 688
    gcsrwr  $t0, 0x8d
    ld.d    $t0, $sp, 696
    gcsrwr  $t0, 0x8e
    ld.d    $t0, $sp, 704
    gcsrwr  $t0, 0x8f
    ld.d    $t0, $sp, 712
    gcsrwr  $t0, 0x180
    ld.d    $t0, $sp, 720
    gcsrwr  $t0, 0x181
    ld.d    $t0, $sp, 728
    gcsrwr  $t0, 0x182
    ld.d    $t0, $sp, 736
    gcsrwr  $t0, 0x183

    ld.d    $r1,  $sp, 8
    ld.d    $r2,  $sp, 16
    ld.d    $r4,  $sp, 32
    ld.d    $r5,  $sp, 40
    ld.d    $r6,  $sp, 48
    ld.d    $r7,  $sp, 56
    ld.d    $r8,  $sp, 64
    ld.d    $r9,  $sp, 72
    ld.d    $r10, $sp, 80
    ld.d    $r11, $sp, 88
    ld.d    $r12, $sp, 96
    ld.d    $r13, $sp, 104
    ld.d    $r14, $sp, 112
    ld.d    $r15, $sp, 120
    ld.d    $r16, $sp, 128
    ld.d    $r17, $sp, 136
    ld.d    $r18, $sp, 144
    ld.d    $r19, $sp, 152
    ld.d    $r20, $sp, 160
    ld.d    $r21, $sp, 168
    ld.d    $r22, $sp, 176
    ld.d    $r23, $sp, 184
    ld.d    $r24, $sp, 192
    ld.d    $r25, $sp, 200
    ld.d    $r26, $sp, 208
    ld.d    $r27, $sp, 216
    ld.d    $r28, $sp, 224
    ld.d    $r29, $sp, 232
    ld.d    $r30, $sp, 240
    ld.d    $r31, $sp, 248
    ld.d    $r0,  $sp, 0
    csrwr   $sp, 0x502
.endm

.global _run_guest
_run_guest:
    move    $t1, $ra
    addi.d  $t0, $a0, 792
    csrwr   $t0, 0x33
    move    $sp, $a0
    RESTORE_GUEST_REGS
    ertn
    jr      $t1

.global _guest_exit
_guest_exit:
    b       vmexit_trampoline

.section .vectors, "ax"
.align 12
.global _exception_vectors
_exception_vectors:
    .org 0x0
    b       _irq_handler_vector
    .org 0x80
    b       _sync_handler_vector
    .org 0x100
    b       _sync_handler_vector
    .org 0x180
    b       _sync_handler_vector
    .org 0x200
    b       _sync_handler_vector
    .org 0x280
    b       _sync_handler_vector
    .org 0x400
    b       _sync_handler_vector
    .org 0x480
    b       _sync_handler_vector
    .org 0x500
    b       _sync_handler_vector
    .org 0x580
    b       _sync_handler_vector
    .org 0x600
    b       _sync_handler_vector
    .org 0x680
    b       _sync_handler_vector
    .org 0x700
    b       _sync_handler_vector
    .org 0x780
    b       _sync_handler_vector
    .org 0x800
    b       _sync_handler_vector
    .org 0x880
    b       _sync_handler_vector
    .org 0x900
    b       _sync_handler_vector
    .org 0x980
    b       _sync_handler_vector
    .org 0xa00
    b       _sync_handler_vector
    .org 0xa80
    b       _sync_handler_vector
    .org 0xb00
    b       _hvc_handler_vector
    .org 0xb80
    b       _sync_handler_vector
    .org 0xc00
    b       _sync_handler_vector
    .org 0xc80
    b       _sync_handler_vector
    .org 0xd00
    b       _sync_handler_vector
    .org 0xd80
    b       _sync_handler_vector

_irq_handler_vector:
    csrwr   $sp, 0x502
    csrrd   $sp, 0x33
    addi.d  $sp, $sp, -792
    SAVE_GUEST_REGS
    li.d    $a0, 1
    b       _guest_exit

_sync_handler_vector:
    csrwr   $sp, 0x502
    csrrd   $sp, 0x33
    addi.d  $sp, $sp, -792
    SAVE_GUEST_REGS
    li.d    $a0, 0
    b       _guest_exit

_hvc_handler_vector:
    csrwr   $sp, 0x502
    csrrd   $sp, 0x33
    addi.d  $sp, $sp, -792
    SAVE_GUEST_REGS
    li.d    $a0, 0
    b       _guest_exit