2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
|
|
|
|
* Licensed under the GPL
|
|
|
|
*/
|
|
|
|
|
2005-09-22 00:40:10 +08:00
|
|
|
#include <unistd.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/wait.h>
|
2006-03-31 18:30:08 +08:00
|
|
|
#include <sys/mman.h>
|
2006-10-02 17:18:37 +08:00
|
|
|
#include <sys/syscall.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include "ptrace_user.h"
|
|
|
|
#include "os.h"
|
|
|
|
#include "user.h"
|
2005-09-04 06:57:47 +08:00
|
|
|
#include "process.h"
|
|
|
|
#include "irq_user.h"
|
|
|
|
#include "kern_util.h"
|
[PATCH] uml: implement soft interrupts
This patch implements soft interrupts. Interrupt enabling and disabling no
longer map to sigprocmask. Rather, a flag is set indicating whether
interrupts may be handled. If a signal comes in and interrupts are marked as
OK, then it is handled normally. If interrupts are marked as off, then the
signal handler simply returns after noting that a signal needs handling. When
interrupts are enabled later on, this pending signals flag is checked, and the
IRQ handlers are called at that point.
The point of this is to reduce the cost of local_irq_save et al, since they
are very much more common than the signals that they are enabling and
disabling. Soft interrupts produce a speed-up of ~25% on a kernel build.
Subtleties -
UML uses sigsetjmp/siglongjmp to switch contexts. sigsetjmp has been
wrapped in a save_flags-like macro which remembers the interrupt state at
setjmp time, and restores it when it is longjmp-ed back to.
The enable_signals function has to loop because the IRQ handler
disables interrupts before returning. enable_signals has to return with
signals enabled, and signals may come in between the disabling and the
return to enable_signals. So, it loops for as long as there are pending
signals, ensuring that signals are enabled when it finally returns, and
that there are no pending signals that need to be dealt with.
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-01-19 09:42:49 +08:00
|
|
|
#include "longjmp.h"
|
2006-02-25 05:03:55 +08:00
|
|
|
#include "skas_ptrace.h"
|
2006-03-31 18:30:08 +08:00
|
|
|
#include "kern_constants.h"
|
2007-03-08 12:41:10 +08:00
|
|
|
#include "uml-config.h"
|
2007-05-07 05:51:11 +08:00
|
|
|
#include "init.h"
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#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];
|
|
|
|
unsigned long pc;
|
|
|
|
int fd, err;
|
|
|
|
|
|
|
|
sprintf(proc_stat, "/proc/%d/stat", pid);
|
|
|
|
fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
|
|
|
|
if(fd < 0){
|
|
|
|
printk("os_process_pc - couldn't open '%s', err = %d\n",
|
|
|
|
proc_stat, -fd);
|
2007-05-07 05:51:33 +08:00
|
|
|
return ARBITRARY_ADDR;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-05-07 05:51:35 +08:00
|
|
|
CATCH_EINTR(err = read(fd, buf, sizeof(buf)));
|
2005-04-17 06:20:36 +08:00
|
|
|
if(err < 0){
|
|
|
|
printk("os_process_pc - couldn't read '%s', err = %d\n",
|
2007-05-07 05:51:35 +08:00
|
|
|
proc_stat, errno);
|
2005-04-17 06:20:36 +08:00
|
|
|
os_close_file(fd);
|
2007-05-07 05:51:33 +08:00
|
|
|
return ARBITRARY_ADDR;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
os_close_file(fd);
|
|
|
|
pc = ARBITRARY_ADDR;
|
|
|
|
if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
|
|
|
|
"%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
|
|
|
|
"%*d %*d %*d %*d %*d %lu", &pc) != 1){
|
|
|
|
printk("os_process_pc - couldn't find pc in '%s'\n", buf);
|
|
|
|
}
|
2007-05-07 05:51:33 +08:00
|
|
|
return pc;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_process_parent(int pid)
|
|
|
|
{
|
|
|
|
char stat[STAT_PATH_LEN];
|
|
|
|
char data[256];
|
|
|
|
int parent, n, fd;
|
|
|
|
|
2007-05-07 05:51:33 +08:00
|
|
|
if(pid == -1)
|
|
|
|
return -1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
|
|
|
|
fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
|
|
|
|
if(fd < 0){
|
|
|
|
printk("Couldn't open '%s', err = %d\n", stat, -fd);
|
2007-05-07 05:51:33 +08:00
|
|
|
return FAILURE_PID;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-05-07 05:51:35 +08:00
|
|
|
CATCH_EINTR(n = read(fd, data, sizeof(data)));
|
2005-04-17 06:20:36 +08:00
|
|
|
os_close_file(fd);
|
|
|
|
|
|
|
|
if(n < 0){
|
2007-05-07 05:51:35 +08:00
|
|
|
printk("Couldn't read '%s', err = %d\n", stat, errno);
|
2007-05-07 05:51:33 +08:00
|
|
|
return FAILURE_PID;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
parent = FAILURE_PID;
|
|
|
|
n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
|
|
|
|
if(n != 1)
|
|
|
|
printk("Failed to scan '%s'\n", data);
|
|
|
|
|
2007-05-07 05:51:33 +08:00
|
|
|
return parent;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void os_stop_process(int pid)
|
|
|
|
{
|
|
|
|
kill(pid, SIGSTOP);
|
|
|
|
}
|
|
|
|
|
|
|
|
void os_kill_process(int pid, int reap_child)
|
|
|
|
{
|
|
|
|
kill(pid, SIGKILL);
|
|
|
|
if(reap_child)
|
|
|
|
CATCH_EINTR(waitpid(pid, NULL, 0));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-02-25 05:03:55 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* 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);
|
|
|
|
if(reap_child)
|
|
|
|
CATCH_EINTR(waitpid(pid, NULL, 0));
|
|
|
|
}
|
|
|
|
|
2005-09-04 06:57:47 +08:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
int os_getpid(void)
|
|
|
|
{
|
2007-05-07 05:51:33 +08:00
|
|
|
return syscall(__NR_getpid);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-05-06 07:15:32 +08:00
|
|
|
int os_getpgrp(void)
|
|
|
|
{
|
|
|
|
return getpgrp();
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
|
|
|
|
(x ? PROT_EXEC : 0);
|
|
|
|
|
|
|
|
loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
|
|
|
|
fd, off);
|
|
|
|
if(loc == MAP_FAILED)
|
2007-05-07 05:51:33 +08:00
|
|
|
return -errno;
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
|
|
|
|
{
|
|
|
|
int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
|
|
|
|
(x ? PROT_EXEC : 0));
|
|
|
|
|
|
|
|
if(mprotect(addr, len, prot) < 0)
|
2007-05-07 05:51:33 +08:00
|
|
|
return -errno;
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_unmap_memory(void *addr, int len)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = munmap(addr, len);
|
|
|
|
if(err < 0)
|
2007-05-07 05:51:33 +08:00
|
|
|
return -errno;
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-03-31 18:30:08 +08:00
|
|
|
#ifndef MADV_REMOVE
|
2006-04-19 13:20:24 +08:00
|
|
|
#define MADV_REMOVE KERNEL_MADV_REMOVE
|
2006-03-31 18:30:08 +08:00
|
|
|
#endif
|
|
|
|
|
2007-07-24 09:43:48 +08:00
|
|
|
int os_drop_memory(void *addr, int length)
|
2006-03-31 18:30:08 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = madvise(addr, length, MADV_REMOVE);
|
|
|
|
if(err < 0)
|
|
|
|
err = -errno;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-05-07 05:51:11 +08:00
|
|
|
int __init can_drop_memory(void)
|
2006-03-31 18:30:08 +08:00
|
|
|
{
|
|
|
|
void *addr;
|
2006-05-02 03:15:58 +08:00
|
|
|
int fd, ok = 0;
|
2006-03-31 18:30:08 +08:00
|
|
|
|
|
|
|
printk("Checking host MADV_REMOVE support...");
|
|
|
|
fd = create_mem_file(UM_KERN_PAGE_SIZE);
|
|
|
|
if(fd < 0){
|
|
|
|
printk("Creating test memory file failed, err = %d\n", -fd);
|
2006-05-02 03:15:58 +08:00
|
|
|
goto out;
|
2006-03-31 18:30:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
|
2006-04-19 13:20:24 +08:00
|
|
|
MAP_SHARED, fd, 0);
|
2006-03-31 18:30:08 +08:00
|
|
|
if(addr == MAP_FAILED){
|
|
|
|
printk("Mapping test memory file failed, err = %d\n", -errno);
|
2006-05-02 03:15:58 +08:00
|
|
|
goto out_close;
|
2006-03-31 18:30:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0){
|
|
|
|
printk("MADV_REMOVE failed, err = %d\n", -errno);
|
2006-05-02 03:15:58 +08:00
|
|
|
goto out_unmap;
|
2006-03-31 18:30:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printk("OK\n");
|
2006-05-02 03:15:58 +08:00
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
out_unmap:
|
|
|
|
munmap(addr, UM_KERN_PAGE_SIZE);
|
|
|
|
out_close:
|
|
|
|
close(fd);
|
|
|
|
out:
|
|
|
|
return ok;
|
2006-03-31 18:30:08 +08:00
|
|
|
}
|
|
|
|
|
2006-07-10 19:45:07 +08:00
|
|
|
void init_new_thread_signals(void)
|
2005-09-04 06:57:47 +08:00
|
|
|
{
|
2006-07-10 19:45:07 +08:00
|
|
|
set_handler(SIGSEGV, (__sighandler_t) sig_handler, SA_ONSTACK,
|
2005-09-04 06:57:47 +08:00
|
|
|
SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
|
2006-07-10 19:45:07 +08:00
|
|
|
set_handler(SIGTRAP, (__sighandler_t) sig_handler, SA_ONSTACK,
|
2005-09-04 06:57:47 +08:00
|
|
|
SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
|
2006-07-10 19:45:07 +08:00
|
|
|
set_handler(SIGFPE, (__sighandler_t) sig_handler, SA_ONSTACK,
|
2005-09-04 06:57:47 +08:00
|
|
|
SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
|
2006-07-10 19:45:07 +08:00
|
|
|
set_handler(SIGILL, (__sighandler_t) sig_handler, SA_ONSTACK,
|
2005-09-04 06:57:47 +08:00
|
|
|
SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
|
2006-07-10 19:45:07 +08:00
|
|
|
set_handler(SIGBUS, (__sighandler_t) sig_handler, SA_ONSTACK,
|
2005-09-04 06:57:47 +08:00
|
|
|
SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
|
2006-07-10 19:45:07 +08:00
|
|
|
init_irq_signals(1);
|
2005-09-04 06:57:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
|
|
|
|
{
|
2006-04-19 13:21:41 +08:00
|
|
|
jmp_buf buf;
|
2006-07-14 15:24:02 +08:00
|
|
|
int n;
|
[PATCH] uml: implement soft interrupts
This patch implements soft interrupts. Interrupt enabling and disabling no
longer map to sigprocmask. Rather, a flag is set indicating whether
interrupts may be handled. If a signal comes in and interrupts are marked as
OK, then it is handled normally. If interrupts are marked as off, then the
signal handler simply returns after noting that a signal needs handling. When
interrupts are enabled later on, this pending signals flag is checked, and the
IRQ handlers are called at that point.
The point of this is to reduce the cost of local_irq_save et al, since they
are very much more common than the signals that they are enabling and
disabling. Soft interrupts produce a speed-up of ~25% on a kernel build.
Subtleties -
UML uses sigsetjmp/siglongjmp to switch contexts. sigsetjmp has been
wrapped in a save_flags-like macro which remembers the interrupt state at
setjmp time, and restores it when it is longjmp-ed back to.
The enable_signals function has to loop because the IRQ handler
disables interrupts before returning. enable_signals has to return with
signals enabled, and signals may come in between the disabling and the
return to enable_signals. So, it loops for as long as there are pending
signals, ensuring that signals are enabled when it finally returns, and
that there are no pending signals that need to be dealt with.
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-01-19 09:42:49 +08:00
|
|
|
|
|
|
|
*jmp_ptr = &buf;
|
2006-07-14 15:24:02 +08:00
|
|
|
n = UML_SETJMP(&buf);
|
[PATCH] uml: implement soft interrupts
This patch implements soft interrupts. Interrupt enabling and disabling no
longer map to sigprocmask. Rather, a flag is set indicating whether
interrupts may be handled. If a signal comes in and interrupts are marked as
OK, then it is handled normally. If interrupts are marked as off, then the
signal handler simply returns after noting that a signal needs handling. When
interrupts are enabled later on, this pending signals flag is checked, and the
IRQ handlers are called at that point.
The point of this is to reduce the cost of local_irq_save et al, since they
are very much more common than the signals that they are enabling and
disabling. Soft interrupts produce a speed-up of ~25% on a kernel build.
Subtleties -
UML uses sigsetjmp/siglongjmp to switch contexts. sigsetjmp has been
wrapped in a save_flags-like macro which remembers the interrupt state at
setjmp time, and restores it when it is longjmp-ed back to.
The enable_signals function has to loop because the IRQ handler
disables interrupts before returning. enable_signals has to return with
signals enabled, and signals may come in between the disabling and the
return to enable_signals. So, it loops for as long as there are pending
signals, ensuring that signals are enabled when it finally returns, and
that there are no pending signals that need to be dealt with.
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-01-19 09:42:49 +08:00
|
|
|
if(n != 0)
|
2006-07-14 15:24:02 +08:00
|
|
|
return n;
|
[PATCH] uml: implement soft interrupts
This patch implements soft interrupts. Interrupt enabling and disabling no
longer map to sigprocmask. Rather, a flag is set indicating whether
interrupts may be handled. If a signal comes in and interrupts are marked as
OK, then it is handled normally. If interrupts are marked as off, then the
signal handler simply returns after noting that a signal needs handling. When
interrupts are enabled later on, this pending signals flag is checked, and the
IRQ handlers are called at that point.
The point of this is to reduce the cost of local_irq_save et al, since they
are very much more common than the signals that they are enabling and
disabling. Soft interrupts produce a speed-up of ~25% on a kernel build.
Subtleties -
UML uses sigsetjmp/siglongjmp to switch contexts. sigsetjmp has been
wrapped in a save_flags-like macro which remembers the interrupt state at
setjmp time, and restores it when it is longjmp-ed back to.
The enable_signals function has to loop because the IRQ handler
disables interrupts before returning. enable_signals has to return with
signals enabled, and signals may come in between the disabling and the
return to enable_signals. So, it loops for as long as there are pending
signals, ensuring that signals are enabled when it finally returns, and
that there are no pending signals that need to be dealt with.
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-01-19 09:42:49 +08:00
|
|
|
(*fn)(arg);
|
2006-07-14 15:24:02 +08:00
|
|
|
return 0;
|
2005-09-04 06:57:47 +08:00
|
|
|
}
|