2 * VMI specific paravirt-ops implementation
4 * Copyright (C) 2005, VMware, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Send feedback to zach@vmware.com
25 #include <linux/module.h>
26 #include <linux/cpu.h>
27 #include <linux/bootmem.h>
29 #include <linux/highmem.h>
32 #include <asm/fixmap.h>
33 #include <asm/apicdef.h>
35 #include <asm/processor.h>
36 #include <asm/timer.h>
37 #include <asm/vmi_time.h>
38 #include <asm/kmap_types.h>
40 /* Convenient for calling VMI functions indirectly in the ROM */
41 typedef u32 __attribute__((regparm(1))) (VROMFUNC)(void);
42 typedef u64 __attribute__((regparm(2))) (VROMLONGFUNC)(int);
44 #define call_vrom_func(rom,func) \
45 (((VROMFUNC *)(rom->func))())
47 #define call_vrom_long_func(rom,func,arg) \
48 (((VROMLONGFUNC *)(rom->func)) (arg))
50 static struct vrom_header *vmi_rom;
51 static int disable_pge;
52 static int disable_pse;
53 static int disable_sep;
54 static int disable_tsc;
55 static int disable_mtrr;
56 static int disable_noidle;
57 static int disable_vmi_timer;
59 /* Cached VMI operations */
61 void (*cpuid)(void /* non-c */);
62 void (*_set_ldt)(u32 selector);
63 void (*set_tr)(u32 selector);
64 void (*set_kernel_stack)(u32 selector, u32 esp0);
65 void (*allocate_page)(u32, u32, u32, u32, u32);
66 void (*release_page)(u32, u32);
67 void (*set_pte)(pte_t, pte_t *, unsigned);
68 void (*update_pte)(pte_t *, unsigned);
69 void (*set_linear_mapping)(int, void *, u32, u32);
70 void (*_flush_tlb)(int);
71 void (*set_initial_ap_state)(int, int);
73 void (*set_lazy_mode)(int mode);
76 /* Cached VMI operations */
77 struct vmi_timer_ops vmi_timer_ops;
80 * VMI patching routines.
82 #define MNEM_CALL 0xe8
86 #define IRQ_PATCH_INT_MASK 0
87 #define IRQ_PATCH_DISABLE 5
89 static inline void patch_offset(unsigned char *eip, unsigned char *dest)
91 *(unsigned long *)(eip+1) = dest-eip-5;
94 static unsigned patch_internal(int call, unsigned len, void *insns)
97 struct vmi_relocation_info *const rel = (struct vmi_relocation_info *)&reloc;
98 reloc = call_vrom_long_func(vmi_rom, get_reloc, call);
100 case VMI_RELOCATION_CALL_REL:
102 *(char *)insns = MNEM_CALL;
103 patch_offset(insns, rel->eip);
106 case VMI_RELOCATION_JUMP_REL:
108 *(char *)insns = MNEM_JMP;
109 patch_offset(insns, rel->eip);
112 case VMI_RELOCATION_NOP:
113 /* obliterate the whole thing */
116 case VMI_RELOCATION_NONE:
117 /* leave native code in place */
127 * Apply patch if appropriate, return length of new instruction
128 * sequence. The callee does nop padding for us.
130 static unsigned vmi_patch(u8 type, u16 clobbers, void *insns, unsigned len)
133 case PARAVIRT_PATCH(irq_disable):
134 return patch_internal(VMI_CALL_DisableInterrupts, len, insns);
135 case PARAVIRT_PATCH(irq_enable):
136 return patch_internal(VMI_CALL_EnableInterrupts, len, insns);
137 case PARAVIRT_PATCH(restore_fl):
138 return patch_internal(VMI_CALL_SetInterruptMask, len, insns);
139 case PARAVIRT_PATCH(save_fl):
140 return patch_internal(VMI_CALL_GetInterruptMask, len, insns);
141 case PARAVIRT_PATCH(iret):
142 return patch_internal(VMI_CALL_IRET, len, insns);
143 case PARAVIRT_PATCH(irq_enable_sysexit):
144 return patch_internal(VMI_CALL_SYSEXIT, len, insns);
151 /* CPUID has non-C semantics, and paravirt-ops API doesn't match hardware ISA */
152 static void vmi_cpuid(unsigned int *eax, unsigned int *ebx,
153 unsigned int *ecx, unsigned int *edx)
158 asm volatile ("call *%6"
163 : "0" (*eax), "2" (*ecx), "r" (vmi_ops.cpuid));
166 *edx &= ~X86_FEATURE_PSE;
168 *edx &= ~X86_FEATURE_PGE;
170 *edx &= ~X86_FEATURE_SEP;
172 *edx &= ~X86_FEATURE_TSC;
174 *edx &= ~X86_FEATURE_MTRR;
178 static inline void vmi_maybe_load_tls(struct desc_struct *gdt, int nr, struct desc_struct *new)
180 if (gdt[nr].a != new->a || gdt[nr].b != new->b)
181 write_gdt_entry(gdt, nr, new->a, new->b);
184 static void vmi_load_tls(struct thread_struct *t, unsigned int cpu)
186 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
187 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 0, &t->tls_array[0]);
188 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 1, &t->tls_array[1]);
189 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 2, &t->tls_array[2]);
192 static void vmi_set_ldt(const void *addr, unsigned entries)
194 unsigned cpu = smp_processor_id();
197 pack_descriptor(&low, &high, (unsigned long)addr,
198 entries * sizeof(struct desc_struct) - 1,
200 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, low, high);
201 vmi_ops._set_ldt(entries ? GDT_ENTRY_LDT*sizeof(struct desc_struct) : 0);
204 static void vmi_set_tr(void)
206 vmi_ops.set_tr(GDT_ENTRY_TSS*sizeof(struct desc_struct));
209 static void vmi_load_esp0(struct tss_struct *tss,
210 struct thread_struct *thread)
212 tss->x86_tss.esp0 = thread->esp0;
214 /* This can only happen when SEP is enabled, no need to test "SEP"arately */
215 if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
216 tss->x86_tss.ss1 = thread->sysenter_cs;
217 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
219 vmi_ops.set_kernel_stack(__KERNEL_DS, tss->x86_tss.esp0);
222 static void vmi_flush_tlb_user(void)
224 vmi_ops._flush_tlb(VMI_FLUSH_TLB);
227 static void vmi_flush_tlb_kernel(void)
229 vmi_ops._flush_tlb(VMI_FLUSH_TLB | VMI_FLUSH_GLOBAL);
232 /* Stub to do nothing at all; used for delays and unimplemented calls */
233 static void vmi_nop(void)
237 #ifdef CONFIG_DEBUG_PAGE_TYPE
239 #ifdef CONFIG_X86_PAE
240 #define MAX_BOOT_PTS (2048+4+1)
242 #define MAX_BOOT_PTS (1024+1)
246 * During boot, mem_map is not yet available in paging_init, so stash
247 * all the boot page allocations here.
252 } boot_page_allocations[MAX_BOOT_PTS];
253 static int num_boot_page_allocations;
254 static int boot_allocations_applied;
256 void vmi_apply_boot_page_allocations(void)
260 for (i = 0; i < num_boot_page_allocations; i++) {
261 struct page *page = pfn_to_page(boot_page_allocations[i].pfn);
262 page->type = boot_page_allocations[i].type;
263 page->type = boot_page_allocations[i].type &
264 ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
266 boot_allocations_applied = 1;
269 static void record_page_type(u32 pfn, int type)
271 BUG_ON(num_boot_page_allocations >= MAX_BOOT_PTS);
272 boot_page_allocations[num_boot_page_allocations].pfn = pfn;
273 boot_page_allocations[num_boot_page_allocations].type = type;
274 num_boot_page_allocations++;
277 static void check_zeroed_page(u32 pfn, int type, struct page *page)
281 int limit = PAGE_SIZE / sizeof(int);
283 if (page_address(page))
284 ptr = (u32 *)page_address(page);
286 ptr = (u32 *)__va(pfn << PAGE_SHIFT);
288 * When cloning the root in non-PAE mode, only the userspace
289 * pdes need to be zeroed.
291 if (type & VMI_PAGE_CLONE)
292 limit = USER_PTRS_PER_PGD;
293 for (i = 0; i < limit; i++)
298 * We stash the page type into struct page so we can verify the page
299 * types are used properly.
301 static void vmi_set_page_type(u32 pfn, int type)
303 /* PAE can have multiple roots per page - don't track */
304 if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
307 if (boot_allocations_applied) {
308 struct page *page = pfn_to_page(pfn);
309 if (type != VMI_PAGE_NORMAL)
312 BUG_ON(page->type == VMI_PAGE_NORMAL);
313 page->type = type & ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
314 if (type & VMI_PAGE_ZEROED)
315 check_zeroed_page(pfn, type, page);
317 record_page_type(pfn, type);
321 static void vmi_check_page_type(u32 pfn, int type)
323 /* PAE can have multiple roots per page - skip checks */
324 if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
327 type &= ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
328 if (boot_allocations_applied) {
329 struct page *page = pfn_to_page(pfn);
330 BUG_ON((page->type ^ type) & VMI_PAGE_PAE);
331 BUG_ON(type == VMI_PAGE_NORMAL && page->type);
332 BUG_ON((type & page->type) == 0);
336 #define vmi_set_page_type(p,t) do { } while (0)
337 #define vmi_check_page_type(p,t) do { } while (0)
340 #ifdef CONFIG_HIGHPTE
341 static void *vmi_kmap_atomic_pte(struct page *page, enum km_type type)
343 void *va = kmap_atomic(page, type);
346 * Internally, the VMI ROM must map virtual addresses to physical
347 * addresses for processing MMU updates. By the time MMU updates
348 * are issued, this information is typically already lost.
349 * Fortunately, the VMI provides a cache of mapping slots for active
352 * We use slot zero for the linear mapping of physical memory, and
353 * in HIGHPTE kernels, slot 1 and 2 for KM_PTE0 and KM_PTE1.
355 * args: SLOT VA COUNT PFN
357 BUG_ON(type != KM_PTE0 && type != KM_PTE1);
358 vmi_ops.set_linear_mapping((type - KM_PTE0)+1, va, 1, page_to_pfn(page));
364 static void vmi_allocate_pt(u32 pfn)
366 vmi_set_page_type(pfn, VMI_PAGE_L1);
367 vmi_ops.allocate_page(pfn, VMI_PAGE_L1, 0, 0, 0);
370 static void vmi_allocate_pd(u32 pfn)
373 * This call comes in very early, before mem_map is setup.
374 * It is called only for swapper_pg_dir, which already has
377 vmi_set_page_type(pfn, VMI_PAGE_L2);
378 vmi_ops.allocate_page(pfn, VMI_PAGE_L2, 0, 0, 0);
381 static void vmi_allocate_pd_clone(u32 pfn, u32 clonepfn, u32 start, u32 count)
383 vmi_set_page_type(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE);
384 vmi_check_page_type(clonepfn, VMI_PAGE_L2);
385 vmi_ops.allocate_page(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE, clonepfn, start, count);
388 static void vmi_release_pt(u32 pfn)
390 vmi_ops.release_page(pfn, VMI_PAGE_L1);
391 vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
394 static void vmi_release_pd(u32 pfn)
396 vmi_ops.release_page(pfn, VMI_PAGE_L2);
397 vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
401 * Helper macros for MMU update flags. We can defer updates until a flush
402 * or page invalidation only if the update is to the current address space
403 * (otherwise, there is no flush). We must check against init_mm, since
404 * this could be a kernel update, which usually passes init_mm, although
405 * sometimes this check can be skipped if we know the particular function
406 * is only called on user mode PTEs. We could change the kernel to pass
407 * current->active_mm here, but in particular, I was unsure if changing
408 * mm/highmem.c to do this would still be correct on other architectures.
410 #define is_current_as(mm, mustbeuser) ((mm) == current->active_mm || \
411 (!mustbeuser && (mm) == &init_mm))
412 #define vmi_flags_addr(mm, addr, level, user) \
413 ((level) | (is_current_as(mm, user) ? \
414 (VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
415 #define vmi_flags_addr_defer(mm, addr, level, user) \
416 ((level) | (is_current_as(mm, user) ? \
417 (VMI_PAGE_DEFER | VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
419 static void vmi_update_pte(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
421 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
422 vmi_ops.update_pte(ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
425 static void vmi_update_pte_defer(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
427 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
428 vmi_ops.update_pte(ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 0));
431 static void vmi_set_pte(pte_t *ptep, pte_t pte)
433 /* XXX because of set_pmd_pte, this can be called on PT or PD layers */
434 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE | VMI_PAGE_PD);
435 vmi_ops.set_pte(pte, ptep, VMI_PAGE_PT);
438 static void vmi_set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
440 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
441 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
444 static void vmi_set_pmd(pmd_t *pmdp, pmd_t pmdval)
446 #ifdef CONFIG_X86_PAE
447 const pte_t pte = { pmdval.pmd, pmdval.pmd >> 32 };
448 vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PMD);
450 const pte_t pte = { pmdval.pud.pgd.pgd };
451 vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PGD);
453 vmi_ops.set_pte(pte, (pte_t *)pmdp, VMI_PAGE_PD);
456 #ifdef CONFIG_X86_PAE
458 static void vmi_set_pte_atomic(pte_t *ptep, pte_t pteval)
461 * XXX This is called from set_pmd_pte, but at both PT
462 * and PD layers so the VMI_PAGE_PT flag is wrong. But
463 * it is only called for large page mapping changes,
464 * the Xen backend, doesn't support large pages, and the
465 * ESX backend doesn't depend on the flag.
467 set_64bit((unsigned long long *)ptep,pte_val(pteval));
468 vmi_ops.update_pte(ptep, VMI_PAGE_PT);
471 static void vmi_set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
473 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
474 vmi_ops.set_pte(pte, ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 1));
477 static void vmi_set_pud(pud_t *pudp, pud_t pudval)
480 const pte_t pte = { pudval.pgd.pgd, pudval.pgd.pgd >> 32 };
481 vmi_check_page_type(__pa(pudp) >> PAGE_SHIFT, VMI_PAGE_PGD);
482 vmi_ops.set_pte(pte, (pte_t *)pudp, VMI_PAGE_PDP);
485 static void vmi_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
487 const pte_t pte = { 0 };
488 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
489 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
492 static void vmi_pmd_clear(pmd_t *pmd)
494 const pte_t pte = { 0 };
495 vmi_check_page_type(__pa(pmd) >> PAGE_SHIFT, VMI_PAGE_PMD);
496 vmi_ops.set_pte(pte, (pte_t *)pmd, VMI_PAGE_PD);
501 static void __devinit
502 vmi_startup_ipi_hook(int phys_apicid, unsigned long start_eip,
503 unsigned long start_esp)
505 struct vmi_ap_state ap;
507 /* Default everything to zero. This is fine for most GPRs. */
508 memset(&ap, 0, sizeof(struct vmi_ap_state));
510 ap.gdtr_limit = GDT_SIZE - 1;
511 ap.gdtr_base = (unsigned long) get_cpu_gdt_table(phys_apicid);
513 ap.idtr_limit = IDT_ENTRIES * 8 - 1;
514 ap.idtr_base = (unsigned long) idt_table;
519 ap.eip = (unsigned long) start_eip;
521 ap.esp = (unsigned long) start_esp;
525 ap.fs = __KERNEL_PERCPU;
530 #ifdef CONFIG_X86_PAE
531 /* efer should match BSP efer. */
534 rdmsr(MSR_EFER, l, h);
535 ap.efer = (unsigned long long) h << 32 | l;
539 ap.cr3 = __pa(swapper_pg_dir);
540 /* Protected mode, paging, AM, WP, NE, MP. */
542 ap.cr4 = mmu_cr4_features;
543 vmi_ops.set_initial_ap_state((u32)&ap, phys_apicid);
547 static void vmi_set_lazy_mode(enum paravirt_lazy_mode mode)
549 static DEFINE_PER_CPU(enum paravirt_lazy_mode, lazy_mode);
551 if (!vmi_ops.set_lazy_mode)
554 /* Modes should never nest or overlap */
555 BUG_ON(__get_cpu_var(lazy_mode) && !(mode == PARAVIRT_LAZY_NONE ||
556 mode == PARAVIRT_LAZY_FLUSH));
558 if (mode == PARAVIRT_LAZY_FLUSH) {
559 vmi_ops.set_lazy_mode(0);
560 vmi_ops.set_lazy_mode(__get_cpu_var(lazy_mode));
562 vmi_ops.set_lazy_mode(mode);
563 __get_cpu_var(lazy_mode) = mode;
567 static inline int __init check_vmi_rom(struct vrom_header *rom)
569 struct pci_header *pci;
570 struct pnp_header *pnp;
571 const char *manufacturer = "UNKNOWN";
572 const char *product = "UNKNOWN";
573 const char *license = "unspecified";
575 if (rom->rom_signature != 0xaa55)
577 if (rom->vrom_signature != VMI_SIGNATURE)
579 if (rom->api_version_maj != VMI_API_REV_MAJOR ||
580 rom->api_version_min+1 < VMI_API_REV_MINOR+1) {
581 printk(KERN_WARNING "VMI: Found mismatched rom version %d.%d\n",
582 rom->api_version_maj,
583 rom->api_version_min);
588 * Relying on the VMI_SIGNATURE field is not 100% safe, so check
589 * the PCI header and device type to make sure this is really a
592 if (!rom->pci_header_offs) {
593 printk(KERN_WARNING "VMI: ROM does not contain PCI header.\n");
597 pci = (struct pci_header *)((char *)rom+rom->pci_header_offs);
598 if (pci->vendorID != PCI_VENDOR_ID_VMWARE ||
599 pci->deviceID != PCI_DEVICE_ID_VMWARE_VMI) {
600 /* Allow it to run... anyways, but warn */
601 printk(KERN_WARNING "VMI: ROM from unknown manufacturer\n");
604 if (rom->pnp_header_offs) {
605 pnp = (struct pnp_header *)((char *)rom+rom->pnp_header_offs);
606 if (pnp->manufacturer_offset)
607 manufacturer = (const char *)rom+pnp->manufacturer_offset;
608 if (pnp->product_offset)
609 product = (const char *)rom+pnp->product_offset;
612 if (rom->license_offs)
613 license = (char *)rom+rom->license_offs;
615 printk(KERN_INFO "VMI: Found %s %s, API version %d.%d, ROM version %d.%d\n",
616 manufacturer, product,
617 rom->api_version_maj, rom->api_version_min,
618 pci->rom_version_maj, pci->rom_version_min);
620 /* Don't allow BSD/MIT here for now because we don't want to end up
621 with any binary only shim layers */
622 if (strcmp(license, "GPL") && strcmp(license, "GPL v2")) {
623 printk(KERN_WARNING "VMI: Non GPL license `%s' found for ROM. Not used.\n",
632 * Probe for the VMI option ROM
634 static inline int __init probe_vmi_rom(void)
638 /* VMI ROM is in option ROM area, check signature */
639 for (base = 0xC0000; base < 0xE0000; base += 2048) {
640 struct vrom_header *romstart;
641 romstart = (struct vrom_header *)isa_bus_to_virt(base);
642 if (check_vmi_rom(romstart)) {
651 * VMI setup common to all processors
653 void vmi_bringup(void)
655 /* We must establish the lowmem mapping for MMU ops to work */
656 if (vmi_ops.set_linear_mapping)
657 vmi_ops.set_linear_mapping(0, (void *)__PAGE_OFFSET, max_low_pfn, 0);
661 * Return a pointer to a VMI function or NULL if unimplemented
663 static void *vmi_get_function(int vmicall)
666 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
667 reloc = call_vrom_long_func(vmi_rom, get_reloc, vmicall);
668 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);
669 if (rel->type == VMI_RELOCATION_CALL_REL)
670 return (void *)rel->eip;
676 * Helper macro for making the VMI paravirt-ops fill code readable.
677 * For unimplemented operations, fall back to default, unless nop
678 * is returned by the ROM.
680 #define para_fill(opname, vmicall) \
682 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
683 VMI_CALL_##vmicall); \
684 if (rel->type == VMI_RELOCATION_CALL_REL) \
685 paravirt_ops.opname = (void *)rel->eip; \
686 else if (rel->type == VMI_RELOCATION_NOP) \
687 paravirt_ops.opname = (void *)vmi_nop; \
688 else if (rel->type != VMI_RELOCATION_NONE) \
689 printk(KERN_WARNING "VMI: Unknown relocation " \
690 "type %d for " #vmicall"\n",\
695 * Helper macro for making the VMI paravirt-ops fill code readable.
696 * For cached operations which do not match the VMI ROM ABI and must
697 * go through a tranlation stub. Ignore NOPs, since it is not clear
698 * a NOP * VMI function corresponds to a NOP paravirt-op when the
699 * functions are not in 1-1 correspondence.
701 #define para_wrap(opname, wrapper, cache, vmicall) \
703 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
704 VMI_CALL_##vmicall); \
705 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL); \
706 if (rel->type == VMI_RELOCATION_CALL_REL) { \
707 paravirt_ops.opname = wrapper; \
708 vmi_ops.cache = (void *)rel->eip; \
713 * Activate the VMI interface and switch into paravirtualized mode
715 static inline int __init activate_vmi(void)
719 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
721 if (call_vrom_func(vmi_rom, vmi_init) != 0) {
722 printk(KERN_ERR "VMI ROM failed to initialize!");
725 savesegment(cs, kernel_cs);
727 paravirt_ops.paravirt_enabled = 1;
728 paravirt_ops.kernel_rpl = kernel_cs & SEGMENT_RPL_MASK;
730 paravirt_ops.patch = vmi_patch;
731 paravirt_ops.name = "vmi";
734 * Many of these operations are ABI compatible with VMI.
735 * This means we can fill in the paravirt-ops with direct
736 * pointers into the VMI ROM. If the calling convention for
737 * these operations changes, this code needs to be updated.
740 * CPUID paravirt-op uses pointers, not the native ISA
741 * halt has no VMI equivalent; all VMI halts are "safe"
742 * no MSR support yet - just trap and emulate. VMI uses the
743 * same ABI as the native ISA, but Linux wants exceptions
744 * from bogus MSR read / write handled
745 * rdpmc is not yet used in Linux
748 /* CPUID is special, so very special it gets wrapped like a present */
749 para_wrap(cpuid, vmi_cpuid, cpuid, CPUID);
751 para_fill(clts, CLTS);
752 para_fill(get_debugreg, GetDR);
753 para_fill(set_debugreg, SetDR);
754 para_fill(read_cr0, GetCR0);
755 para_fill(read_cr2, GetCR2);
756 para_fill(read_cr3, GetCR3);
757 para_fill(read_cr4, GetCR4);
758 para_fill(write_cr0, SetCR0);
759 para_fill(write_cr2, SetCR2);
760 para_fill(write_cr3, SetCR3);
761 para_fill(write_cr4, SetCR4);
762 para_fill(save_fl, GetInterruptMask);
763 para_fill(restore_fl, SetInterruptMask);
764 para_fill(irq_disable, DisableInterrupts);
765 para_fill(irq_enable, EnableInterrupts);
767 para_fill(wbinvd, WBINVD);
768 para_fill(read_tsc, RDTSC);
770 /* The following we emulate with trap and emulate for now */
771 /* paravirt_ops.read_msr = vmi_rdmsr */
772 /* paravirt_ops.write_msr = vmi_wrmsr */
773 /* paravirt_ops.rdpmc = vmi_rdpmc */
775 /* TR interface doesn't pass TR value, wrap */
776 para_wrap(load_tr_desc, vmi_set_tr, set_tr, SetTR);
778 /* LDT is special, too */
779 para_wrap(set_ldt, vmi_set_ldt, _set_ldt, SetLDT);
781 para_fill(load_gdt, SetGDT);
782 para_fill(load_idt, SetIDT);
783 para_fill(store_gdt, GetGDT);
784 para_fill(store_idt, GetIDT);
785 para_fill(store_tr, GetTR);
786 paravirt_ops.load_tls = vmi_load_tls;
787 para_fill(write_ldt_entry, WriteLDTEntry);
788 para_fill(write_gdt_entry, WriteGDTEntry);
789 para_fill(write_idt_entry, WriteIDTEntry);
790 para_wrap(load_esp0, vmi_load_esp0, set_kernel_stack, UpdateKernelStack);
791 para_fill(set_iopl_mask, SetIOPLMask);
792 para_fill(io_delay, IODelay);
793 para_wrap(set_lazy_mode, vmi_set_lazy_mode, set_lazy_mode, SetLazyMode);
795 /* user and kernel flush are just handled with different flags to FlushTLB */
796 para_wrap(flush_tlb_user, vmi_flush_tlb_user, _flush_tlb, FlushTLB);
797 para_wrap(flush_tlb_kernel, vmi_flush_tlb_kernel, _flush_tlb, FlushTLB);
798 para_fill(flush_tlb_single, InvalPage);
801 * Until a standard flag format can be agreed on, we need to
802 * implement these as wrappers in Linux. Get the VMI ROM
803 * function pointers for the two backend calls.
805 #ifdef CONFIG_X86_PAE
806 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxELong);
807 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxELong);
809 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxE);
810 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxE);
813 if (vmi_ops.set_pte) {
814 paravirt_ops.set_pte = vmi_set_pte;
815 paravirt_ops.set_pte_at = vmi_set_pte_at;
816 paravirt_ops.set_pmd = vmi_set_pmd;
817 #ifdef CONFIG_X86_PAE
818 paravirt_ops.set_pte_atomic = vmi_set_pte_atomic;
819 paravirt_ops.set_pte_present = vmi_set_pte_present;
820 paravirt_ops.set_pud = vmi_set_pud;
821 paravirt_ops.pte_clear = vmi_pte_clear;
822 paravirt_ops.pmd_clear = vmi_pmd_clear;
826 if (vmi_ops.update_pte) {
827 paravirt_ops.pte_update = vmi_update_pte;
828 paravirt_ops.pte_update_defer = vmi_update_pte_defer;
831 vmi_ops.allocate_page = vmi_get_function(VMI_CALL_AllocatePage);
832 if (vmi_ops.allocate_page) {
833 paravirt_ops.alloc_pt = vmi_allocate_pt;
834 paravirt_ops.alloc_pd = vmi_allocate_pd;
835 paravirt_ops.alloc_pd_clone = vmi_allocate_pd_clone;
838 vmi_ops.release_page = vmi_get_function(VMI_CALL_ReleasePage);
839 if (vmi_ops.release_page) {
840 paravirt_ops.release_pt = vmi_release_pt;
841 paravirt_ops.release_pd = vmi_release_pd;
844 /* Set linear is needed in all cases */
845 vmi_ops.set_linear_mapping = vmi_get_function(VMI_CALL_SetLinearMapping);
846 #ifdef CONFIG_HIGHPTE
847 if (vmi_ops.set_linear_mapping)
848 paravirt_ops.kmap_atomic_pte = vmi_kmap_atomic_pte;
852 * These MUST always be patched. Don't support indirect jumps
853 * through these operations, as the VMI interface may use either
854 * a jump or a call to get to these operations, depending on
855 * the backend. They are performance critical anyway, so requiring
856 * a patch is not a big problem.
858 paravirt_ops.irq_enable_sysexit = (void *)0xfeedbab0;
859 paravirt_ops.iret = (void *)0xbadbab0;
862 para_wrap(startup_ipi_hook, vmi_startup_ipi_hook, set_initial_ap_state, SetInitialAPState);
865 #ifdef CONFIG_X86_LOCAL_APIC
866 para_fill(apic_read, APICRead);
867 para_fill(apic_write, APICWrite);
868 para_fill(apic_write_atomic, APICWrite);
872 * Check for VMI timer functionality by probing for a cycle frequency method
874 reloc = call_vrom_long_func(vmi_rom, get_reloc, VMI_CALL_GetCycleFrequency);
875 if (!disable_vmi_timer && rel->type != VMI_RELOCATION_NONE) {
876 vmi_timer_ops.get_cycle_frequency = (void *)rel->eip;
877 vmi_timer_ops.get_cycle_counter =
878 vmi_get_function(VMI_CALL_GetCycleCounter);
879 vmi_timer_ops.get_wallclock =
880 vmi_get_function(VMI_CALL_GetWallclockTime);
881 vmi_timer_ops.wallclock_updated =
882 vmi_get_function(VMI_CALL_WallclockUpdated);
883 vmi_timer_ops.set_alarm = vmi_get_function(VMI_CALL_SetAlarm);
884 vmi_timer_ops.cancel_alarm =
885 vmi_get_function(VMI_CALL_CancelAlarm);
886 paravirt_ops.time_init = vmi_time_init;
887 paravirt_ops.get_wallclock = vmi_get_wallclock;
888 paravirt_ops.set_wallclock = vmi_set_wallclock;
889 #ifdef CONFIG_X86_LOCAL_APIC
890 paravirt_ops.setup_boot_clock = vmi_time_bsp_init;
891 paravirt_ops.setup_secondary_clock = vmi_time_ap_init;
893 paravirt_ops.get_scheduled_cycles = vmi_get_sched_cycles;
894 paravirt_ops.get_cpu_khz = vmi_cpu_khz;
896 /* We have true wallclock functions; disable CMOS clock sync */
897 no_sync_cmos_clock = 1;
900 disable_vmi_timer = 1;
903 para_fill(safe_halt, Halt);
906 * Alternative instruction rewriting doesn't happen soon enough
907 * to convert VMI_IRET to a call instead of a jump; so we have
908 * to do this before IRQs get reenabled. Fortunately, it is
911 apply_paravirt(__parainstructions, __parainstructions_end);
920 void __init vmi_init(void)
927 check_vmi_rom(vmi_rom);
929 /* In case probing for or validating the ROM failed, basil */
933 reserve_top_address(-vmi_rom->virtual_top);
935 local_irq_save(flags);
938 #ifdef CONFIG_X86_IO_APIC
939 /* This is virtual hardware; timer routing is wired correctly */
942 local_irq_restore(flags & X86_EFLAGS_IF);
945 static int __init parse_vmi(char *arg)
950 if (!strcmp(arg, "disable_pge")) {
951 clear_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
953 } else if (!strcmp(arg, "disable_pse")) {
954 clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
956 } else if (!strcmp(arg, "disable_sep")) {
957 clear_bit(X86_FEATURE_SEP, boot_cpu_data.x86_capability);
959 } else if (!strcmp(arg, "disable_tsc")) {
960 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
962 } else if (!strcmp(arg, "disable_mtrr")) {
963 clear_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability);
965 } else if (!strcmp(arg, "disable_timer")) {
966 disable_vmi_timer = 1;
968 } else if (!strcmp(arg, "disable_noidle"))
973 early_param("vmi", parse_vmi);