Re: [PATCH] LoongArch: Eliminate cmodel compilation warnings

2023-09-18 Thread Xiaotian Wu
在 2023-09-14星期四的 16:26 +0200,Daniel Kiper写道:
> On Tue, Aug 29, 2023 at 04:39:09PM +0200, Daniel Kiper wrote:
> > On Thu, Aug 24, 2023 at 09:04:01PM +0800, Xiaotian Wu wrote:
> > > In the configure phase, the "-mcmodel=large" CFLAGS passed the
> > > test, but
> > > because it has not been implemented in gcc, the following warning
> > > will appear
> > > when compiling:
> > > 
> > >     gcc: warning: 'large' is not supported, now cmodel is set to
> > > 'normal'
> > 
> > Missing Signed-off-by. I can add it on your behalf if you are OK
> > with it.
> 
> Ping?

Please help me to add the Signed-off-by information, thank you.

Signed-off-by: Xiaotian Wu 

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

-- 
Best Regards
Xiaotian Wu


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


[PATCH] LoongArch: Eliminate cmodel compilation warnings

2023-08-24 Thread Xiaotian Wu
In the configure phase, the "-mcmodel=large" CFLAGS passed the test, but
because it has not been implemented in gcc, the following warning will appear
when compiling:

gcc: warning: 'large' is not supported, now cmodel is set to 'normal'
---
 configure.ac | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/configure.ac b/configure.ac
index 27c5918fa..a5e5f54cf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1262,8 +1262,7 @@ AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)
 
 LDFLAGS="$TARGET_LDFLAGS"
 
-if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test 
"$target_cpu" = riscv64 \
-  || test "$target_cpu" = loongarch64 ; then
+if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test 
"$target_cpu" = riscv64 ; 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"
-- 
2.41.0


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


[PATCH v5 0/1] loongarch: Disable relaxation for grub

2023-06-15 Thread Xiaotian Wu
v4->v5:
- update commit message

v3->v4:
- rebase on the latest master branch
- check "-mno-relax" and "-Wa,-mno-relax"

v2->v3:
- Use the -mno-relax cflags for gcc

v1->v2:
- split patch
- drop cast code

Xiaotian Wu (1):
  loongarch: disable relaxation relocations

 configure.ac | 23 +++
 1 file changed, 23 insertions(+)

-- 
2.40.1


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


[PATCH v5 1/1] loongarch: disable relaxation relocations

2023-06-15 Thread Xiaotian Wu
A working grub cannot be built with upcoming binutils and gcc, because linker
relaxation was added [1] causing new unsupported relocations to appear in 
modules.

So we pass -mno-relax to gcc if it is supported, to disable relaxation and make
grub forward-compatible with new toolchains.

While similar code already exists for sparc64 in configure.ac, sparc64 sets
LDFLAGS while loongarch requires CFLAGS to be set. If we only set LDFLAGS on
loongarch, gcc will still generate relaxation relocations in the .o files, so
the sparc64 code cannot be reused.

[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b

Signed-off-by: Xiaotian Wu 
---
 configure.ac | 23 +++
 1 file changed, 23 insertions(+)

diff --git a/configure.ac b/configure.ac
index d9f088d12..a0ea5beae 100644
--- a/configure.ac
+++ b/configure.ac
@@ -874,6 +874,29 @@ if test "x$target_cpu" = xloongarch64; then
 TARGET_CFLAGS="$TARGET_CFLAGS -mno-explicit-relocs -fno-plt"
 TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-explicit-relocs -fno-plt"
   fi
+
+  AC_CACHE_CHECK([for no-relax options], grub_cv_target_cc_mno_relax, [
+grub_cv_target_cc_mno_relax=no
+for cand in "-mno-relax" "-Wa,-mno-relax"; do
+  if test x"$grub_cv_target_cc_mno_relax" != xno ; then
+break
+  fi
+  CFLAGS="$TARGET_CFLAGS $cand -Werror"
+  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
+   asm (".globl start; start:");
+   void __main (void);
+   void __main (void) {}
+   int main (void);
+   ]], [[]])], [grub_cv_target_cc_mno_relax="$cand"], [])
+done
+  ])
+  CFLAGS="$TARGET_CFLAGS"
+
+  if test x"$grub_cv_target_cc_mno_relax" != xno ; then
+TARGET_CFLAGS="$TARGET_CFLAGS $grub_cv_target_cc_mno_relax"
+TARGET_CCASFLAGS="$TARGET_CCASFLAGS $grub_cv_target_cc_mno_relax"
+  fi
+
   TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
   TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
 fi
-- 
2.40.1


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


Re: [PATCH v3 5/5] loongarch: Use the -mno-relax cflags for gcc

2023-06-14 Thread Xiaotian Wu
在 2023-06-13星期二的 20:09 +0800,Xi Ruoyao via Grub-devel写道:
> On Tue, 2023-06-13 at 13:39 +0200, Daniel Kiper wrote:
> > On Tue, Jun 13, 2023 at 05:06:35PM +0800, Xiaotian Wu wrote:
> > > Because the binutils of the loongarch architecture adds
> > > relaxation
> > > support [1],
> > > the next version of binutils will not be able to build grub.
> > > 
> > > So we use the -mno-relax cflags to disable gcc to generate
> > > relaxation
> > > relocations to enhance the compatibility of grub.
> > > 
> > > [1]:
> > > https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b
> > > 
> > > Signed-off-by: Xiaotian Wu 
> > > ---
> > >  configure.ac | 12 
> > >  1 file changed, 12 insertions(+)
> > > 
> > > diff --git a/configure.ac b/configure.ac
> > > index d9f088d12..cd3cc6ba8 100644
> > > --- a/configure.ac
> > > +++ b/configure.ac
> > > @@ -874,6 +874,18 @@ if test "x$target_cpu" = xloongarch64; then
> > >  TARGET_CFLAGS="$TARGET_CFLAGS -mno-explicit-relocs -fno-plt"
> > >  TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-explicit-relocs -
> > > fno-
> > > plt"
> > >    fi
> > > +
> > > +  AC_CACHE_CHECK([whether _mno_relax works],
> > > [grub_cv_cc_mno_relax], [
> > > +    CFLAGS="$TARGET_CFLAGS -mno-relax -Werror"
> > > +    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
> > > +   [grub_cv_cc_mno_relax=yes],
> > > +   [grub_cv_cc_mno_relax=no])
> > > +  ])
> > > +  if test "x$grub_cv_cc_mno_relax" = xyes; then
> > > +    TARGET_CFLAGS="$TARGET_CFLAGS -mno-relax"
> > > +    TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-relax"
> > > +  fi
> > 
> > Could not you reuse sparc64 code which is ~100 lines below? If not
> > please make loongarch code as similar as possible to the sparc64
> > one.
> 
> More important reason is we need to try -Wa,-mno-relax along with -
> mno-
> relax, like SPARC64.  GCC 13 and earlier does not accept -mno-relax,
> but
> the assembler itself can still generate R_LARCH_RELAX etc.  So if a
> distro uses GCC 13 alongside Binutils 2.41, we'll need -Wa,-mno-
> relax.
> 
> Note that Binutils 2.41 will be released in Feb 2024 but GCC 14 will
> be
> released in mid 2024 (Apr to Jun) so it can be expected that some
> distros will really use Binutils 2.41 and GCC 13.

Code for sparc64 cannot be reused.
Because sparc64 sets LDFLAGS, and loongarch needs to set CFLAGS.
If only LDFLAGS is used on loongarch, gcc still generates .o files with
RELAX.

This point was also mentioned in mengqinggang's email:
https://lists.gnu.org/archive/html/grub-devel/2023-06/msg00062.html

> 
> -- 
> Xi Ruoyao 
> School of Aerospace Science and Technology, Xidian University
> 

-- 
Best Regards
Xiaotian Wu


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


[PATCH v4 0/1] loongarch: Disable relaxation for grub

2023-06-14 Thread Xiaotian Wu
A working grub cannot be built with upcoming binutils and gcc, because linker
relaxation was added [1] causing new unsupported relocations to appear in 
modules.

So we pass -mno-relax to gcc if it is supported, to disable relaxation and make
grub forward-compatible with new toolchains.

[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b

v3->v4:
- rebase on the latest master branch
- check "-mno-relax" and "-Wa,-mno-relax"

v2->v3:
- Use the -mno-relax cflags for gcc

v1->v2:
- split patch
- drop cast code


Xiaotian Wu (1):
  loongarch: disable relaxation relocations

 configure.ac | 23 +++
 1 file changed, 23 insertions(+)

-- 
2.40.1


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


[PATCH v4 1/1] loongarch: disable relaxation relocations

2023-06-14 Thread Xiaotian Wu
A working grub cannot be built with upcoming binutils and gcc, because linker
relaxation was added [1] causing new unsupported relocations to appear in 
modules.

So we pass -mno-relax to gcc if it is supported, to disable relaxation and make
grub forward-compatible with new toolchains.

[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b

Signed-off-by: Xiaotian Wu 
---
 configure.ac | 23 +++
 1 file changed, 23 insertions(+)

diff --git a/configure.ac b/configure.ac
index d9f088d12..a0ea5beae 100644
--- a/configure.ac
+++ b/configure.ac
@@ -874,6 +874,29 @@ if test "x$target_cpu" = xloongarch64; then
 TARGET_CFLAGS="$TARGET_CFLAGS -mno-explicit-relocs -fno-plt"
 TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-explicit-relocs -fno-plt"
   fi
+
+  AC_CACHE_CHECK([for no-relax options], grub_cv_target_cc_mno_relax, [
+grub_cv_target_cc_mno_relax=no
+for cand in "-mno-relax" "-Wa,-mno-relax"; do
+  if test x"$grub_cv_target_cc_mno_relax" != xno ; then
+break
+  fi
+  CFLAGS="$TARGET_CFLAGS $cand -Werror"
+  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
+   asm (".globl start; start:");
+   void __main (void);
+   void __main (void) {}
+   int main (void);
+   ]], [[]])], [grub_cv_target_cc_mno_relax="$cand"], [])
+done
+  ])
+  CFLAGS="$TARGET_CFLAGS"
+
+  if test x"$grub_cv_target_cc_mno_relax" != xno ; then
+TARGET_CFLAGS="$TARGET_CFLAGS $grub_cv_target_cc_mno_relax"
+TARGET_CCASFLAGS="$TARGET_CCASFLAGS $grub_cv_target_cc_mno_relax"
+  fi
+
   TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
   TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
 fi
-- 
2.40.1


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


[PATCH v3 1/5] Use the correct format specifier for formatted output

2023-06-13 Thread Xiaotian Wu
Use "PRIxGRUB_INT64_T" format specifier for "grub_int64_t" type, and drop the
casts code.

Signed-off-by: Xiaotian Wu 
Reviewed-by: Daniel Kiper 
---
 grub-core/kern/arm64/dl_helper.c   | 4 ++--
 grub-core/kern/loongarch64/dl_helper.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/grub-core/kern/arm64/dl_helper.c b/grub-core/kern/arm64/dl_helper.c
index cf7d432a3..10e3d1ec2 100644
--- a/grub-core/kern/arm64/dl_helper.c
+++ b/grub-core/kern/arm64/dl_helper.c
@@ -46,9 +46,9 @@ grub_arm64_set_26_offset (grub_uint32_t *place, 
grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc00);
 
-  grub_dprintf ("dl", "  reloc_64 %p %c= 0x%llx\n",
+  grub_dprintf ("dl", "  reloc_64 %p %c= 0x%" PRIxGRUB_INT64_T "\n",
place, offset > 0 ? '+' : '-',
-   offset < 0 ? (long long) -(unsigned long long) offset : offset);
+   offset < 0 ? -offset : offset);
 
   *place &= insmask;
   *place |= grub_cpu_to_le32 (offset >> 2) & ~insmask;
diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index cda1a53c8..e869ce9ac 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -205,9 +205,9 @@ 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 (0xfc00);
 
-  grub_dprintf ("dl", "  reloc_64 %p %c= 0x%llx\n",
+  grub_dprintf ("dl", "  reloc_b26 %p %c= 0x%" PRIxGRUB_INT64_T "\n",
place, offset > 0 ? '+' : '-',
-   offset < 0 ? (long long) -(unsigned long long) offset : offset);
+   offset < 0 ? -offset : offset);
 
   val = ((offset >> 18) & 0x3ff) | (((offset >> 2) & 0x) << 10);
 
-- 
2.40.1


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


[PATCH v3 4/5] loongarch: Add ELF relocation types documentation and comments

2023-06-13 Thread Xiaotian Wu
see https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#relocations

Signed-off-by: Xiaotian Wu 
Reviewed-by: Daniel Kiper 
---
 grub-core/kern/loongarch64/dl_helper.c | 25 +
 1 file changed, 25 insertions(+)

diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index 879ae6189..b5d39b282 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -24,6 +24,10 @@
 #include 
 #include 
 
+/*
+ * LoongArch relocations documentation:
+ * https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#relocations
+ */
 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);
 
@@ -200,6 +204,11 @@ grub_loongarch64_sop_32_s_0_10_10_16_s2 
(grub_loongarch64_stack_t stack,
   *place =(*place) | ((a >> 18) & 0x3ff);
 }
 
+/*
+ * B26 relocation for the 28-bit PC-relative jump
+ * (*(uint32_t *) PC) [9 ... 0] = (S+A-PC) [27 ... 18]
+ * (*(uint32_t *) PC) [25 ... 10] = (S+A-PC) [17 ... 2]
+ */
 void grub_loongarch64_b26 (grub_uint32_t *place, grub_int64_t offset)
 {
   grub_uint32_t val;
@@ -215,6 +224,10 @@ void grub_loongarch64_b26 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
+/*
+ * ABS_HI20/PCALA_HI20 relocations for 32/64-bit absolute address/PC-relative 
offset
+ * (*(uint32_t *) PC) [24 ... 5] = (S+A) [31 ... 12]
+ */
 void grub_loongarch64_xxx_hi20 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfe1f);
@@ -227,6 +240,10 @@ void grub_loongarch64_xxx_hi20 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
+/*
+ * ABS_LO12/PCALA_LO12 relocations for 32/64-bit absolute address
+ * (*(uint32_t *) PC) [21 ... 10] = (S+A) [11 ... 0]
+ */
 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);
@@ -235,6 +252,10 @@ void grub_loongarch64_xxx_lo12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (offset << 10) & ~insmask;
 }
 
+/*
+ * ABS64_HI12 relocation for the 64-bit absolute address
+ * (*(uint32_t *) PC) [21 ... 10] = (S+A) [63 ... 52]
+ */
 void grub_loongarch64_abs64_hi12 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xffc003ff);
@@ -247,6 +268,10 @@ void grub_loongarch64_abs64_hi12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
+/*
+ * ABS64_LO20 relocation for the 64-bit absolute address
+ * (*(uint32_t *) PC) [24 ... 5] = (S+A) [51 ... 32]
+ */
 void grub_loongarch64_abs64_lo20 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfe1f);
-- 
2.40.1


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


[PATCH v3 2/5] loongarch: Optimize code using pc variable

2023-06-13 Thread Xiaotian Wu
We already have the pc variable, no need to calculate it again.

Signed-off-by: Xiaotian Wu 
Reviewed-by: Daniel Kiper 
---
 util/grub-mkimagexx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index e7d5bc631..a3ab04eb1 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -1156,7 +1156,7 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct 
section_metadata *smd,
 {
   grub_int64_t off;
 
-  off = sym_addr - target_section_addr - offset - 
image_target->vaddr_offset;
+  off = sym_addr - pc;
 
   grub_loongarch64_b26 (t32, off);
 }
-- 
2.40.1


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


[PATCH v3 0/5] loongarch: Disable relaxation for grub

2023-06-13 Thread Xiaotian Wu
Because the binutils of the loongarch architecture adds relaxation support [1],
the next version of binutils will not be able to build grub.

So we use the -mno-relax cflags to disable gcc to generate relaxation
relocations to enhance the compatibility of grub.

[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b

v2->v3:
- Use the -mno-relax cflags for gcc

v1->v2:
- split patch
- drop cast code

Xiaotian Wu (5):
  Use the correct format specifier for formatted output
  loongarch: Optimize code using pc variable
  loongarch: Rename function names
  loongarch: Add ELF relocation types documentation and comments
  loongarch: Use the -mno-relax cflags for gcc

 configure.ac   | 12 ++
 grub-core/kern/arm64/dl_helper.c   |  4 ++--
 grub-core/kern/loongarch64/dl.c|  4 ++--
 grub-core/kern/loongarch64/dl_helper.c | 33 ++
 include/grub/loongarch64/reloc.h   |  4 ++--
 util/grub-mkimagexx.c  |  6 ++---
 6 files changed, 50 insertions(+), 13 deletions(-)

-- 
2.40.1


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


[PATCH v3 3/5] loongarch: Rename function names

2023-06-13 Thread Xiaotian Wu
According to the relocation documentation, the following function names are
renamed to show their exact meaning:

- from grub_loongarch64_xxx64_hi12() to grub_loongarch64_abs64_hi12()
- from grub_loongarch64_xxx64_hi12() to grub_loongarch64_abs64_lo20()

Signed-off-by: Xiaotian Wu 
Reviewed-by: Daniel Kiper 
---
 grub-core/kern/loongarch64/dl.c| 4 ++--
 grub-core/kern/loongarch64/dl_helper.c | 4 ++--
 include/grub/loongarch64/reloc.h   | 4 ++--
 util/grub-mkimagexx.c  | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index 43080e72e..7f923b415 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -109,13 +109,13 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
case R_LARCH_ABS64_LO20:
  {
grub_uint32_t *abs_place = place;
-   grub_loongarch64_xxx64_lo20 (abs_place, sym_addr);
+   grub_loongarch64_abs64_lo20 (abs_place, sym_addr);
  }
  break;
case R_LARCH_ABS64_HI12:
  {
grub_uint32_t *abs_place = place;
-   grub_loongarch64_xxx64_hi12 (abs_place, sym_addr);
+   grub_loongarch64_abs64_hi12 (abs_place, sym_addr);
  }
  break;
case R_LARCH_PCALA_HI20:
diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index e869ce9ac..879ae6189 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -235,7 +235,7 @@ void grub_loongarch64_xxx_lo12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (offset << 10) & ~insmask;
 }
 
-void grub_loongarch64_xxx64_hi12 (grub_uint32_t *place, grub_int64_t offset)
+void grub_loongarch64_abs64_hi12 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xffc003ff);
   grub_uint32_t val;
@@ -247,7 +247,7 @@ void grub_loongarch64_xxx64_hi12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
-void grub_loongarch64_xxx64_lo20 (grub_uint32_t *place, grub_int64_t offset)
+void grub_loongarch64_abs64_lo20 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfe1f);
   grub_uint32_t val;
diff --git a/include/grub/loongarch64/reloc.h b/include/grub/loongarch64/reloc.h
index dec7a6b36..8ba355385 100644
--- a/include/grub/loongarch64/reloc.h
+++ b/include/grub/loongarch64/reloc.h
@@ -60,8 +60,8 @@ void grub_loongarch64_sop_32_s_0_10_10_16_s2 
(grub_loongarch64_stack_t stack,
 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);
-void grub_loongarch64_xxx64_hi12  (grub_uint32_t *place, grub_int64_t offset);
-void grub_loongarch64_xxx64_lo20  (grub_uint32_t *place, grub_int64_t offset);
+void grub_loongarch64_abs64_hi12  (grub_uint32_t *place, grub_int64_t offset);
+void grub_loongarch64_abs64_lo20  (grub_uint32_t *place, grub_int64_t offset);
 
 #define GRUB_LOONGARCH64_RELOCATION(STACK, PLACE, OFFSET)  \
   case R_LARCH_SOP_PUSH_ABSOLUTE:  \
diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index a3ab04eb1..e50b29533 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -1165,10 +1165,10 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct 
section_metadata *smd,
 grub_loongarch64_xxx_hi20 (t32, sym_addr);
 break;
   case R_LARCH_ABS64_LO20:
-grub_loongarch64_xxx64_lo20 (t32, sym_addr);
+grub_loongarch64_abs64_lo20 (t32, sym_addr);
 break;
   case R_LARCH_ABS64_HI12:
