xref: /kvmtool/include/kvm/kvm.h (revision 874467f89059aa161302b7ad6336b0043b1522ad)
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)
11*874467f8SSasha Levin #define KVM_32BIT_GAP_SIZE	(512 << 20)
12*874467f8SSasha Levin #define KVM_32BIT_GAP_START	((1ULL << 32) - KVM_32BIT_GAP_SIZE)
13ca99b3d3SCyrill Gorcunov 
14ae1fae34SPekka Enberg struct kvm {
15ae1fae34SPekka Enberg 	int			sys_fd;		/* For system ioctls(), i.e. /dev/kvm */
16ae1fae34SPekka Enberg 	int			vm_fd;		/* For VM ioctls() */
17ce79f1caSPekka Enberg 	timer_t			timerid;	/* Posix timer for interrupts */
18ae1fae34SPekka Enberg 
190c7c14a7SCyrill Gorcunov 	int			nrcpus;		/* Number of cpus to run */
200c7c14a7SCyrill Gorcunov 
213fdf659dSSasha Levin 	u64			ram_size;
22ae1fae34SPekka Enberg 	void			*ram_start;
23ae1fae34SPekka Enberg 
242049569dSPekka Enberg 	bool			nmi_disabled;
252049569dSPekka Enberg 
263fdf659dSSasha Levin 	u16			boot_selector;
273fdf659dSSasha Levin 	u16			boot_ip;
283fdf659dSSasha Levin 	u16			boot_sp;
29edc8a14dSPekka Enberg 
30da8883c1SPekka Enberg 	struct interrupt_table	interrupt_table;
31ae1fae34SPekka Enberg };
32ae1fae34SPekka Enberg 
33192a99d1SCyrill Gorcunov struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size);
34384922b3SPekka Enberg int kvm__max_cpus(struct kvm *self);
35839051d9SSasha Levin void kvm__init_ram(struct kvm *self);
369ef4c68eSPekka Enberg void kvm__delete(struct kvm *self);
372065a6f7SCyrill Gorcunov bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename,
382065a6f7SCyrill Gorcunov 			const char *initrd_filename, const char *kernel_cmdline);
39b3594ec7SCyrill Gorcunov void kvm__setup_bios(struct kvm *self);
40ce79f1caSPekka Enberg void kvm__start_timer(struct kvm *self);
41fbfe68b7SSasha Levin void kvm__stop_timer(struct kvm *self);
428b1ff07eSPekka Enberg void kvm__irq_line(struct kvm *self, int irq, int level);
433fdf659dSSasha Levin bool kvm__emulate_io(struct kvm *self, u16 port, void *data, int direction, int size, u32 count);
443fdf659dSSasha Levin bool kvm__emulate_mmio(struct kvm *self, u64 phys_addr, u8 *data, u32 len, u8 is_write);
45ae1fae34SPekka Enberg 
46ae1fae34SPekka Enberg /*
47ae1fae34SPekka Enberg  * Debugging
48ae1fae34SPekka Enberg  */
49090f898eSCyrill Gorcunov void kvm__dump_mem(struct kvm *self, unsigned long addr, unsigned long size);
50ae1fae34SPekka Enberg 
51ae1fae34SPekka Enberg extern const char *kvm_exit_reasons[];
52ae1fae34SPekka Enberg 
539292f776SCyrill Gorcunov static inline bool host_ptr_in_ram(struct kvm *self, void *p)
549292f776SCyrill Gorcunov {
559292f776SCyrill Gorcunov 	return self->ram_start <= p && p < (self->ram_start + self->ram_size);
569292f776SCyrill Gorcunov }
579292f776SCyrill Gorcunov 
583fdf659dSSasha Levin static inline u32 segment_to_flat(u16 selector, u16 offset)
599292f776SCyrill Gorcunov {
603fdf659dSSasha Levin 	return ((u32)selector << 4) + (u32) offset;
619292f776SCyrill Gorcunov }
629292f776SCyrill Gorcunov 
639292f776SCyrill Gorcunov static inline void *guest_flat_to_host(struct kvm *self, unsigned long offset)
649292f776SCyrill Gorcunov {
659292f776SCyrill Gorcunov 	return self->ram_start + offset;
669292f776SCyrill Gorcunov }
679292f776SCyrill Gorcunov 
683fdf659dSSasha Levin static inline void *guest_real_to_host(struct kvm *self, u16 selector, u16 offset)
699292f776SCyrill Gorcunov {
709292f776SCyrill Gorcunov 	unsigned long flat = segment_to_flat(selector, offset);
719292f776SCyrill Gorcunov 
729292f776SCyrill Gorcunov 	return guest_flat_to_host(self, flat);
739292f776SCyrill Gorcunov }
749292f776SCyrill Gorcunov 
75ae1fae34SPekka Enberg #endif /* KVM__KVM_H */
76