linux-user: User support for AArch64 BTI
v9 cover linux-user: User support for AArch64 BTI
The kernel ABI for this is (finally) close to being merged:
https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git/log/?h=for-next/bti-user
I've made a couple of tweaks from v8:
(1) Only map the interpreter or a static executable with BTI. The interpreter is responsible for handling the pages of a dynamic executable. This is a behaviour change in the kernel ABI since the last time I audited the code.
(2) Rely on the recently released gcc 10 for building the test case. Thanks to Alex for helping me get a docker setup for that.
Based-on: <[email protected]> ("linux-user: mmap/mprotect prot values")
Based-on: An unpublished version of stsquad's testing/next
So for avoidance of doubt, the complete tree may be found at
https://github.com/rth7680/qemu/tree/tgt-arm-bti
r~
Richard Henderson (5):
linux-user/aarch64: Reset btype for signals
linux-user: Set PAGE_TARGET_1 for TARGET_PROT_BTI include/elf: Add defines related to GNU property notes for AArch64
linux-user: Parse NT_GNU_PROPERTY_TYPE_0 notes tests/tcg/aarch64: Add bti smoke test
include/elf.h | 22 +++++ include/exec/cpu-all.h | 2 + linux-user/qemu.h | 4 + linux-user/syscall_defs.h | 4 + linux-user/aarch64/signal.c | 10 ++- linux-user/elfload.c | 143 ++++++++++++++++++++++-------- linux-user/mmap.c | 16 ++++ target/arm/translate-a64.c | 6 +- tests/tcg/aarch64/bti-1.c | 62 +++++++++++++ tests/tcg/aarch64/bti-crt.inc.c | 51 +++++++++++ tests/tcg/aarch64/Makefile.target | 7 ++ tests/tcg/configure.sh | 4 + 12 files changed, 291 insertions(+), 40 deletions(-) create mode 100644 tests/tcg/aarch64/bti-1.c create mode 100644 tests/tcg/aarch64/bti-crt.inc.c
Signature · 1 line
2.20.1
PATCH v9 1/5 linux-user/aarch64: Reset btype for signals
The kernel sets btype for the signal handler as if for a call.
--- linux-user/aarch64/signal.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
linux-user/aarch64/signal.c +8 -2
@@ -506,10 +506,16 @@ static void target_setup_frame(int usig, struct target_sigaction *ka, + offsetof(struct target_rt_frame_record , tramp); } env->xregs[0 ] = usig; - env->xregs[31 ] = frame_addr; env->xregs[29 ] = frame_addr + fr_ofs; - env->pc = ka->_sa_handler; env->xregs[30 ] = return_addr; + env->xregs[31 ] = frame_addr; + env->pc = ka->_sa_handler; + +/* Invoke the signal handler as if by indirect call. */ +if (cpu_isar_feature (aa64_bti,env_archcpu (env))) { + env->btype =2 ; + } +if (info) {tswap_siginfo (&frame->info, info); env->xregs[1 ] = frame_addr + offsetof(struct target_rt_sigframe , info);
Signature · 1 line
2.20.1
Re: [PATCH v9 1/5] linux-user/aarch64: Reset btype for signals
Quoted 18 lines · Richard Henderson
The kernel sets btype for the signal handler as if for a call.
Signed-off-by Richard Henderson <[email protected]>--- linux-user/aarch64/signal.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)linux-user/aarch64/signal.c +0
-1@@ -506,10 +506,16 @@ static void target_setup_frame(int usig, struct target_sigaction *ka, + offsetof(struct target_rt_frame_record , tramp); } env->xregs[0 ] = usig; - env->xregs[31 ] = frame_addr; env->xregs[29 ] = frame_addr + fr_ofs;
linux-user/aarch64/signal.c static void target_setup_frame(int usig, struct target_sigaction *ka, · line 518
Quoted 10 lines
linux-user/aarch64/signal.c +8
-1- env->pc = ka->_sa_handler; env->xregs[30 ] = return_addr; + env->xregs[31 ] = frame_addr; + env->pc = ka->_sa_handler; + +/* Invoke the signal handler as if by indirect call. */ +if (cpu_isar_feature (aa64_bti,env_archcpu (env))) { + env->btype =2 ; + } +
thanks
-- PMM
PATCH v9 2/5 linux-user: Set PAGE_TARGET_1 for TARGET_PROT_BTI
In reply to Richard HendersonTransform the prot bit to a qemu internal page bit, and save it in the page tables.
--- include/exec/cpu-all.h | 2 ++ linux-user/syscall_defs.h | 4 ++++ linux-user/mmap.c | 16 ++++++++++++++++ target/arm/translate-a64.c | 6 +++--- 4 files changed, 25 insertions(+), 3 deletions(-)
include/exec/cpu-all.h +2 -0
@@ -276,6 +276,8 @@ extern intptr_t qemu_host_page_mask;/* FIXME: Code that sets/uses this is broken and needs to go away. */ #define PAGE_RESERVED 0x0020#endif +/* Target-specific bits that will be used via page_get_flags(). */ +#define PAGE_TARGET_1 0x0080#if defined(CONFIG_USER_ONLY )void page_dump (FILE *f);
linux-user/syscall_defs.h +4 -0
@@ -1194,6 +1194,10 @@ struct target_winsize {#define TARGET_PROT_SEM 0x08#endif +#ifdef TARGET_AARCH64 +#define TARGET_PROT_BTI 0x10 +#endif +/* Common */ #define TARGET_MAP_SHARED 0x01/* Share changes */ #define TARGET_MAP_PRIVATE 0x02/* Changes are private */
linux-user/mmap.c +16 -0
@@ -83,6 +83,22 @@ static int validate_prot_to_pageflags(int *host_prot, int prot) *host_prot = (prot & (PROT_READ |PROT_WRITE )) | (prot &PROT_EXEC ?PROT_READ :0 ); +#ifdef TARGET_AARCH64 +/* +* The PROT_BTI bit is only accepted if the cpu supports the feature. +* Since this is the unusual case, don't bother checking unless +* the bit has been requested. If set and valid, record the bit +* within QEMU's page_flags as PAGE_TARGET_1. +*/ +if (prot &TARGET_PROT_BTI ) { +ARMCPU *cpu =ARM_CPU (thread_cpu); +if (cpu_isar_feature (aa64_bti, cpu)) { + valid |=TARGET_PROT_BTI ; + page_flags |=PAGE_TARGET_1 ; + } + } +#endif +return prot & ~valid ?0 : page_flags; }
target/arm/translate-a64.c +3 -3
@@ -13989,10 +13989,10 @@ static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)*/ static bool is_guarded_page (CPUARMState *env,DisasContext *s) { -#ifdef CONFIG_USER_ONLY -return false;/* FIXME */ -#else uint64_t addr = s->base.pc_first; +#ifdef CONFIG_USER_ONLY +return page_get_flags (addr) &PAGE_TARGET_1 ; +#else int mmu_idx =arm_to_core_mmu_idx (s->mmu_idx);unsigned int index =tlb_index (env, mmu_idx, addr);CPUTLBEntry *entry =tlb_entry (env, mmu_idx, addr);
Signature · 1 line
2.20.1
Re: [PATCH v9 2/5] linux-user: Set PAGE_TARGET_1 for TARGET_PROT_BTI
Quoted 17 lines · Richard Henderson
Transform the prot bit to a qemu internal page bit, and save it in the page tables.
Signed-off-by Richard Henderson <[email protected]>--- include/exec/cpu-all.h | 2 ++ linux-user/syscall_defs.h | 4 ++++ linux-user/mmap.c | 16 ++++++++++++++++ target/arm/translate-a64.c | 6 +++--- 4 files changed, 25 insertions(+), 3 deletions(-)include/exec/cpu-all.h +0
-0
include/exec/cpu-all.h extern intptr_t qemu_host_page_mask; · line 280
Quoted 5 lines
include/exec/cpu-all.h +2
-0@@ -276,6 +276,8 @@ extern intptr_t qemu_host_page_mask;/* FIXME: Code that sets/uses this is broken and needs to go away. */ #define PAGE_RESERVED 0x0020#endif +/* Target-specific bits that will be used via page_get_flags(). */ +#define PAGE_TARGET_1 0x0080
I think it would also be nice to have
/* Target-specific names for the target-specific bits */ #ifdef TARGET_AARCH64
#define PAGE_BTI PAGE_TARGET_1
#endif
which can then (a) act as documentation of which PAGE_TARGET_n each target is actually using and (b) mean that code that's using the target-specific bits can use more friendly names.
Otherwise
thanks
-- PMM
PATCH v9 3/5 include/elf: Add defines related to GNU property notes for AArch64
In reply to Richard HendersonThese are all of the defines required to parse GNU_PROPERTY_AARCH64_FEATURE_1_AND, copied from binutils. Other missing defines related to other GNU program headers and notes are elided for now.
--- include/elf.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
include/elf.h +22 -0
@@ -26,9 +26,13 @@ typedef int64_t Elf64_Sxword;#define PT_NOTE 4#define PT_SHLIB 5#define PT_PHDR 6 +#define PT_LOOS 0x60000000 +#define PT_HIOS 0x6fffffff#define PT_LOPROC 0x70000000#define PT_HIPROC 0x7fffffff +#define PT_GNU_PROPERTY (PT_LOOS + 0x474e553) +#define PT_MIPS_REGINFO 0x70000000#define PT_MIPS_RTPROC 0x70000001#define PT_MIPS_OPTIONS 0x70000002
@@ -1652,6 +1656,24 @@ typedef struct elf64_shdr {#define NT_ARM_SYSTEM_CALL 0x404/* ARM system call number */ #define NT_ARM_SVE 0x405/* ARM Scalable Vector Extension regs */ +/* Defined note types for GNU systems. */ + +#define NT_GNU_PROPERTY_TYPE_0 5/* Program property */ + +/* Values used in GNU .note.gnu.property notes (NT_GNU_PROPERTY_TYPE_0). */ + +#define GNU_PROPERTY_STACK_SIZE 1 +#define GNU_PROPERTY_NO_COPY_ON_PROTECTED 2 + +#define GNU_PROPERTY_LOPROC 0xc0000000 +#define GNU_PROPERTY_HIPROC 0xdfffffff +#define GNU_PROPERTY_LOUSER 0xe0000000 +#define GNU_PROPERTY_HIUSER 0xffffffff + +#define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000 +#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI (1u << 0) +#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC (1u << 1) +/* * Physical entry point into the kernel. *
Signature · 1 line
2.20.1
PATCH v9 4/5 linux-user: Parse NT_GNU_PROPERTY_TYPE_0 notes
In reply to Richard HendersonFor aarch64, this includes the GNU_PROPERTY_AARCH64_FEATURE_1_BTI bit, which indicates that the image should be mapped with guarded pages.
---
v9: Only map the startup executable with BTI; anything else must be
handled by the interpreter.
--- linux-user/qemu.h | 4 ++ linux-user/elfload.c | 143 ++++++++++++++++++++++++++++++++----------- 2 files changed, 112 insertions(+), 35 deletions(-)
linux-user/qemu.h +4 -0
@@ -61,6 +61,10 @@ struct image_info {abi_ulong interpreter_loadmap_addr;abi_ulong interpreter_pt_dynamic_addr;struct image_info *other_info; + +/* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */ +uint32_t note_flags; +#ifdef TARGET_MIPS int fp_abi;int interp_fp_abi;
linux-user/elfload.c +108 -35
180 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH v9 4/5] linux-user: Parse NT_GNU_PROPERTY_TYPE_0 notes
Quoted 4 lines · Richard Henderson
For aarch64, this includes the
GNU_PROPERTY_AARCH64_FEATURE_1_BTIbit, which indicates that the image should be mapped with guarded pages.
Quoted 8 lines
Signed-off-by Richard Henderson <[email protected]>---
v9: Only map the startup executable with BTI; anything else must be
handled by the interpreter.--- linux-user/qemu.h | 4 ++ linux-user/elfload.c | 143 ++++++++++++++++++++++++++++++++----------- 2 files changed, 112 insertions(+), 35 deletions(-)
Hi; this patch has some changes which seem to be just code motion (moving the PT_INTERP handling higher up in the function), some changes which are coding style fixes (braces), some which are no-behaviour-change changes (using eppnt->foo instead of phdr[i].foo) and also the actual BTI related functional change. Could you split them out, please? I think it would make this easier to review.
thanks
-- PMM
PATCH v9 5/5 tests/tcg/aarch64: Add bti smoke test
In reply to Richard HendersonThis test requires gcc 10 for -mbranch-protection=standard.
---
v9: Expect and require gcc 10.
--- tests/tcg/aarch64/bti-1.c | 62 +++++++++++++++++++++++++++++++ tests/tcg/aarch64/bti-crt.inc.c | 51 +++++++++++++++++++++++++ tests/tcg/aarch64/Makefile.target | 7 ++++ tests/tcg/configure.sh | 4 ++ 4 files changed, 124 insertions(+) create mode 100644 tests/tcg/aarch64/bti-1.c create mode 100644 tests/tcg/aarch64/bti-crt.inc.c
tests/tcg/aarch64/bti-1.c new file +62 -0
@@ -0,0 +1,62 @@ +/* +* Branch target identification, basic notskip cases. +*/ + +#include "bti-crt.inc.c" + +static void skip2_sigill (int sig,siginfo_t *info,ucontext_t *uc) +{ + uc->uc_mcontext.pc +=8 ; + uc->uc_mcontext.pstate =1 ; +} + +#define NOP "nop" +#define BTI_N "hint #32" +#define BTI_C "hint #34" +#define BTI_J "hint #36" +#define BTI_JC "hint #38" + +#define BTYPE_1 (DEST ) \ + asm("mov %0,#1; adr x16, 1f; br x16; 1: " DEST "; mov %0,#0" \ + : "=r"(skipped) : : "x16") + +#define BTYPE_2 (DEST ) \ + asm("mov %0,#1; adr x16, 1f; blr x16; 1: " DEST "; mov %0,#0" \ + : "=r"(skipped) : : "x16", "x30") + +#define BTYPE_3 (DEST ) \ + asm("mov %0,#1; adr x15, 1f; br x15; 1: " DEST "; mov %0,#0" \ + : "=r"(skipped) : : "x15") + +#define TEST (WHICH ,DEST ,EXPECT ) \ + do { WHICH(DEST); fail += skipped ^ EXPECT; } while (0) + + +int main () +{ +int fail =0 ; +int skipped; + +/* Signal-like with SA_SIGINFO. */ +signal_info (SIGILL , skip2_sigill); + +TEST (BTYPE_1 ,NOP ,1 ); +TEST (BTYPE_1 ,BTI_N ,1 ); +TEST (BTYPE_1 ,BTI_C ,0 ); +TEST (BTYPE_1 ,BTI_J ,0 ); +TEST (BTYPE_1 ,BTI_JC ,0 ); + +TEST (BTYPE_2 ,NOP ,1 ); +TEST (BTYPE_2 ,BTI_N ,1 ); +TEST (BTYPE_2 ,BTI_C ,0 ); +TEST (BTYPE_2 ,BTI_J ,1 ); +TEST (BTYPE_2 ,BTI_JC ,0 ); + +TEST (BTYPE_3 ,NOP ,1 ); +TEST (BTYPE_3 ,BTI_N ,1 ); +TEST (BTYPE_3 ,BTI_C ,1 ); +TEST (BTYPE_3 ,BTI_J ,0 ); +TEST (BTYPE_3 ,BTI_JC ,0 ); + +return fail; +}
tests/tcg/aarch64/bti-crt.inc.c new file +51 -0
51 lines not on this page · raw message
tests/tcg/aarch64/Makefile.target +7 -0
@@ -24,6 +24,13 @@ run-pauth-%: QEMU_OPTS += -cpu max pauth-%: CFLAGS += -march=armv8.3-a endif +# BTI Tests +ifneq ($(DOCKER_IMAGE)$(CROSS_CC_HAS_ARMV8_BTI),) +AARCH64_TESTS += bti-1 +bti-%: CFLAGS += -mbranch-protection=standard +bti-%: LDFLAGS += -nostdlib +endif + # Semihosting smoke test for linux-user AARCH64_TESTS += semihosting run-semihosting: semihosting
tests/tcg/configure.sh +4 -0
@@ -231,6 +231,10 @@ for target in $target_list; do -march=armv8.3-a -o $TMPE $TMPC; then echo "CROSS_CC_HAS_ARMV8_3=y" >> $config_target_mak fi + if do_compiler "$target_compiler" $target_compiler_cflags \ + -mbranch-protection=standard -o $TMPE $TMPC; then + echo "CROSS_CC_HAS_ARMV8_BTI=y" >> $config_target_mak + fi ;; esac
Signature · 1 line
2.20.1
Re: [PATCH v9 5/5] tests/tcg/aarch64: Add bti smoke test
Quoted 6 lines · Richard Henderson
This test requires gcc 10 for -mbranch-protection=standard.
Signed-off-by Richard Henderson <[email protected]>---
v9: Expect and require gcc 10.
Quoted 8 lines
--- tests/tcg/aarch64/bti-1.c | 62 +++++++++++++++++++++++++++++++ tests/tcg/aarch64/bti-crt.inc.c | 51 +++++++++++++++++++++++++ tests/tcg/aarch64/Makefile.target | 7 ++++ tests/tcg/configure.sh | 4 ++ 4 files changed, 124 insertions(+) create mode 100644 tests/tcg/aarch64/bti-1.c create mode 100644 tests/tcg/aarch64/bti-crt.inc.c
thanks
-- PMM
Quoted 14 lines · Richard Henderson
The kernel ABI for this is (finally) close to being merged:
https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git/log/?h=for-next/bti-user
I've made a couple of tweaks from v8:
(1) Only map the interpreter or a static executable with BTI. The interpreter is responsible for handling the pages of a dynamic executable. This is a behaviour change in the kernel ABI since the last time I audited the code.
(2) Rely on the recently released gcc 10 for building the test case. Thanks to Alex for helping me get a docker setup for that.
Quoted 8 lines
Based-on: <[email protected]> ("linux-user: mmap/mprotect prot values")
Based-on: An unpublished version of stsquad's testing/next
So for avoidance of doubt, the complete tree may be found at
The Linux ABI on which this is based has been merged for v5.8-rc1, so pinging this series.
Alex's testing/next is upstream, but the "mmap/mprotect prot values" patch is still outstanding.
Everything rebases without issue onto qemu master. Again for avoidance of doubt, I have updated my branch.
r~