1From fd40eee42273220fb0050fe10744b10067adc0a7 Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?An=C3=ADbal=20Lim=C3=B3n?= <anibal.limon@linux.intel.com>
3Date: Fri, 31 Aug 2018 17:31:50 +0200
4Subject: [PATCH] x86_64: Add support to build kexec-tools with x32 ABI
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9Summary of changes,
10
11configure.ac: Add test for detect x32 ABI.
12purgatory/arch/x86_64/Makefile: Not use mcmodel large when
13	x32 ABI is set.
14kexec/arch/x86_64/kexec-elf-rel-x86_64.c: When x32 ABI is set
15	use ELFCLASS32 instead of ELFCLASS64.
16kexec/kexec-syscall.h: Add correct syscall number for x32 ABI.
17
18Upstream-Status: Submitted
19
20Signed-off-by: Aníbal Limón <anibal.limon@linux.intel.com>
21Signed-off-by: Mariano Lopez <mariano.lopez@linux.intel.com>
22
23---
24 configure.ac                             | 9 +++++++++
25 kexec/arch/x86_64/kexec-elf-rel-x86_64.c | 4 ++++
26 kexec/kexec-syscall.h                    | 4 ++++
27 purgatory/arch/x86_64/Makefile           | 4 +++-
28 4 files changed, 20 insertions(+), 1 deletion(-)
29
30diff --git a/configure.ac b/configure.ac
31index e05d601..c428146 100644
32--- a/configure.ac
33+++ b/configure.ac
34@@ -54,6 +54,15 @@ case $target_cpu in
35 		;;
36 	ia64|x86_64|alpha|m68k )
37 		ARCH="$target_cpu"
38+
39+		dnl ---Test for x32 ABI in x86_64
40+		if test "x$ARCH" = "xx86_64" ; then
41+			AC_EGREP_CPP(x32_test,
42+			[#if defined(__x86_64__) && defined (__ILP32__)
43+				x32_test
44+			#endif
45+			], SUBARCH='x32', SUBARCH='64')
46+		fi
47 		;;
48 	* )
49 		AC_MSG_ERROR([unsupported architecture $target_cpu])
50diff --git a/kexec/arch/x86_64/kexec-elf-rel-x86_64.c b/kexec/arch/x86_64/kexec-elf-rel-x86_64.c
51index 761a4ed..1c0e3f8 100644
52--- a/kexec/arch/x86_64/kexec-elf-rel-x86_64.c
53+++ b/kexec/arch/x86_64/kexec-elf-rel-x86_64.c
54@@ -8,7 +8,11 @@ int machine_verify_elf_rel(struct mem_ehdr *ehdr)
55 	if (ehdr->ei_data != ELFDATA2LSB) {
56 		return 0;
57 	}
58+#ifdef __ILP32__
59+	if (ehdr->ei_class != ELFCLASS32) {
60+#else
61 	if (ehdr->ei_class != ELFCLASS64) {
62+#endif
63 		return 0;
64 	}
65 	if (ehdr->e_machine != EM_X86_64) {
66diff --git a/kexec/kexec-syscall.h b/kexec/kexec-syscall.h
67index 2a3794d..3e67078 100644
68--- a/kexec/kexec-syscall.h
69+++ b/kexec/kexec-syscall.h
70@@ -31,8 +31,12 @@
71 #define __NR_kexec_load		268
72 #endif
73 #ifdef __x86_64__
74+#ifdef __ILP32__
75+#define __NR_kexec_load		528
76+#else
77 #define __NR_kexec_load		246
78 #endif
79+#endif
80 #ifdef __s390x__
81 #define __NR_kexec_load		277
82 #endif
83diff --git a/purgatory/arch/x86_64/Makefile b/purgatory/arch/x86_64/Makefile
84index 7300937..4af11e4 100644
85--- a/purgatory/arch/x86_64/Makefile
86+++ b/purgatory/arch/x86_64/Makefile
87@@ -23,4 +23,6 @@ x86_64_PURGATORY_SRCS += purgatory/arch/i386/console-x86.c
88 x86_64_PURGATORY_SRCS += purgatory/arch/i386/vga.c
89 x86_64_PURGATORY_SRCS += purgatory/arch/i386/pic.c
90
91-x86_64_PURGATORY_EXTRA_CFLAGS = -mcmodel=large
92+ifeq ($(SUBARCH),64)
93+        x86_64_PURGATORY_EXTRA_CFLAGS = -mcmodel=large
94+endif
95