1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
4 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
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>
17 static int init_stub_pte(struct mm_struct
*mm
, unsigned long proc
,
26 pgd
= pgd_offset(mm
, proc
);
28 p4d
= p4d_alloc(mm
, pgd
, proc
);
32 pud
= pud_alloc(mm
, p4d
, proc
);
36 pmd
= pmd_alloc(mm
, pud
, proc
);
40 pte
= pte_alloc_map(mm
, pmd
, proc
);
44 *pte
= mk_pte(virt_to_page(kernel
), __pgprot(_PAGE_PRESENT
));
45 *pte
= pte_mkread(*pte
);
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;
65 stack
= get_zeroed_page(GFP_KERNEL
);
69 to_mm
->id
.stack
= stack
;
70 if (current
->mm
!= NULL
&& current
->mm
!= &init_mm
)
71 from_mm
= ¤t
->mm
->context
;
73 block_signals_trace();
75 to_mm
->id
.u
.pid
= copy_context_skas0(stack
,
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
;
85 ret
= init_new_ldt(to_mm
, from_mm
);
87 printk(KERN_ERR
"init_new_context_skas - init_ldt"
88 " failed, errno = %d\n", ret
);
95 if (to_mm
->id
.stack
!= 0)
96 free_page(to_mm
->id
.stack
);
101 void uml_setup_stubs(struct mm_struct
*mm
)
105 ret
= init_stub_pte(mm
, STUB_CODE
,
106 (unsigned long) __syscall_stub_start
);
110 ret
= init_stub_pte(mm
, STUB_DATA
, mm
->context
.id
.stack
);
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
);
123 printk(KERN_ERR
"install_special_mapping returned %d\n", err
);
129 force_sigsegv(SIGSEGV
);
132 void arch_exit_mmap(struct mm_struct
*mm
)
136 pte
= virt_to_pte(mm
, STUB_CODE
);
138 pte_clear(mm
, STUB_CODE
, pte
);
140 pte
= virt_to_pte(mm
, STUB_DATA
);
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",
162 os_kill_ptraced_process(mmu
->id
.u
.pid
, 1);
164 free_page(mmu
->id
.stack
);