Skip to contentSkip to conversation

riscv: compat: syscall: Add compat_sys_call_table implementation

PATCH Superseded [email protected] 2 participants 5 messages Jan 20, 2022 – Jan 21, 2022

Subsystems (4)

v3 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH v3 8/17 riscv: compat: syscall: Add compat_sys_call_table implementation

From: Guo Ren <[email protected]>

Implement compat_syscall_table.c with compat_sys_call_table & fixup system call such as truncate64,pread64,fallocate which need two regs to indicate 64bit-arg (copied from arm64).

---
 arch/riscv/include/asm/syscall.h         |  3 +
 arch/riscv/include/uapi/asm/unistd.h     |  2 +-
 arch/riscv/kernel/Makefile               |  1 +
 arch/riscv/kernel/compat_syscall_table.c | 72 ++++++++++++++++++++++++
 arch/riscv/kernel/sys_riscv.c            |  6 +-
 5 files changed, 81 insertions(+), 3 deletions(-)
 create mode 100644 arch/riscv/kernel/compat_syscall_table.c
arch/riscv/include/asm/syscall.h +3 -0
index 7ac6a0e275f2..4ff98a22ef24 100644
@@ -16,6 +16,9 @@
 
 /* The array of function pointers for syscalls. */
 extern void * const sys_call_table[];
+#ifdef CONFIG_COMPAT
+extern void * const compat_sys_call_table[];
+#endif
 
 /*
  * Only the low 32 bits of orig_r0 are meaningful, so we return int.
arch/riscv/include/uapi/asm/unistd.h +1 -1
index 8062996c2dfd..c9e50eed14aa 100644
@@ -15,7 +15,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-#ifdef __LP64__
+#if defined(__LP64__) && !defined(__SYSCALL_COMPAT)
 #define __ARCH_WANT_NEW_STAT
 #define __ARCH_WANT_SET_GET_RLIMIT
 #endif /* __LP64__ */
arch/riscv/kernel/Makefile +1 -0
index 3397ddac1a30..1f2111179615 100644
@@ -65,3 +65,4 @@ obj-$(CONFIG_CRASH_DUMP)	+= crash_dump.o
 obj-$(CONFIG_JUMP_LABEL)	+= jump_label.o
 
 obj-$(CONFIG_EFI)		+= efi.o
+obj-$(CONFIG_COMPAT)		+= compat_syscall_table.o
arch/riscv/kernel/compat_syscall_table.c new file +72 -0

72 lines not on this page · raw message

arch/riscv/kernel/sys_riscv.c +4 -2
index 12f8a7fce78b..9c0194f176fc 100644
@@ -33,7 +33,9 @@ SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
 {
 	return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 0);
 }
-#else
+#endif
+
+#if defined(CONFIG_32BIT) || defined(CONFIG_COMPAT)
 SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
 	unsigned long, prot, unsigned long, flags,
 	unsigned long, fd, off_t, offset)
@@ -44,7 +46,7 @@ SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
 	 */
 	return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 12);
 }
-#endif /* !CONFIG_64BIT */
+#endif
 
 /*
  * Allows the instruction cache to be flushed from userspace.  Despite RISC-V
Signature · 1 line
2.25.1
#1 lore raw

arch/riscv/include/asm/syscall.h line 21

Quoted 6 lines · On Thu, Jan 20, 2022 at 8:39 AM <[email protected]> wrote:

/* The array of function pointers for syscalls. */ extern void * const sys_call_table[];

arch/riscv/include/asm/syscall.h +3 -0
+#ifdef CONFIG_COMPAT
+extern void * const compat_sys_call_table[];
+#endif

No need for the #ifdef, the normal convention is to just define the extern declaration unconditionally for symbols that may or may not be defined.

arch/riscv/kernel/compat_syscall_table.c line 20

Quoted 5 lines
arch/riscv/kernel/compat_syscall_table.c new file +5 -0
+COMPAT_SYSCALL_DEFINE3(truncate64, const char __user *, pathname,
+                      arg_u32p(length))
+{
+       return ksys_truncate(pathname, arg_u64(length));
+}

Are you sure these are the right calling conventions? According to [1], I think the 64-bit argument should be in an aligned pair of registers, which means you need an extra pad argument as in the arm64 version of these functions. Same for ftruncate64, pread64, pwrite64, and readahead.

