process.c 17.6 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"
J
Jeff Dike 已提交
18
#include "kern_util.h"
19
#include "mem.h"
J
Jeff Dike 已提交
20
#include "os.h"
21
#include "process.h"
J
Jeff Dike 已提交
22 23 24 25 26 27 28
#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"
29 30 31

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

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

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

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

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

51
	return 0;
52 53
}

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

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

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

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

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

		err = ptrace(PTRACE_CONT, pid, 0, 0);
76 77 78 79 80
		if (err) {
			printk(UM_KERN_ERR "wait_stub_done : continue failed, "
			       "errno = %d\n", errno);
			fatal_sigsegv();
		}
81
	}
J
Jeff Dike 已提交
82

J
Jeff Dike 已提交
83
	if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
J
Jeff Dike 已提交
84 85 86 87
		return;

bad_wait:
	err = ptrace_dump_regs(pid);
J
Jeff Dike 已提交
88 89 90
	if (err)
		printk(UM_KERN_ERR "Failed to get registers from stub, "
		       "errno = %d\n", -err);
91 92 93 94
	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);
	fatal_sigsegv();
95 96 97 98 99 100 101 102
}

extern unsigned long current_stub_stack(void);

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

J
Jeff Dike 已提交
103
	if (ptrace_faultinfo) {
104
		err = ptrace(PTRACE_FAULTINFO, pid, 0, fi);
105 106 107 108 109
		if (err) {
			printk(UM_KERN_ERR "get_skas_faultinfo - "
			       "PTRACE_FAULTINFO failed, errno = %d\n", errno);
			fatal_sigsegv();
		}
110 111 112 113 114 115 116 117

		/* 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 已提交
118
		err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
119 120 121 122 123
		if (err) {
			printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
			       "errno = %d\n", pid, errno);
			fatal_sigsegv();
		}
J
Jeff Dike 已提交
124
		wait_stub_done(pid);
125

J
Jeff Dike 已提交
126 127
		/*
		 * faultinfo is prepared by the stub-segv-handler at start of
128 129 130 131 132 133
		 * the stub stack page. We just have to copy it.
		 */
		memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
	}
}

134
static void handle_segv(int pid, struct uml_pt_regs * regs)
135
{
136 137
	get_skas_faultinfo(pid, &regs->faultinfo);
	segv(regs->faultinfo, 0, 1, NULL);
138 139
}

J
Jeff Dike 已提交
140 141 142 143 144 145
/*
 * 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)
146 147 148
{
	int err, status;

149 150 151
	if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
		fatal_sigsegv();

152
	/* Mark this as a syscall */
153
	UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->gp);
154 155 156 157 158

	if (!local_using_sysemu)
	{
		err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
			     __NR_getpid);
159 160 161 162 163
		if (err < 0) {
			printk(UM_KERN_ERR "handle_trap - nullifying syscall "
			       "failed, errno = %d\n", errno);
			fatal_sigsegv();
		}
164 165

		err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
166 167 168 169 170
		if (err < 0) {
			printk(UM_KERN_ERR "handle_trap - continuing to end of "
			       "syscall failed, errno = %d\n", errno);
			fatal_sigsegv();
		}
171

172
		CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
J
Jeff Dike 已提交
173
		if ((err < 0) || !WIFSTOPPED(status) ||
174 175 176 177
		    (WSTOPSIG(status) != SIGTRAP + 0x80)) {
			err = ptrace_dump_regs(pid);
			if (err)
				printk(UM_KERN_ERR "Failed to get registers "
J
Jeff Dike 已提交
178
				       "from process, errno = %d\n", -err);
179 180 181 182 183
			printk(UM_KERN_ERR "handle_trap - failed to wait at "
			       "end of syscall, errno = %d, status = %d\n",
			       errno, status);
			fatal_sigsegv();
		}
184 185 186 187 188 189 190 191 192 193
	}

	handle_syscall(regs);
}

extern int __syscall_stub_start;

