1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
4 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
14 #include <sys/ptrace.h>
15 #include <sys/prctl.h>
17 #include <asm/unistd.h>
21 #include <skas/skas.h>
23 void os_alarm_process(int pid)
31 void os_kill_process(int pid, int reap_child)
36 /* Block signals until child is reaped */
41 CATCH_EINTR(waitpid(pid, NULL, __WALL));
46 /* Kill off a ptraced child by all means available. kill it normally first,
47 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
48 * which it can't exit directly.
51 void os_kill_ptraced_process(int pid, int reap_child)
56 /* Block signals until child is reaped */
60 ptrace(PTRACE_KILL, pid);
61 ptrace(PTRACE_CONT, pid);
63 CATCH_EINTR(waitpid(pid, NULL, __WALL));
68 pid_t os_reap_child(void)
72 /* Try to reap a child */
73 return waitpid(-1, &status, WNOHANG);
76 /* Don't use the glibc version, which caches the result in TLS. It misses some
77 * syscalls, and also breaks with clone(), which does not unshare the TLS.
82 return syscall(__NR_getpid);
85 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
91 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
94 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
96 if (loc == MAP_FAILED)
101 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
103 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
104 (x ? PROT_EXEC : 0));
106 if (mprotect(addr, len, prot) < 0)
112 int os_unmap_memory(void *addr, int len)
116 err = munmap(addr, len);
123 #define MADV_REMOVE KERNEL_MADV_REMOVE
126 int os_drop_memory(void *addr, int length)
130 err = madvise(addr, length, MADV_REMOVE);
136 int __init can_drop_memory(void)
141 printk(UM_KERN_INFO "Checking host MADV_REMOVE support...");
142 fd = create_mem_file(UM_KERN_PAGE_SIZE);
144 printk(UM_KERN_ERR "Creating test memory file failed, "
149 addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
151 if (addr == MAP_FAILED) {
152 printk(UM_KERN_ERR "Mapping test memory file failed, "
153 "err = %d\n", -errno);
157 if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) {
158 printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno);
162 printk(UM_KERN_CONT "OK\n");
166 munmap(addr, UM_KERN_PAGE_SIZE);
173 void init_new_thread_signals(void)
175 set_handler(SIGSEGV);
176 set_handler(SIGTRAP);
180 signal(SIGHUP, SIG_IGN);
182 /* We (currently) only use the child reaper IRQ in seccomp mode */
184 set_handler(SIGCHLD);
185 signal(SIGWINCH, SIG_IGN);
188 void os_set_pdeathsig(void)
190 prctl(PR_SET_PDEATHSIG, SIGKILL);