process.c 16.5 KB
Newer Older
1
/*
A
Anton Ivanov 已提交
2
 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
J
Jeff Dike 已提交
3
 * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 5 6 7 8 9
 * Licensed under the GPL
 */

#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
J
Jeff Dike 已提交
10 11
#include <errno.h>
#include <string.h>
12
#include <sys/mman.h>
J
Jeff Dike 已提交
13 14
#include <sys/wait.h>
#include <asm/unistd.h>
15 16 17 18 19 20 21 22 23
#include <as-layout.h>
#include <init.h>
#include <kern_util.h>
#include <mem.h>
#include <os.h>
#include <ptrace_user.h>
#include <registers.h>
#include <skas.h>
#include <sysdep/stub.h>
24
#include <linux/threads.h>
25 26 27

int is_skas_winch(int pid, int fd, void *data)
{
28
	return pid == getpgrp();
29 30
}

31 32
static int ptrace_dump_regs(int pid)
{
33 34
	unsigned long regs[MAX_REG_NR];
	int i;
35

36 37
	if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
		return -errno;
J
Jeff Dike 已提交
38 39 40 41

	printk(UM_KERN_ERR "Stub registers -\n");
	for (i = 0; i < ARRAY_SIZE(regs); i++)
		printk(UM_KERN_ERR "\t%d - %lx\n", i, regs[i]);
42

43
	return 0;
44 45
}

J
Jeff Dike 已提交
46 47 48 49
/*
 * Signals that are OK to receive in the stub - we'll just continue it.
 * SIGWINCH will happen when UML is inside a detached screen.
 */
A
Anton Ivanov 已提交
50
#define STUB_SIG_MASK ((1 << SIGALRM) | (1 << SIGWINCH))
J
Jeff Dike 已提交
51 52

/* Signals that the stub will finish with - anything else is an error */
J
Jeff Dike 已提交
53
#define STUB_DONE_MASK (1 << SIGTRAP)
J
Jeff Dike 已提交
54 55

void wait_stub_done(int pid)
56
{
57
	int n, status, err;
58

J
Jeff Dike 已提交
59
	while (1) {
60
		CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
J
Jeff Dike 已提交
61
		if ((n < 0) || !WIFSTOPPED(status))
J
Jeff Dike 已提交
62 63
			goto bad_wait;

J
Jeff Dike 已提交
64
		if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
J
Jeff Dike 已提交
65 66 67
			break;

		err = ptrace(PTRACE_CONT, pid, 0, 0);
68 69 70 71 72
		if (err) {
			printk(UM_KERN_ERR "wait_stub_done : continue failed, "
			       "errno = %d\n", errno);
			fatal_sigsegv();
		}
73
	}
J
Jeff Dike 已提交
74

J
Jeff Dike 已提交
75
	if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
J
Jeff Dike 已提交
76 77 78 79
		return;

bad_wait:
	err = ptrace_dump_regs(pid);
J
Jeff Dike 已提交
80 81 82
	if (err)
		printk(UM_KERN_ERR "Failed to get registers from stub, "
		       "errno = %d\n", -err);
83 84 85
	printk(UM_KERN_ERR "wait_stub_done : failed to wait for SIGTRAP, "
	       "pid = %d, n = %d, errno = %d, status = 0x%x\n", pid, n, errno,
	       status);
86
	fatal_sigsegv();
87 88 89 90
}

extern unsigned long current_stub_stack(void);

91
static void get_skas_faultinfo(int pid, struct faultinfo *fi, unsigned long *aux_fp_regs)
92 93 94
{
	int err;

95
	err = get_fp_registers(pid, aux_fp_regs);
R
Richard Weinberger 已提交
96 97 98 99
	if (err < 0) {
		printk(UM_KERN_ERR "save_fp_registers returned %d\n",
		       err);
		fatal_sigsegv();
100
	}
R
Richard Weinberger 已提交
101 102 103 104 105 106 107
	err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
	if (err) {
		printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
		       "errno = %d\n", pid, errno);
		fatal_sigsegv();
	}
	wait_stub_done(pid);
108

