translator_use_goto_tb() refuses to chain unless the destination is on the
same page as the start of the TB. For guests whose text is much larger than
a page this is expensive: an emulated alpha gcc compiling a 255k line
translation unit takes the indirect dispatch path for 8.4 billion of its
34.2 billion TB exits, and a large share of those are ordinary direct
branches that simply crossed an 8 KiB page boundary.
The restriction was made unconditional by d3a2a1d803 ("accel/tcg:
Introduce translator_use_goto_tb"), whose rationale was:
Various targets avoid the page crossing test for CONFIG_USER_ONLY,
but that is wrong: mmap and mprotect can change page permissions.
That is true, but in user-only builds the invalidation path already covers
it. There are no page tables: every mmap, mprotect and munmap reaches
page_set_flags(), which calls tb_invalidate_phys_range() whenever the flags
actually change, and tb_phys_invalidate() calls tb_jmp_unlink() to reset
incoming jumps. A chained cross-page jump is therefore broken whenever the
destination page's permissions change. This is not true in system mode,
where TBs are keyed by physical address and a page table change invalidates
nothing, so the restriction is kept there.
The rule protects one more thing, which the original rationale does not
mention: it guarantees that execution cannot enter a page without a TB
lookup, and so without check_for_breakpoints(). That is what makes a
breakpoint set after a block was translated take effect, since insertion
deliberately invalidates nothing. A link established before the breakpoint
was set would jump straight over it.
So the chaining is only enabled for a run that can never acquire a
breakpoint. In user-only mode every breakpoint comes from gdb -- BP_CPU is
g_assert_not_reached() there, and the guest cannot ask for one -- and gdb
has to be requested with -g before the first block is translated, even
though with suspend=n it may connect later. gdb_may_set_breakpoints()
reports whether it was, and is fixed for the lifetime of the process.
Add tests/tcg/multiarch/test-xpage-chain.c to cover both hazards directly.
It writes the last instruction of one page and the first of the next, so
that the fall-through between them is a cross-page goto_tb, runs it 200000
times so the chain is established, then checks that mprotect(PROT_NONE)
makes the next call fault, and that different code written into the page
once it is mapped back runs rather than a stale translation.
The two instructions -- set the return value register, and return -- are
all the architecture specific code there is; thirteen architectures supply
them and the rest skip.
The test detects the hazard it is meant to detect: with the
tb_invalidate_phys_range() call in page_set_flags() commented out,
it fails both phases, executing page B after PROT_NONE and returning
the stale result.
Run with -b, the same binary stops once the chain is established and
lets tests/tcg/multiarch/gdbstub/xpage-bp.py set a breakpoint on the
far side of it, which the next call has to stop on.
With gdb_may_set_breakpoints() forced to false so that the chaining
stays on under gdb, that breakpoint is missed and the test fails,
which is what makes it a test of the gate rather than of gdb.
Signed-off-by: Matt Turner <mattst88@gmail.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
[rth: Update for meson test infrastructure]
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Message-ID: <20260901034808.3524945-8-mattst88@gmail.com>
79 lines
2.3 KiB
C
79 lines
2.3 KiB
C
/*
|
|
* gdbstub user-mode only APIs
|
|
*
|
|
* Copyright (c) 2022 Linaro Ltd
|
|
*
|
|
* SPDX-License-Identifier: LGPL-2.0-or-later
|
|
*/
|
|
|
|
#ifndef GDBSTUB_USER_H
|
|
#define GDBSTUB_USER_H
|
|
|
|
#define MAX_SIGINFO_LENGTH 128
|
|
|
|
/**
|
|
* gdb_may_set_breakpoints() - whether a breakpoint can ever be inserted
|
|
*
|
|
* In user-only mode every breakpoint comes from gdb, and gdb is only ever
|
|
* reachable if -g was given at startup, before the guest ran a single
|
|
* instruction. A run that has no gdbstub can therefore never acquire a
|
|
* breakpoint, which lets translation take shortcuts that a breakpoint
|
|
* would invalidate. Stays true once true, even if gdb detaches.
|
|
*/
|
|
bool gdb_may_set_breakpoints(void);
|
|
|
|
/**
|
|
* gdb_handlesig() - yield control to gdb
|
|
* @cpu: CPU
|
|
* @sig: if non-zero, the signal number which caused us to stop
|
|
* @reason: stop reason for stop reply packet or NULL
|
|
* @siginfo: target-specific siginfo struct
|
|
* @siginfo_len: target-specific siginfo struct length
|
|
*
|
|
* This function yields control to gdb, when a user-mode-only target
|
|
* needs to stop execution. If @sig is non-zero, then we will send a
|
|
* stop packet to tell gdb that we have stopped because of this signal.
|
|
*
|
|
* This function will block (handling protocol requests from gdb)
|
|
* until gdb tells us to continue target execution. When it does
|
|
* return, the return value is a signal to deliver to the target,
|
|
* or 0 if no signal should be delivered, ie the signal that caused
|
|
* us to stop should be ignored.
|
|
*/
|
|
int gdb_handlesig(CPUState *, int, const char *, void *, int);
|
|
|
|
/**
|
|
* gdb_signalled() - inform remote gdb of sig exit
|
|
* @as: current CPUArchState
|
|
* @sig: signal number
|
|
*/
|
|
void gdb_signalled(CPUArchState *as, int sig);
|
|
|
|
/**
|
|
* gdbserver_fork_start() - inform gdb of the upcoming fork()
|
|
*/
|
|
void gdbserver_fork_start(void);
|
|
|
|
/**
|
|
* gdbserver_fork_end() - inform gdb of the completed fork()
|
|
* @cs: CPU
|
|
* @pid: 0 if in child process, -1 if fork failed, child process pid otherwise
|
|
*/
|
|
void gdbserver_fork_end(CPUState *cs, pid_t pid);
|
|
|
|
/**
|
|
* gdb_syscall_entry() - inform gdb of syscall entry and yield control to it
|
|
* @cs: CPU
|
|
* @num: syscall number
|
|
*/
|
|
void gdb_syscall_entry(CPUState *cs, int num);
|
|
|
|
/**
|
|
* gdb_syscall_entry() - inform gdb of syscall return and yield control to it
|
|
* @cs: CPU
|
|
* @num: syscall number
|
|
*/
|
|
void gdb_syscall_return(CPUState *cs, int num);
|
|
|
|
#endif /* GDBSTUB_USER_H */
|