2019-08-25 03:49:18 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2006-01-18 18:42:46 -07:00
|
|
|
/*
|
2015-11-02 09:16:37 -07:00
|
|
|
* Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
|
2007-10-16 02:27:00 -06:00
|
|
|
* Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
2006-01-18 18:42:46 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sched.h>
|
2007-10-16 02:27:00 -06:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2006-01-18 18:42:46 -07:00
|
|
|
#include <sys/mman.h>
|
2007-10-16 02:27:00 -06:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <asm/unistd.h>
|
2012-10-07 20:27:32 -06:00
|
|
|
#include <as-layout.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <kern_util.h>
|
|
|
|
#include <mem.h>
|
|
|
|
#include <os.h>
|
|
|
|
#include <ptrace_user.h>
|
|
|
|
#include <registers.h>
|
|
|
|
#include <skas.h>
|
|
|
|
#include <sysdep/stub.h>
|
2017-05-04 00:15:10 -06:00
|
|
|
#include <linux/threads.h>
|
2006-01-18 18:42:46 -07:00
|
|
|
|
|
|
|
int is_skas_winch(int pid, int fd, void *data)
|
|
|
|
{
|
2011-08-18 13:08:19 -06:00
|
|
|
return pid == getpgrp();
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
2007-05-06 15:51:29 -06:00
|
|
|
static int ptrace_dump_regs(int pid)
|
|
|
|
{
|
2008-02-04 23:30:58 -07:00
|
|
|
unsigned long regs[MAX_REG_NR];
|
|
|
|
int i;
|
2007-05-06 15:51:29 -06:00
|
|
|
|
2008-02-04 23:30:58 -07:00
|
|
|
if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
|
|
|
|
return -errno;
|
2007-10-16 02:27:00 -06:00
|
|
|
|
|
|
|
printk(UM_KERN_ERR "Stub registers -\n");
|
|
|
|
for (i = 0; i < ARRAY_SIZE(regs); i++)
|
|
|
|
printk(UM_KERN_ERR "\t%d - %lx\n", i, regs[i]);
|
2007-05-06 15:51:29 -06:00
|
|
|
|
2008-02-04 23:30:58 -07:00
|
|
|
return 0;
|
2007-05-06 15:51:29 -06:00
|
|
|
}
|
|
|
|
|
2007-05-06 15:51:48 -06:00
|
|
|
/*
|
|
|
|
* Signals that are OK to receive in the stub - we'll just continue it.
|
|
|
|
* SIGWINCH will happen when UML is inside a detached screen.
|
|
|
|
*/
|
2015-11-02 09:16:37 -07:00
|
|
|
#define STUB_SIG_MASK ((1 << SIGALRM) | (1 << SIGWINCH))
|
2007-05-06 15:51:48 -06:00
|
|
|
|
|
|
|
/* Signals that the stub will finish with - anything else is an error */
|
2008-02-04 23:30:56 -07:00
|
|
|
#define STUB_DONE_MASK (1 << SIGTRAP)
|
2007-05-06 15:51:48 -06:00
|
|
|
|
|
|
|
void wait_stub_done(int pid)
|
2006-01-18 18:42:46 -07:00
|
|
|
{
|
2014-07-20 04:56:34 -06:00
|
|
|
int n, status, err;
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
while (1) {
|
2007-12-17 17:19:46 -07:00
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
|
2007-10-16 02:27:00 -06:00
|
|
|
if ((n < 0) || !WIFSTOPPED(status))
|
2007-05-06 15:51:48 -06:00
|
|
|
goto bad_wait;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
|
2007-05-06 15:51:48 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
err = ptrace(PTRACE_CONT, pid, 0, 0);
|
2008-02-04 23:30:58 -07:00
|
|
|
if (err) {
|
|
|
|
printk(UM_KERN_ERR "wait_stub_done : continue failed, "
|
|
|
|
"errno = %d\n", errno);
|
|
|
|
fatal_sigsegv();
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
2007-05-06 15:51:48 -06:00
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
|
2007-05-06 15:51:48 -06:00
|
|
|
return;
|
|
|
|
|
|
|
|
bad_wait:
|
|
|
|
err = ptrace_dump_regs(pid);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (err)
|
|
|
|
printk(UM_KERN_ERR "Failed to get registers from stub, "
|
|
|
|
"errno = %d\n", -err);
|
2008-02-04 23:30:58 -07:00
|
|
|
printk(UM_KERN_ERR "wait_stub_done : failed to wait for SIGTRAP, "
|
|
|
|
"pid = %d, n = %d, errno = %d, status = 0x%x\n", pid, n, errno,
|
|
|
|
status);
|
2014-07-20 04:56:34 -06:00
|
|
|
fatal_sigsegv();
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
extern unsigned long current_stub_stack(void);
|
|
|
|
|
2017-07-29 09:03:23 -06:00
|
|
|
static void get_skas_faultinfo(int pid, struct faultinfo *fi, unsigned long *aux_fp_regs)
|
2006-01-18 18:42:46 -07:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2017-07-29 09:03:23 -06:00
|
|
|
err = get_fp_registers(pid, aux_fp_regs);
|
2015-03-18 14:31:27 -06:00
|
|
|
if (err < 0) {
|
|
|
|
printk(UM_KERN_ERR "save_fp_registers returned %d\n",
|
|
|
|
err);
|
|
|
|
fatal_sigsegv();
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
2015-03-18 14:31:27 -06:00
|
|
|
err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
|
|
|
|
if (err) {
|
|
|
|
printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
|
|
|
|
"errno = %d\n", pid, errno);
|
|
|
|
fatal_sigsegv();
|
|
|
|
}
|
|
|
|
wait_stub_done(pid);
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2015-03-18 14:31:27 -06:00
|
|
|
/*
|
2017-07-05 16:34:05 -06:00
|
|
|
* faultinfo is prepared by the stub_segv_handler at start of
|
2015-03-18 14:31:27 -06:00
|
|
|
* the stub stack page. We just have to copy it.
|
|
|
|
*/
|
|
|
|
memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
|
uml: fix FP register corruption
Commit ee3d9bd4de1ed93d2a7ee41c331ed30a1c7b8acd ("uml: simplify SIGSEGV
handling"), while greatly simplifying the kernel SIGSEGV handler that
runs in the process address space, introduced a bug which corrupts FP
state in the process.
Previously, the SIGSEGV handler called the sigreturn system call by hand - it
couldn't return through the restorer provided to it because that could try to
call the libc restorer which likely wouldn't exist in the process address
space. So, it blocked off some signals, including SIGUSR1, on entry to the
SIGSEGV handler, queued a SIGUSR1 to itself, and invoked sigreturn. The
SIGUSR1 was delivered, and was visible to the UML kernel after sigreturn
finished.
The commit eliminated the signal masking and the call to sigreturn. The
handler simply hits itself with a SIGTRAP to let the UML kernel know that it
is finished. UML then restores the process registers, which effectively
longjmps the process out of the signal handler, skipping sigreturn's restoring
of register state and the signal mask.
The bug is that the host apparently sets used_fp to 0 when it saves the
process FP state in the sigcontext on the process signal stack. Thus, when
the process is longjmped out of the handler, its FP state is corrupt because
it wasn't saved on the context switch to the UML kernel.
This manifested itself as sleep hanging. For some reason, sleep uses floating
point in order to calculate the sleep interval. When a page fault corrupts
its FP state, it is faked into essentially sleeping forever.
This patch saves the FP state before entering the SIGSEGV handler and restores
it afterwards.
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-02-23 16:23:49 -07:00
|
|
|
|
2017-07-29 09:03:23 -06:00
|
|
|
err = put_fp_registers(pid, aux_fp_regs);
|
2015-03-18 14:31:27 -06:00
|
|
|
if (err < 0) {
|
|
|
|
printk(UM_KERN_ERR "put_fp_registers returned %d\n",
|
|
|
|
err);
|
|
|
|
fatal_sigsegv();
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 09:03:23 -06:00
|
|
|
static void handle_segv(int pid, struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
|
2006-01-18 18:42:46 -07:00
|
|
|
{
|
2017-07-29 09:03:23 -06:00
|
|
|
get_skas_faultinfo(pid, ®s->faultinfo, aux_fp_regs);
|
2007-10-16 02:26:58 -06:00
|
|
|
segv(regs->faultinfo, 0, 1, NULL);
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
/*
|
|
|
|
* To use the same value of using_sysemu as the caller, ask it that value
|
|
|
|
* (in local_using_sysemu
|
|
|
|
*/
|
|
|
|
static void handle_trap(int pid, struct uml_pt_regs *regs,
|
|
|
|
int local_using_sysemu)
|
2006-01-18 18:42:46 -07:00
|
|
|
{
|
|
|
|
int err, status;
|
|
|
|
|
2008-02-04 23:31:12 -07:00
|
|
|
if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
|
|
|
|
fatal_sigsegv();
|
|
|
|
|
2006-01-18 18:42:46 -07:00
|
|
|
if (!local_using_sysemu)
|
|
|
|
{
|
2011-08-18 13:12:19 -06:00
|
|
|
err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
|
2006-01-18 18:42:46 -07:00
|
|
|
__NR_getpid);
|
2008-02-04 23:30:58 -07:00
|
|
|
if (err < 0) {
|
|
|
|
printk(UM_KERN_ERR "handle_trap - nullifying syscall "
|
|
|
|
"failed, errno = %d\n", errno);
|
|
|
|
fatal_sigsegv();
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
|
|
|
err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
|
2008-02-04 23:30:58 -07:00
|
|
|
if (err < 0) {
|
|
|
|
printk(UM_KERN_ERR "handle_trap - continuing to end of "
|
|
|
|
"syscall failed, errno = %d\n", errno);
|
|
|
|
fatal_sigsegv();
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2007-12-17 17:19:46 -07:00
|
|
|
CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
|
2007-10-16 02:27:00 -06:00
|
|
|
if ((err < 0) || !WIFSTOPPED(status) ||
|
2008-02-04 23:30:58 -07:00
|
|
|
(WSTOPSIG(status) != SIGTRAP + 0x80)) {
|
|
|
|
err = ptrace_dump_regs(pid);
|
|
|
|
if (err)
|
|
|
|
printk(UM_KERN_ERR "Failed to get registers "
|
2007-10-16 02:27:00 -06:00
|
|
|
"from process, errno = %d\n", -err);
|
2008-02-04 23:30:58 -07:00
|
|
|
printk(UM_KERN_ERR "handle_trap - failed to wait at "
|
|
|
|
"end of syscall, errno = %d, status = %d\n",
|
|
|
|
errno, status);
|
|
|
|
fatal_sigsegv();
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
handle_syscall(regs);
|
|
|
|
}
|
|
|
|
|
2014-10-12 05:02:13 -06:00
|
|
|
extern char __syscall_stub_start[];
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2017-07-05 16:34:05 -06:00
|
|
|
/**
|
|
|
|
* userspace_tramp() - userspace trampoline
|
|
|
|
* @stack: pointer to the new userspace stack page, can be NULL, if? FIXME:
|
|
|
|
*
|
|
|
|
* The userspace trampoline is used to setup a new userspace process in start_userspace() after it was clone()'ed.
|
|
|
|
* This function will run on a temporary stack page.
|
|
|
|
* It ptrace()'es itself, then
|
|
|
|
* Two pages are mapped into the userspace address space:
|
|
|
|
* - STUB_CODE (with EXEC), which contains the skas stub code
|
|
|
|
* - STUB_DATA (with R/W), which contains a data page that is used to transfer certain data between the UML userspace process and the UML kernel.
|
|
|
|
* Also for the userspace process a SIGSEGV handler is installed to catch pagefaults in the userspace process.
|
|
|
|
* And last the process stops itself to give control to the UML kernel for this userspace process.
|
|
|
|
*
|
|
|
|
* Return: Always zero, otherwise the current userspace process is ended with non null exit() call
|
|
|
|
*/
|
2006-01-18 18:42:46 -07:00
|
|
|
static int userspace_tramp(void *stack)
|
|
|
|
{
|
|
|
|
void *addr;
|
2015-11-02 09:16:37 -07:00
|
|
|
int fd;
|
2015-03-18 14:31:27 -06:00
|
|
|
unsigned long long offset;
|
2006-01-18 18:42:46 -07:00
|
|
|
|
|
|
|
ptrace(PTRACE_TRACEME, 0, 0, 0);
|
|
|
|
|
2007-10-16 02:27:35 -06:00
|
|
|
signal(SIGTERM, SIG_DFL);
|
2008-02-04 23:30:56 -07:00
|
|
|
signal(SIGWINCH, SIG_IGN);
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2015-03-18 14:31:27 -06:00
|
|
|
/*
|
|
|
|
* This has a pte, but it can't be mapped in with the usual
|
|
|
|
* tlb_flush mechanism because this is part of that mechanism
|
|
|
|
*/
|
2014-10-12 05:02:13 -06:00
|
|
|
fd = phys_mapping(to_phys(__syscall_stub_start), &offset);
|
2015-03-18 14:31:27 -06:00
|
|
|
addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
|
|
|
|
PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
|
|
|
|
if (addr == MAP_FAILED) {
|
|
|
|
printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, "
|
|
|
|
"errno = %d\n", STUB_CODE, errno);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack != NULL) {
|
|
|
|
fd = phys_mapping(to_phys(stack), &offset);
|
|
|
|
addr = mmap((void *) STUB_DATA,
|
|
|
|
UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_FIXED | MAP_SHARED, fd, offset);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (addr == MAP_FAILED) {
|
2015-03-18 14:31:27 -06:00
|
|
|
printk(UM_KERN_ERR "mapping segfault stack "
|
|
|
|
"at 0x%lx failed, errno = %d\n",
|
|
|
|
STUB_DATA, errno);
|
2006-01-18 18:42:46 -07:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 14:31:27 -06:00
|
|
|
if (stack != NULL) {
|
2006-09-26 00:33:04 -06:00
|
|
|
struct sigaction sa;
|
|
|
|
|
uml: fix stub address calculations
The calculation of CONFIG_STUB_CODE and CONFIG_STUB_DATA didn't take into
account anything but 3G/1G and 2G/2G, leaving the other vmsplits out in the
cold.
I'd rather not duplicate the four known host vmsplit cases for each of these
symbols. I'd also like to calculate them based on the highest userspace
address.
The Kconfig language seems not to allow calculation of hex constants, so I
moved this to as-layout.h. CONFIG_STUB_CODE, CONFIG_STUB_DATA, and
CONFIG_STUB_START are now gone. In their place are STUB_CODE, STUB_DATA, and
STUB_START in as-layout.h.
i386 and x86_64 seem to differ as to whether an unadorned constant is an int
or a long, so I cast them to unsigned long so they can be printed
consistently. However, they are also used in stub.S, where C types don't work
so well. So, there are ASM_ versions of these constants for use in stub.S. I
also ifdef-ed the non-asm-friendly portion of as-layout.h.
With this in place, most of the rest of this patch is changing CONFIG_STUB_*
to STUB_*, except in stub.S, where they are changed to ASM_STUB_*.
defconfig has the old symbols deleted.
I also print these addresses out in case there is any problem mapping them on
the host.
The two stub.S files had some trailing whitespace, so that is cleaned up here.
[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-10-16 02:27:33 -06:00
|
|
|
unsigned long v = STUB_CODE +
|
2006-01-18 18:42:46 -07:00
|
|
|
(unsigned long) stub_segv_handler -
|
2014-10-12 05:02:13 -06:00
|
|
|
(unsigned long) __syscall_stub_start;
|
2006-01-18 18:42:46 -07:00
|
|
|
|
uml: fix stub address calculations
The calculation of CONFIG_STUB_CODE and CONFIG_STUB_DATA didn't take into
account anything but 3G/1G and 2G/2G, leaving the other vmsplits out in the
cold.
I'd rather not duplicate the four known host vmsplit cases for each of these
symbols. I'd also like to calculate them based on the highest userspace
address.
The Kconfig language seems not to allow calculation of hex constants, so I
moved this to as-layout.h. CONFIG_STUB_CODE, CONFIG_STUB_DATA, and
CONFIG_STUB_START are now gone. In their place are STUB_CODE, STUB_DATA, and
STUB_START in as-layout.h.
i386 and x86_64 seem to differ as to whether an unadorned constant is an int
or a long, so I cast them to unsigned long so they can be printed
consistently. However, they are also used in stub.S, where C types don't work
so well. So, there are ASM_ versions of these constants for use in stub.S. I
also ifdef-ed the non-asm-friendly portion of as-layout.h.
With this in place, most of the rest of this patch is changing CONFIG_STUB_*
to STUB_*, except in stub.S, where they are changed to ASM_STUB_*.
defconfig has the old symbols deleted.
I also print these addresses out in case there is any problem mapping them on
the host.
The two stub.S files had some trailing whitespace, so that is cleaned up here.
[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-10-16 02:27:33 -06:00
|
|
|
set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE);
|
2006-09-26 00:33:04 -06:00
|
|
|
sigemptyset(&sa.sa_mask);
|
2011-08-18 13:04:09 -06:00
|
|
|
sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
|
|
|
|
sa.sa_sigaction = (void *) v;
|
2006-09-26 00:33:04 -06:00
|
|
|
sa.sa_restorer = NULL;
|
2008-02-04 23:30:58 -07:00
|
|
|
if (sigaction(SIGSEGV, &sa, NULL) < 0) {
|
|
|
|
printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV "
|
|
|
|
"handler failed - errno = %d\n", errno);
|
|
|
|
exit(1);
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:11 -06:00
|
|
|
kill(os_getpid(), SIGSTOP);
|
2007-10-16 02:27:00 -06:00
|
|
|
return 0;
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int userspace_pid[NR_CPUS];
|
|
|
|
|
2017-07-05 16:34:05 -06:00
|
|
|
/**
|
|
|
|
* start_userspace() - prepare a new userspace process
|
|
|
|
* @stub_stack: pointer to the stub stack. Can be NULL, if? FIXME:
|
|
|
|
*
|
|
|
|
* Setups a new temporary stack page that is used while userspace_tramp() runs
|
|
|
|
* Clones the kernel process into a new userspace process, with FDs only.
|
|
|
|
*
|
|
|
|
* Return: When positive: the process id of the new userspace process,
|
|
|
|
* when negative: an error number.
|
|
|
|
* FIXME: can PIDs become negative?!
|
|
|
|
*/
|
2006-01-18 18:42:46 -07:00
|
|
|
int start_userspace(unsigned long stub_stack)
|
|
|
|
{
|
|
|
|
void *stack;
|
|
|
|
unsigned long sp;
|
2008-02-04 23:30:58 -07:00
|
|
|
int pid, status, n, flags, err;
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2017-07-05 16:34:05 -06:00
|
|
|
/* setup a temporary stack page */
|
2007-06-16 11:16:09 -06:00
|
|
|
stack = mmap(NULL, UM_KERN_PAGE_SIZE,
|
|
|
|
PROT_READ | PROT_WRITE | PROT_EXEC,
|
2006-01-18 18:42:46 -07:00
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2008-02-04 23:30:58 -07:00
|
|
|
if (stack == MAP_FAILED) {
|
|
|
|
err = -errno;
|
|
|
|
printk(UM_KERN_ERR "start_userspace : mmap failed, "
|
2008-02-04 23:31:21 -07:00
|
|
|
"errno = %d\n", errno);
|
2008-02-04 23:30:58 -07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-07-05 16:34:05 -06:00
|
|
|
/* set stack pointer to the end of the stack page, so it can grow downwards */
|
2007-06-16 11:16:09 -06:00
|
|
|
sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2015-03-18 14:31:27 -06:00
|
|
|
flags = CLONE_FILES | SIGCHLD;
|
2007-10-16 02:27:00 -06:00
|
|
|
|
2017-07-05 16:34:05 -06:00
|
|
|
/* clone into new userspace process */
|
2006-01-18 18:42:46 -07:00
|
|
|
pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
|
2008-02-04 23:30:58 -07:00
|
|
|
if (pid < 0) {
|
|
|
|
err = -errno;
|
|
|
|
printk(UM_KERN_ERR "start_userspace : clone failed, "
|
2008-02-04 23:31:21 -07:00
|
|
|
"errno = %d\n", errno);
|
2008-02-04 23:30:58 -07:00
|
|
|
return err;
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
|
|
|
do {
|
2007-12-17 17:19:46 -07:00
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
|
2008-02-04 23:30:58 -07:00
|
|
|
if (n < 0) {
|
|
|
|
err = -errno;
|
|
|
|
printk(UM_KERN_ERR "start_userspace : wait failed, "
|
2008-02-04 23:31:21 -07:00
|
|
|
"errno = %d\n", errno);
|
2008-02-04 23:30:58 -07:00
|
|
|
goto out_kill;
|
|
|
|
}
|
2015-11-02 09:16:37 -07:00
|
|
|
} while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGALRM));
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2008-02-04 23:30:58 -07:00
|
|
|
if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
|
|
|
|
err = -EINVAL;
|
|
|
|
printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got "
|
2008-02-04 23:31:21 -07:00
|
|
|
"status = %d\n", status);
|
2008-02-04 23:30:58 -07:00
|
|
|
goto out_kill;
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
|
2008-02-04 23:30:58 -07:00
|
|
|
(void *) PTRACE_O_TRACESYSGOOD) < 0) {
|
|
|
|
err = -errno;
|
|
|
|
printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS "
|
|
|
|
"failed, errno = %d\n", errno);
|
|
|
|
goto out_kill;
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2008-02-04 23:30:58 -07:00
|
|
|
if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
|
|
|
|
err = -errno;
|
|
|
|
printk(UM_KERN_ERR "start_userspace : munmap failed, "
|
|
|
|
"errno = %d\n", errno);
|
|
|
|
goto out_kill;
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
return pid;
|
2008-02-04 23:30:58 -07:00
|
|
|
|
|
|
|
out_kill:
|
|
|
|
os_kill_ptraced_process(pid, 1);
|
|
|
|
return err;
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
2017-07-29 09:03:23 -06:00
|
|
|
void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
|
2006-01-18 18:42:46 -07:00
|
|
|
{
|
|
|
|
int err, status, op, pid = userspace_pid[0];
|
2007-05-10 23:22:32 -06:00
|
|
|
/* To prevent races if using_sysemu changes under us.*/
|
|
|
|
int local_using_sysemu;
|
2012-08-01 16:49:17 -06:00
|
|
|
siginfo_t si;
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2012-05-22 22:25:15 -06:00
|
|
|
/* Handle any immediate reschedules or signals */
|
|
|
|
interrupt_end();
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
while (1) {
|
2015-11-02 09:16:37 -07:00
|
|
|
|
2008-02-04 23:30:58 -07:00
|
|
|
/*
|
|
|
|
* This can legitimately fail if the process loads a
|
|
|
|
* bogus value into a segment register. It will
|
|
|
|
* segfault and PTRACE_GETREGS will read that value
|
|
|
|
* out of the process. However, PTRACE_SETREGS will
|
|
|
|
* fail. In this case, there is nothing to do but
|
|
|
|
* just kill the process.
|
|
|
|
*/
|
2017-07-05 16:31:14 -06:00
|
|
|
if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp)) {
|
|
|
|
printk(UM_KERN_ERR "userspace - ptrace set regs "
|
|
|
|
"failed, errno = %d\n", errno);
|
2008-02-04 23:30:58 -07:00
|
|
|
fatal_sigsegv();
|
2017-07-05 16:31:14 -06:00
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2017-07-05 16:31:14 -06:00
|
|
|
if (put_fp_registers(pid, regs->fp)) {
|
|
|
|
printk(UM_KERN_ERR "userspace - ptrace set fp regs "
|
|
|
|
"failed, errno = %d\n", errno);
|
2011-09-14 17:21:23 -06:00
|
|
|
fatal_sigsegv();
|
2017-07-05 16:31:14 -06:00
|
|
|
}
|
2011-09-14 17:21:23 -06:00
|
|
|
|
2006-01-18 18:42:46 -07:00
|
|
|
/* Now we set local_using_sysemu to be used for one loop */
|
|
|
|
local_using_sysemu = get_using_sysemu();
|
|
|
|
|
2007-05-10 23:22:32 -06:00
|
|
|
op = SELECT_PTRACE_OPERATION(local_using_sysemu,
|
|
|
|
singlestepping(NULL));
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2008-02-04 23:30:58 -07:00
|
|
|
if (ptrace(op, pid, 0, 0)) {
|
|
|
|
printk(UM_KERN_ERR "userspace - ptrace continue "
|
|
|
|
"failed, op = %d, errno = %d\n", op, errno);
|
|
|
|
fatal_sigsegv();
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2007-12-17 17:19:46 -07:00
|
|
|
CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
|
2008-02-04 23:30:58 -07:00
|
|
|
if (err < 0) {
|
|
|
|
printk(UM_KERN_ERR "userspace - wait failed, "
|
|
|
|
"errno = %d\n", errno);
|
|
|
|
fatal_sigsegv();
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2007-10-16 02:26:58 -06:00
|
|
|
regs->is_user = 1;
|
2008-02-04 23:30:58 -07:00
|
|
|
if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
|
|
|
|
printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, "
|
|
|
|
"errno = %d\n", errno);
|
|
|
|
fatal_sigsegv();
|
|
|
|
}
|
2008-02-04 23:30:57 -07:00
|
|
|
|
2011-09-14 17:21:23 -06:00
|
|
|
if (get_fp_registers(pid, regs->fp)) {
|
|
|
|
printk(UM_KERN_ERR "userspace - get_fp_registers failed, "
|
|
|
|
"errno = %d\n", errno);
|
|
|
|
fatal_sigsegv();
|
|
|
|
}
|
|
|
|
|
2006-01-18 18:42:46 -07:00
|
|
|
UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (WIFSTOPPED(status)) {
|
2007-05-06 15:51:48 -06:00
|
|
|
int sig = WSTOPSIG(status);
|
2012-08-01 16:49:17 -06:00
|
|
|
|
2020-11-10 06:02:21 -07:00
|
|
|
/* These signal handlers need the si argument.
|
|
|
|
* The SIGIO and SIGALARM handlers which constitute the
|
|
|
|
* majority of invocations, do not use it.
|
|
|
|
*/
|
|
|
|
switch (sig) {
|
|
|
|
case SIGSEGV:
|
|
|
|
case SIGTRAP:
|
|
|
|
case SIGILL:
|
|
|
|
case SIGBUS:
|
|
|
|
case SIGFPE:
|
|
|
|
case SIGWINCH:
|
|
|
|
ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si);
|
|
|
|
break;
|
|
|
|
}
|
2012-08-01 16:49:17 -06:00
|
|
|
|
2008-02-08 05:22:08 -07:00
|
|
|
switch (sig) {
|
2006-01-18 18:42:46 -07:00
|
|
|
case SIGSEGV:
|
2015-03-18 14:31:27 -06:00
|
|
|
if (PTRACE_FULL_FAULTINFO) {
|
2007-10-16 02:27:00 -06:00
|
|
|
get_skas_faultinfo(pid,
|
2017-07-29 09:03:23 -06:00
|
|
|
®s->faultinfo, aux_fp_regs);
|
2013-07-19 03:31:36 -06:00
|
|
|
(*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si,
|
2012-08-01 16:49:17 -06:00
|
|
|
regs);
|
2007-05-06 15:51:48 -06:00
|
|
|
}
|
2017-07-29 09:03:23 -06:00
|
|
|
else handle_segv(pid, regs, aux_fp_regs);
|
2006-01-18 18:42:46 -07:00
|
|
|
break;
|
|
|
|
case SIGTRAP + 0x80:
|
|
|
|
handle_trap(pid, regs, local_using_sysemu);
|
|
|
|
break;
|
|
|
|
case SIGTRAP:
|
2013-07-19 03:31:36 -06:00
|
|
|
relay_signal(SIGTRAP, (struct siginfo *)&si, regs);
|
2006-01-18 18:42:46 -07:00
|
|
|
break;
|
2015-11-02 09:16:37 -07:00
|
|
|
case SIGALRM:
|
2007-10-16 02:27:25 -06:00
|
|
|
break;
|
|
|
|
case SIGIO:
|
2006-01-18 18:42:46 -07:00
|
|
|
case SIGILL:
|
|
|
|
case SIGBUS:
|
|
|
|
case SIGFPE:
|
|
|
|
case SIGWINCH:
|
2019-08-23 05:16:23 -06:00
|
|
|
block_signals_trace();
|
2013-07-19 03:31:36 -06:00
|
|
|
(*sig_info[sig])(sig, (struct siginfo *)&si, regs);
|
2019-08-23 05:16:23 -06:00
|
|
|
unblock_signals_trace();
|
2006-01-18 18:42:46 -07:00
|
|
|
break;
|
|
|
|
default:
|
2008-05-12 15:01:48 -06:00
|
|
|
printk(UM_KERN_ERR "userspace - child stopped "
|
2007-10-16 02:27:00 -06:00
|
|
|
"with signal %d\n", sig);
|
2008-02-04 23:30:58 -07:00
|
|
|
fatal_sigsegv();
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
pid = userspace_pid[0];
|
|
|
|
interrupt_end();
|
|
|
|
|
|
|
|
/* Avoid -ERESTARTSYS handling in host */
|
2007-10-16 02:27:00 -06:00
|
|
|
if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
|
2007-10-16 02:27:07 -06:00
|
|
|
PT_SYSCALL_NR(regs->gp) = -1;
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-06 15:51:48 -06:00
|
|
|
static unsigned long thread_regs[MAX_REG_NR];
|
2011-09-14 17:21:23 -06:00
|
|
|
static unsigned long thread_fp_regs[FP_SIZE];
|
2007-05-06 15:51:48 -06:00
|
|
|
|
|
|
|
static int __init init_thread_regs(void)
|
|
|
|
{
|
2011-09-14 17:21:23 -06:00
|
|
|
get_safe_registers(thread_regs, thread_fp_regs);
|
2007-05-06 15:51:48 -06:00
|
|
|
/* Set parent's instruction pointer to start of clone-stub */
|
uml: fix stub address calculations
The calculation of CONFIG_STUB_CODE and CONFIG_STUB_DATA didn't take into
account anything but 3G/1G and 2G/2G, leaving the other vmsplits out in the
cold.
I'd rather not duplicate the four known host vmsplit cases for each of these
symbols. I'd also like to calculate them based on the highest userspace
address.
The Kconfig language seems not to allow calculation of hex constants, so I
moved this to as-layout.h. CONFIG_STUB_CODE, CONFIG_STUB_DATA, and
CONFIG_STUB_START are now gone. In their place are STUB_CODE, STUB_DATA, and
STUB_START in as-layout.h.
i386 and x86_64 seem to differ as to whether an unadorned constant is an int
or a long, so I cast them to unsigned long so they can be printed
consistently. However, they are also used in stub.S, where C types don't work
so well. So, there are ASM_ versions of these constants for use in stub.S. I
also ifdef-ed the non-asm-friendly portion of as-layout.h.
With this in place, most of the rest of this patch is changing CONFIG_STUB_*
to STUB_*, except in stub.S, where they are changed to ASM_STUB_*.
defconfig has the old symbols deleted.
I also print these addresses out in case there is any problem mapping them on
the host.
The two stub.S files had some trailing whitespace, so that is cleaned up here.
[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-10-16 02:27:33 -06:00
|
|
|
thread_regs[REGS_IP_INDEX] = STUB_CODE +
|
2007-05-06 15:51:48 -06:00
|
|
|
(unsigned long) stub_clone_handler -
|
2014-10-12 05:02:13 -06:00
|
|
|
(unsigned long) __syscall_stub_start;
|
uml: fix stub address calculations
The calculation of CONFIG_STUB_CODE and CONFIG_STUB_DATA didn't take into
account anything but 3G/1G and 2G/2G, leaving the other vmsplits out in the
cold.
I'd rather not duplicate the four known host vmsplit cases for each of these
symbols. I'd also like to calculate them based on the highest userspace
address.
The Kconfig language seems not to allow calculation of hex constants, so I
moved this to as-layout.h. CONFIG_STUB_CODE, CONFIG_STUB_DATA, and
CONFIG_STUB_START are now gone. In their place are STUB_CODE, STUB_DATA, and
STUB_START in as-layout.h.
i386 and x86_64 seem to differ as to whether an unadorned constant is an int
or a long, so I cast them to unsigned long so they can be printed
consistently. However, they are also used in stub.S, where C types don't work
so well. So, there are ASM_ versions of these constants for use in stub.S. I
also ifdef-ed the non-asm-friendly portion of as-layout.h.
With this in place, most of the rest of this patch is changing CONFIG_STUB_*
to STUB_*, except in stub.S, where they are changed to ASM_STUB_*.
defconfig has the old symbols deleted.
I also print these addresses out in case there is any problem mapping them on
the host.
The two stub.S files had some trailing whitespace, so that is cleaned up here.
[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-10-16 02:27:33 -06:00
|
|
|
thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE -
|
2007-05-06 15:51:48 -06:00
|
|
|
sizeof(void *);
|
|
|
|
#ifdef __SIGNAL_FRAMESIZE
|
|
|
|
thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__initcall(init_thread_regs);
|
|
|
|
|
2006-01-18 18:42:46 -07:00
|
|
|
int copy_context_skas0(unsigned long new_stack, int pid)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
unsigned long current_stack = current_stub_stack();
|
|
|
|
struct stub_data *data = (struct stub_data *) current_stack;
|
|
|
|
struct stub_data *child_data = (struct stub_data *) new_stack;
|
2007-10-16 02:27:05 -06:00
|
|
|
unsigned long long new_offset;
|
2006-01-18 18:42:46 -07:00
|
|
|
int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
/*
|
|
|
|
* prepare offset and fd of child's stack as argument for parent's
|
2006-01-18 18:42:46 -07:00
|
|
|
* and child's mmap2 calls
|
|
|
|
*/
|
2015-11-02 09:16:37 -07:00
|
|
|
*data = ((struct stub_data) {
|
|
|
|
.offset = MMAP_OFFSET(new_offset),
|
|
|
|
.fd = new_fd
|
|
|
|
});
|
2007-10-16 02:27:25 -06:00
|
|
|
|
2007-05-06 15:51:48 -06:00
|
|
|
err = ptrace_setregs(pid, thread_regs);
|
2008-02-04 23:30:58 -07:00
|
|
|
if (err < 0) {
|
|
|
|
err = -errno;
|
|
|
|
printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS "
|
|
|
|
"failed, pid = %d, errno = %d\n", pid, -err);
|
|
|
|
return err;
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2011-09-14 17:21:23 -06:00
|
|
|
err = put_fp_registers(pid, thread_fp_regs);
|
|
|
|
if (err < 0) {
|
|
|
|
printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers "
|
|
|
|
"failed, pid = %d, err = %d\n", pid, err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-01-18 18:42:46 -07:00
|
|
|
/* set a well known return code for detection of child write failure */
|
|
|
|
child_data->err = 12345678;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
/*
|
|
|
|
* Wait, until parent has finished its work: read child's pid from
|
2006-01-18 18:42:46 -07:00
|
|
|
* parent's stack, and check, if bad result.
|
|
|
|
*/
|
2007-05-06 15:51:48 -06:00
|
|
|
err = ptrace(PTRACE_CONT, pid, 0, 0);
|
2008-02-04 23:30:58 -07:00
|
|
|
if (err) {
|
|
|
|
err = -errno;
|
|
|
|
printk(UM_KERN_ERR "Failed to continue new process, pid = %d, "
|
|
|
|
"errno = %d\n", pid, errno);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-05-06 15:51:48 -06:00
|
|
|
wait_stub_done(pid);
|
2006-01-18 18:42:46 -07:00
|
|
|
|
|
|
|
pid = data->err;
|
2008-02-04 23:30:58 -07:00
|
|
|
if (pid < 0) {
|
|
|
|
printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports "
|
|
|
|
"error %d\n", -pid);
|
|
|
|
return pid;
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
/*
|
|
|
|
* Wait, until child has finished too: read child's result from
|
2006-01-18 18:42:46 -07:00
|
|
|
* child's stack and check it.
|
|
|
|
*/
|
2007-05-06 15:51:48 -06:00
|
|
|
wait_stub_done(pid);
|
2008-02-04 23:30:58 -07:00
|
|
|
if (child_data->err != STUB_DATA) {
|
|
|
|
printk(UM_KERN_ERR "copy_context_skas0 - stub-child reports "
|
|
|
|
"error %ld\n", child_data->err);
|
|
|
|
err = child_data->err;
|
|
|
|
goto out_kill;
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
|
|
|
if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
|
2008-02-04 23:30:58 -07:00
|
|
|
(void *)PTRACE_O_TRACESYSGOOD) < 0) {
|
|
|
|
err = -errno;
|
|
|
|
printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS "
|
|
|
|
"failed, errno = %d\n", errno);
|
|
|
|
goto out_kill;
|
|
|
|
}
|
2006-01-18 18:42:46 -07:00
|
|
|
|
|
|
|
return pid;
|
2008-02-04 23:30:58 -07:00
|
|
|
|
|
|
|
out_kill:
|
|
|
|
os_kill_ptraced_process(pid, 1);
|
|
|
|
return err;
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
[PATCH] uml: thread creation tidying
fork on UML has always somewhat subtle. The underlying cause has been the
need to initialize a stack for the new process. The only portable way to
initialize a new stack is to set it as the alternate signal stack and take a
signal. The signal handler does whatever initialization is needed and jumps
back to the original stack, where the fork processing is finished. The basic
context switching mechanism is a jmp_buf for each process. You switch to a
new process by longjmping to its jmp_buf.
Now that UML has its own implementation of setjmp and longjmp, and I can poke
around inside a jmp_buf without fear that libc will change the structure, a
much simpler mechanism is possible. The jmpbuf can simply be initialized by
hand.
This eliminates -
the need to set up and remove the alternate signal stack
sending and handling a signal
the signal blocking needed around the stack switching, since
there is no stack switching
setting up the jmp_buf needed to jump back to the original
stack after the new one is set up
In addition, since jmp_buf is now defined by UML, and not by libc, it can be
embedded in the thread struct. This makes it unnecessary to have it exist on
the stack, where it used to be. It also simplifies interfaces, since the
switch jmp_buf used to be a void * inside the thread struct, and functions
which took it as an argument needed to define a jmp_buf variable and assign it
from the void *.
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-09-27 02:50:40 -06:00
|
|
|
void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
|
2006-01-18 18:42:46 -07:00
|
|
|
{
|
[PATCH] uml: thread creation tidying
fork on UML has always somewhat subtle. The underlying cause has been the
need to initialize a stack for the new process. The only portable way to
initialize a new stack is to set it as the alternate signal stack and take a
signal. The signal handler does whatever initialization is needed and jumps
back to the original stack, where the fork processing is finished. The basic
context switching mechanism is a jmp_buf for each process. You switch to a
new process by longjmping to its jmp_buf.
Now that UML has its own implementation of setjmp and longjmp, and I can poke
around inside a jmp_buf without fear that libc will change the structure, a
much simpler mechanism is possible. The jmpbuf can simply be initialized by
hand.
This eliminates -
the need to set up and remove the alternate signal stack
sending and handling a signal
the signal blocking needed around the stack switching, since
there is no stack switching
setting up the jmp_buf needed to jump back to the original
stack after the new one is set up
In addition, since jmp_buf is now defined by UML, and not by libc, it can be
embedded in the thread struct. This makes it unnecessary to have it exist on
the stack, where it used to be. It also simplifies interfaces, since the
switch jmp_buf used to be a void * inside the thread struct, and functions
which took it as an argument needed to define a jmp_buf variable and assign it
from the void *.
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-09-27 02:50:40 -06:00
|
|
|
(*buf)[0].JB_IP = (unsigned long) handler;
|
2007-05-10 23:22:31 -06:00
|
|
|
(*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
|
|
|
|
sizeof(void *);
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
2006-02-07 13:58:43 -07:00
|
|
|
#define INIT_JMP_NEW_THREAD 0
|
[PATCH] uml: thread creation tidying
fork on UML has always somewhat subtle. The underlying cause has been the
need to initialize a stack for the new process. The only portable way to
initialize a new stack is to set it as the alternate signal stack and take a
signal. The signal handler does whatever initialization is needed and jumps
back to the original stack, where the fork processing is finished. The basic
context switching mechanism is a jmp_buf for each process. You switch to a
new process by longjmping to its jmp_buf.
Now that UML has its own implementation of setjmp and longjmp, and I can poke
around inside a jmp_buf without fear that libc will change the structure, a
much simpler mechanism is possible. The jmpbuf can simply be initialized by
hand.
This eliminates -
the need to set up and remove the alternate signal stack
sending and handling a signal
the signal blocking needed around the stack switching, since
there is no stack switching
setting up the jmp_buf needed to jump back to the original
stack after the new one is set up
In addition, since jmp_buf is now defined by UML, and not by libc, it can be
embedded in the thread struct. This makes it unnecessary to have it exist on
the stack, where it used to be. It also simplifies interfaces, since the
switch jmp_buf used to be a void * inside the thread struct, and functions
which took it as an argument needed to define a jmp_buf variable and assign it
from the void *.
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-09-27 02:50:40 -06:00
|
|
|
#define INIT_JMP_CALLBACK 1
|
|
|
|
#define INIT_JMP_HALT 2
|
|
|
|
#define INIT_JMP_REBOOT 3
|
2006-01-18 18:42:46 -07:00
|
|
|
|
[PATCH] uml: thread creation tidying
fork on UML has always somewhat subtle. The underlying cause has been the
need to initialize a stack for the new process. The only portable way to
initialize a new stack is to set it as the alternate signal stack and take a
signal. The signal handler does whatever initialization is needed and jumps
back to the original stack, where the fork processing is finished. The basic
context switching mechanism is a jmp_buf for each process. You switch to a
new process by longjmping to its jmp_buf.
Now that UML has its own implementation of setjmp and longjmp, and I can poke
around inside a jmp_buf without fear that libc will change the structure, a
much simpler mechanism is possible. The jmpbuf can simply be initialized by
hand.
This eliminates -
the need to set up and remove the alternate signal stack
sending and handling a signal
the signal blocking needed around the stack switching, since
there is no stack switching
setting up the jmp_buf needed to jump back to the original
stack after the new one is set up
In addition, since jmp_buf is now defined by UML, and not by libc, it can be
embedded in the thread struct. This makes it unnecessary to have it exist on
the stack, where it used to be. It also simplifies interfaces, since the
switch jmp_buf used to be a void * inside the thread struct, and functions
which took it as an argument needed to define a jmp_buf variable and assign it
from the void *.
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-09-27 02:50:40 -06:00
|
|
|
void switch_threads(jmp_buf *me, jmp_buf *you)
|
2006-01-18 18:42:46 -07:00
|
|
|
{
|
2007-10-16 02:27:00 -06:00
|
|
|
if (UML_SETJMP(me) == 0)
|
[PATCH] uml: thread creation tidying
fork on UML has always somewhat subtle. The underlying cause has been the
need to initialize a stack for the new process. The only portable way to
initialize a new stack is to set it as the alternate signal stack and take a
signal. The signal handler does whatever initialization is needed and jumps
back to the original stack, where the fork processing is finished. The basic
context switching mechanism is a jmp_buf for each process. You switch to a
new process by longjmping to its jmp_buf.
Now that UML has its own implementation of setjmp and longjmp, and I can poke
around inside a jmp_buf without fear that libc will change the structure, a
much simpler mechanism is possible. The jmpbuf can simply be initialized by
hand.
This eliminates -
the need to set up and remove the alternate signal stack
sending and handling a signal
the signal blocking needed around the stack switching, since
there is no stack switching
setting up the jmp_buf needed to jump back to the original
stack after the new one is set up
In addition, since jmp_buf is now defined by UML, and not by libc, it can be
embedded in the thread struct. This makes it unnecessary to have it exist on
the stack, where it used to be. It also simplifies interfaces, since the
switch jmp_buf used to be a void * inside the thread struct, and functions
which took it as an argument needed to define a jmp_buf variable and assign it
from the void *.
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-09-27 02:50:40 -06:00
|
|
|
UML_LONGJMP(you, 1);
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
2006-04-18 23:21:41 -06:00
|
|
|
static jmp_buf initial_jmpbuf;
|
2006-01-18 18:42:46 -07:00
|
|
|
|
|
|
|
/* XXX Make these percpu */
|
|
|
|
static void (*cb_proc)(void *arg);
|
|
|
|
static void *cb_arg;
|
2006-04-18 23:21:41 -06:00
|
|
|
static jmp_buf *cb_back;
|
2006-01-18 18:42:46 -07:00
|
|
|
|
[PATCH] uml: thread creation tidying
fork on UML has always somewhat subtle. The underlying cause has been the
need to initialize a stack for the new process. The only portable way to
initialize a new stack is to set it as the alternate signal stack and take a
signal. The signal handler does whatever initialization is needed and jumps
back to the original stack, where the fork processing is finished. The basic
context switching mechanism is a jmp_buf for each process. You switch to a
new process by longjmping to its jmp_buf.
Now that UML has its own implementation of setjmp and longjmp, and I can poke
around inside a jmp_buf without fear that libc will change the structure, a
much simpler mechanism is possible. The jmpbuf can simply be initialized by
hand.
This eliminates -
the need to set up and remove the alternate signal stack
sending and handling a signal
the signal blocking needed around the stack switching, since
there is no stack switching
setting up the jmp_buf needed to jump back to the original
stack after the new one is set up
In addition, since jmp_buf is now defined by UML, and not by libc, it can be
embedded in the thread struct. This makes it unnecessary to have it exist on
the stack, where it used to be. It also simplifies interfaces, since the
switch jmp_buf used to be a void * inside the thread struct, and functions
which took it as an argument needed to define a jmp_buf variable and assign it
from the void *.
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-09-27 02:50:40 -06:00
|
|
|
int start_idle_thread(void *stack, jmp_buf *switch_buf)
|
2006-01-18 18:42:46 -07:00
|
|
|
{
|
2006-07-14 01:24:02 -06:00
|
|
|
int n;
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2011-08-18 13:04:39 -06:00
|
|
|
set_handler(SIGWINCH);
|
2006-01-18 18:42:46 -07:00
|
|
|
|
2007-05-06 15:51:40 -06:00
|
|
|
/*
|
|
|
|
* Can't use UML_SETJMP or UML_LONGJMP here because they save
|
|
|
|
* and restore signals, with the possible side-effect of
|
|
|
|
* trying to handle any signals which came when they were
|
|
|
|
* blocked, which can't be done on this stack.
|
|
|
|
* Signals must be blocked when jumping back here and restored
|
|
|
|
* after returning to the jumper.
|
|
|
|
*/
|
|
|
|
n = setjmp(initial_jmpbuf);
|
2008-02-08 05:22:08 -07:00
|
|
|
switch (n) {
|
2006-01-18 18:42:46 -07:00
|
|
|
case INIT_JMP_NEW_THREAD:
|
2015-03-28 02:59:46 -06:00
|
|
|
(*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
|
[PATCH] uml: thread creation tidying
fork on UML has always somewhat subtle. The underlying cause has been the
need to initialize a stack for the new process. The only portable way to
initialize a new stack is to set it as the alternate signal stack and take a
signal. The signal handler does whatever initialization is needed and jumps
back to the original stack, where the fork processing is finished. The basic
context switching mechanism is a jmp_buf for each process. You switch to a
new process by longjmping to its jmp_buf.
Now that UML has its own implementation of setjmp and longjmp, and I can poke
around inside a jmp_buf without fear that libc will change the structure, a
much simpler mechanism is possible. The jmpbuf can simply be initialized by
hand.
This eliminates -
the need to set up and remove the alternate signal stack
sending and handling a signal
the signal blocking needed around the stack switching, since
there is no stack switching
setting up the jmp_buf needed to jump back to the original
stack after the new one is set up
In addition, since jmp_buf is now defined by UML, and not by libc, it can be
embedded in the thread struct. This makes it unnecessary to have it exist on
the stack, where it used to be. It also simplifies interfaces, since the
switch jmp_buf used to be a void * inside the thread struct, and functions
which took it as an argument needed to define a jmp_buf variable and assign it
from the void *.
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-09-27 02:50:40 -06:00
|
|
|
(*switch_buf)[0].JB_SP = (unsigned long) stack +
|
2007-05-10 23:22:31 -06:00
|
|
|
UM_THREAD_SIZE - sizeof(void *);
|
2006-01-18 18:42:46 -07:00
|
|
|
break;
|
|
|
|
case INIT_JMP_CALLBACK:
|
|
|
|
(*cb_proc)(cb_arg);
|
2007-05-06 15:51:40 -06:00
|
|
|
longjmp(*cb_back, 1);
|
2006-01-18 18:42:46 -07:00
|
|
|
break;
|
|
|
|
case INIT_JMP_HALT:
|
|
|
|
kmalloc_ok = 0;
|
2007-10-16 02:27:00 -06:00
|
|
|
return 0;
|
2006-01-18 18:42:46 -07:00
|
|
|
case INIT_JMP_REBOOT:
|
|
|
|
kmalloc_ok = 0;
|
2007-10-16 02:27:00 -06:00
|
|
|
return 1;
|
2006-01-18 18:42:46 -07:00
|
|
|
default:
|
2008-02-04 23:30:58 -07:00
|
|
|
printk(UM_KERN_ERR "Bad sigsetjmp return in "
|
|
|
|
"start_idle_thread - %d\n", n);
|
|
|
|
fatal_sigsegv();
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
2007-05-06 15:51:40 -06:00
|
|
|
longjmp(*switch_buf, 1);
|
2018-06-15 08:42:56 -06:00
|
|
|
|
|
|
|
/* unreachable */
|
|
|
|
printk(UM_KERN_ERR "impossible long jump!");
|
|
|
|
fatal_sigsegv();
|
|
|
|
return 0;
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void initial_thread_cb_skas(void (*proc)(void *), void *arg)
|
|
|
|
{
|
2006-04-18 23:21:41 -06:00
|
|
|
jmp_buf here;
|
2006-01-18 18:42:46 -07:00
|
|
|
|
|
|
|
cb_proc = proc;
|
|
|
|
cb_arg = arg;
|
|
|
|
cb_back = &here;
|
|
|
|
|
2019-08-23 05:16:23 -06:00
|
|
|
block_signals_trace();
|
2007-10-16 02:27:00 -06:00
|
|
|
if (UML_SETJMP(&here) == 0)
|
2006-04-18 23:21:41 -06:00
|
|
|
UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
|
2019-08-23 05:16:23 -06:00
|
|
|
unblock_signals_trace();
|
2006-01-18 18:42:46 -07:00
|
|
|
|
|
|
|
cb_proc = NULL;
|
|
|
|
cb_arg = NULL;
|
|
|
|
cb_back = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void halt_skas(void)
|
|
|
|
{
|
2019-08-23 05:16:23 -06:00
|
|
|
block_signals_trace();
|
2006-04-18 23:21:41 -06:00
|
|
|
UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void reboot_skas(void)
|
|
|
|
{
|
2019-08-23 05:16:23 -06:00
|
|
|
block_signals_trace();
|
2006-04-18 23:21:41 -06:00
|
|
|
UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|
|
|
|
|
2007-10-16 02:26:58 -06:00
|
|
|
void __switch_mm(struct mm_id *mm_idp)
|
2006-01-18 18:42:46 -07:00
|
|
|
{
|
2015-03-18 14:31:27 -06:00
|
|
|
userspace_pid[0] = mm_idp->u.pid;
|
2006-01-18 18:42:46 -07:00
|
|
|
}
|