blob: 22fd6577156a9ac81453792185e3cc0a317d96d2 [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>
Alok Kataria31343d82008-08-08 12:15:57 -070040#include <asm/setup.h>
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010041
42/* Convenient for calling VMI functions indirectly in the ROM */
43typedef u32 __attribute__((regparm(1))) (VROMFUNC)(void);
44typedef u64 __attribute__((regparm(2))) (VROMLONGFUNC)(int);
45
46#define call_vrom_func(rom,func) \
47 (((VROMFUNC *)(rom->func))())
48
49#define call_vrom_long_func(rom,func,arg) \
50 (((VROMLONGFUNC *)(rom->func)) (arg))
51
52static struct vrom_header *vmi_rom;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010053static int disable_pge;
54static int disable_pse;
55static int disable_sep;
56static int disable_tsc;
57static int disable_mtrr;
Zachary Amsden7507ba32007-03-05 00:30:34 -080058static int disable_noidle;
Zachary Amsden772205f2007-03-05 00:30:41 -080059static int disable_vmi_timer;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010060
61/* Cached VMI operations */
Adrian Bunk30a15282007-05-02 19:27:08 +020062static struct {
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010063 void (*cpuid)(void /* non-c */);
64 void (*_set_ldt)(u32 selector);
65 void (*set_tr)(u32 selector);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +010066 void (*write_idt_entry)(struct desc_struct *, int, u32, u32);
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +010067 void (*write_gdt_entry)(struct desc_struct *, int, u32, u32);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +010068 void (*write_ldt_entry)(struct desc_struct *, int, u32, u32);
H. Peter Anvinfaca6222008-01-30 13:31:02 +010069 void (*set_kernel_stack)(u32 selector, u32 sp0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010070 void (*allocate_page)(u32, u32, u32, u32, u32);
71 void (*release_page)(u32, u32);
72 void (*set_pte)(pte_t, pte_t *, unsigned);
73 void (*update_pte)(pte_t *, unsigned);
Zachary Amsdeneeef9c62007-05-02 19:27:16 +020074 void (*set_linear_mapping)(int, void *, u32, u32);
75 void (*_flush_tlb)(int);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010076 void (*set_initial_ap_state)(int, int);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +010077 void (*halt)(void);
Zachary Amsden49f19712007-04-08 16:04:01 -070078 void (*set_lazy_mode)(int mode);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010079} vmi_ops;
80
Zachary Amsdene0bb8642007-05-02 19:27:16 +020081/* Cached VMI operations */
82struct vmi_timer_ops vmi_timer_ops;
83
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010084/*
85 * VMI patching routines.
86 */
87#define MNEM_CALL 0xe8
88#define MNEM_JMP 0xe9
89#define MNEM_RET 0xc3
90
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010091#define IRQ_PATCH_INT_MASK 0
92#define IRQ_PATCH_DISABLE 5
93
Andi Kleenab144f52007-08-10 22:31:03 +020094static inline void patch_offset(void *insnbuf,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010095 unsigned long ip, unsigned long dest)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010096{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010097 *(unsigned long *)(insnbuf+1) = dest-ip-5;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010098}
99
Andi Kleenab144f52007-08-10 22:31:03 +0200100static unsigned patch_internal(int call, unsigned len, void *insnbuf,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100101 unsigned long ip)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100102{
103 u64 reloc;
104 struct vmi_relocation_info *const rel = (struct vmi_relocation_info *)&reloc;
105 reloc = call_vrom_long_func(vmi_rom, get_reloc, call);
106 switch(rel->type) {
107 case VMI_RELOCATION_CALL_REL:
108 BUG_ON(len < 5);
Andi Kleenab144f52007-08-10 22:31:03 +0200109 *(char *)insnbuf = MNEM_CALL;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100110 patch_offset(insnbuf, ip, (unsigned long)rel->eip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100111 return 5;
112
113 case VMI_RELOCATION_JUMP_REL:
114 BUG_ON(len < 5);
Andi Kleenab144f52007-08-10 22:31:03 +0200115 *(char *)insnbuf = MNEM_JMP;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100116 patch_offset(insnbuf, ip, (unsigned long)rel->eip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100117 return 5;
118
119 case VMI_RELOCATION_NOP:
120 /* obliterate the whole thing */
121 return 0;
122
123 case VMI_RELOCATION_NONE:
124 /* leave native code in place */
125 break;
126
127 default:
128 BUG();
129 }
130 return len;
131}
132
133/*
134 * Apply patch if appropriate, return length of new instruction
135 * sequence. The callee does nop padding for us.
136 */
Andi Kleenab144f52007-08-10 22:31:03 +0200137static unsigned vmi_patch(u8 type, u16 clobbers, void *insns,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100138 unsigned long ip, unsigned len)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100139{
140 switch (type) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700141 case PARAVIRT_PATCH(pv_irq_ops.irq_disable):
Andi Kleenab144f52007-08-10 22:31:03 +0200142 return patch_internal(VMI_CALL_DisableInterrupts, 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.irq_enable):
Andi Kleenab144f52007-08-10 22:31:03 +0200145 return patch_internal(VMI_CALL_EnableInterrupts, 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.restore_fl):
Andi Kleenab144f52007-08-10 22:31:03 +0200148 return patch_internal(VMI_CALL_SetInterruptMask, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100149 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700150 case PARAVIRT_PATCH(pv_irq_ops.save_fl):
Andi Kleenab144f52007-08-10 22:31:03 +0200151 return patch_internal(VMI_CALL_GetInterruptMask, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100152 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700153 case PARAVIRT_PATCH(pv_cpu_ops.iret):
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100154 return patch_internal(VMI_CALL_IRET, len, insns, ip);
Jeremy Fitzhardinged75cd222008-06-25 00:19:26 -0400155 case PARAVIRT_PATCH(pv_cpu_ops.irq_enable_sysexit):
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100156 return patch_internal(VMI_CALL_SYSEXIT, len, insns, ip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100157 default:
158 break;
159 }
160 return len;
161}
162
163/* CPUID has non-C semantics, and paravirt-ops API doesn't match hardware ISA */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100164static void vmi_cpuid(unsigned int *ax, unsigned int *bx,
165 unsigned int *cx, unsigned int *dx)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100166{
167 int override = 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100168 if (*ax == 1)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100169 override = 1;
170 asm volatile ("call *%6"
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100171 : "=a" (*ax),
172 "=b" (*bx),
173 "=c" (*cx),
174 "=d" (*dx)
175 : "0" (*ax), "2" (*cx), "r" (vmi_ops.cpuid));
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100176 if (override) {
177 if (disable_pse)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100178 *dx &= ~X86_FEATURE_PSE;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100179 if (disable_pge)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100180 *dx &= ~X86_FEATURE_PGE;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100181 if (disable_sep)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100182 *dx &= ~X86_FEATURE_SEP;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100183 if (disable_tsc)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100184 *dx &= ~X86_FEATURE_TSC;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100185 if (disable_mtrr)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100186 *dx &= ~X86_FEATURE_MTRR;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100187 }
188}
189
190static inline void vmi_maybe_load_tls(struct desc_struct *gdt, int nr, struct desc_struct *new)
191{
192 if (gdt[nr].a != new->a || gdt[nr].b != new->b)
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100193 write_gdt_entry(gdt, nr, new, 0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100194}
195
196static void vmi_load_tls(struct thread_struct *t, unsigned int cpu)
197{
198 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
199 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 0, &t->tls_array[0]);
200 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 1, &t->tls_array[1]);
201 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 2, &t->tls_array[2]);
202}
203
204static void vmi_set_ldt(const void *addr, unsigned entries)
205{
206 unsigned cpu = smp_processor_id();
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100207 struct desc_struct desc;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100208
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100209 pack_descriptor(&desc, (unsigned long)addr,
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100210 entries * sizeof(struct desc_struct) - 1,
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100211 DESC_LDT, 0);
212 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, &desc, DESC_LDT);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100213 vmi_ops._set_ldt(entries ? GDT_ENTRY_LDT*sizeof(struct desc_struct) : 0);
214}
215
216static void vmi_set_tr(void)
217{
218 vmi_ops.set_tr(GDT_ENTRY_TSS*sizeof(struct desc_struct));
219}
220
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100221static void vmi_write_idt_entry(gate_desc *dt, int entry, const gate_desc *g)
222{
223 u32 *idt_entry = (u32 *)g;
Ingo Molnar262d5ee2008-02-04 16:47:54 +0100224 vmi_ops.write_idt_entry(dt, entry, idt_entry[0], idt_entry[1]);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100225}
226
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100227static void vmi_write_gdt_entry(struct desc_struct *dt, int entry,
228 const void *desc, int type)
229{
230 u32 *gdt_entry = (u32 *)desc;
Ingo Molnar262d5ee2008-02-04 16:47:54 +0100231 vmi_ops.write_gdt_entry(dt, entry, gdt_entry[0], gdt_entry[1]);
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100232}
233
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100234static void vmi_write_ldt_entry(struct desc_struct *dt, int entry,
235 const void *desc)
236{
237 u32 *ldt_entry = (u32 *)desc;
Zachary Amsdende599852008-09-30 11:02:12 -0700238 vmi_ops.write_ldt_entry(dt, entry, ldt_entry[0], ldt_entry[1]);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100239}
240
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100241static void vmi_load_sp0(struct tss_struct *tss,
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100242 struct thread_struct *thread)
243{
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100244 tss->x86_tss.sp0 = thread->sp0;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100245
246 /* This can only happen when SEP is enabled, no need to test "SEP"arately */
Rusty Russella75c54f2007-05-02 19:27:13 +0200247 if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
248 tss->x86_tss.ss1 = thread->sysenter_cs;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100249 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
250 }
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100251 vmi_ops.set_kernel_stack(__KERNEL_DS, tss->x86_tss.sp0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100252}
253
254static void vmi_flush_tlb_user(void)
255{
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200256 vmi_ops._flush_tlb(VMI_FLUSH_TLB);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100257}
258
259static void vmi_flush_tlb_kernel(void)
260{
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200261 vmi_ops._flush_tlb(VMI_FLUSH_TLB | VMI_FLUSH_GLOBAL);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100262}
263
264/* Stub to do nothing at all; used for delays and unimplemented calls */
265static void vmi_nop(void)
266{
267}
268
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100269#ifdef CONFIG_DEBUG_PAGE_TYPE
270
271#ifdef CONFIG_X86_PAE
272#define MAX_BOOT_PTS (2048+4+1)
273#else
274#define MAX_BOOT_PTS (1024+1)
275#endif
276
277/*
278 * During boot, mem_map is not yet available in paging_init, so stash
279 * all the boot page allocations here.
280 */
281static struct {
282 u32 pfn;
283 int type;
284} boot_page_allocations[MAX_BOOT_PTS];
285static int num_boot_page_allocations;
286static int boot_allocations_applied;
287
288void vmi_apply_boot_page_allocations(void)
289{
290 int i;
291 BUG_ON(!mem_map);
292 for (i = 0; i < num_boot_page_allocations; i++) {
293 struct page *page = pfn_to_page(boot_page_allocations[i].pfn);
294 page->type = boot_page_allocations[i].type;
295 page->type = boot_page_allocations[i].type &
296 ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
297 }
298 boot_allocations_applied = 1;
299}
300
301static void record_page_type(u32 pfn, int type)
302{
303 BUG_ON(num_boot_page_allocations >= MAX_BOOT_PTS);
304 boot_page_allocations[num_boot_page_allocations].pfn = pfn;
305 boot_page_allocations[num_boot_page_allocations].type = type;
306 num_boot_page_allocations++;
307}
308
309static void check_zeroed_page(u32 pfn, int type, struct page *page)
310{
311 u32 *ptr;
312 int i;
313 int limit = PAGE_SIZE / sizeof(int);
314
315 if (page_address(page))
316 ptr = (u32 *)page_address(page);
317 else
318 ptr = (u32 *)__va(pfn << PAGE_SHIFT);
319 /*
320 * When cloning the root in non-PAE mode, only the userspace
321 * pdes need to be zeroed.
322 */
323 if (type & VMI_PAGE_CLONE)
Jeremy Fitzhardinge68db0652008-03-17 16:37:13 -0700324 limit = KERNEL_PGD_BOUNDARY;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100325 for (i = 0; i < limit; i++)
326 BUG_ON(ptr[i]);
327}
328
329/*
330 * We stash the page type into struct page so we can verify the page
331 * types are used properly.
332 */
333static void vmi_set_page_type(u32 pfn, int type)
334{
335 /* PAE can have multiple roots per page - don't track */
336 if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
337 return;
338
339 if (boot_allocations_applied) {
340 struct page *page = pfn_to_page(pfn);
341 if (type != VMI_PAGE_NORMAL)
342 BUG_ON(page->type);
343 else
344 BUG_ON(page->type == VMI_PAGE_NORMAL);
345 page->type = type & ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
346 if (type & VMI_PAGE_ZEROED)
347 check_zeroed_page(pfn, type, page);
348 } else {
349 record_page_type(pfn, type);
350 }
351}
352
353static void vmi_check_page_type(u32 pfn, int type)
354{
355 /* PAE can have multiple roots per page - skip checks */
356 if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
357 return;
358
359 type &= ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
360 if (boot_allocations_applied) {
361 struct page *page = pfn_to_page(pfn);
362 BUG_ON((page->type ^ type) & VMI_PAGE_PAE);
363 BUG_ON(type == VMI_PAGE_NORMAL && page->type);
364 BUG_ON((type & page->type) == 0);
365 }
366}
367#else
368#define vmi_set_page_type(p,t) do { } while (0)
369#define vmi_check_page_type(p,t) do { } while (0)
370#endif
371
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200372#ifdef CONFIG_HIGHPTE
373static void *vmi_kmap_atomic_pte(struct page *page, enum km_type type)
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800374{
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200375 void *va = kmap_atomic(page, type);
376
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800377 /*
378 * Internally, the VMI ROM must map virtual addresses to physical
379 * addresses for processing MMU updates. By the time MMU updates
380 * are issued, this information is typically already lost.
381 * Fortunately, the VMI provides a cache of mapping slots for active
382 * page tables.
383 *
384 * We use slot zero for the linear mapping of physical memory, and
385 * in HIGHPTE kernels, slot 1 and 2 for KM_PTE0 and KM_PTE1.
386 *
387 * args: SLOT VA COUNT PFN
388 */
389 BUG_ON(type != KM_PTE0 && type != KM_PTE1);
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200390 vmi_ops.set_linear_mapping((type - KM_PTE0)+1, va, 1, page_to_pfn(page));
391
392 return va;
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800393}
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200394#endif
Zachary Amsden9a1c13e2007-03-05 00:30:37 -0800395
Eduardo Habkostf8639932008-07-30 18:32:27 -0300396static void vmi_allocate_pte(struct mm_struct *mm, unsigned long pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100397{
398 vmi_set_page_type(pfn, VMI_PAGE_L1);
399 vmi_ops.allocate_page(pfn, VMI_PAGE_L1, 0, 0, 0);
400}
401
Eduardo Habkostf8639932008-07-30 18:32:27 -0300402static void vmi_allocate_pmd(struct mm_struct *mm, unsigned long pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100403{
404 /*
405 * This call comes in very early, before mem_map is setup.
406 * It is called only for swapper_pg_dir, which already has
407 * data on it.
408 */
409 vmi_set_page_type(pfn, VMI_PAGE_L2);
410 vmi_ops.allocate_page(pfn, VMI_PAGE_L2, 0, 0, 0);
411}
412
Eduardo Habkostf8639932008-07-30 18:32:27 -0300413static void vmi_allocate_pmd_clone(unsigned long pfn, unsigned long clonepfn, unsigned long start, unsigned long count)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100414{
415 vmi_set_page_type(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE);
416 vmi_check_page_type(clonepfn, VMI_PAGE_L2);
417 vmi_ops.allocate_page(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE, clonepfn, start, count);
418}
419
Eduardo Habkostf8639932008-07-30 18:32:27 -0300420static void vmi_release_pte(unsigned long pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100421{
422 vmi_ops.release_page(pfn, VMI_PAGE_L1);
423 vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
424}
425
Eduardo Habkostf8639932008-07-30 18:32:27 -0300426static void vmi_release_pmd(unsigned long pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100427{
428 vmi_ops.release_page(pfn, VMI_PAGE_L2);
429 vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
430}
431
432/*
433 * Helper macros for MMU update flags. We can defer updates until a flush
434 * or page invalidation only if the update is to the current address space
435 * (otherwise, there is no flush). We must check against init_mm, since
436 * this could be a kernel update, which usually passes init_mm, although
437 * sometimes this check can be skipped if we know the particular function
438 * is only called on user mode PTEs. We could change the kernel to pass
439 * current->active_mm here, but in particular, I was unsure if changing
440 * mm/highmem.c to do this would still be correct on other architectures.
441 */
442#define is_current_as(mm, mustbeuser) ((mm) == current->active_mm || \
443 (!mustbeuser && (mm) == &init_mm))
444#define vmi_flags_addr(mm, addr, level, user) \
445 ((level) | (is_current_as(mm, user) ? \
446 (VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
447#define vmi_flags_addr_defer(mm, addr, level, user) \
448 ((level) | (is_current_as(mm, user) ? \
449 (VMI_PAGE_DEFER | VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
450
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200451static void vmi_update_pte(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100452{
453 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
454 vmi_ops.update_pte(ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
455}
456
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200457static void vmi_update_pte_defer(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100458{
459 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
460 vmi_ops.update_pte(ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 0));
461}
462
463static void vmi_set_pte(pte_t *ptep, pte_t pte)
464{
465 /* XXX because of set_pmd_pte, this can be called on PT or PD layers */
466 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE | VMI_PAGE_PD);
467 vmi_ops.set_pte(pte, ptep, VMI_PAGE_PT);
468}
469
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200470static 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 +0100471{
472 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
473 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
474}
475
476static void vmi_set_pmd(pmd_t *pmdp, pmd_t pmdval)
477{
478#ifdef CONFIG_X86_PAE
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100479 const pte_t pte = { .pte = pmdval.pmd };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100480 vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PMD);
481#else
482 const pte_t pte = { pmdval.pud.pgd.pgd };
483 vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PGD);
484#endif
485 vmi_ops.set_pte(pte, (pte_t *)pmdp, VMI_PAGE_PD);
486}
487
488#ifdef CONFIG_X86_PAE
489
490static void vmi_set_pte_atomic(pte_t *ptep, pte_t pteval)
491{
492 /*
493 * XXX This is called from set_pmd_pte, but at both PT
494 * and PD layers so the VMI_PAGE_PT flag is wrong. But
495 * it is only called for large page mapping changes,
496 * the Xen backend, doesn't support large pages, and the
497 * ESX backend doesn't depend on the flag.
498 */
499 set_64bit((unsigned long long *)ptep,pte_val(pteval));
500 vmi_ops.update_pte(ptep, VMI_PAGE_PT);
501}
502
503static void vmi_set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
504{
505 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
506 vmi_ops.set_pte(pte, ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 1));
507}
508
509static void vmi_set_pud(pud_t *pudp, pud_t pudval)
510{
511 /* Um, eww */
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100512 const pte_t pte = { .pte = pudval.pgd.pgd };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100513 vmi_check_page_type(__pa(pudp) >> PAGE_SHIFT, VMI_PAGE_PGD);
514 vmi_ops.set_pte(pte, (pte_t *)pudp, VMI_PAGE_PDP);
515}
516
517static void vmi_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
518{
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100519 const pte_t pte = { .pte = 0 };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100520 vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
521 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
522}
523
Adrian Bunk8eb68fa2007-05-02 19:27:09 +0200524static void vmi_pmd_clear(pmd_t *pmd)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100525{
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100526 const pte_t pte = { .pte = 0 };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100527 vmi_check_page_type(__pa(pmd) >> PAGE_SHIFT, VMI_PAGE_PMD);
528 vmi_ops.set_pte(pte, (pte_t *)pmd, VMI_PAGE_PD);
529}
530#endif
531
532#ifdef CONFIG_SMP
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800533static void __devinit
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100534vmi_startup_ipi_hook(int phys_apicid, unsigned long start_eip,
535 unsigned long start_esp)
536{
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800537 struct vmi_ap_state ap;
538
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100539 /* Default everything to zero. This is fine for most GPRs. */
540 memset(&ap, 0, sizeof(struct vmi_ap_state));
541
542 ap.gdtr_limit = GDT_SIZE - 1;
543 ap.gdtr_base = (unsigned long) get_cpu_gdt_table(phys_apicid);
544
545 ap.idtr_limit = IDT_ENTRIES * 8 - 1;
546 ap.idtr_base = (unsigned long) idt_table;
547
548 ap.ldtr = 0;
549
550 ap.cs = __KERNEL_CS;
551 ap.eip = (unsigned long) start_eip;
552 ap.ss = __KERNEL_DS;
553 ap.esp = (unsigned long) start_esp;
554
555 ap.ds = __USER_DS;
556 ap.es = __USER_DS;
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200557 ap.fs = __KERNEL_PERCPU;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100558 ap.gs = 0;
559
560 ap.eflags = 0;
561
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100562#ifdef CONFIG_X86_PAE
563 /* efer should match BSP efer. */
564 if (cpu_has_nx) {
565 unsigned l, h;
566 rdmsr(MSR_EFER, l, h);
567 ap.efer = (unsigned long long) h << 32 | l;
568 }
569#endif
570
571 ap.cr3 = __pa(swapper_pg_dir);
572 /* Protected mode, paging, AM, WP, NE, MP. */
573 ap.cr0 = 0x80050023;
574 ap.cr4 = mmu_cr4_features;
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800575 vmi_ops.set_initial_ap_state((u32)&ap, phys_apicid);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100576}
577#endif
578
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700579static void vmi_enter_lazy_cpu(void)
Zachary Amsden49f19712007-04-08 16:04:01 -0700580{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700581 paravirt_enter_lazy_cpu();
582 vmi_ops.set_lazy_mode(2);
583}
Zachary Amsden49f19712007-04-08 16:04:01 -0700584
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700585static void vmi_enter_lazy_mmu(void)
586{
587 paravirt_enter_lazy_mmu();
588 vmi_ops.set_lazy_mode(1);
589}
Zachary Amsden49f19712007-04-08 16:04:01 -0700590
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700591static void vmi_leave_lazy(void)
592{
593 paravirt_leave_lazy(paravirt_get_lazy_mode());
594 vmi_ops.set_lazy_mode(0);
Zachary Amsden49f19712007-04-08 16:04:01 -0700595}
596
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100597static inline int __init check_vmi_rom(struct vrom_header *rom)
598{
599 struct pci_header *pci;
600 struct pnp_header *pnp;
601 const char *manufacturer = "UNKNOWN";
602 const char *product = "UNKNOWN";
603 const char *license = "unspecified";
604
605 if (rom->rom_signature != 0xaa55)
606 return 0;
607 if (rom->vrom_signature != VMI_SIGNATURE)
608 return 0;
609 if (rom->api_version_maj != VMI_API_REV_MAJOR ||
610 rom->api_version_min+1 < VMI_API_REV_MINOR+1) {
611 printk(KERN_WARNING "VMI: Found mismatched rom version %d.%d\n",
612 rom->api_version_maj,
613 rom->api_version_min);
614 return 0;
615 }
616
617 /*
618 * Relying on the VMI_SIGNATURE field is not 100% safe, so check
619 * the PCI header and device type to make sure this is really a
620 * VMI device.
621 */
622 if (!rom->pci_header_offs) {
623 printk(KERN_WARNING "VMI: ROM does not contain PCI header.\n");
624 return 0;
625 }
626
627 pci = (struct pci_header *)((char *)rom+rom->pci_header_offs);
628 if (pci->vendorID != PCI_VENDOR_ID_VMWARE ||
629 pci->deviceID != PCI_DEVICE_ID_VMWARE_VMI) {
630 /* Allow it to run... anyways, but warn */
631 printk(KERN_WARNING "VMI: ROM from unknown manufacturer\n");
632 }
633
634 if (rom->pnp_header_offs) {
635 pnp = (struct pnp_header *)((char *)rom+rom->pnp_header_offs);
636 if (pnp->manufacturer_offset)
637 manufacturer = (const char *)rom+pnp->manufacturer_offset;
638 if (pnp->product_offset)
639 product = (const char *)rom+pnp->product_offset;
640 }
641
642 if (rom->license_offs)
643 license = (char *)rom+rom->license_offs;
644
645 printk(KERN_INFO "VMI: Found %s %s, API version %d.%d, ROM version %d.%d\n",
646 manufacturer, product,
647 rom->api_version_maj, rom->api_version_min,
648 pci->rom_version_maj, pci->rom_version_min);
649
Andi Kleen302cf932007-03-16 21:07:36 +0100650 /* Don't allow BSD/MIT here for now because we don't want to end up
651 with any binary only shim layers */
652 if (strcmp(license, "GPL") && strcmp(license, "GPL v2")) {
653 printk(KERN_WARNING "VMI: Non GPL license `%s' found for ROM. Not used.\n",
654 license);
655 return 0;
656 }
657
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100658 return 1;
659}
660
661/*
662 * Probe for the VMI option ROM
663 */
664static inline int __init probe_vmi_rom(void)
665{
666 unsigned long base;
667
668 /* VMI ROM is in option ROM area, check signature */
669 for (base = 0xC0000; base < 0xE0000; base += 2048) {
670 struct vrom_header *romstart;
671 romstart = (struct vrom_header *)isa_bus_to_virt(base);
672 if (check_vmi_rom(romstart)) {
673 vmi_rom = romstart;
674 return 1;
675 }
676 }
677 return 0;
678}
679
680/*
681 * VMI setup common to all processors
682 */
683void vmi_bringup(void)
684{
685 /* We must establish the lowmem mapping for MMU ops to work */
Zachary Amsden772205f2007-03-05 00:30:41 -0800686 if (vmi_ops.set_linear_mapping)
Alok Kataria31343d82008-08-08 12:15:57 -0700687 vmi_ops.set_linear_mapping(0, (void *)__PAGE_OFFSET, MAXMEM_PFN, 0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100688}
689
690/*
Zachary Amsden772205f2007-03-05 00:30:41 -0800691 * Return a pointer to a VMI function or NULL if unimplemented
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100692 */
693static void *vmi_get_function(int vmicall)
694{
695 u64 reloc;
696 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
697 reloc = call_vrom_long_func(vmi_rom, get_reloc, vmicall);
698 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);
699 if (rel->type == VMI_RELOCATION_CALL_REL)
700 return (void *)rel->eip;
701 else
Zachary Amsden772205f2007-03-05 00:30:41 -0800702 return NULL;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100703}
704
705/*
706 * Helper macro for making the VMI paravirt-ops fill code readable.
Zachary Amsden772205f2007-03-05 00:30:41 -0800707 * For unimplemented operations, fall back to default, unless nop
708 * is returned by the ROM.
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100709 */
710#define para_fill(opname, vmicall) \
711do { \
712 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
713 VMI_CALL_##vmicall); \
Zachary Amsden0492c372007-04-12 19:28:46 -0700714 if (rel->type == VMI_RELOCATION_CALL_REL) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700715 opname = (void *)rel->eip; \
Zachary Amsden0492c372007-04-12 19:28:46 -0700716 else if (rel->type == VMI_RELOCATION_NOP) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700717 opname = (void *)vmi_nop; \
Zachary Amsden0492c372007-04-12 19:28:46 -0700718 else if (rel->type != VMI_RELOCATION_NONE) \
719 printk(KERN_WARNING "VMI: Unknown relocation " \
720 "type %d for " #vmicall"\n",\
721 rel->type); \
Zachary Amsden772205f2007-03-05 00:30:41 -0800722} while (0)
723
724/*
725 * Helper macro for making the VMI paravirt-ops fill code readable.
726 * For cached operations which do not match the VMI ROM ABI and must
727 * go through a tranlation stub. Ignore NOPs, since it is not clear
728 * a NOP * VMI function corresponds to a NOP paravirt-op when the
729 * functions are not in 1-1 correspondence.
730 */
731#define para_wrap(opname, wrapper, cache, vmicall) \
732do { \
733 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
734 VMI_CALL_##vmicall); \
735 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL); \
736 if (rel->type == VMI_RELOCATION_CALL_REL) { \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700737 opname = wrapper; \
Zachary Amsden772205f2007-03-05 00:30:41 -0800738 vmi_ops.cache = (void *)rel->eip; \
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100739 } \
740} while (0)
741
742/*
743 * Activate the VMI interface and switch into paravirtualized mode
744 */
745static inline int __init activate_vmi(void)
746{
747 short kernel_cs;
748 u64 reloc;
749 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
750
751 if (call_vrom_func(vmi_rom, vmi_init) != 0) {
752 printk(KERN_ERR "VMI ROM failed to initialize!");
753 return 0;
754 }
755 savesegment(cs, kernel_cs);
756
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700757 pv_info.paravirt_enabled = 1;
758 pv_info.kernel_rpl = kernel_cs & SEGMENT_RPL_MASK;
759 pv_info.name = "vmi";
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100760
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700761 pv_init_ops.patch = vmi_patch;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100762
763 /*
764 * Many of these operations are ABI compatible with VMI.
765 * This means we can fill in the paravirt-ops with direct
766 * pointers into the VMI ROM. If the calling convention for
767 * these operations changes, this code needs to be updated.
768 *
769 * Exceptions
770 * CPUID paravirt-op uses pointers, not the native ISA
771 * halt has no VMI equivalent; all VMI halts are "safe"
772 * no MSR support yet - just trap and emulate. VMI uses the
773 * same ABI as the native ISA, but Linux wants exceptions
774 * from bogus MSR read / write handled
775 * rdpmc is not yet used in Linux
776 */
777
Zachary Amsden772205f2007-03-05 00:30:41 -0800778 /* CPUID is special, so very special it gets wrapped like a present */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700779 para_wrap(pv_cpu_ops.cpuid, vmi_cpuid, cpuid, CPUID);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100780
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700781 para_fill(pv_cpu_ops.clts, CLTS);
782 para_fill(pv_cpu_ops.get_debugreg, GetDR);
783 para_fill(pv_cpu_ops.set_debugreg, SetDR);
784 para_fill(pv_cpu_ops.read_cr0, GetCR0);
785 para_fill(pv_mmu_ops.read_cr2, GetCR2);
786 para_fill(pv_mmu_ops.read_cr3, GetCR3);
787 para_fill(pv_cpu_ops.read_cr4, GetCR4);
788 para_fill(pv_cpu_ops.write_cr0, SetCR0);
789 para_fill(pv_mmu_ops.write_cr2, SetCR2);
790 para_fill(pv_mmu_ops.write_cr3, SetCR3);
791 para_fill(pv_cpu_ops.write_cr4, SetCR4);
792 para_fill(pv_irq_ops.save_fl, GetInterruptMask);
793 para_fill(pv_irq_ops.restore_fl, SetInterruptMask);
794 para_fill(pv_irq_ops.irq_disable, DisableInterrupts);
795 para_fill(pv_irq_ops.irq_enable, EnableInterrupts);
Zachary Amsden772205f2007-03-05 00:30:41 -0800796
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700797 para_fill(pv_cpu_ops.wbinvd, WBINVD);
798 para_fill(pv_cpu_ops.read_tsc, RDTSC);
Zachary Amsden772205f2007-03-05 00:30:41 -0800799
800 /* The following we emulate with trap and emulate for now */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100801 /* paravirt_ops.read_msr = vmi_rdmsr */
802 /* paravirt_ops.write_msr = vmi_wrmsr */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100803 /* paravirt_ops.rdpmc = vmi_rdpmc */
804
Zachary Amsden772205f2007-03-05 00:30:41 -0800805 /* TR interface doesn't pass TR value, wrap */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700806 para_wrap(pv_cpu_ops.load_tr_desc, vmi_set_tr, set_tr, SetTR);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100807
808 /* LDT is special, too */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700809 para_wrap(pv_cpu_ops.set_ldt, vmi_set_ldt, _set_ldt, SetLDT);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100810
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700811 para_fill(pv_cpu_ops.load_gdt, SetGDT);
812 para_fill(pv_cpu_ops.load_idt, SetIDT);
813 para_fill(pv_cpu_ops.store_gdt, GetGDT);
814 para_fill(pv_cpu_ops.store_idt, GetIDT);
815 para_fill(pv_cpu_ops.store_tr, GetTR);
816 pv_cpu_ops.load_tls = vmi_load_tls;
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100817 para_wrap(pv_cpu_ops.write_ldt_entry, vmi_write_ldt_entry,
818 write_ldt_entry, WriteLDTEntry);
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100819 para_wrap(pv_cpu_ops.write_gdt_entry, vmi_write_gdt_entry,
820 write_gdt_entry, WriteGDTEntry);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100821 para_wrap(pv_cpu_ops.write_idt_entry, vmi_write_idt_entry,
822 write_idt_entry, WriteIDTEntry);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100823 para_wrap(pv_cpu_ops.load_sp0, vmi_load_sp0, set_kernel_stack, UpdateKernelStack);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700824 para_fill(pv_cpu_ops.set_iopl_mask, SetIOPLMask);
825 para_fill(pv_cpu_ops.io_delay, IODelay);
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700826
827 para_wrap(pv_cpu_ops.lazy_mode.enter, vmi_enter_lazy_cpu,
828 set_lazy_mode, SetLazyMode);
829 para_wrap(pv_cpu_ops.lazy_mode.leave, vmi_leave_lazy,
830 set_lazy_mode, SetLazyMode);
831
832 para_wrap(pv_mmu_ops.lazy_mode.enter, vmi_enter_lazy_mmu,
833 set_lazy_mode, SetLazyMode);
834 para_wrap(pv_mmu_ops.lazy_mode.leave, vmi_leave_lazy,
835 set_lazy_mode, SetLazyMode);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100836
Zachary Amsden772205f2007-03-05 00:30:41 -0800837 /* user and kernel flush are just handled with different flags to FlushTLB */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700838 para_wrap(pv_mmu_ops.flush_tlb_user, vmi_flush_tlb_user, _flush_tlb, FlushTLB);
839 para_wrap(pv_mmu_ops.flush_tlb_kernel, vmi_flush_tlb_kernel, _flush_tlb, FlushTLB);
840 para_fill(pv_mmu_ops.flush_tlb_single, InvalPage);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100841
842 /*
843 * Until a standard flag format can be agreed on, we need to
844 * implement these as wrappers in Linux. Get the VMI ROM
845 * function pointers for the two backend calls.
846 */
847#ifdef CONFIG_X86_PAE
848 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxELong);
849 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxELong);
850#else
851 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxE);
852 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxE);
853#endif
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100854
Zachary Amsden772205f2007-03-05 00:30:41 -0800855 if (vmi_ops.set_pte) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700856 pv_mmu_ops.set_pte = vmi_set_pte;
857 pv_mmu_ops.set_pte_at = vmi_set_pte_at;
858 pv_mmu_ops.set_pmd = vmi_set_pmd;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100859#ifdef CONFIG_X86_PAE
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700860 pv_mmu_ops.set_pte_atomic = vmi_set_pte_atomic;
861 pv_mmu_ops.set_pte_present = vmi_set_pte_present;
862 pv_mmu_ops.set_pud = vmi_set_pud;
863 pv_mmu_ops.pte_clear = vmi_pte_clear;
864 pv_mmu_ops.pmd_clear = vmi_pmd_clear;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100865#endif
Zachary Amsden772205f2007-03-05 00:30:41 -0800866 }
867
868 if (vmi_ops.update_pte) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700869 pv_mmu_ops.pte_update = vmi_update_pte;
870 pv_mmu_ops.pte_update_defer = vmi_update_pte_defer;
Zachary Amsden772205f2007-03-05 00:30:41 -0800871 }
872
873 vmi_ops.allocate_page = vmi_get_function(VMI_CALL_AllocatePage);
874 if (vmi_ops.allocate_page) {
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700875 pv_mmu_ops.alloc_pte = vmi_allocate_pte;
876 pv_mmu_ops.alloc_pmd = vmi_allocate_pmd;
877 pv_mmu_ops.alloc_pmd_clone = vmi_allocate_pmd_clone;
Zachary Amsden772205f2007-03-05 00:30:41 -0800878 }
879
880 vmi_ops.release_page = vmi_get_function(VMI_CALL_ReleasePage);
881 if (vmi_ops.release_page) {
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700882 pv_mmu_ops.release_pte = vmi_release_pte;
883 pv_mmu_ops.release_pmd = vmi_release_pmd;
Zachary Amsden772205f2007-03-05 00:30:41 -0800884 }
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200885
886 /* Set linear is needed in all cases */
887 vmi_ops.set_linear_mapping = vmi_get_function(VMI_CALL_SetLinearMapping);
888#ifdef CONFIG_HIGHPTE
889 if (vmi_ops.set_linear_mapping)
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700890 pv_mmu_ops.kmap_atomic_pte = vmi_kmap_atomic_pte;
Jeremy Fitzhardingea27fe802007-05-02 19:27:15 +0200891#endif
Zachary Amsden772205f2007-03-05 00:30:41 -0800892
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100893 /*
894 * These MUST always be patched. Don't support indirect jumps
895 * through these operations, as the VMI interface may use either
896 * a jump or a call to get to these operations, depending on
897 * the backend. They are performance critical anyway, so requiring
898 * a patch is not a big problem.
899 */
Jeremy Fitzhardinged75cd222008-06-25 00:19:26 -0400900 pv_cpu_ops.irq_enable_sysexit = (void *)0xfeedbab0;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700901 pv_cpu_ops.iret = (void *)0xbadbab0;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100902
903#ifdef CONFIG_SMP
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700904 para_wrap(pv_apic_ops.startup_ipi_hook, vmi_startup_ipi_hook, set_initial_ap_state, SetInitialAPState);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100905#endif
906
907#ifdef CONFIG_X86_LOCAL_APIC
Suresh Siddha9a8f0e62008-07-18 09:59:40 -0700908 para_fill(apic_ops->read, APICRead);
909 para_fill(apic_ops->write, 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;
Alok Katariae93ef942008-07-01 11:43:36 -0700935 pv_time_ops.get_tsc_khz = vmi_tsc_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{
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100963 if (!vmi_rom)
964 probe_vmi_rom();
965 else
966 check_vmi_rom(vmi_rom);
967
968 /* In case probing for or validating the ROM failed, basil */
969 if (!vmi_rom)
970 return;
971
972 reserve_top_address(-vmi_rom->virtual_top);
973
Zachary Amsden7507ba32007-03-05 00:30:34 -0800974#ifdef CONFIG_X86_IO_APIC
Zachary Amsden772205f2007-03-05 00:30:41 -0800975 /* This is virtual hardware; timer routing is wired correctly */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100976 no_timer_check = 1;
977#endif
Zachary Amsdenae8d04e2008-12-13 12:36:58 -0800978}
979
980void vmi_activate(void)
981{
982 unsigned long flags;
983
984 if (!vmi_rom)
985 return;
986
987 local_irq_save(flags);
988 activate_vmi();
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100989 local_irq_restore(flags & X86_EFLAGS_IF);
990}
991
992static int __init parse_vmi(char *arg)
993{
994 if (!arg)
995 return -EINVAL;
996
Zachary Amsdeneda08b12007-03-05 00:30:38 -0800997 if (!strcmp(arg, "disable_pge")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100998 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PGE);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100999 disable_pge = 1;
1000 } else if (!strcmp(arg, "disable_pse")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001001 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PSE);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001002 disable_pse = 1;
1003 } else if (!strcmp(arg, "disable_sep")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001004 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_SEP);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001005 disable_sep = 1;
1006 } else if (!strcmp(arg, "disable_tsc")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001007 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_TSC);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001008 disable_tsc = 1;
1009 } else if (!strcmp(arg, "disable_mtrr")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001010 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_MTRR);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001011 disable_mtrr = 1;
Zachary Amsden772205f2007-03-05 00:30:41 -08001012 } else if (!strcmp(arg, "disable_timer")) {
1013 disable_vmi_timer = 1;
1014 disable_noidle = 1;
Zachary Amsden7507ba32007-03-05 00:30:34 -08001015 } else if (!strcmp(arg, "disable_noidle"))
1016 disable_noidle = 1;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001017 return 0;
1018}
1019
1020early_param("vmi", parse_vmi);