public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sohil Mehta <sohil.mehta@intel.com>
To: Brian Gerst <brgerst@gmail.com>, <linux-kernel@vger.kernel.org>,
	<x86@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>, "H . Peter Anvin" <hpa@zytor.com>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	Borislav Petkov <bp@alien8.de>,
	"Andy Lutomirski" <luto@kernel.org>,
	Juergen Gross <jgross@suse.com>,
	"Boris Ostrovsky" <boris.ostrovsky@oracle.com>
Subject: Re: [PATCH 2/5] x86/syscall/32: Move 32-bit syscall dispatch code
Date: Thu, 13 Mar 2025 16:44:42 -0700	[thread overview]
Message-ID: <5ac97442-78b1-40ce-9282-06cd62d97a67@intel.com> (raw)
In-Reply-To: <20250313182236.655724-3-brgerst@gmail.com>

On 3/13/2025 11:22 AM, Brian Gerst wrote:

>  SYSCALL_DEFINE0(ni_syscall)
>  {
>  	return -ENOSYS;
> diff --git a/arch/x86/entry/syscall_32.c b/arch/x86/entry/syscall_32.c
> index 8cc9950d7104..7c286e89fd04 100644
> --- a/arch/x86/entry/syscall_32.c
> +++ b/arch/x86/entry/syscall_32.c
> @@ -1,10 +1,23 @@
> -// SPDX-License-Identifier: GPL-2.0
> -/* System call table for i386. */
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * 32-bit system call dispatch
> + *
> + * Copyright (c) 2015 Andrew Lutomirski
> + *
> + * Based on asm and ptrace code by many authors.  The code here originated
> + * in ptrace.c and signal.c.

Wondering if we can skip copying over some of these old comments? As the
file grows, it's hard to discerne what specific code originated where.
For example, the FRED code is all new.


> +#ifdef CONFIG_IA32_EMULATION
> +static __always_inline bool int80_is_external(void)
> +{
> +	const unsigned int offs = (0x80 / 32) * 0x10;
> +	const u32 bit = BIT(0x80 % 32);
> +
> +	/* The local APIC on XENPV guests is fake */
> +	if (cpu_feature_enabled(X86_FEATURE_XENPV))
> +		return false;
> +
> +	/*
> +	 * If vector 0x80 is set in the APIC ISR then this is an external
> +	 * interrupt. Either from broken hardware or injected by a VMM.
> +	 *
> +	 * Note: In guest mode this is only valid for secure guests where
> +	 * the secure module fully controls the vAPIC exposed to the guest.
> +	 */
> +	return apic_read(APIC_ISR + offs) & bit;
> +}
> +
> +/**
> + * do_int80_emulation - 32-bit legacy syscall C entry from asm
> + * @regs: syscall arguments in struct pt_args on the stack.
> + *
> + * This entry point can be used by 32-bit and 64-bit programs to perform
> + * 32-bit system calls.  Instances of INT $0x80 can be found inline in
> + * various programs and libraries.  It is also used by the vDSO's
> + * __kernel_vsyscall fallback for hardware that doesn't support a faster
> + * entry method.  Restarted 32-bit system calls also fall back to INT
> + * $0x80 regardless of what instruction was originally used to do the
> + * system call.
> + *
> + * This is considered a slow path.  It is not used by most libc
> + * implementations on modern hardware except during process startup.
> + *
> + * The arguments for the INT $0x80 based syscall are on stack in the
> + * pt_regs structure:
> + *   eax:				system call number
> + *   ebx, ecx, edx, esi, edi, ebp:	arg1 - arg 6
> + */
> +__visible noinstr void do_int80_emulation(struct pt_regs *regs)
> +{
> +	int nr;
> +
> +	/* Kernel does not use INT $0x80! */
> +	if (unlikely(!user_mode(regs))) {
> +		irqentry_enter(regs);
> +		instrumentation_begin();
> +		panic("Unexpected external interrupt 0x80\n");
> +	}
> +
> +	/*
> +	 * Establish kernel context for instrumentation, including for
> +	 * int80_is_external() below which calls into the APIC driver.
> +	 * Identical for soft and external interrupts.
> +	 */
> +	enter_from_user_mode(regs);
> +
> +	instrumentation_begin();
> +	add_random_kstack_offset();
> +
> +	/* Validate that this is a soft interrupt to the extent possible */
> +	if (unlikely(int80_is_external()))
> +		panic("Unexpected external interrupt 0x80\n");
> +
> +	/*
> +	 * The low level idtentry code pushed -1 into regs::orig_ax
> +	 * and regs::ax contains the syscall number.
> +	 *
> +	 * User tracing code (ptrace or signal handlers) might assume
> +	 * that the regs::orig_ax contains a 32-bit number on invoking
> +	 * a 32-bit syscall.
> +	 *
> +	 * Establish the syscall convention by saving the 32bit truncated
> +	 * syscall number in regs::orig_ax and by invalidating regs::ax.
> +	 */
> +	regs->orig_ax = regs->ax & GENMASK(31, 0);
> +	regs->ax = -ENOSYS;
> +
> +	nr = syscall_32_enter(regs);
> +
> +	local_irq_enable();
> +	nr = syscall_enter_from_user_mode_work(regs, nr);
> +	do_syscall_32_irqs_on(regs, nr);
> +
> +	instrumentation_end();
> +	syscall_exit_to_user_mode(regs);
> +}
> +
> +#ifdef CONFIG_X86_FRED
> +/*
> + * A FRED-specific INT80 handler is warranted for the follwing reasons:
> + *
> + * 1) As INT instructions and hardware interrupts are separate event
> + *    types, FRED does not preclude the use of vector 0x80 for external
> + *    interrupts. As a result, the FRED setup code does not reserve
> + *    vector 0x80 and calling int80_is_external() is not merely
> + *    suboptimal but actively incorrect: it could cause a system call
> + *    to be incorrectly ignored.
> + *
> + * 2) It is called only for handling vector 0x80 of event type
> + *    EVENT_TYPE_SWINT and will never be called to handle any external
> + *    interrupt (event type EVENT_TYPE_EXTINT).
> + *
> + * 3) FRED has separate entry flows depending on if the event came from
> + *    user space or kernel space, and because the kernel does not use
> + *    INT insns, the FRED kernel entry handler fred_entry_from_kernel()
> + *    falls through to fred_bad_type() if the event type is
> + *    EVENT_TYPE_SWINT, i.e., INT insns. So if the kernel is handling
> + *    an INT insn, it can only be from a user level.
> + *
> + * 4) int80_emulation() does a CLEAR_BRANCH_HISTORY. While FRED will
> + *    likely take a different approach if it is ever needed: it
> + *    probably belongs in either fred_intx()/ fred_other() or
> + *    asm_fred_entrypoint_user(), depending on if this ought to be done
> + *    for all entries from userspace or only system
> + *    calls.
> + *
> + * 5) INT $0x80 is the fast path for 32-bit system calls under FRED.
> + */
> +DEFINE_FREDENTRY_RAW(int80_emulation)
> +{
> +	int nr;
> +
> +	enter_from_user_mode(regs);
> +
> +	instrumentation_begin();
> +	add_random_kstack_offset();
> +
> +	/*
> +	 * FRED pushed 0 into regs::orig_ax and regs::ax contains the
> +	 * syscall number.
> +	 *
> +	 * User tracing code (ptrace or signal handlers) might assume
> +	 * that the regs::orig_ax contains a 32-bit number on invoking
> +	 * a 32-bit syscall.
> +	 *
> +	 * Establish the syscall convention by saving the 32bit truncated
> +	 * syscall number in regs::orig_ax and by invalidating regs::ax.
> +	 */
> +	regs->orig_ax = regs->ax & GENMASK(31, 0);
> +	regs->ax = -ENOSYS;
> +
> +	nr = syscall_32_enter(regs);
> +
> +	local_irq_enable();
> +	nr = syscall_enter_from_user_mode_work(regs, nr);
> +	do_syscall_32_irqs_on(regs, nr);
> +
> +	instrumentation_end();
> +	syscall_exit_to_user_mode(regs);
> +}
> +#endif

Nit: Would it be useful to add /* CONFIG_X86_FRED */ here since there
are nested #ifdefs?




> +#else /* CONFIG_IA32_EMULATION */
> +
> +/* Handles int $0x80 on a 32bit kernel */
> +__visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
> +{
> +	int nr = syscall_32_enter(regs);
> +
> +	add_random_kstack_offset();
> +	/*
> +	 * Subtlety here: if ptrace pokes something larger than 2^31-1 into
> +	 * orig_ax, the int return value truncates it. This matches
> +	 * the semantics of syscall_get_nr().
> +	 */
> +	nr = syscall_enter_from_user_mode(regs, nr);
> +	instrumentation_begin();
> +
> +	do_syscall_32_irqs_on(regs, nr);
> +
> +	instrumentation_end();
> +	syscall_exit_to_user_mode(regs);
> +}
> +#endif /* !CONFIG_IA32_EMULATION */
> +

  reply	other threads:[~2025-03-13 23:45 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-13 18:22 [PATCH 0/5] x86/entry: Break up common.c Brian Gerst
2025-03-13 18:22 ` [PATCH 1/5] x86/xen: Move Xen upcall handler Brian Gerst
2025-03-14  7:37   ` Juergen Gross
2025-03-14  9:47   ` [tip: x86/cpu] x86/xen: Move Xen upcall handler to Xen specific code files tip-bot2 for Brian Gerst
2025-03-14  9:53     ` Jürgen Groß
2025-03-14 10:08       ` Ingo Molnar
2025-03-14 10:14         ` Jürgen Groß
2025-03-13 18:22 ` [PATCH 2/5] x86/syscall/32: Move 32-bit syscall dispatch code Brian Gerst
2025-03-13 23:44   ` Sohil Mehta [this message]
2025-03-14  1:25     ` Brian Gerst
2025-03-14  9:27       ` Ingo Molnar
2025-03-14  9:46   ` [tip: x86/cpu] x86/syscall/32: Move the 32-bit syscall dispatch code to arch/x86/entry/syscall_32.c tip-bot2 for Brian Gerst
2025-03-13 18:22 ` [PATCH 3/5] x86/syscall/64: Move 64-bit syscall dispatch code Brian Gerst
2025-03-14  9:46   ` [tip: x86/cpu] x86/syscall/64: Move the 64-bit syscall dispatch code to arch/x86/entry/syscall_64.c tip-bot2 for Brian Gerst
2025-03-13 18:22 ` [PATCH 4/5] x86/syscall/x32: Move x32 syscall table Brian Gerst
2025-03-13 23:47   ` Sohil Mehta
2025-03-14  9:25     ` Ingo Molnar
2025-03-14  9:34     ` Ingo Molnar
2025-03-14 16:02       ` Sohil Mehta
2025-03-14  9:46   ` [tip: x86/cpu] x86/syscall/x32: Move the x32 syscall table to arch/x86/entry/syscall_64.c tip-bot2 for Brian Gerst
2025-03-13 18:22 ` [PATCH 5/5] x86/syscall: Move sys_ni_syscall() Brian Gerst
2025-03-14  9:46   ` [tip: x86/cpu] x86/syscall: Move sys_ni_syscall() to arch/x86/kernel/process.c tip-bot2 for Brian Gerst

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5ac97442-78b1-40ce-9282-06cd62d97a67@intel.com \
    --to=sohil.mehta@intel.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=bp@alien8.de \
    --cc=brgerst@gmail.com \
    --cc=hpa@zytor.com \
    --cc=jgross@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox