pageattr.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/module.h>
  16. #include <linux/sched.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/tlbflush.h>
  19. struct page_change_data {
  20. pgprot_t set_mask;
  21. pgprot_t clear_mask;
  22. };
  23. static int change_page_range(pte_t *ptep, pgtable_t token, unsigned long addr,
  24. void *data)
  25. {
  26. struct page_change_data *cdata = data;
  27. pte_t pte = *ptep;
  28. pte = clear_pte_bit(pte, cdata->clear_mask);
  29. pte = set_pte_bit(pte, cdata->set_mask);
  30. set_pte(ptep, pte);
  31. return 0;
  32. }
  33. static int change_memory_common(unsigned long addr, int numpages,
  34. pgprot_t set_mask, pgprot_t clear_mask)
  35. {
  36. unsigned long start = addr;
  37. unsigned long size = PAGE_SIZE*numpages;
  38. unsigned long end = start + size;
  39. int ret;
  40. struct page_change_data data;
  41. if (!PAGE_ALIGNED(addr)) {
  42. start &= PAGE_MASK;
  43. end = start + size;
  44. WARN_ON_ONCE(1);
  45. }
  46. if (start < MODULES_VADDR || start >= MODULES_END)
  47. return -EINVAL;
  48. if (end < MODULES_VADDR || end >= MODULES_END)
  49. return -EINVAL;
  50. if (!numpages)
  51. return 0;
  52. data.set_mask = set_mask;
  53. data.clear_mask = clear_mask;
  54. ret = apply_to_page_range(&init_mm, start, size, change_page_range,
  55. &data);
  56. flush_tlb_kernel_range(start, end);
  57. return ret;
  58. }
  59. int set_memory_ro(unsigned long addr, int numpages)
  60. {
  61. return change_memory_common(addr, numpages,
  62. __pgprot(PTE_RDONLY),
  63. __pgprot(PTE_WRITE));
  64. }
  65. int set_memory_rw(unsigned long addr, int numpages)
  66. {
  67. return change_memory_common(addr, numpages,
  68. __pgprot(PTE_WRITE),
  69. __pgprot(PTE_RDONLY));
  70. }
  71. int set_memory_nx(unsigned long addr, int numpages)
  72. {
  73. return change_memory_common(addr, numpages,
  74. __pgprot(PTE_PXN),
  75. __pgprot(0));
  76. }
  77. EXPORT_SYMBOL_GPL(set_memory_nx);
  78. int set_memory_x(unsigned long addr, int numpages)
  79. {
  80. return change_memory_common(addr, numpages,
  81. __pgprot(0),
  82. __pgprot(PTE_PXN));
  83. }
  84. EXPORT_SYMBOL_GPL(set_memory_x);