blob: 39815c6b5e4510ecd0fdaef22c73fc87a82c06ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
4 */
5
Paolo 'Blaisorblade' Giarrussoae756df2005-09-21 18:40:10 +02006#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <stdio.h>
8#include <errno.h>
9#include <signal.h>
Gennady Sharapov60d339f2005-09-03 15:57:47 -070010#include <setjmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/unistd.h>
12#include <sys/mman.h>
13#include <sys/wait.h>
14#include "ptrace_user.h"
15#include "os.h"
16#include "user.h"
17#include "user_util.h"
Gennady Sharapov60d339f2005-09-03 15:57:47 -070018#include "process.h"
19#include "irq_user.h"
20#include "kern_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#define ARBITRARY_ADDR -1
23#define FAILURE_PID -1
24
25#define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
26#define COMM_SCANF "%*[^)])"
27
28unsigned long os_process_pc(int pid)
29{
30 char proc_stat[STAT_PATH_LEN], buf[256];
31 unsigned long pc;
32 int fd, err;
33
34 sprintf(proc_stat, "/proc/%d/stat", pid);
35 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
36 if(fd < 0){
37 printk("os_process_pc - couldn't open '%s', err = %d\n",
38 proc_stat, -fd);
39 return(ARBITRARY_ADDR);
40 }
41 err = os_read_file(fd, buf, sizeof(buf));
42 if(err < 0){
43 printk("os_process_pc - couldn't read '%s', err = %d\n",
44 proc_stat, -err);
45 os_close_file(fd);
46 return(ARBITRARY_ADDR);
47 }
48 os_close_file(fd);
49 pc = ARBITRARY_ADDR;
50 if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
51 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
52 "%*d %*d %*d %*d %*d %lu", &pc) != 1){
53 printk("os_process_pc - couldn't find pc in '%s'\n", buf);
54 }
55 return(pc);
56}
57
58int os_process_parent(int pid)
59{
60 char stat[STAT_PATH_LEN];
61 char data[256];
62 int parent, n, fd;
63
64 if(pid == -1) return(-1);
65
66 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
67 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
68 if(fd < 0){
69 printk("Couldn't open '%s', err = %d\n", stat, -fd);
70 return(FAILURE_PID);
71 }
72
73 n = os_read_file(fd, data, sizeof(data));
74 os_close_file(fd);
75
76 if(n < 0){
77 printk("Couldn't read '%s', err = %d\n", stat, -n);
78 return(FAILURE_PID);
79 }
80
81 parent = FAILURE_PID;
82 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
83 if(n != 1)
84 printk("Failed to scan '%s'\n", data);
85
86 return(parent);
87}
88
89void os_stop_process(int pid)
90{
91 kill(pid, SIGSTOP);
92}
93
94void os_kill_process(int pid, int reap_child)
95{
96 kill(pid, SIGKILL);
97 if(reap_child)
98 CATCH_EINTR(waitpid(pid, NULL, 0));
99
100}
101
102/* Kill off a ptraced child by all means available. kill it normally first,
103 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
104 * which it can't exit directly.
105 */
106
107void os_kill_ptraced_process(int pid, int reap_child)
108{
109 kill(pid, SIGKILL);
110 ptrace(PTRACE_KILL, pid);
111 ptrace(PTRACE_CONT, pid);
112 if(reap_child)
113 CATCH_EINTR(waitpid(pid, NULL, 0));
114}
115
116void os_usr1_process(int pid)
117{
118 kill(pid, SIGUSR1);
119}
120
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700121/* Don't use the glibc version, which caches the result in TLS. It misses some
122 * syscalls, and also breaks with clone(), which does not unshare the TLS.
123 */
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125inline _syscall0(pid_t, getpid)
126
127int os_getpid(void)
128{
129 return(getpid());
130}
131
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700132int os_getpgrp(void)
133{
134 return getpgrp();
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
138 int r, int w, int x)
139{
140 void *loc;
141 int prot;
142
143 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
144 (x ? PROT_EXEC : 0);
145
146 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
147 fd, off);
148 if(loc == MAP_FAILED)
149 return(-errno);
150 return(0);
151}
152
153int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
154{
155 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
156 (x ? PROT_EXEC : 0));
157
158 if(mprotect(addr, len, prot) < 0)
159 return(-errno);
160 return(0);
161}
162
163int os_unmap_memory(void *addr, int len)
164{
165 int err;
166
167 err = munmap(addr, len);
168 if(err < 0)
169 return(-errno);
170 return(0);
171}
172
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700173void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
174{
175 int flags = 0, pages;
176
177 if(sig_stack != NULL){
178 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
179 set_sigstack(sig_stack, pages * page_size());
180 flags = SA_ONSTACK;
181 }
182 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
183}
184
185void init_new_thread_signals(int altstack)
186{
187 int flags = altstack ? SA_ONSTACK : 0;
188
189 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
190 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
191 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
192 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
193 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
194 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
195 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
196 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
197 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
198 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
199 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
200 flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
201 signal(SIGHUP, SIG_IGN);
202
203 init_irq_signals(altstack);
204}
205
206int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
207{
208 sigjmp_buf buf;
209 int n;
210
211 *jmp_ptr = &buf;
212 n = sigsetjmp(buf, 1);
213 if(n != 0)
214 return(n);
215 (*fn)(arg);
216 return(0);
217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*
220 * Overrides for Emacs so that we follow Linus's tabbing style.
221 * Emacs will notice this stuff at the end of the file and automatically
222 * adjust the settings for this buffer only. This must remain at the end
223 * of the file.
224 * ---------------------------------------------------------------------------
225 * Local variables:
226 * c-file-style: "linux"
227 * End:
228 */