[PATCH] i386: PARAVIRT: add kmap_atomic_pte for mapping highpte pages
[linux-2.6] / arch / i386 / kernel / paravirt.c
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>
22 #include <linux/start_kernel.h>
23 #include <linux/highmem.h>
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>
33 #include <asm/fixmap.h>
34 #include <asm/apic.h>
35 #include <asm/tlbflush.h>
36 #include <asm/timer.h>
37
38 /* nop stub */
39 void _paravirt_nop(void)
40 {
41 }
42
43 static void __init default_banner(void)
44 {
45         printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
46                paravirt_ops.name);
47 }
48
49 char *memory_setup(void)
50 {
51         return paravirt_ops.memory_setup();
52 }
53
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 ":")
58
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");
70
71 DEF_NATIVE(ud2a, "ud2a");
72
73 static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len)
74 {
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
118 unsigned paravirt_patch_nop(void)
119 {
120         return 0;
121 }
122
123 unsigned paravirt_patch_ignore(unsigned len)
124 {
125         return len;
126 }
127
128 unsigned 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 */
139
140         *call++ = 0xe8;         /* call */
141         *(unsigned long *)call = delta;
142
143         return 5;
144 }
145
146 unsigned 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
160 unsigned 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
184 unsigned paravirt_patch_insns(void *site, unsigned len,
185                               const char *start, const char *end)
186 {
187         unsigned insn_len = end - start;
188
189         if (insn_len > len || start == NULL)
190                 insn_len = len;
191         else
192                 memcpy(site, start, insn_len);
193
194         return insn_len;
195 }
196
197 void init_IRQ(void)
198 {
199         paravirt_ops.init_IRQ();
200 }
201
202 static void native_flush_tlb(void)
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  */
211 static void native_flush_tlb_global(void)
212 {
213         __native_flush_tlb_global();
214 }
215
216 static void native_flush_tlb_single(unsigned long addr)
217 {
218         __native_flush_tlb_single(addr);
219 }
220
221 /* These are in entry.S */
222 extern void native_iret(void);
223 extern void native_irq_enable_sysexit(void);
224
225 static int __init print_banner(void)
226 {
227         paravirt_ops.banner();
228         return 0;
229 }
230 core_initcall(print_banner);
231
232 struct paravirt_ops paravirt_ops = {
233         .name = "bare hardware",
234         .paravirt_enabled = 0,
235         .kernel_rpl = 0,
236         .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
237
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,
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,
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,
285
286         .set_iopl_mask = native_set_iopl_mask,
287         .io_delay = native_io_delay,
288
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,
295 #endif
296         .set_lazy_mode = paravirt_nop,
297
298         .pagetable_setup_start = native_pagetable_setup_start,
299         .pagetable_setup_done = native_pagetable_setup_done,
300
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,
305
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,
311
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,
317
318         .ptep_get_and_clear = native_ptep_get_and_clear,
319
320 #ifdef CONFIG_HIGHPTE
321         .kmap_atomic_pte = kmap_atomic,
322 #endif
323
324 #ifdef CONFIG_X86_PAE
325         .set_pte_atomic = native_set_pte_atomic,
326         .set_pte_present = native_set_pte_present,
327         .set_pud = native_set_pud,
328         .pte_clear = native_pte_clear,
329         .pmd_clear = native_pmd_clear,
330
331         .pmd_val = native_pmd_val,
332         .make_pmd = native_make_pmd,
333 #endif
334
335         .pte_val = native_pte_val,
336         .pgd_val = native_pgd_val,
337
338         .make_pte = native_make_pte,
339         .make_pgd = native_make_pgd,
340
341         .irq_enable_sysexit = native_irq_enable_sysexit,
342         .iret = native_iret,
343
344         .dup_mmap = paravirt_nop,
345         .exit_mmap = paravirt_nop,
346         .activate_mm = paravirt_nop,
347
348         .startup_ipi_hook = paravirt_nop,
349 };
350
351 /*
352  * NOTE: CONFIG_PARAVIRT is experimental and the paravirt_ops
353  * semantics are subject to change. Hence we only do this
354  * internal-only export of this, until it gets sorted out and
355  * all lowlevel CPU ops used by modules are separately exported.
356  */
357 EXPORT_SYMBOL_GPL(paravirt_ops);