[PATCH] i386: fix mtrr sections
[linux-2.6-block.git] / arch / i386 / kernel / paravirt.c
CommitLineData
d3561b7f
RR
1/* 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>
c9ccf30d 22#include <linux/start_kernel.h>
ce6234b5 23#include <linux/highmem.h>
d3561b7f
RR
24
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>
13623d79
RR
33#include <asm/fixmap.h>
34#include <asm/apic.h>
da181a8b 35#include <asm/tlbflush.h>
6cb9a835 36#include <asm/timer.h>
d3561b7f
RR
37
38/* nop stub */
45876233 39void _paravirt_nop(void)
d3561b7f
RR
40{
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
139ec7c4
RR
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 ":")
63f70270
JF
58
59DEF_NATIVE(irq_disable, "cli");
60DEF_NATIVE(irq_enable, "sti");
61DEF_NATIVE(restore_fl, "push %eax; popf");
62DEF_NATIVE(save_fl, "pushf; pop %eax");
139ec7c4 63DEF_NATIVE(iret, "iret");
63f70270
JF
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");
139ec7c4 70
63f70270 71DEF_NATIVE(ud2a, "ud2a");
139ec7c4
RR
72
73static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len)
74{
63f70270
JF
75 const unsigned char *start, *end;
76 unsigned ret;
77
78 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
92
93 patch_site:
94 ret = paravirt_patch_insns(insns, len, start, end);
95 break;
96
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):
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;
109
110 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 */
139ec7c4 139
63f70270
JF
140 *call++ = 0xe8; /* call */
141 *(unsigned long *)call = delta;
139ec7c4 142
63f70270
JF
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;
139ec7c4 188
63f70270
JF
189 if (insn_len > len || start == NULL)
190 insn_len = len;
191 else
192 memcpy(site, start, insn_len);
139ec7c4 193
139ec7c4
RR
194 return insn_len;
195}
196
d3561b7f
RR
197void init_IRQ(void)
198{
199 paravirt_ops.init_IRQ();
200}
201
1a1eecd1 202static void native_flush_tlb(void)
da181a8b
RR
203{
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 */
1a1eecd1 211static void native_flush_tlb_global(void)
da181a8b
RR
212{
213 __native_flush_tlb_global();
214}
215
63f70270 216static void native_flush_tlb_single(unsigned long addr)
da181a8b
RR
217{
218 __native_flush_tlb_single(addr);
219}
220
d3561b7f 221/* These are in entry.S */
1a1eecd1
AK
222extern void native_iret(void);
223extern void native_irq_enable_sysexit(void);
d3561b7f
RR
224
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,
5311ab62 236 .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
d3561b7f 237
139ec7c4 238 .patch = native_patch,
d3561b7f 239 .banner = default_banner,
45876233 240 .arch_setup = paravirt_nop,
d3561b7f
RR
241 .memory_setup = machine_specific_memory_setup,
242 .get_wallclock = native_get_wallclock,
243 .set_wallclock = native_set_wallclock,
e30fab3a 244 .time_init = hpet_time_init,
d3561b7f
RR
245 .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,
90a0a06a
RR
267 .read_msr = native_read_msr_safe,
268 .write_msr = native_write_msr_safe,
d3561b7f
RR
269 .read_tsc = native_read_tsc,
270 .read_pmc = native_read_pmc,
6cb9a835 271 .get_scheduled_cycles = native_read_tsc,
1182d852 272 .get_cpu_khz = native_calculate_cpu_khz,
d3561b7f
RR
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,
90a0a06a
RR
281 .write_ldt_entry = write_dt_entry,
282 .write_gdt_entry = write_dt_entry,
283 .write_idt_entry = write_dt_entry,
d3561b7f
RR
284 .load_esp0 = native_load_esp0,
285
286 .set_iopl_mask = native_set_iopl_mask,
287 .io_delay = native_io_delay,
d3561b7f 288
13623d79
RR
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,
bbab4f3b
ZA
293 .setup_boot_clock = setup_boot_APIC_clock,
294 .setup_secondary_clock = setup_secondary_APIC_clock,
13623d79 295#endif
45876233 296 .set_lazy_mode = paravirt_nop,
13623d79 297
b239fb25
JF
298 .pagetable_setup_start = native_pagetable_setup_start,
299 .pagetable_setup_done = native_pagetable_setup_done,
300
da181a8b
RR
301 .flush_tlb_user = native_flush_tlb,
302 .flush_tlb_kernel = native_flush_tlb_global,
303 .flush_tlb_single = native_flush_tlb_single,
d4c10477 304 .flush_tlb_others = native_flush_tlb_others,
da181a8b 305
45876233
JF
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,
c119ecce 311
da181a8b
RR
312 .set_pte = native_set_pte,
313 .set_pte_at = native_set_pte_at,
314 .set_pmd = native_set_pmd,
45876233
JF
315 .pte_update = paravirt_nop,
316 .pte_update_defer = paravirt_nop,
3dc494e8 317
ce6234b5
JF
318#ifdef CONFIG_HIGHPTE
319 .kmap_atomic_pte = kmap_atomic,
320#endif
321
da181a8b
RR
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,
3dc494e8
JF
328
329 .pmd_val = native_pmd_val,
330 .make_pmd = native_make_pmd,
da181a8b
RR
331#endif
332
3dc494e8
JF
333 .pte_val = native_pte_val,
334 .pgd_val = native_pgd_val,
335
336 .make_pte = native_make_pte,
337 .make_pgd = native_make_pgd,
338
d3561b7f
RR
339 .irq_enable_sysexit = native_irq_enable_sysexit,
340 .iret = native_iret,
ae5da273 341
d6dd61c8
JF
342 .dup_mmap = paravirt_nop,
343 .exit_mmap = paravirt_nop,
344 .activate_mm = paravirt_nop,
345
45876233 346 .startup_ipi_hook = paravirt_nop,
d3561b7f 347};
0dbe5a11 348
21564fd2 349EXPORT_SYMBOL(paravirt_ops);