1ae1fae34SPekka Enberg #ifndef KVM__KVM_H 2ae1fae34SPekka Enberg #define KVM__KVM_H 3ae1fae34SPekka Enberg 4da8883c1SPekka Enberg #include "kvm/interrupt.h" 5da8883c1SPekka Enberg 6ae1fae34SPekka Enberg #include <linux/kvm.h> /* for struct kvm_regs */ 7ae1fae34SPekka Enberg 8ae1fae34SPekka Enberg #include <stdbool.h> 9ae1fae34SPekka Enberg #include <stdint.h> 10ae1fae34SPekka Enberg 11ae1fae34SPekka Enberg struct kvm { 12ae1fae34SPekka Enberg int sys_fd; /* For system ioctls(), i.e. /dev/kvm */ 13ae1fae34SPekka Enberg int vm_fd; /* For VM ioctls() */ 14ae1fae34SPekka Enberg int vcpu_fd; /* For VCPU ioctls() */ 15ae1fae34SPekka Enberg struct kvm_run *kvm_run; 16ae1fae34SPekka Enberg 17ae1fae34SPekka Enberg uint64_t ram_size; 18ae1fae34SPekka Enberg void *ram_start; 19ae1fae34SPekka Enberg 202049569dSPekka Enberg bool nmi_disabled; 212049569dSPekka Enberg 22dbdb74c2SPekka Enberg uint16_t boot_selector; 23edc8a14dSPekka Enberg uint16_t boot_ip; 24dbdb74c2SPekka Enberg uint16_t boot_sp; 25edc8a14dSPekka Enberg 26ae1fae34SPekka Enberg struct kvm_regs regs; 2753602077SPekka Enberg struct kvm_sregs sregs; 28ce4d0766SPekka Enberg struct kvm_fpu fpu; 2953d48714SPekka Enberg struct kvm_msrs *msrs; /* dynamically allocated */ 30da8883c1SPekka Enberg 31da8883c1SPekka Enberg struct interrupt_table interrupt_table; 32ae1fae34SPekka Enberg }; 33ae1fae34SPekka Enberg 34192a99d1SCyrill Gorcunov struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size); 359ef4c68eSPekka Enberg void kvm__delete(struct kvm *self); 36a1fe6bc5SPekka Enberg void kvm__setup_cpuid(struct kvm *self); 37ae1fae34SPekka Enberg void kvm__enable_singlestep(struct kvm *self); 38*2065a6f7SCyrill Gorcunov bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename, 39*2065a6f7SCyrill Gorcunov const char *initrd_filename, const char *kernel_cmdline); 407fb218bdSPekka Enberg void kvm__reset_vcpu(struct kvm *self); 412f3976eeSPekka Enberg void kvm__setup_mem(struct kvm *self); 42ae1fae34SPekka Enberg void kvm__run(struct kvm *self); 432049569dSPekka Enberg bool kvm__emulate_io(struct kvm *self, uint16_t port, void *data, int direction, int size, uint32_t count); 4429443dabSPekka Enberg bool kvm__emulate_mmio(struct kvm *self, uint64_t phys_addr, uint8_t *data, uint32_t len, uint8_t is_write); 45ae1fae34SPekka Enberg 46ae1fae34SPekka Enberg /* 47ae1fae34SPekka Enberg * Debugging 48ae1fae34SPekka Enberg */ 49ae1fae34SPekka Enberg void kvm__show_code(struct kvm *self); 50ae1fae34SPekka Enberg void kvm__show_registers(struct kvm *self); 51f01944c8SPekka Enberg void kvm__show_page_tables(struct kvm *self); 52090f898eSCyrill Gorcunov void kvm__dump_mem(struct kvm *self, unsigned long addr, unsigned long size); 53ae1fae34SPekka Enberg 54ae1fae34SPekka Enberg extern const char *kvm_exit_reasons[]; 55ae1fae34SPekka Enberg 569292f776SCyrill Gorcunov static inline bool host_ptr_in_ram(struct kvm *self, void *p) 579292f776SCyrill Gorcunov { 589292f776SCyrill Gorcunov return self->ram_start <= p && p < (self->ram_start + self->ram_size); 599292f776SCyrill Gorcunov } 609292f776SCyrill Gorcunov 619292f776SCyrill Gorcunov static inline uint32_t segment_to_flat(uint16_t selector, uint16_t offset) 629292f776SCyrill Gorcunov { 639292f776SCyrill Gorcunov return ((uint32_t)selector << 4) + (uint32_t) offset; 649292f776SCyrill Gorcunov } 659292f776SCyrill Gorcunov 669292f776SCyrill Gorcunov static inline void *guest_flat_to_host(struct kvm *self, unsigned long offset) 679292f776SCyrill Gorcunov { 689292f776SCyrill Gorcunov return self->ram_start + offset; 699292f776SCyrill Gorcunov } 709292f776SCyrill Gorcunov 719292f776SCyrill Gorcunov static inline void *guest_real_to_host(struct kvm *self, uint16_t selector, uint16_t offset) 729292f776SCyrill Gorcunov { 739292f776SCyrill Gorcunov unsigned long flat = segment_to_flat(selector, offset); 749292f776SCyrill Gorcunov 759292f776SCyrill Gorcunov return guest_flat_to_host(self, flat); 769292f776SCyrill Gorcunov } 779292f776SCyrill Gorcunov 78ae1fae34SPekka Enberg #endif /* KVM__KVM_H */ 79