R
Richard Weinberger 已提交
109
	/*
110
	 * faultinfo is prepared by the stub_segv_handler at start of
R
Richard Weinberger 已提交
111 112 113
	 * the stub stack page. We just have to copy it.
	 */
	memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
J
Jeff Dike 已提交
114

115
	err = put_fp_registers(pid, aux_fp_regs);
R
Richard Weinberger 已提交
116 117 118 119
	if (err < 0) {
		printk(UM_KERN_ERR "put_fp_registers returned %d\n",
		       err);
		fatal_sigsegv();
120 121 122
	}
}

123
static void handle_segv(int pid, struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
124
{
125
	get_skas_faultinfo(pid, &regs->faultinfo, aux_fp_regs);
126
	segv(regs->faultinfo, 0, 1, NULL);
127 128
}

J
Jeff Dike 已提交
129 130 131 132 133 134
/*
 * To use the same value of using_sysemu as the caller, ask it that value
 * (in local_using_sysemu
 */
static void handle_trap(int pid, struct uml_pt_regs *regs,
			int local_using_sysemu)
135 136 137
{
	int err, status;

138 139 140
	if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
		fatal_sigsegv();

141 142
	if (!local_using_sysemu)
	{
A
Al Viro 已提交
143
		err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
144
			     __NR_getpid);
145 146 147 148 149
		if (err < 0) {
			printk(UM_KERN_ERR "handle_trap - nullifying syscall "
			       "failed, errno = %d\n", errno);
			fatal_sigsegv();
		}
150 151

		err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
152 153 154 155 156
		if (err < 0) {
			printk(UM_KERN_ERR "handle_trap - continuing to end of "
			       "syscall failed, errno = %d\n", errno);
			fatal_sigsegv();
		}
157

158
		CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
J
Jeff Dike 已提交
159
		if ((err < 0) || !WIFSTOPPED(status) ||
160 161 162 163
		    (WSTOPSIG(status) != SIGTRAP + 0x80)) {
			err = ptrace_dump_regs(pid);
			if (err)
				printk(UM_KERN_ERR "Failed to get registers "
J
Jeff Dike 已提交
164
				       "from process, errno = %d\n", -err);
165 166 167 168 169
			printk(UM_KERN_ERR "handle_trap - failed to wait at "
			       "end of syscall, errno = %d, status = %d\n",
			       errno, status);
			fatal_sigsegv();
		}
170 171 172 173 174
	}

	handle_syscall(regs);
}

175
extern char __syscall_stub_start[];
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/**
 * userspace_tramp() - userspace trampoline
 * @stack:	pointer to the new userspace stack page, can be NULL, if? FIXME:
 *
 * The userspace trampoline is used to setup a new userspace process in start_userspace() after it was clone()'ed.
 * This function will run on a temporary stack page.
 * It ptrace()'es itself, then
 * Two pages are mapped into the userspace address space:
 * - STUB_CODE (with EXEC), which contains the skas stub code
 * - 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.
 * Also for the userspace process a SIGSEGV handler is installed to catch pagefaults in the userspace process.
 * And last the process stops itself to give control to the UML kernel for this userspace process.
 *
 * Return: Always zero, otherwise the current userspace process is ended with non null exit() call
 */
192 193 194
static int userspace_tramp(void *stack)
{
	void *addr;
A
Anton Ivanov 已提交
195
	int fd;
R
Richard Weinberger 已提交
196
	unsigned long long offset;
197 198 199

	ptrace(PTRACE_TRACEME, 0, 0, 0);

200
	signal(SIGTERM, SIG_DFL);
J
Jeff Dike 已提交
201
	signal(SIGWINCH, SIG_IGN);
202

R
Richard Weinberger 已提交
203 204 205 206
	/*
	 * This has a pte, but it can't be mapped in with the usual
	 * tlb_flush mechanism because this is part of that mechanism
	 */
207
	fd = phys_mapping(to_phys(__syscall_stub_start), &offset);
R
Richard Weinberger 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220
	addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
		      PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
	if (addr == MAP_FAILED) {
		printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, "
		       "errno = %d\n", STUB_CODE, errno);
		exit(1);
	}

	if (stack != NULL) {
		fd = phys_mapping(to_phys(stack), &offset);
		addr = mmap((void *) STUB_DATA,
			    UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
			    MAP_FIXED | MAP_SHARED, fd, offset);
J
Jeff Dike 已提交
221
		if (addr == MAP_FAILED) {
R
Richard Weinberger 已提交
222 223 224
			printk(UM_KERN_ERR "mapping segfault stack "
			       "at 0x%lx failed, errno = %d\n",
			       STUB_DATA, errno);
225 226 227
			exit(1);
		}
	}
