blob: c5475ecd9fd4980f31a9e8e668347c7bfb806118 [file] [log] [blame]
Jeff Dikeba180fd2007-10-16 01:27:00 -07001/*
2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Diked67b5692005-07-07 17:56:49 -07006#include "linux/mm.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -07007#include "linux/sched.h"
Jeff Diked67b5692005-07-07 17:56:49 -07008#include "asm/pgalloc.h"
9#include "asm/pgtable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "os.h"
11#include "skas.h"
12
Jeff Diked67b5692005-07-07 17:56:49 -070013extern int __syscall_stub_start;
14
15static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
16 unsigned long kernel)
17{
18 pgd_t *pgd;
19 pud_t *pud;
20 pmd_t *pmd;
21 pte_t *pte;
22
Jeff Diked67b5692005-07-07 17:56:49 -070023 pgd = pgd_offset(mm, proc);
24 pud = pud_alloc(mm, pgd, proc);
25 if (!pud)
26 goto out;
27
28 pmd = pmd_alloc(mm, pud, proc);
29 if (!pmd)
30 goto out_pmd;
31
32 pte = pte_alloc_map(mm, pmd, proc);
33 if (!pte)
34 goto out_pte;
35
Jeff Dikeba180fd2007-10-16 01:27:00 -070036 /*
37 * There's an interaction between the skas0 stub pages, stack
Jeff Diked67b5692005-07-07 17:56:49 -070038 * randomization, and the BUG at the end of exit_mmap. exit_mmap
Jeff Dikeba180fd2007-10-16 01:27:00 -070039 * checks that the number of page tables freed is the same as had
40 * been allocated. If the stack is on the last page table page,
Jeff Diked67b5692005-07-07 17:56:49 -070041 * then the stack pte page will be freed, and if not, it won't. To
42 * avoid having to know where the stack is, or if the process mapped
43 * something at the top of its address space for some other reason,
44 * we set TASK_SIZE to end at the start of the last page table.
45 * This keeps exit_mmap off the last page, but introduces a leak
46 * of that page. So, we hang onto it here and free it in
47 * destroy_context_skas.
48 */
49
Jeff Dikeba180fd2007-10-16 01:27:00 -070050 mm->context.skas.last_page_table = pmd_page_vaddr(*pmd);
Jeff Dike7ef93902005-09-03 15:57:52 -070051#ifdef CONFIG_3_LEVEL_PGTABLES
Jeff Dikeba180fd2007-10-16 01:27:00 -070052 mm->context.skas.last_pmd = (unsigned long) __va(pud_val(*pud));
Jeff Dike7ef93902005-09-03 15:57:52 -070053#endif
Jeff Diked67b5692005-07-07 17:56:49 -070054
55 *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
Paolo 'Blaisorblade' Giarrusso21c935e2006-10-11 01:21:32 -070056 *pte = pte_mkread(*pte);
Jeff Dikeba180fd2007-10-16 01:27:00 -070057 return 0;
Jeff Diked67b5692005-07-07 17:56:49 -070058
59 out_pmd:
60 pud_free(pud);
61 out_pte:
62 pmd_free(pmd);
63 out:
Jeff Dikeba180fd2007-10-16 01:27:00 -070064 return -ENOMEM;
Jeff Diked67b5692005-07-07 17:56:49 -070065}
66
Jeff Dike77bf4402007-10-16 01:26:58 -070067int init_new_context(struct task_struct *task, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Jeff Dikeba180fd2007-10-16 01:27:00 -070069 struct mmu_context_skas *from_mm = NULL;
Bodo Stroesser858259c2005-11-07 00:58:55 -080070 struct mmu_context_skas *to_mm = &mm->context.skas;
Bodo Stroesser8b513042005-09-03 15:57:49 -070071 unsigned long stack = 0;
Bodo Stroesser12919aa2006-01-18 17:42:39 -080072 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Jeff Dikeba180fd2007-10-16 01:27:00 -070074 if (skas_needs_stub) {
Bodo Stroesser8b513042005-09-03 15:57:49 -070075 stack = get_zeroed_page(GFP_KERNEL);
Jeff Dikeba180fd2007-10-16 01:27:00 -070076 if (stack == 0)
Bodo Stroesser8b513042005-09-03 15:57:49 -070077 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Jeff Dikeba180fd2007-10-16 01:27:00 -070079 /*
80 * This zeros the entry that pgd_alloc didn't, needed since
Jeff Diked67b5692005-07-07 17:56:49 -070081 * we are about to reinitialize it, and want mm.nr_ptes to
82 * be accurate.
83 */
84 mm->pgd[USER_PTRS_PER_PGD] = __pgd(0);
85
86 ret = init_stub_pte(mm, CONFIG_STUB_CODE,
87 (unsigned long) &__syscall_stub_start);
Jeff Dikeba180fd2007-10-16 01:27:00 -070088 if (ret)
Bodo Stroesser8b513042005-09-03 15:57:49 -070089 goto out_free;
Jeff Diked67b5692005-07-07 17:56:49 -070090
91 ret = init_stub_pte(mm, CONFIG_STUB_DATA, stack);
Jeff Dikeba180fd2007-10-16 01:27:00 -070092 if (ret)
Jeff Diked67b5692005-07-07 17:56:49 -070093 goto out_free;
94
95 mm->nr_ptes--;
Bodo Stroesser8b513042005-09-03 15:57:49 -070096 }
Bodo Stroesser858259c2005-11-07 00:58:55 -080097
98 to_mm->id.stack = stack;
Jeff Dikeba180fd2007-10-16 01:27:00 -070099 if (current->mm != NULL && current->mm != &init_mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800100 from_mm = &current->mm->context.skas;
Bodo Stroesser9786a8f2005-07-07 17:56:50 -0700101
Jeff Dikeba180fd2007-10-16 01:27:00 -0700102 if (proc_mm) {
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800103 ret = new_mm(stack);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700104 if (ret < 0) {
105 printk(KERN_ERR "init_new_context_skas - "
106 "new_mm failed, errno = %d\n", ret);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700107 goto out_free;
108 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800109 to_mm->id.u.mm_fd = ret;
Bodo Stroesser8b513042005-09-03 15:57:49 -0700110 }
111 else {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700112 if (from_mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800113 to_mm->id.u.pid = copy_context_skas0(stack,
114 from_mm->id.u.pid);
115 else to_mm->id.u.pid = start_userspace(stack);
116 }
117
118 ret = init_new_ldt(to_mm, from_mm);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700119 if (ret < 0) {
120 printk(KERN_ERR "init_new_context_skas - init_ldt"
Bodo Stroesser858259c2005-11-07 00:58:55 -0800121 " failed, errno = %d\n", ret);
122 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Jeff Diked67b5692005-07-07 17:56:49 -0700125 return 0;
126
127 out_free:
Jeff Dikeba180fd2007-10-16 01:27:00 -0700128 if (to_mm->id.stack != 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800129 free_page(to_mm->id.stack);
Jeff Diked67b5692005-07-07 17:56:49 -0700130 out:
131 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Jeff Dike77bf4402007-10-16 01:26:58 -0700134void destroy_context(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Jeff Diked67b5692005-07-07 17:56:49 -0700136 struct mmu_context_skas *mmu = &mm->context.skas;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jeff Dikeba180fd2007-10-16 01:27:00 -0700138 if (proc_mm)
Jeff Diked67b5692005-07-07 17:56:49 -0700139 os_close_file(mmu->id.u.mm_fd);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700140 else
Jeff Diked67b5692005-07-07 17:56:49 -0700141 os_kill_ptraced_process(mmu->id.u.pid, 1);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700142
Jeff Dikeba180fd2007-10-16 01:27:00 -0700143 if (!proc_mm || !ptrace_faultinfo) {
Jeff Diked67b5692005-07-07 17:56:49 -0700144 free_page(mmu->id.stack);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700145 pte_lock_deinit(virt_to_page(mmu->last_page_table));
Jeff Dike7ef93902005-09-03 15:57:52 -0700146 pte_free_kernel((pte_t *) mmu->last_page_table);
Christoph Lameterdf849a12006-06-30 01:55:38 -0700147 dec_zone_page_state(virt_to_page(mmu->last_page_table), NR_PAGETABLE);
Jeff Dike7ef93902005-09-03 15:57:52 -0700148#ifdef CONFIG_3_LEVEL_PGTABLES
149 pmd_free((pmd_t *) mmu->last_pmd);
150#endif
Jeff Diked67b5692005-07-07 17:56:49 -0700151 }
152}