kvm_host.h 14 KB

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