2 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
3 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
8 #include <linux/sched/signal.h>
9 #include <linux/slab.h>
11 #include <asm/pgalloc.h>
12 #include <asm/pgtable.h>
13 #include <asm/sections.h>
14 #include <as-layout.h>
18 static int init_stub_pte(struct mm_struct
*mm
, unsigned long proc
,
26 pgd
= pgd_offset(mm
, proc
);
27 pud
= pud_alloc(mm
, pgd
, proc
);
31 pmd
= pmd_alloc(mm
, pud
, proc
);
35 pte
= pte_alloc_map(mm
, pmd
, proc
);
39 *pte
= mk_pte(virt_to_page(kernel
), __pgprot(_PAGE_PRESENT
));
40 *pte
= pte_mkread(*pte
);
51 int init_new_context(struct task_struct
*task
, struct mm_struct
*mm
)
53 struct mm_context
*from_mm
= NULL
;
54 struct mm_context
*to_mm
= &mm
->context
;
55 unsigned long stack
= 0;
58 stack
= get_zeroed_page(GFP_KERNEL
);
62 to_mm
->id
.stack
= stack
;
63 if (current
->mm
!= NULL
&& current
->mm
!= &init_mm
)
64 from_mm
= ¤t
->mm
->context
;
68 to_mm
->id
.u
.pid
= copy_context_skas0(stack
,
70 else to_mm
->id
.u
.pid
= start_userspace(stack
);
73 if (to_mm
->id
.u
.pid
< 0) {
74 ret
= to_mm
->id
.u
.pid
;
78 ret
= init_new_ldt(to_mm
, from_mm
);
80 printk(KERN_ERR
"init_new_context_skas - init_ldt"
81 " failed, errno = %d\n", ret
);
88 if (to_mm
->id
.stack
!= 0)
89 free_page(to_mm
->id
.stack
);
94 void uml_setup_stubs(struct mm_struct
*mm
)
98 ret
= init_stub_pte(mm
, STUB_CODE
,
99 (unsigned long) __syscall_stub_start
);
103 ret
= init_stub_pte(mm
, STUB_DATA
, mm
->context
.id
.stack
);
107 mm
->context
.stub_pages
[0] = virt_to_page(__syscall_stub_start
);
108 mm
->context
.stub_pages
[1] = virt_to_page(mm
->context
.id
.stack
);
110 /* dup_mmap already holds mmap_sem */
111 err
= install_special_mapping(mm
, STUB_START
, STUB_END
- STUB_START
,
112 VM_READ
| VM_MAYREAD
| VM_EXEC
|
113 VM_MAYEXEC
| VM_DONTCOPY
| VM_PFNMAP
,
114 mm
->context
.stub_pages
);
116 printk(KERN_ERR
"install_special_mapping returned %d\n", err
);
122 force_sigsegv(SIGSEGV
, current
);
125 void arch_exit_mmap(struct mm_struct
*mm
)
129 pte
= virt_to_pte(mm
, STUB_CODE
);
131 pte_clear(mm
, STUB_CODE
, pte
);
133 pte
= virt_to_pte(mm
, STUB_DATA
);
137 pte_clear(mm
, STUB_DATA
, pte
);
140 void destroy_context(struct mm_struct
*mm
)
142 struct mm_context
*mmu
= &mm
->context
;
145 * If init_new_context wasn't called, this will be
146 * zero, resulting in a kill(0), which will result in the
147 * whole UML suddenly dying. Also, cover negative and
148 * 1 cases, since they shouldn't happen either.
150 if (mmu
->id
.u
.pid
< 2) {
151 printk(KERN_ERR
"corrupt mm_context - pid = %d\n",
155 os_kill_ptraced_process(mmu
->id
.u
.pid
, 1);
157 free_page(mmu
->id
.stack
);