Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | #ifndef __KVM_H |
| 2 | #define __KVM_H |
| 3 | |
| 4 | /* |
| 5 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 6 | * the COPYING file in the top-level directory. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
Christian Borntraeger | e56a7a2 | 2007-10-18 14:39:10 +0200 | [diff] [blame] | 10 | #include <linux/hardirq.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11 | #include <linux/list.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/spinlock.h> |
Markus Rechberger | 06ff0d3 | 2007-05-27 10:46:52 +0300 | [diff] [blame] | 14 | #include <linux/signal.h> |
| 15 | #include <linux/sched.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 16 | #include <linux/mm.h> |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 17 | #include <linux/preempt.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 18 | #include <asm/signal.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 19 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 20 | #include <linux/kvm.h> |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 21 | #include <linux/kvm_para.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 22 | |
Hollis Blanchard | d77a39d9 | 2007-12-03 15:30:23 -0600 | [diff] [blame] | 23 | #include "types.h" |
| 24 | |
Avi Kivity | ef9254d | 2007-06-05 14:37:09 +0300 | [diff] [blame] | 25 | #define KVM_MAX_VCPUS 4 |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 26 | #define KVM_ALIAS_SLOTS 4 |
Izik Eidus | 2e2c618 | 2007-08-20 18:11:00 +0300 | [diff] [blame] | 27 | #define KVM_MEMORY_SLOTS 8 |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 28 | /* memory slots that does not exposed to userspace */ |
| 29 | #define KVM_PRIVATE_MEM_SLOTS 4 |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 30 | #define KVM_PERMILLE_MMU_PAGES 20 |
| 31 | #define KVM_MIN_ALLOC_MMU_PAGES 64 |
Avi Kivity | 7494c0c | 2007-05-01 18:24:38 +0300 | [diff] [blame] | 32 | #define KVM_NUM_MMU_PAGES 1024 |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 33 | #define KVM_MIN_FREE_MMU_PAGES 5 |
| 34 | #define KVM_REFILL_PAGES 25 |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 35 | #define KVM_MAX_CPUID_ENTRIES 40 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 36 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 37 | #define KVM_PIO_PAGE_OFFSET 1 |
| 38 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 39 | /* |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 40 | * vcpu->requests bit members |
| 41 | */ |
Avi Kivity | 3176bc3 | 2007-10-16 17:22:08 +0200 | [diff] [blame] | 42 | #define KVM_REQ_TLB_FLUSH 0 |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 43 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 44 | #define NR_PTE_CHAIN_ENTRIES 5 |
| 45 | |
| 46 | struct kvm_pte_chain { |
| 47 | u64 *parent_ptes[NR_PTE_CHAIN_ENTRIES]; |
| 48 | struct hlist_node link; |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * kvm_mmu_page_role, below, is defined as: |
| 53 | * |
| 54 | * bits 0:3 - total guest paging levels (2-4, or zero for real mode) |
| 55 | * bits 4:7 - page table level for this shadow (1-4) |
| 56 | * bits 8:9 - page table quadrant for 2-level guests |
| 57 | * bit 16 - "metaphysical" - gfn is not a real page (huge page/real mode) |
Avi Kivity | d55e2cb | 2007-07-10 17:50:55 +0300 | [diff] [blame] | 58 | * bits 17:19 - "access" - the user, writable, and nx bits of a huge page pde |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 59 | */ |
| 60 | union kvm_mmu_page_role { |
| 61 | unsigned word; |
| 62 | struct { |
| 63 | unsigned glevels : 4; |
| 64 | unsigned level : 4; |
| 65 | unsigned quadrant : 2; |
| 66 | unsigned pad_for_nice_hex_output : 6; |
| 67 | unsigned metaphysical : 1; |
Avi Kivity | d55e2cb | 2007-07-10 17:50:55 +0300 | [diff] [blame] | 68 | unsigned hugepage_access : 3; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 69 | }; |
| 70 | }; |
| 71 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 72 | struct kvm_mmu_page { |
| 73 | struct list_head link; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 74 | struct hlist_node hash_link; |
| 75 | |
| 76 | /* |
| 77 | * The following two entries are used to key the shadow page in the |
| 78 | * hash table. |
| 79 | */ |
| 80 | gfn_t gfn; |
| 81 | union kvm_mmu_page_role role; |
| 82 | |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 83 | u64 *spt; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 84 | /* hold the gfn of each spte inside spt */ |
| 85 | gfn_t *gfns; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 86 | unsigned long slot_bitmap; /* One bit set per slot which has memory |
| 87 | * in this shadow page. |
| 88 | */ |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 89 | int multimapped; /* More than one parent_pte? */ |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 90 | int root_count; /* Currently serving as active root */ |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 91 | union { |
| 92 | u64 *parent_pte; /* !multimapped */ |
| 93 | struct hlist_head parent_ptes; /* multimapped, kvm_pte_chain */ |
| 94 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 97 | struct kvm_vcpu; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 98 | extern struct kmem_cache *kvm_vcpu_cache; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level |
| 102 | * 32-bit). The kvm_mmu structure abstracts the details of the current mmu |
| 103 | * mode. |
| 104 | */ |
| 105 | struct kvm_mmu { |
| 106 | void (*new_cr3)(struct kvm_vcpu *vcpu); |
| 107 | int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 108 | void (*free)(struct kvm_vcpu *vcpu); |
| 109 | gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 110 | void (*prefetch_page)(struct kvm_vcpu *vcpu, |
| 111 | struct kvm_mmu_page *page); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 112 | hpa_t root_hpa; |
| 113 | int root_level; |
| 114 | int shadow_root_level; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 115 | |
| 116 | u64 *pae_root; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 119 | #define KVM_NR_MEM_OBJS 40 |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 120 | |
Hollis Blanchard | 7faa8f6 | 2007-11-19 14:04:44 -0600 | [diff] [blame] | 121 | /* |
| 122 | * We don't want allocation failures within the mmu code, so we preallocate |
| 123 | * enough memory for a single page fault in a cache. |
| 124 | */ |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 125 | struct kvm_mmu_memory_cache { |
| 126 | int nobjs; |
| 127 | void *objects[KVM_NR_MEM_OBJS]; |
| 128 | }; |
| 129 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 130 | struct kvm_guest_debug { |
| 131 | int enabled; |
| 132 | unsigned long bp[4]; |
| 133 | int singlestep; |
| 134 | }; |
| 135 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 136 | struct kvm_pio_request { |
| 137 | unsigned long count; |
| 138 | int cur_count; |
| 139 | struct page *guest_pages[2]; |
| 140 | unsigned guest_page_offset; |
| 141 | int in; |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 142 | int port; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 143 | int size; |
| 144 | int string; |
| 145 | int down; |
| 146 | int rep; |
| 147 | }; |
| 148 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 149 | struct kvm_vcpu_stat { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 150 | u32 pf_fixed; |
| 151 | u32 pf_guest; |
| 152 | u32 tlb_flush; |
| 153 | u32 invlpg; |
| 154 | |
| 155 | u32 exits; |
| 156 | u32 io_exits; |
| 157 | u32 mmio_exits; |
| 158 | u32 signal_exits; |
| 159 | u32 irq_window_exits; |
| 160 | u32 halt_exits; |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 161 | u32 halt_wakeup; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 162 | u32 request_irq_exits; |
| 163 | u32 irq_exits; |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 164 | u32 host_state_reload; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 165 | u32 efer_reload; |
Avi Kivity | f096ed8 | 2007-11-18 13:54:33 +0200 | [diff] [blame] | 166 | u32 fpu_reload; |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 167 | u32 insn_emulation; |
| 168 | u32 insn_emulation_fail; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 169 | }; |
| 170 | |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 171 | /* |
| 172 | * It would be nice to use something smarter than a linear search, TBD... |
| 173 | * Thankfully we dont expect many devices to register (famous last words :), |
| 174 | * so until then it will suffice. At least its abstracted so we can change |
| 175 | * in one place. |
| 176 | */ |
| 177 | struct kvm_io_bus { |
| 178 | int dev_count; |
| 179 | #define NR_IOBUS_DEVS 6 |
| 180 | struct kvm_io_device *devs[NR_IOBUS_DEVS]; |
| 181 | }; |
| 182 | |
| 183 | void kvm_io_bus_init(struct kvm_io_bus *bus); |
| 184 | void kvm_io_bus_destroy(struct kvm_io_bus *bus); |
| 185 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr); |
| 186 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, |
| 187 | struct kvm_io_device *dev); |
| 188 | |
Zhang Xiantao | 34c16ee | 2007-10-20 15:34:38 +0800 | [diff] [blame] | 189 | #ifdef CONFIG_HAS_IOMEM |
| 190 | #define KVM_VCPU_MMIO \ |
| 191 | int mmio_needed; \ |
| 192 | int mmio_read_completed; \ |
| 193 | int mmio_is_write; \ |
| 194 | int mmio_size; \ |
| 195 | unsigned char mmio_data[8]; \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 196 | gpa_t mmio_phys_addr; |
| 197 | |
Zhang Xiantao | 34c16ee | 2007-10-20 15:34:38 +0800 | [diff] [blame] | 198 | #else |
| 199 | #define KVM_VCPU_MMIO |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 200 | |
Zhang Xiantao | 34c16ee | 2007-10-20 15:34:38 +0800 | [diff] [blame] | 201 | #endif |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 202 | |
Zhang Xiantao | 34c16ee | 2007-10-20 15:34:38 +0800 | [diff] [blame] | 203 | #define KVM_VCPU_COMM \ |
| 204 | struct kvm *kvm; \ |
| 205 | struct preempt_notifier preempt_notifier; \ |
| 206 | int vcpu_id; \ |
| 207 | struct mutex mutex; \ |
| 208 | int cpu; \ |
| 209 | struct kvm_run *run; \ |
| 210 | int guest_mode; \ |
| 211 | unsigned long requests; \ |
| 212 | struct kvm_guest_debug guest_debug; \ |
| 213 | int fpu_active; \ |
| 214 | int guest_fpu_loaded; \ |
| 215 | wait_queue_head_t wq; \ |
| 216 | int sigset_active; \ |
| 217 | sigset_t sigset; \ |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 218 | struct kvm_vcpu_stat stat; \ |
Zhang Xiantao | 34c16ee | 2007-10-20 15:34:38 +0800 | [diff] [blame] | 219 | KVM_VCPU_MMIO |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 220 | |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 221 | struct kvm_mem_alias { |
| 222 | gfn_t base_gfn; |
| 223 | unsigned long npages; |
| 224 | gfn_t target_gfn; |
| 225 | }; |
| 226 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 227 | struct kvm_memory_slot { |
| 228 | gfn_t base_gfn; |
| 229 | unsigned long npages; |
| 230 | unsigned long flags; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 231 | unsigned long *rmap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 232 | unsigned long *dirty_bitmap; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 233 | unsigned long userspace_addr; |
Izik Eidus | 80b14b5 | 2007-10-25 11:54:04 +0200 | [diff] [blame] | 234 | int user_alloc; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 235 | }; |
| 236 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 237 | struct kvm_vm_stat { |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 238 | u32 mmu_shadow_zapped; |
| 239 | u32 mmu_pte_write; |
| 240 | u32 mmu_pte_updated; |
| 241 | u32 mmu_pde_zapped; |
| 242 | u32 mmu_flooded; |
| 243 | u32 mmu_recycled; |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 244 | u32 remote_tlb_flush; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 245 | }; |
| 246 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 247 | struct kvm { |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 248 | struct mutex lock; /* protects everything except vcpus */ |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 249 | struct mm_struct *mm; /* userspace tied to this vm */ |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 250 | int naliases; |
| 251 | struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 252 | int nmemslots; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 253 | struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS + |
| 254 | KVM_PRIVATE_MEM_SLOTS]; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 255 | /* |
| 256 | * Hash table of struct kvm_mmu_page. |
| 257 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 258 | struct list_head active_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 259 | unsigned int n_free_mmu_pages; |
| 260 | unsigned int n_requested_mmu_pages; |
| 261 | unsigned int n_alloc_mmu_pages; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 262 | struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES]; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 263 | struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 264 | struct list_head vm_list; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 265 | struct file *filp; |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 266 | struct kvm_io_bus mmio_bus; |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 267 | struct kvm_io_bus pio_bus; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 268 | struct kvm_pic *vpic; |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 269 | struct kvm_ioapic *vioapic; |
He, Qing | 932f72a | 2007-09-03 17:01:36 +0300 | [diff] [blame] | 270 | int round_robin_prev_vcpu; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 271 | unsigned int tss_addr; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 272 | struct page *apic_access_page; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 273 | struct kvm_vm_stat stat; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 274 | }; |
| 275 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 276 | static inline struct kvm_pic *pic_irqchip(struct kvm *kvm) |
| 277 | { |
| 278 | return kvm->vpic; |
| 279 | } |
| 280 | |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 281 | static inline struct kvm_ioapic *ioapic_irqchip(struct kvm *kvm) |
| 282 | { |
| 283 | return kvm->vioapic; |
| 284 | } |
| 285 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 286 | static inline int irqchip_in_kernel(struct kvm *kvm) |
| 287 | { |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 288 | return pic_irqchip(kvm) != NULL; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 289 | } |
| 290 | |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 291 | /* The guest did something we don't support. */ |
| 292 | #define pr_unimpl(vcpu, fmt, ...) \ |
| 293 | do { \ |
| 294 | if (printk_ratelimit()) \ |
| 295 | printk(KERN_ERR "kvm: %i: cpu%i " fmt, \ |
| 296 | current->tgid, (vcpu)->vcpu_id , ## __VA_ARGS__); \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 297 | } while (0) |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 298 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 299 | #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt) |
| 300 | #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt) |
| 301 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 302 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id); |
| 303 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu); |
| 304 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 305 | void vcpu_load(struct kvm_vcpu *vcpu); |
| 306 | void vcpu_put(struct kvm_vcpu *vcpu); |
| 307 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 308 | void decache_vcpus_on_cpu(int cpu); |
| 309 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 310 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 311 | int kvm_init(void *opaque, unsigned int vcpu_size, |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 312 | struct module *module); |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 313 | void kvm_exit(void); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 314 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 315 | #define HPA_MSB ((sizeof(hpa_t) * 8) - 1) |
| 316 | #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB) |
| 317 | static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; } |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 318 | struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 319 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 320 | extern struct page *bad_page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 321 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 322 | int is_error_page(struct page *page); |
Izik Eidus | f9d46eb | 2007-11-11 22:02:22 +0200 | [diff] [blame] | 323 | int kvm_is_error_hva(unsigned long addr); |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 324 | int kvm_set_memory_region(struct kvm *kvm, |
| 325 | struct kvm_userspace_memory_region *mem, |
| 326 | int user_alloc); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 327 | int __kvm_set_memory_region(struct kvm *kvm, |
| 328 | struct kvm_userspace_memory_region *mem, |
| 329 | int user_alloc); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 330 | int kvm_arch_set_memory_region(struct kvm *kvm, |
| 331 | struct kvm_userspace_memory_region *mem, |
| 332 | struct kvm_memory_slot old, |
| 333 | int user_alloc); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 334 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn); |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 335 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn); |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 336 | void kvm_release_page_clean(struct page *page); |
| 337 | void kvm_release_page_dirty(struct page *page); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 338 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
| 339 | int len); |
| 340 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len); |
| 341 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
| 342 | int offset, int len); |
| 343 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
| 344 | unsigned long len); |
| 345 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len); |
| 346 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 347 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn); |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 348 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 349 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn); |
| 350 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 351 | void kvm_vcpu_block(struct kvm_vcpu *vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 352 | void kvm_resched(struct kvm_vcpu *vcpu); |
Avi Kivity | 7702fd1 | 2007-06-14 16:27:40 +0300 | [diff] [blame] | 353 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu); |
| 354 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 355 | void kvm_flush_remote_tlbs(struct kvm *kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 356 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 357 | long kvm_arch_dev_ioctl(struct file *filp, |
| 358 | unsigned int ioctl, unsigned long arg); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 359 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 360 | unsigned int ioctl, unsigned long arg); |
| 361 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu); |
| 362 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu); |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 363 | |
| 364 | int kvm_dev_ioctl_check_extension(long ext); |
| 365 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 366 | int kvm_get_dirty_log(struct kvm *kvm, |
| 367 | struct kvm_dirty_log *log, int *is_dirty); |
| 368 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 369 | struct kvm_dirty_log *log); |
| 370 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 371 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
| 372 | struct |
| 373 | kvm_userspace_memory_region *mem, |
| 374 | int user_alloc); |
| 375 | long kvm_arch_vm_ioctl(struct file *filp, |
| 376 | unsigned int ioctl, unsigned long arg); |
| 377 | void kvm_arch_destroy_vm(struct kvm *kvm); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 378 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 379 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu); |
| 380 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu); |
| 381 | |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 382 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 383 | struct kvm_translation *tr); |
| 384 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 385 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs); |
| 386 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs); |
| 387 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 388 | struct kvm_sregs *sregs); |
| 389 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 390 | struct kvm_sregs *sregs); |
| 391 | int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, |
| 392 | struct kvm_debug_guest *dbg); |
| 393 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run); |
| 394 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 395 | int kvm_arch_init(void *opaque); |
| 396 | void kvm_arch_exit(void); |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 397 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 398 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu); |
| 399 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu); |
| 400 | |
| 401 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu); |
| 402 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu); |
| 403 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu); |
| 404 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id); |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 405 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu); |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 406 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 407 | |
| 408 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu); |
| 409 | void kvm_arch_hardware_enable(void *garbage); |
| 410 | void kvm_arch_hardware_disable(void *garbage); |
| 411 | int kvm_arch_hardware_setup(void); |
| 412 | void kvm_arch_hardware_unsetup(void); |
| 413 | void kvm_arch_check_processor_compat(void *rtn); |
| 414 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 415 | void kvm_free_physmem(struct kvm *kvm); |
| 416 | |
| 417 | struct kvm *kvm_arch_create_vm(void); |
| 418 | void kvm_arch_destroy_vm(struct kvm *kvm); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 419 | |
Laurent Vivier | d172fcd | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 420 | static inline void kvm_guest_enter(void) |
| 421 | { |
Christian Borntraeger | e56a7a2 | 2007-10-18 14:39:10 +0200 | [diff] [blame] | 422 | account_system_vtime(current); |
Laurent Vivier | d172fcd | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 423 | current->flags |= PF_VCPU; |
| 424 | } |
| 425 | |
| 426 | static inline void kvm_guest_exit(void) |
| 427 | { |
Christian Borntraeger | e56a7a2 | 2007-10-18 14:39:10 +0200 | [diff] [blame] | 428 | account_system_vtime(current); |
Laurent Vivier | d172fcd | 2007-10-15 17:00:19 +0200 | [diff] [blame] | 429 | current->flags &= ~PF_VCPU; |
| 430 | } |
| 431 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 432 | static inline int memslot_id(struct kvm *kvm, struct kvm_memory_slot *slot) |
| 433 | { |
| 434 | return slot - kvm->memslots; |
| 435 | } |
| 436 | |
Avi Kivity | 1755fbc | 2007-11-21 14:44:45 +0200 | [diff] [blame] | 437 | static inline gpa_t gfn_to_gpa(gfn_t gfn) |
| 438 | { |
| 439 | return (gpa_t)gfn << PAGE_SHIFT; |
| 440 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 441 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 442 | enum kvm_stat_kind { |
| 443 | KVM_STAT_VM, |
| 444 | KVM_STAT_VCPU, |
| 445 | }; |
| 446 | |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 447 | struct kvm_stats_debugfs_item { |
| 448 | const char *name; |
| 449 | int offset; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 450 | enum kvm_stat_kind kind; |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 451 | struct dentry *dentry; |
| 452 | }; |
| 453 | extern struct kvm_stats_debugfs_item debugfs_entries[]; |
| 454 | |
Hollis Blanchard | e01a1b5 | 2007-12-03 15:30:25 -0600 | [diff] [blame^] | 455 | #if defined(CONFIG_X86) |
| 456 | #include "x86.h" |
| 457 | #endif |
| 458 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 459 | #endif |