m68k: Define the m68k-specific syscalls with SYSCALL_DEFINE
PATCH 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.
---
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 | 10 +++++----- arch/m68k/kernel/sys_m68k.c | 26 ++++++++++++-------------- 2 files changed, 17 insertions(+), 19 deletions(-)
arch/m68k/include/asm/syscalls.h +5 -5
@@ -5,13 +5,13 @@#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 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 int sys_getpagesize(void);asmlinkage unsigned long sys_get_thread_area (void ); -asmlinkage int sys_set_thread_area(unsigned long tp); +asmlinkage long sys_set_thread_area (unsigned long tp); asmlinkage int sys_atomic_barrier(void);#include <asm-generic/syscalls.h>
arch/m68k/kernel/sys_m68k.c +12 -14
@@ -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 (;;) {
@@ -533,8 +532,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 +541,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;
@@ -571,7 +569,7 @@ asmlinkage unsigned long sys_get_thread_area(void)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 ;
Signature · 1 line
2.55.0
Replying to myself..
Quoted 1 line · Daniel Palmer
sys_atomic_cmpxchg_32()
Sashiko reports that this got broken because it can't use its trick for getting pt_regs now that it's wrapped.
I'll send a v2 with this fix at some point:
arch/m68k/kernel/sys_m68k.c +0 -0
@@ -514,12 +514,8 @@ SYSCALL_DEFINE6(atomic_cmpxchg_32, unsigned long,
newval, int, oldval,
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
Quoted 15 lines · Daniel Palmer
On m68k a syscall handler's stack arguments are the user's registers saved in
pt_regs, andRESTORE_ALLreloads 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 withasmlinkage_protect(), butsys_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 toksys_mmap_pgoff(), which builds its call tovm_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.
Quoted 8 lines
Define
sys_mmap2(),sys_cacheflush(),sys_atomic_cmpxchg_32()andsys_set_thread_area()withSYSCALL_DEFINEn()so their arguments are protected, and make their prototypes inasm/syscalls.hreturn long to match.Fixes a90f590a1bee ("mm: add ksys_mmap_pgoff() helper; removein-kernel calls to
sys_mmap_pgoff()")Signed-off-by Daniel Palmer <[email protected]>
This looks all good to me, nice analysis and description!
With the follow-up fix you posted
It would be nice to also wrap the other four syscalls that don't take an argument for consistency and also have them use SYSCALL_DEFINE0(). Can you add a second patch for that?
Arnd