Commit | Line | Data |
---|---|---|
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> |
d3561b7f RR |
23 | |
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> | |
13623d79 RR |
32 | #include <asm/fixmap.h> |
33 | #include <asm/apic.h> | |
da181a8b | 34 | #include <asm/tlbflush.h> |
6cb9a835 | 35 | #include <asm/timer.h> |
d3561b7f RR |
36 | |
37 | /* nop stub */ | |
45876233 | 38 | void _paravirt_nop(void) |
d3561b7f RR |
39 | { |
40 | } | |
41 | ||
42 | static void __init default_banner(void) | |
43 | { | |
44 | printk(KERN_INFO "Booting paravirtualized kernel on %s\n", | |
45 | paravirt_ops.name); | |
46 | } | |
47 | ||
48 | char *memory_setup(void) | |
49 | { | |
50 | return paravirt_ops.memory_setup(); | |
51 | } | |
52 | ||
139ec7c4 RR |
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 ":") | |
63f70270 JF |
57 | |
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"); | |
139ec7c4 | 62 | DEF_NATIVE(iret, "iret"); |
63f70270 JF |
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"); | |
139ec7c4 | 69 | |
63f70270 | 70 | DEF_NATIVE(ud2a, "ud2a"); |
139ec7c4 RR |
71 | |
72 | static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len) | |
73 | { | |
63f70270 JF |
74 | const unsigned char *start, *end; |
75 | unsigned ret; | |
76 | ||
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 | |
91 | ||
92 | patch_site: | |
93 | ret = paravirt_patch_insns(insns, len, start, end); | |
94 | break; | |
95 | ||
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; | |
108 | ||
109 | default: | |
110 | ret = paravirt_patch_default(type, clobbers, insns, len); | |
111 | break; | |
112 | } | |
113 | ||
114 | return ret; | |
115 | } | |
116 | ||
117 | unsigned paravirt_patch_nop(void) | |
118 | { | |
119 | return 0; | |
120 | } | |
121 | ||
122 | unsigned paravirt_patch_ignore(unsigned len) | |
123 | { | |
124 | return len; | |
125 | } | |
126 | ||
127 | unsigned paravirt_patch_call(void *target, u16 tgt_clobbers, | |
128 | void *site, u16 site_clobbers, | |
129 | unsigned len) | |
130 | { | |
131 | unsigned char *call = site; | |
132 | unsigned long delta = (unsigned long)target - (unsigned long)(call+5); | |
133 | ||
134 | if (tgt_clobbers & ~site_clobbers) | |
135 | return len; /* target would clobber too much for this site */ | |
136 | if (len < 5) | |
137 | return len; /* call too long for patch site */ | |
139ec7c4 | 138 | |
63f70270 JF |
139 | *call++ = 0xe8; /* call */ |
140 | *(unsigned long *)call = delta; | |
139ec7c4 | 141 | |
63f70270 JF |
142 | return 5; |
143 | } | |
144 | ||
145 | unsigned paravirt_patch_jmp(void *target, void *site, unsigned len) | |
146 | { | |
147 | unsigned char *jmp = site; | |
148 | unsigned long delta = (unsigned long)target - (unsigned long)(jmp+5); | |
149 | ||
150 | if (len < 5) | |
151 | return len; /* call too long for patch site */ | |
152 | ||
153 | *jmp++ = 0xe9; /* jmp */ | |
154 | *(unsigned long *)jmp = delta; | |
155 | ||
156 | return 5; | |
157 | } | |
158 | ||
159 | unsigned paravirt_patch_default(u8 type, u16 clobbers, void *site, unsigned len) | |
160 | { | |
161 | void *opfunc = *((void **)¶virt_ops + type); | |
162 | unsigned ret; | |
163 | ||
164 | if (opfunc == NULL) | |
165 | /* If there's no function, patch it with a ud2a (BUG) */ | |
166 | ret = paravirt_patch_insns(site, len, start_ud2a, end_ud2a); | |
167 | else if (opfunc == paravirt_nop) | |
168 | /* If the operation is a nop, then nop the callsite */ | |
169 | ret = paravirt_patch_nop(); | |
170 | else if (type == PARAVIRT_PATCH(iret) || | |
171 | type == PARAVIRT_PATCH(irq_enable_sysexit)) | |
172 | /* If operation requires a jmp, then jmp */ | |
173 | ret = paravirt_patch_jmp(opfunc, site, len); | |
174 | else | |
175 | /* Otherwise call the function; assume target could | |
176 | clobber any caller-save reg */ | |
177 | ret = paravirt_patch_call(opfunc, CLBR_ANY, | |
178 | site, clobbers, len); | |
179 | ||
180 | return ret; | |
181 | } | |
182 | ||
183 | unsigned paravirt_patch_insns(void *site, unsigned len, | |
184 | const char *start, const char *end) | |
185 | { | |
186 | unsigned insn_len = end - start; | |
139ec7c4 | 187 | |
63f70270 JF |
188 | if (insn_len > len || start == NULL) |
189 | insn_len = len; | |
190 | else | |
191 | memcpy(site, start, insn_len); | |
139ec7c4 | 192 | |
139ec7c4 RR |
193 | return insn_len; |
194 | } | |
195 | ||
d3561b7f RR |
196 | void init_IRQ(void) |
197 | { | |
198 | paravirt_ops.init_IRQ(); | |
199 | } | |
200 | ||
1a1eecd1 | 201 | static void native_flush_tlb(void) |
da181a8b RR |
202 | { |
203 | __native_flush_tlb(); | |
204 | } | |
205 | ||
206 | /* | |
207 | * Global pages have to be flushed a bit differently. Not a real | |
208 | * performance problem because this does not happen often. | |
209 | */ | |
1a1eecd1 | 210 | static void native_flush_tlb_global(void) |
da181a8b RR |
211 | { |
212 | __native_flush_tlb_global(); | |
213 | } | |
214 | ||
63f70270 | 215 | static void native_flush_tlb_single(unsigned long addr) |
da181a8b RR |
216 | { |
217 | __native_flush_tlb_single(addr); | |
218 | } | |
219 | ||
d3561b7f | 220 | /* These are in entry.S */ |
1a1eecd1 AK |
221 | extern void native_iret(void); |
222 | extern void native_irq_enable_sysexit(void); | |
d3561b7f RR |
223 | |
224 | static int __init print_banner(void) | |
225 | { | |
226 | paravirt_ops.banner(); | |
227 | return 0; | |
228 | } | |
229 | core_initcall(print_banner); | |
230 | ||
231 | struct paravirt_ops paravirt_ops = { | |
232 | .name = "bare hardware", | |
233 | .paravirt_enabled = 0, | |
234 | .kernel_rpl = 0, | |
5311ab62 | 235 | .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */ |
d3561b7f | 236 | |
139ec7c4 | 237 | .patch = native_patch, |
d3561b7f | 238 | .banner = default_banner, |
45876233 | 239 | .arch_setup = paravirt_nop, |
d3561b7f RR |
240 | .memory_setup = machine_specific_memory_setup, |
241 | .get_wallclock = native_get_wallclock, | |
242 | .set_wallclock = native_set_wallclock, | |
e30fab3a | 243 | .time_init = hpet_time_init, |
d3561b7f RR |
244 | .init_IRQ = native_init_IRQ, |
245 | ||
246 | .cpuid = native_cpuid, | |
247 | .get_debugreg = native_get_debugreg, | |
248 | .set_debugreg = native_set_debugreg, | |
249 | .clts = native_clts, | |
250 | .read_cr0 = native_read_cr0, | |
251 | .write_cr0 = native_write_cr0, | |
252 | .read_cr2 = native_read_cr2, | |
253 | .write_cr2 = native_write_cr2, | |
254 | .read_cr3 = native_read_cr3, | |
255 | .write_cr3 = native_write_cr3, | |
256 | .read_cr4 = native_read_cr4, | |
257 | .read_cr4_safe = native_read_cr4_safe, | |
258 | .write_cr4 = native_write_cr4, | |
259 | .save_fl = native_save_fl, | |
260 | .restore_fl = native_restore_fl, | |
261 | .irq_disable = native_irq_disable, | |
262 | .irq_enable = native_irq_enable, | |
263 | .safe_halt = native_safe_halt, | |
264 | .halt = native_halt, | |
265 | .wbinvd = native_wbinvd, | |
90a0a06a RR |
266 | .read_msr = native_read_msr_safe, |
267 | .write_msr = native_write_msr_safe, | |
d3561b7f RR |
268 | .read_tsc = native_read_tsc, |
269 | .read_pmc = native_read_pmc, | |
6cb9a835 | 270 | .get_scheduled_cycles = native_read_tsc, |
1182d852 | 271 | .get_cpu_khz = native_calculate_cpu_khz, |
d3561b7f RR |
272 | .load_tr_desc = native_load_tr_desc, |
273 | .set_ldt = native_set_ldt, | |
274 | .load_gdt = native_load_gdt, | |
275 | .load_idt = native_load_idt, | |
276 | .store_gdt = native_store_gdt, | |
277 | .store_idt = native_store_idt, | |
278 | .store_tr = native_store_tr, | |
279 | .load_tls = native_load_tls, | |
90a0a06a RR |
280 | .write_ldt_entry = write_dt_entry, |
281 | .write_gdt_entry = write_dt_entry, | |
282 | .write_idt_entry = write_dt_entry, | |
d3561b7f RR |
283 | .load_esp0 = native_load_esp0, |
284 | ||
285 | .set_iopl_mask = native_set_iopl_mask, | |
286 | .io_delay = native_io_delay, | |
d3561b7f | 287 | |
13623d79 RR |
288 | #ifdef CONFIG_X86_LOCAL_APIC |
289 | .apic_write = native_apic_write, | |
290 | .apic_write_atomic = native_apic_write_atomic, | |
291 | .apic_read = native_apic_read, | |
bbab4f3b ZA |
292 | .setup_boot_clock = setup_boot_APIC_clock, |
293 | .setup_secondary_clock = setup_secondary_APIC_clock, | |
13623d79 | 294 | #endif |
45876233 | 295 | .set_lazy_mode = paravirt_nop, |
13623d79 | 296 | |
b239fb25 JF |
297 | .pagetable_setup_start = native_pagetable_setup_start, |
298 | .pagetable_setup_done = native_pagetable_setup_done, | |
299 | ||
da181a8b RR |
300 | .flush_tlb_user = native_flush_tlb, |
301 | .flush_tlb_kernel = native_flush_tlb_global, | |
302 | .flush_tlb_single = native_flush_tlb_single, | |
d4c10477 | 303 | .flush_tlb_others = native_flush_tlb_others, |
da181a8b | 304 | |
45876233 JF |
305 | .alloc_pt = paravirt_nop, |
306 | .alloc_pd = paravirt_nop, | |
307 | .alloc_pd_clone = paravirt_nop, | |
308 | .release_pt = paravirt_nop, | |
309 | .release_pd = paravirt_nop, | |
c119ecce | 310 | |
da181a8b RR |
311 | .set_pte = native_set_pte, |
312 | .set_pte_at = native_set_pte_at, | |
313 | .set_pmd = native_set_pmd, | |
45876233 JF |
314 | .pte_update = paravirt_nop, |
315 | .pte_update_defer = paravirt_nop, | |
3dc494e8 JF |
316 | |
317 | .ptep_get_and_clear = native_ptep_get_and_clear, | |
318 | ||
da181a8b RR |
319 | #ifdef CONFIG_X86_PAE |
320 | .set_pte_atomic = native_set_pte_atomic, | |
321 | .set_pte_present = native_set_pte_present, | |
322 | .set_pud = native_set_pud, | |
323 | .pte_clear = native_pte_clear, | |
324 | .pmd_clear = native_pmd_clear, | |
3dc494e8 JF |
325 | |
326 | .pmd_val = native_pmd_val, | |
327 | .make_pmd = native_make_pmd, | |
da181a8b RR |
328 | #endif |
329 | ||
3dc494e8 JF |
330 | .pte_val = native_pte_val, |
331 | .pgd_val = native_pgd_val, | |
332 | ||
333 | .make_pte = native_make_pte, | |
334 | .make_pgd = native_make_pgd, | |
335 | ||
d3561b7f RR |
336 | .irq_enable_sysexit = native_irq_enable_sysexit, |
337 | .iret = native_iret, | |
ae5da273 | 338 | |
d6dd61c8 JF |
339 | .dup_mmap = paravirt_nop, |
340 | .exit_mmap = paravirt_nop, | |
341 | .activate_mm = paravirt_nop, | |
342 | ||
45876233 | 343 | .startup_ipi_hook = paravirt_nop, |
d3561b7f | 344 | }; |
0dbe5a11 IM |
345 | |
346 | /* | |
347 | * NOTE: CONFIG_PARAVIRT is experimental and the paravirt_ops | |
348 | * semantics are subject to change. Hence we only do this | |
349 | * internal-only export of this, until it gets sorted out and | |
350 | * all lowlevel CPU ops used by modules are separately exported. | |
351 | */ | |
352 | EXPORT_SYMBOL_GPL(paravirt_ops); |