R
Richard Weinberger 已提交
228
	if (stack != NULL) {
229 230
		struct sigaction sa;

J
Jeff Dike 已提交
231
		unsigned long v = STUB_CODE +
232
				  (unsigned long) stub_segv_handler -
233
				  (unsigned long) __syscall_stub_start;
234

J
Jeff Dike 已提交
235
		set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE);
236
		sigemptyset(&sa.sa_mask);
237 238
		sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
		sa.sa_sigaction = (void *) v;
239
		sa.sa_restorer = NULL;
240 241 242 243 244
		if (sigaction(SIGSEGV, &sa, NULL) < 0) {
			printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV "
			       "handler failed - errno = %d\n", errno);
			exit(1);
		}
245 246
	}

247
	kill(os_getpid(), SIGSTOP);
J
Jeff Dike 已提交
248
	return 0;
249 250 251 252
}

int userspace_pid[NR_CPUS];

253 254 255 256 257 258 259 260 261 262 263
/**
 * start_userspace() - prepare a new userspace process
 * @stub_stack:	pointer to the stub stack. Can be NULL, if? FIXME:
 *
 * Setups a new temporary stack page that is used while userspace_tramp() runs
 * Clones the kernel process into a new userspace process, with FDs only.
 *
 * Return: When positive: the process id of the new userspace process,
 *         when negative: an error number.
 * FIXME: can PIDs become negative?!
 */
264 265 266 267
int start_userspace(unsigned long stub_stack)
{
	void *stack;
	unsigned long sp;
268
	int pid, status, n, flags, err;
269

270
	/* setup a temporary stack page */
J
Jeff Dike 已提交
271 272
	stack = mmap(NULL, UM_KERN_PAGE_SIZE,
		     PROT_READ | PROT_WRITE | PROT_EXEC,
273
		     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
274 275 276
	if (stack == MAP_FAILED) {
		err = -errno;
		printk(UM_KERN_ERR "start_userspace : mmap failed, "
J
Jeff Dike 已提交
277
		       "errno = %d\n", errno);
278 279 280
		return err;
	}

281
	/* set stack pointer to the end of the stack page, so it can grow downwards */
J
Jeff Dike 已提交
282
	sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
283

R
Richard Weinberger 已提交
284
	flags = CLONE_FILES | SIGCHLD;
J
Jeff Dike 已提交
285

286
	/* clone into new userspace process */
287
	pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
288 289 290
	if (pid < 0) {
		err = -errno;
		printk(UM_KERN_ERR "start_userspace : clone failed, "
J
Jeff Dike 已提交
291
		       "errno = %d\n", errno);
292 293
		return err;
	}
294 295

	do {
296
		CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
297 298 299
		if (n < 0) {
			err = -errno;
			printk(UM_KERN_ERR "start_userspace : wait failed, "
J
Jeff Dike 已提交
300
			       "errno = %d\n", errno);
301 302
			goto out_kill;
		}
A
Anton Ivanov 已提交
303
	} while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGALRM));
304

305 306 307
	if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
		err = -EINVAL;
		printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got "
J
Jeff Dike 已提交
308
		       "status = %d\n", status);
309 310
		goto out_kill;
	}
311

J
Jeff Dike 已提交
312
	if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
313 314 315 316 317 318
		   (void *) PTRACE_O_TRACESYSGOOD) < 0) {
		err = -errno;
		printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS "
		       "failed, errno = %d\n", errno);
		goto out_kill;
	}
319

