VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   33 Branches   55 Tags
97870c34b4532 (Alex Dewar         2019-08-25 10:49:18 +0100   1) // SPDX-License-Identifier: GPL-2.0
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800   2) /*
2eb5f31bc4ea2 (Anton Ivanov       2015-11-02 16:16:37 +0000   3)  * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700   4)  * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800   5)  */
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800   6) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800   7) #include <stdlib.h>
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800   8) #include <unistd.h>
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800   9) #include <sched.h>
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700  10) #include <errno.h>
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700  11) #include <string.h>
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800  12) #include <sys/mman.h>
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700  13) #include <sys/wait.h>
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700  14) #include <asm/unistd.h>
37185b3324087 (Al Viro            2012-10-08 03:27:32 +0100  15) #include <as-layout.h>
37185b3324087 (Al Viro            2012-10-08 03:27:32 +0100  16) #include <init.h>
37185b3324087 (Al Viro            2012-10-08 03:27:32 +0100  17) #include <kern_util.h>
37185b3324087 (Al Viro            2012-10-08 03:27:32 +0100  18) #include <mem.h>
37185b3324087 (Al Viro            2012-10-08 03:27:32 +0100  19) #include <os.h>
37185b3324087 (Al Viro            2012-10-08 03:27:32 +0100  20) #include <ptrace_user.h>
37185b3324087 (Al Viro            2012-10-08 03:27:32 +0100  21) #include <registers.h>
37185b3324087 (Al Viro            2012-10-08 03:27:32 +0100  22) #include <skas.h>
37185b3324087 (Al Viro            2012-10-08 03:27:32 +0100  23) #include <sysdep/stub.h>
ce4586063f1af (Richard Weinberger 2017-05-04 08:15:10 +0200  24) #include <linux/threads.h>
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800  25) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800  26) int is_skas_winch(int pid, int fd, void *data)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800  27) {
17e052093bcd2 (Al Viro            2011-08-18 20:08:19 +0100  28) 	return pid == getpgrp();
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800  29) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800  30) 
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  31) static const char *ptrace_reg_name(int idx)
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  32) {
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  33) #define R(n) case HOST_##n: return #n
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  34) 
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  35) 	switch (idx) {
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  36) #ifdef __x86_64__
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  37) 	R(BX);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  38) 	R(CX);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  39) 	R(DI);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  40) 	R(SI);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  41) 	R(DX);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  42) 	R(BP);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  43) 	R(AX);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  44) 	R(R8);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  45) 	R(R9);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  46) 	R(R10);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  47) 	R(R11);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  48) 	R(R12);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  49) 	R(R13);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  50) 	R(R14);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  51) 	R(R15);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  52) 	R(ORIG_AX);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  53) 	R(CS);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  54) 	R(SS);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  55) 	R(EFLAGS);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  56) #elif defined(__i386__)
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  57) 	R(IP);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  58) 	R(SP);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  59) 	R(EFLAGS);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  60) 	R(AX);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  61) 	R(BX);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  62) 	R(CX);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  63) 	R(DX);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  64) 	R(SI);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  65) 	R(DI);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  66) 	R(BP);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  67) 	R(CS);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  68) 	R(SS);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  69) 	R(DS);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  70) 	R(FS);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  71) 	R(ES);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  72) 	R(GS);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  73) 	R(ORIG_AX);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  74) #endif
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  75) 	}
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  76) 	return "";
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  77) }
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  78) 
f30c2c983e094 (Jeff Dike          2007-05-06 14:51:29 -0700  79) static int ptrace_dump_regs(int pid)
f30c2c983e094 (Jeff Dike          2007-05-06 14:51:29 -0700  80) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800  81) 	unsigned long regs[MAX_REG_NR];
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800  82) 	int i;
f30c2c983e094 (Jeff Dike          2007-05-06 14:51:29 -0700  83) 
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800  84) 	if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800  85) 		return -errno;
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700  86) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700  87) 	printk(UM_KERN_ERR "Stub registers -\n");
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  88) 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  89) 		const char *regname = ptrace_reg_name(i);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  90) 
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  91) 		printk(UM_KERN_ERR "\t%s\t(%2d): %lx\n", regname, i, regs[i]);
e1e22d0d9183a (Johannes Berg      2021-01-13 22:07:41 +0100  92) 	}
f30c2c983e094 (Jeff Dike          2007-05-06 14:51:29 -0700  93) 
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800  94) 	return 0;
f30c2c983e094 (Jeff Dike          2007-05-06 14:51:29 -0700  95) }
f30c2c983e094 (Jeff Dike          2007-05-06 14:51:29 -0700  96) 
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700  97) /*
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700  98)  * Signals that are OK to receive in the stub - we'll just continue it.
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700  99)  * SIGWINCH will happen when UML is inside a detached screen.
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 100)  */
2eb5f31bc4ea2 (Anton Ivanov       2015-11-02 16:16:37 +0000 101) #define STUB_SIG_MASK ((1 << SIGALRM) | (1 << SIGWINCH))
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 102) 
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 103) /* Signals that the stub will finish with - anything else is an error */
ee3d9bd4de1ed (Jeff Dike          2008-02-04 22:30:56 -0800 104) #define STUB_DONE_MASK (1 << SIGTRAP)
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 105) 
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 106) void wait_stub_done(int pid)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 107) {
ae5db6d123416 (Richard Weinberger 2014-07-20 12:56:34 +0200 108) 	int n, status, err;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 109) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 110) 	while (1) {
4dbed85a35ed3 (Stanislaw Gruszka  2007-12-17 16:19:46 -0800 111) 		CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 112) 		if ((n < 0) || !WIFSTOPPED(status))
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 113) 			goto bad_wait;
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 114) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 115) 		if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 116) 			break;
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 117) 
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 118) 		err = ptrace(PTRACE_CONT, pid, 0, 0);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 119) 		if (err) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 120) 			printk(UM_KERN_ERR "wait_stub_done : continue failed, "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 121) 			       "errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 122) 			fatal_sigsegv();
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 123) 		}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 124) 	}
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 125) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 126) 	if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 127) 		return;
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 128) 
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 129) bad_wait:
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 130) 	err = ptrace_dump_regs(pid);
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 131) 	if (err)
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 132) 		printk(UM_KERN_ERR "Failed to get registers from stub, "
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 133) 		       "errno = %d\n", -err);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 134) 	printk(UM_KERN_ERR "wait_stub_done : failed to wait for SIGTRAP, "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 135) 	       "pid = %d, n = %d, errno = %d, status = 0x%x\n", pid, n, errno,
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 136) 	       status);
ae5db6d123416 (Richard Weinberger 2014-07-20 12:56:34 +0200 137) 	fatal_sigsegv();
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 138) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 139) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 140) extern unsigned long current_stub_stack(void);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 141) 
6f602afda7275 (Thomas Meyer       2017-07-29 17:03:23 +0200 142) static void get_skas_faultinfo(int pid, struct faultinfo *fi, unsigned long *aux_fp_regs)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 143) {
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 144) 	int err;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 145) 
6f602afda7275 (Thomas Meyer       2017-07-29 17:03:23 +0200 146) 	err = get_fp_registers(pid, aux_fp_regs);
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 147) 	if (err < 0) {
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 148) 		printk(UM_KERN_ERR "save_fp_registers returned %d\n",
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 149) 		       err);
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 150) 		fatal_sigsegv();
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 151) 	}
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 152) 	err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 153) 	if (err) {
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 154) 		printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 155) 		       "errno = %d\n", pid, errno);
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 156) 		fatal_sigsegv();
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 157) 	}
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 158) 	wait_stub_done(pid);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 159) 
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 160) 	/*
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 161) 	 * faultinfo is prepared by the stub_segv_handler at start of
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 162) 	 * the stub stack page. We just have to copy it.
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 163) 	 */
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 164) 	memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
2f56debd77a8f (Jeff Dike          2008-02-23 15:23:49 -0800 165) 
6f602afda7275 (Thomas Meyer       2017-07-29 17:03:23 +0200 166) 	err = put_fp_registers(pid, aux_fp_regs);
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 167) 	if (err < 0) {
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 168) 		printk(UM_KERN_ERR "put_fp_registers returned %d\n",
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 169) 		       err);
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 170) 		fatal_sigsegv();
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 171) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 172) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 173) 
6f602afda7275 (Thomas Meyer       2017-07-29 17:03:23 +0200 174) static void handle_segv(int pid, struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 175) {
6f602afda7275 (Thomas Meyer       2017-07-29 17:03:23 +0200 176) 	get_skas_faultinfo(pid, &regs->faultinfo, aux_fp_regs);
77bf4400319db (Jeff Dike          2007-10-16 01:26:58 -0700 177) 	segv(regs->faultinfo, 0, 1, NULL);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 178) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 179) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 180) /*
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 181)  * To use the same value of using_sysemu as the caller, ask it that value
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 182)  * (in local_using_sysemu
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 183)  */
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 184) static void handle_trap(int pid, struct uml_pt_regs *regs,
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 185) 			int local_using_sysemu)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 186) {
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 187) 	int err, status;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 188) 
e06173bde0ec9 (Jeff Dike          2008-02-04 22:31:12 -0800 189) 	if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
e06173bde0ec9 (Jeff Dike          2008-02-04 22:31:12 -0800 190) 		fatal_sigsegv();
e06173bde0ec9 (Jeff Dike          2008-02-04 22:31:12 -0800 191) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 192) 	if (!local_using_sysemu)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 193) 	{
966e803ab1253 (Al Viro            2011-08-18 20:12:19 +0100 194) 		err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 195) 			     __NR_getpid);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 196) 		if (err < 0) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 197) 			printk(UM_KERN_ERR "handle_trap - nullifying syscall "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 198) 			       "failed, errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 199) 			fatal_sigsegv();
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 200) 		}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 201) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 202) 		err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 203) 		if (err < 0) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 204) 			printk(UM_KERN_ERR "handle_trap - continuing to end of "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 205) 			       "syscall failed, errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 206) 			fatal_sigsegv();
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 207) 		}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 208) 
4dbed85a35ed3 (Stanislaw Gruszka  2007-12-17 16:19:46 -0800 209) 		CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 210) 		if ((err < 0) || !WIFSTOPPED(status) ||
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 211) 		    (WSTOPSIG(status) != SIGTRAP + 0x80)) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 212) 			err = ptrace_dump_regs(pid);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 213) 			if (err)
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 214) 				printk(UM_KERN_ERR "Failed to get registers "
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 215) 				       "from process, errno = %d\n", -err);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 216) 			printk(UM_KERN_ERR "handle_trap - failed to wait at "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 217) 			       "end of syscall, errno = %d, status = %d\n",
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 218) 			       errno, status);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 219) 			fatal_sigsegv();
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 220) 		}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 221) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 222) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 223) 	handle_syscall(regs);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 224) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 225) 
5f32943b5e9a4 (Nicolas Iooss      2014-10-12 13:02:13 +0200 226) extern char __syscall_stub_start[];
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 227) 
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 228) /**
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 229)  * userspace_tramp() - userspace trampoline
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 230)  * @stack:	pointer to the new userspace stack page, can be NULL, if? FIXME:
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 231)  *
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 232)  * The userspace trampoline is used to setup a new userspace process in start_userspace() after it was clone()'ed.
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 233)  * This function will run on a temporary stack page.
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 234)  * It ptrace()'es itself, then
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 235)  * Two pages are mapped into the userspace address space:
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 236)  * - STUB_CODE (with EXEC), which contains the skas stub code
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 237)  * - STUB_DATA (with R/W), which contains a data page that is used to transfer certain data between the UML userspace process and the UML kernel.
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 238)  * Also for the userspace process a SIGSEGV handler is installed to catch pagefaults in the userspace process.
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 239)  * And last the process stops itself to give control to the UML kernel for this userspace process.
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 240)  *
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 241)  * Return: Always zero, otherwise the current userspace process is ended with non null exit() call
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 242)  */
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 243) static int userspace_tramp(void *stack)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 244) {
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 245) 	void *addr;
2eb5f31bc4ea2 (Anton Ivanov       2015-11-02 16:16:37 +0000 246) 	int fd;
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 247) 	unsigned long long offset;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 248) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 249) 	ptrace(PTRACE_TRACEME, 0, 0, 0);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 250) 
a24864a1d52a9 (Lepton Wu          2007-10-16 01:27:35 -0700 251) 	signal(SIGTERM, SIG_DFL);
ee3d9bd4de1ed (Jeff Dike          2008-02-04 22:30:56 -0800 252) 	signal(SIGWINCH, SIG_IGN);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 253) 
5f32943b5e9a4 (Nicolas Iooss      2014-10-12 13:02:13 +0200 254) 	fd = phys_mapping(to_phys(__syscall_stub_start), &offset);
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 255) 	addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 256) 		      PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 257) 	if (addr == MAP_FAILED) {
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 258) 		printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, "
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 259) 		       "errno = %d\n", STUB_CODE, errno);
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 260) 		exit(1);
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 261) 	}
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 262) 
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 263) 	if (stack != NULL) {
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 264) 		fd = phys_mapping(to_phys(stack), &offset);
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 265) 		addr = mmap((void *) STUB_DATA,
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 266) 			    UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 267) 			    MAP_FIXED | MAP_SHARED, fd, offset);
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 268) 		if (addr == MAP_FAILED) {
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 269) 			printk(UM_KERN_ERR "mapping segfault stack "
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 270) 			       "at 0x%lx failed, errno = %d\n",
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 271) 			       STUB_DATA, errno);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 272) 			exit(1);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 273) 		}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 274) 	}
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 275) 	if (stack != NULL) {
4b84c69b5f6c0 (Jeff Dike          2006-09-25 23:33:04 -0700 276) 		struct sigaction sa;
4b84c69b5f6c0 (Jeff Dike          2006-09-25 23:33:04 -0700 277) 
54ae36f24b103 (Jeff Dike          2007-10-16 01:27:33 -0700 278) 		unsigned long v = STUB_CODE +
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 279) 				  (unsigned long) stub_segv_handler -
5f32943b5e9a4 (Nicolas Iooss      2014-10-12 13:02:13 +0200 280) 				  (unsigned long) __syscall_stub_start;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 281) 
54ae36f24b103 (Jeff Dike          2007-10-16 01:27:33 -0700 282) 		set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE);
4b84c69b5f6c0 (Jeff Dike          2006-09-25 23:33:04 -0700 283) 		sigemptyset(&sa.sa_mask);
9b25fcbdc6436 (Al Viro            2011-08-18 20:04:09 +0100 284) 		sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
9b25fcbdc6436 (Al Viro            2011-08-18 20:04:09 +0100 285) 		sa.sa_sigaction = (void *) v;
4b84c69b5f6c0 (Jeff Dike          2006-09-25 23:33:04 -0700 286) 		sa.sa_restorer = NULL;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 287) 		if (sigaction(SIGSEGV, &sa, NULL) < 0) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 288) 			printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 289) 			       "handler failed - errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 290) 			exit(1);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 291) 		}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 292) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 293) 
512b6fb1c14d4 (Jeff Dike          2007-10-16 01:27:11 -0700 294) 	kill(os_getpid(), SIGSTOP);
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 295) 	return 0;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 296) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 297) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 298) int userspace_pid[NR_CPUS];
a7d48886cacf8 (Johannes Berg      2021-01-13 22:08:03 +0100 299) int kill_userspace_mm[NR_CPUS];
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 300) 
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 301) /**
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 302)  * start_userspace() - prepare a new userspace process
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 303)  * @stub_stack:	pointer to the stub stack. Can be NULL, if? FIXME:
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 304)  *
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 305)  * Setups a new temporary stack page that is used while userspace_tramp() runs
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 306)  * Clones the kernel process into a new userspace process, with FDs only.
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 307)  *
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 308)  * Return: When positive: the process id of the new userspace process,
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 309)  *         when negative: an error number.
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 310)  * FIXME: can PIDs become negative?!
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 311)  */
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 312) int start_userspace(unsigned long stub_stack)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 313) {
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 314) 	void *stack;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 315) 	unsigned long sp;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 316) 	int pid, status, n, flags, err;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 317) 
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 318) 	/* setup a temporary stack page */
c539ab73070b3 (Jeff Dike          2007-06-16 10:16:09 -0700 319) 	stack = mmap(NULL, UM_KERN_PAGE_SIZE,
c539ab73070b3 (Jeff Dike          2007-06-16 10:16:09 -0700 320) 		     PROT_READ | PROT_WRITE | PROT_EXEC,
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 321) 		     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 322) 	if (stack == MAP_FAILED) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 323) 		err = -errno;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 324) 		printk(UM_KERN_ERR "start_userspace : mmap failed, "
b54988325c4cb (Jeff Dike          2008-02-04 22:31:21 -0800 325) 		       "errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 326) 		return err;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 327) 	}
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 328) 
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 329) 	/* set stack pointer to the end of the stack page, so it can grow downwards */
558f9b2f94dbd (YiFei Zhu          2021-04-20 00:56:10 -0500 330) 	sp = (unsigned long)stack + UM_KERN_PAGE_SIZE;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 331) 
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 332) 	flags = CLONE_FILES | SIGCHLD;
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 333) 
e90998302682c (Thomas Meyer       2017-07-06 00:34:05 +0200 334) 	/* clone into new userspace process */
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 335) 	pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 336) 	if (pid < 0) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 337) 		err = -errno;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 338) 		printk(UM_KERN_ERR "start_userspace : clone failed, "
b54988325c4cb (Jeff Dike          2008-02-04 22:31:21 -0800 339) 		       "errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 340) 		return err;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 341) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 342) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 343) 	do {
4dbed85a35ed3 (Stanislaw Gruszka  2007-12-17 16:19:46 -0800 344) 		CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 345) 		if (n < 0) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 346) 			err = -errno;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 347) 			printk(UM_KERN_ERR "start_userspace : wait failed, "
b54988325c4cb (Jeff Dike          2008-02-04 22:31:21 -0800 348) 			       "errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 349) 			goto out_kill;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 350) 		}
2eb5f31bc4ea2 (Anton Ivanov       2015-11-02 16:16:37 +0000 351) 	} while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGALRM));
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 352) 
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 353) 	if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 354) 		err = -EINVAL;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 355) 		printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got "
b54988325c4cb (Jeff Dike          2008-02-04 22:31:21 -0800 356) 		       "status = %d\n", status);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 357) 		goto out_kill;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 358) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 359) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 360) 	if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 361) 		   (void *) PTRACE_O_TRACESYSGOOD) < 0) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 362) 		err = -errno;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 363) 		printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 364) 		       "failed, errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 365) 		goto out_kill;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 366) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 367) 
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 368) 	if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 369) 		err = -errno;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 370) 		printk(UM_KERN_ERR "start_userspace : munmap failed, "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 371) 		       "errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 372) 		goto out_kill;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 373) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 374) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 375) 	return pid;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 376) 
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 377)  out_kill:
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 378) 	os_kill_ptraced_process(pid, 1);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 379) 	return err;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 380) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 381) 
6f602afda7275 (Thomas Meyer       2017-07-29 17:03:23 +0200 382) void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 383) {
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 384) 	int err, status, op, pid = userspace_pid[0];
2ea5bc5e5bb51 (Jeff Dike          2007-05-10 22:22:32 -0700 385) 	/* To prevent races if using_sysemu changes under us.*/
2ea5bc5e5bb51 (Jeff Dike          2007-05-10 22:22:32 -0700 386) 	int local_using_sysemu;
d3c1cfcdb43e0 (Martin Pärtel      2012-08-02 00:49:17 +0200 387) 	siginfo_t si;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 388) 
b8a420952336e (Al Viro            2012-05-23 00:25:15 -0400 389) 	/* Handle any immediate reschedules or signals */
b8a420952336e (Al Viro            2012-05-23 00:25:15 -0400 390) 	interrupt_end();
b8a420952336e (Al Viro            2012-05-23 00:25:15 -0400 391) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 392) 	while (1) {
a7d48886cacf8 (Johannes Berg      2021-01-13 22:08:03 +0100 393) 		if (kill_userspace_mm[0])
a7d48886cacf8 (Johannes Berg      2021-01-13 22:08:03 +0100 394) 			fatal_sigsegv();
2eb5f31bc4ea2 (Anton Ivanov       2015-11-02 16:16:37 +0000 395) 
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 396) 		/*
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 397) 		 * This can legitimately fail if the process loads a
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 398) 		 * bogus value into a segment register.  It will
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 399) 		 * segfault and PTRACE_GETREGS will read that value
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 400) 		 * out of the process.  However, PTRACE_SETREGS will
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 401) 		 * fail.  In this case, there is nothing to do but
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 402) 		 * just kill the process.
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 403) 		 */
22e19c8d062cc (Thomas Meyer       2017-07-06 00:31:14 +0200 404) 		if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp)) {
22e19c8d062cc (Thomas Meyer       2017-07-06 00:31:14 +0200 405) 			printk(UM_KERN_ERR "userspace - ptrace set regs "
22e19c8d062cc (Thomas Meyer       2017-07-06 00:31:14 +0200 406) 			       "failed, errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 407) 			fatal_sigsegv();
22e19c8d062cc (Thomas Meyer       2017-07-06 00:31:14 +0200 408) 		}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 409) 
22e19c8d062cc (Thomas Meyer       2017-07-06 00:31:14 +0200 410) 		if (put_fp_registers(pid, regs->fp)) {
22e19c8d062cc (Thomas Meyer       2017-07-06 00:31:14 +0200 411) 			printk(UM_KERN_ERR "userspace - ptrace set fp regs "
22e19c8d062cc (Thomas Meyer       2017-07-06 00:31:14 +0200 412) 			       "failed, errno = %d\n", errno);
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 413) 			fatal_sigsegv();
22e19c8d062cc (Thomas Meyer       2017-07-06 00:31:14 +0200 414) 		}
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 415) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 416) 		/* Now we set local_using_sysemu to be used for one loop */
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 417) 		local_using_sysemu = get_using_sysemu();
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 418) 
2ea5bc5e5bb51 (Jeff Dike          2007-05-10 22:22:32 -0700 419) 		op = SELECT_PTRACE_OPERATION(local_using_sysemu,
2ea5bc5e5bb51 (Jeff Dike          2007-05-10 22:22:32 -0700 420) 					     singlestepping(NULL));
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 421) 
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 422) 		if (ptrace(op, pid, 0, 0)) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 423) 			printk(UM_KERN_ERR "userspace - ptrace continue "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 424) 			       "failed, op = %d, errno = %d\n", op, errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 425) 			fatal_sigsegv();
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 426) 		}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 427) 
4dbed85a35ed3 (Stanislaw Gruszka  2007-12-17 16:19:46 -0800 428) 		CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 429) 		if (err < 0) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 430) 			printk(UM_KERN_ERR "userspace - wait failed, "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 431) 			       "errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 432) 			fatal_sigsegv();
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 433) 		}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 434) 
77bf4400319db (Jeff Dike          2007-10-16 01:26:58 -0700 435) 		regs->is_user = 1;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 436) 		if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 437) 			printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 438) 			       "errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 439) 			fatal_sigsegv();
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 440) 		}
d25f2e1235aab (Jeff Dike          2008-02-04 22:30:57 -0800 441) 
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 442) 		if (get_fp_registers(pid, regs->fp)) {
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 443) 			printk(UM_KERN_ERR "userspace -  get_fp_registers failed, "
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 444) 			       "errno = %d\n", errno);
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 445) 			fatal_sigsegv();
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 446) 		}
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 447) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 448) 		UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 449) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 450) 		if (WIFSTOPPED(status)) {
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 451) 			int sig = WSTOPSIG(status);
d3c1cfcdb43e0 (Martin Pärtel      2012-08-02 00:49:17 +0200 452) 
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 453) 			/* These signal handlers need the si argument.
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 454) 			 * The SIGIO and SIGALARM handlers which constitute the
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 455) 			 * majority of invocations, do not use it.
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 456) 			 */
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 457) 			switch (sig) {
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 458) 			case SIGSEGV:
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 459) 			case SIGTRAP:
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 460) 			case SIGILL:
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 461) 			case SIGBUS:
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 462) 			case SIGFPE:
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 463) 			case SIGWINCH:
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 464) 				ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si);
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 465) 				break;
3c6ac61bc91ea (Anton Ivanov       2020-11-10 13:02:21 +0000 466) 			}
d3c1cfcdb43e0 (Martin Pärtel      2012-08-02 00:49:17 +0200 467) 
5134d8fea06ab (Jeff Dike          2008-02-08 04:22:08 -0800 468) 			switch (sig) {
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 469) 			case SIGSEGV:
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 470) 				if (PTRACE_FULL_FAULTINFO) {
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 471) 					get_skas_faultinfo(pid,
6f602afda7275 (Thomas Meyer       2017-07-29 17:03:23 +0200 472) 							   &regs->faultinfo, aux_fp_regs);
9a8c1359571c5 (Richard Weinberger 2013-07-19 11:31:36 +0200 473) 					(*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si,
d3c1cfcdb43e0 (Martin Pärtel      2012-08-02 00:49:17 +0200 474) 							     regs);
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 475) 				}
6f602afda7275 (Thomas Meyer       2017-07-29 17:03:23 +0200 476) 				else handle_segv(pid, regs, aux_fp_regs);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 477) 				break;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 478) 			case SIGTRAP + 0x80:
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 479) 			        handle_trap(pid, regs, local_using_sysemu);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 480) 				break;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 481) 			case SIGTRAP:
9a8c1359571c5 (Richard Weinberger 2013-07-19 11:31:36 +0200 482) 				relay_signal(SIGTRAP, (struct siginfo *)&si, regs);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 483) 				break;
2eb5f31bc4ea2 (Anton Ivanov       2015-11-02 16:16:37 +0000 484) 			case SIGALRM:
d2753a6d19979 (Jeff Dike          2007-10-16 01:27:25 -0700 485) 				break;
d2753a6d19979 (Jeff Dike          2007-10-16 01:27:25 -0700 486) 			case SIGIO:
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 487) 			case SIGILL:
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 488) 			case SIGBUS:
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 489) 			case SIGFPE:
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 490) 			case SIGWINCH:
0dafcbe128d2a (Johannes Berg      2019-08-23 13:16:23 +0200 491) 				block_signals_trace();
9a8c1359571c5 (Richard Weinberger 2013-07-19 11:31:36 +0200 492) 				(*sig_info[sig])(sig, (struct siginfo *)&si, regs);
0dafcbe128d2a (Johannes Berg      2019-08-23 13:16:23 +0200 493) 				unblock_signals_trace();
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 494) 				break;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 495) 			default:
96cee3044dca2 (Jeff Dike          2008-05-12 14:01:48 -0700 496) 				printk(UM_KERN_ERR "userspace - child stopped "
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 497) 				       "with signal %d\n", sig);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 498) 				fatal_sigsegv();
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 499) 			}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 500) 			pid = userspace_pid[0];
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 501) 			interrupt_end();
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 502) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 503) 			/* Avoid -ERESTARTSYS handling in host */
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 504) 			if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
18badddaa84e1 (Jeff Dike          2007-10-16 01:27:07 -0700 505) 				PT_SYSCALL_NR(regs->gp) = -1;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 506) 		}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 507) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 508) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 509) 
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 510) static unsigned long thread_regs[MAX_REG_NR];
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 511) static unsigned long thread_fp_regs[FP_SIZE];
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 512) 
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 513) static int __init init_thread_regs(void)
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 514) {
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 515) 	get_safe_registers(thread_regs, thread_fp_regs);
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 516) 	/* Set parent's instruction pointer to start of clone-stub */
54ae36f24b103 (Jeff Dike          2007-10-16 01:27:33 -0700 517) 	thread_regs[REGS_IP_INDEX] = STUB_CODE +
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 518) 				(unsigned long) stub_clone_handler -
5f32943b5e9a4 (Nicolas Iooss      2014-10-12 13:02:13 +0200 519) 				(unsigned long) __syscall_stub_start;
54ae36f24b103 (Jeff Dike          2007-10-16 01:27:33 -0700 520) 	thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE -
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 521) 		sizeof(void *);
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 522) #ifdef __SIGNAL_FRAMESIZE
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 523) 	thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 524) #endif
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 525) 	return 0;
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 526) }
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 527) 
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 528) __initcall(init_thread_regs);
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 529) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 530) int copy_context_skas0(unsigned long new_stack, int pid)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 531) {
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 532) 	int err;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 533) 	unsigned long current_stack = current_stub_stack();
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 534) 	struct stub_data *data = (struct stub_data *) current_stack;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 535) 	struct stub_data *child_data = (struct stub_data *) new_stack;
0a7675aa204ac (Jeff Dike          2007-10-16 01:27:05 -0700 536) 	unsigned long long new_offset;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 537) 	int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 538) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 539) 	/*
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 540) 	 * prepare offset and fd of child's stack as argument for parent's
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 541) 	 * and child's mmap2 calls
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 542) 	 */
2eb5f31bc4ea2 (Anton Ivanov       2015-11-02 16:16:37 +0000 543) 	*data = ((struct stub_data) {
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 544) 		.offset	= MMAP_OFFSET(new_offset),
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 545) 		.fd     = new_fd,
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 546) 		.parent_err = -ESRCH,
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 547) 		.child_err = 0,
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 548) 	});
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 549) 
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 550) 	*child_data = ((struct stub_data) {
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 551) 		.child_err = -ESRCH,
2eb5f31bc4ea2 (Anton Ivanov       2015-11-02 16:16:37 +0000 552) 	});
d2753a6d19979 (Jeff Dike          2007-10-16 01:27:25 -0700 553) 
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 554) 	err = ptrace_setregs(pid, thread_regs);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 555) 	if (err < 0) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 556) 		err = -errno;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 557) 		printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 558) 		       "failed, pid = %d, errno = %d\n", pid, -err);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 559) 		return err;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 560) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 561) 
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 562) 	err = put_fp_registers(pid, thread_fp_regs);
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 563) 	if (err < 0) {
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 564) 		printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers "
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 565) 		       "failed, pid = %d, err = %d\n", pid, err);
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 566) 		return err;
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 567) 	}
fbfe9c847edf5 (Ingo van Lil       2011-09-14 16:21:23 -0700 568) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 569) 	/*
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 570) 	 * Wait, until parent has finished its work: read child's pid from
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 571) 	 * parent's stack, and check, if bad result.
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 572) 	 */
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 573) 	err = ptrace(PTRACE_CONT, pid, 0, 0);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 574) 	if (err) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 575) 		err = -errno;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 576) 		printk(UM_KERN_ERR "Failed to continue new process, pid = %d, "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 577) 		       "errno = %d\n", pid, errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 578) 		return err;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 579) 	}
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 580) 
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 581) 	wait_stub_done(pid);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 582) 
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 583) 	pid = data->parent_err;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 584) 	if (pid < 0) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 585) 		printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 586) 		       "error %d\n", -pid);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 587) 		return pid;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 588) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 589) 
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 590) 	/*
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 591) 	 * Wait, until child has finished too: read child's result from
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 592) 	 * child's stack and check it.
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 593) 	 */
16dd07bc6404c (Jeff Dike          2007-05-06 14:51:48 -0700 594) 	wait_stub_done(pid);
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 595) 	if (child_data->child_err != STUB_DATA) {
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 596) 		printk(UM_KERN_ERR "copy_context_skas0 - stub-child %d reports "
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 597) 		       "error %ld\n", pid, data->child_err);
84b2789d61156 (Johannes Berg      2021-01-13 22:09:42 +0100 598) 		err = data->child_err;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 599) 		goto out_kill;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 600) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 601) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 602) 	if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 603) 		   (void *)PTRACE_O_TRACESYSGOOD) < 0) {
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 604) 		err = -errno;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 605) 		printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 606) 		       "failed, errno = %d\n", errno);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 607) 		goto out_kill;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 608) 	}
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 609) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 610) 	return pid;
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 611) 
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 612)  out_kill:
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 613) 	os_kill_ptraced_process(pid, 1);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 614) 	return err;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 615) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 616) 
3c9173509985b (Jeff Dike          2006-09-27 01:50:40 -0700 617) void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 618) {
3c9173509985b (Jeff Dike          2006-09-27 01:50:40 -0700 619) 	(*buf)[0].JB_IP = (unsigned long) handler;
e1a79c400a86f (Jeff Dike          2007-05-10 22:22:31 -0700 620) 	(*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
e1a79c400a86f (Jeff Dike          2007-05-10 22:22:31 -0700 621) 		sizeof(void *);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 622) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 623) 
e2216feb37f1d (Jeff Dike          2006-02-07 12:58:43 -0800 624) #define INIT_JMP_NEW_THREAD 0
3c9173509985b (Jeff Dike          2006-09-27 01:50:40 -0700 625) #define INIT_JMP_CALLBACK 1
3c9173509985b (Jeff Dike          2006-09-27 01:50:40 -0700 626) #define INIT_JMP_HALT 2
3c9173509985b (Jeff Dike          2006-09-27 01:50:40 -0700 627) #define INIT_JMP_REBOOT 3
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 628) 
3c9173509985b (Jeff Dike          2006-09-27 01:50:40 -0700 629) void switch_threads(jmp_buf *me, jmp_buf *you)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 630) {
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 631) 	if (UML_SETJMP(me) == 0)
3c9173509985b (Jeff Dike          2006-09-27 01:50:40 -0700 632) 		UML_LONGJMP(you, 1);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 633) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 634) 
ad28e029789ef (Jeff Dike          2006-04-18 22:21:41 -0700 635) static jmp_buf initial_jmpbuf;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 636) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 637) /* XXX Make these percpu */
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 638) static void (*cb_proc)(void *arg);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 639) static void *cb_arg;
ad28e029789ef (Jeff Dike          2006-04-18 22:21:41 -0700 640) static jmp_buf *cb_back;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 641) 
3c9173509985b (Jeff Dike          2006-09-27 01:50:40 -0700 642) int start_idle_thread(void *stack, jmp_buf *switch_buf)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 643) {
a5df0d1a2c0f7 (Jeff Dike          2006-07-14 00:24:02 -0700 644) 	int n;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 645) 
00361683ce562 (Al Viro            2011-08-18 20:04:39 +0100 646) 	set_handler(SIGWINCH);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 647) 
77f6af778dc23 (Jeff Dike          2007-05-06 14:51:40 -0700 648) 	/*
77f6af778dc23 (Jeff Dike          2007-05-06 14:51:40 -0700 649) 	 * Can't use UML_SETJMP or UML_LONGJMP here because they save
77f6af778dc23 (Jeff Dike          2007-05-06 14:51:40 -0700 650) 	 * and restore signals, with the possible side-effect of
77f6af778dc23 (Jeff Dike          2007-05-06 14:51:40 -0700 651) 	 * trying to handle any signals which came when they were
77f6af778dc23 (Jeff Dike          2007-05-06 14:51:40 -0700 652) 	 * blocked, which can't be done on this stack.
77f6af778dc23 (Jeff Dike          2007-05-06 14:51:40 -0700 653) 	 * Signals must be blocked when jumping back here and restored
77f6af778dc23 (Jeff Dike          2007-05-06 14:51:40 -0700 654) 	 * after returning to the jumper.
77f6af778dc23 (Jeff Dike          2007-05-06 14:51:40 -0700 655) 	 */
77f6af778dc23 (Jeff Dike          2007-05-06 14:51:40 -0700 656) 	n = setjmp(initial_jmpbuf);
5134d8fea06ab (Jeff Dike          2008-02-08 04:22:08 -0800 657) 	switch (n) {
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 658) 	case INIT_JMP_NEW_THREAD:
33bbc30654147 (Thomas Meyer       2015-03-28 09:59:46 +0100 659) 		(*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
3c9173509985b (Jeff Dike          2006-09-27 01:50:40 -0700 660) 		(*switch_buf)[0].JB_SP = (unsigned long) stack +
e1a79c400a86f (Jeff Dike          2007-05-10 22:22:31 -0700 661) 			UM_THREAD_SIZE - sizeof(void *);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 662) 		break;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 663) 	case INIT_JMP_CALLBACK:
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 664) 		(*cb_proc)(cb_arg);
77f6af778dc23 (Jeff Dike          2007-05-06 14:51:40 -0700 665) 		longjmp(*cb_back, 1);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 666) 		break;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 667) 	case INIT_JMP_HALT:
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 668) 		kmalloc_ok = 0;
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 669) 		return 0;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 670) 	case INIT_JMP_REBOOT:
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 671) 		kmalloc_ok = 0;
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 672) 		return 1;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 673) 	default:
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 674) 		printk(UM_KERN_ERR "Bad sigsetjmp return in "
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 675) 		       "start_idle_thread - %d\n", n);
3e6f2ac480ce3 (Jeff Dike          2008-02-04 22:30:58 -0800 676) 		fatal_sigsegv();
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 677) 	}
77f6af778dc23 (Jeff Dike          2007-05-06 14:51:40 -0700 678) 	longjmp(*switch_buf, 1);
7ff1e34bbdc15 (Richard Weinberger 2018-06-15 16:42:56 +0200 679) 
7ff1e34bbdc15 (Richard Weinberger 2018-06-15 16:42:56 +0200 680) 	/* unreachable */
7ff1e34bbdc15 (Richard Weinberger 2018-06-15 16:42:56 +0200 681) 	printk(UM_KERN_ERR "impossible long jump!");
7ff1e34bbdc15 (Richard Weinberger 2018-06-15 16:42:56 +0200 682) 	fatal_sigsegv();
7ff1e34bbdc15 (Richard Weinberger 2018-06-15 16:42:56 +0200 683) 	return 0;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 684) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 685) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 686) void initial_thread_cb_skas(void (*proc)(void *), void *arg)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 687) {
ad28e029789ef (Jeff Dike          2006-04-18 22:21:41 -0700 688) 	jmp_buf here;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 689) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 690) 	cb_proc = proc;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 691) 	cb_arg = arg;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 692) 	cb_back = &here;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 693) 
0dafcbe128d2a (Johannes Berg      2019-08-23 13:16:23 +0200 694) 	block_signals_trace();
ba180fd437156 (Jeff Dike          2007-10-16 01:27:00 -0700 695) 	if (UML_SETJMP(&here) == 0)
ad28e029789ef (Jeff Dike          2006-04-18 22:21:41 -0700 696) 		UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
0dafcbe128d2a (Johannes Berg      2019-08-23 13:16:23 +0200 697) 	unblock_signals_trace();
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 698) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 699) 	cb_proc = NULL;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 700) 	cb_arg = NULL;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 701) 	cb_back = NULL;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 702) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 703) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 704) void halt_skas(void)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 705) {
0dafcbe128d2a (Johannes Berg      2019-08-23 13:16:23 +0200 706) 	block_signals_trace();
ad28e029789ef (Jeff Dike          2006-04-18 22:21:41 -0700 707) 	UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 708) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 709) 
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 710) void reboot_skas(void)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 711) {
0dafcbe128d2a (Johannes Berg      2019-08-23 13:16:23 +0200 712) 	block_signals_trace();
ad28e029789ef (Jeff Dike          2006-04-18 22:21:41 -0700 713) 	UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 714) }
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 715) 
77bf4400319db (Jeff Dike          2007-10-16 01:26:58 -0700 716) void __switch_mm(struct mm_id *mm_idp)
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 717) {
d0b5e15f0c0fd (Richard Weinberger 2015-03-18 21:31:27 +0100 718) 	userspace_pid[0] = mm_idp->u.pid;
a7d48886cacf8 (Johannes Berg      2021-01-13 22:08:03 +0100 719) 	kill_userspace_mm[0] = mm_idp->kill;
abaf69773d8dd (Gennady Sharapov   2006-01-18 17:42:46 -0800 720) }