paravirt.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Paravirtualization interfaces
  3. Copyright (C) 2006 Rusty Russell IBM Corporation
  4. 2007 - x86_64 support added by Glauber de Oliveira Costa, Red Hat Inc
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/init.h>
  8. #include <linux/export.h>
  9. #include <linux/efi.h>
  10. #include <linux/bcd.h>
  11. #include <linux/highmem.h>
  12. #include <linux/kprobes.h>
  13. #include <linux/pgtable.h>
  14. #include <linux/static_call.h>
  15. #include <asm/bug.h>
  16. #include <asm/paravirt.h>
  17. #include <asm/debugreg.h>
  18. #include <asm/desc.h>
  19. #include <asm/setup.h>
  20. #include <asm/time.h>
  21. #include <asm/pgalloc.h>
  22. #include <asm/irq.h>
  23. #include <asm/delay.h>
  24. #include <asm/fixmap.h>
  25. #include <asm/apic.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/timer.h>
  28. #include <asm/special_insns.h>
  29. #include <asm/tlb.h>
  30. #include <asm/io_bitmap.h>
  31. #include <asm/gsseg.h>
  32. /* stub always returning 0. */
  33. DEFINE_ASM_FUNC(paravirt_ret0, "xor %eax,%eax", .entry.text);
  34. void __init default_banner(void)
  35. {
  36. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  37. pv_info.name);
  38. }
  39. #ifdef CONFIG_PARAVIRT_XXL
  40. DEFINE_ASM_FUNC(_paravirt_ident_64, "mov %rdi, %rax", .text);
  41. DEFINE_ASM_FUNC(pv_native_save_fl, "pushf; pop %rax", .noinstr.text);
  42. DEFINE_ASM_FUNC(pv_native_irq_disable, "cli", .noinstr.text);
  43. DEFINE_ASM_FUNC(pv_native_irq_enable, "sti", .noinstr.text);
  44. DEFINE_ASM_FUNC(pv_native_read_cr2, "mov %cr2, %rax", .noinstr.text);
  45. #endif
  46. DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key);
  47. void __init native_pv_lock_init(void)
  48. {
  49. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  50. static_branch_enable(&virt_spin_lock_key);
  51. }
  52. static void native_tlb_remove_table(struct mmu_gather *tlb, void *table)
  53. {
  54. tlb_remove_page(tlb, table);
  55. }
  56. struct static_key paravirt_steal_enabled;
  57. struct static_key paravirt_steal_rq_enabled;
  58. static u64 native_steal_clock(int cpu)
  59. {
  60. return 0;
  61. }
  62. DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock);
  63. DEFINE_STATIC_CALL(pv_sched_clock, native_sched_clock);
  64. void paravirt_set_sched_clock(u64 (*func)(void))
  65. {
  66. static_call_update(pv_sched_clock, func);
  67. }
  68. /* These are in entry.S */
  69. static struct resource reserve_ioports = {
  70. .start = 0,
  71. .end = IO_SPACE_LIMIT,
  72. .name = "paravirt-ioport",
  73. .flags = IORESOURCE_IO | IORESOURCE_BUSY,
  74. };
  75. /*
  76. * Reserve the whole legacy IO space to prevent any legacy drivers
  77. * from wasting time probing for their hardware. This is a fairly
  78. * brute-force approach to disabling all non-virtual drivers.
  79. *
  80. * Note that this must be called very early to have any effect.
  81. */
  82. int paravirt_disable_iospace(void)
  83. {
  84. return request_resource(&ioport_resource, &reserve_ioports);
  85. }
  86. static noinstr void pv_native_safe_halt(void)
  87. {
  88. native_safe_halt();
  89. }
  90. #ifdef CONFIG_PARAVIRT_XXL
  91. static noinstr void pv_native_write_cr2(unsigned long val)
  92. {
  93. native_write_cr2(val);
  94. }
  95. static noinstr unsigned long pv_native_get_debugreg(int regno)
  96. {
  97. return native_get_debugreg(regno);
  98. }
  99. static noinstr void pv_native_set_debugreg(int regno, unsigned long val)
  100. {
  101. native_set_debugreg(regno, val);
  102. }
  103. noinstr void pv_native_wbinvd(void)
  104. {
  105. native_wbinvd();
  106. }
  107. #endif
  108. struct pv_info pv_info = {
  109. .name = "bare hardware",
  110. #ifdef CONFIG_PARAVIRT_XXL
  111. .extra_user_64bit_cs = __USER_CS,
  112. #endif
  113. };
  114. /* 64-bit pagetable entries */
  115. #define PTE_IDENT __PV_IS_CALLEE_SAVE(_paravirt_ident_64)
  116. struct paravirt_patch_template pv_ops = {
  117. /* Cpu ops. */
  118. .cpu.io_delay = native_io_delay,
  119. #ifdef CONFIG_PARAVIRT_XXL
  120. .cpu.cpuid = native_cpuid,
  121. .cpu.get_debugreg = pv_native_get_debugreg,
  122. .cpu.set_debugreg = pv_native_set_debugreg,
  123. .cpu.read_cr0 = native_read_cr0,
  124. .cpu.write_cr0 = native_write_cr0,
  125. .cpu.write_cr4 = native_write_cr4,
  126. .cpu.wbinvd = pv_native_wbinvd,
  127. .cpu.read_msr = native_read_msr,
  128. .cpu.write_msr = native_write_msr,
  129. .cpu.read_msr_safe = native_read_msr_safe,
  130. .cpu.write_msr_safe = native_write_msr_safe,
  131. .cpu.read_pmc = native_read_pmc,
  132. .cpu.load_tr_desc = native_load_tr_desc,
  133. .cpu.set_ldt = native_set_ldt,
  134. .cpu.load_gdt = native_load_gdt,
  135. .cpu.load_idt = native_load_idt,
  136. .cpu.store_tr = native_store_tr,
  137. .cpu.load_tls = native_load_tls,
  138. .cpu.load_gs_index = native_load_gs_index,
  139. .cpu.write_ldt_entry = native_write_ldt_entry,
  140. .cpu.write_gdt_entry = native_write_gdt_entry,
  141. .cpu.write_idt_entry = native_write_idt_entry,
  142. .cpu.alloc_ldt = paravirt_nop,
  143. .cpu.free_ldt = paravirt_nop,
  144. .cpu.load_sp0 = native_load_sp0,
  145. #ifdef CONFIG_X86_IOPL_IOPERM
  146. .cpu.invalidate_io_bitmap = native_tss_invalidate_io_bitmap,
  147. .cpu.update_io_bitmap = native_tss_update_io_bitmap,
  148. #endif
  149. .cpu.start_context_switch = paravirt_nop,
  150. .cpu.end_context_switch = paravirt_nop,
  151. /* Irq ops. */
  152. .irq.save_fl = __PV_IS_CALLEE_SAVE(pv_native_save_fl),
  153. .irq.irq_disable = __PV_IS_CALLEE_SAVE(pv_native_irq_disable),
  154. .irq.irq_enable = __PV_IS_CALLEE_SAVE(pv_native_irq_enable),
  155. #endif /* CONFIG_PARAVIRT_XXL */
  156. /* Irq HLT ops. */
  157. .irq.safe_halt = pv_native_safe_halt,
  158. .irq.halt = native_halt,
  159. /* Mmu ops. */
  160. .mmu.flush_tlb_user = native_flush_tlb_local,
  161. .mmu.flush_tlb_kernel = native_flush_tlb_global,
  162. .mmu.flush_tlb_one_user = native_flush_tlb_one_user,
  163. .mmu.flush_tlb_multi = native_flush_tlb_multi,
  164. .mmu.tlb_remove_table = native_tlb_remove_table,
  165. .mmu.exit_mmap = paravirt_nop,
  166. .mmu.notify_page_enc_status_changed = paravirt_nop,
  167. #ifdef CONFIG_PARAVIRT_XXL
  168. .mmu.read_cr2 = __PV_IS_CALLEE_SAVE(pv_native_read_cr2),
  169. .mmu.write_cr2 = pv_native_write_cr2,
  170. .mmu.read_cr3 = __native_read_cr3,
  171. .mmu.write_cr3 = native_write_cr3,
  172. .mmu.pgd_alloc = __paravirt_pgd_alloc,
  173. .mmu.pgd_free = paravirt_nop,
  174. .mmu.alloc_pte = paravirt_nop,
  175. .mmu.alloc_pmd = paravirt_nop,
  176. .mmu.alloc_pud = paravirt_nop,
  177. .mmu.alloc_p4d = paravirt_nop,
  178. .mmu.release_pte = paravirt_nop,
  179. .mmu.release_pmd = paravirt_nop,
  180. .mmu.release_pud = paravirt_nop,
  181. .mmu.release_p4d = paravirt_nop,
  182. .mmu.set_pte = native_set_pte,
  183. .mmu.set_pmd = native_set_pmd,
  184. .mmu.ptep_modify_prot_start = __ptep_modify_prot_start,
  185. .mmu.ptep_modify_prot_commit = __ptep_modify_prot_commit,
  186. .mmu.set_pud = native_set_pud,
  187. .mmu.pmd_val = PTE_IDENT,
  188. .mmu.make_pmd = PTE_IDENT,
  189. .mmu.pud_val = PTE_IDENT,
  190. .mmu.make_pud = PTE_IDENT,
  191. .mmu.set_p4d = native_set_p4d,
  192. #if CONFIG_PGTABLE_LEVELS >= 5
  193. .mmu.p4d_val = PTE_IDENT,
  194. .mmu.make_p4d = PTE_IDENT,
  195. .mmu.set_pgd = native_set_pgd,
  196. #endif /* CONFIG_PGTABLE_LEVELS >= 5 */
  197. .mmu.pte_val = PTE_IDENT,
  198. .mmu.pgd_val = PTE_IDENT,
  199. .mmu.make_pte = PTE_IDENT,
  200. .mmu.make_pgd = PTE_IDENT,
  201. .mmu.enter_mmap = paravirt_nop,
  202. .mmu.lazy_mode = {
  203. .enter = paravirt_nop,
  204. .leave = paravirt_nop,
  205. .flush = paravirt_nop,
  206. },
  207. .mmu.set_fixmap = native_set_fixmap,
  208. #endif /* CONFIG_PARAVIRT_XXL */
  209. #if defined(CONFIG_PARAVIRT_SPINLOCKS)
  210. /* Lock ops. */
  211. #ifdef CONFIG_SMP
  212. .lock.queued_spin_lock_slowpath = native_queued_spin_lock_slowpath,
  213. .lock.queued_spin_unlock =
  214. PV_CALLEE_SAVE(__native_queued_spin_unlock),
  215. .lock.wait = paravirt_nop,
  216. .lock.kick = paravirt_nop,
  217. .lock.vcpu_is_preempted =
  218. PV_CALLEE_SAVE(__native_vcpu_is_preempted),
  219. #endif /* SMP */
  220. #endif
  221. };
  222. #ifdef CONFIG_PARAVIRT_XXL
  223. NOKPROBE_SYMBOL(native_load_idt);
  224. #endif
  225. EXPORT_SYMBOL(pv_ops);
  226. EXPORT_SYMBOL_GPL(pv_info);