subpage-prot.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 2007-2008 Paul Mackerras, IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/gfp.h>
  12. #include <linux/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/mm.h>
  15. #include <linux/hugetlb.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/tlbflush.h>
  19. /*
  20. * Free all pages allocated for subpage protection maps and pointers.
  21. * Also makes sure that the subpage_prot_table structure is
  22. * reinitialized for the next user.
  23. */
  24. void subpage_prot_free(pgd_t *pgd)
  25. {
  26. struct subpage_prot_table *spt = pgd_subpage_prot(pgd);
  27. unsigned long i, j, addr;
  28. u32 **p;
  29. for (i = 0; i < 4; ++i) {
  30. if (spt->low_prot[i]) {
  31. free_page((unsigned long)spt->low_prot[i]);
  32. spt->low_prot[i] = NULL;
  33. }
  34. }
  35. addr = 0;
  36. for (i = 0; i < 2; ++i) {
  37. p = spt->protptrs[i];
  38. if (!p)
  39. continue;
  40. spt->protptrs[i] = NULL;
  41. for (j = 0; j < SBP_L2_COUNT && addr < spt->maxaddr;
  42. ++j, addr += PAGE_SIZE)
  43. if (p[j])
  44. free_page((unsigned long)p[j]);
  45. free_page((unsigned long)p);
  46. }
  47. spt->maxaddr = 0;
  48. }
  49. static void hpte_flush_range(struct mm_struct *mm, unsigned long addr,
  50. int npages)
  51. {
  52. pgd_t *pgd;
  53. pud_t *pud;
  54. pmd_t *pmd;
  55. pte_t *pte;
  56. spinlock_t *ptl;
  57. pgd = pgd_offset(mm, addr);
  58. if (pgd_none(*pgd))
  59. return;
  60. pud = pud_offset(pgd, addr);
  61. if (pud_none(*pud))
  62. return;
  63. pmd = pmd_offset(pud, addr);
  64. if (pmd_none(*pmd))
  65. return;
  66. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  67. arch_enter_lazy_mmu_mode();
  68. for (; npages > 0; --npages) {
  69. pte_update(mm, addr, pte, 0, 0);
  70. addr += PAGE_SIZE;
  71. ++pte;
  72. }
  73. arch_leave_lazy_mmu_mode();
  74. pte_unmap_unlock(pte - 1, ptl);
  75. }
  76. /*
  77. * Clear the subpage protection map for an address range, allowing
  78. * all accesses that are allowed by the pte permissions.
  79. */
  80. static void subpage_prot_clear(unsigned long addr, unsigned long len)
  81. {
  82. struct mm_struct *mm = current->mm;
  83. struct subpage_prot_table *spt = pgd_subpage_prot(mm->pgd);
  84. u32 **spm, *spp;
  85. int i, nw;
  86. unsigned long next, limit;
  87. down_write(&mm->mmap_sem);
  88. limit = addr + len;
  89. if (limit > spt->maxaddr)
  90. limit = spt->maxaddr;
  91. for (; addr < limit; addr = next) {
  92. next = pmd_addr_end(addr, limit);
  93. if (addr < 0x100000000) {
  94. spm = spt->low_prot;
  95. } else {
  96. spm = spt->protptrs[addr >> SBP_L3_SHIFT];
  97. if (!spm)
  98. continue;
  99. }
  100. spp = spm[(addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  101. if (!spp)
  102. continue;
  103. spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
  104. i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  105. nw = PTRS_PER_PTE - i;
  106. if (addr + (nw << PAGE_SHIFT) > next)
  107. nw = (next - addr) >> PAGE_SHIFT;
  108. memset(spp, 0, nw * sizeof(u32));
  109. /* now flush any existing HPTEs for the range */
  110. hpte_flush_range(mm, addr, nw);
  111. }
  112. up_write(&mm->mmap_sem);
  113. }
  114. /*
  115. * Copy in a subpage protection map for an address range.
  116. * The map has 2 bits per 4k subpage, so 32 bits per 64k page.
  117. * Each 2-bit field is 0 to allow any access, 1 to prevent writes,
  118. * 2 or 3 to prevent all accesses.
  119. * Note that the normal page protections also apply; the subpage
  120. * protection mechanism is an additional constraint, so putting 0
  121. * in a 2-bit field won't allow writes to a page that is otherwise
  122. * write-protected.
  123. */
  124. long sys_subpage_prot(unsigned long addr, unsigned long len, u32 __user *map)
  125. {
  126. struct mm_struct *mm = current->mm;
  127. struct subpage_prot_table *spt = pgd_subpage_prot(mm->pgd);
  128. u32 **spm, *spp;
  129. int i, nw;
  130. unsigned long next, limit;
  131. int err;
  132. /* Check parameters */
  133. if ((addr & ~PAGE_MASK) || (len & ~PAGE_MASK) ||
  134. addr >= TASK_SIZE || len >= TASK_SIZE || addr + len > TASK_SIZE)
  135. return -EINVAL;
  136. if (is_hugepage_only_range(mm, addr, len))
  137. return -EINVAL;
  138. if (!map) {
  139. /* Clear out the protection map for the address range */
  140. subpage_prot_clear(addr, len);
  141. return 0;
  142. }
  143. if (!access_ok(VERIFY_READ, map, (len >> PAGE_SHIFT) * sizeof(u32)))
  144. return -EFAULT;
  145. down_write(&mm->mmap_sem);
  146. for (limit = addr + len; addr < limit; addr = next) {
  147. next = pmd_addr_end(addr, limit);
  148. err = -ENOMEM;
  149. if (addr < 0x100000000) {
  150. spm = spt->low_prot;
  151. } else {
  152. spm = spt->protptrs[addr >> SBP_L3_SHIFT];
  153. if (!spm) {
  154. spm = (u32 **)get_zeroed_page(GFP_KERNEL);
  155. if (!spm)
  156. goto out;
  157. spt->protptrs[addr >> SBP_L3_SHIFT] = spm;
  158. }
  159. }
  160. spm += (addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1);
  161. spp = *spm;
  162. if (!spp) {
  163. spp = (u32 *)get_zeroed_page(GFP_KERNEL);
  164. if (!spp)
  165. goto out;
  166. *spm = spp;
  167. }
  168. spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
  169. local_irq_disable();
  170. demote_segment_4k(mm, addr);
  171. local_irq_enable();
  172. i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  173. nw = PTRS_PER_PTE - i;
  174. if (addr + (nw << PAGE_SHIFT) > next)
  175. nw = (next - addr) >> PAGE_SHIFT;
  176. up_write(&mm->mmap_sem);
  177. err = -EFAULT;
  178. if (__copy_from_user(spp, map, nw * sizeof(u32)))
  179. goto out2;
  180. map += nw;
  181. down_write(&mm->mmap_sem);
  182. /* now flush any existing HPTEs for the range */
  183. hpte_flush_range(mm, addr, nw);
  184. }
  185. if (limit > spt->maxaddr)
  186. spt->maxaddr = limit;
  187. err = 0;
  188. out:
  189. up_write(&mm->mmap_sem);
  190. out2:
  191. return err;
  192. }