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

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

commit e9f96105dd4204c3e7c7929a6a943b73915523dc
Author: Ville Juven <[email protected]>
AuthorDate: Wed Oct 2 11:39:03 2024 +0300

    risc-v/syscall: Simplify dispatch_syscall for RISC-V
    
    Port the simplification from ARM64, this removes the ugly inline assembly
    trampoline "do_syscall" and replaces it with a simple table lookup and
    call via function pointer.
---
 arch/risc-v/src/common/riscv_swint.c | 69 +++++-------------------------------
 1 file changed, 9 insertions(+), 60 deletions(-)

diff --git a/arch/risc-v/src/common/riscv_swint.c 
b/arch/risc-v/src/common/riscv_swint.c
index 91261169fb..ce2a2ad154 100644
--- a/arch/risc-v/src/common/riscv_swint.c
+++ b/arch/risc-v/src/common/riscv_swint.c
@@ -46,73 +46,17 @@
 #include "addrenv.h"
 
 /****************************************************************************
- * Pre-processor Definitions
+ * Private Types
  ****************************************************************************/
 
+typedef uintptr_t (*syscall_t)(unsigned int, ...);
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
 
 #ifdef CONFIG_LIB_SYSCALL
 
-/****************************************************************************
- * Name: do_syscall
- *
- * Description:
- *   Call the stub function corresponding to the system call.  NOTE the non-
- *   standard parameter passing:
- *
- *     A0 = SYS_ call number
- *     A1 = parm0
- *     A2 = parm1
- *     A3 = parm2
- *     A4 = parm3
- *     A5 = parm4
- *     A6 = parm5
- *
- * Note:
- *   Do not allow the compiler to inline this function, as it does a jump to
- *   another procedure which can clobber any register and the compiler will
- *   not understand it happens.
- *
- ****************************************************************************/
-
-static uintptr_t do_syscall(unsigned int nbr, uintptr_t parm1,
-                            uintptr_t parm2, uintptr_t parm3,
-                            uintptr_t parm4, uintptr_t parm5,
-                            uintptr_t parm6) noinline_function;
-static uintptr_t do_syscall(unsigned int nbr, uintptr_t parm1,
-                            uintptr_t parm2, uintptr_t parm3,
-                            uintptr_t parm4, uintptr_t parm5,
-                            uintptr_t parm6)
-{
-  register long a0 asm("a0") = (long)(nbr);
-  register long a1 asm("a1") = (long)(parm1);
-  register long a2 asm("a2") = (long)(parm2);
-  register long a3 asm("a3") = (long)(parm3);
-  register long a4 asm("a4") = (long)(parm4);
-  register long a5 asm("a5") = (long)(parm5);
-  register long a6 asm("a6") = (long)(parm6);
-
-  asm volatile
-    (
-     "la   t0, g_stublookup\n" /* t0=The base of the stub lookup table */
-#ifdef CONFIG_ARCH_RV32
-     "slli a0, a0, 2\n"        /* a0=Offset for the stub lookup table */
-#else
-     "slli a0, a0, 3\n"        /* a0=Offset for the stub lookup table */
-#endif
-     "add  t0, t0, a0\n"       /* t0=The address in the table */
-     REGLOAD " t0, 0(t0)\n"    /* t0=The address of the stub for this syscall 
*/
-     "jalr ra, t0\n"           /* Call the stub (modifies ra) */
-     : "+r"(a0)
-     : "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5), "r"(a6)
-     : "t0", "ra", "memory"
-  );
-
-  return a0;
-}
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -140,6 +84,7 @@ uintptr_t dispatch_syscall(unsigned int nbr, uintptr_t parm1,
                            uintptr_t parm4, uintptr_t parm5,
                            uintptr_t parm6, void *context)
 {
+  struct tcb_s *rtcb         = this_task();
   register long a0 asm("a0") = (long)(nbr);
   register long a1 asm("a1") = (long)(parm1);
   register long a2 asm("a2") = (long)(parm2);
@@ -147,7 +92,7 @@ uintptr_t dispatch_syscall(unsigned int nbr, uintptr_t parm1,
   register long a4 asm("a4") = (long)(parm4);
   register long a5 asm("a5") = (long)(parm5);
   register long a6 asm("a6") = (long)(parm6);
-  register struct tcb_s *rtcb asm("tp");
+  syscall_t do_syscall;
   uintptr_t ret;
 
   /* Valid system call ? */
@@ -171,6 +116,10 @@ uintptr_t dispatch_syscall(unsigned int nbr, uintptr_t 
parm1,
 
   a0 -= CONFIG_SYS_RESERVED;
 
+  /* Find the system call from the lookup table */
+
+  do_syscall = (syscall_t)g_stublookup[a0];
+
   /* Run the system call, save return value locally */
 
   ret = do_syscall(a0, a1, a2, a3, a4, a5, a6);

Reply via email to