From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 253EC270551 for ; Sun, 2 Nov 2025 09:50:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762077005; cv=none; b=SS95V/rZ7OPjxe2bQ7uQu++2ky02d+/80WNHtTU++uuoVFsTMN8s5ffCZSEUAOC4rGWpd2RsBxuQh6qjuin4kWYiG/4W8zxNnq5PVw96McLOy/1QDOhLzPgIAFoOCgDjqyaVqfpVvSAlQVTgMdS8tk7tY8TJZLEN7Jy7zEdfW8c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762077005; c=relaxed/simple; bh=Q1RsL2/EtjIgu5QYIPwEJ1zosyelA3KCSVkAQjDY+RA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cXNyLoB5qDAIpb46k19YXBSHOxXv9tKjBCxYzNW4MUxaKkV7P88qJCrRn1exrgWD4Jso4B513eHdtSXHLEG98+0Ia4BapHrEJBcD6qnoMFHU80WShhh5hiZ7vcA9RMG54dKgR9CQneVX1QUTIssMKlUpshbb/uIIoNdwrjiWfVc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Fl9gbhWU; arc=none smtp.client-ip=209.85.215.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Fl9gbhWU" Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-b9a5b5b47bfso192401a12.1 for ; Sun, 02 Nov 2025 01:50:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762077002; x=1762681802; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sZpI4L1Kh98JdhM/QsHv9mwFGGWJGOR2fxfWgHIsbW8=; b=Fl9gbhWU4bYCCFThPOgBZQ91jRbfpdA1UL0AYp9JyE1/x93s+dwRTqsLhf21/rFKdE 5SIvbQVc9snHxGz+V1hTCK5NVV3vuYVeb3P4r3VIuSM4PONKMh/KFWCreEOOsLhGhX3e 1YRYgqLetZlqcmHqJ3wtSq8HQpoq++nbC9zgsAFtVmxYG3SL7NQcl0vXPGPvlaCBjFeX w/u2eVsJznA3RPVrcnilNT6/8OJdnfUq5G1kX4XucRfzSLqAQgAsIwLgkFzy4rsY1lP1 FjGAA+e4Piq9RSZ4xIbz3l9/sTSL90biBDlkwkto6aAhU9aILsCiQI0/lwDb4Qgvh+Qw osGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762077002; x=1762681802; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sZpI4L1Kh98JdhM/QsHv9mwFGGWJGOR2fxfWgHIsbW8=; b=q5FxxuD18Ztlhmoehk8Tub2HZ6Ppc5b2KIDe2aycAMMoucuFllzSEj3Z0IP2uc31hL 0L4ZwgLYfIftYzS5q+pk4uZ/KvUQNQZUyR4+8p+KypsYSOudqv44+7ArrIG7I5WrlcIB c5jpvveQN0KlBdEK8ahZLWOV7L/ZhQcXuKZme8pfOVOdRjSda18Lv0BzILr/TOuo5IQA NkoLiYDjEO4Yg2ma0YZ917YNQwCfxFS0C1V7/JnWnyFiKsKlcfnZkZwq7N0D6RSYMwW6 t/2nBEovJCDnHciCtZE7oSMRkDW4bdeC3+rUub6of+KsaiNk7j7FzlHXlG02D6YlIEDh eFRw== X-Forwarded-Encrypted: i=1; AJvYcCX1jIzXWoc8dqLcdB8e7lIOJuU+1wzIHCgRgZPT092CzXs0XJzSd3SDSWKjGM9DJbyfhuepjPWcXVpk6jU=@vger.kernel.org X-Gm-Message-State: AOJu0YyP3ilqYU4nLH9ixjKwkD9NPKT7aWEUqHlOlwhPajmfYzTRwyeh ibqOPgJwMeE5RtHR5/JIlkRpkca0Y3p9YP6ZggwkxsdUb5smiI38Nbjd X-Gm-Gg: ASbGnct/icFZNJCQN4L7SvZXfIL0yq7Xlv/tJ6Ex0FDrUlKLMsp1MwtUYxvnYdg/HmS 1M0E5H0XofYfJK0V31RCV74C1ZsCPZ6G3q+nxjHElwvfqziMz+aFkmpAsysGbXL3WQjvk8MaoCn HO/cCg66xVLEBV2BrFDi89LgIc3czAFmX63r0zvKoPDM/t+v4FbZ61f7nbtvjd4fsDwTqg8NI5r NKX9w5CctWbIfuyRKZ6R4BefSyf3pX780AyCl0Q4ALHr1nswtutDChu7+tlw6chGxqhpqj/53Fm T9LT+y5xOrDqANNhgnTg0rDGk9JUJ1+Er58/oMxbCq3b27YzDEcIrvfAQrXFEq/jBUKkEZTSten XgJ9ulAh9n+Giy3gVAWFgMhsoaoP46ZSl8E+SHxq8d7BO4Bd1JLEyPdgQh6/TxP5MRo6ar2DLsE CAbWyTTdjzzMO3xxEUUSbsPazsnoAYToxTf+kvsIKAWYD3rA== X-Google-Smtp-Source: AGHT+IEbHt8HzruA82vaWyVMP115xJROv7lZ5AWsz7H0jSQB1/KL43tJCy4khao1owqwLyjejtW9ng== X-Received: by 2002:a17:902:ce8b:b0:295:560a:e499 with SMTP id d9443c01a7336-295560ae620mr65873175ad.5.1762077002176; Sun, 02 Nov 2025 01:50:02 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-295586482c6sm45045515ad.22.2025.11.02.01.49.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Nov 2025 01:50:00 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id 0AE49112380F; Sun, 2 Nov 2025 18:49:55 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH v12 09/13] x86/um: nommu: signal handling Date: Sun, 2 Nov 2025 18:49:34 +0900 Message-ID: <32debc0728ce22cd4db50cdf1cd4e8db430ad402.1762075876.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This commit updates the behavior of signal handling under !MMU environment. It adds the alignment code for signal frame as the frame is used in userspace as-is. floating point register is carefully handling upon entry/leave of syscall routine so that signal handlers can read/write the contents of the register. It also adds the follow up routine for SIGSEGV as a signal delivery runs in the same stack frame while we have to avoid endless SIGSEGV. Signed-off-by: Hajime Tazaki --- arch/um/include/shared/kern_util.h | 4 + arch/um/nommu/Makefile | 2 +- arch/um/nommu/os-Linux/signal.c | 8 + arch/um/nommu/trap.c | 201 ++++++++++++++++++++++++++ arch/um/os-Linux/signal.c | 3 +- arch/x86/um/nommu/do_syscall_64.c | 6 + arch/x86/um/nommu/os-Linux/mcontext.c | 11 ++ arch/x86/um/shared/sysdep/mcontext.h | 1 + arch/x86/um/shared/sysdep/ptrace.h | 2 +- 9 files changed, 235 insertions(+), 3 deletions(-) create mode 100644 arch/um/nommu/trap.c diff --git a/arch/um/include/shared/kern_util.h b/arch/um/include/shared/kern_util.h index 7798f16a4677..46c8d6336ca1 100644 --- a/arch/um/include/shared/kern_util.h +++ b/arch/um/include/shared/kern_util.h @@ -70,4 +70,8 @@ void um_idle_sleep(void); void kasan_map_memory(void *start, size_t len); +#ifndef CONFIG_MMU +extern void nommu_relay_signal(void *ptr); +#endif + #endif diff --git a/arch/um/nommu/Makefile b/arch/um/nommu/Makefile index baab7c2f57c2..096221590cfd 100644 --- a/arch/um/nommu/Makefile +++ b/arch/um/nommu/Makefile @@ -1,3 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y := os-Linux/ +obj-y := trap.o os-Linux/ diff --git a/arch/um/nommu/os-Linux/signal.c b/arch/um/nommu/os-Linux/signal.c index 19043b9652e2..6febb178dcda 100644 --- a/arch/um/nommu/os-Linux/signal.c +++ b/arch/um/nommu/os-Linux/signal.c @@ -5,6 +5,7 @@ #include #include #include +#include void sigsys_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs, void *ptr) @@ -14,3 +15,10 @@ void sigsys_handler(int sig, struct siginfo *si, /* hook syscall via SIGSYS */ set_mc_sigsys_hook(mc); } + +void nommu_relay_signal(void *ptr) +{ + mcontext_t *mc = (mcontext_t *) ptr; + + set_mc_relay_signal(mc); +} diff --git a/arch/um/nommu/trap.c b/arch/um/nommu/trap.c new file mode 100644 index 000000000000..430297517455 --- /dev/null +++ b/arch/um/nommu/trap.c @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * Note this is constrained to return 0, -EFAULT, -EACCES, -ENOMEM by + * segv(). + */ +int handle_page_fault(unsigned long address, unsigned long ip, + int is_write, int is_user, int *code_out) +{ + /* !MMU has no pagefault */ + return -EFAULT; +} + +static void show_segv_info(struct uml_pt_regs *regs) +{ + struct task_struct *tsk = current; + struct faultinfo *fi = UPT_FAULTINFO(regs); + + if (!unhandled_signal(tsk, SIGSEGV)) + return; + + pr_warn_ratelimited("%s%s[%d]: segfault at %lx ip %p sp %p error %x", + task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG, + tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi), + (void *)UPT_IP(regs), (void *)UPT_SP(regs), + fi->error_code); +} + +static void bad_segv(struct faultinfo fi, unsigned long ip) +{ + current->thread.arch.faultinfo = fi; + force_sig_fault(SIGSEGV, SEGV_ACCERR, (void __user *) FAULT_ADDRESS(fi)); +} + +void fatal_sigsegv(void) +{ + force_fatal_sig(SIGSEGV); + do_signal(¤t->thread.regs); + /* + * This is to tell gcc that we're not returning - do_signal + * can, in general, return, but in this case, it's not, since + * we just got a fatal SIGSEGV queued. + */ + os_dump_core(); +} + +/** + * segv_handler() - the SIGSEGV handler + * @sig: the signal number + * @unused_si: the signal info struct; unused in this handler + * @regs: the ptrace register information + * + * The handler first extracts the faultinfo from the UML ptrace regs struct. + * If the userfault did not happen in an UML userspace process, bad_segv is called. + * Otherwise the signal did happen in a cloned userspace process, handle it. + */ +void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs, + void *mc) +{ + struct faultinfo *fi = UPT_FAULTINFO(regs); + + /* !MMU specific part; detection of userspace */ + /* mark is_user=1 when the IP is from userspace code. */ + if (UPT_IP(regs) > uml_reserved && UPT_IP(regs) < high_physmem) + regs->is_user = 1; + + if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) { + show_segv_info(regs); + bad_segv(*fi, UPT_IP(regs)); + return; + } + segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs, mc); + + /* !MMU specific part; detection of userspace */ + relay_signal(sig, unused_si, regs, mc); +} + +/* + * We give a *copy* of the faultinfo in the regs to segv. + * This must be done, since nesting SEGVs could overwrite + * the info in the regs. A pointer to the info then would + * give us bad data! + */ +unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, + struct uml_pt_regs *regs, void *mc) +{ + int si_code; + int err; + int is_write = FAULT_WRITE(fi); + unsigned long address = FAULT_ADDRESS(fi); + + if (!is_user && regs) + current->thread.segv_regs = container_of(regs, struct pt_regs, regs); + + if (current->mm == NULL) { + show_regs(container_of(regs, struct pt_regs, regs)); + panic("Segfault with no mm"); + } else if (!is_user && address > PAGE_SIZE && address < TASK_SIZE) { + show_regs(container_of(regs, struct pt_regs, regs)); + panic("Kernel tried to access user memory at addr 0x%lx, ip 0x%lx", + address, ip); + } + + if (SEGV_IS_FIXABLE(&fi)) + err = handle_page_fault(address, ip, is_write, is_user, + &si_code); + else { + err = -EFAULT; + /* + * A thread accessed NULL, we get a fault, but CR2 is invalid. + * This code is used in __do_copy_from_user() of TT mode. + * XXX tt mode is gone, so maybe this isn't needed any more + */ + address = 0; + } + + if (!err) + goto out; + else if (!is_user && arch_fixup(ip, regs)) + goto out; + + if (!is_user) { + show_regs(container_of(regs, struct pt_regs, regs)); + panic("Kernel mode fault at addr 0x%lx, ip 0x%lx", + address, ip); + } + + show_segv_info(regs); + + if (err == -EACCES) { + current->thread.arch.faultinfo = fi; + force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address); + } else { + WARN_ON_ONCE(err != -EFAULT); + current->thread.arch.faultinfo = fi; + force_sig_fault(SIGSEGV, si_code, (void __user *) address); + } + +out: + if (regs) + current->thread.segv_regs = NULL; + + return 0; +} + +void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs, + void *mc) +{ + int code, err; + + /* !MMU specific part; detection of userspace */ + /* mark is_user=1 when the IP is from userspace code. */ + if (UPT_IP(regs) > uml_reserved && UPT_IP(regs) < high_physmem) + regs->is_user = 1; + + if (!UPT_IS_USER(regs)) { + if (sig == SIGBUS) + pr_err("Bus error - the host /dev/shm or /tmp mount likely just ran out of space\n"); + panic("Kernel mode signal %d", sig); + } + /* if is_user==1, set return to userspace sig handler to relay signal */ + nommu_relay_signal(mc); + + arch_examine_signal(sig, regs); + + /* Is the signal layout for the signal known? + * Signal data must be scrubbed to prevent information leaks. + */ + code = si->si_code; + err = si->si_errno; + if ((err == 0) && (siginfo_layout(sig, code) == SIL_FAULT)) { + struct faultinfo *fi = UPT_FAULTINFO(regs); + + current->thread.arch.faultinfo = *fi; + force_sig_fault(sig, code, (void __user *)FAULT_ADDRESS(*fi)); + } else { + pr_err("Attempted to relay unknown signal %d (si_code = %d) with errno %d\n", + sig, code, err); + force_sig(sig); + } +} + +void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs, + void *mc) +{ + do_IRQ(WINCH_IRQ, regs); +} diff --git a/arch/um/os-Linux/signal.c b/arch/um/os-Linux/signal.c index 2f6795cd884c..28754f56c42b 100644 --- a/arch/um/os-Linux/signal.c +++ b/arch/um/os-Linux/signal.c @@ -41,9 +41,10 @@ static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc) int save_errno = errno; r.is_user = 0; + if (mc) + get_regs_from_mc(&r, mc); if (sig == SIGSEGV) { /* For segfaults, we want the data from the sigcontext. */ - get_regs_from_mc(&r, mc); GET_FAULTINFO_FROM_MC(r.faultinfo, mc); } diff --git a/arch/x86/um/nommu/do_syscall_64.c b/arch/x86/um/nommu/do_syscall_64.c index 9bc630995df9..cf5a347ee9b1 100644 --- a/arch/x86/um/nommu/do_syscall_64.c +++ b/arch/x86/um/nommu/do_syscall_64.c @@ -44,6 +44,9 @@ __visible void do_syscall_64(struct pt_regs *regs) /* set fs register to the original host one */ os_x86_arch_prctl(0, ARCH_SET_FS, (void *)host_fs); + /* save fp registers */ + asm volatile("fxsaveq %0" : "=m"(*(struct _xstate *)regs->regs.fp)); + if (likely(syscall < NR_syscalls)) { unsigned long ret; @@ -61,6 +64,9 @@ __visible void do_syscall_64(struct pt_regs *regs) /* handle tasks and signals at the end */ interrupt_end(); + /* restore fp registers */ + asm volatile("fxrstorq %0" : : "m"((current->thread.regs.regs.fp))); + /* restore back fs register to userspace configured one */ os_x86_arch_prctl(0, ARCH_SET_FS, (void *)(current->thread.regs.regs.gp[FS_BASE diff --git a/arch/x86/um/nommu/os-Linux/mcontext.c b/arch/x86/um/nommu/os-Linux/mcontext.c index b62a6195096f..afa20f1e235a 100644 --- a/arch/x86/um/nommu/os-Linux/mcontext.c +++ b/arch/x86/um/nommu/os-Linux/mcontext.c @@ -4,10 +4,21 @@ #include #include #include +#include +#include "../syscalls.h" extern long __kernel_vsyscall(int64_t a0, int64_t a1, int64_t a2, int64_t a3, int64_t a4, int64_t a5, int64_t a6); +void set_mc_relay_signal(mcontext_t *mc) +{ + /* configure stack and userspace returning routine as + * instruction pointer + */ + mc->gregs[REG_RSP] = (unsigned long) current_top_of_stack; + mc->gregs[REG_RIP] = (unsigned long) userspace; +} + void set_mc_sigsys_hook(mcontext_t *mc) { mc->gregs[REG_RCX] = mc->gregs[REG_RIP]; diff --git a/arch/x86/um/shared/sysdep/mcontext.h b/arch/x86/um/shared/sysdep/mcontext.h index 9a0d6087f357..82a5f38b350f 100644 --- a/arch/x86/um/shared/sysdep/mcontext.h +++ b/arch/x86/um/shared/sysdep/mcontext.h @@ -19,6 +19,7 @@ extern int set_stub_state(struct uml_pt_regs *regs, struct stub_data *data, #ifndef CONFIG_MMU extern void set_mc_sigsys_hook(mcontext_t *mc); +extern void set_mc_relay_signal(mcontext_t *mc); #endif #ifdef __i386__ diff --git a/arch/x86/um/shared/sysdep/ptrace.h b/arch/x86/um/shared/sysdep/ptrace.h index 572ea2d79131..6ed6bb1ca50e 100644 --- a/arch/x86/um/shared/sysdep/ptrace.h +++ b/arch/x86/um/shared/sysdep/ptrace.h @@ -53,7 +53,7 @@ struct uml_pt_regs { int is_user; /* Dynamically sized FP registers (holds an XSTATE) */ - unsigned long fp[]; + unsigned long fp[] __attribute__((aligned(16))); }; #define EMPTY_UML_PT_REGS { } -- 2.43.0