1/*
2 * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
6#include <stdlib.h>
7#include <unistd.h>
8#include <sched.h>
9#include <errno.h>
10#include <string.h>
11#include <sys/mman.h>
12#include <sys/wait.h>
13#include <asm/unistd.h>
14#include <as-layout.h>
15#include <init.h>
16#include <kern_util.h>
17#include <mem.h>
18#include <os.h>
19#include <ptrace_user.h>
20#include <registers.h>
21#include <skas.h>
22#include <sysdep/stub.h>
23
24int is_skas_winch(int pid, int fd, void *data)
25{
26	return pid == getpgrp();
27}
28
29static int ptrace_dump_regs(int pid)
30{
31	unsigned long regs[MAX_REG_NR];
32	int i;
33
34	if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
35		return -errno;
36
37	printk(UM_KERN_ERR "Stub registers -\n");
38	for (i = 0; i < ARRAY_SIZE(regs); i++)
39		printk(UM_KERN_ERR "\t%d - %lx\n", i, regs[i]);
40
41	return 0;
42}
43
44/*
45 * Signals that are OK to receive in the stub - we'll just continue it.
46 * SIGWINCH will happen when UML is inside a detached screen.
47 */
48#define STUB_SIG_MASK ((1 << SIGVTALRM) | (1 << SIGWINCH))
49
50/* Signals that the stub will finish with - anything else is an error */
51#define STUB_DONE_MASK (1 << SIGTRAP)
52
53void wait_stub_done(int pid)
54{
55	int n, status, err;
56
57	while (1) {
58		CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
59		if ((n < 0) || !WIFSTOPPED(status))
60			goto bad_wait;
61
62		if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
63			break;
64
65		err = ptrace(PTRACE_CONT, pid, 0, 0);
66		if (err) {
67			printk(UM_KERN_ERR "wait_stub_done : continue failed, "
68			       "errno = %d\n", errno);
69			fatal_sigsegv();
70		}
71	}
72
73	if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
74		return;
75
76bad_wait:
77	err = ptrace_dump_regs(pid);
78	if (err)
79		printk(UM_KERN_ERR "Failed to get registers from stub, "
80		       "errno = %d\n", -err);
81	printk(UM_KERN_ERR "wait_stub_done : failed to wait for SIGTRAP, "
82	       "pid = %d, n = %d, errno = %d, status = 0x%x\n", pid, n, errno,
83	       status);
84	fatal_sigsegv();
85}
86
87extern unsigned long current_stub_stack(void);
88
89static void get_skas_faultinfo(int pid, struct faultinfo *fi)
90{
91	int err;
92	unsigned long fpregs[FP_SIZE];
93
94	err = get_fp_registers(pid, fpregs);
95	if (err < 0) {
96		printk(UM_KERN_ERR "save_fp_registers returned %d\n",
97		       err);
98		fatal_sigsegv();
99	}
100	err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
101	if (err) {
102		printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
103		       "errno = %d\n", pid, errno);
104		fatal_sigsegv();
105	}
106	wait_stub_done(pid);
107
108	/*
109	 * faultinfo is prepared by the stub-segv-handler at start of
110	 * the stub stack page. We just have to copy it.
111	 */
112	memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
113
114	err = put_fp_registers(pid, fpregs);
115	if (err < 0) {
116		printk(UM_KERN_ERR "put_fp_registers returned %d\n",
117		       err);
118		fatal_sigsegv();
119	}
120}
121
122static void handle_segv(int pid, struct uml_pt_regs * regs)
123{
124	get_skas_faultinfo(pid, &regs->faultinfo);
125	segv(regs->faultinfo, 0, 1, NULL);
126}
127
128/*
129 * To use the same value of using_sysemu as the caller, ask it that value
130 * (in local_using_sysemu
131 */
132static void handle_trap(int pid, struct uml_pt_regs *regs,
133			int local_using_sysemu)
134{
135	int err, status;
136
137	if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
138		fatal_sigsegv();
139
140	/* Mark this as a syscall */
141	UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->gp);
142
143	if (!local_using_sysemu)
144	{
145		err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
146			     __NR_getpid);
147		if (err < 0) {
148			printk(UM_KERN_ERR "handle_trap - nullifying syscall "
149			       "failed, errno = %d\n", errno);
150			fatal_sigsegv();
151		}
152
153		err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
154		if (err < 0) {
155			printk(UM_KERN_ERR "handle_trap - continuing to end of "
156			       "syscall failed, errno = %d\n", errno);
157			fatal_sigsegv();
158		}
159
160		CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
161		if ((err < 0) || !WIFSTOPPED(status) ||
162		    (WSTOPSIG(status) != SIGTRAP + 0x80)) {
163			err = ptrace_dump_regs(pid);
164			if (err)
165				printk(UM_KERN_ERR "Failed to get registers "
166				       "from process, errno = %d\n", -err);
167			printk(UM_KERN_ERR "handle_trap - failed to wait at "
168			       "end of syscall, errno = %d, status = %d\n",
169			       errno, status);
170			fatal_sigsegv();
171		}
172	}
173
174	handle_syscall(regs);
175}
176
177extern int __syscall_stub_start;
178
179static int userspace_tramp(void *stack)
180{
181	void *addr;
182	int err, fd;
183	unsigned long long offset;
184
185	ptrace(PTRACE_TRACEME, 0, 0, 0);
186
187	signal(SIGTERM, SIG_DFL);
188	signal(SIGWINCH, SIG_IGN);
189	err = set_interval();
190	if (err) {
191		printk(UM_KERN_ERR "userspace_tramp - setting timer failed, "
192		       "errno = %d\n", err);
193		exit(1);
194	}
195
196	/*
197	 * This has a pte, but it can't be mapped in with the usual
198	 * tlb_flush mechanism because this is part of that mechanism
199	 */
200	fd = phys_mapping(to_phys(&__syscall_stub_start), &offset);
201	addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
202		      PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
203	if (addr == MAP_FAILED) {
204		printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, "
205		       "errno = %d\n", STUB_CODE, errno);
206		exit(1);
207	}
208
209	if (stack != NULL) {
210		fd = phys_mapping(to_phys(stack), &offset);
211		addr = mmap((void *) STUB_DATA,
212			    UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
213			    MAP_FIXED | MAP_SHARED, fd, offset);
214		if (addr == MAP_FAILED) {
215			printk(UM_KERN_ERR "mapping segfault stack "
216			       "at 0x%lx failed, errno = %d\n",
217			       STUB_DATA, errno);
218			exit(1);
219		}
220	}
221	if (stack != NULL) {
222		struct sigaction sa;
223
224		unsigned long v = STUB_CODE +
225				  (unsigned long) stub_segv_handler -
226				  (unsigned long) &__syscall_stub_start;
227
228		set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE);
229		sigemptyset(&sa.sa_mask);
230		sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
231		sa.sa_sigaction = (void *) v;
232		sa.sa_restorer = NULL;
233		if (sigaction(SIGSEGV, &sa, NULL) < 0) {
234			printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV "
235			       "handler failed - errno = %d\n", errno);
236			exit(1);
237		}
238	}
239
240	kill(os_getpid(), SIGSTOP);
241	return 0;
242}
243
244/* Each element set once, and only accessed by a single processor anyway */
245#undef NR_CPUS
246#define NR_CPUS 1
247int userspace_pid[NR_CPUS];
248
249int start_userspace(unsigned long stub_stack)
250{
251	void *stack;
252	unsigned long sp;
253	int pid, status, n, flags, err;
254
255	stack = mmap(NULL, UM_KERN_PAGE_SIZE,
256		     PROT_READ | PROT_WRITE | PROT_EXEC,
257		     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
258	if (stack == MAP_FAILED) {
259		err = -errno;
260		printk(UM_KERN_ERR "start_userspace : mmap failed, "
261		       "errno = %d\n", errno);
262		return err;
263	}
264
265	sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
266
267	flags = CLONE_FILES | SIGCHLD;
268
269	pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
270	if (pid < 0) {
271		err = -errno;
272		printk(UM_KERN_ERR "start_userspace : clone failed, "
273		       "errno = %d\n", errno);
274		return err;
275	}
276
277	do {
278		CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
279		if (n < 0) {
280			err = -errno;
281			printk(UM_KERN_ERR "start_userspace : wait failed, "
282			       "errno = %d\n", errno);
283			goto out_kill;
284		}
285	} while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
286
287	if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
288		err = -EINVAL;
289		printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got "
290		       "status = %d\n", status);
291		goto out_kill;
292	}
293
294	if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
295		   (void *) PTRACE_O_TRACESYSGOOD) < 0) {
296		err = -errno;
297		printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS "
298		       "failed, errno = %d\n", errno);
299		goto out_kill;
300	}
301
302	if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
303		err = -errno;
304		printk(UM_KERN_ERR "start_userspace : munmap failed, "
305		       "errno = %d\n", errno);
306		goto out_kill;
307	}
308
309	return pid;
310
311 out_kill:
312	os_kill_ptraced_process(pid, 1);
313	return err;
314}
315
316void userspace(struct uml_pt_regs *regs)
317{
318	struct itimerval timer;
319	unsigned long long nsecs, now;
320	int err, status, op, pid = userspace_pid[0];
321	/* To prevent races if using_sysemu changes under us.*/
322	int local_using_sysemu;
323	siginfo_t si;
324
325	/* Handle any immediate reschedules or signals */
326	interrupt_end();
327
328	if (getitimer(ITIMER_VIRTUAL, &timer))
329		printk(UM_KERN_ERR "Failed to get itimer, errno = %d\n", errno);
330	nsecs = timer.it_value.tv_sec * UM_NSEC_PER_SEC +
331		timer.it_value.tv_usec * UM_NSEC_PER_USEC;
332	nsecs += os_nsecs();
333
334	while (1) {
335		/*
336		 * This can legitimately fail if the process loads a
337		 * bogus value into a segment register.  It will
338		 * segfault and PTRACE_GETREGS will read that value
339		 * out of the process.  However, PTRACE_SETREGS will
340		 * fail.  In this case, there is nothing to do but
341		 * just kill the process.
342		 */
343		if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp))
344			fatal_sigsegv();
345
346		if (put_fp_registers(pid, regs->fp))
347			fatal_sigsegv();
348
349		/* Now we set local_using_sysemu to be used for one loop */
350		local_using_sysemu = get_using_sysemu();
351
352		op = SELECT_PTRACE_OPERATION(local_using_sysemu,
353					     singlestepping(NULL));
354
355		if (ptrace(op, pid, 0, 0)) {
356			printk(UM_KERN_ERR "userspace - ptrace continue "
357			       "failed, op = %d, errno = %d\n", op, errno);
358			fatal_sigsegv();
359		}
360
361		CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
362		if (err < 0) {
363			printk(UM_KERN_ERR "userspace - wait failed, "
364			       "errno = %d\n", errno);
365			fatal_sigsegv();
366		}
367
368		regs->is_user = 1;
369		if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
370			printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, "
371			       "errno = %d\n", errno);
372			fatal_sigsegv();
373		}
374
375		if (get_fp_registers(pid, regs->fp)) {
376			printk(UM_KERN_ERR "userspace -  get_fp_registers failed, "
377			       "errno = %d\n", errno);
378			fatal_sigsegv();
379		}
380
381		UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
382
383		if (WIFSTOPPED(status)) {
384			int sig = WSTOPSIG(status);
385
386			ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si);
387
388			switch (sig) {
389			case SIGSEGV:
390				if (PTRACE_FULL_FAULTINFO) {
391					get_skas_faultinfo(pid,
392							   &regs->faultinfo);
393					(*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si,
394							     regs);
395				}
396				else handle_segv(pid, regs);
397				break;
398			case SIGTRAP + 0x80:
399			        handle_trap(pid, regs, local_using_sysemu);
400				break;
401			case SIGTRAP:
402				relay_signal(SIGTRAP, (struct siginfo *)&si, regs);
403				break;
404			case SIGVTALRM:
405				now = os_nsecs();
406				if (now < nsecs)
407					break;
408				block_signals();
409				(*sig_info[sig])(sig, (struct siginfo *)&si, regs);
410				unblock_signals();
411				nsecs = timer.it_value.tv_sec *
412					UM_NSEC_PER_SEC +
413					timer.it_value.tv_usec *
414					UM_NSEC_PER_USEC;
415				nsecs += os_nsecs();
416				break;
417			case SIGIO:
418			case SIGILL:
419			case SIGBUS:
420			case SIGFPE:
421			case SIGWINCH:
422				block_signals();
423				(*sig_info[sig])(sig, (struct siginfo *)&si, regs);
424				unblock_signals();
425				break;
426			default:
427				printk(UM_KERN_ERR "userspace - child stopped "
428				       "with signal %d\n", sig);
429				fatal_sigsegv();
430			}
431			pid = userspace_pid[0];
432			interrupt_end();
433
434			/* Avoid -ERESTARTSYS handling in host */
435			if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
436				PT_SYSCALL_NR(regs->gp) = -1;
437		}
438	}
439}
440
441static unsigned long thread_regs[MAX_REG_NR];
442static unsigned long thread_fp_regs[FP_SIZE];
443
444static int __init init_thread_regs(void)
445{
446	get_safe_registers(thread_regs, thread_fp_regs);
447	/* Set parent's instruction pointer to start of clone-stub */
448	thread_regs[REGS_IP_INDEX] = STUB_CODE +
449				(unsigned long) stub_clone_handler -
450				(unsigned long) &__syscall_stub_start;
451	thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE -
452		sizeof(void *);
453#ifdef __SIGNAL_FRAMESIZE
454	thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
455#endif
456	return 0;
457}
458
459__initcall(init_thread_regs);
460
461int copy_context_skas0(unsigned long new_stack, int pid)
462{
463	struct timeval tv = { .tv_sec = 0, .tv_usec = UM_USEC_PER_SEC / UM_HZ };
464	int err;
465	unsigned long current_stack = current_stub_stack();
466	struct stub_data *data = (struct stub_data *) current_stack;
467	struct stub_data *child_data = (struct stub_data *) new_stack;
468	unsigned long long new_offset;
469	int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
470
471	/*
472	 * prepare offset and fd of child's stack as argument for parent's
473	 * and child's mmap2 calls
474	 */
475	*data = ((struct stub_data) { .offset	= MMAP_OFFSET(new_offset),
476				      .fd	= new_fd,
477				      .timer    = ((struct itimerval)
478					           { .it_value = tv,
479						     .it_interval = tv }) });
480
481	err = ptrace_setregs(pid, thread_regs);
482	if (err < 0) {
483		err = -errno;
484		printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS "
485		       "failed, pid = %d, errno = %d\n", pid, -err);
486		return err;
487	}
488
489	err = put_fp_registers(pid, thread_fp_regs);
490	if (err < 0) {
491		printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers "
492		       "failed, pid = %d, err = %d\n", pid, err);
493		return err;
494	}
495
496	/* set a well known return code for detection of child write failure */
497	child_data->err = 12345678;
498
499	/*
500	 * Wait, until parent has finished its work: read child's pid from
501	 * parent's stack, and check, if bad result.
502	 */
503	err = ptrace(PTRACE_CONT, pid, 0, 0);
504	if (err) {
505		err = -errno;
506		printk(UM_KERN_ERR "Failed to continue new process, pid = %d, "
507		       "errno = %d\n", pid, errno);
508		return err;
509	}
510
511	wait_stub_done(pid);
512
513	pid = data->err;
514	if (pid < 0) {
515		printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports "
516		       "error %d\n", -pid);
517		return pid;
518	}
519
520	/*
521	 * Wait, until child has finished too: read child's result from
522	 * child's stack and check it.
523	 */
524	wait_stub_done(pid);
525	if (child_data->err != STUB_DATA) {
526		printk(UM_KERN_ERR "copy_context_skas0 - stub-child reports "
527		       "error %ld\n", child_data->err);
528		err = child_data->err;
529		goto out_kill;
530	}
531
532	if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
533		   (void *)PTRACE_O_TRACESYSGOOD) < 0) {
534		err = -errno;
535		printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS "
536		       "failed, errno = %d\n", errno);
537		goto out_kill;
538	}
539
540	return pid;
541
542 out_kill:
543	os_kill_ptraced_process(pid, 1);
544	return err;
545}
546
547void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
548{
549	(*buf)[0].JB_IP = (unsigned long) handler;
550	(*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
551		sizeof(void *);
552}
553
554#define INIT_JMP_NEW_THREAD 0
555#define INIT_JMP_CALLBACK 1
556#define INIT_JMP_HALT 2
557#define INIT_JMP_REBOOT 3
558
559void switch_threads(jmp_buf *me, jmp_buf *you)
560{
561	if (UML_SETJMP(me) == 0)
562		UML_LONGJMP(you, 1);
563}
564
565static jmp_buf initial_jmpbuf;
566
567/* XXX Make these percpu */
568static void (*cb_proc)(void *arg);
569static void *cb_arg;
570static jmp_buf *cb_back;
571
572int start_idle_thread(void *stack, jmp_buf *switch_buf)
573{
574	int n;
575
576	set_handler(SIGWINCH);
577
578	/*
579	 * Can't use UML_SETJMP or UML_LONGJMP here because they save
580	 * and restore signals, with the possible side-effect of
581	 * trying to handle any signals which came when they were
582	 * blocked, which can't be done on this stack.
583	 * Signals must be blocked when jumping back here and restored
584	 * after returning to the jumper.
585	 */
586	n = setjmp(initial_jmpbuf);
587	switch (n) {
588	case INIT_JMP_NEW_THREAD:
589		(*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
590		(*switch_buf)[0].JB_SP = (unsigned long) stack +
591			UM_THREAD_SIZE - sizeof(void *);
592		break;
593	case INIT_JMP_CALLBACK:
594		(*cb_proc)(cb_arg);
595		longjmp(*cb_back, 1);
596		break;
597	case INIT_JMP_HALT:
598		kmalloc_ok = 0;
599		return 0;
600	case INIT_JMP_REBOOT:
601		kmalloc_ok = 0;
602		return 1;
603	default:
604		printk(UM_KERN_ERR "Bad sigsetjmp return in "
605		       "start_idle_thread - %d\n", n);
606		fatal_sigsegv();
607	}
608	longjmp(*switch_buf, 1);
609}
610
611void initial_thread_cb_skas(void (*proc)(void *), void *arg)
612{
613	jmp_buf here;
614
615	cb_proc = proc;
616	cb_arg = arg;
617	cb_back = &here;
618
619	block_signals();
620	if (UML_SETJMP(&here) == 0)
621		UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
622	unblock_signals();
623
624	cb_proc = NULL;
625	cb_arg = NULL;
626	cb_back = NULL;
627}
628
629void halt_skas(void)
630{
631	block_signals();
632	UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
633}
634
635void reboot_skas(void)
636{
637	block_signals();
638	UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
639}
640
641void __switch_mm(struct mm_id *mm_idp)
642{
643	userspace_pid[0] = mm_idp->u.pid;
644}
645