bd9c80edd7ec8548786059c3989ac4d1d484a908
[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 while (1);
26 }
27
28 #define stringify1(x) #x
29 #define stringify(x) stringify1(x)
30 #define assert(x) do { \
31 if (x) break; \
32 cputstring("Assertion failed: " stringify(x)); \
33 terminate(3); \
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 | PTE_UW | PTE_UR | PTE_UX | PTE_SW | PTE_SR | PTE_SX | PTE_V;
93 mtpcr(PCR_FATC, 0);
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 }
185 else if (tf->cause == CAUSE_FAULT_FETCH)
186 handle_fault(tf->epc);
187 else if (tf->cause == CAUSE_ILLEGAL_INSTRUCTION)
188 {
189 int fssr;
190 asm ("la %0, 1f; lw %0, 0(%0); b 2f; 1: fssr x0; 2:" : "=r"(fssr));
191
192 if (tf->insn == fssr)
193 terminate(1); // FP test on non-FP hardware. "succeed."
194 else if ((tf->insn & 0xF83FFFFF) == 0x37B)
195 emulate_vxcptsave(tf);
196 else if ((tf->insn & 0xF83FFFFF) == 0x77B)
197 emulate_vxcptrestore(tf);
198 else
199 assert(0);
200 tf->epc += 4;
201 }
202 else if (tf->cause == CAUSE_FAULT_LOAD || tf->cause == CAUSE_FAULT_STORE ||
203 tf->cause == CAUSE_VECTOR_FAULT_LOAD || tf->cause == CAUSE_VECTOR_FAULT_STORE ||
204 tf->cause == CAUSE_VECTOR_FAULT_FETCH)
205 handle_fault(tf->badvaddr);
206 else
207 assert(0);
208
209 out:
210 if (!(tf->sr & SR_PS) && (tf->sr & SR_EV))
211 restore_vector(tf);
212 pop_tf(tf);
213 }
214
215 void vm_boot(long test_addr, long seed)
216 {
217 while (mfpcr(PCR_HARTID) > 0); // only core 0 proceeds
218
219 assert(SIZEOF_TRAPFRAME_T == sizeof(trapframe_t));
220
221 seed = 1 + (seed % MAX_TEST_PAGES);
222 freelist_head = RELOC(&freelist_nodes[0]);
223 freelist_tail = RELOC(&freelist_nodes[MAX_TEST_PAGES-1]);
224 for (long i = 0; i < MAX_TEST_PAGES; i++)
225 {
226 freelist_nodes[i].addr = (MAX_TEST_PAGES+i)*PGSIZE;
227 freelist_nodes[i].next = RELOC(&freelist_nodes[i+1]);
228 seed = LFSR_NEXT(seed);
229 }
230 freelist_nodes[MAX_TEST_PAGES-1].next = 0;
231
232 assert(MAX_TEST_PAGES*2 < PTES_PER_PT);
233 l1pt[0] = (pte_t)l2pt | PTE_V | PTE_T;
234 l2pt[0] = (pte_t)l3pt | PTE_V | PTE_T;
235 for (long i = 0; i < MAX_TEST_PAGES; i++)
236 l3pt[i] = l3pt[i+MAX_TEST_PAGES] = (i*PGSIZE) | PTE_SW | PTE_SR | PTE_SX | PTE_V;
237
238 mtpcr(PCR_PTBR, l1pt);
239 mtpcr(PCR_SR, mfpcr(PCR_SR) | SR_VM | SR_EF);
240
241 // relocate
242 long adjustment = RELOC(0L), tmp;
243 mtpcr(PCR_EVEC, (char*)&trap_entry + adjustment);
244 asm volatile ("add sp, sp, %1; rdpc %0; addi %0, %0, 16; add %0, %0, %1; jr %0" : "=&r"(tmp) : "r"(adjustment));
245
246 memset(RELOC(&l3pt[0]), 0, MAX_TEST_PAGES*sizeof(pte_t));
247 mtpcr(PCR_FATC, 0);
248
249 trapframe_t tf;
250 memset(&tf, 0, sizeof(tf));
251 tf.sr = SR_EF | SR_EV | SR_S | SR_U64 | SR_S64 | SR_VM;
252 tf.epc = test_addr;
253
254 pop_tf(&tf);
255 }