pgtable.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * arch/s390/mm/pgtable.c
  3. *
  4. * Copyright IBM Corp. 2007
  5. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/kernel.h>
  9. #include <linux/errno.h>
  10. #include <linux/mm.h>
  11. #include <linux/swap.h>
  12. #include <linux/smp.h>
  13. #include <linux/highmem.h>
  14. #include <linux/slab.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/module.h>
  18. #include <linux/quicklist.h>
  19. #include <asm/system.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/pgalloc.h>
  22. #include <asm/tlb.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/mmu_context.h>
  25. #ifndef CONFIG_64BIT
  26. #define ALLOC_ORDER 1
  27. #define TABLES_PER_PAGE 4
  28. #define FRAG_MASK 15UL
  29. #define SECOND_HALVES 10UL
  30. #else
  31. #define ALLOC_ORDER 2
  32. #define TABLES_PER_PAGE 2
  33. #define FRAG_MASK 3UL
  34. #define SECOND_HALVES 2UL
  35. #endif
  36. unsigned long *crst_table_alloc(struct mm_struct *mm, int noexec)
  37. {
  38. struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  39. if (!page)
  40. return NULL;
  41. page->index = 0;
  42. if (noexec) {
  43. struct page *shadow = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  44. if (!shadow) {
  45. __free_pages(page, ALLOC_ORDER);
  46. return NULL;
  47. }
  48. page->index = page_to_phys(shadow);
  49. }
  50. spin_lock(&mm->page_table_lock);
  51. list_add(&page->lru, &mm->context.crst_list);
  52. spin_unlock(&mm->page_table_lock);
  53. return (unsigned long *) page_to_phys(page);
  54. }
  55. void crst_table_free(struct mm_struct *mm, unsigned long *table)
  56. {
  57. unsigned long *shadow = get_shadow_table(table);
  58. struct page *page = virt_to_page(table);
  59. spin_lock(&mm->page_table_lock);
  60. list_del(&page->lru);
  61. spin_unlock(&mm->page_table_lock);
  62. if (shadow)
  63. free_pages((unsigned long) shadow, ALLOC_ORDER);
  64. free_pages((unsigned long) table, ALLOC_ORDER);
  65. }
  66. #ifdef CONFIG_64BIT
  67. int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
  68. {
  69. unsigned long *table, *pgd;
  70. unsigned long entry;
  71. BUG_ON(limit > (1UL << 53));
  72. repeat:
  73. table = crst_table_alloc(mm, mm->context.noexec);
  74. if (!table)
  75. return -ENOMEM;
  76. spin_lock(&mm->page_table_lock);
  77. if (mm->context.asce_limit < limit) {
  78. pgd = (unsigned long *) mm->pgd;
  79. if (mm->context.asce_limit <= (1UL << 31)) {
  80. entry = _REGION3_ENTRY_EMPTY;
  81. mm->context.asce_limit = 1UL << 42;
  82. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  83. _ASCE_USER_BITS |
  84. _ASCE_TYPE_REGION3;
  85. } else {
  86. entry = _REGION2_ENTRY_EMPTY;
  87. mm->context.asce_limit = 1UL << 53;
  88. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  89. _ASCE_USER_BITS |
  90. _ASCE_TYPE_REGION2;
  91. }
  92. crst_table_init(table, entry);
  93. pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
  94. mm->pgd = (pgd_t *) table;
  95. table = NULL;
  96. }
  97. spin_unlock(&mm->page_table_lock);
  98. if (table)
  99. crst_table_free(mm, table);
  100. if (mm->context.asce_limit < limit)
  101. goto repeat;
  102. update_mm(mm, current);
  103. return 0;
  104. }
  105. void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
  106. {
  107. pgd_t *pgd;
  108. if (mm->context.asce_limit <= limit)
  109. return;
  110. __tlb_flush_mm(mm);
  111. while (mm->context.asce_limit > limit) {
  112. pgd = mm->pgd;
  113. switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
  114. case _REGION_ENTRY_TYPE_R2:
  115. mm->context.asce_limit = 1UL << 42;
  116. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  117. _ASCE_USER_BITS |
  118. _ASCE_TYPE_REGION3;
  119. break;
  120. case _REGION_ENTRY_TYPE_R3:
  121. mm->context.asce_limit = 1UL << 31;
  122. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  123. _ASCE_USER_BITS |
  124. _ASCE_TYPE_SEGMENT;
  125. break;
  126. default:
  127. BUG();
  128. }
  129. mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
  130. crst_table_free(mm, (unsigned long *) pgd);
  131. }
  132. update_mm(mm, current);
  133. }
  134. #endif
  135. /*
  136. * page table entry allocation/free routines.
  137. */
  138. unsigned long *page_table_alloc(struct mm_struct *mm)
  139. {
  140. struct page *page;
  141. unsigned long *table;
  142. unsigned long bits;
  143. bits = mm->context.noexec ? 3UL : 1UL;
  144. spin_lock(&mm->page_table_lock);
  145. page = NULL;
  146. if (!list_empty(&mm->context.pgtable_list)) {
  147. page = list_first_entry(&mm->context.pgtable_list,
  148. struct page, lru);
  149. if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1))
  150. page = NULL;
  151. }
  152. if (!page) {
  153. spin_unlock(&mm->page_table_lock);
  154. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  155. if (!page)
  156. return NULL;
  157. pgtable_page_ctor(page);
  158. page->flags &= ~FRAG_MASK;
  159. table = (unsigned long *) page_to_phys(page);
  160. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE);
  161. spin_lock(&mm->page_table_lock);
  162. list_add(&page->lru, &mm->context.pgtable_list);
  163. }
  164. table = (unsigned long *) page_to_phys(page);
  165. while (page->flags & bits) {
  166. table += 256;
  167. bits <<= 1;
  168. }
  169. page->flags |= bits;
  170. if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1))
  171. list_move_tail(&page->lru, &mm->context.pgtable_list);
  172. spin_unlock(&mm->page_table_lock);
  173. return table;
  174. }
  175. void page_table_free(struct mm_struct *mm, unsigned long *table)
  176. {
  177. struct page *page;
  178. unsigned long bits;
  179. bits = mm->context.noexec ? 3UL : 1UL;
  180. bits <<= (__pa(table) & (PAGE_SIZE - 1)) / 256 / sizeof(unsigned long);
  181. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  182. spin_lock(&mm->page_table_lock);
  183. page->flags ^= bits;
  184. if (page->flags & FRAG_MASK) {
  185. /* Page now has some free pgtable fragments. */
  186. list_move(&page->lru, &mm->context.pgtable_list);
  187. page = NULL;
  188. } else
  189. /* All fragments of the 4K page have been freed. */
  190. list_del(&page->lru);
  191. spin_unlock(&mm->page_table_lock);
  192. if (page) {
  193. pgtable_page_dtor(page);
  194. __free_page(page);
  195. }
  196. }
  197. void disable_noexec(struct mm_struct *mm, struct task_struct *tsk)
  198. {
  199. struct page *page;
  200. spin_lock(&mm->page_table_lock);
  201. /* Free shadow region and segment tables. */
  202. list_for_each_entry(page, &mm->context.crst_list, lru)
  203. if (page->index) {
  204. free_pages((unsigned long) page->index, ALLOC_ORDER);
  205. page->index = 0;
  206. }
  207. /* "Free" second halves of page tables. */
  208. list_for_each_entry(page, &mm->context.pgtable_list, lru)
  209. page->flags &= ~SECOND_HALVES;
  210. spin_unlock(&mm->page_table_lock);
  211. mm->context.noexec = 0;
  212. update_mm(mm, tsk);
  213. }