xref: /linux/arch/um/kernel/skas/mmu.c (revision 001821b0e79716c4e17c71d8e053a23599a7a508)
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 <asm/mmu_context.h>
14 #include <as-layout.h>
15 #include <os.h>
16 #include <skas.h>
17 
18 int init_new_context(struct task_struct *task, struct mm_struct *mm)
19 {
20  	struct mm_context *from_mm = NULL;
21 	struct mm_context *to_mm = &mm->context;
22 	unsigned long stack = 0;
23 	int ret = -ENOMEM;
24 
25 	stack = __get_free_pages(GFP_KERNEL | __GFP_ZERO, ilog2(STUB_DATA_PAGES));
26 	if (stack == 0)
27 		goto out;
28 
29 	to_mm->id.stack = stack;
30 	if (current->mm != NULL && current->mm != &init_mm)
31 		from_mm = &current->mm->context;
32 
33 	block_signals_trace();
34 	if (from_mm)
35 		to_mm->id.u.pid = copy_context_skas0(stack,
36 						     from_mm->id.u.pid);
37 	else to_mm->id.u.pid = start_userspace(stack);
38 	unblock_signals_trace();
39 
40 	if (to_mm->id.u.pid < 0) {
41 		ret = to_mm->id.u.pid;
42 		goto out_free;
43 	}
44 
45 	ret = init_new_ldt(to_mm, from_mm);
46 	if (ret < 0) {
47 		printk(KERN_ERR "init_new_context_skas - init_ldt"
48 		       " failed, errno = %d\n", ret);
49 		goto out_free;
50 	}
51 
52 	return 0;
53 
54  out_free:
55 	if (to_mm->id.stack != 0)
56 		free_pages(to_mm->id.stack, ilog2(STUB_DATA_PAGES));
57  out:
58 	return ret;
59 }
60 
61 void destroy_context(struct mm_struct *mm)
62 {
63 	struct mm_context *mmu = &mm->context;
64 
65 	/*
66 	 * If init_new_context wasn't called, this will be
67 	 * zero, resulting in a kill(0), which will result in the
68 	 * whole UML suddenly dying.  Also, cover negative and
69 	 * 1 cases, since they shouldn't happen either.
70 	 */
71 	if (mmu->id.u.pid < 2) {
72 		printk(KERN_ERR "corrupt mm_context - pid = %d\n",
73 		       mmu->id.u.pid);
74 		return;
75 	}
76 	os_kill_ptraced_process(mmu->id.u.pid, 1);
77 
78 	free_pages(mmu->id.stack, ilog2(STUB_DATA_PAGES));
79 	free_ldt(mmu);
80 }
81