1 /* Paravirtualization interfaces
2 Copyright (C) 2006 Rusty Russell IBM Corporation
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.
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.
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
18 #include <linux/errno.h>
19 #include <linux/module.h>
20 #include <linux/efi.h>
21 #include <linux/bcd.h>
22 #include <linux/highmem.h>
25 #include <asm/paravirt.h>
27 #include <asm/setup.h>
28 #include <asm/arch_hooks.h>
31 #include <asm/delay.h>
32 #include <asm/fixmap.h>
34 #include <asm/tlbflush.h>
35 #include <asm/timer.h>
38 void _paravirt_nop(void)
42 static void __init default_banner(void)
44 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
48 char *memory_setup(void)
50 return paravirt_ops.memory_setup();
53 /* Simple instruction patching code. */
54 #define DEF_NATIVE(name, code) \
55 extern const char start_##name[], end_##name[]; \
56 asm("start_" #name ": " code "; end_" #name ":")
58 DEF_NATIVE(irq_disable, "cli");
59 DEF_NATIVE(irq_enable, "sti");
60 DEF_NATIVE(restore_fl, "push %eax; popf");
61 DEF_NATIVE(save_fl, "pushf; pop %eax");
62 DEF_NATIVE(iret, "iret");
63 DEF_NATIVE(irq_enable_sysexit, "sti; sysexit");
64 DEF_NATIVE(read_cr2, "mov %cr2, %eax");
65 DEF_NATIVE(write_cr3, "mov %eax, %cr3");
66 DEF_NATIVE(read_cr3, "mov %cr3, %eax");
67 DEF_NATIVE(clts, "clts");
68 DEF_NATIVE(read_tsc, "rdtsc");
70 DEF_NATIVE(ud2a, "ud2a");
72 static unsigned native_patch(u8 type, u16 clobbers, void *ibuf,
73 unsigned long addr, unsigned len)
75 const unsigned char *start, *end;
79 #define SITE(x) case PARAVIRT_PATCH(x): start = start_##x; end = end_##x; goto patch_site
85 SITE(irq_enable_sysexit);
94 ret = paravirt_patch_insns(ibuf, len, start, end);
97 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):
105 /* These functions end up returning exactly what
106 they're passed, in the same registers. */
107 ret = paravirt_patch_nop();
111 ret = paravirt_patch_default(type, clobbers, ibuf, addr, len);
118 unsigned paravirt_patch_nop(void)
123 unsigned paravirt_patch_ignore(unsigned len)
129 unsigned char opcode;
131 } __attribute__((packed));
133 unsigned paravirt_patch_call(void *insnbuf,
134 const void *target, u16 tgt_clobbers,
135 unsigned long addr, u16 site_clobbers,
138 struct branch *b = insnbuf;
139 unsigned long delta = (unsigned long)target - (addr+5);
141 if (tgt_clobbers & ~site_clobbers)
142 return len; /* target would clobber too much for this site */
144 return len; /* call too long for patch site */
146 b->opcode = 0xe8; /* call */
148 BUILD_BUG_ON(sizeof(*b) != 5);
153 unsigned paravirt_patch_jmp(const void *target, void *insnbuf,
154 unsigned long addr, unsigned len)
156 struct branch *b = insnbuf;
157 unsigned long delta = (unsigned long)target - (addr+5);
160 return len; /* call too long for patch site */
162 b->opcode = 0xe9; /* jmp */
168 unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
169 unsigned long addr, unsigned len)
171 void *opfunc = *((void **)¶virt_ops + type);
175 /* If there's no function, patch it with a ud2a (BUG) */
176 ret = paravirt_patch_insns(insnbuf, len, start_ud2a, end_ud2a);
177 else if (opfunc == paravirt_nop)
178 /* If the operation is a nop, then nop the callsite */
179 ret = paravirt_patch_nop();
180 else if (type == PARAVIRT_PATCH(iret) ||
181 type == PARAVIRT_PATCH(irq_enable_sysexit))
182 /* If operation requires a jmp, then jmp */
183 ret = paravirt_patch_jmp(opfunc, insnbuf, addr, len);
185 /* Otherwise call the function; assume target could
186 clobber any caller-save reg */
187 ret = paravirt_patch_call(insnbuf, opfunc, CLBR_ANY,
188 addr, clobbers, len);
193 unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
194 const char *start, const char *end)
196 unsigned insn_len = end - start;
198 if (insn_len > len || start == NULL)
201 memcpy(insnbuf, start, insn_len);
208 paravirt_ops.init_IRQ();
211 static void native_flush_tlb(void)
213 __native_flush_tlb();
217 * Global pages have to be flushed a bit differently. Not a real
218 * performance problem because this does not happen often.
220 static void native_flush_tlb_global(void)
222 __native_flush_tlb_global();
225 static void native_flush_tlb_single(unsigned long addr)
227 __native_flush_tlb_single(addr);
230 /* These are in entry.S */
231 extern void native_iret(void);
232 extern void native_irq_enable_sysexit(void);
234 static int __init print_banner(void)
236 paravirt_ops.banner();
239 core_initcall(print_banner);
241 static struct resource reserve_ioports = {
243 .end = IO_SPACE_LIMIT,
244 .name = "paravirt-ioport",
245 .flags = IORESOURCE_IO | IORESOURCE_BUSY,
248 static struct resource reserve_iomem = {
251 .name = "paravirt-iomem",
252 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
256 * Reserve the whole legacy IO space to prevent any legacy drivers
257 * from wasting time probing for their hardware. This is a fairly
258 * brute-force approach to disabling all non-virtual drivers.
260 * Note that this must be called very early to have any effect.
262 int paravirt_disable_iospace(void)
266 ret = request_resource(&ioport_resource, &reserve_ioports);
268 ret = request_resource(&iomem_resource, &reserve_iomem);
270 release_resource(&reserve_ioports);
276 struct paravirt_ops paravirt_ops = {
277 .name = "bare hardware",
278 .paravirt_enabled = 0,
280 .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
282 .patch = native_patch,
283 .banner = default_banner,
284 .arch_setup = paravirt_nop,
285 .memory_setup = machine_specific_memory_setup,
286 .get_wallclock = native_get_wallclock,
287 .set_wallclock = native_set_wallclock,
288 .time_init = hpet_time_init,
289 .init_IRQ = native_init_IRQ,
291 .cpuid = native_cpuid,
292 .get_debugreg = native_get_debugreg,
293 .set_debugreg = native_set_debugreg,
295 .read_cr0 = native_read_cr0,
296 .write_cr0 = native_write_cr0,
297 .read_cr2 = native_read_cr2,
298 .write_cr2 = native_write_cr2,
299 .read_cr3 = native_read_cr3,
300 .write_cr3 = native_write_cr3,
301 .read_cr4 = native_read_cr4,
302 .read_cr4_safe = native_read_cr4_safe,
303 .write_cr4 = native_write_cr4,
304 .save_fl = native_save_fl,
305 .restore_fl = native_restore_fl,
306 .irq_disable = native_irq_disable,
307 .irq_enable = native_irq_enable,
308 .safe_halt = native_safe_halt,
310 .wbinvd = native_wbinvd,
311 .read_msr = native_read_msr_safe,
312 .write_msr = native_write_msr_safe,
313 .read_tsc = native_read_tsc,
314 .read_pmc = native_read_pmc,
315 .sched_clock = native_sched_clock,
316 .get_cpu_khz = native_calculate_cpu_khz,
317 .load_tr_desc = native_load_tr_desc,
318 .set_ldt = native_set_ldt,
319 .load_gdt = native_load_gdt,
320 .load_idt = native_load_idt,
321 .store_gdt = native_store_gdt,
322 .store_idt = native_store_idt,
323 .store_tr = native_store_tr,
324 .load_tls = native_load_tls,
325 .write_ldt_entry = write_dt_entry,
326 .write_gdt_entry = write_dt_entry,
327 .write_idt_entry = write_dt_entry,
328 .load_esp0 = native_load_esp0,
330 .set_iopl_mask = native_set_iopl_mask,
331 .io_delay = native_io_delay,
333 #ifdef CONFIG_X86_LOCAL_APIC
334 .apic_write = native_apic_write,
335 .apic_write_atomic = native_apic_write_atomic,
336 .apic_read = native_apic_read,
337 .setup_boot_clock = setup_boot_APIC_clock,
338 .setup_secondary_clock = setup_secondary_APIC_clock,
339 .startup_ipi_hook = paravirt_nop,
341 .set_lazy_mode = paravirt_nop,
343 .pagetable_setup_start = native_pagetable_setup_start,
344 .pagetable_setup_done = native_pagetable_setup_done,
346 .flush_tlb_user = native_flush_tlb,
347 .flush_tlb_kernel = native_flush_tlb_global,
348 .flush_tlb_single = native_flush_tlb_single,
349 .flush_tlb_others = native_flush_tlb_others,
351 .alloc_pt = paravirt_nop,
352 .alloc_pd = paravirt_nop,
353 .alloc_pd_clone = paravirt_nop,
354 .release_pt = paravirt_nop,
355 .release_pd = paravirt_nop,
357 .set_pte = native_set_pte,
358 .set_pte_at = native_set_pte_at,
359 .set_pmd = native_set_pmd,
360 .pte_update = paravirt_nop,
361 .pte_update_defer = paravirt_nop,
363 #ifdef CONFIG_HIGHPTE
364 .kmap_atomic_pte = kmap_atomic,
367 #ifdef CONFIG_X86_PAE
368 .set_pte_atomic = native_set_pte_atomic,
369 .set_pte_present = native_set_pte_present,
370 .set_pud = native_set_pud,
371 .pte_clear = native_pte_clear,
372 .pmd_clear = native_pmd_clear,
374 .pmd_val = native_pmd_val,
375 .make_pmd = native_make_pmd,
378 .pte_val = native_pte_val,
379 .pgd_val = native_pgd_val,
381 .make_pte = native_make_pte,
382 .make_pgd = native_make_pgd,
384 .irq_enable_sysexit = native_irq_enable_sysexit,
387 .dup_mmap = paravirt_nop,
388 .exit_mmap = paravirt_nop,
389 .activate_mm = paravirt_nop,
392 EXPORT_SYMBOL(paravirt_ops);