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