1 /* Bare metal PPC KVM app, for libre-soc simulator comparison purposes
2 Copyright (C) 2021 Lauri Kasanen
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.
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.
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/>.
22 #include <linux/kvm.h>
28 #include <sys/ioctl.h>
31 #include <sys/types.h>
35 #define RAMSIZE (64 * 1024 * 1024)
36 #define PROGSTART 0x20000000
38 static void nukenewline(char buf
[]) {
40 for (i
= 0; buf
[i
]; i
++) {
48 static void help(const char argv0
[]) {
50 printf("Usage: %s [args] -i file.bin\n\n"
52 "-i --binary file Raw, bare metal executable\n"
53 "-g --intregs file Text file setting up GPRs\n"
54 "-f --fpregs file Text file setting up FPRs\n"
55 "-s --spregs file Text file setting up SPRs (unnecessary if only LR is needed)\n"
56 "-l --load file:addr Load this binary to RAM\n"
57 "-d --dump file:addr:len Save this RAM area after running\n"
58 "-t --trace file Save a full trace to this file\n"
59 "-h --help This help\n\n"
61 "Load and dump may be given multiple times. GPR/FPR are numbered,\n"
62 "SPRs are named.\n", argv0
);
67 static void parseregs(const char name
[], const uint8_t gpr
) {
69 FILE *f
= fopen(name
, "r");
71 printf("Can't open %s\n", name
);
77 while (fgets(buf
, 256, f
)) {
82 const uint8_t reg
= strtol(buf
, NULL
, 0);
83 const char *ptr
= strchr(buf
+ 1, ' ');
85 printf("Invalid line '%s'\n", buf
);
89 const uint64_t val
= strtol(ptr
, NULL
, 0);
100 static void parsesprs(const char name
[]) {
102 FILE *f
= fopen(name
, "r");
104 printf("Can't open %s\n", name
);
110 while (fgets(buf
, 256, f
)) {
117 #define check(a) if (!strncasecmp(buf, a, sizeof(a) - 1))
126 printf("Unknown (unimplemented?) SPR register '%s'\n",
131 const char *ptr
= strchr(buf
+ 1, ' ');
133 printf("Invalid line '%s'\n", buf
);
137 const uint64_t val
= strtol(ptr
, NULL
, 0);
145 static void load(const char arg
[]) {
148 const char *ptr
= strchr(arg
, ':');
150 printf("Invalid load\n");
154 const unsigned namelen
= ptr
- arg
;
156 strncpy(name
, arg
, namelen
);
157 name
[namelen
] = '\0';
159 const uint64_t addr
= strtol(ptr
+ 1, NULL
, 0);
161 FILE *f
= fopen(name
, "r");
163 printf("Can't open %s\n", name
);
167 fseek(f
, 0, SEEK_END
);
168 const unsigned len
= ftell(f
);
176 int main(int argc
, char **argv
) {
178 const struct option longopts
[] = {
179 { "binary", 1, NULL
, 'i' },
180 { "intregs", 1, NULL
, 'g' },
181 { "fpregs", 1, NULL
, 'f' },
182 { "spregs", 1, NULL
, 's' },
183 { "load", 1, NULL
, 'l' },
184 { "dump", 1, NULL
, 'd' },
185 { "trace", 1, NULL
, 't' },
186 { "help", 0, NULL
, 'h' },
189 const char opts
[] = "i:g:f:s:l:d:t:h";
192 struct kvm_regs regs
;
193 struct kvm_sregs sregs
;
195 FILE *binary
= NULL
, *trace
= NULL
;
196 char dumps
[MAXDUMPS
][PATH_MAX
];
197 unsigned num_dumps
= 0;
198 uint8_t *ram
, *progmem
;
202 const int c
= getopt_long(argc
, argv
, opts
, longopts
, NULL
);
209 printf("Only one binary allowed\n");
213 binary
= fopen(optarg
, "r");
215 printf("Failed to open %s\n", optarg
);
219 binpath
= strdup(optarg
);
222 parseregs(optarg
, 1);
225 parseregs(optarg
, 0);
234 if (num_dumps
>= MAXDUMPS
) {
235 printf("Too many dumps\n");
239 strncpy(dumps
[num_dumps
], optarg
, PATH_MAX
);
240 dumps
[num_dumps
][PATH_MAX
- 1] = '\0';
245 printf("Only one trace allowed\n");
249 trace
= fopen(optarg
, "w");
251 printf("Failed to open %s\n", optarg
);
267 if (posix_memalign(&ram
, 64 * 1024, RAMSIZE
))
269 memset(ram
, 0, RAMSIZE
);
271 fseek(binary
, 0, SEEK_END
);
272 const unsigned binlen
= ftell(binary
);
275 printf("Loading binary %u bytes\n", binlen
); // TODO
277 if (posix_memalign(&progmem
, 64 * 1024, binlen
))
280 if (fread(progmem
, binlen
, 1, binary
) != 1)
284 kvm
= open("/dev/kvm", O_RDWR
| O_CLOEXEC
);
286 printf("Failed to open kvm, perhaps you lack permissions?\n");
291 ret
= ioctl(kvm
, KVM_GET_API_VERSION
, NULL
);
292 if (ret
== -1 || ret
!= 12) abort();
294 ret
= ioctl(kvm
, KVM_CHECK_EXTENSION
, KVM_CAP_PPC_GUEST_DEBUG_SSTEP
);
295 if (ret
== -1 || !ret
) {
296 printf("This system lacks single-stepping!\n");
300 vmfd
= ioctl(kvm
, KVM_CREATE_VM
, (unsigned long)0);
302 struct kvm_userspace_memory_region region
= {
304 .guest_phys_addr
= 0,
305 .memory_size
= RAMSIZE
,
306 .userspace_addr
= ram
,
309 ioctl(vmfd
, KVM_SET_USER_MEMORY_REGION
, ®ion
);
312 region
.guest_phys_addr
= PROGSTART
;
313 region
.memory_size
= binlen
;
314 region
.userspace_addr
= progmem
;
315 region
.flags
= KVM_MEM_READONLY
;
316 ioctl(vmfd
, KVM_SET_USER_MEMORY_REGION
, ®ion
);
318 vcpu
= ioctl(vmfd
, KVM_CREATE_VCPU
, (unsigned long)0);
319 const unsigned vcpulen
= ioctl(kvm
, KVM_GET_VCPU_MMAP_SIZE
, NULL
);
321 run
= mmap(NULL
, vcpulen
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, vcpu
, 0);
323 if (ioctl(vcpu
, KVM_GET_SREGS
, &sregs
) == -1)
325 if (ioctl(vcpu
, KVM_SET_SREGS
, &sregs
) == -1)
328 if (ioctl(vcpu
, KVM_SET_REGS
, ®s
) == -1)
331 const struct kvm_guest_debug dbg
= {
332 .control
= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
;
334 if (ioctl(vcpu
, KVM_SET_GUEST_DEBUG
, &dbg
) == -1)
339 if (ioctl(vcpu
, KVM_RUN
, NULL
) == -1)
341 printf("Exited because %u\n", run
->exit_reason
);
347 free((char *) binpath
);