paravirt_patch_64.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <asm/paravirt.h>
  2. #include <asm/asm-offsets.h>
  3. #include <linux/stringify.h>
  4. DEF_NATIVE(pv_irq_ops, irq_disable, "cli");
  5. DEF_NATIVE(pv_irq_ops, irq_enable, "sti");
  6. DEF_NATIVE(pv_irq_ops, restore_fl, "pushq %rdi; popfq");
  7. DEF_NATIVE(pv_irq_ops, save_fl, "pushfq; popq %rax");
  8. DEF_NATIVE(pv_mmu_ops, read_cr2, "movq %cr2, %rax");
  9. DEF_NATIVE(pv_mmu_ops, read_cr3, "movq %cr3, %rax");
  10. DEF_NATIVE(pv_mmu_ops, write_cr3, "movq %rdi, %cr3");
  11. DEF_NATIVE(pv_mmu_ops, flush_tlb_single, "invlpg (%rdi)");
  12. DEF_NATIVE(pv_cpu_ops, wbinvd, "wbinvd");
  13. DEF_NATIVE(pv_cpu_ops, usergs_sysret64, "swapgs; sysretq");
  14. DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs");
  15. DEF_NATIVE(, mov32, "mov %edi, %eax");
  16. DEF_NATIVE(, mov64, "mov %rdi, %rax");
  17. #if defined(CONFIG_PARAVIRT_SPINLOCKS)
  18. DEF_NATIVE(pv_lock_ops, queued_spin_unlock, "movb $0, (%rdi)");
  19. DEF_NATIVE(pv_lock_ops, vcpu_is_preempted, "xor %rax, %rax");
  20. #endif
  21. unsigned paravirt_patch_ident_32(void *insnbuf, unsigned len)
  22. {
  23. return paravirt_patch_insns(insnbuf, len,
  24. start__mov32, end__mov32);
  25. }
  26. unsigned paravirt_patch_ident_64(void *insnbuf, unsigned len)
  27. {
  28. return paravirt_patch_insns(insnbuf, len,
  29. start__mov64, end__mov64);
  30. }
  31. extern bool pv_is_native_spin_unlock(void);
  32. extern bool pv_is_native_vcpu_is_preempted(void);
  33. unsigned native_patch(u8 type, u16 clobbers, void *ibuf,
  34. unsigned long addr, unsigned len)
  35. {
  36. const unsigned char *start, *end;
  37. unsigned ret;
  38. #define PATCH_SITE(ops, x) \
  39. case PARAVIRT_PATCH(ops.x): \
  40. start = start_##ops##_##x; \
  41. end = end_##ops##_##x; \
  42. goto patch_site
  43. switch(type) {
  44. PATCH_SITE(pv_irq_ops, restore_fl);
  45. PATCH_SITE(pv_irq_ops, save_fl);
  46. PATCH_SITE(pv_irq_ops, irq_enable);
  47. PATCH_SITE(pv_irq_ops, irq_disable);
  48. PATCH_SITE(pv_cpu_ops, usergs_sysret64);
  49. PATCH_SITE(pv_cpu_ops, swapgs);
  50. PATCH_SITE(pv_mmu_ops, read_cr2);
  51. PATCH_SITE(pv_mmu_ops, read_cr3);
  52. PATCH_SITE(pv_mmu_ops, write_cr3);
  53. PATCH_SITE(pv_mmu_ops, flush_tlb_single);
  54. PATCH_SITE(pv_cpu_ops, wbinvd);
  55. #if defined(CONFIG_PARAVIRT_SPINLOCKS)
  56. case PARAVIRT_PATCH(pv_lock_ops.queued_spin_unlock):
  57. if (pv_is_native_spin_unlock()) {
  58. start = start_pv_lock_ops_queued_spin_unlock;
  59. end = end_pv_lock_ops_queued_spin_unlock;
  60. goto patch_site;
  61. }
  62. goto patch_default;
  63. case PARAVIRT_PATCH(pv_lock_ops.vcpu_is_preempted):
  64. if (pv_is_native_vcpu_is_preempted()) {
  65. start = start_pv_lock_ops_vcpu_is_preempted;
  66. end = end_pv_lock_ops_vcpu_is_preempted;
  67. goto patch_site;
  68. }
  69. goto patch_default;
  70. #endif
  71. default:
  72. patch_default: __maybe_unused
  73. ret = paravirt_patch_default(type, clobbers, ibuf, addr, len);
  74. break;
  75. patch_site:
  76. ret = paravirt_patch_insns(ibuf, len, start, end);
  77. break;
  78. }
  79. #undef PATCH_SITE
  80. return ret;
  81. }