process.c 15.5 KB
Newer Older
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 4 5 6 7 8
 * Licensed under the GPL
 */

#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
J
Jeff Dike 已提交
9 10
#include <errno.h>
#include <string.h>
11
#include <sys/mman.h>
J
Jeff Dike 已提交
12 13 14 15
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <asm/unistd.h>
#include "as-layout.h"
16
#include "chan_user.h"
J
Jeff Dike 已提交
17
#include "kern_constants.h"
18
#include "mem.h"
J
Jeff Dike 已提交
19
#include "os.h"
20
#include "process.h"
J
Jeff Dike 已提交
21 22 23 24 25 26 27
#include "proc_mm.h"
#include "ptrace_user.h"
#include "registers.h"
#include "skas.h"
#include "skas_ptrace.h"
#include "user.h"
#include "sysdep/stub.h"
28 29 30

int is_skas_winch(int pid, int fd, void *data)
{
31
	if (pid != getpgrp())
J
Jeff Dike 已提交
32
		return 0;
33

J
Jeff Dike 已提交
34
	register_winch_irq(-1, fd, -1, data, 0);
J
Jeff Dike 已提交
35
	return 1;
36 37
}

38 39 40 41 42
static int ptrace_dump_regs(int pid)
{
        unsigned long regs[MAX_REG_NR];
        int i;

J
Jeff Dike 已提交
43
        if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
44
                return -errno;
J
Jeff Dike 已提交
45 46 47 48

	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]);
49 50 51 52

        return 0;
}

J
Jeff Dike 已提交
53 54 55 56 57 58 59 60 61 62
/*
 * Signals that are OK to receive in the stub - we'll just continue it.
 * SIGWINCH will happen when UML is inside a detached screen.
 */
#define STUB_SIG_MASK ((1 << SIGVTALRM) | (1 << SIGWINCH))

/* Signals that the stub will finish with - anything else is an error */
#define STUB_DONE_MASK ((1 << SIGUSR1) | (1 << SIGTRAP))

void wait_stub_done(int pid)
63 64 65
{
	int n, status, err;

J
Jeff Dike 已提交
66
	while (1) {
67
		CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
J
Jeff Dike 已提交
68
		if ((n < 0) || !WIFSTOPPED(status))
J
Jeff Dike 已提交
69 70
			goto bad_wait;

J
Jeff Dike 已提交
71
		if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
J
Jeff Dike 已提交
72 73 74
			break;

		err = ptrace(PTRACE_CONT, pid, 0, 0);
J
Jeff Dike 已提交
75
		if (err)
J
Jeff Dike 已提交
76 77
			panic("wait_stub_done : continue failed, errno = %d\n",
			      errno);
78
	}
J
Jeff Dike 已提交
79

J
Jeff Dike 已提交
80
	if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
J
Jeff Dike 已提交
81 82 83 84
		return;

bad_wait:
	err = ptrace_dump_regs(pid);
J
Jeff Dike 已提交
85 86 87
	if (err)
		printk(UM_KERN_ERR "Failed to get registers from stub, "
		       "errno = %d\n", -err);
J
Jeff Dike 已提交
88 89
	panic("wait_stub_done : failed to wait for SIGUSR1/SIGTRAP, pid = %d, "
	      "n = %d, errno = %d, status = 0x%x\n", pid, n, errno, status);
90 91 92 93 94 95 96 97
}

extern unsigned long current_stub_stack(void);

void get_skas_faultinfo(int pid, struct faultinfo * fi)
{
	int err;

J
Jeff Dike 已提交
98
	if (ptrace_faultinfo) {
99
		err = ptrace(PTRACE_FAULTINFO, pid, 0, fi);
J
Jeff Dike 已提交
100
		if (err)
101 102 103 104 105 106 107 108 109 110
			panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
			      "errno = %d\n", errno);

		/* Special handling for i386, which has different structs */
		if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo))
			memset((char *)fi + sizeof(struct ptrace_faultinfo), 0,
			       sizeof(struct faultinfo) -
			       sizeof(struct ptrace_faultinfo));
	}
	else {
J
Jeff Dike 已提交
111
		err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
J
Jeff Dike 已提交
112
		if (err)
J
Jeff Dike 已提交
113 114 115
			panic("Failed to continue stub, pid = %d, errno = %d\n",
			      pid, errno);
		wait_stub_done(pid);
116

J
Jeff Dike 已提交
117 118
		/*
		 * faultinfo is prepared by the stub-segv-handler at start of
119 120 121 122 123 124
		 * the stub stack page. We just have to copy it.
		 */
		memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
	}
}

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

