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>
31 #include <asm/fixmap.h>
32 #include <asm/apicdef.h>
34 #include <asm/processor.h>
35 #include <asm/timer.h>
36 #include <asm/vmi_time.h>
37 #include <asm/kmap_types.h>
39 /* Convenient for calling VMI functions indirectly in the ROM */
40 typedef u32 __attribute__((regparm(1))) (VROMFUNC)(void);
41 typedef u64 __attribute__((regparm(2))) (VROMLONGFUNC)(int);
43 #define call_vrom_func(rom,func) \
44 (((VROMFUNC *)(rom->func))())
46 #define call_vrom_long_func(rom,func,arg) \
47 (((VROMLONGFUNC *)(rom->func)) (arg))
49 static struct vrom_header *vmi_rom;
50 static int disable_pge;
51 static int disable_pse;
52 static int disable_sep;
53 static int disable_tsc;
54 static int disable_mtrr;
55 static int disable_noidle;
56 static int disable_vmi_timer;
58 /* Cached VMI operations */
60 void (*cpuid)(void /* non-c */);
61 void (*_set_ldt)(u32 selector);
62 void (*set_tr)(u32 selector);
63 void (*set_kernel_stack)(u32 selector, u32 esp0);
64 void (*allocate_page)(u32, u32, u32, u32, u32);
65 void (*release_page)(u32, u32);
66 void (*set_pte)(pte_t, pte_t *, unsigned);
67 void (*update_pte)(pte_t *, unsigned);
68 void (*set_linear_mapping)(int, u32, u32, u32);
69 void (*flush_tlb)(int);
70 void (*set_initial_ap_state)(int, int);
72 void (*set_lazy_mode)(int mode);
75 /* XXX move this to alternative.h */
76 extern struct paravirt_patch __start_parainstructions[],
77 __stop_parainstructions[];
80 * VMI patching routines.
82 #define MNEM_CALL 0xe8
86 static char irq_save_disable_callout[] = {
87 MNEM_CALL, 0, 0, 0, 0,
88 MNEM_CALL, 0, 0, 0, 0,
91 #define IRQ_PATCH_INT_MASK 0
92 #define IRQ_PATCH_DISABLE 5
94 static inline void patch_offset(unsigned char *eip, unsigned char *dest)
96 *(unsigned long *)(eip+1) = dest-eip-5;
99 static unsigned patch_internal(int call, unsigned len, void *insns)
102 struct vmi_relocation_info *const rel = (struct vmi_relocation_info *)&reloc;
103 reloc = call_vrom_long_func(vmi_rom, get_reloc, call);
105 case VMI_RELOCATION_CALL_REL:
107 *(char *)insns = MNEM_CALL;
108 patch_offset(insns, rel->eip);
111 case VMI_RELOCATION_JUMP_REL:
113 *(char *)insns = MNEM_JMP;
114 patch_offset(insns, rel->eip);
117 case VMI_RELOCATION_NOP:
118 /* obliterate the whole thing */
121 case VMI_RELOCATION_NONE:
122 /* leave native code in place */
132 * Apply patch if appropriate, return length of new instruction
133 * sequence. The callee does nop padding for us.
135 static unsigned vmi_patch(u8 type, u16 clobbers, void *insns, unsigned len)
138 case PARAVIRT_IRQ_DISABLE:
139 return patch_internal(VMI_CALL_DisableInterrupts, len, insns);
140 case PARAVIRT_IRQ_ENABLE:
141 return patch_internal(VMI_CALL_EnableInterrupts, len, insns);
142 case PARAVIRT_RESTORE_FLAGS:
143 return patch_internal(VMI_CALL_SetInterruptMask, len, insns);
144 case PARAVIRT_SAVE_FLAGS:
145 return patch_internal(VMI_CALL_GetInterruptMask, len, insns);
146 case PARAVIRT_SAVE_FLAGS_IRQ_DISABLE:
148 patch_internal(VMI_CALL_GetInterruptMask, len, insns);
149 patch_internal(VMI_CALL_DisableInterrupts, len-5, insns+5);
153 * You bastards didn't leave enough room to
154 * patch save_flags_irq_disable inline. Patch
158 *(char *)insns = MNEM_CALL;
159 patch_offset(insns, irq_save_disable_callout);
162 case PARAVIRT_INTERRUPT_RETURN:
163 return patch_internal(VMI_CALL_IRET, len, insns);
164 case PARAVIRT_STI_SYSEXIT:
165 return patch_internal(VMI_CALL_SYSEXIT, len, insns);
172 /* CPUID has non-C semantics, and paravirt-ops API doesn't match hardware ISA */
173 static void vmi_cpuid(unsigned int *eax, unsigned int *ebx,
174 unsigned int *ecx, unsigned int *edx)
179 asm volatile ("call *%6"
184 : "0" (*eax), "2" (*ecx), "r" (vmi_ops.cpuid));
187 *edx &= ~X86_FEATURE_PSE;
189 *edx &= ~X86_FEATURE_PGE;
191 *edx &= ~X86_FEATURE_SEP;
193 *edx &= ~X86_FEATURE_TSC;
195 *edx &= ~X86_FEATURE_MTRR;
199 static inline void vmi_maybe_load_tls(struct desc_struct *gdt, int nr, struct desc_struct *new)
201 if (gdt[nr].a != new->a || gdt[nr].b != new->b)
202 write_gdt_entry(gdt, nr, new->a, new->b);
205 static void vmi_load_tls(struct thread_struct *t, unsigned int cpu)
207 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
208 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 0, &t->tls_array[0]);
209 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 1, &t->tls_array[1]);
210 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 2, &t->tls_array[2]);
213 static void vmi_set_ldt(const void *addr, unsigned entries)
215 unsigned cpu = smp_processor_id();
218 pack_descriptor(&low, &high, (unsigned long)addr,
219 entries * sizeof(struct desc_struct) - 1,
221 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, low, high);
222 vmi_ops._set_ldt(entries ? GDT_ENTRY_LDT*sizeof(struct desc_struct) : 0);
225 static void vmi_set_tr(void)
227 vmi_ops.set_tr(GDT_ENTRY_TSS*sizeof(struct desc_struct));
230 static void vmi_load_esp0(struct tss_struct *tss,
231 struct thread_struct *thread)
233 tss->esp0 = thread->esp0;
235 /* This can only happen when SEP is enabled, no need to test "SEP"arately */
236 if (unlikely(tss->ss1 != thread->sysenter_cs)) {
237 tss->ss1 = thread->sysenter_cs;
238 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
240 vmi_ops.set_kernel_stack(__KERNEL_DS, tss->esp0);
243 static void vmi_flush_tlb_user(void)
245 vmi_ops.flush_tlb(VMI_FLUSH_TLB);
248 static void vmi_flush_tlb_kernel(void)
250 vmi_ops.flush_tlb(VMI_FLUSH_TLB | VMI_FLUSH_GLOBAL);
253 /* Stub to do nothing at all; used for delays and unimplemented calls */
254 static void vmi_nop(void)
258 /* For NO_IDLE_HZ, we stop the clock when halting the kernel */
259 static fastcall void vmi_safe_halt(void)
261 int idle = vmi_stop_hz_timer();
265 vmi_account_time_restart_hz_timer();
270 #ifdef CONFIG_DEBUG_PAGE_TYPE
272 #ifdef CONFIG_X86_PAE
273 #define MAX_BOOT_PTS (2048+4+1)
275 #define MAX_BOOT_PTS (1024+1)
279 * During boot, mem_map is not yet available in paging_init, so stash
280 * all the boot page allocations here.
285 } boot_page_allocations[MAX_BOOT_PTS];
286 static int num_boot_page_allocations;
287 static int boot_allocations_applied;
289 void vmi_apply_boot_page_allocations(void)
293 for (i = 0; i < num_boot_page_allocations; i++) {
294 struct page *page = pfn_to_page(boot_page_allocations[i].pfn);
295 page->type = boot_page_allocations[i].type;
296 page->type = boot_page_allocations[i].type &
297 ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
299 boot_allocations_applied = 1;
302 static void record_page_type(u32 pfn, int type)
304 BUG_ON(num_boot_page_allocations >= MAX_BOOT_PTS);
305 boot_page_allocations[num_boot_page_allocations].pfn = pfn;
306 boot_page_allocations[num_boot_page_allocations].type = type;
307 num_boot_page_allocations++;
310 static void check_zeroed_page(u32 pfn, int type, struct page *page)
314 int limit = PAGE_SIZE / sizeof(int);
316 if (page_address(page))
317 ptr = (u32 *)page_address(page);
319 ptr = (u32 *)__va(pfn << PAGE_SHIFT);
321 * When cloning the root in non-PAE mode, only the userspace
322 * pdes need to be zeroed.
324 if (type & VMI_PAGE_CLONE)
325 limit = USER_PTRS_PER_PGD;
326 for (i = 0; i < limit; i++)
331 * We stash the page type into struct page so we can verify the page
332 * types are used properly.
334 static void vmi_set_page_type(u32 pfn, int type)
336 /* PAE can have multiple roots per page - don't track */
337 if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
340 if (boot_allocations_applied) {
341 struct page *page = pfn_to_page(pfn);
342 if (type != VMI_PAGE_NORMAL)
345 BUG_ON(page->type == VMI_PAGE_NORMAL);
346 page->type = type & ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
347 if (type & VMI_PAGE_ZEROED)
348 check_zeroed_page(pfn, type, page);
350 record_page_type(pfn, type);
354 static void vmi_check_page_type(u32 pfn, int type)
356 /* PAE can have multiple roots per page - skip checks */
357 if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
360 type &= ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
361 if (boot_allocations_applied) {
362 struct page *page = pfn_to_page(pfn);
363 BUG_ON((page->type ^ type) & VMI_PAGE_PAE);
364 BUG_ON(type == VMI_PAGE_NORMAL && page->type);
365 BUG_ON((type & page->type) == 0);
369 #define vmi_set_page_type(p,t) do { } while (0)
370 #define vmi_check_page_type(p,t) do { } while (0)
373 static void vmi_map_pt_hook(int type, pte_t *va, u32 pfn)
376 * Internally, the VMI ROM must map virtual addresses to physical
377 * addresses for processing MMU updates. By the time MMU updates
378 * are issued, this information is typically already lost.
379 * Fortunately, the VMI provides a cache of mapping slots for active
382 * We use slot zero for the linear mapping of physical memory, and
383 * in HIGHPTE kernels, slot 1 and 2 for KM_PTE0 and KM_PTE1.
385 * args: SLOT VA COUNT PFN
387 BUG_ON(type != KM_PTE0 && type != KM_PTE1);
388 vmi_ops.set_linear_mapping((type - KM_PTE0)+1, (u32)va, 1, pfn);
391 static void vmi_allocate_pt(u32 pfn)
393 vmi_set_page_type(pfn, VMI_PAGE_L1);
394 vmi_ops.allocate_page(pfn, VMI_PAGE_L1, 0, 0, 0);
397 static void vmi_allocate_pd(u32 pfn)
400 * This call comes in very early, before mem_map is setup.
401 * It is called only for swapper_pg_dir, which already has
404 vmi_set_page_type(pfn, VMI_PAGE_L2);
405 vmi_ops.allocate_page(pfn, VMI_PAGE_L2, 0, 0, 0);
408 static void vmi_allocate_pd_clone(u32 pfn, u32 clonepfn, u32 start, u32 count)
410 vmi_set_page_type(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE);
411 vmi_check_page_type(clonepfn, VMI_PAGE_L2);
412 vmi_ops.allocate_page(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE, clonepfn, start, count);
415 static void vmi_release_pt(u32 pfn)
417 vmi_ops.release_page(pfn, VMI_PAGE_L1);
418 vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
421 static void vmi_release_pd(u32 pfn)
423 vmi_ops.release_page(pfn, VMI_PAGE_L2);
424 vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
428 * Helper macros for MMU update flags. We can defer updates until a flush
429 * or page invalidation only if the update is to the current address space
430 * (otherwise, there is no flush). We must check against init_mm, since
431 * this could be a kernel update, which usually passes init_mm, although
432 * sometimes this check can be skipped if we know the particular function
433 * is only called on user mode PTEs. We could change the kernel to pass
434 * current->active_mm here, but in particular, I was unsure if changing
435 * mm/highmem.c to do this would still be correct on other architectures.
437 #define is_current_as(mm, mustbeuser) ((mm) == current->active_mm || \
438 (!mustbeuser && (mm) == &init_mm))
439 #define vmi_flags_addr(mm, addr, level, user) \
440 ((level) | (is_current_as(mm, user) ? \
441 (VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
442 #define vmi_flags_addr_defer(mm, addr, level, user) \
443 ((level) | (is_current_as(mm, user) ? \
444 (VMI_PAGE_DEFER | VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
446 static void vmi_update_pte(struct mm_struct *mm, u32 addr, pte_t *ptep)
448 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
449 vmi_ops.update_pte(ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
452 static void vmi_update_pte_defer(struct mm_struct *mm, u32 addr, pte_t *ptep)
454 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
455 vmi_ops.update_pte(ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 0));
458 static void vmi_set_pte(pte_t *ptep, pte_t pte)
460 /* XXX because of set_pmd_pte, this can be called on PT or PD layers */
461 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE | VMI_PAGE_PD);
462 vmi_ops.set_pte(pte, ptep, VMI_PAGE_PT);
465 static void vmi_set_pte_at(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pte)
467 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
468 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
471 static void vmi_set_pmd(pmd_t *pmdp, pmd_t pmdval)
473 #ifdef CONFIG_X86_PAE
474 const pte_t pte = { pmdval.pmd, pmdval.pmd >> 32 };
475 vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PMD);
477 const pte_t pte = { pmdval.pud.pgd.pgd };
478 vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PGD);
480 vmi_ops.set_pte(pte, (pte_t *)pmdp, VMI_PAGE_PD);
483 #ifdef CONFIG_X86_PAE
485 static void vmi_set_pte_atomic(pte_t *ptep, pte_t pteval)
488 * XXX This is called from set_pmd_pte, but at both PT
489 * and PD layers so the VMI_PAGE_PT flag is wrong. But
490 * it is only called for large page mapping changes,
491 * the Xen backend, doesn't support large pages, and the
492 * ESX backend doesn't depend on the flag.
494 set_64bit((unsigned long long *)ptep,pte_val(pteval));
495 vmi_ops.update_pte(ptep, VMI_PAGE_PT);
498 static void vmi_set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
500 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
501 vmi_ops.set_pte(pte, ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 1));
504 static void vmi_set_pud(pud_t *pudp, pud_t pudval)
507 const pte_t pte = { pudval.pgd.pgd, pudval.pgd.pgd >> 32 };
508 vmi_check_page_type(__pa(pudp) >> PAGE_SHIFT, VMI_PAGE_PGD);
509 vmi_ops.set_pte(pte, (pte_t *)pudp, VMI_PAGE_PDP);
512 static void vmi_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
514 const pte_t pte = { 0 };
515 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
516 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
519 void vmi_pmd_clear(pmd_t *pmd)
521 const pte_t pte = { 0 };
522 vmi_check_page_type(__pa(pmd) >> PAGE_SHIFT, VMI_PAGE_PMD);
523 vmi_ops.set_pte(pte, (pte_t *)pmd, VMI_PAGE_PD);
528 extern void setup_pda(void);
530 static void __devinit
531 vmi_startup_ipi_hook(int phys_apicid, unsigned long start_eip,
532 unsigned long start_esp)
534 struct vmi_ap_state ap;
536 /* Default everything to zero. This is fine for most GPRs. */
537 memset(&ap, 0, sizeof(struct vmi_ap_state));
539 ap.gdtr_limit = GDT_SIZE - 1;
540 ap.gdtr_base = (unsigned long) get_cpu_gdt_table(phys_apicid);
542 ap.idtr_limit = IDT_ENTRIES * 8 - 1;
543 ap.idtr_base = (unsigned long) idt_table;
548 ap.eip = (unsigned long) start_eip;
550 ap.esp = (unsigned long) start_esp;
554 ap.fs = __KERNEL_PDA;
561 #ifdef CONFIG_X86_PAE
562 /* efer should match BSP efer. */
565 rdmsr(MSR_EFER, l, h);
566 ap.efer = (unsigned long long) h << 32 | l;
570 ap.cr3 = __pa(swapper_pg_dir);
571 /* Protected mode, paging, AM, WP, NE, MP. */
573 ap.cr4 = mmu_cr4_features;
574 vmi_ops.set_initial_ap_state((u32)&ap, phys_apicid);
578 static void vmi_set_lazy_mode(int mode)
580 static DEFINE_PER_CPU(int, lazy_mode);
582 if (!vmi_ops.set_lazy_mode)
585 /* Modes should never nest or overlap */
586 BUG_ON(__get_cpu_var(lazy_mode) && !(mode == PARAVIRT_LAZY_NONE ||
587 mode == PARAVIRT_LAZY_FLUSH));
589 if (mode == PARAVIRT_LAZY_FLUSH) {
590 vmi_ops.set_lazy_mode(0);
591 vmi_ops.set_lazy_mode(__get_cpu_var(lazy_mode));
593 vmi_ops.set_lazy_mode(mode);
594 __get_cpu_var(lazy_mode) = mode;
598 static inline int __init check_vmi_rom(struct vrom_header *rom)
600 struct pci_header *pci;
601 struct pnp_header *pnp;
602 const char *manufacturer = "UNKNOWN";
603 const char *product = "UNKNOWN";
604 const char *license = "unspecified";
606 if (rom->rom_signature != 0xaa55)
608 if (rom->vrom_signature != VMI_SIGNATURE)
610 if (rom->api_version_maj != VMI_API_REV_MAJOR ||
611 rom->api_version_min+1 < VMI_API_REV_MINOR+1) {
612 printk(KERN_WARNING "VMI: Found mismatched rom version %d.%d\n",
613 rom->api_version_maj,
614 rom->api_version_min);
619 * Relying on the VMI_SIGNATURE field is not 100% safe, so check
620 * the PCI header and device type to make sure this is really a
623 if (!rom->pci_header_offs) {
624 printk(KERN_WARNING "VMI: ROM does not contain PCI header.\n");
628 pci = (struct pci_header *)((char *)rom+rom->pci_header_offs);
629 if (pci->vendorID != PCI_VENDOR_ID_VMWARE ||
630 pci->deviceID != PCI_DEVICE_ID_VMWARE_VMI) {
631 /* Allow it to run... anyways, but warn */
632 printk(KERN_WARNING "VMI: ROM from unknown manufacturer\n");
635 if (rom->pnp_header_offs) {
636 pnp = (struct pnp_header *)((char *)rom+rom->pnp_header_offs);
637 if (pnp->manufacturer_offset)
638 manufacturer = (const char *)rom+pnp->manufacturer_offset;
639 if (pnp->product_offset)
640 product = (const char *)rom+pnp->product_offset;
643 if (rom->license_offs)
644 license = (char *)rom+rom->license_offs;
646 printk(KERN_INFO "VMI: Found %s %s, API version %d.%d, ROM version %d.%d\n",
647 manufacturer, product,
648 rom->api_version_maj, rom->api_version_min,
649 pci->rom_version_maj, pci->rom_version_min);
651 /* Don't allow BSD/MIT here for now because we don't want to end up
652 with any binary only shim layers */
653 if (strcmp(license, "GPL") && strcmp(license, "GPL v2")) {
654 printk(KERN_WARNING "VMI: Non GPL license `%s' found for ROM. Not used.\n",
663 * Probe for the VMI option ROM
665 static inline int __init probe_vmi_rom(void)
669 /* VMI ROM is in option ROM area, check signature */
670 for (base = 0xC0000; base < 0xE0000; base += 2048) {
671 struct vrom_header *romstart;
672 romstart = (struct vrom_header *)isa_bus_to_virt(base);
673 if (check_vmi_rom(romstart)) {
682 * VMI setup common to all processors
684 void vmi_bringup(void)
686 /* We must establish the lowmem mapping for MMU ops to work */
687 if (vmi_ops.set_linear_mapping)
688 vmi_ops.set_linear_mapping(0, __PAGE_OFFSET, max_low_pfn, 0);
692 * Return a pointer to a VMI function or NULL if unimplemented
694 static void *vmi_get_function(int vmicall)
697 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
698 reloc = call_vrom_long_func(vmi_rom, get_reloc, vmicall);
699 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);
700 if (rel->type == VMI_RELOCATION_CALL_REL)
701 return (void *)rel->eip;
707 * Helper macro for making the VMI paravirt-ops fill code readable.
708 * For unimplemented operations, fall back to default, unless nop
709 * is returned by the ROM.
711 #define para_fill(opname, vmicall) \
713 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
714 VMI_CALL_##vmicall); \
715 if (rel->type == VMI_RELOCATION_CALL_REL) \
716 paravirt_ops.opname = (void *)rel->eip; \
717 else if (rel->type == VMI_RELOCATION_NOP) \
718 paravirt_ops.opname = (void *)vmi_nop; \
719 else if (rel->type != VMI_RELOCATION_NONE) \
720 printk(KERN_WARNING "VMI: Unknown relocation " \
721 "type %d for " #vmicall"\n",\
726 * Helper macro for making the VMI paravirt-ops fill code readable.
727 * For cached operations which do not match the VMI ROM ABI and must
728 * go through a tranlation stub. Ignore NOPs, since it is not clear
729 * a NOP * VMI function corresponds to a NOP paravirt-op when the
730 * functions are not in 1-1 correspondence.
732 #define para_wrap(opname, wrapper, cache, vmicall) \
734 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
735 VMI_CALL_##vmicall); \
736 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL); \
737 if (rel->type == VMI_RELOCATION_CALL_REL) { \
738 paravirt_ops.opname = wrapper; \
739 vmi_ops.cache = (void *)rel->eip; \
745 * Activate the VMI interface and switch into paravirtualized mode
747 static inline int __init activate_vmi(void)
751 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
753 if (call_vrom_func(vmi_rom, vmi_init) != 0) {
754 printk(KERN_ERR "VMI ROM failed to initialize!");
757 savesegment(cs, kernel_cs);
759 paravirt_ops.paravirt_enabled = 1;
760 paravirt_ops.kernel_rpl = kernel_cs & SEGMENT_RPL_MASK;
762 paravirt_ops.patch = vmi_patch;
763 paravirt_ops.name = "vmi";
766 * Many of these operations are ABI compatible with VMI.
767 * This means we can fill in the paravirt-ops with direct
768 * pointers into the VMI ROM. If the calling convention for
769 * these operations changes, this code needs to be updated.
772 * CPUID paravirt-op uses pointers, not the native ISA
773 * halt has no VMI equivalent; all VMI halts are "safe"
774 * no MSR support yet - just trap and emulate. VMI uses the
775 * same ABI as the native ISA, but Linux wants exceptions
776 * from bogus MSR read / write handled
777 * rdpmc is not yet used in Linux
780 /* CPUID is special, so very special it gets wrapped like a present */
781 para_wrap(cpuid, vmi_cpuid, cpuid, CPUID);
783 para_fill(clts, CLTS);
784 para_fill(get_debugreg, GetDR);
785 para_fill(set_debugreg, SetDR);
786 para_fill(read_cr0, GetCR0);
787 para_fill(read_cr2, GetCR2);
788 para_fill(read_cr3, GetCR3);
789 para_fill(read_cr4, GetCR4);
790 para_fill(write_cr0, SetCR0);
791 para_fill(write_cr2, SetCR2);
792 para_fill(write_cr3, SetCR3);
793 para_fill(write_cr4, SetCR4);
794 para_fill(save_fl, GetInterruptMask);
795 para_fill(restore_fl, SetInterruptMask);
796 para_fill(irq_disable, DisableInterrupts);
797 para_fill(irq_enable, EnableInterrupts);
799 /* irq_save_disable !!! sheer pain */
800 patch_offset(&irq_save_disable_callout[IRQ_PATCH_INT_MASK],
801 (char *)paravirt_ops.save_fl);
802 patch_offset(&irq_save_disable_callout[IRQ_PATCH_DISABLE],
803 (char *)paravirt_ops.irq_disable);
805 para_fill(wbinvd, WBINVD);
806 para_fill(read_tsc, RDTSC);
808 /* The following we emulate with trap and emulate for now */
809 /* paravirt_ops.read_msr = vmi_rdmsr */
810 /* paravirt_ops.write_msr = vmi_wrmsr */
811 /* paravirt_ops.rdpmc = vmi_rdpmc */
813 /* TR interface doesn't pass TR value, wrap */
814 para_wrap(load_tr_desc, vmi_set_tr, set_tr, SetTR);
816 /* LDT is special, too */
817 para_wrap(set_ldt, vmi_set_ldt, _set_ldt, SetLDT);
819 para_fill(load_gdt, SetGDT);
820 para_fill(load_idt, SetIDT);
821 para_fill(store_gdt, GetGDT);
822 para_fill(store_idt, GetIDT);
823 para_fill(store_tr, GetTR);
824 paravirt_ops.load_tls = vmi_load_tls;
825 para_fill(write_ldt_entry, WriteLDTEntry);
826 para_fill(write_gdt_entry, WriteGDTEntry);
827 para_fill(write_idt_entry, WriteIDTEntry);
828 para_wrap(load_esp0, vmi_load_esp0, set_kernel_stack, UpdateKernelStack);
829 para_fill(set_iopl_mask, SetIOPLMask);
830 para_fill(io_delay, IODelay);
831 para_wrap(set_lazy_mode, vmi_set_lazy_mode, set_lazy_mode, SetLazyMode);
833 /* user and kernel flush are just handled with different flags to FlushTLB */
834 para_wrap(flush_tlb_user, vmi_flush_tlb_user, flush_tlb, FlushTLB);
835 para_wrap(flush_tlb_kernel, vmi_flush_tlb_kernel, flush_tlb, FlushTLB);
836 para_fill(flush_tlb_single, InvalPage);
839 * Until a standard flag format can be agreed on, we need to
840 * implement these as wrappers in Linux. Get the VMI ROM
841 * function pointers for the two backend calls.
843 #ifdef CONFIG_X86_PAE
844 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxELong);
845 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxELong);
847 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxE);
848 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxE);
851 if (vmi_ops.set_pte) {
852 paravirt_ops.set_pte = vmi_set_pte;
853 paravirt_ops.set_pte_at = vmi_set_pte_at;
854 paravirt_ops.set_pmd = vmi_set_pmd;
855 #ifdef CONFIG_X86_PAE
856 paravirt_ops.set_pte_atomic = vmi_set_pte_atomic;
857 paravirt_ops.set_pte_present = vmi_set_pte_present;
858 paravirt_ops.set_pud = vmi_set_pud;
859 paravirt_ops.pte_clear = vmi_pte_clear;
860 paravirt_ops.pmd_clear = vmi_pmd_clear;
864 if (vmi_ops.update_pte) {
865 paravirt_ops.pte_update = vmi_update_pte;
866 paravirt_ops.pte_update_defer = vmi_update_pte_defer;
869 vmi_ops.allocate_page = vmi_get_function(VMI_CALL_AllocatePage);
870 if (vmi_ops.allocate_page) {
871 paravirt_ops.alloc_pt = vmi_allocate_pt;
872 paravirt_ops.alloc_pd = vmi_allocate_pd;
873 paravirt_ops.alloc_pd_clone = vmi_allocate_pd_clone;
876 vmi_ops.release_page = vmi_get_function(VMI_CALL_ReleasePage);
877 if (vmi_ops.release_page) {
878 paravirt_ops.release_pt = vmi_release_pt;
879 paravirt_ops.release_pd = vmi_release_pd;
881 para_wrap(map_pt_hook, vmi_map_pt_hook, set_linear_mapping,
885 * These MUST always be patched. Don't support indirect jumps
886 * through these operations, as the VMI interface may use either
887 * a jump or a call to get to these operations, depending on
888 * the backend. They are performance critical anyway, so requiring
889 * a patch is not a big problem.
891 paravirt_ops.irq_enable_sysexit = (void *)0xfeedbab0;
892 paravirt_ops.iret = (void *)0xbadbab0;
895 para_wrap(startup_ipi_hook, vmi_startup_ipi_hook, set_initial_ap_state, SetInitialAPState);
898 #ifdef CONFIG_X86_LOCAL_APIC
899 para_fill(apic_read, APICRead);
900 para_fill(apic_write, APICWrite);
901 para_fill(apic_write_atomic, APICWrite);
905 * Check for VMI timer functionality by probing for a cycle frequency method
907 reloc = call_vrom_long_func(vmi_rom, get_reloc, VMI_CALL_GetCycleFrequency);
908 if (!disable_vmi_timer && rel->type != VMI_RELOCATION_NONE) {
909 vmi_timer_ops.get_cycle_frequency = (void *)rel->eip;
910 vmi_timer_ops.get_cycle_counter =
911 vmi_get_function(VMI_CALL_GetCycleCounter);
912 vmi_timer_ops.get_wallclock =
913 vmi_get_function(VMI_CALL_GetWallclockTime);
914 vmi_timer_ops.wallclock_updated =
915 vmi_get_function(VMI_CALL_WallclockUpdated);
916 vmi_timer_ops.set_alarm = vmi_get_function(VMI_CALL_SetAlarm);
917 vmi_timer_ops.cancel_alarm =
918 vmi_get_function(VMI_CALL_CancelAlarm);
919 paravirt_ops.time_init = vmi_time_init;
920 paravirt_ops.get_wallclock = vmi_get_wallclock;
921 paravirt_ops.set_wallclock = vmi_set_wallclock;
922 #ifdef CONFIG_X86_LOCAL_APIC
923 paravirt_ops.setup_boot_clock = vmi_timer_setup_boot_alarm;
924 paravirt_ops.setup_secondary_clock = vmi_timer_setup_secondary_alarm;
926 paravirt_ops.get_scheduled_cycles = vmi_get_sched_cycles;
927 paravirt_ops.get_cpu_khz = vmi_cpu_khz;
929 /* We have true wallclock functions; disable CMOS clock sync */
930 no_sync_cmos_clock = 1;
933 disable_vmi_timer = 1;
936 /* No idle HZ mode only works if VMI timer and no idle is enabled */
937 if (disable_noidle || disable_vmi_timer)
938 para_fill(safe_halt, Halt);
940 para_wrap(safe_halt, vmi_safe_halt, halt, Halt);
943 * Alternative instruction rewriting doesn't happen soon enough
944 * to convert VMI_IRET to a call instead of a jump; so we have
945 * to do this before IRQs get reenabled. Fortunately, it is
948 apply_paravirt(__start_parainstructions, __stop_parainstructions);
957 void __init vmi_init(void)
964 check_vmi_rom(vmi_rom);
966 /* In case probing for or validating the ROM failed, basil */
970 reserve_top_address(-vmi_rom->virtual_top);
972 local_irq_save(flags);
975 #ifdef CONFIG_X86_IO_APIC
976 /* This is virtual hardware; timer routing is wired correctly */
979 local_irq_restore(flags & X86_EFLAGS_IF);
982 static int __init parse_vmi(char *arg)
987 if (!strcmp(arg, "disable_pge")) {
988 clear_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
990 } else if (!strcmp(arg, "disable_pse")) {
991 clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
993 } else if (!strcmp(arg, "disable_sep")) {
994 clear_bit(X86_FEATURE_SEP, boot_cpu_data.x86_capability);
996 } else if (!strcmp(arg, "disable_tsc")) {
997 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
999 } else if (!strcmp(arg, "disable_mtrr")) {
1000 clear_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability);
1002 } else if (!strcmp(arg, "disable_timer")) {
1003 disable_vmi_timer = 1;
1005 } else if (!strcmp(arg, "disable_noidle"))
1010 early_param("vmi", parse_vmi);