pageattr.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MMU-generic set_memory implementation for powerpc
  4. *
  5. * Copyright 2019-2021, IBM Corporation.
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/set_memory.h>
  10. #include <asm/mmu.h>
  11. #include <asm/page.h>
  12. #include <asm/pgtable.h>
  13. #include <mm/mmu_decl.h>
  14. static pte_basic_t pte_update_delta(pte_t *ptep, unsigned long addr,
  15. unsigned long old, unsigned long new)
  16. {
  17. return pte_update(&init_mm, addr, ptep, old & ~new, new & ~old, 0);
  18. }
  19. /*
  20. * Updates the attributes of a page atomically.
  21. *
  22. * This sequence is safe against concurrent updates, and also allows updating the
  23. * attributes of a page currently being executed or accessed.
  24. */
  25. static int change_page_attr(pte_t *ptep, unsigned long addr, void *data)
  26. {
  27. long action = (long)data;
  28. addr &= PAGE_MASK;
  29. /* modify the PTE bits as desired */
  30. switch (action) {
  31. case SET_MEMORY_RO:
  32. /* Don't clear DIRTY bit */
  33. pte_update_delta(ptep, addr, _PAGE_KERNEL_RW & ~_PAGE_DIRTY, _PAGE_KERNEL_RO);
  34. break;
  35. case SET_MEMORY_ROX:
  36. /* Don't clear DIRTY bit */
  37. pte_update_delta(ptep, addr, _PAGE_KERNEL_RW & ~_PAGE_DIRTY, _PAGE_KERNEL_ROX);
  38. break;
  39. case SET_MEMORY_RW:
  40. pte_update_delta(ptep, addr, _PAGE_KERNEL_RO, _PAGE_KERNEL_RW);
  41. break;
  42. case SET_MEMORY_NX:
  43. pte_update_delta(ptep, addr, _PAGE_KERNEL_ROX, _PAGE_KERNEL_RO);
  44. break;
  45. case SET_MEMORY_X:
  46. pte_update_delta(ptep, addr, _PAGE_KERNEL_RO, _PAGE_KERNEL_ROX);
  47. break;
  48. case SET_MEMORY_NP:
  49. pte_update(&init_mm, addr, ptep, _PAGE_PRESENT, 0, 0);
  50. break;
  51. case SET_MEMORY_P:
  52. pte_update(&init_mm, addr, ptep, 0, _PAGE_PRESENT, 0);
  53. break;
  54. default:
  55. WARN_ON_ONCE(1);
  56. break;
  57. }
  58. /* See ptesync comment in radix__set_pte_at() */
  59. if (radix_enabled())
  60. asm volatile("ptesync": : :"memory");
  61. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  62. return 0;
  63. }
  64. int change_memory_attr(unsigned long addr, int numpages, long action)
  65. {
  66. unsigned long start = ALIGN_DOWN(addr, PAGE_SIZE);
  67. unsigned long size = numpages * PAGE_SIZE;
  68. if (!numpages)
  69. return 0;
  70. if (WARN_ON_ONCE(is_vmalloc_or_module_addr((void *)addr) &&
  71. is_vm_area_hugepages((void *)addr)))
  72. return -EINVAL;
  73. #ifdef CONFIG_PPC_BOOK3S_64
  74. /*
  75. * On hash, the linear mapping is not in the Linux page table so
  76. * apply_to_existing_page_range() will have no effect. If in the future
  77. * the set_memory_* functions are used on the linear map this will need
  78. * to be updated.
  79. */
  80. if (!radix_enabled()) {
  81. int region = get_region_id(addr);
  82. if (WARN_ON_ONCE(region != VMALLOC_REGION_ID && region != IO_REGION_ID))
  83. return -EINVAL;
  84. }
  85. #endif
  86. return apply_to_existing_page_range(&init_mm, start, size,
  87. change_page_attr, (void *)action);
  88. }
  89. #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KFENCE)
  90. #ifdef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
  91. void __kernel_map_pages(struct page *page, int numpages, int enable)
  92. {
  93. int err;
  94. unsigned long addr = (unsigned long)page_address(page);
  95. if (PageHighMem(page))
  96. return;
  97. if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled())
  98. err = hash__kernel_map_pages(page, numpages, enable);
  99. else if (enable)
  100. err = set_memory_p(addr, numpages);
  101. else
  102. err = set_memory_np(addr, numpages);
  103. if (err)
  104. panic("%s: changing memory protections failed\n", __func__);
  105. }
  106. #endif
  107. #endif