Quoted 31 lines
arch/riscv/kernel/compat_syscall_table.c new file +31 -0
+COMPAT_SYSCALL_DEFINE3(ftruncate64, unsigned int, fd, arg_u32p(length))
+{
+       return ksys_ftruncate(fd, arg_u64(length));
+}
+
+COMPAT_SYSCALL_DEFINE6(fallocate, int, fd, int, mode,
+                      arg_u32p(offset), arg_u32p(len))
+{
+       return ksys_fallocate(fd, mode, arg_u64(offset), arg_u64(len));
+}
+
+COMPAT_SYSCALL_DEFINE5(pread64, unsigned int, fd, char __user *, buf,
+                      size_t, count, arg_u32p(pos))
+{
+       return ksys_pread64(fd, buf, count, arg_u64(pos));
+}
+
+COMPAT_SYSCALL_DEFINE5(pwrite64, unsigned int, fd,
+                      const char __user *, buf, size_t, count, arg_u32p(pos))
+{
+       return ksys_pwrite64(fd, buf, count, arg_u64(pos));
+}
+
+COMPAT_SYSCALL_DEFINE6(sync_file_range, int, fd, arg_u32p(offset),
+                      arg_u32p(nbytes), unsigned int, flags)
+{
+       return ksys_sync_file_range(fd, arg_u64(offset), arg_u64(nbytes),
+                                   flags);
+}
+
+COMPAT_SYSCALL_DEFINE4(readahead, int, fd, arg_u32p(offset),

arch/riscv/kernel/compat_syscall_table.c line 62

Quoted 10 lines
arch/riscv/kernel/compat_syscall_table.c new file +10 -0
+                      size_t, count)
+{
+       return ksys_readahead(fd, arg_u64(offset), count);
+}
+
+COMPAT_SYSCALL_DEFINE6(fadvise64_64, int, fd, int, advice, arg_u32p(offset),
+                      arg_u32p(len))
+{
+       return ksys_fadvise64_64(fd, arg_u64(offset), arg_u64(len), advice);
+}

I still feel like these should be the common implementations next to the native handlers inside of an #ifdef CONFIG_COMPAT.

The names clash with the custom versions defined for powerpc and sparc, but the duplicates look compatible if you can account for the padded argument and the lo/hi order of the pairs, so could just be removed here (all other architectures use custom function names instead).

Arnd

[1] https://riscv.org/wp-content/uploads/2015/01/riscv-calling.pdf

author maintainer
#2 lore raw
Quoted 11 lines · Arnd Bergmann

arch/riscv/include/asm/syscall.h line 21

Quoted 6 lines · On Thu, Jan 20, 2022 at 8:39 AM <[email protected]> wrote:

/* The array of function pointers for syscalls. */ extern void * const sys_call_table[];

arch/riscv/include/asm/syscall.h +3 -0
+#ifdef CONFIG_COMPAT
+extern void * const compat_sys_call_table[];
+#endif

No need for the #ifdef, the normal convention is to just define the extern declaration unconditionally for symbols that may or may not be defined.

Okay

Quoted 12 lines

arch/riscv/kernel/compat_syscall_table.c line 20

Quoted 6 lines
arch/riscv/kernel/compat_syscall_table.c new file +5 -0
+COMPAT_SYSCALL_DEFINE3(truncate64, const char __user *, pathname,
+                      arg_u32p(length))
+{
+       return ksys_truncate(pathname, arg_u64(length));
+}

Are you sure these are the right calling conventions? According to [1], I think the 64-bit argument should be in an aligned pair of registers, which means you need an extra pad argument as in the arm64 version of these functions. Same for ftruncate64, pread64, pwrite64, and readahead.

[1] has abandoned.

See:
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc

Ltp test results:

ftruncate01                                        PASS       0
ftruncate01_64                                     PASS       0
ftruncate03                                        PASS       0
ftruncate03_64                                     PASS       0
ftruncate04                                        CONF       32
ftruncate04_64                                     CONF       32
truncate02                                         PASS       0
truncate02_64                                      PASS       0
truncate03                                         PASS       0
truncate03_64                                      PASS       0
pread01                                            PASS       0
pread01_64                                         PASS       0
pread02                                            PASS       0
pread02_64                                         PASS       0
pread03                                            PASS       0
pread03_64                                         PASS       0
pwrite01_64                                        PASS       0
pwrite02_64                                        PASS       0
pwrite03_64                                        PASS       0
pwrite04_64                                        PASS       0
readahead01                                        PASS       0
readahead02                                        CONF       32
Quoted 46 lines
Quoted 32 lines
arch/riscv/kernel/compat_syscall_table.c new file +31 -0
+COMPAT_SYSCALL_DEFINE3(ftruncate64, unsigned int, fd, arg_u32p(length))
+{
+       return ksys_ftruncate(fd, arg_u64(length));
+}
+
+COMPAT_SYSCALL_DEFINE6(fallocate, int, fd, int, mode,
+                      arg_u32p(offset), arg_u32p(len))
+{
+       return ksys_fallocate(fd, mode, arg_u64(offset), arg_u64(len));
+}
+
+COMPAT_SYSCALL_DEFINE5(pread64, unsigned int, fd, char __user *, buf,
+                      size_t, count, arg_u32p(pos))
+{
+       return ksys_pread64(fd, buf, count, arg_u64(pos));
+}
+
+COMPAT_SYSCALL_DEFINE5(pwrite64, unsigned int, fd,
+                      const char __user *, buf, size_t, count, arg_u32p(pos))
+{
+       return ksys_pwrite64(fd, buf, count, arg_u64(pos));
+}
+
+COMPAT_SYSCALL_DEFINE6(sync_file_range, int, fd, arg_u32p(offset),
+                      arg_u32p(nbytes), unsigned int, flags)
+{
+       return ksys_sync_file_range(fd, arg_u64(offset), arg_u64(nbytes),
+                                   flags);
+}
+
+COMPAT_SYSCALL_DEFINE4(readahead, int, fd, arg_u32p(offset),

arch/riscv/kernel/compat_syscall_table.c line 62

Quoted 10 lines
arch/riscv/kernel/compat_syscall_table.c new file +10 -0
+                      size_t, count)
+{
+       return ksys_readahead(fd, arg_u64(offset), count);
+}
+
+COMPAT_SYSCALL_DEFINE6(fadvise64_64, int, fd, int, advice, arg_u32p(offset),
+                      arg_u32p(len))
+{
+       return ksys_fadvise64_64(fd, arg_u64(offset), arg_u64(len), advice);
+}

