pageattr.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. static pte_basic_t pte_update_delta(pte_t *ptep, unsigned long addr,
  14. unsigned long old, unsigned long new)
  15. {
  16. return pte_update(&init_mm, addr, ptep, old & ~new, new & ~old, 0);
  17. }
  18. /*
  19. * Updates the attributes of a page atomically.
  20. *
  21. * This sequence is safe against concurrent updates, and also allows updating the
  22. * attributes of a page currently being executed or accessed.
  23. */
  24. static int change_page_attr(pte_t *ptep, unsigned long addr, void *data)
  25. {
  26. long action = (long)data;
  27. addr &= PAGE_MASK;
  28. /* modify the PTE bits as desired */
  29. switch (action) {
  30. case SET_MEMORY_RO:
  31. /* Don't clear DIRTY bit */
  32. pte_update_delta(ptep, addr, _PAGE_KERNEL_RW & ~_PAGE_DIRTY, _PAGE_KERNEL_RO);
  33. break;
  34. case SET_MEMORY_RW:
  35. pte_update_delta(ptep, addr, _PAGE_KERNEL_RO, _PAGE_KERNEL_RW);
  36. break;
  37. case SET_MEMORY_NX:
  38. pte_update_delta(ptep, addr, _PAGE_KERNEL_ROX, _PAGE_KERNEL_RO);
  39. break;
  40. case SET_MEMORY_X:
  41. pte_update_delta(ptep, addr, _PAGE_KERNEL_RO, _PAGE_KERNEL_ROX);
  42. break;
  43. case SET_MEMORY_NP:
  44. pte_update(&init_mm, addr, ptep, _PAGE_PRESENT, 0, 0);
  45. break;
  46. case SET_MEMORY_P:
  47. pte_update(&init_mm, addr, ptep, 0, _PAGE_PRESENT, 0);
  48. break;
  49. default:
  50. WARN_ON_ONCE(1);
  51. break;
  52. }
  53. /* See ptesync comment in radix__set_pte_at() */
  54. if (radix_enabled())
  55. asm volatile("ptesync": : :"memory");
  56. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  57. return 0;
  58. }
  59. int change_memory_attr(unsigned long addr, int numpages, long action)
  60. {
  61. unsigned long start = ALIGN_DOWN(addr, PAGE_SIZE);
  62. unsigned long size = numpages * PAGE_SIZE;
  63. if (!numpages)
  64. return 0;
  65. if (WARN_ON_ONCE(is_vmalloc_or_module_addr((void *)addr) &&
  66. is_vm_area_hugepages((void *)addr)))
  67. return -EINVAL;
  68. #ifdef CONFIG_PPC_BOOK3S_64
  69. /*
  70. * On hash, the linear mapping is not in the Linux page table so
  71. * apply_to_existing_page_range() will have no effect. If in the future
  72. * the set_memory_* functions are used on the linear map this will need
  73. * to be updated.
  74. */
  75. if (!radix_enabled()) {
  76. int region = get_region_id(addr);
  77. if (WARN_ON_ONCE(region != VMALLOC_REGION_ID && region != IO_REGION_ID))
  78. return -EINVAL;
  79. }
  80. #endif
  81. return apply_to_existing_page_range(&init_mm, start, size,
  82. change_page_attr, (void *)action);
  83. }