1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
4 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
5 */
6
7 #include <linux/mm.h>
8 #include <linux/sched/signal.h>
9 #include <linux/slab.h>
10
11 #include <asm/pgalloc.h>
12 #include <asm/sections.h>
13 #include <as-layout.h>
14 #include <os.h>
15 #include <skas.h>
16
init_stub_pte(struct mm_struct * mm,unsigned long proc,unsigned long kernel)17 static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
18 unsigned long kernel)
19 {
20 pgd_t *pgd;
21 p4d_t *p4d;
22 pud_t *pud;
23 pmd_t *pmd;
24 pte_t *pte;
25
26 pgd = pgd_offset(mm, proc);
27
28 p4d = p4d_alloc(mm, pgd, proc);
29 if (!p4d)
30 goto out;
31
32 pud = pud_alloc(mm, p4d, proc);
33 if (!pud)
34 goto out_pud;
35
36 pmd = pmd_alloc(mm, pud, proc);
37 if (!pmd)
38 goto out_pmd;
39
40 pte = pte_alloc_map(mm, pmd, proc);
41 if (!pte)
42 goto out_pte;
43
44 *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
45 *pte = pte_mkread(*pte);
46 return 0;
47
48 out_pte:
49 pmd_free(mm, pmd);
50 out_pmd:
51 pud_free(mm, pud);
52 out_pud:
53 p4d_free(mm, p4d);
54 out:
55 return -ENOMEM;
56 }
57
init_new_context(struct task_struct * task,struct mm_struct * mm)58 int init_new_context(struct task_struct *task, struct mm_struct *mm)
59 {
60 struct mm_context *from_mm = NULL;
61 struct mm_context *to_mm = &mm->context;
62 unsigned long stack = 0;
63 int ret = -ENOMEM;
64
65 stack = get_zeroed_page(GFP_KERNEL);
66 if (stack == 0)
67 goto out;
68
69 to_mm->id.stack = stack;
70 if (current->mm != NULL && current->mm != &init_mm)
71 from_mm = ¤t->mm->context;
72
73 block_signals_trace();
74 if (from_mm)
75 to_mm->id.u.pid = copy_context_skas0(stack,
76 from_mm->id.u.pid);
77 else to_mm->id.u.pid = start_userspace(stack);
78 unblock_signals_trace();
79
80 if (to_mm->id.u.pid < 0) {
81 ret = to_mm->id.u.pid;
82 goto out_free;
83 }
84
85 ret = init_new_ldt(to_mm, from_mm);
86 if (ret < 0) {
87 printk(KERN_ERR "init_new_context_skas - init_ldt"
88 " failed, errno = %d\n", ret);
89 goto out_free;
90 }
91
92 return 0;
93
94 out_free:
95 if (to_mm->id.stack != 0)
96 free_page(to_mm->id.stack);
97 out:
98 return ret;
99 }
100
uml_setup_stubs(struct mm_struct * mm)101 void uml_setup_stubs(struct mm_struct *mm)
102 {
103 int err, ret;
104
105 ret = init_stub_pte(mm, STUB_CODE,
106 (unsigned long) __syscall_stub_start);
107 if (ret)
108 goto out;
109
110 ret = init_stub_pte(mm, STUB_DATA, mm->context.id.stack);
111 if (ret)
112 goto out;
113
114 mm->context.stub_pages[0] = virt_to_page(__syscall_stub_start);
115 mm->context.stub_pages[1] = virt_to_page(mm->context.id.stack);
116
117 /* dup_mmap already holds mmap_lock */
118 err = install_special_mapping(mm, STUB_START, STUB_END - STUB_START,
119 VM_READ | VM_MAYREAD | VM_EXEC |
120 VM_MAYEXEC | VM_DONTCOPY | VM_PFNMAP,
121 mm->context.stub_pages);
122 if (err) {
123 printk(KERN_ERR "install_special_mapping returned %d\n", err);
124 goto out;
125 }
126 return;
127
128 out:
129 force_sigsegv(SIGSEGV);
130 }
131
arch_exit_mmap(struct mm_struct * mm)132 void arch_exit_mmap(struct mm_struct *mm)
133 {
134 pte_t *pte;
135
136 pte = virt_to_pte(mm, STUB_CODE);
137 if (pte != NULL)
138 pte_clear(mm, STUB_CODE, pte);
139
140 pte = virt_to_pte(mm, STUB_DATA);
141 if (pte == NULL)
142 return;
143
144 pte_clear(mm, STUB_DATA, pte);
145 }
146
destroy_context(struct mm_struct * mm)147 void destroy_context(struct mm_struct *mm)
148 {
149 struct mm_context *mmu = &mm->context;
150
151 /*
152 * If init_new_context wasn't called, this will be
153 * zero, resulting in a kill(0), which will result in the
154 * whole UML suddenly dying. Also, cover negative and
155 * 1 cases, since they shouldn't happen either.
156 */
157 if (mmu->id.u.pid < 2) {
158 printk(KERN_ERR "corrupt mm_context - pid = %d\n",
159 mmu->id.u.pid);
160 return;
161 }
162 os_kill_ptraced_process(mmu->id.u.pid, 1);
163
164 free_page(mmu->id.stack);
165 free_ldt(mmu);
166 }
167