linux-user/arm: Fix BKPT, SVC immediate handling
cover linux-user/arm: Fix BKPT, SVC immediate handling
This patchseries fixes issues with the code in linux-user/arm/cpu_loop.c:
* it incorrectly thinks BKPT is a syscall instruction
(https://bugs.launchpad.net/qemu/+bug/1873898, reported via irc)
* a stale line of code means we incorrectly NOP SVC #0xf0002
* we don't implement the distinction between 0x9f0000..0x9f07ff (should return -ENOSYS if not implemented) and higher numbers (should cause a SIGILL)
* we abort() for bad immediate values to SVC (ie not the 0 of EABI or the >0x9f0000 of OABI); the kernel delivers a SIGILL for these
* for Thumb mode, we never use the immediate value from the insn, but we always read it anyway
This patchseries fixes all those things. (I started out fixing the BKPT bug; everything else is problems I spotted along the way while I was reading this bit of code...)
thanks
-- PMM
Peter Maydell (4):
linux-user/arm: BKPT should cause SIGTRAP, not be a syscall linux-user/arm: Remove bogus SVC 0xf0002 handling linux-user/arm: Handle invalid arm-specific syscalls correctly linux-user/arm: Fix identification of syscall numbers
linux-user/arm/cpu_loop.c | 145 +++++++++++++++++++++----------------- 1 file changed, 81 insertions(+), 64 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/4 linux-user/arm: BKPT should cause SIGTRAP, not be a syscall
In linux-user/arm/cpu-loop.c we incorrectly treat EXCP_BKPT similarly to EXCP_SWI, which means that if the guest executes a BKPT insn then QEMU will perform a syscall for it (which syscall depends on what value happens to be in r7...). The correct behaviour is that the guest process should take a SIGTRAP.
This code has been like this (more or less) since commit 06c949e62a098f in 2006 which added BKPT in the first place. This is probably because at the time the same code path was used to handle both Linux syscalls and semihosting calls, and (on M profile) BKPT with a suitable magic number is used for semihosting calls. But these days we've moved handling of semihosting out to an entirely different codepath, so we can fix this bug by simply removing this handling of EXCP_BKPT and instead making it deliver a SIGTRAP like EXCP_DEBUG (as we do already on aarch64).
--- linux-user/arm/cpu_loop.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-)
linux-user/arm/cpu_loop.c +8 -22
@@ -295,32 +295,17 @@ void cpu_loop(CPUARMState *env) }break ;case EXCP_SWI : -case EXCP_BKPT : { env->eabi =1 ;/* system call */ -if (trapnr ==EXCP_BKPT ) { -if (env->thumb) { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u16 (insn, env->regs[15 ], env); - n = insn &0xff ; - env->regs[15 ] +=2 ; - }else { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u32 (insn, env->regs[15 ], env); - n = (insn &0xf ) | ((insn >>4 ) &0xff0 ); - env->regs[15 ] +=4 ; - } +if (env->thumb) { +/* FIXME - what to do if get_user() fails? */ +get_user_code_u16 (insn, env->regs[15 ] -2 , env); + n = insn &0xff ; }else { -if (env->thumb) { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u16 (insn, env->regs[15 ] -2 , env); - n = insn &0xff ; - }else { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u32 (insn, env->regs[15 ] -4 , env); - n = insn &0xffffff ; - } +/* FIXME - what to do if get_user() fails? */ +get_user_code_u32 (insn, env->regs[15 ] -4 , env); + n = insn &0xffffff ; }if (n == ARM_NR_cacheflush) {
@@ -396,6 +381,7 @@ void cpu_loop(CPUARMState *env) }break ;case EXCP_DEBUG : +case EXCP_BKPT : excp_debug: info.si_signo =TARGET_SIGTRAP ; info.si_errno =0 ;
Signature · 1 line
2.20.1
Re: [PATCH 1/4] linux-user/arm: BKPT should cause SIGTRAP, not be a syscall
Quoted 55 lines · Peter Maydell
In
linux-user/arm/cpu-loop.cwe incorrectly treatEXCP_BKPTsimilarly toEXCP_SWI, which means that if the guest executes a BKPT insn then QEMU will perform a syscall for it (which syscall depends on what value happens to be in r7...). The correct behaviour is that the guest process should take a SIGTRAP.This code has been like this (more or less) since commit
06c949e62a098fin 2006 which added BKPT in the first place. This is probably because at the time the same code path was used to handle both Linux syscalls and semihosting calls, and (on M profile) BKPT with a suitable magic number is used for semihosting calls. But these days we've moved handling of semihosting out to an entirely different codepath, so we can fix this bug by simply removing this handling ofEXCP_BKPTand instead making it deliver a SIGTRAP likeEXCP_DEBUG(as we do already on aarch64).Reported-by <[email protected]>Signed-off-by Peter Maydell <[email protected]>--- linux-user/arm/cpu_loop.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-)linux-user/arm/cpu_loop.c +4
-16@@ -295,32 +295,17 @@ void cpu_loop(CPUARMState *env) }break ;case EXCP_SWI : -case EXCP_BKPT : { env->eabi =1 ;/* system call */ -if (trapnr ==EXCP_BKPT ) { -if (env->thumb) { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u16 (insn, env->regs[15 ], env); - n = insn &0xff ; - env->regs[15 ] +=2 ; - }else { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u32 (insn, env->regs[15 ], env); - n = (insn &0xf ) | ((insn >>4 ) &0xff0 ); - env->regs[15 ] +=4 ; - } +if (env->thumb) { +/* FIXME - what to do if get_user() fails? */ +get_user_code_u16 (insn, env->regs[15 ] -2 , env); + n = insn &0xff ; }else { -if (env->thumb) { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u16 (insn, env->regs[15 ] -2 , env);
linux-user/arm/cpu_loop.c void cpu_loop(CPUARMState *env) · line 309
Quoted 10 lines
linux-user/arm/cpu_loop.c +3
-6- n = insn &0xff ; - }else { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u32 (insn, env->regs[15 ] -4 , env); - n = insn &0xffffff ; - } +/* FIXME - what to do if get_user() fails? */ +get_user_code_u32 (insn, env->regs[15 ] -4 , env); + n = insn &0xffffff ; }
I couldn't find a git-diff option to display this change in an obvious way.
Quoted 11 lines
linux-user/arm/cpu_loop.c +1
-0if (n == ARM_NR_cacheflush) {@@ -396,6 +381,7 @@ void cpu_loop(CPUARMState *env) }break ;case EXCP_DEBUG : +case EXCP_BKPT : excp_debug: info.si_signo =TARGET_SIGTRAP ; info.si_errno =0 ;
Re: [PATCH 1/4] linux-user/arm: BKPT should cause SIGTRAP, not be a syscall
Quoted 1 line · Philippe Mathieu-Daudé
I couldn't find a git-diff option to display this change in an obvious way.
Yeah, as a human you can say "it would be easier to read if the '} else {' line was not treated as unchanged", but the automatic diff output isn't totally awful. Sometimes you do just have to look at the resulting code...
thanks
-- PMM
Re: [PATCH 1/4] linux-user/arm: BKPT should cause SIGTRAP, not be a syscall
In reply to Peter MaydellQuoted 16 lines · Peter Maydell
In
linux-user/arm/cpu-loop.cwe incorrectly treatEXCP_BKPTsimilarly toEXCP_SWI, which means that if the guest executes a BKPT insn then QEMU will perform a syscall for it (which syscall depends on what value happens to be in r7...). The correct behaviour is that the guest process should take a SIGTRAP.This code has been like this (more or less) since commit
06c949e62a098fin 2006 which added BKPT in the first place. This is probably because at the time the same code path was used to handle both Linux syscalls and semihosting calls, and (on M profile) BKPT with a suitable magic number is used for semihosting calls. But these days we've moved handling of semihosting out to an entirely different codepath, so we can fix this bug by simply removing this handling ofEXCP_BKPTand instead making it deliver a SIGTRAP likeEXCP_DEBUG(as we do already on aarch64).
Quoted 3 lines
Reported-by <[email protected]>Signed-off-by Peter Maydell <[email protected]>
Quoted 60 lines
--- linux-user/arm/cpu_loop.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-)linux-user/arm/cpu_loop.c +8
-22@@ -295,32 +295,17 @@ void cpu_loop(CPUARMState *env) }break ;case EXCP_SWI : -case EXCP_BKPT : { env->eabi =1 ;/* system call */ -if (trapnr ==EXCP_BKPT ) { -if (env->thumb) { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u16 (insn, env->regs[15 ], env); - n = insn &0xff ; - env->regs[15 ] +=2 ; - }else { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u32 (insn, env->regs[15 ], env); - n = (insn &0xf ) | ((insn >>4 ) &0xff0 ); - env->regs[15 ] +=4 ; - } +if (env->thumb) { +/* FIXME - what to do if get_user() fails? */ +get_user_code_u16 (insn, env->regs[15 ] -2 , env); + n = insn &0xff ; }else { -if (env->thumb) { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u16 (insn, env->regs[15 ] -2 , env); - n = insn &0xff ; - }else { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u32 (insn, env->regs[15 ] -4 , env); - n = insn &0xffffff ; - } +/* FIXME - what to do if get_user() fails? */ +get_user_code_u32 (insn, env->regs[15 ] -4 , env); + n = insn &0xffffff ; }if (n == ARM_NR_cacheflush) {@@ -396,6 +381,7 @@ void cpu_loop(CPUARMState *env) }break ;case EXCP_DEBUG : +case EXCP_BKPT : excp_debug: info.si_signo =TARGET_SIGTRAP ; info.si_errno =0 ;Signature · 1 line
2.20.1
PATCH 2/4 linux-user/arm: Remove bogus SVC 0xf0002 handling
In reply to Peter MaydellWe incorrectly treat SVC 0xf0002 as a cacheflush request (which is a NOP for QEMU). This is the wrong syscall number, because in the svc-immediate OABI syscall numbers are all offset by the ARM_SYSCALL_BASE value and so the correct insn is SVC 0x9f0002. (This is handled further down in the code with the other Arm-specific syscalls like NR_breakpoint.)
When this code was initially added in commit 6f1f31c069b20611 in 2004, ARM_NR_cacheflush was defined as (ARM_SYSCALL_BASE + 0xf0000 + 2) so the value in the comparison took account of the extra 0x900000 offset. In commit fbb4a2e371f2fa7 in 2008, the ARM_SYSCALL_BASE was removed from the definition of ARM_NR_cacheflush and handling for this group of syscalls was added below the point where we subtract ARM_SYSCALL_BASE from the SVC immediate value. However that commit forgot to remove the now-obsolete earlier handling code.
Remove the spurious ARM_NR_cacheflush condition.
--- linux-user/arm/cpu_loop.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
linux-user/arm/cpu_loop.c +1 -3
@@ -308,9 +308,7 @@ void cpu_loop(CPUARMState *env) n = insn &0xffffff ; } -if (n == ARM_NR_cacheflush) { -/* nop */ - }else if (n ==0 || n >=ARM_SYSCALL_BASE || env->thumb) { +if (n ==0 || n >=ARM_SYSCALL_BASE || env->thumb) {/* linux syscall */ if (env->thumb || n ==0 ) { n = env->regs[7 ];
Signature · 1 line
2.20.1
Re: [PATCH 2/4] linux-user/arm: Remove bogus SVC 0xf0002 handling
Quoted 7 lines · Peter Maydell
We incorrectly treat SVC
0xf0002as a cacheflush request (which is a NOP for QEMU). This is the wrong syscall number, because in the svc-immediate OABI syscall numbers are all offset by theARM_SYSCALL_BASEvalue and so the correct insn is SVC0x9f0002. (This is handled further down in the code with the other Arm-specific syscalls likeNR_breakpoint.)
Quoted 8 lines
When this code was initially added in commit
6f1f31c069b20611in 2004,ARM_NR_cacheflushwas defined as (ARM_SYSCALL_BASE+0xf0000+ 2) so the value in the comparison took account of the extra0x900000offset. In commitfbb4a2e371f2fa7in 2008, theARM_SYSCALL_BASEwas removed from the definition ofARM_NR_cacheflushand handling for this group of syscalls was added below the point where we subtractARM_SYSCALL_BASEfrom the SVC immediate value. However that commit forgot to remove the now-obsolete earlier handling code.
I imagine you wrote this patch wearing an archeologist hat =)
Quoted 24 lines
Remove the spurious
ARM_NR_cacheflushcondition.Signed-off-by Peter Maydell <[email protected]>--- linux-user/arm/cpu_loop.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)linux-user/arm/cpu_loop.c +1
-3@@ -308,9 +308,7 @@ void cpu_loop(CPUARMState *env) n = insn &0xffffff ; } -if (n == ARM_NR_cacheflush) { -/* nop */ - }else if (n ==0 || n >=ARM_SYSCALL_BASE || env->thumb) { +if (n ==0 || n >=ARM_SYSCALL_BASE || env->thumb) {/* linux syscall */ if (env->thumb || n ==0 ) { n = env->regs[7 ];
Re: [PATCH 2/4] linux-user/arm: Remove bogus SVC 0xf0002 handling
In reply to Peter MaydellQuoted 16 lines · Peter Maydell
We incorrectly treat SVC
0xf0002as a cacheflush request (which is a NOP for QEMU). This is the wrong syscall number, because in the svc-immediate OABI syscall numbers are all offset by theARM_SYSCALL_BASEvalue and so the correct insn is SVC0x9f0002. (This is handled further down in the code with the other Arm-specific syscalls likeNR_breakpoint.)When this code was initially added in commit
6f1f31c069b20611in 2004,ARM_NR_cacheflushwas defined as (ARM_SYSCALL_BASE+0xf0000+ 2) so the value in the comparison took account of the extra0x900000offset. In commitfbb4a2e371f2fa7in 2008, theARM_SYSCALL_BASEwas removed from the definition ofARM_NR_cacheflushand handling for this group of syscalls was added below the point where we subtractARM_SYSCALL_BASEfrom the SVC immediate value. However that commit forgot to remove the now-obsolete earlier handling code.
Quoted 1 line
Remove the spurious
ARM_NR_cacheflushcondition.
Quoted 25 lines
Signed-off-by Peter Maydell <[email protected]>--- linux-user/arm/cpu_loop.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)linux-user/arm/cpu_loop.c +1
-3@@ -308,9 +308,7 @@ void cpu_loop(CPUARMState *env) n = insn &0xffffff ; } -if (n == ARM_NR_cacheflush) { -/* nop */ - }else if (n ==0 || n >=ARM_SYSCALL_BASE || env->thumb) { +if (n ==0 || n >=ARM_SYSCALL_BASE || env->thumb) {/* linux syscall */ if (env->thumb || n ==0 ) { n = env->regs[7 ];Signature · 1 line
2.20.1
PATCH 3/4 linux-user/arm: Handle invalid arm-specific syscalls correctly
In reply to Peter MaydellThe kernel has different handling for syscalls with invalid numbers that are in the "arm-specific" range 0x9f0000 and up:
* 0x9f0000..0x9f07ff return -ENOSYS if not implemented
* other out of range syscalls cause a SIGILL
(see the kernel's arch/arm/kernel/traps.c:arm_syscall())
Implement this distinction. (Note that our code doesn't look quite like the kernel's, because we have removed the 0x900000 prefix by this point, whereas the kernel retains it in arm_syscall().)
--- linux-user/arm/cpu_loop.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)
linux-user/arm/cpu_loop.c +26 -4
@@ -332,10 +332,32 @@ void cpu_loop(CPUARMState *env) env->regs[0 ] =cpu_get_tls (env);break ;default : -qemu_log_mask (LOG_UNIMP , -"qemu: Unsupported ARM syscall: 0x%x\n" , - n); - env->regs[0 ] = -TARGET_ENOSYS ; +if (n <0xf0800 ) { +/* +* Syscalls 0xf0000..0xf07ff (or 0x9f0000.. +* 0x9f07ff in OABI numbering) are defined +* to return -ENOSYS rather than raising +* SIGILL. Note that we have already +* removed the 0x900000 prefix. +*/ +qemu_log_mask (LOG_UNIMP , +"qemu: Unsupported ARM syscall: 0x%x\n" , + n); + env->regs[0 ] = -TARGET_ENOSYS ; + }else { +/* Otherwise SIGILL */ + info.si_signo =TARGET_SIGILL ; + info.si_errno =0 ; + info.si_code =TARGET_ILL_ILLTRP ; + info._sifields._sigfault._addr = env->regs[15 ]; +if (env->thumb) { + info._sifields._sigfault._addr -=2 ; + }else { + info._sifields._sigfault._addr -=2 ; + } +queue_signal (env, info.si_signo, +QEMU_SI_FAULT , &info); + }break ; } }else {
Signature · 1 line
2.20.1
Re: [PATCH 3/4] linux-user/arm: Handle invalid arm-specific syscalls correctly
Quoted 48 lines · Peter Maydell
The kernel has different handling for syscalls with invalid numbers that are in the "arm-specific" range
0x9f0000and up:
* 0x9f0000..0x9f07ff return-ENOSYSif not implemented
* other out of range syscalls cause a SIGILL
(see the kernel's arch/arm/kernel/traps.c:arm_syscall())Implement this distinction. (Note that our code doesn't look quite like the kernel's, because we have removed the
0x900000prefix by this point, whereas the kernel retains it inarm_syscall().)Signed-off-by Peter Maydell <[email protected]>--- linux-user/arm/cpu_loop.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)linux-user/arm/cpu_loop.c +19
-4@@ -332,10 +332,32 @@ void cpu_loop(CPUARMState *env) env->regs[0 ] =cpu_get_tls (env);break ;default : -qemu_log_mask (LOG_UNIMP , -"qemu: Unsupported ARM syscall: 0x%x\n" , - n); - env->regs[0 ] = -TARGET_ENOSYS ; +if (n <0xf0800 ) { +/* +* Syscalls 0xf0000..0xf07ff (or 0x9f0000.. +* 0x9f07ff in OABI numbering) are defined +* to return -ENOSYS rather than raising +* SIGILL. Note that we have already +* removed the 0x900000 prefix. +*/ +qemu_log_mask (LOG_UNIMP , +"qemu: Unsupported ARM syscall: 0x%x\n" , + n); + env->regs[0 ] = -TARGET_ENOSYS ; + }else { +/* Otherwise SIGILL */ + info.si_signo =TARGET_SIGILL ; + info.si_errno =0 ; + info.si_code =TARGET_ILL_ILLTRP ; + info._sifields._sigfault._addr = env->regs[15 ]; +if (env->thumb) {
linux-user/arm/cpu_loop.c void cpu_loop(CPUARMState *env) · line 363
Quoted 10 lines
linux-user/arm/cpu_loop.c +7
-0+ info._sifields._sigfault._addr -=2 ; + }else { + info._sifields._sigfault._addr -=2 ; + } +queue_signal (env, info.si_signo, +QEMU_SI_FAULT , &info); + }break ; } }else {
Re: [PATCH 3/4] linux-user/arm: Handle invalid arm-specific syscalls correctly
In reply to Peter MaydellQuoted 41 lines · Peter Maydell
The kernel has different handling for syscalls with invalid numbers that are in the "arm-specific" range
0x9f0000and up:
* 0x9f0000..0x9f07ff return-ENOSYSif not implemented
* other out of range syscalls cause a SIGILL
(see the kernel's arch/arm/kernel/traps.c:arm_syscall())Implement this distinction. (Note that our code doesn't look quite like the kernel's, because we have removed the
0x900000prefix by this point, whereas the kernel retains it inarm_syscall().)Signed-off-by Peter Maydell <[email protected]>--- linux-user/arm/cpu_loop.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)linux-user/arm/cpu_loop.c +13
-4@@ -332,10 +332,32 @@ void cpu_loop(CPUARMState *env) env->regs[0 ] =cpu_get_tls (env);break ;default : -qemu_log_mask (LOG_UNIMP , -"qemu: Unsupported ARM syscall: 0x%x\n" , - n); - env->regs[0 ] = -TARGET_ENOSYS ; +if (n <0xf0800 ) { +/* +* Syscalls 0xf0000..0xf07ff (or 0x9f0000.. +* 0x9f07ff in OABI numbering) are defined +* to return -ENOSYS rather than raising +* SIGILL. Note that we have already +* removed the 0x900000 prefix. +*/ +qemu_log_mask (LOG_UNIMP , +"qemu: Unsupported ARM syscall: 0x%x\n" , + n); + env->regs[0 ] = -TARGET_ENOSYS ; + }else {
linux-user/arm/cpu_loop.c void cpu_loop(CPUARMState *env) · line 357
Quoted 10 lines
linux-user/arm/cpu_loop.c +10
-0+/* Otherwise SIGILL */ + info.si_signo =TARGET_SIGILL ; + info.si_errno =0 ; + info.si_code =TARGET_ILL_ILLTRP ; + info._sifields._sigfault._addr = env->regs[15 ]; +if (env->thumb) { + info._sifields._sigfault._addr -=2 ; + }else { + info._sifields._sigfault._addr -=2 ; + }
Am I missing some detail or are both branches of the if-else doing the same thing?
Cheers,
Edgar
Quoted 10 lines
linux-user/arm/cpu_loop.c +3
-0+queue_signal (env, info.si_signo, +QEMU_SI_FAULT , &info); + }break ; } }else {Signature · 1 line
2.20.1
Re: [PATCH 3/4] linux-user/arm: Handle invalid arm-specific syscalls correctly
Quoted 48 lines · Edgar E. Iglesias
Quoted 47 lines · Peter Maydell
The kernel has different handling for syscalls with invalid numbers that are in the "arm-specific" range
0x9f0000and up:
* 0x9f0000..0x9f07ff return-ENOSYSif not implemented
* other out of range syscalls cause a SIGILL
(see the kernel's arch/arm/kernel/traps.c:arm_syscall())Implement this distinction. (Note that our code doesn't look quite like the kernel's, because we have removed the
0x900000prefix by this point, whereas the kernel retains it inarm_syscall().)Signed-off-by Peter Maydell <[email protected]>--- linux-user/arm/cpu_loop.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)linux-user/arm/cpu_loop.c +19
-4@@ -332,10 +332,32 @@ void cpu_loop(CPUARMState *env) env->regs[0 ] =cpu_get_tls (env);break ;default : -qemu_log_mask (LOG_UNIMP , -"qemu: Unsupported ARM syscall: 0x%x\n" , - n); - env->regs[0 ] = -TARGET_ENOSYS ; +if (n <0xf0800 ) { +/* +* Syscalls 0xf0000..0xf07ff (or 0x9f0000.. +* 0x9f07ff in OABI numbering) are defined +* to return -ENOSYS rather than raising +* SIGILL. Note that we have already +* removed the 0x900000 prefix. +*/ +qemu_log_mask (LOG_UNIMP , +"qemu: Unsupported ARM syscall: 0x%x\n" , + n); + env->regs[0 ] = -TARGET_ENOSYS ; + }else { +/* Otherwise SIGILL */ + info.si_signo =TARGET_SIGILL ; + info.si_errno =0 ; + info.si_code =TARGET_ILL_ILLTRP ; + info._sifields._sigfault._addr = env->regs[15 ]; +if (env->thumb) {
Quoted 8 lines
linux-user/arm/cpu_loop.c void cpu_loop(CPUARMState *env) · line 357
Quoted 4 lines
linux-user/arm/cpu_loop.c +4
-0+ info._sifields._sigfault._addr -=2 ; + }else { + info._sifields._sigfault._addr -=2 ; + }Am I missing some detail or are both branches of the if-else doing the same thing?
Oops good catch. R-b stands using '-= 4' on 2nd line.
Quoted 17 lines
Cheers,
EdgarQuoted 10 lines
linux-user/arm/cpu_loop.c +3
-0+queue_signal (env, info.si_signo, +QEMU_SI_FAULT , &info); + }break ; } }else {Signature · 1 line
2.20.1
Re: [PATCH 3/4] linux-user/arm: Handle invalid arm-specific syscalls correctly
In reply to Edgar E. IglesiasQuoted 11 lines · Edgar E. Iglesias
linux-user/arm/cpu_loop.c void cpu_loop(CPUARMState *env) · line 357
Quoted 5 lines · Peter Maydell
linux-user/arm/cpu_loop.c +5
-0+if (env->thumb) { + info._sifields._sigfault._addr -=2 ; + }else { + info._sifields._sigfault._addr -=2 ; + }Am I missing some detail or are both branches of the if-else doing the same thing?
Oops, yes: cut-n-paste error; as Philippe says, the not-thumb branch should be "-= 4".
thanks
-- PMM
Re: [PATCH 3/4] linux-user/arm: Handle invalid arm-specific syscalls correctly
In reply to Peter Maydellпон, 20. апр 2020. у 23:25 Peter Maydell <[email protected]> је написао/ла:
Quoted 7 lines
The kernel has different handling for syscalls with invalid numbers that are in the "arm-specific" range
0x9f0000and up:
* 0x9f0000..0x9f07ff return-ENOSYSif not implemented
* other out of range syscalls cause a SIGILL
(see the kernel's arch/arm/kernel/traps.c:arm_syscall())
Quoted 5 lines
Implement this distinction. (Note that our code doesn't look quite like the kernel's, because we have removed the
0x900000prefix by this point, whereas the kernel retains it inarm_syscall().)
Hmm, I suspect other targets could have a similar problem.
I am definitely going to take a look at the mips target, but did you Peter have a chance to take a more global look whether this problem is actually widespread?
Regards,
Aleksandar
Quoted 50 lines
Signed-off-by Peter Maydell <[email protected]>--- linux-user/arm/cpu_loop.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)linux-user/arm/cpu_loop.c +26
-5@@ -332,10 +332,32 @@ void cpu_loop(CPUARMState *env) env->regs[0 ] =cpu_get_tls (env);break ;default : -qemu_log_mask (LOG_UNIMP , -"qemu: Unsupported ARM syscall: 0x%x\n" , - n); - env->regs[0 ] = -TARGET_ENOSYS ; +if (n <0xf0800 ) { +/* +* Syscalls 0xf0000..0xf07ff (or 0x9f0000.. +* 0x9f07ff in OABI numbering) are defined +* to return -ENOSYS rather than raising +* SIGILL. Note that we have already +* removed the 0x900000 prefix. +*/ +qemu_log_mask (LOG_UNIMP , +"qemu: Unsupported ARM syscall: 0x%x\n" , + n); + env->regs[0 ] = -TARGET_ENOSYS ; + }else { +/* Otherwise SIGILL */ + info.si_signo =TARGET_SIGILL ; + info.si_errno =0 ; + info.si_code =TARGET_ILL_ILLTRP ; + info._sifields._sigfault._addr = env->regs[15 ]; +if (env->thumb) { + info._sifields._sigfault._addr -=2 ; + }else { + info._sifields._sigfault._addr -=2 ; + } +queue_signal (env, info.si_signo, +QEMU_SI_FAULT , &info); + }break ; } }else { --2.20.1
Re: [PATCH 3/4] linux-user/arm: Handle invalid arm-specific syscalls correctly
Quoted 16 lines · Aleksandar Markovic
пон, 20. апр 2020. у 23:25 Peter Maydell <[email protected]> је написао/ла:
Quoted 7 lines
The kernel has different handling for syscalls with invalid numbers that are in the "arm-specific" range
0x9f0000and up:
* 0x9f0000..0x9f07ff return-ENOSYSif not implemented
* other out of range syscalls cause a SIGILL
(see the kernel's arch/arm/kernel/traps.c:arm_syscall())Quoted 5 lines
Implement this distinction. (Note that our code doesn't look quite like the kernel's, because we have removed the
0x900000prefix by this point, whereas the kernel retains it inarm_syscall().)
Quoted 5 lines
Hmm, I suspect other targets could have a similar problem.
I am definitely going to take a look at the mips target, but did you Peter have a chance to take a more global look whether this problem is actually widespread?
My guess is that this is Arm-specific, because both the OABI-vs-EABI "do we pass the syscall number in the insn immediate field or via a register" changeover and also the oddball "arm-specific handful of syscalls in a distinct range" are Arm hacks, not something the kernel deals with in generic code.
thanks
-- PMM
PATCH 4/4 linux-user/arm: Fix identification of syscall numbers
In reply to Peter MaydellOur code to identify syscall numbers has some issues:
* for Thumb mode, we never need the immediate value from the insn, but we always read it anyway
* bad immediate values in the svc insn should cause a SIGILL, but we were abort()ing instead (via "goto error")
We can fix both these things by refactoring the code that identifies the syscall number to more closely follow the kernel COMPAT_OABI code:
* for Thumb it is always r7
* for Arm, if the immediate value is 0, then this is an EABI call with the syscall number in r7
* otherwise, we XOR the immediate value with 0x900000 (ARM_SYSCALL_BASE for QEMU; __NR_OABI_SYSCALL_BASE in the kernel), which converts valid syscall immediates into the desired value, and puts all invalid immediates in the range 0x100000 or above
* then we can just let the existing "value too large, deliver SIGILL" case handle invalid numbers, and drop the 'goto error'
---
You might prefer to read this patch with an "ignore whitespace changes" diff, as a big chunk of code is no longer inside an if() and got re-indented out one level.
--- linux-user/arm/cpu_loop.c | 143 ++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 66 deletions(-)
linux-user/arm/cpu_loop.c +77 -66
@@ -299,85 +299,96 @@ void cpu_loop(CPUARMState *env) env->eabi =1 ;/* system call */ if (env->thumb) { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u16 (insn, env->regs[15 ] -2 , env); - n = insn &0xff ; +/* Thumb is always EABI style with syscall number in r7 */ + n = env->regs[7 ]; }else { +/* +* 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 || n >=ARM_SYSCALL_BASE || env->thumb) { -/* linux syscall */ -if (env->thumb || n ==0 ) { +if (n ==0 ) { +/* zero immediate: EABI, syscall number in r7 */ n = env->regs[7 ]; }else { - n -=ARM_SYSCALL_BASE ; +/* +* This XOR matches the kernel code: an immediate +* in the valid range (0x900000 .. 0x9fffff) is +* converted into the correct EABI-style syscall +* number; invalid immediates end up as values +* > 0xfffff and are handled below as out-of-range. +*/ + n ^=ARM_SYSCALL_BASE ; env->eabi =0 ; } -if ( n >ARM_NR_BASE ) { -switch (n) { -case ARM_NR_cacheflush: -/* nop */ -break ; -case ARM_NR_set_tls: -cpu_set_tls (env, env->regs[0 ]); - env->regs[0 ] =0 ; -break ; -case ARM_NR_breakpoint: - env->regs[15 ] -= env->thumb ?2 :4 ; - goto excp_debug; -case ARM_NR_get_tls: - env->regs[0 ] =cpu_get_tls (env); -break ; -default : -if (n <0xf0800 ) { -/* -* Syscalls 0xf0000..0xf07ff (or 0x9f0000.. -* 0x9f07ff in OABI numbering) are defined -* to return -ENOSYS rather than raising -* SIGILL. Note that we have already -* removed the 0x900000 prefix. -*/ -qemu_log_mask (LOG_UNIMP , -"qemu: Unsupported ARM syscall: 0x%x\n" , - n); - env->regs[0 ] = -TARGET_ENOSYS ; + } + +if (n >ARM_NR_BASE ) { +switch (n) { +case ARM_NR_cacheflush: +/* nop */ +break ; +case ARM_NR_set_tls: +cpu_set_tls (env, env->regs[0 ]); + env->regs[0 ] =0 ; +break ; +case ARM_NR_breakpoint: + env->regs[15 ] -= env->thumb ?2 :4 ; + goto excp_debug; +case ARM_NR_get_tls: + env->regs[0 ] =cpu_get_tls (env); +break ; +default : +if (n <0xf0800 ) { +/* +* Syscalls 0xf0000..0xf07ff (or 0x9f0000.. +* 0x9f07ff in OABI numbering) are defined +* to return -ENOSYS rather than raising +* SIGILL. Note that we have already +* removed the 0x900000 prefix. +*/ +qemu_log_mask (LOG_UNIMP , +"qemu: Unsupported ARM syscall: 0x%x\n" , + n); + env->regs[0 ] = -TARGET_ENOSYS ; + }else { +/* +* Otherwise SIGILL. This includes any SWI with +* immediate not originally 0x9fxxxx, because +* of the earlier XOR. +*/ + info.si_signo =TARGET_SIGILL ; + info.si_errno =0 ; + info.si_code =TARGET_ILL_ILLTRP ; + info._sifields._sigfault._addr = env->regs[15 ]; +if (env->thumb) { + info._sifields._sigfault._addr -=2 ; }else { -/* Otherwise SIGILL */ - info.si_signo =TARGET_SIGILL ; - info.si_errno =0 ; - info.si_code =TARGET_ILL_ILLTRP ; - info._sifields._sigfault._addr = env->regs[15 ]; -if (env->thumb) { - info._sifields._sigfault._addr -=2 ; - }else { - info._sifields._sigfault._addr -=2 ; - } -queue_signal (env, info.si_signo, -QEMU_SI_FAULT , &info); + info._sifields._sigfault._addr -=2 ; } -break ; - } - }else { - ret =do_syscall (env, - n, - env->regs[0 ], - env->regs[1 ], - env->regs[2 ], - env->regs[3 ], - env->regs[4 ], - env->regs[5 ], -0 ,0 ); -if (ret == -TARGET_ERESTARTSYS ) { - env->regs[15 ] -= env->thumb ?2 :4 ; - }else if (ret != -TARGET_QEMU_ESIGRETURN ) { - env->regs[0 ] = ret; +queue_signal (env, info.si_signo, +QEMU_SI_FAULT , &info); } +break ; } }else { - goto error; + ret =do_syscall (env, + n, + env->regs[0 ], + env->regs[1 ], + env->regs[2 ], + env->regs[3 ], + env->regs[4 ], + env->regs[5 ], +0 ,0 ); +if (ret == -TARGET_ERESTARTSYS ) { + env->regs[15 ] -= env->thumb ?2 :4 ; + }else if (ret != -TARGET_QEMU_ESIGRETURN ) { + env->regs[0 ] = ret; + } } }break ;
Signature · 1 line
2.20.1
Re: [PATCH 4/4] linux-user/arm: Fix identification of syscall numbers
Quoted 6 lines · Peter Maydell
Our code to identify syscall numbers has some issues:
* for Thumb mode, we never need the immediate value from the insn, but we always read it anyway
* bad immediate values in the svc insn should cause a SIGILL, but we were abort()ing instead (via "goto error")
Quoted 11 lines
We can fix both these things by refactoring the code that identifies the syscall number to more closely follow the kernel
COMPAT_OABIcode:
* for Thumb it is always r7
* for Arm, if the immediate value is 0, then this is an EABI call with the syscall number in r7
* otherwise, we XOR the immediate value with0x900000(ARM_SYSCALL_BASEfor QEMU;__NR_OABI_SYSCALL_BASEin the kernel), which converts valid syscall immediates into the desired value, and puts all invalid immediates in the range0x100000or above
* then we can just let the existing "value too large, deliver SIGILL" case handle invalid numbers, and drop the 'goto error'
I guess the -2 vs -4 issue has propagated into this patch but with that fixed:
Quoted 181 lines
Signed-off-by Peter Maydell <[email protected]>---
You might prefer to read this patch with an "ignore whitespace changes" diff, as a big chunk of code is no longer inside anif()and got re-indented out one level.--- linux-user/arm/cpu_loop.c | 143 ++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 66 deletions(-)linux-user/arm/cpu_loop.c +77
-66@@ -299,85 +299,96 @@ void cpu_loop(CPUARMState *env) env->eabi =1 ;/* system call */ if (env->thumb) { -/* FIXME - what to do if get_user() fails? */ -get_user_code_u16 (insn, env->regs[15 ] -2 , env); - n = insn &0xff ; +/* Thumb is always EABI style with syscall number in r7 */ + n = env->regs[7 ]; }else { +/* +* 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 || n >=ARM_SYSCALL_BASE || env->thumb) { -/* linux syscall */ -if (env->thumb || n ==0 ) { +if (n ==0 ) { +/* zero immediate: EABI, syscall number in r7 */ n = env->regs[7 ]; }else { - n -=ARM_SYSCALL_BASE ; +/* +* This XOR matches the kernel code: an immediate +* in the valid range (0x900000 .. 0x9fffff) is +* converted into the correct EABI-style syscall +* number; invalid immediates end up as values +* > 0xfffff and are handled below as out-of-range. +*/ + n ^=ARM_SYSCALL_BASE ; env->eabi =0 ; } -if ( n >ARM_NR_BASE ) { -switch (n) { -case ARM_NR_cacheflush: -/* nop */ -break ; -case ARM_NR_set_tls: -cpu_set_tls (env, env->regs[0 ]); - env->regs[0 ] =0 ; -break ; -case ARM_NR_breakpoint: - env->regs[15 ] -= env->thumb ?2 :4 ; - goto excp_debug; -case ARM_NR_get_tls: - env->regs[0 ] =cpu_get_tls (env); -break ; -default : -if (n <0xf0800 ) { -/* -* Syscalls 0xf0000..0xf07ff (or 0x9f0000.. -* 0x9f07ff in OABI numbering) are defined -* to return -ENOSYS rather than raising -* SIGILL. Note that we have already -* removed the 0x900000 prefix. -*/ -qemu_log_mask (LOG_UNIMP , -"qemu: Unsupported ARM syscall: 0x%x\n" , - n); - env->regs[0 ] = -TARGET_ENOSYS ; + } + +if (n >ARM_NR_BASE ) { +switch (n) { +case ARM_NR_cacheflush: +/* nop */ +break ; +case ARM_NR_set_tls: +cpu_set_tls (env, env->regs[0 ]); + env->regs[0 ] =0 ; +break ; +case ARM_NR_breakpoint: + env->regs[15 ] -= env->thumb ?2 :4 ; + goto excp_debug; +case ARM_NR_get_tls: + env->regs[0 ] =cpu_get_tls (env); +break ; +default : +if (n <0xf0800 ) { +/* +* Syscalls 0xf0000..0xf07ff (or 0x9f0000.. +* 0x9f07ff in OABI numbering) are defined +* to return -ENOSYS rather than raising +* SIGILL. Note that we have already +* removed the 0x900000 prefix. +*/ +qemu_log_mask (LOG_UNIMP , +"qemu: Unsupported ARM syscall: 0x%x\n" , + n); + env->regs[0 ] = -TARGET_ENOSYS ; + }else { +/* +* Otherwise SIGILL. This includes any SWI with +* immediate not originally 0x9fxxxx, because +* of the earlier XOR. +*/ + info.si_signo =TARGET_SIGILL ; + info.si_errno =0 ; + info.si_code =TARGET_ILL_ILLTRP ; + info._sifields._sigfault._addr = env->regs[15 ]; +if (env->thumb) { + info._sifields._sigfault._addr -=2 ; }else { -/* Otherwise SIGILL */ - info.si_signo =TARGET_SIGILL ; - info.si_errno =0 ; - info.si_code =TARGET_ILL_ILLTRP ; - info._sifields._sigfault._addr = env->regs[15 ]; -if (env->thumb) { - info._sifields._sigfault._addr -=2 ; - }else { - info._sifields._sigfault._addr -=2 ; - } -queue_signal (env, info.si_signo, -QEMU_SI_FAULT , &info); + info._sifields._sigfault._addr -=2 ; } -break ; - } - }else { - ret =do_syscall (env, - n, - env->regs[0 ], - env->regs[1 ], - env->regs[2 ], - env->regs[3 ], - env->regs[4 ], - env->regs[5 ], -0 ,0 ); -if (ret == -TARGET_ERESTARTSYS ) { - env->regs[15 ] -= env->thumb ?2 :4 ; - }else if (ret != -TARGET_QEMU_ESIGRETURN ) { - env->regs[0 ] = ret; +queue_signal (env, info.si_signo, +QEMU_SI_FAULT , &info); } +break ; } }else { - goto error; + ret =do_syscall (env, + n, + env->regs[0 ], + env->regs[1 ], + env->regs[2 ], + env->regs[3 ], + env->regs[4 ], + env->regs[5 ], +0 ,0 ); +if (ret == -TARGET_ERESTARTSYS ) { + env->regs[15 ] -= env->thumb ?2 :4 ; + }else if (ret != -TARGET_QEMU_ESIGRETURN ) { + env->regs[0 ] = ret; + } } }break ;Signature · 1 line
2.20.1
Quoted 13 lines · Peter Maydell
This patchseries fixes issues with the code in
linux-user/arm/cpu_loop.c:
* it incorrectly thinks BKPT is a syscall instruction
(https://bugs.launchpad.net/qemu/+bug/1873898, reported via irc)
* a stale line of code means we incorrectly NOP SVC #0xf0002
* we don't implement the distinction between 0x9f0000..0x9f07ff (should return-ENOSYSif not implemented) and higher numbers (should cause a SIGILL)
* weabort()for bad immediate values to SVC (ie not the 0 of EABI or the >0x9f0000 of OABI); the kernel delivers a SIGILL for these
* for Thumb mode, we never use the immediate value from the insn, but we always read it anyway
Quoted 3 lines
This patchseries fixes all those things. (I started out fixing the BKPT bug; everything else is problems I spotted along the way while I was reading this bit of code...)
Laurent, do you want me to post a v2 with the -2/-4 thinko fixed so you can put it via the linux-user tree, or should I just take this via target-arm.next?
thanks
-- PMM
Peter Maydell
AppliedQuoted 15 lines · Peter Maydell
Quoted 13 lines · Peter Maydell
This patchseries fixes issues with the code in
linux-user/arm/cpu_loop.c:
* it incorrectly thinks BKPT is a syscall instruction
(https://bugs.launchpad.net/qemu/+bug/1873898, reported via irc)
* a stale line of code means we incorrectly NOP SVC #0xf0002
* we don't implement the distinction between 0x9f0000..0x9f07ff (should return-ENOSYSif not implemented) and higher numbers (should cause a SIGILL)
* weabort()for bad immediate values to SVC (ie not the 0 of EABI or the >0x9f0000 of OABI); the kernel delivers a SIGILL for these
* for Thumb mode, we never use the immediate value from the insn, but we always read it anyway
Quoted 7 lines
Quoted 3 lines
This patchseries fixes all those things. (I started out fixing the BKPT bug; everything else is problems I spotted along the way while I was reading this bit of code...)
Laurent, do you want me to post a v2 with the -2/-4 thinko fixed so you can put it via the linux-user tree, or should I just take this via target-arm.next?
I've applied this series (with the fixup) to target-arm.next; thanks.
-- PMM