7e1e570b908c41a7f2a9b7be7f47b64b116580e9
[riscv-tests.git] / env / v / vm.c
1 #include <stdint.h>
2 #include <string.h>
3 #include <stdio.h>
4
5 #include "riscv_test.h"
6
7 void trap_entry();
8 void pop_tf(trapframe_t*);
9
10 static void cputchar(int x)
11 {
12 while (mtpcr(PCR_TOHOST, 0x0101000000000000 | (unsigned char)x));
13 }
14
15 static void cputstring(const char* s)
16 {
17 while(*s)
18 cputchar(*s++);
19 cputchar('\n');
20 }
21
22 static void terminate(int code)
23 {
24 while (mtpcr(PCR_TOHOST, code));
25 }
26
27 #define stringify1(x) #x
28 #define stringify(x) stringify1(x)
29 #define assert(x) do { \
30 if (x) break; \
31 cputstring("Assertion failed: " stringify(x)); \
32 terminate(3); \
33 while(1); \
34 } while(0)
35
36 #define RELOC(x) ((typeof(x))((char*)(x) + (PGSIZE*MAX_TEST_PAGES)))
37
38 typedef struct { pte_t addr; void* next; } freelist_t;
39
40 pte_t l1pt[PTES_PER_PT] __attribute__((aligned(PGSIZE)));
41 pte_t l2pt[PTES_PER_PT] __attribute__((aligned(PGSIZE)));
42 pte_t l3pt[PTES_PER_PT] __attribute__((aligned(PGSIZE)));
43 freelist_t user_mapping[MAX_TEST_PAGES];
44 freelist_t freelist_nodes[MAX_TEST_PAGES];
45 freelist_t *freelist_head, *freelist_tail;
46
47 void printhex(uint64_t x)
48 {
49 char str[17];
50 for (int i = 0; i < 16; i++)
51 {
52 str[15-i] = (x & 0xF) + ((x & 0xF) < 10 ? '0' : 'a'-10);
53 x >>= 4;
54 }
55 str[16] = 0;
56
57 cputstring(str);
58 }
59
60 void evict(unsigned long addr)
61 {
62 assert(addr >= PGSIZE && addr < RELOC(0L));
63 addr = addr/PGSIZE*PGSIZE;
64
65 freelist_t* node = RELOC(&user_mapping[addr/PGSIZE]);
66 if (node->addr)
67 {
68 memcpy((void*)RELOC(addr), (void*)addr, PGSIZE);
69 RELOC(&user_mapping[addr/PGSIZE])->addr = 0;
70
71 if (*RELOC(&freelist_tail) == 0)
72 *RELOC(&freelist_head) = *RELOC(&freelist_tail) = node;
73 else
74 {
75 (*RELOC(&freelist_tail))->next = node;
76 *RELOC(&freelist_tail) = node;
77 }
78 }
79 }
80
81 void handle_fault(unsigned long addr)
82 {
83 assert(addr >= PGSIZE && addr < RELOC(0L));
84 addr = addr/PGSIZE*PGSIZE;
85
86 freelist_t* node = *RELOC(&freelist_head);
87 assert(node);
88 *RELOC(&freelist_head) = node->next;
89 if (*RELOC(&freelist_head) == *RELOC(&freelist_tail))
90 *RELOC(&freelist_tail) = 0;
91
92 *RELOC(&l3pt[addr/PGSIZE]) = node->addr | 0x3F2;
93 mtpcr(PCR_PTBR, l1pt);
94
95 assert(RELOC(&user_mapping[addr/PGSIZE])->addr == 0);
96 *RELOC(&user_mapping[addr/PGSIZE]) = *node;
97 memcpy((void*)addr, (void*)RELOC(addr), PGSIZE);
98
99 __builtin___clear_cache(0,0);
100 }
101
102 void emulate_vxcptsave(trapframe_t* tf)
103 {
104 long where = tf->gpr[(tf->insn >> 22) & 0x1F];
105
106 asm volatile ("vxcptevac %0" : : "r"(where));
107 fencevl();
108 }
109
110 void do_vxcptrestore(long* where)
111 {
112 vxcpthold();
113
114 int idx = 0;
115 long dword, cmd, pf;
116 int first = 1;
117
118 while (1)
119 {
120 dword = where[idx++];
121
122 if (dword < 0) break;
123
124 if (dword_bit_cnt(dword))
125 {
126 venqcnt(dword, pf | (dword_bit_cmd(where[idx]) << 1));
127 }
128 else
129 {
130 if (!first)
131 {
132 venqcmd(cmd, pf);
133 }
134
135 first = 0;
136 cmd = dword;
137 pf = dword_bit_pf(cmd);
138
139 if (dword_bit_imm1(cmd))
140 {
141 venqimm1(where[idx++], pf);
142 }
143 if (dword_bit_imm2(cmd))
144 {
145 venqimm2(where[idx++], pf);
146 }
147 }
148 }
149 if (!first)
150 {
151 venqcmd(cmd, pf);
152 }
153 }
154
155 void emulate_vxcptrestore(trapframe_t* tf)
156 {
157 long* where = (long*)tf->gpr[(tf->insn >> 22) & 0x1F];
158 vxcptkill();
159 vcfg(tf->veccfg);
160 do_vxcptrestore(where);
161 }
162
163 void restore_vector(trapframe_t* tf)
164 {
165 mtpcr(PCR_VECBANK, tf->vecbank);
166 vcfg(tf->veccfg);
167
168 if (mfpcr(PCR_IMPL) == IMPL_ROCKET)
169 do_vxcptrestore(tf->evac);
170 else
171 asm volatile("vxcptrestore %0" : : "r"(tf->evac) : "memory");
172 }
173
174 void handle_trap(trapframe_t* tf)
175 {
176 if (tf->cause == CAUSE_SYSCALL)
177 {
178 int n = tf->gpr[18];
179
180 for (long i = 1; i < MAX_TEST_PAGES; i++)
181 evict(i*PGSIZE);
182
183 terminate(n);
184 while(1);
185 }
186 else if (tf->cause == CAUSE_FAULT_FETCH)
187 handle_fault(tf->epc);
188 else if (tf->cause == CAUSE_ILLEGAL_INSTRUCTION)
189 {
190 if ((tf->insn & 0xF83FFFFF) == 0x37B)
191 emulate_vxcptsave(tf);
192 else if ((tf->insn & 0xF83FFFFF) == 0x77B)
193 emulate_vxcptrestore(tf);
194 else
195 assert(0);
196 tf->epc += 4;
197 }
198 else if (tf->cause == CAUSE_FAULT_LOAD || tf->cause == CAUSE_FAULT_STORE ||
199 tf->cause == CAUSE_VECTOR_FAULT_LOAD || tf->cause == CAUSE_VECTOR_FAULT_STORE ||
200 tf->cause == CAUSE_VECTOR_FAULT_FETCH)
201 handle_fault(tf->badvaddr);
202 else
203 assert(0);
204
205 out:
206 if (!(tf->sr & SR_PS) && (tf->sr & SR_EV))
207 restore_vector(tf);
208 pop_tf(tf);
209 }
210
211 void vm_boot(long test_addr, long seed)
212 {
213 while (mfpcr(PCR_COREID) > 0); // only core 0 proceeds
214
215 assert(SIZEOF_TRAPFRAME_T == sizeof(trapframe_t));
216
217 seed = 1 + (seed % MAX_TEST_PAGES);
218 freelist_head = RELOC(&freelist_nodes[0]);
219 freelist_tail = RELOC(&freelist_nodes[MAX_TEST_PAGES-1]);
220 for (long i = 0; i < MAX_TEST_PAGES; i++)
221 {
222 freelist_nodes[i].addr = (MAX_TEST_PAGES+i)*PGSIZE;
223 freelist_nodes[i].next = RELOC(&freelist_nodes[i+1]);
224 seed = LFSR_NEXT(seed);
225 }
226 freelist_nodes[MAX_TEST_PAGES-1].next = 0;
227
228 assert(MAX_TEST_PAGES*2 < PTES_PER_PT);
229 l1pt[0] = (pte_t)l2pt | 1;
230 l2pt[0] = (pte_t)l3pt | 1;
231 for (long i = 0; i < MAX_TEST_PAGES; i++)
232 l3pt[i] = l3pt[i+MAX_TEST_PAGES] = (i*PGSIZE) | 0x382;
233
234 mtpcr(PCR_PTBR, l1pt);
235 mtpcr(PCR_SR, mfpcr(PCR_SR) | SR_VM | SR_EF);
236
237 if (mfpcr(PCR_SR) & SR_EF)
238 asm volatile ("mtfsr x0");
239
240 // relocate
241 long adjustment = RELOC(0L), tmp;
242 mtpcr(PCR_EVEC, (char*)&trap_entry + adjustment);
243 asm volatile ("add sp, sp, %1; rdpc %0; addi %0, %0, 16; add %0, %0, %1; jr %0" : "=&r"(tmp) : "r"(adjustment));
244
245 memset(RELOC(&l3pt[0]), 0, MAX_TEST_PAGES*sizeof(pte_t));
246 mtpcr(PCR_PTBR, l1pt);
247
248 trapframe_t tf;
249 memset(&tf, 0, sizeof(tf));
250 tf.sr = SR_EF | SR_EV | SR_S | SR_U64 | SR_S64 | SR_VM;
251 tf.epc = test_addr;
252
253 pop_tf(&tf);
254 }