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