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 #define MSR_64 (1UL<<63)
39 #define MSR_LE (1UL<<0)
45 static void nukenewline(char buf
[]) {
47 for (i
= 0; buf
[i
]; i
++) {
55 static void help(const char argv0
[]) {
57 printf("Usage: %s [args] -i file.bin\n\n"
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"
68 "Load and dump may be given multiple times. GPR/FPR are numbered,\n"
69 "SPRs are named.\n", argv0
);
74 static void parseregs(const char name
[], const uint8_t gpr
, struct kvm_regs
*regs
,
75 struct kvm_fpu
*fpregs
) {
77 FILE *f
= fopen(name
, "r");
79 printf("Can't open %s\n", name
);
85 while (fgets(buf
, 256, f
)) {
90 const uint8_t reg
= strtol(buf
, NULL
, 0);
91 const char *ptr
= strchr(buf
+ 1, ' ');
93 printf("Invalid line '%s'\n", buf
);
97 const uint64_t val
= strtol(ptr
, NULL
, 0);
101 regs
->gpr
[reg
] = val
;
103 fpregs
->fpr
[reg
] = val
;
110 static void parsesprs(const char name
[], struct kvm_regs
*regs
) {
112 FILE *f
= fopen(name
, "r");
114 printf("Can't open %s\n", name
);
120 while (fgets(buf
, 256, f
)) {
127 #define check(a) if (!strncasecmp(buf, a, sizeof(a) - 1))
136 printf("Unknown (unimplemented?) SPR register '%s'\n",
141 const char *ptr
= strchr(buf
+ 1, ' ');
143 printf("Invalid line '%s'\n", buf
);
147 const uint64_t val
= strtol(ptr
, NULL
, 0);
160 static void load(const char arg
[], uint8_t *ram
) {
163 const char *ptr
= strchr(arg
, ':');
165 printf("Invalid load\n");
169 const unsigned namelen
= ptr
- arg
;
171 strncpy(name
, arg
, namelen
);
172 name
[namelen
] = '\0';
174 const uint64_t addr
= strtol(ptr
+ 1, NULL
, 0);
176 FILE *f
= fopen(name
, "r");
178 printf("Can't open %s\n", name
);
182 fseek(f
, 0, SEEK_END
);
183 const unsigned len
= ftell(f
);
186 if (addr
+ len
>= RAMSIZE
) {
187 printf("Tried to use too much RAM\n");
191 printf("Loading %s to 0x%lx, %u bytes\n", name
, addr
, len
);
193 if (fread(&ram
[addr
], len
, 1, f
) != 1)
199 static void setfpregs(const int vcpu
, struct kvm_fpu
*fpregs
) {
200 // KVM_[SG]ET_FPU isn't supported on PPC, we have to move individual regs...
202 for (i
= 0; i
< 32; i
++) {
203 struct kvm_one_reg r
= {
204 .id
= KVM_REG_PPC_FPR(i
),
205 .addr
= (uint64_t) &fpregs
->fpr
[i
]
208 if (ioctl(vcpu
, KVM_SET_ONE_REG
, &r
) != 0)
213 static void getfpregs(const int vcpu
, struct kvm_fpu
*fpregs
) {
214 // KVM_[SG]ET_FPU isn't supported on PPC, we have to move individual regs...
216 for (i
= 0; i
< 32; i
++) {
217 struct kvm_one_reg r
= {
218 .id
= KVM_REG_PPC_FPR(i
),
219 .addr
= (uint64_t) &fpregs
->fpr
[i
]
222 if (ioctl(vcpu
, KVM_GET_ONE_REG
, &r
) != 0)
227 int main(int argc
, char **argv
) {
229 const struct option longopts
[] = {
230 { "binary", 1, NULL
, 'i' },
231 { "intregs", 1, NULL
, 'g' },
232 { "fpregs", 1, NULL
, 'f' },
233 { "spregs", 1, NULL
, 's' },
234 { "load", 1, NULL
, 'l' },
235 { "dump", 1, NULL
, 'd' },
236 { "trace", 1, NULL
, 't' },
237 { "help", 0, NULL
, 'h' },
240 const char opts
[] = "i:g:f:s:l:d:t:h";
243 struct kvm_regs regs
;
244 struct kvm_sregs sregs
;
245 struct kvm_fpu fpregs
;
247 FILE *binary
= NULL
, *trace
= NULL
;
248 char dumps
[MAXDUMPS
][PATH_MAX
];
249 unsigned num_dumps
= 0;
250 uint8_t *ram
, *progmem
;
254 if (posix_memalign((void **) &ram
, 64 * 1024, RAMSIZE
))
256 memset(ram
, 0, RAMSIZE
);
258 memset(®s
, 0, sizeof(struct kvm_regs
));
259 memset(&fpregs
, 0, sizeof(struct kvm_fpu
));
263 regs
.msr
= MSR_64
| MSR_LE
;
266 const int c
= getopt_long(argc
, argv
, opts
, longopts
, NULL
);
273 printf("Only one binary allowed\n");
277 binary
= fopen(optarg
, "r");
279 printf("Failed to open %s\n", optarg
);
283 binpath
= strdup(optarg
);
286 parseregs(optarg
, 1, ®s
, &fpregs
);
289 parseregs(optarg
, 0, ®s
, &fpregs
);
292 parsesprs(optarg
, ®s
);
298 if (num_dumps
>= MAXDUMPS
) {
299 printf("Too many dumps\n");
303 strncpy(dumps
[num_dumps
], optarg
, PATH_MAX
);
304 dumps
[num_dumps
][PATH_MAX
- 1] = '\0';
309 printf("Only one trace allowed\n");
313 trace
= fopen(optarg
, "w");
315 printf("Failed to open %s\n", optarg
);
330 fseek(binary
, 0, SEEK_END
);
331 const unsigned binlen
= ftell(binary
);
334 printf("Loading binary %u bytes\n", binlen
);
336 if (posix_memalign((void **) &progmem
, 64 * 1024, binlen
))
339 if (fread(progmem
, binlen
, 1, binary
) != 1)
343 kvm
= open("/dev/kvm", O_RDWR
| O_CLOEXEC
);
345 printf("Failed to open kvm, perhaps you lack permissions?\n");
350 ret
= ioctl(kvm
, KVM_GET_API_VERSION
, NULL
);
351 if (ret
== -1 || ret
!= 12) abort();
353 ret
= ioctl(kvm
, KVM_CHECK_EXTENSION
, KVM_CAP_PPC_GUEST_DEBUG_SSTEP
);
354 if (ret
== -1 || !ret
) {
355 printf("This system lacks single-stepping!\n");
359 vmfd
= ioctl(kvm
, KVM_CREATE_VM
, (unsigned long)0);
361 struct kvm_userspace_memory_region region
= {
363 .guest_phys_addr
= 0,
364 .memory_size
= RAMSIZE
,
365 .userspace_addr
= (uint64_t) ram
,
368 if (ioctl(vmfd
, KVM_SET_USER_MEMORY_REGION
, ®ion
) == -1)
372 region
.guest_phys_addr
= PROGSTART
;
373 region
.memory_size
= binlen
;
374 region
.userspace_addr
= (uint64_t) progmem
;
375 region
.flags
= KVM_MEM_READONLY
;
376 if (ioctl(vmfd
, KVM_SET_USER_MEMORY_REGION
, ®ion
) == -1)
379 vcpu
= ioctl(vmfd
, KVM_CREATE_VCPU
, (unsigned long)0);
380 const unsigned vcpulen
= ioctl(kvm
, KVM_GET_VCPU_MMAP_SIZE
, NULL
);
382 run
= mmap(NULL
, vcpulen
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, vcpu
, 0);
384 if (ioctl(vcpu
, KVM_GET_SREGS
, &sregs
) == -1)
386 if (ioctl(vcpu
, KVM_SET_SREGS
, &sregs
) == -1)
389 if (ioctl(vcpu
, KVM_SET_REGS
, ®s
) == -1)
391 setfpregs(vcpu
, &fpregs
);
393 const struct kvm_guest_debug dbg
= {
394 .control
= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
396 if (ioctl(vcpu
, KVM_SET_GUEST_DEBUG
, &dbg
) == -1)
401 if (ioctl(vcpu
, KVM_RUN
, NULL
) == -1)
403 printf("Exited because %u\n", run
->exit_reason
);
409 free((char *) binpath
);