All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/8] Add support for LoongArch
@ 2022-02-23 13:46 Xiaotian Wu
  2022-02-23 13:46 ` [PATCH v2 1/8] PE: Add LoongArch definitions Xiaotian Wu
                   ` (8 more replies)
  0 siblings, 9 replies; 19+ messages in thread
From: Xiaotian Wu @ 2022-02-23 13:46 UTC (permalink / raw)
  To: grub-devel; +Cc: git, 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.

Please review the patches, thank you.

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

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                             |   7 +
 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       |  68 ++++++++
 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                    |  77 +++++++++
 util/grub-mknetdir.c                     |   1 +
 util/grub-mkrescue.c                     |   7 +
 util/grub-module-verifier.c              |  26 +++
 util/mkimage.c                           |  16 ++
 37 files changed, 1132 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.32.0



^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v2 1/8] PE: Add LoongArch definitions
  2022-02-23 13:46 [PATCH v2 0/8] Add support for LoongArch Xiaotian Wu
@ 2022-02-23 13:46 ` Xiaotian Wu
  2022-02-23 15:57   ` WANG Xuerui
  2022-02-23 13:46 ` [PATCH v2 2/8] " Xiaotian Wu
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 19+ messages in thread
From: Xiaotian Wu @ 2022-02-23 13:46 UTC (permalink / raw)
  To: grub-devel; +Cc: git, Xiaotian Wu, Zhou Yang

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
---
 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_ARM64			0xAA64
+#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_LOW		2
-#define GRUB_PE32_REL_BASED_HIGHLOW	3
-#define GRUB_PE32_REL_BASED_HIGHADJ	4
-#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_SECTION	6
-#define GRUB_PE32_REL_BASED_REL		7
-#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_LOW			2
+#define GRUB_PE32_REL_BASED_HIGHLOW		3
+#define GRUB_PE32_REL_BASED_HIGHADJ		4
+#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_SECTION		6
+#define GRUB_PE32_REL_BASED_REL			7
+#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_LA	8
+#define GRUB_PE32_REL_BASED_LOONGARCH64_MARK_LA	8
+#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.32.0



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 2/8] Add LoongArch definitions
  2022-02-23 13:46 [PATCH v2 0/8] Add support for LoongArch Xiaotian Wu
  2022-02-23 13:46 ` [PATCH v2 1/8] PE: Add LoongArch definitions Xiaotian Wu
@ 2022-02-23 13:46 ` Xiaotian Wu
  2022-02-23 16:01   ` WANG Xuerui
  2022-02-23 13:46 ` [PATCH v2 3/8] LoongArch: Add setjmp implementation Xiaotian Wu
                   ` (6 subsequent siblings)
  8 siblings, 1 reply; 19+ messages in thread
From: Xiaotian Wu @ 2022-02-23 13:46 UTC (permalink / raw)
  To: grub-devel; +Cc: git, Xiaotian Wu, Zhou Yang

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
---
 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_PCREL        57
 
+/* 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_ABSOLUTE	      23
+#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_5	      38
+#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_S2	      42
+#define R_LARCH_SOP_POP_32_S_5_20	      43
+#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2     44
+#define R_LARCH_SOP_POP_32_S_0_10_10_16_S2    45
+
 #ifdef GRUB_TARGET_WORDSIZE
 #if GRUB_TARGET_WORDSIZE == 32
 
-- 
2.32.0



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 3/8] LoongArch: Add setjmp implementation
  2022-02-23 13:46 [PATCH v2 0/8] Add support for LoongArch Xiaotian Wu
  2022-02-23 13:46 ` [PATCH v2 1/8] PE: Add LoongArch definitions Xiaotian Wu
  2022-02-23 13:46 ` [PATCH v2 2/8] " Xiaotian Wu
@ 2022-02-23 13:46 ` Xiaotian Wu
  2022-02-23 16:13   ` WANG Xuerui
  2022-02-23 13:46 ` [PATCH v2 4/8] LoongArch: Add early startup code Xiaotian Wu
                   ` (5 subsequent siblings)
  8 siblings, 1 reply; 19+ messages in thread
From: Xiaotian Wu @ 2022-02-23 13:46 UTC (permalink / raw)
  To: grub-devel; +Cc: git, Xiaotian Wu, Zhou Yang, Sun Haiyong

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
Signed-off-by: Sun Haiyong <sunhaiyong@loongson.cn>
---
 grub-core/lib/loongarch64/setjmp.S | 68 ++++++++++++++++++++++++++++++
 grub-core/lib/setjmp.S             |  2 +
 include/grub/loongarch64/setjmp.h  | 27 ++++++++++++
 3 files changed, 97 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 000000000..c11fe63ea
--- /dev/null
+++ b/grub-core/lib/loongarch64/setjmp.S
@@ -0,0 +1,68 @@
+/*
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+
+	.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
+
+	li.w $a0, 1
+	beqz $a1, .L0
+	move $a0, $a1
+.L0:
+	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 000000000..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 <http://www.gnu.org/licenses/>.
+ */
+
+#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.32.0



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 4/8] LoongArch: Add early startup code
  2022-02-23 13:46 [PATCH v2 0/8] Add support for LoongArch Xiaotian Wu
                   ` (2 preceding siblings ...)
  2022-02-23 13:46 ` [PATCH v2 3/8] LoongArch: Add setjmp implementation Xiaotian Wu
@ 2022-02-23 13:46 ` Xiaotian Wu
  2022-02-23 16:15   ` WANG Xuerui
  2022-02-23 13:46 ` [PATCH v2 5/8] LoongArch: Add stubs for Linux loading commands Xiaotian Wu
                   ` (4 subsequent siblings)
  8 siblings, 1 reply; 19+ messages in thread
From: Xiaotian Wu @ 2022-02-23 13:46 UTC (permalink / raw)
  To: grub-devel; +Cc: git, Xiaotian Wu, Zhou Yang

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
---
 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 000000000..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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+
+	.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.32.0



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 5/8] LoongArch: Add stubs for Linux loading commands
  2022-02-23 13:46 [PATCH v2 0/8] Add support for LoongArch Xiaotian Wu
                   ` (3 preceding siblings ...)
  2022-02-23 13:46 ` [PATCH v2 4/8] LoongArch: Add early startup code Xiaotian Wu
@ 2022-02-23 13:46 ` Xiaotian Wu
  2022-02-23 13:46 ` [PATCH v2 6/8] LoongArch: Add awareness for LoongArch relocations Xiaotian Wu
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 19+ messages in thread
From: Xiaotian Wu @ 2022-02-23 13:46 UTC (permalink / raw)
  To: grub-devel; +Cc: git, Xiaotian Wu

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
---
 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 000000000..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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/command.h>
+#include <grub/dl.h>
+#include <grub/lib/cmdline.h>
+
+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 000000000..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 <http://www.gnu.org/licenses/>.
+ */
+
+#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.32.0



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 6/8] LoongArch: Add awareness for LoongArch relocations
  2022-02-23 13:46 [PATCH v2 0/8] Add support for LoongArch Xiaotian Wu
                   ` (4 preceding siblings ...)
  2022-02-23 13:46 ` [PATCH v2 5/8] LoongArch: Add stubs for Linux loading commands Xiaotian Wu
@ 2022-02-23 13:46 ` Xiaotian Wu
  2022-02-23 15:53   ` WANG Xuerui
  2022-02-23 13:46 ` [PATCH v2 7/8] LoongArch: Add auxiliary files Xiaotian Wu
                   ` (2 subsequent siblings)
  8 siblings, 1 reply; 19+ messages in thread
From: Xiaotian Wu @ 2022-02-23 13:46 UTC (permalink / raw)
  To: grub-devel; +Cc: git, Xiaotian Wu, Zhou Yang

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
---
 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                  |  77 ++++++++++
 util/grub-module-verifier.c            |  26 ++++
 6 files changed, 410 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 6a52de168..e58663279 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 000000000..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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/types.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
+#include <grub/cpu/reloc.h>
+
+/* 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_UINT64_T,
+			   (grub_uint64_t) ELF_R_TYPE (rel->r_info));
+	    return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+			       N_("relocation 0x%s is not implemented yet"), rel_info);
+	  }
+	  break;
+	}
+    }
+  return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/loongarch64/dl_helper.c b/grub-core/kern/loongarch64/dl_helper.c
new file mode 100644
index 000000000..becc89565
--- /dev/null
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -0,0 +1,198 @@
+/* dl_helper.c - relocation helper functions for modules and grub-mkimage */
+/*
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
+#include <grub/cpu/reloc.h>
+
+static void grub_loongarch64_stack_push (grub_loongarch64_stack_t stack, grub_uint64_t x);
+static grub_uint64_t grub_loongarch64_stack_pop (grub_loongarch64_stack_t stack);
+
+void
+grub_loongarch64_stack_init (grub_loongarch64_stack_t stack)
+{
+  stack->top = -1;
+  stack->count = LOONGARCH64_STACK_MAX;
+}
+
+static void
+grub_loongarch64_stack_push (grub_loongarch64_stack_t stack, grub_uint64_t x)
+{
+  if (stack->top == stack->count)
+    return;
+  stack->data[++stack->top] = x;
+}
+
+static grub_uint64_t
+grub_loongarch64_stack_pop (grub_loongarch64_stack_t stack)
+{
+  if (stack->top == -1)
+    return -1;
+  return stack->data[stack->top--];
+}
+
+void
+grub_loongarch64_sop_push (grub_loongarch64_stack_t stack, grub_int64_t offset)
+{
+  grub_loongarch64_stack_push (stack, offset);
+}
+
+/* opr2 = pop (), opr1 = pop (), push (opr1 - opr2) */
+void
+grub_loongarch64_sop_sub (grub_loongarch64_stack_t stack)
+{
+  grub_uint64_t a, b;
+  b = grub_loongarch64_stack_pop (stack);
+  a = grub_loongarch64_stack_pop (stack);
+  grub_loongarch64_stack_push (stack, a - b);
+}
+
+/* opr2 = pop (), opr1 = pop (), push (opr1 << opr2) */
+void
+grub_loongarch64_sop_sl (grub_loongarch64_stack_t stack)
+{
+  grub_uint64_t a, b;
+  b = grub_loongarch64_stack_pop (stack);
+  a = grub_loongarch64_stack_pop (stack);
+  grub_loongarch64_stack_push (stack, a << b);
+}
+
+/* opr2 = pop (), opr1 = pop (), push (opr1 >> opr2) */
+void
+grub_loongarch64_sop_sr (grub_loongarch64_stack_t stack)
+{
+  grub_uint64_t a, b;
+  b = grub_loongarch64_stack_pop (stack);
+  a = grub_loongarch64_stack_pop (stack);
+  grub_loongarch64_stack_push (stack, a >> b);
+}
+
+/* opr2 = pop (), opr1 = pop (), push (opr1 + opr2) */
+void
+grub_loongarch64_sop_add (grub_loongarch64_stack_t stack)
+{
+  grub_uint64_t a, b;
+  b = grub_loongarch64_stack_pop (stack);
+  a = grub_loongarch64_stack_pop (stack);
+  grub_loongarch64_stack_push (stack, a + b);
+}
+
+/* opr2 = pop (), opr1 = pop (), push (opr1 & opr2) */
+void
+grub_loongarch64_sop_and (grub_loongarch64_stack_t stack)
+{
+  grub_uint64_t a, b;
+  b = grub_loongarch64_stack_pop (stack);
+  a = grub_loongarch64_stack_pop (stack);
+  grub_loongarch64_stack_push (stack, a & b);
+}
+
+/* opr3 = pop (), opr2 = pop (), opr1 = pop (), push (opr1 ? opr2 : opr3) */
+void
+grub_loongarch64_sop_if_else (grub_loongarch64_stack_t stack)
+{
+  grub_uint64_t a, b, c;
+  c = grub_loongarch64_stack_pop (stack);
+  b = grub_loongarch64_stack_pop (stack);
+  a = grub_loongarch64_stack_pop (stack);
+
+  if (a) {
+      grub_loongarch64_stack_push (stack, b);
+  } else {
+      grub_loongarch64_stack_push (stack, c);
+  }
+}
+
+/* opr1 = pop (), (*(uint32_t *) PC) [14 ... 10] = opr1 [4 ... 0] */
+void
+grub_loongarch64_sop_32_s_10_5 (grub_loongarch64_stack_t stack,
+				grub_uint64_t *place)
+{
+  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
+  *place |= ((a & 0x1f) << 10);
+}
+
+/* opr1 = pop (), (*(uint32_t *) PC) [21 ... 10] = opr1 [11 ... 0] */
+void
+grub_loongarch64_sop_32_u_10_12 (grub_loongarch64_stack_t stack,
+				 grub_uint64_t *place)
+{
+  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
+  *place = *place | ((a & 0xfff) << 10);
+}
+
+/* opr1 = pop (), (*(uint32_t *) PC) [21 ... 10] = opr1 [11 ... 0] */
+void
+grub_loongarch64_sop_32_s_10_12 (grub_loongarch64_stack_t stack,
+				 grub_uint64_t *place)
+{
+  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
+  *place = (*place) | ((a & 0xfff) << 10);
+}
+
+/* opr1 = pop (), (*(uint32_t *) PC) [25 ... 10] = opr1 [15 ... 0] */
+void
+grub_loongarch64_sop_32_s_10_16 (grub_loongarch64_stack_t stack,
+				 grub_uint64_t *place)
+{
+  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
+  *place = (*place) | ((a & 0xffff) << 10);
+}
+
+/* opr1 = pop (), (*(uint32_t *) PC) [25 ... 10] = opr1 [17 ... 2] */
+void
+grub_loongarch64_sop_32_s_10_16_s2 (grub_loongarch64_stack_t stack,
+				    grub_uint64_t *place)
+{
+  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
+  *place = (*place) | (((a >> 2) & 0xffff) << 10);
+}
+
+/* opr1 = pop (), (*(uint32_t *) PC) [24 ... 5] = opr1 [19 ... 0] */
+void
+grub_loongarch64_sop_32_s_5_20 (grub_loongarch64_stack_t stack, grub_uint64_t *place)
+{
+  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
+  *place = (*place) | ((a & 0xfffff)<<5);
+}
+
+/* opr1 = pop (), (*(uint32_t *) PC) [4 ... 0] = opr1 [22 ... 18] */
+void
+grub_loongarch64_sop_32_s_0_5_10_16_s2 (grub_loongarch64_stack_t stack,
+					grub_uint64_t *place)
+{
+  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
+
+  *place =(*place) | (((a >> 2) & 0xffff) << 10);
+  *place =(*place) | ((a >> 18) & 0x1f);
+}
+
+/* opr1 = pop (), (*(uint32_t *) PC) [9 ... 0] = opr1 [27 ... 18] */
+void
+grub_loongarch64_sop_32_s_0_10_10_16_s2 (grub_loongarch64_stack_t stack,
+					 grub_uint64_t *place)
+{
+  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
+  *place =(*place) | (((a >> 2) & 0xffff) << 10);
+  *place =(*place) | ((a >> 18) & 0x3ff);
+}
diff --git a/include/grub/dl.h b/include/grub/dl.h
index b3753c9ca..3b9b19933 100644
--- a/include/grub/dl.h
+++ b/include/grub/dl.h
@@ -301,6 +301,7 @@ grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
 #endif
 
 #if defined (__aarch64__) || defined (__sparc__) || \
