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>
34 #define PAGE_SIZE (64 * 1024) // assumption
36 #define RAMSIZE (64 * 1024 * 1024)
37 #define PROGSTART 0x20000000
39 #define MSR_64 (1UL<<63)
40 #define MSR_LE (1UL<<0)
46 static void nukenewline(char buf
[]) {
48 for (i
= 0; buf
[i
]; i
++) {
56 static void help(const char argv0
[]) {
58 printf("Usage: %s [args] -i file.bin\n\n"
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"
69 "Load and dump may be given multiple times. GPR/FPR are numbered,\n"
70 "SPRs are named.\n", argv0
);
75 static void parseregs(const char name
[], const uint8_t gpr
, struct kvm_regs
*regs
,
76 struct kvm_fpu
*fpregs
) {
78 FILE *f
= fopen(name
, "r");
80 printf("Can't open %s\n", name
);
86 while (fgets(buf
, 256, f
)) {
91 const uint8_t reg
= strtol(buf
, NULL
, 0);
92 const char *ptr
= strchr(buf
+ 1, ' ');
94 printf("Invalid line '%s'\n", buf
);
98 const uint64_t val
= strtol(ptr
, NULL
, 0);
102 regs
->gpr
[reg
] = val
;
104 fpregs
->fpr
[reg
] = val
;
111 static void parsesprs(const char name
[], struct kvm_regs
*regs
) {
113 FILE *f
= fopen(name
, "r");
115 printf("Can't open %s\n", name
);
121 while (fgets(buf
, 256, f
)) {
128 #define check(a) if (!strncasecmp(buf, a, sizeof(a) - 1))
137 printf("Unknown (unimplemented?) SPR register '%s'\n",
142 const char *ptr
= strchr(buf
+ 1, ' ');
144 printf("Invalid line '%s'\n", buf
);
148 const uint64_t val
= strtol(ptr
, NULL
, 0);
161 static void load(const char arg
[], uint8_t *ram
) {
164 const char *ptr
= strchr(arg
, ':');
166 printf("Invalid load\n");
170 const unsigned namelen
= ptr
- arg
;
172 strncpy(name
, arg
, namelen
);
173 name
[namelen
] = '\0';
175 const uint64_t addr
= strtol(ptr
+ 1, NULL
, 0);
177 FILE *f
= fopen(name
, "r");
179 printf("Can't open %s\n", name
);
183 fseek(f
, 0, SEEK_END
);
184 const unsigned len
= ftell(f
);
187 if (addr
+ len
>= RAMSIZE
) {
188 printf("Tried to use too much RAM\n");
192 printf("Loading %s to 0x%lx, %u bytes\n", name
, addr
, len
);
194 if (fread(&ram
[addr
], len
, 1, f
) != 1)
200 static void dump(const char src
[], const uint8_t ram
[]) {
201 const char *ptr
= strchr(src
, ':');
203 printf("Invalid dump\n");
208 memcpy(name
, src
, ptr
- src
);
209 name
[ptr
- src
] = '\0';
212 const uint32_t addr
= strtol(ptr
, NULL
, 0);
214 ptr
= strchr(ptr
, ':');
216 printf("Invalid dump\n");
221 const uint32_t len
= strtol(ptr
, NULL
, 0);
223 //printf("Saving to %s, from %x, len %u\n", name, addr, len);
225 FILE *f
= fopen(name
, "w");
227 printf("Can't open %s\n", name
);
231 if (fwrite(&ram
[addr
], len
, 1, f
) != 1)
237 static void setfpregs(const int vcpu
, struct kvm_fpu
*fpregs
) {
238 // KVM_[SG]ET_FPU isn't supported on PPC, we have to move individual regs...
240 for (i
= 0; i
< 32; i
++) {
241 struct kvm_one_reg r
= {
242 .id
= KVM_REG_PPC_FPR(i
),
243 .addr
= (uint64_t) &fpregs
->fpr
[i
]
246 if (ioctl(vcpu
, KVM_SET_ONE_REG
, &r
) != 0)
251 static void getfpregs(const int vcpu
, struct kvm_fpu
*fpregs
) {
252 // KVM_[SG]ET_FPU isn't supported on PPC, we have to move individual regs...
254 for (i
= 0; i
< 32; i
++) {
255 struct kvm_one_reg r
= {
256 .id
= KVM_REG_PPC_FPR(i
),
257 .addr
= (uint64_t) &fpregs
->fpr
[i
]
260 if (ioctl(vcpu
, KVM_GET_ONE_REG
, &r
) != 0)
265 int main(int argc
, char **argv
) {
267 const struct option longopts
[] = {
268 { "binary", 1, NULL
, 'i' },
269 { "intregs", 1, NULL
, 'g' },
270 { "fpregs", 1, NULL
, 'f' },
271 { "spregs", 1, NULL
, 's' },
272 { "load", 1, NULL
, 'l' },
273 { "dump", 1, NULL
, 'd' },
274 { "trace", 1, NULL
, 't' },
275 { "help", 0, NULL
, 'h' },
278 const char opts
[] = "i:g:f:s:l:d:t:h";
281 struct kvm_regs regs
;
282 struct kvm_sregs sregs
;
283 struct kvm_fpu fpregs
;
285 FILE *binary
= NULL
, *trace
= NULL
;
286 char dumps
[MAXDUMPS
][PATH_MAX
];
287 unsigned num_dumps
= 0;
288 uint8_t *ram
, *progmem
;
292 if (posix_memalign((void **) &ram
, 64 * 1024, RAMSIZE
))
294 memset(ram
, 0, RAMSIZE
);
296 memset(®s
, 0, sizeof(struct kvm_regs
));
297 memset(&fpregs
, 0, sizeof(struct kvm_fpu
));
301 regs
.msr
= MSR_64
| MSR_LE
;
304 const int c
= getopt_long(argc
, argv
, opts
, longopts
, NULL
);
311 printf("Only one binary allowed\n");
315 binary
= fopen(optarg
, "r");
317 printf("Failed to open %s\n", optarg
);
321 binpath
= strdup(optarg
);
324 parseregs(optarg
, 1, ®s
, &fpregs
);
327 parseregs(optarg
, 0, ®s
, &fpregs
);
330 parsesprs(optarg
, ®s
);
336 if (num_dumps
>= MAXDUMPS
) {
337 printf("Too many dumps\n");
341 strncpy(dumps
[num_dumps
], optarg
, PATH_MAX
);
342 dumps
[num_dumps
][PATH_MAX
- 1] = '\0';
347 printf("Only one trace allowed\n");
351 trace
= fopen(optarg
, "w");
353 printf("Failed to open %s\n", optarg
);
368 fseek(binary
, 0, SEEK_END
);
369 const unsigned binlen
= ftell(binary
);
372 printf("Loading binary %u bytes\n", binlen
);
374 const unsigned progmemlen
= (binlen
+ (PAGE_SIZE
- 1)) & ~(PAGE_SIZE
- 1);
376 if (posix_memalign((void **) &progmem
, 64 * 1024, progmemlen
))
379 if (fread(progmem
, binlen
, 1, binary
) != 1)
383 kvm
= open("/dev/kvm", O_RDWR
| O_CLOEXEC
);
385 printf("Failed to open kvm, perhaps you lack permissions?\n");
390 ret
= ioctl(kvm
, KVM_GET_API_VERSION
, NULL
);
391 if (ret
== -1 || ret
!= 12) abort();
393 ret
= ioctl(kvm
, KVM_CHECK_EXTENSION
, KVM_CAP_PPC_GUEST_DEBUG_SSTEP
);
394 if (ret
== -1 || !ret
) {
395 printf("This system lacks single-stepping!\n");
399 vmfd
= ioctl(kvm
, KVM_CREATE_VM
, (unsigned long)0);
401 struct kvm_userspace_memory_region region
= {
403 .guest_phys_addr
= 0,
404 .memory_size
= RAMSIZE
,
405 .userspace_addr
= (uint64_t) ram
,
408 if (ioctl(vmfd
, KVM_SET_USER_MEMORY_REGION
, ®ion
) == -1)
412 region
.guest_phys_addr
= PROGSTART
;
413 region
.memory_size
= progmemlen
;
414 region
.userspace_addr
= (uint64_t) progmem
;
415 if (ioctl(vmfd
, KVM_SET_USER_MEMORY_REGION
, ®ion
) == -1)
418 vcpu
= ioctl(vmfd
, KVM_CREATE_VCPU
, (unsigned long)0);
419 const unsigned vcpulen
= ioctl(kvm
, KVM_GET_VCPU_MMAP_SIZE
, NULL
);
421 run
= mmap(NULL
, vcpulen
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, vcpu
, 0);
423 if (ioctl(vcpu
, KVM_GET_SREGS
, &sregs
) == -1)
425 // It defaults to host cpu on POWER5+, which is what we want.
426 // http://pearpc.sourceforge.net/pvr.html
427 if (ioctl(vcpu
, KVM_SET_SREGS
, &sregs
) == -1)
430 if (ioctl(vcpu
, KVM_SET_REGS
, ®s
) == -1)
432 setfpregs(vcpu
, &fpregs
);
434 const struct kvm_guest_debug dbg
= {
435 .control
= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_SINGLESTEP
437 if (ioctl(vcpu
, KVM_SET_GUEST_DEBUG
, &dbg
) == -1)
442 if (ioctl(vcpu
, KVM_RUN
, NULL
) == -1)
444 printf("Exited because %u\n", run
->exit_reason
);
448 for (i
= 0; i
< num_dumps
; i
++) {
455 free((char *) binpath
);