mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Palmer <daniel@0x0f.com>
To: geert@linux-m68k.org, gerg@linux-m68k.org,
	linux-m68k@lists.linux-m68k.org
Cc: linux-kernel@vger.kernel.org, arnd@arndb.de,
	Daniel Palmer <daniel@0x0f.com>
Subject: [PATCH] m68k: Define the m68k-specific syscalls with SYSCALL_DEFINE
Date: Tue, 24 Nov 2026 11:55:55 +0900	[thread overview]
Message-ID: <20261124025555.2348580-1-daniel@0x0f.com> (raw)

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.

Fixes: a90f590a1bee ("mm: add ksys_mmap_pgoff() helper; remove in-kernel calls to sys_mmap_pgoff()")
Signed-off-by: Daniel Palmer <daniel@0x0f.com>
---

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(-)

diff --git a/arch/m68k/include/asm/syscalls.h b/arch/m68k/include/asm/syscalls.h
index fb3639acd07b..f317b35d6970 100644
--- a/arch/m68k/include/asm/syscalls.h
+++ b/arch/m68k/include/asm/syscalls.h
@@ -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>
diff --git a/arch/m68k/kernel/sys_m68k.c b/arch/m68k/kernel/sys_m68k.c
index 1af5e6082467..9e6d38e57495 100644
--- a/arch/m68k/kernel/sys_m68k.c
+++ b/arch/m68k/kernel/sys_m68k.c
@@ -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;
-- 
2.55.0


             reply	other threads:[~2026-11-24  2:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  2:55 Daniel Palmer [this message]
2026-11-24  3:45 ` Daniel Palmer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124025555.2348580-1-daniel@0x0f.com \
    --to=daniel@0x0f.com \
    --cc=arnd@arndb.de \
    --cc=geert@linux-m68k.org \
    --cc=gerg@linux-m68k.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-m68k@lists.linux-m68k.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®