xref: /linux/arch/um/kernel/skas/mmu.c (revision a5d2cfe749e2917266956751262328da35e0925d)
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 #include <stub-data.h>
18 
19 /* Ensure the stub_data struct covers the allocated area */
20 static_assert(sizeof(struct stub_data) == STUB_DATA_PAGES * UM_KERN_PAGE_SIZE);
21 
22 int init_new_context(struct task_struct *task, struct mm_struct *mm)
23 {
24 	struct mm_id *new_id = &mm->context.id;
25 	unsigned long stack = 0;
26 	int ret = -ENOMEM;
27 
28 	stack = __get_free_pages(GFP_KERNEL | __GFP_ZERO, ilog2(STUB_DATA_PAGES));
29 	if (stack == 0)
30 		goto out;
31 
32 	new_id->stack = stack;
33 
34 	block_signals_trace();
35 	new_id->u.pid = start_userspace(stack);
36 	unblock_signals_trace();
37 
38 	if (new_id->u.pid < 0) {
39 		ret = new_id->u.pid;
40 		goto out_free;
41 	}
42 
43 	return 0;
44 
45  out_free:
46 	if (new_id->stack != 0)
47 		free_pages(new_id->stack, ilog2(STUB_DATA_PAGES));
48  out:
49 	return ret;
50 }
51 
52 void destroy_context(struct mm_struct *mm)
53 {
54 	struct mm_context *mmu = &mm->context;
55 
56 	/*
57 	 * If init_new_context wasn't called, this will be
58 	 * zero, resulting in a kill(0), which will result in the
59 	 * whole UML suddenly dying.  Also, cover negative and
60 	 * 1 cases, since they shouldn't happen either.
61 	 */
62 	if (mmu->id.u.pid < 2) {
63 		printk(KERN_ERR "corrupt mm_context - pid = %d\n",
64 		       mmu->id.u.pid);
65 		return;
66 	}
67 	os_kill_ptraced_process(mmu->id.u.pid, 1);
68 
69 	free_pages(mmu->id.stack, ilog2(STUB_DATA_PAGES));
70 }
71