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> 7*3fdf659dSSasha 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 19*3fdf659dSSasha Levin u64 ram_size; 20ae1fae34SPekka Enberg void *ram_start; 21ae1fae34SPekka Enberg 222049569dSPekka Enberg bool nmi_disabled; 232049569dSPekka Enberg 24*3fdf659dSSasha Levin u16 boot_selector; 25*3fdf659dSSasha Levin u16 boot_ip; 26*3fdf659dSSasha 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); 32839051d9SSasha Levin void kvm__init_ram(struct kvm *self); 339ef4c68eSPekka Enberg void kvm__delete(struct kvm *self); 342065a6f7SCyrill Gorcunov bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename, 352065a6f7SCyrill Gorcunov const char *initrd_filename, const char *kernel_cmdline); 36b3594ec7SCyrill Gorcunov void kvm__setup_bios(struct kvm *self); 37ce79f1caSPekka Enberg void kvm__start_timer(struct kvm *self); 38fbfe68b7SSasha Levin void kvm__stop_timer(struct kvm *self); 398b1ff07eSPekka Enberg void kvm__irq_line(struct kvm *self, int irq, int level); 40*3fdf659dSSasha Levin bool kvm__emulate_io(struct kvm *self, u16 port, void *data, int direction, int size, u32 count); 41*3fdf659dSSasha Levin bool kvm__emulate_mmio(struct kvm *self, u64 phys_addr, u8 *data, u32 len, u8 is_write); 42ae1fae34SPekka Enberg 43ae1fae34SPekka Enberg /* 44ae1fae34SPekka Enberg * Debugging 45ae1fae34SPekka Enberg */ 46090f898eSCyrill Gorcunov void kvm__dump_mem(struct kvm *self, unsigned long addr, unsigned long size); 47ae1fae34SPekka Enberg 48ae1fae34SPekka Enberg extern const char *kvm_exit_reasons[]; 49ae1fae34SPekka Enberg 509292f776SCyrill Gorcunov static inline bool host_ptr_in_ram(struct kvm *self, void *p) 519292f776SCyrill Gorcunov { 529292f776SCyrill Gorcunov return self->ram_start <= p && p < (self->ram_start + self->ram_size); 539292f776SCyrill Gorcunov } 549292f776SCyrill Gorcunov 55*3fdf659dSSasha Levin static inline u32 segment_to_flat(u16 selector, u16 offset) 569292f776SCyrill Gorcunov { 57*3fdf659dSSasha Levin return ((u32)selector << 4) + (u32) offset; 589292f776SCyrill Gorcunov } 599292f776SCyrill Gorcunov 609292f776SCyrill Gorcunov static inline void *guest_flat_to_host(struct kvm *self, unsigned long offset) 619292f776SCyrill Gorcunov { 629292f776SCyrill Gorcunov return self->ram_start + offset; 639292f776SCyrill Gorcunov } 649292f776SCyrill Gorcunov 65*3fdf659dSSasha Levin static inline void *guest_real_to_host(struct kvm *self, u16 selector, u16 offset) 669292f776SCyrill Gorcunov { 679292f776SCyrill Gorcunov unsigned long flat = segment_to_flat(selector, offset); 689292f776SCyrill Gorcunov 699292f776SCyrill Gorcunov return guest_flat_to_host(self, flat); 709292f776SCyrill Gorcunov } 719292f776SCyrill Gorcunov 72ae1fae34SPekka Enberg #endif /* KVM__KVM_H */ 73