blob: e0477c3ee89448f72320b01a493a97cb280ecad6 [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 "kern_constants.h"
17#include "longjmp.h"
18#include "os.h"
19#include "process.h"
20#include "skas_ptrace.h"
21#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
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
29unsigned long os_process_pc(int pid)
30{
31 char proc_stat[STAT_PATH_LEN], buf[256];
Jeff Dike512b6fb2007-10-16 01:27:11 -070032 unsigned long pc = ARBITRARY_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int fd, err;
34
35 sprintf(proc_stat, "/proc/%d/stat", pid);
Jeff Dike512b6fb2007-10-16 01:27:11 -070036 fd = open(proc_stat, O_RDONLY, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -070037 if (fd < 0) {
38 printk(UM_KERN_ERR "os_process_pc - couldn't open '%s', "
Jeff Dike512b6fb2007-10-16 01:27:11 -070039 "errno = %d\n", proc_stat, errno);
40 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 }
Jeff Dikea61f3342007-05-06 14:51:35 -070042 CATCH_EINTR(err = read(fd, buf, sizeof(buf)));
Jeff Dikeba180fd2007-10-16 01:27:00 -070043 if (err < 0) {
44 printk(UM_KERN_ERR "os_process_pc - couldn't read '%s', "
45 "err = %d\n", proc_stat, errno);
Jeff Dike512b6fb2007-10-16 01:27:11 -070046 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48 os_close_file(fd);
49 pc = ARBITRARY_ADDR;
Jeff Dikeba180fd2007-10-16 01:27:00 -070050 if (sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
Jeff Dike512b6fb2007-10-16 01:27:11 -070051 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
52 "%*d %*d %*d %*d %*d %lu", &pc) != 1)
Jeff Dikeba180fd2007-10-16 01:27:00 -070053 printk(UM_KERN_ERR "os_process_pc - couldn't find pc in '%s'\n",
54 buf);
Jeff Dike512b6fb2007-10-16 01:27:11 -070055 out_close:
56 close(fd);
57 out:
Jeff Dikeef0470c2007-05-06 14:51:33 -070058 return pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61int os_process_parent(int pid)
62{
63 char stat[STAT_PATH_LEN];
64 char data[256];
Jeff Dike512b6fb2007-10-16 01:27:11 -070065 int parent = FAILURE_PID, n, fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jeff Dikeba180fd2007-10-16 01:27:00 -070067 if (pid == -1)
Jeff Dike512b6fb2007-10-16 01:27:11 -070068 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
Jeff Dike512b6fb2007-10-16 01:27:11 -070071 fd = open(stat, O_RDONLY, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -070072 if (fd < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -070073 printk(UM_KERN_ERR "Couldn't open '%s', errno = %d\n", stat,
74 errno);
75 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
Jeff Dikea61f3342007-05-06 14:51:35 -070078 CATCH_EINTR(n = read(fd, data, sizeof(data)));
Jeff Dike512b6fb2007-10-16 01:27:11 -070079 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Jeff Dikeba180fd2007-10-16 01:27:00 -070081 if (n < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -070082 printk(UM_KERN_ERR "Couldn't read '%s', errno = %d\n", stat,
Jeff Dikeba180fd2007-10-16 01:27:00 -070083 errno);
Jeff Dike512b6fb2007-10-16 01:27:11 -070084 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
87 parent = FAILURE_PID;
88 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
Jeff Dikeba180fd2007-10-16 01:27:00 -070089 if (n != 1)
90 printk(UM_KERN_ERR "Failed to scan '%s'\n", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jeff Dikeef0470c2007-05-06 14:51:33 -070092 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95void os_stop_process(int pid)
96{
97 kill(pid, SIGSTOP);
98}
99
100void os_kill_process(int pid, int reap_child)
101{
102 kill(pid, SIGKILL);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700103 if (reap_child)
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800104 CATCH_EINTR(waitpid(pid, NULL, __WALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -0800107/* 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
111long 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* 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
127void os_kill_ptraced_process(int pid, int reap_child)
128{
129 kill(pid, SIGKILL);
130 ptrace(PTRACE_KILL, pid);
131 ptrace(PTRACE_CONT, pid);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700132 if (reap_child)
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800133 CATCH_EINTR(waitpid(pid, NULL, __WALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700136/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140int os_getpid(void)
141{
Jeff Dikeef0470c2007-05-06 14:51:33 -0700142 return syscall(__NR_getpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700145int os_getpgrp(void)
146{
147 return getpgrp();
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150int 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
Jeff Dikeba180fd2007-10-16 01:27:00 -0700156 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 (x ? PROT_EXEC : 0);
158
159 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
160 fd, off);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700161 if (loc == MAP_FAILED)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700162 return -errno;
163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
167{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700168 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 (x ? PROT_EXEC : 0));
170
Jeff Dikeba180fd2007-10-16 01:27:00 -0700171 if (mprotect(addr, len, prot) < 0)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700172 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700173
174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177int os_unmap_memory(void *addr, int len)
178{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700179 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Jeff Dikeba180fd2007-10-16 01:27:00 -0700181 err = munmap(addr, len);
182 if (err < 0)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700183 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Jeff Dike02dea082006-03-31 02:30:08 -0800187#ifndef MADV_REMOVE
Jeff Dikeb73781c2006-04-18 22:20:24 -0700188#define MADV_REMOVE KERNEL_MADV_REMOVE
Jeff Dike02dea082006-03-31 02:30:08 -0800189#endif
190
Jeff Dike97a1fcb2007-07-23 18:43:48 -0700191int os_drop_memory(void *addr, int length)
Jeff Dike02dea082006-03-31 02:30:08 -0800192{
193 int err;
194
195 err = madvise(addr, length, MADV_REMOVE);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700196 if (err < 0)
Jeff Dike02dea082006-03-31 02:30:08 -0800197 err = -errno;
198 return err;
199}
200
Jeff Dike36e45462007-05-06 14:51:11 -0700201int __init can_drop_memory(void)
Jeff Dike02dea082006-03-31 02:30:08 -0800202{
203 void *addr;
Jeff Dikee3104f52006-05-01 12:15:58 -0700204 int fd, ok = 0;
Jeff Dike02dea082006-03-31 02:30:08 -0800205
Jeff Dikeba180fd2007-10-16 01:27:00 -0700206 printk(UM_KERN_INFO "Checking host MADV_REMOVE support...");
Jeff Dike02dea082006-03-31 02:30:08 -0800207 fd = create_mem_file(UM_KERN_PAGE_SIZE);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700208 if (fd < 0) {
209 printk(UM_KERN_ERR "Creating test memory file failed, "
210 "err = %d\n", -fd);
Jeff Dikee3104f52006-05-01 12:15:58 -0700211 goto out;
Jeff Dike02dea082006-03-31 02:30:08 -0800212 }
213
214 addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
Jeff Dikeb73781c2006-04-18 22:20:24 -0700215 MAP_SHARED, fd, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700216 if (addr == MAP_FAILED) {
217 printk(UM_KERN_ERR "Mapping test memory file failed, "
218 "err = %d\n", -errno);
Jeff Dikee3104f52006-05-01 12:15:58 -0700219 goto out_close;
Jeff Dike02dea082006-03-31 02:30:08 -0800220 }
221
Jeff Dikeba180fd2007-10-16 01:27:00 -0700222 if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) {
223 printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno);
Jeff Dikee3104f52006-05-01 12:15:58 -0700224 goto out_unmap;
Jeff Dike02dea082006-03-31 02:30:08 -0800225 }
226
Jeff Dike5134d8f2008-02-08 04:22:08 -0800227 printk(UM_KERN_CONT "OK\n");
Jeff Dikee3104f52006-05-01 12:15:58 -0700228 ok = 1;
229
230out_unmap:
231 munmap(addr, UM_KERN_PAGE_SIZE);
232out_close:
233 close(fd);
234out:
235 return ok;
Jeff Dike02dea082006-03-31 02:30:08 -0800236}
237
Jeff Dikee64bd132006-07-10 04:45:07 -0700238void init_new_thread_signals(void)
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700239{
Jeff Dikee64bd132006-07-10 04:45:07 -0700240 set_handler(SIGSEGV, (__sighandler_t) sig_handler, SA_ONSTACK,
Jeff Dike61b63c52007-10-16 01:27:27 -0700241 SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
Jeff Dikee64bd132006-07-10 04:45:07 -0700242 set_handler(SIGTRAP, (__sighandler_t) sig_handler, SA_ONSTACK,
Jeff Dike61b63c52007-10-16 01:27:27 -0700243 SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
Jeff Dikee64bd132006-07-10 04:45:07 -0700244 set_handler(SIGFPE, (__sighandler_t) sig_handler, SA_ONSTACK,
Jeff Dike61b63c52007-10-16 01:27:27 -0700245 SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
Jeff Dikee64bd132006-07-10 04:45:07 -0700246 set_handler(SIGILL, (__sighandler_t) sig_handler, SA_ONSTACK,
Jeff Dike61b63c52007-10-16 01:27:27 -0700247 SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
Jeff Dikee64bd132006-07-10 04:45:07 -0700248 set_handler(SIGBUS, (__sighandler_t) sig_handler, SA_ONSTACK,
Jeff Dike61b63c52007-10-16 01:27:27 -0700249 SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700250 signal(SIGHUP, SIG_IGN);
251
Jeff Dike3a24ebf2008-02-04 22:31:16 -0800252 set_handler(SIGIO, (__sighandler_t) sig_handler,
253 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGALRM,
254 SIGVTALRM, -1);
255 signal(SIGWINCH, SIG_IGN);
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700256}
257
Jeff Dikefab95c52007-10-16 01:27:05 -0700258int run_kernel_thread(int (*fn)(void *), void *arg, jmp_buf **jmp_ptr)
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700259{
Jeff Dikead28e022006-04-18 22:21:41 -0700260 jmp_buf buf;
Jeff Dikea5df0d12006-07-14 00:24:02 -0700261 int n;
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700262
Jeff Dike1d7173b2006-01-18 17:42:49 -0800263 *jmp_ptr = &buf;
Jeff Dikea5df0d12006-07-14 00:24:02 -0700264 n = UML_SETJMP(&buf);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700265 if (n != 0)
Jeff Dikea5df0d12006-07-14 00:24:02 -0700266 return n;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800267 (*fn)(arg);
Jeff Dikea5df0d12006-07-14 00:24:02 -0700268 return 0;
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700269}