10d1fb0a4SAlex Dewar // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
32eb5f31bSAnton Ivanov * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
4ba180fd4SJeff Dike * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
51da177e4SLinus Torvalds */
61da177e4SLinus Torvalds
737185b33SAl Viro #include <linux/mm.h>
83f07c014SIngo Molnar #include <linux/sched/signal.h>
937185b33SAl Viro #include <linux/slab.h>
103f07c014SIngo Molnar
1137185b33SAl Viro #include <asm/pgalloc.h>
12d5f20be7SNicolas Iooss #include <asm/sections.h>
1337185b33SAl Viro #include <as-layout.h>
1437185b33SAl Viro #include <os.h>
1537185b33SAl Viro #include <skas.h>
161da177e4SLinus Torvalds
init_new_context(struct task_struct * task,struct mm_struct * mm)1777bf4400SJeff Dike int init_new_context(struct task_struct *task, struct mm_struct *mm)
18d67b569fSJeff Dike {
196c738ffaSJeff Dike struct mm_context *from_mm = NULL;
206c738ffaSJeff Dike struct mm_context *to_mm = &mm->context;
218b51304eSBodo Stroesser unsigned long stack = 0;
2212919aa6SBodo Stroesser int ret = -ENOMEM;
23d67b569fSJeff Dike
24*6032aca0SJohannes Berg stack = __get_free_pages(GFP_KERNEL | __GFP_ZERO, ilog2(STUB_DATA_PAGES));
258b51304eSBodo Stroesser if (stack == 0)
268b51304eSBodo Stroesser goto out;
27858259cfSBodo Stroesser
28858259cfSBodo Stroesser to_mm->id.stack = stack;
29858259cfSBodo Stroesser if (current->mm != NULL && current->mm != &init_mm)
306c738ffaSJeff Dike from_mm = ¤t->mm->context;
319786a8f3SBodo Stroesser
320dafcbe1SJohannes Berg block_signals_trace();
33858259cfSBodo Stroesser if (from_mm)
34858259cfSBodo Stroesser to_mm->id.u.pid = copy_context_skas0(stack,
35858259cfSBodo Stroesser from_mm->id.u.pid);
36858259cfSBodo Stroesser else to_mm->id.u.pid = start_userspace(stack);
370dafcbe1SJohannes Berg unblock_signals_trace();
383e6f2ac4SJeff Dike
393e6f2ac4SJeff Dike if (to_mm->id.u.pid < 0) {
403e6f2ac4SJeff Dike ret = to_mm->id.u.pid;
413e6f2ac4SJeff Dike goto out_free;
423e6f2ac4SJeff Dike }
43858259cfSBodo Stroesser
44858259cfSBodo Stroesser ret = init_new_ldt(to_mm, from_mm);
45858259cfSBodo Stroesser if (ret < 0) {
46ba180fd4SJeff Dike printk(KERN_ERR "init_new_context_skas - init_ldt"
47858259cfSBodo Stroesser " failed, errno = %d\n", ret);
48858259cfSBodo Stroesser goto out_free;
49d67b569fSJeff Dike }
50d67b569fSJeff Dike
51d67b569fSJeff Dike return 0;
52d67b569fSJeff Dike
53d67b569fSJeff Dike out_free:
54858259cfSBodo Stroesser if (to_mm->id.stack != 0)
55*6032aca0SJohannes Berg free_pages(to_mm->id.stack, ilog2(STUB_DATA_PAGES));
56d67b569fSJeff Dike out:
57d67b569fSJeff Dike return ret;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds
destroy_context(struct mm_struct * mm)6077bf4400SJeff Dike void destroy_context(struct mm_struct *mm)
611da177e4SLinus Torvalds {
626c738ffaSJeff Dike struct mm_context *mmu = &mm->context;
631da177e4SLinus Torvalds
6400a905e6SJeff Dike /*
6500a905e6SJeff Dike * If init_new_context wasn't called, this will be
6600a905e6SJeff Dike * zero, resulting in a kill(0), which will result in the
6700a905e6SJeff Dike * whole UML suddenly dying. Also, cover negative and
6800a905e6SJeff Dike * 1 cases, since they shouldn't happen either.
6900a905e6SJeff Dike */
7000a905e6SJeff Dike if (mmu->id.u.pid < 2) {
7100a905e6SJeff Dike printk(KERN_ERR "corrupt mm_context - pid = %d\n",
7200a905e6SJeff Dike mmu->id.u.pid);
7300a905e6SJeff Dike return;
7400a905e6SJeff Dike }
75d67b569fSJeff Dike os_kill_ptraced_process(mmu->id.u.pid, 1);
768b51304eSBodo Stroesser
77*6032aca0SJohannes Berg free_pages(mmu->id.stack, ilog2(STUB_DATA_PAGES));
7828078e8fSJeff Dike free_ldt(mmu);
79d67b569fSJeff Dike }
80