blob: f859ec306cd5ded54f4239bba0519d58eb52f2e3 [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"
Jeff Dike54ae36f2007-10-16 01:27:33 -070010#include "as-layout.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "os.h"
12#include "skas.h"
13
Jeff Diked67b5692005-07-07 17:56:49 -070014extern int __syscall_stub_start;
15
16static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
17 unsigned long kernel)
18{
19 pgd_t *pgd;
20 pud_t *pud;
21 pmd_t *pmd;
22 pte_t *pte;
23
Jeff Diked67b5692005-07-07 17:56:49 -070024 pgd = pgd_offset(mm, proc);
25 pud = pud_alloc(mm, pgd, proc);
26 if (!pud)
27 goto out;
28
29 pmd = pmd_alloc(mm, pud, proc);
30 if (!pmd)
31 goto out_pmd;
32
33 pte = pte_alloc_map(mm, pmd, proc);
34 if (!pte)
35 goto out_pte;
36
Jeff Dikeba180fd2007-10-16 01:27:00 -070037 /*
38 * There's an interaction between the skas0 stub pages, stack
Jeff Diked67b5692005-07-07 17:56:49 -070039 * randomization, and the BUG at the end of exit_mmap. exit_mmap
Jeff Dikeba180fd2007-10-16 01:27:00 -070040 * checks that the number of page tables freed is the same as had
41 * been allocated. If the stack is on the last page table page,
Jeff Diked67b5692005-07-07 17:56:49 -070042 * then the stack pte page will be freed, and if not, it won't. To
43 * avoid having to know where the stack is, or if the process mapped
44 * something at the top of its address space for some other reason,
45 * we set TASK_SIZE to end at the start of the last page table.
46 * This keeps exit_mmap off the last page, but introduces a leak
47 * of that page. So, we hang onto it here and free it in
48 * destroy_context_skas.
49 */
50
Jeff Dike6c738ff2007-10-16 01:27:06 -070051 mm->context.last_page_table = pmd_page_vaddr(*pmd);
Jeff Dike7ef93902005-09-03 15:57:52 -070052#ifdef CONFIG_3_LEVEL_PGTABLES
Jeff Dike6c738ff2007-10-16 01:27:06 -070053 mm->context.last_pmd = (unsigned long) __va(pud_val(*pud));
Jeff Dike7ef93902005-09-03 15:57:52 -070054#endif
Jeff Diked67b5692005-07-07 17:56:49 -070055
56 *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
Paolo 'Blaisorblade' Giarrusso21c935e2006-10-11 01:21:32 -070057 *pte = pte_mkread(*pte);
Jeff Dikeba180fd2007-10-16 01:27:00 -070058 return 0;
Jeff Diked67b5692005-07-07 17:56:49 -070059
60 out_pmd:
61 pud_free(pud);
62 out_pte:
63 pmd_free(pmd);
64 out:
Jeff Dikeba180fd2007-10-16 01:27:00 -070065 return -ENOMEM;
Jeff Diked67b5692005-07-07 17:56:49 -070066}
67
Jeff Dike77bf4402007-10-16 01:26:58 -070068int init_new_context(struct task_struct *task, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Jeff Dike6c738ff2007-10-16 01:27:06 -070070 struct mm_context *from_mm = NULL;
71 struct mm_context *to_mm = &mm->context;
Bodo Stroesser8b513042005-09-03 15:57:49 -070072 unsigned long stack = 0;
Bodo Stroesser12919aa2006-01-18 17:42:39 -080073 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jeff Dikeba180fd2007-10-16 01:27:00 -070075 if (skas_needs_stub) {
Bodo Stroesser8b513042005-09-03 15:57:49 -070076 stack = get_zeroed_page(GFP_KERNEL);
Jeff Dikeba180fd2007-10-16 01:27:00 -070077 if (stack == 0)
Bodo Stroesser8b513042005-09-03 15:57:49 -070078 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Jeff Dikeba180fd2007-10-16 01:27:00 -070080 /*
81 * This zeros the entry that pgd_alloc didn't, needed since
Jeff Diked67b5692005-07-07 17:56:49 -070082 * we are about to reinitialize it, and want mm.nr_ptes to
83 * be accurate.
84 */
85 mm->pgd[USER_PTRS_PER_PGD] = __pgd(0);
86
Jeff Dike54ae36f2007-10-16 01:27:33 -070087 ret = init_stub_pte(mm, STUB_CODE,
Jeff Diked67b5692005-07-07 17:56:49 -070088 (unsigned long) &__syscall_stub_start);
Jeff Dikeba180fd2007-10-16 01:27:00 -070089 if (ret)
Bodo Stroesser8b513042005-09-03 15:57:49 -070090 goto out_free;
Jeff Diked67b5692005-07-07 17:56:49 -070091
Jeff Dike54ae36f2007-10-16 01:27:33 -070092 ret = init_stub_pte(mm, STUB_DATA, stack);
Jeff Dikeba180fd2007-10-16 01:27:00 -070093 if (ret)
Jeff Diked67b5692005-07-07 17:56:49 -070094 goto out_free;
95
96 mm->nr_ptes--;
Bodo Stroesser8b513042005-09-03 15:57:49 -070097 }
Bodo Stroesser858259c2005-11-07 00:58:55 -080098
99 to_mm->id.stack = stack;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700100 if (current->mm != NULL && current->mm != &init_mm)
Jeff Dike6c738ff2007-10-16 01:27:06 -0700101 from_mm = &current->mm->context;
Bodo Stroesser9786a8f2005-07-07 17:56:50 -0700102
Jeff Dikeba180fd2007-10-16 01:27:00 -0700103 if (proc_mm) {
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800104 ret = new_mm(stack);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700105 if (ret < 0) {
106 printk(KERN_ERR "init_new_context_skas - "
107 "new_mm failed, errno = %d\n", ret);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700108 goto out_free;
109 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800110 to_mm->id.u.mm_fd = ret;
Bodo Stroesser8b513042005-09-03 15:57:49 -0700111 }
112 else {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700113 if (from_mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800114 to_mm->id.u.pid = copy_context_skas0(stack,
115 from_mm->id.u.pid);
116 else to_mm->id.u.pid = start_userspace(stack);
117 }
118
119 ret = init_new_ldt(to_mm, from_mm);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700120 if (ret < 0) {
121 printk(KERN_ERR "init_new_context_skas - init_ldt"
Bodo Stroesser858259c2005-11-07 00:58:55 -0800122 " failed, errno = %d\n", ret);
123 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125
Jeff Diked67b5692005-07-07 17:56:49 -0700126 return 0;
127
128 out_free:
Jeff Dikeba180fd2007-10-16 01:27:00 -0700129 if (to_mm->id.stack != 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800130 free_page(to_mm->id.stack);
Jeff Diked67b5692005-07-07 17:56:49 -0700131 out:
132 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Jeff Dike77bf4402007-10-16 01:26:58 -0700135void destroy_context(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Jeff Dike6c738ff2007-10-16 01:27:06 -0700137 struct mm_context *mmu = &mm->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jeff Dikeba180fd2007-10-16 01:27:00 -0700139 if (proc_mm)
Jeff Diked67b5692005-07-07 17:56:49 -0700140 os_close_file(mmu->id.u.mm_fd);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700141 else
Jeff Diked67b5692005-07-07 17:56:49 -0700142 os_kill_ptraced_process(mmu->id.u.pid, 1);
Bodo Stroesser8b513042005-09-03 15:57:49 -0700143
Jeff Dikeba180fd2007-10-16 01:27:00 -0700144 if (!proc_mm || !ptrace_faultinfo) {
Jeff Diked67b5692005-07-07 17:56:49 -0700145 free_page(mmu->id.stack);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700146 pte_lock_deinit(virt_to_page(mmu->last_page_table));
Jeff Dike7ef93902005-09-03 15:57:52 -0700147 pte_free_kernel((pte_t *) mmu->last_page_table);
Christoph Lameterdf849a12006-06-30 01:55:38 -0700148 dec_zone_page_state(virt_to_page(mmu->last_page_table), NR_PAGETABLE);
Jeff Dike7ef93902005-09-03 15:57:52 -0700149#ifdef CONFIG_3_LEVEL_PGTABLES
150 pmd_free((pmd_t *) mmu->last_pmd);
151#endif
Jeff Diked67b5692005-07-07 17:56:49 -0700152 }
Jeff Dike28078e82007-10-16 01:27:08 -0700153
154 free_ldt(mmu);
Jeff Diked67b5692005-07-07 17:56:49 -0700155}