process.c 5.6 KB
Newer Older
J
Jeff Dike 已提交
1 2
/*
 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5 6
 * Licensed under the GPL
 */

#include <stdio.h>
J
Jeff Dike 已提交
7
#include <unistd.h>
L
Linus Torvalds 已提交
8 9
#include <errno.h>
#include <signal.h>
10
#include <fcntl.h>
L
Linus Torvalds 已提交
11
#include <sys/mman.h>
J
Jeff Dike 已提交
12
#include <sys/ptrace.h>
L
Linus Torvalds 已提交
13
#include <sys/wait.h>
J
Jeff Dike 已提交
14 15 16 17
#include <asm/unistd.h>
#include "init.h"
#include "kern_constants.h"
#include "longjmp.h"
L
Linus Torvalds 已提交
18
#include "os.h"
19
#include "process.h"
20
#include "skas_ptrace.h"
J
Jeff Dike 已提交
21
#include "user.h"
L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30 31

#define ARBITRARY_ADDR -1
#define FAILURE_PID    -1

#define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
#define COMM_SCANF "%*[^)])"

unsigned long os_process_pc(int pid)
{
	char proc_stat[STAT_PATH_LEN], buf[256];
32
	unsigned long pc = ARBITRARY_ADDR;
L
Linus Torvalds 已提交
33 34 35
	int fd, err;

	sprintf(proc_stat, "/proc/%d/stat", pid);
36
	fd = open(proc_stat, O_RDONLY, 0);
J
Jeff Dike 已提交
37 38
	if (fd < 0) {
		printk(UM_KERN_ERR "os_process_pc - couldn't open '%s', "
39 40
		       "errno = %d\n", proc_stat, errno);
		goto out;
L
Linus Torvalds 已提交
41
	}
42
	CATCH_EINTR(err = read(fd, buf, sizeof(buf)));
J
Jeff Dike 已提交
43 44 45
	if (err < 0) {
		printk(UM_KERN_ERR "os_process_pc - couldn't read '%s', "
		       "err = %d\n", proc_stat, errno);
46
		goto out_close;
L
Linus Torvalds 已提交
47 48 49
	}
	os_close_file(fd);
	pc = ARBITRARY_ADDR;
J
Jeff Dike 已提交
50
	if (sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
51 52
		   "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
		   "%*d %*d %*d %*d %*d %lu", &pc) != 1)
J
Jeff Dike 已提交
53 54
		printk(UM_KERN_ERR "os_process_pc - couldn't find pc in '%s'\n",
		       buf);
55 56 57
 out_close:
	close(fd);
 out:
J
Jeff Dike 已提交
58
	return pc;
L
Linus Torvalds 已提交
59 60 61 62 63 64
}

int os_process_parent(int pid)
{
	char stat[STAT_PATH_LEN];
	char data[256];
65
	int parent = FAILURE_PID, n, fd;
L
Linus Torvalds 已提交
66

J
Jeff Dike 已提交
67
	if (pid == -1)
68
		return parent;
L
Linus Torvalds 已提交
69 70

	snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
71
	fd = open(stat, O_RDONLY, 0);
J
Jeff Dike 已提交
72
	if (fd < 0) {
73 74 75
		printk(UM_KERN_ERR "Couldn't open '%s', errno = %d\n", stat,
		       errno);
		return parent;
L
Linus Torvalds 已提交
76 77
	}

78
	CATCH_EINTR(n = read(fd, data, sizeof(data)));
79
	close(fd);
L
Linus Torvalds 已提交
80

J
Jeff Dike 已提交
81
	if (n < 0) {
82
		printk(UM_KERN_ERR "Couldn't read '%s', errno = %d\n", stat,
J
Jeff Dike 已提交
83
		       errno);
84
		return parent;
L
Linus Torvalds 已提交
85 86 87 88
	}

	parent = FAILURE_PID;
	n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
J
Jeff Dike 已提交
89 90
	if (n != 1)
		printk(UM_KERN_ERR "Failed to scan '%s'\n", data);
L
Linus Torvalds 已提交
91

J
Jeff Dike 已提交
92
	return parent;
L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101 102
}

void os_stop_process(int pid)
{
	kill(pid, SIGSTOP);
}

void os_kill_process(int pid, int reap_child)
{
	kill(pid, SIGKILL);
J
Jeff Dike 已提交
103
	if (reap_child)
104
		CATCH_EINTR(waitpid(pid, NULL, __WALL));
L
Linus Torvalds 已提交
105 106
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/* This is here uniquely to have access to the userspace errno, i.e. the one
 * used by ptrace in case of error.
 */

long os_ptrace_ldt(long pid, long addr, long data)
{
	int ret;

	ret = ptrace(PTRACE_LDT, pid, addr, data);

	if (ret < 0)
		return -errno;
	return ret;
}

L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129 130 131
/* Kill off a ptraced child by all means available.  kill it normally first,
 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
 * which it can't exit directly.
 */

void os_kill_ptraced_process(int pid, int reap_child)
{
	kill(pid, SIGKILL);
	ptrace(PTRACE_KILL, pid);
	ptrace(PTRACE_CONT, pid);
J
Jeff Dike 已提交
132
	if (reap_child)
133
		CATCH_EINTR(waitpid(pid, NULL, __WALL));
L
Linus Torvalds 已提交
134 135
}

136 137 138 139
/* Don't use the glibc version, which caches the result in TLS. It misses some
 * syscalls, and also breaks with clone(), which does not unshare the TLS.
 */

L
Linus Torvalds 已提交
140 141
int os_getpid(void)
{
J
Jeff Dike 已提交
142
	return syscall(__NR_getpid);
L
Linus Torvalds 已提交
143 144
}

J
Jeff Dike 已提交
145 146 147 148 149
int os_getpgrp(void)
{
	return getpgrp();
}

L
Linus Torvalds 已提交
150 151 152 153 154 155
int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
		  int r, int w, int x)
{
	void *loc;
	int prot;

J
Jeff Dike 已提交
156
	prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
L
Linus Torvalds 已提交
157 158 159 160
		(x ? PROT_EXEC : 0);

	loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
		     fd, off);
