Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

labs/templates: Update 7-kvm-vmm skel #409

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions tools/labs/templates/assignments/7-kvm-vmm/skel/Makefile
Original file line number Diff line number Diff line change
@@ -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
$(RM) kvm-hello-world vmm.o payload.o guest_16_bits/guest16.o \
guest64.o guest64.img guest64.img.o
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.code16
.global code16, code16_end
.global guest16, guest16_end
guest16:
movw $42, %ax
movw %ax, 0x400
hlt
guest16_end:
guest16_end:
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdint.h>
#include "device.h"

// Phys addr memory layout:
Expand Down Expand Up @@ -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);
Expand All @@ -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");
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@
typedef struct vcpu {
int fd;
struct kvm_run *kvm_run;
} virtual_cpu;
} virtual_cpu;

#endif
4 changes: 3 additions & 1 deletion tools/labs/templates/assignments/7-kvm-vmm/skel/include/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ typedef struct vm {
int sys_fd;
int fd;
char *mem;
} virtual_machine;
} virtual_machine;

#endif
4 changes: 2 additions & 2 deletions tools/labs/templates/assignments/7-kvm-vmm/skel/payload.ld
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ SECTIONS
{
.payload16 0 : {
guest16 = .;
guest16.o(.text)
guest_16_bits/guest16.o(.text)
guest16_end = .;
}
.payload64 0 : AT(LOADADDR(.payload16)+SIZEOF(.payload16)) {
guest64 = .;
guest64.img.o
guest64_end = .;
}
}
}
14 changes: 8 additions & 6 deletions tools/labs/templates/assignments/7-kvm-vmm/skel/vmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, &regs) < 0) {
if (ioctl(virtual_cpu.fd, KVM_GET_REGS, &regs) < 0) {
perror("KVM_GET_REGS");
exit(1);
}
Expand All @@ -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);
Expand All @@ -69,4 +71,4 @@ int main(int argc, char **argv) {

printf("%s\n", "Finished vmm");
return 0;
}
}