blob: ce9fbacb7526284f35c8290d994a8f804c4e61c0 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/gfp.h>
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010032#include <asm/vmi.h>
33#include <asm/io.h>
34#include <asm/fixmap.h>
35#include <asm/apicdef.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010036#include <asm/apic.h>
Ian Campbell3249b7e2010-02-26 17:16:01 +000037#include <asm/pgalloc.h>
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010038#include <asm/processor.h>
39#include <asm/timer.h>
Zachary Amsdenbbab4f32007-02-13 13:26:21 +010040#include <asm/vmi_time.h>
Adrian Bunk8f485612007-03-05 00:30:56 -080041#include <asm/kmap_types.h>
Alok Kataria31343d82008-08-08 12:15:57 -070042#include <asm/setup.h>
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010043
44/* Convenient for calling VMI functions indirectly in the ROM */
45typedef u32 __attribute__((regparm(1))) (VROMFUNC)(void);
46typedef u64 __attribute__((regparm(2))) (VROMLONGFUNC)(int);
47
48#define call_vrom_func(rom,func) \
49 (((VROMFUNC *)(rom->func))())
50
51#define call_vrom_long_func(rom,func,arg) \
52 (((VROMLONGFUNC *)(rom->func)) (arg))
53
54static struct vrom_header *vmi_rom;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010055static int disable_pge;
56static int disable_pse;
57static int disable_sep;
58static int disable_tsc;
59static int disable_mtrr;
Zachary Amsden7507ba32007-03-05 00:30:34 -080060static int disable_noidle;
Zachary Amsden772205f2007-03-05 00:30:41 -080061static int disable_vmi_timer;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010062
63/* Cached VMI operations */
Adrian Bunk30a15282007-05-02 19:27:08 +020064static struct {
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010065 void (*cpuid)(void /* non-c */);
66 void (*_set_ldt)(u32 selector);
67 void (*set_tr)(u32 selector);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +010068 void (*write_idt_entry)(struct desc_struct *, int, u32, u32);
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +010069 void (*write_gdt_entry)(struct desc_struct *, int, u32, u32);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +010070 void (*write_ldt_entry)(struct desc_struct *, int, u32, u32);
H. Peter Anvinfaca6222008-01-30 13:31:02 +010071 void (*set_kernel_stack)(u32 selector, u32 sp0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010072 void (*allocate_page)(u32, u32, u32, u32, u32);
73 void (*release_page)(u32, u32);
74 void (*set_pte)(pte_t, pte_t *, unsigned);
75 void (*update_pte)(pte_t *, unsigned);
Zachary Amsdeneeef9c62007-05-02 19:27:16 +020076 void (*set_linear_mapping)(int, void *, u32, u32);
77 void (*_flush_tlb)(int);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010078 void (*set_initial_ap_state)(int, int);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +010079 void (*halt)(void);
Zachary Amsden49f19712007-04-08 16:04:01 -070080 void (*set_lazy_mode)(int mode);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010081} vmi_ops;
82
Zachary Amsdene0bb8642007-05-02 19:27:16 +020083/* Cached VMI operations */
84struct vmi_timer_ops vmi_timer_ops;
85
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010086/*
87 * VMI patching routines.
88 */
89#define MNEM_CALL 0xe8
90#define MNEM_JMP 0xe9
91#define MNEM_RET 0xc3
92
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010093#define IRQ_PATCH_INT_MASK 0
94#define IRQ_PATCH_DISABLE 5
95
Andi Kleenab144f52007-08-10 22:31:03 +020096static inline void patch_offset(void *insnbuf,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010097 unsigned long ip, unsigned long dest)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010098{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010099 *(unsigned long *)(insnbuf+1) = dest-ip-5;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100100}
101
Andi Kleenab144f52007-08-10 22:31:03 +0200102static unsigned patch_internal(int call, unsigned len, void *insnbuf,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100103 unsigned long ip)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100104{
105 u64 reloc;
106 struct vmi_relocation_info *const rel = (struct vmi_relocation_info *)&reloc;
107 reloc = call_vrom_long_func(vmi_rom, get_reloc, call);
108 switch(rel->type) {
109 case VMI_RELOCATION_CALL_REL:
110 BUG_ON(len < 5);
Andi Kleenab144f52007-08-10 22:31:03 +0200111 *(char *)insnbuf = MNEM_CALL;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100112 patch_offset(insnbuf, ip, (unsigned long)rel->eip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100113 return 5;
114
115 case VMI_RELOCATION_JUMP_REL:
116 BUG_ON(len < 5);
Andi Kleenab144f52007-08-10 22:31:03 +0200117 *(char *)insnbuf = MNEM_JMP;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100118 patch_offset(insnbuf, ip, (unsigned long)rel->eip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100119 return 5;
120
121 case VMI_RELOCATION_NOP:
122 /* obliterate the whole thing */
123 return 0;
124
125 case VMI_RELOCATION_NONE:
126 /* leave native code in place */
127 break;
128
129 default:
130 BUG();
131 }
132 return len;
133}
134
135/*
136 * Apply patch if appropriate, return length of new instruction
137 * sequence. The callee does nop padding for us.
138 */
Andi Kleenab144f52007-08-10 22:31:03 +0200139static unsigned vmi_patch(u8 type, u16 clobbers, void *insns,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100140 unsigned long ip, unsigned len)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100141{
142 switch (type) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700143 case PARAVIRT_PATCH(pv_irq_ops.irq_disable):
Andi Kleenab144f52007-08-10 22:31:03 +0200144 return patch_internal(VMI_CALL_DisableInterrupts, 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.irq_enable):
Andi Kleenab144f52007-08-10 22:31:03 +0200147 return patch_internal(VMI_CALL_EnableInterrupts, 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.restore_fl):
Andi Kleenab144f52007-08-10 22:31:03 +0200150 return patch_internal(VMI_CALL_SetInterruptMask, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100151 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700152 case PARAVIRT_PATCH(pv_irq_ops.save_fl):
Andi Kleenab144f52007-08-10 22:31:03 +0200153 return patch_internal(VMI_CALL_GetInterruptMask, len,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100154 insns, ip);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700155 case PARAVIRT_PATCH(pv_cpu_ops.iret):
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100156 return patch_internal(VMI_CALL_IRET, len, insns, ip);
Jeremy Fitzhardinged75cd222008-06-25 00:19:26 -0400157 case PARAVIRT_PATCH(pv_cpu_ops.irq_enable_sysexit):
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100158 return patch_internal(VMI_CALL_SYSEXIT, len, insns, ip);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100159 default:
160 break;
161 }
162 return len;
163}
164
165/* CPUID has non-C semantics, and paravirt-ops API doesn't match hardware ISA */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100166static void vmi_cpuid(unsigned int *ax, unsigned int *bx,
167 unsigned int *cx, unsigned int *dx)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100168{
169 int override = 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100170 if (*ax == 1)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100171 override = 1;
172 asm volatile ("call *%6"
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100173 : "=a" (*ax),
174 "=b" (*bx),
175 "=c" (*cx),
176 "=d" (*dx)
177 : "0" (*ax), "2" (*cx), "r" (vmi_ops.cpuid));
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100178 if (override) {
179 if (disable_pse)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100180 *dx &= ~X86_FEATURE_PSE;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100181 if (disable_pge)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100182 *dx &= ~X86_FEATURE_PGE;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100183 if (disable_sep)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100184 *dx &= ~X86_FEATURE_SEP;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100185 if (disable_tsc)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100186 *dx &= ~X86_FEATURE_TSC;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100187 if (disable_mtrr)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100188 *dx &= ~X86_FEATURE_MTRR;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100189 }
190}
191
192static inline void vmi_maybe_load_tls(struct desc_struct *gdt, int nr, struct desc_struct *new)
193{
194 if (gdt[nr].a != new->a || gdt[nr].b != new->b)
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100195 write_gdt_entry(gdt, nr, new, 0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100196}
197
198static void vmi_load_tls(struct thread_struct *t, unsigned int cpu)
199{
200 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
201 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 0, &t->tls_array[0]);
202 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 1, &t->tls_array[1]);
203 vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 2, &t->tls_array[2]);
204}
205
206static void vmi_set_ldt(const void *addr, unsigned entries)
207{
208 unsigned cpu = smp_processor_id();
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100209 struct desc_struct desc;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100210
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100211 pack_descriptor(&desc, (unsigned long)addr,
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100212 entries * sizeof(struct desc_struct) - 1,
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100213 DESC_LDT, 0);
214 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, &desc, DESC_LDT);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100215 vmi_ops._set_ldt(entries ? GDT_ENTRY_LDT*sizeof(struct desc_struct) : 0);
216}
217
218static void vmi_set_tr(void)
219{
220 vmi_ops.set_tr(GDT_ENTRY_TSS*sizeof(struct desc_struct));
221}
222
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100223static void vmi_write_idt_entry(gate_desc *dt, int entry, const gate_desc *g)
224{
225 u32 *idt_entry = (u32 *)g;
Ingo Molnar262d5ee2008-02-04 16:47:54 +0100226 vmi_ops.write_idt_entry(dt, entry, idt_entry[0], idt_entry[1]);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100227}
228
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100229static void vmi_write_gdt_entry(struct desc_struct *dt, int entry,
230 const void *desc, int type)
231{
232 u32 *gdt_entry = (u32 *)desc;
Ingo Molnar262d5ee2008-02-04 16:47:54 +0100233 vmi_ops.write_gdt_entry(dt, entry, gdt_entry[0], gdt_entry[1]);
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100234}
235
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100236static void vmi_write_ldt_entry(struct desc_struct *dt, int entry,
237 const void *desc)
238{
239 u32 *ldt_entry = (u32 *)desc;
Zachary Amsdende599852008-09-30 11:02:12 -0700240 vmi_ops.write_ldt_entry(dt, entry, ldt_entry[0], ldt_entry[1]);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100241}
242
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100243static void vmi_load_sp0(struct tss_struct *tss,
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100244 struct thread_struct *thread)
245{
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100246 tss->x86_tss.sp0 = thread->sp0;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100247
248 /* This can only happen when SEP is enabled, no need to test "SEP"arately */
Rusty Russella75c54f2007-05-02 19:27:13 +0200249 if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
250 tss->x86_tss.ss1 = thread->sysenter_cs;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100251 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
252 }
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100253 vmi_ops.set_kernel_stack(__KERNEL_DS, tss->x86_tss.sp0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100254}
255
256static void vmi_flush_tlb_user(void)
257{
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200258 vmi_ops._flush_tlb(VMI_FLUSH_TLB);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100259}
260
261static void vmi_flush_tlb_kernel(void)
262{
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200263 vmi_ops._flush_tlb(VMI_FLUSH_TLB | VMI_FLUSH_GLOBAL);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100264}
265
266/* Stub to do nothing at all; used for delays and unimplemented calls */
267static void vmi_nop(void)
268{
269}
270
Eduardo Habkostf8639932008-07-30 18:32:27 -0300271static void vmi_allocate_pte(struct mm_struct *mm, unsigned long pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100272{
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100273 vmi_ops.allocate_page(pfn, VMI_PAGE_L1, 0, 0, 0);
274}
275
Eduardo Habkostf8639932008-07-30 18:32:27 -0300276static void vmi_allocate_pmd(struct mm_struct *mm, unsigned long pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100277{
278 /*
279 * This call comes in very early, before mem_map is setup.
280 * It is called only for swapper_pg_dir, which already has
281 * data on it.
282 */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100283 vmi_ops.allocate_page(pfn, VMI_PAGE_L2, 0, 0, 0);
284}
285
Eduardo Habkostf8639932008-07-30 18:32:27 -0300286static void vmi_allocate_pmd_clone(unsigned long pfn, unsigned long clonepfn, unsigned long start, unsigned long count)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100287{
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100288 vmi_ops.allocate_page(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE, clonepfn, start, count);
289}
290
Eduardo Habkostf8639932008-07-30 18:32:27 -0300291static void vmi_release_pte(unsigned long pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100292{
293 vmi_ops.release_page(pfn, VMI_PAGE_L1);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100294}
295
Eduardo Habkostf8639932008-07-30 18:32:27 -0300296static void vmi_release_pmd(unsigned long pfn)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100297{
298 vmi_ops.release_page(pfn, VMI_PAGE_L2);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100299}
300
301/*
Alok Kataria55a8ba42009-02-06 10:29:35 -0800302 * We use the pgd_free hook for releasing the pgd page:
303 */
304static void vmi_pgd_free(struct mm_struct *mm, pgd_t *pgd)
305{
306 unsigned long pfn = __pa(pgd) >> PAGE_SHIFT;
307
308 vmi_ops.release_page(pfn, VMI_PAGE_L2);
309}
310
311/*
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100312 * Helper macros for MMU update flags. We can defer updates until a flush
313 * or page invalidation only if the update is to the current address space
314 * (otherwise, there is no flush). We must check against init_mm, since
315 * this could be a kernel update, which usually passes init_mm, although
316 * sometimes this check can be skipped if we know the particular function
317 * is only called on user mode PTEs. We could change the kernel to pass
318 * current->active_mm here, but in particular, I was unsure if changing
319 * mm/highmem.c to do this would still be correct on other architectures.
320 */
321#define is_current_as(mm, mustbeuser) ((mm) == current->active_mm || \
322 (!mustbeuser && (mm) == &init_mm))
323#define vmi_flags_addr(mm, addr, level, user) \
324 ((level) | (is_current_as(mm, user) ? \
325 (VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
326#define vmi_flags_addr_defer(mm, addr, level, user) \
327 ((level) | (is_current_as(mm, user) ? \
328 (VMI_PAGE_DEFER | VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
329
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200330static void vmi_update_pte(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100331{
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100332 vmi_ops.update_pte(ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
333}
334
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200335static void vmi_update_pte_defer(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100336{
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100337 vmi_ops.update_pte(ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 0));
338}
339
340static void vmi_set_pte(pte_t *ptep, pte_t pte)
341{
342 /* XXX because of set_pmd_pte, this can be called on PT or PD layers */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100343 vmi_ops.set_pte(pte, ptep, VMI_PAGE_PT);
344}
345
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200346static 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 +0100347{
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100348 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
349}
350
351static void vmi_set_pmd(pmd_t *pmdp, pmd_t pmdval)
352{
353#ifdef CONFIG_X86_PAE
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100354 const pte_t pte = { .pte = pmdval.pmd };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100355#else
356 const pte_t pte = { pmdval.pud.pgd.pgd };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100357#endif
358 vmi_ops.set_pte(pte, (pte_t *)pmdp, VMI_PAGE_PD);
359}
360
361#ifdef CONFIG_X86_PAE
362
363static void vmi_set_pte_atomic(pte_t *ptep, pte_t pteval)
364{
365 /*
366 * XXX This is called from set_pmd_pte, but at both PT
367 * and PD layers so the VMI_PAGE_PT flag is wrong. But
368 * it is only called for large page mapping changes,
369 * the Xen backend, doesn't support large pages, and the
370 * ESX backend doesn't depend on the flag.
371 */
372 set_64bit((unsigned long long *)ptep,pte_val(pteval));
373 vmi_ops.update_pte(ptep, VMI_PAGE_PT);
374}
375
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100376static void vmi_set_pud(pud_t *pudp, pud_t pudval)
377{
378 /* Um, eww */
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100379 const pte_t pte = { .pte = pudval.pgd.pgd };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100380 vmi_ops.set_pte(pte, (pte_t *)pudp, VMI_PAGE_PDP);
381}
382
383static void vmi_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
384{
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100385 const pte_t pte = { .pte = 0 };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100386 vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
387}
388
Adrian Bunk8eb68fa2007-05-02 19:27:09 +0200389static void vmi_pmd_clear(pmd_t *pmd)
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100390{
Jeremy Fitzhardingee3328702008-01-30 13:32:58 +0100391 const pte_t pte = { .pte = 0 };
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100392 vmi_ops.set_pte(pte, (pte_t *)pmd, VMI_PAGE_PD);
393}
394#endif
395
396#ifdef CONFIG_SMP
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800397static void __devinit
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100398vmi_startup_ipi_hook(int phys_apicid, unsigned long start_eip,
399 unsigned long start_esp)
400{
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800401 struct vmi_ap_state ap;
402
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100403 /* Default everything to zero. This is fine for most GPRs. */
404 memset(&ap, 0, sizeof(struct vmi_ap_state));
405
406 ap.gdtr_limit = GDT_SIZE - 1;
407 ap.gdtr_base = (unsigned long) get_cpu_gdt_table(phys_apicid);
408
409 ap.idtr_limit = IDT_ENTRIES * 8 - 1;
410 ap.idtr_base = (unsigned long) idt_table;
411
412 ap.ldtr = 0;
413
414 ap.cs = __KERNEL_CS;
415 ap.eip = (unsigned long) start_eip;
416 ap.ss = __KERNEL_DS;
417 ap.esp = (unsigned long) start_esp;
418
419 ap.ds = __USER_DS;
420 ap.es = __USER_DS;
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200421 ap.fs = __KERNEL_PERCPU;
Alok Kataria7d5b0052009-08-04 15:34:22 -0700422 ap.gs = __KERNEL_STACK_CANARY;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100423
424 ap.eflags = 0;
425
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100426#ifdef CONFIG_X86_PAE
427 /* efer should match BSP efer. */
428 if (cpu_has_nx) {
429 unsigned l, h;
430 rdmsr(MSR_EFER, l, h);
431 ap.efer = (unsigned long long) h << 32 | l;
432 }
433#endif
434
435 ap.cr3 = __pa(swapper_pg_dir);
436 /* Protected mode, paging, AM, WP, NE, MP. */
437 ap.cr0 = 0x80050023;
438 ap.cr4 = mmu_cr4_features;
Zachary Amsdenc6b36e92007-03-05 00:30:43 -0800439 vmi_ops.set_initial_ap_state((u32)&ap, phys_apicid);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100440}
441#endif
442
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800443static void vmi_start_context_switch(struct task_struct *prev)
Zachary Amsden49f19712007-04-08 16:04:01 -0700444{
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800445 paravirt_start_context_switch(prev);
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700446 vmi_ops.set_lazy_mode(2);
447}
Zachary Amsden49f19712007-04-08 16:04:01 -0700448
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800449static void vmi_end_context_switch(struct task_struct *next)
Jeremy Fitzhardingeb407fc52009-02-17 23:46:21 -0800450{
451 vmi_ops.set_lazy_mode(0);
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800452 paravirt_end_context_switch(next);
Jeremy Fitzhardingeb407fc52009-02-17 23:46:21 -0800453}
454
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700455static void vmi_enter_lazy_mmu(void)
456{
457 paravirt_enter_lazy_mmu();
458 vmi_ops.set_lazy_mode(1);
459}
Zachary Amsden49f19712007-04-08 16:04:01 -0700460
Jeremy Fitzhardingeb407fc52009-02-17 23:46:21 -0800461static void vmi_leave_lazy_mmu(void)
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700462{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700463 vmi_ops.set_lazy_mode(0);
Jeremy Fitzhardingeb407fc52009-02-17 23:46:21 -0800464 paravirt_leave_lazy_mmu();
Zachary Amsden49f19712007-04-08 16:04:01 -0700465}
466
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100467static inline int __init check_vmi_rom(struct vrom_header *rom)
468{
469 struct pci_header *pci;
470 struct pnp_header *pnp;
471 const char *manufacturer = "UNKNOWN";
472 const char *product = "UNKNOWN";
473 const char *license = "unspecified";
474
475 if (rom->rom_signature != 0xaa55)
476 return 0;
477 if (rom->vrom_signature != VMI_SIGNATURE)
478 return 0;
479 if (rom->api_version_maj != VMI_API_REV_MAJOR ||
480 rom->api_version_min+1 < VMI_API_REV_MINOR+1) {
481 printk(KERN_WARNING "VMI: Found mismatched rom version %d.%d\n",
482 rom->api_version_maj,
483 rom->api_version_min);
484 return 0;
485 }
486
487 /*
488 * Relying on the VMI_SIGNATURE field is not 100% safe, so check
489 * the PCI header and device type to make sure this is really a
490 * VMI device.
491 */
492 if (!rom->pci_header_offs) {
493 printk(KERN_WARNING "VMI: ROM does not contain PCI header.\n");
494 return 0;
495 }
496
497 pci = (struct pci_header *)((char *)rom+rom->pci_header_offs);
498 if (pci->vendorID != PCI_VENDOR_ID_VMWARE ||
499 pci->deviceID != PCI_DEVICE_ID_VMWARE_VMI) {
500 /* Allow it to run... anyways, but warn */
501 printk(KERN_WARNING "VMI: ROM from unknown manufacturer\n");
502 }
503
504 if (rom->pnp_header_offs) {
505 pnp = (struct pnp_header *)((char *)rom+rom->pnp_header_offs);
506 if (pnp->manufacturer_offset)
507 manufacturer = (const char *)rom+pnp->manufacturer_offset;
508 if (pnp->product_offset)
509 product = (const char *)rom+pnp->product_offset;
510 }
511
512 if (rom->license_offs)
513 license = (char *)rom+rom->license_offs;
514
515 printk(KERN_INFO "VMI: Found %s %s, API version %d.%d, ROM version %d.%d\n",
516 manufacturer, product,
517 rom->api_version_maj, rom->api_version_min,
518 pci->rom_version_maj, pci->rom_version_min);
519
Andi Kleen302cf932007-03-16 21:07:36 +0100520 /* Don't allow BSD/MIT here for now because we don't want to end up
521 with any binary only shim layers */
522 if (strcmp(license, "GPL") && strcmp(license, "GPL v2")) {
523 printk(KERN_WARNING "VMI: Non GPL license `%s' found for ROM. Not used.\n",
524 license);
525 return 0;
526 }
527
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100528 return 1;
529}
530
531/*
532 * Probe for the VMI option ROM
533 */
534static inline int __init probe_vmi_rom(void)
535{
536 unsigned long base;
537
538 /* VMI ROM is in option ROM area, check signature */
539 for (base = 0xC0000; base < 0xE0000; base += 2048) {
540 struct vrom_header *romstart;
541 romstart = (struct vrom_header *)isa_bus_to_virt(base);
542 if (check_vmi_rom(romstart)) {
543 vmi_rom = romstart;
544 return 1;
545 }
546 }
547 return 0;
548}
549
550/*
551 * VMI setup common to all processors
552 */
553void vmi_bringup(void)
554{
555 /* We must establish the lowmem mapping for MMU ops to work */
Zachary Amsden772205f2007-03-05 00:30:41 -0800556 if (vmi_ops.set_linear_mapping)
Alok Kataria31343d82008-08-08 12:15:57 -0700557 vmi_ops.set_linear_mapping(0, (void *)__PAGE_OFFSET, MAXMEM_PFN, 0);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100558}
559
560/*
Zachary Amsden772205f2007-03-05 00:30:41 -0800561 * Return a pointer to a VMI function or NULL if unimplemented
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100562 */
563static void *vmi_get_function(int vmicall)
564{
565 u64 reloc;
566 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
567 reloc = call_vrom_long_func(vmi_rom, get_reloc, vmicall);
568 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);
569 if (rel->type == VMI_RELOCATION_CALL_REL)
570 return (void *)rel->eip;
571 else
Zachary Amsden772205f2007-03-05 00:30:41 -0800572 return NULL;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100573}
574
575/*
576 * Helper macro for making the VMI paravirt-ops fill code readable.
Zachary Amsden772205f2007-03-05 00:30:41 -0800577 * For unimplemented operations, fall back to default, unless nop
578 * is returned by the ROM.
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100579 */
580#define para_fill(opname, vmicall) \
581do { \
582 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
583 VMI_CALL_##vmicall); \
Zachary Amsden0492c372007-04-12 19:28:46 -0700584 if (rel->type == VMI_RELOCATION_CALL_REL) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700585 opname = (void *)rel->eip; \
Zachary Amsden0492c372007-04-12 19:28:46 -0700586 else if (rel->type == VMI_RELOCATION_NOP) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700587 opname = (void *)vmi_nop; \
Zachary Amsden0492c372007-04-12 19:28:46 -0700588 else if (rel->type != VMI_RELOCATION_NONE) \
589 printk(KERN_WARNING "VMI: Unknown relocation " \
590 "type %d for " #vmicall"\n",\
591 rel->type); \
Zachary Amsden772205f2007-03-05 00:30:41 -0800592} while (0)
593
594/*
595 * Helper macro for making the VMI paravirt-ops fill code readable.
596 * For cached operations which do not match the VMI ROM ABI and must
597 * go through a tranlation stub. Ignore NOPs, since it is not clear
598 * a NOP * VMI function corresponds to a NOP paravirt-op when the
599 * functions are not in 1-1 correspondence.
600 */
601#define para_wrap(opname, wrapper, cache, vmicall) \
602do { \
603 reloc = call_vrom_long_func(vmi_rom, get_reloc, \
604 VMI_CALL_##vmicall); \
605 BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL); \
606 if (rel->type == VMI_RELOCATION_CALL_REL) { \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700607 opname = wrapper; \
Zachary Amsden772205f2007-03-05 00:30:41 -0800608 vmi_ops.cache = (void *)rel->eip; \
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100609 } \
610} while (0)
611
612/*
613 * Activate the VMI interface and switch into paravirtualized mode
614 */
615static inline int __init activate_vmi(void)
616{
617 short kernel_cs;
618 u64 reloc;
619 const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
620
Ian Campbell3249b7e2010-02-26 17:16:01 +0000621 /*
622 * Prevent page tables from being allocated in highmem, even if
623 * CONFIG_HIGHPTE is enabled.
624 */
625 __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
626
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100627 if (call_vrom_func(vmi_rom, vmi_init) != 0) {
628 printk(KERN_ERR "VMI ROM failed to initialize!");
629 return 0;
630 }
631 savesegment(cs, kernel_cs);
632
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700633 pv_info.paravirt_enabled = 1;
634 pv_info.kernel_rpl = kernel_cs & SEGMENT_RPL_MASK;
Alok Katariad0153ca2009-09-29 10:25:24 -0700635 pv_info.name = "vmi [deprecated]";
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100636
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700637 pv_init_ops.patch = vmi_patch;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100638
639 /*
640 * Many of these operations are ABI compatible with VMI.
641 * This means we can fill in the paravirt-ops with direct
642 * pointers into the VMI ROM. If the calling convention for
643 * these operations changes, this code needs to be updated.
644 *
645 * Exceptions
646 * CPUID paravirt-op uses pointers, not the native ISA
647 * halt has no VMI equivalent; all VMI halts are "safe"
648 * no MSR support yet - just trap and emulate. VMI uses the
649 * same ABI as the native ISA, but Linux wants exceptions
650 * from bogus MSR read / write handled
651 * rdpmc is not yet used in Linux
652 */
653
Zachary Amsden772205f2007-03-05 00:30:41 -0800654 /* CPUID is special, so very special it gets wrapped like a present */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700655 para_wrap(pv_cpu_ops.cpuid, vmi_cpuid, cpuid, CPUID);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100656
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700657 para_fill(pv_cpu_ops.clts, CLTS);
658 para_fill(pv_cpu_ops.get_debugreg, GetDR);
659 para_fill(pv_cpu_ops.set_debugreg, SetDR);
660 para_fill(pv_cpu_ops.read_cr0, GetCR0);
661 para_fill(pv_mmu_ops.read_cr2, GetCR2);
662 para_fill(pv_mmu_ops.read_cr3, GetCR3);
663 para_fill(pv_cpu_ops.read_cr4, GetCR4);
664 para_fill(pv_cpu_ops.write_cr0, SetCR0);
665 para_fill(pv_mmu_ops.write_cr2, SetCR2);
666 para_fill(pv_mmu_ops.write_cr3, SetCR3);
667 para_fill(pv_cpu_ops.write_cr4, SetCR4);
Jeremy Fitzhardinge664c7952009-01-30 23:18:41 -0800668
669 para_fill(pv_irq_ops.save_fl.func, GetInterruptMask);
670 para_fill(pv_irq_ops.restore_fl.func, SetInterruptMask);
671 para_fill(pv_irq_ops.irq_disable.func, DisableInterrupts);
672 para_fill(pv_irq_ops.irq_enable.func, EnableInterrupts);
Zachary Amsden772205f2007-03-05 00:30:41 -0800673
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700674 para_fill(pv_cpu_ops.wbinvd, WBINVD);
675 para_fill(pv_cpu_ops.read_tsc, RDTSC);
Zachary Amsden772205f2007-03-05 00:30:41 -0800676
677 /* The following we emulate with trap and emulate for now */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100678 /* paravirt_ops.read_msr = vmi_rdmsr */
679 /* paravirt_ops.write_msr = vmi_wrmsr */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100680 /* paravirt_ops.rdpmc = vmi_rdpmc */
681
Zachary Amsden772205f2007-03-05 00:30:41 -0800682 /* TR interface doesn't pass TR value, wrap */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700683 para_wrap(pv_cpu_ops.load_tr_desc, vmi_set_tr, set_tr, SetTR);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100684
685 /* LDT is special, too */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700686 para_wrap(pv_cpu_ops.set_ldt, vmi_set_ldt, _set_ldt, SetLDT);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100687
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700688 para_fill(pv_cpu_ops.load_gdt, SetGDT);
689 para_fill(pv_cpu_ops.load_idt, SetIDT);
690 para_fill(pv_cpu_ops.store_gdt, GetGDT);
691 para_fill(pv_cpu_ops.store_idt, GetIDT);
692 para_fill(pv_cpu_ops.store_tr, GetTR);
693 pv_cpu_ops.load_tls = vmi_load_tls;
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100694 para_wrap(pv_cpu_ops.write_ldt_entry, vmi_write_ldt_entry,
695 write_ldt_entry, WriteLDTEntry);
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100696 para_wrap(pv_cpu_ops.write_gdt_entry, vmi_write_gdt_entry,
697 write_gdt_entry, WriteGDTEntry);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100698 para_wrap(pv_cpu_ops.write_idt_entry, vmi_write_idt_entry,
699 write_idt_entry, WriteIDTEntry);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100700 para_wrap(pv_cpu_ops.load_sp0, vmi_load_sp0, set_kernel_stack, UpdateKernelStack);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700701 para_fill(pv_cpu_ops.set_iopl_mask, SetIOPLMask);
702 para_fill(pv_cpu_ops.io_delay, IODelay);
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700703
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800704 para_wrap(pv_cpu_ops.start_context_switch, vmi_start_context_switch,
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700705 set_lazy_mode, SetLazyMode);
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800706 para_wrap(pv_cpu_ops.end_context_switch, vmi_end_context_switch,
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700707 set_lazy_mode, SetLazyMode);
708
709 para_wrap(pv_mmu_ops.lazy_mode.enter, vmi_enter_lazy_mmu,
710 set_lazy_mode, SetLazyMode);
Jeremy Fitzhardingeb407fc52009-02-17 23:46:21 -0800711 para_wrap(pv_mmu_ops.lazy_mode.leave, vmi_leave_lazy_mmu,
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700712 set_lazy_mode, SetLazyMode);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100713
Zachary Amsden772205f2007-03-05 00:30:41 -0800714 /* user and kernel flush are just handled with different flags to FlushTLB */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700715 para_wrap(pv_mmu_ops.flush_tlb_user, vmi_flush_tlb_user, _flush_tlb, FlushTLB);
716 para_wrap(pv_mmu_ops.flush_tlb_kernel, vmi_flush_tlb_kernel, _flush_tlb, FlushTLB);
717 para_fill(pv_mmu_ops.flush_tlb_single, InvalPage);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100718
719 /*
720 * Until a standard flag format can be agreed on, we need to
721 * implement these as wrappers in Linux. Get the VMI ROM
722 * function pointers for the two backend calls.
723 */
724#ifdef CONFIG_X86_PAE
725 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxELong);
726 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxELong);
727#else
728 vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxE);
729 vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxE);
730#endif
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100731
Zachary Amsden772205f2007-03-05 00:30:41 -0800732 if (vmi_ops.set_pte) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700733 pv_mmu_ops.set_pte = vmi_set_pte;
734 pv_mmu_ops.set_pte_at = vmi_set_pte_at;
735 pv_mmu_ops.set_pmd = vmi_set_pmd;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100736#ifdef CONFIG_X86_PAE
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700737 pv_mmu_ops.set_pte_atomic = vmi_set_pte_atomic;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700738 pv_mmu_ops.set_pud = vmi_set_pud;
739 pv_mmu_ops.pte_clear = vmi_pte_clear;
740 pv_mmu_ops.pmd_clear = vmi_pmd_clear;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100741#endif
Zachary Amsden772205f2007-03-05 00:30:41 -0800742 }
743
744 if (vmi_ops.update_pte) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700745 pv_mmu_ops.pte_update = vmi_update_pte;
746 pv_mmu_ops.pte_update_defer = vmi_update_pte_defer;
Zachary Amsden772205f2007-03-05 00:30:41 -0800747 }
748
749 vmi_ops.allocate_page = vmi_get_function(VMI_CALL_AllocatePage);
750 if (vmi_ops.allocate_page) {
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700751 pv_mmu_ops.alloc_pte = vmi_allocate_pte;
752 pv_mmu_ops.alloc_pmd = vmi_allocate_pmd;
753 pv_mmu_ops.alloc_pmd_clone = vmi_allocate_pmd_clone;
Zachary Amsden772205f2007-03-05 00:30:41 -0800754 }
755
756 vmi_ops.release_page = vmi_get_function(VMI_CALL_ReleasePage);
757 if (vmi_ops.release_page) {
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700758 pv_mmu_ops.release_pte = vmi_release_pte;
759 pv_mmu_ops.release_pmd = vmi_release_pmd;
Alok Kataria55a8ba42009-02-06 10:29:35 -0800760 pv_mmu_ops.pgd_free = vmi_pgd_free;
Zachary Amsden772205f2007-03-05 00:30:41 -0800761 }
Zachary Amsdeneeef9c62007-05-02 19:27:16 +0200762
763 /* Set linear is needed in all cases */
764 vmi_ops.set_linear_mapping = vmi_get_function(VMI_CALL_SetLinearMapping);
Zachary Amsden772205f2007-03-05 00:30:41 -0800765
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100766 /*
767 * These MUST always be patched. Don't support indirect jumps
768 * through these operations, as the VMI interface may use either
769 * a jump or a call to get to these operations, depending on
770 * the backend. They are performance critical anyway, so requiring
771 * a patch is not a big problem.
772 */
Jeremy Fitzhardinged75cd222008-06-25 00:19:26 -0400773 pv_cpu_ops.irq_enable_sysexit = (void *)0xfeedbab0;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700774 pv_cpu_ops.iret = (void *)0xbadbab0;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100775
776#ifdef CONFIG_SMP
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700777 para_wrap(pv_apic_ops.startup_ipi_hook, vmi_startup_ipi_hook, set_initial_ap_state, SetInitialAPState);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100778#endif
779
780#ifdef CONFIG_X86_LOCAL_APIC
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800781 para_fill(apic->read, APICRead);
782 para_fill(apic->write, APICWrite);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100783#endif
784
785 /*
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100786 * Check for VMI timer functionality by probing for a cycle frequency method
787 */
788 reloc = call_vrom_long_func(vmi_rom, get_reloc, VMI_CALL_GetCycleFrequency);
Zachary Amsden772205f2007-03-05 00:30:41 -0800789 if (!disable_vmi_timer && rel->type != VMI_RELOCATION_NONE) {
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100790 vmi_timer_ops.get_cycle_frequency = (void *)rel->eip;
791 vmi_timer_ops.get_cycle_counter =
792 vmi_get_function(VMI_CALL_GetCycleCounter);
793 vmi_timer_ops.get_wallclock =
794 vmi_get_function(VMI_CALL_GetWallclockTime);
795 vmi_timer_ops.wallclock_updated =
796 vmi_get_function(VMI_CALL_WallclockUpdated);
797 vmi_timer_ops.set_alarm = vmi_get_function(VMI_CALL_SetAlarm);
798 vmi_timer_ops.cancel_alarm =
799 vmi_get_function(VMI_CALL_CancelAlarm);
Thomas Gleixner845b3942009-08-19 15:37:03 +0200800 x86_init.timers.timer_init = vmi_time_init;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100801#ifdef CONFIG_X86_LOCAL_APIC
Thomas Gleixner736deca2009-08-19 12:35:53 +0200802 x86_init.timers.setup_percpu_clockev = vmi_time_bsp_init;
803 x86_cpuinit.setup_percpu_clockev = vmi_time_ap_init;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100804#endif
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700805 pv_time_ops.sched_clock = vmi_sched_clock;
Thomas Gleixner2d826402009-08-20 17:06:25 +0200806 x86_platform.calibrate_tsc = vmi_tsc_khz;
Feng Tang7bd867d2009-09-10 10:48:56 +0800807 x86_platform.get_wallclock = vmi_get_wallclock;
808 x86_platform.set_wallclock = vmi_set_wallclock;
Zachary Amsden772205f2007-03-05 00:30:41 -0800809
810 /* We have true wallclock functions; disable CMOS clock sync */
811 no_sync_cmos_clock = 1;
812 } else {
813 disable_noidle = 1;
814 disable_vmi_timer = 1;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100815 }
Zachary Amsden772205f2007-03-05 00:30:41 -0800816
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700817 para_fill(pv_irq_ops.safe_halt, Halt);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100818
819 /*
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100820 * Alternative instruction rewriting doesn't happen soon enough
821 * to convert VMI_IRET to a call instead of a jump; so we have
822 * to do this before IRQs get reenabled. Fortunately, it is
823 * idempotent.
824 */
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200825 apply_paravirt(__parainstructions, __parainstructions_end);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100826
827 vmi_bringup();
828
829 return 1;
830}
831
832#undef para_fill
833
834void __init vmi_init(void)
835{
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100836 if (!vmi_rom)
837 probe_vmi_rom();
838 else
839 check_vmi_rom(vmi_rom);
840
841 /* In case probing for or validating the ROM failed, basil */
842 if (!vmi_rom)
843 return;
844
845 reserve_top_address(-vmi_rom->virtual_top);
846
Zachary Amsden7507ba32007-03-05 00:30:34 -0800847#ifdef CONFIG_X86_IO_APIC
Zachary Amsden772205f2007-03-05 00:30:41 -0800848 /* This is virtual hardware; timer routing is wired correctly */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100849 no_timer_check = 1;
850#endif
Zachary Amsdenae8d04e2008-12-13 12:36:58 -0800851}
852
Rakib Mullick659d2612009-01-24 01:46:03 +0600853void __init vmi_activate(void)
Zachary Amsdenae8d04e2008-12-13 12:36:58 -0800854{
855 unsigned long flags;
856
857 if (!vmi_rom)
858 return;
859
860 local_irq_save(flags);
861 activate_vmi();
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100862 local_irq_restore(flags & X86_EFLAGS_IF);
863}
864
865static int __init parse_vmi(char *arg)
866{
867 if (!arg)
868 return -EINVAL;
869
Zachary Amsdeneda08b12007-03-05 00:30:38 -0800870 if (!strcmp(arg, "disable_pge")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100871 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PGE);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100872 disable_pge = 1;
873 } else if (!strcmp(arg, "disable_pse")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100874 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PSE);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100875 disable_pse = 1;
876 } else if (!strcmp(arg, "disable_sep")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100877 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_SEP);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100878 disable_sep = 1;
879 } else if (!strcmp(arg, "disable_tsc")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100880 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_TSC);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100881 disable_tsc = 1;
882 } else if (!strcmp(arg, "disable_mtrr")) {
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +0100883 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_MTRR);
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100884 disable_mtrr = 1;
Zachary Amsden772205f2007-03-05 00:30:41 -0800885 } else if (!strcmp(arg, "disable_timer")) {
886 disable_vmi_timer = 1;
887 disable_noidle = 1;
Zachary Amsden7507ba32007-03-05 00:30:34 -0800888 } else if (!strcmp(arg, "disable_noidle"))
889 disable_noidle = 1;
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100890 return 0;
891}
892
893early_param("vmi", parse_vmi);