Files

212 lines
4.0 KiB
C
Raw Permalink Normal View History

// SPDX-License-Identifier: GPL-2.0
2007-10-16 01:27:00 -07:00
/*
2015-11-02 16:16:37 +00:00
* Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
2007-10-16 01:27:00 -07:00
* Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
2005-04-16 15:20:36 -07:00
*/
#include <stdio.h>
2013-08-17 18:46:00 +02:00
#include <stdlib.h>
2007-10-16 01:27:00 -07:00
#include <unistd.h>
2005-04-16 15:20:36 -07:00
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
2025-10-27 08:18:12 +08:00
#include <limits.h>
#include <linux/futex.h>
2005-04-16 15:20:36 -07:00
#include <sys/mman.h>
2007-10-16 01:27:00 -07:00
#include <sys/ptrace.h>
2024-10-24 22:28:25 +08:00
#include <sys/prctl.h>
2005-04-16 15:20:36 -07:00
#include <sys/wait.h>
2007-10-16 01:27:00 -07:00
#include <asm/unistd.h>
#include <init.h>
#include <longjmp.h>
#include <os.h>
#include <skas/skas.h>
2005-04-16 15:20:36 -07:00
2015-11-02 16:16:37 +00:00
void os_alarm_process(int pid)
{
if (pid <= 0)
return;
2015-11-02 16:16:37 +00:00
kill(pid, SIGALRM);
}
2005-04-16 15:20:36 -07:00
void os_kill_process(int pid, int reap_child)
{
if (pid <= 0)
return;
/* Block signals until child is reaped */
block_signals();
2005-04-16 15:20:36 -07:00
kill(pid, SIGKILL);
2007-10-16 01:27:00 -07:00
if (reap_child)
CATCH_EINTR(waitpid(pid, NULL, __WALL));
unblock_signals();
2005-04-16 15:20:36 -07: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)
{
if (pid <= 0)
return;
/* Block signals until child is reaped */
block_signals();
2005-04-16 15:20:36 -07:00
kill(pid, SIGKILL);
ptrace(PTRACE_KILL, pid);
ptrace(PTRACE_CONT, pid);
2007-10-16 01:27:00 -07:00
if (reap_child)
CATCH_EINTR(waitpid(pid, NULL, __WALL));
unblock_signals();
}
pid_t os_reap_child(void)
{
int status;
/* Try to reap a child */
return waitpid(-1, &status, WNOHANG);
2005-04-16 15:20:36 -07: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-16 15:20:36 -07:00
int os_getpid(void)
{
2007-05-06 14:51:33 -07:00
return syscall(__NR_getpid);
2005-04-16 15:20:36 -07: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;
2007-10-16 01:27:00 -07:00
prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
2005-04-16 15:20:36 -07:00
(x ? PROT_EXEC : 0);
loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
fd, off);
2007-10-16 01:27:00 -07:00
if (loc == MAP_FAILED)
2007-05-06 14:51:33 -07:00
return -errno;
return 0;
2005-04-16 15:20:36 -07:00
}
int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
{
2007-10-16 01:27:00 -07:00
int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
2005-04-16 15:20:36 -07:00
(x ? PROT_EXEC : 0));
2007-10-16 01:27:00 -07:00
if (mprotect(addr, len, prot) < 0)
2007-05-06 14:51:33 -07:00
return -errno;
2007-10-16 01:27:00 -07:00
return 0;
2005-04-16 15:20:36 -07:00
}
int os_unmap_memory(void *addr, int len)
{
2007-10-16 01:27:00 -07:00
int err;
2005-04-16 15:20:36 -07:00
2007-10-16 01:27:00 -07:00
err = munmap(addr, len);
if (err < 0)
2007-05-06 14:51:33 -07:00
return -errno;
2007-10-16 01:27:00 -07:00
return 0;
2005-04-16 15:20:36 -07:00
}
2006-03-31 02:30:08 -08:00
#ifndef MADV_REMOVE
2006-04-18 22:20:24 -07:00
#define MADV_REMOVE KERNEL_MADV_REMOVE
2006-03-31 02:30:08 -08:00
#endif
2007-07-23 18:43:48 -07:00
int os_drop_memory(void *addr, int length)
2006-03-31 02:30:08 -08:00
{
int err;
err = madvise(addr, length, MADV_REMOVE);
2007-10-16 01:27:00 -07:00
if (err < 0)
2006-03-31 02:30:08 -08:00
err = -errno;
return err;
}
2007-05-06 14:51:11 -07:00
int __init can_drop_memory(void)
2006-03-31 02:30:08 -08:00
{
void *addr;
2006-05-01 12:15:58 -07:00
int fd, ok = 0;
2006-03-31 02:30:08 -08:00
2007-10-16 01:27:00 -07:00
printk(UM_KERN_INFO "Checking host MADV_REMOVE support...");
2006-03-31 02:30:08 -08:00
fd = create_mem_file(UM_KERN_PAGE_SIZE);
2007-10-16 01:27:00 -07:00
if (fd < 0) {
printk(UM_KERN_ERR "Creating test memory file failed, "
"err = %d\n", -fd);
2006-05-01 12:15:58 -07:00
goto out;
2006-03-31 02:30:08 -08:00
}
addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
2006-04-18 22:20:24 -07:00
MAP_SHARED, fd, 0);
2007-10-16 01:27:00 -07:00
if (addr == MAP_FAILED) {
printk(UM_KERN_ERR "Mapping test memory file failed, "
"err = %d\n", -errno);
2006-05-01 12:15:58 -07:00
goto out_close;
2006-03-31 02:30:08 -08:00
}
2007-10-16 01:27:00 -07:00
if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) {
printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno);
2006-05-01 12:15:58 -07:00
goto out_unmap;
2006-03-31 02:30:08 -08:00
}
2008-02-08 04:22:08 -08:00
printk(UM_KERN_CONT "OK\n");
2006-05-01 12:15:58 -07:00
ok = 1;
out_unmap:
munmap(addr, UM_KERN_PAGE_SIZE);
out_close:
close(fd);
out:
return ok;
2006-03-31 02:30:08 -08:00
}
2006-07-10 04:45:07 -07:00
void init_new_thread_signals(void)
{
2011-08-18 20:04:39 +01:00
set_handler(SIGSEGV);
set_handler(SIGTRAP);
set_handler(SIGFPE);
set_handler(SIGILL);
set_handler(SIGBUS);
signal(SIGHUP, SIG_IGN);
2011-08-18 20:04:39 +01:00
set_handler(SIGIO);
/* We (currently) only use the child reaper IRQ in seccomp mode */
if (using_seccomp)
set_handler(SIGCHLD);
2008-02-04 22:31:16 -08:00
signal(SIGWINCH, SIG_IGN);
}
2024-10-24 22:28:25 +08:00
void os_set_pdeathsig(void)
{
prctl(PR_SET_PDEATHSIG, SIGKILL);
}
2025-10-27 08:18:12 +08:00
int os_futex_wait(void *uaddr, unsigned int val)
{
int r;
CATCH_EINTR(r = syscall(__NR_futex, uaddr, FUTEX_WAIT, val,
NULL, NULL, 0));
return r < 0 ? -errno : r;
}
int os_futex_wake(void *uaddr)
{
int r;
CATCH_EINTR(r = syscall(__NR_futex, uaddr, FUTEX_WAKE, INT_MAX,
NULL, NULL, 0));
return r < 0 ? -errno : r;
}