J
Jeff Dike 已提交
161
	if (loc == MAP_FAILED)
J
Jeff Dike 已提交
162 163
		return -errno;
	return 0;
L
Linus Torvalds 已提交
164 165 166 167
}

int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
{
J
Jeff Dike 已提交
168
	int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
L
Linus Torvalds 已提交
169 170
		    (x ? PROT_EXEC : 0));

J
Jeff Dike 已提交
171
	if (mprotect(addr, len, prot) < 0)
J
Jeff Dike 已提交
172
		return -errno;
J
Jeff Dike 已提交
173 174

	return 0;
L
Linus Torvalds 已提交
175 176 177 178
}

int os_unmap_memory(void *addr, int len)
{
J
Jeff Dike 已提交
179
	int err;
L
Linus Torvalds 已提交
180

J
Jeff Dike 已提交
181 182
	err = munmap(addr, len);
	if (err < 0)
J
Jeff Dike 已提交
183
		return -errno;
J
Jeff Dike 已提交
184
	return 0;
L
Linus Torvalds 已提交
185 186
}

J
Jeff Dike 已提交
187
#ifndef MADV_REMOVE
J
Jeff Dike 已提交
188
#define MADV_REMOVE KERNEL_MADV_REMOVE
J
Jeff Dike 已提交
189 190
#endif

J
Jeff Dike 已提交
191
int os_drop_memory(void *addr, int length)
J
Jeff Dike 已提交
192 193 194 195
{
	int err;

	err = madvise(addr, length, MADV_REMOVE);
J
Jeff Dike 已提交
196
	if (err < 0)
J
Jeff Dike 已提交
197 198 199 200
		err = -errno;
	return err;
}

J
Jeff Dike 已提交
201
int __init can_drop_memory(void)
J
Jeff Dike 已提交
202 203
{
	void *addr;
204
	int fd, ok = 0;
J
Jeff Dike 已提交
205

J
Jeff Dike 已提交
206
	printk(UM_KERN_INFO "Checking host MADV_REMOVE support...");
J
Jeff Dike 已提交
207
	fd = create_mem_file(UM_KERN_PAGE_SIZE);
J
Jeff Dike 已提交
208 209 210
	if (fd < 0) {
		printk(UM_KERN_ERR "Creating test memory file failed, "
		       "err = %d\n", -fd);
211
		goto out;
J
Jeff Dike 已提交
212 213 214
	}

	addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
J
Jeff Dike 已提交
215
		      MAP_SHARED, fd, 0);
J
Jeff Dike 已提交
216 217 218
	if (addr == MAP_FAILED) {
		printk(UM_KERN_ERR "Mapping test memory file failed, "
		       "err = %d\n", -errno);
219
		goto out_close;
J
Jeff Dike 已提交
220 221
	}

J
Jeff Dike 已提交
222 223
	if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) {
		printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno);
224
		goto out_unmap;
J
Jeff Dike 已提交
225 226 227
	}

	printk("OK\n");
228 229 230 231 232 233 234 235
	ok = 1;

out_unmap:
	munmap(addr, UM_KERN_PAGE_SIZE);
out_close:
	close(fd);
out:
	return ok;
J
Jeff Dike 已提交
236 237
}

238
void init_new_thread_signals(void)
239
{
240
	set_handler(SIGSEGV, (__sighandler_t) sig_handler, SA_ONSTACK,
J
Jeff Dike 已提交
241
		    SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
242
	set_handler(SIGTRAP, (__sighandler_t) sig_handler, SA_ONSTACK,
J
Jeff Dike 已提交
243
		    SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
244
	set_handler(SIGFPE, (__sighandler_t) sig_handler, SA_ONSTACK,
J
Jeff Dike 已提交
245
		    SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
246
	set_handler(SIGILL, (__sighandler_t) sig_handler, SA_ONSTACK,
J
Jeff Dike 已提交
247
		    SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
248
	set_handler(SIGBUS, (__sighandler_t) sig_handler, SA_ONSTACK,
J
Jeff Dike 已提交
249
		    SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
250 251
	signal(SIGHUP, SIG_IGN);

252
	init_irq_signals(1);
253 254
}

J
Jeff Dike 已提交
255
int run_kernel_thread(int (*fn)(void *), void *arg, jmp_buf **jmp_ptr)
256
{
257
	jmp_buf buf;
J
Jeff Dike 已提交
258
	int n;
259 260

	*jmp_ptr = &buf;
J
Jeff Dike 已提交
261
	n = UML_SETJMP(&buf);
J
Jeff Dike 已提交
262
	if (n != 0)
J
Jeff Dike 已提交
263
		return n;
264
	(*fn)(arg);
J
Jeff Dike 已提交
265
	return 0;
266
}