J
Jeff Dike 已提交
131 132 133 134 135 136
/*
 * 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)
137 138 139 140
{
	int err, status;

	/* Mark this as a syscall */
141
	UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->gp);
142 143 144 145 146

	if (!local_using_sysemu)
	{
		err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
			     __NR_getpid);
J
Jeff Dike 已提交
147 148 149
		if (err < 0)
			panic("handle_trap - nullifying syscall failed, "
			      "errno = %d\n", errno);
150 151

		err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
J
Jeff Dike 已提交
152 153 154
		if (err < 0)
			panic("handle_trap - continuing to end of syscall "
			      "failed, errno = %d\n", errno);
155 156

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

	handle_syscall(regs);
}

extern int __syscall_stub_start;

static int userspace_tramp(void *stack)
{
	void *addr;
J
Jeff Dike 已提交
176
	int err;
177 178 179

	ptrace(PTRACE_TRACEME, 0, 0, 0);

180
	init_new_thread_signals();
J
Jeff Dike 已提交
181
	err = set_interval();
J
Jeff Dike 已提交
182
	if (err)
J
Jeff Dike 已提交
183 184
		panic("userspace_tramp - setting timer failed, errno = %d\n",
		      err);
185

J
Jeff Dike 已提交
186 187 188
	if (!proc_mm) {
		/*
		 * This has a pte, but it can't be mapped in with the usual
189 190
		 * tlb_flush mechanism because this is part of that mechanism
		 */
J
Jeff Dike 已提交
191
		int fd;
J
Jeff Dike 已提交
192
		unsigned long long offset;
J
Jeff Dike 已提交
193
		fd = phys_mapping(to_phys(&__syscall_stub_start), &offset);
J
Jeff Dike 已提交
194
		addr = mmap64((void *) UML_CONFIG_STUB_CODE, UM_KERN_PAGE_SIZE,
J
Jeff Dike 已提交
195
			      PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
J
Jeff Dike 已提交
196 197 198
		if (addr == MAP_FAILED) {
			printk(UM_KERN_ERR "mapping mmap stub failed, "
			       "errno = %d\n", errno);
199 200 201
			exit(1);
		}

J
Jeff Dike 已提交
202
		if (stack != NULL) {
203
			fd = phys_mapping(to_phys(stack), &offset);
J
Jeff Dike 已提交
204 205
			addr = mmap((void *) UML_CONFIG_STUB_DATA,
				    UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
206
				    MAP_FIXED | MAP_SHARED, fd, offset);
J
Jeff Dike 已提交
207 208 209
			if (addr == MAP_FAILED) {
				printk(UM_KERN_ERR "mapping segfault stack "
				       "failed, errno = %d\n", errno);
210 211 212 213
				exit(1);
			}
		}
	}
J
Jeff Dike 已提交
214
	if (!ptrace_faultinfo && (stack != NULL)) {
215 216
		struct sigaction sa;

217 218 219 220
		unsigned long v = UML_CONFIG_STUB_CODE +
				  (unsigned long) stub_segv_handler -
				  (unsigned long) &__syscall_stub_start;

J
Jeff Dike 已提交
221
		set_sigstack((void *) UML_CONFIG_STUB_DATA, UM_KERN_PAGE_SIZE);
222 223 224 225 226 227 228 229 230
		sigemptyset(&sa.sa_mask);
		sigaddset(&sa.sa_mask, SIGIO);
		sigaddset(&sa.sa_mask, SIGWINCH);
		sigaddset(&sa.sa_mask, SIGALRM);
		sigaddset(&sa.sa_mask, SIGVTALRM);
		sigaddset(&sa.sa_mask, SIGUSR1);
		sa.sa_flags = SA_ONSTACK;
		sa.sa_handler = (void *) v;
		sa.sa_restorer = NULL;
J
Jeff Dike 已提交
231
		if (sigaction(SIGSEGV, &sa, NULL) < 0)
232 233
			panic("userspace_tramp - setting SIGSEGV handler "
			      "failed - errno = %d\n", errno);
234 235
	}

236
	kill(os_getpid(), SIGSTOP);
J
Jeff Dike 已提交
237
	return 0;
238 239 240 241 242 243 244 245 246 247 248 249 250
}

/* Each element set once, and only accessed by a single processor anyway */
#undef NR_CPUS
#define NR_CPUS 1
int userspace_pid[NR_CPUS];

int start_userspace(unsigned long stub_stack)
{
	void *stack;
	unsigned long sp;
	int pid, status, n, flags;

J
Jeff Dike 已提交
251 252
	stack = mmap(NULL, UM_KERN_PAGE_SIZE,
		     PROT_READ | PROT_WRITE | PROT_EXEC,
253
		     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
J
Jeff Dike 已提交
254
	if (stack == MAP_FAILED)
255
		panic("start_userspace : mmap failed, errno = %d", errno);
J
Jeff Dike 已提交
256
	sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
257 258

	flags = CLONE_FILES | SIGCHLD;
J
Jeff Dike 已提交
259 260 261
	if (proc_mm)
		flags |= CLONE_VM;

262
	pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
J
Jeff Dike 已提交
263
	if (pid < 0)
264 265 266 267
		panic("start_userspace : clone failed, errno = %d", errno);

	do {
		CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
J
Jeff Dike 已提交
268
		if (n < 0)
269 270
			panic("start_userspace : wait failed, errno = %d",
			      errno);
J
Jeff Dike 已提交
271
	} while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
272

J
Jeff Dike 已提交
273
	if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
274 275 276
		panic("start_userspace : expected SIGSTOP, got status = %d",
		      status);

J
Jeff Dike 已提交
277 278 279 280
	if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
		   (void *) PTRACE_O_TRACESYSGOOD) < 0)
		panic("start_userspace : PTRACE_OLDSETOPTIONS failed, "
		      "errno = %d\n", errno);
281

J
Jeff Dike 已提交
282
	if (munmap(stack, UM_KERN_PAGE_SIZE) < 0)
283 284
		panic("start_userspace : munmap failed, errno = %d\n", errno);

J
Jeff Dike 已提交
285
	return pid;
286 287
}

