paravirt.c 11 KB

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