Clean up mmap decoding

Previous code merges too many similar, but different ways
of decoding mmap. For example, sys_old_mmap is "params in memory"
API... except SH[64], where it is "params in regs",
i.e. what sys_mmap ("new mmap") function does on other arches!

It's much simpler when every mmap handler has same API regardless
of arch. Where API means whether params are in regs or in memory,
and whether offset is in bytes, pages, or 4k blocks.

Then we just insert correct function pointers into
arch syscall tables.

It turns out there are four common mmap APIs over
all architectures which exist in Linux kernel,
and one outlier for S390.

A number of mmap decoders were plain wrong in arch tables.
For example, BFIN has no old_mmap. It returns ENOSYS.
I checked kernel sources for all arches nad fixed the tables.

There was dead code for x86_64 for old_mmap:
x86_64 has no old_mmap.

* mem.c: Refactor mmap functions so that we have five mmap syscall
handlers, each with the fixed API (not varying by arch).
* pathtrace.c (pathtrace_match): Adjust sys_func == mmap_func checks.
* linux/syscall.h: Declare new mmap syscall handler functions.
* linux/arm/syscallent.h: mmap2 is sys_mmap_pgoff.
* linux/avr32/syscallent.h: mmap is sys_mmap_pgoff.
* linux/bfin/syscallent.h: old_mmap is ENOSYS, mmap2 is sys_mmap_pgoff.
* linux/hppa/syscallent.h: mmap2 is sys_mmap_4koff.
* linux/i386/syscallent.h: mmap2 is sys_mmap_pgoff.
* linux/ia64/syscallent.h: mmap2 is sys_mmap_pgoff.
* linux/m68k/syscallent.h: mmap2 is sys_mmap_pgoff.
* linux/microblaze/syscallent.h: old_mmap is sys_mmap, mmap2 is sys_mmap_pgoff.
* linux/mips/syscallent.h: mmap is sys_mmap_4kgoff.
* linux/or1k/syscallent.h: mmap2 is sys_mmap_pgoff.
* linux/powerpc/syscallent.h: mmap2 is sys_mmap_4kgoff.
* linux/s390/syscallent.h: mmap2 is sys_old_mmap_pgoff.
* linux/s390x/syscallent.h: mmap is sys_old_mmap and thus has 1 arg.
* linux/sh/syscallent.h: old_mmap2 is sys_mmap, mmap2 is sys_mmap_4koff.
* linux/sh64/syscallent.h: Likewise.
* linux/sparc/syscallent1.h: mmap is TD|TM.
* linux/tile/syscallent1.h: mmap2 is sys_mmap_4koff.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/mem.c b/mem.c
index b67a1b6..6f22922 100644
--- a/mem.c
+++ b/mem.c
@@ -39,8 +39,14 @@
 #  define modify_ldt_ldt_s user_desc
 # endif
 #endif
+
+#include <sys/user.h>	/* for PAGE_SHIFT */
 #if defined(SH64)
-# include <asm/page.h>	    /* for PAGE_SHIFT */
+# include <asm/page.h>	/* for PAGE_SHIFT */
+#endif
+#if !defined(PAGE_SHIFT)
+# warning Failed to get PAGE_SHIFT, assuming 12
+# define PAGE_SHIFT 12
 #endif
 
 int
@@ -236,67 +242,99 @@
 	return RVAL_HEX;
 }
 
-int sys_old_mmap(struct tcb *tcp)
+/* Syscall name<->function correspondence is messed up on many arches.
+ * For example:
+ * i386 has __NR_mmap == 90, and it is "old mmap", and
+ * also it has __NR_mmap2 == 192, which is a "new mmap with page offsets".
+ * But x86_64 has just one __NR_mmap == 9, a "new mmap with byte offsets".
+ * Confused? Me too!
+ */
+
+/* Params are pointed to by u_arg[0], offset is in bytes */
+int
+sys_old_mmap(struct tcb *tcp)
 {
+	long u_arg[6];
 #if defined(IA64)
 	/*
 	 * IA64 processes never call this routine, they only use the
-	 * new `sys_mmap' interface.
-	 * For IA32 processes, this code converts the integer arguments
-	 * that they pushed onto the stack, into longs.
+	 * new 'sys_mmap' interface. Only IA32 processes come here.
 	 */
 	int i;
-	long u_arg[6];
 	unsigned narrow_arg[6];
 	if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), (char *) narrow_arg) == -1)
 		return 0;
 	for (i = 0; i < 6; i++)