320 321 322 323 324 325
	if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
		err = -errno;
		printk(UM_KERN_ERR "start_userspace : munmap failed, "
		       "errno = %d\n", errno);
		goto out_kill;
	}
326

J
Jeff Dike 已提交
327
	return pid;
328 329 330 331

 out_kill:
	os_kill_ptraced_process(pid, 1);
	return err;
332 333
}

334
void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
335 336
{
	int err, status, op, pid = userspace_pid[0];
J
Jeff Dike 已提交
337 338
	/* To prevent races if using_sysemu changes under us.*/
	int local_using_sysemu;
339
	siginfo_t si;
340

341 342 343
	/* Handle any immediate reschedules or signals */
	interrupt_end();

J
Jeff Dike 已提交
344
	while (1) {
A
Anton Ivanov 已提交
345

346 347 348 349 350 351 352 353
		/*
		 * This can legitimately fail if the process loads a
		 * bogus value into a segment register.  It will
		 * segfault and PTRACE_GETREGS will read that value
		 * out of the process.  However, PTRACE_SETREGS will
		 * fail.  In this case, there is nothing to do but
		 * just kill the process.
		 */
354 355 356
		if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp)) {
			printk(UM_KERN_ERR "userspace - ptrace set regs "
			       "failed, errno = %d\n", errno);
357
			fatal_sigsegv();
358
		}
359

360 361 362
		if (put_fp_registers(pid, regs->fp)) {
			printk(UM_KERN_ERR "userspace - ptrace set fp regs "
			       "failed, errno = %d\n", errno);
363
			fatal_sigsegv();
364
		}
365

366 367 368
		/* Now we set local_using_sysemu to be used for one loop */
		local_using_sysemu = get_using_sysemu();

J
Jeff Dike 已提交
369 370
		op = SELECT_PTRACE_OPERATION(local_using_sysemu,
					     singlestepping(NULL));
371

372 373 374 375 376
		if (ptrace(op, pid, 0, 0)) {
			printk(UM_KERN_ERR "userspace - ptrace continue "
			       "failed, op = %d, errno = %d\n", op, errno);
			fatal_sigsegv();
		}
377

378
		CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
379 380 381 382 383
		if (err < 0) {
			printk(UM_KERN_ERR "userspace - wait failed, "
			       "errno = %d\n", errno);
			fatal_sigsegv();
		}
384

385
		regs->is_user = 1;
386 387 388 389 390
		if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
			printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, "
			       "errno = %d\n", errno);
			fatal_sigsegv();
		}
391

392 393 394 395 396 397
		if (get_fp_registers(pid, regs->fp)) {
			printk(UM_KERN_ERR "userspace -  get_fp_registers failed, "
			       "errno = %d\n", errno);
			fatal_sigsegv();
		}

398 399
		UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */

J
Jeff Dike 已提交
400
		if (WIFSTOPPED(status)) {
J
Jeff Dike 已提交
401
			int sig = WSTOPSIG(status);
402

R
Richard Weinberger 已提交
403
			ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si);
404

J
Jeff Dike 已提交
405
			switch (sig) {
406
			case SIGSEGV:
R
Richard Weinberger 已提交
407
				if (PTRACE_FULL_FAULTINFO) {
J
Jeff Dike 已提交
408
					get_skas_faultinfo(pid,
409
							   &regs->faultinfo, aux_fp_regs);
R
Richard Weinberger 已提交
410
					(*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si,
411
							     regs);
J
Jeff Dike 已提交
412
				}
413
				else handle_segv(pid, regs, aux_fp_regs);
414 415 416 417 418
				break;
			case SIGTRAP + 0x80:
			        handle_trap(pid, regs, local_using_sysemu);
				break;
			case SIGTRAP:
R
Richard Weinberger 已提交
419
				relay_signal(SIGTRAP, (struct siginfo *)&si, regs);
420
				break;
A
Anton Ivanov 已提交
421
			case SIGALRM:
J
Jeff Dike 已提交
422 423
				break;
			case SIGIO:
424 425 426 427
			case SIGILL:
			case SIGBUS:
			case SIGFPE:
			case SIGWINCH:
J
Jeff Dike 已提交
428
				block_signals();
R
Richard Weinberger 已提交
429
				(*sig_info[sig])(sig, (struct siginfo *)&si, regs);
J
Jeff Dike 已提交
430
				unblock_signals();
431 432
				break;
			default:
J
Jeff Dike 已提交
433
				printk(UM_KERN_ERR "userspace - child stopped "
J
Jeff Dike 已提交
434
				       "with signal %d\n", sig);
435
				fatal_sigsegv();
436 437 438 439 440
			}
			pid = userspace_pid[0];
			interrupt_end();

			/* Avoid -ERESTARTSYS handling in host */