288
void userspace(struct uml_pt_regs *regs)
289 290
{
	int err, status, op, pid = userspace_pid[0];
J
Jeff Dike 已提交
291 292
	/* To prevent races if using_sysemu changes under us.*/
	int local_using_sysemu;
293

J
Jeff Dike 已提交
294
	while (1) {
295 296 297 298 299
		restore_registers(pid, regs);

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

J
Jeff Dike 已提交
300 301
		op = SELECT_PTRACE_OPERATION(local_using_sysemu,
					     singlestepping(NULL));
302 303

		err = ptrace(op, pid, 0, 0);
J
Jeff Dike 已提交
304
		if (err)
305 306
			panic("userspace - could not resume userspace process, "
			      "pid=%d, ptrace operation = %d, errno = %d\n",
307
			      pid, op, errno);
308 309

		CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
J
Jeff Dike 已提交
310
		if (err < 0)
311 312 313
			panic("userspace - waitpid failed, errno = %d\n",
			      errno);

314
		regs->is_user = 1;
315 316 317
		save_registers(pid, regs);
		UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */

J
Jeff Dike 已提交
318
		if (WIFSTOPPED(status)) {
J
Jeff Dike 已提交
319
			int sig = WSTOPSIG(status);
J
Jeff Dike 已提交
320
		  	switch(sig) {
321
			case SIGSEGV:
J
Jeff Dike 已提交
322 323 324 325
				if (PTRACE_FULL_FAULTINFO ||
				    !ptrace_faultinfo) {
					get_skas_faultinfo(pid,
							   &regs->faultinfo);
J
Jeff Dike 已提交
326 327
					(*sig_info[SIGSEGV])(SIGSEGV, regs);
				}
328 329 330 331 332 333 334 335 336 337 338 339 340 341
				else handle_segv(pid, regs);
				break;
			case SIGTRAP + 0x80:
			        handle_trap(pid, regs, local_using_sysemu);
				break;
			case SIGTRAP:
				relay_signal(SIGTRAP, regs);
				break;
			case SIGIO:
			case SIGVTALRM:
			case SIGILL:
			case SIGBUS:
			case SIGFPE:
			case SIGWINCH:
J
Jeff Dike 已提交
342 343 344
				block_signals();
				(*sig_info[sig])(sig, regs);
				unblock_signals();
345 346
				break;
			default:
J
Jeff Dike 已提交
347 348
			        printk(UM_KERN_ERR "userspace - child stopped "
				       "with signal %d\n", sig);
349 350 351 352 353
			}
			pid = userspace_pid[0];
			interrupt_end();

			/* Avoid -ERESTARTSYS handling in host */
J
Jeff Dike 已提交
354
			if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
355
				PT_SYSCALL_NR(regs->gp) = -1;
356 357 358 359
		}
	}
}

J
Jeff Dike 已提交
360 361 362 363
static unsigned long thread_regs[MAX_REG_NR];

