target/arm: Implement ARMv8.5-MemTag, user mode
cover target/arm: Implement ARMv8.5-MemTag, user mode
This is a refresh of the user-only patch set from March.
I do not attempt to implement any part of a kernel abi wrt mmap and/or mprotect. Instead, it uses a x-tagged-pages property to assume that all anonymous pages have tags.
The tests added are disabled by default, but do pass if you have binutils 2.32 installed.
r~
Based-on: <[email protected]> ("target/arm: Implement ARMv8.5-MemTag, system mode")
Based-on: <[email protected]> ("target/arm: Implement ARMv8.5-BTI for linux-user")
Complete tree: https://github.com/rth7680/qemu/tree/tgt-arm-mte-user
Richard Henderson (3):
tcg: Introduce target-specific page data for user-only target/arm: Add allocation tag storage for user mode tests/tcg/aarch64: Add mte smoke tests
include/exec/cpu-all.h | 10 ++++++-- target/arm/cpu.h | 4 ++++ accel/tcg/translate-all.c | 28 ++++++++++++++++++++++ linux-user/mmap.c | 8 ++++++- linux-user/syscall.c | 4 ++-- target/arm/cpu64.c | 20 ++++++++++++++++ target/arm/mte_helper.c | 35 +++++++++++++++++++++++++-- tests/tcg/aarch64/mte-1.c | 27 +++++++++++++++++++++ tests/tcg/aarch64/mte-2.c | 39 +++++++++++++++++++++++++++++++ tests/tcg/aarch64/Makefile.target | 5 ++++ 10 files changed, 173 insertions(+), 7 deletions(-) create mode 100644 tests/tcg/aarch64/mte-1.c create mode 100644 tests/tcg/aarch64/mte-2.c
Signature · 1 line
2.17.1
PATCH v3 1/3 tcg: Introduce target-specific page data for user-only
At the same time, remember MAP_SHARED as PAGE_SHARED. When mapping new pages, make sure that old target-specific page data is removed.
--- include/exec/cpu-all.h | 10 ++++++++-- accel/tcg/translate-all.c | 28 ++++++++++++++++++++++++++++ linux-user/mmap.c | 8 +++++++- linux-user/syscall.c | 4 ++-- 4 files changed, 45 insertions(+), 5 deletions(-)
include/exec/cpu-all.h +8 -2
@@ -243,10 +243,14 @@ extern intptr_t qemu_host_page_mask;#define PAGE_WRITE_ORG 0x0010/* Invalidate the TLB entry immediately, helpful for s390x * Low-Address-Protection. Used with PAGE_WRITE in tlb_set_page_with_attrs() */ -#define PAGE_WRITE_INV 0x0040 +#define PAGE_WRITE_INV 0x0020 +/* Page is mapped shared. */ +#define PAGE_SHARED 0x0040 +/* For use with page_set_flags: page is being replaced; target_data cleared. */ +#define PAGE_RESET 0x0080#if defined(CONFIG_BSD ) && defined(CONFIG_USER_ONLY )/* FIXME: Code that sets/uses this is broken and needs to go away. */ -#define PAGE_RESERVED 0x0020 +#define PAGE_RESERVED 0x0100#endif /* Target-specific bits that will be used via page_get_flags(). */ #define PAGE_TARGET_1 0x0080
@@ -261,6 +265,8 @@ int walk_memory_regions(void *, walk_memory_regions_fn);int page_get_flags (target_ulong address);void page_set_flags (target_ulong start,target_ulong end,int flags);int page_check_range (target_ulong start,target_ulong len,int flags); +void *page_get_target_data (target_ulong address); +void *page_alloc_target_data (target_ulong address,size_t size);#endif CPUArchState *cpu_copy (CPUArchState *env);
accel/tcg/translate-all.c +28 -0
@@ -110,6 +110,7 @@ typedef struct PageDesc {unsigned int code_write_count;#else unsigned long flags; +void *target_data;#endif #ifndef CONFIG_USER_ONLY QemuSpin lock;
@@ -2513,6 +2514,7 @@ int page_get_flags(target_ulong address)void page_set_flags (target_ulong start,target_ulong end,int flags) {target_ulong addr, len; +bool reset_target_data;/* This function should never be called with addresses outside the guest address space. If this assert fires, it probably indicates
@@ -2529,6 +2531,8 @@ void page_set_flags(target_ulong start, target_ulong end, int flags)if (flags &PAGE_WRITE ) { flags |=PAGE_WRITE_ORG ; } + reset_target_data = !(flags &PAGE_VALID ) || (flags &PAGE_RESET ); + flags &= ~PAGE_RESET ; for (addr = start, len = end - start; len != 0;
@@ -2542,10 +2546,34 @@ void page_set_flags(target_ulong start, target_ulong end, int flags) p->first_tb) {tb_invalidate_phys_page (addr,0 ); } +if (reset_target_data && p->target_data) { +g_free (p->target_data); + p->target_data =NULL ; + } p->flags = flags; } } +void *page_get_target_data (target_ulong address) +{ +PageDesc *p =page_find (address >>TARGET_PAGE_BITS ); +return p ? p->target_data :NULL ; +} + +void *page_alloc_target_data (target_ulong address,size_t size) +{ +PageDesc *p =page_find (address >>TARGET_PAGE_BITS ); +void *ret =NULL ; + +if (p) { + ret = p->target_data; +if (!ret && (p->flags &PAGE_VALID )) { + p->target_data = ret =g_malloc0 (size); + } + } +return ret; +} +int page_check_range (target_ulong start,target_ulong len,int flags) {PageDesc *p;
linux-user/mmap.c +7 -1
@@ -626,6 +626,10 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, } } the_end1: +if ((flags &MAP_TYPE ) ==MAP_SHARED ) { + page_flags |=PAGE_SHARED ; + } + page_flags |=PAGE_RESET ;page_set_flags (start, start + len, page_flags); the_end: #ifdef DEBUG_MMAP
@@ -818,9 +822,11 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, new_addr =-1 ; }else { new_addr =h2g (host_addr); +/* FIXME: Move page flags (and target_data?) for each page. */ prot =page_get_flags (old_addr);page_set_flags (old_addr, old_addr + old_size,0 ); -page_set_flags (new_addr, new_addr + new_size, prot |PAGE_VALID ); +page_set_flags (new_addr, new_addr + new_size, + prot |PAGE_VALID |PAGE_RESET ); }tb_invalidate_phys_range (new_addr, new_addr + new_size);mmap_unlock ();
linux-user/syscall.c +2 -2
@@ -4061,8 +4061,8 @@ static inline abi_ulong do_shmat(CPUArchState *cpu_env, raddr=h2g ((unsigned long )host_raddr);page_set_flags (raddr, raddr + shm_info.shm_segsz, -PAGE_VALID |PAGE_READ | - ((shmflg &SHM_RDONLY )?0 :PAGE_WRITE )); +PAGE_VALID |PAGE_SHARED |PAGE_RESET |PAGE_READ | + (shmflg &SHM_RDONLY ?0 :PAGE_WRITE ));for (i =0 ; i <N_SHM_REGIONS ; i++) {if (!shm_regions[i].in_use) {
Signature · 1 line
2.17.1
PATCH v3 2/3 target/arm: Add allocation tag storage for user mode
In reply to Richard HendersonControl this with x-tagged-pages, which is off by default.
The limitation to non-shared pages is not part of a future kernel API, but a limitation of linux-user not being able to map virtual pages back to physical pages.
---
v2: Add the x-tagged-pages cpu property
--- target/arm/cpu.h | 4 ++++ target/arm/cpu64.c | 20 ++++++++++++++++++++ target/arm/mte_helper.c | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 2 deletions(-)
target/arm/cpu.h +4 -0
@@ -912,6 +912,10 @@ struct ARMCPU {*/ bool cfgend; +#ifdef CONFIG_USER_ONLY +bool tagged_pages; +#endif + QLIST_HEAD(, ARMELChangeHook) pre_el_change_hooks; QLIST_HEAD(, ARMELChangeHook) el_change_hooks;
target/arm/cpu64.c +20 -0
@@ -279,6 +279,20 @@ static void cpu_max_set_sve_vq(Object *obj, Visitor *v, const char *name,error_propagate (errp, err); } +#ifdef CONFIG_USER_ONLY +static bool aarch64_cpu_get_tagged_pages (Object *obj,Error **errp) +{ +ARMCPU *cpu =ARM_CPU (obj); +return cpu->tagged_pages; +} + +static void aarch64_cpu_set_tagged_pages (Object *obj,bool val,Error **errp) +{ +ARMCPU *cpu =ARM_CPU (obj); + cpu->tagged_pages = val; +} +#endif +/* -cpu max: if KVM is enabled, like -cpu host (best possible with this host); * otherwise, a CPU with as many features enabled as our emulation supports. * The version of '-cpu max' for qemu-system-arm is defined in cpu.c;
@@ -389,6 +403,12 @@ static void aarch64_max_initfn(Object *obj)*/ cpu->ctr =0x80038003 ;/* 32 byte I and D cacheline size, VIPT icache */ cpu->dcz_blocksize =7 ;/* 512 bytes */ + +object_property_add_bool (obj,"x-tagged-pages" , + aarch64_cpu_get_tagged_pages, + aarch64_cpu_set_tagged_pages,NULL ); +object_property_set_description (obj,"x-tagged-pages" , +"Set on/off MemAttr Tagged for all pages" ,NULL );#endif cpu->sve_max_vq =ARM_MAX_VQ ;
target/arm/mte_helper.c +33 -2
@@ -29,8 +29,39 @@ static uint8_t *allocation_tag_mem(CPUARMState *env, uint64_t ptr, bool write, uintptr_t ra) {#ifdef CONFIG_USER_ONLY -/* Tag storage not implemented. */ -return NULL ; +ARMCPU *cpu =env_archcpu (env); +uint8_t *tags; +uintptr_t index; +int flags; + + flags =page_get_flags (ptr); + +if (!(flags &PAGE_VALID ) || !(flags & (write ?PAGE_WRITE :PAGE_READ ))) { +/* SIGSEGV */ + env->exception.vaddress = ptr; +cpu_restore_state (CPU (cpu), ra, true); +raise_exception (env,EXCP_DATA_ABORT ,0 ,1 ); + } + +if (!cpu->tagged_pages) { +/* Tag storage is disabled. */ +return NULL ; + } +if (flags &PAGE_SHARED ) { +/* There may be multiple mappings; pretend not implemented. */ +return NULL ; + } + + tags =page_get_target_data (ptr); +if (tags ==NULL ) { +size_t alloc_size =TARGET_PAGE_SIZE >> (LOG2_TAG_GRANULE +1 ); + tags =page_alloc_target_data (ptr, alloc_size); +assert (tags !=NULL ); + } + + index =extract32 (ptr,LOG2_TAG_GRANULE +1 , +TARGET_PAGE_BITS -LOG2_TAG_GRANULE -1 ); +return tags + index;#else CPUState *cs =env_cpu (env);uintptr_t index;
Signature · 1 line
2.17.1
PATCH v3 3/3 tests/tcg/aarch64: Add mte smoke tests
In reply to Richard Henderson--- tests/tcg/aarch64/mte-1.c | 27 +++++++++++++++++++++ tests/tcg/aarch64/mte-2.c | 39 +++++++++++++++++++++++++++++++ tests/tcg/aarch64/Makefile.target | 5 ++++ 3 files changed, 71 insertions(+) create mode 100644 tests/tcg/aarch64/mte-1.c create mode 100644 tests/tcg/aarch64/mte-2.c
tests/tcg/aarch64/mte-1.c new file +27 -0
@@ -0,0 +1,27 @@ +/* +* Memory tagging, basic pass cases. +*/ + +#include <assert.h> + +asm(".arch armv8.5-a+memtag" ); + +int data[16 / sizeof(int)] __attribute__((aligned(16))); + +int main (int ac,char **av) +{ +int *p0 = data; +int *p1, *p2; +long c; + + asm("irg %0,%1,%2" :"=r" (p1) :"r" (p0),"r" (1 )); +assert (p1 != p0); + asm("subp %0,%1,%2" :"=r" (c) :"r" (p0),"r" (p1)); +assert (c ==0 ); + + asm("stg %0, [%0]" : :"r" (p1)); + asm("ldg %0, [%1]" :"=r" (p2) :"r" (p0),"0" (p0)); +assert (p1 == p2); + +return 0 ; +}
tests/tcg/aarch64/mte-2.c new file +39 -0
@@ -0,0 +1,39 @@ +/* +* Memory tagging, basic fail cases. +*/ + +#include <assert.h> +#include <signal.h> +#include <stdlib.h> + +asm(".arch armv8.5-a+memtag" ); + +int data[16 / sizeof(int)] __attribute__((aligned(16))); + +void pass (int sig) +{ +exit (0 ); +} + +int main (int ac,char **av) +{ +int *p0 = data; +int *p1, *p2; +long excl =1 ; + +/* Create two differently tagged pointers. */ + asm("irg %0,%1,%2" :"=r" (p1) :"r" (p0),"r" (excl)); + asm("gmi %0,%1,%0" :"+r" (excl) :"r" (p1)); +assert (excl !=1 ); + asm("irg %0,%1,%2" :"=r" (p2) :"r" (p0),"r" (excl)); +assert (p1 != p2); + +/* Store the tag from the first pointer. */ + asm("stg %0, [%0]" : :"r" (p1)); + + *p1 =0 ; +signal (SIGSEGV , pass); + *p2 =0 ; + +assert (0 ); +}
tests/tcg/aarch64/Makefile.target +5 -0
@@ -32,4 +32,9 @@ run-semihosting: semihosting # AARCH64_TESTS += bti-1 bti-1: LDFLAGS += -nostdlib -Wl,-T,$(AARCH64_SRC)/bti.ld +# We need binutils-2.32 to assemble this test case. +# AARCH64_TESTS += mte-1 mte-2 +mte-%: CFLAGS += -O -g +run-mte-%: QEMU += -cpu max,x-tagged-pages=on + TESTS += $(AARCH64_TESTS)
Signature · 1 line
2.17.1
Quoted 9 lines · Richard Henderson
This is a refresh of the user-only patch set from March.
I do not attempt to implement any part of a kernel abi wrt mmap and/or mprotect. Instead, it uses a x-tagged-pages property to assume that all anonymous pages have tags.
The tests added are disabled by default, but do pass if you have binutils 2.32 installed.
I'm going to skip reviewing this until we have a defined kernel ABI to compare against.
thanks
-- PMM