2 * Copyright IBM Corp. 2007,2009
3 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
6 #include <linux/sched.h>
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
10 #include <linux/swap.h>
11 #include <linux/smp.h>
12 #include <linux/highmem.h>
13 #include <linux/slab.h>
14 #include <linux/pagemap.h>
15 #include <linux/spinlock.h>
16 #include <linux/module.h>
17 #include <linux/quicklist.h>
19 #include <asm/system.h>
20 #include <asm/pgtable.h>
21 #include <asm/pgalloc.h>
23 #include <asm/tlbflush.h>
24 #include <asm/mmu_context.h>
28 #define TABLES_PER_PAGE 4
29 #define FRAG_MASK 15UL
30 #define SECOND_HALVES 10UL
32 void clear_table_pgstes(unsigned long *table
)
34 clear_table(table
, _PAGE_TYPE_EMPTY
, PAGE_SIZE
/4);
35 memset(table
+ 256, 0, PAGE_SIZE
/4);
36 clear_table(table
+ 512, _PAGE_TYPE_EMPTY
, PAGE_SIZE
/4);
37 memset(table
+ 768, 0, PAGE_SIZE
/4);
42 #define TABLES_PER_PAGE 2
44 #define SECOND_HALVES 2UL
46 void clear_table_pgstes(unsigned long *table
)
48 clear_table(table
, _PAGE_TYPE_EMPTY
, PAGE_SIZE
/2);
49 memset(table
+ 256, 0, PAGE_SIZE
/2);
54 unsigned long VMALLOC_START
= VMALLOC_END
- VMALLOC_SIZE
;
55 EXPORT_SYMBOL(VMALLOC_START
);
57 static int __init
parse_vmalloc(char *arg
)
61 VMALLOC_START
= (VMALLOC_END
- memparse(arg
, &arg
)) & PAGE_MASK
;
64 early_param("vmalloc", parse_vmalloc
);
66 unsigned long *crst_table_alloc(struct mm_struct
*mm
, int noexec
)
68 struct page
*page
= alloc_pages(GFP_KERNEL
, ALLOC_ORDER
);
74 struct page
*shadow
= alloc_pages(GFP_KERNEL
, ALLOC_ORDER
);
76 __free_pages(page
, ALLOC_ORDER
);
79 page
->index
= page_to_phys(shadow
);
81 spin_lock(&mm
->page_table_lock
);
82 list_add(&page
->lru
, &mm
->context
.crst_list
);
83 spin_unlock(&mm
->page_table_lock
);
84 return (unsigned long *) page_to_phys(page
);
87 void crst_table_free(struct mm_struct
*mm
, unsigned long *table
)
89 unsigned long *shadow
= get_shadow_table(table
);
90 struct page
*page
= virt_to_page(table
);
92 spin_lock(&mm
->page_table_lock
);
94 spin_unlock(&mm
->page_table_lock
);
96 free_pages((unsigned long) shadow
, ALLOC_ORDER
);
97 free_pages((unsigned long) table
, ALLOC_ORDER
);
101 int crst_table_upgrade(struct mm_struct
*mm
, unsigned long limit
)
103 unsigned long *table
, *pgd
;
106 BUG_ON(limit
> (1UL << 53));
108 table
= crst_table_alloc(mm
, mm
->context
.noexec
);
111 spin_lock(&mm
->page_table_lock
);
112 if (mm
->context
.asce_limit
< limit
) {
113 pgd
= (unsigned long *) mm
->pgd
;
114 if (mm
->context
.asce_limit
<= (1UL << 31)) {
115 entry
= _REGION3_ENTRY_EMPTY
;
116 mm
->context
.asce_limit
= 1UL << 42;
117 mm
->context
.asce_bits
= _ASCE_TABLE_LENGTH
|
121 entry
= _REGION2_ENTRY_EMPTY
;
122 mm
->context
.asce_limit
= 1UL << 53;
123 mm
->context
.asce_bits
= _ASCE_TABLE_LENGTH
|
127 crst_table_init(table
, entry
);
128 pgd_populate(mm
, (pgd_t
*) table
, (pud_t
*) pgd
);
129 mm
->pgd
= (pgd_t
*) table
;
130 mm
->task_size
= mm
->context
.asce_limit
;
133 spin_unlock(&mm
->page_table_lock
);
135 crst_table_free(mm
, table
);
136 if (mm
->context
.asce_limit
< limit
)
138 update_mm(mm
, current
);
142 void crst_table_downgrade(struct mm_struct
*mm
, unsigned long limit
)
146 if (mm
->context
.asce_limit
<= limit
)
149 while (mm
->context
.asce_limit
> limit
) {
151 switch (pgd_val(*pgd
) & _REGION_ENTRY_TYPE_MASK
) {
152 case _REGION_ENTRY_TYPE_R2
:
153 mm
->context
.asce_limit
= 1UL << 42;
154 mm
->context
.asce_bits
= _ASCE_TABLE_LENGTH
|
158 case _REGION_ENTRY_TYPE_R3
:
159 mm
->context
.asce_limit
= 1UL << 31;
160 mm
->context
.asce_bits
= _ASCE_TABLE_LENGTH
|
167 mm
->pgd
= (pgd_t
*) (pgd_val(*pgd
) & _REGION_ENTRY_ORIGIN
);
168 mm
->task_size
= mm
->context
.asce_limit
;
169 crst_table_free(mm
, (unsigned long *) pgd
);
171 update_mm(mm
, current
);
176 * page table entry allocation/free routines.
178 unsigned long *page_table_alloc(struct mm_struct
*mm
)
181 unsigned long *table
;
184 bits
= (mm
->context
.noexec
|| mm
->context
.has_pgste
) ? 3UL : 1UL;
185 spin_lock(&mm
->page_table_lock
);
187 if (!list_empty(&mm
->context
.pgtable_list
)) {
188 page
= list_first_entry(&mm
->context
.pgtable_list
,
190 if ((page
->flags
& FRAG_MASK
) == ((1UL << TABLES_PER_PAGE
) - 1))
194 spin_unlock(&mm
->page_table_lock
);
195 page
= alloc_page(GFP_KERNEL
|__GFP_REPEAT
);
198 pgtable_page_ctor(page
);
199 page
->flags
&= ~FRAG_MASK
;
200 table
= (unsigned long *) page_to_phys(page
);
201 if (mm
->context
.has_pgste
)
202 clear_table_pgstes(table
);
204 clear_table(table
, _PAGE_TYPE_EMPTY
, PAGE_SIZE
);
205 spin_lock(&mm
->page_table_lock
);
206 list_add(&page
->lru
, &mm
->context
.pgtable_list
);
208 table
= (unsigned long *) page_to_phys(page
);
209 while (page
->flags
& bits
) {
214 if ((page
->flags
& FRAG_MASK
) == ((1UL << TABLES_PER_PAGE
) - 1))
215 list_move_tail(&page
->lru
, &mm
->context
.pgtable_list
);
216 spin_unlock(&mm
->page_table_lock
);
220 void page_table_free(struct mm_struct
*mm
, unsigned long *table
)
225 bits
= (mm
->context
.noexec
|| mm
->context
.has_pgste
) ? 3UL : 1UL;
226 bits
<<= (__pa(table
) & (PAGE_SIZE
- 1)) / 256 / sizeof(unsigned long);
227 page
= pfn_to_page(__pa(table
) >> PAGE_SHIFT
);
228 spin_lock(&mm
->page_table_lock
);
230 if (page
->flags
& FRAG_MASK
) {
231 /* Page now has some free pgtable fragments. */
232 list_move(&page
->lru
, &mm
->context
.pgtable_list
);
235 /* All fragments of the 4K page have been freed. */
236 list_del(&page
->lru
);
237 spin_unlock(&mm
->page_table_lock
);
239 pgtable_page_dtor(page
);
244 void disable_noexec(struct mm_struct
*mm
, struct task_struct
*tsk
)
248 spin_lock(&mm
->page_table_lock
);
249 /* Free shadow region and segment tables. */
250 list_for_each_entry(page
, &mm
->context
.crst_list
, lru
)
252 free_pages((unsigned long) page
->index
, ALLOC_ORDER
);
255 /* "Free" second halves of page tables. */
256 list_for_each_entry(page
, &mm
->context
.pgtable_list
, lru
)
257 page
->flags
&= ~SECOND_HALVES
;
258 spin_unlock(&mm
->page_table_lock
);
259 mm
->context
.noexec
= 0;
264 * switch on pgstes for its userspace process (for kvm)
266 int s390_enable_sie(void)
268 struct task_struct
*tsk
= current
;
269 struct mm_struct
*mm
, *old_mm
;
271 /* Do we have switched amode? If no, we cannot do sie */
275 /* Do we have pgstes? if yes, we are done */
276 if (tsk
->mm
->context
.has_pgste
)
279 /* lets check if we are allowed to replace the mm */
281 if (!tsk
->mm
|| atomic_read(&tsk
->mm
->mm_users
) > 1 ||
282 tsk
->mm
!= tsk
->active_mm
|| !hlist_empty(&tsk
->mm
->ioctx_list
)) {
288 /* we copy the mm and let dup_mm create the page tables with_pgstes */
289 tsk
->mm
->context
.alloc_pgste
= 1;
291 tsk
->mm
->context
.alloc_pgste
= 0;
295 /* Now lets check again if something happened */
297 if (!tsk
->mm
|| atomic_read(&tsk
->mm
->mm_users
) > 1 ||
298 tsk
->mm
!= tsk
->active_mm
|| !hlist_empty(&tsk
->mm
->ioctx_list
)) {
304 /* ok, we are alone. No ptrace, no threads, etc. */
306 tsk
->mm
= tsk
->active_mm
= mm
;
309 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm
));
315 EXPORT_SYMBOL_GPL(s390_enable_sie
);
317 #ifdef CONFIG_DEBUG_PAGEALLOC
318 #ifdef CONFIG_HIBERNATION
319 bool kernel_page_present(struct page
*page
)
324 addr
= page_to_phys(page
);
328 :"=d"(cc
),"+a"(addr
)::"cc");
332 #endif /* CONFIG_HIBERNATION */
333 #endif /* CONFIG_DEBUG_PAGEALLOC */