mmu_hvm.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include <linux/crash_dump.h>
  4. #include <xen/interface/xen.h>
  5. #include <xen/hvm.h>
  6. #include "mmu.h"
  7. #ifdef CONFIG_PROC_VMCORE
  8. /*
  9. * This function is used in two contexts:
  10. * - the kdump kernel has to check whether a pfn of the crashed kernel
  11. * was a ballooned page. vmcore is using this function to decide
  12. * whether to access a pfn of the crashed kernel.
  13. * - the kexec kernel has to check whether a pfn was ballooned by the
  14. * previous kernel. If the pfn is ballooned, handle it properly.
  15. * Returns 0 if the pfn is not backed by a RAM page, the caller may
  16. * handle the pfn special in this case.
  17. */
  18. static int xen_oldmem_pfn_is_ram(unsigned long pfn)
  19. {
  20. struct xen_hvm_get_mem_type a = {
  21. .domid = DOMID_SELF,
  22. .pfn = pfn,
  23. };
  24. int ram;
  25. if (HYPERVISOR_hvm_op(HVMOP_get_mem_type, &a))
  26. return -ENXIO;
  27. switch (a.mem_type) {
  28. case HVMMEM_mmio_dm:
  29. ram = 0;
  30. break;
  31. case HVMMEM_ram_rw:
  32. case HVMMEM_ram_ro:
  33. default:
  34. ram = 1;
  35. break;
  36. }
  37. return ram;
  38. }
  39. #endif
  40. static void xen_hvm_exit_mmap(struct mm_struct *mm)
  41. {
  42. struct xen_hvm_pagetable_dying a;
  43. int rc;
  44. a.domid = DOMID_SELF;
  45. a.gpa = __pa(mm->pgd);
  46. rc = HYPERVISOR_hvm_op(HVMOP_pagetable_dying, &a);
  47. WARN_ON_ONCE(rc < 0);
  48. }
  49. static int is_pagetable_dying_supported(void)
  50. {
  51. struct xen_hvm_pagetable_dying a;
  52. int rc = 0;
  53. a.domid = DOMID_SELF;
  54. a.gpa = 0x00;
  55. rc = HYPERVISOR_hvm_op(HVMOP_pagetable_dying, &a);
  56. if (rc < 0) {
  57. printk(KERN_DEBUG "HVMOP_pagetable_dying not supported\n");
  58. return 0;
  59. }
  60. return 1;
  61. }
  62. void __init xen_hvm_init_mmu_ops(void)
  63. {
  64. if (is_pagetable_dying_supported())
  65. pv_mmu_ops.exit_mmap = xen_hvm_exit_mmap;
  66. #ifdef CONFIG_PROC_VMCORE
  67. WARN_ON(register_oldmem_pfn_is_ram(&xen_oldmem_pfn_is_ram));
  68. #endif
  69. }