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