static int __init init_thread_regs(void)
{
J
Jeff Dike 已提交
364
	get_safe_registers(thread_regs);
J
Jeff Dike 已提交
365 366 367 368
	/* Set parent's instruction pointer to start of clone-stub */
	thread_regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
				(unsigned long) stub_clone_handler -
				(unsigned long) &__syscall_stub_start;
J
Jeff Dike 已提交
369
	thread_regs[REGS_SP_INDEX] = UML_CONFIG_STUB_DATA + UM_KERN_PAGE_SIZE -
J
Jeff Dike 已提交
370 371 372 373 374 375 376 377 378
		sizeof(void *);
#ifdef __SIGNAL_FRAMESIZE
	thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
#endif
	return 0;
}

__initcall(init_thread_regs);

379 380 381 382 383 384
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;
385
	unsigned long long new_offset;
386 387
	int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);

J
Jeff Dike 已提交
388 389
	/*
	 * prepare offset and fd of child's stack as argument for parent's
390 391 392 393 394
	 * and child's mmap2 calls
	 */
	*data = ((struct stub_data) { .offset	= MMAP_OFFSET(new_offset),
				      .fd	= new_fd,
				      .timer    = ((struct itimerval)
J
Jeff Dike 已提交
395 396 397
					            { { 0, 1000000 / UM_HZ },
						      { 0, 1000000 / UM_HZ }})
				 });
J
Jeff Dike 已提交
398
	err = ptrace_setregs(pid, thread_regs);
J
Jeff Dike 已提交
399
	if (err < 0)
400
		panic("copy_context_skas0 : PTRACE_SETREGS failed, "
J
Jeff Dike 已提交
401
		      "pid = %d, errno = %d\n", pid, -err);
402 403 404 405

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

J
Jeff Dike 已提交
406 407
	/*
	 * Wait, until parent has finished its work: read child's pid from
408 409
	 * parent's stack, and check, if bad result.
	 */
J
Jeff Dike 已提交
410
	err = ptrace(PTRACE_CONT, pid, 0, 0);
J
Jeff Dike 已提交
411
	if (err)
J
Jeff Dike 已提交
412 413 414
		panic("Failed to continue new process, pid = %d, "
		      "errno = %d\n", pid, errno);
	wait_stub_done(pid);
415 416

	pid = data->err;
J
Jeff Dike 已提交
417
	if (pid < 0)
418
		panic("copy_context_skas0 - stub-parent reports error %d\n",
J
Jeff Dike 已提交
419
		      -pid);
420

J
Jeff Dike 已提交
421 422
	/*
	 * Wait, until child has finished too: read child's result from
423 424
	 * child's stack and check it.
	 */
J
Jeff Dike 已提交
425
	wait_stub_done(pid);
426
	if (child_data->err != UML_CONFIG_STUB_DATA)
427
		panic("copy_context_skas0 - stub-child reports error %ld\n",
428 429 430 431 432 433 434 435 436 437 438 439
		      child_data->err);

	if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
		   (void *)PTRACE_O_TRACESYSGOOD) < 0)
		panic("copy_context_skas0 : PTRACE_OLDSETOPTIONS failed, "
		      "errno = %d\n", errno);

	return pid;
}

/*
 * This is used only, if stub pages are needed, while proc_mm is
J
Jeff Dike 已提交
440
 * available. Opening /proc/mm creates a new mm_context, which lacks
441 442 443 444 445 446 447
 * the stub-pages. Thus, we map them using /proc/mm-fd
 */
void map_stub_pages(int fd, unsigned long code,
		    unsigned long data, unsigned long stack)
{
	struct proc_mm_op mmop;
	int n;
448
	unsigned long long code_offset;
J
Jeff Dike 已提交
449 450
	int code_fd = phys_mapping(to_phys((void *) &__syscall_stub_start),
				   &code_offset);
451 452 453 454 455

	mmop = ((struct proc_mm_op) { .op        = MM_MMAP,
				      .u         =
				      { .mmap    =
					{ .addr    = code,
J
Jeff Dike 已提交
456
					  .len     = UM_KERN_PAGE_SIZE,
457 458
					  .prot    = PROT_EXEC,
					  .flags   = MAP_FIXED | MAP_PRIVATE,
J
Jeff Dike 已提交
459 460
					  .fd      = code_fd,
					  .offset  = code_offset
461
	} } });
462
	CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop)));
