blob: fa412515af795e878b9aa0ef90f81d107680d636 [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>
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +020022#include <linux/highmem.h>
Rusty Russelld3561b72006-12-07 02:14:07 +010023
24#include <asm/bug.h>
25#include <asm/paravirt.h>
26#include <asm/desc.h>
27#include <asm/setup.h>
28#include <asm/arch_hooks.h>
29#include <asm/time.h>
30#include <asm/irq.h>
31#include <asm/delay.h>
Rusty Russell13623d72006-12-07 02:14:08 +010032#include <asm/fixmap.h>
33#include <asm/apic.h>
Rusty Russellda181a82006-12-07 02:14:08 +010034#include <asm/tlbflush.h>
Zachary Amsden6cb9a832007-03-05 00:30:35 -080035#include <asm/timer.h>
Rusty Russelld3561b72006-12-07 02:14:07 +010036
37/* nop stub */
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +020038void _paravirt_nop(void)
Rusty Russelld3561b72006-12-07 02:14:07 +010039{
40}
41
42static void __init default_banner(void)
43{
44 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070045 pv_info.name);
Rusty Russelld3561b72006-12-07 02:14:07 +010046}
47
48char *memory_setup(void)
49{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070050 return pv_init_ops.memory_setup();
Rusty Russelld3561b72006-12-07 02:14:07 +010051}
52
Rusty Russell139ec7c2006-12-07 02:14:08 +010053/* Simple instruction patching code. */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070054#define DEF_NATIVE(ops, name, code) \
55 extern const char start_##ops##_##name[], end_##ops##_##name[]; \
56 asm("start_" #ops "_" #name ": " code "; end_" #ops "_" #name ":")
Rusty Russell139ec7c2006-12-07 02:14:08 +010057
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070058DEF_NATIVE(pv_irq_ops, irq_disable, "cli");
59DEF_NATIVE(pv_irq_ops, irq_enable, "sti");
60DEF_NATIVE(pv_irq_ops, restore_fl, "push %eax; popf");
61DEF_NATIVE(pv_irq_ops, save_fl, "pushf; pop %eax");
62DEF_NATIVE(pv_cpu_ops, iret, "iret");
63DEF_NATIVE(pv_cpu_ops, irq_enable_sysexit, "sti; sysexit");
64DEF_NATIVE(pv_mmu_ops, read_cr2, "mov %cr2, %eax");
65DEF_NATIVE(pv_mmu_ops, write_cr3, "mov %eax, %cr3");
66DEF_NATIVE(pv_mmu_ops, read_cr3, "mov %cr3, %eax");
67DEF_NATIVE(pv_cpu_ops, clts, "clts");
68DEF_NATIVE(pv_cpu_ops, read_tsc, "rdtsc");
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +020069
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070070/* Undefined instruction for dealing with missing ops pointers. */
71static const unsigned char ud2a[] = { 0x0f, 0x0b };
Rusty Russell139ec7c2006-12-07 02:14:08 +010072
Andi Kleenab144f52007-08-10 22:31:03 +020073static unsigned native_patch(u8 type, u16 clobbers, void *ibuf,
74 unsigned long addr, unsigned len)
Rusty Russell139ec7c2006-12-07 02:14:08 +010075{
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +020076 const unsigned char *start, *end;
77 unsigned ret;
Rusty Russell139ec7c2006-12-07 02:14:08 +010078
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +020079 switch(type) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070080#define SITE(ops, x) \
81 case PARAVIRT_PATCH(ops.x): \
82 start = start_##ops##_##x; \
83 end = end_##ops##_##x; \
84 goto patch_site
85
86 SITE(pv_irq_ops, irq_disable);
87 SITE(pv_irq_ops, irq_enable);
88 SITE(pv_irq_ops, restore_fl);
89 SITE(pv_irq_ops, save_fl);
90 SITE(pv_cpu_ops, iret);
91 SITE(pv_cpu_ops, irq_enable_sysexit);
92 SITE(pv_mmu_ops, read_cr2);
93 SITE(pv_mmu_ops, read_cr3);
94 SITE(pv_mmu_ops, write_cr3);
95 SITE(pv_cpu_ops, clts);
96 SITE(pv_cpu_ops, read_tsc);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +020097#undef SITE
Rusty Russell139ec7c2006-12-07 02:14:08 +010098
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +020099 patch_site:
Andi Kleenab144f52007-08-10 22:31:03 +0200100 ret = paravirt_patch_insns(ibuf, len, start, end);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200101 break;
Rusty Russell139ec7c2006-12-07 02:14:08 +0100102
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200103 default:
Andi Kleenab144f52007-08-10 22:31:03 +0200104 ret = paravirt_patch_default(type, clobbers, ibuf, addr, len);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200105 break;
106 }
107
108 return ret;
109}
110
111unsigned paravirt_patch_nop(void)
112{
113 return 0;
114}
115
116unsigned paravirt_patch_ignore(unsigned len)
117{
118 return len;
119}
120
Andi Kleen19d36cc2007-07-22 11:12:31 +0200121struct branch {
122 unsigned char opcode;
123 u32 delta;
124} __attribute__((packed));
125
Andi Kleenab144f52007-08-10 22:31:03 +0200126unsigned paravirt_patch_call(void *insnbuf,
127 const void *target, u16 tgt_clobbers,
128 unsigned long addr, u16 site_clobbers,
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200129 unsigned len)
130{
Andi Kleenab144f52007-08-10 22:31:03 +0200131 struct branch *b = insnbuf;
132 unsigned long delta = (unsigned long)target - (addr+5);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200133
134 if (tgt_clobbers & ~site_clobbers)
135 return len; /* target would clobber too much for this site */
136 if (len < 5)
137 return len; /* call too long for patch site */
138
Andi Kleenab144f52007-08-10 22:31:03 +0200139 b->opcode = 0xe8; /* call */
140 b->delta = delta;
141 BUILD_BUG_ON(sizeof(*b) != 5);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200142
143 return 5;
144}
145
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700146unsigned paravirt_patch_jmp(void *insnbuf, const void *target,
Andi Kleenab144f52007-08-10 22:31:03 +0200147 unsigned long addr, unsigned len)
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200148{
Andi Kleenab144f52007-08-10 22:31:03 +0200149 struct branch *b = insnbuf;
150 unsigned long delta = (unsigned long)target - (addr+5);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200151
152 if (len < 5)
153 return len; /* call too long for patch site */
154
Andi Kleenab144f52007-08-10 22:31:03 +0200155 b->opcode = 0xe9; /* jmp */
156 b->delta = delta;
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200157
158 return 5;
159}
160
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700161/* Neat trick to map patch type back to the call within the
162 * corresponding structure. */
163static void *get_call_destination(u8 type)
164{
165 struct paravirt_patch_template tmpl = {
166 .pv_init_ops = pv_init_ops,
167 .pv_misc_ops = pv_misc_ops,
168 .pv_time_ops = pv_time_ops,
169 .pv_cpu_ops = pv_cpu_ops,
170 .pv_irq_ops = pv_irq_ops,
171 .pv_apic_ops = pv_apic_ops,
172 .pv_mmu_ops = pv_mmu_ops,
173 };
174 return *((void **)&tmpl + type);
175}
176
Andi Kleenab144f52007-08-10 22:31:03 +0200177unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
178 unsigned long addr, unsigned len)
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200179{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700180 void *opfunc = get_call_destination(type);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200181 unsigned ret;
182
183 if (opfunc == NULL)
184 /* If there's no function, patch it with a ud2a (BUG) */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700185 ret = paravirt_patch_insns(insnbuf, len, ud2a, ud2a+sizeof(ud2a));
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200186 else if (opfunc == paravirt_nop)
187 /* If the operation is a nop, then nop the callsite */
188 ret = paravirt_patch_nop();
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700189 else if (type == PARAVIRT_PATCH(pv_cpu_ops.iret) ||
190 type == PARAVIRT_PATCH(pv_cpu_ops.irq_enable_sysexit))
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200191 /* If operation requires a jmp, then jmp */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700192 ret = paravirt_patch_jmp(insnbuf, opfunc, addr, len);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200193 else
194 /* Otherwise call the function; assume target could
195 clobber any caller-save reg */
Andi Kleenab144f52007-08-10 22:31:03 +0200196 ret = paravirt_patch_call(insnbuf, opfunc, CLBR_ANY,
197 addr, clobbers, len);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200198
199 return ret;
200}
201
Andi Kleenab144f52007-08-10 22:31:03 +0200202unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200203 const char *start, const char *end)
204{
205 unsigned insn_len = end - start;
206
207 if (insn_len > len || start == NULL)
208 insn_len = len;
209 else
Andi Kleenab144f52007-08-10 22:31:03 +0200210 memcpy(insnbuf, start, insn_len);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200211
Rusty Russell139ec7c2006-12-07 02:14:08 +0100212 return insn_len;
213}
214
Rusty Russelld3561b72006-12-07 02:14:07 +0100215void init_IRQ(void)
216{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700217 pv_irq_ops.init_IRQ();
Rusty Russelld3561b72006-12-07 02:14:07 +0100218}
219
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100220static void native_flush_tlb(void)
Rusty Russellda181a82006-12-07 02:14:08 +0100221{
222 __native_flush_tlb();
223}
224
225/*
226 * Global pages have to be flushed a bit differently. Not a real
227 * performance problem because this does not happen often.
228 */
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100229static void native_flush_tlb_global(void)
Rusty Russellda181a82006-12-07 02:14:08 +0100230{
231 __native_flush_tlb_global();
232}
233
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200234static void native_flush_tlb_single(unsigned long addr)
Rusty Russellda181a82006-12-07 02:14:08 +0100235{
236 __native_flush_tlb_single(addr);
237}
238
Rusty Russelld3561b72006-12-07 02:14:07 +0100239/* These are in entry.S */
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100240extern void native_iret(void);
241extern void native_irq_enable_sysexit(void);
Rusty Russelld3561b72006-12-07 02:14:07 +0100242
243static int __init print_banner(void)
244{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700245 pv_init_ops.banner();
Rusty Russelld3561b72006-12-07 02:14:07 +0100246 return 0;
247}
248core_initcall(print_banner);
249
Jeremy Fitzhardinged5729292007-07-17 18:37:04 -0700250static struct resource reserve_ioports = {
251 .start = 0,
252 .end = IO_SPACE_LIMIT,
253 .name = "paravirt-ioport",
254 .flags = IORESOURCE_IO | IORESOURCE_BUSY,
255};
256
257static struct resource reserve_iomem = {
258 .start = 0,
259 .end = -1,
260 .name = "paravirt-iomem",
261 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
262};
263
264/*
265 * Reserve the whole legacy IO space to prevent any legacy drivers
266 * from wasting time probing for their hardware. This is a fairly
267 * brute-force approach to disabling all non-virtual drivers.
268 *
269 * Note that this must be called very early to have any effect.
270 */
271int paravirt_disable_iospace(void)
272{
273 int ret;
274
275 ret = request_resource(&ioport_resource, &reserve_ioports);
276 if (ret == 0) {
277 ret = request_resource(&iomem_resource, &reserve_iomem);
278 if (ret)
279 release_resource(&reserve_ioports);
280 }
281
282 return ret;
283}
284
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700285struct pv_info pv_info = {
Rusty Russelld3561b72006-12-07 02:14:07 +0100286 .name = "bare hardware",
287 .paravirt_enabled = 0,
288 .kernel_rpl = 0,
Jeremy Fitzhardinge5311ab62007-05-02 19:27:13 +0200289 .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700290};
Rusty Russelld3561b72006-12-07 02:14:07 +0100291
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700292struct pv_init_ops pv_init_ops = {
293 .patch = native_patch,
Rusty Russelld3561b72006-12-07 02:14:07 +0100294 .banner = default_banner,
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +0200295 .arch_setup = paravirt_nop,
Rusty Russelld3561b72006-12-07 02:14:07 +0100296 .memory_setup = machine_specific_memory_setup,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700297};
298
299struct pv_time_ops pv_time_ops = {
300 .time_init = hpet_time_init,
Rusty Russelld3561b72006-12-07 02:14:07 +0100301 .get_wallclock = native_get_wallclock,
302 .set_wallclock = native_set_wallclock,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700303 .sched_clock = native_sched_clock,
304 .get_cpu_khz = native_calculate_cpu_khz,
305};
Rusty Russelld3561b72006-12-07 02:14:07 +0100306
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700307struct pv_irq_ops pv_irq_ops = {
308 .init_IRQ = native_init_IRQ,
Rusty Russelld3561b72006-12-07 02:14:07 +0100309 .save_fl = native_save_fl,
310 .restore_fl = native_restore_fl,
311 .irq_disable = native_irq_disable,
312 .irq_enable = native_irq_enable,
313 .safe_halt = native_safe_halt,
314 .halt = native_halt,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700315};
316
317struct pv_cpu_ops pv_cpu_ops = {
318 .cpuid = native_cpuid,
319 .get_debugreg = native_get_debugreg,
320 .set_debugreg = native_set_debugreg,
321 .clts = native_clts,
322 .read_cr0 = native_read_cr0,
323 .write_cr0 = native_write_cr0,
324 .read_cr4 = native_read_cr4,
325 .read_cr4_safe = native_read_cr4_safe,
326 .write_cr4 = native_write_cr4,
Rusty Russelld3561b72006-12-07 02:14:07 +0100327 .wbinvd = native_wbinvd,
Rusty Russell90a0a062007-05-02 19:27:10 +0200328 .read_msr = native_read_msr_safe,
329 .write_msr = native_write_msr_safe,
Rusty Russelld3561b72006-12-07 02:14:07 +0100330 .read_tsc = native_read_tsc,
331 .read_pmc = native_read_pmc,
332 .load_tr_desc = native_load_tr_desc,
333 .set_ldt = native_set_ldt,
334 .load_gdt = native_load_gdt,
335 .load_idt = native_load_idt,
336 .store_gdt = native_store_gdt,
337 .store_idt = native_store_idt,
338 .store_tr = native_store_tr,
339 .load_tls = native_load_tls,
Rusty Russell90a0a062007-05-02 19:27:10 +0200340 .write_ldt_entry = write_dt_entry,
341 .write_gdt_entry = write_dt_entry,
342 .write_idt_entry = write_dt_entry,
Rusty Russelld3561b72006-12-07 02:14:07 +0100343 .load_esp0 = native_load_esp0,
344
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700345 .irq_enable_sysexit = native_irq_enable_sysexit,
346 .iret = native_iret,
347
Rusty Russelld3561b72006-12-07 02:14:07 +0100348 .set_iopl_mask = native_set_iopl_mask,
349 .io_delay = native_io_delay,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700350};
Rusty Russelld3561b72006-12-07 02:14:07 +0100351
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700352struct pv_apic_ops pv_apic_ops = {
Rusty Russell13623d72006-12-07 02:14:08 +0100353#ifdef CONFIG_X86_LOCAL_APIC
354 .apic_write = native_apic_write,
355 .apic_write_atomic = native_apic_write_atomic,
356 .apic_read = native_apic_read,
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100357 .setup_boot_clock = setup_boot_APIC_clock,
358 .setup_secondary_clock = setup_secondary_APIC_clock,
Jeremy Fitzhardinge0260c192007-05-02 19:27:18 +0200359 .startup_ipi_hook = paravirt_nop,
Rusty Russell13623d72006-12-07 02:14:08 +0100360#endif
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700361};
Rusty Russell13623d72006-12-07 02:14:08 +0100362
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700363struct pv_misc_ops pv_misc_ops = {
364 .set_lazy_mode = paravirt_nop,
365};
366
367struct pv_mmu_ops pv_mmu_ops = {
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200368 .pagetable_setup_start = native_pagetable_setup_start,
369 .pagetable_setup_done = native_pagetable_setup_done,
370
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700371 .read_cr2 = native_read_cr2,
372 .write_cr2 = native_write_cr2,
373 .read_cr3 = native_read_cr3,
374 .write_cr3 = native_write_cr3,
375
Rusty Russellda181a82006-12-07 02:14:08 +0100376 .flush_tlb_user = native_flush_tlb,
377 .flush_tlb_kernel = native_flush_tlb_global,
378 .flush_tlb_single = native_flush_tlb_single,
Jeremy Fitzhardinged4c10472007-05-02 19:27:15 +0200379 .flush_tlb_others = native_flush_tlb_others,
Rusty Russellda181a82006-12-07 02:14:08 +0100380
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +0200381 .alloc_pt = paravirt_nop,
382 .alloc_pd = paravirt_nop,
383 .alloc_pd_clone = paravirt_nop,
384 .release_pt = paravirt_nop,
385 .release_pd = paravirt_nop,
Zachary Amsdenc119ecc2007-02-13 13:26:21 +0100386
Rusty Russellda181a82006-12-07 02:14:08 +0100387 .set_pte = native_set_pte,
388 .set_pte_at = native_set_pte_at,
389 .set_pmd = native_set_pmd,
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +0200390 .pte_update = paravirt_nop,
391 .pte_update_defer = paravirt_nop,
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200392
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +0200393#ifdef CONFIG_HIGHPTE
394 .kmap_atomic_pte = kmap_atomic,
395#endif
396
Rusty Russellda181a82006-12-07 02:14:08 +0100397#ifdef CONFIG_X86_PAE
398 .set_pte_atomic = native_set_pte_atomic,
399 .set_pte_present = native_set_pte_present,
400 .set_pud = native_set_pud,
401 .pte_clear = native_pte_clear,
402 .pmd_clear = native_pmd_clear,
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200403
404 .pmd_val = native_pmd_val,
405 .make_pmd = native_make_pmd,
Rusty Russellda181a82006-12-07 02:14:08 +0100406#endif
407
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200408 .pte_val = native_pte_val,
409 .pgd_val = native_pgd_val,
410
411 .make_pte = native_make_pte,
412 .make_pgd = native_make_pgd,
413
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200414 .dup_mmap = paravirt_nop,
415 .exit_mmap = paravirt_nop,
416 .activate_mm = paravirt_nop,
Rusty Russelld3561b72006-12-07 02:14:07 +0100417};
Ingo Molnar0dbe5a12007-01-22 20:40:36 -0800418
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700419EXPORT_SYMBOL_GPL(pv_time_ops);
420EXPORT_SYMBOL_GPL(pv_cpu_ops);
421EXPORT_SYMBOL_GPL(pv_mmu_ops);
422EXPORT_SYMBOL_GPL(pv_apic_ops);
423EXPORT_SYMBOL_GPL(pv_info);
424EXPORT_SYMBOL (pv_irq_ops);