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/start_kernel.h>
23 #include <linux/highmem.h>
26 #include <asm/paravirt.h>
28 #include <asm/setup.h>
29 #include <asm/arch_hooks.h>
32 #include <asm/delay.h>
33 #include <asm/fixmap.h>
35 #include <asm/tlbflush.h>
36 #include <asm/timer.h>
39 void _paravirt_nop(void)
43 static void __init
default_banner(void)
45 printk(KERN_INFO
"Booting paravirtualized kernel on %s\n",
49 char *memory_setup(void)
51 return paravirt_ops
.memory_setup();
54 /* 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 ":")
59 DEF_NATIVE(irq_disable
, "cli");
60 DEF_NATIVE(irq_enable
, "sti");
61 DEF_NATIVE(restore_fl
, "push %eax; popf");
62 DEF_NATIVE(save_fl
, "pushf; pop %eax");
63 DEF_NATIVE(iret
, "iret");
64 DEF_NATIVE(irq_enable_sysexit
, "sti; sysexit");
65 DEF_NATIVE(read_cr2
, "mov %cr2, %eax");
66 DEF_NATIVE(write_cr3
, "mov %eax, %cr3");
67 DEF_NATIVE(read_cr3
, "mov %cr3, %eax");
68 DEF_NATIVE(clts
, "clts");
69 DEF_NATIVE(read_tsc
, "rdtsc");
71 DEF_NATIVE(ud2a
, "ud2a");
73 static unsigned native_patch(u8 type
, u16 clobbers
, void *insns
, 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(insns
, 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
, insns
, len
);
118 unsigned paravirt_patch_nop(void)
123 unsigned paravirt_patch_ignore(unsigned len
)
128 unsigned paravirt_patch_call(void *target
, u16 tgt_clobbers
,
129 void *site
, u16 site_clobbers
,
132 unsigned char *call
= site
;
133 unsigned long delta
= (unsigned long)target
- (unsigned long)(call
+5);
135 if (tgt_clobbers
& ~site_clobbers
)
136 return len
; /* target would clobber too much for this site */
138 return len
; /* call too long for patch site */
140 *call
++ = 0xe8; /* call */
141 *(unsigned long *)call
= delta
;
146 unsigned paravirt_patch_jmp(void *target
, void *site
, unsigned len
)
148 unsigned char *jmp
= site
;
149 unsigned long delta
= (unsigned long)target
- (unsigned long)(jmp
+5);
152 return len
; /* call too long for patch site */
154 *jmp
++ = 0xe9; /* jmp */
155 *(unsigned long *)jmp
= delta
;
160 unsigned paravirt_patch_default(u8 type
, u16 clobbers
, void *site
, unsigned len
)
162 void *opfunc
= *((void **)¶virt_ops
+ type
);
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
);
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
);
184 unsigned paravirt_patch_insns(void *site
, unsigned len
,
185 const char *start
, const char *end
)
187 unsigned insn_len
= end
- start
;
189 if (insn_len
> len
|| start
== NULL
)
192 memcpy(site
, start
, insn_len
);
199 paravirt_ops
.init_IRQ();
202 static void native_flush_tlb(void)
204 __native_flush_tlb();
208 * Global pages have to be flushed a bit differently. Not a real
209 * performance problem because this does not happen often.
211 static void native_flush_tlb_global(void)
213 __native_flush_tlb_global();
216 static void native_flush_tlb_single(unsigned long addr
)
218 __native_flush_tlb_single(addr
);
221 /* These are in entry.S */
222 extern void native_iret(void);
223 extern void native_irq_enable_sysexit(void);
225 static int __init
print_banner(void)
227 paravirt_ops
.banner();
230 core_initcall(print_banner
);
232 struct paravirt_ops paravirt_ops
= {
233 .name
= "bare hardware",
234 .paravirt_enabled
= 0,
236 .shared_kernel_pmd
= 1, /* Only used when CONFIG_X86_PAE is set */
238 .patch
= native_patch
,
239 .banner
= default_banner
,
240 .arch_setup
= paravirt_nop
,
241 .memory_setup
= machine_specific_memory_setup
,
242 .get_wallclock
= native_get_wallclock
,
243 .set_wallclock
= native_set_wallclock
,
244 .time_init
= hpet_time_init
,
245 .init_IRQ
= native_init_IRQ
,
247 .cpuid
= native_cpuid
,
248 .get_debugreg
= native_get_debugreg
,
249 .set_debugreg
= native_set_debugreg
,
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
,
266 .wbinvd
= native_wbinvd
,
267 .read_msr
= native_read_msr_safe
,
268 .write_msr
= native_write_msr_safe
,
269 .read_tsc
= native_read_tsc
,
270 .read_pmc
= native_read_pmc
,
271 .get_scheduled_cycles
= native_read_tsc
,
272 .get_cpu_khz
= native_calculate_cpu_khz
,
273 .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
,
281 .write_ldt_entry
= write_dt_entry
,
282 .write_gdt_entry
= write_dt_entry
,
283 .write_idt_entry
= write_dt_entry
,
284 .load_esp0
= native_load_esp0
,
286 .set_iopl_mask
= native_set_iopl_mask
,
287 .io_delay
= native_io_delay
,
289 #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
,
293 .setup_boot_clock
= setup_boot_APIC_clock
,
294 .setup_secondary_clock
= setup_secondary_APIC_clock
,
296 .set_lazy_mode
= paravirt_nop
,
298 .pagetable_setup_start
= native_pagetable_setup_start
,
299 .pagetable_setup_done
= native_pagetable_setup_done
,
301 .flush_tlb_user
= native_flush_tlb
,
302 .flush_tlb_kernel
= native_flush_tlb_global
,
303 .flush_tlb_single
= native_flush_tlb_single
,
304 .flush_tlb_others
= native_flush_tlb_others
,
306 .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
,
312 .set_pte
= native_set_pte
,
313 .set_pte_at
= native_set_pte_at
,
314 .set_pmd
= native_set_pmd
,
315 .pte_update
= paravirt_nop
,
316 .pte_update_defer
= paravirt_nop
,
318 #ifdef CONFIG_HIGHPTE
319 .kmap_atomic_pte
= kmap_atomic
,
322 #ifdef CONFIG_X86_PAE
323 .set_pte_atomic
= native_set_pte_atomic
,
324 .set_pte_present
= native_set_pte_present
,
325 .set_pud
= native_set_pud
,
326 .pte_clear
= native_pte_clear
,
327 .pmd_clear
= native_pmd_clear
,
329 .pmd_val
= native_pmd_val
,
330 .make_pmd
= native_make_pmd
,
333 .pte_val
= native_pte_val
,
334 .pgd_val
= native_pgd_val
,
336 .make_pte
= native_make_pte
,
337 .make_pgd
= native_make_pgd
,
339 .irq_enable_sysexit
= native_irq_enable_sysexit
,
342 .dup_mmap
= paravirt_nop
,
343 .exit_mmap
= paravirt_nop
,
344 .activate_mm
= paravirt_nop
,
346 .startup_ipi_hook
= paravirt_nop
,
350 * NOTE: CONFIG_PARAVIRT is experimental and the paravirt_ops
351 * semantics are subject to change. Hence we only do this
352 * internal-only export of this, until it gets sorted out and
353 * all lowlevel CPU ops used by modules are separately exported.
355 EXPORT_SYMBOL_GPL(paravirt_ops
);