You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/02/25 18:06:03 UTC

[incubator-nuttx] branch master updated: armv7-m: Fix syscall stack alignment

This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 9a8169a  armv7-m: Fix syscall stack alignment
9a8169a is described below

commit 9a8169acdf365ddb7eab92be597f59b9003498d9
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Wed Feb 26 02:25:55 2020 +0900

    armv7-m: Fix syscall stack alignment
    
    This fixes "df -h" with PROTECTED build.  Tested on qemu.
    
    Reference:
    	aapcs32 "6.2.1 The Stack/Stack constraints at a public interface"
---
 arch/arm/src/armv7-m/up_svcall.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/arch/arm/src/armv7-m/up_svcall.c b/arch/arm/src/armv7-m/up_svcall.c
index 0c1430b..079e293 100644
--- a/arch/arm/src/armv7-m/up_svcall.c
+++ b/arch/arm/src/armv7-m/up_svcall.c
@@ -97,16 +97,28 @@ static void dispatch_syscall(void)
 {
   __asm__ __volatile__
   (
-    " sub sp, sp, #16\n"           /* Create a stack frame to hold 3 parms + lr */
+    /* Create a stack frame to hold 3 parameters + LR and SP adjustment value.
+     * Also, Ensure 8 bytes alignment. We use IP as a scratch.
+     *
+     * NOTE: new_SP = (orig_SP - 20) & ~7
+     *              = orig_SP - 20 - ((orig_SP - 20) & ~7)
+     */
+    " mov ip, sp\n"                /* Calculate (orig_SP - new_SP) */
+    " sub ip, ip, #20\n"
+    " and ip, ip, #7\n"
+    " add ip, ip, #20\n"
+    " sub sp, sp, ip\n"
     " str r4, [sp, #0]\n"          /* Move parameter 4 (if any) into position */
     " str r5, [sp, #4]\n"          /* Move parameter 5 (if any) into position */
     " str r6, [sp, #8]\n"          /* Move parameter 6 (if any) into position */
     " str lr, [sp, #12]\n"         /* Save lr in the stack frame */
+    " str ip, [sp, #16]\n"         /* Save (orig_SP - new_SP) value */
     " ldr ip, =g_stublookup\n"     /* R12=The base of the stub lookup table */
     " ldr ip, [ip, r0, lsl #2]\n"  /* R12=The address of the stub for this syscall */
     " blx ip\n"                    /* Call the stub (modifies lr) */
     " ldr lr, [sp, #12]\n"         /* Restore lr */
-    " add sp, sp, #16\n"           /* Destroy the stack frame */
+    " ldr r2, [sp, #16]\n"         /* Restore (orig_SP - new_SP) value */
+    " add sp, sp, r2\n"            /* Restore SP */
     " mov r2, r0\n"                /* R2=Save return value in R2 */
     " mov r0, #3\n"                /* R0=SYS_syscall_return */
     " svc 0"                       /* Return from the syscall */