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 <stdbool.h> 73fdf659dSSasha Levin #include <linux/types.h> 8ce79f1caSPekka Enberg #include <time.h> 9ae1fae34SPekka Enberg 10ca99b3d3SCyrill Gorcunov #define KVM_NR_CPUS (255) 11ca99b3d3SCyrill Gorcunov 12ae1fae34SPekka Enberg struct kvm { 13ae1fae34SPekka Enberg int sys_fd; /* For system ioctls(), i.e. /dev/kvm */ 14ae1fae34SPekka Enberg int vm_fd; /* For VM ioctls() */ 15ce79f1caSPekka Enberg timer_t timerid; /* Posix timer for interrupts */ 16ae1fae34SPekka Enberg 170c7c14a7SCyrill Gorcunov int nrcpus; /* Number of cpus to run */ 180c7c14a7SCyrill Gorcunov 193fdf659dSSasha Levin u64 ram_size; 20ae1fae34SPekka Enberg void *ram_start; 21ae1fae34SPekka Enberg 222049569dSPekka Enberg bool nmi_disabled; 232049569dSPekka Enberg 243fdf659dSSasha Levin u16 boot_selector; 253fdf659dSSasha Levin u16 boot_ip; 263fdf659dSSasha Levin u16 boot_sp; 27edc8a14dSPekka Enberg 28da8883c1SPekka Enberg struct interrupt_table interrupt_table; 29ae1fae34SPekka Enberg }; 30ae1fae34SPekka Enberg 31192a99d1SCyrill Gorcunov struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size); 32*384922b3SPekka Enberg int kvm__max_cpus(struct kvm *self); 33839051d9SSasha Levin void kvm__init_ram(struct kvm *self); 349ef4c68eSPekka Enberg void kvm__delete(struct kvm *self); 352065a6f7SCyrill Gorcunov bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename, 362065a6f7SCyrill Gorcunov const char *initrd_filename, const char *kernel_cmdline); 37b3594ec7SCyrill Gorcunov void kvm__setup_bios(struct kvm *self); 38ce79f1caSPekka Enberg void kvm__start_timer(struct kvm *self); 39fbfe68b7SSasha Levin void kvm__stop_timer(struct kvm *self); 408b1ff07eSPekka Enberg void kvm__irq_line(struct kvm *self, int irq, int level); 413fdf659dSSasha Levin bool kvm__emulate_io(struct kvm *self, u16 port, void *data, int direction, int size, u32 count); 423fdf659dSSasha Levin bool kvm__emulate_mmio(struct kvm *self, u64 phys_addr, u8 *data, u32 len, u8 is_write); 43ae1fae34SPekka Enberg 44ae1fae34SPekka Enberg /* 45ae1fae34SPekka Enberg * Debugging 46ae1fae34SPekka Enberg */ 47090f898eSCyrill Gorcunov void kvm__dump_mem(struct kvm *self, unsigned long addr, unsigned long size); 48ae1fae34SPekka Enberg 49ae1fae34SPekka Enberg extern const char *kvm_exit_reasons[]; 50ae1fae34SPekka Enberg 519292f776SCyrill Gorcunov static inline bool host_ptr_in_ram(struct kvm *self, void *p) 529292f776SCyrill Gorcunov { 539292f776SCyrill Gorcunov return self->ram_start <= p && p < (self->ram_start + self->ram_size); 549292f776SCyrill Gorcunov } 559292f776SCyrill Gorcunov 563fdf659dSSasha Levin static inline u32 segment_to_flat(u16 selector, u16 offset) 579292f776SCyrill Gorcunov { 583fdf659dSSasha Levin return ((u32)selector << 4) + (u32) offset; 599292f776SCyrill Gorcunov } 609292f776SCyrill Gorcunov 619292f776SCyrill Gorcunov static inline void *guest_flat_to_host(struct kvm *self, unsigned long offset) 629292f776SCyrill Gorcunov { 639292f776SCyrill Gorcunov return self->ram_start + offset; 649292f776SCyrill Gorcunov } 659292f776SCyrill Gorcunov 663fdf659dSSasha Levin static inline void *guest_real_to_host(struct kvm *self, u16 selector, u16 offset) 679292f776SCyrill Gorcunov { 689292f776SCyrill Gorcunov unsigned long flat = segment_to_flat(selector, offset); 699292f776SCyrill Gorcunov 709292f776SCyrill Gorcunov return guest_flat_to_host(self, flat); 719292f776SCyrill Gorcunov } 729292f776SCyrill Gorcunov 73ae1fae34SPekka Enberg #endif /* KVM__KVM_H */ 74