2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/sched.h"
7 #include "linux/list.h"
8 #include "linux/spinlock.h"
9 #include "linux/slab.h"
10 #include "linux/errno.h"
12 #include "asm/current.h"
13 #include "asm/segment.h"
15 #include "asm/pgalloc.h"
16 #include "asm/pgtable.h"
21 extern int __syscall_stub_start;
23 static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
31 pgd = pgd_offset(mm, proc);
32 pud = pud_alloc(mm, pgd, proc);
36 pmd = pmd_alloc(mm, pud, proc);
40 pte = pte_alloc_map(mm, pmd, proc);
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.
57 mm->context.skas.last_page_table = pmd_page_vaddr(*pmd);
58 #ifdef CONFIG_3_LEVEL_PGTABLES
59 mm->context.skas.last_pmd = (unsigned long) __va(pud_val(*pud));
62 *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
63 /* This is wrong for the code page, but it doesn't matter since the
64 * stub is mapped by hand with the correct permissions.
66 *pte = pte_mkwrite(*pte);
77 int init_new_context_skas(struct task_struct *task, struct mm_struct *mm)
79 struct mmu_context_skas *from_mm = NULL;
80 struct mmu_context_skas *to_mm = &mm->context.skas;
81 unsigned long stack = 0;
85 stack = get_zeroed_page(GFP_KERNEL);
89 /* This zeros the entry that pgd_alloc didn't, needed since
90 * we are about to reinitialize it, and want mm.nr_ptes to
93 mm->pgd[USER_PTRS_PER_PGD] = __pgd(0);
95 ret = init_stub_pte(mm, CONFIG_STUB_CODE,
96 (unsigned long) &__syscall_stub_start);
100 ret = init_stub_pte(mm, CONFIG_STUB_DATA, stack);
107 to_mm->id.stack = stack;
108 if(current->mm != NULL && current->mm != &init_mm)
109 from_mm = ¤t->mm->context.skas;
114 printk("init_new_context_skas - new_mm failed, "
115 "errno = %d\n", ret);
118 to_mm->id.u.mm_fd = ret;
122 to_mm->id.u.pid = copy_context_skas0(stack,
124 else to_mm->id.u.pid = start_userspace(stack);
127 ret = init_new_ldt(to_mm, from_mm);
129 printk("init_new_context_skas - init_ldt"
130 " failed, errno = %d\n", ret);
137 if(to_mm->id.stack != 0)
138 free_page(to_mm->id.stack);
143 void destroy_context_skas(struct mm_struct *mm)
145 struct mmu_context_skas *mmu = &mm->context.skas;
148 os_close_file(mmu->id.u.mm_fd);
150 os_kill_ptraced_process(mmu->id.u.pid, 1);
152 if(!proc_mm || !ptrace_faultinfo){
153 free_page(mmu->id.stack);
154 pte_lock_deinit(virt_to_page(mmu->last_page_table));
155 pte_free_kernel((pte_t *) mmu->last_page_table);
156 dec_zone_page_state(virt_to_page(mmu->last_page_table), NR_PAGETABLE);
157 #ifdef CONFIG_3_LEVEL_PGTABLES
158 pmd_free((pmd_t *) mmu->last_pmd);