All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v10 00/11] Add support for LoongArch
@ 2022-08-24  7:26 Xiaotian Wu
  2022-08-24  7:26 ` [PATCH v10 01/11] PE: Add LoongArch definitions Xiaotian Wu
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:26 UTC (permalink / raw)
  To: grub-devel; +Cc: git, development, daniel.kiper, 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 patchset adds support for LoongArch to boot the kernel.

Please review the patches, thank you.

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

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

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

v4->v5:
- Fix make distcheck failed.
- Add tests support.
- based on the last commit.

v5->v6:
- Support new relocation types.
- Fix some test cases.

v6->v7:
- Fix typo
- change for test cases.

v7->v8:
- change for test cases.

v8->v9:
- limit memory allocation to less than 4G

v9->v10:
- based on the last commit.
- add efistub kernel support

Xiaotian Wu (11):
  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
  LoongArch: Support new relocation types
  LoongArch: add efistub kernel support
  tests: add support for LoongArch

 Makefile.util.def                        |   1 +
 conf/Makefile.common                     |   3 +
 configure.ac                             |  24 +-
 gentpl.py                                |  27 +-
 grub-core/Makefile.am                    |   6 +
 grub-core/Makefile.core.def              |  16 +
 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          | 167 ++++++++++
 grub-core/kern/loongarch64/dl_helper.c   | 270 ++++++++++++++++
 grub-core/kern/loongarch64/efi/init.c    |  77 +++++
 grub-core/kern/loongarch64/efi/startup.S |  34 +++
 grub-core/lib/efi/halt.c                 |   2 +-
 grub-core/lib/loongarch64/setjmp.S       |  69 +++++
 grub-core/lib/setjmp.S                   |   2 +
 grub-core/loader/loongarch64/linux.c     | 374 +++++++++++++++++++++++
 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                       |  28 ++
 include/grub/loongarch64/efi/memory.h    |  24 ++
 include/grub/loongarch64/linux.h         |  43 +++
 include/grub/loongarch64/reloc.h         | 111 +++++++
 include/grub/loongarch64/setjmp.h        |  27 ++
 include/grub/loongarch64/time.h          |  28 ++
 include/grub/loongarch64/types.h         |  34 +++
 include/grub/util/install.h              |   1 +
 tests/ahci_test.in                       |   2 +-
 tests/ehci_test.in                       |   2 +-
 tests/ohci_test.in                       |   2 +-
 tests/pata_test.in                       |   2 +-
 tests/uhci_test.in                       |   2 +-
 tests/util/grub-shell.in                 |  14 +
 util/grub-install-common.c               |  49 +--
 util/grub-install.c                      |  16 +
 util/grub-mkimagexx.c                    | 164 +++++++++-
 util/grub-mknetdir.c                     |   1 +
 util/grub-mkrescue.c                     |   8 +
 util/grub-module-verifier.c              |  31 ++
 util/mkimage.c                           |  16 +
 42 files changed, 1729 insertions(+), 64 deletions(-)
 create mode 100644 grub-core/kern/loongarch64/cache.c
 create mode 100644 grub-core/kern/loongarch64/cache_flush.S
 create mode 100644 grub-core/kern/loongarch64/dl.c
 create mode 100644 grub-core/kern/loongarch64/dl_helper.c
 create mode 100644 grub-core/kern/loongarch64/efi/init.c
 create mode 100644 grub-core/kern/loongarch64/efi/startup.S
 create mode 100644 grub-core/lib/loongarch64/setjmp.S
 create mode 100644 grub-core/loader/loongarch64/linux.c
 create mode 100644 include/grub/loongarch64/efi/memory.h
 create mode 100644 include/grub/loongarch64/linux.h
 create mode 100644 include/grub/loongarch64/reloc.h
 create mode 100644 include/grub/loongarch64/setjmp.h
 create mode 100644 include/grub/loongarch64/time.h
 create mode 100644 include/grub/loongarch64/types.h

-- 
2.35.1



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

* [PATCH v10 01/11] PE: Add LoongArch definitions
  2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
@ 2022-08-24  7:26 ` Xiaotian Wu
  2022-08-24  7:26 ` [PATCH v10 02/11] " Xiaotian Wu
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:26 UTC (permalink / raw)
  To: grub-devel; +Cc: git, development, daniel.kiper, 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.35.1



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

* [PATCH v10 02/11] Add LoongArch definitions
  2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
  2022-08-24  7:26 ` [PATCH v10 01/11] PE: Add LoongArch definitions Xiaotian Wu
@ 2022-08-24  7:26 ` Xiaotian Wu
  2022-08-24  7:26 ` [PATCH v10 03/11] LoongArch: Add setjmp implementation Xiaotian Wu
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:26 UTC (permalink / raw)
  To: grub-devel; +Cc: git, development, daniel.kiper, 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 0e70651d4..beef94512 100644
--- a/include/grub/elf.h
+++ b/include/grub/elf.h
@@ -252,6 +252,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
@@ -2536,6 +2537,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
+
 extern grub_err_t grub_elf32_get_shnum (Elf32_Ehdr *e, Elf32_Shnum *shnum);
 extern grub_err_t grub_elf32_get_shstrndx (Elf32_Ehdr *e, Elf32_Word *shstrndx);
 extern grub_err_t grub_elf32_get_phnum (Elf32_Ehdr *e, Elf32_Word *phnum);
-- 
2.35.1



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

* [PATCH v10 03/11] LoongArch: Add setjmp implementation
  2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
  2022-08-24  7:26 ` [PATCH v10 01/11] PE: Add LoongArch definitions Xiaotian Wu
  2022-08-24  7:26 ` [PATCH v10 02/11] " Xiaotian Wu
@ 2022-08-24  7:26 ` Xiaotian Wu
  2022-08-24  7:26 ` [PATCH v10 04/11] LoongArch: Add early startup code Xiaotian Wu
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:26 UTC (permalink / raw)
  To: grub-devel
  Cc: git, development, daniel.kiper, 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 | 69 ++++++++++++++++++++++++++++++
 grub-core/lib/setjmp.S             |  2 +
 include/grub/loongarch64/setjmp.h  | 27 ++++++++++++
 3 files changed, 98 insertions(+)
 create mode 100644 grub-core/lib/loongarch64/setjmp.S
 create mode 100644 include/grub/loongarch64/setjmp.h

diff --git a/grub-core/lib/loongarch64/setjmp.S b/grub-core/lib/loongarch64/setjmp.S
new file mode 100644
index 000000000..41d58f569
--- /dev/null
+++ b/grub-core/lib/loongarch64/setjmp.S
@@ -0,0 +1,69 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <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
+
+	/* Return 1 if passed 0, otherwise returns the value in place. */
+	li.w $a0, 1
+	beqz $a1, 1f
+	move $a0, $a1
+1:
+	jr   $ra
diff --git a/grub-core/lib/setjmp.S b/grub-core/lib/setjmp.S
index 9c8721088..cba1d546d 100644
--- a/grub-core/lib/setjmp.S
+++ b/grub-core/lib/setjmp.S
@@ -19,6 +19,8 @@
 #include "./arm/setjmp.S"
 #elif defined(__aarch64__)
 #include "./arm64/setjmp.S"
+#elif defined(__loongarch64)
+#include "./loongarch64/setjmp.S"
 #elif defined(__riscv)
 #include "./riscv/setjmp.S"
 #else
diff --git a/include/grub/loongarch64/setjmp.h b/include/grub/loongarch64/setjmp.h
new file mode 100644
index 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.35.1



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

* [PATCH v10 04/11] LoongArch: Add early startup code
  2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
                   ` (2 preceding siblings ...)
  2022-08-24  7:26 ` [PATCH v10 03/11] LoongArch: Add setjmp implementation Xiaotian Wu