+    defined (__loongarch64) || \
     (defined(__riscv) && (__riscv_xlen == 64))
 #define GRUB_ARCH_DL_TRAMP_ALIGN 8
 #define GRUB_ARCH_DL_GOT_ALIGN 8
diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index d78fa3e53..2136ee36f 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -44,6 +44,7 @@
 #include <grub/arm/reloc.h>
 #include <grub/arm64/reloc.h>
 #include <grub/ia64/reloc.h>
+#include <grub/loongarch64/reloc.h>
 #include <grub/osdep/hostfile.h>
 #include <grub/util/install.h>
 #include <grub/util/mkimage.h>
@@ -784,6 +785,8 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
   struct grub_ia64_trampoline *tr = (void *) (pe_target + tramp_off);
   grub_uint64_t *gpptr = (void *) (pe_target + got_off);
   unsigned unmatched_adr_got_page = 0;
+  struct grub_loongarch64_stack stack;
+  grub_loongarch64_stack_init (&stack);
 #define MASK19 ((1 << 19) - 1)
 #else
   grub_uint32_t *tr = (void *) (pe_target + tramp_off);
@@ -1123,6 +1126,31 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
 		   }
 	       break;
 	       }
+	     case EM_LOONGARCH:
+	       {
+		 sym_addr += addend;
+		 switch (ELF_R_TYPE (info))
+		   {
+		   case R_LARCH_64:
+		     *target = grub_host_to_target64 (grub_target_to_host64 (*target) + 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
+						-(target_section_addr
+						  +offset
+						  +image_target->vaddr_offset));
+		     break;
+		   GRUB_LOONGARCH64_RELOCATION (&stack, target, sym_addr)
+		   default:
+		     grub_util_error (_("relocation 0x%x is not implemented yet"),
+				      (unsigned int) ELF_R_TYPE (info));
+		     break;
+		   }
+		 break;
+	       }
 #endif
 #if defined(MKIMAGE_ELF32)
 	     case EM_ARM:
@@ -1668,6 +1696,55 @@ translate_relocation_pe (struct translate_context *ctx,
 	}
       break;
       break;
+    case EM_LOONGARCH:
+      switch (ELF_R_TYPE (info))
+	{
+	case R_LARCH_64:
+	  {
+	    ctx->current_address = add_fixup_entry (&ctx->lst,
+						    GRUB_PE32_REL_BASED_DIR64,
+						    addr, 0, ctx->current_address,
+						    image_target);
+	  }
+	  break;
+	case R_LARCH_MARK_LA:
+	  {
+	    ctx->current_address = add_fixup_entry (&ctx->lst,
+						    GRUB_PE32_REL_BASED_LOONGARCH64_MARK_LA,
+						    addr, 0, ctx->current_address,
+						    image_target);
+	  }
+	  break;
+	  /* Relative relocations do not require fixup entries. */
+	case R_LARCH_NONE:
+	case R_LARCH_SOP_PUSH_PCREL:
+	case R_LARCH_SOP_PUSH_ABSOLUTE:
+	case R_LARCH_SOP_PUSH_PLT_PCREL:
+	case R_LARCH_SOP_SUB:
+	case R_LARCH_SOP_SL:
+	case R_LARCH_SOP_SR:
+	case R_LARCH_SOP_ADD:
+	case R_LARCH_SOP_AND:
+	case R_LARCH_SOP_IF_ELSE:
+	case R_LARCH_SOP_POP_32_S_10_5:
+	case R_LARCH_SOP_POP_32_U_10_12:
+	case R_LARCH_SOP_POP_32_S_10_12:
+	case R_LARCH_SOP_POP_32_S_10_16:
+	case R_LARCH_SOP_POP_32_S_10_16_S2:
+	case R_LARCH_SOP_POP_32_S_5_20:
+	case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
+	case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
+	  grub_util_info ("  %s:  not adding fixup: 0x%08x : 0x%08x",
+			  __FUNCTION__,
+			  (unsigned int) addr,
+			  (unsigned int) ctx->current_address);
+	  break;
+	default:
+	  grub_util_error (_("relocation 0x%x is not implemented yet"),
+			   (unsigned int) ELF_R_TYPE (info));
+	  break;
+	}
+      break;
 #if defined(MKIMAGE_ELF32)
     case EM_ARM:
       switch (ELF_R_TYPE (info))
diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c
index 163529ca9..b510461fa 100644
--- a/util/grub-module-verifier.c
+++ b/util/grub-module-verifier.c
@@ -119,6 +119,32 @@ struct grub_module_verifier_arch archs[] = {
       R_AARCH64_PREL32,
       -1
     } },
+  { "loongarch64", 8, 0, EM_LOONGARCH, GRUB_MODULE_VERIFY_SUPPORTS_REL | GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
+      R_LARCH_NONE,
+      R_LARCH_64,
+      R_LARCH_MARK_LA,
+      R_LARCH_SOP_PUSH_PCREL,
+      R_LARCH_SOP_PUSH_ABSOLUTE,
+      R_LARCH_SOP_PUSH_PLT_PCREL,
+      R_LARCH_SOP_SUB,
+      R_LARCH_SOP_SL,
+      R_LARCH_SOP_SR,
+      R_LARCH_SOP_ADD,
+      R_LARCH_SOP_AND,
+      R_LARCH_SOP_IF_ELSE,
+      R_LARCH_SOP_POP_32_S_10_5,
+      R_LARCH_SOP_POP_32_U_10_12,
+      R_LARCH_SOP_POP_32_S_10_12,
+      R_LARCH_SOP_POP_32_S_10_16,
+      R_LARCH_SOP_POP_32_S_10_16_S2,
+      R_LARCH_SOP_POP_32_S_5_20,
+      R_LARCH_SOP_POP_32_S_0_5_10_16_S2,
+      R_LARCH_SOP_POP_32_S_0_10_10_16_S2,
+      -1
+    }, (int[]){
+      -1
+    }
+  },
   { "riscv32", 4, 0, EM_RISCV, GRUB_MODULE_VERIFY_SUPPORTS_REL | GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
       R_RISCV_32,
       R_RISCV_64,
-- 
2.32.0



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 7/8] LoongArch: Add auxiliary files
  2022-02-23 13:46 [PATCH v2 0/8] Add support for LoongArch Xiaotian Wu
                   ` (5 preceding siblings ...)
  2022-02-23 13:46 ` [PATCH v2 6/8] LoongArch: Add awareness for LoongArch relocations Xiaotian Wu
