semihosting: Improve ARMv7A support
PATCH 1/1 semihosting: Improve ARMv7A support
Previous implementation of semihosting didn't account for cases where doing an SVC call would clobber various data(most notable case would be 'lr' and 'spsr' when doing SVC call in Supervisor mode). This patch is an adaptation of the code from Newlib's Angel_SWI feature (can be found in libc/sys/arm/swi.h) which hopefuly fixes the problem.
Tested with modified OpenOCD and custom Vybrid VF610 based board
--- arch/arm/lib/semihosting.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)
arch/arm/lib/semihosting.c +26 -4
@@ -26,18 +26,40 @@/* * Call the handler */ +#if defined(CONFIG_ARM64 )static noinline long smh_trap (unsigned int sysnum,void *addr) { registerlong result asm("r0" ); -#if defined(CONFIG_ARM64 ) asmvolatile ("hlt #0xf000" :"=r" (result) :"0" (sysnum),"r" (addr)); +return result; +}#else -/* Note - untested placeholder */ - asmvolatile ("svc #0x123456" :"=r" (result) :"0" (sysnum),"r" (addr)); +#if defined (CONFIG_SYS_THUMB_BUILD ) +#error "Support for semihosting in THUMB mode is not implemented"#endif +/* +* Call the handler +*/ +static noinline long smh_trap (unsigned int sysnum,void *addr) +{ +int result; + + asmvolatile ("mov r0, %1" "\n\t" +"mov r1, %2" "\n\t" +"svc #0x123456" "\n\t" +"mov %0, r0" "\n\t" + :"=r" (result) + :"r" (sysnum),"r" (addr) + :"r0" ,"r1" ,"r2" ,"r3" ,"ip" ,"lr" ,"memory" ,"cc" ); +/* Clobbers r0 and r1, and lr if in supervisor +mode Accordingly to page 13-77 of ARM DUI +0040D other registers can also be clobbered. +Some memory positions may also be changed by +a system call, so they should not be kept in +registers. */ return result; } - +#endif /* * Open a file on the host. Mode is "r" or "rb" currently. Returns a file * descriptor or -1 on error.
--
2.1.4
PATCH 1/1 semihosting: Improve ARMv7A support
Hello Andrey,
Two nitpicks:
Quoted 18 lines · Andrey Smirnov
Previous implementation of semihosting didn't account for cases where doing an SVC call would clobber various data(most notable case would be 'lr' and 'spsr' when doing SVC call in Supervisor mode). This patch is an adaptation of the code from Newlib's
Angel_SWIfeature (can be found inlibc/sys/arm/swi.h) which hopefuly fixes the problem.Tested with modified OpenOCD and custom Vybrid VF610 based board
Signed-off-by Andrey Smirnov <[email protected]>--- arch/arm/lib/semihosting.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)arch/arm/lib/semihosting.c +0
-0
arch/arm/lib/semihosting.c line 32
Quoted 8 lines
arch/arm/lib/semihosting.c +1
-1@@ -26,18 +26,40 @@/* * Call the handler */ +#if defined(CONFIG_ARM64 )static noinline long smh_trap (unsigned int sysnum,void *addr) { registerlong result asm("r0" ); -#if defined(CONFIG_ARM64 )
What's the point of moving the #ifdef up? It just forces you to duplicate the function start with no discernable benefit since the function name and arguments are the same in the 64 and 32 bit case.
arch/arm/lib/semihosting.c line 42
Quoted 12 lines
arch/arm/lib/semihosting.c +7
-2asmvolatile ("hlt #0xf000" :"=r" (result) :"0" (sysnum),"r" (addr)); + return result; +}#else -/* Note - untested placeholder */ - asmvolatile ("svc #0x123456" :"=r" (result) :"0" (sysnum),"r" (addr)); +#if defined (CONFIG_SYS_THUMB_BUILD ) +#error "Support for semihosting in THUMB mode is not implemented"#endif +/* +* Call the handler +*/
No point in duplicating this comment.
Quoted 25 lines
arch/arm/lib/semihosting.c +18
-2+static noinline long smh_trap (unsigned int sysnum,void *addr) +{ +int result; + + asmvolatile ("mov r0, %1" "\n\t" +"mov r1, %2" "\n\t" +"svc #0x123456" "\n\t" +"mov %0, r0" "\n\t" + :"=r" (result) + :"r" (sysnum),"r" (addr) + :"r0" ,"r1" ,"r2" ,"r3" ,"ip" ,"lr" ,"memory" ,"cc" ); +/* Clobbers r0 and r1, and lr if in supervisor +mode Accordingly to page 13-77 of ARM DUI +0040D other registers can also be clobbered. +Some memory positions may also be changed by +a system call, so they should not be kept in +registers. */ return result; } - +#endif /* * Open a file on the host. Mode is "r" or "rb" currently. Returns a file * descriptor or -1 on error. --
Quoted 5 lines
2.1.4
_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot
Amicalement,
Signature · 1 line
Albert.
PATCH 1/1 semihosting: Improve ARMv7A support
Quoted 4 lines · Albert ARIBAUD
Hello Andrey,
Two nitpicks:
Quoted 10 lines
arch/arm/lib/semihosting.c line 32
Quoted 5 lines
arch/arm/lib/semihosting.c +1
-1+#if defined(CONFIG_ARM64 )static noinline long smh_trap (unsigned int sysnum,void *addr) { registerlong result asm("r0" ); -#if defined(CONFIG_ARM64 )What's the point of moving the #ifdef up? It just forces you to duplicate the function start with no discernable benefit since the function name and arguments are the same in the 64 and 32 bit case.
The point is to improve readability of the code. With #ifdef moved up you have to figure out the version of the code you care about first and then you can read the entirety of the function and reason about it. With #ifdefs inside of the body of the function, every time you read it you have to filter out all of the "irrelevant junk" that compiler would not even see on your platform.