static int userspace_tramp(void *stack)
{
	void *addr;
J
Jeff Dike 已提交
194
	int err;
195 196 197

	ptrace(PTRACE_TRACEME, 0, 0, 0);

198
	signal(SIGTERM, SIG_DFL);
J
Jeff Dike 已提交
199
	signal(SIGWINCH, SIG_IGN);
J
Jeff Dike 已提交
200
	err = set_interval();
201 202 203 204 205
	if (err) {
		printk(UM_KERN_ERR "userspace_tramp - setting timer failed, "
		       "errno = %d\n", err);
		exit(1);
	}
206

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

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

J
Jeff Dike 已提交
239
		unsigned long v = STUB_CODE +
240 241 242
				  (unsigned long) stub_segv_handler -
				  (unsigned long) &__syscall_stub_start;

J
Jeff Dike 已提交
243
		set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE);
244
		sigemptyset(&sa.sa_mask);
J
Jeff Dike 已提交
245
		sa.sa_flags = SA_ONSTACK | SA_NODEFER;
246 247
		sa.sa_handler = (void *) v;
		sa.sa_restorer = NULL;
248 249 250 251 252
		if (sigaction(SIGSEGV, &sa, NULL) < 0) {
			printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV "
			       "handler failed - errno = %d\n", errno);
			exit(1);
		}
253 254
	}

255
	kill(os_getpid(), SIGSTOP);
J
Jeff Dike 已提交
256
	return 0;
257 258 259 260 261 262 263 264 265 266 267
}

/* 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;
268
	int pid, status, n, flags, err;
269

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

J
Jeff Dike 已提交
280
	sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
281

282
	flags = CLONE_FILES;
J
Jeff Dike 已提交
283 284
	if (proc_mm)
		flags |= CLONE_VM;
285 286
	else
		flags |= SIGCHLD;
J
Jeff Dike 已提交
287

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

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

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

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

321 322 323 324 325 326
	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;
	}
327

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

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

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

J
Jeff Dike 已提交
343 344
	if (getitimer(ITIMER_VIRTUAL, &timer))
		printk("Failed to get itimer, errno = %d\n", errno);
J
Jeff Dike 已提交
345 346
	nsecs = timer.it_value.tv_sec * UM_NSEC_PER_SEC +
		timer.it_value.tv_usec * UM_NSEC_PER_USEC;
J
Jeff Dike 已提交
347 348
	nsecs += os_nsecs();

J
Jeff Dike 已提交
349
	while (1) {
350 351 352 353 354 355 356 357
		/*
		 * 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.
		 */
358
		if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp))
359
			fatal_sigsegv();
360 361 362 363

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

J
Jeff Dike 已提交
364 365
		op = SELECT_PTRACE_OPERATION(local_using_sysemu,
					     singlestepping(NULL));
366

367 368 369 370 371
		if (ptrace(op, pid, 0, 0)) {
			printk(UM_KERN_ERR "userspace - ptrace continue "
			       "failed, op = %d, errno = %d\n", op, errno);
			fatal_sigsegv();
		}
372

373
		CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
374 375 376 377 378
		if (err < 0) {
			printk(UM_KERN_ERR "userspace - wait failed, "
			       "errno = %d\n", errno);
			fatal_sigsegv();
		}
379

380
		regs->is_user = 1;
381 382 383 384 385
		if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
			printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, "
			       "errno = %d\n", errno);
			fatal_sigsegv();
		}
386

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

