[PATCH] i386: Now that the VDSO can be relocated, we can support it in VMI configurat...
[deliverable/linux.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
c6b36e9a 507static void __devinit
7ce0bcfd
ZA
508vmi_startup_ipi_hook(int phys_apicid, unsigned long start_eip,
509 unsigned long start_esp)
510{
c6b36e9a
ZA
511 struct vmi_ap_state ap;
512
7ce0bcfd
ZA
513 /* Default everything to zero. This is fine for most GPRs. */
514 memset(&ap, 0, sizeof(struct vmi_ap_state));
515
516 ap.gdtr_limit = GDT_SIZE - 1;
517 ap.gdtr_base = (unsigned long) get_cpu_gdt_table(phys_apicid);
518
519 ap.idtr_limit = IDT_ENTRIES * 8 - 1;
520 ap.idtr_base = (unsigned long) idt_table;
521
522 ap.ldtr = 0;
523
524 ap.cs = __KERNEL_CS;
525 ap.eip = (unsigned long) start_eip;
526 ap.ss = __KERNEL_DS;
527 ap.esp = (unsigned long) start_esp;
528
529 ap.ds = __USER_DS;
530 ap.es = __USER_DS;
7c3576d2 531 ap.fs = __KERNEL_PERCPU;
7ce0bcfd
ZA
532 ap.gs = 0;
533
534 ap.eflags = 0;
535
7ce0bcfd
ZA
536#ifdef CONFIG_X86_PAE
537 /* efer should match BSP efer. */
538 if (cpu_has_nx) {
539 unsigned l, h;
540 rdmsr(MSR_EFER, l, h);
541 ap.efer = (unsigned long long) h << 32 | l;
542 }
543#endif
544
545 ap.cr3 = __pa(swapper_pg_dir);
546 /* Protected mode, paging, AM, WP, NE, MP. */
547 ap.cr0 = 0x80050023;
548 ap.cr4 = mmu_cr4_features;
c6b36e9a 549 vmi_ops.set_initial_ap_state((u32)&ap, phys_apicid);
7ce0bcfd
ZA
550}
551#endif
552
49f19710
ZA
553static void vmi_set_lazy_mode(int mode)
554{
555 static DEFINE_PER_CPU(int, lazy_mode);
556
557 if (!vmi_ops.set_lazy_mode)
558 return;
559
560 /* Modes should never nest or overlap */
561 BUG_ON(__get_cpu_var(lazy_mode) && !(mode == PARAVIRT_LAZY_NONE ||
562 mode == PARAVIRT_LAZY_FLUSH));
563
564 if (mode == PARAVIRT_LAZY_FLUSH) {
565 vmi_ops.set_lazy_mode(0);
566 vmi_ops.set_lazy_mode(__get_cpu_var(lazy_mode));
567 } else {
568 vmi_ops.set_lazy_mode(mode);
569 __get_cpu_var(lazy_mode) = mode;
570 }
571}
572
7ce0bcfd
ZA
573static inline int __init check_vmi_rom(struct vrom_header *rom)
574{
575 struct pci_header *pci;
576 struct pnp_header *pnp;
577 const char *manufacturer = "UNKNOWN";
578 const char *product = "UNKNOWN";
579 const char *license = "unspecified";
580
581 if (rom->rom_signature != 0xaa55)
582 return 0;
583 if (rom->vrom_signature != VMI_SIGNATURE)
584 return 0;
585 if (rom->api_version_maj != VMI_API_REV_MAJOR ||
586 rom->api_version_min+1 < VMI_API_REV_MINOR+1) {
587 printk(KERN_WARNING "VMI: Found mismatched rom version %d.%d\n",
588 rom->api_version_maj,
589 rom->api_version_min);
590 return 0;
591 }
592
593 /*
594 * Relying on the VMI_SIGNATURE field is not 100% safe, so check
595 * the PCI header and device type to make sure this is really a
596 * VMI device.
597 */
598 if (!rom->pci_header_offs) {
599 printk(KERN_WARNING "VMI: ROM does not contain PCI header.\n");
600 return 0;
601 }
602
603 pci = (struct pci_header *)((char *)rom+rom->pci_header_offs);
604 if (pci->vendorID != PCI_VENDOR_ID_VMWARE ||
605 pci->deviceID != PCI_DEVICE_ID_VMWARE_VMI) {
606 /* Allow it to run... anyways, but warn */
607 printk(KERN_WARNING "VMI: ROM from unknown manufacturer\n");
608 }
609
610 if (rom->pnp_header_offs) {
611 pnp = (struct pnp_header *)((char *)rom+rom->pnp_header_offs);
612 if (pnp->manufacturer_offset)
613 manufacturer = (const char *)rom+pnp->manufacturer_offset;
614 if (pnp->product_offset)
615 product = (const char *)rom+pnp->product_offset;
616 }
617
618 if (rom->license_offs)
619 license = (char *)rom+rom->license_offs;
620
621 printk(KERN_INFO "VMI: Found %s %s, API version %d.%d, ROM version %d.%d\n",
622 manufacturer, product,
623 rom->api_version_maj, rom->api_version_min,
624 pci->rom_version_maj, pci->rom_version_min);
625
302cf930
AK
626 /* Don't allow BSD/MIT here for now because we don't want to end up
627 with any binary only shim layers */
628 if (strcmp(license, "GPL") && strcmp(license, "GPL v2")) {
629 printk(KERN_WARNING "VMI: Non GPL license `%s' found for ROM. Not used.\n",
630 license);
631 return 0;
632 }
633
7ce0bcfd
ZA
634 return 1;
635}
636
637/*
638 * Probe for the VMI option ROM
639 */
640static inline int __init probe_vmi_rom(void)
641{
642 unsigned long base;
643
644 /* VMI ROM is in option ROM area, check signature */
645 for (base = 0xC0000; base < 0xE0000; base += 2048) {
646 struct vrom_header *romstart;
647 romstart = (struct vrom_header *)isa_bus_to_virt(base);
648 if (check_vmi_rom(romstart)) {
649 vmi_rom = romstart;
650 return 1;
651 }
652 }
653 return 0;
654}
655
656/*
657 * VMI setup common to all processors
658 */
659void vmi_bringup(void)
660{
661 /* We must establish the lowmem mapping for MMU ops to work */
772205f6 662 if (vmi_ops.set_linear_mapping)
7ce0bcfd
ZA
663 vmi_ops.set_linear_mapping(0, __PAGE_OFFSET, max_low_pfn, 0);
664}
665
666/*
772205f6 667 * Return a pointer to a VMI function or NULL if unimplemented
7ce0bcfd
ZA
668 */
669static void *vmi_get_function(int vmicall)
670{
671 u64 reloc;
672 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
673 reloc = call_vrom_long_func(vmi_rom, get_reloc, vmicall);
674 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);
675 if (rel->type == VMI_RELOCATION_CALL_REL)
676 return (void *)rel->eip;
677 else
772205f6 678 return NULL;
7ce0bcfd
ZA
679}
680
681/*
682 * Helper macro for making the VMI paravirt-ops fill code readable.
772205f6
ZA
683 * For unimplemented operations, fall back to default, unless nop
684 * is returned by the ROM.
7ce0bcfd
ZA
685 */
686#define para_fill(opname, vmicall) \
687do { \
688 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
689 VMI_CALL_##vmicall); \
0492c371 690 if (rel->type == VMI_RELOCATION_CALL_REL) \
7ce0bcfd 691 paravirt_ops.opname = (void *)rel->eip; \
0492c371 692 else if (rel->type == VMI_RELOCATION_NOP) \
772205f6 693 paravirt_ops.opname = (void *)vmi_nop; \
0492c371
ZA
694 else if (rel->type != VMI_RELOCATION_NONE) \
695 printk(KERN_WARNING "VMI: Unknown relocation " \
696 "type %d for " #vmicall"\n",\
697 rel->type); \
772205f6
ZA
698} while (0)
699
700/*
701 * Helper macro for making the VMI paravirt-ops fill code readable.
702 * For cached operations which do not match the VMI ROM ABI and must
703 * go through a tranlation stub. Ignore NOPs, since it is not clear
704 * a NOP * VMI function corresponds to a NOP paravirt-op when the
705 * functions are not in 1-1 correspondence.
706 */
707#define para_wrap(opname, wrapper, cache, vmicall) \
708do { \
709 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
710 VMI_CALL_##vmicall); \
711 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL); \
712 if (rel->type == VMI_RELOCATION_CALL_REL) { \
713 paravirt_ops.opname = wrapper; \
714 vmi_ops.cache = (void *)rel->eip; \
7ce0bcfd
ZA
715 } \
716} while (0)
717
772205f6 718
7ce0bcfd
ZA
719/*
720 * Activate the VMI interface and switch into paravirtualized mode
721 */
722static inline int __init activate_vmi(void)
723{
724 short kernel_cs;
725 u64 reloc;
726 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
727
728 if (call_vrom_func(vmi_rom, vmi_init) != 0) {
729 printk(KERN_ERR "VMI ROM failed to initialize!");
730 return 0;
731 }
732 savesegment(cs, kernel_cs);
733
734 paravirt_ops.paravirt_enabled = 1;
735 paravirt_ops.kernel_rpl = kernel_cs & SEGMENT_RPL_MASK;
736
737 paravirt_ops.patch = vmi_patch;
738 paravirt_ops.name = "vmi";
739
740 /*
741 * Many of these operations are ABI compatible with VMI.
742 * This means we can fill in the paravirt-ops with direct
743 * pointers into the VMI ROM. If the calling convention for
744 * these operations changes, this code needs to be updated.
745 *
746 * Exceptions
747 * CPUID paravirt-op uses pointers, not the native ISA
748 * halt has no VMI equivalent; all VMI halts are "safe"
749 * no MSR support yet - just trap and emulate. VMI uses the
750 * same ABI as the native ISA, but Linux wants exceptions
751 * from bogus MSR read / write handled
752 * rdpmc is not yet used in Linux
753 */
754
772205f6
ZA
755 /* CPUID is special, so very special it gets wrapped like a present */
756 para_wrap(cpuid, vmi_cpuid, cpuid, CPUID);
7ce0bcfd
ZA
757
758 para_fill(clts, CLTS);
759 para_fill(get_debugreg, GetDR);
760 para_fill(set_debugreg, SetDR);
761 para_fill(read_cr0, GetCR0);
762 para_fill(read_cr2, GetCR2);
763 para_fill(read_cr3, GetCR3);
764 para_fill(read_cr4, GetCR4);
765 para_fill(write_cr0, SetCR0);
766 para_fill(write_cr2, SetCR2);
767 para_fill(write_cr3, SetCR3);
768 para_fill(write_cr4, SetCR4);
769 para_fill(save_fl, GetInterruptMask);
770 para_fill(restore_fl, SetInterruptMask);
771 para_fill(irq_disable, DisableInterrupts);
772 para_fill(irq_enable, EnableInterrupts);
772205f6 773
7ce0bcfd 774 para_fill(wbinvd, WBINVD);
772205f6
ZA
775 para_fill(read_tsc, RDTSC);
776
777 /* The following we emulate with trap and emulate for now */
7ce0bcfd
ZA
778 /* paravirt_ops.read_msr = vmi_rdmsr */
779 /* paravirt_ops.write_msr = vmi_wrmsr */
7ce0bcfd
ZA
780 /* paravirt_ops.rdpmc = vmi_rdpmc */
781
772205f6
ZA
782 /* TR interface doesn't pass TR value, wrap */
783 para_wrap(load_tr_desc, vmi_set_tr, set_tr, SetTR);
7ce0bcfd
ZA
784
785 /* LDT is special, too */
772205f6 786 para_wrap(set_ldt, vmi_set_ldt, _set_ldt, SetLDT);
7ce0bcfd
ZA
787
788 para_fill(load_gdt, SetGDT);
789 para_fill(load_idt, SetIDT);
790 para_fill(store_gdt, GetGDT);
791 para_fill(store_idt, GetIDT);
792 para_fill(store_tr, GetTR);
793 paravirt_ops.load_tls = vmi_load_tls;
794 para_fill(write_ldt_entry, WriteLDTEntry);
795 para_fill(write_gdt_entry, WriteGDTEntry);
796 para_fill(write_idt_entry, WriteIDTEntry);
772205f6 797 para_wrap(load_esp0, vmi_load_esp0, set_kernel_stack, UpdateKernelStack);
7ce0bcfd 798 para_fill(set_iopl_mask, SetIOPLMask);
772205f6 799 para_fill(io_delay, IODelay);
49f19710 800 para_wrap(set_lazy_mode, vmi_set_lazy_mode, set_lazy_mode, SetLazyMode);
7ce0bcfd 801
772205f6
ZA
802 /* user and kernel flush are just handled with different flags to FlushTLB */
803 para_wrap(flush_tlb_user, vmi_flush_tlb_user, flush_tlb, FlushTLB);
804 para_wrap(flush_tlb_kernel, vmi_flush_tlb_kernel, flush_tlb, FlushTLB);
7ce0bcfd
ZA
805 para_fill(flush_tlb_single, InvalPage);
806
807 /*
808 * Until a standard flag format can be agreed on, we need to
809 * implement these as wrappers in Linux. Get the VMI ROM
810 * function pointers for the two backend calls.
811 */
812#ifdef CONFIG_X86_PAE
813 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxELong);
814 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxELong);
815#else
816 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxE);
817 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxE);
818#endif
7ce0bcfd 819
772205f6
ZA
820 if (vmi_ops.set_pte) {
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;
7ce0bcfd 824#ifdef CONFIG_X86_PAE
772205f6
ZA
825 paravirt_ops.set_pte_atomic = vmi_set_pte_atomic;
826 paravirt_ops.set_pte_present = vmi_set_pte_present;
827 paravirt_ops.set_pud = vmi_set_pud;
828 paravirt_ops.pte_clear = vmi_pte_clear;
829 paravirt_ops.pmd_clear = vmi_pmd_clear;
7ce0bcfd 830#endif
772205f6
ZA
831 }
832
833 if (vmi_ops.update_pte) {
834 paravirt_ops.pte_update = vmi_update_pte;
835 paravirt_ops.pte_update_defer = vmi_update_pte_defer;
836 }
837
838 vmi_ops.allocate_page = vmi_get_function(VMI_CALL_AllocatePage);
839 if (vmi_ops.allocate_page) {
840 paravirt_ops.alloc_pt = vmi_allocate_pt;
841 paravirt_ops.alloc_pd = vmi_allocate_pd;
842 paravirt_ops.alloc_pd_clone = vmi_allocate_pd_clone;
843 }
844
845 vmi_ops.release_page = vmi_get_function(VMI_CALL_ReleasePage);
846 if (vmi_ops.release_page) {
847 paravirt_ops.release_pt = vmi_release_pt;
848 paravirt_ops.release_pd = vmi_release_pd;
849 }
a27fe809 850#if 0
772205f6
ZA
851 para_wrap(map_pt_hook, vmi_map_pt_hook, set_linear_mapping,
852 SetLinearMapping);
a27fe809 853#endif
772205f6 854
7ce0bcfd
ZA
855 /*
856 * These MUST always be patched. Don't support indirect jumps
857 * through these operations, as the VMI interface may use either
858 * a jump or a call to get to these operations, depending on
859 * the backend. They are performance critical anyway, so requiring
860 * a patch is not a big problem.
861 */
862 paravirt_ops.irq_enable_sysexit = (void *)0xfeedbab0;
863 paravirt_ops.iret = (void *)0xbadbab0;
864
865#ifdef CONFIG_SMP
772205f6 866 para_wrap(startup_ipi_hook, vmi_startup_ipi_hook, set_initial_ap_state, SetInitialAPState);
7ce0bcfd
ZA
867#endif
868
869#ifdef CONFIG_X86_LOCAL_APIC
772205f6
ZA
870 para_fill(apic_read, APICRead);
871 para_fill(apic_write, APICWrite);
872 para_fill(apic_write_atomic, APICWrite);
7ce0bcfd
ZA
873#endif
874
bbab4f3b
ZA
875 /*
876 * Check for VMI timer functionality by probing for a cycle frequency method
877 */
878 reloc = call_vrom_long_func(vmi_rom, get_reloc, VMI_CALL_GetCycleFrequency);
772205f6 879 if (!disable_vmi_timer && rel->type != VMI_RELOCATION_NONE) {
bbab4f3b
ZA
880 vmi_timer_ops.get_cycle_frequency = (void *)rel->eip;
881 vmi_timer_ops.get_cycle_counter =
882 vmi_get_function(VMI_CALL_GetCycleCounter);
883 vmi_timer_ops.get_wallclock =
884 vmi_get_function(VMI_CALL_GetWallclockTime);
885 vmi_timer_ops.wallclock_updated =
886 vmi_get_function(VMI_CALL_WallclockUpdated);
887 vmi_timer_ops.set_alarm = vmi_get_function(VMI_CALL_SetAlarm);
888 vmi_timer_ops.cancel_alarm =
889 vmi_get_function(VMI_CALL_CancelAlarm);
890 paravirt_ops.time_init = vmi_time_init;
891 paravirt_ops.get_wallclock = vmi_get_wallclock;
892 paravirt_ops.set_wallclock = vmi_set_wallclock;
893#ifdef CONFIG_X86_LOCAL_APIC
894 paravirt_ops.setup_boot_clock = vmi_timer_setup_boot_alarm;
895 paravirt_ops.setup_secondary_clock = vmi_timer_setup_secondary_alarm;
896#endif
6cb9a835 897 paravirt_ops.get_scheduled_cycles = vmi_get_sched_cycles;
1182d852 898 paravirt_ops.get_cpu_khz = vmi_cpu_khz;
772205f6
ZA
899
900 /* We have true wallclock functions; disable CMOS clock sync */
901 no_sync_cmos_clock = 1;
902 } else {
903 disable_noidle = 1;
904 disable_vmi_timer = 1;
bbab4f3b 905 }
772205f6
ZA
906
907 /* No idle HZ mode only works if VMI timer and no idle is enabled */
908 if (disable_noidle || disable_vmi_timer)
7507ba34 909 para_fill(safe_halt, Halt);
772205f6
ZA
910 else
911 para_wrap(safe_halt, vmi_safe_halt, halt, Halt);
bbab4f3b 912
7ce0bcfd
ZA
913 /*
914 * Alternative instruction rewriting doesn't happen soon enough
915 * to convert VMI_IRET to a call instead of a jump; so we have
916 * to do this before IRQs get reenabled. Fortunately, it is
917 * idempotent.
918 */
919 apply_paravirt(__start_parainstructions, __stop_parainstructions);
920
921 vmi_bringup();
922
923 return 1;
924}
925
926#undef para_fill
927
928void __init vmi_init(void)
929{
930 unsigned long flags;
931
932 if (!vmi_rom)
933 probe_vmi_rom();
934 else
935 check_vmi_rom(vmi_rom);
936
937 /* In case probing for or validating the ROM failed, basil */
938 if (!vmi_rom)
939 return;
940
941 reserve_top_address(-vmi_rom->virtual_top);
942
943 local_irq_save(flags);
944 activate_vmi();
7507ba34
ZA
945
946#ifdef CONFIG_X86_IO_APIC
772205f6 947 /* This is virtual hardware; timer routing is wired correctly */
7ce0bcfd
ZA
948 no_timer_check = 1;
949#endif
950 local_irq_restore(flags & X86_EFLAGS_IF);
951}
952
953static int __init parse_vmi(char *arg)
954{
955 if (!arg)
956 return -EINVAL;
957
eda08b1b 958 if (!strcmp(arg, "disable_pge")) {
7ce0bcfd
ZA
959 clear_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
960 disable_pge = 1;
961 } else if (!strcmp(arg, "disable_pse")) {
962 clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
963 disable_pse = 1;
964 } else if (!strcmp(arg, "disable_sep")) {
965 clear_bit(X86_FEATURE_SEP, boot_cpu_data.x86_capability);
966 disable_sep = 1;
967 } else if (!strcmp(arg, "disable_tsc")) {
968 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
969 disable_tsc = 1;
970 } else if (!strcmp(arg, "disable_mtrr")) {
971 clear_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability);
972 disable_mtrr = 1;
772205f6
ZA
973 } else if (!strcmp(arg, "disable_timer")) {
974 disable_vmi_timer = 1;
975 disable_noidle = 1;
7507ba34
ZA
976 } else if (!strcmp(arg, "disable_noidle"))
977 disable_noidle = 1;
7ce0bcfd
ZA
978 return 0;
979}
980
981early_param("vmi", parse_vmi);
This page took 0.165985 seconds and 5 git commands to generate.