kvm_host.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * kvm_host.h: used for kvm module, and hold ia64-specific sections.
  3. *
  4. * Copyright (C) 2007, Intel Corporation.
  5. *
  6. * Xiantao Zhang <xiantao.zhang@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  19. * Place - Suite 330, Boston, MA 02111-1307 USA.
  20. *
  21. */
  22. #ifndef __ASM_KVM_HOST_H
  23. #define __ASM_KVM_HOST_H
  24. #include <linux/types.h>
  25. #include <linux/mm.h>
  26. #include <linux/kvm.h>
  27. #include <linux/kvm_para.h>
  28. #include <linux/kvm_types.h>
  29. #include <asm/pal.h>
  30. #include <asm/sal.h>
  31. #define KVM_MAX_VCPUS 4
  32. #define KVM_MEMORY_SLOTS 32
  33. /* memory slots that does not exposed to userspace */
  34. #define KVM_PRIVATE_MEM_SLOTS 4
  35. #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
  36. /* define exit reasons from vmm to kvm*/
  37. #define EXIT_REASON_VM_PANIC 0
  38. #define EXIT_REASON_MMIO_INSTRUCTION 1
  39. #define EXIT_REASON_PAL_CALL 2
  40. #define EXIT_REASON_SAL_CALL 3
  41. #define EXIT_REASON_SWITCH_RR6 4
  42. #define EXIT_REASON_VM_DESTROY 5
  43. #define EXIT_REASON_EXTERNAL_INTERRUPT 6
  44. #define EXIT_REASON_IPI 7
  45. #define EXIT_REASON_PTC_G 8
  46. /*Define vmm address space and vm data space.*/
  47. #define KVM_VMM_SIZE (16UL<<20)
  48. #define KVM_VMM_SHIFT 24
  49. #define KVM_VMM_BASE 0xD000000000000000UL
  50. #define VMM_SIZE (8UL<<20)
  51. /*
  52. * Define vm_buffer, used by PAL Services, base address.
  53. * Note: vmbuffer is in the VMM-BLOCK, the size must be < 8M
  54. */
  55. #define KVM_VM_BUFFER_BASE (KVM_VMM_BASE + VMM_SIZE)
  56. #define KVM_VM_BUFFER_SIZE (8UL<<20)
  57. /*Define Virtual machine data layout.*/
  58. #define KVM_VM_DATA_SHIFT 24
  59. #define KVM_VM_DATA_SIZE (1UL << KVM_VM_DATA_SHIFT)
  60. #define KVM_VM_DATA_BASE (KVM_VMM_BASE + KVM_VMM_SIZE)
  61. #define KVM_P2M_BASE KVM_VM_DATA_BASE
  62. #define KVM_P2M_OFS 0
  63. #define KVM_P2M_SIZE (8UL << 20)
  64. #define KVM_VHPT_BASE (KVM_P2M_BASE + KVM_P2M_SIZE)
  65. #define KVM_VHPT_OFS KVM_P2M_SIZE
  66. #define KVM_VHPT_BLOCK_SIZE (2UL << 20)
  67. #define VHPT_SHIFT 18
  68. #define VHPT_SIZE (1UL << VHPT_SHIFT)
  69. #define VHPT_NUM_ENTRIES (1<<(VHPT_SHIFT-5))
  70. #define KVM_VTLB_BASE (KVM_VHPT_BASE+KVM_VHPT_BLOCK_SIZE)
  71. #define KVM_VTLB_OFS (KVM_VHPT_OFS+KVM_VHPT_BLOCK_SIZE)
  72. #define KVM_VTLB_BLOCK_SIZE (1UL<<20)
  73. #define VTLB_SHIFT 17
  74. #define VTLB_SIZE (1UL<<VTLB_SHIFT)
  75. #define VTLB_NUM_ENTRIES (1<<(VTLB_SHIFT-5))
  76. #define KVM_VPD_BASE (KVM_VTLB_BASE+KVM_VTLB_BLOCK_SIZE)
  77. #define KVM_VPD_OFS (KVM_VTLB_OFS+KVM_VTLB_BLOCK_SIZE)
  78. #define KVM_VPD_BLOCK_SIZE (2UL<<20)
  79. #define VPD_SHIFT 16
  80. #define VPD_SIZE (1UL<<VPD_SHIFT)
  81. #define KVM_VCPU_BASE (KVM_VPD_BASE+KVM_VPD_BLOCK_SIZE)
  82. #define KVM_VCPU_OFS (KVM_VPD_OFS+KVM_VPD_BLOCK_SIZE)
  83. #define KVM_VCPU_BLOCK_SIZE (2UL<<20)
  84. #define VCPU_SHIFT 18
  85. #define VCPU_SIZE (1UL<<VCPU_SHIFT)
  86. #define MAX_VCPU_NUM KVM_VCPU_BLOCK_SIZE/VCPU_SIZE
  87. #define KVM_VM_BASE (KVM_VCPU_BASE+KVM_VCPU_BLOCK_SIZE)
  88. #define KVM_VM_OFS (KVM_VCPU_OFS+KVM_VCPU_BLOCK_SIZE)
  89. #define KVM_VM_BLOCK_SIZE (1UL<<19)
  90. #define KVM_MEM_DIRTY_LOG_BASE (KVM_VM_BASE+KVM_VM_BLOCK_SIZE)
  91. #define KVM_MEM_DIRTY_LOG_OFS (KVM_VM_OFS+KVM_VM_BLOCK_SIZE)
  92. #define KVM_MEM_DIRTY_LOG_SIZE (1UL<<19)
  93. /* Get vpd, vhpt, tlb, vcpu, base*/
  94. #define VPD_ADDR(n) (KVM_VPD_BASE+n*VPD_SIZE)
  95. #define VHPT_ADDR(n) (KVM_VHPT_BASE+n*VHPT_SIZE)
  96. #define VTLB_ADDR(n) (KVM_VTLB_BASE+n*VTLB_SIZE)
  97. #define VCPU_ADDR(n) (KVM_VCPU_BASE+n*VCPU_SIZE)
  98. /*IO section definitions*/
  99. #define IOREQ_READ 1
  100. #define IOREQ_WRITE 0
  101. #define STATE_IOREQ_NONE 0
  102. #define STATE_IOREQ_READY 1
  103. #define STATE_IOREQ_INPROCESS 2
  104. #define STATE_IORESP_READY 3
  105. /*Guest Physical address layout.*/
  106. #define GPFN_MEM (0UL << 60) /* Guest pfn is normal mem */
  107. #define GPFN_FRAME_BUFFER (1UL << 60) /* VGA framebuffer */
  108. #define GPFN_LOW_MMIO (2UL << 60) /* Low MMIO range */
  109. #define GPFN_PIB (3UL << 60) /* PIB base */
  110. #define GPFN_IOSAPIC (4UL << 60) /* IOSAPIC base */
  111. #define GPFN_LEGACY_IO (5UL << 60) /* Legacy I/O base */
  112. #define GPFN_GFW (6UL << 60) /* Guest Firmware */
  113. #define GPFN_HIGH_MMIO (7UL << 60) /* High MMIO range */
  114. #define GPFN_IO_MASK (7UL << 60) /* Guest pfn is I/O type */
  115. #define GPFN_INV_MASK (1UL << 63) /* Guest pfn is invalid */
  116. #define INVALID_MFN (~0UL)
  117. #define MEM_G (1UL << 30)
  118. #define MEM_M (1UL << 20)
  119. #define MMIO_START (3 * MEM_G)
  120. #define MMIO_SIZE (512 * MEM_M)
  121. #define VGA_IO_START 0xA0000UL
  122. #define VGA_IO_SIZE 0x20000
  123. #define LEGACY_IO_START (MMIO_START + MMIO_SIZE)
  124. #define LEGACY_IO_SIZE (64 * MEM_M)
  125. #define IO_SAPIC_START 0xfec00000UL
  126. #define IO_SAPIC_SIZE 0x100000
  127. #define PIB_START 0xfee00000UL
  128. #define PIB_SIZE 0x200000
  129. #define GFW_START (4 * MEM_G - 16 * MEM_M)
  130. #define GFW_SIZE (16 * MEM_M)
  131. /*Deliver mode, defined for ioapic.c*/
  132. #define dest_Fixed IOSAPIC_FIXED
  133. #define dest_LowestPrio IOSAPIC_LOWEST_PRIORITY
  134. #define NMI_VECTOR 2
  135. #define ExtINT_VECTOR 0
  136. #define NULL_VECTOR (-1)
  137. #define IA64_SPURIOUS_INT_VECTOR 0x0f
  138. #define VCPU_LID(v) (((u64)(v)->vcpu_id) << 24)
  139. /*
  140. *Delivery mode
  141. */
  142. #define SAPIC_DELIV_SHIFT 8
  143. #define SAPIC_FIXED 0x0
  144. #define SAPIC_LOWEST_PRIORITY 0x1
  145. #define SAPIC_PMI 0x2
  146. #define SAPIC_NMI 0x4
  147. #define SAPIC_INIT 0x5
  148. #define SAPIC_EXTINT 0x7
  149. /*
  150. * vcpu->requests bit members for arch
  151. */
  152. #define KVM_REQ_PTC_G 32
  153. #define KVM_REQ_RESUME 33
  154. #define KVM_PAGES_PER_HPAGE 1
  155. struct kvm;
  156. struct kvm_vcpu;
  157. struct kvm_guest_debug{
  158. };
  159. struct kvm_mmio_req {
  160. uint64_t addr; /* physical address */
  161. uint64_t size; /* size in bytes */
  162. uint64_t data; /* data (or paddr of data) */
  163. uint8_t state:4;
  164. uint8_t dir:1; /* 1=read, 0=write */
  165. };
  166. /*Pal data struct */
  167. struct kvm_pal_call{
  168. /*In area*/
  169. uint64_t gr28;
  170. uint64_t gr29;
  171. uint64_t gr30;
  172. uint64_t gr31;
  173. /*Out area*/
  174. struct ia64_pal_retval ret;
  175. };
  176. /* Sal data structure */
  177. struct kvm_sal_call{
  178. /*In area*/
  179. uint64_t in0;
  180. uint64_t in1;
  181. uint64_t in2;
  182. uint64_t in3;
  183. uint64_t in4;
  184. uint64_t in5;
  185. uint64_t in6;
  186. uint64_t in7;
  187. struct sal_ret_values ret;
  188. };
  189. /*Guest change rr6*/
  190. struct kvm_switch_rr6 {
  191. uint64_t old_rr;
  192. uint64_t new_rr;
  193. };
  194. union ia64_ipi_a{
  195. unsigned long val;
  196. struct {
  197. unsigned long rv : 3;
  198. unsigned long ir : 1;
  199. unsigned long eid : 8;
  200. unsigned long id : 8;
  201. unsigned long ib_base : 44;
  202. };
  203. };
  204. union ia64_ipi_d {
  205. unsigned long val;
  206. struct {
  207. unsigned long vector : 8;
  208. unsigned long dm : 3;
  209. unsigned long ig : 53;
  210. };
  211. };
  212. /*ipi check exit data*/
  213. struct kvm_ipi_data{
  214. union ia64_ipi_a addr;
  215. union ia64_ipi_d data;
  216. };
  217. /*global purge data*/
  218. struct kvm_ptc_g {
  219. unsigned long vaddr;
  220. unsigned long rr;
  221. unsigned long ps;
  222. struct kvm_vcpu *vcpu;
  223. };
  224. /*Exit control data */
  225. struct exit_ctl_data{
  226. uint32_t exit_reason;
  227. uint32_t vm_status;
  228. union {
  229. struct kvm_mmio_req ioreq;
  230. struct kvm_pal_call pal_data;
  231. struct kvm_sal_call sal_data;
  232. struct kvm_switch_rr6 rr_data;
  233. struct kvm_ipi_data ipi_data;
  234. struct kvm_ptc_g ptc_g_data;
  235. } u;
  236. };
  237. union pte_flags {
  238. unsigned long val;
  239. struct {
  240. unsigned long p : 1; /*0 */
  241. unsigned long : 1; /* 1 */
  242. unsigned long ma : 3; /* 2-4 */
  243. unsigned long a : 1; /* 5 */
  244. unsigned long d : 1; /* 6 */
  245. unsigned long pl : 2; /* 7-8 */
  246. unsigned long ar : 3; /* 9-11 */
  247. unsigned long ppn : 38; /* 12-49 */
  248. unsigned long : 2; /* 50-51 */
  249. unsigned long ed : 1; /* 52 */
  250. };
  251. };
  252. union ia64_pta {
  253. unsigned long val;
  254. struct {
  255. unsigned long ve : 1;
  256. unsigned long reserved0 : 1;
  257. unsigned long size : 6;
  258. unsigned long vf : 1;
  259. unsigned long reserved1 : 6;
  260. unsigned long base : 49;
  261. };
  262. };
  263. struct thash_cb {
  264. /* THASH base information */
  265. struct thash_data *hash; /* hash table pointer */
  266. union ia64_pta pta;
  267. int num;
  268. };
  269. struct kvm_vcpu_stat {
  270. };
  271. struct kvm_vcpu_arch {
  272. int launched;
  273. int last_exit;
  274. int last_run_cpu;
  275. int vmm_tr_slot;
  276. int vm_tr_slot;
  277. #define KVM_MP_STATE_RUNNABLE 0
  278. #define KVM_MP_STATE_UNINITIALIZED 1
  279. #define KVM_MP_STATE_INIT_RECEIVED 2
  280. #define KVM_MP_STATE_HALTED 3
  281. int mp_state;
  282. #define MAX_PTC_G_NUM 3
  283. int ptc_g_count;
  284. struct kvm_ptc_g ptc_g_data[MAX_PTC_G_NUM];
  285. /*halt timer to wake up sleepy vcpus*/
  286. struct hrtimer hlt_timer;
  287. long ht_active;
  288. struct kvm_lapic *apic; /* kernel irqchip context */
  289. struct vpd *vpd;
  290. /* Exit data for vmm_transition*/
  291. struct exit_ctl_data exit_data;
  292. cpumask_t cache_coherent_map;
  293. unsigned long vmm_rr;
  294. unsigned long host_rr6;
  295. unsigned long psbits[8];
  296. unsigned long cr_iipa;
  297. unsigned long cr_isr;
  298. unsigned long vsa_base;
  299. unsigned long dirty_log_lock_pa;
  300. unsigned long __gp;
  301. /* TR and TC. */
  302. struct thash_data itrs[NITRS];
  303. struct thash_data dtrs[NDTRS];
  304. /* Bit is set if there is a tr/tc for the region. */
  305. unsigned char itr_regions;
  306. unsigned char dtr_regions;
  307. unsigned char tc_regions;
  308. /* purge all */
  309. unsigned long ptce_base;
  310. unsigned long ptce_count[2];
  311. unsigned long ptce_stride[2];
  312. /* itc/itm */
  313. unsigned long last_itc;
  314. long itc_offset;
  315. unsigned long itc_check;
  316. unsigned long timer_check;
  317. unsigned long timer_pending;
  318. unsigned long vrr[8];
  319. unsigned long ibr[8];
  320. unsigned long dbr[8];
  321. unsigned long insvc[4]; /* Interrupt in service. */
  322. unsigned long xtp;
  323. unsigned long metaphysical_rr0; /* from kvm_arch (so is pinned) */
  324. unsigned long metaphysical_rr4; /* from kvm_arch (so is pinned) */
  325. unsigned long metaphysical_saved_rr0; /* from kvm_arch */
  326. unsigned long metaphysical_saved_rr4; /* from kvm_arch */
  327. unsigned long fp_psr; /*used for lazy float register */
  328. unsigned long saved_gp;
  329. /*for phycial emulation */
  330. int mode_flags;
  331. struct thash_cb vtlb;
  332. struct thash_cb vhpt;
  333. char irq_check;
  334. char irq_new_pending;
  335. unsigned long opcode;
  336. unsigned long cause;
  337. union context host;
  338. union context guest;
  339. };
  340. struct kvm_vm_stat {
  341. u64 remote_tlb_flush;
  342. };
  343. struct kvm_sal_data {
  344. unsigned long boot_ip;
  345. unsigned long boot_gp;
  346. };
  347. struct kvm_arch {
  348. unsigned long vm_base;
  349. unsigned long metaphysical_rr0;
  350. unsigned long metaphysical_rr4;
  351. unsigned long vmm_init_rr;
  352. unsigned long vhpt_base;
  353. unsigned long vtlb_base;
  354. unsigned long vpd_base;
  355. spinlock_t dirty_log_lock;
  356. struct kvm_ioapic *vioapic;
  357. struct kvm_vm_stat stat;
  358. struct kvm_sal_data rdv_sal_data;
  359. };
  360. union cpuid3_t {
  361. u64 value;
  362. struct {
  363. u64 number : 8;
  364. u64 revision : 8;
  365. u64 model : 8;
  366. u64 family : 8;
  367. u64 archrev : 8;
  368. u64 rv : 24;
  369. };
  370. };
  371. struct kvm_pt_regs {
  372. /* The following registers are saved by SAVE_MIN: */
  373. unsigned long b6; /* scratch */
  374. unsigned long b7; /* scratch */
  375. unsigned long ar_csd; /* used by cmp8xchg16 (scratch) */
  376. unsigned long ar_ssd; /* reserved for future use (scratch) */
  377. unsigned long r8; /* scratch (return value register 0) */
  378. unsigned long r9; /* scratch (return value register 1) */
  379. unsigned long r10; /* scratch (return value register 2) */
  380. unsigned long r11; /* scratch (return value register 3) */
  381. unsigned long cr_ipsr; /* interrupted task's psr */
  382. unsigned long cr_iip; /* interrupted task's instruction pointer */
  383. unsigned long cr_ifs; /* interrupted task's function state */
  384. unsigned long ar_unat; /* interrupted task's NaT register (preserved) */
  385. unsigned long ar_pfs; /* prev function state */
  386. unsigned long ar_rsc; /* RSE configuration */
  387. /* The following two are valid only if cr_ipsr.cpl > 0: */
  388. unsigned long ar_rnat; /* RSE NaT */
  389. unsigned long ar_bspstore; /* RSE bspstore */
  390. unsigned long pr; /* 64 predicate registers (1 bit each) */
  391. unsigned long b0; /* return pointer (bp) */
  392. unsigned long loadrs; /* size of dirty partition << 16 */
  393. unsigned long r1; /* the gp pointer */
  394. unsigned long r12; /* interrupted task's memory stack pointer */
  395. unsigned long r13; /* thread pointer */
  396. unsigned long ar_fpsr; /* floating point status (preserved) */
  397. unsigned long r15; /* scratch */
  398. /* The remaining registers are NOT saved for system calls. */
  399. unsigned long r14; /* scratch */
  400. unsigned long r2; /* scratch */
  401. unsigned long r3; /* scratch */
  402. unsigned long r16; /* scratch */
  403. unsigned long r17; /* scratch */
  404. unsigned long r18; /* scratch */
  405. unsigned long r19; /* scratch */
  406. unsigned long r20; /* scratch */
  407. unsigned long r21; /* scratch */
  408. unsigned long r22; /* scratch */
  409. unsigned long r23; /* scratch */
  410. unsigned long r24; /* scratch */
  411. unsigned long r25; /* scratch */
  412. unsigned long r26; /* scratch */
  413. unsigned long r27; /* scratch */
  414. unsigned long r28; /* scratch */
  415. unsigned long r29; /* scratch */
  416. unsigned long r30; /* scratch */
  417. unsigned long r31; /* scratch */
  418. unsigned long ar_ccv; /* compare/exchange value (scratch) */
  419. /*
  420. * Floating point registers that the kernel considers scratch:
  421. */
  422. struct ia64_fpreg f6; /* scratch */
  423. struct ia64_fpreg f7; /* scratch */
  424. struct ia64_fpreg f8; /* scratch */
  425. struct ia64_fpreg f9; /* scratch */
  426. struct ia64_fpreg f10; /* scratch */
  427. struct ia64_fpreg f11; /* scratch */
  428. unsigned long r4; /* preserved */
  429. unsigned long r5; /* preserved */
  430. unsigned long r6; /* preserved */
  431. unsigned long r7; /* preserved */
  432. unsigned long eml_unat; /* used for emulating instruction */
  433. unsigned long pad0; /* alignment pad */
  434. };
  435. static inline struct kvm_pt_regs *vcpu_regs(struct kvm_vcpu *v)
  436. {
  437. return (struct kvm_pt_regs *) ((unsigned long) v + IA64_STK_OFFSET) - 1;
  438. }
  439. typedef int kvm_vmm_entry(void);
  440. typedef void kvm_tramp_entry(union context *host, union context *guest);
  441. struct kvm_vmm_info{
  442. struct module *module;
  443. kvm_vmm_entry *vmm_entry;
  444. kvm_tramp_entry *tramp_entry;
  445. unsigned long vmm_ivt;
  446. };
  447. int kvm_highest_pending_irq(struct kvm_vcpu *vcpu);
  448. int kvm_emulate_halt(struct kvm_vcpu *vcpu);
  449. int kvm_pal_emul(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
  450. void kvm_sal_emul(struct kvm_vcpu *vcpu);
  451. static inline void kvm_inject_nmi(struct kvm_vcpu *vcpu) {}
  452. #endif