J
Jeff Dike 已提交
389
		if (WIFSTOPPED(status)) {
J
Jeff Dike 已提交
390
			int sig = WSTOPSIG(status);
J
Jeff Dike 已提交
391
		  	switch(sig) {
392
			case SIGSEGV:
J
Jeff Dike 已提交
393 394 395 396
				if (PTRACE_FULL_FAULTINFO ||
				    !ptrace_faultinfo) {
					get_skas_faultinfo(pid,
							   &regs->faultinfo);
J
Jeff Dike 已提交
397 398
					(*sig_info[SIGSEGV])(SIGSEGV, regs);
				}
399 400 401 402 403 404 405 406 407
				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 SIGVTALRM:
J
Jeff Dike 已提交
408
				now = os_nsecs();
409
				if (now < nsecs)
J
Jeff Dike 已提交
410 411 412 413
					break;
				block_signals();
				(*sig_info[sig])(sig, regs);
				unblock_signals();
J
Jeff Dike 已提交
414 415 416 417
				nsecs = timer.it_value.tv_sec *
					UM_NSEC_PER_SEC +
					timer.it_value.tv_usec *
					UM_NSEC_PER_USEC;
J
Jeff Dike 已提交
418 419 420
				nsecs += os_nsecs();
				break;
			case SIGIO:
421 422 423 424
			case SIGILL:
			case SIGBUS:
			case SIGFPE:
			case SIGWINCH:
J
Jeff Dike 已提交
425 426 427
				block_signals();
				(*sig_info[sig])(sig, regs);
				unblock_signals();
428 429
				break;
			default:
J
Jeff Dike 已提交
430 431
			        printk(UM_KERN_ERR "userspace - child stopped "
				       "with signal %d\n", sig);
432
				fatal_sigsegv();
433 434 435 436 437
			}
			pid = userspace_pid[0];
			interrupt_end();

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

J
Jeff Dike 已提交
444 445 446 447
static unsigned long thread_regs[MAX_REG_NR];

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

__initcall(init_thread_regs);

463 464
int copy_context_skas0(unsigned long new_stack, int pid)
{
J
Jeff Dike 已提交
465
	struct timeval tv = { .tv_sec = 0, .tv_usec = UM_USEC_PER_SEC / UM_HZ };
466 467 468 469
	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;
470
	unsigned long long new_offset;
471 472
	int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);

J
Jeff Dike 已提交
473 474
	/*
	 * prepare offset and fd of child's stack as argument for parent's
475 476 477 478 479
	 * and child's mmap2 calls
	 */
	*data = ((struct stub_data) { .offset	= MMAP_OFFSET(new_offset),
				      .fd	= new_fd,
				      .timer    = ((struct itimerval)
J
Jeff Dike 已提交
480 481 482
					           { .it_value = tv,
						     .it_interval = tv }) });

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

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

J
Jeff Dike 已提交
494 495
	/*
	 * Wait, until parent has finished its work: read child's pid from
496 497
	 * parent's stack, and check, if bad result.
	 */
J
Jeff Dike 已提交
498
	err = ptrace(PTRACE_CONT, pid, 0, 0);
499 500 501 502 503 504 505
	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 已提交
506
	wait_stub_done(pid);
507 508

	pid = data->err;
509 510 511 512 513
	if (pid < 0) {
		printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports "
		       "error %d\n", -pid);
		return pid;
	}
514

J
Jeff Dike 已提交
515 516
	/*
	 * Wait, until child has finished too: read child's result from
517 518
	 * child's stack and check it.
	 */
J
Jeff Dike 已提交
519
	wait_stub_done(pid);
520 521 522 523 524 525
	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;
	}
526 527

	if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
528 529 530 531 532 533
		   (void *)PTRACE_O_TRACESYSGOOD) < 0) {
		err = -errno;
		printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS "
		       "failed, errno = %d\n", errno);
		goto out_kill;
	}
534 535

	return pid;
536 537 538 539

 out_kill:
	os_kill_ptraced_process(pid, 1);
	return err;
540 541 542 543
}

/*
 * This is used only, if stub pages are needed, while proc_mm is
J
Jeff Dike 已提交
544
 * available. Opening /proc/mm creates a new mm_context, which lacks
545 546
 * the stub-pages. Thus, we map them using /proc/mm-fd
 */
547 548
int map_stub_pages(int fd, unsigned long code, unsigned long data,
		   unsigned long stack)
549 550 551
{
	struct proc_mm_op mmop;
	int n;
552
	unsigned long long code_offset;
J
Jeff Dike 已提交
553 554
	int code_fd = phys_mapping(to_phys((void *) &__syscall_stub_start),
				   &code_offset);
555 556 557 558 559

	mmop = ((struct proc_mm_op) { .op        = MM_MMAP,
				      .u         =
				      { .mmap    =
					{ .addr    = code,
J
Jeff Dike 已提交
560
					  .len     = UM_KERN_PAGE_SIZE,
561 562
					  .prot    = PROT_EXEC,
					  .flags   = MAP_FIXED | MAP_PRIVATE,
J
Jeff Dike 已提交
563 564
					  .fd      = code_fd,
					  .offset  = code_offset
565
	} } });
566
	CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop)));
J
Jeff Dike 已提交
567
	if (n != sizeof(mmop)) {
568
		n = errno;
J
Jeff Dike 已提交
569 570 571
		printk(UM_KERN_ERR "mmap args - addr = 0x%lx, fd = %d, "
		       "offset = %llx\n", code, code_fd,
		       (unsigned long long) code_offset);
572 573 574
		printk(UM_KERN_ERR "map_stub_pages : /proc/mm map for code "
		       "failed, err = %d\n", n);
		return -n;
J
Jeff Dike 已提交
575
	}
