WIP FPC-III support
[linux/fpc-iii.git] / arch / um / kernel / skas / mmu.c
blobd9961163da6617ae4164d58ce8c921a518701f91
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 */
7 #include <linux/mm.h>
8 #include <linux/sched/signal.h>
9 #include <linux/slab.h>
11 #include <asm/pgalloc.h>
12 #include <asm/sections.h>
13 #include <as-layout.h>
14 #include <os.h>
15 #include <skas.h>
17 static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
18 unsigned long kernel)
20 pgd_t *pgd;
21 p4d_t *p4d;
22 pud_t *pud;
23 pmd_t *pmd;
24 pte_t *pte;
26 pgd = pgd_offset(mm, proc);
28 p4d = p4d_alloc(mm, pgd, proc);
29 if (!p4d)
30 goto out;
32 pud = pud_alloc(mm, p4d, proc);
33 if (!pud)
34 goto out_pud;
36 pmd = pmd_alloc(mm, pud, proc);
37 if (!pmd)
38 goto out_pmd;
40 pte = pte_alloc_map(mm, pmd, proc);
41 if (!pte)
42 goto out_pte;
44 *pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
45 *pte = pte_mkread(*pte);
46 return 0;
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;
58 int init_new_context(struct task_struct *task, struct mm_struct *mm)
60 struct mm_context *from_mm = NULL;
61 struct mm_context *to_mm = &mm->context;
62 unsigned long stack = 0;
63 int ret = -ENOMEM;
65 stack = get_zeroed_page(GFP_KERNEL);
66 if (stack == 0)
67 goto out;
69 to_mm->id.stack = stack;
70 if (current->mm != NULL && current->mm != &init_mm)
71 from_mm = &current->mm->context;
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();
80 if (to_mm->id.u.pid < 0) {
81 ret = to_mm->id.u.pid;
82 goto out_free;
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;
92 return 0;
94 out_free:
95 if (to_mm->id.stack != 0)
96 free_page(to_mm->id.stack);
97 out:
98 return ret;
101 void uml_setup_stubs(struct mm_struct *mm)
103 int err, ret;
105 ret = init_stub_pte(mm, STUB_CODE,
106 (unsigned long) __syscall_stub_start);
107 if (ret)
108 goto out;
110 ret = init_stub_pte(mm, STUB_DATA, mm->context.id.stack);
111 if (ret)
112 goto out;
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);
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;
126 return;
128 out:
129 force_sigsegv(SIGSEGV);
132 void arch_exit_mmap(struct mm_struct *mm)
134 pte_t *pte;
136 pte = virt_to_pte(mm, STUB_CODE);
137 if (pte != NULL)
138 pte_clear(mm, STUB_CODE, pte);
140 pte = virt_to_pte(mm, STUB_DATA);
141 if (pte == NULL)
142 return;
144 pte_clear(mm, STUB_DATA, pte);
147 void destroy_context(struct mm_struct *mm)
149 struct mm_context *mmu = &mm->context;
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.
157 if (mmu->id.u.pid < 2) {
158 printk(KERN_ERR "corrupt mm_context - pid = %d\n",
159 mmu->id.u.pid);
160 return;
162 os_kill_ptraced_process(mmu->id.u.pid, 1);
164 free_page(mmu->id.stack);
165 free_ldt(mmu);