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 23#ifndef __ASM_KVM_HOST_H 24#define __ASM_KVM_HOST_H 25 26#define KVM_USER_MEM_SLOTS 32 27 28#define KVM_COALESCED_MMIO_PAGE_OFFSET 1 29#define KVM_IRQCHIP_NUM_PINS KVM_IOAPIC_NUM_PINS 30 31/* define exit reasons from vmm to kvm*/ 32#define EXIT_REASON_VM_PANIC 0 33#define EXIT_REASON_MMIO_INSTRUCTION 1 34#define EXIT_REASON_PAL_CALL 2 35#define EXIT_REASON_SAL_CALL 3 36#define EXIT_REASON_SWITCH_RR6 4 37#define EXIT_REASON_VM_DESTROY 5 38#define EXIT_REASON_EXTERNAL_INTERRUPT 6 39#define EXIT_REASON_IPI 7 40#define EXIT_REASON_PTC_G 8 41#define EXIT_REASON_DEBUG 20 42 43/*Define vmm address space and vm data space.*/ 44#define KVM_VMM_SIZE (__IA64_UL_CONST(16)<<20) 45#define KVM_VMM_SHIFT 24 46#define KVM_VMM_BASE 0xD000000000000000 47#define VMM_SIZE (__IA64_UL_CONST(8)<<20) 48 49/* 50 * Define vm_buffer, used by PAL Services, base address. 51 * Note: vm_buffer is in the VMM-BLOCK, the size must be < 8M 52 */ 53#define KVM_VM_BUFFER_BASE (KVM_VMM_BASE + VMM_SIZE) 54#define KVM_VM_BUFFER_SIZE (__IA64_UL_CONST(8)<<20) 55 56/* 57 * kvm guest's data area looks as follow: 58 * 59 * +----------------------+ ------- KVM_VM_DATA_SIZE 60 * | vcpu[n]'s data | | ___________________KVM_STK_OFFSET 61 * | | | / | 62 * | .......... | | /vcpu's struct&stack | 63 * | .......... | | /---------------------|---- 0 64 * | vcpu[5]'s data | | / vpd | 65 * | vcpu[4]'s data | |/-----------------------| 66 * | vcpu[3]'s data | / vtlb | 67 * | vcpu[2]'s data | /|------------------------| 68 * | vcpu[1]'s data |/ | vhpt | 69 * | vcpu[0]'s data |____________________________| 70 * +----------------------+ | 71 * | memory dirty log | | 72 * +----------------------+ | 73 * | vm's data struct | | 74 * +----------------------+ | 75 * | | | 76 * | | | 77 * | | | 78 * | | | 79 * | | | 80 * | | | 81 * | | | 82 * | vm's p2m table | | 83 * | | | 84 * | | | 85 * | | | | 86 * vm's data->| | | | 87 * +----------------------+ ------- 0 88 * To support large memory, needs to increase the size of p2m. 89 * To support more vcpus, needs to ensure it has enough space to 90 * hold vcpus' data. 91 */ 92 93#define KVM_VM_DATA_SHIFT 26 94#define KVM_VM_DATA_SIZE (__IA64_UL_CONST(1) << KVM_VM_DATA_SHIFT) 95#define KVM_VM_DATA_BASE (KVM_VMM_BASE + KVM_VM_DATA_SIZE) 96 97#define KVM_P2M_BASE KVM_VM_DATA_BASE 98#define KVM_P2M_SIZE (__IA64_UL_CONST(24) << 20) 99 100#define VHPT_SHIFT 16 101#define VHPT_SIZE (__IA64_UL_CONST(1) << VHPT_SHIFT) 102#define VHPT_NUM_ENTRIES (__IA64_UL_CONST(1) << (VHPT_SHIFT-5)) 103 104#define VTLB_SHIFT 16 105#define VTLB_SIZE (__IA64_UL_CONST(1) << VTLB_SHIFT) 106#define VTLB_NUM_ENTRIES (1UL << (VHPT_SHIFT-5)) 107 108#define VPD_SHIFT 16 109#define VPD_SIZE (__IA64_UL_CONST(1) << VPD_SHIFT) 110 111#define VCPU_STRUCT_SHIFT 16 112#define VCPU_STRUCT_SIZE (__IA64_UL_CONST(1) << VCPU_STRUCT_SHIFT) 113 114/* 115 * This must match KVM_IA64_VCPU_STACK_{SHIFT,SIZE} arch/ia64/include/asm/kvm.h 116 */ 117#define KVM_STK_SHIFT 16 118#define KVM_STK_OFFSET (__IA64_UL_CONST(1)<< KVM_STK_SHIFT) 119 120#define KVM_VM_STRUCT_SHIFT 19 121#define KVM_VM_STRUCT_SIZE (__IA64_UL_CONST(1) << KVM_VM_STRUCT_SHIFT) 122 123#define KVM_MEM_DIRY_LOG_SHIFT 19 124#define KVM_MEM_DIRTY_LOG_SIZE (__IA64_UL_CONST(1) << KVM_MEM_DIRY_LOG_SHIFT) 125 126#ifndef __ASSEMBLY__ 127 128/*Define the max vcpus and memory for Guests.*/ 129#define KVM_MAX_VCPUS (KVM_VM_DATA_SIZE - KVM_P2M_SIZE - KVM_VM_STRUCT_SIZE -\ 130 KVM_MEM_DIRTY_LOG_SIZE) / sizeof(struct kvm_vcpu_data) 131#define KVM_MAX_MEM_SIZE (KVM_P2M_SIZE >> 3 << PAGE_SHIFT) 132 133#define VMM_LOG_LEN 256 134 135#include <linux/types.h> 136#include <linux/mm.h> 137#include <linux/kvm.h> 138#include <linux/kvm_para.h> 139#include <linux/kvm_types.h> 140 141#include <asm/pal.h> 142#include <asm/sal.h> 143#include <asm/page.h> 144 145struct kvm_vcpu_data { 146 char vcpu_vhpt[VHPT_SIZE]; 147 char vcpu_vtlb[VTLB_SIZE]; 148 char vcpu_vpd[VPD_SIZE]; 149 char vcpu_struct[VCPU_STRUCT_SIZE]; 150}; 151 152struct kvm_vm_data { 153 char kvm_p2m[KVM_P2M_SIZE]; 154 char kvm_vm_struct[KVM_VM_STRUCT_SIZE]; 155 char kvm_mem_dirty_log[KVM_MEM_DIRTY_LOG_SIZE]; 156 struct kvm_vcpu_data vcpu_data[KVM_MAX_VCPUS]; 157}; 158 159#define VCPU_BASE(n) (KVM_VM_DATA_BASE + \ 160 offsetof(struct kvm_vm_data, vcpu_data[n])) 161#define KVM_VM_BASE (KVM_VM_DATA_BASE + \ 162 offsetof(struct kvm_vm_data, kvm_vm_struct)) 163#define KVM_MEM_DIRTY_LOG_BASE KVM_VM_DATA_BASE + \ 164 offsetof(struct kvm_vm_data, kvm_mem_dirty_log) 165 166#define VHPT_BASE(n) (VCPU_BASE(n) + offsetof(struct kvm_vcpu_data, vcpu_vhpt)) 167#define VTLB_BASE(n) (VCPU_BASE(n) + offsetof(struct kvm_vcpu_data, vcpu_vtlb)) 168#define VPD_BASE(n) (VCPU_BASE(n) + offsetof(struct kvm_vcpu_data, vcpu_vpd)) 169#define VCPU_STRUCT_BASE(n) (VCPU_BASE(n) + \ 170 offsetof(struct kvm_vcpu_data, vcpu_struct)) 171 172/*IO section definitions*/ 173#define IOREQ_READ 1 174#define IOREQ_WRITE 0 175 176#define STATE_IOREQ_NONE 0 177#define STATE_IOREQ_READY 1 178#define STATE_IOREQ_INPROCESS 2 179#define STATE_IORESP_READY 3 180 181/*Guest Physical address layout.*/ 182#define GPFN_MEM (0UL << 60) /* Guest pfn is normal mem */ 183#define GPFN_FRAME_BUFFER (1UL << 60) /* VGA framebuffer */ 184#define GPFN_LOW_MMIO (2UL << 60) /* Low MMIO range */ 185#define GPFN_PIB (3UL << 60) /* PIB base */ 186#define GPFN_IOSAPIC (4UL << 60) /* IOSAPIC base */ 187#define GPFN_LEGACY_IO (5UL << 60) /* Legacy I/O base */ 188#define GPFN_GFW (6UL << 60) /* Guest Firmware */ 189#define GPFN_PHYS_MMIO (7UL << 60) /* Directed MMIO Range */ 190 191#define GPFN_IO_MASK (7UL << 60) /* Guest pfn is I/O type */ 192#define GPFN_INV_MASK (1UL << 63) /* Guest pfn is invalid */ 193#define INVALID_MFN (~0UL) 194#define MEM_G (1UL << 30) 195#define MEM_M (1UL << 20) 196#define MMIO_START (3 * MEM_G) 197#define MMIO_SIZE (512 * MEM_M) 198#define VGA_IO_START 0xA0000UL 199#define VGA_IO_SIZE 0x20000 200#define LEGACY_IO_START (MMIO_START + MMIO_SIZE) 201#define LEGACY_IO_SIZE (64 * MEM_M) 202#define IO_SAPIC_START 0xfec00000UL 203#define IO_SAPIC_SIZE 0x100000 204#define PIB_START 0xfee00000UL 205#define PIB_SIZE 0x200000 206#define GFW_START (4 * MEM_G - 16 * MEM_M) 207#define GFW_SIZE (16 * MEM_M) 208 209/*Deliver mode, defined for ioapic.c*/ 210#define dest_Fixed IOSAPIC_FIXED 211#define dest_LowestPrio IOSAPIC_LOWEST_PRIORITY 212 213#define NMI_VECTOR 2 214#define ExtINT_VECTOR 0 215#define NULL_VECTOR (-1) 216#define IA64_SPURIOUS_INT_VECTOR 0x0f 217 218#define VCPU_LID(v) (((u64)(v)->vcpu_id) << 24) 219 220/* 221 *Delivery mode 222 */ 223#define SAPIC_DELIV_SHIFT 8 224#define SAPIC_FIXED 0x0 225#define SAPIC_LOWEST_PRIORITY 0x1 226#define SAPIC_PMI 0x2 227#define SAPIC_NMI 0x4 228#define SAPIC_INIT 0x5 229#define SAPIC_EXTINT 0x7 230 231/* 232 * vcpu->requests bit members for arch 233 */ 234#define KVM_REQ_PTC_G 32 235#define KVM_REQ_RESUME 33 236 237struct kvm; 238struct kvm_vcpu; 239 240struct kvm_mmio_req { 241 uint64_t addr; /* physical address */ 242 uint64_t size; /* size in bytes */ 243 uint64_t data; /* data (or paddr of data) */ 244 uint8_t state:4; 245 uint8_t dir:1; /* 1=read, 0=write */ 246}; 247 248/*Pal data struct */ 249struct kvm_pal_call{ 250 /*In area*/ 251 uint64_t gr28; 252 uint64_t gr29; 253 uint64_t gr30; 254 uint64_t gr31; 255 /*Out area*/ 256 struct ia64_pal_retval ret; 257}; 258 259/* Sal data structure */ 260struct kvm_sal_call{ 261 /*In area*/ 262 uint64_t in0; 263 uint64_t in1; 264 uint64_t in2; 265 uint64_t in3; 266 uint64_t in4; 267 uint64_t in5; 268 uint64_t in6; 269 uint64_t in7; 270 struct sal_ret_values ret; 271}; 272 273/*Guest change rr6*/ 274struct kvm_switch_rr6 { 275 uint64_t old_rr; 276 uint64_t new_rr; 277}; 278 279union ia64_ipi_a{ 280 unsigned long val; 281 struct { 282 unsigned long rv : 3; 283 unsigned long ir : 1; 284 unsigned long eid : 8; 285 unsigned long id : 8; 286 unsigned long ib_base : 44; 287 }; 288}; 289 290union ia64_ipi_d { 291 unsigned long val; 292 struct { 293 unsigned long vector : 8; 294 unsigned long dm : 3; 295 unsigned long ig : 53; 296 }; 297}; 298 299/*ipi check exit data*/ 300struct kvm_ipi_data{ 301 union ia64_ipi_a addr; 302 union ia64_ipi_d data; 303}; 304 305/*global purge data*/ 306struct kvm_ptc_g { 307 unsigned long vaddr; 308 unsigned long rr; 309 unsigned long ps; 310 struct kvm_vcpu *vcpu; 311}; 312 313/*Exit control data */ 314struct exit_ctl_data{ 315 uint32_t exit_reason; 316 uint32_t vm_status; 317 union { 318 struct kvm_mmio_req ioreq; 319 struct kvm_pal_call pal_data; 320 struct kvm_sal_call sal_data; 321 struct kvm_switch_rr6 rr_data; 322 struct kvm_ipi_data ipi_data; 323 struct kvm_ptc_g ptc_g_data; 324 } u; 325}; 326 327union pte_flags { 328 unsigned long val; 329 struct { 330 unsigned long p : 1; /*0 */ 331 unsigned long : 1; /* 1 */ 332 unsigned long ma : 3; /* 2-4 */ 333 unsigned long a : 1; /* 5 */ 334 unsigned long d : 1; /* 6 */ 335 unsigned long pl : 2; /* 7-8 */ 336 unsigned long ar : 3; /* 9-11 */ 337 unsigned long ppn : 38; /* 12-49 */ 338 unsigned long : 2; /* 50-51 */ 339 unsigned long ed : 1; /* 52 */ 340 }; 341}; 342 343union ia64_pta { 344 unsigned long val; 345 struct { 346 unsigned long ve : 1; 347 unsigned long reserved0 : 1; 348 unsigned long size : 6; 349 unsigned long vf : 1; 350 unsigned long reserved1 : 6; 351 unsigned long base : 49; 352 }; 353}; 354 355struct thash_cb { 356 /* THASH base information */ 357 struct thash_data *hash; /* hash table pointer */ 358 union ia64_pta pta; 359 int num; 360}; 361 362struct kvm_vcpu_stat { 363 u32 halt_wakeup; 364}; 365 366struct kvm_vcpu_arch { 367 int launched; 368 int last_exit; 369 int last_run_cpu; 370 int vmm_tr_slot; 371 int vm_tr_slot; 372 int sn_rtc_tr_slot; 373 374#define KVM_MP_STATE_RUNNABLE 0 375#define KVM_MP_STATE_UNINITIALIZED 1 376#define KVM_MP_STATE_INIT_RECEIVED 2 377#define KVM_MP_STATE_HALTED 3 378 int mp_state; 379 380#define MAX_PTC_G_NUM 3 381 int ptc_g_count; 382 struct kvm_ptc_g ptc_g_data[MAX_PTC_G_NUM]; 383 384 /*halt timer to wake up sleepy vcpus*/ 385 struct hrtimer hlt_timer; 386 long ht_active; 387 388 struct kvm_lapic *apic; /* kernel irqchip context */ 389 struct vpd *vpd; 390 391 /* Exit data for vmm_transition*/ 392 struct exit_ctl_data exit_data; 393 394 cpumask_t cache_coherent_map; 395 396 unsigned long vmm_rr; 397 unsigned long host_rr6; 398 unsigned long psbits[8]; 399 unsigned long cr_iipa; 400 unsigned long cr_isr; 401 unsigned long vsa_base; 402 unsigned long dirty_log_lock_pa; 403 unsigned long __gp; 404 /* TR and TC. */ 405 struct thash_data itrs[NITRS]; 406 struct thash_data dtrs[NDTRS]; 407 /* Bit is set if there is a tr/tc for the region. */ 408 unsigned char itr_regions; 409 unsigned char dtr_regions; 410 unsigned char tc_regions; 411 /* purge all */ 412 unsigned long ptce_base; 413 unsigned long ptce_count[2]; 414 unsigned long ptce_stride[2]; 415 /* itc/itm */ 416 unsigned long last_itc; 417 long itc_offset; 418 unsigned long itc_check; 419 unsigned long timer_check; 420 unsigned int timer_pending; 421 unsigned int timer_fired; 422 423 unsigned long vrr[8]; 424 unsigned long ibr[8]; 425 unsigned long dbr[8]; 426 unsigned long insvc[4]; /* Interrupt in service. */ 427 unsigned long xtp; 428 429 unsigned long metaphysical_rr0; /* from kvm_arch (so is pinned) */ 430 unsigned long metaphysical_rr4; /* from kvm_arch (so is pinned) */ 431 unsigned long metaphysical_saved_rr0; /* from kvm_arch */ 432 unsigned long metaphysical_saved_rr4; /* from kvm_arch */ 433 unsigned long fp_psr; /*used for lazy float register */ 434 unsigned long saved_gp; 435 /*for phycial emulation */ 436 int mode_flags; 437 struct thash_cb vtlb; 438 struct thash_cb vhpt; 439 char irq_check; 440 char irq_new_pending; 441 442 unsigned long opcode; 443 unsigned long cause; 444 char log_buf[VMM_LOG_LEN]; 445 union context host; 446 union context guest; 447 448 char mmio_data[8]; 449}; 450 451struct kvm_vm_stat { 452 u64 remote_tlb_flush; 453}; 454 455struct kvm_sal_data { 456 unsigned long boot_ip; 457 unsigned long boot_gp; 458}; 459 460struct kvm_arch_memory_slot { 461}; 462 463struct kvm_arch { 464 spinlock_t dirty_log_lock; 465 466 unsigned long vm_base; 467 unsigned long metaphysical_rr0; 468 unsigned long metaphysical_rr4; 469 unsigned long vmm_init_rr; 470 471 int is_sn2; 472 473 struct kvm_ioapic *vioapic; 474 struct kvm_vm_stat stat; 475 struct kvm_sal_data rdv_sal_data; 476 477 struct list_head assigned_dev_head; 478 struct iommu_domain *iommu_domain; 479 bool iommu_noncoherent; 480 481 unsigned long irq_sources_bitmap; 482 unsigned long irq_states[KVM_IOAPIC_NUM_PINS]; 483}; 484 485union cpuid3_t { 486 u64 value; 487 struct { 488 u64 number : 8; 489 u64 revision : 8; 490 u64 model : 8; 491 u64 family : 8; 492 u64 archrev : 8; 493 u64 rv : 24; 494 }; 495}; 496 497struct kvm_pt_regs { 498 /* The following registers are saved by SAVE_MIN: */ 499 unsigned long b6; /* scratch */ 500 unsigned long b7; /* scratch */ 501 502 unsigned long ar_csd; /* used by cmp8xchg16 (scratch) */ 503 unsigned long ar_ssd; /* reserved for future use (scratch) */ 504 505 unsigned long r8; /* scratch (return value register 0) */ 506 unsigned long r9; /* scratch (return value register 1) */ 507 unsigned long r10; /* scratch (return value register 2) */ 508 unsigned long r11; /* scratch (return value register 3) */ 509 510 unsigned long cr_ipsr; /* interrupted task's psr */ 511 unsigned long cr_iip; /* interrupted task's instruction pointer */ 512 unsigned long cr_ifs; /* interrupted task's function state */ 513 514 unsigned long ar_unat; /* interrupted task's NaT register (preserved) */ 515 unsigned long ar_pfs; /* prev function state */ 516 unsigned long ar_rsc; /* RSE configuration */ 517 /* The following two are valid only if cr_ipsr.cpl > 0: */ 518 unsigned long ar_rnat; /* RSE NaT */ 519 unsigned long ar_bspstore; /* RSE bspstore */ 520 521 unsigned long pr; /* 64 predicate registers (1 bit each) */ 522 unsigned long b0; /* return pointer (bp) */ 523 unsigned long loadrs; /* size of dirty partition << 16 */ 524 525 unsigned long r1; /* the gp pointer */ 526 unsigned long r12; /* interrupted task's memory stack pointer */ 527 unsigned long r13; /* thread pointer */ 528 529 unsigned long ar_fpsr; /* floating point status (preserved) */ 530 unsigned long r15; /* scratch */ 531 532 /* The remaining registers are NOT saved for system calls. */ 533 unsigned long r14; /* scratch */ 534 unsigned long r2; /* scratch */ 535 unsigned long r3; /* scratch */ 536 unsigned long r16; /* scratch */ 537 unsigned long r17; /* scratch */ 538 unsigned long r18; /* scratch */ 539 unsigned long r19; /* scratch */ 540 unsigned long r20; /* scratch */ 541 unsigned long r21; /* scratch */ 542 unsigned long r22; /* scratch */ 543 unsigned long r23; /* scratch */ 544 unsigned long r24; /* scratch */ 545 unsigned long r25; /* scratch */ 546 unsigned long r26; /* scratch */ 547 unsigned long r27; /* scratch */ 548 unsigned long r28; /* scratch */ 549 unsigned long r29; /* scratch */ 550 unsigned long r30; /* scratch */ 551 unsigned long r31; /* scratch */ 552 unsigned long ar_ccv; /* compare/exchange value (scratch) */ 553 554 /* 555 * Floating point registers that the kernel considers scratch: 556 */ 557 struct ia64_fpreg f6; /* scratch */ 558 struct ia64_fpreg f7; /* scratch */ 559 struct ia64_fpreg f8; /* scratch */ 560 struct ia64_fpreg f9; /* scratch */ 561 struct ia64_fpreg f10; /* scratch */ 562 struct ia64_fpreg f11; /* scratch */ 563 564 unsigned long r4; /* preserved */ 565 unsigned long r5; /* preserved */ 566 unsigned long r6; /* preserved */ 567 unsigned long r7; /* preserved */ 568 unsigned long eml_unat; /* used for emulating instruction */ 569 unsigned long pad0; /* alignment pad */ 570}; 571 572static inline struct kvm_pt_regs *vcpu_regs(struct kvm_vcpu *v) 573{ 574 return (struct kvm_pt_regs *) ((unsigned long) v + KVM_STK_OFFSET) - 1; 575} 576 577typedef int kvm_vmm_entry(void); 578typedef void kvm_tramp_entry(union context *host, union context *guest); 579 580struct kvm_vmm_info{ 581 struct module *module; 582 kvm_vmm_entry *vmm_entry; 583 kvm_tramp_entry *tramp_entry; 584 unsigned long vmm_ivt; 585 unsigned long patch_mov_ar; 586 unsigned long patch_mov_ar_sn2; 587}; 588 589int kvm_highest_pending_irq(struct kvm_vcpu *vcpu); 590int kvm_emulate_halt(struct kvm_vcpu *vcpu); 591int kvm_pal_emul(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run); 592void kvm_sal_emul(struct kvm_vcpu *vcpu); 593 594#define __KVM_HAVE_ARCH_VM_ALLOC 1 595struct kvm *kvm_arch_alloc_vm(void); 596void kvm_arch_free_vm(struct kvm *kvm); 597 598#endif /* __ASSEMBLY__*/ 599 600#endif 601