blob: e08fe305beb1d1bdf9a27530b14f0d075648ebbe [file] [log] [blame]
Jeff Dikeba180fd2007-10-16 01:27:00 -07001/*
2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <stdio.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -07007#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <errno.h>
9#include <signal.h>
Jeff Dike512b6fb2007-10-16 01:27:11 -070010#include <fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <sys/mman.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070012#include <sys/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <sys/wait.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070014#include <asm/unistd.h>
Jeff Dike36e45462007-05-06 14:51:11 -070015#include "init.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070016#include "longjmp.h"
17#include "os.h"
18#include "process.h"
19#include "skas_ptrace.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
93void os_stop_process(int pid)
94{
95 kill(pid, SIGSTOP);
96}
97
98void os_kill_process(int pid, int reap_child)
99{
100 kill(pid, SIGKILL);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700101 if (reap_child)
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800102 CATCH_EINTR(waitpid(pid, NULL, __WALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -0800105/* This is here uniquely to have access to the userspace errno, i.e. the one
106 * used by ptrace in case of error.
107 */
108
109long os_ptrace_ldt(long pid, long addr, long data)
110{
111 int ret;
112
113 ret = ptrace(PTRACE_LDT, pid, addr, data);
114
115 if (ret < 0)
116 return -errno;
117 return ret;
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* Kill off a ptraced child by all means available. kill it normally first,
121 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
122 * which it can't exit directly.
123 */
124
125void os_kill_ptraced_process(int pid, int reap_child)
126{
127 kill(pid, SIGKILL);
128 ptrace(PTRACE_KILL, pid);
129 ptrace(PTRACE_CONT, pid);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700130 if (reap_child)
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800131 CATCH_EINTR(waitpid(pid, NULL, __WALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700134/* Don't use the glibc version, which caches the result in TLS. It misses some
135 * syscalls, and also breaks with clone(), which does not unshare the TLS.
136 */
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138int os_getpid(void)
139{
Jeff Dikeef0470c2007-05-06 14:51:33 -0700140 return syscall(__NR_getpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700143int os_getpgrp(void)
144{
145 return getpgrp();
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
149 int r, int w, int x)
150{
151 void *loc;
152 int prot;
153
Jeff Dikeba180fd2007-10-16 01:27:00 -0700154 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 (x ? PROT_EXEC : 0);
156
157 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
158 fd, off);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700159 if (loc == MAP_FAILED)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700160 return -errno;
161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
165{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700166 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 (x ? PROT_EXEC : 0));
168
Jeff Dikeba180fd2007-10-16 01:27:00 -0700169 if (mprotect(addr, len, prot) < 0)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700170 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700171
172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175int os_unmap_memory(void *addr, int len)
176{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700177 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Jeff Dikeba180fd2007-10-16 01:27:00 -0700179 err = munmap(addr, len);
180 if (err < 0)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700181 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Jeff Dike02dea082006-03-31 02:30:08 -0800185#ifndef MADV_REMOVE
Jeff Dikeb73781c2006-04-18 22:20:24 -0700186#define MADV_REMOVE KERNEL_MADV_REMOVE
Jeff Dike02dea082006-03-31 02:30:08 -0800187#endif
188
Jeff Dike97a1fcb2007-07-23 18:43:48 -0700189int os_drop_memory(void *addr, int length)
Jeff Dike02dea082006-03-31 02:30:08 -0800190{
191 int err;
192
193 err = madvise(addr, length, MADV_REMOVE);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700194 if (err < 0)
Jeff Dike02dea082006-03-31 02:30:08 -0800195 err = -errno;
196 return err;
197}
198
Jeff Dike36e45462007-05-06 14:51:11 -0700199int __init can_drop_memory(void)
Jeff Dike02dea082006-03-31 02:30:08 -0800200{
201 void *addr;
Jeff Dikee3104f52006-05-01 12:15:58 -0700202 int fd, ok = 0;
Jeff Dike02dea082006-03-31 02:30:08 -0800203
Jeff Dikeba180fd2007-10-16 01:27:00 -0700204 printk(UM_KERN_INFO "Checking host MADV_REMOVE support...");
Jeff Dike02dea082006-03-31 02:30:08 -0800205 fd = create_mem_file(UM_KERN_PAGE_SIZE);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700206 if (fd < 0) {
207 printk(UM_KERN_ERR "Creating test memory file failed, "
208 "err = %d\n", -fd);
Jeff Dikee3104f52006-05-01 12:15:58 -0700209 goto out;
Jeff Dike02dea082006-03-31 02:30:08 -0800210 }
211
212 addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
Jeff Dikeb73781c2006-04-18 22:20:24 -0700213 MAP_SHARED, fd, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700214 if (addr == MAP_FAILED) {
215 printk(UM_KERN_ERR "Mapping test memory file failed, "
216 "err = %d\n", -errno);
Jeff Dikee3104f52006-05-01 12:15:58 -0700217 goto out_close;
Jeff Dike02dea082006-03-31 02:30:08 -0800218 }
219
Jeff Dikeba180fd2007-10-16 01:27:00 -0700220 if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) {
221 printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno);
Jeff Dikee3104f52006-05-01 12:15:58 -0700222 goto out_unmap;
Jeff Dike02dea082006-03-31 02:30:08 -0800223 }
224
Jeff Dike5134d8f2008-02-08 04:22:08 -0800225 printk(UM_KERN_CONT "OK\n");
Jeff Dikee3104f52006-05-01 12:15:58 -0700226 ok = 1;
227
228out_unmap:
229 munmap(addr, UM_KERN_PAGE_SIZE);
230out_close:
231 close(fd);
232out:
233 return ok;
Jeff Dike02dea082006-03-31 02:30:08 -0800234}
235
Jeff Dikee64bd132006-07-10 04:45:07 -0700236void init_new_thread_signals(void)
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700237{
Al Viro00361682011-08-18 20:04:39 +0100238 set_handler(SIGSEGV);
239 set_handler(SIGTRAP);
240 set_handler(SIGFPE);
241 set_handler(SIGILL);
242 set_handler(SIGBUS);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700243 signal(SIGHUP, SIG_IGN);
Al Viro00361682011-08-18 20:04:39 +0100244 set_handler(SIGIO);
Jeff Dike3a24ebf2008-02-04 22:31:16 -0800245 signal(SIGWINCH, SIG_IGN);
Richard Weinbergerdb271cf2011-05-24 17:13:04 -0700246 signal(SIGTERM, SIG_DFL);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700247}
248
Jeff Dikefab95c52007-10-16 01:27:05 -0700249int run_kernel_thread(int (*fn)(void *), void *arg, jmp_buf **jmp_ptr)
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700250{
Jeff Dikead28e022006-04-18 22:21:41 -0700251 jmp_buf buf;
Jeff Dikea5df0d12006-07-14 00:24:02 -0700252 int n;
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700253
Jeff Dike1d7173b2006-01-18 17:42:49 -0800254 *jmp_ptr = &buf;
Jeff Dikea5df0d12006-07-14 00:24:02 -0700255 n = UML_SETJMP(&buf);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700256 if (n != 0)
Jeff Dikea5df0d12006-07-14 00:24:02 -0700257 return n;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800258 (*fn)(arg);
Jeff Dikea5df0d12006-07-14 00:24:02 -0700259 return 0;
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700260}