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