diff mbox series

[v5,09/14] tools/nolibc: add missing my_syscall6() for mips

Message ID 5089ddd75a7cb01486f664c025876c803fe26d5b.1687957589.git.falcon@tinylab.org
State New
Headers show
Series [v5,01/14] tools/nolibc: sys.h: add a syscall return helper | expand

Commit Message

Zhangjin Wu June 28, 2023, 1:37 p.m. UTC
It is able to pass the 6th argument like the 5th argument via the stack
for mips, let's add a new my_syscall6() now, see [1] for details:

  The mips/o32 system call convention passes arguments 5 through 8 on
  the user stack.

Both mmap() and pselect6() require my_syscall6().

[1]: https://man7.org/linux/man-pages/man2/syscall.2.html

Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
 tools/include/nolibc/arch-mips.h | 25 +++++++++++++++++++++++++
 tools/include/nolibc/nolibc.h    |  9 ++++-----
 2 files changed, 29 insertions(+), 5 deletions(-)

Comments

Willy Tarreau July 2, 2023, 6:55 p.m. UTC | #1
Hi Zhangjin,

On Wed, Jun 28, 2023 at 09:37:29PM +0800, Zhangjin Wu wrote:
> It is able to pass the 6th argument like the 5th argument via the stack
> for mips, let's add a new my_syscall6() now, see [1] for details:
> 
>   The mips/o32 system call convention passes arguments 5 through 8 on
>   the user stack.
> 
> Both mmap() and pselect6() require my_syscall6().

Very interesting, I didn't manage to make it work previously. Did you
test it to confirm that it works ? I guess so but since you didn't
mention, I preferred to ask.

Thanks!
Willy
Zhangjin Wu July 3, 2023, 10:13 a.m. UTC | #2
Hi, Willy

> Hi Zhangjin,
> 
> On Wed, Jun 28, 2023 at 09:37:29PM +0800, Zhangjin Wu wrote:
> > It is able to pass the 6th argument like the 5th argument via the stack
> > for mips, let's add a new my_syscall6() now, see [1] for details:
> > 
> >   The mips/o32 system call convention passes arguments 5 through 8 on
> >   the user stack.
> > 
> > Both mmap() and pselect6() require my_syscall6().
> 
> Very interesting, I didn't manage to make it work previously. Did you
> test it to confirm that it works ? I guess so but since you didn't
> mention, I preferred to ask.
>

Yes, I'm curious too ;-) 

we did add the mmap test cases and run it for mips, as Thomas
suggested, we pass a none-zero pa_offset to mmap() to make sure the 6th
argument is used.

I just re-tested it for mips and printed something like this:

    44 mmap_bad = <0xffffffff> EINVAL                                [OK]
    45 munmap_bad = -1 EINVAL                                        [OK]
    46 mmap_munmap_good
    pa_offset: 8192
    length: 1
    file_size: 12287
     = 0                                          [OK]
    47 open_tty = 3                                                  [OK]
    48 open_blah = -1 ENOENT                                         [OK]

And I also checked the mips support of musl, it evan provide a
__syscall7, so, it should be ok ;-)

The only difference is, musl provide a different clobber list for
'__mips_isa_rev >= 6', I didn't look into the details yet:
https://elixir.bootlin.com/musl/latest/source/arch/mips/syscall_arch.h

Best regards,
Zhangjin

> Thanks!
> Willy
diff mbox series

Patch

diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-mips.h
index 55a9f01825e0..a8b33d6914a4 100644
--- a/tools/include/nolibc/arch-mips.h
+++ b/tools/include/nolibc/arch-mips.h
@@ -176,6 +176,31 @@  struct sys_stat_struct {
 	_arg4 ? -_num : _num;							\
 })
 
+#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6)			\
+({										\
+	register long _num __asm__ ("v0")  = (num);				\
+	register long _arg1 __asm__ ("a0") = (long)(arg1);			\
+	register long _arg2 __asm__ ("a1") = (long)(arg2);			\
+	register long _arg3 __asm__ ("a2") = (long)(arg3);			\
+	register long _arg4 __asm__ ("a3") = (long)(arg4);			\
+	register long _arg5 = (long)(arg5);					\
+	register long _arg6 = (long)(arg6);					\
+										\
+	__asm__ volatile (							\
+		"addiu $sp, $sp, -32\n"						\
+		"sw %7, 16($sp)\n"						\
+		"sw %8, 20($sp)\n"						\
+		"syscall\n  "							\
+		"addiu $sp, $sp, 32\n"						\
+		: "=r" (_num), "=r"(_arg4)					\
+		: "0"(_num),							\
+		  "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5),	\
+		  "r"(_arg6)							\
+		: SYSCALL_CLOBBERLIST						\
+	);									\
+	_arg4 ? -_num : _num;							\
+})
+
 char **environ __attribute__((weak));
 const unsigned long *_auxv __attribute__((weak));
 
diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h
index 05a228a6ee78..1f8d821000ac 100644
--- a/tools/include/nolibc/nolibc.h
+++ b/tools/include/nolibc/nolibc.h
@@ -13,11 +13,10 @@ 
  * Syscalls are split into 3 levels:
  *   - The lower level is the arch-specific syscall() definition, consisting in
  *     assembly code in compound expressions. These are called my_syscall0() to
- *     my_syscall6() depending on the number of arguments. The MIPS
- *     implementation is limited to 5 arguments. All input arguments are cast
- *     to a long stored in a register. These expressions always return the
- *     syscall's return value as a signed long value which is often either a
- *     pointer or the negated errno value.
+ *     my_syscall6() depending on the number of arguments. All input arguments
+ *     are castto a long stored in a register. These expressions always return
+ *     the syscall's return value as a signed long value which is often either
+ *     a pointer or the negated errno value.
  *
  *   - The second level is mostly architecture-independent. It is made of
  *     static functions called sys_<name>() which rely on my_syscallN()