-		u_arg[i] = narrow_arg[i];
-#elif defined(SH) || defined(SH64)
-	/* SH has always passed the args in registers */
-	long *u_arg = tcp->u_arg;
+		u_arg[i] = (unsigned long) narrow_arg[i];
 #elif defined(X86_64)
-	long u_arg[6];
-	if (current_personality == 1) {
-		int i;
-		unsigned narrow_arg[6];
-		if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), (char *) narrow_arg) == -1)
-			return 0;
-		for (i = 0; i < 6; ++i)
-			u_arg[i] = narrow_arg[i];
-	} else {
-		if (umoven(tcp, tcp->u_arg[0], sizeof(u_arg), (char *) u_arg) == -1)
-			return 0;
-	}
+	/* We are here only in personality 1 (i386) */
+	int i;
+	unsigned narrow_arg[6];
+	if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), (char *) narrow_arg) == -1)
+		return 0;
+	for (i = 0; i < 6; ++i)
+		u_arg[i] = (unsigned long) narrow_arg[i];
 #else
-	long u_arg[6];
 	if (umoven(tcp, tcp->u_arg[0], sizeof(u_arg), (char *) u_arg) == -1)
 		return 0;
 #endif
-	return print_mmap(tcp, u_arg, (unsigned long)u_arg[5]);
+	return print_mmap(tcp, u_arg, (unsigned long) u_arg[5]);
 }
 
+#if defined(S390)
+/* Params are pointed to by u_arg[0], offset is in pages */
+int
+sys_old_mmap_pgoff(struct tcb *tcp)
+{
+	long u_arg[5];
+	int i;
+	unsigned narrow_arg[6];
+	unsigned long long offset;
+	if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), (char *) narrow_arg) == -1)
+		return 0;
+	for (i = 0; i < 5; i++)
+		u_arg[i] = (unsigned long) narrow_arg[i];
+	offset = narrow_arg[5];
+	offset <<= PAGE_SHIFT;
+	return print_mmap(tcp, u_arg, offset);
+}
+#endif
+
+/* Params are passed directly, offset is in bytes */
 int
 sys_mmap(struct tcb *tcp)
 {
 	unsigned long long offset = (unsigned long) tcp->u_arg[5];
-
-#if defined(SH64)
-	/*
-	 * Old mmap differs from new mmap in specifying the
-	 * offset in units of bytes rather than pages.  We
-	 * pretend it's in byte units so the user only ever
-	 * sees bytes in the printout.
-	 */
-	offset <<= PAGE_SHIFT;
-#elif defined(I386)
-	/* Try test/mmap_offset_decode.c */
-	offset <<= 12; /* 4096 byte pages */
-#elif defined(LINUX_MIPSN32) || defined(X32)
+#if defined(LINUX_MIPSN32) || defined(X32)
 	/* Try test/x32_mmap.c */
-	/* At least for X32 it definitely should not be page-shifted! */
 	offset = tcp->ext_arg[5];
 #endif
+	/* Example of kernel-side handling of this variety of mmap:
+	 * arch/x86/kernel/sys_x86_64.c::SYSCALL_DEFINE6(mmap, ...) calls
+	 * sys_mmap_pgoff(..., off >> PAGE_SHIFT); i.e. off is in bytes,
+	 * since the above code converts off to pages.
+	 */
+	return print_mmap(tcp, tcp->u_arg, offset);
+}
+
+/* Params are passed directly, offset is in pages */
+int
+sys_mmap_pgoff(struct tcb *tcp)
+{
+	/* Try test/mmap_offset_decode.c */
+	unsigned long long offset;
+	offset = (unsigned long) tcp->u_arg[5];
+	offset <<= PAGE_SHIFT;
+	return print_mmap(tcp, tcp->u_arg, offset);
+}
+
+/* Params are passed directly, offset is in 4k units */
+int
+sys_mmap_4koff(struct tcb *tcp)
+{
+	unsigned long long offset;
+	offset = (unsigned long) tcp->u_arg[5];
+	offset <<= 12;
 	return print_mmap(tcp, tcp->u_arg, offset);
 }