From b2e9852cf2cfb329a0f355fba4d0b6136c0518be Mon Sep 17 00:00:00 2001
From: Dirk Mueller <dmueller@suse.com>
Date: Sun, 15 Mar 2015 12:44:37 +0100
Subject: [PATCH] ARM64: Fix support for ARMv7 userspace

(cherry picked from commit fb9603538934ca94b4cff972f3b8515e07d061e7)
---
 ...-Implement-the-compat_sys_call_table-in-C.patch | 355 +++++++++++++++++++++
 patches.arch/arm64-fix-mmap2-compat-syscall.patch  |  93 ++++++
 series.conf                                        |   2 +
 3 files changed, 450 insertions(+)
 create mode 100644 patches.arch/arm64-Implement-the-compat_sys_call_table-in-C.patch
 create mode 100644 patches.arch/arm64-fix-mmap2-compat-syscall.patch

diff --git a/patches.arch/arm64-Implement-the-compat_sys_call_table-in-C.patch b/patches.arch/arm64-Implement-the-compat_sys_call_table-in-C.patch
new file mode 100644
index 0000000..c44a89a
--- /dev/null
+++ b/patches.arch/arm64-Implement-the-compat_sys_call_table-in-C.patch
@@ -0,0 +1,355 @@
+From 0156411b1828771c09c92f034e7b81f702b84f07 Mon Sep 17 00:00:00 2001
+From: Catalin Marinas <catalin.marinas@arm.com>
+Date: Tue, 6 Jan 2015 16:42:32 +0000
+Patch-Mainline: 0156411b1828771c09c92f034e7b81f702b84f07
+Subject: [PATCH] arm64: Implement the compat_sys_call_table in C
+
+Unlike the sys_call_table[], the compat one was implemented in sys32.S
+making it impossible to notice discrepancies between the number of
+compat syscalls and the __NR_compat_syscalls macro, the latter having to
+be defined in asm/unistd.h as including asm/unistd32.h would cause
+conflicts on __NR_* definitions. With this patch, incorrect
+__NR_compat_syscalls values will result in a build-time error.
+
+Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
+Signed-off-by: Dirk Mueller <dmueller@suse.com>
+Suggested-by: Mark Rutland <mark.rutland@arm.com>
+Acked-by: Mark Rutland <mark.rutland@arm.com>
+---
+ arch/arm64/include/asm/unistd.h |   3 ++
+ arch/arm64/kernel/Makefile      |   2 +-
+ arch/arm64/kernel/entry.S       |   2 +-
+ arch/arm64/kernel/entry32.S     | 105 ++++++++++++++++++++++++++++++++++++
+ arch/arm64/kernel/sys32.S       | 115 ----------------------------------------
+ arch/arm64/kernel/sys32.c       |  51 ++++++++++++++++++
+ 6 files changed, 161 insertions(+), 117 deletions(-)
+ create mode 100644 arch/arm64/kernel/entry32.S
+ delete mode 100644 arch/arm64/kernel/sys32.S
+ create mode 100644 arch/arm64/kernel/sys32.c
+
+Index: linux-3.16-openSUSE-13.2/arch/arm64/include/asm/unistd.h
+===================================================================
+--- linux-3.16-openSUSE-13.2.orig/arch/arm64/include/asm/unistd.h
++++ linux-3.16-openSUSE-13.2/arch/arm64/include/asm/unistd.h
+@@ -28,6 +28,9 @@
+ #define __ARCH_WANT_SYS_VFORK
+ #endif
+ #define __ARCH_WANT_SYS_CLONE
++
++#ifndef __COMPAT_SYSCALL_NR
+ #include <uapi/asm/unistd.h>
++#endif
+ 
+ #define NR_syscalls (__NR_syscalls)
+Index: linux-3.16-openSUSE-13.2/arch/arm64/kernel/Makefile
+===================================================================
+--- linux-3.16-openSUSE-13.2.orig/arch/arm64/kernel/Makefile
++++ linux-3.16-openSUSE-13.2/arch/arm64/kernel/Makefile
+@@ -18,7 +18,7 @@ arm64-obj-y		:= cputable.o debug-monitor
+ 			   hyp-stub.o psci.o cpu_ops.o insn.o return_address.o
+ 
+ arm64-obj-$(CONFIG_COMPAT)		+= sys32.o kuser32.o signal32.o 	\
+-					   sys_compat.o
++					   sys_compat.o entry32.o
+ arm64-obj-$(CONFIG_FUNCTION_TRACER)	+= ftrace.o entry-ftrace.o
+ arm64-obj-$(CONFIG_MODULES)		+= arm64ksyms.o module.o
+ arm64-obj-$(CONFIG_SMP)			+= smp.o smp_spin_table.o topology.o
+Index: linux-3.16-openSUSE-13.2/arch/arm64/kernel/entry.S
+===================================================================
+--- linux-3.16-openSUSE-13.2.orig/arch/arm64/kernel/entry.S
++++ linux-3.16-openSUSE-13.2/arch/arm64/kernel/entry.S
+@@ -407,7 +407,7 @@ el0_svc_compat:
+ 	/*
+ 	 * AArch32 syscall handling
+ 	 */
+-	adr	stbl, compat_sys_call_table	// load compat syscall table pointer
++	adrp	stbl, compat_sys_call_table	// load compat syscall table pointer
+ 	uxtw	scno, w7			// syscall number in w7 (r7)
+ 	mov     sc_nr, #__NR_compat_syscalls
+ 	b	el0_svc_naked
+Index: linux-3.16-openSUSE-13.2/arch/arm64/kernel/entry32.S
+===================================================================
+--- /dev/null
++++ linux-3.16-openSUSE-13.2/arch/arm64/kernel/entry32.S
+@@ -0,0 +1,105 @@
++/*
++ * Compat system call wrappers
++ *
++ * Copyright (C) 2012 ARM Ltd.
++ * Authors: Will Deacon <will.deacon@arm.com>
++ *	    Catalin Marinas <catalin.marinas@arm.com>
++ *
++ * This program is free software; you can redistribute it and/or modify
++ * it under the terms of the GNU General Public License version 2 as
++ * published by the Free Software Foundation.
++ *
++ * This program is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
++ * GNU General Public License for more details.
++ *
++ * You should have received a copy of the GNU General Public License
++ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
++ */
++
++#include <linux/linkage.h>
++
++#include <asm/assembler.h>
++#include <asm/asm-offsets.h>
++
++/*
++ * System call wrappers for the AArch32 compatibility layer.
++ */
++
++ENTRY(compat_sys_sigreturn_wrapper)
++	mov	x0, sp
++	mov	x27, #0		// prevent syscall restart handling (why)
++	b	compat_sys_sigreturn
++ENDPROC(compat_sys_sigreturn_wrapper)
++
++ENTRY(compat_sys_rt_sigreturn_wrapper)
++	mov	x0, sp
++	mov	x27, #0		// prevent syscall restart handling (why)
++	b	compat_sys_rt_sigreturn
++ENDPROC(compat_sys_rt_sigreturn_wrapper)
++
++ENTRY(compat_sys_statfs64_wrapper)
++	mov	w3, #84
++	cmp	w1, #88
++	csel	w1, w3, w1, eq
++	b	compat_sys_statfs64
++ENDPROC(compat_sys_statfs64_wrapper)
++
++ENTRY(compat_sys_fstatfs64_wrapper)
++	mov	w3, #84
++	cmp	w1, #88
++	csel	w1, w3, w1, eq
++	b	compat_sys_fstatfs64
++ENDPROC(compat_sys_fstatfs64_wrapper)
++
++/*
++ * Wrappers for AArch32 syscalls that either take 64-bit parameters
++ * in registers or that take 32-bit parameters which require sign
++ * extension.
++ */
++ENTRY(compat_sys_pread64_wrapper)
++	regs_to_64	x3, x4, x5
++	b	sys_pread64
++ENDPROC(compat_sys_pread64_wrapper)
++
++ENTRY(compat_sys_pwrite64_wrapper)
++	regs_to_64	x3, x4, x5
++	b	sys_pwrite64
++ENDPROC(compat_sys_pwrite64_wrapper)
++
++ENTRY(compat_sys_truncate64_wrapper)
++	regs_to_64	x1, x2, x3
++	b	sys_truncate
++ENDPROC(compat_sys_truncate64_wrapper)
++
++ENTRY(compat_sys_ftruncate64_wrapper)
++	regs_to_64	x1, x2, x3
++	b	sys_ftruncate
++ENDPROC(compat_sys_ftruncate64_wrapper)
++
++ENTRY(compat_sys_readahead_wrapper)
++	regs_to_64	x1, x2, x3
++	mov	w2, w4
++	b	sys_readahead
++ENDPROC(compat_sys_readahead_wrapper)
++
++ENTRY(compat_sys_fadvise64_64_wrapper)
++	mov	w6, w1
++	regs_to_64	x1, x2, x3
++	regs_to_64	x2, x4, x5
++	mov	w3, w6
++	b	sys_fadvise64_64
++ENDPROC(compat_sys_fadvise64_64_wrapper)
++
++ENTRY(compat_sys_sync_file_range2_wrapper)
++	regs_to_64	x2, x2, x3
++	regs_to_64	x3, x4, x5
++	b	sys_sync_file_range2
++ENDPROC(compat_sys_sync_file_range2_wrapper)
++
++ENTRY(compat_sys_fallocate_wrapper)
++	regs_to_64	x2, x2, x3
++	regs_to_64	x3, x4, x5
++	b	sys_fallocate
++ENDPROC(compat_sys_fallocate_wrapper)
+Index: linux-3.16-openSUSE-13.2/arch/arm64/kernel/sys32.S
+===================================================================
+--- linux-3.16-openSUSE-13.2.orig/arch/arm64/kernel/sys32.S
++++ /dev/null
+@@ -1,115 +0,0 @@
+-/*
+- * Compat system call wrappers
+- *
+- * Copyright (C) 2012 ARM Ltd.
+- * Authors: Will Deacon <will.deacon@arm.com>
+- *	    Catalin Marinas <catalin.marinas@arm.com>
+- *
+- * This program is free software; you can redistribute it and/or modify
+- * it under the terms of the GNU General Public License version 2 as
+- * published by the Free Software Foundation.
+- *
+- * This program is distributed in the hope that it will be useful,
+- * but WITHOUT ANY WARRANTY; without even the implied warranty of
+- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+- * GNU General Public License for more details.
+- *
+- * You should have received a copy of the GNU General Public License
+- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+- */
+-
+-#include <linux/linkage.h>
+-
+-#include <asm/assembler.h>
+-#include <asm/asm-offsets.h>
+-
+-/*
+- * System call wrappers for the AArch32 compatibility layer.
+- */
+-
+-compat_sys_sigreturn_wrapper:
+-	mov	x0, sp
+-	mov	x27, #0		// prevent syscall restart handling (why)
+-	b	compat_sys_sigreturn
+-ENDPROC(compat_sys_sigreturn_wrapper)
+-
+-compat_sys_rt_sigreturn_wrapper:
+-	mov	x0, sp
+-	mov	x27, #0		// prevent syscall restart handling (why)
+-	b	compat_sys_rt_sigreturn
+-ENDPROC(compat_sys_rt_sigreturn_wrapper)
+-
+-compat_sys_statfs64_wrapper:
+-	mov	w3, #84
+-	cmp	w1, #88
+-	csel	w1, w3, w1, eq
+-	b	compat_sys_statfs64
+-ENDPROC(compat_sys_statfs64_wrapper)
+-
+-compat_sys_fstatfs64_wrapper:
+-	mov	w3, #84
+-	cmp	w1, #88
+-	csel	w1, w3, w1, eq
+-	b	compat_sys_fstatfs64
+-ENDPROC(compat_sys_fstatfs64_wrapper)
+-
+-/*
+- * Wrappers for AArch32 syscalls that either take 64-bit parameters
+- * in registers or that take 32-bit parameters which require sign
+- * extension.
+- */
+-compat_sys_pread64_wrapper:
+-	regs_to_64	x3, x4, x5
+-	b	sys_pread64
+-ENDPROC(compat_sys_pread64_wrapper)
+-
+-compat_sys_pwrite64_wrapper:
+-	regs_to_64	x3, x4, x5
+-	b	sys_pwrite64
+-ENDPROC(compat_sys_pwrite64_wrapper)
+-
+-compat_sys_truncate64_wrapper:
+-	regs_to_64	x1, x2, x3
+-	b	sys_truncate
+-ENDPROC(compat_sys_truncate64_wrapper)
+-
+-compat_sys_ftruncate64_wrapper:
+-	regs_to_64	x1, x2, x3
+-	b	sys_ftruncate
+-ENDPROC(compat_sys_ftruncate64_wrapper)
+-
+-compat_sys_readahead_wrapper:
+-	regs_to_64	x1, x2, x3
+-	mov	w2, w4
+-	b	sys_readahead
+-ENDPROC(compat_sys_readahead_wrapper)
+-
+-compat_sys_fadvise64_64_wrapper:
+-	mov	w6, w1
+-	regs_to_64	x1, x2, x3
+-	regs_to_64	x2, x4, x5
+-	mov	w3, w6
+-	b	sys_fadvise64_64
+-ENDPROC(compat_sys_fadvise64_64_wrapper)
+-
+-compat_sys_sync_file_range2_wrapper:
+-	regs_to_64	x2, x2, x3
+-	regs_to_64	x3, x4, x5
+-	b	sys_sync_file_range2
+-ENDPROC(compat_sys_sync_file_range2_wrapper)
+-
+-compat_sys_fallocate_wrapper:
+-	regs_to_64	x2, x2, x3
+-	regs_to_64	x3, x4, x5
+-	b	sys_fallocate
+-ENDPROC(compat_sys_fallocate_wrapper)
+-
+-#undef __SYSCALL
+-#define __SYSCALL(x, y)		.quad	y	// x
+-
+-/*
+- * The system calls table must be 4KB aligned.
+- */
+-	.align	12
+-ENTRY(compat_sys_call_table)
+-#include <asm/unistd32.h>
+Index: linux-3.16-openSUSE-13.2/arch/arm64/kernel/sys32.c
+===================================================================
+--- /dev/null
++++ linux-3.16-openSUSE-13.2/arch/arm64/kernel/sys32.c
+@@ -0,0 +1,51 @@
++/*
++ * arch/arm64/kernel/sys32.c
++ *
++ * Copyright (C) 2015 ARM Ltd.
++ *
++ * This program is free software(void); you can redistribute it and/or modify
++ * it under the terms of the GNU General Public License version 2 as
++ * published by the Free Software Foundation.
++ *
++ * This program is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
++ * GNU General Public License for more details.
++ *
++ * You should have received a copy of the GNU General Public License
++ * along with this program.  If not, see <http(void);//www.gnu.org/licenses/>.
++ */
++
++/*
++ * Needed to avoid conflicting __NR_* macros between uapi/asm/unistd.h and
++ * asm/unistd32.h.
++ */
++#define __COMPAT_SYSCALL_NR
++
++#include <linux/compiler.h>
++#include <linux/syscalls.h>
++
++asmlinkage long compat_sys_sigreturn_wrapper(void);
++asmlinkage long compat_sys_rt_sigreturn_wrapper(void);
++asmlinkage long compat_sys_statfs64_wrapper(void);
++asmlinkage long compat_sys_fstatfs64_wrapper(void);
++asmlinkage long compat_sys_pread64_wrapper(void);
++asmlinkage long compat_sys_pwrite64_wrapper(void);
++asmlinkage long compat_sys_truncate64_wrapper(void);
++asmlinkage long compat_sys_ftruncate64_wrapper(void);
++asmlinkage long compat_sys_readahead_wrapper(void);
++asmlinkage long compat_sys_fadvise64_64_wrapper(void);
++asmlinkage long compat_sys_sync_file_range2_wrapper(void);
++asmlinkage long compat_sys_fallocate_wrapper(void);
++
++#undef __SYSCALL
++#define __SYSCALL(nr, sym)	[nr] = sym,
++
++/*
++ * The sys_call_table array must be 4K aligned to be accessible from
++ * kernel/entry.S.
++ */
++void * const compat_sys_call_table[__NR_compat_syscalls] __aligned(4096) = {
++	[0 ... __NR_compat_syscalls - 1] = sys_ni_syscall,
++#include <asm/unistd32.h>
++};
diff --git a/patches.arch/arm64-fix-mmap2-compat-syscall.patch b/patches.arch/arm64-fix-mmap2-compat-syscall.patch
new file mode 100644
index 0000000..12bedff
--- /dev/null
+++ b/patches.arch/arm64-fix-mmap2-compat-syscall.patch
@@ -0,0 +1,93 @@
+From: Andreas Schwab <schwab@suse.de>
+Date: Mon, 9 Mar 2015 17:27:36 +0100
+Subject: [PATCH] arm64: fix implementation of mmap2 compat syscall
+Patch-mainline: No
+
+The arm mmap2 syscall takes the offset in units of 4K, thus with 64K pages
+the offset needs to be scaled to units of pages.
+
+Tested-by: Alexander Graf <agraf@suse.de>
+Signed-off-by: Andreas Schwab <schwab@suse.de>
+
+---
+ arch/arm64/include/asm/unistd32.h |  2 +-
+ arch/arm64/kernel/entry32.S       | 18 ++++++++++++++++++
+ arch/arm64/kernel/sys32.c         |  6 ++++++
+ 3 files changed, 25 insertions(+), 1 deletion(-)
+
+Index: linux-3.16-openSUSE-13.2/arch/arm64/include/asm/unistd32.h
+===================================================================
+--- linux-3.16-openSUSE-13.2.orig/arch/arm64/include/asm/unistd32.h
++++ linux-3.16-openSUSE-13.2/arch/arm64/include/asm/unistd32.h
+@@ -213,7 +213,7 @@ __SYSCALL(188, sys_ni_syscall)			/* 188
+ __SYSCALL(189, sys_ni_syscall)			/* 189 reserved */
+ __SYSCALL(190, sys_vfork)
+ __SYSCALL(191, compat_sys_getrlimit)		/* SuS compliant getrlimit */
+-__SYSCALL(192, sys_mmap_pgoff)
++__SYSCALL(192, compat_sys_mmap2_wrapper)
+ __SYSCALL(193, compat_sys_truncate64_wrapper)
+ __SYSCALL(194, compat_sys_ftruncate64_wrapper)
+ __SYSCALL(195, sys_stat64)
+Index: linux-3.16-openSUSE-13.2/arch/arm64/kernel/entry32.S
+===================================================================
+--- linux-3.16-openSUSE-13.2.orig/arch/arm64/kernel/entry32.S
++++ linux-3.16-openSUSE-13.2/arch/arm64/kernel/entry32.S
+@@ -19,9 +19,12 @@
+  */
+ 
+ #include <linux/linkage.h>
++#include <linux/const.h>
+ 
+ #include <asm/assembler.h>
+ #include <asm/asm-offsets.h>
++#include <asm/errno.h>
++#include <asm/page.h>
+ 
+ /*
+  * System call wrappers for the AArch32 compatibility layer.
+@@ -54,6 +57,21 @@ ENTRY(compat_sys_fstatfs64_wrapper)
+ ENDPROC(compat_sys_fstatfs64_wrapper)
+ 
+ /*
++ * Note: off_4k (w5) is always units of 4K.  If we can't do the requested
++ * offset, we return EINVAL.
++ */
++#if PAGE_SHIFT > 12
++ENTRY(compat_sys_mmap2_wrapper)
++       tst     w5, #~PAGE_MASK >> 12
++       b.ne    1f
++       lsr     w5, w5, #PAGE_SHIFT - 12
++       b       sys_mmap_pgoff
++1:     mov     x0, #-EINVAL
++       ret     lr
++ENDPROC(compat_sys_mmap2_wrapper)
++#endif
++
++/*
+  * Wrappers for AArch32 syscalls that either take 64-bit parameters
+  * in registers or that take 32-bit parameters which require sign
+  * extension.
+Index: linux-3.16-openSUSE-13.2/arch/arm64/kernel/sys32.c
+===================================================================
+--- linux-3.16-openSUSE-13.2.orig/arch/arm64/kernel/sys32.c
++++ linux-3.16-openSUSE-13.2/arch/arm64/kernel/sys32.c
+@@ -24,6 +24,7 @@
+ 
+ #include <linux/compiler.h>
+ #include <linux/syscalls.h>
++#include <asm/page.h>
+ 
+ asmlinkage long compat_sys_sigreturn_wrapper(void);
+ asmlinkage long compat_sys_rt_sigreturn_wrapper(void);
+@@ -37,6 +38,11 @@ asmlinkage long compat_sys_readahead_wra
+ asmlinkage long compat_sys_fadvise64_64_wrapper(void);
+ asmlinkage long compat_sys_sync_file_range2_wrapper(void);
+ asmlinkage long compat_sys_fallocate_wrapper(void);
++#if PAGE_SHIFT > 12
++asmlinkage long compat_sys_mmap2_wrapper(void);
++#else
++#define compat_sys_mmap2_wrapper sys_mmap_pgoff
++#endif
+ 
+ #undef __SYSCALL
+ #define __SYSCALL(nr, sym)	[nr] = sym,
diff --git a/series.conf b/series.conf
index 880cb24..b582922 100644
--- a/series.conf
+++ b/series.conf
@@ -230,6 +230,8 @@
 	patches.arch/arm64-0037-drivers-net-fddi-skfp-h-skfbi.h-Remove-useless-PCI_B.patch
 	patches.arch/arm64-0038-arm64-mustang-Disable-sgenet-and-xgenet.patch
 	patches.arch/arm64-0039-generic-pci.patch
+	patches.arch/arm64-Implement-the-compat_sys_call_table-in-C.patch
+	patches.arch/arm64-fix-mmap2-compat-syscall.patch
 
 	########################################################
 	# S/390
-- 
2.0.4

