subpage-prot.c 5.3 KB

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