pgtable.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. void clear_table_pgstes(unsigned long *table)
  31. {
  32. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/4);
  33. memset(table + 256, 0, PAGE_SIZE/4);
  34. clear_table(table + 512, _PAGE_TYPE_EMPTY, PAGE_SIZE/4);
  35. memset(table + 768, 0, PAGE_SIZE/4);
  36. }
  37. #else
  38. #define ALLOC_ORDER 2
  39. #define TABLES_PER_PAGE 2
  40. #define FRAG_MASK 3UL
  41. #define SECOND_HALVES 2UL
  42. void clear_table_pgstes(unsigned long *table)
  43. {
  44. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/2);
  45. memset(table + 256, 0, PAGE_SIZE/2);
  46. }
  47. #endif
  48. unsigned long *crst_table_alloc(struct mm_struct *mm, int noexec)
  49. {
  50. struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  51. if (!page)
  52. return NULL;
  53. page->index = 0;
  54. if (noexec) {
  55. struct page *shadow = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  56. if (!shadow) {
  57. __free_pages(page, ALLOC_ORDER);
  58. return NULL;
  59. }
  60. page->index = page_to_phys(shadow);
  61. }
  62. spin_lock(&mm->page_table_lock);
  63. list_add(&page->lru, &mm->context.crst_list);
  64. spin_unlock(&mm->page_table_lock);
  65. return (unsigned long *) page_to_phys(page);
  66. }
  67. void crst_table_free(struct mm_struct *mm, unsigned long *table)
  68. {
  69. unsigned long *shadow = get_shadow_table(table);
  70. struct page *page = virt_to_page(table);
  71. spin_lock(&mm->page_table_lock);
  72. list_del(&page->lru);
  73. spin_unlock(&mm->page_table_lock);
  74. if (shadow)
  75. free_pages((unsigned long) shadow, ALLOC_ORDER);
  76. free_pages((unsigned long) table, ALLOC_ORDER);
  77. }
  78. #ifdef CONFIG_64BIT
  79. int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
  80. {
  81. unsigned long *table, *pgd;
  82. unsigned long entry;
  83. BUG_ON(limit > (1UL << 53));
  84. repeat:
  85. table = crst_table_alloc(mm, mm->context.noexec);
  86. if (!table)
  87. return -ENOMEM;
  88. spin_lock(&mm->page_table_lock);
  89. if (mm->context.asce_limit < limit) {
  90. pgd = (unsigned long *) mm->pgd;
  91. if (mm->context.asce_limit <= (1UL << 31)) {
  92. entry = _REGION3_ENTRY_EMPTY;
  93. mm->context.asce_limit = 1UL << 42;
  94. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  95. _ASCE_USER_BITS |
  96. _ASCE_TYPE_REGION3;
  97. } else {
  98. entry = _REGION2_ENTRY_EMPTY;
  99. mm->context.asce_limit = 1UL << 53;
  100. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  101. _ASCE_USER_BITS |
  102. _ASCE_TYPE_REGION2;
  103. }
  104. crst_table_init(table, entry);
  105. pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
  106. mm->pgd = (pgd_t *) table;
  107. mm->task_size = mm->context.asce_limit;
  108. table = NULL;
  109. }
  110. spin_unlock(&mm->page_table_lock);
  111. if (table)
  112. crst_table_free(mm, table);
  113. if (mm->context.asce_limit < limit)
  114. goto repeat;
  115. update_mm(mm, current);
  116. return 0;
  117. }
  118. void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
  119. {
  120. pgd_t *pgd;
  121. if (mm->context.asce_limit <= limit)
  122. return;
  123. __tlb_flush_mm(mm);
  124. while (mm->context.asce_limit > limit) {
  125. pgd = mm->pgd;
  126. switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
  127. case _REGION_ENTRY_TYPE_R2:
  128. mm->context.asce_limit = 1UL << 42;
  129. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  130. _ASCE_USER_BITS |
  131. _ASCE_TYPE_REGION3;
  132. break;
  133. case _REGION_ENTRY_TYPE_R3:
  134. mm->context.asce_limit = 1UL << 31;
  135. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  136. _ASCE_USER_BITS |
  137. _ASCE_TYPE_SEGMENT;
  138. break;
  139. default:
  140. BUG();
  141. }
  142. mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
  143. mm->task_size = mm->context.asce_limit;
  144. crst_table_free(mm, (unsigned long *) pgd);
  145. }
  146. update_mm(mm, current);
  147. }
  148. #endif
  149. /*
  150. * page table entry allocation/free routines.
  151. */
  152. unsigned long *page_table_alloc(struct mm_struct *mm)
  153. {
  154. struct page *page;
  155. unsigned long *table;
  156. unsigned long bits;
  157. bits = (mm->context.noexec || mm->context.has_pgste) ? 3UL : 1UL;
  158. spin_lock(&mm->page_table_lock);
  159. page = NULL;
  160. if (!list_empty(&mm->context.pgtable_list)) {
  161. page = list_first_entry(&mm->context.pgtable_list,
  162. struct page, lru);
  163. if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1))
  164. page = NULL;
  165. }
  166. if (!page) {
  167. spin_unlock(&mm->page_table_lock);
  168. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  169. if (!page)
  170. return NULL;
  171. pgtable_page_ctor(page);
  172. page->flags &= ~FRAG_MASK;
  173. table = (unsigned long *) page_to_phys(page);
  174. if (mm->context.has_pgste)
  175. clear_table_pgstes(table);
  176. else
  177. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE);
  178. spin_lock(&mm->page_table_lock);
  179. list_add(&page->lru, &mm->context.pgtable_list);
  180. }
  181. table = (unsigned long *) page_to_phys(page);
  182. while (page->flags & bits) {
  183. table += 256;
  184. bits <<= 1;
  185. }
  186. page->flags |= bits;
  187. if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1))
  188. list_move_tail(&page->lru, &mm->context.pgtable_list);
  189. spin_unlock(&mm->page_table_lock);
  190. return table;
  191. }
  192. void page_table_free(struct mm_struct *mm, unsigned long *table)
  193. {
  194. struct page *page;
  195. unsigned long bits;
  196. bits = (mm->context.noexec || mm->context.has_pgste) ? 3UL : 1UL;
  197. bits <<= (__pa(table) & (PAGE_SIZE - 1)) / 256 / sizeof(unsigned long);
  198. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  199. spin_lock(&mm->page_table_lock);
  200. page->flags ^= bits;
  201. if (page->flags & FRAG_MASK) {
  202. /* Page now has some free pgtable fragments. */
  203. list_move(&page->lru, &mm->context.pgtable_list);
  204. page = NULL;
  205. } else
  206. /* All fragments of the 4K page have been freed. */
  207. list_del(&page->lru);
  208. spin_unlock(&mm->page_table_lock);
  209. if (page) {
  210. pgtable_page_dtor(page);
  211. __free_page(page);
  212. }
  213. }
  214. void disable_noexec(struct mm_struct *mm, struct task_struct *tsk)
  215. {
  216. struct page *page;
  217. spin_lock(&mm->page_table_lock);
  218. /* Free shadow region and segment tables. */
  219. list_for_each_entry(page, &mm->context.crst_list, lru)
  220. if (page->index) {
  221. free_pages((unsigned long) page->index, ALLOC_ORDER);
  222. page->index = 0;
  223. }
  224. /* "Free" second halves of page tables. */
  225. list_for_each_entry(page, &mm->context.pgtable_list, lru)
  226. page->flags &= ~SECOND_HALVES;
  227. spin_unlock(&mm->page_table_lock);
  228. mm->context.noexec = 0;
  229. update_mm(mm, tsk);
  230. }
  231. /*
  232. * switch on pgstes for its userspace process (for kvm)
  233. */
  234. int s390_enable_sie(void)
  235. {
  236. struct task_struct *tsk = current;
  237. struct mm_struct *mm, *old_mm;
  238. /* Do we have pgstes? if yes, we are done */
  239. if (tsk->mm->context.has_pgste)
  240. return 0;
  241. /* lets check if we are allowed to replace the mm */
  242. task_lock(tsk);
  243. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  244. tsk->mm != tsk->active_mm || !hlist_empty(&tsk->mm->ioctx_list)) {
  245. task_unlock(tsk);
  246. return -EINVAL;
  247. }
  248. task_unlock(tsk);
  249. /* we copy the mm and let dup_mm create the page tables with_pgstes */
  250. tsk->mm->context.alloc_pgste = 1;
  251. mm = dup_mm(tsk);
  252. tsk->mm->context.alloc_pgste = 0;
  253. if (!mm)
  254. return -ENOMEM;
  255. /* Now lets check again if something happened */
  256. task_lock(tsk);
  257. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  258. tsk->mm != tsk->active_mm || !hlist_empty(&tsk->mm->ioctx_list)) {
  259. mmput(mm);
  260. task_unlock(tsk);
  261. return -EINVAL;
  262. }
  263. /* ok, we are alone. No ptrace, no threads, etc. */
  264. old_mm = tsk->mm;
  265. tsk->mm = tsk->active_mm = mm;
  266. preempt_disable();
  267. update_mm(mm, tsk);
  268. cpu_set(smp_processor_id(), mm->cpu_vm_mask);
  269. preempt_enable();
  270. task_unlock(tsk);
  271. mmput(old_mm);
  272. return 0;
  273. }
  274. EXPORT_SYMBOL_GPL(s390_enable_sie);