blob: c17eddcf89b3ce8ab627cbdad0f2574cdd3035cb [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
6#include "linux/sched.h"
7#include "linux/list.h"
8#include "linux/spinlock.h"
9#include "linux/slab.h"
Jeff Diked67b5692005-07-07 17:56:49 -070010#include "linux/errno.h"
11#include "linux/mm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "asm/current.h"
13#include "asm/segment.h"
14#include "asm/mmu.h"
Jeff Diked67b5692005-07-07 17:56:49 -070015#include "asm/pgalloc.h"
16#include "asm/pgtable.h"
Bodo Stroesser858259c2005-11-07 00:58:55 -080017#include "asm/ldt.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "os.h"
19#include "skas.h"
20
Jeff Diked67b5692005-07-07 17:56:49 -070021extern int __syscall_stub_start;
22
23static 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
Jeff Diked67b5692005-07-07 17:56:49 -070031 pgd = pgd_offset(mm, proc);
32 pud = pud_alloc(mm, pgd, proc);
33 if (!pud)
34 goto out;
35
36 pmd = pmd_alloc(mm, pud, proc);
37 if (!pmd)
38 goto out_pmd;
39
40 pte = pte_alloc_map(mm, pmd, proc);
41 if (!pte)
42 goto out_pte;
43
44 /* There's an interaction between the skas0 stub pages, stack
45 * randomization, and the BUG at the end of exit_mmap. exit_mmap
46 * checks that the number of page tables freed is the same as had
47 * been allocated. If the stack is on the last page table page,
48 * then the stack pte page will be freed, and if not, it won't. To
49 * avoid having to know where the stack is, or if the process mapped
50 * something at the top of its address space for some other reason,
51 * we set TASK_SIZE to end at the start of the last page table.
52 * This keeps exit_mmap off the last page, but introduces a leak
53 * of that page. So, we hang onto it here and free it in
54 * destroy_context_skas.
55 */
56
Dave McCracken46a82b22006-09-25 23:31:48 -070057 mm->context.skas.last_page_table = pmd_page_vaddr(*pmd);
Jeff Dike7ef93902005-09-03 15:57:52 -070058#ifdef CONFIG_3_LEVEL_PGTABLES
59 mm->context.skas.last_pmd = (unsigned long) __va(pud_val(*pud));
60#endif
Jeff Diked67b5692005-07-07 17:56:49 -070061
62 *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
Jeff Dike5906e412006-09-29 01:58:54 -070063 /* This is wrong for the code page, but it doesn't matter since the
64 * stub is mapped by hand with the correct permissions.
65 */
66 *pte = pte_mkwrite(*pte);
Jeff Diked67b5692005-07-07 17:56:49 -070067 return(0);
68
69 out_pmd:
70 pud_free(pud);
71 out_pte:
72 pmd_free(pmd);
73 out:
Jeff Diked67b5692005-07-07 17:56:49 -070074 return(-ENOMEM);
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077int init_new_context_skas(struct task_struct *task, struct mm_struct *mm)
78{
Bodo Stroesser858259c2005-11-07 00:58:55 -080079 struct mmu_context_skas *from_mm = NULL;
80 struct mmu_context_skas *to_mm = &mm->context.skas;
Bodo Stroesser8b513042005-09-03 15:57:49 -070081 unsigned long stack = 0;
Bodo Stroesser12919aa2006-01-18 17:42:39 -080082 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Bodo Stroesser858259c2005-11-07 00:58:55 -080084 if(skas_needs_stub){
Bodo Stroesser8b513042005-09-03 15:57:49 -070085 stack = get_zeroed_page(GFP_KERNEL);
86 if(stack == 0)
87 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Jeff Diked67b5692005-07-07 17:56:49 -070089 /* This zeros the entry that pgd_alloc didn't, needed since
90 * we are about to reinitialize it, and want mm.nr_ptes to
91 * be accurate.
92 */
93 mm->pgd[USER_PTRS_PER_PGD] = __pgd(0);
94
95 ret = init_stub_pte(mm, CONFIG_STUB_CODE,
96 (unsigned long) &__syscall_stub_start);
97 if(ret)
Bodo Stroesser8b513042005-09-03 15:57:49 -070098 goto out_free;
Jeff Diked67b5692005-07-07 17:56:49 -070099
100 ret = init_stub_pte(mm, CONFIG_STUB_DATA, stack);
101 if(ret)
102 goto out_free;
103
104 mm->nr_ptes--;
Bodo Stroesser8b513042005-09-03 15:57:49 -0700105 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800106
107 to_mm->id.stack = stack;
108 if(current->mm != NULL && current->mm != &init_mm)
109 from_mm = &current->mm->context.skas;
Bodo Stroesser9786a8f2005-07-07 17:56:50 -0700110
Bodo Stroesser8b513042005-09-03 15:57:49 -0700111 if(proc_mm){
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800112 ret = new_mm(stack);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700113 if(ret < 0){
114 printk("init_new_context_skas - new_mm failed, "
115 "errno = %d\n", ret);
116 goto out_free;
117 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800118 to_mm->id.u.mm_fd = ret;
Bodo Stroesser8b513042005-09-03 15:57:49 -0700119 }
120 else {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800121 if(from_mm)
122 to_mm->id.u.pid = copy_context_skas0(stack,
123 from_mm->id.u.pid);
124 else to_mm->id.u.pid = start_userspace(stack);
125 }
126
127 ret = init_new_ldt(to_mm, from_mm);
128 if(ret < 0){
129 printk("init_new_context_skas - init_ldt"
130 " failed, errno = %d\n", ret);
131 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
Jeff Diked67b5692005-07-07 17:56:49 -0700134 return 0;
135
136 out_free:
Bodo Stroesser858259c2005-11-07 00:58:55 -0800137 if(to_mm->id.stack != 0)
138 free_page(to_mm->id.stack);
Jeff Diked67b5692005-07-07 17:56:49 -0700139 out:
140 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143void destroy_context_skas(struct mm_struct *mm)
144{
Jeff Diked67b5692005-07-07 17:56:49 -0700145 struct mmu_context_skas *mmu = &mm->context.skas;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jeff Diked67b5692005-07-07 17:56:49 -0700147 if(proc_mm)
148 os_close_file(mmu->id.u.mm_fd);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700149 else
Jeff Diked67b5692005-07-07 17:56:49 -0700150 os_kill_ptraced_process(mmu->id.u.pid, 1);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700151
152 if(!proc_mm || !ptrace_faultinfo){
Jeff Diked67b5692005-07-07 17:56:49 -0700153 free_page(mmu->id.stack);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700154 pte_lock_deinit(virt_to_page(mmu->last_page_table));
Jeff Dike7ef93902005-09-03 15:57:52 -0700155 pte_free_kernel((pte_t *) mmu->last_page_table);
Christoph Lameterdf849a12006-06-30 01:55:38 -0700156 dec_zone_page_state(virt_to_page(mmu->last_page_table), NR_PAGETABLE);
Jeff Dike7ef93902005-09-03 15:57:52 -0700157#ifdef CONFIG_3_LEVEL_PGTABLES
158 pmd_free((pmd_t *) mmu->last_pmd);
159#endif
Jeff Diked67b5692005-07-07 17:56:49 -0700160 }
161}