0b9263f8b3107e84d8d0f9848f2368adc01a94ef
[kvm-minippc.git] / main.c
1 /* Bare metal PPC KVM app, for libre-soc simulator comparison purposes
2 Copyright (C) 2021 Lauri Kasanen
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, version 3 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #define _GNU_SOURCE
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <getopt.h>
22 #include <linux/kvm.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/ioctl.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 #define PAGE_SIZE (64 * 1024) // assumption
35 #define MAXDUMPS 10
36 #define RAMSIZE (64 * 1024 * 1024)
37 #define PROGSTART 0x20000000
38
39 #define MSR_64 (1UL<<63)
40 #define MSR_LE (1UL<<0)
41
42 enum {
43 SPR_LR,
44 };
45
46 static void nukenewline(char buf[]) {
47 unsigned i;
48 for (i = 0; buf[i]; i++) {
49 if (buf[i] == '\n') {
50 buf[i] = '\0';
51 break;
52 }
53 }
54 }
55
56 static void help(const char argv0[]) {
57
58 printf("Usage: %s [args] -i file.bin\n\n"
59
60 "-i --binary file Raw, bare metal executable\n"
61 "-g --intregs file Text file setting up GPRs\n"
62 "-f --fpregs file Text file setting up FPRs\n"
63 "-s --spregs file Text file setting up SPRs (unnecessary if only LR is needed)\n"
64 "-l --load file:addr Load this binary to RAM\n"
65 "-d --dump file:addr:len Save this RAM area after running\n"
66 "-t --trace file Save a full trace to this file\n"
67 "-h --help This help\n\n"
68
69 "Load and dump may be given multiple times. GPR/FPR are numbered,\n"
70 "SPRs are named.\n", argv0);
71
72 exit(0);
73 }
74
75 static void parseregs(const char name[], const uint8_t gpr, struct kvm_regs *regs,
76 struct kvm_fpu *fpregs) {
77
78 FILE *f = fopen(name, "r");
79 if (!f) {
80 printf("Can't open %s\n", name);
81 exit(1);
82 }
83
84 char buf[256];
85
86 while (fgets(buf, 256, f)) {
87 if (buf[0] == '#')
88 continue;
89 nukenewline(buf);
90
91 const uint8_t reg = strtol(buf, NULL, 0);
92 const char *ptr = strchr(buf + 1, ' ');
93 if (!ptr) {
94 printf("Invalid line '%s'\n", buf);
95 continue;
96 }
97
98 const uint64_t val = strtol(ptr, NULL, 0);
99
100 // apply reg
101 if (gpr) {
102 regs->gpr[reg] = val;
103 } else {
104 fpregs->fpr[reg] = val;
105 }
106 }
107
108 fclose(f);
109 }
110
111 static void parsesprs(const char name[], struct kvm_regs *regs) {
112
113 FILE *f = fopen(name, "r");
114 if (!f) {
115 printf("Can't open %s\n", name);
116 exit(1);
117 }
118
119 char buf[256];
120
121 while (fgets(buf, 256, f)) {
122 if (buf[0] == '#')
123 continue;
124 nukenewline(buf);
125
126 uint8_t reg = 0xff;
127
128 #define check(a) if (!strncasecmp(buf, a, sizeof(a) - 1))
129
130 check("LR:") {
131 reg = SPR_LR;
132 }
133
134 #undef check
135
136 if (reg == 0xff) {
137 printf("Unknown (unimplemented?) SPR register '%s'\n",
138 buf);
139 continue;
140 }
141
142 const char *ptr = strchr(buf + 1, ' ');
143 if (!ptr) {
144 printf("Invalid line '%s'\n", buf);
145 continue;
146 }
147
148 const uint64_t val = strtol(ptr, NULL, 0);
149
150 // apply reg
151 switch (reg) {
152 case SPR_LR:
153 regs->lr = val;
154 break;
155 }
156 }
157
158 fclose(f);
159 }
160
161 static void load(const char arg[], uint8_t *ram) {
162
163 char name[PATH_MAX];
164 const char *ptr = strchr(arg, ':');
165 if (!ptr) {
166 printf("Invalid load\n");
167 exit(1);
168 }
169
170 const unsigned namelen = ptr - arg;
171
172 strncpy(name, arg, namelen);
173 name[namelen] = '\0';
174
175 const uint64_t addr = strtol(ptr + 1, NULL, 0);
176
177 FILE *f = fopen(name, "r");
178 if (!f) {
179 printf("Can't open %s\n", name);
180 exit(1);
181 }
182
183 fseek(f, 0, SEEK_END);
184 const unsigned len = ftell(f);
185 rewind(f);
186
187 if (addr + len >= RAMSIZE) {
188 printf("Tried to use too much RAM\n");
189 exit(1);
190 }
191
192 printf("Loading %s to 0x%lx, %u bytes\n", name, addr, len);
193
194 if (fread(&ram[addr], len, 1, f) != 1)
195 abort();
196
197 fclose(f);
198 }
199
200 static void setfpregs(const int vcpu, struct kvm_fpu *fpregs) {
201 // KVM_[SG]ET_FPU isn't supported on PPC, we have to move individual regs...
202 unsigned i;
203 for (i = 0; i < 32; i++) {
204 struct kvm_one_reg r = {
205 .id = KVM_REG_PPC_FPR(i),
206 .addr = (uint64_t) &fpregs->fpr[i]
207 };
208
209 if (ioctl(vcpu, KVM_SET_ONE_REG, &r) != 0)
210 abort();
211 }
212 }
213
214 static void getfpregs(const int vcpu, struct kvm_fpu *fpregs) {
215 // KVM_[SG]ET_FPU isn't supported on PPC, we have to move individual regs...
216 unsigned i;
217 for (i = 0; i < 32; i++) {
218 struct kvm_one_reg r = {
219 .id = KVM_REG_PPC_FPR(i),
220 .addr = (uint64_t) &fpregs->fpr[i]
221 };
222
223 if (ioctl(vcpu, KVM_GET_ONE_REG, &r) != 0)
224 abort();
225 }
226 }
227
228 int main(int argc, char **argv) {
229
230 const struct option longopts[] = {
231 { "binary", 1, NULL, 'i' },
232 { "intregs", 1, NULL, 'g' },
233 { "fpregs", 1, NULL, 'f' },
234 { "spregs", 1, NULL, 's' },
235 { "load", 1, NULL, 'l' },
236 { "dump", 1, NULL, 'd' },
237 { "trace", 1, NULL, 't' },
238 { "help", 0, NULL, 'h' },
239 { NULL, 0, NULL, 0 }
240 };
241 const char opts[] = "i:g:f:s:l:d:t:h";
242
243 struct kvm_run *run;
244 struct kvm_regs regs;
245 struct kvm_sregs sregs;
246 struct kvm_fpu fpregs;
247 int kvm, vmfd, vcpu;
248 FILE *binary = NULL, *trace = NULL;
249 char dumps[MAXDUMPS][PATH_MAX];
250 unsigned num_dumps = 0;
251 uint8_t *ram, *progmem;
252 const char *binpath;
253
254 // Yes, we're frugal
255 if (posix_memalign((void **) &ram, 64 * 1024, RAMSIZE))
256 abort();
257 memset(ram, 0, RAMSIZE);
258
259 memset(&regs, 0, sizeof(struct kvm_regs));
260 memset(&fpregs, 0, sizeof(struct kvm_fpu));
261
262 regs.lr = -1;
263 regs.pc = PROGSTART;
264 regs.msr = MSR_64 | MSR_LE;
265
266 while (1) {
267 const int c = getopt_long(argc, argv, opts, longopts, NULL);
268 if (c == -1)
269 break;
270
271 switch (c) {
272 case 'i':
273 if (binary) {
274 printf("Only one binary allowed\n");
275 return 1;
276 }
277
278 binary = fopen(optarg, "r");
279 if (!binary) {
280 printf("Failed to open %s\n", optarg);
281 return 1;
282 }
283
284 binpath = strdup(optarg);
285 break;
286 case 'g':
287 parseregs(optarg, 1, &regs, &fpregs);
288 break;
289 case 'f':
290 parseregs(optarg, 0, &regs, &fpregs);
291 break;
292 case 's':
293 parsesprs(optarg, &regs);
294 break;
295 case 'l':
296 load(optarg, ram);
297 break;
298 case 'd':
299 if (num_dumps >= MAXDUMPS) {
300 printf("Too many dumps\n");
301 return 1;
302 }
303
304 strncpy(dumps[num_dumps], optarg, PATH_MAX);
305 dumps[num_dumps][PATH_MAX - 1] = '\0';
306 num_dumps++;
307 break;
308 case 't':
309 if (trace) {
310 printf("Only one trace allowed\n");
311 return 1;
312 }
313
314 trace = fopen(optarg, "w");
315 if (!trace) {
316 printf("Failed to open %s\n", optarg);
317 return 1;
318 }
319 break;
320 case 'h':
321 default:
322 help(argv[0]);
323 break;
324 }
325 }
326
327 if (!binary) {
328 help(argv[0]);
329 }
330
331 fseek(binary, 0, SEEK_END);
332 const unsigned binlen = ftell(binary);
333 rewind(binary);
334
335 printf("Loading binary %u bytes\n", binlen);
336
337 const unsigned progmemlen = (binlen + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
338
339 if (posix_memalign((void **) &progmem, 64 * 1024, progmemlen))
340 abort();
341
342 if (fread(progmem, binlen, 1, binary) != 1)
343 abort();
344 fclose(binary);
345
346 kvm = open("/dev/kvm", O_RDWR | O_CLOEXEC);
347 if (kvm < 0) {
348 printf("Failed to open kvm, perhaps you lack permissions?\n");
349 return 1;
350 }
351
352 int ret;
353 ret = ioctl(kvm, KVM_GET_API_VERSION, NULL);
354 if (ret == -1 || ret != 12) abort();
355
356 ret = ioctl(kvm, KVM_CHECK_EXTENSION, KVM_CAP_PPC_GUEST_DEBUG_SSTEP);
357 if (ret == -1 || !ret) {
358 printf("This system lacks single-stepping!\n");
359 return 1;
360 }
361
362 vmfd = ioctl(kvm, KVM_CREATE_VM, (unsigned long)0);
363
364 struct kvm_userspace_memory_region region = {
365 .slot = 0,
366 .guest_phys_addr = 0,
367 .memory_size = RAMSIZE,
368 .userspace_addr = (uint64_t) ram,
369 .flags = 0
370 };
371 if (ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, &region) == -1)
372 abort();
373
374 region.slot = 1;
375 region.guest_phys_addr = PROGSTART;
376 region.memory_size = progmemlen;
377 region.userspace_addr = (uint64_t) progmem;
378 region.flags = KVM_MEM_READONLY;
379 if (ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, &region) == -1)
380 abort();
381
382 vcpu = ioctl(vmfd, KVM_CREATE_VCPU, (unsigned long)0);
383 const unsigned vcpulen = ioctl(kvm, KVM_GET_VCPU_MMAP_SIZE, NULL);
384
385 run = mmap(NULL, vcpulen, PROT_READ | PROT_WRITE, MAP_SHARED, vcpu, 0);
386
387 if (ioctl(vcpu, KVM_GET_SREGS, &sregs) == -1)
388 abort();
389 if (ioctl(vcpu, KVM_SET_SREGS, &sregs) == -1)
390 abort();
391
392 if (ioctl(vcpu, KVM_SET_REGS, &regs) == -1)
393 abort();
394 setfpregs(vcpu, &fpregs);
395
396 const struct kvm_guest_debug dbg = {
397 .control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP
398 };
399 if (ioctl(vcpu, KVM_SET_GUEST_DEBUG, &dbg) == -1)
400 abort();
401
402 // Runtime
403 while (1) {
404 if (ioctl(vcpu, KVM_RUN, NULL) == -1)
405 abort();
406 printf("Exited because %u\n", run->exit_reason);
407 }
408
409 close(kvm);
410 free(progmem);
411 free(ram);
412 free((char *) binpath);
413 return 0;
414 }