]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/um/kernel/skas/mmu.c
Merge branches 'for-4.4/upstream-fixes', 'for-4.5/async-suspend', 'for-4.5/container...
[mirror_ubuntu-artful-kernel.git] / arch / um / kernel / skas / mmu.c
CommitLineData
ba180fd4 1/*
2eb5f31b 2 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
ba180fd4 3 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
1da177e4
LT
4 * Licensed under the GPL
5 */
6
37185b33
AV
7#include <linux/mm.h>
8#include <linux/sched.h>
9#include <linux/slab.h>
10#include <asm/pgalloc.h>
11#include <asm/pgtable.h>
d5f20be7 12#include <asm/sections.h>
37185b33
AV
13#include <as-layout.h>
14#include <os.h>
15#include <skas.h>
1da177e4 16
d67b569f
JD
17static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
18 unsigned long kernel)
19{
20 pgd_t *pgd;
21 pud_t *pud;
22 pmd_t *pmd;
23 pte_t *pte;
24
d67b569f
JD
25 pgd = pgd_offset(mm, proc);
26 pud = pud_alloc(mm, pgd, proc);
27 if (!pud)
28 goto out;
29
30 pmd = pmd_alloc(mm, pud, proc);
31 if (!pmd)
32 goto out_pmd;
33
8ac1f832 34 pte = pte_alloc_map(mm, NULL, pmd, proc);
d67b569f
JD
35 if (!pte)
36 goto out_pte;
37
d67b569f 38 *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
21c935e5 39 *pte = pte_mkread(*pte);
ba180fd4 40 return 0;
d67b569f 41
d67b569f 42 out_pte:
5e541973 43 pmd_free(mm, pmd);
4d04c707
RK
44 out_pmd:
45 pud_free(mm, pud);
d67b569f 46 out:
ba180fd4 47 return -ENOMEM;
d67b569f
JD
48}
49
77bf4400 50int init_new_context(struct task_struct *task, struct mm_struct *mm)
1da177e4 51{
6c738ffa
JD
52 struct mm_context *from_mm = NULL;
53 struct mm_context *to_mm = &mm->context;
8b51304e 54 unsigned long stack = 0;
12919aa6 55 int ret = -ENOMEM;
1da177e4 56
d0b5e15f
RW
57 stack = get_zeroed_page(GFP_KERNEL);
58 if (stack == 0)
59 goto out;
858259cf
BS
60
61 to_mm->id.stack = stack;
ba180fd4 62 if (current->mm != NULL && current->mm != &init_mm)
6c738ffa 63 from_mm = &current->mm->context;
9786a8f3 64
2eb5f31b 65 block_signals();
d0b5e15f
RW
66 if (from_mm)
67 to_mm->id.u.pid = copy_context_skas0(stack,
68 from_mm->id.u.pid);
69 else to_mm->id.u.pid = start_userspace(stack);
2eb5f31b 70 unblock_signals();
d0b5e15f
RW
71
72 if (to_mm->id.u.pid < 0) {
73 ret = to_mm->id.u.pid;
74 goto out_free;
858259cf
BS
75 }
76
77 ret = init_new_ldt(to_mm, from_mm);
ba180fd4
JD
78 if (ret < 0) {
79 printk(KERN_ERR "init_new_context_skas - init_ldt"
858259cf
BS
80 " failed, errno = %d\n", ret);
81 goto out_free;
d67b569f
JD
82 }
83
84 return 0;
85
86 out_free:
ba180fd4 87 if (to_mm->id.stack != 0)
858259cf 88 free_page(to_mm->id.stack);
d67b569f
JD
89 out:
90 return ret;
1da177e4
LT
91}
92
ac2aca28 93void uml_setup_stubs(struct mm_struct *mm)
3963333f 94{
3963333f
JD
95 int err, ret;
96
3963333f 97 ret = init_stub_pte(mm, STUB_CODE,
05eacfd0 98 (unsigned long) __syscall_stub_start);
3963333f
JD
99 if (ret)
100 goto out;
101
102 ret = init_stub_pte(mm, STUB_DATA, mm->context.id.stack);
103 if (ret)
104 goto out;
105
05eacfd0 106 mm->context.stub_pages[0] = virt_to_page(__syscall_stub_start);
ea6fb417 107 mm->context.stub_pages[1] = virt_to_page(mm->context.id.stack);
3963333f
JD
108
109 /* dup_mmap already holds mmap_sem */
110 err = install_special_mapping(mm, STUB_START, STUB_END - STUB_START,
111 VM_READ | VM_MAYREAD | VM_EXEC |
dee20035 112 VM_MAYEXEC | VM_DONTCOPY | VM_PFNMAP,
ea6fb417 113 mm->context.stub_pages);
3963333f
JD
114 if (err) {
115 printk(KERN_ERR "install_special_mapping returned %d\n", err);
ea6fb417 116 goto out;
3963333f
JD
117 }
118 return;
119
3963333f
JD
120out:
121 force_sigsegv(SIGSEGV, current);
122}
123
124void arch_exit_mmap(struct mm_struct *mm)
125{
126 pte_t *pte;
127
128 pte = virt_to_pte(mm, STUB_CODE);
129 if (pte != NULL)
130 pte_clear(mm, STUB_CODE, pte);
131
132 pte = virt_to_pte(mm, STUB_DATA);
133 if (pte == NULL)
134 return;
135
136 pte_clear(mm, STUB_DATA, pte);
137}
138
77bf4400 139void destroy_context(struct mm_struct *mm)
1da177e4 140{
6c738ffa 141 struct mm_context *mmu = &mm->context;
1da177e4 142
d0b5e15f
RW
143 /*
144 * If init_new_context wasn't called, this will be
145 * zero, resulting in a kill(0), which will result in the
146 * whole UML suddenly dying. Also, cover negative and
147 * 1 cases, since they shouldn't happen either.
148 */
149 if (mmu->id.u.pid < 2) {
150 printk(KERN_ERR "corrupt mm_context - pid = %d\n",
151 mmu->id.u.pid);
152 return;
00a905e6 153 }
d0b5e15f 154 os_kill_ptraced_process(mmu->id.u.pid, 1);
8b51304e 155
d0b5e15f 156 free_page(mmu->id.stack);
28078e8f 157 free_ldt(mmu);
d67b569f 158}