@ 2022-02-23 13:46 ` Xiaotian Wu
  2022-02-23 13:46 ` [PATCH v2 8/8] LoongArch: Add to build system Xiaotian Wu
  2022-02-23 13:59 ` [PATCH v2 0/8] Add support for LoongArch John Paul Adrian Glaubitz
  8 siblings, 0 replies; 19+ messages in thread
From: Xiaotian Wu @ 2022-02-23 13:46 UTC (permalink / raw)
  To: grub-devel; +Cc: git, Xiaotian Wu, Zhou Yang

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
---
 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 9838fb2f5..ae0d1ee87 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 000000000..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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/cache.h>
+#include <grub/misc.h>
+
+/* 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 000000000..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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+
+	.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 000000000..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 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/env.h>
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/time.h>
+#include <grub/efi/efi.h>
+#include <grub/loader.h>
+
+#define EFI_TIMER_PERIOD_MILLISECONDS(ms) ((grub_uint64_t)(ms * 10000))
+
+static grub_uint64_t tmr;
+static grub_efi_event_t tmr_evt;
+
+static grub_uint64_t
+grub_efi_get_time_ms (void)
+{
+  return tmr;
+}
+
+static void
+grub_loongson_increment_timer (grub_efi_event_t event __attribute__ ((unused)),
+			       void *context __attribute__ ((unused)))
+{
+  tmr += 10;
+}
+
+void
+grub_machine_init (void)
+{
+  grub_efi_boot_services_t *b;
+
+  grub_efi_init ();
+
+  b = grub_efi_system_table->boot_services;
+  efi_call_5 (b->create_event, GRUB_EFI_EVT_TIMER | GRUB_EFI_EVT_NOTIFY_SIGNAL,
+	      GRUB_EFI_TPL_CALLBACK, grub_loongson_increment_timer, NULL, &tmr_evt);
+  efi_call_3 (b->set_timer, tmr_evt, GRUB_EFI_TIMER_PERIODIC, EFI_TIMER_PERIOD_MILLISECONDS(10));
+
+  grub_install_get_time_ms (grub_efi_get_time_ms);
+}
+
+void
+grub_machine_fini (int flags)
+{
+  grub_efi_boot_services_t *b;
+
+  if (!(flags & GRUB_LOADER_FLAG_NORETURN))
+    return;
+
+  b = grub_efi_system_table->boot_services;
+
+  efi_call_3 (b->set_timer, tmr_evt, GRUB_EFI_TIMER_CANCEL, 0);
+  efi_call_1 (b->close_event, tmr_evt);
+
+  grub_efi_fini ();
+
+  if (!(flags & GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY))
+    grub_efi_memory_fini ();
+}
diff --git a/grub-core/lib/efi/halt.c b/grub-core/lib/efi/halt.c
index 29d413641..e6356894a 100644
--- a/grub-core/lib/efi/halt.c
+++ b/grub-core/lib/efi/halt.c
@@ -31,7 +31,7 @@ grub_halt (void)
   grub_machine_fini (GRUB_LOADER_FLAG_NORETURN |
 		     GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY);
 #if !defined(__ia64__) && !defined(__arm__) && !defined(__aarch64__) && \
-    !defined(__riscv)
+    !defined(__loongarch__) && !defined(__riscv)
   grub_acpi_halt ();
 #endif
   efi_call_4 (grub_efi_system_table->runtime_services->reset_system,
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
index fc723962d..43adb5002 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -98,7 +98,7 @@ extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd,
 						char **device,
 						char **path);
 
-#if defined(__arm__) || defined(__aarch64__) || defined(__riscv)
+#if defined(__arm__) || defined(__aarch64__) || defined(__riscv) || defined(__loongarch__)
 void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void);
 grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *);
 #include <grub/cpu/linux.h>
diff --git a/include/grub/loongarch64/efi/memory.h b/include/grub/loongarch64/efi/memory.h
new file mode 100644
index 000000000..ca8dbfea5
--- /dev/null
+++ b/include/grub/loongarch64/efi/memory.h
@@ -0,0 +1,24 @@
+/*
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_MEMORY_CPU_HEADER
+#include <grub/efi/memory.h>
+
+#define GRUB_EFI_MAX_USABLE_ADDRESS 0xffffffffffffULL
+
+#endif /* ! GRUB_MEMORY_CPU_HEADER */
diff --git a/include/grub/loongarch64/reloc.h b/include/grub/loongarch64/reloc.h
new file mode 100644
index 000000000..3f6b0792a
--- /dev/null
+++ b/include/grub/loongarch64/reloc.h
@@ -0,0 +1,107 @@
+/*
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_LOONGARCH64_RELOC_H
+#define GRUB_LOONGARCH64_RELOC_H 1
+#include <grub/types.h>
+
+#define LOONGARCH64_STACK_MAX 16
+
+struct grub_loongarch64_stack
+{
+  grub_uint64_t data[LOONGARCH64_STACK_MAX];
+  int count;
+  int top;
+};
+
+typedef struct grub_loongarch64_stack* grub_loongarch64_stack_t;
+
+void grub_loongarch64_stack_init	     (grub_loongarch64_stack_t stack);
+void grub_loongarch64_sop_push		     (grub_loongarch64_stack_t stack,
+					      grub_int64_t offset);
+void grub_loongarch64_sop_sub		     (grub_loongarch64_stack_t stack);
+void grub_loongarch64_sop_sl		     (grub_loongarch64_stack_t stack);
+void grub_loongarch64_sop_sr		     (grub_loongarch64_stack_t stack);
+void grub_loongarch64_sop_add		     (grub_loongarch64_stack_t stack);
+void grub_loongarch64_sop_and		     (grub_loongarch64_stack_t stack);
+void grub_loongarch64_sop_if_else	     (grub_loongarch64_stack_t stack);
+void grub_loongarch64_sop_32_s_10_5	     (grub_loongarch64_stack_t stack,
+					      grub_uint64_t *place);
+void grub_loongarch64_sop_32_u_10_12	     (grub_loongarch64_stack_t stack,
+					      grub_uint64_t *place);
+void grub_loongarch64_sop_32_s_10_12	     (grub_loongarch64_stack_t stack,
+					      grub_uint64_t *place);
+void grub_loongarch64_sop_32_s_10_16	     (grub_loongarch64_stack_t stack,
+					      grub_uint64_t *place);
+void grub_loongarch64_sop_32_s_10_16_s2	     (grub_loongarch64_stack_t stack,
+					      grub_uint64_t *place);
+void grub_loongarch64_sop_32_s_5_20	     (grub_loongarch64_stack_t stack,
+					      grub_uint64_t *place);
+void grub_loongarch64_sop_32_s_0_5_10_16_s2  (grub_loongarch64_stack_t stack,
+					      grub_uint64_t *place);
+void grub_loongarch64_sop_32_s_0_10_10_16_s2 (grub_loongarch64_stack_t stack,
+					      grub_uint64_t *place);
+
+#define GRUB_LOONGARCH64_RELOCATION(STACK, PLACE, OFFSET)	\
+  case R_LARCH_SOP_PUSH_ABSOLUTE:				\
+    grub_loongarch64_sop_push (STACK, OFFSET);			\
+    break;							\
+  case R_LARCH_SOP_SUB:						\
+    grub_loongarch64_sop_sub (STACK);				\
+    break;							\
+  case R_LARCH_SOP_SL:		      				\
+    grub_loongarch64_sop_sl (STACK);				\
+    break;			      				\
+  case R_LARCH_SOP_SR:		      				\
+    grub_loongarch64_sop_sr (STACK);				\
+    break;					    		\
+  case R_LARCH_SOP_ADD:		      		    		\
+    grub_loongarch64_sop_add (STACK); 		    		\
+    break;			      		    		\
+  case R_LARCH_SOP_AND:		      		    		\
+    grub_loongarch64_sop_and (STACK);		    		\
+    break;					    		\
+  case R_LARCH_SOP_IF_ELSE:			    		\
+    grub_loongarch64_sop_if_else (STACK);	    		\
+    break;					    		\
+  case R_LARCH_SOP_POP_32_S_10_5:		    		\
+    grub_loongarch64_sop_32_s_10_5 (STACK, PLACE);  		\
+    break;					    		\
+  case R_LARCH_SOP_POP_32_U_10_12:		    		\
+    grub_loongarch64_sop_32_u_10_12 (STACK, PLACE);		\
+    break;							\
+  case R_LARCH_SOP_POP_32_S_10_12:				\
+    grub_loongarch64_sop_32_s_10_12 (STACK, PLACE);		\
+    break;							\
+  case R_LARCH_SOP_POP_32_S_10_16:				\
+    grub_loongarch64_sop_32_s_10_16 (STACK, PLACE);		\
+    break;							\
+  case R_LARCH_SOP_POP_32_S_10_16_S2:				\
+    grub_loongarch64_sop_32_s_10_16_s2 (STACK, PLACE);		\
+    break;							\
+  case R_LARCH_SOP_POP_32_S_5_20:				\
+    grub_loongarch64_sop_32_s_5_20 (STACK, PLACE);		\
+    break;							\
+  case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:			\
+    grub_loongarch64_sop_32_s_0_5_10_16_s2 (STACK, PLACE);	\
+    break;							\
+  case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:			\
+    grub_loongarch64_sop_32_s_0_10_10_16_s2 (STACK, PLACE);	\
+    break;
+
+#endif /* GRUB_LOONGARCH64_RELOC_H */
diff --git a/include/grub/loongarch64/time.h b/include/grub/loongarch64/time.h
new file mode 100644
index 000000000..e5724bd83
--- /dev/null
+++ b/include/grub/loongarch64/time.h
@@ -0,0 +1,28 @@
+/*
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef KERNEL_CPU_TIME_HEADER
+#define KERNEL_CPU_TIME_HEADER	1
+
+static inline void
+grub_cpu_idle(void)
+{
+  __asm__ __volatile__("idle 0");
+}
+
+#endif
diff --git a/include/grub/loongarch64/types.h b/include/grub/loongarch64/types.h
new file mode 100644
index 000000000..a9e09171a
--- /dev/null
+++ b/include/grub/loongarch64/types.h
@@ -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 <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
+
+/* LoongArch is little-endian.  */
+#undef GRUB_TARGET_WORDS_BIGENDIAN
+
+/* Unaligned accesses are only supported if MMU is enabled.  */
+#undef GRUB_HAVE_UNALIGNED_ACCESS
+
+#endif /* ! GRUB_TYPES_CPU_HEADER */
-- 
2.32.0



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 8/8] LoongArch: Add to build system
  2022-02-23 13:46 [PATCH v2 0/8] Add support for LoongArch Xiaotian Wu
                   ` (6 preceding siblings ...)
  2022-02-23 13:46 ` [PATCH v2 7/8] LoongArch: Add auxiliary files Xiaotian Wu
@ 2022-02-23 13:46 ` Xiaotian Wu
  2022-02-23 15:47   ` WANG Xuerui
  2022-02-23 13:59 ` [PATCH v2 0/8] Add support for LoongArch John Paul Adrian Glaubitz
  8 siblings, 1 reply; 19+ messages in thread
From: Xiaotian Wu @ 2022-02-23 13:46 UTC (permalink / raw)
  To: grub-devel; +Cc: git, Xiaotian Wu, Zhou Yang

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
---
 Makefile.util.def           |  1 +
 conf/Makefile.common        |  3 +++
 configure.ac                |  7 ++++++
 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        |  7 ++++++
 util/mkimage.c              | 16 ++++++++++++
 13 files changed, 112 insertions(+), 37 deletions(-)

diff --git a/Makefile.util.def b/Makefile.util.def
index f8b356cc1..520154727 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 f0bb6e160..7c5b5dfe7 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 5c01af0fa..5f9d855b7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -113,6 +113,9 @@ case "$target_cpu" in
   aarch64*)
 		target_cpu=arm64
 		;;
+  loongarch64)
+		target_cpu=loongarch64
+		;;
   riscv32*)
 		target_cpu=riscv32
 		;;
@@ -142,6 +145,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 ;;
     *)
@@ -192,6 +196,7 @@ case "$target_cpu"-"$platform" in
   arm-coreboot) ;;
   arm-efi) ;;
   arm64-efi) ;;
+  loongarch64-efi) ;;
   riscv32-efi) ;;
   riscv64-efi) ;;
   *-emu) ;;
@@ -2021,6 +2026,8 @@ AM_CONDITIONAL([COND_arm_coreboot], [test x$target_cpu = xarm -a x$platform = xc
 AM_CONDITIONAL([COND_arm_efi], [test x$target_cpu = xarm -a x$platform = xefi])
 AM_CONDITIONAL([COND_arm64], [test x$target_cpu = xarm64 ])
 AM_CONDITIONAL([COND_arm64_efi], [test x$target_cpu = xarm64 -a x$platform = xefi])
+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_riscv32], [test x$target_cpu = xriscv32 ])
 AM_CONDITIONAL([COND_riscv64], [test x$target_cpu = xriscv64 ])
 AM_CONDITIONAL([COND_riscv32_efi], [test x$target_cpu = xriscv32 -a x$platform = xefi])
diff --git a/gentpl.py b/gentpl.py
index 05ab13fa0..846cb8edd 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" ]
+GROUPS["riscv64"]     = [ "riscv64_efi" ]
 
 # Groups based on firmware
 GROUPS["efi"]  = [ "i386_efi", "x86_64_efi", "ia64_efi", "arm_efi", "arm64_efi",
-		   "riscv32_efi", "riscv64_efi" ]
+		   "loongarch64_efi", "riscv32_efi", "riscv64_efi" ]
 GROUPS["ieee1275"]   = [ "i386_ieee1275", "sparc64_ieee1275", "powerpc_ieee1275" ]
 GROUPS["uboot"] = [ "arm_uboot" ]
 GROUPS["xen"]  = [ "i386_xen", "x86_64_xen" ]
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index ee88e44e9..aa933b93c 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -288,6 +288,12 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
 endif
 
+if COND_loongarch64_efi
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
+endif
+
 if COND_riscv32_efi
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 8022e1c0a..f062762de 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -65,6 +65,9 @@ kernel = {
   arm64_efi_ldflags          = '-Wl,-r,-d';
   arm64_efi_stripflags       = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
 
+  loongarch64_efi_ldflags      = '-Wl,-r,-d';
+  loongarch64_efi_stripflags   = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
+
   riscv32_efi_ldflags      = '-Wl,-r,-d';
   riscv32_efi_stripflags   = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
 
@@ -120,6 +123,7 @@ kernel = {
   arm_coreboot_startup = kern/arm/startup.S;
   arm_efi_startup = kern/arm/efi/startup.S;
   arm64_efi_startup = kern/arm64/efi/startup.S;
+  loongarch64_efi_startup = kern/loongarch64/efi/startup.S;
   riscv32_efi_startup = kern/riscv/efi/startup.S;
   riscv64_efi_startup = kern/riscv/efi/startup.S;
 
@@ -258,6 +262,9 @@ kernel = {
   arm64_efi = kern/arm64/efi/init.c;
   arm64_efi = kern/efi/fdt.c;
 
+  loongarch64_efi = kern/loongarch64/efi/init.c;
+  loongarch64_efi = kern/efi/fdt.c;
+
   riscv32_efi = kern/riscv/efi/init.c;
   riscv32_efi = kern/efi/fdt.c;
 
@@ -336,6 +343,11 @@ kernel = {
   arm64 = kern/arm64/dl.c;
   arm64 = kern/arm64/dl_helper.c;
 
+  loongarch64 = kern/loongarch64/cache.c;
+  loongarch64 = kern/loongarch64/cache_flush.S;
+  loongarch64 = kern/loongarch64/dl.c;
+  loongarch64 = kern/loongarch64/dl_helper.c;
+
   riscv32 = kern/riscv/cache.c;
   riscv32 = kern/riscv/cache_flush.S;
   riscv32 = kern/riscv/dl.c;
@@ -827,6 +839,7 @@ module = {
   enable = arm64_efi;
   enable = arm_uboot;
   enable = arm_coreboot;
+  enable = loongarch64_efi;
   enable = riscv32_efi;
   enable = riscv64_efi;
 };
@@ -1809,6 +1822,7 @@ module = {
   arm_efi = loader/arm64/linux.c;
   arm_uboot = loader/arm/linux.c;
   arm64 = loader/arm64/linux.c;
+  loongarch64 = loader/loongarch64/linux.c;
   riscv32 = loader/riscv/linux.c;
   riscv64 = loader/riscv/linux.c;
   common = loader/linux.c;
@@ -1905,6 +1919,7 @@ module = {
   enable = ia64_efi;
   enable = arm_efi;
   enable = arm64_efi;
+  enable = loongarch64_efi;
   enable = riscv32_efi;
   enable = riscv64_efi;
   enable = mips;
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
index 6c8d06e15..d0bc350f3 100644
--- a/include/grub/efi/api.h
+++ b/include/grub/efi/api.h
@@ -1731,7 +1731,7 @@ typedef struct grub_efi_rng_protocol grub_efi_rng_protocol_t;
 
 #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \
   || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__) \
-  || defined(__riscv)
+  || defined(__riscv) || defined (__loongarch__)
 
 #define efi_call_0(func)		func()
 #define efi_call_1(func, a)		func(a)
diff --git a/include/grub/util/install.h b/include/grub/util/install.h
index 7df3191f4..51e84048d 100644
--- a/include/grub/util/install.h
+++ b/include/grub/util/install.h
@@ -107,6 +107,7 @@ enum grub_install_plat
     GRUB_INSTALL_PLATFORM_I386_XEN_PVH,
     GRUB_INSTALL_PLATFORM_ARM64_EFI,
     GRUB_INSTALL_PLATFORM_ARM_COREBOOT,
+    GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI,
     GRUB_INSTALL_PLATFORM_RISCV32_EFI,
     GRUB_INSTALL_PLATFORM_RISCV64_EFI,
     GRUB_INSTALL_PLATFORM_MAX
diff --git a/util/grub-install-common.c b/util/grub-install-common.c
index fe77e3945..0514ac2e6 100644
--- a/util/grub-install-common.c
+++ b/util/grub-install-common.c
@@ -881,30 +881,31 @@ static struct
   const char *platform;
 } platforms[GRUB_INSTALL_PLATFORM_MAX] =
   {
-    [GRUB_INSTALL_PLATFORM_I386_PC] =          { "i386",    "pc"        },
-    [GRUB_INSTALL_PLATFORM_I386_EFI] =         { "i386",    "efi"       },
-    [GRUB_INSTALL_PLATFORM_I386_QEMU] =        { "i386",    "qemu"      },
-    [GRUB_INSTALL_PLATFORM_I386_COREBOOT] =    { "i386",    "coreboot"  },
-    [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] =   { "i386",    "multiboot" },
-    [GRUB_INSTALL_PLATFORM_I386_IEEE1275] =    { "i386",    "ieee1275"  },
-    [GRUB_INSTALL_PLATFORM_X86_64_EFI] =       { "x86_64",  "efi"       },
-    [GRUB_INSTALL_PLATFORM_I386_XEN] =         { "i386",    "xen"       },
-    [GRUB_INSTALL_PLATFORM_X86_64_XEN] =       { "x86_64",  "xen"       },
-    [GRUB_INSTALL_PLATFORM_I386_XEN_PVH] =     { "i386",    "xen_pvh"   },
-    [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] =  { "mipsel",  "loongson"  },
-    [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel",  "qemu_mips" },
-    [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] =   { "mips",    "qemu_mips" },
-    [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] =       { "mipsel",  "arc"       },
-    [GRUB_INSTALL_PLATFORM_MIPS_ARC] =         { "mips",    "arc"       },
-    [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275"  },
-    [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275"  },
-    [GRUB_INSTALL_PLATFORM_IA64_EFI] =         { "ia64",    "efi"       },
-    [GRUB_INSTALL_PLATFORM_ARM_EFI] =          { "arm",     "efi"       },
-    [GRUB_INSTALL_PLATFORM_ARM64_EFI] =        { "arm64",   "efi"       },
-    [GRUB_INSTALL_PLATFORM_ARM_UBOOT] =        { "arm",     "uboot"     },
-    [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] =     { "arm",     "coreboot"  },
-    [GRUB_INSTALL_PLATFORM_RISCV32_EFI] =      { "riscv32", "efi"       },
-    [GRUB_INSTALL_PLATFORM_RISCV64_EFI] =      { "riscv64", "efi"       },
+    [GRUB_INSTALL_PLATFORM_I386_PC] =          { "i386",        "pc"        },
+    [GRUB_INSTALL_PLATFORM_I386_EFI] =         { "i386",        "efi"       },
+    [GRUB_INSTALL_PLATFORM_I386_QEMU] =        { "i386",        "qemu"      },
+    [GRUB_INSTALL_PLATFORM_I386_COREBOOT] =    { "i386",        "coreboot"  },
+    [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] =   { "i386",        "multiboot" },
+    [GRUB_INSTALL_PLATFORM_I386_IEEE1275] =    { "i386",        "ieee1275"  },
+    [GRUB_INSTALL_PLATFORM_X86_64_EFI] =       { "x86_64",      "efi"       },
+    [GRUB_INSTALL_PLATFORM_I386_XEN] =         { "i386",        "xen"       },
+    [GRUB_INSTALL_PLATFORM_X86_64_XEN] =       { "x86_64",      "xen"       },
+    [GRUB_INSTALL_PLATFORM_I386_XEN_PVH] =     { "i386",        "xen_pvh"   },
+    [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] =  { "mipsel",      "loongson"  },
+    [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel",      "qemu_mips" },
+    [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] =   { "mips",        "qemu_mips" },
+    [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] =       { "mipsel",      "arc"       },
+    [GRUB_INSTALL_PLATFORM_MIPS_ARC] =         { "mips",        "arc"       },
+    [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64",     "ieee1275"  },
+    [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc",     "ieee1275"  },
+    [GRUB_INSTALL_PLATFORM_IA64_EFI] =         { "ia64",        "efi"       },
+    [GRUB_INSTALL_PLATFORM_ARM_EFI] =          { "arm",         "efi"       },
+    [GRUB_INSTALL_PLATFORM_ARM64_EFI] =        { "arm64",       "efi"       },
+    [GRUB_INSTALL_PLATFORM_ARM_UBOOT] =        { "arm",         "uboot"     },
+    [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] =     { "arm",         "coreboot"  },
+    [GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI] =  { "loongarch64", "efi"       },
+    [GRUB_INSTALL_PLATFORM_RISCV32_EFI] =      { "riscv32",     "efi"       },
+    [GRUB_INSTALL_PLATFORM_RISCV64_EFI] =      { "riscv64",     "efi"       },
   }; 
 
 char *
diff --git a/util/grub-install.c b/util/grub-install.c
index 0fbe7f78c..25fdaba7f 100644
--- a/util/grub-install.c
+++ b/util/grub-install.c
@@ -324,6 +324,8 @@ get_default_platform (void)
    return "arm64-efi";
 #elif defined (__amd64__) || defined (__x86_64__) || defined (__i386__)
    return grub_install_get_default_x86_platform ();
+#elif defined (__loongarch64)
+   return "loongarch64-efi";
 #elif defined (__riscv)
 #if __riscv_xlen == 32
    return "riscv32-efi";
@@ -485,6 +487,7 @@ have_bootdev (enum grub_install_plat pl)
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
@@ -910,6 +913,7 @@ main (int argc, char *argv[])
     case GRUB_INSTALL_PLATFORM_X86_64_EFI:
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
@@ -957,6 +961,7 @@ main (int argc, char *argv[])
     case GRUB_INSTALL_PLATFORM_X86_64_EFI:
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
@@ -1012,6 +1017,7 @@ main (int argc, char *argv[])
     case GRUB_INSTALL_PLATFORM_X86_64_EFI:
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
@@ -1127,6 +1133,9 @@ main (int argc, char *argv[])
 	    case GRUB_INSTALL_PLATFORM_ARM64_EFI:
 	      efi_file = "BOOTAA64.EFI";
 	      break;
+	    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
+	      efi_file = "BOOTLOONGARCH64.EFI";
+	      break;
 	    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
 	      efi_file = "BOOTRISCV32.EFI";
 	      break;
@@ -1160,6 +1169,9 @@ main (int argc, char *argv[])
 	    case GRUB_INSTALL_PLATFORM_ARM64_EFI:
 	      efi_file = "grubaa64.efi";
 	      break;
+	    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
+	      efi_file = "grubloongarch64.efi";
+	      break;
 	    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
 	      efi_file = "grubriscv32.efi";
 	      break;
@@ -1468,6 +1480,7 @@ main (int argc, char *argv[])
 		  case GRUB_INSTALL_PLATFORM_X86_64_EFI:
 		  case GRUB_INSTALL_PLATFORM_ARM_EFI:
 		  case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+		  case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
 		  case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
 		  case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
 		  case GRUB_INSTALL_PLATFORM_IA64_EFI:
@@ -1563,6 +1576,7 @@ main (int argc, char *argv[])
     case GRUB_INSTALL_PLATFORM_X86_64_EFI:
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
@@ -1668,6 +1682,7 @@ main (int argc, char *argv[])
       break;
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
@@ -1922,6 +1937,7 @@ main (int argc, char *argv[])
       /* FALLTHROUGH */
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
     case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
diff --git a/util/grub-mknetdir.c b/util/grub-mknetdir.c
index a2461cda1..016fc11b0 100644
--- a/util/grub-mknetdir.c
+++ b/util/grub-mknetdir.c
@@ -108,6 +108,7 @@ static const struct
     [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64-efi", "efinet", ".efi" },
     [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm-efi", "efinet", ".efi" },
     [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64-efi", "efinet", ".efi" },
+    [GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI] = { "loongarch64-efi", "efinet", ".efi" },
     [GRUB_INSTALL_PLATFORM_RISCV32_EFI] = { "riscv32-efi", "efinet", ".efi" },
     [GRUB_INSTALL_PLATFORM_RISCV64_EFI] = { "riscv64-efi", "efinet", ".efi" },
   };
diff --git a/util/grub-mkrescue.c b/util/grub-mkrescue.c
index 59c48b123..d36832ca8 100644
--- a/util/grub-mkrescue.c
+++ b/util/grub-mkrescue.c
@@ -742,6 +742,7 @@ main (int argc, char *argv[])
       || source_dirs[GRUB_INSTALL_PLATFORM_IA64_EFI]
       || source_dirs[GRUB_INSTALL_PLATFORM_ARM_EFI]
       || source_dirs[GRUB_INSTALL_PLATFORM_ARM64_EFI]
+      || source_dirs[GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI]
       || source_dirs[GRUB_INSTALL_PLATFORM_RISCV32_EFI]
       || source_dirs[GRUB_INSTALL_PLATFORM_RISCV64_EFI])
     {
@@ -778,6 +779,12 @@ main (int argc, char *argv[])
 			     imgname);
       free (imgname);
 
+      imgname = grub_util_path_concat (2, efidir_efi_boot, "bootloongarch64.efi");
+      make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI,
+			     "loongarch64-efi",
+			     imgname);
+      free (imgname);
+
       imgname = grub_util_path_concat (2, efidir_efi_boot, "bootriscv32.efi");
       make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_RISCV32_EFI, "riscv32-efi",
 			     imgname);
diff --git a/util/mkimage.c b/util/mkimage.c
index a26cf76f7..f24073545 100644
--- a/util/mkimage.c
+++ b/util/mkimage.c
@@ -622,6 +622,22 @@ static const struct grub_install_image_target_desc image_targets[] =
       .pe_target = GRUB_PE32_MACHINE_ARM64,
       .elf_target = EM_AARCH64,
     },
+    {
+      .dirname = "loongarch64-efi",
+      .names = { "loongarch64-efi", NULL },
+      .voidp_sizeof = 8,
+      .bigendian = 0,
+      .id = IMAGE_EFI,
+      .flags = PLATFORM_FLAGS_NONE,
+      .total_module_size = TARGET_NO_FIELD,
+      .decompressor_compressed_size = TARGET_NO_FIELD,
+      .decompressor_uncompressed_size = TARGET_NO_FIELD,
+      .decompressor_uncompressed_addr = TARGET_NO_FIELD,
+      .section_align = GRUB_PE32_SECTION_ALIGNMENT,
+      .vaddr_offset = EFI64_HEADER_SIZE,
+      .pe_target = GRUB_PE32_MACHINE_LOONGARCH64,
+      .elf_target = EM_LOONGARCH,
+    },
     {
       .dirname = "riscv32-efi",
       .names = { "riscv32-efi", NULL },
-- 
2.32.0



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 0/8] Add support for LoongArch
  2022-02-23 13:46 [PATCH v2 0/8] Add support for LoongArch Xiaotian Wu
                   ` (7 preceding siblings ...)
  2022-02-23 13:46 ` [PATCH v2 8/8] LoongArch: Add to build system Xiaotian Wu
@ 2022-02-23 13:59 ` John Paul Adrian Glaubitz
  2022-02-24  2:12   ` 武校田
  8 siblings, 1 reply; 19+ messages in thread
From: John Paul Adrian Glaubitz @ 2022-02-23 13:59 UTC (permalink / raw)
  To: The development of GNU GRUB, Xiaotian Wu; +Cc: git

Hello!

On 2/23/22 14:46, Xiaotian Wu wrote:
> LoongArch is a new Loongson 3A5000 CPU instruction set, you can read
> documents[1] or visit the development community[2] to get more information.

Is there any chance the support for mips64el EFI [1] could be upstreamed as well?

There is a repository available with a branch rebased against grub master [2].

Currently, GRUB EFI on mips64el is stuck at version 2.02 which is rather old.

Adrian

> [1] https://github.com/loongson-community/grub
> [2] https://github.com/AOSC-Dev/grub-loongson3

-- 
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - glaubitz@debian.org
`. `'   Freie Universitaet Berlin - glaubitz@physik.fu-berlin.de
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 8/8] LoongArch: Add to build system
  2022-02-23 13:46 ` [PATCH v2 8/8] LoongArch: Add to build system Xiaotian Wu
@ 2022-02-23 15:47   ` WANG Xuerui
  2022-02-24  9:00     ` 武校田
  0 siblings, 1 reply; 19+ messages in thread
From: WANG Xuerui @ 2022-02-23 15:47 UTC (permalink / raw)
  To: Xiaotian Wu, grub-devel; +Cc: Zhou Yang


[-- Attachment #1.1.1: Type: text/plain, Size: 21857 bytes --]

Hi,

On 2/23/22 21:46, Xiaotian Wu wrote:
> Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
> Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
> ---
>   Makefile.util.def           |  1 +
>   conf/Makefile.common        |  3 +++
>   configure.ac                |  7 ++++++
>   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        |  7 ++++++
>   util/mkimage.c              | 16 ++++++++++++
>   13 files changed, 112 insertions(+), 37 deletions(-)
>
> diff --git a/Makefile.util.def b/Makefile.util.def
> index f8b356cc1..520154727 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 f0bb6e160..7c5b5dfe7 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 5c01af0fa..5f9d855b7 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -113,6 +113,9 @@ case "$target_cpu" in
>     aarch64*)
>   		target_cpu=arm64
>   		;;
> +  loongarch64)
> +		target_cpu=loongarch64
> +		;;
>     riscv32*)
>   		target_cpu=riscv32
>   		;;
> @@ -142,6 +145,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 ;;
>       *)
> @@ -192,6 +196,7 @@ case "$target_cpu"-"$platform" in
>     arm-coreboot) ;;
>     arm-efi) ;;
>     arm64-efi) ;;
> +  loongarch64-efi) ;;
>     riscv32-efi) ;;
>     riscv64-efi) ;;
>     *-emu) ;;
> @@ -2021,6 +2026,8 @@ AM_CONDITIONAL([COND_arm_coreboot], [test x$target_cpu = xarm -a x$platform = xc
>   AM_CONDITIONAL([COND_arm_efi], [test x$target_cpu = xarm -a x$platform = xefi])
>   AM_CONDITIONAL([COND_arm64], [test x$target_cpu = xarm64 ])
>   AM_CONDITIONAL([COND_arm64_efi], [test x$target_cpu = xarm64 -a x$platform = xefi])
> +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_riscv32], [test x$target_cpu = xriscv32 ])
>   AM_CONDITIONAL([COND_riscv64], [test x$target_cpu = xriscv64 ])
>   AM_CONDITIONAL([COND_riscv32_efi], [test x$target_cpu = xriscv32 -a x$platform = xefi])
> diff --git a/gentpl.py b/gentpl.py
> index 05ab13fa0..846cb8edd 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" ]
> +GROUPS["riscv64"]     = [ "riscv64_efi" ]
>   
>   # Groups based on firmware
>   GROUPS["efi"]  = [ "i386_efi", "x86_64_efi", "ia64_efi", "arm_efi", "arm64_efi",
> -		   "riscv32_efi", "riscv64_efi" ]
> +		   "loongarch64_efi", "riscv32_efi", "riscv64_efi" ]
>   GROUPS["ieee1275"]   = [ "i386_ieee1275", "sparc64_ieee1275", "powerpc_ieee1275" ]
>   GROUPS["uboot"] = [ "arm_uboot" ]
>   GROUPS["xen"]  = [ "i386_xen", "x86_64_xen" ]
> diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
> index ee88e44e9..aa933b93c 100644
> --- a/grub-core/Makefile.am
> +++ b/grub-core/Makefile.am
> @@ -288,6 +288,12 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
>   KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
>   endif
>   
> +if COND_loongarch64_efi
> +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
> +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
> +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
> +endif
> +
>   if COND_riscv32_efi
>   KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
>   KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
> diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
> index 8022e1c0a..f062762de 100644
> --- a/grub-core/Makefile.core.def
> +++ b/grub-core/Makefile.core.def
> @@ -65,6 +65,9 @@ kernel = {
>     arm64_efi_ldflags          = '-Wl,-r,-d';
>     arm64_efi_stripflags       = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
>   
> +  loongarch64_efi_ldflags      = '-Wl,-r,-d';
> +  loongarch64_efi_stripflags   = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
> +
>     riscv32_efi_ldflags      = '-Wl,-r,-d';
>     riscv32_efi_stripflags   = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
>   
> @@ -120,6 +123,7 @@ kernel = {
>     arm_coreboot_startup = kern/arm/startup.S;
>     arm_efi_startup = kern/arm/efi/startup.S;
>     arm64_efi_startup = kern/arm64/efi/startup.S;
> +  loongarch64_efi_startup = kern/loongarch64/efi/startup.S;
>     riscv32_efi_startup = kern/riscv/efi/startup.S;
>     riscv64_efi_startup = kern/riscv/efi/startup.S;
>   
> @@ -258,6 +262,9 @@ kernel = {
>     arm64_efi = kern/arm64/efi/init.c;
>     arm64_efi = kern/efi/fdt.c;
>   
> +  loongarch64_efi = kern/loongarch64/efi/init.c;
> +  loongarch64_efi = kern/efi/fdt.c;
> +
>     riscv32_efi = kern/riscv/efi/init.c;
>     riscv32_efi = kern/efi/fdt.c;
>   
> @@ -336,6 +343,11 @@ kernel = {
>     arm64 = kern/arm64/dl.c;
>     arm64 = kern/arm64/dl_helper.c;
>   
> +  loongarch64 = kern/loongarch64/cache.c;
> +  loongarch64 = kern/loongarch64/cache_flush.S;
> +  loongarch64 = kern/loongarch64/dl.c;
> +  loongarch64 = kern/loongarch64/dl_helper.c;
> +
>     riscv32 = kern/riscv/cache.c;
>     riscv32 = kern/riscv/cache_flush.S;
>     riscv32 = kern/riscv/dl.c;
> @@ -827,6 +839,7 @@ module = {
>     enable = arm64_efi;
>     enable = arm_uboot;
>     enable = arm_coreboot;
> +  enable = loongarch64_efi;
>     enable = riscv32_efi;
>     enable = riscv64_efi;
>   };
> @@ -1809,6 +1822,7 @@ module = {
>     arm_efi = loader/arm64/linux.c;
>     arm_uboot = loader/arm/linux.c;
>     arm64 = loader/arm64/linux.c;
> +  loongarch64 = loader/loongarch64/linux.c;
>     riscv32 = loader/riscv/linux.c;
>     riscv64 = loader/riscv/linux.c;
>     common = loader/linux.c;
> @@ -1905,6 +1919,7 @@ module = {
>     enable = ia64_efi;
>     enable = arm_efi;
>     enable = arm64_efi;
> +  enable = loongarch64_efi;
>     enable = riscv32_efi;
>     enable = riscv64_efi;
>     enable = mips;
> diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
> index 6c8d06e15..d0bc350f3 100644
> --- a/include/grub/efi/api.h
> +++ b/include/grub/efi/api.h
> @@ -1731,7 +1731,7 @@ typedef struct grub_efi_rng_protocol grub_efi_rng_protocol_t;
>   
>   #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \
>     || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__) \
> -  || defined(__riscv)
> +  || defined(__riscv) || defined (__loongarch__)
>   
>   #define efi_call_0(func)		func()
>   #define efi_call_1(func, a)		func(a)
> diff --git a/include/grub/util/install.h b/include/grub/util/install.h
> index 7df3191f4..51e84048d 100644
> --- a/include/grub/util/install.h
> +++ b/include/grub/util/install.h
> @@ -107,6 +107,7 @@ enum grub_install_plat
>       GRUB_INSTALL_PLATFORM_I386_XEN_PVH,
>       GRUB_INSTALL_PLATFORM_ARM64_EFI,
>       GRUB_INSTALL_PLATFORM_ARM_COREBOOT,
> +    GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI,
>       GRUB_INSTALL_PLATFORM_RISCV32_EFI,
>       GRUB_INSTALL_PLATFORM_RISCV64_EFI,
>       GRUB_INSTALL_PLATFORM_MAX
> diff --git a/util/grub-install-common.c b/util/grub-install-common.c
> index fe77e3945..0514ac2e6 100644
> --- a/util/grub-install-common.c
> +++ b/util/grub-install-common.c
> @@ -881,30 +881,31 @@ static struct
>     const char *platform;
>   } platforms[GRUB_INSTALL_PLATFORM_MAX] =
>     {
> -    [GRUB_INSTALL_PLATFORM_I386_PC] =          { "i386",    "pc"        },
> -    [GRUB_INSTALL_PLATFORM_I386_EFI] =         { "i386",    "efi"       },
> -    [GRUB_INSTALL_PLATFORM_I386_QEMU] =        { "i386",    "qemu"      },
> -    [GRUB_INSTALL_PLATFORM_I386_COREBOOT] =    { "i386",    "coreboot"  },
> -    [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] =   { "i386",    "multiboot" },
> -    [GRUB_INSTALL_PLATFORM_I386_IEEE1275] =    { "i386",    "ieee1275"  },
> -    [GRUB_INSTALL_PLATFORM_X86_64_EFI] =       { "x86_64",  "efi"       },
> -    [GRUB_INSTALL_PLATFORM_I386_XEN] =         { "i386",    "xen"       },
> -    [GRUB_INSTALL_PLATFORM_X86_64_XEN] =       { "x86_64",  "xen"       },
> -    [GRUB_INSTALL_PLATFORM_I386_XEN_PVH] =     { "i386",    "xen_pvh"   },
> -    [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] =  { "mipsel",  "loongson"  },
> -    [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel",  "qemu_mips" },
> -    [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] =   { "mips",    "qemu_mips" },
> -    [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] =       { "mipsel",  "arc"       },
> -    [GRUB_INSTALL_PLATFORM_MIPS_ARC] =         { "mips",    "arc"       },
> -    [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275"  },
> -    [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275"  },
> -    [GRUB_INSTALL_PLATFORM_IA64_EFI] =         { "ia64",    "efi"       },
> -    [GRUB_INSTALL_PLATFORM_ARM_EFI] =          { "arm",     "efi"       },
> -    [GRUB_INSTALL_PLATFORM_ARM64_EFI] =        { "arm64",   "efi"       },
> -    [GRUB_INSTALL_PLATFORM_ARM_UBOOT] =        { "arm",     "uboot"     },
> -    [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] =     { "arm",     "coreboot"  },
> -    [GRUB_INSTALL_PLATFORM_RISCV32_EFI] =      { "riscv32", "efi"       },
> -    [GRUB_INSTALL_PLATFORM_RISCV64_EFI] =      { "riscv64", "efi"       },
> +    [GRUB_INSTALL_PLATFORM_I386_PC] =          { "i386",        "pc"        },
> +    [GRUB_INSTALL_PLATFORM_I386_EFI] =         { "i386",        "efi"       },
> +    [GRUB_INSTALL_PLATFORM_I386_QEMU] =        { "i386",        "qemu"      },
> +    [GRUB_INSTALL_PLATFORM_I386_COREBOOT] =    { "i386",        "coreboot"  },
> +    [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] =   { "i386",        "multiboot" },
> +    [GRUB_INSTALL_PLATFORM_I386_IEEE1275] =    { "i386",        "ieee1275"  },
> +    [GRUB_INSTALL_PLATFORM_X86_64_EFI] =       { "x86_64",      "efi"       },
> +    [GRUB_INSTALL_PLATFORM_I386_XEN] =         { "i386",        "xen"       },
> +    [GRUB_INSTALL_PLATFORM_X86_64_XEN] =       { "x86_64",      "xen"       },
> +    [GRUB_INSTALL_PLATFORM_I386_XEN_PVH] =     { "i386",        "xen_pvh"   },
> +    [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] =  { "mipsel",      "loongson"  },
> +    [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel",      "qemu_mips" },
> +    [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] =   { "mips",        "qemu_mips" },
> +    [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] =       { "mipsel",      "arc"       },
> +    [GRUB_INSTALL_PLATFORM_MIPS_ARC] =         { "mips",        "arc"       },
> +    [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64",     "ieee1275"  },
> +    [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc",     "ieee1275"  },
> +    [GRUB_INSTALL_PLATFORM_IA64_EFI] =         { "ia64",        "efi"       },
> +    [GRUB_INSTALL_PLATFORM_ARM_EFI] =          { "arm",         "efi"       },
> +    [GRUB_INSTALL_PLATFORM_ARM64_EFI] =        { "arm64",       "efi"       },
> +    [GRUB_INSTALL_PLATFORM_ARM_UBOOT] =        { "arm",         "uboot"     },
> +    [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] =     { "arm",         "coreboot"  },
> +    [GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI] =  { "loongarch64", "efi"       },
> +    [GRUB_INSTALL_PLATFORM_RISCV32_EFI] =      { "riscv32",     "efi"       },
> +    [GRUB_INSTALL_PLATFORM_RISCV64_EFI] =      { "riscv64",     "efi"       },
>     };
>   
>   char *
> diff --git a/util/grub-install.c b/util/grub-install.c
> index 0fbe7f78c..25fdaba7f 100644
> --- a/util/grub-install.c
> +++ b/util/grub-install.c
> @@ -324,6 +324,8 @@ get_default_platform (void)
>      return "arm64-efi";
>   #elif defined (__amd64__) || defined (__x86_64__) || defined (__i386__)
>      return grub_install_get_default_x86_platform ();
> +#elif defined (__loongarch64)
> +   return "loongarch64-efi";
>   #elif defined (__riscv)
>   #if __riscv_xlen == 32
>      return "riscv32-efi";
> @@ -485,6 +487,7 @@ have_bootdev (enum grub_install_plat pl)
>       case GRUB_INSTALL_PLATFORM_IA64_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM64_EFI:
> +    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
>       case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
> @@ -910,6 +913,7 @@ main (int argc, char *argv[])
>       case GRUB_INSTALL_PLATFORM_X86_64_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM64_EFI:
> +    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
>       case GRUB_INSTALL_PLATFORM_IA64_EFI:
> @@ -957,6 +961,7 @@ main (int argc, char *argv[])
>       case GRUB_INSTALL_PLATFORM_X86_64_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM64_EFI:
> +    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
>       case GRUB_INSTALL_PLATFORM_IA64_EFI:
> @@ -1012,6 +1017,7 @@ main (int argc, char *argv[])
>       case GRUB_INSTALL_PLATFORM_X86_64_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM64_EFI:
> +    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
>       case GRUB_INSTALL_PLATFORM_IA64_EFI:
> @@ -1127,6 +1133,9 @@ main (int argc, char *argv[])
>   	    case GRUB_INSTALL_PLATFORM_ARM64_EFI:
>   	      efi_file = "BOOTAA64.EFI";
>   	      break;
> +	    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
> +	      efi_file = "BOOTLOONGARCH64.EFI";
> +	      break;
>   	    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
>   	      efi_file = "BOOTRISCV32.EFI";
>   	      break;
> @@ -1160,6 +1169,9 @@ main (int argc, char *argv[])
>   	    case GRUB_INSTALL_PLATFORM_ARM64_EFI:
>   	      efi_file = "grubaa64.efi";
>   	      break;
> +	    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
> +	      efi_file = "grubloongarch64.efi";
> +	      break;
>   	    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
>   	      efi_file = "grubriscv32.efi";
>   	      break;
> @@ -1468,6 +1480,7 @@ main (int argc, char *argv[])
>   		  case GRUB_INSTALL_PLATFORM_X86_64_EFI:
>   		  case GRUB_INSTALL_PLATFORM_ARM_EFI:
>   		  case GRUB_INSTALL_PLATFORM_ARM64_EFI:
> +		  case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
>   		  case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
>   		  case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
>   		  case GRUB_INSTALL_PLATFORM_IA64_EFI:
> @@ -1563,6 +1576,7 @@ main (int argc, char *argv[])
>       case GRUB_INSTALL_PLATFORM_X86_64_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM64_EFI:
> +    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
>       case GRUB_INSTALL_PLATFORM_IA64_EFI:
> @@ -1668,6 +1682,7 @@ main (int argc, char *argv[])
>         break;
>       case GRUB_INSTALL_PLATFORM_ARM_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM64_EFI:
> +    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
>       case GRUB_INSTALL_PLATFORM_IA64_EFI:
> @@ -1922,6 +1937,7 @@ main (int argc, char *argv[])
>         /* FALLTHROUGH */
>       case GRUB_INSTALL_PLATFORM_ARM_EFI:
>       case GRUB_INSTALL_PLATFORM_ARM64_EFI:
> +    case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
>       case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
>       case GRUB_INSTALL_PLATFORM_IA64_EFI:
> diff --git a/util/grub-mknetdir.c b/util/grub-mknetdir.c
> index a2461cda1..016fc11b0 100644
> --- a/util/grub-mknetdir.c
> +++ b/util/grub-mknetdir.c
> @@ -108,6 +108,7 @@ static const struct
>       [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64-efi", "efinet", ".efi" },
>       [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm-efi", "efinet", ".efi" },
>       [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64-efi", "efinet", ".efi" },
> +    [GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI] = { "loongarch64-efi", "efinet", ".efi" },
>       [GRUB_INSTALL_PLATFORM_RISCV32_EFI] = { "riscv32-efi", "efinet", ".efi" },
>       [GRUB_INSTALL_PLATFORM_RISCV64_EFI] = { "riscv64-efi", "efinet", ".efi" },
>     };
> diff --git a/util/grub-mkrescue.c b/util/grub-mkrescue.c
> index 59c48b123..d36832ca8 100644
> --- a/util/grub-mkrescue.c
> +++ b/util/grub-mkrescue.c
> @@ -742,6 +742,7 @@ main (int argc, char *argv[])
>         || source_dirs[GRUB_INSTALL_PLATFORM_IA64_EFI]
>         || source_dirs[GRUB_INSTALL_PLATFORM_ARM_EFI]
>         || source_dirs[GRUB_INSTALL_PLATFORM_ARM64_EFI]
> +      || source_dirs[GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI]
For grub-mkrescue.c, there is a snippet located above (at around line 
544) similar to the hunk here; does that need changing too?
>         || source_dirs[GRUB_INSTALL_PLATFORM_RISCV32_EFI]
>         || source_dirs[GRUB_INSTALL_PLATFORM_RISCV64_EFI])
>       {
> @@ -778,6 +779,12 @@ main (int argc, char *argv[])
>   			     imgname);
>         free (imgname);
>   
> +      imgname = grub_util_path_concat (2, efidir_efi_boot, "bootloongarch64.efi");
> +      make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI,
> +			     "loongarch64-efi",
> +			     imgname);
> +      free (imgname);
> +
>         imgname = grub_util_path_concat (2, efidir_efi_boot, "bootriscv32.efi");
>         make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_RISCV32_EFI, "riscv32-efi",
>   			     imgname);
> diff --git a/util/mkimage.c b/util/mkimage.c
> index a26cf76f7..f24073545 100644
> --- a/util/mkimage.c
> +++ b/util/mkimage.c
> @@ -622,6 +622,22 @@ static const struct grub_install_image_target_desc image_targets[] =
>         .pe_target = GRUB_PE32_MACHINE_ARM64,
>         .elf_target = EM_AARCH64,
>       },
> +    {
> +      .dirname = "loongarch64-efi",
> +      .names = { "loongarch64-efi", NULL },
> +      .voidp_sizeof = 8,
> +      .bigendian = 0,
> +      .id = IMAGE_EFI,
> +      .flags = PLATFORM_FLAGS_NONE,
> +      .total_module_size = TARGET_NO_FIELD,
> +      .decompressor_compressed_size = TARGET_NO_FIELD,
> +      .decompressor_uncompressed_size = TARGET_NO_FIELD,
> +      .decompressor_uncompressed_addr = TARGET_NO_FIELD,
> +      .section_align = GRUB_PE32_SECTION_ALIGNMENT,
> +      .vaddr_offset = EFI64_HEADER_SIZE,
> +      .pe_target = GRUB_PE32_MACHINE_LOONGARCH64,
> +      .elf_target = EM_LOONGARCH,
> +    },
>       {
>         .dirname = "riscv32-efi",
>         .names = { "riscv32-efi", NULL },

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 36281 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 6/8] LoongArch: Add awareness for LoongArch relocations
  2022-02-23 13:46 ` [PATCH v2 6/8] LoongArch: Add awareness for LoongArch relocations Xiaotian Wu
@ 2022-02-23 15:53   ` WANG Xuerui
  2022-02-24  8:00     ` 武校田
  0 siblings, 1 reply; 19+ messages in thread
From: WANG Xuerui @ 2022-02-23 15:53 UTC (permalink / raw)
  To: Xiaotian Wu, grub-devel; +Cc: Zhou Yang


[-- Attachment #1.1.1: Type: text/plain, Size: 18246 bytes --]

Hi,

On 2/23/22 21:46, Xiaotian Wu wrote:
> Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
> Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
> ---
>   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                  |  77 ++++++++++
>   util/grub-module-verifier.c            |  26 ++++
>   6 files changed, 410 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 6a52de168..e58663279 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 000000000..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 <http://www.gnu.org/licenses/>.
> + */
> +
> +#include <grub/dl.h>
> +#include <grub/elf.h>
> +#include <grub/misc.h>
> +#include <grub/err.h>
> +#include <grub/types.h>
> +#include <grub/mm.h>
> +#include <grub/i18n.h>
> +#include <grub/cpu/reloc.h>
> +
> +/* 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)
We are at patch 6 right now, yet this macro is only being introduced in 
patch 7. So maybe reorder or reorganize a bit, to make each step buildable?
> +	default:
> +	  {
> +	    char rel_info[17]; /* log16(2^64) = 16, plus NUL.  */
> +
> +	    grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
> +			   (grub_uint64_t) ELF_R_TYPE (rel->r_info));
> +	    return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
> +			       N_("relocation 0x%s is not implemented yet"), rel_info);
> +	  }
> +	  break;
> +	}
> +    }
> +  return GRUB_ERR_NONE;
> +}
> diff --git a/grub-core/kern/loongarch64/dl_helper.c b/grub-core/kern/loongarch64/dl_helper.c
> new file mode 100644
> index 000000000..becc89565
> --- /dev/null
> +++ b/grub-core/kern/loongarch64/dl_helper.c
> @@ -0,0 +1,198 @@
> +/* dl_helper.c - relocation helper functions for modules and grub-mkimage */
> +/*
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +
> +#include <grub/dl.h>
> +#include <grub/elf.h>
> +#include <grub/misc.h>
> +#include <grub/err.h>
> +#include <grub/mm.h>
> +#include <grub/i18n.h>
> +#include <grub/cpu/reloc.h>
> +
> +static void grub_loongarch64_stack_push (grub_loongarch64_stack_t stack, grub_uint64_t x);
> +static grub_uint64_t grub_loongarch64_stack_pop (grub_loongarch64_stack_t stack);
> +
> +void
> +grub_loongarch64_stack_init (grub_loongarch64_stack_t stack)
> +{
> +  stack->top = -1;
> +  stack->count = LOONGARCH64_STACK_MAX;
> +}
> +
> +static void
> +grub_loongarch64_stack_push (grub_loongarch64_stack_t stack, grub_uint64_t x)
> +{
> +  if (stack->top == stack->count)
> +    return;
> +  stack->data[++stack->top] = x;
> +}
> +
> +static grub_uint64_t
> +grub_loongarch64_stack_pop (grub_loongarch64_stack_t stack)
> +{
> +  if (stack->top == -1)
> +    return -1;
> +  return stack->data[stack->top--];
> +}
> +
> +void
> +grub_loongarch64_sop_push (grub_loongarch64_stack_t stack, grub_int64_t offset)
> +{
> +  grub_loongarch64_stack_push (stack, offset);
> +}
> +
> +/* opr2 = pop (), opr1 = pop (), push (opr1 - opr2) */
> +void
> +grub_loongarch64_sop_sub (grub_loongarch64_stack_t stack)
> +{
> +  grub_uint64_t a, b;
> +  b = grub_loongarch64_stack_pop (stack);
> +  a = grub_loongarch64_stack_pop (stack);
> +  grub_loongarch64_stack_push (stack, a - b);
> +}
> +
> +/* opr2 = pop (), opr1 = pop (), push (opr1 << opr2) */
> +void
> +grub_loongarch64_sop_sl (grub_loongarch64_stack_t stack)
> +{
> +  grub_uint64_t a, b;
> +  b = grub_loongarch64_stack_pop (stack);
> +  a = grub_loongarch64_stack_pop (stack);
> +  grub_loongarch64_stack_push (stack, a << b);
> +}
> +
> +/* opr2 = pop (), opr1 = pop (), push (opr1 >> opr2) */
> +void
> +grub_loongarch64_sop_sr (grub_loongarch64_stack_t stack)
> +{
> +  grub_uint64_t a, b;
> +  b = grub_loongarch64_stack_pop (stack);
> +  a = grub_loongarch64_stack_pop (stack);
> +  grub_loongarch64_stack_push (stack, a >> b);
> +}
> +
> +/* opr2 = pop (), opr1 = pop (), push (opr1 + opr2) */
> +void
> +grub_loongarch64_sop_add (grub_loongarch64_stack_t stack)
> +{
> +  grub_uint64_t a, b;
> +  b = grub_loongarch64_stack_pop (stack);
> +  a = grub_loongarch64_stack_pop (stack);
> +  grub_loongarch64_stack_push (stack, a + b);
> +}
> +
> +/* opr2 = pop (), opr1 = pop (), push (opr1 & opr2) */
> +void
> +grub_loongarch64_sop_and (grub_loongarch64_stack_t stack)
> +{
> +  grub_uint64_t a, b;
> +  b = grub_loongarch64_stack_pop (stack);
> +  a = grub_loongarch64_stack_pop (stack);
> +  grub_loongarch64_stack_push (stack, a & b);
> +}
> +
> +/* opr3 = pop (), opr2 = pop (), opr1 = pop (), push (opr1 ? opr2 : opr3) */
> +void
> +grub_loongarch64_sop_if_else (grub_loongarch64_stack_t stack)
> +{
> +  grub_uint64_t a, b, c;
> +  c = grub_loongarch64_stack_pop (stack);
> +  b = grub_loongarch64_stack_pop (stack);
> +  a = grub_loongarch64_stack_pop (stack);
> +
> +  if (a) {
> +      grub_loongarch64_stack_push (stack, b);
> +  } else {
> +      grub_loongarch64_stack_push (stack, c);
> +  }
> +}
> +
> +/* opr1 = pop (), (*(uint32_t *) PC) [14 ... 10] = opr1 [4 ... 0] */
> +void
> +grub_loongarch64_sop_32_s_10_5 (grub_loongarch64_stack_t stack,
> +				grub_uint64_t *place)
> +{
> +  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
> +  *place |= ((a & 0x1f) << 10);
> +}
> +
> +/* opr1 = pop (), (*(uint32_t *) PC) [21 ... 10] = opr1 [11 ... 0] */
> +void
> +grub_loongarch64_sop_32_u_10_12 (grub_loongarch64_stack_t stack,
> +				 grub_uint64_t *place)
> +{
> +  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
> +  *place = *place | ((a & 0xfff) << 10);
> +}
> +
> +/* opr1 = pop (), (*(uint32_t *) PC) [21 ... 10] = opr1 [11 ... 0] */
> +void
> +grub_loongarch64_sop_32_s_10_12 (grub_loongarch64_stack_t stack,
> +				 grub_uint64_t *place)
> +{
> +  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
> +  *place = (*place) | ((a & 0xfff) << 10);
> +}
> +
> +/* opr1 = pop (), (*(uint32_t *) PC) [25 ... 10] = opr1 [15 ... 0] */
> +void
> +grub_loongarch64_sop_32_s_10_16 (grub_loongarch64_stack_t stack,
> +				 grub_uint64_t *place)
> +{
> +  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
> +  *place = (*place) | ((a & 0xffff) << 10);
> +}
> +
> +/* opr1 = pop (), (*(uint32_t *) PC) [25 ... 10] = opr1 [17 ... 2] */
> +void
> +grub_loongarch64_sop_32_s_10_16_s2 (grub_loongarch64_stack_t stack,
> +				    grub_uint64_t *place)
> +{
> +  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
> +  *place = (*place) | (((a >> 2) & 0xffff) << 10);
> +}
> +
> +/* opr1 = pop (), (*(uint32_t *) PC) [24 ... 5] = opr1 [19 ... 0] */
> +void
> +grub_loongarch64_sop_32_s_5_20 (grub_loongarch64_stack_t stack, grub_uint64_t *place)
> +{
> +  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
> +  *place = (*place) | ((a & 0xfffff)<<5);
> +}
> +
> +/* opr1 = pop (), (*(uint32_t *) PC) [4 ... 0] = opr1 [22 ... 18] */
> +void
> +grub_loongarch64_sop_32_s_0_5_10_16_s2 (grub_loongarch64_stack_t stack,
> +					grub_uint64_t *place)
> +{
> +  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
> +
> +  *place =(*place) | (((a >> 2) & 0xffff) << 10);
> +  *place =(*place) | ((a >> 18) & 0x1f);
> +}
> +
> +/* opr1 = pop (), (*(uint32_t *) PC) [9 ... 0] = opr1 [27 ... 18] */
> +void
> +grub_loongarch64_sop_32_s_0_10_10_16_s2 (grub_loongarch64_stack_t stack,
> +					 grub_uint64_t *place)
> +{
> +  grub_uint64_t a = grub_loongarch64_stack_pop (stack);
> +  *place =(*place) | (((a >> 2) & 0xffff) << 10);
> +  *place =(*place) | ((a >> 18) & 0x3ff);
> +}
> diff --git a/include/grub/dl.h b/include/grub/dl.h
> index b3753c9ca..3b9b19933 100644
> --- a/include/grub/dl.h
> +++ b/include/grub/dl.h
> @@ -301,6 +301,7 @@ grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
>   #endif
>   
>   #if defined (__aarch64__) || defined (__sparc__) || \
> +    defined (__loongarch64) || \
>       (defined(__riscv) && (__riscv_xlen == 64))
>   #define GRUB_ARCH_DL_TRAMP_ALIGN 8
>   #define GRUB_ARCH_DL_GOT_ALIGN 8
> diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
> index d78fa3e53..2136ee36f 100644
> --- a/util/grub-mkimagexx.c
> +++ b/util/grub-mkimagexx.c
> @@ -44,6 +44,7 @@
>   #include <grub/arm/reloc.h>
>   #include <grub/arm64/reloc.h>
>   #include <grub/ia64/reloc.h>
> +#include <grub/loongarch64/reloc.h>
>   #include <grub/osdep/hostfile.h>
>   #include <grub/util/install.h>
>   #include <grub/util/mkimage.h>
> @@ -784,6 +785,8 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
>     struct grub_ia64_trampoline *tr = (void *) (pe_target + tramp_off);
>     grub_uint64_t *gpptr = (void *) (pe_target + got_off);
>     unsigned unmatched_adr_got_page = 0;
> +  struct grub_loongarch64_stack stack;
> +  grub_loongarch64_stack_init (&stack);
>   #define MASK19 ((1 << 19) - 1)
>   #else
>     grub_uint32_t *tr = (void *) (pe_target + tramp_off);
> @@ -1123,6 +1126,31 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
>   		   }
>   	       break;
>   	       }
> +	     case EM_LOONGARCH:
> +	       {
> +		 sym_addr += addend;
> +		 switch (ELF_R_TYPE (info))
> +		   {
> +		   case R_LARCH_64:
> +		     *target = grub_host_to_target64 (grub_target_to_host64 (*target) + 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
> +						-(target_section_addr
> +						  +offset
> +						  +image_target->vaddr_offset));
> +		     break;
> +		   GRUB_LOONGARCH64_RELOCATION (&stack, target, sym_addr)
> +		   default:
> +		     grub_util_error (_("relocation 0x%x is not implemented yet"),
> +				      (unsigned int) ELF_R_TYPE (info));
> +		     break;
> +		   }
> +		 break;
> +	       }
>   #endif
>   #if defined(MKIMAGE_ELF32)
>   	     case EM_ARM:
> @@ -1668,6 +1696,55 @@ translate_relocation_pe (struct translate_context *ctx,
>   	}
>         break;
>         break;
> +    case EM_LOONGARCH:
> +      switch (ELF_R_TYPE (info))
> +	{
> +	case R_LARCH_64:
> +	  {
> +	    ctx->current_address = add_fixup_entry (&ctx->lst,
> +						    GRUB_PE32_REL_BASED_DIR64,
> +						    addr, 0, ctx->current_address,
> +						    image_target);
> +	  }
> +	  break;
> +	case R_LARCH_MARK_LA:
> +	  {
> +	    ctx->current_address = add_fixup_entry (&ctx->lst,
> +						    GRUB_PE32_REL_BASED_LOONGARCH64_MARK_LA,
> +						    addr, 0, ctx->current_address,
> +						    image_target);
> +	  }
> +	  break;
> +	  /* Relative relocations do not require fixup entries. */
> +	case R_LARCH_NONE:
> +	case R_LARCH_SOP_PUSH_PCREL:
> +	case R_LARCH_SOP_PUSH_ABSOLUTE:
> +	case R_LARCH_SOP_PUSH_PLT_PCREL:
> +	case R_LARCH_SOP_SUB:
> +	case R_LARCH_SOP_SL:
> +	case R_LARCH_SOP_SR:
> +	case R_LARCH_SOP_ADD:
> +	case R_LARCH_SOP_AND:
> +	case R_LARCH_SOP_IF_ELSE:
> +	case R_LARCH_SOP_POP_32_S_10_5:
> +	case R_LARCH_SOP_POP_32_U_10_12:
> +	case R_LARCH_SOP_POP_32_S_10_12:
> +	case R_LARCH_SOP_POP_32_S_10_16:
> +	case R_LARCH_SOP_POP_32_S_10_16_S2:
> +	case R_LARCH_SOP_POP_32_S_5_20:
> +	case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
> +	case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
> +	  grub_util_info ("  %s:  not adding fixup: 0x%08x : 0x%08x",
> +			  __FUNCTION__,
> +			  (unsigned int) addr,
> +			  (unsigned int) ctx->current_address);
> +	  break;
> +	default:
> +	  grub_util_error (_("relocation 0x%x is not implemented yet"),
> +			   (unsigned int) ELF_R_TYPE (info));
> +	  break;
> +	}
> +      break;
>   #if defined(MKIMAGE_ELF32)
>       case EM_ARM:
>         switch (ELF_R_TYPE (info))
> diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c
> index 163529ca9..b510461fa 100644
> --- a/util/grub-module-verifier.c
> +++ b/util/grub-module-verifier.c
> @@ -119,6 +119,32 @@ struct grub_module_verifier_arch archs[] = {
>         R_AARCH64_PREL32,
>         -1
>       } },
> +  { "loongarch64", 8, 0, EM_LOONGARCH, GRUB_MODULE_VERIFY_SUPPORTS_REL | GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
> +      R_LARCH_NONE,
> +      R_LARCH_64,
> +      R_LARCH_MARK_LA,
> +      R_LARCH_SOP_PUSH_PCREL,
> +      R_LARCH_SOP_PUSH_ABSOLUTE,
> +      R_LARCH_SOP_PUSH_PLT_PCREL,
> +      R_LARCH_SOP_SUB,
> +      R_LARCH_SOP_SL,
> +      R_LARCH_SOP_SR,
> +      R_LARCH_SOP_ADD,
> +      R_LARCH_SOP_AND,
> +      R_LARCH_SOP_IF_ELSE,
> +      R_LARCH_SOP_POP_32_S_10_5,
> +      R_LARCH_SOP_POP_32_U_10_12,
> +      R_LARCH_SOP_POP_32_S_10_12,
> +      R_LARCH_SOP_POP_32_S_10_16,
> +      R_LARCH_SOP_POP_32_S_10_16_S2,
> +      R_LARCH_SOP_POP_32_S_5_20,
> +      R_LARCH_SOP_POP_32_S_0_5_10_16_S2,
> +      R_LARCH_SOP_POP_32_S_0_10_10_16_S2,
> +      -1
> +    }, (int[]){
> +      -1
> +    }
> +  },
>     { "riscv32", 4, 0, EM_RISCV, GRUB_MODULE_VERIFY_SUPPORTS_REL | GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
>         R_RISCV_32,
>         R_RISCV_64,

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 36281 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 1/8] PE: Add LoongArch definitions
  2022-02-23 13:46 ` [PATCH v2 1/8] PE: Add LoongArch definitions Xiaotian Wu
@ 2022-02-23 15:57   ` WANG Xuerui
  0 siblings, 0 replies; 19+ messages in thread
From: WANG Xuerui @ 2022-02-23 15:57 UTC (permalink / raw)
  To: Xiaotian Wu, grub-devel; +Cc: Zhou Yang


[-- Attachment #1.1.1: Type: text/plain, Size: 2813 bytes --]


On 2/23/22 21:46, Xiaotian Wu wrote:
> Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
> Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
> ---
>   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_ARM64			0xAA64
> +#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_LOW		2
> -#define GRUB_PE32_REL_BASED_HIGHLOW	3
> -#define GRUB_PE32_REL_BASED_HIGHADJ	4
> -#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_SECTION	6
> -#define GRUB_PE32_REL_BASED_REL		7
> -#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_LOW			2
> +#define GRUB_PE32_REL_BASED_HIGHLOW		3
> +#define GRUB_PE32_REL_BASED_HIGHADJ		4
> +#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_SECTION		6
> +#define GRUB_PE32_REL_BASED_REL			7
> +#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_LA	8
> +#define GRUB_PE32_REL_BASED_LOONGARCH64_MARK_LA	8
> +#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
>   {

This is consistent with the updated PE format docs [1] (added with this 
Microsoft PR [2]), so:

Reviewed-by: WANG Xuerui <git@xen0n.name>

[1]: https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
[2]: https://github.com/MicrosoftDocs/win32/pull/1067


[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 36281 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 2/8] Add LoongArch definitions
  2022-02-23 13:46 ` [PATCH v2 2/8] " Xiaotian Wu
@ 2022-02-23 16:01   ` WANG Xuerui
  0 siblings, 0 replies; 19+ messages in thread
From: WANG Xuerui @ 2022-02-23 16:01 UTC (permalink / raw)
  To: Xiaotian Wu, grub-devel; +Cc: Zhou Yang


[-- Attachment #1.1.1: Type: text/plain, Size: 2192 bytes --]

On 2/23/22 21:46, Xiaotian Wu wrote:

> Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
> Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
> ---
>   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_PCREL        57
>   
> +/* 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_ABSOLUTE	      23
> +#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_5	      38
> +#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_S2	      42
> +#define R_LARCH_SOP_POP_32_S_5_20	      43
> +#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2     44
> +#define R_LARCH_SOP_POP_32_S_0_10_10_16_S2    45
> +
>   #ifdef GRUB_TARGET_WORDSIZE
>   #if GRUB_TARGET_WORDSIZE == 32
>   

This is consistent with the binutils definitions [1], trimmed down to 
remove reloc types unused by GRUB, so:

Reviewed-by: WANG Xuerui <git@xen0n.name>

[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=include/elf/loongarch.h;h=1893ef5faae6a42eec31ecedd6701e18625840c8;hb=HEAD


[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 36281 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 3/8] LoongArch: Add setjmp implementation
  2022-02-23 13:46 ` [PATCH v2 3/8] LoongArch: Add setjmp implementation Xiaotian Wu
@ 2022-02-23 16:13   ` WANG Xuerui
  0 siblings, 0 replies; 19+ messages in thread
From: WANG Xuerui @ 2022-02-23 16:13 UTC (permalink / raw)
  To: Xiaotian Wu, grub-devel; +Cc: Zhou Yang, Sun Haiyong


[-- Attachment #1.1.1: Type: text/plain, Size: 4656 bytes --]

On 2/23/22 21:46, Xiaotian Wu wrote:
> Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
> Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
> Signed-off-by: Sun Haiyong <sunhaiyong@loongson.cn>
> ---
>   grub-core/lib/loongarch64/setjmp.S | 68 ++++++++++++++++++++++++++++++
>   grub-core/lib/setjmp.S             |  2 +
>   include/grub/loongarch64/setjmp.h  | 27 ++++++++++++
>   3 files changed, 97 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 000000000..c11fe63ea
> --- /dev/null
> +++ b/grub-core/lib/loongarch64/setjmp.S
> @@ -0,0 +1,68 @@
> +/*
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +
> +#include <grub/dl.h>
> +
> +	.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
> +
> +	li.w $a0, 1
> +	beqz $a1, .L0
> +	move $a0, $a1
> +.L0:
While this label name could just be "1" and the beqz reference changed 
to "1f", I don't have really strong opinion, besides perhaps adding a 
comment explaining the double return logic would be useful (refer to the 
riscv version's comment).
> +	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 000000000..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 <http://www.gnu.org/licenses/>.
> + */
> +
> +#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 */

With the nit addressed:

Reviewed-by: WANG Xuerui <git@xen0n.name>


[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 36281 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 4/8] LoongArch: Add early startup code
  2022-02-23 13:46 ` [PATCH v2 4/8] LoongArch: Add early startup code Xiaotian Wu
@ 2022-02-23 16:15   ` WANG Xuerui
  0 siblings, 0 replies; 19+ messages in thread
From: WANG Xuerui @ 2022-02-23 16:15 UTC (permalink / raw)
  To: Xiaotian Wu, grub-devel; +Cc: Zhou Yang


[-- Attachment #1.1.1: Type: text/plain, Size: 1769 bytes --]

On 2/23/22 21:46, Xiaotian Wu wrote:
> Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
> Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
> ---
>   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 000000000..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 <http://www.gnu.org/licenses/>.
> + */
> +
> +#include <grub/symbol.h>
> +
> +	.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)
Reviewed-by: WANG Xuerui <git@xen0n.name>

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 36281 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Re: [PATCH v2 0/8] Add support for LoongArch
  2022-02-23 13:59 ` [PATCH v2 0/8] Add support for LoongArch John Paul Adrian Glaubitz
@ 2022-02-24  2:12   ` 武校田
  0 siblings, 0 replies; 19+ messages in thread
From: 武校田 @ 2022-02-24  2:12 UTC (permalink / raw)
  To: John Paul Adrian Glaubitz; +Cc: The development of GNU GRUB, git

&gt; Is there any chance the support for mips64el EFI [1] could be upstreamed as well?

This series of patches is only to support LoongArch.

Maybe you can contact the original author to ask: https://lists.nongnu.org/archive/html/grub-devel/2015-06/msg00038.html

本邮件及其附件含有龙芯中科的商业秘密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制或散发)本邮件及其附件中的信息。如果您错收本邮件,请您立即电话或邮件通知发件人并删除本邮件。 
This email and its attachments contain confidential information from Loongson Technology , which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this email in error, please notify the sender by phone or email immediately and delete it. 

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Re: [PATCH v2 6/8] LoongArch: Add awareness for LoongArch relocations
  2022-02-23 15:53   ` WANG Xuerui
@ 2022-02-24  8:00     ` 武校田
  0 siblings, 0 replies; 19+ messages in thread
From: 武校田 @ 2022-02-24  8:00 UTC (permalink / raw)
  To: WANG Xuerui; +Cc: grub-devel, Zhou Yang



&gt; &gt; diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
&gt; We are at patch 6 right now, yet this macro is only being introduced in 
&gt; patch 7. So maybe reorder or reorganize a bit, to make each step buildable?

Thank you for your review.
Enable LoongArch build in the 8th patch, this file will not be built in other architectures, so it does not break the build.

本邮件及其附件含有龙芯中科的商业秘密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制或散发)本邮件及其附件中的信息。如果您错收本邮件,请您立即电话或邮件通知发件人并删除本邮件。 
This email and its attachments contain confidential information from Loongson Technology , which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this email in error, please notify the sender by phone or email immediately and delete it. 

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 8/8] LoongArch: Add to build system
  2022-02-23 15:47   ` WANG Xuerui
@ 2022-02-24  9:00     ` 武校田
  0 siblings, 0 replies; 19+ messages in thread
From: 武校田 @ 2022-02-24  9:00 UTC (permalink / raw)
  To: WANG Xuerui, grub-devel; +Cc: Zhou Yang


>>
> For grub-mkrescue.c, there is a snippet located above (at around line 
> 544) similar to the hunk here; does that need changing too?
  Yes, I forgot to change the line, thank you.



^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2022-02-24  9:01 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-23 13:46 [PATCH v2 0/8] Add support for LoongArch Xiaotian Wu
2022-02-23 13:46 ` [PATCH v2 1/8] PE: Add LoongArch definitions Xiaotian Wu
2022-02-23 15:57   ` WANG Xuerui
2022-02-23 13:46 ` [PATCH v2 2/8] " Xiaotian Wu
2022-02-23 16:01   ` WANG Xuerui
2022-02-23 13:46 ` [PATCH v2 3/8] LoongArch: Add setjmp implementation Xiaotian Wu
2022-02-23 16:13   ` WANG Xuerui
2022-02-23 13:46 ` [PATCH v2 4/8] LoongArch: Add early startup code Xiaotian Wu
2022-02-23 16:15   ` WANG Xuerui
2022-02-23 13:46 ` [PATCH v2 5/8] LoongArch: Add stubs for Linux loading commands Xiaotian Wu
2022-02-23 13:46 ` [PATCH v2 6/8] LoongArch: Add awareness for LoongArch relocations Xiaotian Wu
2022-02-23 15:53   ` WANG Xuerui
2022-02-24  8:00     ` 武校田
2022-02-23 13:46 ` [PATCH v2 7/8] LoongArch: Add auxiliary files Xiaotian Wu
2022-02-23 13:46 ` [PATCH v2 8/8] LoongArch: Add to build system Xiaotian Wu
2022-02-23 15:47   ` WANG Xuerui
2022-02-24  9:00     ` 武校田
2022-02-23 13:59 ` [PATCH v2 0/8] Add support for LoongArch John Paul Adrian Glaubitz
2022-02-24  2:12   ` 武校田

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.