Merge branch 'drm-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/airlied/drm-2.6
[linux-2.6] / arch / um / kernel / skas / mmu.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/config.h"
7 #include "linux/sched.h"
8 #include "linux/list.h"
9 #include "linux/spinlock.h"
10 #include "linux/slab.h"
11 #include "linux/errno.h"
12 #include "linux/mm.h"
13 #include "asm/current.h"
14 #include "asm/segment.h"
15 #include "asm/mmu.h"
16 #include "asm/pgalloc.h"
17 #include "asm/pgtable.h"
18 #include "os.h"
19 #include "skas.h"
20
21 extern int __syscall_stub_start;
22
23 static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
24                          unsigned long kernel)
25 {
26         pgd_t *pgd;
27         pud_t *pud;
28         pmd_t *pmd;
29         pte_t *pte;
30
31         spin_lock(&mm->page_table_lock);
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
58         mm->context.skas.last_page_table = pmd_page_kernel(*pmd);
59 #ifdef CONFIG_3_LEVEL_PGTABLES
60         mm->context.skas.last_pmd = (unsigned long) __va(pud_val(*pud));
61 #endif
62
63         *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
64         *pte = pte_mkexec(*pte);
65         *pte = pte_wrprotect(*pte);
66         spin_unlock(&mm->page_table_lock);
67         return(0);
68
69  out_pmd:
70         pud_free(pud);
71  out_pte:
72         pmd_free(pmd);
73  out:
74         spin_unlock(&mm->page_table_lock);
75         return(-ENOMEM);
76 }
77
78 int init_new_context_skas(struct task_struct *task, struct mm_struct *mm)
79 {
80         struct mm_struct *cur_mm = current->mm;
81         struct mm_id *cur_mm_id = &cur_mm->context.skas.id;
82         struct mm_id *mm_id = &mm->context.skas.id;
83         unsigned long stack = 0;
84         int from, ret = -ENOMEM;
85
86         if(!proc_mm || !ptrace_faultinfo){
87                 stack = get_zeroed_page(GFP_KERNEL);
88                 if(stack == 0)
89                         goto out;
90
91                 /* This zeros the entry that pgd_alloc didn't, needed since
92                  * we are about to reinitialize it, and want mm.nr_ptes to
93                  * be accurate.
94                  */
95                 mm->pgd[USER_PTRS_PER_PGD] = __pgd(0);
96
97                 ret = init_stub_pte(mm, CONFIG_STUB_CODE,
98                                     (unsigned long) &__syscall_stub_start);
99                 if(ret)
100                         goto out_free;
101
102                 ret = init_stub_pte(mm, CONFIG_STUB_DATA, stack);
103                 if(ret)
104                         goto out_free;
105
106                 mm->nr_ptes--;
107         }
108         mm_id->stack = stack;
109
110         if(proc_mm){
111                 if((cur_mm != NULL) && (cur_mm != &init_mm))
112                         from = cur_mm_id->u.mm_fd;
113                 else from = -1;
114
115                 ret = new_mm(from, stack);
116                 if(ret < 0){
117                         printk("init_new_context_skas - new_mm failed, "
118                                "errno = %d\n", ret);
119                         goto out_free;
120                 }
121                 mm_id->u.mm_fd = ret;
122         }
123         else {
124                 if((cur_mm != NULL) && (cur_mm != &init_mm))
125                         mm_id->u.pid = copy_context_skas0(stack,
126                                                           cur_mm_id->u.pid);
127                 else mm_id->u.pid = start_userspace(stack);
128         }
129
130         return 0;
131
132  out_free:
133         if(mm_id->stack != 0)
134                 free_page(mm_id->stack);
135  out:
136         return ret;
137 }
138
139 void destroy_context_skas(struct mm_struct *mm)
140 {
141         struct mmu_context_skas *mmu = &mm->context.skas;
142
143         if(proc_mm)
144                 os_close_file(mmu->id.u.mm_fd);
145         else
146                 os_kill_ptraced_process(mmu->id.u.pid, 1);
147
148         if(!proc_mm || !ptrace_faultinfo){
149                 free_page(mmu->id.stack);
150                 pte_free_kernel((pte_t *) mmu->last_page_table);
151                 dec_page_state(nr_page_table_pages);
152 #ifdef CONFIG_3_LEVEL_PGTABLES
153                 pmd_free((pmd_t *) mmu->last_pmd);
154 #endif
155         }
156 }