target/arm: Remove bswap_code
cover target/arm: Remove bswap_code
Based-on: [email protected] ("[PATCH v11 00/21] target/arm: single-binary")
Eliminate the last usage of bswap_code, fix a little bug, and drop the now unused function.
r~
Richard Henderson (2):
linux-user/arm: Improve get_user_code_u32 and usage target/arm: Remove bswap_code
target/arm/cpu.h | 16 ---------- linux-user/arm/cpu_loop.c | 64 ++++++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 40 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 linux-user/arm: Improve get_user_code_u32 and usage
Force a little-endian load before applying SCTLR.B. Handle the BE32 swap correctly in thumb mode. This fixes a bug with its later usage in insn_is_linux_bkpt.
Handle lock_user failure: in one case by simply falling back to the original SIGILL and the other by raising SIGSEGV.
--- linux-user/arm/cpu_loop.c | 64 ++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 24 deletions(-)
linux-user/arm/cpu_loop.c +40 -24
@@ -29,13 +29,34 @@#include "user/page-protection.h" #include "target/arm/syndrome.h" -#define get_user_code_u32 (x, gaddr, env) \ - ({ abi_long __r = get_user_u32((x), (gaddr)); \ - if (!__r && bswap_code(arm_sctlr_b(env))) { \ - (x) = bswap32(x); \ - } \ - __r; \ - }) +static bool get_user_code_u32 (uint32_t *ret,uint32_t va,CPUARMState *env) +{ +uint32_t insn, *hptr; + + hptr =lock_user (VERIFY_READ , va,sizeof (uint32_t ),1 ); +if (!hptr) { + *ret =0 ; +return false; + } + +/* Load as little-endian by default. */ +__get_user_e (insn, hptr, le); +unlock_user (hptr, va,0 ); + +/* +* Adjust the little-endian load for BE32 as required. +* In thumb mode, bswap both halfwords by bswap of word then +* swapping the halfwords again to restore the original order. +*/ +if (arm_sctlr_b (env)) { + insn =bswap32 (insn); +if (env->thumb) { + insn =hswap32 (insn); + } + } + *ret = insn; +return true; +}/* * Note that if we need to do data accesses here, they should do a
@@ -271,7 +292,8 @@ void cpu_loop(CPUARMState *env) {CPUState *cs =env_cpu (env);int trapnr, si_signo, si_code; -unsigned int n, insn; +unsigned int n; +uint32_t insn;abi_ulong ret;for (;;) {
@@ -284,31 +306,23 @@ void cpu_loop(CPUARMState *env)case EXCP_UDEF :case EXCP_NOCP :case EXCP_INVSTATE : - { -uint32_t opcode; - -/* we handle the FPU emulation here, as Linux */ -/* we get the opcode */ -/* FIXME - what to do if get_user() fails? */ -get_user_code_u32 (opcode, env->regs[15 ], env); - +/* we handle the FPU emulation here, as Linux */ +if (get_user_code_u32 (&insn, env->regs[15 ], env)) {/* * The Linux kernel treats some UDF patterns specially * to use as breakpoints (instead of the architectural * bkpt insn). These should trigger a SIGTRAP rather * than SIGILL. */ -if (insn_is_linux_bkpt (opcode, env->thumb)) { +if (insn_is_linux_bkpt (insn, env->thumb)) { goto excp_debug; } -if (!env->thumb &&emulate_arm_fpa11 (env, opcode)) { +if (!env->thumb &&emulate_arm_fpa11 (env, insn)) {break ; } - -force_sig_fault (TARGET_SIGILL ,TARGET_ILL_ILLOPN , - env->regs[15 ]); } +force_sig_fault (TARGET_SIGILL ,TARGET_ILL_ILLOPN , env->regs[15 ]);break ;case EXCP_SWI : {
@@ -317,14 +331,12 @@ void cpu_loop(CPUARMState *env)if (env->thumb) {/* Thumb is always EABI style with syscall number in r7 */ n = env->regs[7 ]; - }else { + }else if (get_user_code_u32 (&insn, env->regs[15 ] -4 , env)) {/* * Equivalent of kernel CONFIG_OABI_COMPAT: read the * Arm SVC insn to extract the immediate, which is the * syscall number in OABI. */ -/* FIXME - what to do if get_user() fails? */ -get_user_code_u32 (insn, env->regs[15 ] -4 , env); n = insn &0xffffff ;if (n ==0 ) {/* zero immediate: EABI, syscall number in r7 */
@@ -340,6 +352,10 @@ void cpu_loop(CPUARMState *env) n ^=ARM_SYSCALL_BASE ; env->eabi = false; } + }else { +/* Failed to re-load instruction. */ +force_sig_fault (TARGET_SIGSEGV ,0 , env->regs[15 ] -4 ); +break ; }if (n >ARM_NR_BASE ) {
Signature · 1 line
2.43.0
Re: [PATCH 1/2] linux-user/arm: Improve get_user_code_u32 and usage
Quoted 122 lines · Richard Henderson
Force a little-endian load before applying SCTLR.B. Handle the BE32 swap correctly in thumb mode. This fixes a bug with its later usage in
insn_is_linux_bkpt.Handle
lock_userfailure: in one case by simply falling back to the original SIGILL and the other by raising SIGSEGV.Signed-off-by Richard Henderson <[email protected]>--- linux-user/arm/cpu_loop.c | 64 ++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 24 deletions(-)linux-user/arm/cpu_loop.c +36
-24@@ -29,13 +29,34 @@#include "user/page-protection.h" #include "target/arm/syndrome.h" -#define get_user_code_u32 (x, gaddr, env) \ - ({ abi_long __r = get_user_u32((x), (gaddr)); \ - if (!__r && bswap_code(arm_sctlr_b(env))) { \ - (x) = bswap32(x); \ - } \ - __r; \ - }) +static bool get_user_code_u32 (uint32_t *ret,uint32_t va,CPUARMState *env) +{ +uint32_t insn, *hptr; + + hptr =lock_user (VERIFY_READ , va,sizeof (uint32_t ),1 ); +if (!hptr) { + *ret =0 ; +return false; + } + +/* Load as little-endian by default. */ +__get_user_e (insn, hptr, le); +unlock_user (hptr, va,0 ); + +/* +* Adjust the little-endian load for BE32 as required. +* In thumb mode, bswap both halfwords by bswap of word then +* swapping the halfwords again to restore the original order. +*/ +if (arm_sctlr_b (env)) { + insn =bswap32 (insn); +if (env->thumb) { + insn =hswap32 (insn); + } + } + *ret = insn; +return true; +}/* * Note that if we need to do data accesses here, they should do a @@ -271,7 +292,8 @@ void cpu_loop(CPUARMState *env) {CPUState *cs =env_cpu (env);int trapnr, si_signo, si_code; -unsigned int n, insn; +unsigned int n; +uint32_t insn;abi_ulong ret;for (;;) {@@ -284,31 +306,23 @@ void cpu_loop(CPUARMState *env)case EXCP_UDEF :case EXCP_NOCP :case EXCP_INVSTATE : - { -uint32_t opcode; - -/* we handle the FPU emulation here, as Linux */ -/* we get the opcode */ -/* FIXME - what to do if get_user() fails? */ -get_user_code_u32 (opcode, env->regs[15 ], env); - +/* we handle the FPU emulation here, as Linux */ +if (get_user_code_u32 (&insn, env->regs[15 ], env)) {/* * The Linux kernel treats some UDF patterns specially * to use as breakpoints (instead of the architectural * bkpt insn). These should trigger a SIGTRAP rather * than SIGILL. */ -if (insn_is_linux_bkpt (opcode, env->thumb)) { +if (insn_is_linux_bkpt (insn, env->thumb)) { goto excp_debug; } -if (!env->thumb &&emulate_arm_fpa11 (env, opcode)) { +if (!env->thumb &&emulate_arm_fpa11 (env, insn)) {break ; } - -force_sig_fault (TARGET_SIGILL ,TARGET_ILL_ILLOPN , - env->regs[15 ]); } +force_sig_fault (TARGET_SIGILL ,TARGET_ILL_ILLOPN , env->regs[15 ]);break ;case EXCP_SWI : {@@ -317,14 +331,12 @@ void cpu_loop(CPUARMState *env)if (env->thumb) {/* Thumb is always EABI style with syscall number in r7 */ n = env->regs[7 ]; - }else { + }else if (get_user_code_u32 (&insn, env->regs[15 ] -4 , env)) {/* * Equivalent of kernel CONFIG_OABI_COMPAT: read the * Arm SVC insn to extract the immediate, which is the * syscall number in OABI. */ -/* FIXME - what to do if get_user() fails? */ -get_user_code_u32 (insn, env->regs[15 ] -4 , env); n = insn &0xffffff ;if (n ==0 ) {/* zero immediate: EABI, syscall number in r7 */
linux-user/arm/cpu_loop.c void cpu_loop(CPUARMState *env) · line 361
Quoted 10 lines
linux-user/arm/cpu_loop.c +4
-0@@ -340,6 +352,10 @@ void cpu_loop(CPUARMState *env) n ^=ARM_SYSCALL_BASE ; env->eabi = false; } + }else { +/* Failed to re-load instruction. */ +force_sig_fault (TARGET_SIGSEGV ,0 , env->regs[15 ] -4 ); +break ; }if (n >ARM_NR_BASE ) {
Regards,
Pierrick
Re: [PATCH 1/2] linux-user/arm: Improve get_user_code_u32 and usage
In reply to Richard HendersonQuoted 45 lines · Richard Henderson
Force a little-endian load before applying SCTLR.B. Handle the BE32 swap correctly in thumb mode. This fixes a bug with its later usage in
insn_is_linux_bkpt.Handle
lock_userfailure: in one case by simply falling back to the original SIGILL and the other by raising SIGSEGV.Signed-off-by Richard Henderson <[email protected]>--- linux-user/arm/cpu_loop.c | 64 ++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 24 deletions(-)linux-user/arm/cpu_loop.c +18
-7@@ -29,13 +29,34 @@#include "user/page-protection.h" #include "target/arm/syndrome.h" -#define get_user_code_u32 (x, gaddr, env) \ - ({ abi_long __r = get_user_u32((x), (gaddr)); \ - if (!__r && bswap_code(arm_sctlr_b(env))) { \ - (x) = bswap32(x); \ - } \ - __r; \ - }) +static bool get_user_code_u32 (uint32_t *ret,uint32_t va,CPUARMState *env) +{ +uint32_t insn, *hptr; + + hptr =lock_user (VERIFY_READ , va,sizeof (uint32_t ),1 ); +if (!hptr) { + *ret =0 ; +return false; + } + +/* Load as little-endian by default. */ +__get_user_e (insn, hptr, le); +unlock_user (hptr, va,0 ); + +/* +* Adjust the little-endian load for BE32 as required. +* In thumb mode, bswap both halfwords by bswap of word then +* swapping the halfwords again to restore the original order.
linux-user/arm/cpu_loop.c line 59
Quoted 10 lines
linux-user/arm/cpu_loop.c +10
-0+*/ +if (arm_sctlr_b (env)) { + insn =bswap32 (insn); +if (env->thumb) { + insn =hswap32 (insn); + } + } + *ret = insn; +return true; +}
This isn't right for thumb be32, at least not matching translate.c.
Translate loads 2 halfwords from
va ^ 2
(va + 2) ^ 2
Let va = A + 2, where A % 4 == 0:
(A + 2) ^ 2 == A
((A + 2) + 2) ^ 2 = (A + 4) ^ 2 = A + 6
We'd need to lock 8 bytes in this case, loading the first and last two.
It is tempting to have translate.c store the instruction that it loaded as part of raising the exception so that we don't have to think about such things again here.
Peter, what do you think?
r~
PATCH 2/2 target/arm: Remove bswap_code
In reply to Richard HendersonThis function is no loner used.
--- target/arm/cpu.h | 16 ---------------- 1 file changed, 16 deletions(-)
target/arm/cpu.h +0 -16
@@ -2568,22 +2568,6 @@ static inline int sme_vq(CPUARMState *env)return EX_TBFLAG_A64 (env->hflags,SVL ) +1 ; } -static inline bool bswap_code (bool sctlr_b) -{ -#ifdef CONFIG_USER_ONLY -/* BE8 (SCTLR.B = 0, TARGET_BIG_ENDIAN = 1) is mixed endian. -* The invalid combination SCTLR.B=1/CPSR.E=1/TARGET_BIG_ENDIAN=0 -* would also end up as a mixed-endian mode with BE code, LE data. -*/ -return TARGET_BIG_ENDIAN ^ sctlr_b; -#else -/* All code access in ARM is little endian, and there are no loaders -* doing swaps that need to be reversed -*/ -return 0 ; -#endif -} -enum {QEMU_PSCI_CONDUIT_DISABLED =0 ,QEMU_PSCI_CONDUIT_SMC =1 ,
Signature · 1 line
2.43.0
Quoted 24 lines · Richard Henderson
This function is no loner used.
Signed-off-by Richard Henderson <[email protected]>--- target/arm/cpu.h | 16 ---------------- 1 file changed, 16 deletions(-)target/arm/cpu.h +0
-9@@ -2568,22 +2568,6 @@ static inline int sme_vq(CPUARMState *env)return EX_TBFLAG_A64 (env->hflags,SVL ) +1 ; } -static inline bool bswap_code (bool sctlr_b) -{ -#ifdef CONFIG_USER_ONLY -/* BE8 (SCTLR.B = 0, TARGET_BIG_ENDIAN = 1) is mixed endian. -* The invalid combination SCTLR.B=1/CPSR.E=1/TARGET_BIG_ENDIAN=0 -* would also end up as a mixed-endian mode with BE code, LE data. -*/ -return TARGET_BIG_ENDIAN ^ sctlr_b; -#else
target/arm/cpu.h static inline int sme_vq(CPUARMState *env) · line 2573
Quoted 10 lines
target/arm/cpu.h +0
-7-/* All code access in ARM is little endian, and there are no loaders -* doing swaps that need to be reversed -*/ -return 0 ; -#endif -} -enum {QEMU_PSCI_CONDUIT_DISABLED =0 ,QEMU_PSCI_CONDUIT_SMC =1 ,
Quoted 1 line · Richard Henderson
This function is no loner used.
\o/
(Typo "longer")
Quoted 4 lines
Signed-off-by Richard Henderson <[email protected]>--- target/arm/cpu.h | 16 ---------------- 1 file changed, 16 deletions(-)