blob: a635b22de25f763e200ac729fa4848dfc8c85514 [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);
H. Peter Anvinfaca6222008-01-30 13:31:02 +010066 void (*set_kernel_stack)(u32 selector, u32 sp0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010067 void (*allocate_page)(u32, u32, u32, u32, u32);
68 void (*release_page)(u32, u32);
69 void (*set_pte)(pte_t, pte_t *, unsigned);
70 void (*update_pte)(pte_t *, unsigned);
Zachary Amsdeneeef9c62007-05-02 19:27:16 +020071 void (*set_linear_mapping)(int, void *, u32, u32);
72 void (*_flush_tlb)(int);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010073 void (*set_initial_ap_state)(int, int);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +010074 void (*halt)(void);
Zachary Amsden49f19712007-04-08 16:04:01 -070075 void (*set_lazy_mode)(int mode);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010076} vmi_ops;
77
Zachary Amsdene0bb8642007-05-02 19:27:16 +020078/* Cached VMI operations */
79struct vmi_timer_ops vmi_timer_ops;
80
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010081/*
82 * VMI patching routines.
83 */
84#define MNEM_CALL 0xe8
85#define MNEM_JMP 0xe9
86#define MNEM_RET 0xc3
87
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010088#define IRQ_PATCH_INT_MASK 0
89#define IRQ_PATCH_DISABLE 5
90
Andi Kleenab144f52007-08-10 22:31:03 +020091static inline void patch_offset(void *insnbuf,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010092 unsigned long ip, unsigned long dest)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010093{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010094 *(unsigned long *)(insnbuf+1) = dest-ip-5;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010095}
96
Andi Kleenab144f52007-08-10 22:31:03 +020097static unsigned patch_internal(int call, unsigned len, void *insnbuf,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010098 unsigned long ip)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010099{
100 u64 reloc;
101 struct vmi_relocation_info *const rel = (struct vmi_relocation_info *)&reloc;
102 reloc = call_vrom_long_func(vmi_rom, get_reloc, call);
103 switch(rel->type) {
104 case VMI_RELOCATION_CALL_REL:
105 BUG_ON(len < 5);
Andi Kleenab144f52007-08-10 22:31:03 +0200106 *(char *)insnbuf = MNEM_CALL;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100107 patch_offset(insnbuf, ip, (unsigned long)rel->eip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100108 return 5;
109
110 case VMI_RELOCATION_JUMP_REL:
111 BUG_ON(len < 5);
Andi Kleenab144f52007-08-10 22:31:03 +0200112 *(char *)insnbuf = MNEM_JMP;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100113 patch_offset(insnbuf, ip, (unsigned long)rel->eip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100114 return 5;
115
116 case VMI_RELOCATION_NOP:
117 /* obliterate the whole thing */
118 return 0;
119
120 case VMI_RELOCATION_NONE:
121 /* leave native code in place */
122 break;
123
124 default:
125 BUG();
126 }
127 return len;
128}
129
130/*
131 * Apply patch if appropriate, return length of new instruction
132 * sequence. The callee does nop padding for us.
133 */
Andi Kleenab144f52007-08-10 22:31:03 +0200134static unsigned vmi_patch(u8 type, u16 clobbers, void *insns,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100135 unsigned long ip, unsigned len)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100136{
137 switch (type) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700138 case PARAVIRT_PATCH(pv_irq_ops.irq_disable):
Andi Kleenab144f52007-08-10 22:31:03 +0200139 return patch_internal(VMI_CALL_DisableInterrupts, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100140 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700141 case PARAVIRT_PATCH(pv_irq_ops.irq_enable):
Andi Kleenab144f52007-08-10 22:31:03 +0200142 return patch_internal(VMI_CALL_EnableInterrupts, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100143 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700144 case PARAVIRT_PATCH(pv_irq_ops.restore_fl):
Andi Kleenab144f52007-08-10 22:31:03 +0200145 return patch_internal(VMI_CALL_SetInterruptMask, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100146 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700147 case PARAVIRT_PATCH(pv_irq_ops.save_fl):
Andi Kleenab144f52007-08-10 22:31:03 +0200148 return patch_internal(VMI_CALL_GetInterruptMask, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100149 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700150 case PARAVIRT_PATCH(pv_cpu_ops.iret):
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100151 return patch_internal(VMI_CALL_IRET, len, insns, ip);
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100152 case PARAVIRT_PATCH(pv_cpu_ops.irq_enable_syscall_ret):
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100153 return patch_internal(VMI_CALL_SYSEXIT, len, insns, ip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100154 default:
155 break;
156 }
157 return len;
158}
159
160/* CPUID has non-C semantics, and paravirt-ops API doesn't match hardware ISA */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100161static void vmi_cpuid(unsigned int *ax, unsigned int *bx,
162 unsigned int *cx, unsigned int *dx)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100163{
164 int override = 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100165 if (*ax == 1)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100166 override = 1;
167 asm volatile ("call *%6"
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100168 : "=a" (*ax),
169 "=b" (*bx),
170 "=c" (*cx),
171 "=d" (*dx)
172 : "0" (*ax), "2" (*cx), "r" (vmi_ops.cpuid));
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100173 if (override) {
174 if (disable_pse)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100175 *dx &= ~X86_FEATURE_PSE;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100176 if (disable_pge)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100177 *dx &= ~X86_FEATURE_PGE;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100178 if (disable_sep)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100179 *dx &= ~X86_FEATURE_SEP;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100180 if (disable_tsc)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100181 *dx &= ~X86_FEATURE_TSC;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100182 if (disable_mtrr)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100183 *dx &= ~X86_FEATURE_MTRR;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100184 }
185}
186
187static inline void vmi_maybe_load_tls(struct desc_struct *gdt, int nr, struct desc_struct *new)
188{
189 if (gdt[nr].a != new->a || gdt[nr].b != new->b)
190 write_gdt_entry(gdt, nr, new->a, new->b);
191}
192
193static void vmi_load_tls(struct thread_struct *t, unsigned int cpu)
194{
195 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
196 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 0, &t->tls_array[0]);
197 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 1, &t->tls_array[1]);
198 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 2, &t->tls_array[2]);
199}
200
201static void vmi_set_ldt(const void *addr, unsigned entries)
202{
203 unsigned cpu = smp_processor_id();
204 u32 low, high;
205
206 pack_descriptor(&low, &high, (unsigned long)addr,
207 entries * sizeof(struct desc_struct) - 1,
208 DESCTYPE_LDT, 0);
209 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, low, high);
210 vmi_ops._set_ldt(entries ? GDT_ENTRY_LDT*sizeof(struct desc_struct) : 0);
211}
212
213static void vmi_set_tr(void)
214{
215 vmi_ops.set_tr(GDT_ENTRY_TSS*sizeof(struct desc_struct));
216}
217
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100218static void vmi_write_idt_entry(gate_desc *dt, int entry, const gate_desc *g)
219{
220 u32 *idt_entry = (u32 *)g;
221 vmi_ops.write_idt_entry(dt, entry, idt_entry[0], idt_entry[2]);
222}
223
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100224static void vmi_load_sp0(struct tss_struct *tss,
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100225 struct thread_struct *thread)
226{
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100227 tss->x86_tss.sp0 = thread->sp0;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100228
229 /* This can only happen when SEP is enabled, no need to test "SEP"arately */
Rusty Russella75c54f2007-05-02 19:27:13 +0200230 if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
231 tss->x86_tss.ss1 = thread->sysenter_cs;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100232 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
233 }
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100234 vmi_ops.set_kernel_stack(__KERNEL_DS, tss->x86_tss.sp0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100235}
236
237static void vmi_flush_tlb_user(void)
238{
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200239 vmi_ops._flush_tlb(VMI_FLUSH_TLB);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100240}
241
242static void vmi_flush_tlb_kernel(void)
243{
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200244 vmi_ops._flush_tlb(VMI_FLUSH_TLB | VMI_FLUSH_GLOBAL);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100245}
246
247/* Stub to do nothing at all; used for delays and unimplemented calls */
248static void vmi_nop(void)
249{
250}
251
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100252#ifdef CONFIG_DEBUG_PAGE_TYPE
253
254#ifdef CONFIG_X86_PAE
255#define MAX_BOOT_PTS (2048+4+1)
256#else
257#define MAX_BOOT_PTS (1024+1)
258#endif
259
260/*
261 * During boot, mem_map is not yet available in paging_init, so stash
262 * all the boot page allocations here.
263 */
264static struct {
265 u32 pfn;
266 int type;
267} boot_page_allocations[MAX_BOOT_PTS];
268static int num_boot_page_allocations;
269static int boot_allocations_applied;
270
271void vmi_apply_boot_page_allocations(void)
272{
273 int i;
274 BUG_ON(!mem_map);
275 for (i = 0; i < num_boot_page_allocations; i++) {
276 struct page *page = pfn_to_page(boot_page_allocations[i].pfn);
277 page->type = boot_page_allocations[i].type;
278 page->type = boot_page_allocations[i].type &
279 ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
280 }
281 boot_allocations_applied = 1;
282}
283
284static void record_page_type(u32 pfn, int type)
285{
286 BUG_ON(num_boot_page_allocations >= MAX_BOOT_PTS);
287 boot_page_allocations[num_boot_page_allocations].pfn = pfn;
288 boot_page_allocations[num_boot_page_allocations].type = type;
289 num_boot_page_allocations++;
290}
291
292static void check_zeroed_page(u32 pfn, int type, struct page *page)
293{
294 u32 *ptr;
295 int i;
296 int limit = PAGE_SIZE / sizeof(int);
297
298 if (page_address(page))
299 ptr = (u32 *)page_address(page);
300 else
301 ptr = (u32 *)__va(pfn << PAGE_SHIFT);
302 /*
303 * When cloning the root in non-PAE mode, only the userspace
304 * pdes need to be zeroed.
305 */
306 if (type & VMI_PAGE_CLONE)
307 limit = USER_PTRS_PER_PGD;
308 for (i = 0; i < limit; i++)
309 BUG_ON(ptr[i]);
310}
311
312/*
313 * We stash the page type into struct page so we can verify the page
314 * types are used properly.
315 */
316static void vmi_set_page_type(u32 pfn, int type)
317{
318 /* PAE can have multiple roots per page - don't track */
319 if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
320 return;
321
322 if (boot_allocations_applied) {
323 struct page *page = pfn_to_page(pfn);
324 if (type != VMI_PAGE_NORMAL)
325 BUG_ON(page->type);
326 else
327 BUG_ON(page->type == VMI_PAGE_NORMAL);
328 page->type = type & ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
329 if (type & VMI_PAGE_ZEROED)
330 check_zeroed_page(pfn, type, page);
331 } else {
332 record_page_type(pfn, type);
333 }
334}
335
336static void vmi_check_page_type(u32 pfn, int type)
337{
338 /* PAE can have multiple roots per page - skip checks */
339 if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
340 return;
341
342 type &= ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
343 if (boot_allocations_applied) {
344 struct page *page = pfn_to_page(pfn);
345 BUG_ON((page->type ^ type) & VMI_PAGE_PAE);
346 BUG_ON(type == VMI_PAGE_NORMAL && page->type);
347 BUG_ON((type & page->type) == 0);
348 }
349}
350#else
351#define vmi_set_page_type(p,t) do { } while (0)
352#define vmi_check_page_type(p,t) do { } while (0)
353#endif
354
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200355#ifdef CONFIG_HIGHPTE
356static void *vmi_kmap_atomic_pte(struct page *page, enum km_type type)
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800357{
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200358 void *va = kmap_atomic(page, type);
359
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800360 /*
361 * Internally, the VMI ROM must map virtual addresses to physical
362 * addresses for processing MMU updates. By the time MMU updates
363 * are issued, this information is typically already lost.
364 * Fortunately, the VMI provides a cache of mapping slots for active
365 * page tables.
366 *
367 * We use slot zero for the linear mapping of physical memory, and
368 * in HIGHPTE kernels, slot 1 and 2 for KM_PTE0 and KM_PTE1.
369 *
370 * args: SLOT VA COUNT PFN
371 */
372 BUG_ON(type != KM_PTE0 && type != KM_PTE1);
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200373 vmi_ops.set_linear_mapping((type - KM_PTE0)+1, va, 1, page_to_pfn(page));
374
375 return va;
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800376}
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200377#endif
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800378
Jeremy Fitzhardingefdb4c332007-07-17 18:37:03 -0700379static void vmi_allocate_pt(struct mm_struct *mm, u32 pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100380{
381 vmi_set_page_type(pfn, VMI_PAGE_L1);
382 vmi_ops.allocate_page(pfn, VMI_PAGE_L1, 0, 0, 0);
383}
384
385static void vmi_allocate_pd(u32 pfn)
386{
387 /*
388 * This call comes in very early, before mem_map is setup.
389 * It is called only for swapper_pg_dir, which already has
390 * data on it.
391 */
392 vmi_set_page_type(pfn, VMI_PAGE_L2);
393 vmi_ops.allocate_page(pfn, VMI_PAGE_L2, 0, 0, 0);
394}
395
396static void vmi_allocate_pd_clone(u32 pfn, u32 clonepfn, u32 start, u32 count)
397{
398 vmi_set_page_type(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE);
399 vmi_check_page_type(clonepfn, VMI_PAGE_L2);
400 vmi_ops.allocate_page(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE, clonepfn, start, count);
401}
402
403static void vmi_release_pt(u32 pfn)
404{
405 vmi_ops.release_page(pfn, VMI_PAGE_L1);
406 vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
407}
408
409static void vmi_release_pd(u32 pfn)
410{
411 vmi_ops.release_page(pfn, VMI_PAGE_L2);
412 vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
413}
414
415/*
416 * Helper macros for MMU update flags. We can defer updates until a flush
417 * or page invalidation only if the update is to the current address space
418 * (otherwise, there is no flush). We must check against init_mm, since
419 * this could be a kernel update, which usually passes init_mm, although
420 * sometimes this check can be skipped if we know the particular function
421 * is only called on user mode PTEs. We could change the kernel to pass
422 * current->active_mm here, but in particular, I was unsure if changing
423 * mm/highmem.c to do this would still be correct on other architectures.
424 */
425#define is_current_as(mm, mustbeuser) ((mm) == current->active_mm || \
426 (!mustbeuser && (mm) == &init_mm))
427#define vmi_flags_addr(mm, addr, level, user) \
428 ((level) | (is_current_as(mm, user) ? \
429 (VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
430#define vmi_flags_addr_defer(mm, addr, level, user) \
431 ((level) | (is_current_as(mm, user) ? \
432 (VMI_PAGE_DEFER | VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
433
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200434static void vmi_update_pte(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100435{
436 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
437 vmi_ops.update_pte(ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
438}
439
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200440static void vmi_update_pte_defer(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100441{
442 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
443 vmi_ops.update_pte(ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 0));
444}
445
446static void vmi_set_pte(pte_t *ptep, pte_t pte)
447{
448 /* XXX because of set_pmd_pte, this can be called on PT or PD layers */
449 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE | VMI_PAGE_PD);
450 vmi_ops.set_pte(pte, ptep, VMI_PAGE_PT);
451}
452
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200453static 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 +0100454{
455 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
456 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
457}
458
459static void vmi_set_pmd(pmd_t *pmdp, pmd_t pmdval)
460{
461#ifdef CONFIG_X86_PAE
462 const pte_t pte = { pmdval.pmd, pmdval.pmd >> 32 };
463 vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PMD);
464#else
465 const pte_t pte = { pmdval.pud.pgd.pgd };
466 vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PGD);
467#endif
468 vmi_ops.set_pte(pte, (pte_t *)pmdp, VMI_PAGE_PD);
469}
470
471#ifdef CONFIG_X86_PAE
472
473static void vmi_set_pte_atomic(pte_t *ptep, pte_t pteval)
474{
475 /*
476 * XXX This is called from set_pmd_pte, but at both PT
477 * and PD layers so the VMI_PAGE_PT flag is wrong. But
478 * it is only called for large page mapping changes,
479 * the Xen backend, doesn't support large pages, and the
480 * ESX backend doesn't depend on the flag.
481 */
482 set_64bit((unsigned long long *)ptep,pte_val(pteval));
483 vmi_ops.update_pte(ptep, VMI_PAGE_PT);
484}
485
486static void vmi_set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
487{
488 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
489 vmi_ops.set_pte(pte, ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 1));
490}
491
492static void vmi_set_pud(pud_t *pudp, pud_t pudval)
493{
494 /* Um, eww */
495 const pte_t pte = { pudval.pgd.pgd, pudval.pgd.pgd >> 32 };
496 vmi_check_page_type(__pa(pudp) >> PAGE_SHIFT, VMI_PAGE_PGD);
497 vmi_ops.set_pte(pte, (pte_t *)pudp, VMI_PAGE_PDP);
498}
499
500static void vmi_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
501{
502 const pte_t pte = { 0 };
503 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
504 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
505}
506
Adrian Bunk8eb68fa2007-05-02 19:27:09 +0200507static void vmi_pmd_clear(pmd_t *pmd)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100508{
509 const pte_t pte = { 0 };
510 vmi_check_page_type(__pa(pmd) >> PAGE_SHIFT, VMI_PAGE_PMD);
511 vmi_ops.set_pte(pte, (pte_t *)pmd, VMI_PAGE_PD);
512}
513#endif
514
515#ifdef CONFIG_SMP
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800516static void __devinit
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100517vmi_startup_ipi_hook(int phys_apicid, unsigned long start_eip,
518 unsigned long start_esp)
519{
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800520 struct vmi_ap_state ap;
521
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100522 /* Default everything to zero. This is fine for most GPRs. */
523 memset(&ap, 0, sizeof(struct vmi_ap_state));
524
525 ap.gdtr_limit = GDT_SIZE - 1;
526 ap.gdtr_base = (unsigned long) get_cpu_gdt_table(phys_apicid);
527
528 ap.idtr_limit = IDT_ENTRIES * 8 - 1;
529 ap.idtr_base = (unsigned long) idt_table;
530
531 ap.ldtr = 0;
532
533 ap.cs = __KERNEL_CS;
534 ap.eip = (unsigned long) start_eip;
535 ap.ss = __KERNEL_DS;
536 ap.esp = (unsigned long) start_esp;
537
538 ap.ds = __USER_DS;
539 ap.es = __USER_DS;
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200540 ap.fs = __KERNEL_PERCPU;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100541 ap.gs = 0;
542
543 ap.eflags = 0;
544
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100545#ifdef CONFIG_X86_PAE
546 /* efer should match BSP efer. */
547 if (cpu_has_nx) {
548 unsigned l, h;
549 rdmsr(MSR_EFER, l, h);
550 ap.efer = (unsigned long long) h << 32 | l;
551 }
552#endif
553
554 ap.cr3 = __pa(swapper_pg_dir);
555 /* Protected mode, paging, AM, WP, NE, MP. */
556 ap.cr0 = 0x80050023;
557 ap.cr4 = mmu_cr4_features;
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800558 vmi_ops.set_initial_ap_state((u32)&ap, phys_apicid);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100559}
560#endif
561
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700562static void vmi_enter_lazy_cpu(void)
Zachary Amsden49f19712007-04-08 16:04:01 -0700563{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700564 paravirt_enter_lazy_cpu();
565 vmi_ops.set_lazy_mode(2);
566}
Zachary Amsden49f19712007-04-08 16:04:01 -0700567
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700568static void vmi_enter_lazy_mmu(void)
569{
570 paravirt_enter_lazy_mmu();
571 vmi_ops.set_lazy_mode(1);
572}
Zachary Amsden49f19712007-04-08 16:04:01 -0700573
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700574static void vmi_leave_lazy(void)
575{
576 paravirt_leave_lazy(paravirt_get_lazy_mode());
577 vmi_ops.set_lazy_mode(0);
Zachary Amsden49f19712007-04-08 16:04:01 -0700578}
579
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100580static inline int __init check_vmi_rom(struct vrom_header *rom)
581{
582 struct pci_header *pci;
583 struct pnp_header *pnp;
584 const char *manufacturer = "UNKNOWN";
585 const char *product = "UNKNOWN";
586 const char *license = "unspecified";
587
588 if (rom->rom_signature != 0xaa55)
589 return 0;
590 if (rom->vrom_signature != VMI_SIGNATURE)
591 return 0;
592 if (rom->api_version_maj != VMI_API_REV_MAJOR ||
593 rom->api_version_min+1 < VMI_API_REV_MINOR+1) {
594 printk(KERN_WARNING "VMI: Found mismatched rom version %d.%d\n",
595 rom->api_version_maj,
596 rom->api_version_min);
597 return 0;
598 }
599
600 /*
601 * Relying on the VMI_SIGNATURE field is not 100% safe, so check
602 * the PCI header and device type to make sure this is really a
603 * VMI device.
604 */
605 if (!rom->pci_header_offs) {
606 printk(KERN_WARNING "VMI: ROM does not contain PCI header.\n");
607 return 0;
608 }
609
610 pci = (struct pci_header *)((char *)rom+rom->pci_header_offs);
611 if (pci->vendorID != PCI_VENDOR_ID_VMWARE ||
612 pci->deviceID != PCI_DEVICE_ID_VMWARE_VMI) {
613 /* Allow it to run... anyways, but warn */
614 printk(KERN_WARNING "VMI: ROM from unknown manufacturer\n");
615 }
616
617 if (rom->pnp_header_offs) {
618 pnp = (struct pnp_header *)((char *)rom+rom->pnp_header_offs);
619 if (pnp->manufacturer_offset)
620 manufacturer = (const char *)rom+pnp->manufacturer_offset;
621 if (pnp->product_offset)
622 product = (const char *)rom+pnp->product_offset;
623 }
624
625 if (rom->license_offs)
626 license = (char *)rom+rom->license_offs;
627
628 printk(KERN_INFO "VMI: Found %s %s, API version %d.%d, ROM version %d.%d\n",
629 manufacturer, product,
630 rom->api_version_maj, rom->api_version_min,
631 pci->rom_version_maj, pci->rom_version_min);
632
Andi Kleen302cf932007-03-16 21:07:36 +0100633 /* Don't allow BSD/MIT here for now because we don't want to end up
634 with any binary only shim layers */
635 if (strcmp(license, "GPL") && strcmp(license, "GPL v2")) {
636 printk(KERN_WARNING "VMI: Non GPL license `%s' found for ROM. Not used.\n",
637 license);
638 return 0;
639 }
640
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100641 return 1;
642}
643
644/*
645 * Probe for the VMI option ROM
646 */
647static inline int __init probe_vmi_rom(void)
648{
649 unsigned long base;
650
651 /* VMI ROM is in option ROM area, check signature */
652 for (base = 0xC0000; base < 0xE0000; base += 2048) {
653 struct vrom_header *romstart;
654 romstart = (struct vrom_header *)isa_bus_to_virt(base);
655 if (check_vmi_rom(romstart)) {
656 vmi_rom = romstart;
657 return 1;
658 }
659 }
660 return 0;
661}
662
663/*
664 * VMI setup common to all processors
665 */
666void vmi_bringup(void)
667{
668 /* We must establish the lowmem mapping for MMU ops to work */
Zachary Amsden772205f2007-03-05 00:30:41 -0800669 if (vmi_ops.set_linear_mapping)
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200670 vmi_ops.set_linear_mapping(0, (void *)__PAGE_OFFSET, max_low_pfn, 0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100671}
672
673/*
Zachary Amsden772205f2007-03-05 00:30:41 -0800674 * Return a pointer to a VMI function or NULL if unimplemented
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100675 */
676static void *vmi_get_function(int vmicall)
677{
678 u64 reloc;
679 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
680 reloc = call_vrom_long_func(vmi_rom, get_reloc, vmicall);
681 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);
682 if (rel->type == VMI_RELOCATION_CALL_REL)
683 return (void *)rel->eip;
684 else
Zachary Amsden772205f2007-03-05 00:30:41 -0800685 return NULL;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100686}
687
688/*
689 * Helper macro for making the VMI paravirt-ops fill code readable.
Zachary Amsden772205f2007-03-05 00:30:41 -0800690 * For unimplemented operations, fall back to default, unless nop
691 * is returned by the ROM.
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100692 */
693#define para_fill(opname, vmicall) \
694do { \
695 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
696 VMI_CALL_##vmicall); \
Zachary Amsden0492c372007-04-12 19:28:46 -0700697 if (rel->type == VMI_RELOCATION_CALL_REL) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700698 opname = (void *)rel->eip; \
Zachary Amsden0492c372007-04-12 19:28:46 -0700699 else if (rel->type == VMI_RELOCATION_NOP) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700700 opname = (void *)vmi_nop; \
Zachary Amsden0492c372007-04-12 19:28:46 -0700701 else if (rel->type != VMI_RELOCATION_NONE) \
702 printk(KERN_WARNING "VMI: Unknown relocation " \
703 "type %d for " #vmicall"\n",\
704 rel->type); \
Zachary Amsden772205f2007-03-05 00:30:41 -0800705} while (0)
706
707/*
708 * Helper macro for making the VMI paravirt-ops fill code readable.
709 * For cached operations which do not match the VMI ROM ABI and must
710 * go through a tranlation stub. Ignore NOPs, since it is not clear
711 * a NOP * VMI function corresponds to a NOP paravirt-op when the
712 * functions are not in 1-1 correspondence.
713 */
714#define para_wrap(opname, wrapper, cache, vmicall) \
715do { \
716 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
717 VMI_CALL_##vmicall); \
718 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL); \
719 if (rel->type == VMI_RELOCATION_CALL_REL) { \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700720 opname = wrapper; \
Zachary Amsden772205f2007-03-05 00:30:41 -0800721 vmi_ops.cache = (void *)rel->eip; \
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100722 } \
723} while (0)
724
725/*
726 * Activate the VMI interface and switch into paravirtualized mode
727 */
728static inline int __init activate_vmi(void)
729{
730 short kernel_cs;
731 u64 reloc;
732 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
733
734 if (call_vrom_func(vmi_rom, vmi_init) != 0) {
735 printk(KERN_ERR "VMI ROM failed to initialize!");
736 return 0;
737 }
738 savesegment(cs, kernel_cs);
739
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700740 pv_info.paravirt_enabled = 1;
741 pv_info.kernel_rpl = kernel_cs & SEGMENT_RPL_MASK;
742 pv_info.name = "vmi";
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100743
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700744 pv_init_ops.patch = vmi_patch;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100745
746 /*
747 * Many of these operations are ABI compatible with VMI.
748 * This means we can fill in the paravirt-ops with direct
749 * pointers into the VMI ROM. If the calling convention for
750 * these operations changes, this code needs to be updated.
751 *
752 * Exceptions
753 * CPUID paravirt-op uses pointers, not the native ISA
754 * halt has no VMI equivalent; all VMI halts are "safe"
755 * no MSR support yet - just trap and emulate. VMI uses the
756 * same ABI as the native ISA, but Linux wants exceptions
757 * from bogus MSR read / write handled
758 * rdpmc is not yet used in Linux
759 */
760
Zachary Amsden772205f2007-03-05 00:30:41 -0800761 /* CPUID is special, so very special it gets wrapped like a present */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700762 para_wrap(pv_cpu_ops.cpuid, vmi_cpuid, cpuid, CPUID);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100763
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700764 para_fill(pv_cpu_ops.clts, CLTS);
765 para_fill(pv_cpu_ops.get_debugreg, GetDR);
766 para_fill(pv_cpu_ops.set_debugreg, SetDR);
767 para_fill(pv_cpu_ops.read_cr0, GetCR0);
768 para_fill(pv_mmu_ops.read_cr2, GetCR2);
769 para_fill(pv_mmu_ops.read_cr3, GetCR3);
770 para_fill(pv_cpu_ops.read_cr4, GetCR4);
771 para_fill(pv_cpu_ops.write_cr0, SetCR0);
772 para_fill(pv_mmu_ops.write_cr2, SetCR2);
773 para_fill(pv_mmu_ops.write_cr3, SetCR3);
774 para_fill(pv_cpu_ops.write_cr4, SetCR4);
775 para_fill(pv_irq_ops.save_fl, GetInterruptMask);
776 para_fill(pv_irq_ops.restore_fl, SetInterruptMask);
777 para_fill(pv_irq_ops.irq_disable, DisableInterrupts);
778 para_fill(pv_irq_ops.irq_enable, EnableInterrupts);
Zachary Amsden772205f2007-03-05 00:30:41 -0800779
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700780 para_fill(pv_cpu_ops.wbinvd, WBINVD);
781 para_fill(pv_cpu_ops.read_tsc, RDTSC);
Zachary Amsden772205f2007-03-05 00:30:41 -0800782
783 /* The following we emulate with trap and emulate for now */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100784 /* paravirt_ops.read_msr = vmi_rdmsr */
785 /* paravirt_ops.write_msr = vmi_wrmsr */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100786 /* paravirt_ops.rdpmc = vmi_rdpmc */
787
Zachary Amsden772205f2007-03-05 00:30:41 -0800788 /* TR interface doesn't pass TR value, wrap */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700789 para_wrap(pv_cpu_ops.load_tr_desc, vmi_set_tr, set_tr, SetTR);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100790
791 /* LDT is special, too */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700792 para_wrap(pv_cpu_ops.set_ldt, vmi_set_ldt, _set_ldt, SetLDT);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100793
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700794 para_fill(pv_cpu_ops.load_gdt, SetGDT);
795 para_fill(pv_cpu_ops.load_idt, SetIDT);
796 para_fill(pv_cpu_ops.store_gdt, GetGDT);
797 para_fill(pv_cpu_ops.store_idt, GetIDT);
798 para_fill(pv_cpu_ops.store_tr, GetTR);
799 pv_cpu_ops.load_tls = vmi_load_tls;
800 para_fill(pv_cpu_ops.write_ldt_entry, WriteLDTEntry);
801 para_fill(pv_cpu_ops.write_gdt_entry, WriteGDTEntry);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100802 para_wrap(pv_cpu_ops.write_idt_entry, vmi_write_idt_entry,
803 write_idt_entry, WriteIDTEntry);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100804 para_wrap(pv_cpu_ops.load_sp0, vmi_load_sp0, set_kernel_stack, UpdateKernelStack);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700805 para_fill(pv_cpu_ops.set_iopl_mask, SetIOPLMask);
806 para_fill(pv_cpu_ops.io_delay, IODelay);
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700807
808 para_wrap(pv_cpu_ops.lazy_mode.enter, vmi_enter_lazy_cpu,
809 set_lazy_mode, SetLazyMode);
810 para_wrap(pv_cpu_ops.lazy_mode.leave, vmi_leave_lazy,
811 set_lazy_mode, SetLazyMode);
812
813 para_wrap(pv_mmu_ops.lazy_mode.enter, vmi_enter_lazy_mmu,
814 set_lazy_mode, SetLazyMode);
815 para_wrap(pv_mmu_ops.lazy_mode.leave, vmi_leave_lazy,
816 set_lazy_mode, SetLazyMode);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100817
Zachary Amsden772205f2007-03-05 00:30:41 -0800818 /* user and kernel flush are just handled with different flags to FlushTLB */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700819 para_wrap(pv_mmu_ops.flush_tlb_user, vmi_flush_tlb_user, _flush_tlb, FlushTLB);
820 para_wrap(pv_mmu_ops.flush_tlb_kernel, vmi_flush_tlb_kernel, _flush_tlb, FlushTLB);
821 para_fill(pv_mmu_ops.flush_tlb_single, InvalPage);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100822
823 /*
824 * Until a standard flag format can be agreed on, we need to
825 * implement these as wrappers in Linux. Get the VMI ROM
826 * function pointers for the two backend calls.
827 */
828#ifdef CONFIG_X86_PAE
829 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxELong);
830 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxELong);
831#else
832 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxE);
833 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxE);
834#endif
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100835
Zachary Amsden772205f2007-03-05 00:30:41 -0800836 if (vmi_ops.set_pte) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700837 pv_mmu_ops.set_pte = vmi_set_pte;
838 pv_mmu_ops.set_pte_at = vmi_set_pte_at;
839 pv_mmu_ops.set_pmd = vmi_set_pmd;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100840#ifdef CONFIG_X86_PAE
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700841 pv_mmu_ops.set_pte_atomic = vmi_set_pte_atomic;
842 pv_mmu_ops.set_pte_present = vmi_set_pte_present;
843 pv_mmu_ops.set_pud = vmi_set_pud;
844 pv_mmu_ops.pte_clear = vmi_pte_clear;
845 pv_mmu_ops.pmd_clear = vmi_pmd_clear;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100846#endif
Zachary Amsden772205f2007-03-05 00:30:41 -0800847 }
848
849 if (vmi_ops.update_pte) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700850 pv_mmu_ops.pte_update = vmi_update_pte;
851 pv_mmu_ops.pte_update_defer = vmi_update_pte_defer;
Zachary Amsden772205f2007-03-05 00:30:41 -0800852 }
853
854 vmi_ops.allocate_page = vmi_get_function(VMI_CALL_AllocatePage);
855 if (vmi_ops.allocate_page) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700856 pv_mmu_ops.alloc_pt = vmi_allocate_pt;
857 pv_mmu_ops.alloc_pd = vmi_allocate_pd;
858 pv_mmu_ops.alloc_pd_clone = vmi_allocate_pd_clone;
Zachary Amsden772205f2007-03-05 00:30:41 -0800859 }
860
861 vmi_ops.release_page = vmi_get_function(VMI_CALL_ReleasePage);
862 if (vmi_ops.release_page) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700863 pv_mmu_ops.release_pt = vmi_release_pt;
864 pv_mmu_ops.release_pd = vmi_release_pd;
Zachary Amsden772205f2007-03-05 00:30:41 -0800865 }
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200866
867 /* Set linear is needed in all cases */
868 vmi_ops.set_linear_mapping = vmi_get_function(VMI_CALL_SetLinearMapping);
869#ifdef CONFIG_HIGHPTE
870 if (vmi_ops.set_linear_mapping)
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700871 pv_mmu_ops.kmap_atomic_pte = vmi_kmap_atomic_pte;
Jeremy Fitzhardingea27fe802007-05-02 19:27:15 +0200872#endif
Zachary Amsden772205f2007-03-05 00:30:41 -0800873
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100874 /*
875 * These MUST always be patched. Don't support indirect jumps
876 * through these operations, as the VMI interface may use either
877 * a jump or a call to get to these operations, depending on
878 * the backend. They are performance critical anyway, so requiring
879 * a patch is not a big problem.
880 */
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100881 pv_cpu_ops.irq_enable_syscall_ret = (void *)0xfeedbab0;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700882 pv_cpu_ops.iret = (void *)0xbadbab0;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100883
884#ifdef CONFIG_SMP
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700885 para_wrap(pv_apic_ops.startup_ipi_hook, vmi_startup_ipi_hook, set_initial_ap_state, SetInitialAPState);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100886#endif
887
888#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700889 para_fill(pv_apic_ops.apic_read, APICRead);
890 para_fill(pv_apic_ops.apic_write, APICWrite);
891 para_fill(pv_apic_ops.apic_write_atomic, APICWrite);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100892#endif
893
894 /*
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100895 * Check for VMI timer functionality by probing for a cycle frequency method
896 */
897 reloc = call_vrom_long_func(vmi_rom, get_reloc, VMI_CALL_GetCycleFrequency);
Zachary Amsden772205f2007-03-05 00:30:41 -0800898 if (!disable_vmi_timer && rel->type != VMI_RELOCATION_NONE) {
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100899 vmi_timer_ops.get_cycle_frequency = (void *)rel->eip;
900 vmi_timer_ops.get_cycle_counter =
901 vmi_get_function(VMI_CALL_GetCycleCounter);
902 vmi_timer_ops.get_wallclock =
903 vmi_get_function(VMI_CALL_GetWallclockTime);
904 vmi_timer_ops.wallclock_updated =
905 vmi_get_function(VMI_CALL_WallclockUpdated);
906 vmi_timer_ops.set_alarm = vmi_get_function(VMI_CALL_SetAlarm);
907 vmi_timer_ops.cancel_alarm =
908 vmi_get_function(VMI_CALL_CancelAlarm);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700909 pv_time_ops.time_init = vmi_time_init;
910 pv_time_ops.get_wallclock = vmi_get_wallclock;
911 pv_time_ops.set_wallclock = vmi_set_wallclock;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100912#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700913 pv_apic_ops.setup_boot_clock = vmi_time_bsp_init;
914 pv_apic_ops.setup_secondary_clock = vmi_time_ap_init;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100915#endif
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700916 pv_time_ops.sched_clock = vmi_sched_clock;
917 pv_time_ops.get_cpu_khz = vmi_cpu_khz;
Zachary Amsden772205f2007-03-05 00:30:41 -0800918
919 /* We have true wallclock functions; disable CMOS clock sync */
920 no_sync_cmos_clock = 1;
921 } else {
922 disable_noidle = 1;
923 disable_vmi_timer = 1;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100924 }
Zachary Amsden772205f2007-03-05 00:30:41 -0800925
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700926 para_fill(pv_irq_ops.safe_halt, Halt);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100927
928 /*
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100929 * Alternative instruction rewriting doesn't happen soon enough
930 * to convert VMI_IRET to a call instead of a jump; so we have
931 * to do this before IRQs get reenabled. Fortunately, it is
932 * idempotent.
933 */
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200934 apply_paravirt(__parainstructions, __parainstructions_end);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100935
936 vmi_bringup();
937
938 return 1;
939}
940
941#undef para_fill
942
943void __init vmi_init(void)
944{
945 unsigned long flags;
946
947 if (!vmi_rom)
948 probe_vmi_rom();
949 else
950 check_vmi_rom(vmi_rom);
951
952 /* In case probing for or validating the ROM failed, basil */
953 if (!vmi_rom)
954 return;
955
956 reserve_top_address(-vmi_rom->virtual_top);
957
958 local_irq_save(flags);
959 activate_vmi();
Zachary Amsden7507ba32007-03-05 00:30:34 -0800960
961#ifdef CONFIG_X86_IO_APIC
Zachary Amsden772205f2007-03-05 00:30:41 -0800962 /* This is virtual hardware; timer routing is wired correctly */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100963 no_timer_check = 1;
964#endif
965 local_irq_restore(flags & X86_EFLAGS_IF);
966}
967
968static int __init parse_vmi(char *arg)
969{
970 if (!arg)
971 return -EINVAL;
972
Zachary Amsdeneda08b12007-03-05 00:30:38 -0800973 if (!strcmp(arg, "disable_pge")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100974 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PGE);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100975 disable_pge = 1;
976 } else if (!strcmp(arg, "disable_pse")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100977 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PSE);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100978 disable_pse = 1;
979 } else if (!strcmp(arg, "disable_sep")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100980 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_SEP);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100981 disable_sep = 1;
982 } else if (!strcmp(arg, "disable_tsc")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100983 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_TSC);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100984 disable_tsc = 1;
985 } else if (!strcmp(arg, "disable_mtrr")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100986 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_MTRR);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100987 disable_mtrr = 1;
Zachary Amsden772205f2007-03-05 00:30:41 -0800988 } else if (!strcmp(arg, "disable_timer")) {
989 disable_vmi_timer = 1;
990 disable_noidle = 1;
Zachary Amsden7507ba32007-03-05 00:30:34 -0800991 } else if (!strcmp(arg, "disable_noidle"))
992 disable_noidle = 1;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100993 return 0;
994}
995
996early_param("vmi", parse_vmi);