Skip to content

Commit f7d14ef

Browse files
authored
Fix Asm aarch64 bugs (#305)
* Fix Asm aarch64 bug, RS2 occupies x18 register * Fix Asm aarch64 bug, Version is u32, but read out of bounds * Asm aarch64: x18 registers are not being pushed onto the stack * Asm aarch64: modify the register call number for easy debugging * Asm aarch64: reassign register * Asm aarch64: Handling PREPCALL and POSTCALL
1 parent c551414 commit f7d14ef

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

src/machine/asm/execute_aarch64.S

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,32 @@
66

77
#define MACHINE x0
88

9-
#define TEMP1 x8
10-
#define TEMP1w w8
11-
#define TEMP2 x9
12-
#define TEMP2w w9
13-
#define TEMP3 x10
14-
#define TEMP3w w10
15-
#define TEMP4 x11
16-
#define TEMP4w w11
17-
#define TEMP5 x12
18-
#define TEMP5w w12
19-
#define TRACE x13
20-
#define INST_PC x14
21-
#define INST_ARGS x15
9+
#define TEMP1 x9
10+
#define TEMP1w w9
11+
#define TEMP2 x10
12+
#define TEMP2w w10
13+
#define TEMP3 x11
14+
#define TEMP3w w11
15+
#define TEMP4 x12
16+
#define TEMP4w w12
17+
#define TEMP5 x13
18+
#define TEMP5w w13
19+
#define TRACE x19
20+
#define INST_PC x20
21+
#define INST_ARGS x21
2222

23-
#define RD x16
24-
#define RS1 x17
25-
#define RS1w w17
26-
#define RS2 x18
27-
#define RS2w w18
28-
#define RS3 x19
29-
#define RS3w w19
30-
#define IMMEDIATE x20
31-
#define IMMEDIATEw w20
32-
#define FLAG x21
23+
#define RD x22
24+
#define RS1 x23
25+
#define RS1w w23
26+
#define RS2 x24
27+
#define RS2w w24
28+
#define RS3 x25
29+
#define RS3w w25
30+
#define IMMEDIATE x26
31+
#define IMMEDIATEw w26
32+
#define FLAG x27
3333

34-
#define REGISTER_BASE x22
34+
#define REGISTER_BASE x28
3535
#define ZERO_VALUE xzr
3636

3737
#ifdef __APPLE__
@@ -41,20 +41,14 @@
4141
#endif
4242

4343
#define PREPCALL \
44-
stp x0, x8, [sp, -96]! SEP \
45-
stp x9, x10, [sp, 16] SEP \
46-
stp x11, x12, [sp, 32] SEP \
47-
stp x13, x14, [sp, 48] SEP \
48-
stp x15, x16, [sp, 64] SEP \
49-
stp x17, x18, [sp, 80]
44+
stp x0, x9, [sp, -48]! SEP \
45+
stp x10, x11, [sp, 16] SEP \
46+
stp x12, x13, [sp, 32]
5047

5148
#define POSTCALL \
52-
ldp x17, x18, [sp, 80] SEP \
53-
ldp x15, x16, [sp, 64] SEP \
54-
ldp x13, x14, [sp, 48] SEP \
55-
ldp x11, x12, [sp, 32] SEP \
56-
ldp x9, x10, [sp, 16] SEP \
57-
ldp x0, x8, [sp], 96
49+
ldp x12, x13, [sp, 32] SEP \
50+
ldp x10, x11, [sp, 16] SEP \
51+
ldp x0, x9, [sp], 48
5852

5953
#define REGISTER_ADDRESS(r) [REGISTER_BASE, r, lsl 3]
6054
#define ZERO_ADDRESS [REGISTER_BASE]
@@ -63,9 +57,9 @@
6357
#define PC_ADDRESS [MACHINE, CKB_VM_ASM_ASM_CORE_MACHINE_OFFSET_PC]
6458
#define VERSION_ADDRESS [MACHINE, CKB_VM_ASM_ASM_CORE_MACHINE_OFFSET_VERSION]
6559

66-
#define LOAD_VERSION(r) \
60+
#define LOAD_VERSION(r, rw) \
6761
add r, MACHINE, CKB_VM_ASM_ASM_CORE_MACHINE_OFFSET_VERSION SEP \
68-
ldr r, [r]
62+
ldr rw, [r]
6963

7064
#define WRITE_RD(v) \
7165
str v, REGISTER_ADDRESS(RD) SEP \
@@ -90,9 +84,9 @@
9084
asr TEMP1, TEMP1, 24 SEP \
9185
ubfx FLAG, TEMP1, 0, 8 SEP \
9286
asr TEMP1, TEMP1, 8 SEP \
93-
ldr x9, [INST_PC] SEP \
87+
ldr TEMP2, [INST_PC] SEP \
9488
add INST_PC, INST_PC, 8 SEP \
95-
br x9
89+
br TEMP2
9690

9791
#define DECODE_R \
9892
ubfx RS1, TEMP1, 0, 8 SEP \
@@ -242,9 +236,12 @@ _ckb_vm_x64_execute:
242236
.globl ckb_vm_x64_execute
243237
ckb_vm_x64_execute:
244238
#endif
245-
stp x19, x20, [sp, -48]!
239+
stp x19, x20, [sp, -96]!
246240
stp x21, x22, [sp, 16]
247-
stp x23, x30, [sp, 32]
241+
stp x23, x24, [sp, 32]
242+
stp x25, x26, [sp, 48]
243+
stp x27, x28, [sp, 64]
244+
stp x29, x30, [sp, 80]
248245
add REGISTER_BASE, MACHINE, CKB_VM_ASM_ASM_CORE_MACHINE_OFFSET_REGISTERS
249246

250247
.CKB_VM_ASM_LABEL_OP_CUSTOM_TRACE_END:
@@ -494,8 +491,8 @@ ckb_vm_x64_execute:
494491
str RS1, PC_ADDRESS
495492
b .prepare_trace
496493
.CKB_VM_ASM_LABEL_OP_JALR:
497-
LOAD_VERSION(TEMP5)
498-
cmp TEMP5, 1
494+
LOAD_VERSION(TEMP5, TEMP5w)
495+
cmp TEMP5w, 1
499496
bge .CKB_VM_ASM_LABEL_OP_JALR_VERSION1
500497
b .CKB_VM_ASM_LABEL_OP_JALR_VERSION0
501498
.CKB_VM_ASM_LABEL_OP_JALR_VERSION0:
@@ -517,8 +514,8 @@ ckb_vm_x64_execute:
517514
str TEMP2, PC_ADDRESS
518515
b .prepare_trace
519516
.CKB_VM_ASM_LABEL_OP_LB:
520-
LOAD_VERSION(TEMP5)
521-
cmp TEMP5, 1
517+
LOAD_VERSION(TEMP5, TEMP5w)
518+
cmp TEMP5w, 1
522519
bge .CKB_VM_ASM_LABEL_OP_LB_VERSION1
523520
b .CKB_VM_ASM_LABEL_OP_LB_VERSION0
524521
.CKB_VM_ASM_LABEL_OP_LB_VERSION0:
@@ -538,8 +535,8 @@ ckb_vm_x64_execute:
538535
WRITE_RD(RS1)
539536
NEXT_INST
540537
.CKB_VM_ASM_LABEL_OP_LBU:
541-
LOAD_VERSION(TEMP5)
542-
cmp TEMP5, 1
538+
LOAD_VERSION(TEMP5, TEMP5w)
539+
cmp TEMP5w, 1
543540
bge .CKB_VM_ASM_LABEL_OP_LBU_VERSION1
544541
b .CKB_VM_ASM_LABEL_OP_LBU_VERSION0
545542
.CKB_VM_ASM_LABEL_OP_LBU_VERSION0:
@@ -559,8 +556,8 @@ ckb_vm_x64_execute:
559556
WRITE_RD(RS1)
560557
NEXT_INST
561558
.CKB_VM_ASM_LABEL_OP_LD:
562-
LOAD_VERSION(TEMP5)
563-
cmp TEMP5, 1
559+
LOAD_VERSION(TEMP5, TEMP5w)
560+
cmp TEMP5w, 1
564561
bge .CKB_VM_ASM_LABEL_OP_LD_VERSION1
565562
b .CKB_VM_ASM_LABEL_OP_LD_VERSION0
566563
.CKB_VM_ASM_LABEL_OP_LD_VERSION0:
@@ -580,8 +577,8 @@ ckb_vm_x64_execute:
580577
WRITE_RD(RS1)
581578
NEXT_INST
582579
.CKB_VM_ASM_LABEL_OP_LH:
583-
LOAD_VERSION(TEMP5)
584-
cmp TEMP5, 1
580+
LOAD_VERSION(TEMP5, TEMP5w)
581+
cmp TEMP5w, 1
585582
bge .CKB_VM_ASM_LABEL_OP_LH_VERSION1
586583
b .CKB_VM_ASM_LABEL_OP_LH_VERSION0
587584
.CKB_VM_ASM_LABEL_OP_LH_VERSION0:
@@ -601,8 +598,8 @@ ckb_vm_x64_execute:
601598
WRITE_RD(RS1)
602599
NEXT_INST
603600
.CKB_VM_ASM_LABEL_OP_LHU:
604-
LOAD_VERSION(TEMP5)
605-
cmp TEMP5, 1
601+
LOAD_VERSION(TEMP5, TEMP5w)
602+
cmp TEMP5w, 1
606603
bge .CKB_VM_ASM_LABEL_OP_LHU_VERSION1
607604
b .CKB_VM_ASM_LABEL_OP_LHU_VERSION0
608605
.CKB_VM_ASM_LABEL_OP_LHU_VERSION0:
@@ -632,8 +629,8 @@ ckb_vm_x64_execute:
632629
WRITE_RD(IMMEDIATE)
633630
NEXT_INST
634631
.CKB_VM_ASM_LABEL_OP_LW:
635-
LOAD_VERSION(TEMP5)
636-
cmp TEMP5, 1
632+
LOAD_VERSION(TEMP5, TEMP5w)
633+
cmp TEMP5w, 1
637634
bge .CKB_VM_ASM_LABEL_OP_LW_VERSION1
638635
b .CKB_VM_ASM_LABEL_OP_LW_VERSION0
639636
.CKB_VM_ASM_LABEL_OP_LW_VERSION0:
@@ -653,8 +650,8 @@ ckb_vm_x64_execute:
653650
WRITE_RD(RS1)
654651
NEXT_INST
655652
.CKB_VM_ASM_LABEL_OP_LWU:
656-
LOAD_VERSION(TEMP5)
657-
cmp TEMP5, 1
653+
LOAD_VERSION(TEMP5, TEMP5w)
654+
cmp TEMP5w, 1
658655
bge .CKB_VM_ASM_LABEL_OP_LWU_VERSION1
659656
b .CKB_VM_ASM_LABEL_OP_LWU_VERSION0
660657
.CKB_VM_ASM_LABEL_OP_LWU_VERSION0:
@@ -1612,7 +1609,10 @@ ckb_vm_x64_execute:
16121609
mov x0, CKB_VM_ASM_RET_SLOWPATH
16131610
b .exit
16141611
.exit:
1615-
ldp x23, x30, [sp, 32]
1612+
ldp x29, x30, [sp, 80]
1613+
ldp x27, x28, [sp, 64]
1614+
ldp x25, x26, [sp, 48]
1615+
ldp x23, x24, [sp, 32]
16161616
ldp x21, x22, [sp, 16]
1617-
ldp x19, x20, [sp], 48
1617+
ldp x19, x20, [sp], 96
16181618
ret

0 commit comments

Comments
 (0)