blob: 956f38927aa7c5533cc41fd3c2ba0ea807ccb6dd [file] [log] [blame]
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001/*
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>
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010026#include <linux/cpu.h>
27#include <linux/bootmem.h>
28#include <linux/mm.h>
Zachary Amsdeneeef9c62007-05-02 19:27:16 +020029#include <linux/highmem.h>
Alexey Dobriyanfa0aa862007-06-01 00:46:27 -070030#include <linux/sched.h>
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010031#include <asm/vmi.h>
32#include <asm/io.h>
33#include <asm/fixmap.h>
34#include <asm/apicdef.h>
35#include <asm/apic.h>
36#include <asm/processor.h>
37#include <asm/timer.h>
Zachary Amsdenbbab4f32007-02-13 13:26:21 +010038#include <asm/vmi_time.h>
Adrian Bunk8f485612007-03-05 00:30:56 -080039#include <asm/kmap_types.h>
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010040
41/* Convenient for calling VMI functions indirectly in the ROM */
42typedef u32 __attribute__((regparm(1))) (VROMFUNC)(void);
43typedef u64 __attribute__((regparm(2))) (VROMLONGFUNC)(int);
44
45#define call_vrom_func(rom,func) \
46 (((VROMFUNC *)(rom->func))())
47
48#define call_vrom_long_func(rom,func,arg) \
49 (((VROMLONGFUNC *)(rom->func)) (arg))
50
51static struct vrom_header *vmi_rom;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010052static int disable_pge;
53static int disable_pse;
54static int disable_sep;
55static int disable_tsc;
56static int disable_mtrr;
Zachary Amsden7507ba32007-03-05 00:30:34 -080057static int disable_noidle;
Zachary Amsden772205f2007-03-05 00:30:41 -080058static int disable_vmi_timer;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010059
60/* Cached VMI operations */
Adrian Bunk30a15282007-05-02 19:27:08 +020061static struct {
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010062 void (*cpuid)(void /* non-c */);
63 void (*_set_ldt)(u32 selector);
64 void (*set_tr)(u32 selector);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +010065 void (*write_idt_entry)(struct desc_struct *, int, u32, u32);
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +010066 void (*write_gdt_entry)(struct desc_struct *, int, u32, u32);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +010067 void (*write_ldt_entry)(struct desc_struct *, int, u32, u32);
H. Peter Anvinfaca6222008-01-30 13:31:02 +010068 void (*set_kernel_stack)(u32 selector, u32 sp0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010069 void (*allocate_page)(u32, u32, u32, u32, u32);
70 void (*release_page)(u32, u32);
71 void (*set_pte)(pte_t, pte_t *, unsigned);
72 void (*update_pte)(pte_t *, unsigned);
Zachary Amsdeneeef9c62007-05-02 19:27:16 +020073 void (*set_linear_mapping)(int, void *, u32, u32);
74 void (*_flush_tlb)(int);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010075 void (*set_initial_ap_state)(int, int);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +010076 void (*halt)(void);
Zachary Amsden49f19712007-04-08 16:04:01 -070077 void (*set_lazy_mode)(int mode);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010078} vmi_ops;
79
Zachary Amsdene0bb8642007-05-02 19:27:16 +020080/* Cached VMI operations */
81struct vmi_timer_ops vmi_timer_ops;
82
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010083/*
84 * VMI patching routines.
85 */
86#define MNEM_CALL 0xe8
87#define MNEM_JMP 0xe9
88#define MNEM_RET 0xc3
89
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010090#define IRQ_PATCH_INT_MASK 0
91#define IRQ_PATCH_DISABLE 5
92
Andi Kleenab144f52007-08-10 22:31:03 +020093static inline void patch_offset(void *insnbuf,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010094 unsigned long ip, unsigned long dest)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010095{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010096 *(unsigned long *)(insnbuf+1) = dest-ip-5;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010097}
98
Andi Kleenab144f52007-08-10 22:31:03 +020099static unsigned patch_internal(int call, unsigned len, void *insnbuf,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100100 unsigned long ip)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100101{
102 u64 reloc;
103 struct vmi_relocation_info *const rel = (struct vmi_relocation_info *)&reloc;
104 reloc = call_vrom_long_func(vmi_rom, get_reloc, call);
105 switch(rel->type) {
106 case VMI_RELOCATION_CALL_REL:
107 BUG_ON(len < 5);
Andi Kleenab144f52007-08-10 22:31:03 +0200108 *(char *)insnbuf = MNEM_CALL;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100109 patch_offset(insnbuf, ip, (unsigned long)rel->eip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100110 return 5;
111
112 case VMI_RELOCATION_JUMP_REL:
113 BUG_ON(len < 5);
Andi Kleenab144f52007-08-10 22:31:03 +0200114 *(char *)insnbuf = MNEM_JMP;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100115 patch_offset(insnbuf, ip, (unsigned long)rel->eip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100116 return 5;
117
118 case VMI_RELOCATION_NOP:
119 /* obliterate the whole thing */
120 return 0;
121
122 case VMI_RELOCATION_NONE:
123 /* leave native code in place */
124 break;
125
126 default:
127 BUG();
128 }
129 return len;
130}
131
132/*
133 * Apply patch if appropriate, return length of new instruction
134 * sequence. The callee does nop padding for us.
135 */
Andi Kleenab144f52007-08-10 22:31:03 +0200136static unsigned vmi_patch(u8 type, u16 clobbers, void *insns,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100137 unsigned long ip, unsigned len)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100138{
139 switch (type) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700140 case PARAVIRT_PATCH(pv_irq_ops.irq_disable):
Andi Kleenab144f52007-08-10 22:31:03 +0200141 return patch_internal(VMI_CALL_DisableInterrupts, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100142 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700143 case PARAVIRT_PATCH(pv_irq_ops.irq_enable):
Andi Kleenab144f52007-08-10 22:31:03 +0200144 return patch_internal(VMI_CALL_EnableInterrupts, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100145 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700146 case PARAVIRT_PATCH(pv_irq_ops.restore_fl):
Andi Kleenab144f52007-08-10 22:31:03 +0200147 return patch_internal(VMI_CALL_SetInterruptMask, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100148 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700149 case PARAVIRT_PATCH(pv_irq_ops.save_fl):
Andi Kleenab144f52007-08-10 22:31:03 +0200150 return patch_internal(VMI_CALL_GetInterruptMask, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100151 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700152 case PARAVIRT_PATCH(pv_cpu_ops.iret):
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100153 return patch_internal(VMI_CALL_IRET, len, insns, ip);
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100154 case PARAVIRT_PATCH(pv_cpu_ops.irq_enable_syscall_ret):
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100155 return patch_internal(VMI_CALL_SYSEXIT, len, insns, ip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100156 default:
157 break;
158 }
159 return len;
160}
161
162/* CPUID has non-C semantics, and paravirt-ops API doesn't match hardware ISA */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100163static void vmi_cpuid(unsigned int *ax, unsigned int *bx,
164 unsigned int *cx, unsigned int *dx)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100165{
166 int override = 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100167 if (*ax == 1)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100168 override = 1;
169 asm volatile ("call *%6"
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100170 : "=a" (*ax),
171 "=b" (*bx),
172 "=c" (*cx),
173 "=d" (*dx)
174 : "0" (*ax), "2" (*cx), "r" (vmi_ops.cpuid));
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100175 if (override) {
176 if (disable_pse)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100177 *dx &= ~X86_FEATURE_PSE;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100178 if (disable_pge)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100179 *dx &= ~X86_FEATURE_PGE;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100180 if (disable_sep)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100181 *dx &= ~X86_FEATURE_SEP;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100182 if (disable_tsc)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100183 *dx &= ~X86_FEATURE_TSC;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100184 if (disable_mtrr)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100185 *dx &= ~X86_FEATURE_MTRR;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100186 }
187}
188
189static inline void vmi_maybe_load_tls(struct desc_struct *gdt, int nr, struct desc_struct *new)
190{
191 if (gdt[nr].a != new->a || gdt[nr].b != new->b)
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100192 write_gdt_entry(gdt, nr, new, 0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100193}
194
195static void vmi_load_tls(struct thread_struct *t, unsigned int cpu)
196{
197 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
198 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 0, &t->tls_array[0]);
199 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 1, &t->tls_array[1]);
200 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 2, &t->tls_array[2]);
201}
202
203static void vmi_set_ldt(const void *addr, unsigned entries)
204{
205 unsigned cpu = smp_processor_id();
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100206 struct desc_struct desc;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100207
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100208 pack_descriptor(&desc, (unsigned long)addr,
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100209 entries * sizeof(struct desc_struct) - 1,
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100210 DESC_LDT, 0);
211 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, &desc, DESC_LDT);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100212 vmi_ops._set_ldt(entries ? GDT_ENTRY_LDT*sizeof(struct desc_struct) : 0);
213}
214
215static void vmi_set_tr(void)
216{
217 vmi_ops.set_tr(GDT_ENTRY_TSS*sizeof(struct desc_struct));
218}
219
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100220static void vmi_write_idt_entry(gate_desc *dt, int entry, const gate_desc *g)
221{
222 u32 *idt_entry = (u32 *)g;
Ingo Molnar262d5ee2008-02-04 16:47:54 +0100223 vmi_ops.write_idt_entry(dt, entry, idt_entry[0], idt_entry[1]);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100224}
225
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100226static void vmi_write_gdt_entry(struct desc_struct *dt, int entry,
227 const void *desc, int type)
228{
229 u32 *gdt_entry = (u32 *)desc;
Ingo Molnar262d5ee2008-02-04 16:47:54 +0100230 vmi_ops.write_gdt_entry(dt, entry, gdt_entry[0], gdt_entry[1]);
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100231}
232
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100233static void vmi_write_ldt_entry(struct desc_struct *dt, int entry,
234 const void *desc)
235{
236 u32 *ldt_entry = (u32 *)desc;
Ingo Molnar262d5ee2008-02-04 16:47:54 +0100237 vmi_ops.write_idt_entry(dt, entry, ldt_entry[0], ldt_entry[1]);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100238}
239
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100240static void vmi_load_sp0(struct tss_struct *tss,
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100241 struct thread_struct *thread)
242{
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100243 tss->x86_tss.sp0 = thread->sp0;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100244
245 /* This can only happen when SEP is enabled, no need to test "SEP"arately */
Rusty Russella75c54f2007-05-02 19:27:13 +0200246 if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
247 tss->x86_tss.ss1 = thread->sysenter_cs;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100248 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
249 }
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100250 vmi_ops.set_kernel_stack(__KERNEL_DS, tss->x86_tss.sp0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100251}
252
253static void vmi_flush_tlb_user(void)
254{
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200255 vmi_ops._flush_tlb(VMI_FLUSH_TLB);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100256}
257
258static void vmi_flush_tlb_kernel(void)
259{
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200260 vmi_ops._flush_tlb(VMI_FLUSH_TLB | VMI_FLUSH_GLOBAL);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100261}
262
263/* Stub to do nothing at all; used for delays and unimplemented calls */
264static void vmi_nop(void)
265{
266}
267
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100268#ifdef CONFIG_DEBUG_PAGE_TYPE
269
270#ifdef CONFIG_X86_PAE
271#define MAX_BOOT_PTS (2048+4+1)
272#else
273#define MAX_BOOT_PTS (1024+1)
274#endif
275
276/*
277 * During boot, mem_map is not yet available in paging_init, so stash
278 * all the boot page allocations here.
279 */
280static struct {
281 u32 pfn;
282 int type;
283} boot_page_allocations[MAX_BOOT_PTS];
284static int num_boot_page_allocations;
285static int boot_allocations_applied;
286
287void vmi_apply_boot_page_allocations(void)
288{
289 int i;
290 BUG_ON(!mem_map);
291 for (i = 0; i < num_boot_page_allocations; i++) {
292 struct page *page = pfn_to_page(boot_page_allocations[i].pfn);
293 page->type = boot_page_allocations[i].type;
294 page->type = boot_page_allocations[i].type &
295 ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
296 }
297 boot_allocations_applied = 1;
298}
299
300static void record_page_type(u32 pfn, int type)
301{
302 BUG_ON(num_boot_page_allocations >= MAX_BOOT_PTS);
303 boot_page_allocations[num_boot_page_allocations].pfn = pfn;
304 boot_page_allocations[num_boot_page_allocations].type = type;
305 num_boot_page_allocations++;
306}
307
308static void check_zeroed_page(u32 pfn, int type, struct page *page)
309{
310 u32 *ptr;
311 int i;
312 int limit = PAGE_SIZE / sizeof(int);
313
314 if (page_address(page))
315 ptr = (u32 *)page_address(page);
316 else
317 ptr = (u32 *)__va(pfn << PAGE_SHIFT);
318 /*
319 * When cloning the root in non-PAE mode, only the userspace
320 * pdes need to be zeroed.
321 */
322 if (type & VMI_PAGE_CLONE)
Jeremy Fitzhardinge68db0652008-03-17 16:37:13 -0700323 limit = KERNEL_PGD_BOUNDARY;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100324 for (i = 0; i < limit; i++)
325 BUG_ON(ptr[i]);
326}
327
328/*
329 * We stash the page type into struct page so we can verify the page
330 * types are used properly.
331 */
332static void vmi_set_page_type(u32 pfn, int type)
333{
334 /* PAE can have multiple roots per page - don't track */
335 if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
336 return;
337
338 if (boot_allocations_applied) {
339 struct page *page = pfn_to_page(pfn);
340 if (type != VMI_PAGE_NORMAL)
341 BUG_ON(page->type);
342 else
343 BUG_ON(page->type == VMI_PAGE_NORMAL);
344 page->type = type & ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
345 if (type & VMI_PAGE_ZEROED)
346 check_zeroed_page(pfn, type, page);
347 } else {
348 record_page_type(pfn, type);
349 }
350}
351
352static void vmi_check_page_type(u32 pfn, int type)
353{
354 /* PAE can have multiple roots per page - skip checks */
355 if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
356 return;
357
358 type &= ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
359 if (boot_allocations_applied) {
360 struct page *page = pfn_to_page(pfn);
361 BUG_ON((page->type ^ type) & VMI_PAGE_PAE);
362 BUG_ON(type == VMI_PAGE_NORMAL && page->type);
363 BUG_ON((type & page->type) == 0);
364 }
365}
366#else
367#define vmi_set_page_type(p,t) do { } while (0)
368#define vmi_check_page_type(p,t) do { } while (0)
369#endif
370
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200371#ifdef CONFIG_HIGHPTE
372static void *vmi_kmap_atomic_pte(struct page *page, enum km_type type)
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800373{
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200374 void *va = kmap_atomic(page, type);
375
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800376 /*
377 * Internally, the VMI ROM must map virtual addresses to physical
378 * addresses for processing MMU updates. By the time MMU updates
379 * are issued, this information is typically already lost.
380 * Fortunately, the VMI provides a cache of mapping slots for active
381 * page tables.
382 *
383 * We use slot zero for the linear mapping of physical memory, and
384 * in HIGHPTE kernels, slot 1 and 2 for KM_PTE0 and KM_PTE1.
385 *
386 * args: SLOT VA COUNT PFN
387 */
388 BUG_ON(type != KM_PTE0 && type != KM_PTE1);
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200389 vmi_ops.set_linear_mapping((type - KM_PTE0)+1, va, 1, page_to_pfn(page));
390
391 return va;
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800392}
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200393#endif
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800394
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700395static void vmi_allocate_pte(struct mm_struct *mm, u32 pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100396{
397 vmi_set_page_type(pfn, VMI_PAGE_L1);
398 vmi_ops.allocate_page(pfn, VMI_PAGE_L1, 0, 0, 0);
399}
400
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700401static void vmi_allocate_pmd(struct mm_struct *mm, u32 pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100402{
403 /*
404 * This call comes in very early, before mem_map is setup.
405 * It is called only for swapper_pg_dir, which already has
406 * data on it.
407 */
408 vmi_set_page_type(pfn, VMI_PAGE_L2);
409 vmi_ops.allocate_page(pfn, VMI_PAGE_L2, 0, 0, 0);
410}
411
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700412static void vmi_allocate_pmd_clone(u32 pfn, u32 clonepfn, u32 start, u32 count)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100413{
414 vmi_set_page_type(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE);
415 vmi_check_page_type(clonepfn, VMI_PAGE_L2);
416 vmi_ops.allocate_page(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE, clonepfn, start, count);
417}
418
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700419static void vmi_release_pte(u32 pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100420{
421 vmi_ops.release_page(pfn, VMI_PAGE_L1);
422 vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
423}
424
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700425static void vmi_release_pmd(u32 pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100426{
427 vmi_ops.release_page(pfn, VMI_PAGE_L2);
428 vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
429}
430
431/*
432 * Helper macros for MMU update flags. We can defer updates until a flush
433 * or page invalidation only if the update is to the current address space
434 * (otherwise, there is no flush). We must check against init_mm, since
435 * this could be a kernel update, which usually passes init_mm, although
436 * sometimes this check can be skipped if we know the particular function
437 * is only called on user mode PTEs. We could change the kernel to pass
438 * current->active_mm here, but in particular, I was unsure if changing
439 * mm/highmem.c to do this would still be correct on other architectures.
440 */
441#define is_current_as(mm, mustbeuser) ((mm) == current->active_mm || \
442 (!mustbeuser && (mm) == &init_mm))
443#define vmi_flags_addr(mm, addr, level, user) \
444 ((level) | (is_current_as(mm, user) ? \
445 (VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
446#define vmi_flags_addr_defer(mm, addr, level, user) \
447 ((level) | (is_current_as(mm, user) ? \
448 (VMI_PAGE_DEFER | VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
449
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200450static void vmi_update_pte(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100451{
452 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
453 vmi_ops.update_pte(ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
454}
455
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200456static void vmi_update_pte_defer(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100457{
458 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
459 vmi_ops.update_pte(ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 0));
460}
461
462static void vmi_set_pte(pte_t *ptep, pte_t pte)
463{
464 /* XXX because of set_pmd_pte, this can be called on PT or PD layers */
465 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE | VMI_PAGE_PD);
466 vmi_ops.set_pte(pte, ptep, VMI_PAGE_PT);
467}
468
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200469static void vmi_set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100470{
471 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
472 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
473}
474
475static void vmi_set_pmd(pmd_t *pmdp, pmd_t pmdval)
476{
477#ifdef CONFIG_X86_PAE
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100478 const pte_t pte = { .pte = pmdval.pmd };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100479 vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PMD);
480#else
481 const pte_t pte = { pmdval.pud.pgd.pgd };
482 vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PGD);
483#endif
484 vmi_ops.set_pte(pte, (pte_t *)pmdp, VMI_PAGE_PD);
485}
486
487#ifdef CONFIG_X86_PAE
488
489static void vmi_set_pte_atomic(pte_t *ptep, pte_t pteval)
490{
491 /*
492 * XXX This is called from set_pmd_pte, but at both PT
493 * and PD layers so the VMI_PAGE_PT flag is wrong. But
494 * it is only called for large page mapping changes,
495 * the Xen backend, doesn't support large pages, and the
496 * ESX backend doesn't depend on the flag.
497 */
498 set_64bit((unsigned long long *)ptep,pte_val(pteval));
499 vmi_ops.update_pte(ptep, VMI_PAGE_PT);
500}
501
502static void vmi_set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
503{
504 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
505 vmi_ops.set_pte(pte, ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 1));
506}
507
508static void vmi_set_pud(pud_t *pudp, pud_t pudval)
509{
510 /* Um, eww */
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100511 const pte_t pte = { .pte = pudval.pgd.pgd };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100512 vmi_check_page_type(__pa(pudp) >> PAGE_SHIFT, VMI_PAGE_PGD);
513 vmi_ops.set_pte(pte, (pte_t *)pudp, VMI_PAGE_PDP);
514}
515
516static void vmi_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
517{
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100518 const pte_t pte = { .pte = 0 };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100519 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
520 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
521}
522
Adrian Bunk8eb68fa2007-05-02 19:27:09 +0200523static void vmi_pmd_clear(pmd_t *pmd)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100524{
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100525 const pte_t pte = { .pte = 0 };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100526 vmi_check_page_type(__pa(pmd) >> PAGE_SHIFT, VMI_PAGE_PMD);
527 vmi_ops.set_pte(pte, (pte_t *)pmd, VMI_PAGE_PD);
528}
529#endif
530
531#ifdef CONFIG_SMP
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800532static void __devinit
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100533vmi_startup_ipi_hook(int phys_apicid, unsigned long start_eip,
534 unsigned long start_esp)
535{
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800536 struct vmi_ap_state ap;
537
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100538 /* Default everything to zero. This is fine for most GPRs. */
539 memset(&ap, 0, sizeof(struct vmi_ap_state));
540
541 ap.gdtr_limit = GDT_SIZE - 1;
542 ap.gdtr_base = (unsigned long) get_cpu_gdt_table(phys_apicid);
543
544 ap.idtr_limit = IDT_ENTRIES * 8 - 1;
545 ap.idtr_base = (unsigned long) idt_table;
546
547 ap.ldtr = 0;
548
549 ap.cs = __KERNEL_CS;
550 ap.eip = (unsigned long) start_eip;
551 ap.ss = __KERNEL_DS;
552 ap.esp = (unsigned long) start_esp;
553
554 ap.ds = __USER_DS;
555 ap.es = __USER_DS;
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200556 ap.fs = __KERNEL_PERCPU;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100557 ap.gs = 0;
558
559 ap.eflags = 0;
560
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100561#ifdef CONFIG_X86_PAE
562 /* efer should match BSP efer. */
563 if (cpu_has_nx) {
564 unsigned l, h;
565 rdmsr(MSR_EFER, l, h);
566 ap.efer = (unsigned long long) h << 32 | l;
567 }
568#endif
569
570 ap.cr3 = __pa(swapper_pg_dir);
571 /* Protected mode, paging, AM, WP, NE, MP. */
572 ap.cr0 = 0x80050023;
573 ap.cr4 = mmu_cr4_features;
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800574 vmi_ops.set_initial_ap_state((u32)&ap, phys_apicid);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100575}
576#endif
577
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700578static void vmi_enter_lazy_cpu(void)
Zachary Amsden49f19712007-04-08 16:04:01 -0700579{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700580 paravirt_enter_lazy_cpu();
581 vmi_ops.set_lazy_mode(2);
582}
Zachary Amsden49f19712007-04-08 16:04:01 -0700583
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700584static void vmi_enter_lazy_mmu(void)
585{
586 paravirt_enter_lazy_mmu();
587 vmi_ops.set_lazy_mode(1);
588}
Zachary Amsden49f19712007-04-08 16:04:01 -0700589
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700590static void vmi_leave_lazy(void)
591{
592 paravirt_leave_lazy(paravirt_get_lazy_mode());
593 vmi_ops.set_lazy_mode(0);
Zachary Amsden49f19712007-04-08 16:04:01 -0700594}
595
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100596static inline int __init check_vmi_rom(struct vrom_header *rom)
597{
598 struct pci_header *pci;
599 struct pnp_header *pnp;
600 const char *manufacturer = "UNKNOWN";
601 const char *product = "UNKNOWN";
602 const char *license = "unspecified";
603
604 if (rom->rom_signature != 0xaa55)
605 return 0;
606 if (rom->vrom_signature != VMI_SIGNATURE)
607 return 0;
608 if (rom->api_version_maj != VMI_API_REV_MAJOR ||
609 rom->api_version_min+1 < VMI_API_REV_MINOR+1) {
610 printk(KERN_WARNING "VMI: Found mismatched rom version %d.%d\n",
611 rom->api_version_maj,
612 rom->api_version_min);
613 return 0;
614 }
615
616 /*
617 * Relying on the VMI_SIGNATURE field is not 100% safe, so check
618 * the PCI header and device type to make sure this is really a
619 * VMI device.
620 */
621 if (!rom->pci_header_offs) {
622 printk(KERN_WARNING "VMI: ROM does not contain PCI header.\n");
623 return 0;
624 }
625
626 pci = (struct pci_header *)((char *)rom+rom->pci_header_offs);
627 if (pci->vendorID != PCI_VENDOR_ID_VMWARE ||
628 pci->deviceID != PCI_DEVICE_ID_VMWARE_VMI) {
629 /* Allow it to run... anyways, but warn */
630 printk(KERN_WARNING "VMI: ROM from unknown manufacturer\n");
631 }
632
633 if (rom->pnp_header_offs) {
634 pnp = (struct pnp_header *)((char *)rom+rom->pnp_header_offs);
635 if (pnp->manufacturer_offset)
636 manufacturer = (const char *)rom+pnp->manufacturer_offset;
637 if (pnp->product_offset)
638 product = (const char *)rom+pnp->product_offset;
639 }
640
641 if (rom->license_offs)
642 license = (char *)rom+rom->license_offs;
643
644 printk(KERN_INFO "VMI: Found %s %s, API version %d.%d, ROM version %d.%d\n",
645 manufacturer, product,
646 rom->api_version_maj, rom->api_version_min,
647 pci->rom_version_maj, pci->rom_version_min);
648
Andi Kleen302cf932007-03-16 21:07:36 +0100649 /* Don't allow BSD/MIT here for now because we don't want to end up
650 with any binary only shim layers */
651 if (strcmp(license, "GPL") && strcmp(license, "GPL v2")) {
652 printk(KERN_WARNING "VMI: Non GPL license `%s' found for ROM. Not used.\n",
653 license);
654 return 0;
655 }
656
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100657 return 1;
658}
659
660/*
661 * Probe for the VMI option ROM
662 */
663static inline int __init probe_vmi_rom(void)
664{
665 unsigned long base;
666
667 /* VMI ROM is in option ROM area, check signature */
668 for (base = 0xC0000; base < 0xE0000; base += 2048) {
669 struct vrom_header *romstart;
670 romstart = (struct vrom_header *)isa_bus_to_virt(base);
671 if (check_vmi_rom(romstart)) {
672 vmi_rom = romstart;
673 return 1;
674 }
675 }
676 return 0;
677}
678
679/*
680 * VMI setup common to all processors
681 */
682void vmi_bringup(void)
683{
684 /* We must establish the lowmem mapping for MMU ops to work */
Zachary Amsden772205f2007-03-05 00:30:41 -0800685 if (vmi_ops.set_linear_mapping)
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200686 vmi_ops.set_linear_mapping(0, (void *)__PAGE_OFFSET, max_low_pfn, 0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100687}
688
689/*
Zachary Amsden772205f2007-03-05 00:30:41 -0800690 * Return a pointer to a VMI function or NULL if unimplemented
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100691 */
692static void *vmi_get_function(int vmicall)
693{
694 u64 reloc;
695 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
696 reloc = call_vrom_long_func(vmi_rom, get_reloc, vmicall);
697 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);
698 if (rel->type == VMI_RELOCATION_CALL_REL)
699 return (void *)rel->eip;
700 else
Zachary Amsden772205f2007-03-05 00:30:41 -0800701 return NULL;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100702}
703
704/*
705 * Helper macro for making the VMI paravirt-ops fill code readable.
Zachary Amsden772205f2007-03-05 00:30:41 -0800706 * For unimplemented operations, fall back to default, unless nop
707 * is returned by the ROM.
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100708 */
709#define para_fill(opname, vmicall) \
710do { \
711 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
712 VMI_CALL_##vmicall); \
Zachary Amsden0492c372007-04-12 19:28:46 -0700713 if (rel->type == VMI_RELOCATION_CALL_REL) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700714 opname = (void *)rel->eip; \
Zachary Amsden0492c372007-04-12 19:28:46 -0700715 else if (rel->type == VMI_RELOCATION_NOP) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700716 opname = (void *)vmi_nop; \
Zachary Amsden0492c372007-04-12 19:28:46 -0700717 else if (rel->type != VMI_RELOCATION_NONE) \
718 printk(KERN_WARNING "VMI: Unknown relocation " \
719 "type %d for " #vmicall"\n",\
720 rel->type); \
Zachary Amsden772205f2007-03-05 00:30:41 -0800721} while (0)
722
723/*
724 * Helper macro for making the VMI paravirt-ops fill code readable.
725 * For cached operations which do not match the VMI ROM ABI and must
726 * go through a tranlation stub. Ignore NOPs, since it is not clear
727 * a NOP * VMI function corresponds to a NOP paravirt-op when the
728 * functions are not in 1-1 correspondence.
729 */
730#define para_wrap(opname, wrapper, cache, vmicall) \
731do { \
732 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
733 VMI_CALL_##vmicall); \
734 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL); \
735 if (rel->type == VMI_RELOCATION_CALL_REL) { \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700736 opname = wrapper; \
Zachary Amsden772205f2007-03-05 00:30:41 -0800737 vmi_ops.cache = (void *)rel->eip; \
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100738 } \
739} while (0)
740
741/*
742 * Activate the VMI interface and switch into paravirtualized mode
743 */
744static inline int __init activate_vmi(void)
745{
746 short kernel_cs;
747 u64 reloc;
748 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
749
750 if (call_vrom_func(vmi_rom, vmi_init) != 0) {
751 printk(KERN_ERR "VMI ROM failed to initialize!");
752 return 0;
753 }
754 savesegment(cs, kernel_cs);
755
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700756 pv_info.paravirt_enabled = 1;
757 pv_info.kernel_rpl = kernel_cs & SEGMENT_RPL_MASK;
758 pv_info.name = "vmi";
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100759
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700760 pv_init_ops.patch = vmi_patch;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100761
762 /*
763 * Many of these operations are ABI compatible with VMI.
764 * This means we can fill in the paravirt-ops with direct
765 * pointers into the VMI ROM. If the calling convention for
766 * these operations changes, this code needs to be updated.
767 *
768 * Exceptions
769 * CPUID paravirt-op uses pointers, not the native ISA
770 * halt has no VMI equivalent; all VMI halts are "safe"
771 * no MSR support yet - just trap and emulate. VMI uses the
772 * same ABI as the native ISA, but Linux wants exceptions
773 * from bogus MSR read / write handled
774 * rdpmc is not yet used in Linux
775 */
776
Zachary Amsden772205f2007-03-05 00:30:41 -0800777 /* CPUID is special, so very special it gets wrapped like a present */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700778 para_wrap(pv_cpu_ops.cpuid, vmi_cpuid, cpuid, CPUID);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100779
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700780 para_fill(pv_cpu_ops.clts, CLTS);
781 para_fill(pv_cpu_ops.get_debugreg, GetDR);
782 para_fill(pv_cpu_ops.set_debugreg, SetDR);
783 para_fill(pv_cpu_ops.read_cr0, GetCR0);
784 para_fill(pv_mmu_ops.read_cr2, GetCR2);
785 para_fill(pv_mmu_ops.read_cr3, GetCR3);
786 para_fill(pv_cpu_ops.read_cr4, GetCR4);
787 para_fill(pv_cpu_ops.write_cr0, SetCR0);
788 para_fill(pv_mmu_ops.write_cr2, SetCR2);
789 para_fill(pv_mmu_ops.write_cr3, SetCR3);
790 para_fill(pv_cpu_ops.write_cr4, SetCR4);
791 para_fill(pv_irq_ops.save_fl, GetInterruptMask);
792 para_fill(pv_irq_ops.restore_fl, SetInterruptMask);
793 para_fill(pv_irq_ops.irq_disable, DisableInterrupts);
794 para_fill(pv_irq_ops.irq_enable, EnableInterrupts);
Zachary Amsden772205f2007-03-05 00:30:41 -0800795
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700796 para_fill(pv_cpu_ops.wbinvd, WBINVD);
797 para_fill(pv_cpu_ops.read_tsc, RDTSC);
Zachary Amsden772205f2007-03-05 00:30:41 -0800798
799 /* The following we emulate with trap and emulate for now */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100800 /* paravirt_ops.read_msr = vmi_rdmsr */
801 /* paravirt_ops.write_msr = vmi_wrmsr */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100802 /* paravirt_ops.rdpmc = vmi_rdpmc */
803
Zachary Amsden772205f2007-03-05 00:30:41 -0800804 /* TR interface doesn't pass TR value, wrap */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700805 para_wrap(pv_cpu_ops.load_tr_desc, vmi_set_tr, set_tr, SetTR);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100806
807 /* LDT is special, too */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700808 para_wrap(pv_cpu_ops.set_ldt, vmi_set_ldt, _set_ldt, SetLDT);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100809
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700810 para_fill(pv_cpu_ops.load_gdt, SetGDT);
811 para_fill(pv_cpu_ops.load_idt, SetIDT);
812 para_fill(pv_cpu_ops.store_gdt, GetGDT);
813 para_fill(pv_cpu_ops.store_idt, GetIDT);
814 para_fill(pv_cpu_ops.store_tr, GetTR);
815 pv_cpu_ops.load_tls = vmi_load_tls;
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100816 para_wrap(pv_cpu_ops.write_ldt_entry, vmi_write_ldt_entry,
817 write_ldt_entry, WriteLDTEntry);
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100818 para_wrap(pv_cpu_ops.write_gdt_entry, vmi_write_gdt_entry,
819 write_gdt_entry, WriteGDTEntry);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100820 para_wrap(pv_cpu_ops.write_idt_entry, vmi_write_idt_entry,
821 write_idt_entry, WriteIDTEntry);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100822 para_wrap(pv_cpu_ops.load_sp0, vmi_load_sp0, set_kernel_stack, UpdateKernelStack);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700823 para_fill(pv_cpu_ops.set_iopl_mask, SetIOPLMask);
824 para_fill(pv_cpu_ops.io_delay, IODelay);
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700825
826 para_wrap(pv_cpu_ops.lazy_mode.enter, vmi_enter_lazy_cpu,
827 set_lazy_mode, SetLazyMode);
828 para_wrap(pv_cpu_ops.lazy_mode.leave, vmi_leave_lazy,
829 set_lazy_mode, SetLazyMode);
830
831 para_wrap(pv_mmu_ops.lazy_mode.enter, vmi_enter_lazy_mmu,
832 set_lazy_mode, SetLazyMode);
833 para_wrap(pv_mmu_ops.lazy_mode.leave, vmi_leave_lazy,
834 set_lazy_mode, SetLazyMode);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100835
Zachary Amsden772205f2007-03-05 00:30:41 -0800836 /* user and kernel flush are just handled with different flags to FlushTLB */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700837 para_wrap(pv_mmu_ops.flush_tlb_user, vmi_flush_tlb_user, _flush_tlb, FlushTLB);
838 para_wrap(pv_mmu_ops.flush_tlb_kernel, vmi_flush_tlb_kernel, _flush_tlb, FlushTLB);
839 para_fill(pv_mmu_ops.flush_tlb_single, InvalPage);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100840
841 /*
842 * Until a standard flag format can be agreed on, we need to
843 * implement these as wrappers in Linux. Get the VMI ROM
844 * function pointers for the two backend calls.
845 */
846#ifdef CONFIG_X86_PAE
847 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxELong);
848 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxELong);
849#else
850 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxE);
851 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxE);
852#endif
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100853
Zachary Amsden772205f2007-03-05 00:30:41 -0800854 if (vmi_ops.set_pte) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700855 pv_mmu_ops.set_pte = vmi_set_pte;
856 pv_mmu_ops.set_pte_at = vmi_set_pte_at;
857 pv_mmu_ops.set_pmd = vmi_set_pmd;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100858#ifdef CONFIG_X86_PAE
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700859 pv_mmu_ops.set_pte_atomic = vmi_set_pte_atomic;
860 pv_mmu_ops.set_pte_present = vmi_set_pte_present;
861 pv_mmu_ops.set_pud = vmi_set_pud;
862 pv_mmu_ops.pte_clear = vmi_pte_clear;
863 pv_mmu_ops.pmd_clear = vmi_pmd_clear;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100864#endif
Zachary Amsden772205f2007-03-05 00:30:41 -0800865 }
866
867 if (vmi_ops.update_pte) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700868 pv_mmu_ops.pte_update = vmi_update_pte;
869 pv_mmu_ops.pte_update_defer = vmi_update_pte_defer;
Zachary Amsden772205f2007-03-05 00:30:41 -0800870 }
871
872 vmi_ops.allocate_page = vmi_get_function(VMI_CALL_AllocatePage);
873 if (vmi_ops.allocate_page) {
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700874 pv_mmu_ops.alloc_pte = vmi_allocate_pte;
875 pv_mmu_ops.alloc_pmd = vmi_allocate_pmd;
876 pv_mmu_ops.alloc_pmd_clone = vmi_allocate_pmd_clone;
Zachary Amsden772205f2007-03-05 00:30:41 -0800877 }
878
879 vmi_ops.release_page = vmi_get_function(VMI_CALL_ReleasePage);
880 if (vmi_ops.release_page) {
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700881 pv_mmu_ops.release_pte = vmi_release_pte;
882 pv_mmu_ops.release_pmd = vmi_release_pmd;
Zachary Amsden772205f2007-03-05 00:30:41 -0800883 }
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200884
885 /* Set linear is needed in all cases */
886 vmi_ops.set_linear_mapping = vmi_get_function(VMI_CALL_SetLinearMapping);
887#ifdef CONFIG_HIGHPTE
888 if (vmi_ops.set_linear_mapping)
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700889 pv_mmu_ops.kmap_atomic_pte = vmi_kmap_atomic_pte;
Jeremy Fitzhardingea27fe802007-05-02 19:27:15 +0200890#endif
Zachary Amsden772205f2007-03-05 00:30:41 -0800891
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100892 /*
893 * These MUST always be patched. Don't support indirect jumps
894 * through these operations, as the VMI interface may use either
895 * a jump or a call to get to these operations, depending on
896 * the backend. They are performance critical anyway, so requiring
897 * a patch is not a big problem.
898 */
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100899 pv_cpu_ops.irq_enable_syscall_ret = (void *)0xfeedbab0;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700900 pv_cpu_ops.iret = (void *)0xbadbab0;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100901
902#ifdef CONFIG_SMP
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700903 para_wrap(pv_apic_ops.startup_ipi_hook, vmi_startup_ipi_hook, set_initial_ap_state, SetInitialAPState);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100904#endif
905
906#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700907 para_fill(pv_apic_ops.apic_read, APICRead);
908 para_fill(pv_apic_ops.apic_write, APICWrite);
909 para_fill(pv_apic_ops.apic_write_atomic, APICWrite);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100910#endif
911
912 /*
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100913 * Check for VMI timer functionality by probing for a cycle frequency method
914 */
915 reloc = call_vrom_long_func(vmi_rom, get_reloc, VMI_CALL_GetCycleFrequency);
Zachary Amsden772205f2007-03-05 00:30:41 -0800916 if (!disable_vmi_timer && rel->type != VMI_RELOCATION_NONE) {
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100917 vmi_timer_ops.get_cycle_frequency = (void *)rel->eip;
918 vmi_timer_ops.get_cycle_counter =
919 vmi_get_function(VMI_CALL_GetCycleCounter);
920 vmi_timer_ops.get_wallclock =
921 vmi_get_function(VMI_CALL_GetWallclockTime);
922 vmi_timer_ops.wallclock_updated =
923 vmi_get_function(VMI_CALL_WallclockUpdated);
924 vmi_timer_ops.set_alarm = vmi_get_function(VMI_CALL_SetAlarm);
925 vmi_timer_ops.cancel_alarm =
926 vmi_get_function(VMI_CALL_CancelAlarm);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700927 pv_time_ops.time_init = vmi_time_init;
928 pv_time_ops.get_wallclock = vmi_get_wallclock;
929 pv_time_ops.set_wallclock = vmi_set_wallclock;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100930#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700931 pv_apic_ops.setup_boot_clock = vmi_time_bsp_init;
932 pv_apic_ops.setup_secondary_clock = vmi_time_ap_init;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100933#endif
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700934 pv_time_ops.sched_clock = vmi_sched_clock;
935 pv_time_ops.get_cpu_khz = vmi_cpu_khz;
Zachary Amsden772205f2007-03-05 00:30:41 -0800936
937 /* We have true wallclock functions; disable CMOS clock sync */
938 no_sync_cmos_clock = 1;
939 } else {
940 disable_noidle = 1;
941 disable_vmi_timer = 1;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100942 }
Zachary Amsden772205f2007-03-05 00:30:41 -0800943
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700944 para_fill(pv_irq_ops.safe_halt, Halt);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100945
946 /*
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100947 * Alternative instruction rewriting doesn't happen soon enough
948 * to convert VMI_IRET to a call instead of a jump; so we have
949 * to do this before IRQs get reenabled. Fortunately, it is
950 * idempotent.
951 */
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200952 apply_paravirt(__parainstructions, __parainstructions_end);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100953
954 vmi_bringup();
955
956 return 1;
957}
958
959#undef para_fill
960
961void __init vmi_init(void)
962{
963 unsigned long flags;
964
965 if (!vmi_rom)
966 probe_vmi_rom();
967 else
968 check_vmi_rom(vmi_rom);
969
970 /* In case probing for or validating the ROM failed, basil */
971 if (!vmi_rom)
972 return;
973
974 reserve_top_address(-vmi_rom->virtual_top);
975
976 local_irq_save(flags);
977 activate_vmi();
Zachary Amsden7507ba32007-03-05 00:30:34 -0800978
979#ifdef CONFIG_X86_IO_APIC
Zachary Amsden772205f2007-03-05 00:30:41 -0800980 /* This is virtual hardware; timer routing is wired correctly */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100981 no_timer_check = 1;
982#endif
983 local_irq_restore(flags & X86_EFLAGS_IF);
984}
985
986static int __init parse_vmi(char *arg)
987{
988 if (!arg)
989 return -EINVAL;
990
Zachary Amsdeneda08b12007-03-05 00:30:38 -0800991 if (!strcmp(arg, "disable_pge")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100992 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PGE);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100993 disable_pge = 1;
994 } else if (!strcmp(arg, "disable_pse")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100995 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PSE);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100996 disable_pse = 1;
997 } else if (!strcmp(arg, "disable_sep")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100998 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_SEP);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100999 disable_sep = 1;
1000 } else if (!strcmp(arg, "disable_tsc")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001001 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_TSC);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001002 disable_tsc = 1;
1003 } else if (!strcmp(arg, "disable_mtrr")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001004 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_MTRR);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001005 disable_mtrr = 1;
Zachary Amsden772205f2007-03-05 00:30:41 -08001006 } else if (!strcmp(arg, "disable_timer")) {
1007 disable_vmi_timer = 1;
1008 disable_noidle = 1;
Zachary Amsden7507ba32007-03-05 00:30:34 -08001009 } else if (!strcmp(arg, "disable_noidle"))
1010 disable_noidle = 1;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001011 return 0;
1012}
1013
1014early_param("vmi", parse_vmi);