xref: /kvmtool/include/kvm/kvm.h (revision ca99b3d34022301a54d55d8ca4d00d5721832431)
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>
7ae1fae34SPekka Enberg #include <stdint.h>
8ce79f1caSPekka Enberg #include <time.h>
9ae1fae34SPekka Enberg 
10*ca99b3d3SCyrill Gorcunov #define KVM_NR_CPUS		(255)
11*ca99b3d3SCyrill 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 
199f532d00SPekka Enberg 	struct disk_image	*disk_image;
20ae1fae34SPekka Enberg 	uint64_t		ram_size;
21ae1fae34SPekka Enberg 	void			*ram_start;
22ae1fae34SPekka Enberg 
232049569dSPekka Enberg 	bool			nmi_disabled;
242049569dSPekka Enberg 
25dbdb74c2SPekka Enberg 	uint16_t		boot_selector;
26edc8a14dSPekka Enberg 	uint16_t		boot_ip;
27dbdb74c2SPekka Enberg 	uint16_t		boot_sp;
28edc8a14dSPekka Enberg 
29da8883c1SPekka Enberg 	struct interrupt_table	interrupt_table;
30ae1fae34SPekka Enberg };
31ae1fae34SPekka Enberg 
32192a99d1SCyrill Gorcunov struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size);
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);
412049569dSPekka Enberg bool kvm__emulate_io(struct kvm *self, uint16_t port, void *data, int direction, int size, uint32_t count);
4229443dabSPekka Enberg bool kvm__emulate_mmio(struct kvm *self, uint64_t phys_addr, uint8_t *data, uint32_t len, uint8_t 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 
569292f776SCyrill Gorcunov static inline uint32_t segment_to_flat(uint16_t selector, uint16_t offset)
579292f776SCyrill Gorcunov {
589292f776SCyrill Gorcunov 	return ((uint32_t)selector << 4) + (uint32_t) 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 
669292f776SCyrill Gorcunov static inline void *guest_real_to_host(struct kvm *self, uint16_t selector, uint16_t 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