Use atomic cmpxchg to atomically check the exclusive value in a STREX
RFC PATCH v3 1/1 Use atomic cmpxchg to atomically check the exclusive value in a STREX
From: KONRAD Frederic <[email protected]>
This mechanism replaces the existing load/store exclusive mechanism which seems to be broken for multithread.
It follows the intention of the existing mechanism and stores the target address and data values during a load operation and checks that they remain unchanged before a store.
In common with the older approach, this provides weaker semantics than required in that it could be that a different processor writes the same value as a non-exclusive write, however in practise this seems to be irrelevant.
The old implementation didn’t correctly store it’s values as globals, but rather kept a local copy per CPU.
This new mechanism stores the values globally and also uses the atomic cmpxchg macros to ensure atomicity - it is therefore very efficient and threadsafe.
Changes:
V2 -> V3:
* Fix the return address for tlb_fill.
V1 -> V2:
* Remove atomic_check and atomic_release which were unused.
--- target-arm/cpu.c | 21 ++++++++ target-arm/cpu.h | 6 +++ target-arm/helper.c | 12 +++++ target-arm/helper.h | 4 ++ target-arm/op_helper.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++- target-arm/translate.c | 98 +++++++------------------------------ 6 files changed, 187 insertions(+), 82 deletions(-)
target-arm/cpu.c +21 -0
@@ -31,6 +31,26 @@#include "sysemu/kvm.h" #include "kvm_arm.h" +/* Protect cpu_exclusive_* variable .*/ +__threadbool cpu_have_exclusive_lock; +QemuMutex cpu_exclusive_lock; + +inline void arm_exclusive_lock (void ) +{ +if (!cpu_have_exclusive_lock) { +qemu_mutex_lock (&cpu_exclusive_lock); + cpu_have_exclusive_lock = true; + } +} + +inline void arm_exclusive_unlock (void ) +{ +if (cpu_have_exclusive_lock) { + cpu_have_exclusive_lock = false; +qemu_mutex_unlock (&cpu_exclusive_lock); + } +} +static void arm_cpu_set_pc (CPUState *cs,vaddr value) {ARMCPU *cpu =ARM_CPU (cs);
@@ -425,6 +445,7 @@ static void arm_cpu_initfn(Object *obj) cpu->psci_version =2 ;/* TCG implements PSCI 0.2 */ if (!inited) { inited = true; +qemu_mutex_init (&cpu_exclusive_lock);arm_translate_init (); } }
target-arm/cpu.h +6 -0
@@ -506,6 +506,9 @@ static inline bool is_a64(CPUARMState *env)int cpu_arm_signal_handler (int host_signum,void *pinfo,void *puc); +int arm_get_phys_addr (CPUARMState *env,target_ulong address,int access_type, +hwaddr *phys_ptr,int *prot,target_ulong *page_size); +/** * pmccntr_sync * @env: CPUARMState
@@ -1922,4 +1925,7 @@ enum { QEMU_PSCI_CONDUIT_HVC = 2, }; +void arm_exclusive_lock (void ); +void arm_exclusive_unlock (void ); +#endif
target-arm/helper.c +12 -0
@@ -23,6 +23,14 @@ static inline int get_phys_addr(CPUARMState *env, target_ulong address,#define PMCRE 0x1#endif +int arm_get_phys_addr (CPUARMState *env,target_ulong address,int access_type, +hwaddr *phys_ptr,int *prot,target_ulong *page_size) +{ +MemTxAttrs attrs = {}; +return get_phys_addr (env, address, access_type,cpu_mmu_index (env), + phys_ptr, &attrs, prot, page_size); +} +static int vfp_gdb_get_reg (CPUARMState *env,uint8_t *buf,int reg) {int nregs;
@@ -4731,6 +4739,10 @@ void arm_cpu_do_interrupt(CPUState *cs)arm_log_exception (cs->exception_index); +arm_exclusive_lock (); + env->exclusive_addr =-1 ; +arm_exclusive_unlock (); +if (arm_is_psci_call (cpu, cs->exception_index)) {arm_handle_psci_call (cpu);qemu_log_mask (CPU_LOG_INT ,"...handled as PSCI call\n" );
target-arm/helper.h +4 -0
@@ -529,6 +529,10 @@ DEF_HELPER_2(dc_zva, void, env, i64) DEF_HELPER_FLAGS_2(neon_pmull_64_lo, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(neon_pmull_64_hi, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_4(atomic_cmpxchg64, i32, env, i32, i64, i32) +DEF_HELPER_1(atomic_clear, void, env) +DEF_HELPER_3(atomic_claim, void, env, i32, i64) +#ifdef TARGET_AARCH64 #include "helper-a64.h" #endif
target-arm/op_helper.c +127 -1
146 lines not on this page · raw message
target-arm/translate.c +17 -81
135 lines not on this page · raw message
Signature · 1 line
1.9.0
Quoted 1 line · On 18/06/2015 17:44, [email protected] wrote:
+ hwaddr len = 8 << size;
Should be 1 << size, and likewise below in the "if".
Paolo
Quoted 68 lines
target-arm/op_helper.c +68
-0+hwaddr paddr; +target_ulong page_size; +int prot; + +arm_exclusive_lock (); + +if (env->exclusive_addr != addr) { +arm_exclusive_unlock (); +return 1 ; + } + +if (arm_get_phys_addr (env, addr,1 , &paddr, &prot, &page_size) !=0 ) { +tlb_fill (ENV_GET_CPU (env), addr,MMU_DATA_STORE ,cpu_mmu_index (env), + retaddr); +if (arm_get_phys_addr (env, addr,1 , &paddr, &prot, &page_size) !=0 ) { +arm_exclusive_unlock (); +return 1 ; + } + } + +switch (size) { +case 0 : + { +uint8_t oldval, *p; + p =address_space_map (cs->as, paddr, &len, true); +if (len ==8 << size) { + oldval = (uint8_t )env->exclusive_val; + result = (atomic_cmpxchg (p, oldval, (uint8_t )newval) == oldval); + } +address_space_unmap (cs->as, p, len, true, result ?8 :0 ); + } +break ; +case 1 : + { +uint16_t oldval, *p; + p =address_space_map (cs->as, paddr, &len, true); +if (len ==8 << size) { + oldval = (uint16_t )env->exclusive_val; + result = (atomic_cmpxchg (p, oldval, (uint16_t )newval) == oldval); + } +address_space_unmap (cs->as, p, len, true, result ?8 :0 ); + } +break ; +case 2 : + { +uint32_t oldval, *p; + p =address_space_map (cs->as, paddr, &len, true); +if (len ==8 << size) { + oldval = (uint32_t )env->exclusive_val; + result = (atomic_cmpxchg (p, oldval, (uint32_t )newval) == oldval); + } +address_space_unmap (cs->as, p, len, true, result ?8 :0 ); + } +break ; +case 3 : + { +uint64_t oldval, *p; + p =address_space_map (cs->as, paddr, &len, true); +if (len ==8 << size) { + oldval = (uint64_t )env->exclusive_val; + result = (atomic_cmpxchg (p, oldval, (uint64_t )newval) == oldval); + } +address_space_unmap (cs->as, p, len, true, result ?8 :0 ); + } +break ; +default : +abort (); +break ;
target-arm/op_helper.c static void raise_exception(CPUARMState *env, uint32_t excp, · line 118
Quoted 5 lines · On 18 June 2015 at 16:44, <[email protected]> wrote:
target-arm/op_helper.c +5
-0+uint64_t oldval, *p; + p =address_space_map (cs->as, paddr, &len, true); +if (len ==8 << size) { + oldval = (uint64_t )env->exclusive_val; + result = (atomic_cmpxchg (p, oldval, (uint64_t )newval) == oldval);
You can't do an atomic operation on a type that's larger than the pointer size of the host system. That means that for code that isn't host specific, like this, in practice you can't do an atomic operation on a larger size than 4 bytes.
(It happens to work on x86, I think, but this won't build on ppc32, for instance.)
thanks
-- PMM
for the 1<<size thing - I think that code has been used elsewhere, which is a little worrying - I’ll check.
Quoted 9 lines · Peter Maydell
target-arm/op_helper.c static void raise_exception(CPUARMState *env, uint32_t excp, · line 118
Quoted 5 lines · On 18 June 2015 at 16:44, <[email protected]> wrote:
target-arm/op_helper.c +5
-0+uint64_t oldval, *p; + p =address_space_map (cs->as, paddr, &len, true); +if (len ==8 << size) { + oldval = (uint64_t )env->exclusive_val; + result = (atomic_cmpxchg (p, oldval, (uint64_t )newval) == oldval);
Quoted 5 lines
You can't do an atomic operation on a type that's larger than the pointer size of the host system. That means that for code that isn't host specific, like this, in practice you can't do an atomic operation on a larger size than 4 bytes.
I thought they were polymorphic across all types, I didn’t notice the caveat of the size, sorry about that. That makes things more entertaining :-)
Cheers
Mark.
Quoted 5 lines
(It happens to work on x86, I think, but this won't build on ppc32, for instance.)
thanks
-- PMM
+44 (0)20 7100 3485 x 210
+33 (0)5 33 52 01 77x 210
+33 (0)603762104 mark.burton
Quoted 17 lines · Mark Burton
for the 1<<size thing - I think that code has been used elsewhere, which is a little worrying - I’ll check.
Quoted 9 lines · Peter Maydell
target-arm/op_helper.c static void raise_exception(CPUARMState *env, uint32_t excp, · line 118
Quoted 5 lines · On 18 June 2015 at 16:44, <[email protected]> wrote:
target-arm/op_helper.c +5
-0+uint64_t oldval, *p; + p =address_space_map (cs->as, paddr, &len, true); +if (len ==8 << size) { + oldval = (uint64_t )env->exclusive_val; + result = (atomic_cmpxchg (p, oldval, (uint64_t )newval) == oldval);Quoted 5 lines
You can't do an atomic operation on a type that's larger than the pointer size of the host system. That means that for code that isn't host specific, like this, in practice you can't do an atomic operation on a larger size than 4 bytes.
Quoted 3 lines
I thought they were polymorphic across all types, I didn’t notice the caveat of the size, sorry about that. That makes things more entertaining :-)
It's polymorphic across most types... It's been suggested that the macros should refuse types with size > ptrsize on all systems, so you don't have to get bitten by a ppc32 compile failure after the fact, but I don't think that anybody's written the patch yet.
-- PMM
Quoted 25 lines · Peter Maydell
Quoted 17 lines · Mark Burton
for the 1<<size thing - I think that code has been used elsewhere, which is a little worrying - I’ll check.
Quoted 9 lines · Peter Maydell
target-arm/op_helper.c static void raise_exception(CPUARMState *env, uint32_t excp, · line 118
Quoted 5 lines · On 18 June 2015 at 16:44, <[email protected]> wrote:
target-arm/op_helper.c +5
-0+uint64_t oldval, *p; + p =address_space_map (cs->as, paddr, &len, true); +if (len ==8 << size) { + oldval = (uint64_t )env->exclusive_val; + result = (atomic_cmpxchg (p, oldval, (uint64_t )newval) == oldval);Quoted 5 lines
You can't do an atomic operation on a type that's larger than the pointer size of the host system. That means that for code that isn't host specific, like this, in practice you can't do an atomic operation on a larger size than 4 bytes.
Quoted 3 lines
I thought they were polymorphic across all types, I didn’t notice the caveat of the size, sorry about that. That makes things more entertaining :-)
Quoted 6 lines
It's polymorphic across most types... It's been suggested that the macros should refuse types with size > ptrsize on all systems, so you don't have to get bitten by a ppc32 compile failure after the fact, but I don't think that anybody's written the patch yet.
That would be sensible.
In the meantime,
It looks to me like (most implementations of) 32 bit x86 support double word cmpxchg - but I dont know if the library uses that, and I’d have to find a machine to try it on…
In any case, we could support that relatively easily it seems.
So, we’re talking about 64bit ARM ldrex/strex, being run, in multi-thread mode, on a multi-core 32bit probably non x86 host…..
We can add mutex’s around the ld/strex, and effectively implement the mechanism as it is upstream today, (fixing where the address/data is stored). That would give some protection, but it would not have the advantage of the atomicity that gives us somewhat better protection against a non exclusive store breaking the lock.
If we were to do that, we would be saying - on multi-host 32 bit non x86 hosts, with a 64 bit multi-core, multi thread arm guest, and a guest that uses normal stores to break an exclusive lock, we would have a race condition.
I’m minded to say that we should simply not support that case for now. I propose that the multi-thread switch will only allow you to switch into multi-thread mode if the maximum cmpxchg that can be supported is the same or bigger than the guest ptrsize.
Does that seem reasonable?
Do you have a better idea?
Does anybody know if the current atomic_cmpxchg will support 64 bit on a (normal) 32 bit x86, or do we need to special case that with cmpxchg8b ? (I get the impression that it will automatically use cmpxchg8b, but not cmpxchg16b - but I’m by no means sure).
Cheers
Mark.
Quoted 1 line
-- PMM
+44 (0)20 7100 3485 x 210
+33 (0)5 33 52 01 77x 210
+33 (0)603762104 mark.burton
Quoted 4 lines · Mark Burton
Does anybody know if the current
atomic_cmpxchgwill support 64 bit on a (normal) 32 bit x86, or do we need to special case that with cmpxchg8b ? (I get the impression that it will automatically use cmpxchg8b, but not cmpxchg16b - but I’m by no means sure).
Both cmpxchg8b and cmpxchg16b are used, respectively on 32-bit and 64-bit x86.
Paolo
Quoted 14 lines · Paolo Bonzini
Quoted 4 lines · Mark Burton
Does anybody know if the current
atomic_cmpxchgwill support 64 bit on a (normal) 32 bit x86, or do we need to special case that with cmpxchg8b ? (I get the impression that it will automatically use cmpxchg8b, but not cmpxchg16b - but I’m by no means sure).Both cmpxchg8b and cmpxchg16b are used, respectively on 32-bit and 64-bit x86.
Thanks Paolo, so we are OK for x86, but we would need to disable multi-thread for other 32 bit hosts, and provide a correct implementation for non multi-thread… You dont happen to know of a convenient macro we can use to test for ’32 bit hosts that dont support 64bit cmpxchg ….’
Cheers
Mark.
Quoted 1 line
Paolo
+44 (0)20 7100 3485 x 210
+33 (0)5 33 52 01 77x 210
+33 (0)603762104 mark.burton
Quoted 7 lines · Mark Burton
Quoted 7 lines · Mark Burton
Quoted 5 lines
Does anybody know if the current
atomic_cmpxchgwill support 64 bit on a (normal) 32 bit x86, or do we need to special case that with cmpxchg8b ? (I get the impression that it will automatically use cmpxchg8b, but not cmpxchg16b - but I’m by no means sure).
Quoted 6 lines
Quoted 2 lines
Both cmpxchg8b and cmpxchg16b are used, respectively on 32-bit and 64-bit x86.
Thanks Paolo, so we are OK for x86, but we would need to disable multi-thread for other 32 bit hosts, and provide a correct implementation for non multi-thread…
But Alvise's implementation for example would work there. It is just this optimization (that is also not architecturally correct on ARM) that is problematic.
Paolo
Quoted 3 lines
You dont happen to know of a
convenient macro we can use to test for ’32 bit hosts that dont support 64bit cmpxchg ….’
Quoted 16 lines · Paolo Bonzini
Quoted 10 lines · Mark Burton
Quoted 9 lines · Mark Burton
Quoted 5 lines
Does anybody know if the current
atomic_cmpxchgwill support 64 bit on a (normal) 32 bit x86, or do we need to special case that with cmpxchg8b ? (I get the impression that it will automatically use cmpxchg8b, but not cmpxchg16b - but I’m by no means sure).Both cmpxchg8b and cmpxchg16b are used, respectively on 32-bit and 64-bit x86.
Quoted 7 lines
Quoted 3 lines
Thanks Paolo, so we are OK for x86, but we would need to disable multi-thread for other 32 bit hosts, and provide a correct implementation for non multi-thread…
But Alvise's implementation for example would work there. It is just this optimization (that is also not architecturally correct on ARM) that is problematic.
Yes, that is exactly correct.
Cheers
Mark.
Quoted 7 lines
Paolo
Quoted 3 lines
You dont happen to know of a
convenient macro we can use to test for ’32 bit hosts that dont support 64bit cmpxchg ….’
+44 (0)20 7100 3485 x 210
+33 (0)5 33 52 01 77x 210
+33 (0)603762104 mark.burton