]> err.no Git - linux-2.6/blob - arch/i386/kernel/vmi.c
0df0b2cd361729835cc5dc146b11e5041d3a2595
[linux-2.6] / arch / i386 / kernel / vmi.c
1 /*
2  * VMI specific paravirt-ops implementation
3  *
4  * Copyright (C) 2005, VMware, Inc.
5  *
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.
10  *
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
15  * details.
16  *
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.
20  *
21  * Send feedback to zach@vmware.com
22  *
23  */
24
25 #include <linux/module.h>
26 #include <linux/cpu.h>
27 #include <linux/bootmem.h>
28 #include <linux/mm.h>
29 #include <linux/highmem.h>
30 #include <asm/vmi.h>
31 #include <asm/io.h>
32 #include <asm/fixmap.h>
33 #include <asm/apicdef.h>
34 #include <asm/apic.h>
35 #include <asm/processor.h>
36 #include <asm/timer.h>
37 #include <asm/vmi_time.h>
38 #include <asm/kmap_types.h>
39
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);
43
44 #define call_vrom_func(rom,func) \
45    (((VROMFUNC *)(rom->func))())
46
47 #define call_vrom_long_func(rom,func,arg) \
48    (((VROMLONGFUNC *)(rom->func)) (arg))
49
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;
58
59 /* Cached VMI operations */
60 static struct {
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);
72         void (*halt)(void);
73         void (*set_lazy_mode)(int mode);
74 } vmi_ops;
75
76 /* XXX move this to alternative.h */
77 extern struct paravirt_patch __start_parainstructions[],
78         __stop_parainstructions[];
79
80 /*
81  * VMI patching routines.
82  */
83 #define MNEM_CALL 0xe8
84 #define MNEM_JMP  0xe9
85 #define MNEM_RET  0xc3
86
87 #define IRQ_PATCH_INT_MASK 0
88 #define IRQ_PATCH_DISABLE  5
89
90 static inline void patch_offset(unsigned char *eip, unsigned char *dest)
91 {
92         *(unsigned long *)(eip+1) = dest-eip-5;
93 }
94
95 static unsigned patch_internal(int call, unsigned len, void *insns)
96 {
97         u64 reloc;
98         struct vmi_relocation_info *const rel = (struct vmi_relocation_info *)&reloc;
99         reloc = call_vrom_long_func(vmi_rom, get_reloc, call);
100         switch(rel->type) {
101                 case VMI_RELOCATION_CALL_REL:
102                         BUG_ON(len < 5);
103                         *(char *)insns = MNEM_CALL;
104                         patch_offset(insns, rel->eip);
105                         return 5;
106
107                 case VMI_RELOCATION_JUMP_REL:
108                         BUG_ON(len < 5);
109                         *(char *)insns = MNEM_JMP;
110                         patch_offset(insns, rel->eip);
111                         return 5;
112
113                 case VMI_RELOCATION_NOP:
114                         /* obliterate the whole thing */
115                         return 0;
116
117                 case VMI_RELOCATION_NONE:
118                         /* leave native code in place */
119                         break;
120
121                 default:
122                         BUG();
123         }
124         return len;
125 }
126
127 /*
128  * Apply patch if appropriate, return length of new instruction
129  * sequence.  The callee does nop padding for us.
130  */
131 static unsigned vmi_patch(u8 type, u16 clobbers, void *insns, unsigned len)
132 {
133         switch (type) {
134                 case PARAVIRT_PATCH(irq_disable):
135                         return patch_internal(VMI_CALL_DisableInterrupts, len, insns);
136                 case PARAVIRT_PATCH(irq_enable):
137                         return patch_internal(VMI_CALL_EnableInterrupts, len, insns);
138                 case PARAVIRT_PATCH(restore_fl):
139                         return patch_internal(VMI_CALL_SetInterruptMask, len, insns);
140                 case PARAVIRT_PATCH(save_fl):
141                         return patch_internal(VMI_CALL_GetInterruptMask, len, insns);
142                 case PARAVIRT_PATCH(iret):
143                         return patch_internal(VMI_CALL_IRET, len, insns);
144                 case PARAVIRT_PATCH(irq_enable_sysexit):
145                         return patch_internal(VMI_CALL_SYSEXIT, len, insns);
146                 default:
147                         break;
148         }
149         return len;
150 }
151
152 /* CPUID has non-C semantics, and paravirt-ops API doesn't match hardware ISA */
153 static void vmi_cpuid(unsigned int *eax, unsigned int *ebx,
154                                unsigned int *ecx, unsigned int *edx)
155 {
156         int override = 0;
157         if (*eax == 1)
158                 override = 1;
159         asm volatile ("call *%6"
160                       : "=a" (*eax),
161                         "=b" (*ebx),
162                         "=c" (*ecx),
163                         "=d" (*edx)
164                       : "0" (*eax), "2" (*ecx), "r" (vmi_ops.cpuid));
165         if (override) {
166                 if (disable_pse)
167                         *edx &= ~X86_FEATURE_PSE;
168                 if (disable_pge)
169                         *edx &= ~X86_FEATURE_PGE;
170                 if (disable_sep)
171                         *edx &= ~X86_FEATURE_SEP;
172                 if (disable_tsc)
173                         *edx &= ~X86_FEATURE_TSC;
174                 if (disable_mtrr)
175                         *edx &= ~X86_FEATURE_MTRR;
176         }
177 }
178
179 static inline void vmi_maybe_load_tls(struct desc_struct *gdt, int nr, struct desc_struct *new)
180 {
181         if (gdt[nr].a != new->a || gdt[nr].b != new->b)
182                 write_gdt_entry(gdt, nr, new->a, new->b);
183 }
184
185 static void vmi_load_tls(struct thread_struct *t, unsigned int cpu)
186 {
187         struct desc_struct *gdt = get_cpu_gdt_table(cpu);
188         vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 0, &t->tls_array[0]);
189         vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 1, &t->tls_array[1]);
190         vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 2, &t->tls_array[2]);
191 }
192
193 static void vmi_set_ldt(const void *addr, unsigned entries)
194 {
195         unsigned cpu = smp_processor_id();
196         u32 low, high;
197
198         pack_descriptor(&low, &high, (unsigned long)addr,
199                         entries * sizeof(struct desc_struct) - 1,
200                         DESCTYPE_LDT, 0);
201         write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, low, high);
202         vmi_ops._set_ldt(entries ? GDT_ENTRY_LDT*sizeof(struct desc_struct) : 0);
203 }
204
205 static void vmi_set_tr(void)
206 {
207         vmi_ops.set_tr(GDT_ENTRY_TSS*sizeof(struct desc_struct));
208 }
209
210 static void vmi_load_esp0(struct tss_struct *tss,
211                                    struct thread_struct *thread)
212 {
213         tss->x86_tss.esp0 = thread->esp0;
214
215         /* This can only happen when SEP is enabled, no need to test "SEP"arately */
216         if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
217                 tss->x86_tss.ss1 = thread->sysenter_cs;
218                 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
219         }
220         vmi_ops.set_kernel_stack(__KERNEL_DS, tss->x86_tss.esp0);
221 }
222
223 static void vmi_flush_tlb_user(void)
224 {
225         vmi_ops._flush_tlb(VMI_FLUSH_TLB);
226 }
227
228 static void vmi_flush_tlb_kernel(void)
229 {
230         vmi_ops._flush_tlb(VMI_FLUSH_TLB | VMI_FLUSH_GLOBAL);
231 }
232
233 /* Stub to do nothing at all; used for delays and unimplemented calls */
234 static void vmi_nop(void)
235 {
236 }
237
238 /* For NO_IDLE_HZ, we stop the clock when halting the kernel */
239 static fastcall void vmi_safe_halt(void)
240 {
241         int idle = vmi_stop_hz_timer();
242         vmi_ops.halt();
243         if (idle) {
244                 local_irq_disable();
245                 vmi_account_time_restart_hz_timer();
246                 local_irq_enable();
247         }
248 }
249
250 #ifdef CONFIG_DEBUG_PAGE_TYPE
251
252 #ifdef CONFIG_X86_PAE
253 #define MAX_BOOT_PTS (2048+4+1)
254 #else
255 #define MAX_BOOT_PTS (1024+1)
256 #endif
257
258 /*
259  * During boot, mem_map is not yet available in paging_init, so stash
260  * all the boot page allocations here.
261  */
262 static struct {
263         u32 pfn;
264         int type;
265 } boot_page_allocations[MAX_BOOT_PTS];
266 static int num_boot_page_allocations;
267 static int boot_allocations_applied;
268
269 void vmi_apply_boot_page_allocations(void)
270 {
271         int i;
272         BUG_ON(!mem_map);
273         for (i = 0; i < num_boot_page_allocations; i++) {
274                 struct page *page = pfn_to_page(boot_page_allocations[i].pfn);
275                 page->type = boot_page_allocations[i].type;
276                 page->type = boot_page_allocations[i].type &
277                                 ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
278         }
279         boot_allocations_applied = 1;
280 }
281
282 static void record_page_type(u32 pfn, int type)
283 {
284         BUG_ON(num_boot_page_allocations >= MAX_BOOT_PTS);
285         boot_page_allocations[num_boot_page_allocations].pfn = pfn;
286         boot_page_allocations[num_boot_page_allocations].type = type;
287         num_boot_page_allocations++;
288 }
289
290 static void check_zeroed_page(u32 pfn, int type, struct page *page)
291 {
292         u32 *ptr;
293         int i;
294         int limit = PAGE_SIZE / sizeof(int);
295
296         if (page_address(page))
297                 ptr = (u32 *)page_address(page);
298         else
299                 ptr = (u32 *)__va(pfn << PAGE_SHIFT);
300         /*
301          * When cloning the root in non-PAE mode, only the userspace
302          * pdes need to be zeroed.
303          */
304         if (type & VMI_PAGE_CLONE)
305                 limit = USER_PTRS_PER_PGD;
306         for (i = 0; i < limit; i++)
307                 BUG_ON(ptr[i]);
308 }
309
310 /*
311  * We stash the page type into struct page so we can verify the page
312  * types are used properly.
313  */
314 static void vmi_set_page_type(u32 pfn, int type)
315 {
316         /* PAE can have multiple roots per page - don't track */
317         if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
318                 return;
319
320         if (boot_allocations_applied) {
321                 struct page *page = pfn_to_page(pfn);
322                 if (type != VMI_PAGE_NORMAL)
323                         BUG_ON(page->type);
324                 else
325                         BUG_ON(page->type == VMI_PAGE_NORMAL);
326                 page->type = type & ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
327                 if (type & VMI_PAGE_ZEROED)
328                         check_zeroed_page(pfn, type, page);
329         } else {
330                 record_page_type(pfn, type);
331         }
332 }
333
334 static void vmi_check_page_type(u32 pfn, int type)
335 {
336         /* PAE can have multiple roots per page - skip checks */
337         if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
338                 return;
339
340         type &= ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
341         if (boot_allocations_applied) {
342                 struct page *page = pfn_to_page(pfn);
343                 BUG_ON((page->type ^ type) & VMI_PAGE_PAE);
344                 BUG_ON(type == VMI_PAGE_NORMAL && page->type);
345                 BUG_ON((type & page->type) == 0);
346         }
347 }
348 #else
349 #define vmi_set_page_type(p,t) do { } while (0)
350 #define vmi_check_page_type(p,t) do { } while (0)
351 #endif
352
353 #ifdef CONFIG_HIGHPTE
354 static void *vmi_kmap_atomic_pte(struct page *page, enum km_type type)
355 {
356         void *va = kmap_atomic(page, type);
357
358         /*
359          * Internally, the VMI ROM must map virtual addresses to physical
360          * addresses for processing MMU updates.  By the time MMU updates
361          * are issued, this information is typically already lost.
362          * Fortunately, the VMI provides a cache of mapping slots for active
363          * page tables.
364          *
365          * We use slot zero for the linear mapping of physical memory, and
366          * in HIGHPTE kernels, slot 1 and 2 for KM_PTE0 and KM_PTE1.
367          *
368          *  args:                 SLOT                 VA    COUNT PFN
369          */
370         BUG_ON(type != KM_PTE0 && type != KM_PTE1);
371         vmi_ops.set_linear_mapping((type - KM_PTE0)+1, va, 1, page_to_pfn(page));
372
373         return va;
374 }
375 #endif
376
377 static void vmi_allocate_pt(u32 pfn)
378 {
379         vmi_set_page_type(pfn, VMI_PAGE_L1);
380         vmi_ops.allocate_page(pfn, VMI_PAGE_L1, 0, 0, 0);
381 }
382
383 static void vmi_allocate_pd(u32 pfn)
384 {
385         /*
386          * This call comes in very early, before mem_map is setup.
387          * It is called only for swapper_pg_dir, which already has
388          * data on it.
389          */
390         vmi_set_page_type(pfn, VMI_PAGE_L2);
391         vmi_ops.allocate_page(pfn, VMI_PAGE_L2, 0, 0, 0);
392 }
393
394 static void vmi_allocate_pd_clone(u32 pfn, u32 clonepfn, u32 start, u32 count)
395 {
396         vmi_set_page_type(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE);
397         vmi_check_page_type(clonepfn, VMI_PAGE_L2);
398         vmi_ops.allocate_page(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE, clonepfn, start, count);
399 }
400
401 static void vmi_release_pt(u32 pfn)
402 {
403         vmi_ops.release_page(pfn, VMI_PAGE_L1);
404         vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
405 }
406
407 static void vmi_release_pd(u32 pfn)
408 {
409         vmi_ops.release_page(pfn, VMI_PAGE_L2);
410         vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
411 }
412
413 /*
414  * Helper macros for MMU update flags.  We can defer updates until a flush
415  * or page invalidation only if the update is to the current address space
416  * (otherwise, there is no flush).  We must check against init_mm, since
417  * this could be a kernel update, which usually passes init_mm, although
418  * sometimes this check can be skipped if we know the particular function
419  * is only called on user mode PTEs.  We could change the kernel to pass
420  * current->active_mm here, but in particular, I was unsure if changing
421  * mm/highmem.c to do this would still be correct on other architectures.
422  */
423 #define is_current_as(mm, mustbeuser) ((mm) == current->active_mm ||    \
424                                        (!mustbeuser && (mm) == &init_mm))
425 #define vmi_flags_addr(mm, addr, level, user)                           \
426         ((level) | (is_current_as(mm, user) ?                           \
427                 (VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
428 #define vmi_flags_addr_defer(mm, addr, level, user)                     \
429         ((level) | (is_current_as(mm, user) ?                           \
430                 (VMI_PAGE_DEFER | VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
431
432 static void vmi_update_pte(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
433 {
434         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
435         vmi_ops.update_pte(ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
436 }
437
438 static void vmi_update_pte_defer(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
439 {
440         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
441         vmi_ops.update_pte(ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 0));
442 }
443
444 static void vmi_set_pte(pte_t *ptep, pte_t pte)
445 {
446         /* XXX because of set_pmd_pte, this can be called on PT or PD layers */
447         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE | VMI_PAGE_PD);
448         vmi_ops.set_pte(pte, ptep, VMI_PAGE_PT);
449 }
450
451 static void vmi_set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
452 {
453         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
454         vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
455 }
456
457 static void vmi_set_pmd(pmd_t *pmdp, pmd_t pmdval)
458 {
459 #ifdef CONFIG_X86_PAE
460         const pte_t pte = { pmdval.pmd, pmdval.pmd >> 32 };
461         vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PMD);
462 #else
463         const pte_t pte = { pmdval.pud.pgd.pgd };
464         vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PGD);
465 #endif
466         vmi_ops.set_pte(pte, (pte_t *)pmdp, VMI_PAGE_PD);
467 }
468
469 #ifdef CONFIG_X86_PAE
470
471 static void vmi_set_pte_atomic(pte_t *ptep, pte_t pteval)
472 {
473         /*
474          * XXX This is called from set_pmd_pte, but at both PT
475          * and PD layers so the VMI_PAGE_PT flag is wrong.  But
476          * it is only called for large page mapping changes,
477          * the Xen backend, doesn't support large pages, and the
478          * ESX backend doesn't depend on the flag.
479          */
480         set_64bit((unsigned long long *)ptep,pte_val(pteval));
481         vmi_ops.update_pte(ptep, VMI_PAGE_PT);
482 }
483
484 static void vmi_set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
485 {
486         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
487         vmi_ops.set_pte(pte, ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 1));
488 }
489
490 static void vmi_set_pud(pud_t *pudp, pud_t pudval)
491 {
492         /* Um, eww */
493         const pte_t pte = { pudval.pgd.pgd, pudval.pgd.pgd >> 32 };
494         vmi_check_page_type(__pa(pudp) >> PAGE_SHIFT, VMI_PAGE_PGD);
495         vmi_ops.set_pte(pte, (pte_t *)pudp, VMI_PAGE_PDP);
496 }
497
498 static void vmi_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
499 {
500         const pte_t pte = { 0 };
501         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
502         vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
503 }
504
505 static void vmi_pmd_clear(pmd_t *pmd)
506 {
507         const pte_t pte = { 0 };
508         vmi_check_page_type(__pa(pmd) >> PAGE_SHIFT, VMI_PAGE_PMD);
509         vmi_ops.set_pte(pte, (pte_t *)pmd, VMI_PAGE_PD);
510 }
511 #endif
512
513 #ifdef CONFIG_SMP
514 static void __devinit
515 vmi_startup_ipi_hook(int phys_apicid, unsigned long start_eip,
516                      unsigned long start_esp)
517 {
518         struct vmi_ap_state ap;
519
520         /* Default everything to zero.  This is fine for most GPRs. */
521         memset(&ap, 0, sizeof(struct vmi_ap_state));
522
523         ap.gdtr_limit = GDT_SIZE - 1;
524         ap.gdtr_base = (unsigned long) get_cpu_gdt_table(phys_apicid);
525
526         ap.idtr_limit = IDT_ENTRIES * 8 - 1;
527         ap.idtr_base = (unsigned long) idt_table;
528
529         ap.ldtr = 0;
530
531         ap.cs = __KERNEL_CS;
532         ap.eip = (unsigned long) start_eip;
533         ap.ss = __KERNEL_DS;
534         ap.esp = (unsigned long) start_esp;
535
536         ap.ds = __USER_DS;
537         ap.es = __USER_DS;
538         ap.fs = __KERNEL_PERCPU;
539         ap.gs = 0;
540
541         ap.eflags = 0;
542
543 #ifdef CONFIG_X86_PAE
544         /* efer should match BSP efer. */
545         if (cpu_has_nx) {
546                 unsigned l, h;
547                 rdmsr(MSR_EFER, l, h);
548                 ap.efer = (unsigned long long) h << 32 | l;
549         }
550 #endif
551
552         ap.cr3 = __pa(swapper_pg_dir);
553         /* Protected mode, paging, AM, WP, NE, MP. */
554         ap.cr0 = 0x80050023;
555         ap.cr4 = mmu_cr4_features;
556         vmi_ops.set_initial_ap_state((u32)&ap, phys_apicid);
557 }
558 #endif
559
560 static void vmi_set_lazy_mode(int mode)
561 {
562         static DEFINE_PER_CPU(int, lazy_mode);
563
564         if (!vmi_ops.set_lazy_mode)
565                 return;
566
567         /* Modes should never nest or overlap */
568         BUG_ON(__get_cpu_var(lazy_mode) && !(mode == PARAVIRT_LAZY_NONE ||
569                                              mode == PARAVIRT_LAZY_FLUSH));
570
571         if (mode == PARAVIRT_LAZY_FLUSH) {
572                 vmi_ops.set_lazy_mode(0);
573                 vmi_ops.set_lazy_mode(__get_cpu_var(lazy_mode));
574         } else {
575                 vmi_ops.set_lazy_mode(mode);
576                 __get_cpu_var(lazy_mode) = mode;
577         }
578 }
579
580 static inline int __init check_vmi_rom(struct vrom_header *rom)
581 {
582         struct pci_header *pci;
583         struct pnp_header *pnp;
584         const char *manufacturer = "UNKNOWN";
585         const char *product = "UNKNOWN";
586         const char *license = "unspecified";
587
588         if (rom->rom_signature != 0xaa55)
589                 return 0;
590         if (rom->vrom_signature != VMI_SIGNATURE)
591                 return 0;
592         if (rom->api_version_maj != VMI_API_REV_MAJOR ||
593             rom->api_version_min+1 < VMI_API_REV_MINOR+1) {
594                 printk(KERN_WARNING "VMI: Found mismatched rom version %d.%d\n",
595                                 rom->api_version_maj,
596                                 rom->api_version_min);
597                 return 0;
598         }
599
600         /*
601          * Relying on the VMI_SIGNATURE field is not 100% safe, so check
602          * the PCI header and device type to make sure this is really a
603          * VMI device.
604          */
605         if (!rom->pci_header_offs) {
606                 printk(KERN_WARNING "VMI: ROM does not contain PCI header.\n");
607                 return 0;
608         }
609
610         pci = (struct pci_header *)((char *)rom+rom->pci_header_offs);
611         if (pci->vendorID != PCI_VENDOR_ID_VMWARE ||
612             pci->deviceID != PCI_DEVICE_ID_VMWARE_VMI) {
613                 /* Allow it to run... anyways, but warn */
614                 printk(KERN_WARNING "VMI: ROM from unknown manufacturer\n");
615         }
616
617         if (rom->pnp_header_offs) {
618                 pnp = (struct pnp_header *)((char *)rom+rom->pnp_header_offs);
619                 if (pnp->manufacturer_offset)
620                         manufacturer = (const char *)rom+pnp->manufacturer_offset;
621                 if (pnp->product_offset)
622                         product = (const char *)rom+pnp->product_offset;
623         }
624
625         if (rom->license_offs)
626                 license = (char *)rom+rom->license_offs;
627
628         printk(KERN_INFO "VMI: Found %s %s, API version %d.%d, ROM version %d.%d\n",
629                 manufacturer, product,
630                 rom->api_version_maj, rom->api_version_min,
631                 pci->rom_version_maj, pci->rom_version_min);
632
633         /* Don't allow BSD/MIT here for now because we don't want to end up
634            with any binary only shim layers */
635         if (strcmp(license, "GPL") && strcmp(license, "GPL v2")) {
636                 printk(KERN_WARNING "VMI: Non GPL license `%s' found for ROM. Not used.\n",
637                         license);
638                 return 0;
639         }
640
641         return 1;
642 }
643
644 /*
645  * Probe for the VMI option ROM
646  */
647 static inline int __init probe_vmi_rom(void)
648 {
649         unsigned long base;
650
651         /* VMI ROM is in option ROM area, check signature */
652         for (base = 0xC0000; base < 0xE0000; base += 2048) {
653                 struct vrom_header *romstart;
654                 romstart = (struct vrom_header *)isa_bus_to_virt(base);
655                 if (check_vmi_rom(romstart)) {
656                         vmi_rom = romstart;
657                         return 1;
658                 }
659         }
660         return 0;
661 }
662
663 /*
664  * VMI setup common to all processors
665  */
666 void vmi_bringup(void)
667 {
668         /* We must establish the lowmem mapping for MMU ops to work */
669         if (vmi_ops.set_linear_mapping)
670                 vmi_ops.set_linear_mapping(0, (void *)__PAGE_OFFSET, max_low_pfn, 0);
671 }
672
673 /*
674  * Return a pointer to a VMI function or NULL if unimplemented
675  */
676 static void *vmi_get_function(int vmicall)
677 {
678         u64 reloc;
679         const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
680         reloc = call_vrom_long_func(vmi_rom, get_reloc, vmicall);
681         BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);
682         if (rel->type == VMI_RELOCATION_CALL_REL)
683                 return (void *)rel->eip;
684         else
685                 return NULL;
686 }
687
688 /*
689  * Helper macro for making the VMI paravirt-ops fill code readable.
690  * For unimplemented operations, fall back to default, unless nop
691  * is returned by the ROM.
692  */
693 #define para_fill(opname, vmicall)                              \
694 do {                                                            \
695         reloc = call_vrom_long_func(vmi_rom, get_reloc,         \
696                                     VMI_CALL_##vmicall);        \
697         if (rel->type == VMI_RELOCATION_CALL_REL)               \
698                 paravirt_ops.opname = (void *)rel->eip;         \
699         else if (rel->type == VMI_RELOCATION_NOP)               \
700                 paravirt_ops.opname = (void *)vmi_nop;          \
701         else if (rel->type != VMI_RELOCATION_NONE)              \
702                 printk(KERN_WARNING "VMI: Unknown relocation "  \
703                                     "type %d for " #vmicall"\n",\
704                                         rel->type);             \
705 } while (0)
706
707 /*
708  * Helper macro for making the VMI paravirt-ops fill code readable.
709  * For cached operations which do not match the VMI ROM ABI and must
710  * go through a tranlation stub.  Ignore NOPs, since it is not clear
711  * a NOP * VMI function corresponds to a NOP paravirt-op when the
712  * functions are not in 1-1 correspondence.
713  */
714 #define para_wrap(opname, wrapper, cache, vmicall)              \
715 do {                                                            \
716         reloc = call_vrom_long_func(vmi_rom, get_reloc,         \
717                                     VMI_CALL_##vmicall);        \
718         BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);           \
719         if (rel->type == VMI_RELOCATION_CALL_REL) {             \
720                 paravirt_ops.opname = wrapper;                  \
721                 vmi_ops.cache = (void *)rel->eip;               \
722         }                                                       \
723 } while (0)
724
725
726 /*
727  * Activate the VMI interface and switch into paravirtualized mode
728  */
729 static inline int __init activate_vmi(void)
730 {
731         short kernel_cs;
732         u64 reloc;
733         const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
734
735         if (call_vrom_func(vmi_rom, vmi_init) != 0) {
736                 printk(KERN_ERR "VMI ROM failed to initialize!");
737                 return 0;
738         }
739         savesegment(cs, kernel_cs);
740
741         paravirt_ops.paravirt_enabled = 1;
742         paravirt_ops.kernel_rpl = kernel_cs & SEGMENT_RPL_MASK;
743
744         paravirt_ops.patch = vmi_patch;
745         paravirt_ops.name = "vmi";
746
747         /*
748          * Many of these operations are ABI compatible with VMI.
749          * This means we can fill in the paravirt-ops with direct
750          * pointers into the VMI ROM.  If the calling convention for
751          * these operations changes, this code needs to be updated.
752          *
753          * Exceptions
754          *  CPUID paravirt-op uses pointers, not the native ISA
755          *  halt has no VMI equivalent; all VMI halts are "safe"
756          *  no MSR support yet - just trap and emulate.  VMI uses the
757          *    same ABI as the native ISA, but Linux wants exceptions
758          *    from bogus MSR read / write handled
759          *  rdpmc is not yet used in Linux
760          */
761
762         /* CPUID is special, so very special it gets wrapped like a present */
763         para_wrap(cpuid, vmi_cpuid, cpuid, CPUID);
764
765         para_fill(clts, CLTS);
766         para_fill(get_debugreg, GetDR);
767         para_fill(set_debugreg, SetDR);
768         para_fill(read_cr0, GetCR0);
769         para_fill(read_cr2, GetCR2);
770         para_fill(read_cr3, GetCR3);
771         para_fill(read_cr4, GetCR4);
772         para_fill(write_cr0, SetCR0);
773         para_fill(write_cr2, SetCR2);
774         para_fill(write_cr3, SetCR3);
775         para_fill(write_cr4, SetCR4);
776         para_fill(save_fl, GetInterruptMask);
777         para_fill(restore_fl, SetInterruptMask);
778         para_fill(irq_disable, DisableInterrupts);
779         para_fill(irq_enable, EnableInterrupts);
780
781         para_fill(wbinvd, WBINVD);
782         para_fill(read_tsc, RDTSC);
783
784         /* The following we emulate with trap and emulate for now */
785         /* paravirt_ops.read_msr = vmi_rdmsr */
786         /* paravirt_ops.write_msr = vmi_wrmsr */
787         /* paravirt_ops.rdpmc = vmi_rdpmc */
788
789         /* TR interface doesn't pass TR value, wrap */
790         para_wrap(load_tr_desc, vmi_set_tr, set_tr, SetTR);
791
792         /* LDT is special, too */
793         para_wrap(set_ldt, vmi_set_ldt, _set_ldt, SetLDT);
794
795         para_fill(load_gdt, SetGDT);
796         para_fill(load_idt, SetIDT);
797         para_fill(store_gdt, GetGDT);
798         para_fill(store_idt, GetIDT);
799         para_fill(store_tr, GetTR);
800         paravirt_ops.load_tls = vmi_load_tls;
801         para_fill(write_ldt_entry, WriteLDTEntry);
802         para_fill(write_gdt_entry, WriteGDTEntry);
803         para_fill(write_idt_entry, WriteIDTEntry);
804         para_wrap(load_esp0, vmi_load_esp0, set_kernel_stack, UpdateKernelStack);
805         para_fill(set_iopl_mask, SetIOPLMask);
806         para_fill(io_delay, IODelay);
807         para_wrap(set_lazy_mode, vmi_set_lazy_mode, set_lazy_mode, SetLazyMode);
808
809         /* user and kernel flush are just handled with different flags to FlushTLB */
810         para_wrap(flush_tlb_user, vmi_flush_tlb_user, _flush_tlb, FlushTLB);
811         para_wrap(flush_tlb_kernel, vmi_flush_tlb_kernel, _flush_tlb, FlushTLB);
812         para_fill(flush_tlb_single, InvalPage);
813
814         /*
815          * Until a standard flag format can be agreed on, we need to
816          * implement these as wrappers in Linux.  Get the VMI ROM
817          * function pointers for the two backend calls.
818          */
819 #ifdef CONFIG_X86_PAE
820         vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxELong);
821         vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxELong);
822 #else
823         vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxE);
824         vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxE);
825 #endif
826
827         if (vmi_ops.set_pte) {
828                 paravirt_ops.set_pte = vmi_set_pte;
829                 paravirt_ops.set_pte_at = vmi_set_pte_at;
830                 paravirt_ops.set_pmd = vmi_set_pmd;
831 #ifdef CONFIG_X86_PAE
832                 paravirt_ops.set_pte_atomic = vmi_set_pte_atomic;
833                 paravirt_ops.set_pte_present = vmi_set_pte_present;
834                 paravirt_ops.set_pud = vmi_set_pud;
835                 paravirt_ops.pte_clear = vmi_pte_clear;
836                 paravirt_ops.pmd_clear = vmi_pmd_clear;
837 #endif
838         }
839
840         if (vmi_ops.update_pte) {
841                 paravirt_ops.pte_update = vmi_update_pte;
842                 paravirt_ops.pte_update_defer = vmi_update_pte_defer;
843         }
844
845         vmi_ops.allocate_page = vmi_get_function(VMI_CALL_AllocatePage);
846         if (vmi_ops.allocate_page) {
847                 paravirt_ops.alloc_pt = vmi_allocate_pt;
848                 paravirt_ops.alloc_pd = vmi_allocate_pd;
849                 paravirt_ops.alloc_pd_clone = vmi_allocate_pd_clone;
850         }
851
852         vmi_ops.release_page = vmi_get_function(VMI_CALL_ReleasePage);
853         if (vmi_ops.release_page) {
854                 paravirt_ops.release_pt = vmi_release_pt;
855                 paravirt_ops.release_pd = vmi_release_pd;
856         }
857
858         /* Set linear is needed in all cases */
859         vmi_ops.set_linear_mapping = vmi_get_function(VMI_CALL_SetLinearMapping);
860 #ifdef CONFIG_HIGHPTE
861         if (vmi_ops.set_linear_mapping)
862                 paravirt_ops.kmap_atomic_pte = vmi_kmap_atomic_pte;
863 #endif
864
865         /*
866          * These MUST always be patched.  Don't support indirect jumps
867          * through these operations, as the VMI interface may use either
868          * a jump or a call to get to these operations, depending on
869          * the backend.  They are performance critical anyway, so requiring
870          * a patch is not a big problem.
871          */
872         paravirt_ops.irq_enable_sysexit = (void *)0xfeedbab0;
873         paravirt_ops.iret = (void *)0xbadbab0;
874
875 #ifdef CONFIG_SMP
876         para_wrap(startup_ipi_hook, vmi_startup_ipi_hook, set_initial_ap_state, SetInitialAPState);
877 #endif
878
879 #ifdef CONFIG_X86_LOCAL_APIC
880         para_fill(apic_read, APICRead);
881         para_fill(apic_write, APICWrite);
882         para_fill(apic_write_atomic, APICWrite);
883 #endif
884
885         /*
886          * Check for VMI timer functionality by probing for a cycle frequency method
887          */
888         reloc = call_vrom_long_func(vmi_rom, get_reloc, VMI_CALL_GetCycleFrequency);
889         if (!disable_vmi_timer && rel->type != VMI_RELOCATION_NONE) {
890                 vmi_timer_ops.get_cycle_frequency = (void *)rel->eip;
891                 vmi_timer_ops.get_cycle_counter =
892                         vmi_get_function(VMI_CALL_GetCycleCounter);
893                 vmi_timer_ops.get_wallclock =
894                         vmi_get_function(VMI_CALL_GetWallclockTime);
895                 vmi_timer_ops.wallclock_updated =
896                         vmi_get_function(VMI_CALL_WallclockUpdated);
897                 vmi_timer_ops.set_alarm = vmi_get_function(VMI_CALL_SetAlarm);
898                 vmi_timer_ops.cancel_alarm =
899                          vmi_get_function(VMI_CALL_CancelAlarm);
900                 paravirt_ops.time_init = vmi_time_init;
901                 paravirt_ops.get_wallclock = vmi_get_wallclock;
902                 paravirt_ops.set_wallclock = vmi_set_wallclock;
903 #ifdef CONFIG_X86_LOCAL_APIC
904                 paravirt_ops.setup_boot_clock = vmi_timer_setup_boot_alarm;
905                 paravirt_ops.setup_secondary_clock = vmi_timer_setup_secondary_alarm;
906 #endif
907                 paravirt_ops.get_scheduled_cycles = vmi_get_sched_cycles;
908                 paravirt_ops.get_cpu_khz = vmi_cpu_khz;
909
910                 /* We have true wallclock functions; disable CMOS clock sync */
911                 no_sync_cmos_clock = 1;
912         } else {
913                 disable_noidle = 1;
914                 disable_vmi_timer = 1;
915         }
916
917         /* No idle HZ mode only works if VMI timer and no idle is enabled */
918         if (disable_noidle || disable_vmi_timer)
919                 para_fill(safe_halt, Halt);
920         else
921                 para_wrap(safe_halt, vmi_safe_halt, halt, Halt);
922
923         /*
924          * Alternative instruction rewriting doesn't happen soon enough
925          * to convert VMI_IRET to a call instead of a jump; so we have
926          * to do this before IRQs get reenabled.  Fortunately, it is
927          * idempotent.
928          */
929         apply_paravirt(__start_parainstructions, __stop_parainstructions);
930
931         vmi_bringup();
932
933         return 1;
934 }
935
936 #undef para_fill
937
938 void __init vmi_init(void)
939 {
940         unsigned long flags;
941
942         if (!vmi_rom)
943                 probe_vmi_rom();
944         else
945                 check_vmi_rom(vmi_rom);
946
947         /* In case probing for or validating the ROM failed, basil */
948         if (!vmi_rom)
949                 return;
950
951         reserve_top_address(-vmi_rom->virtual_top);
952
953         local_irq_save(flags);
954         activate_vmi();
955
956 #ifdef CONFIG_X86_IO_APIC
957         /* This is virtual hardware; timer routing is wired correctly */
958         no_timer_check = 1;
959 #endif
960         local_irq_restore(flags & X86_EFLAGS_IF);
961 }
962
963 static int __init parse_vmi(char *arg)
964 {
965         if (!arg)
966                 return -EINVAL;
967
968         if (!strcmp(arg, "disable_pge")) {
969                 clear_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
970                 disable_pge = 1;
971         } else if (!strcmp(arg, "disable_pse")) {
972                 clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
973                 disable_pse = 1;
974         } else if (!strcmp(arg, "disable_sep")) {
975                 clear_bit(X86_FEATURE_SEP, boot_cpu_data.x86_capability);
976                 disable_sep = 1;
977         } else if (!strcmp(arg, "disable_tsc")) {
978                 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
979                 disable_tsc = 1;
980         } else if (!strcmp(arg, "disable_mtrr")) {
981                 clear_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability);
982                 disable_mtrr = 1;
983         } else if (!strcmp(arg, "disable_timer")) {
984                 disable_vmi_timer = 1;
985                 disable_noidle = 1;
986         } else if (!strcmp(arg, "disable_noidle"))
987                 disable_noidle = 1;
988         return 0;
989 }
990
991 early_param("vmi", parse_vmi);