]>
Commit | Line | Data |
---|---|---|
ba180fd4 JD |
1 | /* |
2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) | |
1da177e4 LT |
3 | * Licensed under the GPL |
4 | */ | |
5 | ||
1da177e4 | 6 | #include <stdio.h> |
ba180fd4 | 7 | #include <unistd.h> |
1da177e4 LT |
8 | #include <errno.h> |
9 | #include <signal.h> | |
512b6fb1 | 10 | #include <fcntl.h> |
1da177e4 | 11 | #include <sys/mman.h> |
ba180fd4 | 12 | #include <sys/ptrace.h> |
1da177e4 | 13 | #include <sys/wait.h> |
ba180fd4 JD |
14 | #include <asm/unistd.h> |
15 | #include "init.h" | |
16 | #include "kern_constants.h" | |
17 | #include "longjmp.h" | |
1da177e4 | 18 | #include "os.h" |
60d339f6 | 19 | #include "process.h" |
07f4e2c6 | 20 | #include "skas_ptrace.h" |
ba180fd4 | 21 | #include "user.h" |
1da177e4 LT |
22 | |
23 | #define ARBITRARY_ADDR -1 | |
24 | #define FAILURE_PID -1 | |
25 | ||
26 | #define STAT_PATH_LEN sizeof("/proc/#######/stat\0") | |
27 | #define COMM_SCANF "%*[^)])" | |
28 | ||
29 | unsigned long os_process_pc(int pid) | |
30 | { | |
31 | char proc_stat[STAT_PATH_LEN], buf[256]; | |
512b6fb1 | 32 | unsigned long pc = ARBITRARY_ADDR; |
1da177e4 LT |
33 | int fd, err; |
34 | ||
35 | sprintf(proc_stat, "/proc/%d/stat", pid); | |
512b6fb1 | 36 | fd = open(proc_stat, O_RDONLY, 0); |
ba180fd4 JD |
37 | if (fd < 0) { |
38 | printk(UM_KERN_ERR "os_process_pc - couldn't open '%s', " | |
512b6fb1 JD |
39 | "errno = %d\n", proc_stat, errno); |
40 | goto out; | |
1da177e4 | 41 | } |
a61f334f | 42 | CATCH_EINTR(err = read(fd, buf, sizeof(buf))); |
ba180fd4 JD |
43 | if (err < 0) { |
44 | printk(UM_KERN_ERR "os_process_pc - couldn't read '%s', " | |
45 | "err = %d\n", proc_stat, errno); | |
512b6fb1 | 46 | goto out_close; |
1da177e4 LT |
47 | } |
48 | os_close_file(fd); | |
49 | pc = ARBITRARY_ADDR; | |
ba180fd4 | 50 | if (sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d " |
512b6fb1 JD |
51 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " |
52 | "%*d %*d %*d %*d %*d %lu", &pc) != 1) | |
ba180fd4 JD |
53 | printk(UM_KERN_ERR "os_process_pc - couldn't find pc in '%s'\n", |
54 | buf); | |
512b6fb1 JD |
55 | out_close: |
56 | close(fd); | |
57 | out: | |
ef0470c0 | 58 | return pc; |
1da177e4 LT |
59 | } |
60 | ||
61 | int os_process_parent(int pid) | |
62 | { | |
63 | char stat[STAT_PATH_LEN]; | |
64 | char data[256]; | |
512b6fb1 | 65 | int parent = FAILURE_PID, n, fd; |
1da177e4 | 66 | |
ba180fd4 | 67 | if (pid == -1) |
512b6fb1 | 68 | return parent; |
1da177e4 LT |
69 | |
70 | snprintf(stat, sizeof(stat), "/proc/%d/stat", pid); | |
512b6fb1 | 71 | fd = open(stat, O_RDONLY, 0); |
ba180fd4 | 72 | if (fd < 0) { |
512b6fb1 JD |
73 | printk(UM_KERN_ERR "Couldn't open '%s', errno = %d\n", stat, |
74 | errno); | |
75 | return parent; | |
1da177e4 LT |
76 | } |
77 | ||
a61f334f | 78 | CATCH_EINTR(n = read(fd, data, sizeof(data))); |
512b6fb1 | 79 | close(fd); |
1da177e4 | 80 | |
ba180fd4 | 81 | if (n < 0) { |
512b6fb1 | 82 | printk(UM_KERN_ERR "Couldn't read '%s', errno = %d\n", stat, |
ba180fd4 | 83 | errno); |
512b6fb1 | 84 | return parent; |
1da177e4 LT |
85 | } |
86 | ||
87 | parent = FAILURE_PID; | |
88 | n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent); | |
ba180fd4 JD |
89 | if (n != 1) |
90 | printk(UM_KERN_ERR "Failed to scan '%s'\n", data); | |
1da177e4 | 91 | |
ef0470c0 | 92 | return parent; |
1da177e4 LT |
93 | } |
94 | ||
95 | void os_stop_process(int pid) | |
96 | { | |
97 | kill(pid, SIGSTOP); | |
98 | } | |
99 | ||
100 | void os_kill_process(int pid, int reap_child) | |
101 | { | |
102 | kill(pid, SIGKILL); | |
ba180fd4 | 103 | if (reap_child) |
4dbed85a | 104 | CATCH_EINTR(waitpid(pid, NULL, __WALL)); |
1da177e4 LT |
105 | } |
106 | ||
07f4e2c6 PBG |
107 | /* This is here uniquely to have access to the userspace errno, i.e. the one |
108 | * used by ptrace in case of error. | |
109 | */ | |
110 | ||
111 | long os_ptrace_ldt(long pid, long addr, long data) | |
112 | { | |
113 | int ret; | |
114 | ||
115 | ret = ptrace(PTRACE_LDT, pid, addr, data); | |
116 | ||
117 | if (ret < 0) | |
118 | return -errno; | |
119 | return ret; | |
120 | } | |
121 | ||
1da177e4 LT |
122 | /* Kill off a ptraced child by all means available. kill it normally first, |
123 | * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from | |
124 | * which it can't exit directly. | |
125 | */ | |
126 | ||
127 | void os_kill_ptraced_process(int pid, int reap_child) | |
128 | { | |
129 | kill(pid, SIGKILL); | |
130 | ptrace(PTRACE_KILL, pid); | |
131 | ptrace(PTRACE_CONT, pid); | |
ba180fd4 | 132 | if (reap_child) |
4dbed85a | 133 | CATCH_EINTR(waitpid(pid, NULL, __WALL)); |
1da177e4 LT |
134 | } |
135 | ||
60d339f6 GS |
136 | /* Don't use the glibc version, which caches the result in TLS. It misses some |
137 | * syscalls, and also breaks with clone(), which does not unshare the TLS. | |
138 | */ | |
139 | ||
1da177e4 LT |
140 | int os_getpid(void) |
141 | { | |
ef0470c0 | 142 | return syscall(__NR_getpid); |
1da177e4 LT |
143 | } |
144 | ||
cd2ee4a3 JD |
145 | int os_getpgrp(void) |
146 | { | |
147 | return getpgrp(); | |
148 | } | |
149 | ||
1da177e4 LT |
150 | int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len, |
151 | int r, int w, int x) | |
152 | { | |
153 | void *loc; | |
154 | int prot; | |
155 | ||
ba180fd4 | 156 | prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | |
1da177e4 LT |
157 | (x ? PROT_EXEC : 0); |
158 | ||
159 | loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED, | |
160 | fd, off); | |
ba180fd4 | 161 | if (loc == MAP_FAILED) |
ef0470c0 JD |
162 | return -errno; |
163 | return 0; | |
1da177e4 LT |
164 | } |
165 | ||
166 | int os_protect_memory(void *addr, unsigned long len, int r, int w, int x) | |
167 | { | |
ba180fd4 | 168 | int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | |
1da177e4 LT |
169 | (x ? PROT_EXEC : 0)); |
170 | ||
ba180fd4 | 171 | if (mprotect(addr, len, prot) < 0) |
ef0470c0 | 172 | return -errno; |
ba180fd4 JD |
173 | |
174 | return 0; | |
1da177e4 LT |
175 | } |
176 | ||
177 | int os_unmap_memory(void *addr, int len) | |
178 | { | |
ba180fd4 | 179 | int err; |
1da177e4 | 180 | |
ba180fd4 JD |
181 | err = munmap(addr, len); |
182 | if (err < 0) | |
ef0470c0 | 183 | return -errno; |
ba180fd4 | 184 | return 0; |
1da177e4 LT |
185 | } |
186 | ||
02dea087 | 187 | #ifndef MADV_REMOVE |
b73781c8 | 188 | #define MADV_REMOVE KERNEL_MADV_REMOVE |
02dea087 JD |
189 | #endif |
190 | ||
97a1fcbb | 191 | int os_drop_memory(void *addr, int length) |
02dea087 JD |
192 | { |
193 | int err; | |
194 | ||
195 | err = madvise(addr, length, MADV_REMOVE); | |
ba180fd4 | 196 | if (err < 0) |
02dea087 JD |
197 | err = -errno; |
198 | return err; | |
199 | } | |
200 | ||
36e45463 | 201 | int __init can_drop_memory(void) |
02dea087 JD |
202 | { |
203 | void *addr; | |
e3104f50 | 204 | int fd, ok = 0; |
02dea087 | 205 | |
ba180fd4 | 206 | printk(UM_KERN_INFO "Checking host MADV_REMOVE support..."); |
02dea087 | 207 | fd = create_mem_file(UM_KERN_PAGE_SIZE); |
ba180fd4 JD |
208 | if (fd < 0) { |
209 | printk(UM_KERN_ERR "Creating test memory file failed, " | |
210 | "err = %d\n", -fd); | |
e3104f50 | 211 | goto out; |
02dea087 JD |
212 | } |
213 | ||
214 | addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE, | |
b73781c8 | 215 | MAP_SHARED, fd, 0); |
ba180fd4 JD |
216 | if (addr == MAP_FAILED) { |
217 | printk(UM_KERN_ERR "Mapping test memory file failed, " | |
218 | "err = %d\n", -errno); | |
e3104f50 | 219 | goto out_close; |
02dea087 JD |
220 | } |
221 | ||
ba180fd4 JD |
222 | if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) { |
223 | printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno); | |
e3104f50 | 224 | goto out_unmap; |
02dea087 JD |
225 | } |
226 | ||
227 | printk("OK\n"); | |
e3104f50 JD |
228 | ok = 1; |
229 | ||
230 | out_unmap: | |
231 | munmap(addr, UM_KERN_PAGE_SIZE); | |
232 | out_close: | |
233 | close(fd); | |
234 | out: | |
235 | return ok; | |
02dea087 JD |
236 | } |
237 | ||
e64bd134 | 238 | void init_new_thread_signals(void) |
60d339f6 | 239 | { |
e64bd134 | 240 | set_handler(SIGSEGV, (__sighandler_t) sig_handler, SA_ONSTACK, |
61b63c55 | 241 | SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1); |
e64bd134 | 242 | set_handler(SIGTRAP, (__sighandler_t) sig_handler, SA_ONSTACK, |
61b63c55 | 243 | SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1); |
e64bd134 | 244 | set_handler(SIGFPE, (__sighandler_t) sig_handler, SA_ONSTACK, |
61b63c55 | 245 | SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1); |
e64bd134 | 246 | set_handler(SIGILL, (__sighandler_t) sig_handler, SA_ONSTACK, |
61b63c55 | 247 | SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1); |
e64bd134 | 248 | set_handler(SIGBUS, (__sighandler_t) sig_handler, SA_ONSTACK, |
61b63c55 | 249 | SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1); |
60d339f6 GS |
250 | signal(SIGHUP, SIG_IGN); |
251 | ||
e64bd134 | 252 | init_irq_signals(1); |
60d339f6 GS |
253 | } |
254 | ||
fab95c55 | 255 | int run_kernel_thread(int (*fn)(void *), void *arg, jmp_buf **jmp_ptr) |
60d339f6 | 256 | { |
ad28e029 | 257 | jmp_buf buf; |
a5df0d1a | 258 | int n; |
1d7173ba JD |
259 | |
260 | *jmp_ptr = &buf; | |
a5df0d1a | 261 | n = UML_SETJMP(&buf); |
ba180fd4 | 262 | if (n != 0) |
a5df0d1a | 263 | return n; |
1d7173ba | 264 | (*fn)(arg); |
a5df0d1a | 265 | return 0; |
60d339f6 | 266 | } |