576

J
Jeff Dike 已提交
577
	if (stack) {
578
		unsigned long long map_offset;
579 580 581 582 583 584
		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 已提交
585
				      .len     = UM_KERN_PAGE_SIZE,
586 587 588 589 590
				      .prot    = PROT_READ | PROT_WRITE,
				      .flags   = MAP_FIXED | MAP_SHARED,
				      .fd      = map_fd,
				      .offset  = map_offset
		} } });
591
		CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop)));
592 593 594 595 596 597
		if (n != sizeof(mmop)) {
			n = errno;
			printk(UM_KERN_ERR "map_stub_pages : /proc/mm map for "
			       "data failed, err = %d\n", n);
			return -n;
		}
598
	}
599 600

	return 0;
601 602
}

J
Jeff Dike 已提交
603
void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
604
{
J
Jeff Dike 已提交
605
	(*buf)[0].JB_IP = (unsigned long) handler;
606 607
	(*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
		sizeof(void *);
608 609
}

610
#define INIT_JMP_NEW_THREAD 0
J
Jeff Dike 已提交
611 612 613
#define INIT_JMP_CALLBACK 1
#define INIT_JMP_HALT 2
#define INIT_JMP_REBOOT 3
614

J
Jeff Dike 已提交
615
void switch_threads(jmp_buf *me, jmp_buf *you)
616
{
J
Jeff Dike 已提交
617
	if (UML_SETJMP(me) == 0)
J
Jeff Dike 已提交
618
		UML_LONGJMP(you, 1);
619 620
}

621
static jmp_buf initial_jmpbuf;
622 623 624 625

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

J
Jeff Dike 已提交
628
int start_idle_thread(void *stack, jmp_buf *switch_buf)
629
{
J
Jeff Dike 已提交
630
	int n;
631 632

	set_handler(SIGWINCH, (__sighandler_t) sig_handler,
J
Jeff Dike 已提交
633
		    SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGVTALRM, -1);
634

635 636 637 638 639 640 641 642 643
	/*
	 * 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 已提交
644
	switch(n) {
645
	case INIT_JMP_NEW_THREAD:
J
Jeff Dike 已提交
646 647
		(*switch_buf)[0].JB_IP = (unsigned long) new_thread_handler;
		(*switch_buf)[0].JB_SP = (unsigned long) stack +
648
			UM_THREAD_SIZE - sizeof(void *);
649 650 651
		break;
	case INIT_JMP_CALLBACK:
		(*cb_proc)(cb_arg);
652
		longjmp(*cb_back, 1);
653 654 655
		break;
	case INIT_JMP_HALT:
		kmalloc_ok = 0;
J
Jeff Dike 已提交
656
		return 0;
657 658
	case INIT_JMP_REBOOT:
		kmalloc_ok = 0;
J
Jeff Dike 已提交
659
		return 1;
660
	default:
661 662 663
		printk(UM_KERN_ERR "Bad sigsetjmp return in "
		       "start_idle_thread - %d\n", n);
		fatal_sigsegv();
664
	}
665
	longjmp(*switch_buf, 1);
666 667 668 669
}

void initial_thread_cb_skas(void (*proc)(void *), void *arg)
{
670
	jmp_buf here;
671 672 673 674 675 676

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

	block_signals();
J
Jeff Dike 已提交
677
	if (UML_SETJMP(&here) == 0)
678
		UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
679 680 681 682 683 684 685 686 687 688
	unblock_signals();

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

void halt_skas(void)
{
	block_signals();
689
	UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
690 691 692 693 694
}

void reboot_skas(void)
{
	block_signals();
695
	UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
696 697
}

698
void __switch_mm(struct mm_id *mm_idp)
699 700 701
{
	int err;

702
	/* FIXME: need cpu pid in __switch_mm */
J
Jeff Dike 已提交
703
	if (proc_mm) {
704 705
		err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0,
			     mm_idp->u.mm_fd);
706 707 708 709 710
		if (err) {
			printk(UM_KERN_ERR "__switch_mm - PTRACE_SWITCH_MM "
			       "failed, errno = %d\n", errno);
			fatal_sigsegv();
		}
711 712 713
	}
	else userspace_pid[0] = mm_idp->u.pid;
}