J
Jeff Dike 已提交
441
			if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
442
				PT_SYSCALL_NR(regs->gp) = -1;
443 444 445 446
		}
	}
}

J
Jeff Dike 已提交
447
static unsigned long thread_regs[MAX_REG_NR];
448
static unsigned long thread_fp_regs[FP_SIZE];
J
Jeff Dike 已提交
449 450 451

static int __init init_thread_regs(void)
{
452
	get_safe_registers(thread_regs, thread_fp_regs);
J
Jeff Dike 已提交
453
	/* Set parent's instruction pointer to start of clone-stub */
J
Jeff Dike 已提交
454
	thread_regs[REGS_IP_INDEX] = STUB_CODE +
J
Jeff Dike 已提交
455
				(unsigned long) stub_clone_handler -
456
				(unsigned long) __syscall_stub_start;
J
Jeff Dike 已提交
457
	thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE -
J
Jeff Dike 已提交
458 459 460 461 462 463 464 465 466
		sizeof(void *);
#ifdef __SIGNAL_FRAMESIZE
	thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
#endif
	return 0;
}

__initcall(init_thread_regs);

467 468 469 470 471 472
int copy_context_skas0(unsigned long new_stack, int pid)
{
	int err;
	unsigned long current_stack = current_stub_stack();
	struct stub_data *data = (struct stub_data *) current_stack;
	struct stub_data *child_data = (struct stub_data *) new_stack;
473
	unsigned long long new_offset;
474 475
	int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);

J
Jeff Dike 已提交
476 477
	/*
	 * prepare offset and fd of child's stack as argument for parent's
478 479
	 * and child's mmap2 calls
	 */
A
Anton Ivanov 已提交
480 481 482 483
	*data = ((struct stub_data) {
			.offset	= MMAP_OFFSET(new_offset),
			.fd     = new_fd
	});
J
Jeff Dike 已提交
484

J
Jeff Dike 已提交
485
	err = ptrace_setregs(pid, thread_regs);
486 487 488 489 490 491
	if (err < 0) {
		err = -errno;
		printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS "
		       "failed, pid = %d, errno = %d\n", pid, -err);
		return err;
	}
492

493 494 495 496 497 498 499
	err = put_fp_registers(pid, thread_fp_regs);
	if (err < 0) {
		printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers "
		       "failed, pid = %d, err = %d\n", pid, err);
		return err;
	}

500 501 502
	/* set a well known return code for detection of child write failure */
	child_data->err = 12345678;

J
Jeff Dike 已提交
503 504
	/*
	 * Wait, until parent has finished its work: read child's pid from
505 506
	 * parent's stack, and check, if bad result.
	 */
J
Jeff Dike 已提交
507
	err = ptrace(PTRACE_CONT, pid, 0, 0);
508 509 510 511 512 513 514
	if (err) {
		err = -errno;
		printk(UM_KERN_ERR "Failed to continue new process, pid = %d, "
		       "errno = %d\n", pid, errno);
		return err;
	}

J
Jeff Dike 已提交
515
	wait_stub_done(pid);
516 517

	pid = data->err;
518 519 520 521 522
	if (pid < 0) {
		printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports "
		       "error %d\n", -pid);
		return pid;
	}
523

J
Jeff Dike 已提交
524 525
	/*
	 * Wait, until child has finished too: read child's result from
526 527
	 * child's stack and check it.
	 */
J
Jeff Dike 已提交
528
	wait_stub_done(pid);
