xref: /linux/arch/um/kernel/skas/mmu.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
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->pid = start_userspace(stack);
36 	unblock_signals_trace();
37 
38 	if (new_id->pid < 0) {
39 		ret = new_id->pid;
40 		goto out_free;
41 	}
42 
43 	/* Ensure the new MM is clean and nothing unwanted is mapped */
44 	unmap(new_id, 0, STUB_START);
45 
46 	return 0;
47 
48  out_free:
49 	free_pages(new_id->stack, ilog2(STUB_DATA_PAGES));
50  out:
51 	return ret;
52 }
53 
54 void destroy_context(struct mm_struct *mm)
55 {
56 	struct mm_context *mmu = &mm->context;
57 
58 	/*
59 	 * If init_new_context wasn't called, this will be
60 	 * zero, resulting in a kill(0), which will result in the
61 	 * whole UML suddenly dying.  Also, cover negative and
62 	 * 1 cases, since they shouldn't happen either.
63 	 */
64 	if (mmu->id.pid < 2) {
65 		printk(KERN_ERR "corrupt mm_context - pid = %d\n",
66 		       mmu->id.pid);
67 		return;
68 	}
69 	os_kill_ptraced_process(mmu->id.pid, 1);
70 
71 	free_pages(mmu->id.stack, ilog2(STUB_DATA_PAGES));
72 }
73