-grub_loongarch64_xxx64_hi12 (t32, sym_addr);
+grub_loongarch64_abs64_hi12 (t32, sym_addr);
 break;
   case R_LARCH_PCALA_HI20:
 {
-- 
2.40.1


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


[PATCH v3 5/5] loongarch: Use the -mno-relax cflags for gcc

2023-06-13 Thread Xiaotian Wu
Because the binutils of the loongarch architecture adds relaxation support [1],
the next version of binutils will not be able to build grub.

So we use the -mno-relax cflags to disable gcc to generate relaxation
relocations to enhance the compatibility of grub.

[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b

Signed-off-by: Xiaotian Wu 
---
 configure.ac | 12 
 1 file changed, 12 insertions(+)

diff --git a/configure.ac b/configure.ac
index d9f088d12..cd3cc6ba8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -874,6 +874,18 @@ if test "x$target_cpu" = xloongarch64; then
 TARGET_CFLAGS="$TARGET_CFLAGS -mno-explicit-relocs -fno-plt"
 TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-explicit-relocs -fno-plt"
   fi
+
+  AC_CACHE_CHECK([whether _mno_relax works], [grub_cv_cc_mno_relax], [
+CFLAGS="$TARGET_CFLAGS -mno-relax -Werror"
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
+   [grub_cv_cc_mno_relax=yes],
+   [grub_cv_cc_mno_relax=no])
+  ])
+  if test "x$grub_cv_cc_mno_relax" = xyes; then
+TARGET_CFLAGS="$TARGET_CFLAGS -mno-relax"
+TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-relax"
+  fi
+
   TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
   TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
 fi
-- 
2.40.1


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


Re: [PATCH v2 0/5] loongarch: add relaxation support

2023-06-13 Thread Xiaotian Wu
Maybe you can tell me how to add compiler options, thank you.

在 2023-06-13星期二的 14:54 +0800,Xiaotian Wu写道:
> 在 2023-06-13星期二的 14:33 +0800,mengqinggang写道:
> > LDFLAGS do not affect relocations generation. If just modify
> > LDFLAGS,
> > grub still needs to process relaxation relocations.
> >  
> 
> According to you, the no-relax test logic of sparc64 is wrong? So we
> still need patch #5 in v2?
> 
> > 在 2023/6/13 下午2:22, Xiaotian Wu 写道:
> >  
> > > 在 2023-06-13星期二的 11:25 +0800,mengqinggang写道:
> > >  
> > > > Is this  patch used to check if ld supports --no-relax option?
> > > > It need to pass -mno-relax option to as or gcc to disable
> > > > binutils
> > > > generate relaxation relocations.
> > > > 
> > > Check "-mno-relax" and "-Wl,--no-relax" in order, if "-mno-relax"
> > > is
> > > valid, then LDFLAGS="$LDFLAGS -mno-relax"; if "-Wl,--no-relax" is
> > > valid, then LDFLAGS="$LDFLAGS -Wl,--no-relax", otherwise report
> > > an
> > > error.
> > > 
> > > The full code is here:
> > > https://github.com/loongarch64/grub/blob/dev-master/configure.ac#L976-L1000
> > > 
> > > Using binutils 2.40, the LDFLAGS value displayed in the Makefile
> > > is
> > > as
> > > follows, without "-mno-relax" in it:
> > > 
> > > TARGET_LDFLAGS =  "-Wl,--no-relax -no-pie -Wl,--build-id=none"
> > > 
> > >  
> > > > 在 2023/6/13 上午10:37, Xiaotian Wu 写道:
> > > >  
> > > >  
> > > > > New patch is ready:
> > > > > https://github.com/loongarch64/grub/commits/dev-master
> > > > > 
> > > > > I need your help to confirm that binutils-2.40 and binutils-
> > > > > dev
> > > > > code
> > > > > have the same behavior when using compile options, thanks.
> > > > > 
> > > > > 在 2023-06-12星期一的 06:35 +0800,Xi Ruoyao via Grub-devel写道:
> > > > >  
> > > > >  
> > > > > > On Wed, 2023-06-07 at 15:34 +0800, Xiaotian Wu wrote:
> > > > > >  
> > > > > >  
> > > > > > > Because the binutils of the loongarch architecture adds
> > > > > > > relaxation
> > > > > > > support [1], the next version of binutils will not be
> > > > > > > able
> > > > > > > to
> > > > > > > build
> > > > > > > grub.
> > > > > > > 
> > > > > > > So we added the R_LARCH_B16, R_LARCH_B21 and
> > > > > > > R_LARCH_RELAX
> > > > > > > relocations
> > > > > > > to enhance grub compatibility.
> > > > > > Wouldn't it be easier to just pass -mno-relax to the
> > > > > > toolchain
> > > > > > when
> > > > > > we
> > > > > > build GRUB?  I don't think it makes too much sense to
> > > > > > perform
> > > > > > relaxation
> > > > > > on a boot loader.  The boot loader is generally the coldest
> > > > > > code
> > > > > > paths
> > > > > > in a system, so relaxing it won't give any real benefit
> > > > > > (well, if
> > > > > > you
> > > > > > reboot a system 100 times you may finally save one second),
> > > > > > but
> > > > > > increases the maintenance burden.
> > > > > > 
> > > > > > Consider a new relaxation pattern is added after we release
> > > > > > GRUB
> > > > > > 2.12. 
> > > > > > Then if we simply pass -mno-relax, GRUB 2.12 will continue
> > > > > > to
> > > > > > work. 
> > > > > > But
> > > > > > if we try to "support" relaxation this way, every distro
> > > > > > will
> > > > > > have to
> > > > > > patch GRUB 2.12 when the toolchain is updated.
> > > > > > 
> > > > > >  
> > > > > >  
> > > > > > > [1]:
> > > > > > > https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b
> > > > > > > 
> > > > > > > v1->v2:
> > > > > > > - split patch
> > > > > > > - drop cast code
> > > > > > > 
> > > > > > > Xiaotian Wu (5):
> > > > > > >   Use the correct format specifier for formatted output
> > > > > > >   loongarch: Optimize code using pc variable
> > > > > > >   loongarch: Rename function names
> > > > > > >   loongarch: Add ELF relocation types documentation and
> > > > > > > comments
> > > > > > >   loongarch: Add relaxation support
> > > > > > > 
> > > > > > >  grub-core/kern/arm64/dl_helper.c   |  4 +-
> > > > > > >  grub-core/kern/loongarch64/dl.c    | 21 +++-
> > > > > > >  grub-core/kern/loongarch64/dl_helper.c | 72
> > > > > > > -
> > > > > > > -
> > > > > > >  include/grub/elf.h |  3 ++
> > > > > > >  include/grub/loongarch64/reloc.h   |  6 ++-
> > > > > > >  util/grub-mkimagexx.c  | 28 --
> > > > > > >  util/grub-module-verifier.c    |  3 ++
> > > > > > >  7 files changed, 124 insertions(+), 13 deletions(-)
> > > > > > > 
> > > >  
> >  
> 

-- 
Best Regards
Xiaotian Wu


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


Re: [PATCH v2 0/5] loongarch: add relaxation support

2023-06-13 Thread Xiaotian Wu
在 2023-06-13星期二的 14:33 +0800,mengqinggang写道:
> LDFLAGS do not affect relocations generation. If just modify LDFLAGS,
> grub still needs to process relaxation relocations.
>  

According to you, the no-relax test logic of sparc64 is wrong? So we
still need patch #5 in v2?

> 在 2023/6/13 下午2:22, Xiaotian Wu 写道:
>  
> > 在 2023-06-13星期二的 11:25 +0800,mengqinggang写道:
> >  
> > > Is this  patch used to check if ld supports --no-relax option?
> > > It need to pass -mno-relax option to as or gcc to disable
> > > binutils
> > > generate relaxation relocations.
> > > 
> > Check "-mno-relax" and "-Wl,--no-relax" in order, if "-mno-relax"
> > is
> > valid, then LDFLAGS="$LDFLAGS -mno-relax"; if "-Wl,--no-relax" is
> > valid, then LDFLAGS="$LDFLAGS -Wl,--no-relax", otherwise report an
> > error.
> > 
> > The full code is here:
> > https://github.com/loongarch64/grub/blob/dev-master/configure.ac#L976-L1000
> > 
> > Using binutils 2.40, the LDFLAGS value displayed in the Makefile is
> > as
> > follows, without "-mno-relax" in it:
> > 
> > TARGET_LDFLAGS =  "-Wl,--no-relax -no-pie -Wl,--build-id=none"
> > 
> >  
> > > 在 2023/6/13 上午10:37, Xiaotian Wu 写道:
> > >  
> > >  
> > > > New patch is ready:
> > > > https://github.com/loongarch64/grub/commits/dev-master
> > > > 
> > > > I need your help to confirm that binutils-2.40 and binutils-dev
> > > > code
> > > > have the same behavior when using compile options, thanks.
> > > > 
> > > > 在 2023-06-12星期一的 06:35 +0800,Xi Ruoyao via Grub-devel写道:
> > > >  
> > > >  
> > > > > On Wed, 2023-06-07 at 15:34 +0800, Xiaotian Wu wrote:
> > > > >  
> > > > >  
> > > > > > Because the binutils of the loongarch architecture adds
> > > > > > relaxation
> > > > > > support [1], the next version of binutils will not be able
> > > > > > to
> > > > > > build
> > > > > > grub.
> > > > > > 
> > > > > > So we added the R_LARCH_B16, R_LARCH_B21 and R_LARCH_RELAX
> > > > > > relocations
> > > > > > to enhance grub compatibility.
> > > > > Wouldn't it be easier to just pass -mno-relax to the
> > > > > toolchain
> > > > > when
> > > > > we
> > > > > build GRUB?  I don't think it makes too much sense to perform
> > > > > relaxation
> > > > > on a boot loader.  The boot loader is generally the coldest
> > > > > code
> > > > > paths
> > > > > in a system, so relaxing it won't give any real benefit
> > > > > (well, if
> > > > > you
> > > > > reboot a system 100 times you may finally save one second),
> > > > > but
> > > > > increases the maintenance burden.
> > > > > 
> > > > > Consider a new relaxation pattern is added after we release
> > > > > GRUB
> > > > > 2.12. 
> > > > > Then if we simply pass -mno-relax, GRUB 2.12 will continue to
> > > > > work. 
> > > > > But
> > > > > if we try to "support" relaxation this way, every distro will
> > > > > have to
> > > > > patch GRUB 2.12 when the toolchain is updated.
> > > > > 
> > > > >  
> > > > >  
> > > > > > [1]:
> > > > > > https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b
> > > > > > 
> > > > > > v1->v2:
> > > > > > - split patch
> > > > > > - drop cast code
> > > > > > 
> > > > > > Xiaotian Wu (5):
> > > > > >   Use the correct format specifier for formatted output
> > > > > >   loongarch: Optimize code using pc variable
> > > > > >   loongarch: Rename function names
> > > > > >   loongarch: Add ELF relocation types documentation and
> > > > > > comments
> > > > > >   loongarch: Add relaxation support
> > > > > > 
> > > > > >  grub-core/kern/arm64/dl_helper.c   |  4 +-
> > > > > >  grub-core/kern/loongarch64/dl.c    | 21 +++-
> > > > > >  grub-core/kern/loongarch64/dl_helper.c | 72
> > > > > > -
> > > > > > -
> > > > > >  include/grub/elf.h |  3 ++
> > > > > >  include/grub/loongarch64/reloc.h   |  6 ++-
> > > > > >  util/grub-mkimagexx.c  | 28 --
> > > > > >  util/grub-module-verifier.c    |  3 ++
> > > > > >  7 files changed, 124 insertions(+), 13 deletions(-)
> > > > > > 
> > >  
>  

-- 
Best Regards
Xiaotian Wu


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


Re: [PATCH v2 0/5] loongarch: add relaxation support

2023-06-13 Thread Xiaotian Wu
在 2023-06-13星期二的 11:25 +0800,mengqinggang写道:
> Is this  patch used to check if ld supports --no-relax option?
> It need to pass -mno-relax option to as or gcc to disable binutils
> generate relaxation relocations.
> 

Check "-mno-relax" and "-Wl,--no-relax" in order, if "-mno-relax" is
valid, then LDFLAGS="$LDFLAGS -mno-relax"; if "-Wl,--no-relax" is
valid, then LDFLAGS="$LDFLAGS -Wl,--no-relax", otherwise report an
error.

The full code is here:
https://github.com/loongarch64/grub/blob/dev-master/configure.ac#L976-L1000

Using binutils 2.40, the LDFLAGS value displayed in the Makefile is as
follows, without "-mno-relax" in it:

TARGET_LDFLAGS =  "-Wl,--no-relax -no-pie -Wl,--build-id=none"

> 在 2023/6/13 上午10:37, Xiaotian Wu 写道:
>  
> > New patch is ready:
> > https://github.com/loongarch64/grub/commits/dev-master
> > 
> > I need your help to confirm that binutils-2.40 and binutils-dev
> > code
> > have the same behavior when using compile options, thanks.
> > 
> > 在 2023-06-12星期一的 06:35 +0800,Xi Ruoyao via Grub-devel写道:
> >  
> > > On Wed, 2023-06-07 at 15:34 +0800, Xiaotian Wu wrote:
> > >  
> > > > Because the binutils of the loongarch architecture adds
> > > > relaxation
> > > > support [1], the next version of binutils will not be able to
> > > > build
> > > > grub.
> > > > 
> > > > So we added the R_LARCH_B16, R_LARCH_B21 and R_LARCH_RELAX
> > > > relocations
> > > > to enhance grub compatibility.
> > > Wouldn't it be easier to just pass -mno-relax to the toolchain
> > > when
> > > we
> > > build GRUB?  I don't think it makes too much sense to perform
> > > relaxation
> > > on a boot loader.  The boot loader is generally the coldest code
> > > paths
> > > in a system, so relaxing it won't give any real benefit (well, if
> > > you
> > > reboot a system 100 times you may finally save one second), but
> > > increases the maintenance burden.
> > > 
> > > Consider a new relaxation pattern is added after we release GRUB
> > > 2.12. 
> > > Then if we simply pass -mno-relax, GRUB 2.12 will continue to
> > > work. 
> > > But
> > > if we try to "support" relaxation this way, every distro will
> > > have to
> > > patch GRUB 2.12 when the toolchain is updated.
> > > 
> > >  
> > > > [1]:
> > > > https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b
> > > > 
> > > > v1->v2:
> > > > - split patch
> > > > - drop cast code
> > > > 
> > > > Xiaotian Wu (5):
> > > >   Use the correct format specifier for formatted output
> > > >   loongarch: Optimize code using pc variable
> > > >   loongarch: Rename function names
> > > >   loongarch: Add ELF relocation types documentation and
> > > > comments
> > > >   loongarch: Add relaxation support
> > > > 
> > > >  grub-core/kern/arm64/dl_helper.c   |  4 +-
> > > >  grub-core/kern/loongarch64/dl.c    | 21 +++-
> > > >  grub-core/kern/loongarch64/dl_helper.c | 72
> > > > -
> > > > -
> > > >  include/grub/elf.h |  3 ++
> > > >  include/grub/loongarch64/reloc.h   |  6 ++-
> > > >  util/grub-mkimagexx.c  | 28 --
> > > >  util/grub-module-verifier.c    |  3 ++
> > > >  7 files changed, 124 insertions(+), 13 deletions(-)
> > > > 
>  

-- 
Best Regards
Xiaotian Wu


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


Re: [PATCH v2 0/5] loongarch: add relaxation support

2023-06-12 Thread Xiaotian Wu
New patch is ready:
https://github.com/loongarch64/grub/commits/dev-master

I need your help to confirm that binutils-2.40 and binutils-dev code
have the same behavior when using compile options, thanks.

在 2023-06-12星期一的 06:35 +0800,Xi Ruoyao via Grub-devel写道:
> On Wed, 2023-06-07 at 15:34 +0800, Xiaotian Wu wrote:
> > Because the binutils of the loongarch architecture adds relaxation
> > support [1], the next version of binutils will not be able to build
> > grub.
> > 
> > So we added the R_LARCH_B16, R_LARCH_B21 and R_LARCH_RELAX
> > relocations
> > to enhance grub compatibility.
> 
> Wouldn't it be easier to just pass -mno-relax to the toolchain when
> we
> build GRUB?  I don't think it makes too much sense to perform
> relaxation
> on a boot loader.  The boot loader is generally the coldest code
> paths
> in a system, so relaxing it won't give any real benefit (well, if you
> reboot a system 100 times you may finally save one second), but
> increases the maintenance burden.
> 
> Consider a new relaxation pattern is added after we release GRUB
> 2.12. 
> Then if we simply pass -mno-relax, GRUB 2.12 will continue to work. 
> But
> if we try to "support" relaxation this way, every distro will have to
> patch GRUB 2.12 when the toolchain is updated.
> 
> > [1]:
> > https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b
> > 
> > v1->v2:
> > - split patch
> > - drop cast code
> > 
> > Xiaotian Wu (5):
> >   Use the correct format specifier for formatted output
> >   loongarch: Optimize code using pc variable
> >   loongarch: Rename function names
> >   loongarch: Add ELF relocation types documentation and comments
> >   loongarch: Add relaxation support
> > 
> >  grub-core/kern/arm64/dl_helper.c   |  4 +-
> >  grub-core/kern/loongarch64/dl.c    | 21 +++-
> >  grub-core/kern/loongarch64/dl_helper.c | 72
> > -
> > -
> >  include/grub/elf.h |  3 ++
> >  include/grub/loongarch64/reloc.h   |  6 ++-
> >  util/grub-mkimagexx.c  | 28 --
> >  util/grub-module-verifier.c    |  3 ++
> >  7 files changed, 124 insertions(+), 13 deletions(-)
> > 
> 

-- 
Best Regards
Xiaotian Wu


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


Re: [PATCH v2 0/5] loongarch: add relaxation support

2023-06-11 Thread Xiaotian Wu
在 2023-06-12星期一的 06:35 +0800,Xi Ruoyao via Grub-devel写道:
> On Wed, 2023-06-07 at 15:34 +0800, Xiaotian Wu wrote:
> > Because the binutils of the loongarch architecture adds relaxation
> > support [1], the next version of binutils will not be able to build
> > grub.
> > 
> > So we added the R_LARCH_B16, R_LARCH_B21 and R_LARCH_RELAX
> > relocations
> > to enhance grub compatibility.
> 
> Wouldn't it be easier to just pass -mno-relax to the toolchain when
> we
> build GRUB?  I don't think it makes too much sense to perform
> relaxation
> on a boot loader.  The boot loader is generally the coldest code
> paths
> in a system, so relaxing it won't give any real benefit (well, if you
> reboot a system 100 times you may finally save one second), but
> increases the maintenance burden.
> 

Yes, it's easy to pass -mno-relax, but binutils-2.40 doesn't support
this option.

> Consider a new relaxation pattern is added after we release GRUB
> 2.12. 
> Then if we simply pass -mno-relax, GRUB 2.12 will continue to work. 
> But
> if we try to "support" relaxation this way, every distro will have to
> patch GRUB 2.12 when the toolchain is updated.
> 

GRUB 2.12 has not been released yet. If this patch can be merged before
the release of grub 2.12, every distribution does not need to be
patched. Moreover, both binutils-2.40 and future binutils 2.41+ will be
able to compile normally.

> > [1]:
> > https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b
> > 
> > v1->v2:
> > - split patch
> > - drop cast code
> > 
> > Xiaotian Wu (5):
> >   Use the correct format specifier for formatted output
> >   loongarch: Optimize code using pc variable
> >   loongarch: Rename function names
> >   loongarch: Add ELF relocation types documentation and comments
> >   loongarch: Add relaxation support
> > 
> >  grub-core/kern/arm64/dl_helper.c   |  4 +-
> >  grub-core/kern/loongarch64/dl.c    | 21 +++-
> >  grub-core/kern/loongarch64/dl_helper.c | 72
> > -
> > -
> >  include/grub/elf.h |  3 ++
> >  include/grub/loongarch64/reloc.h   |  6 ++-
> >  util/grub-mkimagexx.c  | 28 --
> >  util/grub-module-verifier.c    |  3 ++
> >  7 files changed, 124 insertions(+), 13 deletions(-)
> > 
> 

-- 
Best Regards
Xiaotian Wu


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


[PATCH v2 0/5] loongarch: add relaxation support

2023-06-07 Thread Xiaotian Wu
Because the binutils of the loongarch architecture adds relaxation support [1], 
the next version of binutils will not be able to build grub.

So we added the R_LARCH_B16, R_LARCH_B21 and R_LARCH_RELAX relocations to 
enhance grub compatibility.

[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b

v1->v2:
- split patch
- drop cast code

Xiaotian Wu (5):
  Use the correct format specifier for formatted output
  loongarch: Optimize code using pc variable
  loongarch: Rename function names
  loongarch: Add ELF relocation types documentation and comments
  loongarch: Add relaxation support

 grub-core/kern/arm64/dl_helper.c   |  4 +-
 grub-core/kern/loongarch64/dl.c| 21 +++-
 grub-core/kern/loongarch64/dl_helper.c | 72 --
 include/grub/elf.h |  3 ++
 include/grub/loongarch64/reloc.h   |  6 ++-
 util/grub-mkimagexx.c  | 28 --
 util/grub-module-verifier.c|  3 ++
 7 files changed, 124 insertions(+), 13 deletions(-)

-- 
2.20.1


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


[PATCH v2 5/5] loongarch: Add relaxation support

2023-06-07 Thread Xiaotian Wu
Because the binutils of the loongarch architecture adds relaxation support [1],
the next version of binutils will not be able to build grub.

So we added the R_LARCH_B16, R_LARCH_B21 and R_LARCH_RELAX [2] relocations to
enhance grub compatibility.

[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b
[2]: 
https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#relocations

Signed-off-by: Xiaotian Wu 
---
 grub-core/kern/loongarch64/dl.c| 17 +++
 grub-core/kern/loongarch64/dl_helper.c | 39 ++
 include/grub/elf.h |  3 ++
 include/grub/loongarch64/reloc.h   |  2 ++
 util/grub-mkimagexx.c  | 22 +++
 util/grub-module-verifier.c|  3 ++
 6 files changed, 86 insertions(+)

diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index 7f923b415..c22d2bd52 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -87,11 +87,28 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
  }
  break;
case R_LARCH_MARK_LA:
+   case R_LARCH_RELAX:
  break;
case R_LARCH_SOP_PUSH_PCREL:
case R_LARCH_SOP_PUSH_PLT_PCREL:
  grub_loongarch64_sop_push (, sym_addr - (grub_uint64_t)place);
  break;
+   case R_LARCH_B16:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_ssize_t off = sym_addr - (grub_addr_t) place;
+
+   grub_loongarch64_b16 (abs_place, off);
+ }
+ break;
+   case R_LARCH_B21:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_ssize_t off = sym_addr - (grub_addr_t) place;
+
+   grub_loongarch64_b21 (abs_place, off);
+ }
+ break;
case R_LARCH_B26:
  {
grub_uint32_t *abs_place = place;
diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index b5d39b282..a3569b1e5 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -204,6 +204,45 @@ grub_loongarch64_sop_32_s_0_10_10_16_s2 
(grub_loongarch64_stack_t stack,
   *place =(*place) | ((a >> 18) & 0x3ff);
 }
 
+/*
+ * B16 relocation for the 18-bit PC-relative jump
+ * (*(uint32_t *) PC) [25 ... 10] = (S+A-PC) [17 ... 2]
+ */
+void grub_loongarch64_b16 (grub_uint32_t *place, grub_int64_t offset)
+{
+  grub_uint32_t val;
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc0003ff);
+
+  grub_dprintf ("dl", "  reloc_b16 %p %c= 0x%" PRIxGRUB_INT64_T "\n",
+   place, offset > 0 ? '+' : '-',
+   offset < 0 ? -offset : offset);
+
+  val = ((offset >> 2) & 0x) << 10;
+
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (val) & ~insmask;
+}
+
+/*
+ * B21 relocation for the 23-bit PC-relative jump
+ * (*(uint32_t *) PC) [4 ... 0] = (S+A-PC) [22 ... 18]
+ * (*(uint32_t *) PC) [25 ... 10] = (S+A-PC) [17 ... 2]
+ */
+void grub_loongarch64_b21 (grub_uint32_t *place, grub_int64_t offset)
+{
+  grub_uint32_t val;
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc0003e0);
+
+  grub_dprintf ("dl", "  reloc_b21 %p %c= 0x%" PRIxGRUB_INT64_T "\n",
+   place, offset > 0 ? '+' : '-',
+   offset < 0 ? -offset : offset);
+
+  val = ((offset >> 18) & 0x1f) | (((offset >> 2) & 0x) << 10);
+
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (val) & ~insmask;
+}
+
 /*
  * B26 relocation for the 28-bit PC-relative jump
  * (*(uint32_t *) PC) [9 ... 0] = (S+A-PC) [27 ... 18]
diff --git a/include/grub/elf.h b/include/grub/elf.h
index bd313a70b..12349c9e4 100644
--- a/include/grub/elf.h
+++ b/include/grub/elf.h
@@ -2558,6 +2558,8 @@ typedef Elf32_Addr Elf32_Conflict;
 #define R_LARCH_SOP_POP_32_S_5_2043
 #define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
 #define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+#define R_LARCH_B16  64
+#define R_LARCH_B21  65
 #define R_LARCH_B26  66
 #define R_LARCH_ABS_HI20 67
 #define R_LARCH_ABS_LO12 68
@@ -2565,6 +2567,7 @@ typedef Elf32_Addr Elf32_Conflict;
 #define R_LARCH_ABS64_HI12   70
 #define R_LARCH_PCALA_HI20   71
 #define R_LARCH_PCALA_LO12   72
+#define R_LARCH_RELAX100
 
 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 8ba355385..01d4f6831 100644
--- a/include/grub/loongarch64/reloc.h
+++ b/include/grub/loongarch64/reloc.h
@@ -57,6 +57,8 @@ void grub_

[PATCH v2 2/5] loongarch: Optimize code using pc variable

2023-06-07 Thread Xiaotian Wu
We already have the pc variable, no need to calculate it again.

Signed-off-by: Xiaotian Wu 
---
 util/grub-mkimagexx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index e7d5bc631..a3ab04eb1 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -1156,7 +1156,7 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct 
section_metadata *smd,
 {
   grub_int64_t off;
 
-  off = sym_addr - target_section_addr - offset - 
image_target->vaddr_offset;
+  off = sym_addr - pc;
 
   grub_loongarch64_b26 (t32, off);
 }
-- 
2.20.1


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


[PATCH v2 1/5] Use the correct format specifier for formatted output

2023-06-07 Thread Xiaotian Wu
Use "PRIxGRUB_INT64_T" format specifier for "grub_int64_t" type, and drop the
casts code.

Signed-off-by: Xiaotian Wu 
---
 grub-core/kern/arm64/dl_helper.c   | 4 ++--
 grub-core/kern/loongarch64/dl_helper.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/grub-core/kern/arm64/dl_helper.c b/grub-core/kern/arm64/dl_helper.c
index cf7d432a3..10e3d1ec2 100644
--- a/grub-core/kern/arm64/dl_helper.c
+++ b/grub-core/kern/arm64/dl_helper.c
@@ -46,9 +46,9 @@ grub_arm64_set_26_offset (grub_uint32_t *place, 
grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc00);
 
-  grub_dprintf ("dl", "  reloc_64 %p %c= 0x%llx\n",
+  grub_dprintf ("dl", "  reloc_64 %p %c= 0x%" PRIxGRUB_INT64_T "\n",
place, offset > 0 ? '+' : '-',
-   offset < 0 ? (long long) -(unsigned long long) offset : offset);
+   offset < 0 ? -offset : offset);
 
   *place &= insmask;
   *place |= grub_cpu_to_le32 (offset >> 2) & ~insmask;
diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index cda1a53c8..e869ce9ac 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -205,9 +205,9 @@ 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 (0xfc00);
 
-  grub_dprintf ("dl", "  reloc_64 %p %c= 0x%llx\n",
+  grub_dprintf ("dl", "  reloc_b26 %p %c= 0x%" PRIxGRUB_INT64_T "\n",
place, offset > 0 ? '+' : '-',
-   offset < 0 ? (long long) -(unsigned long long) offset : offset);
+   offset < 0 ? -offset : offset);
 
   val = ((offset >> 18) & 0x3ff) | (((offset >> 2) & 0x) << 10);
 
-- 
2.20.1


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


[PATCH v2 4/5] loongarch: Add ELF relocation types documentation and comments

2023-06-07 Thread Xiaotian Wu
see https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#relocations

Signed-off-by: Xiaotian Wu 
---
 grub-core/kern/loongarch64/dl_helper.c | 25 +
 1 file changed, 25 insertions(+)

diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index 879ae6189..b5d39b282 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -24,6 +24,10 @@
 #include 
 #include 
 
+/*
+ * LoongArch relocations documentation:
+ * https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#relocations
+ */
 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);
 
@@ -200,6 +204,11 @@ grub_loongarch64_sop_32_s_0_10_10_16_s2 
(grub_loongarch64_stack_t stack,
   *place =(*place) | ((a >> 18) & 0x3ff);
 }
 
+/*
+ * B26 relocation for the 28-bit PC-relative jump
+ * (*(uint32_t *) PC) [9 ... 0] = (S+A-PC) [27 ... 18]
+ * (*(uint32_t *) PC) [25 ... 10] = (S+A-PC) [17 ... 2]
+ */
 void grub_loongarch64_b26 (grub_uint32_t *place, grub_int64_t offset)
 {
   grub_uint32_t val;
@@ -215,6 +224,10 @@ void grub_loongarch64_b26 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
+/*
+ * ABS_HI20/PCALA_HI20 relocations for 32/64-bit absolute address/PC-relative 
offset
+ * (*(uint32_t *) PC) [24 ... 5] = (S+A) [31 ... 12]
+ */
 void grub_loongarch64_xxx_hi20 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfe1f);
@@ -227,6 +240,10 @@ void grub_loongarch64_xxx_hi20 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
+/*
+ * ABS_LO12/PCALA_LO12 relocations for 32/64-bit absolute address
+ * (*(uint32_t *) PC) [21 ... 10] = (S+A) [11 ... 0]
+ */
 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);
@@ -235,6 +252,10 @@ void grub_loongarch64_xxx_lo12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (offset << 10) & ~insmask;
 }
 
+/*
+ * ABS64_HI12 relocation for the 64-bit absolute address
+ * (*(uint32_t *) PC) [21 ... 10] = (S+A) [63 ... 52]
+ */
 void grub_loongarch64_abs64_hi12 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xffc003ff);
@@ -247,6 +268,10 @@ void grub_loongarch64_abs64_hi12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
+/*
+ * ABS64_LO20 relocation for the 64-bit absolute address
+ * (*(uint32_t *) PC) [24 ... 5] = (S+A) [51 ... 32]
+ */
 void grub_loongarch64_abs64_lo20 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfe1f);
-- 
2.20.1


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


[PATCH v2 3/5] loongarch: Rename function names

2023-06-07 Thread Xiaotian Wu
According to the relocation documentation, the following function names are
renamed to show their exact meaning:

- from grub_loongarch64_xxx64_hi12() to grub_loongarch64_abs64_hi12()
- from grub_loongarch64_xxx64_hi12() to grub_loongarch64_abs64_lo20()

Signed-off-by: Xiaotian Wu 
---
 grub-core/kern/loongarch64/dl.c| 4 ++--
 grub-core/kern/loongarch64/dl_helper.c | 4 ++--
 include/grub/loongarch64/reloc.h   | 4 ++--
 util/grub-mkimagexx.c  | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index 43080e72e..7f923b415 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -109,13 +109,13 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
case R_LARCH_ABS64_LO20:
  {
grub_uint32_t *abs_place = place;
-   grub_loongarch64_xxx64_lo20 (abs_place, sym_addr);
+   grub_loongarch64_abs64_lo20 (abs_place, sym_addr);
  }
  break;
case R_LARCH_ABS64_HI12:
  {
grub_uint32_t *abs_place = place;
-   grub_loongarch64_xxx64_hi12 (abs_place, sym_addr);
+   grub_loongarch64_abs64_hi12 (abs_place, sym_addr);
  }
  break;
case R_LARCH_PCALA_HI20:
diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index e869ce9ac..879ae6189 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -235,7 +235,7 @@ void grub_loongarch64_xxx_lo12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (offset << 10) & ~insmask;
 }
 
-void grub_loongarch64_xxx64_hi12 (grub_uint32_t *place, grub_int64_t offset)
+void grub_loongarch64_abs64_hi12 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xffc003ff);
   grub_uint32_t val;
@@ -247,7 +247,7 @@ void grub_loongarch64_xxx64_hi12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
-void grub_loongarch64_xxx64_lo20 (grub_uint32_t *place, grub_int64_t offset)
+void grub_loongarch64_abs64_lo20 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfe1f);
   grub_uint32_t val;
diff --git a/include/grub/loongarch64/reloc.h b/include/grub/loongarch64/reloc.h
index dec7a6b36..8ba355385 100644
--- a/include/grub/loongarch64/reloc.h
+++ b/include/grub/loongarch64/reloc.h
@@ -60,8 +60,8 @@ void grub_loongarch64_sop_32_s_0_10_10_16_s2 
(grub_loongarch64_stack_t stack,
 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);
-void grub_loongarch64_xxx64_hi12  (grub_uint32_t *place, grub_int64_t offset);
-void grub_loongarch64_xxx64_lo20  (grub_uint32_t *place, grub_int64_t offset);
+void grub_loongarch64_abs64_hi12  (grub_uint32_t *place, grub_int64_t offset);
+void grub_loongarch64_abs64_lo20  (grub_uint32_t *place, grub_int64_t offset);
 
 #define GRUB_LOONGARCH64_RELOCATION(STACK, PLACE, OFFSET)  \
   case R_LARCH_SOP_PUSH_ABSOLUTE:  \
diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index a3ab04eb1..e50b29533 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -1165,10 +1165,10 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct 
section_metadata *smd,
 grub_loongarch64_xxx_hi20 (t32, sym_addr);
 break;
   case R_LARCH_ABS64_LO20:
-grub_loongarch64_xxx64_lo20 (t32, sym_addr);
+grub_loongarch64_abs64_lo20 (t32, sym_addr);
 break;
   case R_LARCH_ABS64_HI12:
-grub_loongarch64_xxx64_hi12 (t32, sym_addr);
+grub_loongarch64_abs64_hi12 (t32, sym_addr);
 break;
   case R_LARCH_PCALA_HI20:
 {
-- 
2.20.1


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


[PATCH v1 1/1] loongarch: add relaxation support

2023-06-05 Thread Xiaotian Wu
Add R_LARCH_B16, R_LARCH_B21 and R_LARCH_RELAX relocation types to support 
LoongArch relaxation.

https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b
https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#relocations

Signed-off-by: Xiaotian Wu 
---
 grub-core/kern/loongarch64/dl.c| 21 +++-
 grub-core/kern/loongarch64/dl_helper.c | 72 --
 include/grub/elf.h |  3 ++
 include/grub/loongarch64/reloc.h   |  6 ++-
 util/grub-mkimagexx.c  | 28 --
 util/grub-module-verifier.c|  3 ++
 6 files changed, 122 insertions(+), 11 deletions(-)

diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index 43080e72e..c22d2bd52 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -87,11 +87,28 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
  }
  break;
case R_LARCH_MARK_LA:
+   case R_LARCH_RELAX:
  break;
case R_LARCH_SOP_PUSH_PCREL:
case R_LARCH_SOP_PUSH_PLT_PCREL:
  grub_loongarch64_sop_push (, sym_addr - (grub_uint64_t)place);
  break;
+   case R_LARCH_B16:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_ssize_t off = sym_addr - (grub_addr_t) place;
+
+   grub_loongarch64_b16 (abs_place, off);
+ }
+ break;
+   case R_LARCH_B21:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_ssize_t off = sym_addr - (grub_addr_t) place;
+
+   grub_loongarch64_b21 (abs_place, off);
+ }
+ break;
case R_LARCH_B26:
  {
grub_uint32_t *abs_place = place;
@@ -109,13 +126,13 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
case R_LARCH_ABS64_LO20:
  {
grub_uint32_t *abs_place = place;
-   grub_loongarch64_xxx64_lo20 (abs_place, sym_addr);
+   grub_loongarch64_abs64_lo20 (abs_place, sym_addr);
  }
  break;
case R_LARCH_ABS64_HI12:
  {
grub_uint32_t *abs_place = place;
-   grub_loongarch64_xxx64_hi12 (abs_place, sym_addr);
+   grub_loongarch64_abs64_hi12 (abs_place, sym_addr);
  }
  break;
case R_LARCH_PCALA_HI20:
diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index cda1a53c8..2809eeb7f 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -24,6 +24,10 @@
 #include 
 #include 
 
+/*
+ * LoongArch relocations documentation:
+ * https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#relocations
+ */
 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);
 
@@ -200,14 +204,58 @@ grub_loongarch64_sop_32_s_0_10_10_16_s2 
(grub_loongarch64_stack_t stack,
   *place =(*place) | ((a >> 18) & 0x3ff);
 }
 
+/*
+ * B16 relocation for the 18-bit PC-relative jump
+ * (*(uint32_t *) PC) [25 ... 10] = (S+A-PC) [17 ... 2]
+ */
+void grub_loongarch64_b16 (grub_uint32_t *place, grub_int64_t offset)
+{
+  grub_uint32_t val;
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc0003ff);
+
+  grub_dprintf ("dl", "  reloc_b16 %p %c= 0x%"PRIxGRUB_INT64_T"\n",
+   place, offset > 0 ? '+' : '-',
+   offset < 0 ? (grub_int64_t) -(grub_uint64_t) offset : offset);
+
+  val = ((offset >> 2) & 0x) << 10;
+
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (val) & ~insmask;
+}
+
+/*
+ * B21 relocation for the 23-bit PC-relative jump
+ * (*(uint32_t *) PC) [4 ... 0] = (S+A-PC) [22 ... 18]
+ * (*(uint32_t *) PC) [25 ... 10] = (S+A-PC) [17 ... 2]
+ */
+void grub_loongarch64_b21 (grub_uint32_t *place, grub_int64_t offset)
+{
+  grub_uint32_t val;
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc0003e0);
+
+  grub_dprintf ("dl", "  reloc_b21 %p %c= 0x%"PRIxGRUB_INT64_T"\n",
+   place, offset > 0 ? '+' : '-',
+   offset < 0 ? (grub_int64_t) -(grub_uint64_t) offset : offset);
+
+  val = ((offset >> 18) & 0x1f) | (((offset >> 2) & 0x) << 10);
+
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (val) & ~insmask;
+}
+
+/*
+ * B26 relocation for the 28-bit PC-relative jump
+ * (*(uint32_t *) PC) [9 ... 0] = (S+A-PC) [27 ... 18]
+ * (*(uint32_t *) PC) [25 ... 10] = (S+A-PC) [17 ... 2]
+ */
 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 (0xfc00);
 
-  grub_dprintf ("dl", "  reloc_64 %p %c= 0x%llx\n",
+  grub_dprintf ("dl", &

[PATCH v1 0/1] loongarch: add relaxation support

2023-06-05 Thread Xiaotian Wu
Because the binutils of the loongarch architecture adds relaxation support [1], 
the next version of binutils will not be able to build grub.

So we added the R_LARCH_B16, R_LARCH_B21 and R_LARCH_RELAX relocations to 
enhance grub compatibility.

[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b

Xiaotian Wu (1):
  loongarch: add relaxation support

 grub-core/kern/loongarch64/dl.c| 21 +++-
 grub-core/kern/loongarch64/dl_helper.c | 72 --
 include/grub/elf.h |  3 ++
 include/grub/loongarch64/reloc.h   |  6 ++-
 util/grub-mkimagexx.c  | 28 --
 util/grub-module-verifier.c|  3 ++
 6 files changed, 122 insertions(+), 11 deletions(-)

-- 
2.40.1


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


[PATCH] loongarch64: add relaxation support

2023-05-25 Thread Xiaotian Wu
The toolchain for the loongarch architecture will contain a series of patches 
[1]
in the next release to add relaxation support for the LoongArch.

In order for the new toolchain to compile grub 2.12, support for relocation 
types
such as R_LARCH_B16, R_LARCH_B21 and R_LARCH_RELAX to be added.

[1]: https://sourceware.org/pipermail/binutils/2023-May/127539.html

Signed-off-by: Xiaotian Wu 
---
 grub-core/kern/loongarch64/dl.c| 17 ++
 grub-core/kern/loongarch64/dl_helper.c | 32 +-
 include/grub/elf.h |  3 +++
 include/grub/loongarch64/reloc.h   |  2 ++
 util/grub-mkimagexx.c  | 24 ++-
 util/grub-module-verifier.c|  3 +++
 6 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index 43080e72e..4341bf5e4 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -87,11 +87,28 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
  }
  break;
case R_LARCH_MARK_LA:
+   case R_LARCH_RELAX:
  break;
case R_LARCH_SOP_PUSH_PCREL:
case R_LARCH_SOP_PUSH_PLT_PCREL:
  grub_loongarch64_sop_push (, sym_addr - (grub_uint64_t)place);
  break;
+   case R_LARCH_B16:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_ssize_t off = sym_addr - (grub_addr_t) place;
+
+   grub_loongarch64_b16 (abs_place, off);
+ }
+ break;
+   case R_LARCH_B21:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_ssize_t off = sym_addr - (grub_addr_t) place;
+
+   grub_loongarch64_b21 (abs_place, off);
+ }
+ break;
case R_LARCH_B26:
  {
grub_uint32_t *abs_place = place;
diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index e8ec1219f..4d8d83a7e 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -200,12 +200,42 @@ grub_loongarch64_sop_32_s_0_10_10_16_s2 
(grub_loongarch64_stack_t stack,
   *place =(*place) | ((a >> 18) & 0x3ff);
 }
 
+void grub_loongarch64_b16 (grub_uint32_t *place, grub_int64_t offset)
+{
+  grub_uint32_t val;
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc0003ff);
+
+  grub_dprintf ("dl", "  reloc_b16 %p %c= 0x%llx\n",
+   place, offset > 0 ? '+' : '-',
+   offset < 0 ? (long long) -(unsigned long long) offset : offset);
+
+  val = ((offset >> 2) & 0x) << 10;
+
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (val) & ~insmask;
+}
+
+void grub_loongarch64_b21 (grub_uint32_t *place, grub_int64_t offset)
+{
+  grub_uint32_t val;
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc0003e0);
+
+  grub_dprintf ("dl", "  reloc_b21 %p %c= 0x%llx\n",
+   place, offset > 0 ? '+' : '-',
+   offset < 0 ? (long long) -(unsigned long long) offset : offset);
+
+  val = ((offset >> 18) & 0x1f) | (((offset >> 2) & 0x) << 10);
+
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (val) & ~insmask;
+}
+
 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 (0xfc00);
 
-  grub_dprintf ("dl", "  reloc_64 %p %c= 0x%llx\n",
+  grub_dprintf ("dl", "  reloc_b26 %p %c= 0x%llx\n",
place, offset > 0 ? '+' : '-',
offset < 0 ? (long long) -(unsigned long long) offset : offset);
 
diff --git a/include/grub/elf.h b/include/grub/elf.h
index bd313a70b..12349c9e4 100644
--- a/include/grub/elf.h
+++ b/include/grub/elf.h
@@ -2558,6 +2558,8 @@ typedef Elf32_Addr Elf32_Conflict;
 #define R_LARCH_SOP_POP_32_S_5_2043
 #define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
 #define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+#define R_LARCH_B16  64
+#define R_LARCH_B21  65
 #define R_LARCH_B26  66
 #define R_LARCH_ABS_HI20 67
 #define R_LARCH_ABS_LO12 68
@@ -2565,6 +2567,7 @@ typedef Elf32_Addr Elf32_Conflict;
 #define R_LARCH_ABS64_HI12   70
 #define R_LARCH_PCALA_HI20   71
 #define R_LARCH_PCALA_LO12   72
+#define R_LARCH_RELAX100
 
 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 dec7a6b36..524ca78a1 100644
--- a/include/grub/loongarch64/reloc.h
+++ b/include/g

Re: [PATCH] loongarch: Avoid undefined behavior when popping from an empty reloc stack

2023-05-17 Thread Xiaotian Wu
LGTM, thank you.

在 2023-05-18星期四的 10:52 +0800,WANG Xuerui写道:
> The return value of grub_loongarch64_stack_pop is unsigned, so -1
> should
> not be used in the first place. Replacing with 0 is enough to avoid
> the
> UB in this edge case.
> 
> Technically though, proper error handling is needed throughout the
> management of the reloc stack, so no unexpected behavior will happen
> even in case of malformed object code input (right now, pushes become
> no-ops when the stack is full, and garbage results if the stack does
> not
> contain enough operands for an op). The refactor would touch some
> more
> places so would be best done in a separate series.
> 
> Fixes: CID 40
> Fixes: CID 407778
> 
> Signed-off-by: WANG Xuerui 
> ---
>  grub-core/kern/loongarch64/dl_helper.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/grub-core/kern/loongarch64/dl_helper.c b/grub-
> core/kern/loongarch64/dl_helper.c
> index e8ec1219f..cda1a53c8 100644
> --- a/grub-core/kern/loongarch64/dl_helper.c
> +++ b/grub-core/kern/loongarch64/dl_helper.c
> @@ -46,7 +46,7 @@ static grub_uint64_t
>  grub_loongarch64_stack_pop (grub_loongarch64_stack_t stack)
>  {
>    if (stack->top == -1)
> -    return -1;
> +    return 0;
>    return stack->data[stack->top--];
>  }
>  

-- 
Best Regards
Xiaotian Wu


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


Re: [PATCH v16 10/10] tests: Add LoongArch to various test cases

2023-05-02 Thread Xiaotian Wu
在 2023-04-28星期五的 22:36 -0500,Glenn Washburn写道:
> On Thu, 27 Apr 2023 15:46:22 +0800
> Xiaotian Wu  wrote:
> 
> > I ran the test suite on a 3A5000 desktop, a LoongArch architecture
> > machine, using Archlinux for LoongArch distro, see
> > https://github.com/loongarchlinux.
> > 
> > Some software versions are:
> > * linux 6.3.0-rc4
> > * gcc 13.0.1 20230312
> > * binutils 2.40
> > * qemu 7.2.0
> > 
> > The test results of running "make check" with qemu 7.2 are as
> > follows:
> > 
> > =
> >    GRUB 2.11: ./test-suite.log
> > =
> > 
> >   # TOTAL: 85
> >   # PASS:  73
> >   # SKIP:  8
> >   # XFAIL: 0
> >   # FAIL:  2
> >   # XPASS: 0
> >   # ERROR: 2
> > 
> > .. contents:: :depth: 2
> > 
> > ERROR: f2fs_test
> > 
> > 
> > mount: /tmp/grub-fs-
> > tester.20230418175640563815408.f2fs.UDs/f2fs_rw:
> > unknown filesystem type 'f2fs'. dmesg(1) may have more information
> > after failed mount system call. MOUNT FAILED.
> > ERROR f2fs_test (exit status: 99)
> > 
> > FAIL: hfs_test
> > ==
> > 
> > recode: Request `utf8..macroman' is erroneous
> > mkfs.hfs: name required with -v option
> > FAIL hfs_test (exit status: 1)
> > 
> > ERROR: zfs_test
> > ===
> > 
> > zpool not installed; cannot test zfs.
> > ERROR zfs_test (exit status: 99)
> > 
> > SKIP: pata_test
> > ===
> > 
> > SKIP pata_test (exit status: 77)
> > 
> > SKIP: ahci_test
> > ===
> > 
> > SKIP ahci_test (exit status: 77)
> > 
> > SKIP: uhci_test
> > ===
> > 
> > SKIP uhci_test (exit status: 77)
> > 
> > SKIP: ohci_test
> > ===
> > 
> > SKIP ohci_test (exit status: 77)
> > 
> > SKIP: ehci_test
> > ===
> > 
> > SKIP ehci_test (exit status: 77)
> > 
> > SKIP: fddboot_test
> > ==
> > 
> > SKIP fddboot_test (exit status: 77)
> > 
> > SKIP: netboot_test
> > ==
> > 
> > SKIP netboot_test (exit status: 77)
> > 
> > SKIP: pseries_test
> > ==
> > 
> > SKIP pseries_test (exit status: 77)
> > 
> > FAIL: grub_func_test
> > 
> > 
> > WARNING: Image format was not specified for
> > '/tmp/grub-shell.HeTAD8Ty3U/grub.iso' and probing guessed raw.
> > Automatically detecting the format is dangerous for raw images,
> > write
> > operations on block 0 will be restricted. Specify the 'raw' format
> > explicitly to remove the restrictions. Functional test failure:
> > shift_test: ... gfxterm_menu_640x480xi16:3 failed: 0xce34981e vs
> > 0xd9f04953 tests/video_checksum.c:checksum:615: assert failed: 0
> > Checksum gfxterm_menu_640x480xi16:2 failed: 0xa8fb749d vs
> > 0xbf3fa5d0
> >  tests/video_checksum.c:checksum:615: assert failed: 0 Checksum
> > gfxterm_menu_640x480xi16:1 failed: 0xce34981e vs 0xd9f04953
> > gfxterm_menu: FAIL
> > ...
> > videotest_checksum:
> > videotest_checksum: PASS
> > exfctest:
> > exfctest: PASS
> > TEST FAILURE
> > FAIL grub_func_test (exit status: 1)
> > 
> > We got 2 errors:
> > 
> > * f2fs_test
> > The kernel uses 16k pages, causing failures when loading the f2fs
> > kernel module, see
> > https://github.com/torvalds/linux/blob/master/fs/f2fs/super.c#L4670
> > This error can be ignored.
> > 
> > * zfs_test
> > zfs does not support the LoongArch architecture and is not
> > compatible
> > with the 6.3 kernel.
> > This error can be ignored.
> > 
> > We got 2 failures:
> > 
> > * hfs_test
> > I use recode 3.7.14-1 on Archlinux, running `recode -l` gives no
> > output `MacRoman`, so we get this error.
> > On Linux systems that support LoongArch, there is currently no need
> > to use HFS, so this failure can be ignored.
> 
> This might be because the "mac-roman" kernel module is not loaded.
> Regardless, I think its fine for the HFS tests to not work on this
> architecture.
> 
> > 
> > * grub_func_test
> > I don't know the reason for this failure. I guess it may be related
> > to qemu's edk2. In the previous review, I was told that the failure
> > here is the expected behavior. So, we can ignore this failure.
> 
> The above all seems good to me.
> 
&

Re: [PATCH v15 00/10] Add LoongArch support

2023-04-28 Thread Xiaotian Wu
在 2023-04-25星期二的 17:11 +0200,Daniel Kiper写道:
> On Tue, Apr 18, 2023 at 08:04:07PM +0800, Xiaotian Wu wrote:
> > LoongArch is a new Loongson 3A5000 CPU instruction set, you can
> > read
> > documents [1] or visit the development community [2] to get more
> > information.
> > 
> > [1]:
> > https://loongson.github.io/LoongArch-Documentation/README-EN.html
> > [2]: https://github.com/loongson
> > 
> > This patch series adds support for the Loongarch architecture,
> > which can
> > compile and run the linux-6.2+ kernel on LoongArch.
> > 
> > Please review the patches, thank you.
> > 
> > v14->v15:
> > - Update test code to pass more test cases.
> 
> In general LGTM except a few things:
>   - please explain in the #10 commit message how did you run tests,
>     on which platform, why some tests fail, which failures/errors
>     can be safely ignored, etc.,
>   - please change 2022 year to 2023 in all copyright texts which you
> add,
>   - please use "asm volatile" instead of "__asm__ __volatile__",
>   - all casts should be in form "(grub_addr_t)(void *) var", extra
> space
>     before variable name, instead of "(grub_addr_t)(void *)var",
>   - in general stick to GRUB coding style [1].
> 
> Daniel

Thank you, it has been fixed, please see the v16 patch.
> [1]
> https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Coding-style
> 
> ___
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel

-- 
Best Regards
Xiaotian Wu


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


[PATCH v16 10/10] tests: Add LoongArch to various test cases

2023-04-27 Thread Xiaotian Wu
I ran the test suite on a 3A5000 desktop, a LoongArch architecture machine,
using Archlinux for LoongArch distro, see https://github.com/loongarchlinux.

Some software versions are:
* linux 6.3.0-rc4
* gcc 13.0.1 20230312
* binutils 2.40
* qemu 7.2.0

The test results of running "make check" with qemu 7.2 are as follows:

=
   GRUB 2.11: ./test-suite.log
=

  # TOTAL: 85
  # PASS:  73
  # SKIP:  8
  # XFAIL: 0
  # FAIL:  2
  # XPASS: 0
  # ERROR: 2

.. contents:: :depth: 2

ERROR: f2fs_test


mount: /tmp/grub-fs-tester.20230418175640563815408.f2fs.UDs/f2fs_rw: unknown 
filesystem type 'f2fs'.
   dmesg(1) may have more information after failed mount system call.
MOUNT FAILED.
ERROR f2fs_test (exit status: 99)

FAIL: hfs_test
==

recode: Request `utf8..macroman' is erroneous
mkfs.hfs: name required with -v option
FAIL hfs_test (exit status: 1)

ERROR: zfs_test
===

zpool not installed; cannot test zfs.
ERROR zfs_test (exit status: 99)

SKIP: pata_test
===

SKIP pata_test (exit status: 77)

SKIP: ahci_test
===

SKIP ahci_test (exit status: 77)

SKIP: uhci_test
===

SKIP uhci_test (exit status: 77)

SKIP: ohci_test
===

SKIP ohci_test (exit status: 77)

SKIP: ehci_test
===

SKIP ehci_test (exit status: 77)

SKIP: fddboot_test
==

SKIP fddboot_test (exit status: 77)

SKIP: netboot_test
==

SKIP netboot_test (exit status: 77)

SKIP: pseries_test
==

SKIP pseries_test (exit status: 77)

FAIL: grub_func_test


WARNING: Image format was not specified for 
'/tmp/grub-shell.HeTAD8Ty3U/grub.iso' and probing guessed raw.
 Automatically detecting the format is dangerous for raw images, write 
operations on block 0 will be restricted.
 Specify the 'raw' format explicitly to remove the restrictions.
Functional test failure: shift_test:
...
gfxterm_menu_640x480xi16:3 failed: 0xce34981e vs 0xd9f04953
 tests/video_checksum.c:checksum:615: assert failed: 0 Checksum
gfxterm_menu_640x480xi16:2 failed: 0xa8fb749d vs 0xbf3fa5d0
 tests/video_checksum.c:checksum:615: assert failed: 0 Checksum
gfxterm_menu_640x480xi16:1 failed: 0xce34981e vs 0xd9f04953
gfxterm_menu: FAIL
...
videotest_checksum:
videotest_checksum: PASS
exfctest:
exfctest: PASS
TEST FAILURE
FAIL grub_func_test (exit status: 1)

We got 2 errors:

* f2fs_test
The kernel uses 16k pages, causing failures when loading the f2fs kernel module,
see https://github.com/torvalds/linux/blob/master/fs/f2fs/super.c#L4670
This error can be ignored.

* zfs_test
zfs does not support the LoongArch architecture and is not compatible with the
6.3 kernel.
This error can be ignored.

We got 2 failures:

* hfs_test
I use recode 3.7.14-1 on Archlinux, running `recode -l` gives no output 
`MacRoman`,
so we get this error.
On Linux systems that support LoongArch, there is currently no need to use HFS,
so this failure can be ignored.

* grub_func_test
I don't know the reason for this failure. I guess it may be related to qemu's 
edk2.
In the previous review, I was told that the failure here is the expected 
behavior.
So, we can ignore this failure.

Signed-off-by: Xiaotian Wu 
---
 tests/ahci_test.in   |  2 +-
 tests/ehci_test.in   |  2 +-
 tests/grub_func_test.in  |  2 ++
 tests/ohci_test.in   |  2 +-
 tests/pata_test.in   |  2 +-
 tests/uhci_test.in   |  2 +-
 tests/util/grub-shell-luks-tester.in |  3 +++
 tests/util/grub-shell.in | 15 +++
 8 files changed, 25 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/grub_func_test.in b/tests/grub_func_test.in
index d43427c56..1fa3c4352 100644
--- a/tests/grub_func_test.in
+++ b/tests/grub_func_test.in
@@ -12,6 +12,8 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 # PLATFORM: Max RAM is 256

[PATCH v16 08/10] LoongArch: Add to build system

2023-04-27 Thread Xiaotian Wu
This patch adds LoongArch to the grub build system and various tools,
so grub can be built on LoongArch as a UEFI application.

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 Makefile.util.def   |  1 +
 configure.ac|  8 +-
 gentpl.py   | 27 ++--
 grub-core/Makefile.am   |  6 +
 grub-core/Makefile.core.def | 17 +
 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, 113 insertions(+), 39 deletions(-)

diff --git a/Makefile.util.def b/Makefile.util.def
index beaef1168..fdbd58c8f 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 1ded95ca3..d9f088d12 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) ;;
@@ -1236,7 +1239,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"
@@ -2021,6 +2025,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 88abe5b0a..d0470b3b2 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" 

[PATCH v16 09/10] tests: Fix timezone inconsistency in squashfs_test

2023-04-27 Thread Xiaotian Wu
The image timestamp was not returned in UTC, but the following logic
expected and used UTC.

This patch fixes the test failure like described below:

  unsquashfs -s 
/tmp/grub-fs-tester.20230407111703613257436.squash4_gzip.9R4/squash4_gzip_512_4096_1_0.img
  grep '^Creation'
  awk '{print $6 " " $7 " " $8 " " $9 " " $10; }'
  FSTIME='Fri Apr 7 11:17:05 2023'
  date -d 'Fri Apr 7 11:17:05 2023' -u '+%Y-%m-%d %H:%M:%S'
  FSTIME='2023-04-07 11:17:05'
  date -d '2023-04-07 11:17:05 UTC -1 second' -u '+%Y-%m-%d %H:%M:%S'
  FSTIMEM1='2023-04-07 11:17:04'
  date -d '2023-04-07 11:17:05 UTC -2 second' -u '+%Y-%m-%d %H:%M:%S'
  FSTIMEM2='2023-04-07 11:17:03'
  date -d '2023-04-07 11:17:05 UTC -3 second' -u '+%Y-%m-%d %H:%M:%S'
  FSTIMEM3='2023-04-07 11:17:02'
  grep -F 'Last modification time 2023-04-07 11:17:05'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  grep -F 'Last modification time 2023-04-07 11:17:04'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  grep -F 'Last modification time 2023-04-07 11:17:03'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  grep -F 'Last modification time 2023-04-07 11:17:02'
  echo FSTIME FAIL

Signed-off-by: Xiaotian Wu 
Reviewed-by: Glenn Washburn 
---
 tests/util/grub-fs-tester.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/util/grub-fs-tester.in b/tests/util/grub-fs-tester.in
index 9cc6b5811..1030098b4 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -1454,7 +1454,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" 
"$MAXLOGSECSIZE" 1); do
# Creating the squash image may take more than a few
# seconds. Use the more accurate timestamp from the
# superblock.
-   FSTIME="$(unsquashfs -s "${FSIMAGEP}0.img" | grep 
^Creation | awk '{print $6 " " $7 " " $8 " " $9 " " $10; }')"
+   FSTIME="$(unsquashfs -UTC -s "${FSIMAGEP}0.img" | grep 
^Creation | awk '{print $6 " " $7 " " $8 " " $9 " " $10; }')"
FSTIME="$(date -d "$FSTIME" -u '+%Y-%m-%d %H:%M:%S')";;
*)
FSTIME="$(TZ=UTC ls --time-style="+%Y-%m-%d_%H:%M:%S" 
-l -d "${FSIMAGEP}0.img"|awk '{print $6; }'|sed 's,_, ,g')";;
-- 
2.40.0


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


[PATCH v16 07/10] LoongArch: Add auxiliary files

2023-04-27 Thread Xiaotian Wu
Add support for manipulating architectural cache and timers, and EFI
memory maps.

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 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 3705b8b1b..ac13e95e9 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 0..ff834dca4
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache.c
@@ -0,0 +1,39 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2023 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 
+#include 
+
+/* Prototypes for asm functions. */
+void grub_arch_clean_dcache_range (void);
+void grub_arch_invalidate_icache_range (void);
+
+void
+grub_arch_sync_caches (void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  grub_arch_clean_dcache_range ();
+  grub_arch_invalidate_icache_range ();
+}
+
+void
+grub_arch_sync_dma_caches (volatile void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  /* DMA non-coherent devices not supported yet */
+}
diff --git a/grub-core/kern/loongarch64/cache_flush.S 
b/grub-core/kern/loongarch64/cache_flush.S
new file mode 100644
index 0..a587ed58f
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache_flush.S
@@ -0,0 +1,33 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2023 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 
+
+   .file   "cache_flush.S"
+   .text
+/*
+ * No further work to do because cache consistency is maintained by hardware on
+ * LoongArch.
+ */
+FUNCTION(grub_arch_clean_dcache_range)
+   dbar 0
+   jr $ra
+
+FUNCTION(grub_arch_invalidate_icache_range)
+   ibar 0
+   jr $ra
diff --git a/grub-core/kern/loongarch64/efi/init.c 
b/grub-core/kern/loongarch64/efi/init.c
new file mode 100644
index 0..561c50a0a
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/init.c
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2023 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 

[PATCH v16 01/10] PE: Add LoongArch definitions

2023-04-27 Thread Xiaotian Wu
Add PE machine types [1] and relocation types [2] for LoongArch to
the current in-repo definitions.

[1]: 
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types
[2]: 
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#base-relocation-types

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 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 98c4ff177..101859af1 100644
--- a/include/grub/efi/pe32.h
+++ b/include/grub/efi/pe32.h
@@ -88,6 +88,8 @@ struct grub_pe32_coff_header
 #define GRUB_PE32_MACHINE_X86_64   0x8664
 #define GRUB_PE32_MACHINE_ARMTHUMB_MIXED   0x01c2
 #define GRUB_PE32_MACHINE_ARM640xAA64
+#define GRUB_PE32_MACHINE_LOONGARCH32  0x6232
+#define GRUB_PE32_MACHINE_LOONGARCH64  0x6264
 #define GRUB_PE32_MACHINE_RISCV32  0x5032
 #define GRUB_PE32_MACHINE_RISCV64  0x5064
 
@@ -291,22 +293,24 @@ struct grub_pe32_fixup_block
 
 #define GRUB_PE32_FIXUP_ENTRY(type, offset)(((type) << 12) | (offset))
 
-#define GRUB_PE32_REL_BASED_ABSOLUTE   0
-#define GRUB_PE32_REL_BASED_HIGH   1
-#define GRUB_PE32_REL_BASED_LOW2
-#define GRUB_PE32_REL_BASED_HIGHLOW3
-#define GRUB_PE32_REL_BASED_HIGHADJ4
-#define GRUB_PE32_REL_BASED_MIPS_JMPADDR 5
-#define GRUB_PE32_REL_BASED_ARM_MOV32A  5
-#define GRUB_PE32_REL_BASED_RISCV_HI20 5
-#define GRUB_PE32_REL_BASED_SECTION6
-#define GRUB_PE32_REL_BASED_REL7
-#define GRUB_PE32_REL_BASED_ARM_MOV32T  7
-#define GRUB_PE32_REL_BASED_RISCV_LOW12I 7
-#define GRUB_PE32_REL_BASED_RISCV_LOW12S 8
-#define GRUB_PE32_REL_BASED_IA64_IMM64 9
-#define GRUB_PE32_REL_BASED_DIR64  10
-#define GRUB_PE32_REL_BASED_HIGH3ADJ   11
+#define GRUB_PE32_REL_BASED_ABSOLUTE   0
+#define GRUB_PE32_REL_BASED_HIGH   1
+#define GRUB_PE32_REL_BASED_LOW2
+#define GRUB_PE32_REL_BASED_HIGHLOW3
+#define GRUB_PE32_REL_BASED_HIGHADJ4
+#define GRUB_PE32_REL_BASED_MIPS_JMPADDR   5
+#define GRUB_PE32_REL_BASED_ARM_MOV32A 5
+#define GRUB_PE32_REL_BASED_RISCV_HI20 5
+#define GRUB_PE32_REL_BASED_SECTION6
+#define GRUB_PE32_REL_BASED_REL7
+#define GRUB_PE32_REL_BASED_ARM_MOV32T 7
+#define GRUB_PE32_REL_BASED_RISCV_LOW12I   7
+#define GRUB_PE32_REL_BASED_RISCV_LOW12S   8
+#define GRUB_PE32_REL_BASED_LOONGARCH32_MARK_LA8
+#define GRUB_PE32_REL_BASED_LOONGARCH64_MARK_LA8
+#define GRUB_PE32_REL_BASED_IA64_IMM64 9
+#define GRUB_PE32_REL_BASED_DIR64  10
+#define GRUB_PE32_REL_BASED_HIGH3ADJ   11
 
 struct grub_pe32_symbol
 {
-- 
2.40.0


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


[PATCH v16 02/10] ELF: Add LoongArch definitions

2023-04-27 Thread Xiaotian Wu
Add ELF e_machine ID [1] and relocations types [2] for LoongArch to
the current in-repo definitions.

[1]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_e_machine_identifies_the_machine
[2]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_relocations

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

diff --git a/include/grub/elf.h b/include/grub/elf.h
index e6f073bc9..c36d7dab2 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_PCREL57
 
+/* LoongArch relocations */
+#define R_LARCH_NONE 0
+#define R_LARCH_64   2
+#define R_LARCH_MARK_LA  20
+#define R_LARCH_SOP_PUSH_PCREL   22
+#define R_LARCH_SOP_PUSH_ABSOLUTE23
+#define R_LARCH_SOP_PUSH_PLT_PCREL   29
+#define R_LARCH_SOP_SUB  32
+#define R_LARCH_SOP_SL   33
+#define R_LARCH_SOP_SR   34
+#define R_LARCH_SOP_ADD  35
+#define R_LARCH_SOP_AND  36
+#define R_LARCH_SOP_IF_ELSE  37
+#define R_LARCH_SOP_POP_32_S_10_538
+#define R_LARCH_SOP_POP_32_U_10_12   39
+#define R_LARCH_SOP_POP_32_S_10_12   40
+#define R_LARCH_SOP_POP_32_S_10_16   41
+#define R_LARCH_SOP_POP_32_S_10_16_S242
+#define R_LARCH_SOP_POP_32_S_5_2043
+#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
+#define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+
 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.40.0


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


[PATCH v16 03/10] LoongArch: Add setjmp implementation

2023-04-27 Thread Xiaotian Wu
This patch adds a setjmp implementation for LoongArch.

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

diff --git a/grub-core/lib/loongarch64/setjmp.S 
b/grub-core/lib/loongarch64/setjmp.S
new file mode 100644
index 0..2b46c41a3
--- /dev/null
+++ b/grub-core/lib/loongarch64/setjmp.S
@@ -0,0 +1,69 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2023 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 
+
+   .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..ffb26df79 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(__loongarch_lp64)
+#include "./loongarch64/setjmp.S"
 #elif defined(__riscv)
 #include "./riscv/setjmp.S"
 #else
diff --git a/include/grub/loongarch64/setjmp.h 
b/include/grub/loongarch64/setjmp.h
new file mode 100644
index 0..f9a14d4cf
--- /dev/null
+++ b/include/grub/loongarch64/setjmp.h
@@ -0,0 +1,27 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2023 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.40.0


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


[PATCH v16 05/10] LoongArch: Add support for ELF psABI v1.00 relocations

2023-04-27 Thread Xiaotian Wu
This patch adds support of the stack-based LoongArch relocations
throughout grub, including tools, dynamic linkage, and support for
conversion of ELF relocations into PE ones. A stack machine is required
to handle these per the spec [1] (see the R_LARCH_SOP types), of which
a simple implementation is included.

These relocations are produced by binutils 2.38 and 2.39, while the
newer v2.00 relocs require more recent toolchain (binutils 2.40+ & gcc
13+, or LLVM 16+). GCC 13 has not been officially released as of early
2023, so support for v1.00 relocs are expected to stay relevant for a
while.

[1]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_relocations

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 grub-core/kern/dl.c|   9 +-
 grub-core/kern/loongarch64/dl.c| 102 +
 grub-core/kern/loongarch64/dl_helper.c | 201 +
 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, 522 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, , );
   if (err)
 return err;
@@ -304,7 +306,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
  mod->segment = seg;
}
 }
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   mod->trampptr = ptr;
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
new file mode 100644
index 0..effb4c7b0
--- /dev/null
+++ b/grub-core/kern/loongarch64/dl.c
@@ -0,0 +1,102 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2023 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Check if EHDR is a valid ELF header.  */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+  Elf_Ehdr *e = ehdr;
+
+  /* Check the magic numbers.  */
+  if (e->e_ident[EI_CLASS] != ELFCLASS64
+  || e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_LOONGARCH)
+return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF 
magic"));
+
+  return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/*
+ * Unified function for both REL and RELA.
+ */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+  Elf_Shdr *s, grub_dl_segment_t seg)
+{
+  Elf_Rel *rel, *max;
+  struct grub_loongarch64_stack stack;
+  grub_loongarch64_stack_init ();
+
+  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)
+   re

[PATCH v16 06/10] LoongArch: Add support for ELF psABI v2.00 relocations

2023-04-27 Thread Xiaotian Wu
A new set of relocation types was added in the LoongArch ELF psABI v2.00
spec [1][2] to replace the stack-based scheme in v1.00. Toolchain
support is available from binutils 2.40 and gcc 13 onwards.

This patch adds support for the new relocation types, that are simpler
to handle (in particular, stack operations are gone). Support for the
v1.00 relocs are kept for now, for compatibility with older toolchains.

[1]: https://github.com/loongson/LoongArch-Documentation/pull/57
[2]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_appendix_revision_history

Signed-off-by: Xiaotian Wu 
---
 configure.ac   | 15 +++
 grub-core/kern/loongarch64/dl.c| 54 +--
 grub-core/kern/loongarch64/dl_helper.c | 59 ++
 include/grub/elf.h |  7 +++
 include/grub/loongarch64/reloc.h   |  6 +++
 util/grub-mkimagexx.c  | 49 -
 util/grub-module-verifier.c|  7 +++
 7 files changed, 193 insertions(+), 4 deletions(-)

diff --git a/configure.ac b/configure.ac
index ca42ff8f7..1ded95ca3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -860,6 +860,21 @@ 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 _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_mno_explicit_relocs=yes],
+   [grub_cv_cc_mno_explicit_relocs=no])
+  ])
+  if test "x$grub_cv_cc_mno_explicit_relocs" = xyes; then
+TARGET_CFLAGS="$TARGET_CFLAGS -mno-explicit-relocs -fno-plt"
+TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-explicit-relocs -fno-plt"
+  fi
+  TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
+  TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
+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)
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index effb4c7b0..43080e72e 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -58,7 +58,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 +72,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 +92,47 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
case R_LARCH_SOP_PUSH_PLT_PCREL:
  grub_loongarch64_sop_push (, 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_ABS_HI20:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx_hi20 (abs_place, sym_addr);
+ }
+ break;
+   case R_LARCH_ABS64_LO20:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx64_lo20 (abs_place, sym_addr);
+ }
+ break;
+   case R_LARCH_ABS64_HI12:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx64_hi12 (abs_place, sym_addr);
+ }
+ 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_ABS_LO12:
+   case R_LARCH_PCALA_LO12:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarc

[PATCH v16 04/10] LoongArch: Add early startup code

2023-04-27 Thread Xiaotian Wu
On entry, we need to save the system table pointer as well as our image
handle. Add an early startup file that saves them and then brings us
into our main function.

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

diff --git a/grub-core/kern/loongarch64/efi/startup.S 
b/grub-core/kern/loongarch64/efi/startup.S
new file mode 100644
index 0..87cfb23ea
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/startup.S
@@ -0,0 +1,34 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2023 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 
+
+   .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.40.0


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


[PATCH v16 00/10] Add support for LoongArch

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

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

This patch series adds support for the Loongarch architecture, which can
compile and run the linux-6.2+ kernel on LoongArch.

Please review the patches, thank you.

v14->v15:
- Update test code to pass more test cases.

v15->v16:
- Update commit message for the test patch.
- Change 2022 year to 2023 in copyright texts.
- Use "asm volatile" instead of "__asm__ __volatile__".
- Add extra space in casts code.
- Update commented code according to the GRUB coding style.

Xiaotian Wu (10):
  PE: Add LoongArch definitions
  ELF: Add LoongArch definitions
  LoongArch: Add setjmp implementation
  LoongArch: Add early startup code
  LoongArch: Add support for ELF psABI v1.00 relocations
  LoongArch: Add support for ELF psABI v2.00 relocations
  LoongArch: Add auxiliary files
  LoongArch: Add to build system
  tests: Fix timezone inconsistency in squashfs_test
  tests: Add LoongArch to various test cases

 Makefile.util.def|   1 +
 configure.ac |  23 +-
 gentpl.py|  27 +--
 grub-core/Makefile.am|   6 +
 grub-core/Makefile.core.def  |  17 ++
 grub-core/kern/dl.c  |   9 +-
 grub-core/kern/efi/mm.c  |   3 +-
 grub-core/kern/loongarch64/cache.c   |  39 
 grub-core/kern/loongarch64/cache_flush.S |  33 +++
 grub-core/kern/loongarch64/dl.c  | 150 +
 grub-core/kern/loongarch64/dl_helper.c   | 260 +++
 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 +
 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   |  30 +++
 include/grub/loongarch64/efi/memory.h|  24 +++
 include/grub/loongarch64/reloc.h | 113 ++
 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/grub_func_test.in  |   2 +
 tests/ohci_test.in   |   2 +-
 tests/pata_test.in   |   2 +-
 tests/uhci_test.in   |   2 +-
 tests/util/grub-fs-tester.in |   2 +-
 tests/util/grub-shell-luks-tester.in |   3 +
 tests/util/grub-shell.in |  15 ++
 util/grub-install-common.c   |  49 ++---
 util/grub-install.c  |  16 ++
 util/grub-mkimagexx.c| 126 +++
 util/grub-mknetdir.c |   1 +
 util/grub-mkrescue.c |   8 +
 util/grub-module-verifier.c  |  33 +++
 util/mkimage.c   |  16 ++
 43 files changed, 1264 insertions(+), 67 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 include/grub/loongarch64/efi/memory.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.40.0


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


[PATCH v15 07/10] LoongArch: Add auxiliary files

2023-04-18 Thread Xiaotian Wu
Add support for manipulating architectural cache and timers, and EFI
memory maps.

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 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 3705b8b1b..ac13e95e9 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 0..43d314df9
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache.c
@@ -0,0 +1,39 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+
+/* Prototypes for asm functions. */
+void grub_arch_clean_dcache_range (void);
+void grub_arch_invalidate_icache_range (void);
+
+void
+grub_arch_sync_caches (void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  grub_arch_clean_dcache_range ();
+  grub_arch_invalidate_icache_range ();
+}
+
+void
+grub_arch_sync_dma_caches (volatile void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  /* DMA non-coherent devices not supported yet */
+}
diff --git a/grub-core/kern/loongarch64/cache_flush.S 
b/grub-core/kern/loongarch64/cache_flush.S
new file mode 100644
index 0..43b97d822
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache_flush.S
@@ -0,0 +1,33 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "cache_flush.S"
+   .text
+/*
+ * No further work to do because cache consistency is maintained by hardware on
+ * LoongArch.
+ */
+FUNCTION(grub_arch_clean_dcache_range)
+   dbar 0
+   jr $ra
+
+FUNCTION(grub_arch_invalidate_icache_range)
+   ibar 0
+   jr $ra
diff --git a/grub-core/kern/loongarch64/efi/init.c 
b/grub-core/kern/loongarch64/efi/init.c
new file mode 100644
index 0..8cbeafaba
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/init.c
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope 

[PATCH v15 10/10] tests: Add LoongArch to various test cases

2023-04-18 Thread Xiaotian Wu
The test results of running "make check" with qemu 7.2 are as follows:
=
   GRUB 2.11: ./test-suite.log
=

  # TOTAL: 85
  # PASS:  73
  # SKIP:  8
  # XFAIL: 0
  # FAIL:  2
  # XPASS: 0
  # ERROR: 2

.. contents:: :depth: 2

ERROR: f2fs_test


mount: /tmp/grub-fs-tester.20230418175640563815408.f2fs.UDs/f2fs_rw: unknown 
filesystem type 'f2fs'.
   dmesg(1) may have more information after failed mount system call.
MOUNT FAILED.
ERROR f2fs_test (exit status: 99)

FAIL: hfs_test
==

recode: Request `utf8..macroman' is erroneous
mkfs.hfs: name required with -v option
FAIL hfs_test (exit status: 1)

ERROR: zfs_test
===

zpool not installed; cannot test zfs.
ERROR zfs_test (exit status: 99)

SKIP: pata_test
===

SKIP pata_test (exit status: 77)

SKIP: ahci_test
===

SKIP ahci_test (exit status: 77)

SKIP: uhci_test
===

SKIP uhci_test (exit status: 77)

SKIP: ohci_test
===

SKIP ohci_test (exit status: 77)

SKIP: ehci_test
===

SKIP ehci_test (exit status: 77)

SKIP: fddboot_test
==

SKIP fddboot_test (exit status: 77)

SKIP: netboot_test
==

SKIP netboot_test (exit status: 77)

SKIP: pseries_test
==

SKIP pseries_test (exit status: 77)

FAIL: grub_func_test


WARNING: Image format was not specified for 
'/tmp/grub-shell.HeTAD8Ty3U/grub.iso' and probing guessed raw.
 Automatically detecting the format is dangerous for raw images, write 
operations on block 0 will be restricted.
 Specify the 'raw' format explicitly to remove the restrictions.
Functional test failure: shift_test:
...
gfxterm_menu_640x480xi16:3 failed: 0xce34981e vs 0xd9f04953
 tests/video_checksum.c:checksum:615: assert failed: 0 Checksum
gfxterm_menu_640x480xi16:2 failed: 0xa8fb749d vs 0xbf3fa5d0
 tests/video_checksum.c:checksum:615: assert failed: 0 Checksum
gfxterm_menu_640x480xi16:1 failed: 0xce34981e vs 0xd9f04953
gfxterm_menu: FAIL
...
videotest_checksum:
videotest_checksum: PASS
exfctest:
exfctest: PASS
TEST FAILURE
FAIL grub_func_test (exit status: 1)

Signed-off-by: Xiaotian Wu 
---
 tests/ahci_test.in   |  2 +-
 tests/ehci_test.in   |  2 +-
 tests/grub_func_test.in  |  2 ++
 tests/ohci_test.in   |  2 +-
 tests/pata_test.in   |  2 +-
 tests/uhci_test.in   |  2 +-
 tests/util/grub-shell-luks-tester.in |  3 +++
 tests/util/grub-shell.in | 15 +++
 8 files changed, 25 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/grub_func_test.in b/tests/grub_func_test.in
index d43427c56..1fa3c4352 100644
--- a/tests/grub_func_test.in
+++ b/tests/grub_func_test.in
@@ -12,6 +12,8 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 # PLATFORM: Max RAM is 256M
 mips-qemu_mips | mipsel-qemu_mips)
mem=256M;;
+loongarch64-efi)
+   mem=3G;;
 *)
mem=512M;;
 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)
 

[PATCH v15 08/10] LoongArch: Add to build system

2023-04-18 Thread Xiaotian Wu
This patch adds LoongArch to the grub build system and various tools,
so grub can be built on LoongArch as a UEFI application.

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 Makefile.util.def   |  1 +
 configure.ac|  8 +-
 gentpl.py   | 27 ++--
 grub-core/Makefile.am   |  6 +
 grub-core/Makefile.core.def | 17 +
 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, 113 insertions(+), 39 deletions(-)

diff --git a/Makefile.util.def b/Makefile.util.def
index beaef1168..fdbd58c8f 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 1ded95ca3..d9f088d12 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) ;;
@@ -1236,7 +1239,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"
@@ -2021,6 +2025,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 88abe5b0a..d0470b3b2 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" 

[PATCH v15 09/10] tests: Fix timezone inconsistency in squashfs_test

2023-04-18 Thread Xiaotian Wu
The image timestamp was not returned in UTC, but the following logic
expected and used UTC.

This patch fixes the test failure like described below:

  unsquashfs -s 
/tmp/grub-fs-tester.20230407111703613257436.squash4_gzip.9R4/squash4_gzip_512_4096_1_0.img
  grep '^Creation'
  awk '{print $6 " " $7 " " $8 " " $9 " " $10; }'
  FSTIME='Fri Apr 7 11:17:05 2023'
  date -d 'Fri Apr 7 11:17:05 2023' -u '+%Y-%m-%d %H:%M:%S'
  FSTIME='2023-04-07 11:17:05'
  date -d '2023-04-07 11:17:05 UTC -1 second' -u '+%Y-%m-%d %H:%M:%S'
  FSTIMEM1='2023-04-07 11:17:04'
  date -d '2023-04-07 11:17:05 UTC -2 second' -u '+%Y-%m-%d %H:%M:%S'
  FSTIMEM2='2023-04-07 11:17:03'
  date -d '2023-04-07 11:17:05 UTC -3 second' -u '+%Y-%m-%d %H:%M:%S'
  FSTIMEM3='2023-04-07 11:17:02'
  grep -F 'Last modification time 2023-04-07 11:17:05'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  grep -F 'Last modification time 2023-04-07 11:17:04'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  grep -F 'Last modification time 2023-04-07 11:17:03'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  grep -F 'Last modification time 2023-04-07 11:17:02'
  echo FSTIME FAIL

Signed-off-by: Xiaotian Wu 
Reviewed-by: Glenn Washburn 
---
 tests/util/grub-fs-tester.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/util/grub-fs-tester.in b/tests/util/grub-fs-tester.in
index 9cc6b5811..1030098b4 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -1454,7 +1454,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" 
"$MAXLOGSECSIZE" 1); do
# Creating the squash image may take more than a few
# seconds. Use the more accurate timestamp from the
# superblock.
-   FSTIME="$(unsquashfs -s "${FSIMAGEP}0.img" | grep 
^Creation | awk '{print $6 " " $7 " " $8 " " $9 " " $10; }')"
+   FSTIME="$(unsquashfs -UTC -s "${FSIMAGEP}0.img" | grep 
^Creation | awk '{print $6 " " $7 " " $8 " " $9 " " $10; }')"
FSTIME="$(date -d "$FSTIME" -u '+%Y-%m-%d %H:%M:%S')";;
*)
FSTIME="$(TZ=UTC ls --time-style="+%Y-%m-%d_%H:%M:%S" 
-l -d "${FSIMAGEP}0.img"|awk '{print $6; }'|sed 's,_, ,g')";;
-- 
2.39.2


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


[PATCH v15 05/10] LoongArch: Add support for ELF psABI v1.00 relocations

2023-04-18 Thread Xiaotian Wu
This patch adds support of the stack-based LoongArch relocations
throughout grub, including tools, dynamic linkage, and support for
conversion of ELF relocations into PE ones. A stack machine is required
to handle these per the spec [1] (see the R_LARCH_SOP types), of which
a simple implementation is included.

These relocations are produced by binutils 2.38 and 2.39, while the
newer v2.00 relocs require more recent toolchain (binutils 2.40+ & gcc
13+, or LLVM 16+). GCC 13 has not been officially released as of early
2023, so support for v1.00 relocs are expected to stay relevant for a
while.

[1]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_relocations

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 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, , );
   if (err)
 return err;
@@ -304,7 +306,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
  mod->segment = seg;
}
 }
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   mod->trampptr = ptr;
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
new file mode 100644
index 0..3a6aa91cd
--- /dev/null
+++ b/grub-core/kern/loongarch64/dl.c
@@ -0,0 +1,102 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Check if EHDR is a valid ELF header.  */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+  Elf_Ehdr *e = ehdr;
+
+  /* Check the magic numbers.  */
+  if (e->e_ident[EI_CLASS] != ELFCLASS64
+  || e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_LOONGARCH)
+return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF 
magic"));
+
+  return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/*
+ * Unified function for both REL and RELA.
+ */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+  Elf_Shdr *s, grub_dl_segment_t seg)
+{
+  Elf_Rel *rel, *max;
+  struct grub_loongarch64_stack stack;
+  grub_loongarch64_stack_init ();
+
+  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)
+   r

[PATCH v15 01/10] PE: Add LoongArch definitions

2023-04-18 Thread Xiaotian Wu
Add PE machine types [1] and relocation types [2] for LoongArch to
the current in-repo definitions.

[1]: 
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types
[2]: 
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#base-relocation-types

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 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 98c4ff177..101859af1 100644
--- a/include/grub/efi/pe32.h
+++ b/include/grub/efi/pe32.h
@@ -88,6 +88,8 @@ struct grub_pe32_coff_header
 #define GRUB_PE32_MACHINE_X86_64   0x8664
 #define GRUB_PE32_MACHINE_ARMTHUMB_MIXED   0x01c2
 #define GRUB_PE32_MACHINE_ARM640xAA64
+#define GRUB_PE32_MACHINE_LOONGARCH32  0x6232
+#define GRUB_PE32_MACHINE_LOONGARCH64  0x6264
 #define GRUB_PE32_MACHINE_RISCV32  0x5032
 #define GRUB_PE32_MACHINE_RISCV64  0x5064
 
@@ -291,22 +293,24 @@ struct grub_pe32_fixup_block
 
 #define GRUB_PE32_FIXUP_ENTRY(type, offset)(((type) << 12) | (offset))
 
-#define GRUB_PE32_REL_BASED_ABSOLUTE   0
-#define GRUB_PE32_REL_BASED_HIGH   1
-#define GRUB_PE32_REL_BASED_LOW2
-#define GRUB_PE32_REL_BASED_HIGHLOW3
-#define GRUB_PE32_REL_BASED_HIGHADJ4
-#define GRUB_PE32_REL_BASED_MIPS_JMPADDR 5
-#define GRUB_PE32_REL_BASED_ARM_MOV32A  5
-#define GRUB_PE32_REL_BASED_RISCV_HI20 5
-#define GRUB_PE32_REL_BASED_SECTION6
-#define GRUB_PE32_REL_BASED_REL7
-#define GRUB_PE32_REL_BASED_ARM_MOV32T  7
-#define GRUB_PE32_REL_BASED_RISCV_LOW12I 7
-#define GRUB_PE32_REL_BASED_RISCV_LOW12S 8
-#define GRUB_PE32_REL_BASED_IA64_IMM64 9
-#define GRUB_PE32_REL_BASED_DIR64  10
-#define GRUB_PE32_REL_BASED_HIGH3ADJ   11
+#define GRUB_PE32_REL_BASED_ABSOLUTE   0
+#define GRUB_PE32_REL_BASED_HIGH   1
+#define GRUB_PE32_REL_BASED_LOW2
+#define GRUB_PE32_REL_BASED_HIGHLOW3
+#define GRUB_PE32_REL_BASED_HIGHADJ4
+#define GRUB_PE32_REL_BASED_MIPS_JMPADDR   5
+#define GRUB_PE32_REL_BASED_ARM_MOV32A 5
+#define GRUB_PE32_REL_BASED_RISCV_HI20 5
+#define GRUB_PE32_REL_BASED_SECTION6
+#define GRUB_PE32_REL_BASED_REL7
+#define GRUB_PE32_REL_BASED_ARM_MOV32T 7
+#define GRUB_PE32_REL_BASED_RISCV_LOW12I   7
+#define GRUB_PE32_REL_BASED_RISCV_LOW12S   8
+#define GRUB_PE32_REL_BASED_LOONGARCH32_MARK_LA8
+#define GRUB_PE32_REL_BASED_LOONGARCH64_MARK_LA8
+#define GRUB_PE32_REL_BASED_IA64_IMM64 9
+#define GRUB_PE32_REL_BASED_DIR64  10
+#define GRUB_PE32_REL_BASED_HIGH3ADJ   11
 
 struct grub_pe32_symbol
 {
-- 
2.39.2


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


[PATCH v15 06/10] LoongArch: Add support for ELF psABI v2.00 relocations

2023-04-18 Thread Xiaotian Wu
A new set of relocation types was added in the LoongArch ELF psABI v2.00
spec [1][2] to replace the stack-based scheme in v1.00. Toolchain
support is available from binutils 2.40 and gcc 13 onwards.

This patch adds support for the new relocation types, that are simpler
to handle (in particular, stack operations are gone). Support for the
v1.00 relocs are kept for now, for compatibility with older toolchains.

[1]: https://github.com/loongson/LoongArch-Documentation/pull/57
[2]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_appendix_revision_history

Signed-off-by: Xiaotian Wu 
---
 configure.ac   | 15 +++
 grub-core/kern/loongarch64/dl.c| 54 +--
 grub-core/kern/loongarch64/dl_helper.c | 59 ++
 include/grub/elf.h |  7 +++
 include/grub/loongarch64/reloc.h   |  6 +++
 util/grub-mkimagexx.c  | 49 -
 util/grub-module-verifier.c|  7 +++
 7 files changed, 193 insertions(+), 4 deletions(-)

diff --git a/configure.ac b/configure.ac
index ca42ff8f7..1ded95ca3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -860,6 +860,21 @@ 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 _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_mno_explicit_relocs=yes],
+   [grub_cv_cc_mno_explicit_relocs=no])
+  ])
+  if test "x$grub_cv_cc_mno_explicit_relocs" = xyes; then
+TARGET_CFLAGS="$TARGET_CFLAGS -mno-explicit-relocs -fno-plt"
+TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-explicit-relocs -fno-plt"
+  fi
+  TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
+  TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
+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)
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index 3a6aa91cd..47196a219 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -58,7 +58,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 +72,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 +92,47 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
case R_LARCH_SOP_PUSH_PLT_PCREL:
  grub_loongarch64_sop_push (, 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_ABS_HI20:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx_hi20 (abs_place, sym_addr);
+ }
+ break;
+   case R_LARCH_ABS64_LO20:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx64_lo20 (abs_place, sym_addr);
+ }
+ break;
+   case R_LARCH_ABS64_HI12:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx64_hi12 (abs_place, sym_addr);
+ }
+ 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_ABS_LO12:
+   case R_LARCH_PCALA_LO12:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarc

[PATCH v15 03/10] LoongArch: Add setjmp implementation

2023-04-18 Thread Xiaotian Wu
This patch adds a setjmp implementation for LoongArch.

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

diff --git a/grub-core/lib/loongarch64/setjmp.S 
b/grub-core/lib/loongarch64/setjmp.S
new file mode 100644
index 0..41d58f569
--- /dev/null
+++ b/grub-core/lib/loongarch64/setjmp.S
@@ -0,0 +1,69 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "setjmp.S"
+
+GRUB_MOD_LICENSE "GPLv3+"
+
+   .text
+
+/*
+ * int grub_setjmp (jmp_buf env)
+ */
+FUNCTION(grub_setjmp)
+   st.d $s0, $a0, 0x0
+   st.d $s1, $a0, 0x8
+   st.d $s2, $a0, 0x10
+   st.d $s3, $a0, 0x18
+   st.d $s4, $a0, 0x20
+   st.d $s5, $a0, 0x28
+   st.d $s6, $a0, 0x30
+   st.d $s7, $a0, 0x38
+   st.d $s8, $a0, 0x40
+   st.d $fp, $a0, 0x48
+   st.d $sp, $a0, 0x50
+   st.d $ra, $a0, 0x58
+
+   move $a0, $zero
+   jr   $ra
+
+/*
+ * void grub_longjmp (jmp_buf env, int val)
+ */
+FUNCTION(grub_longjmp)
+   ld.d $s0, $a0, 0x0
+   ld.d $s1, $a0, 0x8
+   ld.d $s2, $a0, 0x10
+   ld.d $s3, $a0, 0x18
+   ld.d $s4, $a0, 0x20
+   ld.d $s5, $a0, 0x28
+   ld.d $s6, $a0, 0x30
+   ld.d $s7, $a0, 0x38
+   ld.d $s8, $a0, 0x40
+   ld.d $fp, $a0, 0x48
+   ld.d $sp, $a0, 0x50
+   ld.d $ra, $a0, 0x58
+
+   /* Return 1 if passed 0, otherwise returns the value in place. */
+   li.w $a0, 1
+   beqz $a1, 1f
+   move $a0, $a1
+1:
+   jr   $ra
diff --git a/grub-core/lib/setjmp.S b/grub-core/lib/setjmp.S
index 9c8721088..ffb26df79 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(__loongarch_lp64)
+#include "./loongarch64/setjmp.S"
 #elif defined(__riscv)
 #include "./riscv/setjmp.S"
 #else
diff --git a/include/grub/loongarch64/setjmp.h 
b/include/grub/loongarch64/setjmp.h
new file mode 100644
index 0..cb3e17763
--- /dev/null
+++ b/include/grub/loongarch64/setjmp.h
@@ -0,0 +1,27 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <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.39.2


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


[PATCH v15 02/10] ELF: Add LoongArch definitions

2023-04-18 Thread Xiaotian Wu
Add ELF e_machine ID [1] and relocations types [2] for LoongArch to
the current in-repo definitions.

[1]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_e_machine_identifies_the_machine
[2]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_relocations

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

diff --git a/include/grub/elf.h b/include/grub/elf.h
index e6f073bc9..c36d7dab2 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_PCREL57
 
+/* LoongArch relocations */
+#define R_LARCH_NONE 0
+#define R_LARCH_64   2
+#define R_LARCH_MARK_LA  20
+#define R_LARCH_SOP_PUSH_PCREL   22
+#define R_LARCH_SOP_PUSH_ABSOLUTE23
+#define R_LARCH_SOP_PUSH_PLT_PCREL   29
+#define R_LARCH_SOP_SUB  32
+#define R_LARCH_SOP_SL   33
+#define R_LARCH_SOP_SR   34
+#define R_LARCH_SOP_ADD  35
+#define R_LARCH_SOP_AND  36
+#define R_LARCH_SOP_IF_ELSE  37
+#define R_LARCH_SOP_POP_32_S_10_538
+#define R_LARCH_SOP_POP_32_U_10_12   39
+#define R_LARCH_SOP_POP_32_S_10_12   40
+#define R_LARCH_SOP_POP_32_S_10_16   41
+#define R_LARCH_SOP_POP_32_S_10_16_S242
+#define R_LARCH_SOP_POP_32_S_5_2043
+#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
+#define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+
 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.39.2


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


[PATCH v15 04/10] LoongArch: Add early startup code

2023-04-18 Thread Xiaotian Wu
On entry, we need to save the system table pointer as well as our image
handle. Add an early startup file that saves them and then brings us
into our main function.

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

diff --git a/grub-core/kern/loongarch64/efi/startup.S 
b/grub-core/kern/loongarch64/efi/startup.S
new file mode 100644
index 0..fc8123f8c
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/startup.S
@@ -0,0 +1,34 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "startup.S"
+   .text
+
+FUNCTION(_start)
+   /*
+*  EFI_SYSTEM_TABLE and EFI_HANDLE are passed in $a1/$a0.
+*/
+
+   la  $a2, EXT_C(grub_efi_image_handle)
+   st.d$a0, $a2, 0
+   la  $a2, EXT_C(grub_efi_system_table)
+   st.d$a1, $a2, 0
+
+   b   EXT_C(grub_main)
-- 
2.39.2


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


[PATCH v15 00/10] Add LoongArch support

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

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

This patch series adds support for the Loongarch architecture, which can
compile and run the linux-6.2+ kernel on LoongArch.

Please review the patches, thank you.

v14->v15:
- Update test code to pass more test cases.

Xiaotian Wu (10):
  PE: Add LoongArch definitions
  ELF: Add LoongArch definitions
  LoongArch: Add setjmp implementation
  LoongArch: Add early startup code
  LoongArch: Add support for ELF psABI v1.00 relocations
  LoongArch: Add support for ELF psABI v2.00 relocations
  LoongArch: Add auxiliary files
  LoongArch: Add to build system
  tests: Fix timezone inconsistency in squashfs_test
  tests: Add LoongArch to various test cases

 Makefile.util.def|   1 +
 configure.ac |  23 +-
 gentpl.py|  27 +--
 grub-core/Makefile.am|   6 +
 grub-core/Makefile.core.def  |  17 ++
 grub-core/kern/dl.c  |   9 +-
 grub-core/kern/efi/mm.c  |   3 +-
 grub-core/kern/loongarch64/cache.c   |  39 
 grub-core/kern/loongarch64/cache_flush.S |  33 +++
 grub-core/kern/loongarch64/dl.c  | 150 +
 grub-core/kern/loongarch64/dl_helper.c   | 261 +++
 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 +
 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   |  30 +++
 include/grub/loongarch64/efi/memory.h|  24 +++
 include/grub/loongarch64/reloc.h | 113 ++
 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/grub_func_test.in  |   2 +
 tests/ohci_test.in   |   2 +-
 tests/pata_test.in   |   2 +-
 tests/uhci_test.in   |   2 +-
 tests/util/grub-fs-tester.in |   2 +-
 tests/util/grub-shell-luks-tester.in |   3 +
 tests/util/grub-shell.in |  15 ++
 util/grub-install-common.c   |  49 ++---
 util/grub-install.c  |  16 ++
 util/grub-mkimagexx.c| 126 +++
 util/grub-mknetdir.c |   1 +
 util/grub-mkrescue.c |   8 +
 util/grub-module-verifier.c  |  33 +++
 util/mkimage.c   |  16 ++
 43 files changed, 1265 insertions(+), 67 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 include/grub/loongarch64/efi/memory.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.39.2


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


Re: [PATCH v14 10/10] tests: Add LoongArch to various test cases

2023-04-13 Thread Xiaotian Wu
在 2023-04-12星期三的 05:54 -0300,Glenn Washburn写道:
> On 4/11/23 03:02, Xiaotian Wu wrote:
> > 在 2023-04-10星期一的 15:58 +0800,Xiaotian Wu写道:
> > > 在 2023-04-10星期一的 15:36 +0800,Xiaotian Wu写道:
> > > > 在 2023-04-10星期一的 03:20 -0300,Glenn Washburn写道:
> > > > > On 4/8/23 02:05, Xiaotian Wu wrote:
> > > > > > The test results of running "make check" with qemu 7.2 are
> > > > > > as
> > > > > > follows:
> > > > > >    
> > > > > > ===
> > > > > > 
> > > > > > ==
> > > > > > ==
> > > > > > =
> > > > > >     Testsuite summary for GRUB 2.11
> > > > > >    
> > > > > > ===
> > > > > > 
> > > > > > ==
> > > > > > ==
> > > > > > =
> > > > > >     # TOTAL: 85
> > > > > >     # PASS:  57
> > > > > >     # SKIP:  8
> > > > > >     # XFAIL: 0
> > > > > >     # FAIL:  1
> > > > > >     # XPASS: 0
> > > > > >     # ERROR: 19
> > > > > >    
> > > > > > ===
> > > > > > 
> > > > > > ==
> > > > > > ==
> > > > > > =
> > > > > > 
> > > > > > These ERROR cases need to be run as root user:
> > > > > >     ERROR: ext234_test
> > > > > >     ERROR: hfsplus_test
> > > > > >     ERROR: ntfs_test
> > > > > >     ERROR: reiserfs_test
> > > > > >     ERROR: fat_test
> > > > > >     ERROR: minixfs_test
> > > > > >     ERROR: xfs_test
> > > > > >     ERROR: f2fs_test
> > > > > >     ERROR: nilfs2_test
> > > > > >     ERROR: romfs_test
> > > > > >     ERROR: exfat_test
> > > > > >     ERROR: udf_test
> > > > > >     ERROR: hfs_test
> > > > > >     ERROR: jfs_test
> > > > > >     ERROR: btrfs_test
> > > > > >     ERROR: zfs_test
> > > > > >     ERROR: luks1_test
> > > > > >     ERROR: luks2_test
> > > > > >     ERROR: grub_cmd_cryptomount
> > > > > 
> > > > > It would be great if you would follow the instructions in the
> > > > > INSTALL
> > > > > file to install the appropriate dependencies and to run the
> > > > > tests
> > > > > as
> > > > > root. Are you suggesting that you do not have access to the
> > > > > root
> > > > > user?
> > > > I can try to retest with root, but maybe the test case will
> > > > fail.
> > 
> > I re-run the test program with sudo, the results of these 19 test
> > cases
> > are: 10 passed, 5 errors, 4 failed.
> > 
> > The 10 passed use cases are:
> > PASS: ext234_test
> > PASS: ntfs_test
> > PASS: reiserfs_test
> > PASS: xfs_test
> > PASS: nilfs2_test
> > PASS: udf_test
> > PASS: jfs_test
> > PASS: btrfs_test
> > PASS: luks1_test
> > PASS: luks2_test
> > 
> > The 5 error cases are:
> > ERROR: minixfs_test
> > ERROR: f2fs_test
> > ERROR: romfs_test
> > ERROR: hfs_test
> > ERROR: zfs_test
> > 
> > minixfs_test: The kernel is not configured with minix file system
> > support.
> > f2fs_test: The kernel is not configured with f2fs file system
> > support.
> > romfs_test: No package provides "genromfs" on Archlinux.
> > hfs_test:  No package provides "mkfs.hfs" on Archlinux.
> 
> This is one reason why the tests officially only support being run on
> Debian systems. I leave it up to you or whoever maintains LoongArch 
> support to decide if you care about these tests, as these aren't 
> commonly used filesystems.
> 
I think the testing of these filesystems can be ignored for now so that
we can merge the patches first. After LoongArch for debian is ready,
let's test these filesystems again.

> > zfs_test: zfs has not been ported to the LoongArch architecture
> > yet.
> > 
> > The 4 failed cases are:
> > FAIL: hfsplus_test
> > FAIL: fat_test
> > FAIL: exfat_test
> > FAIL: g

Re: [PATCH v14 10/10] tests: Add LoongArch to various test cases

2023-04-10 Thread Xiaotian Wu
在 2023-04-10星期一的 15:58 +0800,Xiaotian Wu写道:
> 在 2023-04-10星期一的 15:36 +0800,Xiaotian Wu写道:
> > 在 2023-04-10星期一的 03:20 -0300,Glenn Washburn写道:
> > > On 4/8/23 02:05, Xiaotian Wu wrote:
> > > > The test results of running "make check" with qemu 7.2 are as
> > > > follows:
> > > >   
> > > > ===
> > > > ==
> > > > ==
> > > > =
> > > >    Testsuite summary for GRUB 2.11
> > > >   
> > > > ===
> > > > ==
> > > > ==
> > > > =
> > > >    # TOTAL: 85
> > > >    # PASS:  57
> > > >    # SKIP:  8
> > > >    # XFAIL: 0
> > > >    # FAIL:  1
> > > >    # XPASS: 0
> > > >    # ERROR: 19
> > > >   
> > > > ===
> > > > ==
> > > > ==
> > > > =
> > > > 
> > > > These ERROR cases need to be run as root user:
> > > >    ERROR: ext234_test
> > > >    ERROR: hfsplus_test
> > > >    ERROR: ntfs_test
> > > >    ERROR: reiserfs_test
> > > >    ERROR: fat_test
> > > >    ERROR: minixfs_test
> > > >    ERROR: xfs_test
> > > >    ERROR: f2fs_test
> > > >    ERROR: nilfs2_test
> > > >    ERROR: romfs_test
> > > >    ERROR: exfat_test
> > > >    ERROR: udf_test
> > > >    ERROR: hfs_test
> > > >    ERROR: jfs_test
> > > >    ERROR: btrfs_test
> > > >    ERROR: zfs_test
> > > >    ERROR: luks1_test
> > > >    ERROR: luks2_test
> > > >    ERROR: grub_cmd_cryptomount
> > > 
> > > It would be great if you would follow the instructions in the
> > > INSTALL
> > > file to install the appropriate dependencies and to run the tests
> > > as 
> > > root. Are you suggesting that you do not have access to the root
> > > user?
> > I can try to retest with root, but maybe the test case will fail.

I re-run the test program with sudo, the results of these 19 test cases
are: 10 passed, 5 errors, 4 failed.

The 10 passed use cases are:
PASS: ext234_test
PASS: ntfs_test
PASS: reiserfs_test
PASS: xfs_test
PASS: nilfs2_test
PASS: udf_test
PASS: jfs_test
PASS: btrfs_test
PASS: luks1_test
PASS: luks2_test

The 5 error cases are:
ERROR: minixfs_test
ERROR: f2fs_test
ERROR: romfs_test
ERROR: hfs_test
ERROR: zfs_test

minixfs_test: The kernel is not configured with minix file system
support.   
f2fs_test: The kernel is not configured with f2fs file system support.
romfs_test: No package provides "genromfs" on Archlinux.  
hfs_test:  No package provides "mkfs.hfs" on Archlinux.   
zfs_test: zfs has not been ported to the LoongArch architecture yet.  

The 4 failed cases are:
FAIL: hfsplus_test
FAIL: fat_test
FAIL: exfat_test
FAIL: grub_cmd_cryptomount

hfsplus_test:
I use hfsprogs-540.1.linux3-4, the log is:
mkfs.hfsplus: invalid option -- 'w'

fat_test:
The log is:
cp: error writing '/tmp/grub-fs-
tester.20230410181941249964440.vfat12a.1g8/vfat12a_rw//american-
english': No space left on device

exfat_test:
The log give me the usage of "mkfs.exfat", it seems the "mkfs.exfat"
command does not support "-s" "-n" arguments.

grub_cmd_cryptomount:
The log is:
Error[1]: WARNING: Image format was not specified for '/tmp/grub-
shell.2fRnxAhNbU/grub.iso' and probing guessed raw.
 Automatically detecting the format is dangerous for raw
images, write operations on block 0 will be restricted.
 Specify the 'raw' format explicitly to remove the
restrictions.
error: test not verified [cryptomount failed: 2]
LUKS1 test cryptsetup defaults: FAIL
FAIL grub_cmd_cryptomount (exit status: 1)



> > 
> > > > 
> > > > These test cases are skipped:
> > > >    SKIP: pata_test
> > > >    SKIP: ahci_test
> > > >    SKIP: uhci_test
> > > >    SKIP: ohci_test
> > > >    SKIP: ehci_test
> > > >    SKIP: fddboot_test
> > > >    SKIP: netboot_test
> > > >    SKIP: pseries_test
> > > > 
> > > > This test case fails because of: qemu-system-loongarch64:
> > > > ram_size
> > > > must be greater than 1G.
> > > >    FAIL: grub_func_test
> > > 
> > > This sounds strange to me because you give QEMU 4G of memory in 
> > > grub-shell (see below). Ar

Re: [PATCH v14 10/10] tests: Add LoongArch to various test cases

2023-04-10 Thread Xiaotian Wu
在 2023-04-10星期一的 15:36 +0800,Xiaotian Wu写道:
> 在 2023-04-10星期一的 03:20 -0300,Glenn Washburn写道:
> > On 4/8/23 02:05, Xiaotian Wu wrote:
> > > The test results of running "make check" with qemu 7.2 are as
> > > follows:
> > >   
> > > =
> > > ==
> > > =
> > >    Testsuite summary for GRUB 2.11
> > >   
> > > =
> > > ==
> > > =
> > >    # TOTAL: 85
> > >    # PASS:  57
> > >    # SKIP:  8
> > >    # XFAIL: 0
> > >    # FAIL:  1
> > >    # XPASS: 0
> > >    # ERROR: 19
> > >   
> > > =
> > > ==
> > > =
> > > 
> > > These ERROR cases need to be run as root user:
> > >    ERROR: ext234_test
> > >    ERROR: hfsplus_test
> > >    ERROR: ntfs_test
> > >    ERROR: reiserfs_test
> > >    ERROR: fat_test
> > >    ERROR: minixfs_test
> > >    ERROR: xfs_test
> > >    ERROR: f2fs_test
> > >    ERROR: nilfs2_test
> > >    ERROR: romfs_test
> > >    ERROR: exfat_test
> > >    ERROR: udf_test
> > >    ERROR: hfs_test
> > >    ERROR: jfs_test
> > >    ERROR: btrfs_test
> > >    ERROR: zfs_test
> > >    ERROR: luks1_test
> > >    ERROR: luks2_test
> > >    ERROR: grub_cmd_cryptomount
> > 
> > It would be great if you would follow the instructions in the
> > INSTALL
> > file to install the appropriate dependencies and to run the tests
> > as 
> > root. Are you suggesting that you do not have access to the root
> > user?
> I can try to retest with root, but maybe the test case will fail.
> 
> > > 
> > > These test cases are skipped:
> > >    SKIP: pata_test
> > >    SKIP: ahci_test
> > >    SKIP: uhci_test
> > >    SKIP: ohci_test
> > >    SKIP: ehci_test
> > >    SKIP: fddboot_test
> > >    SKIP: netboot_test
> > >    SKIP: pseries_test
> > > 
> > > This test case fails because of: qemu-system-loongarch64:
> > > ram_size
> > > must be greater than 1G.
> > >    FAIL: grub_func_test
> > 
> > This sounds strange to me because you give QEMU 4G of memory in 
> > grub-shell (see below). Are you saying that you ran the tests on a 
> > system with 1G or less of memory?
> > 
> Yes, I set the memory to 4G, but on line 16 of the
> "tests/grub_func_test.in" file, it is reset to 512M.
After modifying "test/grub_func_test.in", I made a test, but it failed,
the log is below, I'm not sure if it's related to grub, please check,
thanks.

https://gist.github.com/yetist/7abfb78b84f06dc198148ce6e344d7bb

> 
> > > 
> > > Signed-off-by: Xiaotian Wu 
> > > ---
> > >   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 |
> 

Re: [PATCH v14 10/10] tests: Add LoongArch to various test cases

2023-04-10 Thread Xiaotian Wu
在 2023-04-10星期一的 03:20 -0300,Glenn Washburn写道:
> On 4/8/23 02:05, Xiaotian Wu wrote:
> > The test results of running "make check" with qemu 7.2 are as
> > follows:
> >   
> > ===
> > =
> >    Testsuite summary for GRUB 2.11
> >   
> > ===
> > =
> >    # TOTAL: 85
> >    # PASS:  57
> >    # SKIP:  8
> >    # XFAIL: 0
> >    # FAIL:  1
> >    # XPASS: 0
> >    # ERROR: 19
> >   
> > ===
> > =
> > 
> > These ERROR cases need to be run as root user:
> >    ERROR: ext234_test
> >    ERROR: hfsplus_test
> >    ERROR: ntfs_test
> >    ERROR: reiserfs_test
> >    ERROR: fat_test
> >    ERROR: minixfs_test
> >    ERROR: xfs_test
> >    ERROR: f2fs_test
> >    ERROR: nilfs2_test
> >    ERROR: romfs_test
> >    ERROR: exfat_test
> >    ERROR: udf_test
> >    ERROR: hfs_test
> >    ERROR: jfs_test
> >    ERROR: btrfs_test
> >    ERROR: zfs_test
> >    ERROR: luks1_test
> >    ERROR: luks2_test
> >    ERROR: grub_cmd_cryptomount
> 
> It would be great if you would follow the instructions in the INSTALL
> file to install the appropriate dependencies and to run the tests as 
> root. Are you suggesting that you do not have access to the root
> user?
I can try to retest with root, but maybe the test case will fail.

> > 
> > These test cases are skipped:
> >    SKIP: pata_test
> >    SKIP: ahci_test
> >    SKIP: uhci_test
> >    SKIP: ohci_test
> >    SKIP: ehci_test
> >    SKIP: fddboot_test
> >    SKIP: netboot_test
> >    SKIP: pseries_test
> > 
> > This test case fails because of: qemu-system-loongarch64: ram_size
> > must be greater than 1G.
> >    FAIL: grub_func_test
> 
> This sounds strange to me because you give QEMU 4G of memory in 
> grub-shell (see below). Are you saying that you ran the tests on a 
> system with 1G or less of memory?
> 
Yes, I set the memory to 4G, but on line 16 of the
"tests/grub_func_test.in" file, it is reset to 512M.

> > 
> > Signed-off-by: Xiaotian Wu 
> > ---
> >   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
> > 

[PATCH v14 10/10] tests: Add LoongArch to various test cases

2023-04-07 Thread Xiaotian Wu
The test results of running "make check" with qemu 7.2 are as follows:
  
  Testsuite summary for GRUB 2.11
  
  # TOTAL: 85
  # PASS:  57
  # SKIP:  8
  # XFAIL: 0
  # FAIL:  1
  # XPASS: 0
  # ERROR: 19
  

These ERROR cases need to be run as root user:
  ERROR: ext234_test
  ERROR: hfsplus_test
  ERROR: ntfs_test
  ERROR: reiserfs_test
  ERROR: fat_test
  ERROR: minixfs_test
  ERROR: xfs_test
  ERROR: f2fs_test
  ERROR: nilfs2_test
  ERROR: romfs_test
  ERROR: exfat_test
  ERROR: udf_test
  ERROR: hfs_test
  ERROR: jfs_test
  ERROR: btrfs_test
  ERROR: zfs_test
  ERROR: luks1_test
  ERROR: luks2_test
  ERROR: grub_cmd_cryptomount

These test cases are skipped:
  SKIP: pata_test
  SKIP: ahci_test
  SKIP: uhci_test
  SKIP: ohci_test
  SKIP: ehci_test
  SKIP: fddboot_test
  SKIP: netboot_test
  SKIP: pseries_test

This test case fails because of: qemu-system-loongarch64: ram_size must be 
greater than 1G.
  FAIL: grub_func_test

Signed-off-by: Xiaotian Wu 
---
 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 75f71dc1a..bbeb63ef4 100644
--- a/tests/util/grub-shell.in
+++ b/tests/util/grub-shell.in
@@ -208,6 +208,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-loongarch-cpu -smp 4 -nographic -m 
4G \
+ -bios /usr/share/edk2/loongarch64/QEMU_CODE.fd $qemuopts"
+   disk="device virtio-blk-pci,drive=hd1 -drive if=none,id=hd1,file="
+   serial_port=
+   ;;
 *)
boot=hd
qemu=qemu-system-i386
@@ -423,6 +433,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 ]; the

[PATCH v14 09/10] tests: Fix timezone inconsistency in squashfs_test

2023-04-07 Thread Xiaotian Wu
The image timestamp was not returned in UTC, but the following logic
expected and used UTC.

This patch fixes the test failure like described below:

  unsquashfs -s 
/tmp/grub-fs-tester.20230407111703613257436.squash4_gzip.9R4/squash4_gzip_512_4096_1_0.img
  grep '^Creation'
  awk '{print $6 " " $7 " " $8 " " $9 " " $10; }'
  FSTIME='Fri Apr 7 11:17:05 2023'
  date -d 'Fri Apr 7 11:17:05 2023' -u '+%Y-%m-%d %H:%M:%S'
  FSTIME='2023-04-07 11:17:05'
  date -d '2023-04-07 11:17:05 UTC -1 second' -u '+%Y-%m-%d %H:%M:%S'
  FSTIMEM1='2023-04-07 11:17:04'
  date -d '2023-04-07 11:17:05 UTC -2 second' -u '+%Y-%m-%d %H:%M:%S'
  FSTIMEM2='2023-04-07 11:17:03'
  date -d '2023-04-07 11:17:05 UTC -3 second' -u '+%Y-%m-%d %H:%M:%S'
  FSTIMEM3='2023-04-07 11:17:02'
  grep -F 'Last modification time 2023-04-07 11:17:05'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  grep -F 'Last modification time 2023-04-07 11:17:04'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  grep -F 'Last modification time 2023-04-07 11:17:03'
  echo 'Device loop0: Filesystem type squash4 - Last modification time 
2023-04-07 03:17:05 Friday - Sector size 512B - Total size 10680KiB'
  grep -F 'Last modification time 2023-04-07 11:17:02'
  echo FSTIME FAIL

Signed-off-by: Xiaotian Wu 
---
 tests/util/grub-fs-tester.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/util/grub-fs-tester.in b/tests/util/grub-fs-tester.in
index 064464eb2..64638d2e9 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -1454,7 +1454,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" 
"$MAXLOGSECSIZE" 1); do
# Creating the squash image may take more than a few
# seconds. Use the more accurate timestamp from the
# superblock.
-   FSTIME="$(unsquashfs -s "${FSIMAGEP}0.img" | grep 
^Creation | awk '{print $6 " " $7 " " $8 " " $9 " " $10; }')"
+   FSTIME="$(unsquashfs -UTC -s "${FSIMAGEP}0.img" | grep 
^Creation | awk '{print $6 " " $7 " " $8 " " $9 " " $10; }')"
FSTIME="$(date -d "$FSTIME" -u '+%Y-%m-%d %H:%M:%S')";;
*)
FSTIME="$(TZ=UTC ls --time-style="+%Y-%m-%d_%H:%M:%S" 
-l -d "${FSIMAGEP}0.img"|awk '{print $6; }'|sed 's,_, ,g')";;
-- 
2.39.2


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


[PATCH v14 08/10] LoongArch: Add to build system

2023-04-07 Thread Xiaotian Wu
This patch adds LoongArch to the grub build system and various tools,
so grub can be built on LoongArch as a UEFI application.

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 Makefile.util.def   |  1 +
 configure.ac|  8 +-
 gentpl.py   | 27 ++--
 grub-core/Makefile.am   |  6 +
 grub-core/Makefile.core.def | 17 +
 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, 113 insertions(+), 39 deletions(-)

diff --git a/Makefile.util.def b/Makefile.util.def
index beaef1168..fdbd58c8f 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 1ded95ca3..d9f088d12 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) ;;
@@ -1236,7 +1239,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"
@@ -2021,6 +2025,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 88abe5b0a..d0470b3b2 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" 

[PATCH v14 07/10] LoongArch: Add auxiliary files

2023-04-07 Thread Xiaotian Wu
Add support for manipulating architectural cache and timers, and EFI
memory maps.

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 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 3705b8b1b..ac13e95e9 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 0..43d314df9
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache.c
@@ -0,0 +1,39 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+
+/* Prototypes for asm functions. */
+void grub_arch_clean_dcache_range (void);
+void grub_arch_invalidate_icache_range (void);
+
+void
+grub_arch_sync_caches (void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  grub_arch_clean_dcache_range ();
+  grub_arch_invalidate_icache_range ();
+}
+
+void
+grub_arch_sync_dma_caches (volatile void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  /* DMA non-coherent devices not supported yet */
+}
diff --git a/grub-core/kern/loongarch64/cache_flush.S 
b/grub-core/kern/loongarch64/cache_flush.S
new file mode 100644
index 0..43b97d822
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache_flush.S
@@ -0,0 +1,33 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "cache_flush.S"
+   .text
+/*
+ * No further work to do because cache consistency is maintained by hardware on
+ * LoongArch.
+ */
+FUNCTION(grub_arch_clean_dcache_range)
+   dbar 0
+   jr $ra
+
+FUNCTION(grub_arch_invalidate_icache_range)
+   ibar 0
+   jr $ra
diff --git a/grub-core/kern/loongarch64/efi/init.c 
b/grub-core/kern/loongarch64/efi/init.c
new file mode 100644
index 0..8cbeafaba
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/init.c
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope 

[PATCH v14 06/10] LoongArch: Add support for ELF psABI v2.00 relocations

2023-04-07 Thread Xiaotian Wu
A new set of relocation types was added in the LoongArch ELF psABI v2.00
spec [1][2] to replace the stack-based scheme in v1.00. Toolchain
support is available from binutils 2.40 and gcc 13 onwards.

This patch adds support for the new relocation types, that are simpler
to handle (in particular, stack operations are gone). Support for the
v1.00 relocs are kept for now, for compatibility with older toolchains.

[1]: https://github.com/loongson/LoongArch-Documentation/pull/57
[2]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_appendix_revision_history

Signed-off-by: Xiaotian Wu 
---
 configure.ac   | 15 +++
 grub-core/kern/loongarch64/dl.c| 54 +--
 grub-core/kern/loongarch64/dl_helper.c | 59 ++
 include/grub/elf.h |  7 +++
 include/grub/loongarch64/reloc.h   |  6 +++
 util/grub-mkimagexx.c  | 49 -
 util/grub-module-verifier.c|  7 +++
 7 files changed, 193 insertions(+), 4 deletions(-)

diff --git a/configure.ac b/configure.ac
index ca42ff8f7..1ded95ca3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -860,6 +860,21 @@ 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 _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_mno_explicit_relocs=yes],
+   [grub_cv_cc_mno_explicit_relocs=no])
+  ])
+  if test "x$grub_cv_cc_mno_explicit_relocs" = xyes; then
+TARGET_CFLAGS="$TARGET_CFLAGS -mno-explicit-relocs -fno-plt"
+TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-explicit-relocs -fno-plt"
+  fi
+  TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
+  TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
+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)
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index 3a6aa91cd..47196a219 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -58,7 +58,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 +72,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 +92,47 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
case R_LARCH_SOP_PUSH_PLT_PCREL:
  grub_loongarch64_sop_push (, 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_ABS_HI20:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx_hi20 (abs_place, sym_addr);
+ }
+ break;
+   case R_LARCH_ABS64_LO20:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx64_lo20 (abs_place, sym_addr);
+ }
+ break;
+   case R_LARCH_ABS64_HI12:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx64_hi12 (abs_place, sym_addr);
+ }
+ 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_ABS_LO12:
+   case R_LARCH_PCALA_LO12:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarc

[PATCH v14 05/10] LoongArch: Add support for ELF psABI v1.00 relocations

2023-04-07 Thread Xiaotian Wu
This patch adds support of the stack-based LoongArch relocations
throughout grub, including tools, dynamic linkage, and support for
conversion of ELF relocations into PE ones. A stack machine is required
to handle these per the spec [1] (see the R_LARCH_SOP types), of which
a simple implementation is included.

These relocations are produced by binutils 2.38 and 2.39, while the
newer v2.00 relocs require more recent toolchain (binutils 2.40+ & gcc
13+, or LLVM 16+). GCC 13 has not been officially released as of early
2023, so support for v1.00 relocs are expected to stay relevant for a
while.

[1]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_relocations

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 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, , );
   if (err)
 return err;
@@ -304,7 +306,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
  mod->segment = seg;
}
 }
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   mod->trampptr = ptr;
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
new file mode 100644
index 0..3a6aa91cd
--- /dev/null
+++ b/grub-core/kern/loongarch64/dl.c
@@ -0,0 +1,102 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Check if EHDR is a valid ELF header.  */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+  Elf_Ehdr *e = ehdr;
+
+  /* Check the magic numbers.  */
+  if (e->e_ident[EI_CLASS] != ELFCLASS64
+  || e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_LOONGARCH)
+return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF 
magic"));
+
+  return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/*
+ * Unified function for both REL and RELA.
+ */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+  Elf_Shdr *s, grub_dl_segment_t seg)
+{
+  Elf_Rel *rel, *max;
+  struct grub_loongarch64_stack stack;
+  grub_loongarch64_stack_init ();
+
+  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)
+   r

[PATCH v14 03/10] LoongArch: Add setjmp implementation

2023-04-07 Thread Xiaotian Wu
This patch adds a setjmp implementation for LoongArch.

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

diff --git a/grub-core/lib/loongarch64/setjmp.S 
b/grub-core/lib/loongarch64/setjmp.S
new file mode 100644
index 0..41d58f569
--- /dev/null
+++ b/grub-core/lib/loongarch64/setjmp.S
@@ -0,0 +1,69 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "setjmp.S"
+
+GRUB_MOD_LICENSE "GPLv3+"
+
+   .text
+
+/*
+ * int grub_setjmp (jmp_buf env)
+ */
+FUNCTION(grub_setjmp)
+   st.d $s0, $a0, 0x0
+   st.d $s1, $a0, 0x8
+   st.d $s2, $a0, 0x10
+   st.d $s3, $a0, 0x18
+   st.d $s4, $a0, 0x20
+   st.d $s5, $a0, 0x28
+   st.d $s6, $a0, 0x30
+   st.d $s7, $a0, 0x38
+   st.d $s8, $a0, 0x40
+   st.d $fp, $a0, 0x48
+   st.d $sp, $a0, 0x50
+   st.d $ra, $a0, 0x58
+
+   move $a0, $zero
+   jr   $ra
+
+/*
+ * void grub_longjmp (jmp_buf env, int val)
+ */
+FUNCTION(grub_longjmp)
+   ld.d $s0, $a0, 0x0
+   ld.d $s1, $a0, 0x8
+   ld.d $s2, $a0, 0x10
+   ld.d $s3, $a0, 0x18
+   ld.d $s4, $a0, 0x20
+   ld.d $s5, $a0, 0x28
+   ld.d $s6, $a0, 0x30
+   ld.d $s7, $a0, 0x38
+   ld.d $s8, $a0, 0x40
+   ld.d $fp, $a0, 0x48
+   ld.d $sp, $a0, 0x50
+   ld.d $ra, $a0, 0x58
+
+   /* Return 1 if passed 0, otherwise returns the value in place. */
+   li.w $a0, 1
+   beqz $a1, 1f
+   move $a0, $a1
+1:
+   jr   $ra
diff --git a/grub-core/lib/setjmp.S b/grub-core/lib/setjmp.S
index 9c8721088..ffb26df79 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(__loongarch_lp64)
+#include "./loongarch64/setjmp.S"
 #elif defined(__riscv)
 #include "./riscv/setjmp.S"
 #else
diff --git a/include/grub/loongarch64/setjmp.h 
b/include/grub/loongarch64/setjmp.h
new file mode 100644
index 0..cb3e17763
--- /dev/null
+++ b/include/grub/loongarch64/setjmp.h
@@ -0,0 +1,27 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <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.39.2


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


[PATCH v14 02/10] ELF: Add LoongArch definitions

2023-04-07 Thread Xiaotian Wu
Add ELF e_machine ID [1] and relocations types [2] for LoongArch to
the current in-repo definitions.

[1]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_e_machine_identifies_the_machine
[2]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_relocations

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

diff --git a/include/grub/elf.h b/include/grub/elf.h
index e6f073bc9..c36d7dab2 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_PCREL57
 
+/* LoongArch relocations */
+#define R_LARCH_NONE 0
+#define R_LARCH_64   2
+#define R_LARCH_MARK_LA  20
+#define R_LARCH_SOP_PUSH_PCREL   22
+#define R_LARCH_SOP_PUSH_ABSOLUTE23
+#define R_LARCH_SOP_PUSH_PLT_PCREL   29
+#define R_LARCH_SOP_SUB  32
+#define R_LARCH_SOP_SL   33
+#define R_LARCH_SOP_SR   34
+#define R_LARCH_SOP_ADD  35
+#define R_LARCH_SOP_AND  36
+#define R_LARCH_SOP_IF_ELSE  37
+#define R_LARCH_SOP_POP_32_S_10_538
+#define R_LARCH_SOP_POP_32_U_10_12   39
+#define R_LARCH_SOP_POP_32_S_10_12   40
+#define R_LARCH_SOP_POP_32_S_10_16   41
+#define R_LARCH_SOP_POP_32_S_10_16_S242
+#define R_LARCH_SOP_POP_32_S_5_2043
+#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
+#define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+
 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.39.2


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


[PATCH v14 04/10] LoongArch: Add early startup code

2023-04-07 Thread Xiaotian Wu
On entry, we need to save the system table pointer as well as our image
handle. Add an early startup file that saves them and then brings us
into our main function.

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

diff --git a/grub-core/kern/loongarch64/efi/startup.S 
b/grub-core/kern/loongarch64/efi/startup.S
new file mode 100644
index 0..fc8123f8c
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/startup.S
@@ -0,0 +1,34 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "startup.S"
+   .text
+
+FUNCTION(_start)
+   /*
+*  EFI_SYSTEM_TABLE and EFI_HANDLE are passed in $a1/$a0.
+*/
+
+   la  $a2, EXT_C(grub_efi_image_handle)
+   st.d$a0, $a2, 0
+   la  $a2, EXT_C(grub_efi_system_table)
+   st.d$a1, $a2, 0
+
+   b   EXT_C(grub_main)
-- 
2.39.2


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


[PATCH v14 00/10] Add LoongArch support

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

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

This patch series adds support for the Loongarch architecture, which can
compile and run the linux-6.2+ kernel on LoongArch.

Please review the patches, thank you.

Xiaotian Wu (10):
  PE: Add LoongArch definitions
  ELF: Add LoongArch definitions
  LoongArch: Add setjmp implementation
  LoongArch: Add early startup code
  LoongArch: Add support for ELF psABI v1.00 relocations
  LoongArch: Add support for ELF psABI v2.00 relocations
  LoongArch: Add auxiliary files
  LoongArch: Add to build system
  tests: Fix timezone inconsistency in squashfs_test
  tests: Add LoongArch to various test cases

 Makefile.util.def|   1 +
 configure.ac |  23 +-
 gentpl.py|  27 +--
 grub-core/Makefile.am|   6 +
 grub-core/Makefile.core.def  |  17 ++
 grub-core/kern/dl.c  |   9 +-
 grub-core/kern/efi/mm.c  |   3 +-
 grub-core/kern/loongarch64/cache.c   |  39 
 grub-core/kern/loongarch64/cache_flush.S |  33 +++
 grub-core/kern/loongarch64/dl.c  | 150 +
 grub-core/kern/loongarch64/dl_helper.c   | 261 +++
 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 +
 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   |  30 +++
 include/grub/loongarch64/efi/memory.h|  24 +++
 include/grub/loongarch64/reloc.h | 113 ++
 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-fs-tester.in |   2 +-
 tests/util/grub-shell.in |  14 ++
 util/grub-install-common.c   |  49 ++---
 util/grub-install.c  |  16 ++
 util/grub-mkimagexx.c| 126 +++
 util/grub-mknetdir.c |   1 +
 util/grub-mkrescue.c |   8 +
 util/grub-module-verifier.c  |  33 +++
 util/mkimage.c   |  16 ++
 41 files changed, 1259 insertions(+), 67 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 include/grub/loongarch64/efi/memory.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.39.2


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


[PATCH v14 01/10] PE: Add LoongArch definitions

2023-04-07 Thread Xiaotian Wu
Add PE machine types [1] and relocation types [2] for LoongArch to
the current in-repo definitions.

[1]: 
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types
[2]: 
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#base-relocation-types

Signed-off-by: Zhou Yang 
Signed-off-by: Xiaotian Wu 
---
 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 98c4ff177..101859af1 100644
--- a/include/grub/efi/pe32.h
+++ b/include/grub/efi/pe32.h
@@ -88,6 +88,8 @@ struct grub_pe32_coff_header
 #define GRUB_PE32_MACHINE_X86_64   0x8664
 #define GRUB_PE32_MACHINE_ARMTHUMB_MIXED   0x01c2
 #define GRUB_PE32_MACHINE_ARM640xAA64
+#define GRUB_PE32_MACHINE_LOONGARCH32  0x6232
+#define GRUB_PE32_MACHINE_LOONGARCH64  0x6264
 #define GRUB_PE32_MACHINE_RISCV32  0x5032
 #define GRUB_PE32_MACHINE_RISCV64  0x5064
 
@@ -291,22 +293,24 @@ struct grub_pe32_fixup_block
 
 #define GRUB_PE32_FIXUP_ENTRY(type, offset)(((type) << 12) | (offset))
 
-#define GRUB_PE32_REL_BASED_ABSOLUTE   0
-#define GRUB_PE32_REL_BASED_HIGH   1
-#define GRUB_PE32_REL_BASED_LOW2
-#define GRUB_PE32_REL_BASED_HIGHLOW3
-#define GRUB_PE32_REL_BASED_HIGHADJ4
-#define GRUB_PE32_REL_BASED_MIPS_JMPADDR 5
-#define GRUB_PE32_REL_BASED_ARM_MOV32A  5
-#define GRUB_PE32_REL_BASED_RISCV_HI20 5
-#define GRUB_PE32_REL_BASED_SECTION6
-#define GRUB_PE32_REL_BASED_REL7
-#define GRUB_PE32_REL_BASED_ARM_MOV32T  7
-#define GRUB_PE32_REL_BASED_RISCV_LOW12I 7
-#define GRUB_PE32_REL_BASED_RISCV_LOW12S 8
-#define GRUB_PE32_REL_BASED_IA64_IMM64 9
-#define GRUB_PE32_REL_BASED_DIR64  10
-#define GRUB_PE32_REL_BASED_HIGH3ADJ   11
+#define GRUB_PE32_REL_BASED_ABSOLUTE   0
+#define GRUB_PE32_REL_BASED_HIGH   1
+#define GRUB_PE32_REL_BASED_LOW2
+#define GRUB_PE32_REL_BASED_HIGHLOW3
+#define GRUB_PE32_REL_BASED_HIGHADJ4
+#define GRUB_PE32_REL_BASED_MIPS_JMPADDR   5
+#define GRUB_PE32_REL_BASED_ARM_MOV32A 5
+#define GRUB_PE32_REL_BASED_RISCV_HI20 5
+#define GRUB_PE32_REL_BASED_SECTION6
+#define GRUB_PE32_REL_BASED_REL7
+#define GRUB_PE32_REL_BASED_ARM_MOV32T 7
+#define GRUB_PE32_REL_BASED_RISCV_LOW12I   7
+#define GRUB_PE32_REL_BASED_RISCV_LOW12S   8
+#define GRUB_PE32_REL_BASED_LOONGARCH32_MARK_LA8
+#define GRUB_PE32_REL_BASED_LOONGARCH64_MARK_LA8
+#define GRUB_PE32_REL_BASED_IA64_IMM64 9
+#define GRUB_PE32_REL_BASED_DIR64  10
+#define GRUB_PE32_REL_BASED_HIGH3ADJ   11
 
 struct grub_pe32_symbol
 {
-- 
2.39.2


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


Re: [PATCH v13 7/9] LoongArch: Add to build system

2023-04-06 Thread Xiaotian Wu
I used "git send-email" to send patches #0 to #9, but after #6 patch
was sent, the mail server told me: "too many commands", so I sent it
again, the error remained.

In the end, I sent #7 #8 #9 patches with "git send-email", so they are
outside the thread.

So, what should I do now? Try resending or just reply to the current
email?

在 2023-04-05星期三的 19:03 +0200,Daniel Kiper写道:
> On Fri, Mar 31, 2023 at 02:17:37PM +0800, Xiaotian Wu wrote:
> > Signed-off-by: Xiaotian Wu 
> > Signed-off-by: Zhou Yang 
> 
> Something is off with the patch #7, #8, #9. They are out of the
> thread.
> Are you sure you are using "git send-email" properly to send the
> patches?
> 
> Daniel
> 
> ___
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel

-- 
Best Regards
Xiaotian Wu


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


Re: [PATCH v12 00/10] Add support for LoongArch

2023-03-31 Thread Xiaotian Wu
Thanks for the reminder, I've sent the v13 patch to the mailing list.
But sorry, there may be a problem with our company's mail server, so
the v13 patch was sent twice.

在 2023-03-30星期四的 20:05 +0200,Daniel Kiper写道:
> Hey,
> 
> On Mon, Dec 05, 2022 at 06:46:28PM +0800, Xiaotian Wu wrote:
> > This patchset adds support for the LoongArch instruction set.
> > 
> > Please review the patches and let me know if changes are needed.
> 
> I have merged Atish's EFI loader unification patches yesterday. Could
> you
> rebase your patch set on top of that and resend it to the grub-devel?
> 
> Daniel
> 
> ___
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel

-- 
Best Regards
Xiaotian Wu


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


[PATCH v13 7/9] LoongArch: Add to build system

2023-03-31 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 Makefile.util.def   |  1 +
 configure.ac| 22 -
 gentpl.py   | 27 ++--
 grub-core/Makefile.am   |  6 +
 grub-core/Makefile.core.def | 17 +
 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, 127 insertions(+), 39 deletions(-)

diff --git a/Makefile.util.def b/Makefile.util.def
index beaef1168..fdbd58c8f 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 ca42ff8f7..c9bdfcccb 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) ;;
@@ -860,6 +863,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)
@@ -1221,7 +1238,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"
@@ -2006,6 +2024,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 88abe5b0a..d0470b3b2 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&qu

[PATCH v13 9/9] tests: add support for LoongArch

2023-03-31 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
---
 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 75f71dc1a..bbeb63ef4 100644
--- a/tests/util/grub-shell.in
+++ b/tests/util/grub-shell.in
@@ -208,6 +208,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-loongarch-cpu -smp 4 -nographic -m 
4G \
+ -bios /usr/share/edk2/loongarch64/QEMU_CODE.fd $qemuopts"
+   disk="device virtio-blk-pci,drive=hd1 -drive if=none,id=hd1,file="
+   serial_port=
+   ;;
 *)
boot=hd
qemu=qemu-system-i386
@@ -423,6 +433,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
@@ -433,6 +445,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.39.2


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


[PATCH v13 8/9] LoongArch: Support new relocation types in v2.00 ABI

2023-03-31 Thread Xiaotian Wu
Link: https://github.com/loongson/LoongArch-Documentation/pull/57
Signed-off-by: Xiaotian Wu 
---
 configure.ac   | 17 
 grub-core/kern/loongarch64/dl.c| 54 +--
 grub-core/kern/loongarch64/dl_helper.c | 59 ++
 include/grub/elf.h |  7 +++
 include/grub/loongarch64/reloc.h   |  6 +++
 util/grub-mkimagexx.c  | 49 -
 util/grub-module-verifier.c|  7 +++
 7 files changed, 187 insertions(+), 12 deletions(-)

diff --git a/configure.ac b/configure.ac
index c9bdfcccb..d9f088d12 100644
--- a/configure.ac
+++ b/configure.ac
@@ -864,17 +864,18 @@ 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
-TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
-TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
+  if test "x$grub_cv_cc_mno_explicit_relocs" = xyes; then
+TARGET_CFLAGS="$TARGET_CFLAGS -mno-explicit-relocs -fno-plt"
+TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-explicit-relocs -fno-plt"
   fi
+  TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
+  TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
 fi
 
 # GRUB doesn't use float or doubles at all. Yet some toolchains may decide
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index 3a6aa91cd..47196a219 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -58,7 +58,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 +72,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 +92,47 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
case R_LARCH_SOP_PUSH_PLT_PCREL:
  grub_loongarch64_sop_push (, 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_ABS_HI20:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx_hi20 (abs_place, sym_addr);
+ }
+ break;
+   case R_LARCH_ABS64_LO20:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx64_lo20 (abs_place, sym_addr);
+ }
+ break;
+   case R_LARCH_ABS64_HI12:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx64_hi12 (abs_place, sym_addr);
+ }
+ 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_ABS_LO12:
+   case R_LARCH_PCALA_LO12:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx_lo12 (abs_place, sym_addr);
+ }
+ break;
GRUB_LOONGARCH64_RELOCATION (, place, sym_addr)
default:
  {
diff --git a/grub-core/kern/loongarch64/d

[PATCH v13 3/9] LoongArch: Add setjmp implementation

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

diff --git a/grub-core/lib/loongarch64/setjmp.S 
b/grub-core/lib/loongarch64/setjmp.S
new file mode 100644
index 0..41d58f569
--- /dev/null
+++ b/grub-core/lib/loongarch64/setjmp.S
@@ -0,0 +1,69 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "setjmp.S"
+
+GRUB_MOD_LICENSE "GPLv3+"
+
+   .text
+
+/*
+ * int grub_setjmp (jmp_buf env)
+ */
+FUNCTION(grub_setjmp)
+   st.d $s0, $a0, 0x0
+   st.d $s1, $a0, 0x8
+   st.d $s2, $a0, 0x10
+   st.d $s3, $a0, 0x18
+   st.d $s4, $a0, 0x20
+   st.d $s5, $a0, 0x28
+   st.d $s6, $a0, 0x30
+   st.d $s7, $a0, 0x38
+   st.d $s8, $a0, 0x40
+   st.d $fp, $a0, 0x48
+   st.d $sp, $a0, 0x50
+   st.d $ra, $a0, 0x58
+
+   move $a0, $zero
+   jr   $ra
+
+/*
+ * void grub_longjmp (jmp_buf env, int val)
+ */
+FUNCTION(grub_longjmp)
+   ld.d $s0, $a0, 0x0
+   ld.d $s1, $a0, 0x8
+   ld.d $s2, $a0, 0x10
+   ld.d $s3, $a0, 0x18
+   ld.d $s4, $a0, 0x20
+   ld.d $s5, $a0, 0x28
+   ld.d $s6, $a0, 0x30
+   ld.d $s7, $a0, 0x38
+   ld.d $s8, $a0, 0x40
+   ld.d $fp, $a0, 0x48
+   ld.d $sp, $a0, 0x50
+   ld.d $ra, $a0, 0x58
+
+   /* Return 1 if passed 0, otherwise returns the value in place. */
+   li.w $a0, 1
+   beqz $a1, 1f
+   move $a0, $a1
+1:
+   jr   $ra
diff --git a/grub-core/lib/setjmp.S b/grub-core/lib/setjmp.S
index 9c8721088..ffb26df79 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(__loongarch_lp64)
+#include "./loongarch64/setjmp.S"
 #elif defined(__riscv)
 #include "./riscv/setjmp.S"
 #else
diff --git a/include/grub/loongarch64/setjmp.h 
b/include/grub/loongarch64/setjmp.h
new file mode 100644
index 0..cb3e17763
--- /dev/null
+++ b/include/grub/loongarch64/setjmp.h
@@ -0,0 +1,27 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <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.39.2


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


[PATCH v13 2/9] Add LoongArch definitions

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

diff --git a/include/grub/elf.h b/include/grub/elf.h
index e6f073bc9..c36d7dab2 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_PCREL57
 
+/* LoongArch relocations */
+#define R_LARCH_NONE 0
+#define R_LARCH_64   2
+#define R_LARCH_MARK_LA  20
+#define R_LARCH_SOP_PUSH_PCREL   22
+#define R_LARCH_SOP_PUSH_ABSOLUTE23
+#define R_LARCH_SOP_PUSH_PLT_PCREL   29
+#define R_LARCH_SOP_SUB  32
+#define R_LARCH_SOP_SL   33
+#define R_LARCH_SOP_SR   34
+#define R_LARCH_SOP_ADD  35
+#define R_LARCH_SOP_AND  36
+#define R_LARCH_SOP_IF_ELSE  37
+#define R_LARCH_SOP_POP_32_S_10_538
+#define R_LARCH_SOP_POP_32_U_10_12   39
+#define R_LARCH_SOP_POP_32_S_10_12   40
+#define R_LARCH_SOP_POP_32_S_10_16   41
+#define R_LARCH_SOP_POP_32_S_10_16_S242
+#define R_LARCH_SOP_POP_32_S_5_2043
+#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
+#define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+
 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.39.2


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


[PATCH v13 0/9] Add LoongArch support

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

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

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

Please review the patches, thank you.

Xiaotian Wu (9):
  PE: Add LoongArch definitions
  Add LoongArch definitions
  LoongArch: Add setjmp implementation
  LoongArch: Add early startup code
  LoongArch: Add awareness for LoongArch relocations
  LoongArch: Add auxiliary files
  LoongArch: Add to build system
  LoongArch: Support new relocation types in v2.00 ABI
  tests: add support for LoongArch

 Makefile.util.def|   1 +
 configure.ac |  23 +-
 gentpl.py|  27 +--
 grub-core/Makefile.am|   6 +
 grub-core/Makefile.core.def  |  17 ++
 grub-core/kern/dl.c  |   9 +-
 grub-core/kern/efi/mm.c  |   3 +-
 grub-core/kern/loongarch64/cache.c   |  39 
 grub-core/kern/loongarch64/cache_flush.S |  33 +++
 grub-core/kern/loongarch64/dl.c  | 150 +
 grub-core/kern/loongarch64/dl_helper.c   | 261 +++
 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 +
 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   |  30 +++
 include/grub/loongarch64/efi/memory.h|  24 +++
 include/grub/loongarch64/reloc.h | 113 ++
 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| 126 +++
 util/grub-mknetdir.c |   1 +
 util/grub-mkrescue.c |   8 +
 util/grub-module-verifier.c  |  33 +++
 util/mkimage.c   |  16 ++
 40 files changed, 1258 insertions(+), 66 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 include/grub/loongarch64/efi/memory.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.39.2


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


[PATCH v13 6/9] LoongArch: Add auxiliary files

2023-03-31 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/efi/mm.c  |  3 +-
 grub-core/kern/loongarch64/cache.c   | 39 
 grub-core/kern/loongarch64/cache_flush.S | 33 ++
 grub-core/kern/loongarch64/efi/init.c| 77 
 grub-core/lib/efi/halt.c |  2 +-
 include/grub/efi/efi.h   |  2 +-
 include/grub/loongarch64/efi/memory.h| 24 
 include/grub/loongarch64/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 3705b8b1b..ac13e95e9 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 0..43d314df9
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache.c
@@ -0,0 +1,39 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+
+/* Prototypes for asm functions. */
+void grub_arch_clean_dcache_range (void);
+void grub_arch_invalidate_icache_range (void);
+
+void
+grub_arch_sync_caches (void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  grub_arch_clean_dcache_range ();
+  grub_arch_invalidate_icache_range ();
+}
+
+void
+grub_arch_sync_dma_caches (volatile void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  /* DMA non-coherent devices not supported yet */
+}
diff --git a/grub-core/kern/loongarch64/cache_flush.S 
b/grub-core/kern/loongarch64/cache_flush.S
new file mode 100644
index 0..43b97d822
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache_flush.S
@@ -0,0 +1,33 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "cache_flush.S"
+   .text
+/*
+ * No further work to do because cache consistency is maintained by hardware on
+ * LoongArch.
+ */
+FUNCTION(grub_arch_clean_dcache_range)
+   dbar 0
+   jr $ra
+
+FUNCTION(grub_arch_invalidate_icache_range)
+   ibar 0
+   jr $ra
diff --git a/grub-core/kern/loongarch64/efi/init.c 
b/grub-core/kern/loongarch64/efi/init.c
new file mode 100644
index 0..8cbeafaba
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/init.c
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without eve

[PATCH v13 6/9] LoongArch: Add auxiliary files

2023-03-31 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/efi/mm.c  |  3 +-
 grub-core/kern/loongarch64/cache.c   | 39 
 grub-core/kern/loongarch64/cache_flush.S | 33 ++
 grub-core/kern/loongarch64/efi/init.c| 77 
 grub-core/lib/efi/halt.c |  2 +-
 include/grub/efi/efi.h   |  2 +-
 include/grub/loongarch64/efi/memory.h| 24 
 include/grub/loongarch64/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 3705b8b1b..ac13e95e9 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 0..43d314df9
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache.c
@@ -0,0 +1,39 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+
+/* Prototypes for asm functions. */
+void grub_arch_clean_dcache_range (void);
+void grub_arch_invalidate_icache_range (void);
+
+void
+grub_arch_sync_caches (void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  grub_arch_clean_dcache_range ();
+  grub_arch_invalidate_icache_range ();
+}
+
+void
+grub_arch_sync_dma_caches (volatile void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  /* DMA non-coherent devices not supported yet */
+}
diff --git a/grub-core/kern/loongarch64/cache_flush.S 
b/grub-core/kern/loongarch64/cache_flush.S
new file mode 100644
index 0..43b97d822
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache_flush.S
@@ -0,0 +1,33 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "cache_flush.S"
+   .text
+/*
+ * No further work to do because cache consistency is maintained by hardware on
+ * LoongArch.
+ */
+FUNCTION(grub_arch_clean_dcache_range)
+   dbar 0
+   jr $ra
+
+FUNCTION(grub_arch_invalidate_icache_range)
+   ibar 0
+   jr $ra
diff --git a/grub-core/kern/loongarch64/efi/init.c 
b/grub-core/kern/loongarch64/efi/init.c
new file mode 100644
index 0..8cbeafaba
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/init.c
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without eve

[PATCH v13 4/9] LoongArch: Add early startup code

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

diff --git a/grub-core/kern/loongarch64/efi/startup.S 
b/grub-core/kern/loongarch64/efi/startup.S
new file mode 100644
index 0..fc8123f8c
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/startup.S
@@ -0,0 +1,34 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "startup.S"
+   .text
+
+FUNCTION(_start)
+   /*
+*  EFI_SYSTEM_TABLE and EFI_HANDLE are passed in $a1/$a0.
+*/
+
+   la  $a2, EXT_C(grub_efi_image_handle)
+   st.d$a0, $a2, 0
+   la  $a2, EXT_C(grub_efi_system_table)
+   st.d$a1, $a2, 0
+
+   b   EXT_C(grub_main)
-- 
2.39.2


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


[PATCH v13 5/9] LoongArch: Add awareness for LoongArch relocations

2023-03-31 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/dl.c|   9 +-
 grub-core/kern/loongarch64/dl.c| 102 +
 grub-core/kern/loongarch64/dl_helper.c | 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, , );
   if (err)
 return err;
@@ -304,7 +306,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
  mod->segment = seg;
}
 }
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   mod->trampptr = ptr;
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
new file mode 100644
index 0..3a6aa91cd
--- /dev/null
+++ b/grub-core/kern/loongarch64/dl.c
@@ -0,0 +1,102 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Check if EHDR is a valid ELF header.  */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+  Elf_Ehdr *e = ehdr;
+
+  /* Check the magic numbers.  */
+  if (e->e_ident[EI_CLASS] != ELFCLASS64
+  || e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_LOONGARCH)
+return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF 
magic"));
+
+  return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/*
+ * Unified function for both REL and RELA.
+ */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+  Elf_Shdr *s, grub_dl_segment_t seg)
+{
+  Elf_Rel *rel, *max;
+  struct grub_loongarch64_stack stack;
+  grub_loongarch64_stack_init ();
+
+  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:

[PATCH v13 4/9] LoongArch: Add early startup code

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

diff --git a/grub-core/kern/loongarch64/efi/startup.S 
b/grub-core/kern/loongarch64/efi/startup.S
new file mode 100644
index 0..fc8123f8c
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/startup.S
@@ -0,0 +1,34 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "startup.S"
+   .text
+
+FUNCTION(_start)
+   /*
+*  EFI_SYSTEM_TABLE and EFI_HANDLE are passed in $a1/$a0.
+*/
+
+   la  $a2, EXT_C(grub_efi_image_handle)
+   st.d$a0, $a2, 0
+   la  $a2, EXT_C(grub_efi_system_table)
+   st.d$a1, $a2, 0
+
+   b   EXT_C(grub_main)
-- 
2.39.2


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


[PATCH v13 1/9] PE: Add LoongArch definitions

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

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


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


[PATCH v13 3/9] LoongArch: Add setjmp implementation

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

diff --git a/grub-core/lib/loongarch64/setjmp.S 
b/grub-core/lib/loongarch64/setjmp.S
new file mode 100644
index 0..41d58f569
--- /dev/null
+++ b/grub-core/lib/loongarch64/setjmp.S
@@ -0,0 +1,69 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "setjmp.S"
+
+GRUB_MOD_LICENSE "GPLv3+"
+
+   .text
+
+/*
+ * int grub_setjmp (jmp_buf env)
+ */
+FUNCTION(grub_setjmp)
+   st.d $s0, $a0, 0x0
+   st.d $s1, $a0, 0x8
+   st.d $s2, $a0, 0x10
+   st.d $s3, $a0, 0x18
+   st.d $s4, $a0, 0x20
+   st.d $s5, $a0, 0x28
+   st.d $s6, $a0, 0x30
+   st.d $s7, $a0, 0x38
+   st.d $s8, $a0, 0x40
+   st.d $fp, $a0, 0x48
+   st.d $sp, $a0, 0x50
+   st.d $ra, $a0, 0x58
+
+   move $a0, $zero
+   jr   $ra
+
+/*
+ * void grub_longjmp (jmp_buf env, int val)
+ */
+FUNCTION(grub_longjmp)
+   ld.d $s0, $a0, 0x0
+   ld.d $s1, $a0, 0x8
+   ld.d $s2, $a0, 0x10
+   ld.d $s3, $a0, 0x18
+   ld.d $s4, $a0, 0x20
+   ld.d $s5, $a0, 0x28
+   ld.d $s6, $a0, 0x30
+   ld.d $s7, $a0, 0x38
+   ld.d $s8, $a0, 0x40
+   ld.d $fp, $a0, 0x48
+   ld.d $sp, $a0, 0x50
+   ld.d $ra, $a0, 0x58
+
+   /* Return 1 if passed 0, otherwise returns the value in place. */
+   li.w $a0, 1
+   beqz $a1, 1f
+   move $a0, $a1
+1:
+   jr   $ra
diff --git a/grub-core/lib/setjmp.S b/grub-core/lib/setjmp.S
index 9c8721088..ffb26df79 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(__loongarch_lp64)
+#include "./loongarch64/setjmp.S"
 #elif defined(__riscv)
 #include "./riscv/setjmp.S"
 #else
diff --git a/include/grub/loongarch64/setjmp.h 
b/include/grub/loongarch64/setjmp.h
new file mode 100644
index 0..cb3e17763
--- /dev/null
+++ b/include/grub/loongarch64/setjmp.h
@@ -0,0 +1,27 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <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.39.2


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


[PATCH v13 5/9] LoongArch: Add awareness for LoongArch relocations

2023-03-31 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/dl.c|   9 +-
 grub-core/kern/loongarch64/dl.c| 102 +
 grub-core/kern/loongarch64/dl_helper.c | 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, , );
   if (err)
 return err;
@@ -304,7 +306,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
  mod->segment = seg;
}
 }
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   mod->trampptr = ptr;
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
new file mode 100644
index 0..3a6aa91cd
--- /dev/null
+++ b/grub-core/kern/loongarch64/dl.c
@@ -0,0 +1,102 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Check if EHDR is a valid ELF header.  */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+  Elf_Ehdr *e = ehdr;
+
+  /* Check the magic numbers.  */
+  if (e->e_ident[EI_CLASS] != ELFCLASS64
+  || e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_LOONGARCH)
+return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF 
magic"));
+
+  return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/*
+ * Unified function for both REL and RELA.
+ */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+  Elf_Shdr *s, grub_dl_segment_t seg)
+{
+  Elf_Rel *rel, *max;
+  struct grub_loongarch64_stack stack;
+  grub_loongarch64_stack_init ();
+
+  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:

[PATCH v13 1/9] PE: Add LoongArch definitions

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

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


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


[PATCH v13 2/9] Add LoongArch definitions

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

diff --git a/include/grub/elf.h b/include/grub/elf.h
index e6f073bc9..c36d7dab2 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_PCREL57
 
+/* LoongArch relocations */
+#define R_LARCH_NONE 0
+#define R_LARCH_64   2
+#define R_LARCH_MARK_LA  20
+#define R_LARCH_SOP_PUSH_PCREL   22
+#define R_LARCH_SOP_PUSH_ABSOLUTE23
+#define R_LARCH_SOP_PUSH_PLT_PCREL   29
+#define R_LARCH_SOP_SUB  32
+#define R_LARCH_SOP_SL   33
+#define R_LARCH_SOP_SR   34
+#define R_LARCH_SOP_ADD  35
+#define R_LARCH_SOP_AND  36
+#define R_LARCH_SOP_IF_ELSE  37
+#define R_LARCH_SOP_POP_32_S_10_538
+#define R_LARCH_SOP_POP_32_U_10_12   39
+#define R_LARCH_SOP_POP_32_S_10_12   40
+#define R_LARCH_SOP_POP_32_S_10_16   41
+#define R_LARCH_SOP_POP_32_S_10_16_S242
+#define R_LARCH_SOP_POP_32_S_5_2043
+#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
+#define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+
 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.39.2


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


[PATCH v13 0/9] Add LoongArch support

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

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

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

Please review the patches, thank you.

Xiaotian Wu (9):
  PE: Add LoongArch definitions
  Add LoongArch definitions
  LoongArch: Add setjmp implementation
  LoongArch: Add early startup code
  LoongArch: Add awareness for LoongArch relocations
  LoongArch: Add auxiliary files
  LoongArch: Add to build system
  LoongArch: Support new relocation types in v2.00 ABI
  tests: add support for LoongArch

 Makefile.util.def|   1 +
 configure.ac |  23 +-
 gentpl.py|  27 +--
 grub-core/Makefile.am|   6 +
 grub-core/Makefile.core.def  |  17 ++
 grub-core/kern/dl.c  |   9 +-
 grub-core/kern/efi/mm.c  |   3 +-
 grub-core/kern/loongarch64/cache.c   |  39 
 grub-core/kern/loongarch64/cache_flush.S |  33 +++
 grub-core/kern/loongarch64/dl.c  | 150 +
 grub-core/kern/loongarch64/dl_helper.c   | 261 +++
 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 +
 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   |  30 +++
 include/grub/loongarch64/efi/memory.h|  24 +++
 include/grub/loongarch64/reloc.h | 113 ++
 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| 126 +++
 util/grub-mknetdir.c |   1 +
 util/grub-mkrescue.c |   8 +
 util/grub-module-verifier.c  |  33 +++
 util/mkimage.c   |  16 ++
 40 files changed, 1258 insertions(+), 66 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 include/grub/loongarch64/efi/memory.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.39.2


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


Re: [PATCH v2 2/2] efi: Put Linux specific magic number in the DOS header

2022-12-07 Thread Xiaotian Wu
在 2022-12-07星期三的 09:06 +0100,Ard Biesheuvel写道:
> On Wed, 7 Dec 2022 at 08:51, Xiaotian Wu 
> wrote:
> > 
> > 在 2022-11-29星期二的 18:56 +0100,Ard Biesheuvel写道:
> > > GRUB currently relies on the magic number in the image header of
> > > ARM
> > > and
> > > arm64 EFI kernel images to decide whether or not the image in
> > > question
> > > is a bootable kernel.
> > > 
> > > However, the purpose of the magic number is to identify the image
> > > as
> > > one
> > > that implements the bare metal boot protocol, and so GRUB, which
> > > only
> > > does EFI boot, can only boot images that could potentially be
> > > booted
> > > in
> > > a non-EFI manner as well.
> > > 
> > > This is problematic for the new zboot decompressor image format,
> > > as
> > > it
> > > can only boot in EFI mode, and must therefore not use the bare
> > > metal
> > > boot magic number in its header.
> > > 
> > > For this reason, the strict magic number was dropped from GRUB,
> > > to
> > > permit essentially any kind of EFI executable to be booted via
> > > the
> > > 'linux' command, blurring the line between the linux loader and
> > > the
> > > chainloader.
> > > 
> > > So let's use the same field in the DOS header that RISC-V and
> > > arm64
> > > already use for their 'bare metal' magic numbers to store a
> > > 'generic
> > > Linux kernel' magic number, which can be used to identify
> > > bootable
> > > kernel images in PE format which don't necessarily implement a
> > > bare
> > > metal boot protocol in the same binary. Note that, in the context
> > > of
> > > EFI, the MSDOS header is only described in terms of the fields
> > > that
> > > it
> > > shares with the hybrid PE/COFF image format, (i.e., the magic
> > > number
> > > at
> > > offset #0 and the PE header offset at byte offset #0x3c). Since
> > > we
> > > aim
> > > for compatibility with EFI only, and not with MS-DOS or MS-
> > > Windows,
> > > we
> > > can use the remaining space in the MS-DOS header however we want.
> > > 
> > > Let's set the generic magic number for x86 images as well:
> > > existing
> > > bootloaders already have their own methods to identify x86 Linux
> > > images
> > > that can be booted in a non-EFI manner, and having the magic
> > > number
> > > in
> > > place there will ease any future transitions in loader
> > > implementations
> > > to merge the x86 and non-x86 EFI boot paths.
> > > 
> > > Note that 32-bit ARM already uses the same location in the header
> > > for
> > > a
> > > different purpose, but the ARM support is already widely
> > > implemented
> > > and
> > > the EFI zboot decompressor is not available on ARM anyway, so we
> > > just
> > > disregard it here.
> > > 
> > > Cc: Huacai Chen 
> > > Cc: Atish Patra 
> > > Cc: Heinrich Schuchardt 
> > > Cc: Daniel Kiper 
> > > Cc: Leif Lindholm 
> > > Signed-off-by: Ard Biesheuvel 
> > > ---
> > >  arch/loongarch/kernel/head.S    | 3 ++-
> > >  arch/x86/boot/header.S  | 3 ++-
> > >  drivers/firmware/efi/libstub/zboot-header.S | 3 ++-
> > >  include/linux/pe.h  | 7 +++
> > >  4 files changed, 13 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/arch/loongarch/kernel/head.S
> > > b/arch/loongarch/kernel/head.S
> > > index 84970e2666588963..caa74439700eee93 100644
> > > --- a/arch/loongarch/kernel/head.S
> > > +++ b/arch/loongarch/kernel/head.S
> > > @@ -25,7 +25,8 @@ _head:
> > >     .dword  kernel_entry    /* Kernel entry point */
> > >     .dword  _end - _text    /* Kernel image effective
> > > size */
> > >     .quad   0   /* Kernel image load
> > > offset
> > > from start of RAM */
> > > -   .org    0x3c    /* 0x20 ~ 0x3b reserved
> > > */
> > > +   .org    0x38    /* 0x20 ~ 0x38 reserved
> > > */
> > > +   .long   LINUX_PE_MAGIC
> > >     .long   pe_header - _head   /* Offset to the PE
> > > header */
> > > 
> > >

Re: [PATCH v2 2/2] efi: Put Linux specific magic number in the DOS header

2022-12-06 Thread Xiaotian Wu
git a/include/linux/pe.h b/include/linux/pe.h
> index 056a1762de904fc1..1db4c944efd78f51 100644
> --- a/include/linux/pe.h
> +++ b/include/linux/pe.h
> @@ -31,6 +31,13 @@
>  #define LINUX_EFISTUB_MAJOR_VERSION0x1
>  #define LINUX_EFISTUB_MINOR_VERSION0x1
>  
> +/*
> + * LINUX_PE_MAGIC appears at offset 0x38 into the MSDOS header of
> EFI bootable
> + * Linux kernel images that target the architecture as specified by
> the PE/COFF
> + * header machine type field.
> + */
> +#define LINUX_PE_MAGIC 0x818223cd
> +
>  #define MZ_MAGIC   0x5a4d  /* "MZ" */
>  
>  #define PE_MAGIC   0x4550  /* "PE\0\0" */


As far as I know, Archlinux automatically generates initramfs according
to the version number in the kernel file. The latest generic compressed
EFI designs do not seem to provide kernel version number information.
This may change the usage habits of Archlinux users. Is it possible to
add the kernel version number to vmlinuz.efi?

https://gitlab.archlinux.org/archlinux/mkinitcpio/mkinitcpio/-/blob/master/functions#L209

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


[PATCH v12 10/10] tests: add support for LoongArch

2022-12-05 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
---
 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.38.1


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


[PATCH v12 09/10] LoongArch: Support new relocation types in v2.00 ABI

2022-12-05 Thread Xiaotian Wu
Link: https://github.com/loongson/LoongArch-Documentation/pull/57
Signed-off-by: Xiaotian Wu 
---
 configure.ac   | 17 
 grub-core/kern/loongarch64/dl.c| 54 +--
 grub-core/kern/loongarch64/dl_helper.c | 59 ++
 include/grub/elf.h |  7 +++
 include/grub/loongarch64/reloc.h   |  6 +++
 util/grub-mkimagexx.c  | 49 -
 util/grub-module-verifier.c|  7 +++
 7 files changed, 187 insertions(+), 12 deletions(-)

diff --git a/configure.ac b/configure.ac
index 575cd0c17..08a1df527 100644
--- a/configure.ac
+++ b/configure.ac
@@ -864,17 +864,18 @@ 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
-TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
-TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
+  if test "x$grub_cv_cc_mno_explicit_relocs" = xyes; then
+TARGET_CFLAGS="$TARGET_CFLAGS -mno-explicit-relocs -fno-plt"
+TARGET_CCASFLAGS="$TARGET_CCASFLAGS -mno-explicit-relocs -fno-plt"
   fi
+  TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
+  TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
 fi
 
 # GRUB doesn't use float or doubles at all. Yet some toolchains may decide
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index 3a6aa91cd..47196a219 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -58,7 +58,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 +72,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 +92,47 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
case R_LARCH_SOP_PUSH_PLT_PCREL:
  grub_loongarch64_sop_push (, 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_ABS_HI20:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx_hi20 (abs_place, sym_addr);
+ }
+ break;
+   case R_LARCH_ABS64_LO20:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx64_lo20 (abs_place, sym_addr);
+ }
+ break;
+   case R_LARCH_ABS64_HI12:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx64_hi12 (abs_place, sym_addr);
+ }
+ 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_ABS_LO12:
+   case R_LARCH_PCALA_LO12:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_loongarch64_xxx_lo12 (abs_place, sym_addr);
+ }
+ break;
GRUB_LOONGARCH64_RELOCATION (, place, sym_addr)
default:
  {
diff --git a/grub-core/kern/loongarch64/d

[PATCH v12 00/10] Add support for LoongArch

2022-12-05 Thread Xiaotian Wu
This patchset adds support for the LoongArch instruction set.

Please review the patches and let me know if changes are needed.

v11->v12:
- based on the last commit.
- use LoadFile2 to loader initrd.

Xiaotian Wu (10):
  PE: Add LoongArch definitions
  Add LoongArch definitions
  LoongArch: Add setjmp implementation
  LoongArch: Add early startup code
  LoongArch: Add efi stubs kernel loader support
  LoongArch: Add awareness for LoongArch relocations
  LoongArch: Add auxiliary files
  LoongArch: Add to build system
  LoongArch: Support new relocation types in v2.00 ABI
  tests: add support for LoongArch

 Makefile.util.def|   1 +
 configure.ac |  23 +-
 gentpl.py|  27 +-
 grub-core/Makefile.am|   6 +
 grub-core/Makefile.core.def  |  17 +
 grub-core/kern/dl.c  |   9 +-
 grub-core/kern/efi/mm.c  |   3 +-
 grub-core/kern/loongarch64/cache.c   |  39 ++
 grub-core/kern/loongarch64/cache_flush.S |  33 ++
 grub-core/kern/loongarch64/dl.c  | 150 +++
 grub-core/kern/loongarch64/dl_helper.c   | 261 
 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 | 513 +++
 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   |  30 ++
 include/grub/loongarch64/efi/memory.h|  24 ++
 include/grub/loongarch64/linux.h |  44 ++
 include/grub/loongarch64/reloc.h | 113 +
 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| 126 ++
 util/grub-mknetdir.c |   1 +
 util/grub-mkrescue.c |   8 +
 util/grub-module-verifier.c  |  33 ++
 util/mkimage.c   |  16 +
 42 files changed, 1815 insertions(+), 66 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.38.1


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


[PATCH v12 07/10] LoongArch: Add auxiliary files

2022-12-05 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/efi/mm.c  |  3 +-
 grub-core/kern/loongarch64/cache.c   | 39 
 grub-core/kern/loongarch64/cache_flush.S | 33 ++
 grub-core/kern/loongarch64/efi/init.c| 77 
 grub-core/lib/efi/halt.c |  2 +-
 include/grub/efi/efi.h   |  2 +-
 include/grub/loongarch64/efi/memory.h| 24 
 include/grub/loongarch64/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 3705b8b1b..ac13e95e9 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 0..43d314df9
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache.c
@@ -0,0 +1,39 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+
+/* Prototypes for asm functions. */
+void grub_arch_clean_dcache_range (void);
+void grub_arch_invalidate_icache_range (void);
+
+void
+grub_arch_sync_caches (void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  grub_arch_clean_dcache_range ();
+  grub_arch_invalidate_icache_range ();
+}
+
+void
+grub_arch_sync_dma_caches (volatile void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  /* DMA non-coherent devices not supported yet */
+}
diff --git a/grub-core/kern/loongarch64/cache_flush.S 
b/grub-core/kern/loongarch64/cache_flush.S
new file mode 100644
index 0..43b97d822
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache_flush.S
@@ -0,0 +1,33 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "cache_flush.S"
+   .text
+/*
+ * No further work to do because cache consistency is maintained by hardware on
+ * LoongArch.
+ */
+FUNCTION(grub_arch_clean_dcache_range)
+   dbar 0
+   jr $ra
+
+FUNCTION(grub_arch_invalidate_icache_range)
+   ibar 0
+   jr $ra
diff --git a/grub-core/kern/loongarch64/efi/init.c 
b/grub-core/kern/loongarch64/efi/init.c
new file mode 100644
index 0..8cbeafaba
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/init.c
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without eve

[PATCH v12 08/10] LoongArch: Add to build system

2022-12-05 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 Makefile.util.def   |  1 +
 configure.ac| 22 -
 gentpl.py   | 27 ++--
 grub-core/Makefile.am   |  6 +
 grub-core/Makefile.core.def | 17 +
 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, 127 insertions(+), 39 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 93626b798..575cd0c17 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) ;;
@@ -860,6 +863,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)
@@ -1221,7 +1238,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"
@@ -2005,6 +2023,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..832d28ae0 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&qu

[PATCH v12 05/10] LoongArch: Add efi stubs kernel loader support

2022-12-05 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
---
 grub-core/loader/loongarch64/linux.c | 513 +++
 include/grub/loongarch64/linux.h |  44 +++
 2 files changed, 557 insertions(+)
 create mode 100644 grub-core/loader/loongarch64/linux.c
 create mode 100644 include/grub/loongarch64/linux.h

diff --git a/grub-core/loader/loongarch64/linux.c 
b/grub-core/loader/loongarch64/linux.c
new file mode 100644
index 0..786a1d358
--- /dev/null
+++ b/grub-core/loader/loongarch64/linux.c
@@ -0,0 +1,513 @@
+/*
+ *  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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+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;
+
+static struct grub_linux_initrd_context initrd_ctx = {0, 0, 0};
+static grub_efi_handle_t initrd_lf2_handle = NULL;
+static bool initrd_use_loadfile2 = false;
+
+static grub_efi_guid_t load_file2_guid = GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID;
+static grub_efi_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
+
+static initrd_media_device_path_t initrd_lf2_device_path = {
+  {
+{
+  GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+  GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+  sizeof(grub_efi_vendor_media_device_path_t),
+},
+LINUX_EFI_INITRD_MEDIA_GUID
+  }, {
+GRUB_EFI_END_DEVICE_PATH_TYPE,
+GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+sizeof(grub_efi_device_path_t)
+  }
+};
+
+static grub_efi_status_t __grub_efi_api
+grub_efi_initrd_load_file2 (grub_efi_load_file2_t *this,
+grub_efi_device_path_t *device_path,
+grub_efi_boolean_t boot_policy,
+grub_efi_uintn_t *buffer_size,
+void *buffer);
+
+static grub_efi_load_file2_t initrd_lf2 = {
+  grub_efi_initrd_load_file2
+};
+
+grub_err_t
+grub_arch_efi_linux_load_image_header (grub_file_t file,
+  struct linux_arch_kernel_header * lh)
+{
+  grub_file_seek (file, 0);
+  if (grub_file_read (file, lh, sizeof (*lh)) < (grub_ssize_t) sizeof (*lh))
+return grub_error(GRUB_ERR_FILE_READ_ERROR, "failed to read Linux image 
header");
+
+  if ((lh->code0 & 0x) != 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);
+
+  /*
+   * The PE/COFF spec permits the COFF header to appear anywhere in the file, 
so
+   * we need to double check whether it was where we expected it, and if not, 
we
+   * must load it from the correct offset into the pe_image_header field of
+   * struct linux_arch_kernel_header.
+   */
+  if ((grub_uint8_t *) lh + lh->hdr_offset != (grub_uint8_t *) 
>pe_image_header)
+{
+  if (grub_file_seek (file, lh->hdr_offset) == (grub_off_t) -1
+  || grub_file_read (file, >pe_image_header,
+ sizeof (struct grub_pe_image_header))
+ != sizeof (struct grub_pe_image_header))
+return grub_error (GRUB_ERR_FILE_READ_ERROR, "failed to read COFF 
image header");
+}
+
+  /*
+   * Linux kernels built for any architecture are guaranteed to support the
+   * LoadFile2 based initrd loading protocol if the image version is >= 1.
+   */
+  if (lh->pe_image_header.optional_header.major_image_version >= 1)
+initrd_use_loadfile2 = true;
+  else
+initrd_use_loadfile2 = false;
+
+  grub_dprintf ("linux", "LoadFile2 initrd loading %sabled\n",
+initrd_use_loadfile2 ? "en" : "dis");
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+finalize_params_linux (void)
+{
+  int node, retval;
+
+  void *fdt;
+
+  /* Set init

[PATCH v12 06/10] LoongArch: Add awareness for LoongArch relocations

2022-12-05 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/dl.c|   9 +-
 grub-core/kern/loongarch64/dl.c| 102 +
 grub-core/kern/loongarch64/dl_helper.c | 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, , );
   if (err)
 return err;
@@ -304,7 +306,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
  mod->segment = seg;
}
 }
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   mod->trampptr = ptr;
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
new file mode 100644
index 0..3a6aa91cd
--- /dev/null
+++ b/grub-core/kern/loongarch64/dl.c
@@ -0,0 +1,102 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Check if EHDR is a valid ELF header.  */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+  Elf_Ehdr *e = ehdr;
+
+  /* Check the magic numbers.  */
+  if (e->e_ident[EI_CLASS] != ELFCLASS64
+  || e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_LOONGARCH)
+return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF 
magic"));
+
+  return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/*
+ * Unified function for both REL and RELA.
+ */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+  Elf_Shdr *s, grub_dl_segment_t seg)
+{
+  Elf_Rel *rel, *max;
+  struct grub_loongarch64_stack stack;
+  grub_loongarch64_stack_init ();
+
+  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:

[PATCH v12 04/10] LoongArch: Add early startup code

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

diff --git a/grub-core/kern/loongarch64/efi/startup.S 
b/grub-core/kern/loongarch64/efi/startup.S
new file mode 100644
index 0..fc8123f8c
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/startup.S
@@ -0,0 +1,34 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "startup.S"
+   .text
+
+FUNCTION(_start)
+   /*
+*  EFI_SYSTEM_TABLE and EFI_HANDLE are passed in $a1/$a0.
+*/
+
+   la  $a2, EXT_C(grub_efi_image_handle)
+   st.d$a0, $a2, 0
+   la  $a2, EXT_C(grub_efi_system_table)
+   st.d$a1, $a2, 0
+
+   b   EXT_C(grub_main)
-- 
2.38.1


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


[PATCH v12 03/10] LoongArch: Add setjmp implementation

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

diff --git a/grub-core/lib/loongarch64/setjmp.S 
b/grub-core/lib/loongarch64/setjmp.S
new file mode 100644
index 0..41d58f569
--- /dev/null
+++ b/grub-core/lib/loongarch64/setjmp.S
@@ -0,0 +1,69 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+
+   .file   "setjmp.S"
+
+GRUB_MOD_LICENSE "GPLv3+"
+
+   .text
+
+/*
+ * int grub_setjmp (jmp_buf env)
+ */
+FUNCTION(grub_setjmp)
+   st.d $s0, $a0, 0x0
+   st.d $s1, $a0, 0x8
+   st.d $s2, $a0, 0x10
+   st.d $s3, $a0, 0x18
+   st.d $s4, $a0, 0x20
+   st.d $s5, $a0, 0x28
+   st.d $s6, $a0, 0x30
+   st.d $s7, $a0, 0x38
+   st.d $s8, $a0, 0x40
+   st.d $fp, $a0, 0x48
+   st.d $sp, $a0, 0x50
+   st.d $ra, $a0, 0x58
+
+   move $a0, $zero
+   jr   $ra
+
+/*
+ * void grub_longjmp (jmp_buf env, int val)
+ */
+FUNCTION(grub_longjmp)
+   ld.d $s0, $a0, 0x0
+   ld.d $s1, $a0, 0x8
+   ld.d $s2, $a0, 0x10
+   ld.d $s3, $a0, 0x18
+   ld.d $s4, $a0, 0x20
+   ld.d $s5, $a0, 0x28
+   ld.d $s6, $a0, 0x30
+   ld.d $s7, $a0, 0x38
+   ld.d $s8, $a0, 0x40
+   ld.d $fp, $a0, 0x48
+   ld.d $sp, $a0, 0x50
+   ld.d $ra, $a0, 0x58
+
+   /* Return 1 if passed 0, otherwise returns the value in place. */
+   li.w $a0, 1
+   beqz $a1, 1f
+   move $a0, $a1
+1:
+   jr   $ra
diff --git a/grub-core/lib/setjmp.S b/grub-core/lib/setjmp.S
index 9c8721088..cba1d546d 100644
--- a/grub-core/lib/setjmp.S
+++ b/grub-core/lib/setjmp.S
@@ -19,6 +19,8 @@
 #include "./arm/setjmp.S"
 #elif defined(__aarch64__)
 #include "./arm64/setjmp.S"
+#elif defined(__loongarch64)
+#include "./loongarch64/setjmp.S"
 #elif defined(__riscv)
 #include "./riscv/setjmp.S"
 #else
diff --git a/include/grub/loongarch64/setjmp.h 
b/include/grub/loongarch64/setjmp.h
new file mode 100644
index 0..cb3e17763
--- /dev/null
+++ b/include/grub/loongarch64/setjmp.h
@@ -0,0 +1,27 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <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.38.1


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


[PATCH v12 01/10] PE: Add LoongArch definitions

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

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


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


[PATCH v12 02/10] Add LoongArch definitions

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

diff --git a/include/grub/elf.h b/include/grub/elf.h
index e6f073bc9..c36d7dab2 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_PCREL57
 
+/* LoongArch relocations */
+#define R_LARCH_NONE 0
+#define R_LARCH_64   2
+#define R_LARCH_MARK_LA  20
+#define R_LARCH_SOP_PUSH_PCREL   22
+#define R_LARCH_SOP_PUSH_ABSOLUTE23
+#define R_LARCH_SOP_PUSH_PLT_PCREL   29
+#define R_LARCH_SOP_SUB  32
+#define R_LARCH_SOP_SL   33
+#define R_LARCH_SOP_SR   34
+#define R_LARCH_SOP_ADD  35
+#define R_LARCH_SOP_AND  36
+#define R_LARCH_SOP_IF_ELSE  37
+#define R_LARCH_SOP_POP_32_S_10_538
+#define R_LARCH_SOP_POP_32_U_10_12   39
+#define R_LARCH_SOP_POP_32_S_10_12   40
+#define R_LARCH_SOP_POP_32_S_10_16   41
+#define R_LARCH_SOP_POP_32_S_10_16_S242
+#define R_LARCH_SOP_POP_32_S_5_2043
+#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
+#define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+
 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.38.1


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


Re: [v6 PATCH 2/3] RISC-V: Update image header

2022-11-23 Thread Xiaotian Wu
arch_kernel_header linux_riscv_kernel_header
> > > diff --git a/include/grub/riscv64/linux.h
> > > b/include/grub/riscv64/linux.h
> > > index 3630c30fbf1a..a69046de34ef 100644
> > > --- a/include/grub/riscv64/linux.h
> > > +++ b/include/grub/riscv64/linux.h
> > > @@ -19,23 +19,26 @@
> > >   #ifndef GRUB_RISCV64_LINUX_HEADER
> > >   #define GRUB_RISCV64_LINUX_HEADER 1
> > > 
> > > -#define GRUB_LINUX_RISCV_MAGIC_SIGNATURE 0x52534356 /* 'RSCV' */
> > > +#include 
> > > 
> > >   #define GRUB_EFI_PE_MAGIC    0x5A4D
> > > 
> > > -/* From linux/Documentation/riscv/booting.txt */
> > > +/* From linux/Documentation/riscv/boot-image-header.rst */
> > >   struct linux_riscv_kernel_header
> > >   {
> > >     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 text_offset; /* Image load offset, little endian
> > > */
> > > +  grub_uint64_t image_size;  /* Effective Image size, little
> > > endian */
> > > +  grub_uint64_t flags;               /* kernel flags, little
> > > endian */
> > > +  grub_uint32_t version;     /* Version of this header */
> > > +  grub_uint32_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, "RSCV" */
> > > +  grub_uint64_t magic;               /* magic (RISC-V specifc,
> > > deprecated)*/
> > > +  grub_uint32_t magic2;              /* Magic number 2 (to match
> > > the ARM64 'magic' field pos) */
> > >     grub_uint32_t hdr_offset;  /* Offset of PE/COFF header */
> > > +
> > > +  struct grub_pe_image_header pe_image_header;
> > >   };
> > 
> > Daniel
> ___
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel

-- 
Best Regards
Xiaotian Wu


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


  1   2   3   >