pageattr.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <linux/vmalloc.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/tlbflush.h>
  20. struct page_change_data {
  21. pgprot_t set_mask;
  22. pgprot_t clear_mask;
  23. };
  24. static int change_page_range(pte_t *ptep, pgtable_t token, unsigned long addr,
  25. void *data)
  26. {
  27. struct page_change_data *cdata = data;
  28. pte_t pte = *ptep;
  29. pte = clear_pte_bit(pte, cdata->clear_mask);
  30. pte = set_pte_bit(pte, cdata->set_mask);
  31. set_pte(ptep, pte);
  32. return 0;
  33. }
  34. static int change_memory_common(unsigned long addr, int numpages,
  35. pgprot_t set_mask, pgprot_t clear_mask)
  36. {
  37. unsigned long start = addr;
  38. unsigned long size = PAGE_SIZE*numpages;
  39. unsigned long end = start + size;
  40. int ret;
  41. struct page_change_data data;
  42. struct vm_struct *area;
  43. if (!PAGE_ALIGNED(addr)) {
  44. start &= PAGE_MASK;
  45. end = start + size;
  46. WARN_ON_ONCE(1);
  47. }
  48. /*
  49. * Kernel VA mappings are always live, and splitting live section
  50. * mappings into page mappings may cause TLB conflicts. This means
  51. * we have to ensure that changing the permission bits of the range
  52. * we are operating on does not result in such splitting.
  53. *
  54. * Let's restrict ourselves to mappings created by vmalloc (or vmap).
  55. * Those are guaranteed to consist entirely of page mappings, and
  56. * splitting is never needed.
  57. *
  58. * So check whether the [addr, addr + size) interval is entirely
  59. * covered by precisely one VM area that has the VM_ALLOC flag set.
  60. */
  61. area = find_vm_area((void *)addr);
  62. if (!area ||
  63. end > (unsigned long)area->addr + area->size ||
  64. !(area->flags & VM_ALLOC))
  65. return -EINVAL;
  66. if (!numpages)
  67. return 0;
  68. data.set_mask = set_mask;
  69. data.clear_mask = clear_mask;
  70. ret = apply_to_page_range(&init_mm, start, size, change_page_range,
  71. &data);
  72. flush_tlb_kernel_range(start, end);
  73. return ret;
  74. }
  75. int set_memory_ro(unsigned long addr, int numpages)
  76. {
  77. return change_memory_common(addr, numpages,
  78. __pgprot(PTE_RDONLY),
  79. __pgprot(PTE_WRITE));
  80. }
  81. int set_memory_rw(unsigned long addr, int numpages)
  82. {
  83. return change_memory_common(addr, numpages,
  84. __pgprot(PTE_WRITE),
  85. __pgprot(PTE_RDONLY));
  86. }
  87. int set_memory_nx(unsigned long addr, int numpages)
  88. {
  89. return change_memory_common(addr, numpages,
  90. __pgprot(PTE_PXN),
  91. __pgprot(0));
  92. }
  93. EXPORT_SYMBOL_GPL(set_memory_nx);
  94. int set_memory_x(unsigned long addr, int numpages)
  95. {
  96. return change_memory_common(addr, numpages,
  97. __pgprot(0),
  98. __pgprot(PTE_PXN));
  99. }
  100. EXPORT_SYMBOL_GPL(set_memory_x);