blob: 8bfd1e90581225eb0e19fada92611399d2a1d4c8 [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 Diked67b5692005-07-07 17:56:49 -070037 *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
Paolo 'Blaisorblade' Giarrusso21c935e2006-10-11 01:21:32 -070038 *pte = pte_mkread(*pte);
Jeff Dikeba180fd2007-10-16 01:27:00 -070039 return 0;
Jeff Diked67b5692005-07-07 17:56:49 -070040
Jeff Diked67b5692005-07-07 17:56:49 -070041 out_pte:
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080042 pmd_free(mm, pmd);
Roel Kluin4d04c702009-09-21 17:04:11 -070043 out_pmd:
44 pud_free(mm, pud);
Jeff Diked67b5692005-07-07 17:56:49 -070045 out:
Jeff Dikeba180fd2007-10-16 01:27:00 -070046 return -ENOMEM;
Jeff Diked67b5692005-07-07 17:56:49 -070047}
48
Jeff Dike77bf4402007-10-16 01:26:58 -070049int init_new_context(struct task_struct *task, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Jeff Dike6c738ff2007-10-16 01:27:06 -070051 struct mm_context *from_mm = NULL;
52 struct mm_context *to_mm = &mm->context;
Bodo Stroesser8b513042005-09-03 15:57:49 -070053 unsigned long stack = 0;
Bodo Stroesser12919aa2006-01-18 17:42:39 -080054 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jeff Dikeba180fd2007-10-16 01:27:00 -070056 if (skas_needs_stub) {
Bodo Stroesser8b513042005-09-03 15:57:49 -070057 stack = get_zeroed_page(GFP_KERNEL);
Jeff Dikeba180fd2007-10-16 01:27:00 -070058 if (stack == 0)
Bodo Stroesser8b513042005-09-03 15:57:49 -070059 goto out;
Bodo Stroesser8b513042005-09-03 15:57:49 -070060 }
Bodo Stroesser858259c2005-11-07 00:58:55 -080061
62 to_mm->id.stack = stack;
Jeff Dikeba180fd2007-10-16 01:27:00 -070063 if (current->mm != NULL && current->mm != &init_mm)
Jeff Dike6c738ff2007-10-16 01:27:06 -070064 from_mm = &current->mm->context;
Bodo Stroesser9786a8f2005-07-07 17:56:50 -070065
Jeff Dikeba180fd2007-10-16 01:27:00 -070066 if (proc_mm) {
Bodo Stroesser12919aa2006-01-18 17:42:39 -080067 ret = new_mm(stack);
Jeff Dikeba180fd2007-10-16 01:27:00 -070068 if (ret < 0) {
69 printk(KERN_ERR "init_new_context_skas - "
70 "new_mm failed, errno = %d\n", ret);
Bodo Stroesser8b513042005-09-03 15:57:49 -070071 goto out_free;
72 }
Bodo Stroesser858259c2005-11-07 00:58:55 -080073 to_mm->id.u.mm_fd = ret;
Bodo Stroesser8b513042005-09-03 15:57:49 -070074 }
75 else {
Jeff Dikeba180fd2007-10-16 01:27:00 -070076 if (from_mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -080077 to_mm->id.u.pid = copy_context_skas0(stack,
78 from_mm->id.u.pid);
79 else to_mm->id.u.pid = start_userspace(stack);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080080
81 if (to_mm->id.u.pid < 0) {
82 ret = to_mm->id.u.pid;
83 goto out_free;
84 }
Bodo Stroesser858259c2005-11-07 00:58:55 -080085 }
86
87 ret = init_new_ldt(to_mm, from_mm);
Jeff Dikeba180fd2007-10-16 01:27:00 -070088 if (ret < 0) {
89 printk(KERN_ERR "init_new_context_skas - init_ldt"
Bodo Stroesser858259c2005-11-07 00:58:55 -080090 " failed, errno = %d\n", ret);
91 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
Jeff Dikeac2a6592008-02-08 04:22:11 -080094 to_mm->stub_pages = NULL;
95
Jeff Diked67b5692005-07-07 17:56:49 -070096 return 0;
97
98 out_free:
Jeff Dikeba180fd2007-10-16 01:27:00 -070099 if (to_mm->id.stack != 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800100 free_page(to_mm->id.stack);
Jeff Diked67b5692005-07-07 17:56:49 -0700101 out:
102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Jeff Dike39633332008-02-04 22:31:01 -0800105void arch_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
106{
107 struct page **pages;
108 int err, ret;
109
110 if (!skas_needs_stub)
111 return;
112
113 ret = init_stub_pte(mm, STUB_CODE,
114 (unsigned long) &__syscall_stub_start);
115 if (ret)
116 goto out;
117
118 ret = init_stub_pte(mm, STUB_DATA, mm->context.id.stack);
119 if (ret)
120 goto out;
121
122 pages = kmalloc(2 * sizeof(struct page *), GFP_KERNEL);
123 if (pages == NULL) {
124 printk(KERN_ERR "arch_dup_mmap failed to allocate 2 page "
125 "pointers\n");
126 goto out;
127 }
128
129 pages[0] = virt_to_page(&__syscall_stub_start);
130 pages[1] = virt_to_page(mm->context.id.stack);
Jeff Dikeac2a6592008-02-08 04:22:11 -0800131 mm->context.stub_pages = pages;
Jeff Dike39633332008-02-04 22:31:01 -0800132
133 /* dup_mmap already holds mmap_sem */
134 err = install_special_mapping(mm, STUB_START, STUB_END - STUB_START,
135 VM_READ | VM_MAYREAD | VM_EXEC |
136 VM_MAYEXEC | VM_DONTCOPY, pages);
137 if (err) {
138 printk(KERN_ERR "install_special_mapping returned %d\n", err);
139 goto out_free;
140 }
141 return;
142
143out_free:
144 kfree(pages);
145out:
146 force_sigsegv(SIGSEGV, current);
147}
148
149void arch_exit_mmap(struct mm_struct *mm)
150{
151 pte_t *pte;
152
Jeff Dikeac2a6592008-02-08 04:22:11 -0800153 if (mm->context.stub_pages != NULL)
154 kfree(mm->context.stub_pages);
Jeff Dike39633332008-02-04 22:31:01 -0800155 pte = virt_to_pte(mm, STUB_CODE);
156 if (pte != NULL)
157 pte_clear(mm, STUB_CODE, pte);
158
159 pte = virt_to_pte(mm, STUB_DATA);
160 if (pte == NULL)
161 return;
162
163 pte_clear(mm, STUB_DATA, pte);
164}
165
Jeff Dike77bf4402007-10-16 01:26:58 -0700166void destroy_context(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Jeff Dike6c738ff2007-10-16 01:27:06 -0700168 struct mm_context *mmu = &mm->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jeff Dikeba180fd2007-10-16 01:27:00 -0700170 if (proc_mm)
Jeff Diked67b5692005-07-07 17:56:49 -0700171 os_close_file(mmu->id.u.mm_fd);
Jeff Dike00a905e2008-02-04 22:31:10 -0800172 else {
173 /*
174 * If init_new_context wasn't called, this will be
175 * zero, resulting in a kill(0), which will result in the
176 * whole UML suddenly dying. Also, cover negative and
177 * 1 cases, since they shouldn't happen either.
178 */
179 if (mmu->id.u.pid < 2) {
180 printk(KERN_ERR "corrupt mm_context - pid = %d\n",
181 mmu->id.u.pid);
182 return;
183 }
Jeff Diked67b5692005-07-07 17:56:49 -0700184 os_kill_ptraced_process(mmu->id.u.pid, 1);
Jeff Dike00a905e2008-02-04 22:31:10 -0800185 }
Bodo Stroesser8b513042005-09-03 15:57:49 -0700186
Jeff Dike39633332008-02-04 22:31:01 -0800187 if (skas_needs_stub)
Jeff Diked67b5692005-07-07 17:56:49 -0700188 free_page(mmu->id.stack);
Jeff Dike28078e82007-10-16 01:27:08 -0700189
190 free_ldt(mmu);
Jeff Diked67b5692005-07-07 17:56:49 -0700191}