Commit Graph

449 Commits

Author SHA1 Message Date
Pekka Enberg 54d4a62605 kvm: Fix setup size calculation
I need a brown paper bag.  When converting sectors to bytes, we need to shift
by 9 ("512"), not 4 ("16")...

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 2aeaf53472 Revert "kvm: Enable 'interrupt request window'"
This reverts commit c1a099311e695bfe1caca9a11e294fbd70d3ba11.  Interrupt
request windows are for host to guest interrupt injection.  Software interrupts
and traps happen 100% in the guest.
2015-06-01 16:39:38 +01:00
Pekka Enberg 616e110816 kvm: Enable 'interrupt request window'
This patch enables KVM 'interrupt request window' so that KVM_RUN returns when
we hit an interrupt.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 4311c21d91 kvm: Document the 'reset vector' hack
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 5d67eaf6e3 kvm: Clean up reset vector setup code
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg fe806d6570 kvm: Add some single-step debugging and don't stop running
Now that we can execute some instructions, don't stop on first single step but
dump some debugging output instead and continue running.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 2a601aafee kvm: Hardcode a "reset vector" at f000:fff0
It seems that if we attempt to run code at any other location than f000:fff0,
we get an KVM_EXIT_UNKNOWN error. To work around that, hardcode a "reset
vector" at f000:fff0 that jumps to 0000:0x7c000 and load the kernel at the
latter location.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg e93ab78a73 kvm: Read the whole bzImage to memory
We need to read the whole bzImage to memory; otherwise we miss some of the
setup code.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 10943d14eb kvm: Fix bzImage setup sector default count
If setup sectors is zero, the real value is 4 as per
Documentation/x86/boot.txt.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 7fb218bdd0 kvm: Load bzImage real-mode boot sector and setup code
We start KVM in 16-bit mode so we should set cs:ip to point to the real-mode
boot sector and setup code, not the protected mode kernel that starts at 1M.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 009b07588a Add support for loading flat binaries
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 3d2312e507 Add a simple "kernel" binary that can be used for testing
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Cyrill Gorcunov 07f9d0dbf9 Add info() helper
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg ce55663629 Print 'avl' bits of a segment
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Cyrill Gorcunov 60baf5923d Makefile: Add CPPFLAGS handling
Some developers may need to pass specific flags
to compiler, usually due to different include/
directory and etc. So add well known CPPFLAGS
here.

Example:

$ CPPFLAGS="-I/home/include" make

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 06e41eeadc Fix up 'ip' in kvm__reset_vcpu() to take real mode into account
We can't just set 'rip' to 1 MB mark because in real mode, 'ip' is just 16 bits
wide.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg ce5e0ecbbf Improve KVM register dump output
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 7085d9359d Fix LDT descriptor type for KVM_SET_SREG
This patch fixes the KVM_EXIT_UNKNOWN error when running bzImage.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 5360207747 Setup segmentation for the guest
This patch adds segmentation setup for the guest. It's not perfect yet as we
get KVM_EXIT_UNKNOWN but it's a start nevertheless.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg ae1fae34a8 Separate KVM code from the main loop
This patch separates the KVM code in its own file and cleans everything up a
bit while at it.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Cyrill Gorcunov b625d4b6aa Don't die on old kernels without single-step debug support
Better inform the user that the host kernel do not
support single-stepping debug.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Cyrill Gorcunov ad054a21d2 Add utility functions
Move helpers out to separate files.
Mostly borrowed from perf

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg aee6632eee Enable KVM guest single-stepping
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg e3a07fc182 Move KVM_SET_REGS to kvm__reset_vcpu()
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 8d20223edc Dump KVM_EXIT_IO details
When KVM_RUN returns with KVM_EXIT_IO, dump the I/O details and exit.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 2177ec43a9 Fix up rflags printing
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg a2fe61994f Setup rflags properly to fix EXIT_UNKNOWN
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 6e8abc3811 Fix guest code printout
Doh, forgot to initialize 'c'...

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 6f10be05fc Show dump of executed code at KVM exit
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg d5741c92aa Print out constant names for KVM exit reasons
Things are way easier to debug that way.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 8e12640544 Initialize RIP before starting to run the guest
Use the return value of load_kernel() to initialize RIP register before
starting the guest with KVM_RUN ioctl().

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 7118d2cabe Dump registers after KVM_RUN returns
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 696a1b0a7d Update .gitignore to include object files
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg ee0e89c5f1 Load bzImage to guest RAM
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 0d1f17ec4d Initialize guest memory region properly
Use posix_memalign() to allocate memory for the guest and register the region
to KVM.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg f5ab5f67ff Initial version of kernel image loading
This patch adds support for checking whether the provided kernel image is a
bzImage or not.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg caf44df2f8 Load kernel image to the VM
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg b6e5a081c5 Move CPU emulation code to cpu.c
The kvm.c file is getting bigger so move CPU emulation code to a separate file.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 1f9cff2305 Mmap the kvm_run structure
Mmap the kvm_run structure and print out ->exit_reason in human-readable form
upon exit to make it more visible what's happening after KVM_RUN ioctl() is
called.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 73ac60e636 Clean up struct kvm file descriptor names
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 895c2fef34 Set up TSS address to make vcpu creation work
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 2b0e334253 Initialize KVM some more
This patch adds some more KVM initialization code. Doesn't work yet, though.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 4076b0411d Clean up KVM initialization
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 2da26a59d3 Check for KVM_CAP_USER_MEMORY
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 9b1fb1c3dc Move KVM fds into 'struct kvm'
This patch introduces a 'struct kvm' and moves the KVM related file descriptors
into it so that we can introduce helper functions more easily.

Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 28fa19c020 Create a VM using ioctl(KVM_CREATE_VM)
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg e1f3acc598 Add .gitignore file
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg 6c7d85141b Check for KVM_API_VERSION at init
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00
Pekka Enberg b8f6afcd5e Initial commit
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
2015-06-01 16:39:38 +01:00