paravirt.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Paravirtualization interfaces
  2. Copyright (C) 2006 Rusty Russell IBM Corporation
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/efi.h>
  18. #include <linux/bcd.h>
  19. #include <linux/highmem.h>
  20. #include <asm/bug.h>
  21. #include <asm/paravirt.h>
  22. #include <asm/desc.h>
  23. #include <asm/setup.h>
  24. #include <asm/arch_hooks.h>
  25. #include <asm/time.h>
  26. #include <asm/irq.h>
  27. #include <asm/delay.h>
  28. #include <asm/fixmap.h>
  29. #include <asm/apic.h>
  30. #include <asm/tlbflush.h>
  31. #include <asm/timer.h>
  32. /* nop stub */
  33. void _paravirt_nop(void)
  34. {
  35. }
  36. static void __init default_banner(void)
  37. {
  38. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  39. paravirt_ops.name);
  40. }
  41. char *memory_setup(void)
  42. {
  43. return paravirt_ops.memory_setup();
  44. }
  45. /* Simple instruction patching code. */
  46. #define DEF_NATIVE(name, code) \
  47. extern const char start_##name[], end_##name[]; \
  48. asm("start_" #name ": " code "; end_" #name ":")
  49. DEF_NATIVE(irq_disable, "cli");
  50. DEF_NATIVE(irq_enable, "sti");
  51. DEF_NATIVE(restore_fl, "push %eax; popf");
  52. DEF_NATIVE(save_fl, "pushf; pop %eax");
  53. DEF_NATIVE(iret, "iret");
  54. DEF_NATIVE(irq_enable_sysexit, "sti; sysexit");
  55. DEF_NATIVE(read_cr2, "mov %cr2, %eax");
  56. DEF_NATIVE(write_cr3, "mov %eax, %cr3");
  57. DEF_NATIVE(read_cr3, "mov %cr3, %eax");
  58. DEF_NATIVE(clts, "clts");
  59. DEF_NATIVE(read_tsc, "rdtsc");
  60. DEF_NATIVE(ud2a, "ud2a");
  61. static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len)
  62. {
  63. const unsigned char *start, *end;
  64. unsigned ret;
  65. switch(type) {
  66. #define SITE(x) case PARAVIRT_PATCH(x): start = start_##x; end = end_##x; goto patch_site
  67. SITE(irq_disable);
  68. SITE(irq_enable);
  69. SITE(restore_fl);
  70. SITE(save_fl);
  71. SITE(iret);
  72. SITE(irq_enable_sysexit);
  73. SITE(read_cr2);
  74. SITE(read_cr3);
  75. SITE(write_cr3);
  76. SITE(clts);
  77. SITE(read_tsc);
  78. #undef SITE
  79. patch_site:
  80. ret = paravirt_patch_insns(insns, len, start, end);
  81. break;
  82. case PARAVIRT_PATCH(make_pgd):
  83. case PARAVIRT_PATCH(make_pte):
  84. case PARAVIRT_PATCH(pgd_val):
  85. case PARAVIRT_PATCH(pte_val):
  86. #ifdef CONFIG_X86_PAE
  87. case PARAVIRT_PATCH(make_pmd):
  88. case PARAVIRT_PATCH(pmd_val):
  89. #endif
  90. /* These functions end up returning exactly what
  91. they're passed, in the same registers. */
  92. ret = paravirt_patch_nop();
  93. break;
  94. default:
  95. ret = paravirt_patch_default(type, clobbers, insns, len);
  96. break;
  97. }
  98. return ret;
  99. }
  100. unsigned paravirt_patch_nop(void)
  101. {
  102. return 0;
  103. }
  104. unsigned paravirt_patch_ignore(unsigned len)
  105. {
  106. return len;
  107. }
  108. unsigned paravirt_patch_call(void *target, u16 tgt_clobbers,
  109. void *site, u16 site_clobbers,
  110. unsigned len)
  111. {
  112. unsigned char *call = site;
  113. unsigned long delta = (unsigned long)target - (unsigned long)(call+5);
  114. if (tgt_clobbers & ~site_clobbers)
  115. return len; /* target would clobber too much for this site */
  116. if (len < 5)
  117. return len; /* call too long for patch site */
  118. *call++ = 0xe8; /* call */
  119. *(unsigned long *)call = delta;
  120. return 5;
  121. }
  122. unsigned paravirt_patch_jmp(void *target, void *site, unsigned len)
  123. {
  124. unsigned char *jmp = site;
  125. unsigned long delta = (unsigned long)target - (unsigned long)(jmp+5);
  126. if (len < 5)
  127. return len; /* call too long for patch site */
  128. *jmp++ = 0xe9; /* jmp */
  129. *(unsigned long *)jmp = delta;
  130. return 5;
  131. }
  132. unsigned paravirt_patch_default(u8 type, u16 clobbers, void *site, unsigned len)
  133. {
  134. void *opfunc = *((void **)&paravirt_ops + type);
  135. unsigned ret;
  136. if (opfunc == NULL)
  137. /* If there's no function, patch it with a ud2a (BUG) */
  138. ret = paravirt_patch_insns(site, len, start_ud2a, end_ud2a);
  139. else if (opfunc == paravirt_nop)
  140. /* If the operation is a nop, then nop the callsite */
  141. ret = paravirt_patch_nop();
  142. else if (type == PARAVIRT_PATCH(iret) ||
  143. type == PARAVIRT_PATCH(irq_enable_sysexit))
  144. /* If operation requires a jmp, then jmp */
  145. ret = paravirt_patch_jmp(opfunc, site, len);
  146. else
  147. /* Otherwise call the function; assume target could
  148. clobber any caller-save reg */
  149. ret = paravirt_patch_call(opfunc, CLBR_ANY,
  150. site, clobbers, len);
  151. return ret;
  152. }
  153. unsigned paravirt_patch_insns(void *site, unsigned len,
  154. const char *start, const char *end)
  155. {
  156. unsigned insn_len = end - start;
  157. if (insn_len > len || start == NULL)
  158. insn_len = len;
  159. else
  160. memcpy(site, start, insn_len);
  161. return insn_len;
  162. }
  163. void init_IRQ(void)
  164. {
  165. paravirt_ops.init_IRQ();
  166. }
  167. static void native_flush_tlb(void)
  168. {
  169. __native_flush_tlb();
  170. }
  171. /*
  172. * Global pages have to be flushed a bit differently. Not a real
  173. * performance problem because this does not happen often.
  174. */
  175. static void native_flush_tlb_global(void)
  176. {
  177. __native_flush_tlb_global();
  178. }
  179. static void native_flush_tlb_single(unsigned long addr)
  180. {
  181. __native_flush_tlb_single(addr);
  182. }
  183. /* These are in entry.S */
  184. extern void native_iret(void);
  185. extern void native_irq_enable_sysexit(void);
  186. static int __init print_banner(void)
  187. {
  188. paravirt_ops.banner();
  189. return 0;
  190. }
  191. core_initcall(print_banner);
  192. struct paravirt_ops paravirt_ops = {
  193. .name = "bare hardware",
  194. .paravirt_enabled = 0,
  195. .kernel_rpl = 0,
  196. .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
  197. .patch = native_patch,
  198. .banner = default_banner,
  199. .arch_setup = paravirt_nop,
  200. .memory_setup = machine_specific_memory_setup,
  201. .get_wallclock = native_get_wallclock,
  202. .set_wallclock = native_set_wallclock,
  203. .time_init = hpet_time_init,
  204. .init_IRQ = native_init_IRQ,
  205. .cpuid = native_cpuid,
  206. .get_debugreg = native_get_debugreg,
  207. .set_debugreg = native_set_debugreg,
  208. .clts = native_clts,
  209. .read_cr0 = native_read_cr0,
  210. .write_cr0 = native_write_cr0,
  211. .read_cr2 = native_read_cr2,
  212. .write_cr2 = native_write_cr2,
  213. .read_cr3 = native_read_cr3,
  214. .write_cr3 = native_write_cr3,
  215. .read_cr4 = native_read_cr4,
  216. .read_cr4_safe = native_read_cr4_safe,
  217. .write_cr4 = native_write_cr4,
  218. .save_fl = native_save_fl,
  219. .restore_fl = native_restore_fl,
  220. .irq_disable = native_irq_disable,
  221. .irq_enable = native_irq_enable,
  222. .safe_halt = native_safe_halt,
  223. .halt = native_halt,
  224. .wbinvd = native_wbinvd,
  225. .read_msr = native_read_msr_safe,
  226. .write_msr = native_write_msr_safe,
  227. .read_tsc = native_read_tsc,
  228. .read_pmc = native_read_pmc,
  229. .get_scheduled_cycles = native_read_tsc,
  230. .get_cpu_khz = native_calculate_cpu_khz,
  231. .load_tr_desc = native_load_tr_desc,
  232. .set_ldt = native_set_ldt,
  233. .load_gdt = native_load_gdt,
  234. .load_idt = native_load_idt,
  235. .store_gdt = native_store_gdt,
  236. .store_idt = native_store_idt,
  237. .store_tr = native_store_tr,
  238. .load_tls = native_load_tls,
  239. .write_ldt_entry = write_dt_entry,
  240. .write_gdt_entry = write_dt_entry,
  241. .write_idt_entry = write_dt_entry,
  242. .load_esp0 = native_load_esp0,
  243. .set_iopl_mask = native_set_iopl_mask,
  244. .io_delay = native_io_delay,
  245. #ifdef CONFIG_X86_LOCAL_APIC
  246. .apic_write = native_apic_write,
  247. .apic_write_atomic = native_apic_write_atomic,
  248. .apic_read = native_apic_read,
  249. .setup_boot_clock = setup_boot_APIC_clock,
  250. .setup_secondary_clock = setup_secondary_APIC_clock,
  251. .startup_ipi_hook = paravirt_nop,
  252. #endif
  253. .set_lazy_mode = paravirt_nop,
  254. .pagetable_setup_start = native_pagetable_setup_start,
  255. .pagetable_setup_done = native_pagetable_setup_done,
  256. .flush_tlb_user = native_flush_tlb,
  257. .flush_tlb_kernel = native_flush_tlb_global,
  258. .flush_tlb_single = native_flush_tlb_single,
  259. .flush_tlb_others = native_flush_tlb_others,
  260. .alloc_pt = paravirt_nop,
  261. .alloc_pd = paravirt_nop,
  262. .alloc_pd_clone = paravirt_nop,
  263. .release_pt = paravirt_nop,
  264. .release_pd = paravirt_nop,
  265. .set_pte = native_set_pte,
  266. .set_pte_at = native_set_pte_at,
  267. .set_pmd = native_set_pmd,
  268. .pte_update = paravirt_nop,
  269. .pte_update_defer = paravirt_nop,
  270. #ifdef CONFIG_HIGHPTE
  271. .kmap_atomic_pte = kmap_atomic,
  272. #endif
  273. #ifdef CONFIG_X86_PAE
  274. .set_pte_atomic = native_set_pte_atomic,
  275. .set_pte_present = native_set_pte_present,
  276. .set_pud = native_set_pud,
  277. .pte_clear = native_pte_clear,
  278. .pmd_clear = native_pmd_clear,
  279. .pmd_val = native_pmd_val,
  280. .make_pmd = native_make_pmd,
  281. #endif
  282. .pte_val = native_pte_val,
  283. .pgd_val = native_pgd_val,
  284. .make_pte = native_make_pte,
  285. .make_pgd = native_make_pgd,
  286. .irq_enable_sysexit = native_irq_enable_sysexit,
  287. .iret = native_iret,
  288. .dup_mmap = paravirt_nop,
  289. .exit_mmap = paravirt_nop,
  290. .activate_mm = paravirt_nop,
  291. };
  292. EXPORT_SYMBOL(paravirt_ops);