[PATCH v4 0/8] Add support for LoongArch

2022-07-01 Thread Xiaotian Wu
LoongArch is a new Loongson 3A5000 CPU instruction set, you can read
documents[1] or visit the development community[2] to get more information.

[1]: https://loongson.github.io/LoongArch-Documentation/README-EN.html
[2]: https://github.com/loongson

This patch series will add the basic support for LoongArch architecture, it can
compile on LoongArch and display the GRUB UI.

The full LoongArch patch has two parts, this is the first part.
The second part needs to wait for the LoadFile2 patch to be merged upstream 
first.

Please review the patches, thank you.

v1->v2:
- rebase on the last commit.
- fix some errors.
- change the year to 2022 for the new files.

v2->v3:
- based on the last commit.
- add a comment to setjmp.S
- complete LoongArch support for grub-mkrescue

v3->v4:
- based on the last commit.

Xiaotian Wu (8):
  PE: Add LoongArch definitions
  Add LoongArch definitions
  LoongArch: Add setjmp implementation
  LoongArch: Add early startup code
  LoongArch: Add stubs for Linux loading commands
  LoongArch: Add awareness for LoongArch relocations
  LoongArch: Add auxiliary files
  LoongArch: Add to build system

 Makefile.util.def|   1 +
 conf/Makefile.common |   3 +
 configure.ac |   5 +
 gentpl.py|  25 +--
 grub-core/Makefile.am|   6 +
 grub-core/Makefile.core.def  |  15 ++
 grub-core/kern/dl.c  |   9 +-
 grub-core/kern/efi/mm.c  |   3 +-
 grub-core/kern/loongarch64/cache.c   |  39 +
 grub-core/kern/loongarch64/cache_flush.S |  33 
 grub-core/kern/loongarch64/dl.c  | 102 
 grub-core/kern/loongarch64/dl_helper.c   | 198 +++
 grub-core/kern/loongarch64/efi/init.c|  77 +
 grub-core/kern/loongarch64/efi/startup.S |  34 
 grub-core/lib/efi/halt.c |   2 +-
 grub-core/lib/loongarch64/setjmp.S   |  69 
 grub-core/lib/setjmp.S   |   2 +
 grub-core/loader/loongarch64/linux.c |  59 +++
 include/grub/dl.h|   1 +
 include/grub/efi/api.h   |   2 +-
 include/grub/efi/efi.h   |   2 +-
 include/grub/efi/pe32.h  |  36 +++--
 include/grub/elf.h   |  23 +++
 include/grub/loongarch64/efi/memory.h|  24 +++
 include/grub/loongarch64/linux.h |  31 
 include/grub/loongarch64/reloc.h | 107 
 include/grub/loongarch64/setjmp.h|  27 
 include/grub/loongarch64/time.h  |  28 
 include/grub/loongarch64/types.h |  34 
 include/grub/util/install.h  |   1 +
 util/grub-install-common.c   |  49 +++---
 util/grub-install.c  |  16 ++
 util/grub-mkimagexx.c|  79 +
 util/grub-mknetdir.c |   1 +
 util/grub-mkrescue.c |   8 +
 util/grub-module-verifier.c  |  26 +++
 util/mkimage.c   |  16 ++
 37 files changed, 1134 insertions(+), 59 deletions(-)
 create mode 100644 grub-core/kern/loongarch64/cache.c
 create mode 100644 grub-core/kern/loongarch64/cache_flush.S
 create mode 100644 grub-core/kern/loongarch64/dl.c
 create mode 100644 grub-core/kern/loongarch64/dl_helper.c
 create mode 100644 grub-core/kern/loongarch64/efi/init.c
 create mode 100644 grub-core/kern/loongarch64/efi/startup.S
 create mode 100644 grub-core/lib/loongarch64/setjmp.S
 create mode 100644 grub-core/loader/loongarch64/linux.c
 create mode 100644 include/grub/loongarch64/efi/memory.h
 create mode 100644 include/grub/loongarch64/linux.h
 create mode 100644 include/grub/loongarch64/reloc.h
 create mode 100644 include/grub/loongarch64/setjmp.h
 create mode 100644 include/grub/loongarch64/time.h
 create mode 100644 include/grub/loongarch64/types.h

-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v4 5/8] LoongArch: Add stubs for Linux loading commands

2022-07-01 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
---
 grub-core/loader/loongarch64/linux.c | 59 
 include/grub/loongarch64/linux.h | 31 +++
 2 files changed, 90 insertions(+)
 create mode 100644 grub-core/loader/loongarch64/linux.c
 create mode 100644 include/grub/loongarch64/linux.h

diff --git a/grub-core/loader/loongarch64/linux.c 
b/grub-core/loader/loongarch64/linux.c
new file mode 100644
index 0..d9d39a20c
--- /dev/null
+++ b/grub-core/loader/loongarch64/linux.c
@@ -0,0 +1,59 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 .
+ */
+
+#include 
+#include 
+#include 
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_err_t
+grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
+int argc __attribute__ ((unused)),
+char *argv[] __attribute__ ((unused)))
+{
+  grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, N_("Linux not supported yet"));
+
+  return grub_errno;
+}
+
+static grub_err_t
+grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
+   int argc __attribute__ ((unused)),
+   char *argv[] __attribute__ ((unused)))
+{
+  grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, N_("Linux not supported yet"));
+
+  return grub_errno;
+}
+
+static grub_command_t cmd_linux, cmd_initrd;
+
+GRUB_MOD_INIT (linux)
+{
+  cmd_linux = grub_register_command ("linux", grub_cmd_linux,
+N_("FILE [ARGS...]"), N_("Load Linux."));
+  cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
+ N_("FILE"), N_("Load initrd."));
+}
+
+GRUB_MOD_FINI (linux)
+{
+  grub_unregister_command (cmd_linux);
+  grub_unregister_command (cmd_initrd);
+}
diff --git a/include/grub/loongarch64/linux.h b/include/grub/loongarch64/linux.h
new file mode 100644
index 0..76fe693f4
--- /dev/null
+++ b/include/grub/loongarch64/linux.h
@@ -0,0 +1,31 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 .
+ */
+
+#ifndef GRUB_LOONGARCH64_LINUX_HEADER
+#define GRUB_LOONGARCH64_LINUX_HEADER 1
+
+struct linux_loongarch64_kernel_header
+{
+  /*
+   * TODO
+   */
+};
+
+#define linux_arch_kernel_header linux_loongarch64_kernel_header
+
+#endif /* ! GRUB_LOONGARCH64_LINUX_HEADER */
-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v4 3/8] LoongArch: Add setjmp implementation

2022-07-01 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
Signed-off-by: Sun Haiyong 
---
 grub-core/lib/loongarch64/setjmp.S | 69 ++
 grub-core/lib/setjmp.S |  2 +
 include/grub/loongarch64/setjmp.h  | 27 
 3 files changed, 98 insertions(+)
 create mode 100644 grub-core/lib/loongarch64/setjmp.S
 create mode 100644 include/grub/loongarch64/setjmp.h

diff --git a/grub-core/lib/loongarch64/setjmp.S 
b/grub-core/lib/loongarch64/setjmp.S
new file mode 100644
index 0..41d58f569
--- /dev/null
+++ b/grub-core/lib/loongarch64/setjmp.S
@@ -0,0 +1,69 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 .
+ */
+
+#include 
+
+   .file   "setjmp.S"
+
+GRUB_MOD_LICENSE "GPLv3+"
+
+   .text
+
+/*
+ * int grub_setjmp (jmp_buf env)
+ */
+FUNCTION(grub_setjmp)
+   st.d $s0, $a0, 0x0
+   st.d $s1, $a0, 0x8
+   st.d $s2, $a0, 0x10
+   st.d $s3, $a0, 0x18
+   st.d $s4, $a0, 0x20
+   st.d $s5, $a0, 0x28
+   st.d $s6, $a0, 0x30
+   st.d $s7, $a0, 0x38
+   st.d $s8, $a0, 0x40
+   st.d $fp, $a0, 0x48
+   st.d $sp, $a0, 0x50
+   st.d $ra, $a0, 0x58
+
+   move $a0, $zero
+   jr   $ra
+
+/*
+ * void grub_longjmp (jmp_buf env, int val)
+ */
+FUNCTION(grub_longjmp)
+   ld.d $s0, $a0, 0x0
+   ld.d $s1, $a0, 0x8
+   ld.d $s2, $a0, 0x10
+   ld.d $s3, $a0, 0x18
+   ld.d $s4, $a0, 0x20
+   ld.d $s5, $a0, 0x28
+   ld.d $s6, $a0, 0x30
+   ld.d $s7, $a0, 0x38
+   ld.d $s8, $a0, 0x40
+   ld.d $fp, $a0, 0x48
+   ld.d $sp, $a0, 0x50
+   ld.d $ra, $a0, 0x58
+
+   /* Return 1 if passed 0, otherwise returns the value in place. */
+   li.w $a0, 1
+   beqz $a1, 1f
+   move $a0, $a1
+1:
+   jr   $ra
diff --git a/grub-core/lib/setjmp.S b/grub-core/lib/setjmp.S
index 9c8721088..cba1d546d 100644
--- a/grub-core/lib/setjmp.S
+++ b/grub-core/lib/setjmp.S
@@ -19,6 +19,8 @@
 #include "./arm/setjmp.S"
 #elif defined(__aarch64__)
 #include "./arm64/setjmp.S"
+#elif defined(__loongarch64)
+#include "./loongarch64/setjmp.S"
 #elif defined(__riscv)
 #include "./riscv/setjmp.S"
 #else
diff --git a/include/grub/loongarch64/setjmp.h 
b/include/grub/loongarch64/setjmp.h
new file mode 100644
index 0..cb3e17763
--- /dev/null
+++ b/include/grub/loongarch64/setjmp.h
@@ -0,0 +1,27 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 .
+ */
+
+#ifndef GRUB_SETJMP_CPU_HEADER
+#define GRUB_SETJMP_CPU_HEADER 1
+
+typedef grub_uint64_t grub_jmp_buf[12];
+
+int grub_setjmp (grub_jmp_buf env) RETURNS_TWICE;
+void grub_longjmp (grub_jmp_buf env, int val) __attribute__ ((noreturn));
+
+#endif /* ! GRUB_SETJMP_CPU_HEADER */
-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v4 7/8] LoongArch: Add auxiliary files

2022-07-01 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/efi/mm.c  |   3 +-
 grub-core/kern/loongarch64/cache.c   |  39 +
 grub-core/kern/loongarch64/cache_flush.S |  33 +++
 grub-core/kern/loongarch64/efi/init.c|  77 
 grub-core/lib/efi/halt.c |   2 +-
 include/grub/efi/efi.h   |   2 +-
 include/grub/loongarch64/efi/memory.h|  24 +
 include/grub/loongarch64/reloc.h | 107 +++
 include/grub/loongarch64/time.h  |  28 ++
 include/grub/loongarch64/types.h |  34 +++
 10 files changed, 346 insertions(+), 3 deletions(-)
 create mode 100644 grub-core/kern/loongarch64/cache.c
 create mode 100644 grub-core/kern/loongarch64/cache_flush.S
 create mode 100644 grub-core/kern/loongarch64/efi/init.c
 create mode 100644 include/grub/loongarch64/efi/memory.h
 create mode 100644 include/grub/loongarch64/reloc.h
 create mode 100644 include/grub/loongarch64/time.h
 create mode 100644 include/grub/loongarch64/types.h

diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
index d8e411454..1dce99882 100644
--- a/grub-core/kern/efi/mm.c
+++ b/grub-core/kern/efi/mm.c
@@ -659,7 +659,8 @@ grub_efi_mm_init (void)
   2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
 }
 
-#if defined (__aarch64__) || defined (__arm__) || defined (__riscv)
+#if defined (__aarch64__) || defined (__arm__) || defined (__riscv) || \
+  defined (__loongarch__)
 grub_err_t
 grub_efi_get_ram_base(grub_addr_t *base_addr)
 {
diff --git a/grub-core/kern/loongarch64/cache.c 
b/grub-core/kern/loongarch64/cache.c
new file mode 100644
index 0..43d314df9
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache.c
@@ -0,0 +1,39 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 .
+ */
+
+#include 
+#include 
+
+/* Prototypes for asm functions. */
+void grub_arch_clean_dcache_range (void);
+void grub_arch_invalidate_icache_range (void);
+
+void
+grub_arch_sync_caches (void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  grub_arch_clean_dcache_range ();
+  grub_arch_invalidate_icache_range ();
+}
+
+void
+grub_arch_sync_dma_caches (volatile void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  /* DMA non-coherent devices not supported yet */
+}
diff --git a/grub-core/kern/loongarch64/cache_flush.S 
b/grub-core/kern/loongarch64/cache_flush.S
new file mode 100644
index 0..43b97d822
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache_flush.S
@@ -0,0 +1,33 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 .
+ */
+
+#include 
+
+   .file   "cache_flush.S"
+   .text
+/*
+ * No further work to do because cache consistency is maintained by hardware on
+ * LoongArch.
+ */
+FUNCTION(grub_arch_clean_dcache_range)
+   dbar 0
+   jr $ra
+
+FUNCTION(grub_arch_invalidate_icache_range)
+   ibar 0
+   jr $ra
diff --git a/grub-core/kern/loongarch64/efi/init.c 
b/grub-core/kern/loongarch64/efi/init.c
new file mode 100644
index 0..8cbeafaba
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/init.c
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 

[PATCH v4 4/8] LoongArch: Add early startup code

2022-07-01 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/loongarch64/efi/startup.S | 34 
 1 file changed, 34 insertions(+)
 create mode 100644 grub-core/kern/loongarch64/efi/startup.S

diff --git a/grub-core/kern/loongarch64/efi/startup.S 
b/grub-core/kern/loongarch64/efi/startup.S
new file mode 100644
index 0..fc8123f8c
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/startup.S
@@ -0,0 +1,34 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 .
+ */
+
+#include 
+
+   .file   "startup.S"
+   .text
+
+FUNCTION(_start)
+   /*
+*  EFI_SYSTEM_TABLE and EFI_HANDLE are passed in $a1/$a0.
+*/
+
+   la  $a2, EXT_C(grub_efi_image_handle)
+   st.d$a0, $a2, 0
+   la  $a2, EXT_C(grub_efi_system_table)
+   st.d$a1, $a2, 0
+
+   b   EXT_C(grub_main)
-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v4 1/8] PE: Add LoongArch definitions

2022-07-01 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 include/grub/efi/pe32.h | 36 
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h
index 0ed8781f0..de56edef6 100644
--- a/include/grub/efi/pe32.h
+++ b/include/grub/efi/pe32.h
@@ -77,6 +77,8 @@ struct grub_pe32_coff_header
 #define GRUB_PE32_MACHINE_X86_64   0x8664
 #define GRUB_PE32_MACHINE_ARMTHUMB_MIXED   0x01c2
 #define GRUB_PE32_MACHINE_ARM640xAA64
+#define GRUB_PE32_MACHINE_LOONGARCH32  0x6232
+#define GRUB_PE32_MACHINE_LOONGARCH64  0x6264
 #define GRUB_PE32_MACHINE_RISCV32  0x5032
 #define GRUB_PE32_MACHINE_RISCV64  0x5064
 
@@ -283,22 +285,24 @@ struct grub_pe32_fixup_block
 
 #define GRUB_PE32_FIXUP_ENTRY(type, offset)(((type) << 12) | (offset))
 
-#define GRUB_PE32_REL_BASED_ABSOLUTE   0
-#define GRUB_PE32_REL_BASED_HIGH   1
-#define GRUB_PE32_REL_BASED_LOW2
-#define GRUB_PE32_REL_BASED_HIGHLOW3
-#define GRUB_PE32_REL_BASED_HIGHADJ4
-#define GRUB_PE32_REL_BASED_MIPS_JMPADDR 5
-#define GRUB_PE32_REL_BASED_ARM_MOV32A  5
-#define GRUB_PE32_REL_BASED_RISCV_HI20 5
-#define GRUB_PE32_REL_BASED_SECTION6
-#define GRUB_PE32_REL_BASED_REL7
-#define GRUB_PE32_REL_BASED_ARM_MOV32T  7
-#define GRUB_PE32_REL_BASED_RISCV_LOW12I 7
-#define GRUB_PE32_REL_BASED_RISCV_LOW12S 8
-#define GRUB_PE32_REL_BASED_IA64_IMM64 9
-#define GRUB_PE32_REL_BASED_DIR64  10
-#define GRUB_PE32_REL_BASED_HIGH3ADJ   11
+#define GRUB_PE32_REL_BASED_ABSOLUTE   0
+#define GRUB_PE32_REL_BASED_HIGH   1
+#define GRUB_PE32_REL_BASED_LOW2
+#define GRUB_PE32_REL_BASED_HIGHLOW3
+#define GRUB_PE32_REL_BASED_HIGHADJ4
+#define GRUB_PE32_REL_BASED_MIPS_JMPADDR   5
+#define GRUB_PE32_REL_BASED_ARM_MOV32A 5
+#define GRUB_PE32_REL_BASED_RISCV_HI20 5
+#define GRUB_PE32_REL_BASED_SECTION6
+#define GRUB_PE32_REL_BASED_REL7
+#define GRUB_PE32_REL_BASED_ARM_MOV32T 7
+#define GRUB_PE32_REL_BASED_RISCV_LOW12I   7
+#define GRUB_PE32_REL_BASED_RISCV_LOW12S   8
+#define GRUB_PE32_REL_BASED_LOONGARCH32_MARK_LA8
+#define GRUB_PE32_REL_BASED_LOONGARCH64_MARK_LA8
+#define GRUB_PE32_REL_BASED_IA64_IMM64 9
+#define GRUB_PE32_REL_BASED_DIR64  10
+#define GRUB_PE32_REL_BASED_HIGH3ADJ   11
 
 struct grub_pe32_symbol
 {
-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v4 2/8] Add LoongArch definitions

2022-07-01 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 include/grub/elf.h | 23 +++
 1 file changed, 23 insertions(+)

diff --git a/include/grub/elf.h b/include/grub/elf.h
index c478933ee..1c8d4f5d5 100644
--- a/include/grub/elf.h
+++ b/include/grub/elf.h
@@ -248,6 +248,7 @@ typedef struct
 #define EM_NUM 95
 #define EM_AARCH64 183 /* ARM 64-bit architecture */
 #define EM_RISCV   243 /* RISC-V */
+#define EM_LOONGARCH   258 /* LoongArch */
 
 /* If it is necessary to assign new unofficial EM_* values, please
pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the
@@ -2531,6 +2532,28 @@ typedef Elf32_Addr Elf32_Conflict;
 #define R_RISCV_SET32   56
 #define R_RISCV_32_PCREL57
 
+/* LoongArch relocations */
+#define R_LARCH_NONE 0
+#define R_LARCH_64   2
+#define R_LARCH_MARK_LA  20
+#define R_LARCH_SOP_PUSH_PCREL   22
+#define R_LARCH_SOP_PUSH_ABSOLUTE23
+#define R_LARCH_SOP_PUSH_PLT_PCREL   29
+#define R_LARCH_SOP_SUB  32
+#define R_LARCH_SOP_SL   33
+#define R_LARCH_SOP_SR   34
+#define R_LARCH_SOP_ADD  35
+#define R_LARCH_SOP_AND  36
+#define R_LARCH_SOP_IF_ELSE  37
+#define R_LARCH_SOP_POP_32_S_10_538
+#define R_LARCH_SOP_POP_32_U_10_12   39
+#define R_LARCH_SOP_POP_32_S_10_12   40
+#define R_LARCH_SOP_POP_32_S_10_16   41
+#define R_LARCH_SOP_POP_32_S_10_16_S242
+#define R_LARCH_SOP_POP_32_S_5_2043
+#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
+#define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+
 #ifdef GRUB_TARGET_WORDSIZE
 #if GRUB_TARGET_WORDSIZE == 32
 
-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v4 6/8] LoongArch: Add awareness for LoongArch relocations

2022-07-01 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/dl.c|   9 +-
 grub-core/kern/loongarch64/dl.c| 102 +
 grub-core/kern/loongarch64/dl_helper.c | 198 +
 include/grub/dl.h  |   1 +
 util/grub-mkimagexx.c  |  79 ++
 util/grub-module-verifier.c|  26 
 6 files changed, 412 insertions(+), 3 deletions(-)
 create mode 100644 grub-core/kern/loongarch64/dl.c
 create mode 100644 grub-core/kern/loongarch64/dl_helper.c

diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
index b1d3a103e..41b5bba00 100644
--- a/grub-core/kern/dl.c
+++ b/grub-core/kern/dl.c
@@ -225,7 +225,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
   unsigned i;
   const Elf_Shdr *s;
   grub_size_t tsize = 0, talign = 1;
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   grub_size_t tramp;
   grub_size_t got;
   grub_err_t err;
@@ -241,7 +242,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
talign = s->sh_addralign;
 }
 
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
   if (err)
 return err;
@@ -304,7 +306,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
  mod->segment = seg;
}
 }
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   mod->trampptr = ptr;
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
new file mode 100644
index 0..3a6aa91cd
--- /dev/null
+++ b/grub-core/kern/loongarch64/dl.c
@@ -0,0 +1,102 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 .
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Check if EHDR is a valid ELF header.  */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+  Elf_Ehdr *e = ehdr;
+
+  /* Check the magic numbers.  */
+  if (e->e_ident[EI_CLASS] != ELFCLASS64
+  || e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_LOONGARCH)
+return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF 
magic"));
+
+  return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/*
+ * Unified function for both REL and RELA.
+ */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+  Elf_Shdr *s, grub_dl_segment_t seg)
+{
+  Elf_Rel *rel, *max;
+  struct grub_loongarch64_stack stack;
+  grub_loongarch64_stack_init (&stack);
+
+  for (rel = (Elf_Rel *) ((char *) ehdr + s->sh_offset),
+max = (Elf_Rel *) ((char *) rel + s->sh_size);
+   rel < max;
+   rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
+{
+  Elf_Sym *sym;
+  grub_uint64_t *place;
+  grub_uint64_t sym_addr;
+
+  if (rel->r_offset >= seg->size)
+   return grub_error (GRUB_ERR_BAD_MODULE,
+  "reloc offset is outside the segment");
+
+  sym = (Elf_Sym *) ((char*)mod->symtab
++ mod->symsize * ELF_R_SYM (rel->r_info));
+
+  sym_addr = sym->st_value;
+  if (s->sh_type == SHT_RELA)
+   sym_addr += ((Elf_Rela *) rel)->r_addend;
+
+  place = (grub_uint64_t *) ((grub_addr_t)seg->addr + rel->r_offset);
+
+  switch (ELF_R_TYPE (rel->r_info))
+   {
+   case R_LARCH_64:
+ *place = sym_addr;
+ break;
+   case R_LARCH_MARK_LA:
+ break;
+   case R_LARCH_SOP_PUSH_PCREL:
+   case R_LARCH_SOP_PUSH_PLT_PCREL:
+ grub_loongarch64_sop_push (&stack, sym_addr - (grub_uint64_t)place);
+ break;
+   GRUB_LOONGARCH64_RELOCATION (&stack, place, sym_addr)
+   default:
+ {
+   char rel_info[17]; /* log16(2^64) = 16, plus NUL.  */
+
+   grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" 
PRIxGRUB

[PATCH v4 8/8] LoongArch: Add to build system

2022-07-01 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 Makefile.util.def   |  1 +
 conf/Makefile.common|  3 +++
 configure.ac|  5 
 gentpl.py   | 25 ++-
 grub-core/Makefile.am   |  6 +
 grub-core/Makefile.core.def | 15 
 include/grub/efi/api.h  |  2 +-
 include/grub/util/install.h |  1 +
 util/grub-install-common.c  | 49 +++--
 util/grub-install.c | 16 
 util/grub-mknetdir.c|  1 +
 util/grub-mkrescue.c|  8 ++
 util/mkimage.c  | 16 
 13 files changed, 111 insertions(+), 37 deletions(-)

diff --git a/Makefile.util.def b/Makefile.util.def
index d919c562c..765c6fea2 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -163,6 +163,7 @@ library = {
   common = grub-core/kern/ia64/dl_helper.c;
   common = grub-core/kern/arm/dl_helper.c;
   common = grub-core/kern/arm64/dl_helper.c;
+  common = grub-core/kern/loongarch64/dl_helper.c;
   common = grub-core/lib/minilzo/minilzo.c;
   common = grub-core/lib/xzembed/xz_dec_bcj.c;
   common = grub-core/lib/xzembed/xz_dec_lzma2.c;
diff --git a/conf/Makefile.common b/conf/Makefile.common
index 2d8f1bf2e..fad6d5ae0 100644
--- a/conf/Makefile.common
+++ b/conf/Makefile.common
@@ -17,6 +17,9 @@ endif
 if COND_arm64
   CFLAGS_PLATFORM += -mcmodel=large
 endif
+if COND_loongarch64
+  CPPFLAGS_PLATFORM = -Wa,-mla-global-with-abs -mcmodel=large
+endif
 if COND_powerpc_ieee1275
   CFLAGS_PLATFORM += -mcpu=powerpc
 endif
diff --git a/configure.ac b/configure.ac
index 57fb70945..c2340c8fc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -115,6 +115,7 @@ case "$target_cpu" in
;;
   arm*)target_cpu=arm ;;
   aarch64*)target_cpu=arm64 ;;
+  loongarch64) target_cpu=loongarch64 ;;
   riscv32*)target_cpu=riscv32 ;;
   riscv64*)target_cpu=riscv64 ;;
 esac
@@ -140,6 +141,7 @@ if test "x$with_platform" = x; then
 ia64-*) platform=efi ;;
 arm-*) platform=uboot ;;
 arm64-*) platform=efi ;;
+loongarch64-*) platform=efi;;
 riscv32-*) platform=efi ;;
 riscv64-*) platform=efi ;;
 *)
@@ -190,6 +192,7 @@ case "$target_cpu"-"$platform" in
   arm-coreboot) ;;
   arm-efi) ;;
   arm64-efi) ;;
+  loongarch64-efi) ;;
   riscv32-efi) ;;
   riscv64-efi) ;;
   *-emu) ;;
@@ -1987,6 +1990,8 @@ AM_CONDITIONAL([COND_i386_coreboot], [test x$target_cpu = 
xi386 -a x$platform =
 AM_CONDITIONAL([COND_i386_multiboot], [test x$target_cpu = xi386 -a x$platform 
= xmultiboot])
 AM_CONDITIONAL([COND_i386_xen], [test x$target_cpu = xi386 -a x$platform = 
xxen])
 AM_CONDITIONAL([COND_i386_xen_pvh], [test x$target_cpu = xi386 -a x$platform = 
xxen_pvh])
+AM_CONDITIONAL([COND_loongarch64], [test x$target_cpu = xloongarch64])
+AM_CONDITIONAL([COND_loongarch64_efi], [test x$target_cpu = xloongarch64 -a 
x$platform = xefi])
 AM_CONDITIONAL([COND_mips], [test x$target_cpu = xmips -o x$target_cpu = 
xmipsel])
 AM_CONDITIONAL([COND_mips_arc], [test "(" x$target_cpu = xmips -o x$target_cpu 
= xmipsel ")"  -a x$platform = xarc])
 AM_CONDITIONAL([COND_mips_loongson], [test x$target_cpu = xmipsel -a 
x$platform = xloongson])
diff --git a/gentpl.py b/gentpl.py
index 9f51e4fb6..fa53e17e8 100644
--- a/gentpl.py
+++ b/gentpl.py
@@ -32,27 +32,28 @@ GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", 
"i386_qemu", "i386_coreboot",
"mips_loongson", "sparc64_ieee1275",
"powerpc_ieee1275", "mips_arc", "ia64_efi",
"mips_qemu_mips", "arm_uboot", "arm_efi", "arm64_efi",
-   "arm_coreboot", "riscv32_efi", "riscv64_efi" ]
+   "arm_coreboot", "loongarch64_efi", "riscv32_efi", 
"riscv64_efi" ]
 
 GROUPS = {}
 
 GROUPS["common"]   = GRUB_PLATFORMS[:]
 
 # Groups based on CPU
-GROUPS["i386"] = [ "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot", 
"i386_multiboot", "i386_ieee1275" ]
-GROUPS["x86_64"]   = [ "x86_64_efi" ]
-GROUPS["x86"]  = GROUPS["i386"] + GROUPS["x86_64"]
-GROUPS["mips"] = [ "mips_loongson", "mips_qemu_mips", "mips_arc" ]
-GROUPS["sparc64"]  = [ "sparc64_ieee1275" ]
-GROUPS["powerpc"]  = [ "powerpc_ieee1275" ]
-GROUPS["arm"]  = [ "arm_uboot", "arm_efi", "arm_coreboot" ]
-GROUPS["arm64"]= [ "arm64_efi" ]
-GROUPS["riscv32"]  = [ "riscv32_efi" ]
-GROUPS["riscv64"]  = [ "riscv64_efi" ]
+GROUPS["i386"]= [ "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot", 
"i386_multiboot", "i386_ieee1275" ]
+GROUPS["x86_64"]  = [ "x86_64_efi" ]
+GROUPS["x86"] = GROUPS["i386"] + GROUPS["x86_64"]
+GROUPS["mips"]= [ "mips_loongson", "mips_qemu_mips", "mips_arc" ]
+GROUPS["sparc64"] = [ "sparc64_ieee1275" ]
+GROUPS["powerpc"] = [ "powerpc_ieee1275" ]
+GROUPS["arm"] = [ "arm_uboot", "arm_efi", "arm_coreboot" ]
+GROUPS["arm64"]   = [ "arm64_efi" ]
+GROUPS["loongarch64"] = [ "loongarch64_efi" ]
+GROUPS["riscv32"] = [ "riscv32_efi" ]
+GROU

Re: GRUB 2.12 release, etc.

2022-07-01 Thread Xiaotian Wu


在 2022/6/30 下午9:12, Daniel Kiper 写道:

On Tue, Jun 14, 2022 at 11:30:47AM -0500, Glenn Washburn wrote:

On Tue, 14 Jun 2022 17:48:39 +0800
Xiaotian Wu  wrote:


Hi Daniel.

Thank you for your release plan.

I would like to ask, can GRUB-2.12 add a new LoongArch architecture support?

LoongArch is a RISC instruction set from China. Currently, Linux-5.19
adds LoongArch CPU support and will support complete EFIStub in version
5.20.

On GRUB, including 2 patch sets:

1. Add the basic support of LoongArch so that GRUB can run on the
LoongArch machine. See
https://github.com/loongarch64/grub/tree/dev/patchwork/base

2. Add the loader for kernel, but it depends on a series of patch of
Loadfile2. See https://github.com/loongarch64/grub/tree/dev/patchwork/efi

These patchs are already available, and tested on CLFS and Archlinux for
LoongArch.

If we plan to support LoongArch in GRUB-2.12, I will update these patchs
and release the 4th version of the patch for review.

I think it would be great to have LoongArch in GRUB. It should be a hard
requirement that support for the architecture be added for the make
check tests. This will require that QEMU have support for LoongArch. It
looks like the current release of QEMU does not have support for
LoongArch, but there is a patch series[1] which adds it. I presume that

What is the status of this patch series right now? If it is in or close
by I would ask for at least minimal tests for the LoongArch support in
the GRUB.


These patchs can be worked, and I just sent the V4 version of the patch 
to the email list.


About the QEMU, we have maintained a branch[1] to generate 
qemu-system-loongarch64, but there are still some jobs that are not 
completed. The expected result is to add it to QEMU-7.1.


I have added related tests in the patch of the second part[2], but 
because the QEMU work is not completed, it cannot pass the all testcase, 
although it works well on the physical machine.


[1] https://github.com/loongson/qemu/tree/tcg-full-system

[2] 
https://github.com/loongarch64/grub/commit/fe92204a681d9d550b724bd9fdc31971eebc77e0




will get accepted at some point. I'd suggest the tests be made assuming
that (or some other patch LoongArch patch series) will get accepted.
The INSTALL file can be updated to indicate that QEMU > 7 is required,
or something like that. Also, if there's any special build requirements
those should also be noted in the INSTALL file and docs/grub*.texi
should be updated for the new arch. For example, what's the minimum GCC
version needed to compile?

Yeah, that would be nice if INSTALL file is updated too.

Daniel

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

--
*武校田*
*通用事业部*

北京市海淀区中关村环保科技示范园龙芯产业园2号楼 100095
电话: +86 (10) 62546668-1724
传真: +86 (10) 62600826
手机: +86 13120280005
___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: GRUB 2.12 release, etc.

2022-07-01 Thread Glenn Washburn
On Fri, 1 Jul 2022 18:26:05 +0800
Xiaotian Wu  wrote:

> 
> 在 2022/6/30 下午9:12, Daniel Kiper 写道:
> > On Tue, Jun 14, 2022 at 11:30:47AM -0500, Glenn Washburn wrote:
> >> On Tue, 14 Jun 2022 17:48:39 +0800
> >> Xiaotian Wu  wrote:
> >>
> >>> Hi Daniel.
> >>>
> >>> Thank you for your release plan.
> >>>
> >>> I would like to ask, can GRUB-2.12 add a new LoongArch architecture 
> >>> support?
> >>>
> >>> LoongArch is a RISC instruction set from China. Currently, Linux-5.19
> >>> adds LoongArch CPU support and will support complete EFIStub in version
> >>> 5.20.
> >>>
> >>> On GRUB, including 2 patch sets:
> >>>
> >>> 1. Add the basic support of LoongArch so that GRUB can run on the
> >>> LoongArch machine. See
> >>> https://github.com/loongarch64/grub/tree/dev/patchwork/base
> >>>
> >>> 2. Add the loader for kernel, but it depends on a series of patch of
> >>> Loadfile2. See https://github.com/loongarch64/grub/tree/dev/patchwork/efi
> >>>
> >>> These patchs are already available, and tested on CLFS and Archlinux for
> >>> LoongArch.
> >>>
> >>> If we plan to support LoongArch in GRUB-2.12, I will update these patchs
> >>> and release the 4th version of the patch for review.
> >> I think it would be great to have LoongArch in GRUB. It should be a hard
> >> requirement that support for the architecture be added for the make
> >> check tests. This will require that QEMU have support for LoongArch. It
> >> looks like the current release of QEMU does not have support for
> >> LoongArch, but there is a patch series[1] which adds it. I presume that
> > What is the status of this patch series right now? If it is in or close
> > by I would ask for at least minimal tests for the LoongArch support in
> > the GRUB.
> 
> These patchs can be worked, and I just sent the V4 version of the patch 
> to the email list.
> 
> About the QEMU, we have maintained a branch[1] to generate 
> qemu-system-loongarch64, but there are still some jobs that are not 
> completed. The expected result is to add it to QEMU-7.1.
> 
> I have added related tests in the patch of the second part[2], but 

Great, this is what I was looking for. Especially important is adding
support for LoongArch via QEMU in grub-shell, which that commit appears
to do.

> because the QEMU work is not completed, it cannot pass the all testcase, 
> although it works well on the physical machine.

I would like something like that commit mentioned to be included with
the series adding support for LoongArch. The GRUB tests that you don't
plan on having support in QEMU to pass anytime soon should be skipped
(as it looks like has been done from that commit). For tests that need
support that is currently lacking in QEMU but is planned to be added
soon, I'm fine with them failing. It would be nice if this situation
could be detected (perhaps QEMU version check?) and a hard error (exit
99) returned, rather than a normal failure. But if its a big pain, is
not super important for me.

Glenn

> [1] https://github.com/loongson/qemu/tree/tcg-full-system
> 
> [2] 
> https://github.com/loongarch64/grub/commit/fe92204a681d9d550b724bd9fdc31971eebc77e0
> 
> 
> >> will get accepted at some point. I'd suggest the tests be made assuming
> >> that (or some other patch LoongArch patch series) will get accepted.
> >> The INSTALL file can be updated to indicate that QEMU > 7 is required,
> >> or something like that. Also, if there's any special build requirements
> >> those should also be noted in the INSTALL file and docs/grub*.texi
> >> should be updated for the new arch. For example, what's the minimum GCC
> >> version needed to compile?
> > Yeah, that would be nice if INSTALL file is updated too.
> >
> > Daniel
> >
> > ___
> > Grub-devel mailing list
> > Grub-devel@gnu.org
> > https://lists.gnu.org/mailman/listinfo/grub-devel

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel