m68k: Define the m68k-specific syscalls with SYSCALL_DEFINE
PATCH v2 1/1 m68k: Define the m68k-specific syscalls with SYSCALL_DEFINE
On m68k a syscall handler's stack arguments are the user's registers saved in pt_regs, and RESTORE_ALL reloads them on the way out. The C ABI lets a function reuse its incoming argument slots, so anything the handler writes there ends up in user space. SYSCALL_DEFINEn() stops the compiler doing that with asmlinkage_protect(), but sys_mmap2() and the other m68k-specific syscalls are plain asmlinkage functions.
With GCC 16 and a kernel built with -O2, sys_mmap2() becomes a tail call to ksys_mmap_pgoff(), which builds its call to vm_mmap_pgoff() by shifting its arguments up one slot in place. User space gets d2-d5 back holding addr/len/prot/flags.
This issue was noticed in nolibc programs that are using mmap() and I initially thought nolibc's m68k support was to blame.
Define sys_mmap2(), sys_cacheflush(), sys_atomic_cmpxchg_32() and sys_set_thread_area() with SYSCALL_DEFINEn() so their arguments are protected, and make their prototypes in asm/syscalls.h return long to match. sys_atomic_cmpxchg_32() gets an additional small logic change to fix how it gets pt_regs as the hack it currently uses is no longer valid since its code is wrapped in an outer function by __SYSCALL_DEFINEx().
For consistency sys_getpagesize(), sys_get_thread_area() and sys_atomic_barrier() get SYSCALL_DEFINE0() even though they don't technically need it. The generated code doesn't change.
---
v2:
- Added a fix to sys_atomic_cmpxchg_32() highlighted by sashiko.
- Converted the argumentless ones too as suggested by Arnd, I considered making this a second commit but that felt like adding two extra emails for something that looks fine squashed into this patch. I assumed Arnd's reviewed-by would carry over so kept it. Shout at me if thats not ok :).
- Follow up work: It looks like the same lump of code for sys_getpagesize() is in multiple arches, I might send some patches to deduplicate that later.
v1:
I think this is right, I've tested it on real 030, 040 and 060 machines.
I found this issue making DRM/KMS programs with nolibc. Since I wrote the m68k support for nolibc I suspected I had missed some ABI thing and messed it up and I put a local workaround in there. Then the issue started showing up a normal libc.
I fixed the sys_mmap2() place by stepping through the kernel in QEMU and working out who was messing up the registers. My fix worked but I wasn't sure why there was an issue to start with so I asked my clanker and it disassembled the kernel and explained the tail-call thing in the commit message.
arch/m68k/include/asm/syscalls.h | 16 +++++++------- arch/m68k/kernel/sys_m68k.c | 38 ++++++++++++++------------------ 2 files changed, 24 insertions(+), 30 deletions(-)
arch/m68k/include/asm/syscalls.h +8 -8
@@ -5,14 +5,14 @@#include <linux/compiler_types.h> #include <linux/linkage.h> -asmlinkage int sys_cacheflush(unsigned long addr, int scope, int cache, -unsigned long len); -asmlinkage int sys_atomic_cmpxchg_32(unsigned long newval, int oldval, int d3, -int d4,int d5,unsigned long __user *mem); -asmlinkage int sys_getpagesize(void); -asmlinkage unsigned long sys_get_thread_area (void ); -asmlinkage int sys_set_thread_area(unsigned long tp); -asmlinkage int sys_atomic_barrier(void); +asmlinkage long sys_cacheflush (unsigned long addr,int scope,int cache, +unsigned long len); +asmlinkage long sys_atomic_cmpxchg_32 (unsigned long newval,int oldval,int d3, +int d4,int d5,unsigned long __user *mem); +asmlinkage long sys_getpagesize (void ); +asmlinkage long sys_get_thread_area (void ); +asmlinkage long sys_set_thread_area (unsigned long tp); +asmlinkage long sys_atomic_barrier (void );#include <asm-generic/syscalls.h>
arch/m68k/kernel/sys_m68k.c +16 -22
@@ -37,9 +37,9 @@#include "../mm/fault.h" -asmlinkage long sys_mmap2 (unsigned long addr,unsigned long len, -unsigned long prot,unsigned long flags, -unsigned long fd,unsigned long pgoff) +SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len, + unsigned long, prot, unsigned long, flags, + unsigned long, fd, unsigned long, pgoff) {/* * This is wrong for sun3 - there PAGE_SIZE is 8Kb,
@@ -374,8 +374,8 @@ cache_flush_060 (unsigned long addr, int scope, int cache, unsigned long len) }/* sys_cacheflush -- flush (part of) the processor cache. */ -asmlinkage int -sys_cacheflush (unsigned long addr,int scope,int cache,unsigned long len) +SYSCALL_DEFINE4(cacheflush, unsigned long, addr, int, scope, int, cache, + unsigned long, len) {int ret = -EINVAL ;
@@ -459,9 +459,8 @@ sys_cacheflush (unsigned long addr, int scope, int cache, unsigned long len)/* This syscall gets its arguments in A0 (mem), D2 (oldval) and D1 (newval). */ -asmlinkage int -sys_atomic_cmpxchg_32 (unsigned long newval,int oldval,int d3,int d4,int d5, -unsigned long __user * mem) +SYSCALL_DEFINE6(atomic_cmpxchg_32, unsigned long, newval, int, oldval, + int, d3, int, d4, int, d5, unsigned long __user *, mem) {/* This was borrowed from ARM's implementation. */ for (;;) {
@@ -515,12 +514,8 @@ sys_atomic_cmpxchg_32(unsigned long newval, int oldval, int d3, int d4, int d5,Make the kernel do the necessary page stuff, then re-iterate. Simulate a write access fault to do that. */ { -/* The first argument of the function corresponds to -D1, which is the first field of struct pt_regs. */ -struct pt_regs *fp = (struct pt_regs *)&newval; -/* '3' is an RMW flag. */ -if (do_page_fault (fp, (unsigned long )mem,3 )) +if (do_page_fault (current_pt_regs (), (unsigned long )mem,3 ))/* If the do_page_fault() failed, we don't have anything meaningful to return. There should be a SIGSEGV pending for
@@ -533,8 +528,8 @@ sys_atomic_cmpxchg_32(unsigned long newval, int oldval, int d3, int d4, int d5,#else /* sys_cacheflush -- flush (part of) the processor cache. */ -asmlinkage int -sys_cacheflush (unsigned long addr,int scope,int cache,unsigned long len) +SYSCALL_DEFINE4(cacheflush, unsigned long, addr, int, scope, int, cache, + unsigned long, len) {flush_cache_all ();return 0 ;
@@ -542,9 +537,8 @@ sys_cacheflush (unsigned long addr, int scope, int cache, unsigned long len)/* This syscall gets its arguments in A0 (mem), D2 (oldval) and D1 (newval). */ -asmlinkage int -sys_atomic_cmpxchg_32 (unsigned long newval,int oldval,int d3,int d4,int d5, -unsigned long __user * mem) +SYSCALL_DEFINE6(atomic_cmpxchg_32, unsigned long, newval, int, oldval, + int, d3, int, d4, int, d5, unsigned long __user *, mem) {struct mm_struct *mm = current->mm;unsigned long mem_value;
@@ -561,23 +555,23 @@ sys_atomic_cmpxchg_32(unsigned long newval, int oldval, int d3, int d4, int d5,#endif /* CONFIG_MMU */ -asmlinkage int sys_getpagesize(void) +SYSCALL_DEFINE0 (getpagesize) {return PAGE_SIZE ; } -asmlinkage unsigned long sys_get_thread_area (void ) +SYSCALL_DEFINE0 (get_thread_area) {return current_thread_info ()->tp_value; } -asmlinkage int sys_set_thread_area(unsigned long tp) +SYSCALL_DEFINE1(set_thread_area, unsigned long, tp) {current_thread_info ()->tp_value = tp;return 0 ; } -asmlinkage int sys_atomic_barrier(void) +SYSCALL_DEFINE0 (atomic_barrier) {/* no code needed for uniprocs */ return 0 ;
Signature · 1 line
2.55.0