pgtable.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright IBM Corp. 2007,2009
  3. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/kernel.h>
  7. #include <linux/errno.h>
  8. #include <linux/mm.h>
  9. #include <linux/swap.h>
  10. #include <linux/smp.h>
  11. #include <linux/highmem.h>
  12. #include <linux/slab.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/module.h>
  16. #include <linux/quicklist.h>
  17. #include <asm/system.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/tlb.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/mmu_context.h>
  23. #ifndef CONFIG_64BIT
  24. #define ALLOC_ORDER 1
  25. #define TABLES_PER_PAGE 4
  26. #define FRAG_MASK 15UL
  27. #define SECOND_HALVES 10UL
  28. void clear_table_pgstes(unsigned long *table)
  29. {
  30. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/4);
  31. memset(table + 256, 0, PAGE_SIZE/4);
  32. clear_table(table + 512, _PAGE_TYPE_EMPTY, PAGE_SIZE/4);
  33. memset(table + 768, 0, PAGE_SIZE/4);
  34. }
  35. #else
  36. #define ALLOC_ORDER 2
  37. #define TABLES_PER_PAGE 2
  38. #define FRAG_MASK 3UL
  39. #define SECOND_HALVES 2UL
  40. void clear_table_pgstes(unsigned long *table)
  41. {
  42. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/2);
  43. memset(table + 256, 0, PAGE_SIZE/2);
  44. }
  45. #endif
  46. unsigned long VMALLOC_START = VMALLOC_END - VMALLOC_SIZE;
  47. EXPORT_SYMBOL(VMALLOC_START);
  48. static int __init parse_vmalloc(char *arg)
  49. {
  50. if (!arg)
  51. return -EINVAL;
  52. VMALLOC_START = (VMALLOC_END - memparse(arg, &arg)) & PAGE_MASK;
  53. return 0;
  54. }
  55. early_param("vmalloc", parse_vmalloc);
  56. unsigned long *crst_table_alloc(struct mm_struct *mm, int noexec)
  57. {
  58. struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  59. if (!page)
  60. return NULL;
  61. page->index = 0;
  62. if (noexec) {
  63. struct page *shadow = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  64. if (!shadow) {
  65. __free_pages(page, ALLOC_ORDER);
  66. return NULL;
  67. }
  68. page->index = page_to_phys(shadow);
  69. }
  70. spin_lock(&mm->context.list_lock);
  71. list_add(&page->lru, &mm->context.crst_list);
  72. spin_unlock(&mm->context.list_lock);
  73. return (unsigned long *) page_to_phys(page);
  74. }
  75. void crst_table_free(struct mm_struct *mm, unsigned long *table)
  76. {
  77. unsigned long *shadow = get_shadow_table(table);
  78. struct page *page = virt_to_page(table);
  79. spin_lock(&mm->context.list_lock);
  80. list_del(&page->lru);
  81. spin_unlock(&mm->context.list_lock);
  82. if (shadow)
  83. free_pages((unsigned long) shadow, ALLOC_ORDER);
  84. free_pages((unsigned long) table, ALLOC_ORDER);
  85. }
  86. #ifdef CONFIG_64BIT
  87. int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
  88. {
  89. unsigned long *table, *pgd;
  90. unsigned long entry;
  91. BUG_ON(limit > (1UL << 53));
  92. repeat:
  93. table = crst_table_alloc(mm, mm->context.noexec);
  94. if (!table)
  95. return -ENOMEM;
  96. spin_lock(&mm->page_table_lock);
  97. if (mm->context.asce_limit < limit) {
  98. pgd = (unsigned long *) mm->pgd;
  99. if (mm->context.asce_limit <= (1UL << 31)) {
  100. entry = _REGION3_ENTRY_EMPTY;
  101. mm->context.asce_limit = 1UL << 42;
  102. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  103. _ASCE_USER_BITS |
  104. _ASCE_TYPE_REGION3;
  105. } else {
  106. entry = _REGION2_ENTRY_EMPTY;
  107. mm->context.asce_limit = 1UL << 53;
  108. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  109. _ASCE_USER_BITS |
  110. _ASCE_TYPE_REGION2;
  111. }
  112. crst_table_init(table, entry);
  113. pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
  114. mm->pgd = (pgd_t *) table;
  115. mm->task_size = mm->context.asce_limit;
  116. table = NULL;
  117. }
  118. spin_unlock(&mm->page_table_lock);
  119. if (table)
  120. crst_table_free(mm, table);
  121. if (mm->context.asce_limit < limit)
  122. goto repeat;
  123. update_mm(mm, current);
  124. return 0;
  125. }
  126. void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
  127. {
  128. pgd_t *pgd;
  129. if (mm->context.asce_limit <= limit)
  130. return;
  131. __tlb_flush_mm(mm);
  132. while (mm->context.asce_limit > limit) {
  133. pgd = mm->pgd;
  134. switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
  135. case _REGION_ENTRY_TYPE_R2:
  136. mm->context.asce_limit = 1UL << 42;
  137. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  138. _ASCE_USER_BITS |
  139. _ASCE_TYPE_REGION3;
  140. break;
  141. case _REGION_ENTRY_TYPE_R3:
  142. mm->context.asce_limit = 1UL << 31;
  143. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  144. _ASCE_USER_BITS |
  145. _ASCE_TYPE_SEGMENT;
  146. break;
  147. default:
  148. BUG();
  149. }
  150. mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
  151. mm->task_size = mm->context.asce_limit;
  152. crst_table_free(mm, (unsigned long *) pgd);
  153. }
  154. update_mm(mm, current);
  155. }
  156. #endif
  157. /*
  158. * page table entry allocation/free routines.
  159. */
  160. unsigned long *page_table_alloc(struct mm_struct *mm)
  161. {
  162. struct page *page;
  163. unsigned long *table;
  164. unsigned long bits;
  165. bits = (mm->context.noexec || mm->context.has_pgste) ? 3UL : 1UL;
  166. spin_lock(&mm->context.list_lock);
  167. page = NULL;
  168. if (!list_empty(&mm->context.pgtable_list)) {
  169. page = list_first_entry(&mm->context.pgtable_list,
  170. struct page, lru);
  171. if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1))
  172. page = NULL;
  173. }
  174. if (!page) {
  175. spin_unlock(&mm->context.list_lock);
  176. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  177. if (!page)
  178. return NULL;
  179. pgtable_page_ctor(page);
  180. page->flags &= ~FRAG_MASK;
  181. table = (unsigned long *) page_to_phys(page);
  182. if (mm->context.has_pgste)
  183. clear_table_pgstes(table);
  184. else
  185. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE);
  186. spin_lock(&mm->context.list_lock);
  187. list_add(&page->lru, &mm->context.pgtable_list);
  188. }
  189. table = (unsigned long *) page_to_phys(page);
  190. while (page->flags & bits) {
  191. table += 256;
  192. bits <<= 1;
  193. }
  194. page->flags |= bits;
  195. if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1))
  196. list_move_tail(&page->lru, &mm->context.pgtable_list);
  197. spin_unlock(&mm->context.list_lock);
  198. return table;
  199. }
  200. void page_table_free(struct mm_struct *mm, unsigned long *table)
  201. {
  202. struct page *page;
  203. unsigned long bits;
  204. bits = (mm->context.noexec || mm->context.has_pgste) ? 3UL : 1UL;
  205. bits <<= (__pa(table) & (PAGE_SIZE - 1)) / 256 / sizeof(unsigned long);
  206. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  207. spin_lock(&mm->context.list_lock);
  208. page->flags ^= bits;
  209. if (page->flags & FRAG_MASK) {
  210. /* Page now has some free pgtable fragments. */
  211. list_move(&page->lru, &mm->context.pgtable_list);
  212. page = NULL;
  213. } else
  214. /* All fragments of the 4K page have been freed. */
  215. list_del(&page->lru);
  216. spin_unlock(&mm->context.list_lock);
  217. if (page) {
  218. pgtable_page_dtor(page);
  219. __free_page(page);
  220. }
  221. }
  222. void disable_noexec(struct mm_struct *mm, struct task_struct *tsk)
  223. {
  224. struct page *page;
  225. spin_lock(&mm->context.list_lock);
  226. /* Free shadow region and segment tables. */
  227. list_for_each_entry(page, &mm->context.crst_list, lru)
  228. if (page->index) {
  229. free_pages((unsigned long) page->index, ALLOC_ORDER);
  230. page->index = 0;
  231. }
  232. /* "Free" second halves of page tables. */
  233. list_for_each_entry(page, &mm->context.pgtable_list, lru)
  234. page->flags &= ~SECOND_HALVES;
  235. spin_unlock(&mm->context.list_lock);
  236. mm->context.noexec = 0;
  237. update_mm(mm, tsk);
  238. }
  239. /*
  240. * switch on pgstes for its userspace process (for kvm)
  241. */
  242. int s390_enable_sie(void)
  243. {
  244. struct task_struct *tsk = current;
  245. struct mm_struct *mm, *old_mm;
  246. /* Do we have switched amode? If no, we cannot do sie */
  247. if (!switch_amode)
  248. return -EINVAL;
  249. /* Do we have pgstes? if yes, we are done */
  250. if (tsk->mm->context.has_pgste)
  251. return 0;
  252. /* lets check if we are allowed to replace the mm */
  253. task_lock(tsk);
  254. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  255. #ifdef CONFIG_AIO
  256. !hlist_empty(&tsk->mm->ioctx_list) ||
  257. #endif
  258. tsk->mm != tsk->active_mm) {
  259. task_unlock(tsk);
  260. return -EINVAL;
  261. }
  262. task_unlock(tsk);
  263. /* we copy the mm and let dup_mm create the page tables with_pgstes */
  264. tsk->mm->context.alloc_pgste = 1;
  265. mm = dup_mm(tsk);
  266. tsk->mm->context.alloc_pgste = 0;
  267. if (!mm)
  268. return -ENOMEM;
  269. /* Now lets check again if something happened */
  270. task_lock(tsk);
  271. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  272. #ifdef CONFIG_AIO
  273. !hlist_empty(&tsk->mm->ioctx_list) ||
  274. #endif
  275. tsk->mm != tsk->active_mm) {
  276. mmput(mm);
  277. task_unlock(tsk);
  278. return -EINVAL;
  279. }
  280. /* ok, we are alone. No ptrace, no threads, etc. */
  281. old_mm = tsk->mm;
  282. tsk->mm = tsk->active_mm = mm;
  283. preempt_disable();
  284. update_mm(mm, tsk);
  285. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  286. preempt_enable();
  287. task_unlock(tsk);
  288. mmput(old_mm);
  289. return 0;
  290. }
  291. EXPORT_SYMBOL_GPL(s390_enable_sie);
  292. #if defined(CONFIG_DEBUG_PAGEALLOC) && defined(CONFIG_HIBERNATION)
  293. bool kernel_page_present(struct page *page)
  294. {
  295. unsigned long addr;
  296. int cc;
  297. addr = page_to_phys(page);
  298. asm volatile(
  299. " lra %1,0(%1)\n"
  300. " ipm %0\n"
  301. " srl %0,28"
  302. : "=d" (cc), "+a" (addr) : : "cc");
  303. return cc == 0;
  304. }
  305. #endif /* CONFIG_HIBERNATION && CONFIG_DEBUG_PAGEALLOC */