xref: /kvmtool/include/kvm/kvm.h (revision 0c7c14a747e9eb2c3cacef60fb74b0698c9d3adf)
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 
10ae1fae34SPekka Enberg struct kvm {
11ae1fae34SPekka Enberg 	int			sys_fd;		/* For system ioctls(), i.e. /dev/kvm */
12ae1fae34SPekka Enberg 	int			vm_fd;		/* For VM ioctls() */
13ce79f1caSPekka Enberg 	timer_t			timerid;	/* Posix timer for interrupts */
14ae1fae34SPekka Enberg 
15*0c7c14a7SCyrill Gorcunov 	int			nrcpus;		/* Number of cpus to run */
16*0c7c14a7SCyrill Gorcunov 
179f532d00SPekka Enberg 	struct disk_image	*disk_image;
18ae1fae34SPekka Enberg 	uint64_t		ram_size;
19ae1fae34SPekka Enberg 	void			*ram_start;
20ae1fae34SPekka Enberg 
212049569dSPekka Enberg 	bool			nmi_disabled;
222049569dSPekka Enberg 
23dbdb74c2SPekka Enberg 	uint16_t		boot_selector;
24edc8a14dSPekka Enberg 	uint16_t		boot_ip;
25dbdb74c2SPekka Enberg 	uint16_t		boot_sp;
26edc8a14dSPekka Enberg 
27da8883c1SPekka Enberg 	struct interrupt_table	interrupt_table;
28ae1fae34SPekka Enberg };
29ae1fae34SPekka Enberg 
30192a99d1SCyrill Gorcunov struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size);
31839051d9SSasha Levin void kvm__init_ram(struct kvm *self);
329ef4c68eSPekka Enberg void kvm__delete(struct kvm *self);
332065a6f7SCyrill Gorcunov bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename,
342065a6f7SCyrill Gorcunov 			const char *initrd_filename, const char *kernel_cmdline);
35b3594ec7SCyrill Gorcunov void kvm__setup_bios(struct kvm *self);
36ce79f1caSPekka Enberg void kvm__start_timer(struct kvm *self);
37fbfe68b7SSasha Levin void kvm__stop_timer(struct kvm *self);
388b1ff07eSPekka Enberg void kvm__irq_line(struct kvm *self, int irq, int level);
392049569dSPekka Enberg bool kvm__emulate_io(struct kvm *self, uint16_t port, void *data, int direction, int size, uint32_t count);
4029443dabSPekka Enberg bool kvm__emulate_mmio(struct kvm *self, uint64_t phys_addr, uint8_t *data, uint32_t len, uint8_t is_write);
41ae1fae34SPekka Enberg 
42ae1fae34SPekka Enberg /*
43ae1fae34SPekka Enberg  * Debugging
44ae1fae34SPekka Enberg  */
45090f898eSCyrill Gorcunov void kvm__dump_mem(struct kvm *self, unsigned long addr, unsigned long size);
46ae1fae34SPekka Enberg 
47ae1fae34SPekka Enberg extern const char *kvm_exit_reasons[];
48ae1fae34SPekka Enberg 
499292f776SCyrill Gorcunov static inline bool host_ptr_in_ram(struct kvm *self, void *p)
509292f776SCyrill Gorcunov {
519292f776SCyrill Gorcunov 	return self->ram_start <= p && p < (self->ram_start + self->ram_size);
529292f776SCyrill Gorcunov }
539292f776SCyrill Gorcunov 
549292f776SCyrill Gorcunov static inline uint32_t segment_to_flat(uint16_t selector, uint16_t offset)
559292f776SCyrill Gorcunov {
569292f776SCyrill Gorcunov 	return ((uint32_t)selector << 4) + (uint32_t) offset;
579292f776SCyrill Gorcunov }
589292f776SCyrill Gorcunov 
599292f776SCyrill Gorcunov static inline void *guest_flat_to_host(struct kvm *self, unsigned long offset)
609292f776SCyrill Gorcunov {
619292f776SCyrill Gorcunov 	return self->ram_start + offset;
629292f776SCyrill Gorcunov }
639292f776SCyrill Gorcunov 
649292f776SCyrill Gorcunov static inline void *guest_real_to_host(struct kvm *self, uint16_t selector, uint16_t offset)
659292f776SCyrill Gorcunov {
669292f776SCyrill Gorcunov 	unsigned long flat = segment_to_flat(selector, offset);
679292f776SCyrill Gorcunov 
689292f776SCyrill Gorcunov 	return guest_flat_to_host(self, flat);
699292f776SCyrill Gorcunov }
709292f776SCyrill Gorcunov 
71ae1fae34SPekka Enberg #endif /* KVM__KVM_H */
72