x86: Fix alternatives and kprobes to remap write-protected kernel text
[linux-2.6/kmemtrace.git] / arch / i386 / kernel / paravirt.c
blob79c167fcaee9d97558c173bde5647bdd83445ebb
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>
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>
32 #include <asm/fixmap.h>
33 #include <asm/apic.h>
34 #include <asm/tlbflush.h>
35 #include <asm/timer.h>
37 /* nop stub */
38 void _paravirt_nop(void)
42 static void __init default_banner(void)
44 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
45 paravirt_ops.name);
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 *insns, unsigned len)
74 const unsigned char *start, *end;
75 unsigned ret;
77 switch(type) {
78 #define SITE(x) case PARAVIRT_PATCH(x): start = start_##x; end = end_##x; goto patch_site
79 SITE(irq_disable);
80 SITE(irq_enable);
81 SITE(restore_fl);
82 SITE(save_fl);
83 SITE(iret);
84 SITE(irq_enable_sysexit);
85 SITE(read_cr2);
86 SITE(read_cr3);
87 SITE(write_cr3);
88 SITE(clts);
89 SITE(read_tsc);
90 #undef SITE
92 patch_site:
93 ret = paravirt_patch_insns(insns, len, start, end);
94 break;
96 case PARAVIRT_PATCH(make_pgd):
97 case PARAVIRT_PATCH(make_pte):
98 case PARAVIRT_PATCH(pgd_val):
99 case PARAVIRT_PATCH(pte_val):
100 #ifdef CONFIG_X86_PAE
101 case PARAVIRT_PATCH(make_pmd):
102 case PARAVIRT_PATCH(pmd_val):
103 #endif
104 /* These functions end up returning exactly what
105 they're passed, in the same registers. */
106 ret = paravirt_patch_nop();
107 break;
109 default:
110 ret = paravirt_patch_default(type, clobbers, insns, len);
111 break;
114 return ret;
117 unsigned paravirt_patch_nop(void)
119 return 0;
122 unsigned paravirt_patch_ignore(unsigned len)
124 return len;
127 struct branch {
128 unsigned char opcode;
129 u32 delta;
130 } __attribute__((packed));
132 unsigned paravirt_patch_call(void *target, u16 tgt_clobbers,
133 void *site, u16 site_clobbers,
134 unsigned len)
136 unsigned char *call = site;
137 unsigned long delta = (unsigned long)target - (unsigned long)(call+5);
138 struct branch b;
140 if (tgt_clobbers & ~site_clobbers)
141 return len; /* target would clobber too much for this site */
142 if (len < 5)
143 return len; /* call too long for patch site */
145 b.opcode = 0xe8; /* call */
146 b.delta = delta;
147 BUILD_BUG_ON(sizeof(b) != 5);
148 text_poke(call, (unsigned char *)&b, 5);
150 return 5;
153 unsigned paravirt_patch_jmp(void *target, void *site, unsigned len)
155 unsigned char *jmp = site;
156 unsigned long delta = (unsigned long)target - (unsigned long)(jmp+5);
158 if (len < 5)
159 return len; /* call too long for patch site */
161 b.opcode = 0xe9; /* jmp */
162 b.delta = delta;
163 text_poke(call, (unsigned char *)&b, 5);
165 return 5;
168 unsigned paravirt_patch_default(u8 type, u16 clobbers, void *site, unsigned len)
170 void *opfunc = *((void **)&paravirt_ops + type);
171 unsigned ret;
173 if (opfunc == NULL)
174 /* If there's no function, patch it with a ud2a (BUG) */
175 ret = paravirt_patch_insns(site, len, start_ud2a, end_ud2a);
176 else if (opfunc == paravirt_nop)
177 /* If the operation is a nop, then nop the callsite */
178 ret = paravirt_patch_nop();
179 else if (type == PARAVIRT_PATCH(iret) ||
180 type == PARAVIRT_PATCH(irq_enable_sysexit))
181 /* If operation requires a jmp, then jmp */
182 ret = paravirt_patch_jmp(opfunc, site, len);
183 else
184 /* Otherwise call the function; assume target could
185 clobber any caller-save reg */
186 ret = paravirt_patch_call(opfunc, CLBR_ANY,
187 site, clobbers, len);
189 return ret;
192 unsigned paravirt_patch_insns(void *site, unsigned len,
193 const char *start, const char *end)
195 unsigned insn_len = end - start;
197 if (insn_len > len || start == NULL)
198 insn_len = len;
199 else
200 memcpy(site, start, insn_len);
202 return insn_len;
205 void init_IRQ(void)
207 paravirt_ops.init_IRQ();
210 static void native_flush_tlb(void)
212 __native_flush_tlb();
216 * Global pages have to be flushed a bit differently. Not a real
217 * performance problem because this does not happen often.
219 static void native_flush_tlb_global(void)
221 __native_flush_tlb_global();
224 static void native_flush_tlb_single(unsigned long addr)
226 __native_flush_tlb_single(addr);
229 /* These are in entry.S */
230 extern void native_iret(void);
231 extern void native_irq_enable_sysexit(void);
233 static int __init print_banner(void)
235 paravirt_ops.banner();
236 return 0;
238 core_initcall(print_banner);
240 static struct resource reserve_ioports = {
241 .start = 0,
242 .end = IO_SPACE_LIMIT,
243 .name = "paravirt-ioport",
244 .flags = IORESOURCE_IO | IORESOURCE_BUSY,
247 static struct resource reserve_iomem = {
248 .start = 0,
249 .end = -1,
250 .name = "paravirt-iomem",
251 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
255 * Reserve the whole legacy IO space to prevent any legacy drivers
256 * from wasting time probing for their hardware. This is a fairly
257 * brute-force approach to disabling all non-virtual drivers.
259 * Note that this must be called very early to have any effect.
261 int paravirt_disable_iospace(void)
263 int ret;
265 ret = request_resource(&ioport_resource, &reserve_ioports);
266 if (ret == 0) {
267 ret = request_resource(&iomem_resource, &reserve_iomem);
268 if (ret)
269 release_resource(&reserve_ioports);
272 return ret;
275 struct paravirt_ops paravirt_ops = {
276 .name = "bare hardware",
277 .paravirt_enabled = 0,
278 .kernel_rpl = 0,
279 .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
281 .patch = native_patch,
282 .banner = default_banner,
283 .arch_setup = paravirt_nop,
284 .memory_setup = machine_specific_memory_setup,
285 .get_wallclock = native_get_wallclock,
286 .set_wallclock = native_set_wallclock,
287 .time_init = hpet_time_init,
288 .init_IRQ = native_init_IRQ,
290 .cpuid = native_cpuid,
291 .get_debugreg = native_get_debugreg,
292 .set_debugreg = native_set_debugreg,
293 .clts = native_clts,
294 .read_cr0 = native_read_cr0,
295 .write_cr0 = native_write_cr0,
296 .read_cr2 = native_read_cr2,
297 .write_cr2 = native_write_cr2,
298 .read_cr3 = native_read_cr3,
299 .write_cr3 = native_write_cr3,
300 .read_cr4 = native_read_cr4,
301 .read_cr4_safe = native_read_cr4_safe,
302 .write_cr4 = native_write_cr4,
303 .save_fl = native_save_fl,
304 .restore_fl = native_restore_fl,
305 .irq_disable = native_irq_disable,
306 .irq_enable = native_irq_enable,
307 .safe_halt = native_safe_halt,
308 .halt = native_halt,
309 .wbinvd = native_wbinvd,
310 .read_msr = native_read_msr_safe,
311 .write_msr = native_write_msr_safe,
312 .read_tsc = native_read_tsc,
313 .read_pmc = native_read_pmc,
314 .sched_clock = native_sched_clock,
315 .get_cpu_khz = native_calculate_cpu_khz,
316 .load_tr_desc = native_load_tr_desc,
317 .set_ldt = native_set_ldt,
318 .load_gdt = native_load_gdt,
319 .load_idt = native_load_idt,
320 .store_gdt = native_store_gdt,
321 .store_idt = native_store_idt,
322 .store_tr = native_store_tr,
323 .load_tls = native_load_tls,
324 .write_ldt_entry = write_dt_entry,
325 .write_gdt_entry = write_dt_entry,
326 .write_idt_entry = write_dt_entry,
327 .load_esp0 = native_load_esp0,
329 .set_iopl_mask = native_set_iopl_mask,
330 .io_delay = native_io_delay,
332 #ifdef CONFIG_X86_LOCAL_APIC
333 .apic_write = native_apic_write,
334 .apic_write_atomic = native_apic_write_atomic,
335 .apic_read = native_apic_read,
336 .setup_boot_clock = setup_boot_APIC_clock,
337 .setup_secondary_clock = setup_secondary_APIC_clock,
338 .startup_ipi_hook = paravirt_nop,
339 #endif
340 .set_lazy_mode = paravirt_nop,
342 .pagetable_setup_start = native_pagetable_setup_start,
343 .pagetable_setup_done = native_pagetable_setup_done,
345 .flush_tlb_user = native_flush_tlb,
346 .flush_tlb_kernel = native_flush_tlb_global,
347 .flush_tlb_single = native_flush_tlb_single,
348 .flush_tlb_others = native_flush_tlb_others,
350 .alloc_pt = paravirt_nop,
351 .alloc_pd = paravirt_nop,
352 .alloc_pd_clone = paravirt_nop,
353 .release_pt = paravirt_nop,
354 .release_pd = paravirt_nop,
356 .set_pte = native_set_pte,
357 .set_pte_at = native_set_pte_at,
358 .set_pmd = native_set_pmd,
359 .pte_update = paravirt_nop,
360 .pte_update_defer = paravirt_nop,
362 #ifdef CONFIG_HIGHPTE
363 .kmap_atomic_pte = kmap_atomic,
364 #endif
366 #ifdef CONFIG_X86_PAE
367 .set_pte_atomic = native_set_pte_atomic,
368 .set_pte_present = native_set_pte_present,
369 .set_pud = native_set_pud,
370 .pte_clear = native_pte_clear,
371 .pmd_clear = native_pmd_clear,
373 .pmd_val = native_pmd_val,
374 .make_pmd = native_make_pmd,
375 #endif
377 .pte_val = native_pte_val,
378 .pgd_val = native_pgd_val,
380 .make_pte = native_make_pte,
381 .make_pgd = native_make_pgd,
383 .irq_enable_sysexit = native_irq_enable_sysexit,
384 .iret = native_iret,
386 .dup_mmap = paravirt_nop,
387 .exit_mmap = paravirt_nop,
388 .activate_mm = paravirt_nop,
391 EXPORT_SYMBOL(paravirt_ops);