J
Jeff Dike 已提交
463
	if (n != sizeof(mmop)) {
464
		n = errno;
J
Jeff Dike 已提交
465 466 467
		printk(UM_KERN_ERR "mmap args - addr = 0x%lx, fd = %d, "
		       "offset = %llx\n", code, code_fd,
		       (unsigned long long) code_offset);
468
		panic("map_stub_pages : /proc/mm map for code failed, "
469
		      "err = %d\n", n);
J
Jeff Dike 已提交
470
	}
471

J
Jeff Dike 已提交
472
	if (stack) {
473
		unsigned long long map_offset;
474 475 476 477 478 479
		int map_fd = phys_mapping(to_phys((void *)stack), &map_offset);
		mmop = ((struct proc_mm_op)
				{ .op        = MM_MMAP,
				  .u         =
				  { .mmap    =
				    { .addr    = data,
J
Jeff Dike 已提交
480
				      .len     = UM_KERN_PAGE_SIZE,
481 482 483 484 485
				      .prot    = PROT_READ | PROT_WRITE,
				      .flags   = MAP_FIXED | MAP_SHARED,
				      .fd      = map_fd,
				      .offset  = map_offset
		} } });
486
		CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop)));
J
Jeff Dike 已提交
487
		if (n != sizeof(mmop))
488
			panic("map_stub_pages : /proc/mm map for data failed, "
489
			      "err = %d\n", errno);
490 491 492
	}
}

J
Jeff Dike 已提交
493
void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
494
{
J
Jeff Dike 已提交
495
	(*buf)[0].JB_IP = (unsigned long) handler;
496 497
	(*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
		sizeof(void *);
498 499
}

500
#define INIT_JMP_NEW_THREAD 0
J
Jeff Dike 已提交
501 502 503
#define INIT_JMP_CALLBACK 1
#define INIT_JMP_HALT 2
#define INIT_JMP_REBOOT 3
504

J
Jeff Dike 已提交
505
void switch_threads(jmp_buf *me, jmp_buf *you)
506
{
J
Jeff Dike 已提交
507
	if (UML_SETJMP(me) == 0)
J
Jeff Dike 已提交
508
		UML_LONGJMP(you, 1);
509 510
}

511
static jmp_buf initial_jmpbuf;
512 513 514 515

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

J
Jeff Dike 已提交
518
int start_idle_thread(void *stack, jmp_buf *switch_buf)
519
{
J
Jeff Dike 已提交
520
	int n;
521 522 523 524 525

	set_handler(SIGWINCH, (__sighandler_t) sig_handler,
		    SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGALRM,
		    SIGVTALRM, -1);

526 527 528 529 530 531 532 533 534
	/*
	 * 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 已提交
535
	switch(n) {
536
	case INIT_JMP_NEW_THREAD:
J
Jeff Dike 已提交
537 538
		(*switch_buf)[0].JB_IP = (unsigned long) new_thread_handler;
		(*switch_buf)[0].JB_SP = (unsigned long) stack +
539
			UM_THREAD_SIZE - sizeof(void *);
540 541 542
		break;
	case INIT_JMP_CALLBACK:
		(*cb_proc)(cb_arg);
543
		longjmp(*cb_back, 1);
544 545 546
		break;
	case INIT_JMP_HALT:
		kmalloc_ok = 0;
J
Jeff Dike 已提交
547
		return 0;
548 549
	case INIT_JMP_REBOOT:
		kmalloc_ok = 0;
J
Jeff Dike 已提交
550
		return 1;
551 552 553
	default:
		panic("Bad sigsetjmp return in start_idle_thread - %d\n", n);
	}
554
	longjmp(*switch_buf, 1);
555 556 557 558
}

void initial_thread_cb_skas(void (*proc)(void *), void *arg)
{
559
	jmp_buf here;
560 561 562 563 564 565

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

	block_signals();
J
Jeff Dike 已提交
566
	if (UML_SETJMP(&here) == 0)
567
		UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
568 569 570 571 572 573 574 575 576 577
	unblock_signals();

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

void halt_skas(void)
{
	block_signals();
578
	UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
579 580 581 582 583
}

void reboot_skas(void)
{
	block_signals();
584
	UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
585 586
}

587
void __switch_mm(struct mm_id *mm_idp)
588 589 590
{
	int err;

591
	/* FIXME: need cpu pid in __switch_mm */
J
Jeff Dike 已提交
592
	if (proc_mm) {
593 594
		err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0,
			     mm_idp->u.mm_fd);
J
Jeff Dike 已提交
595
		if (err)
596
			panic("__switch_mm - PTRACE_SWITCH_MM failed, "
597 598 599 600
			      "errno = %d\n", errno);
	}
	else userspace_pid[0] = mm_idp->u.pid;
}