I still feel like these should be the common implementations next to the native handlers inside of an #ifdef CONFIG_COMPAT.

Quoted 4 lines

The names clash with the custom versions defined for powerpc and sparc, but the duplicates look compatible if you can account for the padded argument and the lo/hi order of the pairs, so could just be removed here (all other architectures use custom function names instead).

I would try it later.

Quoted 4 lines

Arnd

[1] https://riscv.org/wp-content/uploads/2015/01/riscv-calling.pdf

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
#3 lore raw
Quoted 9 lines · Guo Ren
Quoted 7 lines · Arnd Bergmann

On Thu, Jan 20, 2022 at 8:39 AM <[email protected]> wrote:

Are you sure these are the right calling conventions? According to [1], I think the 64-bit argument should be in an aligned pair of registers, which means you need an extra pad argument as in the arm64 version of these functions. Same for ftruncate64, pread64, pwrite64, and readahead.

Quoted 4 lines

[1] has abandoned.

See:
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc

Ok, thanks for the reference, I picked the first one that came up in a google search and didn't expect this to ever have changed.

Quoted 8 lines
Quoted 7 lines

I still feel like these should be the common implementations next to the native handlers inside of an #ifdef CONFIG_COMPAT.

The names clash with the custom versions defined for powerpc and sparc, but the duplicates look compatible if you can account for the padded argument and the lo/hi order of the pairs, so could just be removed here (all other architectures use custom function names instead).

I would try it later.

This becomes easier then, as powerpc and sparc already have the non-padded calling conventions, so you could just generalize those without looking at the other architectures or adding the padding. The powerpc version already has the dual-endian version, so using that will work on big-endian sparc and on little-endian riscv as well, though we may need to come up with a better name for the arg_u32/arg_u64/merge_64 macros in order to put that into a global header without namespace collisions.

Arnd

author maintainer
#4 lore raw
Quoted 28 lines · Arnd Bergmann
Quoted 9 lines · Guo Ren
Quoted 7 lines · Arnd Bergmann

On Thu, Jan 20, 2022 at 8:39 AM <[email protected]> wrote:

Are you sure these are the right calling conventions? According to [1], I think the 64-bit argument should be in an aligned pair of registers, which means you need an extra pad argument as in the arm64 version of these functions. Same for ftruncate64, pread64, pwrite64, and readahead.

Quoted 4 lines

[1] has abandoned.

See:
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc

Ok, thanks for the reference, I picked the first one that came up in a google search and didn't expect this to ever have changed.

Quoted 8 lines
Quoted 7 lines

I still feel like these should be the common implementations next to the native handlers inside of an #ifdef CONFIG_COMPAT.

The names clash with the custom versions defined for powerpc and sparc, but the duplicates look compatible if you can account for the padded argument and the lo/hi order of the pairs, so could just be removed here (all other architectures use custom function names instead).

I would try it later.

Quoted 7 lines

This becomes easier then, as powerpc and sparc already have the non-padded calling conventions, so you could just generalize those without looking at the other architectures or adding the padding. The powerpc version already has the dual-endian version, so using that will work on big-endian sparc and on little-endian riscv as well, though we may need to come up with a better name for the arg_u32/arg_u64/merge_64 macros in order to put that into a global header without namespace collisions.

Sounds good, thanks!

Quoted 2 lines

Arnd

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/