paravirt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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/init.h>
  18. #include <linux/export.h>
  19. #include <linux/efi.h>
  20. #include <linux/bcd.h>
  21. #include <linux/highmem.h>
  22. #include <linux/kprobes.h>
  23. #include <asm/bug.h>
  24. #include <asm/paravirt.h>
  25. #include <asm/debugreg.h>
  26. #include <asm/desc.h>
  27. #include <asm/setup.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/time.h>
  30. #include <asm/pgalloc.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. #include <asm/special_insns.h>
  38. #include <asm/tlb.h>
  39. /*
  40. * nop stub, which must not clobber anything *including the stack* to
  41. * avoid confusing the entry prologues.
  42. */
  43. extern void _paravirt_nop(void);
  44. asm (".pushsection .entry.text, \"ax\"\n"
  45. ".global _paravirt_nop\n"
  46. "_paravirt_nop:\n\t"
  47. "ret\n\t"
  48. ".size _paravirt_nop, . - _paravirt_nop\n\t"
  49. ".type _paravirt_nop, @function\n\t"
  50. ".popsection");
  51. /* identity function, which can be inlined */
  52. u32 notrace _paravirt_ident_32(u32 x)
  53. {
  54. return x;
  55. }
  56. u64 notrace _paravirt_ident_64(u64 x)
  57. {
  58. return x;
  59. }
  60. void __init default_banner(void)
  61. {
  62. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  63. pv_info.name);
  64. }
  65. /* Undefined instruction for dealing with missing ops pointers. */
  66. static const unsigned char ud2a[] = { 0x0f, 0x0b };
  67. struct branch {
  68. unsigned char opcode;
  69. u32 delta;
  70. } __attribute__((packed));
  71. unsigned paravirt_patch_call(void *insnbuf,
  72. const void *target, u16 tgt_clobbers,
  73. unsigned long addr, u16 site_clobbers,
  74. unsigned len)
  75. {
  76. struct branch *b = insnbuf;
  77. unsigned long delta = (unsigned long)target - (addr+5);
  78. if (len < 5) {
  79. #ifdef CONFIG_RETPOLINE
  80. WARN_ONCE(1, "Failing to patch indirect CALL in %ps\n", (void *)addr);
  81. #endif
  82. return len; /* call too long for patch site */
  83. }
  84. b->opcode = 0xe8; /* call */
  85. b->delta = delta;
  86. BUILD_BUG_ON(sizeof(*b) != 5);
  87. return 5;
  88. }
  89. unsigned paravirt_patch_jmp(void *insnbuf, const void *target,
  90. unsigned long addr, unsigned len)
  91. {
  92. struct branch *b = insnbuf;
  93. unsigned long delta = (unsigned long)target - (addr+5);
  94. if (len < 5) {
  95. #ifdef CONFIG_RETPOLINE
  96. WARN_ONCE(1, "Failing to patch indirect JMP in %ps\n", (void *)addr);
  97. #endif
  98. return len; /* call too long for patch site */
  99. }
  100. b->opcode = 0xe9; /* jmp */
  101. b->delta = delta;
  102. return 5;
  103. }
  104. DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
  105. void __init native_pv_lock_init(void)
  106. {
  107. if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
  108. static_branch_disable(&virt_spin_lock_key);
  109. }
  110. /*
  111. * Neat trick to map patch type back to the call within the
  112. * corresponding structure.
  113. */
  114. static void *get_call_destination(u8 type)
  115. {
  116. struct paravirt_patch_template tmpl = {
  117. .pv_init_ops = pv_init_ops,
  118. .pv_time_ops = pv_time_ops,
  119. .pv_cpu_ops = pv_cpu_ops,
  120. .pv_irq_ops = pv_irq_ops,
  121. .pv_mmu_ops = pv_mmu_ops,
  122. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  123. .pv_lock_ops = pv_lock_ops,
  124. #endif
  125. };
  126. return *((void **)&tmpl + type);
  127. }
  128. unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
  129. unsigned long addr, unsigned len)
  130. {
  131. void *opfunc = get_call_destination(type);
  132. unsigned ret;
  133. if (opfunc == NULL)
  134. /* If there's no function, patch it with a ud2a (BUG) */
  135. ret = paravirt_patch_insns(insnbuf, len, ud2a, ud2a+sizeof(ud2a));
  136. else if (opfunc == _paravirt_nop)
  137. ret = 0;
  138. /* identity functions just return their single argument */
  139. else if (opfunc == _paravirt_ident_32)
  140. ret = paravirt_patch_ident_32(insnbuf, len);
  141. else if (opfunc == _paravirt_ident_64)
  142. ret = paravirt_patch_ident_64(insnbuf, len);
  143. else if (type == PARAVIRT_PATCH(pv_cpu_ops.iret) ||
  144. type == PARAVIRT_PATCH(pv_cpu_ops.usergs_sysret64))
  145. /* If operation requires a jmp, then jmp */
  146. ret = paravirt_patch_jmp(insnbuf, opfunc, addr, len);
  147. else
  148. /* Otherwise call the function; assume target could
  149. clobber any caller-save reg */
  150. ret = paravirt_patch_call(insnbuf, opfunc, CLBR_ANY,
  151. addr, clobbers, len);
  152. return ret;
  153. }
  154. unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
  155. const char *start, const char *end)
  156. {
  157. unsigned insn_len = end - start;
  158. if (insn_len > len || start == NULL)
  159. insn_len = len;
  160. else
  161. memcpy(insnbuf, start, insn_len);
  162. return insn_len;
  163. }
  164. static void native_flush_tlb(void)
  165. {
  166. __native_flush_tlb();
  167. }
  168. /*
  169. * Global pages have to be flushed a bit differently. Not a real
  170. * performance problem because this does not happen often.
  171. */
  172. static void native_flush_tlb_global(void)
  173. {
  174. __native_flush_tlb_global();
  175. }
  176. static void native_flush_tlb_one_user(unsigned long addr)
  177. {
  178. __native_flush_tlb_one_user(addr);
  179. }
  180. struct static_key paravirt_steal_enabled;
  181. struct static_key paravirt_steal_rq_enabled;
  182. static u64 native_steal_clock(int cpu)
  183. {
  184. return 0;
  185. }
  186. /* These are in entry.S */
  187. extern void native_iret(void);
  188. extern void native_usergs_sysret64(void);
  189. static struct resource reserve_ioports = {
  190. .start = 0,
  191. .end = IO_SPACE_LIMIT,
  192. .name = "paravirt-ioport",
  193. .flags = IORESOURCE_IO | IORESOURCE_BUSY,
  194. };
  195. /*
  196. * Reserve the whole legacy IO space to prevent any legacy drivers
  197. * from wasting time probing for their hardware. This is a fairly
  198. * brute-force approach to disabling all non-virtual drivers.
  199. *
  200. * Note that this must be called very early to have any effect.
  201. */
  202. int paravirt_disable_iospace(void)
  203. {
  204. return request_resource(&ioport_resource, &reserve_ioports);
  205. }
  206. static DEFINE_PER_CPU(enum paravirt_lazy_mode, paravirt_lazy_mode) = PARAVIRT_LAZY_NONE;
  207. static inline void enter_lazy(enum paravirt_lazy_mode mode)
  208. {
  209. BUG_ON(this_cpu_read(paravirt_lazy_mode) != PARAVIRT_LAZY_NONE);
  210. this_cpu_write(paravirt_lazy_mode, mode);
  211. }
  212. static void leave_lazy(enum paravirt_lazy_mode mode)
  213. {
  214. BUG_ON(this_cpu_read(paravirt_lazy_mode) != mode);
  215. this_cpu_write(paravirt_lazy_mode, PARAVIRT_LAZY_NONE);
  216. }
  217. void paravirt_enter_lazy_mmu(void)
  218. {
  219. enter_lazy(PARAVIRT_LAZY_MMU);
  220. }
  221. void paravirt_leave_lazy_mmu(void)
  222. {
  223. leave_lazy(PARAVIRT_LAZY_MMU);
  224. }
  225. void paravirt_flush_lazy_mmu(void)
  226. {
  227. preempt_disable();
  228. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) {
  229. arch_leave_lazy_mmu_mode();
  230. arch_enter_lazy_mmu_mode();
  231. }
  232. preempt_enable();
  233. }
  234. void paravirt_start_context_switch(struct task_struct *prev)
  235. {
  236. BUG_ON(preemptible());
  237. if (this_cpu_read(paravirt_lazy_mode) == PARAVIRT_LAZY_MMU) {
  238. arch_leave_lazy_mmu_mode();
  239. set_ti_thread_flag(task_thread_info(prev), TIF_LAZY_MMU_UPDATES);
  240. }
  241. enter_lazy(PARAVIRT_LAZY_CPU);
  242. }
  243. void paravirt_end_context_switch(struct task_struct *next)
  244. {
  245. BUG_ON(preemptible());
  246. leave_lazy(PARAVIRT_LAZY_CPU);
  247. if (test_and_clear_ti_thread_flag(task_thread_info(next), TIF_LAZY_MMU_UPDATES))
  248. arch_enter_lazy_mmu_mode();
  249. }
  250. enum paravirt_lazy_mode paravirt_get_lazy_mode(void)
  251. {
  252. if (in_interrupt())
  253. return PARAVIRT_LAZY_NONE;
  254. return this_cpu_read(paravirt_lazy_mode);
  255. }
  256. struct pv_info pv_info = {
  257. .name = "bare hardware",
  258. .kernel_rpl = 0,
  259. .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
  260. #ifdef CONFIG_X86_64
  261. .extra_user_64bit_cs = __USER_CS,
  262. #endif
  263. };
  264. struct pv_init_ops pv_init_ops = {
  265. .patch = native_patch,
  266. };
  267. struct pv_time_ops pv_time_ops = {
  268. .sched_clock = native_sched_clock,
  269. .steal_clock = native_steal_clock,
  270. };
  271. __visible struct pv_irq_ops pv_irq_ops = {
  272. .save_fl = __PV_IS_CALLEE_SAVE(native_save_fl),
  273. .restore_fl = __PV_IS_CALLEE_SAVE(native_restore_fl),
  274. .irq_disable = __PV_IS_CALLEE_SAVE(native_irq_disable),
  275. .irq_enable = __PV_IS_CALLEE_SAVE(native_irq_enable),
  276. .safe_halt = native_safe_halt,
  277. .halt = native_halt,
  278. };
  279. __visible struct pv_cpu_ops pv_cpu_ops = {
  280. .cpuid = native_cpuid,
  281. .get_debugreg = native_get_debugreg,
  282. .set_debugreg = native_set_debugreg,
  283. .read_cr0 = native_read_cr0,
  284. .write_cr0 = native_write_cr0,
  285. .write_cr4 = native_write_cr4,
  286. #ifdef CONFIG_X86_64
  287. .read_cr8 = native_read_cr8,
  288. .write_cr8 = native_write_cr8,
  289. #endif
  290. .wbinvd = native_wbinvd,
  291. .read_msr = native_read_msr,
  292. .write_msr = native_write_msr,
  293. .read_msr_safe = native_read_msr_safe,
  294. .write_msr_safe = native_write_msr_safe,
  295. .read_pmc = native_read_pmc,
  296. .load_tr_desc = native_load_tr_desc,
  297. .set_ldt = native_set_ldt,
  298. .load_gdt = native_load_gdt,
  299. .load_idt = native_load_idt,
  300. .store_tr = native_store_tr,
  301. .load_tls = native_load_tls,
  302. #ifdef CONFIG_X86_64
  303. .load_gs_index = native_load_gs_index,
  304. #endif
  305. .write_ldt_entry = native_write_ldt_entry,
  306. .write_gdt_entry = native_write_gdt_entry,
  307. .write_idt_entry = native_write_idt_entry,
  308. .alloc_ldt = paravirt_nop,
  309. .free_ldt = paravirt_nop,
  310. .load_sp0 = native_load_sp0,
  311. #ifdef CONFIG_X86_64
  312. .usergs_sysret64 = native_usergs_sysret64,
  313. #endif
  314. .iret = native_iret,
  315. .swapgs = native_swapgs,
  316. .set_iopl_mask = native_set_iopl_mask,
  317. .io_delay = native_io_delay,
  318. .start_context_switch = paravirt_nop,
  319. .end_context_switch = paravirt_nop,
  320. };
  321. /* At this point, native_get/set_debugreg has real function entries */
  322. NOKPROBE_SYMBOL(native_get_debugreg);
  323. NOKPROBE_SYMBOL(native_set_debugreg);
  324. NOKPROBE_SYMBOL(native_load_idt);
  325. #if defined(CONFIG_X86_32) && !defined(CONFIG_X86_PAE)
  326. /* 32-bit pagetable entries */
  327. #define PTE_IDENT __PV_IS_CALLEE_SAVE(_paravirt_ident_32)
  328. #else
  329. /* 64-bit pagetable entries */
  330. #define PTE_IDENT __PV_IS_CALLEE_SAVE(_paravirt_ident_64)
  331. #endif
  332. struct pv_mmu_ops pv_mmu_ops __ro_after_init = {
  333. .read_cr2 = native_read_cr2,
  334. .write_cr2 = native_write_cr2,
  335. .read_cr3 = __native_read_cr3,
  336. .write_cr3 = native_write_cr3,
  337. .flush_tlb_user = native_flush_tlb,
  338. .flush_tlb_kernel = native_flush_tlb_global,
  339. .flush_tlb_one_user = native_flush_tlb_one_user,
  340. .flush_tlb_others = native_flush_tlb_others,
  341. .tlb_remove_table = (void (*)(struct mmu_gather *, void *))tlb_remove_page,
  342. .pgd_alloc = __paravirt_pgd_alloc,
  343. .pgd_free = paravirt_nop,
  344. .alloc_pte = paravirt_nop,
  345. .alloc_pmd = paravirt_nop,
  346. .alloc_pud = paravirt_nop,
  347. .alloc_p4d = paravirt_nop,
  348. .release_pte = paravirt_nop,
  349. .release_pmd = paravirt_nop,
  350. .release_pud = paravirt_nop,
  351. .release_p4d = paravirt_nop,
  352. .set_pte = native_set_pte,
  353. .set_pte_at = native_set_pte_at,
  354. .set_pmd = native_set_pmd,
  355. .ptep_modify_prot_start = __ptep_modify_prot_start,
  356. .ptep_modify_prot_commit = __ptep_modify_prot_commit,
  357. #if CONFIG_PGTABLE_LEVELS >= 3
  358. #ifdef CONFIG_X86_PAE
  359. .set_pte_atomic = native_set_pte_atomic,
  360. .pte_clear = native_pte_clear,
  361. .pmd_clear = native_pmd_clear,
  362. #endif
  363. .set_pud = native_set_pud,
  364. .pmd_val = PTE_IDENT,
  365. .make_pmd = PTE_IDENT,
  366. #if CONFIG_PGTABLE_LEVELS >= 4
  367. .pud_val = PTE_IDENT,
  368. .make_pud = PTE_IDENT,
  369. .set_p4d = native_set_p4d,
  370. #if CONFIG_PGTABLE_LEVELS >= 5
  371. .p4d_val = PTE_IDENT,
  372. .make_p4d = PTE_IDENT,
  373. .set_pgd = native_set_pgd,
  374. #endif /* CONFIG_PGTABLE_LEVELS >= 5 */
  375. #endif /* CONFIG_PGTABLE_LEVELS >= 4 */
  376. #endif /* CONFIG_PGTABLE_LEVELS >= 3 */
  377. .pte_val = PTE_IDENT,
  378. .pgd_val = PTE_IDENT,
  379. .make_pte = PTE_IDENT,
  380. .make_pgd = PTE_IDENT,
  381. .dup_mmap = paravirt_nop,
  382. .exit_mmap = paravirt_nop,
  383. .activate_mm = paravirt_nop,
  384. .lazy_mode = {
  385. .enter = paravirt_nop,
  386. .leave = paravirt_nop,
  387. .flush = paravirt_nop,
  388. },
  389. .set_fixmap = native_set_fixmap,
  390. };
  391. EXPORT_SYMBOL_GPL(pv_time_ops);
  392. EXPORT_SYMBOL (pv_cpu_ops);
  393. EXPORT_SYMBOL (pv_mmu_ops);
  394. EXPORT_SYMBOL_GPL(pv_info);
  395. EXPORT_SYMBOL (pv_irq_ops);