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