blob: 596f382c641ce0f5cb59f9e5182019a36b35c505 [file] [log] [blame]
Rusty Russelld3561b72006-12-07 02:14:07 +01001/* Paravirtualization interfaces
2 Copyright (C) 2006 Rusty Russell IBM Corporation
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18#include <linux/errno.h>
19#include <linux/module.h>
20#include <linux/efi.h>
21#include <linux/bcd.h>
Rusty Russellc9ccf302006-12-07 02:14:08 +010022#include <linux/start_kernel.h>
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +020023#include <linux/highmem.h>
Rusty Russelld3561b72006-12-07 02:14:07 +010024
25#include <asm/bug.h>
26#include <asm/paravirt.h>
27#include <asm/desc.h>
28#include <asm/setup.h>
29#include <asm/arch_hooks.h>
30#include <asm/time.h>
31#include <asm/irq.h>
32#include <asm/delay.h>
Rusty Russell13623d72006-12-07 02:14:08 +010033#include <asm/fixmap.h>
34#include <asm/apic.h>
Rusty Russellda181a82006-12-07 02:14:08 +010035#include <asm/tlbflush.h>
Zachary Amsden6cb9a832007-03-05 00:30:35 -080036#include <asm/timer.h>
Rusty Russelld3561b72006-12-07 02:14:07 +010037
38/* nop stub */
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +020039void _paravirt_nop(void)
Rusty Russelld3561b72006-12-07 02:14:07 +010040{
41}
42
43static void __init default_banner(void)
44{
45 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
46 paravirt_ops.name);
47}
48
49char *memory_setup(void)
50{
51 return paravirt_ops.memory_setup();
52}
53
Rusty Russell139ec7c2006-12-07 02:14:08 +010054/* Simple instruction patching code. */
55#define DEF_NATIVE(name, code) \
56 extern const char start_##name[], end_##name[]; \
57 asm("start_" #name ": " code "; end_" #name ":")
Rusty Russell139ec7c2006-12-07 02:14:08 +010058
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +020059DEF_NATIVE(irq_disable, "cli");
60DEF_NATIVE(irq_enable, "sti");
61DEF_NATIVE(restore_fl, "push %eax; popf");
62DEF_NATIVE(save_fl, "pushf; pop %eax");
63DEF_NATIVE(iret, "iret");
64DEF_NATIVE(irq_enable_sysexit, "sti; sysexit");
65DEF_NATIVE(read_cr2, "mov %cr2, %eax");
66DEF_NATIVE(write_cr3, "mov %eax, %cr3");
67DEF_NATIVE(read_cr3, "mov %cr3, %eax");
68DEF_NATIVE(clts, "clts");
69DEF_NATIVE(read_tsc, "rdtsc");
70
71DEF_NATIVE(ud2a, "ud2a");
Rusty Russell139ec7c2006-12-07 02:14:08 +010072
73static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len)
74{
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +020075 const unsigned char *start, *end;
76 unsigned ret;
Rusty Russell139ec7c2006-12-07 02:14:08 +010077
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +020078 switch(type) {
79#define SITE(x) case PARAVIRT_PATCH(x): start = start_##x; end = end_##x; goto patch_site
80 SITE(irq_disable);
81 SITE(irq_enable);
82 SITE(restore_fl);
83 SITE(save_fl);
84 SITE(iret);
85 SITE(irq_enable_sysexit);
86 SITE(read_cr2);
87 SITE(read_cr3);
88 SITE(write_cr3);
89 SITE(clts);
90 SITE(read_tsc);
91#undef SITE
Rusty Russell139ec7c2006-12-07 02:14:08 +010092
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +020093 patch_site:
94 ret = paravirt_patch_insns(insns, len, start, end);
95 break;
Rusty Russell139ec7c2006-12-07 02:14:08 +010096
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +020097 case PARAVIRT_PATCH(make_pgd):
98 case PARAVIRT_PATCH(make_pte):
99 case PARAVIRT_PATCH(pgd_val):
100 case PARAVIRT_PATCH(pte_val):
101#ifdef CONFIG_X86_PAE
102 case PARAVIRT_PATCH(make_pmd):
103 case PARAVIRT_PATCH(pmd_val):
104#endif
105 /* These functions end up returning exactly what
106 they're passed, in the same registers. */
107 ret = paravirt_patch_nop();
108 break;
Rusty Russell139ec7c2006-12-07 02:14:08 +0100109
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200110 default:
111 ret = paravirt_patch_default(type, clobbers, insns, len);
112 break;
113 }
114
115 return ret;
116}
117
118unsigned paravirt_patch_nop(void)
119{
120 return 0;
121}
122
123unsigned paravirt_patch_ignore(unsigned len)
124{
125 return len;
126}
127
128unsigned paravirt_patch_call(void *target, u16 tgt_clobbers,
129 void *site, u16 site_clobbers,
130 unsigned len)
131{
132 unsigned char *call = site;
133 unsigned long delta = (unsigned long)target - (unsigned long)(call+5);
134
135 if (tgt_clobbers & ~site_clobbers)
136 return len; /* target would clobber too much for this site */
137 if (len < 5)
138 return len; /* call too long for patch site */
139
140 *call++ = 0xe8; /* call */
141 *(unsigned long *)call = delta;
142
143 return 5;
144}
145
146unsigned paravirt_patch_jmp(void *target, void *site, unsigned len)
147{
148 unsigned char *jmp = site;
149 unsigned long delta = (unsigned long)target - (unsigned long)(jmp+5);
150
151 if (len < 5)
152 return len; /* call too long for patch site */
153
154 *jmp++ = 0xe9; /* jmp */
155 *(unsigned long *)jmp = delta;
156
157 return 5;
158}
159
160unsigned paravirt_patch_default(u8 type, u16 clobbers, void *site, unsigned len)
161{
162 void *opfunc = *((void **)&paravirt_ops + type);
163 unsigned ret;
164
165 if (opfunc == NULL)
166 /* If there's no function, patch it with a ud2a (BUG) */
167 ret = paravirt_patch_insns(site, len, start_ud2a, end_ud2a);
168 else if (opfunc == paravirt_nop)
169 /* If the operation is a nop, then nop the callsite */
170 ret = paravirt_patch_nop();
171 else if (type == PARAVIRT_PATCH(iret) ||
172 type == PARAVIRT_PATCH(irq_enable_sysexit))
173 /* If operation requires a jmp, then jmp */
174 ret = paravirt_patch_jmp(opfunc, site, len);
175 else
176 /* Otherwise call the function; assume target could
177 clobber any caller-save reg */
178 ret = paravirt_patch_call(opfunc, CLBR_ANY,
179 site, clobbers, len);
180
181 return ret;
182}
183
184unsigned paravirt_patch_insns(void *site, unsigned len,
185 const char *start, const char *end)
186{
187 unsigned insn_len = end - start;
188
189 if (insn_len > len || start == NULL)
190 insn_len = len;
191 else
192 memcpy(site, start, insn_len);
193
Rusty Russell139ec7c2006-12-07 02:14:08 +0100194 return insn_len;
195}
196
Rusty Russelld3561b72006-12-07 02:14:07 +0100197void init_IRQ(void)
198{
199 paravirt_ops.init_IRQ();
200}
201
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100202static void native_flush_tlb(void)
Rusty Russellda181a82006-12-07 02:14:08 +0100203{
204 __native_flush_tlb();
205}
206
207/*
208 * Global pages have to be flushed a bit differently. Not a real
209 * performance problem because this does not happen often.
210 */
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100211static void native_flush_tlb_global(void)
Rusty Russellda181a82006-12-07 02:14:08 +0100212{
213 __native_flush_tlb_global();
214}
215
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200216static void native_flush_tlb_single(unsigned long addr)
Rusty Russellda181a82006-12-07 02:14:08 +0100217{
218 __native_flush_tlb_single(addr);
219}
220
Rusty Russelld3561b72006-12-07 02:14:07 +0100221/* These are in entry.S */
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100222extern void native_iret(void);
223extern void native_irq_enable_sysexit(void);
Rusty Russelld3561b72006-12-07 02:14:07 +0100224
225static int __init print_banner(void)
226{
227 paravirt_ops.banner();
228 return 0;
229}
230core_initcall(print_banner);
231
232struct paravirt_ops paravirt_ops = {
233 .name = "bare hardware",
234 .paravirt_enabled = 0,
235 .kernel_rpl = 0,
Jeremy Fitzhardinge5311ab62007-05-02 19:27:13 +0200236 .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
Rusty Russelld3561b72006-12-07 02:14:07 +0100237
Rusty Russell139ec7c2006-12-07 02:14:08 +0100238 .patch = native_patch,
Rusty Russelld3561b72006-12-07 02:14:07 +0100239 .banner = default_banner,
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +0200240 .arch_setup = paravirt_nop,
Rusty Russelld3561b72006-12-07 02:14:07 +0100241 .memory_setup = machine_specific_memory_setup,
242 .get_wallclock = native_get_wallclock,
243 .set_wallclock = native_set_wallclock,
Zachary Amsdene30fab32007-03-05 00:30:39 -0800244 .time_init = hpet_time_init,
Rusty Russelld3561b72006-12-07 02:14:07 +0100245 .init_IRQ = native_init_IRQ,
246
247 .cpuid = native_cpuid,
248 .get_debugreg = native_get_debugreg,
249 .set_debugreg = native_set_debugreg,
250 .clts = native_clts,
251 .read_cr0 = native_read_cr0,
252 .write_cr0 = native_write_cr0,
253 .read_cr2 = native_read_cr2,
254 .write_cr2 = native_write_cr2,
255 .read_cr3 = native_read_cr3,
256 .write_cr3 = native_write_cr3,
257 .read_cr4 = native_read_cr4,
258 .read_cr4_safe = native_read_cr4_safe,
259 .write_cr4 = native_write_cr4,
260 .save_fl = native_save_fl,
261 .restore_fl = native_restore_fl,
262 .irq_disable = native_irq_disable,
263 .irq_enable = native_irq_enable,
264 .safe_halt = native_safe_halt,
265 .halt = native_halt,
266 .wbinvd = native_wbinvd,
Rusty Russell90a0a062007-05-02 19:27:10 +0200267 .read_msr = native_read_msr_safe,
268 .write_msr = native_write_msr_safe,
Rusty Russelld3561b72006-12-07 02:14:07 +0100269 .read_tsc = native_read_tsc,
270 .read_pmc = native_read_pmc,
Zachary Amsden6cb9a832007-03-05 00:30:35 -0800271 .get_scheduled_cycles = native_read_tsc,
Zachary Amsden1182d852007-03-05 00:30:36 -0800272 .get_cpu_khz = native_calculate_cpu_khz,
Rusty Russelld3561b72006-12-07 02:14:07 +0100273 .load_tr_desc = native_load_tr_desc,
274 .set_ldt = native_set_ldt,
275 .load_gdt = native_load_gdt,
276 .load_idt = native_load_idt,
277 .store_gdt = native_store_gdt,
278 .store_idt = native_store_idt,
279 .store_tr = native_store_tr,
280 .load_tls = native_load_tls,
Rusty Russell90a0a062007-05-02 19:27:10 +0200281 .write_ldt_entry = write_dt_entry,
282 .write_gdt_entry = write_dt_entry,
283 .write_idt_entry = write_dt_entry,
Rusty Russelld3561b72006-12-07 02:14:07 +0100284 .load_esp0 = native_load_esp0,
285
286 .set_iopl_mask = native_set_iopl_mask,
287 .io_delay = native_io_delay,
Rusty Russelld3561b72006-12-07 02:14:07 +0100288
Rusty Russell13623d72006-12-07 02:14:08 +0100289#ifdef CONFIG_X86_LOCAL_APIC
290 .apic_write = native_apic_write,
291 .apic_write_atomic = native_apic_write_atomic,
292 .apic_read = native_apic_read,
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100293 .setup_boot_clock = setup_boot_APIC_clock,
294 .setup_secondary_clock = setup_secondary_APIC_clock,
Rusty Russell13623d72006-12-07 02:14:08 +0100295#endif
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +0200296 .set_lazy_mode = paravirt_nop,
Rusty Russell13623d72006-12-07 02:14:08 +0100297
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200298 .pagetable_setup_start = native_pagetable_setup_start,
299 .pagetable_setup_done = native_pagetable_setup_done,
300
Rusty Russellda181a82006-12-07 02:14:08 +0100301 .flush_tlb_user = native_flush_tlb,
302 .flush_tlb_kernel = native_flush_tlb_global,
303 .flush_tlb_single = native_flush_tlb_single,
Jeremy Fitzhardinged4c10472007-05-02 19:27:15 +0200304 .flush_tlb_others = native_flush_tlb_others,
Rusty Russellda181a82006-12-07 02:14:08 +0100305
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +0200306 .alloc_pt = paravirt_nop,
307 .alloc_pd = paravirt_nop,
308 .alloc_pd_clone = paravirt_nop,
309 .release_pt = paravirt_nop,
310 .release_pd = paravirt_nop,
Zachary Amsdenc119ecc2007-02-13 13:26:21 +0100311
Rusty Russellda181a82006-12-07 02:14:08 +0100312 .set_pte = native_set_pte,
313 .set_pte_at = native_set_pte_at,
314 .set_pmd = native_set_pmd,
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +0200315 .pte_update = paravirt_nop,
316 .pte_update_defer = paravirt_nop,
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200317
318 .ptep_get_and_clear = native_ptep_get_and_clear,
319
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +0200320#ifdef CONFIG_HIGHPTE
321 .kmap_atomic_pte = kmap_atomic,
322#endif
323
Rusty Russellda181a82006-12-07 02:14:08 +0100324#ifdef CONFIG_X86_PAE
325 .set_pte_atomic = native_set_pte_atomic,
326 .set_pte_present = native_set_pte_present,
327 .set_pud = native_set_pud,
328 .pte_clear = native_pte_clear,
329 .pmd_clear = native_pmd_clear,
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200330
331 .pmd_val = native_pmd_val,
332 .make_pmd = native_make_pmd,
Rusty Russellda181a82006-12-07 02:14:08 +0100333#endif
334
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200335 .pte_val = native_pte_val,
336 .pgd_val = native_pgd_val,
337
338 .make_pte = native_make_pte,
339 .make_pgd = native_make_pgd,
340
Rusty Russelld3561b72006-12-07 02:14:07 +0100341 .irq_enable_sysexit = native_irq_enable_sysexit,
342 .iret = native_iret,
Zachary Amsdenae5da272007-02-13 13:26:21 +0100343
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200344 .dup_mmap = paravirt_nop,
345 .exit_mmap = paravirt_nop,
346 .activate_mm = paravirt_nop,
347
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +0200348 .startup_ipi_hook = paravirt_nop,
Rusty Russelld3561b72006-12-07 02:14:07 +0100349};
Ingo Molnar0dbe5a12007-01-22 20:40:36 -0800350
351/*
352 * NOTE: CONFIG_PARAVIRT is experimental and the paravirt_ops
353 * semantics are subject to change. Hence we only do this
354 * internal-only export of this, until it gets sorted out and
355 * all lowlevel CPU ops used by modules are separately exported.
356 */
357EXPORT_SYMBOL_GPL(paravirt_ops);