diff --git a/tools/labs/templates/assignments/7-kvm-vmm/skel/Makefile b/tools/labs/templates/assignments/7-kvm-vmm/skel/Makefile index cb9e9d529bd4da..47baf1ac28527a 100644 --- a/tools/labs/templates/assignments/7-kvm-vmm/skel/Makefile +++ b/tools/labs/templates/assignments/7-kvm-vmm/skel/Makefile @@ -1,24 +1,25 @@ -CFLAGS = -Wall -Wextra -O2 +CFLAGS = -g -Wall -Wextra -O0 -Iinclude/ -run64: kvm-hello-world +.PHONY: run +run: kvm-hello-world ./kvm-hello-world -kvm-hello-world: vmm.o payload.o virtual_machine.o virtual_cpu.o +kvm-hello-world: vmm.o payload.o $(CC) $^ -o $@ -payload.o: payload.ld guest64.img.o guest16.o - $(LD) -T $< -o $@ +payload.o: payload.ld guest_16_bits/guest16.o guest64.img.o + $(LD) -r -T $< -o $@ -guest64.o: guest_code.c +guest64.o: guest_32_bits/guest_code.c $(CC) $(CFLAGS) -m64 -ffreestanding -fno-pic -c -o $@ $^ guest64.img: guest64.o - $(LD) -T guest.ld $^ -o $@ + $(LD) -T guest_32_bits/guest.ld $^ -o $@ %.img.o: %.img $(LD) -b binary -r $^ -o $@ .PHONY: clean clean: - $(RM) kvm-hello-world kvm-hello-world.o payload.o \ - *.o *.img \ No newline at end of file + $(RM) kvm-hello-world vmm.o payload.o guest_16_bits/guest16.o \ + guest64.o guest64.img guest64.img.o diff --git a/tools/labs/templates/assignments/7-kvm-vmm/skel/guest_16_bits/guest16.s b/tools/labs/templates/assignments/7-kvm-vmm/skel/guest_16_bits/guest16.s index 2b93a10460d659..a290e1aa9c6371 100644 --- a/tools/labs/templates/assignments/7-kvm-vmm/skel/guest_16_bits/guest16.s +++ b/tools/labs/templates/assignments/7-kvm-vmm/skel/guest_16_bits/guest16.s @@ -1,7 +1,7 @@ .code16 - .global code16, code16_end + .global guest16, guest16_end guest16: movw $42, %ax movw %ax, 0x400 hlt -guest16_end: \ No newline at end of file +guest16_end: diff --git a/tools/labs/templates/assignments/7-kvm-vmm/skel/guest_16_bits/guest.ld b/tools/labs/templates/assignments/7-kvm-vmm/skel/guest_32_bits/guest.ld similarity index 100% rename from tools/labs/templates/assignments/7-kvm-vmm/skel/guest_16_bits/guest.ld rename to tools/labs/templates/assignments/7-kvm-vmm/skel/guest_32_bits/guest.ld diff --git a/tools/labs/templates/assignments/7-kvm-vmm/skel/guest_32_bits/guest_code.c b/tools/labs/templates/assignments/7-kvm-vmm/skel/guest_32_bits/guest_code.c index a60edde8eed15f..d71e0521df3611 100644 --- a/tools/labs/templates/assignments/7-kvm-vmm/skel/guest_32_bits/guest_code.c +++ b/tools/labs/templates/assignments/7-kvm-vmm/skel/guest_32_bits/guest_code.c @@ -1,3 +1,4 @@ +#include #include "device.h" // Phys addr memory layout: @@ -48,6 +49,7 @@ void __attribute__((noreturn)) __attribute__((section(".start"))) _start(void) { + const char *p; for (p = "Hello, world!\n"; *p; ++p) outb(0xE9, *p); @@ -57,4 +59,5 @@ _start(void) { /* TODO: Using the paravirtualized driver we have written for SIMVIRTIO, send "Ana are mere!\n" */ + asm("hlt" : /* empty */ : "a" (42) : "memory"); } diff --git a/tools/labs/templates/assignments/7-kvm-vmm/skel/include/vcpu.h b/tools/labs/templates/assignments/7-kvm-vmm/skel/include/vcpu.h index c16ec044d07719..65f3be27ccde44 100644 --- a/tools/labs/templates/assignments/7-kvm-vmm/skel/include/vcpu.h +++ b/tools/labs/templates/assignments/7-kvm-vmm/skel/include/vcpu.h @@ -61,4 +61,6 @@ typedef struct vcpu { int fd; struct kvm_run *kvm_run; -} virtual_cpu; \ No newline at end of file +} virtual_cpu; + +#endif diff --git a/tools/labs/templates/assignments/7-kvm-vmm/skel/include/vm.h b/tools/labs/templates/assignments/7-kvm-vmm/skel/include/vm.h index 5af83c85e5ef09..09eba9a12b2ff0 100644 --- a/tools/labs/templates/assignments/7-kvm-vmm/skel/include/vm.h +++ b/tools/labs/templates/assignments/7-kvm-vmm/skel/include/vm.h @@ -6,4 +6,6 @@ typedef struct vm { int sys_fd; int fd; char *mem; -} virtual_machine; \ No newline at end of file +} virtual_machine; + +#endif diff --git a/tools/labs/templates/assignments/7-kvm-vmm/skel/payload.ld b/tools/labs/templates/assignments/7-kvm-vmm/skel/payload.ld index 7476a14372cfe8..bd09024dfbf27e 100644 --- a/tools/labs/templates/assignments/7-kvm-vmm/skel/payload.ld +++ b/tools/labs/templates/assignments/7-kvm-vmm/skel/payload.ld @@ -2,7 +2,7 @@ SECTIONS { .payload16 0 : { guest16 = .; - guest16.o(.text) + guest_16_bits/guest16.o(.text) guest16_end = .; } .payload64 0 : AT(LOADADDR(.payload16)+SIZEOF(.payload16)) { @@ -10,4 +10,4 @@ SECTIONS guest64.img.o guest64_end = .; } -} \ No newline at end of file +} diff --git a/tools/labs/templates/assignments/7-kvm-vmm/skel/vmm.c b/tools/labs/templates/assignments/7-kvm-vmm/skel/vmm.c index 77caf419687514..6c5955117f8e32 100644 --- a/tools/labs/templates/assignments/7-kvm-vmm/skel/vmm.c +++ b/tools/labs/templates/assignments/7-kvm-vmm/skel/vmm.c @@ -18,17 +18,19 @@ int main(int argc, char **argv) { UNUSED_PARAMETER(argv); struct vm virtual_machine; struct vcpu virtual_cpu; + struct kvm_regs regs; + uint16_t memval; /* TODO: Initialize the VM. We will use 0x100000 bytes for the memory */ /* TODO: Initialize the VCPU */ - /* TODO: Setup real mode. We will use guest_16_bits to test this. + /* TODO: Setup real mode. We will use guest_16_bits to test this. */ /* TODO: IF real mode works all right. We can try to set up long mode*/ for (;;) { /* TODO: Run the VCPU with KVM_RUN */ /* TODO: Handle VMEXITs */ - switch (vcpu->kvm_run->exit_reason) { + switch (virtual_cpu.kvm_run->exit_reason) { case KVM_EXIT_HLT: {goto check;} case KVM_EXIT_MMIO: { /* TODO: Handle MMIO read/write. Data is available in the shared memory at @@ -42,13 +44,13 @@ int main(int argc, char **argv) { fprintf(stderr, "\nGot exit_reason %d," " expected KVM_EXIT_HLT (%d)\n", - vcpu->kvm_run->exit_reason, KVM_EXIT_HLT); + virtual_cpu.kvm_run->exit_reason, KVM_EXIT_HLT); exit(1); } /* We verify that the guest code ran accordingly */ check: - if (ioctl(vcpu->fd, KVM_GET_REGS, ®s) < 0) { + if (ioctl(virtual_cpu.fd, KVM_GET_REGS, ®s) < 0) { perror("KVM_GET_REGS"); exit(1); } @@ -60,7 +62,7 @@ int main(int argc, char **argv) { } /* Verify that the guest has written 42 at 0x400 */ - memcpy(&memval, &vm->mem[0x400], sz); + memcpy(&memval, &virtual_machine.mem[0x400], sizeof(uint16_t)); if (memval != 42) { printf("Wrong result: memory at 0x400 is %lld\n", (unsigned long long)memval); @@ -69,4 +71,4 @@ int main(int argc, char **argv) { printf("%s\n", "Finished vmm"); return 0; -} \ No newline at end of file +}