]>
Commit | Line | Data |
---|---|---|
ba180fd4 JD |
1 | /* |
2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) | |
1da177e4 LT |
3 | * Licensed under the GPL |
4 | */ | |
5 | ||
d67b569f | 6 | #include "linux/mm.h" |
ba180fd4 | 7 | #include "linux/sched.h" |
d67b569f JD |
8 | #include "asm/pgalloc.h" |
9 | #include "asm/pgtable.h" | |
1da177e4 LT |
10 | #include "os.h" |
11 | #include "skas.h" | |
12 | ||
d67b569f JD |
13 | extern int __syscall_stub_start; |
14 | ||
15 | static int init_stub_pte(struct mm_struct *mm, unsigned long proc, | |
16 | unsigned long kernel) | |
17 | { | |
18 | pgd_t *pgd; | |
19 | pud_t *pud; | |
20 | pmd_t *pmd; | |
21 | pte_t *pte; | |
22 | ||
d67b569f JD |
23 | pgd = pgd_offset(mm, proc); |
24 | pud = pud_alloc(mm, pgd, proc); | |
25 | if (!pud) | |
26 | goto out; | |
27 | ||
28 | pmd = pmd_alloc(mm, pud, proc); | |
29 | if (!pmd) | |
30 | goto out_pmd; | |
31 | ||
32 | pte = pte_alloc_map(mm, pmd, proc); | |
33 | if (!pte) | |
34 | goto out_pte; | |
35 | ||
ba180fd4 JD |
36 | /* |
37 | * There's an interaction between the skas0 stub pages, stack | |
d67b569f | 38 | * randomization, and the BUG at the end of exit_mmap. exit_mmap |
ba180fd4 JD |
39 | * checks that the number of page tables freed is the same as had |
40 | * been allocated. If the stack is on the last page table page, | |
d67b569f JD |
41 | * then the stack pte page will be freed, and if not, it won't. To |
42 | * avoid having to know where the stack is, or if the process mapped | |
43 | * something at the top of its address space for some other reason, | |
44 | * we set TASK_SIZE to end at the start of the last page table. | |
45 | * This keeps exit_mmap off the last page, but introduces a leak | |
46 | * of that page. So, we hang onto it here and free it in | |
47 | * destroy_context_skas. | |
48 | */ | |
49 | ||
ba180fd4 | 50 | mm->context.skas.last_page_table = pmd_page_vaddr(*pmd); |
7ef93905 | 51 | #ifdef CONFIG_3_LEVEL_PGTABLES |
ba180fd4 | 52 | mm->context.skas.last_pmd = (unsigned long) __va(pud_val(*pud)); |
7ef93905 | 53 | #endif |
d67b569f JD |
54 | |
55 | *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT)); | |
21c935e5 | 56 | *pte = pte_mkread(*pte); |
ba180fd4 | 57 | return 0; |
d67b569f JD |
58 | |
59 | out_pmd: | |
60 | pud_free(pud); | |
61 | out_pte: | |
62 | pmd_free(pmd); | |
63 | out: | |
ba180fd4 | 64 | return -ENOMEM; |
d67b569f JD |
65 | } |
66 | ||
77bf4400 | 67 | int init_new_context(struct task_struct *task, struct mm_struct *mm) |
1da177e4 | 68 | { |
ba180fd4 | 69 | struct mmu_context_skas *from_mm = NULL; |
858259cf | 70 | struct mmu_context_skas *to_mm = &mm->context.skas; |
8b51304e | 71 | unsigned long stack = 0; |
12919aa6 | 72 | int ret = -ENOMEM; |
1da177e4 | 73 | |
ba180fd4 | 74 | if (skas_needs_stub) { |
8b51304e | 75 | stack = get_zeroed_page(GFP_KERNEL); |
ba180fd4 | 76 | if (stack == 0) |
8b51304e | 77 | goto out; |
1da177e4 | 78 | |
ba180fd4 JD |
79 | /* |
80 | * This zeros the entry that pgd_alloc didn't, needed since | |
d67b569f JD |
81 | * we are about to reinitialize it, and want mm.nr_ptes to |
82 | * be accurate. | |
83 | */ | |
84 | mm->pgd[USER_PTRS_PER_PGD] = __pgd(0); | |
1da177e4 | 85 | |
d67b569f JD |
86 | ret = init_stub_pte(mm, CONFIG_STUB_CODE, |
87 | (unsigned long) &__syscall_stub_start); | |
ba180fd4 | 88 | if (ret) |
8b51304e | 89 | goto out_free; |
d67b569f JD |
90 | |
91 | ret = init_stub_pte(mm, CONFIG_STUB_DATA, stack); | |
ba180fd4 | 92 | if (ret) |
d67b569f JD |
93 | goto out_free; |
94 | ||
95 | mm->nr_ptes--; | |
8b51304e | 96 | } |
858259cf BS |
97 | |
98 | to_mm->id.stack = stack; | |
ba180fd4 | 99 | if (current->mm != NULL && current->mm != &init_mm) |
858259cf | 100 | from_mm = ¤t->mm->context.skas; |
9786a8f3 | 101 | |
ba180fd4 | 102 | if (proc_mm) { |
12919aa6 | 103 | ret = new_mm(stack); |
ba180fd4 JD |
104 | if (ret < 0) { |
105 | printk(KERN_ERR "init_new_context_skas - " | |
106 | "new_mm failed, errno = %d\n", ret); | |
8b51304e BS |
107 | goto out_free; |
108 | } | |
858259cf | 109 | to_mm->id.u.mm_fd = ret; |
8b51304e BS |
110 | } |
111 | else { | |
ba180fd4 | 112 | if (from_mm) |
858259cf BS |
113 | to_mm->id.u.pid = copy_context_skas0(stack, |
114 | from_mm->id.u.pid); | |
115 | else to_mm->id.u.pid = start_userspace(stack); | |
116 | } | |
117 | ||
118 | ret = init_new_ldt(to_mm, from_mm); | |
ba180fd4 JD |
119 | if (ret < 0) { |
120 | printk(KERN_ERR "init_new_context_skas - init_ldt" | |
858259cf BS |
121 | " failed, errno = %d\n", ret); |
122 | goto out_free; | |
d67b569f JD |
123 | } |
124 | ||
125 | return 0; | |
126 | ||
127 | out_free: | |
ba180fd4 | 128 | if (to_mm->id.stack != 0) |
858259cf | 129 | free_page(to_mm->id.stack); |
d67b569f JD |
130 | out: |
131 | return ret; | |
1da177e4 LT |
132 | } |
133 | ||
77bf4400 | 134 | void destroy_context(struct mm_struct *mm) |
1da177e4 | 135 | { |
d67b569f | 136 | struct mmu_context_skas *mmu = &mm->context.skas; |
1da177e4 | 137 | |
ba180fd4 | 138 | if (proc_mm) |
d67b569f | 139 | os_close_file(mmu->id.u.mm_fd); |
8b51304e | 140 | else |
d67b569f | 141 | os_kill_ptraced_process(mmu->id.u.pid, 1); |
8b51304e | 142 | |
ba180fd4 | 143 | if (!proc_mm || !ptrace_faultinfo) { |
d67b569f | 144 | free_page(mmu->id.stack); |
4c21e2f2 | 145 | pte_lock_deinit(virt_to_page(mmu->last_page_table)); |
7ef93905 | 146 | pte_free_kernel((pte_t *) mmu->last_page_table); |
df849a15 | 147 | dec_zone_page_state(virt_to_page(mmu->last_page_table), NR_PAGETABLE); |
7ef93905 JD |
148 | #ifdef CONFIG_3_LEVEL_PGTABLES |
149 | pmd_free((pmd_t *) mmu->last_pmd); | |
150 | #endif | |
d67b569f JD |
151 | } |
152 | } |