pgtable.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. table = NULL;
  108. }
  109. spin_unlock(&mm->page_table_lock);
  110. if (table)
  111. crst_table_free(mm, table);
  112. if (mm->context.asce_limit < limit)
  113. goto repeat;
  114. update_mm(mm, current);
  115. return 0;
  116. }
  117. void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
  118. {
  119. pgd_t *pgd;
  120. if (mm->context.asce_limit <= limit)
  121. return;
  122. __tlb_flush_mm(mm);
  123. while (mm->context.asce_limit > limit) {
  124. pgd = mm->pgd;
  125. switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
  126. case _REGION_ENTRY_TYPE_R2:
  127. mm->context.asce_limit = 1UL << 42;
  128. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  129. _ASCE_USER_BITS |
  130. _ASCE_TYPE_REGION3;
  131. break;
  132. case _REGION_ENTRY_TYPE_R3:
  133. mm->context.asce_limit = 1UL << 31;
  134. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  135. _ASCE_USER_BITS |
  136. _ASCE_TYPE_SEGMENT;
  137. break;
  138. default:
  139. BUG();
  140. }
  141. mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
  142. crst_table_free(mm, (unsigned long *) pgd);
  143. }
  144. update_mm(mm, current);
  145. }
  146. #endif
  147. /*
  148. * page table entry allocation/free routines.
  149. */
  150. unsigned long *page_table_alloc(struct mm_struct *mm)
  151. {
  152. struct page *page;
  153. unsigned long *table;
  154. unsigned long bits;
  155. bits = (mm->context.noexec || mm->context.pgstes) ? 3UL : 1UL;
  156. spin_lock(&mm->page_table_lock);
  157. page = NULL;
  158. if (!list_empty(&mm->context.pgtable_list)) {
  159. page = list_first_entry(&mm->context.pgtable_list,
  160. struct page, lru);
  161. if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1))
  162. page = NULL;
  163. }
  164. if (!page) {
  165. spin_unlock(&mm->page_table_lock);
  166. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  167. if (!page)
  168. return NULL;
  169. pgtable_page_ctor(page);
  170. page->flags &= ~FRAG_MASK;
  171. table = (unsigned long *) page_to_phys(page);
  172. if (mm->context.pgstes)
  173. clear_table_pgstes(table);
  174. else
  175. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE);
  176. spin_lock(&mm->page_table_lock);
  177. list_add(&page->lru, &mm->context.pgtable_list);
  178. }
  179. table = (unsigned long *) page_to_phys(page);
  180. while (page->flags & bits) {
  181. table += 256;
  182. bits <<= 1;
  183. }
  184. page->flags |= bits;
  185. if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1))
  186. list_move_tail(&page->lru, &mm->context.pgtable_list);
  187. spin_unlock(&mm->page_table_lock);
  188. return table;
  189. }
  190. void page_table_free(struct mm_struct *mm, unsigned long *table)
  191. {
  192. struct page *page;
  193. unsigned long bits;
  194. bits = (mm->context.noexec || mm->context.pgstes) ? 3UL : 1UL;
  195. bits <<= (__pa(table) & (PAGE_SIZE - 1)) / 256 / sizeof(unsigned long);
  196. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  197. spin_lock(&mm->page_table_lock);
  198. page->flags ^= bits;
  199. if (page->flags & FRAG_MASK) {
  200. /* Page now has some free pgtable fragments. */
  201. list_move(&page->lru, &mm->context.pgtable_list);
  202. page = NULL;
  203. } else
  204. /* All fragments of the 4K page have been freed. */
  205. list_del(&page->lru);
  206. spin_unlock(&mm->page_table_lock);
  207. if (page) {
  208. pgtable_page_dtor(page);
  209. __free_page(page);
  210. }
  211. }
  212. void disable_noexec(struct mm_struct *mm, struct task_struct *tsk)
  213. {
  214. struct page *page;
  215. spin_lock(&mm->page_table_lock);
  216. /* Free shadow region and segment tables. */
  217. list_for_each_entry(page, &mm->context.crst_list, lru)
  218. if (page->index) {
  219. free_pages((unsigned long) page->index, ALLOC_ORDER);
  220. page->index = 0;
  221. }
  222. /* "Free" second halves of page tables. */
  223. list_for_each_entry(page, &mm->context.pgtable_list, lru)
  224. page->flags &= ~SECOND_HALVES;
  225. spin_unlock(&mm->page_table_lock);
  226. mm->context.noexec = 0;
  227. update_mm(mm, tsk);
  228. }
  229. /*
  230. * switch on pgstes for its userspace process (for kvm)
  231. */
  232. int s390_enable_sie(void)
  233. {
  234. struct task_struct *tsk = current;
  235. struct mm_struct *mm, *old_mm;
  236. /* Do we have pgstes? if yes, we are done */
  237. if (tsk->mm->context.pgstes)
  238. return 0;
  239. /* lets check if we are allowed to replace the mm */
  240. task_lock(tsk);
  241. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  242. tsk->mm != tsk->active_mm || tsk->mm->ioctx_list) {
  243. task_unlock(tsk);
  244. return -EINVAL;
  245. }
  246. task_unlock(tsk);
  247. /* we copy the mm with pgstes enabled */
  248. tsk->mm->context.pgstes = 1;
  249. mm = dup_mm(tsk);
  250. tsk->mm->context.pgstes = 0;
  251. if (!mm)
  252. return -ENOMEM;
  253. /* Now lets check again if somebody attached ptrace etc */
  254. task_lock(tsk);
  255. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  256. tsk->mm != tsk->active_mm || tsk->mm->ioctx_list) {
  257. mmput(mm);
  258. task_unlock(tsk);
  259. return -EINVAL;
  260. }
  261. /* ok, we are alone. No ptrace, no threads, etc. */
  262. old_mm = tsk->mm;
  263. tsk->mm = tsk->active_mm = mm;
  264. preempt_disable();
  265. update_mm(mm, tsk);
  266. cpu_set(smp_processor_id(), mm->cpu_vm_mask);
  267. preempt_enable();
  268. task_unlock(tsk);
  269. mmput(old_mm);
  270. return 0;
  271. }
  272. EXPORT_SYMBOL_GPL(s390_enable_sie);