blob: 677871f1b37c65d2379cd4b4b3f58e2fc9aec067 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
Jeff Diked67b5692005-07-07 17:56:49 -07006#include "linux/config.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "linux/sched.h"
8#include "linux/list.h"
9#include "linux/spinlock.h"
10#include "linux/slab.h"
Jeff Diked67b5692005-07-07 17:56:49 -070011#include "linux/errno.h"
12#include "linux/mm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "asm/current.h"
14#include "asm/segment.h"
15#include "asm/mmu.h"
Jeff Diked67b5692005-07-07 17:56:49 -070016#include "asm/pgalloc.h"
17#include "asm/pgtable.h"
Bodo Stroesser858259c2005-11-07 00:58:55 -080018#include "asm/ldt.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "os.h"
20#include "skas.h"
21
Jeff Diked67b5692005-07-07 17:56:49 -070022extern 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
Jeff Diked67b5692005-07-07 17:56:49 -070032 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);
Jeff Dike7ef93902005-09-03 15:57:52 -070059#ifdef CONFIG_3_LEVEL_PGTABLES
60 mm->context.skas.last_pmd = (unsigned long) __va(pud_val(*pud));
61#endif
Jeff Diked67b5692005-07-07 17:56:49 -070062
63 *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
64 *pte = pte_mkexec(*pte);
65 *pte = pte_wrprotect(*pte);
Jeff Diked67b5692005-07-07 17:56:49 -070066 return(0);
67
68 out_pmd:
69 pud_free(pud);
70 out_pte:
71 pmd_free(pmd);
72 out:
Jeff Diked67b5692005-07-07 17:56:49 -070073 return(-ENOMEM);
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076int init_new_context_skas(struct task_struct *task, struct mm_struct *mm)
77{
Bodo Stroesser858259c2005-11-07 00:58:55 -080078 struct mmu_context_skas *from_mm = NULL;
79 struct mmu_context_skas *to_mm = &mm->context.skas;
Bodo Stroesser8b513042005-09-03 15:57:49 -070080 unsigned long stack = 0;
Bodo Stroesser858259c2005-11-07 00:58:55 -080081 int from_fd, ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Bodo Stroesser858259c2005-11-07 00:58:55 -080083 if(skas_needs_stub){
Bodo Stroesser8b513042005-09-03 15:57:49 -070084 stack = get_zeroed_page(GFP_KERNEL);
85 if(stack == 0)
86 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Jeff Diked67b5692005-07-07 17:56:49 -070088 /* This zeros the entry that pgd_alloc didn't, needed since
89 * we are about to reinitialize it, and want mm.nr_ptes to
90 * be accurate.
91 */
92 mm->pgd[USER_PTRS_PER_PGD] = __pgd(0);
93
94 ret = init_stub_pte(mm, CONFIG_STUB_CODE,
95 (unsigned long) &__syscall_stub_start);
96 if(ret)
Bodo Stroesser8b513042005-09-03 15:57:49 -070097 goto out_free;
Jeff Diked67b5692005-07-07 17:56:49 -070098
99 ret = init_stub_pte(mm, CONFIG_STUB_DATA, stack);
100 if(ret)
101 goto out_free;
102
103 mm->nr_ptes--;
Bodo Stroesser8b513042005-09-03 15:57:49 -0700104 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800105
106 to_mm->id.stack = stack;
107 if(current->mm != NULL && current->mm != &init_mm)
108 from_mm = &current->mm->context.skas;
Bodo Stroesser9786a8f2005-07-07 17:56:50 -0700109
Bodo Stroesser8b513042005-09-03 15:57:49 -0700110 if(proc_mm){
Bodo Stroesser858259c2005-11-07 00:58:55 -0800111 if(from_mm)
112 from_fd = from_mm->id.u.mm_fd;
113 else from_fd = -1;
Bodo Stroesser8b513042005-09-03 15:57:49 -0700114
Bodo Stroesser858259c2005-11-07 00:58:55 -0800115 ret = new_mm(from_fd, stack);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700116 if(ret < 0){
117 printk("init_new_context_skas - new_mm failed, "
118 "errno = %d\n", ret);
119 goto out_free;
120 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800121 to_mm->id.u.mm_fd = ret;
Bodo Stroesser8b513042005-09-03 15:57:49 -0700122 }
123 else {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800124 if(from_mm)
125 to_mm->id.u.pid = copy_context_skas0(stack,
126 from_mm->id.u.pid);
127 else to_mm->id.u.pid = start_userspace(stack);
128 }
129
130 ret = init_new_ldt(to_mm, from_mm);
131 if(ret < 0){
132 printk("init_new_context_skas - init_ldt"
133 " failed, errno = %d\n", ret);
134 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
Jeff Diked67b5692005-07-07 17:56:49 -0700137 return 0;
138
139 out_free:
Bodo Stroesser858259c2005-11-07 00:58:55 -0800140 if(to_mm->id.stack != 0)
141 free_page(to_mm->id.stack);
Jeff Diked67b5692005-07-07 17:56:49 -0700142 out:
143 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146void destroy_context_skas(struct mm_struct *mm)
147{
Jeff Diked67b5692005-07-07 17:56:49 -0700148 struct mmu_context_skas *mmu = &mm->context.skas;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Jeff Diked67b5692005-07-07 17:56:49 -0700150 if(proc_mm)
151 os_close_file(mmu->id.u.mm_fd);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700152 else
Jeff Diked67b5692005-07-07 17:56:49 -0700153 os_kill_ptraced_process(mmu->id.u.pid, 1);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700154
155 if(!proc_mm || !ptrace_faultinfo){
Jeff Diked67b5692005-07-07 17:56:49 -0700156 free_page(mmu->id.stack);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700157 pte_lock_deinit(virt_to_page(mmu->last_page_table));
Jeff Dike7ef93902005-09-03 15:57:52 -0700158 pte_free_kernel((pte_t *) mmu->last_page_table);
159 dec_page_state(nr_page_table_pages);
160#ifdef CONFIG_3_LEVEL_PGTABLES
161 pmd_free((pmd_t *) mmu->last_pmd);
162#endif
Jeff Diked67b5692005-07-07 17:56:49 -0700163 }
164}