blob: e9c14329751281efa85b888a393539ea0c60a369 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <sys/mman.h>
11#include <sys/wait.h>
Jeff Dike02dea082006-03-31 02:30:08 -080012#include <sys/mman.h>
Arnd Bergmann5f4c6bc2006-10-02 02:18:37 -070013#include <sys/syscall.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "ptrace_user.h"
15#include "os.h"
16#include "user.h"
Gennady Sharapov60d339f62005-09-03 15:57:47 -070017#include "process.h"
18#include "irq_user.h"
19#include "kern_util.h"
Jeff Dike1d7173b2006-01-18 17:42:49 -080020#include "longjmp.h"
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -080021#include "skas_ptrace.h"
Jeff Dike02dea082006-03-31 02:30:08 -080022#include "kern_constants.h"
Paolo 'Blaisorblade' Giarrusso9ff1d362007-03-07 20:41:10 -080023#include "uml-config.h"
Jeff Dike36e45462007-05-06 14:51:11 -070024#include "init.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define ARBITRARY_ADDR -1
27#define FAILURE_PID -1
28
29#define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
30#define COMM_SCANF "%*[^)])"
31
32unsigned long os_process_pc(int pid)
33{
34 char proc_stat[STAT_PATH_LEN], buf[256];
35 unsigned long pc;
36 int fd, err;
37
38 sprintf(proc_stat, "/proc/%d/stat", pid);
39 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
40 if(fd < 0){
41 printk("os_process_pc - couldn't open '%s', err = %d\n",
42 proc_stat, -fd);
Jeff Dikeef0470c2007-05-06 14:51:33 -070043 return ARBITRARY_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 }
Jeff Dikea61f3342007-05-06 14:51:35 -070045 CATCH_EINTR(err = read(fd, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 if(err < 0){
47 printk("os_process_pc - couldn't read '%s', err = %d\n",
Jeff Dikea61f3342007-05-06 14:51:35 -070048 proc_stat, errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 os_close_file(fd);
Jeff Dikeef0470c2007-05-06 14:51:33 -070050 return ARBITRARY_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
52 os_close_file(fd);
53 pc = ARBITRARY_ADDR;
54 if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
55 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
56 "%*d %*d %*d %*d %*d %lu", &pc) != 1){
57 printk("os_process_pc - couldn't find pc in '%s'\n", buf);
58 }
Jeff Dikeef0470c2007-05-06 14:51:33 -070059 return pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
62int os_process_parent(int pid)
63{
64 char stat[STAT_PATH_LEN];
65 char data[256];
66 int parent, n, fd;
67
Jeff Dikeef0470c2007-05-06 14:51:33 -070068 if(pid == -1)
69 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
72 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
73 if(fd < 0){
74 printk("Couldn't open '%s', err = %d\n", stat, -fd);
Jeff Dikeef0470c2007-05-06 14:51:33 -070075 return FAILURE_PID;
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)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 os_close_file(fd);
80
81 if(n < 0){
Jeff Dikea61f3342007-05-06 14:51:35 -070082 printk("Couldn't read '%s', err = %d\n", stat, errno);
Jeff Dikeef0470c2007-05-06 14:51:33 -070083 return FAILURE_PID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
86 parent = FAILURE_PID;
87 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
88 if(n != 1)
89 printk("Failed to scan '%s'\n", data);
90
Jeff Dikeef0470c2007-05-06 14:51:33 -070091 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94void os_stop_process(int pid)
95{
96 kill(pid, SIGSTOP);
97}
98
99void os_kill_process(int pid, int reap_child)
100{
101 kill(pid, SIGKILL);
102 if(reap_child)
103 CATCH_EINTR(waitpid(pid, NULL, 0));
104
105}
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);
132 if(reap_child)
133 CATCH_EINTR(waitpid(pid, NULL, 0));
134}
135
Paolo 'Blaisorblade' Giarrusso9ff1d362007-03-07 20:41:10 -0800136#ifdef UML_CONFIG_MODE_TT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137void os_usr1_process(int pid)
138{
139 kill(pid, SIGUSR1);
140}
Paolo 'Blaisorblade' Giarrusso9ff1d362007-03-07 20:41:10 -0800141#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700143/* Don't use the glibc version, which caches the result in TLS. It misses some
144 * syscalls, and also breaks with clone(), which does not unshare the TLS.
145 */
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147int os_getpid(void)
148{
Jeff Dikeef0470c2007-05-06 14:51:33 -0700149 return syscall(__NR_getpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700152int os_getpgrp(void)
153{
154 return getpgrp();
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
158 int r, int w, int x)
159{
160 void *loc;
161 int prot;
162
163 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
164 (x ? PROT_EXEC : 0);
165
166 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
167 fd, off);
168 if(loc == MAP_FAILED)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700169 return -errno;
170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
174{
175 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
176 (x ? PROT_EXEC : 0));
177
178 if(mprotect(addr, len, prot) < 0)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700179 return -errno;
180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183int os_unmap_memory(void *addr, int len)
184{
185 int err;
186
187 err = munmap(addr, len);
188 if(err < 0)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700189 return -errno;
190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Jeff Dike02dea082006-03-31 02:30:08 -0800193#ifndef MADV_REMOVE
Jeff Dikeb73781c2006-04-18 22:20:24 -0700194#define MADV_REMOVE KERNEL_MADV_REMOVE
Jeff Dike02dea082006-03-31 02:30:08 -0800195#endif
196
Jeff Dike97a1fcb2007-07-23 18:43:48 -0700197int os_drop_memory(void *addr, int length)
Jeff Dike02dea082006-03-31 02:30:08 -0800198{
199 int err;
200
201 err = madvise(addr, length, MADV_REMOVE);
202 if(err < 0)
203 err = -errno;
204 return err;
205}
206
Jeff Dike36e45462007-05-06 14:51:11 -0700207int __init can_drop_memory(void)
Jeff Dike02dea082006-03-31 02:30:08 -0800208{
209 void *addr;
Jeff Dikee3104f52006-05-01 12:15:58 -0700210 int fd, ok = 0;
Jeff Dike02dea082006-03-31 02:30:08 -0800211
212 printk("Checking host MADV_REMOVE support...");
213 fd = create_mem_file(UM_KERN_PAGE_SIZE);
214 if(fd < 0){
215 printk("Creating test memory file failed, err = %d\n", -fd);
Jeff Dikee3104f52006-05-01 12:15:58 -0700216 goto out;
Jeff Dike02dea082006-03-31 02:30:08 -0800217 }
218
219 addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
Jeff Dikeb73781c2006-04-18 22:20:24 -0700220 MAP_SHARED, fd, 0);
Jeff Dike02dea082006-03-31 02:30:08 -0800221 if(addr == MAP_FAILED){
222 printk("Mapping test memory file failed, err = %d\n", -errno);
Jeff Dikee3104f52006-05-01 12:15:58 -0700223 goto out_close;
Jeff Dike02dea082006-03-31 02:30:08 -0800224 }
225
226 if(madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0){
227 printk("MADV_REMOVE failed, err = %d\n", -errno);
Jeff Dikee3104f52006-05-01 12:15:58 -0700228 goto out_unmap;
Jeff Dike02dea082006-03-31 02:30:08 -0800229 }
230
231 printk("OK\n");
Jeff Dikee3104f52006-05-01 12:15:58 -0700232 ok = 1;
233
234out_unmap:
235 munmap(addr, UM_KERN_PAGE_SIZE);
236out_close:
237 close(fd);
238out:
239 return ok;
Jeff Dike02dea082006-03-31 02:30:08 -0800240}
241
Jeff Dike231f7e92007-05-09 02:33:21 -0700242#ifdef UML_CONFIG_MODE_TT
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700243void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
244{
245 int flags = 0, pages;
246
247 if(sig_stack != NULL){
248 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
Jeff Dike1ffb9162007-05-06 14:51:22 -0700249 set_sigstack(sig_stack, pages * UM_KERN_PAGE_SIZE);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700250 flags = SA_ONSTACK;
251 }
Jeff Dike4b84c692006-09-25 23:33:04 -0700252 if(usr1_handler){
253 struct sigaction sa;
254
255 sa.sa_handler = usr1_handler;
256 sigemptyset(&sa.sa_mask);
257 sa.sa_flags = flags;
258 sa.sa_restorer = NULL;
259 if(sigaction(SIGUSR1, &sa, NULL) < 0)
260 panic("init_new_thread_stack - sigaction failed - "
261 "errno = %d\n", errno);
262 }
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700263}
Jeff Dike231f7e92007-05-09 02:33:21 -0700264#endif
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700265
Jeff Dikee64bd132006-07-10 04:45:07 -0700266void init_new_thread_signals(void)
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700267{
Jeff Dikee64bd132006-07-10 04:45:07 -0700268 set_handler(SIGSEGV, (__sighandler_t) sig_handler, SA_ONSTACK,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700269 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
Jeff Dikee64bd132006-07-10 04:45:07 -0700270 set_handler(SIGTRAP, (__sighandler_t) sig_handler, SA_ONSTACK,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700271 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
Jeff Dikee64bd132006-07-10 04:45:07 -0700272 set_handler(SIGFPE, (__sighandler_t) sig_handler, SA_ONSTACK,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700273 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
Jeff Dikee64bd132006-07-10 04:45:07 -0700274 set_handler(SIGILL, (__sighandler_t) sig_handler, SA_ONSTACK,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700275 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
Jeff Dikee64bd132006-07-10 04:45:07 -0700276 set_handler(SIGBUS, (__sighandler_t) sig_handler, SA_ONSTACK,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700277 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
278 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
Jeff Dikee64bd132006-07-10 04:45:07 -0700279 SA_ONSTACK, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM,
280 -1);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700281 signal(SIGHUP, SIG_IGN);
282
Jeff Dikee64bd132006-07-10 04:45:07 -0700283 init_irq_signals(1);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700284}
285
286int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
287{
Jeff Dikead28e022006-04-18 22:21:41 -0700288 jmp_buf buf;
Jeff Dikea5df0d12006-07-14 00:24:02 -0700289 int n;
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700290
Jeff Dike1d7173b2006-01-18 17:42:49 -0800291 *jmp_ptr = &buf;
Jeff Dikea5df0d12006-07-14 00:24:02 -0700292 n = UML_SETJMP(&buf);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800293 if(n != 0)
Jeff Dikea5df0d12006-07-14 00:24:02 -0700294 return n;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800295 (*fn)(arg);
Jeff Dikea5df0d12006-07-14 00:24:02 -0700296 return 0;
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700297}