Skip to content

Instantly share code, notes, and snippets.

@gucchan22
Last active June 20, 2017 08:49
Show Gist options
  • Save gucchan22/db69d025f1173d59d3091d4239c9242b to your computer and use it in GitHub Desktop.
Save gucchan22/db69d025f1173d59d3091d4239c9242b to your computer and use it in GitHub Desktop.
--- /Users/gucchan/xhyve/hvdos/hvdos.c 2017-06-20 16:25:16.000000000 +0900
+++ hvdos.c 2017-06-20 17:44:12.000000000 +0900
@@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <Hypervisor/hv.h>
#include <Hypervisor/hv_vmx.h>
#include "vmcs.h"
@@ -65,10 +64,21 @@
return (ctrl | (cap & 0xffffffff)) & (cap >> 32);
}
+hv_x86_reg_t x86_debug_registers[10] = {
+ HV_X86_RIP, HV_X86_RFLAGS, HV_X86_RAX, HV_X86_RCX, HV_X86_RDX,
+ HV_X86_RBX, HV_X86_RSI, HV_X86_RDI, HV_X86_RSP, HV_X86_RBP
+};
+void debug_cpu_regs(hv_vcpuid_t vcpu, uint64_t *vmcs) {
+ int i;
+ for(i = 0; i < 10; i++) {
+ if(hv_vcpu_read_register(vcpu, x86_debug_registers[i], (uint64_t *)(vmcs + i)))
+ abort();
+ }
+}
+
int
main(int argc, char **argv)
{
- printf("PID: %d\n", getpid());
if (argc < 2) {
fprintf(stderr, "Usage: hvdos [com file]\n");
exit(1);
@@ -195,20 +205,24 @@
wreg(vcpu, HV_X86_RIP, 0x100);
wreg(vcpu, HV_X86_RFLAGS, 0x2);
wreg(vcpu, HV_X86_RSP, 0x0);
-
+
/* vCPU run loop */
int stop = 0;
+ uint64_t *reg_mems = (uint64_t *)malloc(sizeof(uint64_t) * 10);
do {
if (hv_vcpu_run(vcpu)) {
abort();
- }
+ }
/* handle VMEXIT */
uint64_t exit_reason = rvmcs(vcpu, VMCS_EXIT_REASON);
switch (exit_reason) {
case EXIT_REASON_EXCEPTION: {
uint8_t interrupt_number = rvmcs(vcpu, VMCS_IDT_VECTORING_INFO) & 0xFF;
- fprintf(stderr,"Interrupt Occured: %d\n", interrupt_number);
+ fprintf(stderr, "Interrupt Occured: %d\n", interrupt_number);
+ debug_cpu_regs(vcpu, reg_mems);
+ uint64_t *rs = reg_mems;
+ fprintf(stderr, "Registers: RIP:0x%X, RFLAGS:0x%X, RAX:0x%X, RCX:0x%X, RDX:0x%X, RBX:0x%X\n", rs[0],rs[1],rs[2],rs[3],rs[4],rs[5]);
int Status = Kernel.dispatch(interrupt_number);
switch (Status) {
case DOSKernel::STATUS_HANDLED:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment