From f38ada424e7d991a0121253ba1abc430b86a990b Mon Sep 17 00:00:00 2001
From: John Jolly <jjolly@suse.de>
Date: Wed, 22 Jan 2014 01:18:10 -0700
Subject: [PATCH 1/3]  - Changes made and files added in order to allow s390x
 build

---
 grub-core/kern/emu/cache_s.S |  1 +
 grub-core/kern/emu/lite.c    |  2 ++
 grub-core/kern/s390x/dl.c    | 37 +++++++++++++++++++++++++++++++++++
 grub-core/lib/s390x/setjmp.S | 46 ++++++++++++++++++++++++++++++++++++++++++++
 grub-core/lib/setjmp.S       |  2 ++
 include/grub/cache.h         |  2 +-
 include/grub/s390x/setjmp.h  | 29 ++++++++++++++++++++++++++++
 include/grub/s390x/time.h    | 27 ++++++++++++++++++++++++++
 include/grub/s390x/types.h   | 32 ++++++++++++++++++++++++++++++
 9 files changed, 177 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/kern/s390x/dl.c
 create mode 100644 grub-core/lib/s390x/setjmp.S
 create mode 100644 include/grub/s390x/setjmp.h
 create mode 100644 include/grub/s390x/time.h
 create mode 100644 include/grub/s390x/types.h

--- a/grub-core/kern/emu/cache_s.S
+++ b/grub-core/kern/emu/cache_s.S
@@ -15,6 +15,7 @@
 #include "../powerpc/cache.S"
 #elif defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
       defined(__mips__) || defined(__riscv)
+#elif defined(__s390x__)
 #else
 #error "No target cpu type is defined"
 #endif
--- a/grub-core/kern/emu/lite.c
+++ b/grub-core/kern/emu/lite.c
@@ -26,6 +26,8 @@
 #include "../arm64/dl.c"
 #elif defined(__riscv)
 #include "../riscv/dl.c"
+#elif defined(__s390x__)
+#include "../s390x/dl.c"
 #else
 #error "No target cpu type is defined"
 #endif
--- a/grub-core/kern/dl.c
+++ b/grub-core/kern/dl.c
@@ -230,7 +230,7 @@
   const Elf_Shdr *s;
   grub_size_t tsize = 0, talign = 1;
 #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
-  !defined (__loongarch__)
+  !defined (__loongarch__) && !defined (__s390x__)
   grub_size_t tramp;
   grub_size_t got;
   grub_err_t err;
@@ -247,7 +247,7 @@
     }
 
 #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
-  !defined (__loongarch__)
+  !defined (__loongarch__) && !defined (__s390x__)
   err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
   if (err)
     return err;
@@ -311,7 +311,7 @@
 	}
     }
 #if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
-  !defined (__loongarch__)
+  !defined (__loongarch__) && !defined (__s390x__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   mod->trampptr = ptr;
--- /dev/null
+++ b/grub-core/kern/s390x/dl.c
@@ -0,0 +1,40 @@
+/* dl.c - arch-dependent part of loadable module support */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2002,2004,2005,2007,2009  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+
+/* Check if EHDR is a valid ELF header.  */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+  (void)(ehdr);
+  return GRUB_ERR_BUG;
+}
+
+/* Relocate symbols.  */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+			       Elf_Shdr *s, grub_dl_segment_t seg)
+{
+  (void)(mod);
+  (void)(ehdr);
+  (void)(s);
+  (void)(seg);
+  return GRUB_ERR_BUG;
+}
--- /dev/null
+++ b/grub-core/lib/s390x/setjmp.S
@@ -0,0 +1,46 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2005,2007,2009  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+#include <grub/dl.h>
+
+        .file   "setjmp.S"
+
+GRUB_MOD_LICENSE "GPLv3+"
+
+        .text
+
+/*
+ * int grub_setjmp (grub_jmp_buf env)
+ */
+FUNCTION(grub_setjmp)
+	stmg	%r11,%r15,0(%r2)
+	lghi	%r2,0
+	br	%r14
+
+/*
+ * int grub_longjmp (grub_jmp_buf env, int val)
+ */
+FUNCTION(grub_longjmp)
+	chi	%r3,0
+	jne	.L2
+	lghi	%r3,1
+.L2:
+	lmg	%r11,%r15,0(%r2)
+	lgr	%r2,%r3
+	br	%r14
--- a/grub-core/lib/setjmp.S
+++ b/grub-core/lib/setjmp.S
@@ -23,6 +23,8 @@
 #include "./loongarch64/setjmp.S"
 #elif defined(__riscv)
 #include "./riscv/setjmp.S"
+#elif defined(__s390x__)
+#include "./s390x/setjmp.S"
 #else
 #error "Unknown target cpu type"
 #endif
--- a/include/grub/cache.h
+++ b/include/grub/cache.h
@@ -23,7 +23,7 @@
 #include <grub/symbol.h>
 #include <grub/types.h>
 
-#if defined (__i386__) || defined (__x86_64__)
+#if defined (__i386__) || defined (__x86_64__) || defined (__s390x__)
 static inline void
 grub_arch_sync_caches (void *address __attribute__ ((unused)),
 		       grub_size_t len __attribute__ ((unused)))
--- /dev/null
+++ b/include/grub/s390x/setjmp.h
@@ -0,0 +1,29 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2002,2004,2006,2007,2009  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_SETJMP_CPU_HEADER
+#define GRUB_SETJMP_CPU_HEADER	1
+
+#include <grub/types.h>
+
+typedef grub_uint64_t grub_jmp_buf[5];
+
+int grub_setjmp (grub_jmp_buf env) __attribute__ ((returns_twice));
+void grub_longjmp (grub_jmp_buf env, int val) __attribute__ ((noreturn));
+
+#endif /* ! GRUB_SETJMP_CPU_HEADER */
--- /dev/null
+++ b/include/grub/s390x/time.h
@@ -0,0 +1,27 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2007  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef KERNEL_CPU_TIME_HEADER
+#define KERNEL_CPU_TIME_HEADER	1
+
+static __inline void
+grub_cpu_idle (void)
+{
+}
+
+#endif /* ! KERNEL_CPU_TIME_HEADER */
--- /dev/null
+++ b/include/grub/s390x/types.h
@@ -0,0 +1,32 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2002,2004,2006,2007  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_TYPES_CPU_HEADER
+#define GRUB_TYPES_CPU_HEADER	1
+
+/* The size of void *.  */
+#define GRUB_TARGET_SIZEOF_VOID_P	8
+
+/* The size of long.  */
+#define GRUB_TARGET_SIZEOF_LONG		8
+
+/* s390x is big-endian.  */
+#define GRUB_TARGET_WORDS_BIGENDIAN	1
+
+
+#endif /* ! GRUB_TYPES_CPU_HEADER */
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -407,6 +407,9 @@
   extra_dist = kern/i386/realmode.S;
   extra_dist = boot/i386/pc/lzma_decode.S;
   extra_dist = kern/mips/cache_flush.S;
+
+  extra_dist = kern/s390x/dl.c;
+  extra_dist = lib/s390x/setjmp.S;
 };
 
 program = {
