paravirt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. 2007 - x86_64 support added by Glauber de Oliveira Costa, Red Hat Inc
  15. */
  16. #include <linux/errno.h>
  17. #include <linux/module.h>
  18. #include <linux/efi.h>
  19. #include <linux/bcd.h>
  20. #include <linux/highmem.h>
  21. #include <asm/bug.h>
  22. #include <asm/paravirt.h>
  23. #include <asm/desc.h>
  24. #include <asm/setup.h>
  25. #include <asm/arch_hooks.h>
  26. #include <asm/time.h>
  27. #include <asm/irq.h>
  28. #include <asm/delay.h>
  29. #include <asm/fixmap.h>
  30. #include <asm/apic.h>
  31. #include <asm/tlbflush.h>
  32. #include <asm/timer.h>
  33. /* nop stub */
  34. void _paravirt_nop(void)
  35. {
  36. }
  37. static void __init default_banner(void)
  38. {
  39. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  40. pv_info.name);
  41. }
  42. char *memory_setup(void)
  43. {
  44. return pv_init_ops.memory_setup();
  45. }
  46. /* Simple instruction patching code. */
  47. #define DEF_NATIVE(ops, name, code) \
  48. extern const char start_##ops##_##name[], end_##ops##_##name[]; \
  49. asm("start_" #ops "_" #name ": " code "; end_" #ops "_" #name ":")
  50. /* Undefined instruction for dealing with missing ops pointers. */
  51. static const unsigned char ud2a[] = { 0x0f, 0x0b };
  52. unsigned paravirt_patch_nop(void)
  53. {
  54. return 0;
  55. }
  56. unsigned paravirt_patch_ignore(unsigned len)
  57. {
  58. return len;
  59. }
  60. struct branch {
  61. unsigned char opcode;
  62. u32 delta;
  63. } __attribute__((packed));
  64. unsigned paravirt_patch_call(void *insnbuf,
  65. const void *target, u16 tgt_clobbers,
  66. unsigned long addr, u16 site_clobbers,
  67. unsigned len)
  68. {
  69. struct branch *b = insnbuf;
  70. unsigned long delta = (unsigned long)target - (addr+5);
  71. if (tgt_clobbers & ~site_clobbers)
  72. return len; /* target would clobber too much for this site */
  73. if (len < 5)
  74. return len; /* call too long for patch site */
  75. b->opcode = 0xe8; /* call */
  76. b->delta = delta;
  77. BUILD_BUG_ON(sizeof(*b) != 5);
  78. return 5;
  79. }
  80. unsigned paravirt_patch_jmp(void *insnbuf, const void *target,
  81. unsigned long addr, unsigned len)
  82. {
  83. struct branch *b = insnbuf;
  84. unsigned long delta = (unsigned long)target - (addr+5);
  85. if (len < 5)
  86. return len; /* call too long for patch site */
  87. b->opcode = 0xe9; /* jmp */
  88. b->delta = delta;
  89. return 5;
  90. }
  91. /* Neat trick to map patch type back to the call within the
  92. * corresponding structure. */
  93. static void *get_call_destination(u8 type)
  94. {
  95. struct paravirt_patch_template tmpl = {
  96. .pv_init_ops = pv_init_ops,
  97. .pv_time_ops = pv_time_ops,
  98. .pv_cpu_ops = pv_cpu_ops,
  99. .pv_irq_ops = pv_irq_ops,
  100. .pv_apic_ops = pv_apic_ops,
  101. .pv_mmu_ops = pv_mmu_ops,
  102. };
  103. return *((void **)&tmpl + type);
  104. }
  105. unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
  106. unsigned long addr, unsigned len)
  107. {
  108. void *opfunc = get_call_destination(type);
  109. unsigned ret;
  110. if (opfunc == NULL)
  111. /* If there's no function, patch it with a ud2a (BUG) */
  112. ret = paravirt_patch_insns(insnbuf, len, ud2a, ud2a+sizeof(ud2a));
  113. else if (opfunc == paravirt_nop)
  114. /* If the operation is a nop, then nop the callsite */
  115. ret = paravirt_patch_nop();
  116. else if (type == PARAVIRT_PATCH(pv_cpu_ops.iret) ||
  117. type == PARAVIRT_PATCH(pv_cpu_ops.irq_enable_syscall_ret))
  118. /* If operation requires a jmp, then jmp */
  119. ret = paravirt_patch_jmp(insnbuf, opfunc, addr, len);
  120. else
  121. /* Otherwise call the function; assume target could
  122. clobber any caller-save reg */
  123. ret = paravirt_patch_call(insnbuf, opfunc, CLBR_ANY,
  124. addr, clobbers, len);
  125. return ret;
  126. }
  127. unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
  128. const char *start, const char *end)
  129. {
  130. unsigned insn_len = end - start;
  131. if (insn_len > len || start == NULL)
  132. insn_len = len;
  133. else
  134. memcpy(insnbuf, start, insn_len);
  135. return insn_len;
  136. }
  137. void init_IRQ(void)
  138. {
  139. pv_irq_ops.init_IRQ();
  140. }
  141. static void native_flush_tlb(void)
  142. {
  143. __native_flush_tlb();
  144. }
  145. /*
  146. * Global pages have to be flushed a bit differently. Not a real
  147. * performance problem because this does not happen often.
  148. */
  149. static void native_flush_tlb_global(void)
  150. {
  151. __native_flush_tlb_global();
  152. }
  153. static void native_flush_tlb_single(unsigned long addr)
  154. {
  155. __native_flush_tlb_single(addr);
  156. }
  157. /* These are in entry.S */
  158. extern void native_iret(void);
  159. extern void native_irq_enable_syscall_ret(void);
  160. static int __init print_banner(void)
  161. {
  162. pv_init_ops.banner();
  163. return 0;
  164. }
  165. core_initcall(print_banner);
  166. static struct resource reserve_ioports = {
  167. .start = 0,
  168. .end = IO_SPACE_LIMIT,
  169. .name = "paravirt-ioport",
  170. .flags = IORESOURCE_IO | IORESOURCE_BUSY,
  171. };
  172. /*
  173. * Reserve the whole legacy IO space to prevent any legacy drivers
  174. * from wasting time probing for their hardware. This is a fairly
  175. * brute-force approach to disabling all non-virtual drivers.
  176. *
  177. * Note that this must be called very early to have any effect.
  178. */
  179. int paravirt_disable_iospace(void)
  180. {
  181. return request_resource(&ioport_resource, &reserve_ioports);
  182. }
  183. static DEFINE_PER_CPU(enum paravirt_lazy_mode, paravirt_lazy_mode) = PARAVIRT_LAZY_NONE;
  184. static inline void enter_lazy(enum paravirt_lazy_mode mode)
  185. {
  186. BUG_ON(__get_cpu_var(paravirt_lazy_mode) != PARAVIRT_LAZY_NONE);
  187. BUG_ON(preemptible());
  188. __get_cpu_var(paravirt_lazy_mode) = mode;
  189. }
  190. void paravirt_leave_lazy(enum paravirt_lazy_mode mode)
  191. {
  192. BUG_ON(__get_cpu_var(paravirt_lazy_mode) != mode);
  193. BUG_ON(preemptible());
  194. __get_cpu_var(paravirt_lazy_mode) = PARAVIRT_LAZY_NONE;
  195. }
  196. void paravirt_enter_lazy_mmu(void)
  197. {
  198. enter_lazy(PARAVIRT_LAZY_MMU);
  199. }
  200. void paravirt_leave_lazy_mmu(void)
  201. {
  202. paravirt_leave_lazy(PARAVIRT_LAZY_MMU);
  203. }
  204. void paravirt_enter_lazy_cpu(void)
  205. {
  206. enter_lazy(PARAVIRT_LAZY_CPU);
  207. }
  208. void paravirt_leave_lazy_cpu(void)
  209. {
  210. paravirt_leave_lazy(PARAVIRT_LAZY_CPU);
  211. }
  212. enum paravirt_lazy_mode paravirt_get_lazy_mode(void)
  213. {
  214. return __get_cpu_var(paravirt_lazy_mode);
  215. }
  216. struct pv_info pv_info = {
  217. .name = "bare hardware",
  218. .paravirt_enabled = 0,
  219. .kernel_rpl = 0,
  220. .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
  221. };
  222. struct pv_init_ops pv_init_ops = {
  223. .patch = native_patch,
  224. .banner = default_banner,
  225. .arch_setup = paravirt_nop,
  226. .memory_setup = machine_specific_memory_setup,
  227. };
  228. struct pv_time_ops pv_time_ops = {
  229. .time_init = hpet_time_init,
  230. .get_wallclock = native_get_wallclock,
  231. .set_wallclock = native_set_wallclock,
  232. .sched_clock = native_sched_clock,
  233. .get_cpu_khz = native_calculate_cpu_khz,
  234. };
  235. struct pv_irq_ops pv_irq_ops = {
  236. .init_IRQ = native_init_IRQ,
  237. .save_fl = native_save_fl,
  238. .restore_fl = native_restore_fl,
  239. .irq_disable = native_irq_disable,
  240. .irq_enable = native_irq_enable,
  241. .safe_halt = native_safe_halt,
  242. .halt = native_halt,
  243. };
  244. struct pv_cpu_ops pv_cpu_ops = {
  245. .cpuid = native_cpuid,
  246. .get_debugreg = native_get_debugreg,
  247. .set_debugreg = native_set_debugreg,
  248. .clts = native_clts,
  249. .read_cr0 = native_read_cr0,
  250. .write_cr0 = native_write_cr0,
  251. .read_cr4 = native_read_cr4,
  252. .read_cr4_safe = native_read_cr4_safe,
  253. .write_cr4 = native_write_cr4,
  254. #ifdef CONFIG_X86_64
  255. .read_cr8 = native_read_cr8,
  256. .write_cr8 = native_write_cr8,
  257. #endif
  258. .wbinvd = native_wbinvd,
  259. .read_msr = native_read_msr_safe,
  260. .write_msr = native_write_msr_safe,
  261. .read_tsc = native_read_tsc,
  262. .read_pmc = native_read_pmc,
  263. .read_tscp = native_read_tscp,
  264. .load_tr_desc = native_load_tr_desc,
  265. .set_ldt = native_set_ldt,
  266. .load_gdt = native_load_gdt,
  267. .load_idt = native_load_idt,
  268. .store_gdt = native_store_gdt,
  269. .store_idt = native_store_idt,
  270. .store_tr = native_store_tr,
  271. .load_tls = native_load_tls,
  272. .write_ldt_entry = native_write_ldt_entry,
  273. .write_gdt_entry = native_write_gdt_entry,
  274. .write_idt_entry = native_write_idt_entry,
  275. .load_sp0 = native_load_sp0,
  276. .irq_enable_syscall_ret = native_irq_enable_syscall_ret,
  277. .iret = native_iret,
  278. .swapgs = native_swapgs,
  279. .set_iopl_mask = native_set_iopl_mask,
  280. .io_delay = native_io_delay,
  281. .lazy_mode = {
  282. .enter = paravirt_nop,
  283. .leave = paravirt_nop,
  284. },
  285. };
  286. struct pv_apic_ops pv_apic_ops = {
  287. #ifdef CONFIG_X86_LOCAL_APIC
  288. .apic_write = native_apic_write,
  289. .apic_write_atomic = native_apic_write_atomic,
  290. .apic_read = native_apic_read,
  291. .setup_boot_clock = setup_boot_APIC_clock,
  292. .setup_secondary_clock = setup_secondary_APIC_clock,
  293. .startup_ipi_hook = paravirt_nop,
  294. #endif
  295. };
  296. struct pv_mmu_ops pv_mmu_ops = {
  297. #ifndef CONFIG_X86_64
  298. .pagetable_setup_start = native_pagetable_setup_start,
  299. .pagetable_setup_done = native_pagetable_setup_done,
  300. #endif
  301. .read_cr2 = native_read_cr2,
  302. .write_cr2 = native_write_cr2,
  303. .read_cr3 = native_read_cr3,
  304. .write_cr3 = native_write_cr3,
  305. .flush_tlb_user = native_flush_tlb,
  306. .flush_tlb_kernel = native_flush_tlb_global,
  307. .flush_tlb_single = native_flush_tlb_single,
  308. .flush_tlb_others = native_flush_tlb_others,
  309. .alloc_pte = paravirt_nop,
  310. .alloc_pmd = paravirt_nop,
  311. .alloc_pmd_clone = paravirt_nop,
  312. .alloc_pud = paravirt_nop,
  313. .release_pte = paravirt_nop,
  314. .release_pmd = paravirt_nop,
  315. .release_pud = paravirt_nop,
  316. .set_pte = native_set_pte,
  317. .set_pte_at = native_set_pte_at,
  318. .set_pmd = native_set_pmd,
  319. .pte_update = paravirt_nop,
  320. .pte_update_defer = paravirt_nop,
  321. #ifdef CONFIG_HIGHPTE
  322. .kmap_atomic_pte = kmap_atomic,
  323. #endif
  324. #if PAGETABLE_LEVELS >= 3
  325. #ifdef CONFIG_X86_PAE
  326. .set_pte_atomic = native_set_pte_atomic,
  327. .set_pte_present = native_set_pte_present,
  328. .pte_clear = native_pte_clear,
  329. .pmd_clear = native_pmd_clear,
  330. #endif
  331. .set_pud = native_set_pud,
  332. .pmd_val = native_pmd_val,
  333. .make_pmd = native_make_pmd,
  334. #if PAGETABLE_LEVELS == 4
  335. .pud_val = native_pud_val,
  336. .make_pud = native_make_pud,
  337. .set_pgd = native_set_pgd,
  338. #endif
  339. #endif /* PAGETABLE_LEVELS >= 3 */
  340. .pte_val = native_pte_val,
  341. .pgd_val = native_pgd_val,
  342. .make_pte = native_make_pte,
  343. .make_pgd = native_make_pgd,
  344. .dup_mmap = paravirt_nop,
  345. .exit_mmap = paravirt_nop,
  346. .activate_mm = paravirt_nop,
  347. .lazy_mode = {
  348. .enter = paravirt_nop,
  349. .leave = paravirt_nop,
  350. },
  351. };
  352. EXPORT_SYMBOL_GPL(pv_time_ops);
  353. EXPORT_SYMBOL (pv_cpu_ops);
  354. EXPORT_SYMBOL (pv_mmu_ops);
  355. EXPORT_SYMBOL_GPL(pv_apic_ops);
  356. EXPORT_SYMBOL_GPL(pv_info);
  357. EXPORT_SYMBOL (pv_irq_ops);