blob: b3e0d40932e1128031dc192dca7e220891de0c84 [file] [log] [blame]
Jeff Dikeba180fd2007-10-16 01:27:00 -07001/*
Anton Ivanov2eb5f312015-11-02 16:16:37 +00002 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
Jeff Dikeba180fd2007-10-16 01:27:00 -07003 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Licensed under the GPL
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <stdio.h>
Richard Weinbergerf75b1b12013-08-17 18:46:00 +02008#include <stdlib.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -07009#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <errno.h>
11#include <signal.h>
Jeff Dike512b6fb2007-10-16 01:27:11 -070012#include <fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <sys/mman.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070014#include <sys/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <sys/wait.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070016#include <asm/unistd.h>
Al Viro37185b32012-10-08 03:27:32 +010017#include <init.h>
18#include <longjmp.h>
19#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#define ARBITRARY_ADDR -1
22#define FAILURE_PID -1
23
24#define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
25#define COMM_SCANF "%*[^)])"
26
27unsigned long os_process_pc(int pid)
28{
29 char proc_stat[STAT_PATH_LEN], buf[256];
Jeff Dike512b6fb2007-10-16 01:27:11 -070030 unsigned long pc = ARBITRARY_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 int fd, err;
32
33 sprintf(proc_stat, "/proc/%d/stat", pid);
Jeff Dike512b6fb2007-10-16 01:27:11 -070034 fd = open(proc_stat, O_RDONLY, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -070035 if (fd < 0) {
36 printk(UM_KERN_ERR "os_process_pc - couldn't open '%s', "
Jeff Dike512b6fb2007-10-16 01:27:11 -070037 "errno = %d\n", proc_stat, errno);
38 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 }
Jeff Dikea61f3342007-05-06 14:51:35 -070040 CATCH_EINTR(err = read(fd, buf, sizeof(buf)));
Jeff Dikeba180fd2007-10-16 01:27:00 -070041 if (err < 0) {
42 printk(UM_KERN_ERR "os_process_pc - couldn't read '%s', "
43 "err = %d\n", proc_stat, errno);
Jeff Dike512b6fb2007-10-16 01:27:11 -070044 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
46 os_close_file(fd);
47 pc = ARBITRARY_ADDR;
Jeff Dikeba180fd2007-10-16 01:27:00 -070048 if (sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
Jeff Dike512b6fb2007-10-16 01:27:11 -070049 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
50 "%*d %*d %*d %*d %*d %lu", &pc) != 1)
Jeff Dikeba180fd2007-10-16 01:27:00 -070051 printk(UM_KERN_ERR "os_process_pc - couldn't find pc in '%s'\n",
52 buf);
Jeff Dike512b6fb2007-10-16 01:27:11 -070053 out_close:
54 close(fd);
55 out:
Jeff Dikeef0470c2007-05-06 14:51:33 -070056 return pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59int os_process_parent(int pid)
60{
61 char stat[STAT_PATH_LEN];
62 char data[256];
Jeff Dike512b6fb2007-10-16 01:27:11 -070063 int parent = FAILURE_PID, n, fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jeff Dikeba180fd2007-10-16 01:27:00 -070065 if (pid == -1)
Jeff Dike512b6fb2007-10-16 01:27:11 -070066 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
Jeff Dike512b6fb2007-10-16 01:27:11 -070069 fd = open(stat, O_RDONLY, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -070070 if (fd < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -070071 printk(UM_KERN_ERR "Couldn't open '%s', errno = %d\n", stat,
72 errno);
73 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75
Jeff Dikea61f3342007-05-06 14:51:35 -070076 CATCH_EINTR(n = read(fd, data, sizeof(data)));
Jeff Dike512b6fb2007-10-16 01:27:11 -070077 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Jeff Dikeba180fd2007-10-16 01:27:00 -070079 if (n < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -070080 printk(UM_KERN_ERR "Couldn't read '%s', errno = %d\n", stat,
Jeff Dikeba180fd2007-10-16 01:27:00 -070081 errno);
Jeff Dike512b6fb2007-10-16 01:27:11 -070082 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
85 parent = FAILURE_PID;
86 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
Jeff Dikeba180fd2007-10-16 01:27:00 -070087 if (n != 1)
88 printk(UM_KERN_ERR "Failed to scan '%s'\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Jeff Dikeef0470c2007-05-06 14:51:33 -070090 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Anton Ivanov2eb5f312015-11-02 16:16:37 +000093void os_alarm_process(int pid)
94{
95 kill(pid, SIGALRM);
96}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098void os_stop_process(int pid)
99{
100 kill(pid, SIGSTOP);
101}
102
103void os_kill_process(int pid, int reap_child)
104{
105 kill(pid, SIGKILL);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700106 if (reap_child)
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800107 CATCH_EINTR(waitpid(pid, NULL, __WALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110/* Kill off a ptraced child by all means available. kill it normally first,
111 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
112 * which it can't exit directly.
113 */
114
115void os_kill_ptraced_process(int pid, int reap_child)
116{
117 kill(pid, SIGKILL);
118 ptrace(PTRACE_KILL, pid);
119 ptrace(PTRACE_CONT, pid);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700120 if (reap_child)
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800121 CATCH_EINTR(waitpid(pid, NULL, __WALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700124/* Don't use the glibc version, which caches the result in TLS. It misses some
125 * syscalls, and also breaks with clone(), which does not unshare the TLS.
126 */
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128int os_getpid(void)
129{
Jeff Dikeef0470c2007-05-06 14:51:33 -0700130 return syscall(__NR_getpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700133int os_getpgrp(void)
134{
135 return getpgrp();
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
139 int r, int w, int x)
140{
141 void *loc;
142 int prot;
143
Jeff Dikeba180fd2007-10-16 01:27:00 -0700144 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 (x ? PROT_EXEC : 0);
146
147 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
148 fd, off);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700149 if (loc == MAP_FAILED)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700150 return -errno;
151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
155{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700156 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 (x ? PROT_EXEC : 0));
158
Jeff Dikeba180fd2007-10-16 01:27:00 -0700159 if (mprotect(addr, len, prot) < 0)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700160 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700161
162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165int os_unmap_memory(void *addr, int len)
166{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700167 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jeff Dikeba180fd2007-10-16 01:27:00 -0700169 err = munmap(addr, len);
170 if (err < 0)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700171 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Jeff Dike02dea082006-03-31 02:30:08 -0800175#ifndef MADV_REMOVE
Jeff Dikeb73781c2006-04-18 22:20:24 -0700176#define MADV_REMOVE KERNEL_MADV_REMOVE
Jeff Dike02dea082006-03-31 02:30:08 -0800177#endif
178
Jeff Dike97a1fcb2007-07-23 18:43:48 -0700179int os_drop_memory(void *addr, int length)
Jeff Dike02dea082006-03-31 02:30:08 -0800180{
181 int err;
182
183 err = madvise(addr, length, MADV_REMOVE);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700184 if (err < 0)
Jeff Dike02dea082006-03-31 02:30:08 -0800185 err = -errno;
186 return err;
187}
188
Jeff Dike36e45462007-05-06 14:51:11 -0700189int __init can_drop_memory(void)
Jeff Dike02dea082006-03-31 02:30:08 -0800190{
191 void *addr;
Jeff Dikee3104f52006-05-01 12:15:58 -0700192 int fd, ok = 0;
Jeff Dike02dea082006-03-31 02:30:08 -0800193
Jeff Dikeba180fd2007-10-16 01:27:00 -0700194 printk(UM_KERN_INFO "Checking host MADV_REMOVE support...");
Jeff Dike02dea082006-03-31 02:30:08 -0800195 fd = create_mem_file(UM_KERN_PAGE_SIZE);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700196 if (fd < 0) {
197 printk(UM_KERN_ERR "Creating test memory file failed, "
198 "err = %d\n", -fd);
Jeff Dikee3104f52006-05-01 12:15:58 -0700199 goto out;
Jeff Dike02dea082006-03-31 02:30:08 -0800200 }
201
202 addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
Jeff Dikeb73781c2006-04-18 22:20:24 -0700203 MAP_SHARED, fd, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700204 if (addr == MAP_FAILED) {
205 printk(UM_KERN_ERR "Mapping test memory file failed, "
206 "err = %d\n", -errno);
Jeff Dikee3104f52006-05-01 12:15:58 -0700207 goto out_close;
Jeff Dike02dea082006-03-31 02:30:08 -0800208 }
209
Jeff Dikeba180fd2007-10-16 01:27:00 -0700210 if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) {
211 printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno);
Jeff Dikee3104f52006-05-01 12:15:58 -0700212 goto out_unmap;
Jeff Dike02dea082006-03-31 02:30:08 -0800213 }
214
Jeff Dike5134d8f2008-02-08 04:22:08 -0800215 printk(UM_KERN_CONT "OK\n");
Jeff Dikee3104f52006-05-01 12:15:58 -0700216 ok = 1;
217
218out_unmap:
219 munmap(addr, UM_KERN_PAGE_SIZE);
220out_close:
221 close(fd);
222out:
223 return ok;
Jeff Dike02dea082006-03-31 02:30:08 -0800224}
225
Richard Weinbergerf75b1b12013-08-17 18:46:00 +0200226static int os_page_mincore(void *addr)
227{
228 char vec[2];
229 int ret;
230
231 ret = mincore(addr, UM_KERN_PAGE_SIZE, vec);
232 if (ret < 0) {
233 if (errno == ENOMEM || errno == EINVAL)
234 return 0;
235 else
236 return -errno;
237 }
238
239 return vec[0] & 1;
240}
241
242int os_mincore(void *addr, unsigned long len)
243{
244 char *vec;
245 int ret, i;
246
247 if (len <= UM_KERN_PAGE_SIZE)
248 return os_page_mincore(addr);
249
250 vec = calloc(1, (len + UM_KERN_PAGE_SIZE - 1) / UM_KERN_PAGE_SIZE);
251 if (!vec)
252 return -ENOMEM;
253
254 ret = mincore(addr, UM_KERN_PAGE_SIZE, vec);
255 if (ret < 0) {
256 if (errno == ENOMEM || errno == EINVAL)
257 ret = 0;
258 else
259 ret = -errno;
260
261 goto out;
262 }
263
264 for (i = 0; i < ((len + UM_KERN_PAGE_SIZE - 1) / UM_KERN_PAGE_SIZE); i++) {
265 if (!(vec[i] & 1)) {
266 ret = 0;
267 goto out;
268 }
269 }
270
271 ret = 1;
272out:
273 free(vec);
274 return ret;
275}
276
Jeff Dikee64bd132006-07-10 04:45:07 -0700277void init_new_thread_signals(void)
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700278{
Al Viro00361682011-08-18 20:04:39 +0100279 set_handler(SIGSEGV);
280 set_handler(SIGTRAP);
281 set_handler(SIGFPE);
282 set_handler(SIGILL);
283 set_handler(SIGBUS);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700284 signal(SIGHUP, SIG_IGN);
Al Viro00361682011-08-18 20:04:39 +0100285 set_handler(SIGIO);
Jeff Dike3a24ebf2008-02-04 22:31:16 -0800286 signal(SIGWINCH, SIG_IGN);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700287}