@ 2022-08-24  7:26 ` Xiaotian Wu
  2022-08-24  7:26 ` [PATCH v10 05/11] LoongArch: Add stubs for Linux loading commands Xiaotian Wu
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:26 UTC (permalink / raw)
  To: grub-devel; +Cc: git, development, daniel.kiper, 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.35.1



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

* [PATCH v10 05/11] LoongArch: Add stubs for Linux loading commands
  2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
                   ` (3 preceding siblings ...)
  2022-08-24  7:26 ` [PATCH v10 04/11] LoongArch: Add early startup code Xiaotian Wu
@ 2022-08-24  7:26 ` Xiaotian Wu
  2022-08-24  7:26 ` [PATCH v10 06/11] LoongArch: Add awareness for LoongArch relocations Xiaotian Wu
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:26 UTC (permalink / raw)
  To: grub-devel; +Cc: git, development, daniel.kiper, 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.35.1



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

* [PATCH v10 06/11] LoongArch: Add awareness for LoongArch relocations
  2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
                   ` (4 preceding siblings ...)
  2022-08-24  7:26 ` [PATCH v10 05/11] LoongArch: Add stubs for Linux loading commands Xiaotian Wu
@ 2022-08-24  7:26 ` Xiaotian Wu
  2022-08-24  7:27 ` [PATCH v10 07/11] LoongArch: Add auxiliary files Xiaotian Wu
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:26 UTC (permalink / raw)
  To: grub-devel; +Cc: git, development, daniel.kiper, 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 | 202 +++++++++++++++++++++++++
 include/grub/dl.h                      |   1 +
 include/grub/loongarch64/reloc.h       | 107 +++++++++++++
 util/grub-mkimagexx.c                  |  79 ++++++++++
 util/grub-module-verifier.c            |  26 ++++
 7 files changed, 523 insertions(+), 3 deletions(-)
 create mode 100644 grub-core/kern/loongarch64/dl.c
 create mode 100644 grub-core/kern/loongarch64/dl_helper.c
 create mode 100644 include/grub/loongarch64/reloc.h

diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
index e447fd0fa..0bf40caa6 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..627d1fff2
--- /dev/null
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -0,0 +1,202 @@
+/* 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/loongarch64/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],
+   (*(uint32_t *) PC) [25 ... 10] = opr1 [17 ... 2]
+*/
+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 acb4d4232..cf79b1ce1 100644
--- a/include/grub/dl.h
+++ b/include/grub/dl.h
@@ -300,6 +300,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/include/grub/loongarch64/reloc.h b/include/grub/loongarch64/reloc.h
new file mode 100644
index 000000000..0b7d66ddc
--- /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/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index a1927e786..358b9ab47 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:
@@ -1667,6 +1695,57 @@ translate_relocation_pe (struct translate_context *ctx,
 			   (unsigned int) ELF_R_TYPE (info));
 	  break;
 	}
+#endif /* defined(MKIMAGE_ELF64) */
+      break;
+    case EM_LOONGARCH:
+#if defined(MKIMAGE_ELF64)
+      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;
+	}
 #endif /* defined(MKIMAGE_ELF64) */
       break;
 #if defined(MKIMAGE_ELF32)
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.35.1



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

* [PATCH v10 07/11] LoongArch: Add auxiliary files
  2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
                   ` (5 preceding siblings ...)
  2022-08-24  7:26 ` [PATCH v10 06/11] LoongArch: Add awareness for LoongArch relocations Xiaotian Wu
@ 2022-08-24  7:27 ` Xiaotian Wu
  2022-08-24  7:27 ` [PATCH v10 08/11] LoongArch: Add to build system Xiaotian Wu
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:27 UTC (permalink / raw)
  To: grub-devel; +Cc: git, development, daniel.kiper, 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/time.h          | 28 +++++++++
 include/grub/loongarch64/types.h         | 34 +++++++++++
 9 files changed, 239 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/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 d290c9a76..5a486558e 100644
--- a/grub-core/kern/efi/mm.c
+++ b/grub-core/kern/efi/mm.c
@@ -654,7 +654,8 @@ grub_efi_mm_init (void)
   grub_mm_add_region_fn = grub_efi_mm_add_regions;
 }
 
-#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 eb2dfdfce..6f1a6254e 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..cdd011d2c
--- /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 0xfffffffffffULL
+
+#endif /* ! GRUB_MEMORY_CPU_HEADER */
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.35.1



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

* [PATCH v10 08/11] LoongArch: Add to build system
  2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
                   ` (6 preceding siblings ...)
  2022-08-24  7:27 ` [PATCH v10 07/11] LoongArch: Add auxiliary files Xiaotian Wu
@ 2022-08-24  7:27 ` Xiaotian Wu
  2022-08-24  7:27 ` [PATCH v10 09/11] LoongArch: Support new relocation types Xiaotian Wu
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:27 UTC (permalink / raw)
  To: grub-devel; +Cc: git, development, daniel.kiper, Xiaotian Wu, Zhou Yang

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Signed-off-by: Zhou Yang <zhouyang@loongson.cn>
---
 Makefile.util.def           |  1 +
 configure.ac                | 22 ++++++++++++++++-
 gentpl.py                   | 25 ++++++++++---------
 grub-core/Makefile.am       |  6 +++++
 grub-core/Makefile.core.def | 16 ++++++++++++
 include/grub/efi/api.h      |  2 +-
 include/grub/util/install.h |  1 +
 util/grub-install-common.c  | 49 +++++++++++++++++++------------------
 util/grub-install.c         | 16 ++++++++++++
 util/grub-mknetdir.c        |  1 +
 util/grub-mkrescue.c        |  8 ++++++
 util/mkimage.c              | 16 ++++++++++++
 12 files changed, 125 insertions(+), 38 deletions(-)

diff --git a/Makefile.util.def b/Makefile.util.def
index d919c562c..765c6fea2 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -163,6 +163,7 @@ library = {
   common = grub-core/kern/ia64/dl_helper.c;
   common = grub-core/kern/arm/dl_helper.c;
   common = grub-core/kern/arm64/dl_helper.c;
+  common = grub-core/kern/loongarch64/dl_helper.c;
   common = grub-core/lib/minilzo/minilzo.c;
   common = grub-core/lib/xzembed/xz_dec_bcj.c;
   common = grub-core/lib/xzembed/xz_dec_lzma2.c;
diff --git a/configure.ac b/configure.ac
index 90f686f79..71f65a70d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -119,6 +119,7 @@ case "$target_cpu" in
 		;;
   arm*)		target_cpu=arm ;;
   aarch64*)	target_cpu=arm64 ;;
+  loongarch64)	target_cpu=loongarch64 ;;
   riscv32*)	target_cpu=riscv32 ;;
   riscv64*)	target_cpu=riscv64 ;;
 esac
@@ -144,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 ;;
     *)
@@ -194,6 +196,7 @@ case "$target_cpu"-"$platform" in
   arm-coreboot) ;;
   arm-efi) ;;
   arm64-efi) ;;
+  loongarch64-efi) ;;
   riscv32-efi) ;;
   riscv64-efi) ;;
   *-emu) ;;
@@ -855,6 +858,20 @@ if ( test "x$target_cpu" = xi386 || test "x$target_cpu" = xx86_64 ); then
   fi
 fi
 
+if test "x$target_cpu" = xloongarch64; then
+  AC_CACHE_CHECK([whether -Wa,-mla-global-with-abs works], [grub_cv_cc_mla_global_with_abs], [
+    CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs -Werror"
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
+	[grub_cv_cc_mla_global_with_abs=yes],
+	[grub_cv_cc_mla_global_with_abs=no])
+  ])
+
+  if test "x$grub_cv_cc_mla_global_with_abs" = xyes; then
+    TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
+    TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
+  fi
+fi
+
 # GRUB doesn't use float or doubles at all. Yet some toolchains may decide
 # that floats are a good fit to run instead of what's written in the code.
 # Given that floating point unit is disabled (if present to begin with)
@@ -1216,7 +1233,8 @@ AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)
 
 LDFLAGS="$TARGET_LDFLAGS"
 
-if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64 ; then
+if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64 \
+  || test "$target_cpu" = loongarch64 ; then
   # Use large model to support 4G memory
   AC_CACHE_CHECK([whether option -mcmodel=large works], grub_cv_cc_mcmodel, [
     CFLAGS="$TARGET_CFLAGS -mcmodel=large"
@@ -1996,6 +2014,8 @@ AM_CONDITIONAL([COND_i386_coreboot], [test x$target_cpu = xi386 -a x$platform =
 AM_CONDITIONAL([COND_i386_multiboot], [test x$target_cpu = xi386 -a x$platform = xmultiboot])
 AM_CONDITIONAL([COND_i386_xen], [test x$target_cpu = xi386 -a x$platform = xxen])
 AM_CONDITIONAL([COND_i386_xen_pvh], [test x$target_cpu = xi386 -a x$platform = xxen_pvh])
+AM_CONDITIONAL([COND_loongarch64], [test x$target_cpu = xloongarch64])
+AM_CONDITIONAL([COND_loongarch64_efi], [test x$target_cpu = xloongarch64 -a x$platform = xefi])
 AM_CONDITIONAL([COND_mips], [test x$target_cpu = xmips -o x$target_cpu = xmipsel])
 AM_CONDITIONAL([COND_mips_arc], [test "(" x$target_cpu = xmips -o x$target_cpu = xmipsel ")"  -a x$platform = xarc])
 AM_CONDITIONAL([COND_mips_loongson], [test x$target_cpu = xmipsel -a x$platform = xloongson])
diff --git a/gentpl.py b/gentpl.py
index 9f51e4fb6..fa53e17e8 100644
--- a/gentpl.py
+++ b/gentpl.py
@@ -32,27 +32,28 @@ GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot",
                    "mips_loongson", "sparc64_ieee1275",
                    "powerpc_ieee1275", "mips_arc", "ia64_efi",
                    "mips_qemu_mips", "arm_uboot", "arm_efi", "arm64_efi",
-                   "arm_coreboot", "riscv32_efi", "riscv64_efi" ]
+                   "arm_coreboot", "loongarch64_efi", "riscv32_efi", "riscv64_efi" ]
 
 GROUPS = {}
 
 GROUPS["common"]   = GRUB_PLATFORMS[:]
 
 # Groups based on CPU
-GROUPS["i386"]     = [ "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot", "i386_multiboot", "i386_ieee1275" ]
-GROUPS["x86_64"]   = [ "x86_64_efi" ]
-GROUPS["x86"]      = GROUPS["i386"] + GROUPS["x86_64"]
-GROUPS["mips"]     = [ "mips_loongson", "mips_qemu_mips", "mips_arc" ]
-GROUPS["sparc64"]  = [ "sparc64_ieee1275" ]
-GROUPS["powerpc"]  = [ "powerpc_ieee1275" ]
-GROUPS["arm"]      = [ "arm_uboot", "arm_efi", "arm_coreboot" ]
-GROUPS["arm64"]    = [ "arm64_efi" ]
-GROUPS["riscv32"]  = [ "riscv32_efi" ]
-GROUPS["riscv64"]  = [ "riscv64_efi" ]
+GROUPS["i386"]        = [ "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot", "i386_multiboot", "i386_ieee1275" ]
+GROUPS["x86_64"]      = [ "x86_64_efi" ]
+GROUPS["x86"]         = GROUPS["i386"] + GROUPS["x86_64"]
+GROUPS["mips"]        = [ "mips_loongson", "mips_qemu_mips", "mips_arc" ]
+GROUPS["sparc64"]     = [ "sparc64_ieee1275" ]
+GROUPS["powerpc"]     = [ "powerpc_ieee1275" ]
+GROUPS["arm"]         = [ "arm_uboot", "arm_efi", "arm_coreboot" ]
+GROUPS["arm64"]       = [ "arm64_efi" ]
+GROUPS["loongarch64"] = [ "loongarch64_efi" ]
+GROUPS["riscv32"]     = [ "riscv32_efi" ]
+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 5212dfab1..33543b1df 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -67,6 +67,9 @@ kernel = {
   arm64_efi_ldflags          = '-Wl,-r';
   arm64_efi_stripflags       = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
 
+  loongarch64_efi_ldflags      = '-Wl,-r';
+  loongarch64_efi_stripflags   = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
+
   riscv32_efi_ldflags      = '-Wl,-r';
   riscv32_efi_stripflags   = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
 
@@ -122,6 +125,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;
 
@@ -260,6 +264,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;
 
@@ -338,6 +345,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;
@@ -838,6 +850,7 @@ module = {
   enable = arm64_efi;
   enable = arm_uboot;
   enable = arm_coreboot;
+  enable = loongarch64_efi;
   enable = riscv32_efi;
   enable = riscv64_efi;
 };
@@ -1719,6 +1732,7 @@ module = {
   extra_dist = lib/arm/setjmp.S;
   extra_dist = lib/arm64/setjmp.S;
   extra_dist = lib/riscv/setjmp.S;
+  extra_dist = lib/loongarch64/setjmp.S;
 };
 
 module = {
@@ -1820,6 +1834,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;
@@ -1916,6 +1931,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 1ef404622..ed3e70fb9 100644
--- a/include/grub/efi/api.h
+++ b/include/grub/efi/api.h
@@ -1751,7 +1751,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 7d7445af9..35cf17a8d 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 e45fb2903..57ac4456c 100644
--- a/util/grub-install-common.c
+++ b/util/grub-install-common.c
@@ -886,30 +886,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 7b04bd3c5..aa868e6b5 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:
@@ -1132,6 +1138,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;
@@ -1165,6 +1174,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;
@@ -1473,6 +1485,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:
@@ -1568,6 +1581,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:
@@ -1673,6 +1687,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:
@@ -1927,6 +1942,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 d609ff94c..46f304c2b 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 ba89b1394..145aeed74 100644
--- a/util/grub-mkrescue.c
+++ b/util/grub-mkrescue.c
@@ -542,6 +542,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]
 	  || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
@@ -742,6 +743,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 +780,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 43078c71c..4237383ac 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.35.1



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

* [PATCH v10 09/11] LoongArch: Support new relocation types
  2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
                   ` (7 preceding siblings ...)
  2022-08-24  7:27 ` [PATCH v10 08/11] LoongArch: Add to build system Xiaotian Wu
@ 2022-08-24  7:27 ` Xiaotian Wu
  2022-08-24  7:27 ` [PATCH v10 10/11] LoongArch: add efistub kernel support Xiaotian Wu
  2022-08-24  7:27 ` [PATCH v10 11/11] tests: add support for LoongArch Xiaotian Wu
  10 siblings, 0 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:27 UTC (permalink / raw)
  To: grub-devel; +Cc: git, development, daniel.kiper, Xiaotian Wu

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
---
 conf/Makefile.common                   |  3 +
 configure.ac                           | 14 +++--
 grub-core/kern/dl.c                    |  9 +--
 grub-core/kern/loongarch64/dl.c        | 71 ++++++++++++++++++++-
 grub-core/kern/loongarch64/dl_helper.c | 68 ++++++++++++++++++++
 include/grub/elf.h                     |  5 ++
 include/grub/loongarch64/reloc.h       |  4 ++
 util/grub-mkimagexx.c                  | 87 +++++++++++++++++++++++++-
 util/grub-module-verifier.c            |  5 ++
 9 files changed, 249 insertions(+), 17 deletions(-)

diff --git a/conf/Makefile.common b/conf/Makefile.common
index 2d8f1bf2e..77f20e441 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
+  CFLAGS_PLATFORM += -mcmodel=large
+endif
 if COND_powerpc_ieee1275
   CFLAGS_PLATFORM += -mcpu=powerpc
 endif
diff --git a/configure.ac b/configure.ac
index 71f65a70d..c8daf5dfb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -859,14 +859,16 @@ if ( test "x$target_cpu" = xi386 || test "x$target_cpu" = xx86_64 ); then
 fi
 
 if test "x$target_cpu" = xloongarch64; then
-  AC_CACHE_CHECK([whether -Wa,-mla-global-with-abs works], [grub_cv_cc_mla_global_with_abs], [
-    CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs -Werror"
+  AC_CACHE_CHECK([whether _mno_explicit_relocs works], [grub_cv_cc_mno_explicit_relocs], [
+    CFLAGS="$TARGET_CFLAGS -mno-explicit-relocs -Werror"
     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
-	[grub_cv_cc_mla_global_with_abs=yes],
-	[grub_cv_cc_mla_global_with_abs=no])
+	[grub_cv_cc_mno_explicit_relocs=yes],
+	[grub_cv_cc_mno_explicit_relocs=no])
   ])
-
-  if test "x$grub_cv_cc_mla_global_with_abs" = xyes; then
+  if test "x$grub_cv_cc_mno_explicit_relocs" = xyes; then
+    TARGET_CFLAGS="$TARGET_CFLAGS -mno-explicit-relocs"
+    TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-explicit-relocs"
+  else
     TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
     TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
   fi
diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
index 0bf40caa6..e447fd0fa 100644
--- a/grub-core/kern/dl.c
+++ b/grub-core/kern/dl.c
@@ -225,8 +225,7 @@ 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) && \
-  !defined (__loongarch__)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
   grub_size_t tramp;
   grub_size_t got;
   grub_err_t err;
@@ -242,8 +241,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
 	talign = s->sh_addralign;
     }
 
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
-  !defined (__loongarch__)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
   err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
   if (err)
     return err;
@@ -306,8 +304,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
 	  mod->segment = seg;
 	}
     }
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
-  !defined (__loongarch__)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
   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
index 3a6aa91cd..127f2c2e6 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -49,6 +49,7 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
 			       Elf_Shdr *s, grub_dl_segment_t seg)
 {
   Elf_Rel *rel, *max;
+  unsigned unmatched_got_pc_page = 0;
   struct grub_loongarch64_stack stack;
   grub_loongarch64_stack_init (&stack);
 
@@ -58,7 +59,7 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
        rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
     {
       Elf_Sym *sym;
-      grub_uint64_t *place;
+      void *place;
       grub_uint64_t sym_addr;
 
       if (rel->r_offset >= seg->size)
@@ -72,12 +73,19 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
       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);
+      place = (void *) ((grub_addr_t)seg->addr + rel->r_offset);
 
       switch (ELF_R_TYPE (rel->r_info))
 	{
 	case R_LARCH_64:
-	  *place = sym_addr;
+	  {
+	    grub_uint64_t *abs_place = place;
+
+	    grub_dprintf ("dl", "reloc_abs64 %p => 0x%016llx, %p\n",
+			  place, (unsigned long long) sym_addr, abs_place);
+
+	    *abs_place += (grub_uint64_t) sym_addr;
+	  }
 	  break;
 	case R_LARCH_MARK_LA:
 	  break;
@@ -85,6 +93,63 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
 	case R_LARCH_SOP_PUSH_PLT_PCREL:
 	  grub_loongarch64_sop_push (&stack, sym_addr - (grub_uint64_t)place);
 	  break;
+	case R_LARCH_B26:
+	  {
+	    grub_uint32_t *abs_place = place;
+	    grub_ssize_t off = sym_addr - (grub_addr_t) place;
+
+	    grub_loongarch64_b26 (abs_place, off);
+	  }
+	  break;
+	case R_LARCH_PCALA_HI20:
+	  {
+	    grub_uint32_t *abs_place = place;
+	    grub_int32_t off = (((sym_addr + 0x800) & ~0xfffULL) - ((grub_addr_t)place & ~0xfffULL));
+
+	    grub_loongarch64_xxx_hi20 (abs_place, off);
+	  }
+	  break;
+	case R_LARCH_PCALA_LO12:
+	  {
+	    grub_uint32_t *abs_place = place;
+	    grub_loongarch64_xxx_lo12 (abs_place, sym_addr);
+	  }
+	  break;
+	case R_LARCH_GOT_PC_HI20:
+	  {
+	    grub_uint64_t *gp = mod->gotptr;
+	    grub_int64_t gpoffset;
+	    Elf_Rela *rel2;
+
+	    gpoffset = (((grub_uint64_t) gp + 0x800) & ~0xfff) - (((grub_uint64_t) place) & ~0xfff);
+
+	    *gp = (grub_uint64_t) sym_addr;
+	    mod->gotptr = gp + 1;
+	    unmatched_got_pc_page++;
+	    grub_loongarch64_xxx_hi20 (place, gpoffset);
+	    for (rel2 = (Elf_Rela *) ((char *) rel + s->sh_entsize);
+		 rel2 < (Elf_Rela *) max;
+		 rel2 = (Elf_Rela *) ((char *) rel2 + s->sh_entsize))
+	      if (ELF_R_SYM (rel2->r_info) == ELF_R_SYM (rel->r_info)
+		  && ((Elf_Rela *) rel)->r_addend == rel2->r_addend
+		  && ELF_R_TYPE (rel2->r_info) == R_LARCH_GOT_PC_LO12)
+		{
+		  grub_uint32_t *place2;
+                  place2 = (grub_uint32_t *) ((grub_addr_t) seg->addr + rel2->r_offset);
+		  grub_loongarch64_xxx_lo12 (place2, (grub_uint64_t) gp);
+		  break;
+		}
+	    if (rel2 >= (Elf_Rela *) max)
+	      return grub_error (GRUB_ERR_BAD_MODULE,
+				 "GOT_PC_HI20 without matching GOT_PC_LO12");
+	  }
+	  break;
+	case R_LARCH_GOT_PC_LO12:
+	  if (unmatched_got_pc_page == 0)
+	    return grub_error (GRUB_ERR_BAD_MODULE,
+			       "GOT_PC_LO12 without matching GOT_PC_HI20");
+	  unmatched_got_pc_page--;
+	  break;
 	GRUB_LOONGARCH64_RELOCATION (&stack, place, sym_addr)
 	default:
 	  {
diff --git a/grub-core/kern/loongarch64/dl_helper.c b/grub-core/kern/loongarch64/dl_helper.c
index 627d1fff2..a5b5b8ee9 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -200,3 +200,71 @@ grub_loongarch64_sop_32_s_0_10_10_16_s2 (grub_loongarch64_stack_t stack,
   *place =(*place) | (((a >> 2) & 0xffff) << 10);
   *place =(*place) | ((a >> 18) & 0x3ff);
 }
+
+void grub_loongarch64_b26 (grub_uint32_t *place, grub_int64_t offset)
+{
+  grub_uint32_t val;
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc000000);
+
+  grub_dprintf ("dl", "  reloc_xxxx64 %p %c= 0x%llx\n",
+		place, offset > 0 ? '+' : '-',
+		offset < 0 ? (long long) -(unsigned long long) offset : offset);
+
+  val = ((offset >> 18) & 0x3ff) | (((offset >> 2) & 0xffff) << 10);
+
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (val) & ~insmask;
+}
+
+void grub_loongarch64_xxx_hi20 (grub_uint32_t *place, grub_int64_t offset)
+{
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfe00001f);
+  grub_uint32_t val;
+
+  offset >>= 12;
+  val = ((offset & 0xfffff) << 5);
+
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (val) & ~insmask;
+}
+
+void grub_loongarch64_xxx_lo12 (grub_uint32_t *place, grub_int64_t offset)
+{
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xffc003ff);
+
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (offset << 10) & ~insmask;
+}
+
+grub_err_t
+grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
+				 grub_size_t *got)
+{
+  const Elf64_Ehdr *e = ehdr;
+  const Elf64_Shdr *s;
+  unsigned i;
+
+  *tramp = 0;
+  *got = 0;
+
+  for (i = 0, s = (Elf64_Shdr *) ((char *) e + grub_le_to_cpu64 (e->e_shoff));
+       i < grub_le_to_cpu16 (e->e_shnum);
+       i++, s = (Elf64_Shdr *) ((char *) s + grub_le_to_cpu16 (e->e_shentsize)))
+    if (s->sh_type == grub_cpu_to_le32_compile_time (SHT_RELA))
+      {
+	const Elf64_Rela *rel, *max;
+
+	for (rel = (Elf64_Rela *) ((char *) e + grub_le_to_cpu64 (s->sh_offset)),
+	       max = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_size));
+	     rel < max; rel = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_entsize)))
+	  switch (ELF64_R_TYPE (grub_le_to_cpu64 (rel->r_info)))
+	    {
+	    case R_LARCH_GOT_PC_HI20:
+	    case R_LARCH_GOT_PC_LO12:
+	      *got += 8;
+	      break;
+	    }
+      }
+
+  return GRUB_ERR_NONE;
+}
diff --git a/include/grub/elf.h b/include/grub/elf.h
index beef94512..5eab3feda 100644
--- a/include/grub/elf.h
+++ b/include/grub/elf.h
@@ -2558,6 +2558,11 @@ typedef Elf32_Addr Elf32_Conflict;
 #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
+#define R_LARCH_B26			      66
+#define R_LARCH_PCALA_HI20		      71
+#define R_LARCH_PCALA_LO12		      72
+#define R_LARCH_GOT_PC_HI20		      75
+#define R_LARCH_GOT_PC_LO12		      76
 
 extern grub_err_t grub_elf32_get_shnum (Elf32_Ehdr *e, Elf32_Shnum *shnum);
 extern grub_err_t grub_elf32_get_shstrndx (Elf32_Ehdr *e, Elf32_Word *shstrndx);
diff --git a/include/grub/loongarch64/reloc.h b/include/grub/loongarch64/reloc.h
index 0b7d66ddc..ece565491 100644
--- a/include/grub/loongarch64/reloc.h
+++ b/include/grub/loongarch64/reloc.h
@@ -57,6 +57,10 @@ void grub_loongarch64_sop_32_s_0_5_10_16_s2  (grub_loongarch64_stack_t stack,
 void grub_loongarch64_sop_32_s_0_10_10_16_s2 (grub_loongarch64_stack_t stack,
 					      grub_uint64_t *place);
 
+void grub_loongarch64_b26	  (grub_uint32_t *place, grub_int64_t offset);
+void grub_loongarch64_xxx_hi20	  (grub_uint32_t *place, grub_int64_t offset);
+void grub_loongarch64_xxx_lo12 	  (grub_uint32_t *place, grub_int64_t offset);
+
 #define GRUB_LOONGARCH64_RELOCATION(STACK, PLACE, OFFSET)	\
   case R_LARCH_SOP_PUSH_ABSOLUTE:				\
     grub_loongarch64_sop_push (STACK, OFFSET);			\
diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index 358b9ab47..ebe200d41 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -1128,11 +1128,20 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
 	       }
 	     case EM_LOONGARCH:
 	       {
+		 grub_int64_t pc;
+
+		 grub_uint32_t *t32 = (grub_uint32_t *) target;
 		 sym_addr += addend;
+
+		 pc = offset + target_section_addr + image_target->vaddr_offset;
+
 		 switch (ELF_R_TYPE (info))
 		   {
 		   case R_LARCH_64:
-		     *target = grub_host_to_target64 (grub_target_to_host64 (*target) + sym_addr);
+		     {
+		       grub_uint64_t *t64 = (grub_uint64_t *) target;
+		       *t64 = grub_host_to_target64 (grub_target_to_host64 (*t64) + sym_addr);
+		     }
 		     break;
 		   case R_LARCH_MARK_LA:
 		     break;
@@ -1143,6 +1152,62 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
 						  +offset
 						  +image_target->vaddr_offset));
 		     break;
+		   case R_LARCH_B26:
+		     {
+		       grub_int64_t off;
+
+		       off = sym_addr - target_section_addr - offset - image_target->vaddr_offset;
+
+		       grub_loongarch64_b26 (t32, off);
+		     }
+		     break;
+		   case R_LARCH_PCALA_HI20:
+		     {
+		       grub_int32_t hi20;
+
+		       hi20 = (((sym_addr + 0x800) & ~0xfffULL) - (pc & ~0xfffULL));
+
+		       grub_loongarch64_xxx_hi20 (t32, hi20);
+		     }
+		     break;
+		   case R_LARCH_PCALA_LO12:
+		     grub_loongarch64_xxx_lo12 (t32, sym_addr);
+		     break;
+		   case R_LARCH_GOT_PC_HI20:
+		     {
+		       Elf64_Rela *rel2;
+		       grub_int32_t hi20;
+		       unsigned k;
+
+		       grub_int64_t gpoffset = (char *) gpptr - (char *) pe_target + image_target->vaddr_offset;
+
+		       hi20 = ((gpoffset + 0x800) & ~0xfffULL) - (pc & ~0xfffULL);
+
+		       *gpptr = grub_host_to_target64 (sym_addr);
+		       unmatched_adr_got_page++;
+		       grub_loongarch64_xxx_hi20 ((grub_uint32_t *)target, hi20);
+		       for (k = 0, rel2 = (Elf_Rela *) ((char *) r + r_size);
+			    k < num_rs;
+			    k++, rel2 = (Elf_Rela *) ((char *) rel2 + r_size))
+			 if (ELF_R_SYM (rel2->r_info) == ELF_R_SYM (r->r_info)
+			     && r->r_addend == rel2->r_addend
+			     && ELF_R_TYPE (rel2->r_info) == R_LARCH_GOT_PC_LO12)
+			   {
+			     t32 = (grub_uint32_t *) SUFFIX (get_target_address) (e, target_section,
+										  grub_target_to_host (rel2->r_offset), image_target),
+			     grub_loongarch64_xxx_lo12 (t32, gpoffset);
+			     break;
+			   }
+		       if (k >= num_rs)
+			 grub_util_error ("GOT_PC_HI20 without matching GOT_PC_LO12");
+		       gpptr++;
+		     }
+		     break;
+		   case R_LARCH_GOT_PC_LO12:
+		     if (unmatched_adr_got_page == 0)
+		       grub_util_error ("GOT_PC_LO12 without matching GOT_PC_HI20");
+		     unmatched_adr_got_page--;
+		     break;
 		   GRUB_LOONGARCH64_RELOCATION (&stack, target, sym_addr)
 		   default:
 		     grub_util_error (_("relocation 0x%x is not implemented yet"),
@@ -1736,6 +1801,11 @@ translate_relocation_pe (struct translate_context *ctx,
 	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:
+	case R_LARCH_B26:
+	case R_LARCH_PCALA_HI20:
+	case R_LARCH_PCALA_LO12:
+	case R_LARCH_GOT_PC_HI20:
+	case R_LARCH_GOT_PC_LO12:
 	  grub_util_info ("  %s:  not adding fixup: 0x%08x : 0x%08x",
 			  __FUNCTION__,
 			  (unsigned int) addr,
@@ -2095,7 +2165,7 @@ make_reloc_section (Elf_Ehdr *e, struct grub_mkimage_layout *layout,
 		       + image_target->vaddr_offset,
 		       2 * layout->ia64jmpnum,
 		       image_target);
-  if (image_target->elf_target == EM_IA_64 || image_target->elf_target == EM_AARCH64)
+  if (image_target->elf_target == EM_IA_64 || image_target->elf_target == EM_AARCH64 || image_target->elf_target == EM_LOONGARCH)
     create_u64_fixups (&ctx,
 		       layout->got_off
 		       + image_target->vaddr_offset,
@@ -2246,6 +2316,8 @@ SUFFIX (locate_sections) (Elf_Ehdr *e, const char *kernel_path,
   /* Page-aligning simplifies relocation handling.  */
   if (image_target->elf_target == EM_AARCH64)
     layout->align = 4096;
+  if (image_target->elf_target == EM_LOONGARCH)
+    layout->align = 4096;
 
   layout->kernel_size = 0;
 
@@ -2464,6 +2536,17 @@ SUFFIX (grub_mkimage_load_image) (const char *kernel_path,
 
 	  grub_arm64_dl_get_tramp_got_size (e, &tramp, &layout->got_size);
 
+	  layout->got_off = layout->kernel_size;
+	  layout->kernel_size += ALIGN_UP (layout->got_size, 16);
+	}
+      if (image_target->elf_target == EM_LOONGARCH)
+	{
+	  grub_size_t tramp;
+
+	  layout->kernel_size = ALIGN_UP (layout->kernel_size, 16);
+
+	  grub_arch_dl_get_tramp_got_size (e, &tramp, &layout->got_size);
+
 	  layout->got_off = layout->kernel_size;
 	  layout->kernel_size += ALIGN_UP (layout->got_size, 16);
 	}
diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c
index b510461fa..88351e7eb 100644
--- a/util/grub-module-verifier.c
+++ b/util/grub-module-verifier.c
@@ -140,6 +140,11 @@ struct grub_module_verifier_arch archs[] = {
       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,
+      R_LARCH_B26,
+      R_LARCH_PCALA_HI20,
+      R_LARCH_PCALA_LO12,
+      R_LARCH_GOT_PC_HI20,
+      R_LARCH_GOT_PC_LO12,
       -1
     }, (int[]){
       -1
-- 
2.35.1



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

* [PATCH v10 10/11] LoongArch: add efistub kernel support
  2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
                   ` (8 preceding siblings ...)
  2022-08-24  7:27 ` [PATCH v10 09/11] LoongArch: Support new relocation types Xiaotian Wu
@ 2022-08-24  7:27 ` Xiaotian Wu
  2022-08-24  7:27 ` [PATCH v10 11/11] tests: add support for LoongArch Xiaotian Wu
  10 siblings, 0 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:27 UTC (permalink / raw)
  To: grub-devel; +Cc: git, development, daniel.kiper, Xiaotian Wu, Sun Haiyong

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Signed-off-by: Sun Haiyong <sunhaiyong@loongson.cn>
---
 gentpl.py                            |   2 +-
 grub-core/loader/loongarch64/linux.c | 329 ++++++++++++++++++++++++++-
 include/grub/loongarch64/linux.h     |  18 +-
 3 files changed, 338 insertions(+), 11 deletions(-)

diff --git a/gentpl.py b/gentpl.py
index fa53e17e8..832d28ae0 100644
--- a/gentpl.py
+++ b/gentpl.py
@@ -80,7 +80,7 @@ GROUPS["terminfomodule"]   = GRUB_PLATFORMS[:];
 for i in GROUPS["terminfoinkernel"]: GROUPS["terminfomodule"].remove(i)
 
 # Flattened Device Trees (FDT)
-GROUPS["fdt"] = [ "arm64_efi", "arm_uboot", "arm_efi", "riscv32_efi", "riscv64_efi" ]
+GROUPS["fdt"] = [ "arm64_efi", "arm_uboot", "arm_efi", "loongarch64_efi", "riscv32_efi", "riscv64_efi" ]
 
 # Needs software helpers for division
 # Must match GRUB_DIVISION_IN_SOFTWARE in misc.h
diff --git a/grub-core/loader/loongarch64/linux.c b/grub-core/loader/loongarch64/linux.c
index d9d39a20c..fa5f7c01e 100644
--- a/grub-core/loader/loongarch64/linux.c
+++ b/grub-core/loader/loongarch64/linux.c
@@ -16,28 +16,342 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <grub/charset.h>
 #include <grub/command.h>
-#include <grub/dl.h>
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/fdt.h>
+#include <grub/linux.h>
+#include <grub/loader.h>
+#include <grub/mm.h>
+#include <grub/types.h>
+#include <grub/cpu/linux.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/fdtload.h>
+#include <grub/efi/memory.h>
+#include <grub/efi/pe32.h>
+#include <grub/i18n.h>
 #include <grub/lib/cmdline.h>
+#include <grub/verify.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
+static grub_dl_t my_mod;
+static int loaded;
+
+static void *kernel_addr;
+static grub_uint64_t kernel_size;
+
+static char *linux_args;
+static grub_uint32_t cmdline_size;
+
+static grub_addr_t initrd_start;
+static grub_addr_t initrd_end;
+
+grub_err_t
+grub_arch_efi_linux_check_image (struct linux_arch_kernel_header * lh)
+{
+  if ((lh->code0 & 0xffff) != GRUB_PE32_MAGIC)
+    return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+		       N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
+
+  grub_dprintf ("linux", "UEFI stub kernel:\n");
+  grub_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+finalize_params_linux (void)
+{
+  int node, retval;
+
+  void *fdt;
+
+  fdt = grub_fdt_load (GRUB_EFI_LINUX_FDT_EXTRA_SPACE);
+
+  if (!fdt)
+    goto failure;
+
+  node = grub_fdt_find_subnode (fdt, 0, "chosen");
+  if (node < 0)
+    node = grub_fdt_add_subnode (fdt, 0, "chosen");
+
+  if (node < 1)
+    goto failure;
+
+  /* Set initrd info */
+  if (initrd_start && initrd_end > initrd_start)
+    {
+      grub_dprintf ("linux", "Initrd @ %p-%p\n",
+		    (void *) initrd_start, (void *) initrd_end);
+
+      retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start",
+				    initrd_start);
+      if (retval)
+	goto failure;
+      retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end",
+				    initrd_end);
+      if (retval)
+	goto failure;
+    }
+
+  if (grub_fdt_install() != GRUB_ERR_NONE)
+    goto failure;
+
+  return GRUB_ERR_NONE;
+
+failure:
+  grub_fdt_unload();
+  return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT");
+}
+
+grub_err_t
+grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
+{
+  grub_efi_memory_mapped_device_path_t *mempath;
+  grub_efi_handle_t image_handle;
+  grub_efi_boot_services_t *b;
+  grub_efi_status_t status;
+  grub_efi_loaded_image_t *loaded_image;
+  int len;
+
+  mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t));
+  if (!mempath)
+    return grub_errno;
+
+  mempath[0].header.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE;
+  mempath[0].header.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE;
+  mempath[0].header.length = grub_cpu_to_le16_compile_time (sizeof (*mempath));
+  mempath[0].memory_type = GRUB_EFI_LOADER_DATA;
+  mempath[0].start_address = addr;
+  mempath[0].end_address = addr + size;
+
+  mempath[1].header.type = GRUB_EFI_END_DEVICE_PATH_TYPE;
+  mempath[1].header.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
+  mempath[1].header.length = sizeof (grub_efi_device_path_t);
+
+  b = grub_efi_system_table->boot_services;
+  status = b->load_image (0, grub_efi_image_handle,
+			  (grub_efi_device_path_t *) mempath,
+			  (void *) addr, size, &image_handle);
+  if (status != GRUB_EFI_SUCCESS)
+    return grub_error (GRUB_ERR_BAD_OS, "cannot load image");
+
+  grub_dprintf ("linux", "linux command line: '%s'\n", args);
+
+  /* Convert command line to UCS-2 */
+  loaded_image = grub_efi_get_loaded_image (image_handle);
+  loaded_image->load_options_size = len =
+    (grub_strlen (args) + 1) * sizeof (grub_efi_char16_t);
+  loaded_image->load_options =
+    grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
+  if (!loaded_image->load_options)
+    return grub_errno;
+
+  loaded_image->load_options_size =
+    2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
+			    (grub_uint8_t *) args, len, NULL);
+
+  grub_dprintf ("linux", "starting image %p\n", image_handle);
+  status = b->start_image (image_handle, 0, NULL);
+
+  /* When successful, not reached */
+  b->unload_image (image_handle);
+  grub_efi_free_pages ((grub_addr_t) loaded_image->load_options,
+		       GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
+
+  return grub_errno;
+}
+
+static grub_err_t
+grub_linux_boot (void)
+{
+  if (finalize_params_linux () != GRUB_ERR_NONE)
+    return grub_errno;
+
+  return (grub_arch_efi_linux_boot_image((grub_addr_t)kernel_addr,
+                                          kernel_size, linux_args));
+}
+
+static grub_err_t
+grub_linux_unload (void)
+{
+  grub_dl_unref (my_mod);
+  loaded = 0;
+  if (initrd_start)
+    grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start,
+			 GRUB_EFI_BYTES_TO_PAGES (initrd_end - initrd_start));
+  initrd_start = initrd_end = 0;
+  grub_free (linux_args);
+  if (kernel_addr)
+    grub_efi_free_pages ((grub_addr_t) kernel_addr,
+			 GRUB_EFI_BYTES_TO_PAGES (kernel_size));
+  grub_fdt_unload ();
+  return GRUB_ERR_NONE;
+}
+
+#define INITRD_MAX_ADDRESS_OFFSET (32ULL * 1024 * 1024 * 1024)
+
+/*
+ * This function returns a pointer to a legally allocated initrd buffer,
+ * or NULL if unsuccessful
+ */
+static void *
+allocate_initrd_mem (int initrd_pages)
+{
+  grub_addr_t max_addr;
+
+  if (grub_efi_get_ram_base (&max_addr) != GRUB_ERR_NONE)
+    return NULL;
+
+  max_addr += INITRD_MAX_ADDRESS_OFFSET - 1;
+
+  return grub_efi_allocate_pages_real (max_addr, initrd_pages,
+				       GRUB_EFI_ALLOCATE_MAX_ADDRESS,
+				       GRUB_EFI_LOADER_DATA);
+}
+
 static grub_err_t
 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
-		 int argc __attribute__ ((unused)),
-		 char *argv[] __attribute__ ((unused)))
+		 int argc, char *argv[])
 {
-  grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, N_("Linux not supported yet"));
+  struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
+  int initrd_size, initrd_pages;
+  void *initrd_mem = NULL;
+
+  if (argc == 0)
+    {
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
+      goto fail;
+    }
+
+  if (!loaded)
+    {
+      grub_error (GRUB_ERR_BAD_ARGUMENT,
+		  N_("you need to load the kernel first"));
+      goto fail;
+    }
+
+  if (grub_initrd_init (argc, argv, &initrd_ctx))
+    goto fail;
+
+  initrd_size = grub_get_initrd_size (&initrd_ctx);
+  grub_dprintf ("linux", "Loading initrd\n");
+
+  initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size));
+  initrd_mem = allocate_initrd_mem (initrd_pages);
+
+  if (!initrd_mem)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+      goto fail;
+    }
+
+  if (grub_initrd_load (&initrd_ctx, initrd_mem))
+    goto fail;
+
+  initrd_start = (grub_addr_t) initrd_mem;
+  initrd_end = initrd_start + initrd_size;
+  grub_dprintf ("linux", "[addr=%p, size=0x%x]\n",
+		(void *) initrd_start, initrd_size);
+
+ fail:
+  grub_initrd_close (&initrd_ctx);
+  if (initrd_mem && !initrd_start)
+    grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages);
 
   return grub_errno;
 }
 
 static grub_err_t
 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
-		int argc __attribute__ ((unused)),
-		char *argv[] __attribute__ ((unused)))
+		int argc, char *argv[])
 {
-  grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, N_("Linux not supported yet"));
+  grub_file_t file = 0;
+  struct linux_arch_kernel_header lh;
+  grub_err_t err;
+
+  grub_dl_ref (my_mod);
+
+  if (argc == 0)
+    {
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
+      goto fail;
+    }
+
+  file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL);
+  if (!file)
+    goto fail;
+
+  kernel_size = grub_file_size (file);
+
+  if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
+    return grub_errno;
+
+  if (grub_arch_efi_linux_check_image (&lh) != GRUB_ERR_NONE)
+    goto fail;
+
+  grub_loader_unset();
+
+  grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
+  kernel_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (kernel_size));
+  grub_dprintf ("linux", "kernel numpages: %lld\n",
+		(long long) GRUB_EFI_BYTES_TO_PAGES (kernel_size));
+  if (!kernel_addr)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+      goto fail;
+    }
+
+  grub_file_seek (file, 0);
+  if (grub_file_read (file, kernel_addr, kernel_size)
+      < (grub_int64_t) kernel_size)
+    {
+      if (!grub_errno)
+	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
+      goto fail;
+    }
+
+  grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
+
+  cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE);
+  linux_args = grub_malloc (cmdline_size);
+  if (!linux_args)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+      goto fail;
+    }
+  grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
+  err = grub_create_loader_cmdline (argc, argv,
+				    linux_args + sizeof (LINUX_IMAGE) - 1,
+				    cmdline_size,
+				    GRUB_VERIFY_KERNEL_CMDLINE);
+  if (err)
+    goto fail;
+
+  if (grub_errno == GRUB_ERR_NONE)
+    {
+      grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
+      loaded = 1;
+    }
+
+fail:
+  if (file)
+    grub_file_close (file);
+
+  if (grub_errno != GRUB_ERR_NONE)
+    {
+      grub_dl_unref (my_mod);
+      loaded = 0;
+    }
+
+  if (linux_args && !loaded)
+    grub_free (linux_args);
+
+  if (kernel_addr && !loaded)
+    grub_efi_free_pages ((grub_addr_t) kernel_addr,
+			 GRUB_EFI_BYTES_TO_PAGES (kernel_size));
 
   return grub_errno;
 }
@@ -50,6 +364,7 @@ GRUB_MOD_INIT (linux)
 				     N_("FILE [ARGS...]"), N_("Load Linux."));
   cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
 				      N_("FILE"), N_("Load initrd."));
+  my_mod = mod;
 }
 
 GRUB_MOD_FINI (linux)
diff --git a/include/grub/loongarch64/linux.h b/include/grub/loongarch64/linux.h
index 76fe693f4..3bef41402 100644
--- a/include/grub/loongarch64/linux.h
+++ b/include/grub/loongarch64/linux.h
@@ -19,13 +19,25 @@
 #ifndef GRUB_LOONGARCH64_LINUX_HEADER
 #define GRUB_LOONGARCH64_LINUX_HEADER 1
 
+#include <grub/types.h>
+
+#define GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE  0x3436414C /* 'LA64' */
+
 struct linux_loongarch64_kernel_header
 {
-  /*
-   * TODO
-   */
+  grub_uint32_t code0;		/* Executable code */
+  grub_uint32_t code1;		/* Executable code */
+  grub_uint64_t text_offset;	/* Image load offset */
+  grub_uint64_t res0;		/* reserved */
+  grub_uint64_t res1;		/* reserved */
+  grub_uint64_t res2;		/* reserved */
+  grub_uint64_t res3;		/* reserved */
+  grub_uint64_t res4;		/* reserved */
+  grub_uint32_t magic;		/* Magic number, little endian, "LA64" */
+  grub_uint32_t hdr_offset;	/* Offset of PE/COFF header */
 };
 
 #define linux_arch_kernel_header linux_loongarch64_kernel_header
+#define GRUB_LINUX_ARCH_MAGIC_SIGNATURE GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE
 
 #endif /* ! GRUB_LOONGARCH64_LINUX_HEADER */
-- 
2.35.1



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

* [PATCH v10 11/11] tests: add support for LoongArch
  2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
                   ` (9 preceding siblings ...)
  2022-08-24  7:27 ` [PATCH v10 10/11] LoongArch: add efistub kernel support Xiaotian Wu
@ 2022-08-24  7:27 ` Xiaotian Wu
  10 siblings, 0 replies; 12+ messages in thread
From: Xiaotian Wu @ 2022-08-24  7:27 UTC (permalink / raw)
  To: grub-devel; +Cc: git, development, daniel.kiper, Xiaotian Wu

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
---
 tests/ahci_test.in       |  2 +-
 tests/ehci_test.in       |  2 +-
 tests/ohci_test.in       |  2 +-
 tests/pata_test.in       |  2 +-
 tests/uhci_test.in       |  2 +-
 tests/util/grub-shell.in | 14 ++++++++++++++
 6 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/tests/ahci_test.in b/tests/ahci_test.in
index 6d2e61d4e..70646a24e 100644
--- a/tests/ahci_test.in
+++ b/tests/ahci_test.in
@@ -30,7 +30,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
     mips*-arc | mips*-qemu_mips)
 	exit 77;;
     # FIXME: No native drivers are available for those
-    powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi)
+    powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi | loongarch64-efi)
 	exit 77;;
 esac
 
diff --git a/tests/ehci_test.in b/tests/ehci_test.in
index df671b4b6..bf823a5de 100644
--- a/tests/ehci_test.in
+++ b/tests/ehci_test.in
@@ -30,7 +30,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
     mips*-arc | mips*-qemu_mips)
 	exit 77;;
     # FIXME: No native drivers are available for those
-    powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi)
+    powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi | loongarch64-efi)
 	exit 77;;
 esac
 
diff --git a/tests/ohci_test.in b/tests/ohci_test.in
index 741ad881f..a40d3bc0a 100644
--- a/tests/ohci_test.in
+++ b/tests/ohci_test.in
@@ -30,7 +30,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
     mips*-arc | mips*-qemu_mips)
 	exit 77;;
     # FIXME: No native drivers are available for those
-    powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi)
+    powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi | loongarch64-efi)
 	exit 77;;
 esac
 
diff --git a/tests/pata_test.in b/tests/pata_test.in
index 31144a8fd..4d0e7d573 100644
--- a/tests/pata_test.in
+++ b/tests/pata_test.in
@@ -33,7 +33,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
     i386-efi)
 	exit 77;;
     # FIXME: No native drivers are available for those
-    powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi)
+    powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi | loongarch64-efi)
 	exit 77;;
     i386-ieee1275)
 	disk=hdb
diff --git a/tests/uhci_test.in b/tests/uhci_test.in
index 5aa5eb726..de199a281 100644
--- a/tests/uhci_test.in
+++ b/tests/uhci_test.in
@@ -30,7 +30,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
     mips*-arc | mips*-qemu_mips)
 	exit 77;;
     # FIXME: No native drivers are available for those
-    powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi)
+    powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi | loongarch64-efi)
 	exit 77;;
 esac
 
diff --git a/tests/util/grub-shell.in b/tests/util/grub-shell.in
index ce431757c..a16d7b7ee 100644
--- a/tests/util/grub-shell.in
+++ b/tests/util/grub-shell.in
@@ -205,6 +205,16 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 	disk="device virtio-blk-device,drive=hd1 -drive if=none,id=hd1,file="
 	serial_port=efi0
 	;;
+    loongarch64-efi)
+	qemu=qemu-system-loongarch64
+	boot=hd
+	console=console
+	trim=1
+	qemuopts="-machine virt -cpu la464 -smp 2 -nographic -L ${srcdir} \
+		  -bios edk2-loongarch64-code.fd $qemuopts"
+	disk="device virtio-blk-pci,drive=hd1 -drive if=none,id=hd1,file="
+	serial_port=
+	;;
     *)
 	boot=hd
 	qemu=qemu-system-i386
@@ -408,6 +418,8 @@ fi
 if [ x$boot = xhd ]; then
     if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm64-efi ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm-efi ]; then
 	device="device virtio-blk-device,drive=hd0 -drive if=none,id=hd0,file="
+    elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = loongarch64-efi ]; then
+	device="device virtio-blk-pci,drive=grubdisk -drive if=none,id=grubdisk,file="
     elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mips-arc ]; then
 	device="hdb "
     else
@@ -418,6 +430,8 @@ fi
 if [ x$boot = xcd ]; then
     if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm64-efi ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm-efi ]; then
 	device="device virtio-blk-device,drive=cd0 -drive if=none,id=cd0,media=cdrom,file="
+    elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = loongarch64-efi ]; then
+	device="device virtio-blk-pci,drive=grubcd -drive if=none,id=grubcd,media=cdrom,file="
     elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = powerpc-ieee1275 ] && [ x$pseries != xy ] ; then
 	device="-drive if=ide,media=cdrom,file="
     else
-- 
2.35.1



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

end of thread, other threads:[~2022-08-24  7:28 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-24  7:26 [PATCH v10 00/11] Add support for LoongArch Xiaotian Wu
2022-08-24  7:26 ` [PATCH v10 01/11] PE: Add LoongArch definitions Xiaotian Wu
2022-08-24  7:26 ` [PATCH v10 02/11] " Xiaotian Wu
2022-08-24  7:26 ` [PATCH v10 03/11] LoongArch: Add setjmp implementation Xiaotian Wu
2022-08-24  7:26 ` [PATCH v10 04/11] LoongArch: Add early startup code Xiaotian Wu
2022-08-24  7:26 ` [PATCH v10 05/11] LoongArch: Add stubs for Linux loading commands Xiaotian Wu
2022-08-24  7:26 ` [PATCH v10 06/11] LoongArch: Add awareness for LoongArch relocations Xiaotian Wu
2022-08-24  7:27 ` [PATCH v10 07/11] LoongArch: Add auxiliary files Xiaotian Wu
2022-08-24  7:27 ` [PATCH v10 08/11] LoongArch: Add to build system Xiaotian Wu
2022-08-24  7:27 ` [PATCH v10 09/11] LoongArch: Support new relocation types Xiaotian Wu
2022-08-24  7:27 ` [PATCH v10 10/11] LoongArch: add efistub kernel support Xiaotian Wu
2022-08-24  7:27 ` [PATCH v10 11/11] tests: add support for LoongArch Xiaotian Wu

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.