-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest.c
223 lines (199 loc) · 8.15 KB
/
test.c
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#define _GNU_SOURCE
#include "core.h"
#include <linux/elf.h>
#include <stdio.h>
#include <sys/mman.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "measure.c"
#define MASK(n) (~((~0U << (n))))
#define unlikely(x) __builtin_expect((x),0)
#define likely(x) __builtin_expect((x),1)
// leave a 4 word unmapped gap, to detect stack underflows
#define STACK_INITIAL ((uint32_t)-16)
#define STACK_SIZE (1024 * 1024)
typedef struct {
Elf32_Phdr phdr;
uint32_t* data;
} segment_t;
typedef struct {
segment_t *segments;
size_t nsegments;
} emu_state_t;
static segment_t *find_segment_unchecked(core_t* core, uint32_t exc_cause, uint32_t addr, Elf32_Word required_flag, uint8_t width) {
emu_state_t *data = (emu_state_t *)core->user_data;
// search for appropriate segment
size_t n;
Elf32_Phdr *phdr;
uint32_t end_addr = addr + width; // FIXME: what if we want to access last word, blah blah
for (n = 0; n < data->nsegments; n++) {
phdr = &data->segments[n].phdr;
if (addr >= phdr->p_vaddr && end_addr - phdr->p_vaddr <= phdr->p_memsz)
goto found_segment;
}
core_set_exception(core, exc_cause, addr);
return NULL;
found_segment:
// perform additional checks
if (unlikely(!(phdr->p_flags & required_flag))) {
core_set_exception(core, exc_cause, addr);
return NULL;
}
return &data->segments[n];
}
static segment_t *find_segment(core_t* core, uint32_t exc_cause_f, uint32_t exc_cause, uint32_t addr, Elf32_Word required_flag, uint8_t width) {
// reject misaligned accesses (if we wanted to allow them, we'd at least have to check for overflow)
if (unlikely((addr & (width - 1)))) {
core_set_exception(core, exc_cause, addr);
return NULL;
}
return find_segment_unchecked(core, exc_cause_f, addr, required_flag, width);
}
static void mem_fetch(core_t* core, uint32_t addr, uint32_t* value) {
segment_t *seg;
if (!(seg = find_segment_unchecked(core, RISCV_EXC_FETCH_FAULT, addr, PF_X, 4))) return;
*value = seg->data[(addr - seg->phdr.p_vaddr) >> 2];
}
#define MEM_SWITCH(flag, exc_cause_f, exc_cause, cases) \
const Elf32_Word required_flag = flag; \
const uint32_t _exc_cause_f = exc_cause_f; \
const uint32_t _exc_cause = exc_cause; \
segment_t *seg; \
uint32_t reladdr; \
uint32_t *cell; \
uint8_t offset; \
switch (width) { \
cases \
default: \
core_set_exception(core, RISCV_EXC_ILLEGAL_INSTR, 0); \
return; \
}
#define MEM_CASE(func, width, code) \
case (func): \
if (!(seg = find_segment(core, _exc_cause_f, _exc_cause, addr, required_flag, (width)))) return; \
reladdr = addr - seg->phdr.p_vaddr; \
offset = (reladdr & 0b11) * 8; \
cell = &seg->data[reladdr >> 2]; \
code; break;
static void mem_load(core_t* core, uint32_t addr, uint8_t width, uint32_t* value) {
MEM_SWITCH(PF_R, RISCV_EXC_LOAD_FAULT, RISCV_EXC_LOAD_MISALIGN,
MEM_CASE(RISCV_MEM_LW, 4, *value = *cell)
MEM_CASE(RISCV_MEM_LHU, 2, *value = (uint32_t)(uint16_t)((*cell) >> offset))
MEM_CASE(RISCV_MEM_LH, 2, *value = (uint32_t)(int32_t)(int16_t)((*cell) >> offset))
MEM_CASE(RISCV_MEM_LBU, 1, *value = (uint32_t)(uint8_t)((*cell) >> offset))
MEM_CASE(RISCV_MEM_LB, 1, *value = (uint32_t)(int32_t)(int8_t)((*cell) >> offset))
)
}
static void mem_store(core_t* core, uint32_t addr, uint8_t width, uint32_t value) {
MEM_SWITCH(PF_W, RISCV_EXC_STORE_FAULT, RISCV_EXC_STORE_MISALIGN,
MEM_CASE(RISCV_MEM_SW, 4, *cell = value)
MEM_CASE(RISCV_MEM_SH, 2, *cell = ((*cell) & ~(MASK(16) << offset)) | (value & MASK(16)) << offset)
MEM_CASE(RISCV_MEM_SB, 1, *cell = ((*cell) & ~(MASK(8) << offset)) | (value & MASK(8)) << offset)
)
}
int main(int argc, char** argv) {
// load ELF
if (argc != 2) {
fprintf(stderr, "Usage: ./core <elf file>\n");
return 2;
}
FILE* elf = fopen(argv[1], "r");
if (!elf) {
fprintf(stderr, "Could not open ELF\n");
return 2;
}
Elf32_Ehdr ehdr;
if (!fread(&ehdr, sizeof(ehdr), 1, elf)) {
fprintf(stderr, "Could not read ELF header\n");
return 2;
}
if (memcmp(ehdr.e_ident, ELFMAG, 4) != 0) {
fprintf(stderr, "Not an ELF\n");
return 2;
}
if (ehdr.e_ident[EI_CLASS] != ELFCLASS32 || ehdr.e_ident[EI_DATA] != ELFDATA2LSB || ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
ehdr.e_machine != EM_RISCV || ehdr.e_type != ET_EXEC) {
fprintf(stderr, "Not an RV32 LE executable\n");
return 2;
}
// parse / map segments
emu_state_t data;
assert(ehdr.e_phentsize >= sizeof(Elf32_Phdr));
data.segments = calloc(ehdr.e_phnum + /* stack */ 1, sizeof(*data.segments));
data.nsegments = 0;
assert(data.segments);
for (size_t n = 0; n < ehdr.e_phnum; n++) {
segment_t *segment = &data.segments[data.nsegments];
Elf32_Phdr *phdr = &segment->phdr;
if (fseek(elf, ehdr.e_phoff + n * ehdr.e_phentsize, SEEK_SET) < 0 ||
fread(phdr, sizeof(*phdr), 1, elf) != 1) {
fprintf(stderr, "Could not read PH\n");
return 2;
}
if (phdr->p_type == PT_LOAD) {
data.nsegments++;
assert(phdr->p_memsz == phdr->p_filesz);
assert(!(phdr->p_vaddr & 0b11));
segment->data = (uint32_t*) mmap(NULL, phdr->p_filesz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(elf), phdr->p_offset);
if (segment->data == MAP_FAILED) {
fprintf(stderr, "Could not map PH %lu\n", n);
return 2;
}
assert(!(((uintptr_t)segment->data) & 0b11));
}
}
// map stack
segment_t* stack_seg = &data.segments[data.nsegments++];
Elf32_Phdr *phdr = &stack_seg->phdr;
phdr->p_vaddr = STACK_INITIAL - STACK_SIZE;
phdr->p_memsz = STACK_SIZE;
phdr->p_flags = PF_R | PF_W;
assert(!(phdr->p_vaddr & 0b11));
stack_seg->data = mmap(NULL, phdr->p_memsz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (stack_seg->data == MAP_FAILED) {
fprintf(stderr, "Could not map stack\n");
return 2;
}
assert(!(((uintptr_t)stack_seg->data) & 0b11));
// FIXME: sort segments, check no overlap
// initialize emulator
core_t core;
memset(&core, 0, sizeof(core));
core.user_data = &data;
core.mem_fetch = mem_fetch;
core.mem_load = mem_load;
core.mem_store = mem_store;
core.pc = ehdr.e_entry;
assert(!(core.pc & 0b11));
core.x_regs[RISCV_R_SP] = STACK_INITIAL;
// emulate!
int cycle_count_fd = simple_perf_counter(PERF_COUNT_HW_CPU_CYCLES);
int instr_count_fd = simple_perf_counter(PERF_COUNT_HW_INSTRUCTIONS);
struct timespec start, end;
int res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
ioctl(cycle_count_fd, PERF_EVENT_IOC_ENABLE, 0);
ioctl(instr_count_fd, PERF_EVENT_IOC_ENABLE, 0);
while (1) {
core_step(&core);
if (likely(!core.error)) continue;
if (core.error == ERR_EXCEPTION && core.exc_cause == RISCV_EXC_ECALL_U) break;
fprintf(stderr, "core error %s: %s. val=%#x\n", core_error_str(core.error), core_exc_cause_str(core.exc_cause), core.exc_val);
return 2;
}
res |= ioctl(cycle_count_fd, PERF_EVENT_IOC_DISABLE, 0);
res |= ioctl(instr_count_fd, PERF_EVENT_IOC_DISABLE, 0);
res |= clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
assert(!res);
int64_t elapsed = ((int64_t)end.tv_sec - (int64_t)start.tv_sec) * 1000000000 + ((int64_t)end.tv_nsec - (int64_t)start.tv_nsec);
long long host_instr_count = simple_perf_read(instr_count_fd);
long long host_cycle_count = simple_perf_read(cycle_count_fd);
printf("executed %u instructions in %.3f ms, %lld instructions, %lld cycles\n",
core.instr_count, elapsed / 1e6, host_instr_count, host_cycle_count);
printf("stats: %.3f c/i, %.1f i/I, %.1f c/I, %.1f MIps\n",
((double)host_cycle_count) / ((double)host_instr_count),
((double)host_instr_count) / ((double)core.instr_count),
((double)host_cycle_count) / ((double)core.instr_count),
core.instr_count / (double)elapsed * 1e3);
}