1 // A WORD OF CAUTION
2 //
3 // This entire file basically needs to be kept in sync with itself. It's not
4 // really possible to modify just one bit of this file without understanding
5 // all the other bits. Documentation tries to reference various bits here and
6 // there but try to make sure to read over everything before tweaking things!
7 
8 use wasmtime_asm_macros::asm_func;
9 
10 // fn(top_of_stack(rdi): *mut u8)
11 asm_func!(
12     wasmtime_versioned_export_macros::versioned_stringify_ident!(wasmtime_fiber_switch),
13     "
14       // See https://github.com/rust-lang/rust/issues/80608.
15       .attribute arch, \"rv64gc\"
16 
17       // We're switching to arbitrary code somewhere else, so pessimistically
18       // assume that all callee-save register are clobbered. This means we need
19       // to save/restore all of them.
20       //
21       // Note that this order for saving is important since we use CFI directives
22       // below to point to where all the saved registers are.
23       sd ra,-0x8(sp)
24       sd fp,-0x10(sp)
25       sd s1,-0x18(sp)
26       sd s2,-0x20(sp)
27       sd s3,-0x28(sp)
28       sd s4,-0x30(sp)
29       sd s5,-0x38(sp)
30       sd s6,-0x40(sp)
31       sd s7,-0x48(sp)
32       sd s8,-0x50(sp)
33       sd s9,-0x58(sp)
34       sd s10,-0x60(sp)
35       sd s11,-0x68(sp)
36       fsd fs0,-0x70(sp)
37       fsd fs1,-0x78(sp)
38       fsd fs2,-0x80(sp)
39       fsd fs3,-0x88(sp)
40       fsd fs4,-0x90(sp)
41       fsd fs5,-0x98(sp)
42       fsd fs6,-0xa0(sp)
43       fsd fs7,-0xa8(sp)
44       fsd fs8,-0xb0(sp)
45       fsd fs9,-0xb8(sp)
46       fsd fs10,-0xc0(sp)
47       fsd fs11,-0xc8(sp)
48       addi sp , sp , -0xd0
49 
50       ld t0 ,-0x10(a0)
51       sd sp ,-0x10(a0)
52 
53       // Swap stacks and restore all our callee-saved registers
54       mv sp,t0
55 
56       fld fs11,0x8(sp)
57       fld fs10,0x10(sp)
58       fld fs9,0x18(sp)
59       fld fs8,0x20(sp)
60       fld fs7,0x28(sp)
61       fld fs6,0x30(sp)
62       fld fs5,0x38(sp)
63       fld fs4,0x40(sp)
64       fld fs3,0x48(sp)
65       fld fs2,0x50(sp)
66       fld fs1,0x58(sp)
67       fld fs0,0x60(sp)
68       ld s11,0x68(sp)
69       ld s10,0x70(sp)
70       ld s9,0x78(sp)
71       ld s8,0x80(sp)
72       ld s7,0x88(sp)
73       ld s6,0x90(sp)
74       ld s5,0x98(sp)
75       ld s4,0xa0(sp)
76       ld s3,0xa8(sp)
77       ld s2,0xb0(sp)
78       ld s1,0xb8(sp)
79       ld fp,0xc0(sp)
80       ld ra,0xc8(sp)
81       addi sp , sp , 0xd0
82       jr ra
83   ",
84 );
85 
86 // fn(
87 //    top_of_stack(a0): *mut u8,
88 //    entry_point(a1): extern fn(*mut u8, *mut u8),
89 //    entry_arg0(a2): *mut u8,
90 // )
91 #[rustfmt::skip]
92 asm_func!(
93     wasmtime_versioned_export_macros::versioned_stringify_ident!(wasmtime_fiber_init),
94     "
95       lla t0,{}
96       sd t0,-0x18(a0)  // ra,first should be wasmtime_fiber_start.
97       sd a0,-0x20(a0)  // fp pointer.
98       sd a1,-0x28(a0)  // entry_point will load to s1.
99       sd a2,-0x30(a0)  // entry_arg0 will load to s2.
100 
101       //
102       addi t0,a0,-0xe0
103       sd t0,-0x10(a0)
104       ret
105     ",
106     sym super::wasmtime_fiber_start,
107 );
108 
109 asm_func!(
110     wasmtime_versioned_export_macros::versioned_stringify_ident!(wasmtime_fiber_start),
111     "
112     .cfi_startproc simple
113     .cfi_def_cfa_offset 0
114 
115 
116     .cfi_escape 0x0f, /* DW_CFA_def_cfa_expression */ \
117       5,             /* the byte length of this expression */ \
118       0x52,          /* DW_OP_reg2 (sp) */ \
119       0x06,          /* DW_OP_deref */ \
120       0x08, 0xd0 ,   /* DW_OP_const1u 0xc8 */ \
121       0x22           /* DW_OP_plus */
122 
123 
124       .cfi_rel_offset ra,-0x8
125       .cfi_rel_offset fp,-0x10
126       .cfi_rel_offset s1,-0x18
127       .cfi_rel_offset s2,-0x20
128       .cfi_rel_offset s3,-0x28
129       .cfi_rel_offset s4,-0x30
130       .cfi_rel_offset s5,-0x38
131       .cfi_rel_offset s6,-0x40
132       .cfi_rel_offset s7,-0x48
133       .cfi_rel_offset s8,-0x50
134       .cfi_rel_offset s9,-0x58
135       .cfi_rel_offset s10,-0x60
136       .cfi_rel_offset s11,-0x68
137       .cfi_rel_offset fs0,-0x70
138       .cfi_rel_offset fs1,-0x78
139       .cfi_rel_offset fs2,-0x80
140       .cfi_rel_offset fs3,-0x88
141       .cfi_rel_offset fs4,-0x90
142       .cfi_rel_offset fs5,-0x98
143       .cfi_rel_offset fs6,-0xa0
144       .cfi_rel_offset fs7,-0xa8
145       .cfi_rel_offset fs8,-0xb0
146       .cfi_rel_offset fs9,-0xb8
147       .cfi_rel_offset fs10,-0xc0
148       .cfi_rel_offset fs11,-0xc8
149 
150       mv a0,s2
151       mv a1,fp
152       jalr s1
153       // .4byte 0 will cause panic.
154       // for safety just like x86_64.rs.
155       .4byte 0
156       .cfi_endproc
157   ",
158 );
159