Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 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> |
Jeremy Fitzhardinge | ce6234b | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 22 | #include <linux/highmem.h> |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 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> |
Rusty Russell | 13623d7 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 32 | #include <asm/fixmap.h> |
| 33 | #include <asm/apic.h> |
Rusty Russell | da181a8 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 34 | #include <asm/tlbflush.h> |
Zachary Amsden | 6cb9a83 | 2007-03-05 00:30:35 -0800 | [diff] [blame] | 35 | #include <asm/timer.h> |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 36 | |
| 37 | /* nop stub */ |
Jeremy Fitzhardinge | 4587623 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 38 | void _paravirt_nop(void) |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 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 | |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 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 ":") |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 57 | |
Jeremy Fitzhardinge | 63f7027 | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 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"); |
| 69 | |
| 70 | DEF_NATIVE(ud2a, "ud2a"); |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 71 | |
| 72 | static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len) |
| 73 | { |
Jeremy Fitzhardinge | 63f7027 | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 74 | const unsigned char *start, *end; |
| 75 | unsigned ret; |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 76 | |
Jeremy Fitzhardinge | 63f7027 | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 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 |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 91 | |
Jeremy Fitzhardinge | 63f7027 | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 92 | patch_site: |
| 93 | ret = paravirt_patch_insns(insns, len, start, end); |
| 94 | break; |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 95 | |
Jeremy Fitzhardinge | 63f7027 | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 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; |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 108 | |
Jeremy Fitzhardinge | 63f7027 | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 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 */ |
| 138 | |
| 139 | *call++ = 0xe8; /* call */ |
| 140 | *(unsigned long *)call = delta; |
| 141 | |
| 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; |
| 187 | |
| 188 | if (insn_len > len || start == NULL) |
| 189 | insn_len = len; |
| 190 | else |
| 191 | memcpy(site, start, insn_len); |
| 192 | |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 193 | return insn_len; |
| 194 | } |
| 195 | |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 196 | void init_IRQ(void) |
| 197 | { |
| 198 | paravirt_ops.init_IRQ(); |
| 199 | } |
| 200 | |
Andi Kleen | 1a1eecd | 2007-02-13 13:26:25 +0100 | [diff] [blame] | 201 | static void native_flush_tlb(void) |
Rusty Russell | da181a8 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 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 | */ |
Andi Kleen | 1a1eecd | 2007-02-13 13:26:25 +0100 | [diff] [blame] | 210 | static void native_flush_tlb_global(void) |
Rusty Russell | da181a8 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 211 | { |
| 212 | __native_flush_tlb_global(); |
| 213 | } |
| 214 | |
Jeremy Fitzhardinge | 63f7027 | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 215 | static void native_flush_tlb_single(unsigned long addr) |
Rusty Russell | da181a8 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 216 | { |
| 217 | __native_flush_tlb_single(addr); |
| 218 | } |
| 219 | |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 220 | /* These are in entry.S */ |
Andi Kleen | 1a1eecd | 2007-02-13 13:26:25 +0100 | [diff] [blame] | 221 | extern void native_iret(void); |
| 222 | extern void native_irq_enable_sysexit(void); |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 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, |
Jeremy Fitzhardinge | 5311ab6 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 235 | .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */ |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 236 | |
Rusty Russell | 139ec7c | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 237 | .patch = native_patch, |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 238 | .banner = default_banner, |
Jeremy Fitzhardinge | 4587623 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 239 | .arch_setup = paravirt_nop, |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 240 | .memory_setup = machine_specific_memory_setup, |
| 241 | .get_wallclock = native_get_wallclock, |
| 242 | .set_wallclock = native_set_wallclock, |
Zachary Amsden | e30fab3 | 2007-03-05 00:30:39 -0800 | [diff] [blame] | 243 | .time_init = hpet_time_init, |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 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, |
Rusty Russell | 90a0a06 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 266 | .read_msr = native_read_msr_safe, |
| 267 | .write_msr = native_write_msr_safe, |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 268 | .read_tsc = native_read_tsc, |
| 269 | .read_pmc = native_read_pmc, |
Zachary Amsden | 6cb9a83 | 2007-03-05 00:30:35 -0800 | [diff] [blame] | 270 | .get_scheduled_cycles = native_read_tsc, |
Zachary Amsden | 1182d85 | 2007-03-05 00:30:36 -0800 | [diff] [blame] | 271 | .get_cpu_khz = native_calculate_cpu_khz, |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 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, |
Rusty Russell | 90a0a06 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 280 | .write_ldt_entry = write_dt_entry, |
| 281 | .write_gdt_entry = write_dt_entry, |
| 282 | .write_idt_entry = write_dt_entry, |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 283 | .load_esp0 = native_load_esp0, |
| 284 | |
| 285 | .set_iopl_mask = native_set_iopl_mask, |
| 286 | .io_delay = native_io_delay, |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 287 | |
Rusty Russell | 13623d7 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 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, |
Zachary Amsden | bbab4f3 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 292 | .setup_boot_clock = setup_boot_APIC_clock, |
| 293 | .setup_secondary_clock = setup_secondary_APIC_clock, |
Jeremy Fitzhardinge | 0260c19 | 2007-05-02 19:27:18 +0200 | [diff] [blame] | 294 | .startup_ipi_hook = paravirt_nop, |
Rusty Russell | 13623d7 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 295 | #endif |
Jeremy Fitzhardinge | 4587623 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 296 | .set_lazy_mode = paravirt_nop, |
Rusty Russell | 13623d7 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 297 | |
Jeremy Fitzhardinge | b239fb2 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 298 | .pagetable_setup_start = native_pagetable_setup_start, |
| 299 | .pagetable_setup_done = native_pagetable_setup_done, |
| 300 | |
Rusty Russell | da181a8 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 301 | .flush_tlb_user = native_flush_tlb, |
| 302 | .flush_tlb_kernel = native_flush_tlb_global, |
| 303 | .flush_tlb_single = native_flush_tlb_single, |
Jeremy Fitzhardinge | d4c1047 | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 304 | .flush_tlb_others = native_flush_tlb_others, |
Rusty Russell | da181a8 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 305 | |
Jeremy Fitzhardinge | 4587623 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 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, |
Zachary Amsden | c119ecc | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 311 | |
Rusty Russell | da181a8 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 312 | .set_pte = native_set_pte, |
| 313 | .set_pte_at = native_set_pte_at, |
| 314 | .set_pmd = native_set_pmd, |
Jeremy Fitzhardinge | 4587623 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 315 | .pte_update = paravirt_nop, |
| 316 | .pte_update_defer = paravirt_nop, |
Jeremy Fitzhardinge | 3dc494e | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 317 | |
Jeremy Fitzhardinge | ce6234b | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 318 | #ifdef CONFIG_HIGHPTE |
| 319 | .kmap_atomic_pte = kmap_atomic, |
| 320 | #endif |
| 321 | |
Rusty Russell | da181a8 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 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, |
Jeremy Fitzhardinge | 3dc494e | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 328 | |
| 329 | .pmd_val = native_pmd_val, |
| 330 | .make_pmd = native_make_pmd, |
Rusty Russell | da181a8 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 331 | #endif |
| 332 | |
Jeremy Fitzhardinge | 3dc494e | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 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 | |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 339 | .irq_enable_sysexit = native_irq_enable_sysexit, |
| 340 | .iret = native_iret, |
Zachary Amsden | ae5da27 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 341 | |
Jeremy Fitzhardinge | d6dd61c | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 342 | .dup_mmap = paravirt_nop, |
| 343 | .exit_mmap = paravirt_nop, |
| 344 | .activate_mm = paravirt_nop, |
Rusty Russell | d3561b7 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 345 | }; |
Ingo Molnar | 0dbe5a1 | 2007-01-22 20:40:36 -0800 | [diff] [blame] | 346 | |
Andi Kleen | 21564fd | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 347 | EXPORT_SYMBOL(paravirt_ops); |