xref: /linux/arch/um/kernel/skas/process.c (revision 36ca1195ad7f760a6af3814cb002bd3a3d4b4db1)
1 /*
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5 
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <signal.h>
11 #include <setjmp.h>
12 #include <sched.h>
13 #include <sys/wait.h>
14 #include <sys/mman.h>
15 #include <sys/user.h>
16 #include <asm/unistd.h>
17 #include "user.h"
18 #include "ptrace_user.h"
19 #include "time_user.h"
20 #include "sysdep/ptrace.h"
21 #include "user_util.h"
22 #include "kern_util.h"
23 #include "skas.h"
24 #include "sysdep/sigcontext.h"
25 #include "os.h"
26 #include "proc_mm.h"
27 #include "skas_ptrace.h"
28 #include "chan_user.h"
29 #include "signal_user.h"
30 #include "registers.h"
31 #include "process.h"
32 
33 int is_skas_winch(int pid, int fd, void *data)
34 {
35         if(pid != os_getpgrp())
36 		return(0);
37 
38 	register_winch_irq(-1, fd, -1, data);
39 	return(1);
40 }
41 
42 void get_skas_faultinfo(int pid, struct faultinfo * fi)
43 {
44 	int err;
45 
46         err = ptrace(PTRACE_FAULTINFO, pid, 0, fi);
47 	if(err)
48                 panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
49                       "errno = %d\n", errno);
50 
51         /* Special handling for i386, which has different structs */
52         if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo))
53                 memset((char *)fi + sizeof(struct ptrace_faultinfo), 0,
54                        sizeof(struct faultinfo) -
55                        sizeof(struct ptrace_faultinfo));
56 }
57 
58 static void handle_segv(int pid, union uml_pt_regs * regs)
59 {
60         get_skas_faultinfo(pid, &regs->skas.faultinfo);
61         segv(regs->skas.faultinfo, 0, 1, NULL);
62 }
63 
64 /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
65 static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu)
66 {
67 	int err, status;
68 
69 	/* Mark this as a syscall */
70 	UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs);
71 
72 	if (!local_using_sysemu)
73 	{
74 		err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
75 		if(err < 0)
76 			panic("handle_trap - nullifying syscall failed errno = %d\n",
77 			      errno);
78 
79 		err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
80 		if(err < 0)
81 			panic("handle_trap - continuing to end of syscall failed, "
82 			      "errno = %d\n", errno);
83 
84 		CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
85 		if((err < 0) || !WIFSTOPPED(status) ||
86 		   (WSTOPSIG(status) != SIGTRAP + 0x80))
87 			panic("handle_trap - failed to wait at end of syscall, "
88 			      "errno = %d, status = %d\n", errno, status);
89 	}
90 
91 	handle_syscall(regs);
92 }
93 
94 static int userspace_tramp(void *arg)
95 {
96 	init_new_thread_signals(0);
97 	enable_timer();
98 	ptrace(PTRACE_TRACEME, 0, 0, 0);
99 	os_stop_process(os_getpid());
100 	return(0);
101 }
102 
103 /* Each element set once, and only accessed by a single processor anyway */
104 #undef NR_CPUS
105 #define NR_CPUS 1
106 int userspace_pid[NR_CPUS];
107 
108 void start_userspace(int cpu)
109 {
110 	void *stack;
111 	unsigned long sp;
112 	int pid, status, n;
113 
114 	stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
115 		     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
116 	if(stack == MAP_FAILED)
117 		panic("start_userspace : mmap failed, errno = %d", errno);
118 	sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
119 
120 	pid = clone(userspace_tramp, (void *) sp,
121 		    CLONE_FILES | CLONE_VM | SIGCHLD, NULL);
122 	if(pid < 0)
123 		panic("start_userspace : clone failed, errno = %d", errno);
124 
125 	do {
126 		CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
127 		if(n < 0)
128 			panic("start_userspace : wait failed, errno = %d",
129 			      errno);
130 	} while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
131 
132 	if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
133 		panic("start_userspace : expected SIGSTOP, got status = %d",
134 		      status);
135 
136 	if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0)
137 		panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n",
138 		      errno);
139 
140 	if(munmap(stack, PAGE_SIZE) < 0)
141 		panic("start_userspace : munmap failed, errno = %d\n", errno);
142 
143 	userspace_pid[cpu] = pid;
144 }
145 
146 void userspace(union uml_pt_regs *regs)
147 {
148 	int err, status, op, pid = userspace_pid[0];
149 	int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
150 
151 	while(1){
152 		restore_registers(pid, regs);
153 
154 		/* Now we set local_using_sysemu to be used for one loop */
155 		local_using_sysemu = get_using_sysemu();
156 
157 		op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL));
158 
159 		err = ptrace(op, pid, 0, 0);
160 		if(err)
161 			panic("userspace - could not resume userspace process, "
162 			      "pid=%d, ptrace operation = %d, errno = %d\n",
163 			      op, errno);
164 
165 		CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
166 		if(err < 0)
167 			panic("userspace - waitpid failed, errno = %d\n",
168 			      errno);
169 
170 		regs->skas.is_user = 1;
171 		save_registers(pid, regs);
172 		UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
173 
174 		if(WIFSTOPPED(status)){
175 		  	switch(WSTOPSIG(status)){
176 			case SIGSEGV:
177                                 handle_segv(pid, regs);
178 				break;
179 			case SIGTRAP + 0x80:
180 			        handle_trap(pid, regs, local_using_sysemu);
181 				break;
182 			case SIGTRAP:
183 				relay_signal(SIGTRAP, regs);
184 				break;
185 			case SIGIO:
186 			case SIGVTALRM:
187 			case SIGILL:
188 			case SIGBUS:
189 			case SIGFPE:
190 			case SIGWINCH:
191                                 user_signal(WSTOPSIG(status), regs, pid);
192 				break;
193 			default:
194 			        printk("userspace - child stopped with signal "
195 				       "%d\n", WSTOPSIG(status));
196 			}
197 			interrupt_end();
198 
199 			/* Avoid -ERESTARTSYS handling in host */
200 			PT_SYSCALL_NR(regs->skas.regs) = -1;
201 		}
202 	}
203 }
204 #define INIT_JMP_NEW_THREAD 0
205 #define INIT_JMP_REMOVE_SIGSTACK 1
206 #define INIT_JMP_CALLBACK 2
207 #define INIT_JMP_HALT 3
208 #define INIT_JMP_REBOOT 4
209 
210 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
211 		void (*handler)(int))
212 {
213 	unsigned long flags;
214 	sigjmp_buf switch_buf, fork_buf;
215 
216 	*switch_buf_ptr = &switch_buf;
217 	*fork_buf_ptr = &fork_buf;
218 
219 	/* Somewhat subtle - siglongjmp restores the signal mask before doing
220 	 * the longjmp.  This means that when jumping from one stack to another
221 	 * when the target stack has interrupts enabled, an interrupt may occur
222 	 * on the source stack.  This is bad when starting up a process because
223 	 * it's not supposed to get timer ticks until it has been scheduled.
224 	 * So, we disable interrupts around the sigsetjmp to ensure that
225 	 * they can't happen until we get back here where they are safe.
226 	 */
227 	flags = get_signals();
228 	block_signals();
229 	if(sigsetjmp(fork_buf, 1) == 0)
230 		new_thread_proc(stack, handler);
231 
232 	remove_sigstack();
233 
234 	set_signals(flags);
235 }
236 
237 void thread_wait(void *sw, void *fb)
238 {
239 	sigjmp_buf buf, **switch_buf = sw, *fork_buf;
240 
241 	*switch_buf = &buf;
242 	fork_buf = fb;
243 	if(sigsetjmp(buf, 1) == 0)
244 		siglongjmp(*fork_buf, INIT_JMP_REMOVE_SIGSTACK);
245 }
246 
247 void switch_threads(void *me, void *next)
248 {
249 	sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
250 
251 	*me_ptr = &my_buf;
252 	if(sigsetjmp(my_buf, 1) == 0)
253 		siglongjmp(*next_buf, 1);
254 }
255 
256 static sigjmp_buf initial_jmpbuf;
257 
258 /* XXX Make these percpu */
259 static void (*cb_proc)(void *arg);
260 static void *cb_arg;
261 static sigjmp_buf *cb_back;
262 
263 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
264 {
265 	sigjmp_buf **switch_buf = switch_buf_ptr;
266 	int n;
267 
268 	set_handler(SIGWINCH, (__sighandler_t) sig_handler,
269 		    SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGALRM,
270 		    SIGVTALRM, -1);
271 
272 	*fork_buf_ptr = &initial_jmpbuf;
273 	n = sigsetjmp(initial_jmpbuf, 1);
274         switch(n){
275         case INIT_JMP_NEW_THREAD:
276                 new_thread_proc((void *) stack, new_thread_handler);
277                 break;
278         case INIT_JMP_REMOVE_SIGSTACK:
279                 remove_sigstack();
280                 break;
281         case INIT_JMP_CALLBACK:
282 		(*cb_proc)(cb_arg);
283 		siglongjmp(*cb_back, 1);
284                 break;
285         case INIT_JMP_HALT:
286 		kmalloc_ok = 0;
287 		return(0);
288         case INIT_JMP_REBOOT:
289 		kmalloc_ok = 0;
290 		return(1);
291         default:
292                 panic("Bad sigsetjmp return in start_idle_thread - %d\n", n);
293 	}
294 	siglongjmp(**switch_buf, 1);
295 }
296 
297 void remove_sigstack(void)
298 {
299 	stack_t stack = ((stack_t) { .ss_flags	= SS_DISABLE,
300 				     .ss_sp	= NULL,
301 				     .ss_size	= 0 });
302 
303 	if(sigaltstack(&stack, NULL) != 0)
304 		panic("disabling signal stack failed, errno = %d\n", errno);
305 }
306 
307 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
308 {
309 	sigjmp_buf here;
310 
311 	cb_proc = proc;
312 	cb_arg = arg;
313 	cb_back = &here;
314 
315 	block_signals();
316 	if(sigsetjmp(here, 1) == 0)
317 		siglongjmp(initial_jmpbuf, INIT_JMP_CALLBACK);
318 	unblock_signals();
319 
320 	cb_proc = NULL;
321 	cb_arg = NULL;
322 	cb_back = NULL;
323 }
324 
325 void halt_skas(void)
326 {
327 	block_signals();
328 	siglongjmp(initial_jmpbuf, INIT_JMP_HALT);
329 }
330 
331 void reboot_skas(void)
332 {
333 	block_signals();
334 	siglongjmp(initial_jmpbuf, INIT_JMP_REBOOT);
335 }
336 
337 void switch_mm_skas(int mm_fd)
338 {
339 	int err;
340 
341 #warning need cpu pid in switch_mm_skas
342 	err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
343 	if(err)
344 		panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
345 		      errno);
346 }
347 
348 void kill_off_processes_skas(void)
349 {
350 #warning need to loop over userspace_pids in kill_off_processes_skas
351 	os_kill_ptraced_process(userspace_pid[0], 1);
352 }
353 
354 /*
355  * Overrides for Emacs so that we follow Linus's tabbing style.
356  * Emacs will notice this stuff at the end of the file and automatically
357  * adjust the settings for this buffer only.  This must remain at the end
358  * of the file.
359  * ---------------------------------------------------------------------------
360  * Local variables:
361  * c-file-style: "linux"
362  * End:
363  */
364