529 530 531 532 533 534
	if (child_data->err != STUB_DATA) {
		printk(UM_KERN_ERR "copy_context_skas0 - stub-child reports "
		       "error %ld\n", child_data->err);
		err = child_data->err;
		goto out_kill;
	}
535 536

	if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
537 538 539 540 541 542
		   (void *)PTRACE_O_TRACESYSGOOD) < 0) {
		err = -errno;
		printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS "
		       "failed, errno = %d\n", errno);
		goto out_kill;
	}
543 544

	return pid;
545 546 547 548

 out_kill:
	os_kill_ptraced_process(pid, 1);
	return err;
549 550
}

J
Jeff Dike 已提交
551
void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
552
{
J
Jeff Dike 已提交
553
	(*buf)[0].JB_IP = (unsigned long) handler;
554 555
	(*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
		sizeof(void *);
556 557
}

558
#define INIT_JMP_NEW_THREAD 0
J
Jeff Dike 已提交
559 560 561
#define INIT_JMP_CALLBACK 1
#define INIT_JMP_HALT 2
#define INIT_JMP_REBOOT 3
562

J
Jeff Dike 已提交
563
void switch_threads(jmp_buf *me, jmp_buf *you)
564
{
J
Jeff Dike 已提交
565
	if (UML_SETJMP(me) == 0)
J
Jeff Dike 已提交
566
		UML_LONGJMP(you, 1);
567 568
}

569
static jmp_buf initial_jmpbuf;
570 571 572 573

/* XXX Make these percpu */
static void (*cb_proc)(void *arg);
static void *cb_arg;
574
static jmp_buf *cb_back;
575

J
Jeff Dike 已提交
576
int start_idle_thread(void *stack, jmp_buf *switch_buf)
577
{
J
Jeff Dike 已提交
578
	int n;
579

580
	set_handler(SIGWINCH);
581

582 583 584 585 586 587 588 589 590
	/*
	 * Can't use UML_SETJMP or UML_LONGJMP here because they save
	 * and restore signals, with the possible side-effect of
	 * trying to handle any signals which came when they were
	 * blocked, which can't be done on this stack.
	 * Signals must be blocked when jumping back here and restored
	 * after returning to the jumper.
	 */
	n = setjmp(initial_jmpbuf);
J
Jeff Dike 已提交
591
	switch (n) {
592
	case INIT_JMP_NEW_THREAD:
593
		(*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
J
Jeff Dike 已提交
594
		(*switch_buf)[0].JB_SP = (unsigned long) stack +
595
			UM_THREAD_SIZE - sizeof(void *);
596 597 598
		break;
	case INIT_JMP_CALLBACK:
		(*cb_proc)(cb_arg);
599
		longjmp(*cb_back, 1);
600 601 602
		break;
	case INIT_JMP_HALT:
		kmalloc_ok = 0;
J
Jeff Dike 已提交
603
		return 0;
604 605
	case INIT_JMP_REBOOT:
		kmalloc_ok = 0;
J
Jeff Dike 已提交
606
		return 1;
607
	default:
608 609 610
		printk(UM_KERN_ERR "Bad sigsetjmp return in "
		       "start_idle_thread - %d\n", n);
		fatal_sigsegv();
611
	}
612
	longjmp(*switch_buf, 1);
613 614 615 616
}

void initial_thread_cb_skas(void (*proc)(void *), void *arg)
{
617
	jmp_buf here;
618 619 620 621 622 623

	cb_proc = proc;
	cb_arg = arg;
	cb_back = &here;

	block_signals();
J
Jeff Dike 已提交
624
	if (UML_SETJMP(&here) == 0)
625
		UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
626 627 628 629 630 631 632 633 634 635
	unblock_signals();

	cb_proc = NULL;
	cb_arg = NULL;
	cb_back = NULL;
}

void halt_skas(void)
{
	block_signals();
636
	UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
637 638 639 640 641
}

void reboot_skas(void)
{
	block_signals();
642
	UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
643 644
}

645
void __switch_mm(struct mm_id *mm_idp)
646
{
R
Richard Weinberger 已提交
647
	userspace_pid[0] = mm_idp->u.pid;
648
}