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