Also set fp regs
[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 MAXDUMPS 10
35 #define RAMSIZE (64 * 1024 * 1024)
36 #define PROGSTART 0x20000000
37
38 #define MSR_64 (1UL<<63)
39 #define MSR_LE (1UL<<0)
40
41 enum {
42 SPR_LR,
43 };
44
45 static void nukenewline(char buf[]) {
46 unsigned i;
47 for (i = 0; buf[i]; i++) {
48 if (buf[i] == '\n') {
49 buf[i] = '\0';
50 break;
51 }
52 }
53 }
54
55 static void help(const char argv0[]) {
56
57 printf("Usage: %s [args] -i file.bin\n\n"
58
59 "-i --binary file Raw, bare metal executable\n"
60 "-g --intregs file Text file setting up GPRs\n"
61 "-f --fpregs file Text file setting up FPRs\n"
62 "-s --spregs file Text file setting up SPRs (unnecessary if only LR is needed)\n"
63 "-l --load file:addr Load this binary to RAM\n"
64 "-d --dump file:addr:len Save this RAM area after running\n"
65 "-t --trace file Save a full trace to this file\n"
66 "-h --help This help\n\n"
67
68 "Load and dump may be given multiple times. GPR/FPR are numbered,\n"
69 "SPRs are named.\n", argv0);
70
71 exit(0);
72 }
73
74 static void parseregs(const char name[], const uint8_t gpr, struct kvm_regs *regs,
75 struct kvm_fpu *fpregs) {
76
77 FILE *f = fopen(name, "r");
78 if (!f) {
79 printf("Can't open %s\n", name);
80 exit(1);
81 }
82
83 char buf[256];
84
85 while (fgets(buf, 256, f)) {
86 if (buf[0] == '#')
87 continue;
88 nukenewline(buf);
89
90 const uint8_t reg = strtol(buf, NULL, 0);
91 const char *ptr = strchr(buf + 1, ' ');
92 if (!ptr) {
93 printf("Invalid line '%s'\n", buf);
94 continue;
95 }
96
97 const uint64_t val = strtol(ptr, NULL, 0);
98
99 // apply reg
100 if (gpr) {
101 regs->gpr[reg] = val;
102 } else {
103 fpregs->fpr[reg] = val;
104 }
105 }
106
107 fclose(f);
108 }
109
110 static void parsesprs(const char name[], struct kvm_regs *regs) {
111
112 FILE *f = fopen(name, "r");
113 if (!f) {
114 printf("Can't open %s\n", name);
115 exit(1);
116 }
117
118 char buf[256];
119
120 while (fgets(buf, 256, f)) {
121 if (buf[0] == '#')
122 continue;
123 nukenewline(buf);
124
125 uint8_t reg = 0xff;
126
127 #define check(a) if (!strncasecmp(buf, a, sizeof(a) - 1))
128
129 check("LR:") {
130 reg = SPR_LR;
131 }
132
133 #undef check
134
135 if (reg == 0xff) {
136 printf("Unknown (unimplemented?) SPR register '%s'\n",
137 buf);
138 continue;
139 }
140
141 const char *ptr = strchr(buf + 1, ' ');
142 if (!ptr) {
143 printf("Invalid line '%s'\n", buf);
144 continue;
145 }
146
147 const uint64_t val = strtol(ptr, NULL, 0);
148
149 // apply reg
150 switch (reg) {
151 case SPR_LR:
152 regs->lr = val;
153 break;
154 }
155 }
156
157 fclose(f);
158 }
159
160 static void load(const char arg[], uint8_t *ram) {
161
162 char name[PATH_MAX];
163 const char *ptr = strchr(arg, ':');
164 if (!ptr) {
165 printf("Invalid load\n");
166 exit(1);
167 }
168
169 const unsigned namelen = ptr - arg;
170
171 strncpy(name, arg, namelen);
172 name[namelen] = '\0';
173
174 const uint64_t addr = strtol(ptr + 1, NULL, 0);
175
176 FILE *f = fopen(name, "r");
177 if (!f) {
178 printf("Can't open %s\n", name);
179 exit(1);
180 }
181
182 fseek(f, 0, SEEK_END);
183 const unsigned len = ftell(f);
184 rewind(f);
185
186 if (addr + len >= RAMSIZE) {
187 printf("Tried to use too much RAM\n");
188 exit(1);
189 }
190
191 printf("Loading %s to 0x%lx, %u bytes\n", name, addr, len);
192
193 if (fread(&ram[addr], len, 1, f) != 1)
194 abort();
195
196 fclose(f);
197 }
198
199 int main(int argc, char **argv) {
200
201 const struct option longopts[] = {
202 { "binary", 1, NULL, 'i' },
203 { "intregs", 1, NULL, 'g' },
204 { "fpregs", 1, NULL, 'f' },
205 { "spregs", 1, NULL, 's' },
206 { "load", 1, NULL, 'l' },
207 { "dump", 1, NULL, 'd' },
208 { "trace", 1, NULL, 't' },
209 { "help", 0, NULL, 'h' },
210 { NULL, 0, NULL, 0 }
211 };
212 const char opts[] = "i:g:f:s:l:d:t:h";
213
214 struct kvm_run *run;
215 struct kvm_regs regs;
216 struct kvm_sregs sregs;
217 struct kvm_fpu fpregs;
218 int kvm, vmfd, vcpu;
219 FILE *binary = NULL, *trace = NULL;
220 char dumps[MAXDUMPS][PATH_MAX];
221 unsigned num_dumps = 0;
222 uint8_t *ram, *progmem;
223 const char *binpath;
224
225 // Yes, we're frugal
226 if (posix_memalign((void **) &ram, 64 * 1024, RAMSIZE))
227 abort();
228 memset(ram, 0, RAMSIZE);
229
230 memset(&regs, 0, sizeof(struct kvm_regs));
231 memset(&fpregs, 0, sizeof(struct kvm_fpu));
232
233 regs.lr = -1;
234 regs.pc = PROGSTART;
235 regs.msr = MSR_64 | MSR_LE;
236
237 while (1) {
238 const int c = getopt_long(argc, argv, opts, longopts, NULL);
239 if (c == -1)
240 break;
241
242 switch (c) {
243 case 'i':
244 if (binary) {
245 printf("Only one binary allowed\n");
246 return 1;
247 }
248
249 binary = fopen(optarg, "r");
250 if (!binary) {
251 printf("Failed to open %s\n", optarg);
252 return 1;
253 }
254
255 binpath = strdup(optarg);
256 break;
257 case 'g':
258 parseregs(optarg, 1, &regs, &fpregs);
259 break;
260 case 'f':
261 parseregs(optarg, 0, &regs, &fpregs);
262 break;
263 case 's':
264 parsesprs(optarg, &regs);
265 break;
266 case 'l':
267 load(optarg, ram);
268 break;
269 case 'd':
270 if (num_dumps >= MAXDUMPS) {
271 printf("Too many dumps\n");
272 return 1;
273 }
274
275 strncpy(dumps[num_dumps], optarg, PATH_MAX);
276 dumps[num_dumps][PATH_MAX - 1] = '\0';
277 num_dumps++;
278 break;
279 case 't':
280 if (trace) {
281 printf("Only one trace allowed\n");
282 return 1;
283 }
284
285 trace = fopen(optarg, "w");
286 if (!trace) {
287 printf("Failed to open %s\n", optarg);
288 return 1;
289 }
290 break;
291 case 'h':
292 default:
293 help(argv[0]);
294 break;
295 }
296 }
297
298 if (!binary) {
299 help(argv[0]);
300 }
301
302 fseek(binary, 0, SEEK_END);
303 const unsigned binlen = ftell(binary);
304 rewind(binary);
305
306 printf("Loading binary %u bytes\n", binlen);
307
308 if (posix_memalign((void **) &progmem, 64 * 1024, binlen))
309 abort();
310
311 if (fread(progmem, binlen, 1, binary) != 1)
312 abort();
313 fclose(binary);
314
315 kvm = open("/dev/kvm", O_RDWR | O_CLOEXEC);
316 if (kvm < 0) {
317 printf("Failed to open kvm, perhaps you lack permissions?\n");
318 return 1;
319 }
320
321 int ret;
322 ret = ioctl(kvm, KVM_GET_API_VERSION, NULL);
323 if (ret == -1 || ret != 12) abort();
324
325 ret = ioctl(kvm, KVM_CHECK_EXTENSION, KVM_CAP_PPC_GUEST_DEBUG_SSTEP);
326 if (ret == -1 || !ret) {
327 printf("This system lacks single-stepping!\n");
328 return 1;
329 }
330
331 vmfd = ioctl(kvm, KVM_CREATE_VM, (unsigned long)0);
332
333 struct kvm_userspace_memory_region region = {
334 .slot = 0,
335 .guest_phys_addr = 0,
336 .memory_size = RAMSIZE,
337 .userspace_addr = (uint64_t) ram,
338 .flags = 0
339 };
340 ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, &region);
341
342 region.slot = 1;
343 region.guest_phys_addr = PROGSTART;
344 region.memory_size = binlen;
345 region.userspace_addr = (uint64_t) progmem;
346 region.flags = KVM_MEM_READONLY;
347 ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, &region);
348
349 vcpu = ioctl(vmfd, KVM_CREATE_VCPU, (unsigned long)0);
350 const unsigned vcpulen = ioctl(kvm, KVM_GET_VCPU_MMAP_SIZE, NULL);
351
352 run = mmap(NULL, vcpulen, PROT_READ | PROT_WRITE, MAP_SHARED, vcpu, 0);
353
354 if (ioctl(vcpu, KVM_GET_SREGS, &sregs) == -1)
355 abort();
356 if (ioctl(vcpu, KVM_SET_SREGS, &sregs) == -1)
357 abort();
358
359 if (ioctl(vcpu, KVM_SET_REGS, &regs) == -1)
360 abort();
361 if (ioctl(vcpu, KVM_SET_FPU, &fpregs) == -1)
362 abort();
363
364 const struct kvm_guest_debug dbg = {
365 .control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP
366 };
367 if (ioctl(vcpu, KVM_SET_GUEST_DEBUG, &dbg) == -1)
368 abort();
369
370 // Runtime
371 while (1) {
372 if (ioctl(vcpu, KVM_RUN, NULL) == -1)
373 abort();
374 printf("Exited because %u\n", run->exit_reason);
375 }
376
377 close(kvm);
378 free(progmem);
379 free(ram);
380 free((char *) binpath);
381 return 0;
382 }