-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghostwrite.h
51 lines (43 loc) · 1.11 KB
/
ghostwrite.h
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
#include <string.h>
#include <inttypes.h>
#ifndef __ghostwrite_h__
#define __ghostwrite_h__
char __attribute__((aligned(4096))) buffer[2<<20];
inline __attribute__((always_inline)) void maccess(void *addr) {
asm volatile("ld a7, (%0)" : : "r"(addr) : "a7", "memory");
}
// evict the TLB
void evict() {
for (unsigned i = 0; i < sizeof(buffer); i+=4096) {
maccess(&buffer[i]);
}
asm volatile("fence\n\t");
}
void evict_init() {
memset(buffer, 0xff, sizeof(buffer));
}
void write_8(size_t phys_addr, uint8_t val) {
evict();
int vl = 8;
asm volatile(\
"vsetvli %2, x0, e16, m1\n\t"
"vmv.v.x v1, %1\n\t"
"mv gp, %0\n\t"
".fill 1, 4, 0xf201f0a7\n\t"
:: "r"(phys_addr), "r"(val), "r"(vl) : "a5", "gp");
asm volatile("fence\n\t");
}
void write_64(size_t phys_addr, uint64_t val) {
evict();
int vl = 8;
for (int i = 0; i < 8; i++) {
asm volatile(\
"vsetvli %2, x0, e16, m1\n\t"
"vmv.v.x v1, %1\n\t"
"mv gp, %0\n\t"
".fill 1, 4, 0xf201f0a7\n\t"
:: "r"(phys_addr+i), "r"((val>>(8*i))&0xff), "r"(vl) : "a5", "gp");
}
asm volatile("fence\n\t");
}
#endif