Re: [PATCH v3 07/10] RISC-V: Add awareness for RISC-V reloations

2018-11-25 Thread Alexander Graf


On 21.11.18 16:51, Daniel Kiper wrote:
> On Wed, Nov 14, 2018 at 06:27:36PM +0100, Alexander Graf wrote:
>> This patch adds awareness of RISC-V relocations throughout the grub tools
>> as well as dynamic linkage and elf->PE relocation conversion support.
>>
>> Signed-off-by: Alexander Graf 
>>
>> ---
>>
>> v2 -> v3:
>>
>>   - Fix riscv32 target
>> ---
>>  grub-core/kern/dl.c |   6 +-
>>  grub-core/kern/riscv/dl.c   | 335 
>> 
>>  include/grub/dl.h   |   6 +-
>>  util/grub-mkimagexx.c   | 268 +++
>>  util/grub-module-verifier.c |  56 
>>  5 files changed, 666 insertions(+), 5 deletions(-)
>>  create mode 100644 grub-core/kern/riscv/dl.c
>>
>> diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
>> index f8d58f029..48eb5e7b6 100644
>> --- a/grub-core/kern/dl.c
>> +++ b/grub-core/kern/dl.c
>> @@ -225,7 +225,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
>>unsigned i;
>>const Elf_Shdr *s;
>>grub_size_t tsize = 0, talign = 1;
>> -#if !defined (__i386__) && !defined (__x86_64__)
>> +#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
> 
> Could not we use __riscv__?

Unfortunately __riscv__ is not defined in recent versions of gcc:


https://github.com/riscv/riscv-toolchain-conventions#cc-preprocessor-definitions

> 
>>grub_size_t tramp;
>>grub_size_t got;
>>grub_err_t err;
>> @@ -241,7 +241,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
>>  talign = s->sh_addralign;
>>  }
>>
>> -#if !defined (__i386__) && !defined (__x86_64__)
>> +#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
>>err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
>>if (err)
>>  return err;
>> @@ -304,7 +304,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
>>mod->segment = seg;
>>  }
>>  }
>> -#if !defined (__i386__) && !defined (__x86_64__)
>> +#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
>>ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
>>mod->tramp = ptr;
>>mod->trampptr = ptr;
>> diff --git a/grub-core/kern/riscv/dl.c b/grub-core/kern/riscv/dl.c
>> new file mode 100644
>> index 0..503b67df1
>> --- /dev/null
>> +++ b/grub-core/kern/riscv/dl.c
>> @@ -0,0 +1,335 @@
>> +/* dl.c - arch-dependent part of loadable module support */
>> +/*
>> + *  GRUB  --  GRand Unified Bootloader
>> + *  Copyright (C) 2013  Free Software Foundation, Inc.
> 
> s/2013/2018/
> 
>> + *
>> + *  GRUB is free software: you can redistribute it and/or modify
>> + *  it under the terms of the GNU General Public License as published by
>> + *  the Free Software Foundation, either version 3 of the License, or
>> + *  (at your option) any later version.
>> + *
>> + *  GRUB is distributed in the hope that it will be useful,
>> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + *  GNU General Public License for more details.
>> + *
>> + *  You should have received a copy of the GNU General Public License
>> + *  along with GRUB.  If not, see .
>> + */
>> +
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +
>> +#define LDR 0x5850
>> +#define BR 0xd61f0200
>> +
>> +
> 
> Please drop this empty line.
> 
>> +/*
>> + * 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_DATA] != ELFDATA2LSB || e->e_machine != EM_RISCV)
>> +return grub_error (GRUB_ERR_BAD_OS,
>> +   N_("invalid arch-dependent ELF magic"));
>> +
>> +  return GRUB_ERR_NONE;
>> +}
>> +
>> +#pragma GCC diagnostic ignored "-Wcast-align"
> 
> Why? Could you add a comment here?

Mostly because all other targets have that as well?

I assume it's to silence warnings that the alignment could be mismatched
now, because offsets to elf data pointers could potentially be odd. In
practice, it never is though.

If I remove it, I get the following compile errors:

kern/riscv/dl.c: In function ‘grub_arch_dl_relocate_symbols’:
kern/riscv/dl.c:59:14: error: cast increases required alignment of
target type [-Werror=cast-align]
   for (rel = (Elf_Rel *) ((char *) ehdr + s->sh_offset),
  ^
kern/riscv/dl.c:60:9: error: cast increases required alignment of target
type [-Werror=cast-align]
   max = (Elf_Rel *) ((char *) rel + s->sh_size);
 ^
kern/riscv/dl.c:62:14: error: cast increases required alignment of
target type [-Werror=cast-align]
rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
  ^
kern/riscv/dl.c:72:13: error: cast increases required alignment of
target type [-Werror=cast-align]
   sym = (Elf_Sym *) ((char *) mod->symtab
 ^
kern/r

Re: [PATCH v3 08/10] RISC-V: Add auxiliary files

2018-11-25 Thread Alexander Graf



On 21.11.18 17:02, Daniel Kiper wrote:
> On Wed, Nov 14, 2018 at 06:27:37PM +0100, Alexander Graf wrote:
>> To support a new architecture we need to provide a few helper functions
>> for memory, cache, timer, etc support.
>>
>> This patch adds the remainders of those. Some bits are still disabled,
>> as I couldn't guarantee that we're always running on models / in modes
>> where the respective hardware is available.
>>
>> Signed-off-by: Alexander Graf 
>>
>> ---
>>
>> v2 -> v3:
>>
>>   - Fix riscv32 target
>> ---
>>  grub-core/kern/riscv/cache.c   | 64 +
>>  grub-core/kern/riscv/cache_flush.S | 40 +++
>>  grub-core/kern/riscv/efi/init.c| 82 
>> ++
>>  include/grub/riscv32/efi/memory.h  |  6 +++
>>  include/grub/riscv32/time.h| 28 +
>>  include/grub/riscv32/types.h   | 34 
>>  include/grub/riscv64/efi/memory.h  |  6 +++
>>  include/grub/riscv64/time.h| 28 +
>>  include/grub/riscv64/types.h   | 34 
>>  9 files changed, 322 insertions(+)
>>  create mode 100644 grub-core/kern/riscv/cache.c
>>  create mode 100644 grub-core/kern/riscv/cache_flush.S
>>  create mode 100644 grub-core/kern/riscv/efi/init.c
>>  create mode 100644 include/grub/riscv32/efi/memory.h
>>  create mode 100644 include/grub/riscv32/time.h
>>  create mode 100644 include/grub/riscv32/types.h
>>  create mode 100644 include/grub/riscv64/efi/memory.h
>>  create mode 100644 include/grub/riscv64/time.h
>>  create mode 100644 include/grub/riscv64/types.h
>>
>> diff --git a/grub-core/kern/riscv/cache.c b/grub-core/kern/riscv/cache.c
>> new file mode 100644
>> index 0..15dbe6927
>> --- /dev/null
>> +++ b/grub-core/kern/riscv/cache.c
>> @@ -0,0 +1,64 @@
>> +/*
>> + *  GRUB  --  GRand Unified Bootloader
>> + *  Copyright (C) 2013  Free Software Foundation, Inc.
> 
> s/2013/2018/
> 
>> + *
>> + *  GRUB is free software: you can redistribute it and/or modify
>> + *  it under the terms of the GNU General Public License as published by
>> + *  the Free Software Foundation, either version 3 of the License, or
>> + *  (at your option) any later version.
>> + *
>> + *  GRUB is distributed in the hope that it will be useful,
>> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + *  GNU General Public License for more details.
>> + *
>> + *  You should have received a copy of the GNU General Public License
>> + *  along with GRUB.  If not, see .
>> + */
>> +
>> +#include 
>> +#include 
>> +
>> +static grub_int64_t dlinesz;
>> +static grub_int64_t ilinesz;
>> +
>> +/* Prototypes for asm functions. */
>> +void grub_arch_clean_dcache_range (grub_addr_t beg, grub_addr_t end,
>> +   grub_size_t line_size);
>> +void grub_arch_invalidate_icache_range (grub_addr_t beg, grub_addr_t end,
>> +grub_size_t line_size);
>> +
>> +static void
>> +probe_caches (void)
>> +{
>> +  /* TODO */
>> +  dlinesz = 32;
>> +  ilinesz = 32;
>> +}
>> +
>> +void
>> +grub_arch_sync_caches (void *address, grub_size_t len)
>> +{
>> +  grub_size_t start, end, max_align;
>> +
>> +  if (dlinesz == 0)
>> +probe_caches();
>> +  if (dlinesz == 0)
>> +grub_fatal ("Unknown cache line size!");
>> +
>> +  max_align = dlinesz > ilinesz ? dlinesz : ilinesz;
>> +
>> +  start = ALIGN_DOWN ((grub_size_t) address, max_align);
>> +  end = ALIGN_UP ((grub_size_t) address + len, max_align);
>> +
>> +  grub_arch_clean_dcache_range (start, end, dlinesz);
>> +  grub_arch_invalidate_icache_range (start, end, ilinesz);
>> +}
>> +
>> +void
>> +grub_arch_sync_dma_caches (volatile void *address, grub_size_t len)
>> +{
>> +  /* DMA incoherent devices not supported yet */
>> +  if (address || len || 1)
>> +return;
> 
> I think that you can drop the code and leave the comment here.

IIRC I had issues with some gcc versions that complained about unused
arguments in that case.

> 
>> +}
>> diff --git a/grub-core/kern/riscv/cache_flush.S 
>> b/grub-core/kern/riscv/cache_flush.S
>> new file mode 100644
>> index 0..033a8aac3
>> --- /dev/null
>> +++ b/grub-core/kern/riscv/cache_flush.S
>> @@ -0,0 +1,40 @@
>> +/*
>> + *  GRUB  --  GRand Unified Bootloader
>> + *  Copyright (C) 2013  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

Re: [PATCH v3 08/10] RISC-V: Add auxiliary files

2018-11-25 Thread Alexander Graf



On 17.11.18 23:51, Auer, Lukas wrote:
> On Wed, 2018-11-14 at 18:27 +0100, Alexander Graf wrote:
>> To support a new architecture we need to provide a few helper
>> functions
>> for memory, cache, timer, etc support.
>>
>> This patch adds the remainders of those. Some bits are still
>> disabled,
>> as I couldn't guarantee that we're always running on models / in
>> modes
>> where the respective hardware is available.
>>
>> Signed-off-by: Alexander Graf 
>>
>> ---
>>
>> v2 -> v3:
>>
>>   - Fix riscv32 target
>> ---
>>  grub-core/kern/riscv/cache.c   | 64
>> +
>>  grub-core/kern/riscv/cache_flush.S | 40 +++
>>  grub-core/kern/riscv/efi/init.c| 82
>> ++
>>  include/grub/riscv32/efi/memory.h  |  6 +++
>>  include/grub/riscv32/time.h| 28 +
>>  include/grub/riscv32/types.h   | 34 
>>  include/grub/riscv64/efi/memory.h  |  6 +++
>>  include/grub/riscv64/time.h| 28 +
>>  include/grub/riscv64/types.h   | 34 
>>  9 files changed, 322 insertions(+)
>>  create mode 100644 grub-core/kern/riscv/cache.c
>>  create mode 100644 grub-core/kern/riscv/cache_flush.S
>>  create mode 100644 grub-core/kern/riscv/efi/init.c
>>  create mode 100644 include/grub/riscv32/efi/memory.h
>>  create mode 100644 include/grub/riscv32/time.h
>>  create mode 100644 include/grub/riscv32/types.h
>>  create mode 100644 include/grub/riscv64/efi/memory.h
>>  create mode 100644 include/grub/riscv64/time.h
>>  create mode 100644 include/grub/riscv64/types.h
>>
>> diff --git a/grub-core/kern/riscv/cache.c b/grub-
>> core/kern/riscv/cache.c
>> new file mode 100644
>> index 0..15dbe6927
>> --- /dev/null
>> +++ b/grub-core/kern/riscv/cache.c
>> @@ -0,0 +1,64 @@
>> +/*
>> + *  GRUB  --  GRand Unified Bootloader
>> + *  Copyright (C) 2013  Free Software Foundation, Inc.
>> + *
>> + *  GRUB is free software: you can redistribute it and/or modify
>> + *  it under the terms of the GNU General Public License as
>> published by
>> + *  the Free Software Foundation, either version 3 of the License,
>> or
>> + *  (at your option) any later version.
>> + *
>> + *  GRUB is distributed in the hope that it will be useful,
>> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + *  GNU General Public License for more details.
>> + *
>> + *  You should have received a copy of the GNU General Public
>> License
>> + *  along with GRUB.  If not, see .
>> + */
>> +
>> +#include 
>> +#include 
>> +
>> +static grub_int64_t dlinesz;
>> +static grub_int64_t ilinesz;
>> +
>> +/* Prototypes for asm functions. */
>> +void grub_arch_clean_dcache_range (grub_addr_t beg, grub_addr_t end,
>> +   grub_size_t line_size);
>> +void grub_arch_invalidate_icache_range (grub_addr_t beg, grub_addr_t
>> end,
>> +grub_size_t line_size);
>> +
>> +static void
>> +probe_caches (void)
>> +{
>> +  /* TODO */
>> +  dlinesz = 32;
>> +  ilinesz = 32;
>> +}
>> +
>> +void
>> +grub_arch_sync_caches (void *address, grub_size_t len)
>> +{
>> +  grub_size_t start, end, max_align;
>> +
>> +  if (dlinesz == 0)
>> +probe_caches();
>> +  if (dlinesz == 0)
>> +grub_fatal ("Unknown cache line size!");
>> +
>> +  max_align = dlinesz > ilinesz ? dlinesz : ilinesz;
>> +
>> +  start = ALIGN_DOWN ((grub_size_t) address, max_align);
>> +  end = ALIGN_UP ((grub_size_t) address + len, max_align);
>> +
>> +  grub_arch_clean_dcache_range (start, end, dlinesz);
>> +  grub_arch_invalidate_icache_range (start, end, ilinesz);
>> +}
>> +
>> +void
>> +grub_arch_sync_dma_caches (volatile void *address, grub_size_t len)
>> +{
>> +  /* DMA incoherent devices not supported yet */
>> +  if (address || len || 1)
>> +return;
>> +}
>> diff --git a/grub-core/kern/riscv/cache_flush.S b/grub-
>> core/kern/riscv/cache_flush.S
>> new file mode 100644
>> index 0..033a8aac3
>> --- /dev/null
>> +++ b/grub-core/kern/riscv/cache_flush.S
>> @@ -0,0 +1,40 @@
>> +/*
>> + *  GRUB  --  GRand Unified Bootloader
>> + *  Copyright (C) 2013  Free Software Foundation, Inc.
>> + *
>> + *  GRUB is free software: you can redistribute it and/or modify
>> + *  it under the terms of the GNU General Public License as
>> published by
>> + *  the Free Software Foundation, either version 3 of the License,
>> or
>> + *  (at your option) any later version.
>> + *
>> + *  GRUB is distributed in the hope that it will be useful,
>> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + *  GNU General Public License for more details.
>> + *
>> + *  You should have received a copy of the GNU General Public
>> License
>> + *  along with GRUB.  If not, see .
>> + */
>> +
>> +#include 

Re: [PATCH v3 09/10] RISC-V: Add to build system

2018-11-25 Thread Alexander Graf



On 18.11.18 12:41, Bin Meng wrote:
> Hi Alex,
> 
> On Thu, Nov 15, 2018 at 1:27 AM Alexander Graf  wrote:
>>
>> This patch adds support for RISC-V to the grub build system. With this
>> patch, I can successfully build grub on RISC-V as a UEFI application.
>>
>> Signed-off-by: Alexander Graf 
>> Reviewed-by: Alistair Francis 
>>
>> ---
>>
>> v2 -> v3:
>>
>>   - Fix riscv32 target
>> ---
>>  configure.ac | 28 ++--
>>  gentpl.py| 11 +++
>>  grub-core/Makefile.am| 12 
>>  grub-core/Makefile.core.def  | 29 +
>>  grub-core/commands/file.c| 14 +-
>>  grub-core/kern/compiler-rt.c |  6 --
>>  grub-core/kern/efi/mm.c  |  2 +-
>>  grub-core/kern/emu/cache.c   |  6 ++
>>  grub-core/kern/emu/cache_s.S |  1 +
>>  grub-core/kern/emu/lite.c|  2 ++
>>  grub-core/lib/efi/halt.c |  3 ++-
>>  grub-core/lib/setjmp.S   |  2 ++
>>  include/grub/compiler-rt.h   | 12 
>>  include/grub/efi/api.h   |  3 ++-
>>  include/grub/efi/efi.h   |  2 +-
>>  include/grub/misc.h  |  3 ++-
>>  include/grub/util/install.h  |  2 ++
>>  util/grub-install-common.c   |  2 ++
>>  util/grub-install.c  | 28 
>>  util/grub-mknetdir.c |  4 +++-
>>  util/grub-mkrescue.c | 16 +++-
>>  util/mkimage.c   | 32 
>>  22 files changed, 200 insertions(+), 20 deletions(-)
>>
> 
> Reviewed-by: Bin Meng 
> Tested-by: Bin Meng 
> 
> But please see one comment below:
> 
>> diff --git a/configure.ac b/configure.ac
>> index 5e63c4af3..2cc2ffb2c 100644
>> --- a/configure.ac
>> +++ b/configure.ac
>> @@ -104,6 +104,12 @@ case "$target_cpu" in
>>aarch64*)
>> target_cpu=arm64
>> ;;
>> +  riscv32*)
>> +   target_cpu=riscv32
>> +   ;;
>> +  riscv64*)
>> +   target_cpu=riscv64
>> +   ;;
>>  esac
>>
>>  # Specify the platform (such as firmware).
>> @@ -127,6 +133,8 @@ if test "x$with_platform" = x; then
>>  ia64-*) platform=efi ;;
>>  arm-*) platform=uboot ;;
>>  arm64-*) platform=efi ;;
>> +riscv32-*) platform=efi ;;
>> +riscv64-*) platform=efi ;;
>>  *)
>>AC_MSG_WARN([unsupported CPU: "$target_cpu" - only building 
>> utilities])
>>platform=none
>> @@ -174,6 +182,8 @@ case "$target_cpu"-"$platform" in
>>arm-coreboot) ;;
>>arm-efi) ;;
>>arm64-efi) ;;
>> +  riscv32-efi) ;;
>> +  riscv64-efi) ;;
>>*-emu) ;;
>>*-none) ;;
>>*) AC_MSG_ERROR([platform "$platform" is not supported for target CPU 
>> "$target_cpu"]) ;;
>> @@ -826,6 +836,16 @@ if test x"$platform" != xemu ; then
>> AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
>>  
>> [grub_cv_target_cc_soft_float="-mgeneral-regs-only"], [])
>>  fi
>> +if test "x$target_cpu" = xriscv32; then
>> +   CFLAGS="$TARGET_CFLAGS -march=rv32imac -mabi=ilp32 -Werror"
>> +   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
>> +[grub_cv_target_cc_soft_float="-march=rv32imac 
>> -mabi=ilp32"], [])
>> +fi
>> +if test "x$target_cpu" = xriscv64; then
>> +   CFLAGS="$TARGET_CFLAGS -march=rv64imac -mabi=lp64 -Werror"
>> +   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
>> +[grub_cv_target_cc_soft_float="-march=rv64imac 
>> -mabi=lp64"], [])
>> +fi
>>  if test "x$target_cpu" = xia64; then
>> CFLAGS="$TARGET_CFLAGS -mno-inline-float-divide -mno-inline-sqrt 
>> -Werror"
>> AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
>> @@ -1141,7 +1161,7 @@ AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)
>>
>>  LDFLAGS="$TARGET_LDFLAGS"
>>
>> -if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 ; 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"
>> @@ -1151,7 +1171,7 @@ if test "$target_cpu" = x86_64 || test "$target_cpu" = 
>> sparc64 ; then
>>])
>>if test "x$grub_cv_cc_mcmodel" = xyes; then
>>  TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=large"
>> -  elif test "$target_cpu" = sparc64; then
>> +  elif test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64; then
>>  TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
> 
> The patch does not indicate the code model explicitly for riscv32.
> It's better we pass the -mcmodel=medlow (suitable for riscv32) to the
> compiler in case the compiler is configured with a different code
> model by default.

I'm afraid there is no other model?

  https://gcc.gnu.org/onlinedocs/gcc/RISC-V-Options.html


Alex

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.g

[PATCH v4 03/10] elf.h: Add RISC-V definitions

2018-11-25 Thread Alexander Graf
The RISC-V ABI document outlines ELF header structure and relocation
information. Pull the respective magic numbers into our elf header
so we can make use of them.

Signed-off-by: Alexander Graf 
Reviewed-by: Alistair Francis 
Reviewed-by: Bin Meng 
Tested-by: Bin Meng 
---
 include/grub/elf.h | 59 ++
 1 file changed, 59 insertions(+)

diff --git a/include/grub/elf.h b/include/grub/elf.h
index c8492f9dc..76c6a5a02 100644
--- a/include/grub/elf.h
+++ b/include/grub/elf.h
@@ -247,6 +247,7 @@ typedef struct
 #define EM_XTENSA  94  /* Tensilica Xtensa Architecture */
 #define EM_NUM 95
 #define EM_AARCH64 183 /* ARM 64-bit architecture */
+#define EM_RISCV   243 /* RISC-V */
 
 /* If it is necessary to assign new unofficial EM_* values, please
pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the
@@ -2473,6 +2474,64 @@ typedef Elf32_Addr Elf32_Conflict;
 
 #define R_X86_64_NUM   24
 
+/* RISC-V relocations */
+#define R_RISCV_NONE0
+#define R_RISCV_32  1
+#define R_RISCV_64  2
+#define R_RISCV_RELATIVE3
+#define R_RISCV_COPY4
+#define R_RISCV_JUMP_SLOT   5
+#define R_RISCV_TLS_DTPMOD326
+#define R_RISCV_TLS_DTPMOD647
+#define R_RISCV_TLS_DTPREL328
+#define R_RISCV_TLS_DTPREL649
+#define R_RISCV_TLS_TPREL32 10
+#define R_RISCV_TLS_TPREL64 11
+
+#define R_RISCV_BRANCH  16
+#define R_RISCV_JAL 17
+#define R_RISCV_CALL18
+#define R_RISCV_CALL_PLT19
+#define R_RISCV_GOT_HI2020
+#define R_RISCV_TLS_GOT_HI2021
+#define R_RISCV_TLS_GD_HI20 22
+#define R_RISCV_PCREL_HI20  23
+#define R_RISCV_PCREL_LO12_I24
+#define R_RISCV_PCREL_LO12_S25
+#define R_RISCV_HI2026
+#define R_RISCV_LO12_I  27
+#define R_RISCV_LO12_S  28
+#define R_RISCV_TPREL_HI20  29
+#define R_RISCV_TPREL_LO12_I30
+#define R_RISCV_TPREL_LO12_S31
+#define R_RISCV_TPREL_ADD   32
+#define R_RISCV_ADD833
+#define R_RISCV_ADD16   34
+#define R_RISCV_ADD32   35
+#define R_RISCV_ADD64   36
+#define R_RISCV_SUB837
+#define R_RISCV_SUB16   38
+#define R_RISCV_SUB32   39
+#define R_RISCV_SUB64   40
+#define R_RISCV_GNU_VTINHERIT   41
+#define R_RISCV_GNU_VTENTRY 42
+#define R_RISCV_ALIGN   43
+#define R_RISCV_RVC_BRANCH  44
+#define R_RISCV_RVC_JUMP45
+#define R_RISCV_LUI 46
+#define R_RISCV_GPREL_I 47
+#define R_RISCV_GPREL_S 48
+#define R_RISCV_TPREL_I 49
+#define R_RISCV_TPREL_S 50
+#define R_RISCV_RELAX   51
+#define R_RISCV_SUB652
+#define R_RISCV_SET653
+#define R_RISCV_SET854
+#define R_RISCV_SET16   55
+#define R_RISCV_SET32   56
+#define R_RISCV_32_PCREL57
+
+
 #ifdef GRUB_TARGET_WORDSIZE
 #if GRUB_TARGET_WORDSIZE == 32
 
-- 
2.12.3


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


[PATCH v4 05/10] RISC-V: Add early startup code

2018-11-25 Thread Alexander Graf
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: Alexander Graf 
Reviewed-by: Alistair Francis 
Reviewed-by: Bin Meng 
Tested-by: Bin Meng 

---

v3 -> v4:

  - Change copyright from 2013 to 2018
---
 grub-core/kern/riscv/efi/startup.S | 49 ++
 1 file changed, 49 insertions(+)
 create mode 100644 grub-core/kern/riscv/efi/startup.S

diff --git a/grub-core/kern/riscv/efi/startup.S 
b/grub-core/kern/riscv/efi/startup.S
new file mode 100644
index 0..287930f6a
--- /dev/null
+++ b/grub-core/kern/riscv/efi/startup.S
@@ -0,0 +1,49 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see .
+ */
+
+#include 
+
+#if __riscv_xlen == 64
+#define sl sd
+#define ll ld
+#else
+#define sl sw
+#define ll lw
+#endif
+
+
+   .file   "startup.S"
+   .text
+FUNCTION(_start)
+   /*
+*  EFI_SYSTEM_TABLE and EFI_HANDLE are passed in a1/a0.
+*/
+
+   ll  a2, efi_image_handle_val
+   sl  a0, 0(a2)
+   ll  a2, efi_system_table_val
+   sl  a1, 0(a2)
+   ll  a2, grub_main_val
+   jr  a2
+grub_main_val:
+   .quad   EXT_C(grub_main)
+efi_system_table_val:
+   .quad   EXT_C(grub_efi_system_table)
+efi_image_handle_val:
+   .quad   EXT_C(grub_efi_image_handle)
+
-- 
2.12.3


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


[PATCH v4 02/10] PE: Add RISC-V definitions

2018-11-25 Thread Alexander Graf
The PE format defines magic numbers as well as relocation identifiers for
RISC-V. Add them to our include file, so we can make use of them.

Signed-off-by: Alexander Graf 
Reviewed-by: Leif Lindholm 
Reviewed-by: Alistair Francis 
Reviewed-by: Bin Meng 
Tested-by: Bin Meng 
---
 include/grub/efi/pe32.h | 5 +
 1 file changed, 5 insertions(+)

diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h
index 7d44732d2..d1359eb66 100644
--- a/include/grub/efi/pe32.h
+++ b/include/grub/efi/pe32.h
@@ -70,6 +70,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_RISCV32  0x5032
+#define GRUB_PE32_MACHINE_RISCV64  0x5064
 
 #define GRUB_PE32_RELOCS_STRIPPED  0x0001
 #define GRUB_PE32_EXECUTABLE_IMAGE 0x0002
@@ -281,9 +283,12 @@ struct grub_pe32_fixup_block
 #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
-- 
2.12.3


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


[PATCH v4 00/10] Add RISC-V support

2018-11-25 Thread Alexander Graf
As part of the plan for total world domination, we would like to make sure
that booting on RISC-V is in a sane state before anyone goes and does quick
hacks "just because".

For that reason, U-Boot supports UEFI booting on RISC-V for a while now.
This patch set is the second part of the puzzle, with grub learning how to
deal with a UEFI enabled RISC-V target.

The third bit (still missing) is to actually make a working Linux UEFI port.
But that will come, I'm sure :).

Looking forward to review feedback and testing,

v1 -> v2:

  - adapt to new grub_open_file() API
  - adapt to new grub_create_loader_cmdline() API

v2 -> v3:

  - fix riscv32 target

v3 -> v4:

  - Rebase onto current git master
  - Change copyright from 2013 to 2018
  - Coding style fixes
  - Add spec reference
  - Resurrect time reading code

Alex

Alexander Graf (10):
  efi: Rename armxx to arch
  PE: Add RISC-V definitions
  elf.h: Add RISC-V definitions
  RISC-V: Add setjmp implementation
  RISC-V: Add early startup code
  RISC-V: Add Linux load logic
  RISC-V: Add awareness for RISC-V reloations
  RISC-V: Add auxiliary files
  RISC-V: Add to build system
  fdt: Treat device tree file type like ACPI

 configure.ac   |  28 ++-
 gentpl.py  |  11 +-
 grub-core/Makefile.am  |  12 ++
 grub-core/Makefile.core.def|  29 +++
 grub-core/commands/efi/shim_lock.c |   1 +
 grub-core/commands/file.c  |  14 +-
 grub-core/kern/compiler-rt.c   |   6 +-
 grub-core/kern/dl.c|   6 +-
 grub-core/kern/efi/mm.c|   2 +-
 grub-core/kern/emu/cache.c |   6 +
 grub-core/kern/emu/cache_s.S   |   1 +
 grub-core/kern/emu/lite.c  |   2 +
 grub-core/kern/riscv/cache.c   |  64 +++
 grub-core/kern/riscv/cache_flush.S |  44 +
 grub-core/kern/riscv/dl.c  | 340 +++
 grub-core/kern/riscv/efi/init.c|  77 
 grub-core/kern/riscv/efi/startup.S |  49 ++
 grub-core/lib/efi/halt.c   |   3 +-
 grub-core/lib/riscv/setjmp.S   |  82 +
 grub-core/lib/setjmp.S |   2 +
 grub-core/loader/arm64/linux.c |  10 +-
 grub-core/loader/arm64/xen_boot.c  |   6 +-
 grub-core/loader/riscv/linux.c | 351 +
 include/grub/arm/linux.h   |   2 +-
 include/grub/arm64/linux.h |   2 +-
 include/grub/compiler-rt.h |  12 +-
 include/grub/dl.h  |   6 +-
 include/grub/efi/api.h |   3 +-
 include/grub/efi/efi.h |   6 +-
 include/grub/efi/pe32.h|   5 +
 include/grub/elf.h |  59 +++
 include/grub/file.h|   4 +-
 include/grub/misc.h|   3 +-
 include/grub/riscv32/efi/memory.h  |   6 +
 include/grub/riscv32/linux.h   |  41 +
 include/grub/riscv32/setjmp.h  |  27 +++
 include/grub/riscv32/time.h|  28 +++
 include/grub/riscv32/types.h   |  34 
 include/grub/riscv64/efi/memory.h  |   6 +
 include/grub/riscv64/linux.h   |  43 +
 include/grub/riscv64/setjmp.h  |  27 +++
 include/grub/riscv64/time.h|  28 +++
 include/grub/riscv64/types.h   |  34 
 include/grub/util/install.h|   2 +
 util/grub-install-common.c |   2 +
 util/grub-install.c|  28 +++
 util/grub-mkimagexx.c  | 268 
 util/grub-mknetdir.c   |   4 +-
 util/grub-mkrescue.c   |  16 +-
 util/grub-module-verifier.c|  56 ++
 util/mkimage.c |  32 
 51 files changed, 1891 insertions(+), 39 deletions(-)
 create mode 100644 grub-core/kern/riscv/cache.c
 create mode 100644 grub-core/kern/riscv/cache_flush.S
 create mode 100644 grub-core/kern/riscv/dl.c
 create mode 100644 grub-core/kern/riscv/efi/init.c
 create mode 100644 grub-core/kern/riscv/efi/startup.S
 create mode 100644 grub-core/lib/riscv/setjmp.S
 create mode 100644 grub-core/loader/riscv/linux.c
 create mode 100644 include/grub/riscv32/efi/memory.h
 create mode 100644 include/grub/riscv32/linux.h
 create mode 100644 include/grub/riscv32/setjmp.h
 create mode 100644 include/grub/riscv32/time.h
 create mode 100644 include/grub/riscv32/types.h
 create mode 100644 include/grub/riscv64/efi/memory.h
 create mode 100644 include/grub/riscv64/linux.h
 create mode 100644 include/grub/riscv64/setjmp.h
 create mode 100644 include/grub/riscv64/time.h
 create mode 100644 include/grub/riscv64/types.h

-- 
2.12.3


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


[PATCH v4 01/10] efi: Rename armxx to arch

2018-11-25 Thread Alexander Graf
Some architectures want to boot Linux as plain UEFI binary. Today that
really only encompasses ARM and AArch64, but going forward more
architectures may adopt that model.

So rename our internal API accordingly.

Signed-off-by: Alexander Graf 
Acked-by: Leif Lindholm 
Reviewed-by: Alistair Francis 
Reviewed-by: Bin Meng 
Tested-by: Bin Meng 

---

v3 -> v4:

  - Rebase
---
 grub-core/loader/arm64/linux.c| 10 +-
 grub-core/loader/arm64/xen_boot.c |  6 +++---
 include/grub/arm/linux.h  |  2 +-
 include/grub/arm64/linux.h|  2 +-
 include/grub/efi/efi.h|  4 ++--
 5 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c
index c37295c0b..35891cc1c 100644
--- a/grub-core/loader/arm64/linux.c
+++ b/grub-core/loader/arm64/linux.c
@@ -49,7 +49,7 @@ static grub_addr_t initrd_start;
 static grub_addr_t initrd_end;
 
 grub_err_t
-grub_armxx_efi_linux_check_image (struct linux_armxx_kernel_header * lh)
+grub_arch_efi_linux_check_image (struct linux_arch_kernel_header * lh)
 {
   if (lh->magic != GRUB_LINUX_ARMXX_MAGIC_SIGNATURE)
 return grub_error(GRUB_ERR_BAD_OS, "invalid magic number");
@@ -110,7 +110,7 @@ failure:
 }
 
 grub_err_t
-grub_armxx_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char 
*args)
+grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
 {
   grub_efi_memory_mapped_device_path_t *mempath;
   grub_efi_handle_t image_handle;
@@ -173,7 +173,7 @@ grub_linux_boot (void)
   if (finalize_params_linux () != GRUB_ERR_NONE)
 return grub_errno;
 
-  return (grub_armxx_efi_linux_boot_image((grub_addr_t)kernel_addr,
+  return (grub_arch_efi_linux_boot_image((grub_addr_t)kernel_addr,
   kernel_size, linux_args));
 }
 
@@ -287,7 +287,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
 {
   grub_file_t file = 0;
-  struct linux_armxx_kernel_header lh;
+  struct linux_arch_kernel_header lh;
   grub_err_t err;
 
   grub_dl_ref (my_mod);
@@ -307,7 +307,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
   if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
 return grub_errno;
 
-  if (grub_armxx_efi_linux_check_image (&lh) != GRUB_ERR_NONE)
+  if (grub_arch_efi_linux_check_image (&lh) != GRUB_ERR_NONE)
 goto fail;
 
   grub_loader_unset();
diff --git a/grub-core/loader/arm64/xen_boot.c 
b/grub-core/loader/arm64/xen_boot.c
index 33a855df4..5cab48b60 100644
--- a/grub-core/loader/arm64/xen_boot.c
+++ b/grub-core/loader/arm64/xen_boot.c
@@ -265,7 +265,7 @@ xen_boot (void)
   if (err)
 return err;
 
-  return grub_armxx_efi_linux_boot_image (xen_hypervisor->start,
+  return grub_arch_efi_linux_boot_image (xen_hypervisor->start,
  xen_hypervisor->size,
  xen_hypervisor->cmdline);
 }
@@ -469,8 +469,8 @@ grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ 
((unused)),
 
   if (grub_file_read (file, &sh, sizeof (sh)) != (long) sizeof (sh))
 goto fail;
-  if (grub_armxx_efi_linux_check_image
-  ((struct linux_armxx_kernel_header *) &sh) != GRUB_ERR_NONE)
+  if (grub_arch_efi_linux_check_image
+  ((struct linux_arch_kernel_header *) &sh) != GRUB_ERR_NONE)
 goto fail;
   grub_file_seek (file, 0);
 
diff --git a/include/grub/arm/linux.h b/include/grub/arm/linux.h
index 712ba17b9..995800126 100644
--- a/include/grub/arm/linux.h
+++ b/include/grub/arm/linux.h
@@ -36,7 +36,7 @@ struct linux_arm_kernel_header {
 
 #if defined(__arm__)
 # define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM_MAGIC_SIGNATURE
-# define linux_armxx_kernel_header linux_arm_kernel_header
+# define linux_arch_kernel_header linux_arm_kernel_header
 #endif
 
 #if defined GRUB_MACHINE_UBOOT
diff --git a/include/grub/arm64/linux.h b/include/grub/arm64/linux.h
index 8655067e0..4269adc6d 100644
--- a/include/grub/arm64/linux.h
+++ b/include/grub/arm64/linux.h
@@ -38,7 +38,7 @@ struct linux_arm64_kernel_header
 
 #if defined(__aarch64__)
 # define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM64_MAGIC_SIGNATURE
-# define linux_armxx_kernel_header linux_arm64_kernel_header
+# define linux_arch_kernel_header linux_arm64_kernel_header
 #endif
 
 #endif /* ! GRUB_ARM64_LINUX_HEADER */
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
index 2c6648d46..ec44aef7e 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -94,8 +94,8 @@ extern void (*EXPORT_VAR(grub_efi_net_config)) 
(grub_efi_handle_t hnd,
 void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void);
 grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *);
 #include 
-grub_err_t grub_armxx_efi_linux_check_image(struct linux_armxx_kernel_header 
*lh);
-grub_err_t grub_armxx_efi_linux_boot_image(grub_addr_t addr, grub_size_t size,
+grub_err_t grub_arch_efi_linux_check_image(struct linux_arch_kernel_

[PATCH v4 10/10] fdt: Treat device tree file type like ACPI

2018-11-25 Thread Alexander Graf
We now have signature check logic in grub which allows us to treat
files differently depending on their file type.

Treat a loaded device tree like an overlayed ACPI table.
Both describe hardware, so I suppose their threat level is the same.

Signed-off-by: Alexander Graf 

---

v3 -> v4:

  - Rebase onto current git master
---
 grub-core/commands/efi/shim_lock.c | 1 +
 include/grub/file.h| 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/grub-core/commands/efi/shim_lock.c 
b/grub-core/commands/efi/shim_lock.c
index 01246b0fc..83568cb2b 100644
--- a/grub-core/commands/efi/shim_lock.c
+++ b/grub-core/commands/efi/shim_lock.c
@@ -81,6 +81,7 @@ shim_lock_init (grub_file_t io, enum grub_file_type type,
   /* Fall through. */
 
 case GRUB_FILE_TYPE_ACPI_TABLE:
+case GRUB_FILE_TYPE_DEVICE_TREE_IMAGE:
   *flags = GRUB_VERIFY_FLAGS_DEFER_AUTH;
 
   return GRUB_ERR_NONE;
diff --git a/include/grub/file.h b/include/grub/file.h
index 9aae46355..8c9bf5e5d 100644
--- a/include/grub/file.h
+++ b/include/grub/file.h
@@ -69,8 +69,6 @@ enum grub_file_type
 
 GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE,
 
-GRUB_FILE_TYPE_DEVICE_TREE_IMAGE,
-
 /* File holding signature.  */
 GRUB_FILE_TYPE_SIGNATURE,
 /* File holding public key to verify signature once.  */
@@ -95,6 +93,8 @@ enum grub_file_type
 GRUB_FILE_TYPE_FILE_ID,
 /* File holding ACPI table.  */
 GRUB_FILE_TYPE_ACPI_TABLE,
+/* File holding Device Tree.  */
+GRUB_FILE_TYPE_DEVICE_TREE_IMAGE,
 /* File we intend show to user.  */
 GRUB_FILE_TYPE_CAT,
 GRUB_FILE_TYPE_HEXCAT,
-- 
2.12.3


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


[PATCH v4 04/10] RISC-V: Add setjmp implementation

2018-11-25 Thread Alexander Graf
This patch adds a 32/64 capable setjmp implementation for RISC-V.

Signed-off-by: Alexander Graf 
Reviewed-by: Alistair Francis 
Reviewed-by: Bin Meng 
Tested-by: Bin Meng 

---

v3 -> v4:

  - Change copyright from 2013 to 2018
---
 grub-core/lib/riscv/setjmp.S  | 82 +++
 include/grub/riscv32/setjmp.h | 27 ++
 include/grub/riscv64/setjmp.h | 27 ++
 3 files changed, 136 insertions(+)
 create mode 100644 grub-core/lib/riscv/setjmp.S
 create mode 100644 include/grub/riscv32/setjmp.h
 create mode 100644 include/grub/riscv64/setjmp.h

diff --git a/grub-core/lib/riscv/setjmp.S b/grub-core/lib/riscv/setjmp.S
new file mode 100644
index 0..a27a39fae
--- /dev/null
+++ b/grub-core/lib/riscv/setjmp.S
@@ -0,0 +1,82 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see .
+ */
+
+#include 
+#include 
+
+   .file   "setjmp.S"
+GRUB_MOD_LICENSE "GPLv3+"
+   .text
+
+#if __riscv_xlen == 64
+#define STORE_IDX(reg, idx) sd reg, (idx*8)(a0)
+#define LOAD_IDX(reg, idx)  ld reg, (idx*8)(a0)
+#else
+#define STORE_IDX(reg, idx) sw reg, (idx*4)(a0)
+#define LOAD_IDX(reg, idx)  lw reg, (idx*4)(a0)
+#endif
+
+/*
+ * int grub_setjmp (grub_jmp_buf env)
+ */
+FUNCTION(grub_setjmp)
+/* Preserve all callee-saved registers and the SP */
+STORE_IDX(s0, 0)
+STORE_IDX(s1, 1)
+STORE_IDX(s2, 2)
+STORE_IDX(s3, 3)
+STORE_IDX(s4, 4)
+STORE_IDX(s5, 5)
+STORE_IDX(s6, 6)
+STORE_IDX(s7, 7)
+STORE_IDX(s8, 8)
+STORE_IDX(s9, 9)
+STORE_IDX(s10, 10)
+STORE_IDX(s11, 11)
+STORE_IDX(ra, 12)
+STORE_IDX(sp, 13)
+li  a0, 0
+ret
+
+/*
+ * int grub_longjmp (grub_jmp_buf env, int val)
+ */
+FUNCTION(grub_longjmp)
+LOAD_IDX(s0, 0)
+LOAD_IDX(s1, 1)
+LOAD_IDX(s2, 2)
+LOAD_IDX(s3, 3)
+LOAD_IDX(s4, 4)
+LOAD_IDX(s5, 5)
+LOAD_IDX(s6, 6)
+LOAD_IDX(s7, 7)
+LOAD_IDX(s8, 8)
+LOAD_IDX(s9, 9)
+LOAD_IDX(s10, 10)
+LOAD_IDX(s11, 11)
+LOAD_IDX(ra, 12)
+LOAD_IDX(sp, 13)
+
+/* Move the return value in place, but return 1 if passed 0. */
+beq a1, zero, longjmp_1
+mv a0, a1
+ret
+
+longjmp_1:
+li a0, 1
+ret
diff --git a/include/grub/riscv32/setjmp.h b/include/grub/riscv32/setjmp.h
new file mode 100644
index 0..5a2123846
--- /dev/null
+++ b/include/grub/riscv32/setjmp.h
@@ -0,0 +1,27 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see .
+ */
+
+#ifndef GRUB_SETJMP_CPU_HEADER
+#define GRUB_SETJMP_CPU_HEADER 1
+
+typedef unsigned long long grub_jmp_buf[14];
+
+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 */
diff --git a/include/grub/riscv64/setjmp.h b/include/grub/riscv64/setjmp.h
new file mode 100644
index 0..5a2123846
--- /dev/null
+++ b/include/grub/riscv64/setjmp.h
@@ -0,0 +1,27 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  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 FO

[PATCH v4 08/10] RISC-V: Add auxiliary files

2018-11-25 Thread Alexander Graf
To support a new architecture we need to provide a few helper functions
for memory, cache, timer, etc support.

This patch adds the remainders of those. Some bits are still disabled,
as I couldn't guarantee that we're always running on models / in modes
where the respective hardware is available.

Signed-off-by: Alexander Graf 

---

v2 -> v3:

  - Fix riscv32 target

v3 -> v4:

  - Change copyright from 2013 to 2018
  - Fix coding style
  - Resurrect time reading code
  - Add fence.i for icache flush
---
 grub-core/kern/riscv/cache.c   | 64 +++
 grub-core/kern/riscv/cache_flush.S | 44 ++
 grub-core/kern/riscv/efi/init.c| 77 ++
 include/grub/riscv32/efi/memory.h  |  6 +++
 include/grub/riscv32/time.h| 28 ++
 include/grub/riscv32/types.h   | 34 +
 include/grub/riscv64/efi/memory.h  |  6 +++
 include/grub/riscv64/time.h| 28 ++
 include/grub/riscv64/types.h   | 34 +
 9 files changed, 321 insertions(+)
 create mode 100644 grub-core/kern/riscv/cache.c
 create mode 100644 grub-core/kern/riscv/cache_flush.S
 create mode 100644 grub-core/kern/riscv/efi/init.c
 create mode 100644 include/grub/riscv32/efi/memory.h
 create mode 100644 include/grub/riscv32/time.h
 create mode 100644 include/grub/riscv32/types.h
 create mode 100644 include/grub/riscv64/efi/memory.h
 create mode 100644 include/grub/riscv64/time.h
 create mode 100644 include/grub/riscv64/types.h

diff --git a/grub-core/kern/riscv/cache.c b/grub-core/kern/riscv/cache.c
new file mode 100644
index 0..7e04d2e06
--- /dev/null
+++ b/grub-core/kern/riscv/cache.c
@@ -0,0 +1,64 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see .
+ */
+
+#include 
+#include 
+
+static grub_int64_t dlinesz;
+static grub_int64_t ilinesz;
+
+/* Prototypes for asm functions. */
+void grub_arch_clean_dcache_range (grub_addr_t beg, grub_addr_t end,
+  grub_size_t line_size);
+void grub_arch_invalidate_icache_range (grub_addr_t beg, grub_addr_t end,
+   grub_size_t line_size);
+
+static void
+probe_caches (void)
+{
+  /* TODO */
+  dlinesz = 32;
+  ilinesz = 32;
+}
+
+void
+grub_arch_sync_caches (void *address, grub_size_t len)
+{
+  grub_size_t start, end, max_align;
+
+  if (dlinesz == 0)
+probe_caches();
+  if (dlinesz == 0)
+grub_fatal ("Unknown cache line size!");
+
+  max_align = dlinesz > ilinesz ? dlinesz : ilinesz;
+
+  start = ALIGN_DOWN ((grub_size_t) address, max_align);
+  end = ALIGN_UP ((grub_size_t) address + len, max_align);
+
+  grub_arch_clean_dcache_range (start, end, dlinesz);
+  grub_arch_invalidate_icache_range (start, end, ilinesz);
+}
+
+void
+grub_arch_sync_dma_caches (volatile void *address, grub_size_t len)
+{
+  /* DMA incoherent devices not supported yet */
+  if (address || len || 1)
+return;
+}
diff --git a/grub-core/kern/riscv/cache_flush.S 
b/grub-core/kern/riscv/cache_flush.S
new file mode 100644
index 0..41de6e411
--- /dev/null
+++ b/grub-core/kern/riscv/cache_flush.S
@@ -0,0 +1,44 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see .
+ */
+
+#include 
+
+   .file   "cache_flush.S"
+   .text
+
+/*
+ * Simple cache maintenance functions
+ */
+
+/*
+ * a0 - *beg (inclusive)
+ * a1 - *end (exclusive)
+ * a2 - line size
+*/
+FUNCTION(grub_arch_clean_dcache_range)
+   /* TODO */
+   ret
+
+/*
+ * a0 - *beg (inclusive)
+ * a1 - *end (exclusive)
+ * a2 - line size
+ */
+FUNCTION(grub_arch_invalidate_icache_range

[PATCH v4 06/10] RISC-V: Add Linux load logic

2018-11-25 Thread Alexander Graf
We currently only support to run grub on RISC-V as UEFI payload. Ideally,
we also only want to support running Linux underneath as UEFI payload.

Prepare that with a Linux boot case that is not enabled in Linux yet. At
least it will give people something to test against when they enable the
Linux UEFI port.

Signed-off-by: Alexander Graf 
Reviewed-by: Alistair Francis 

---

v1 -> v2:

  - adapt to new grub_open_file() API
  - adapt to new grub_create_loader_cmdline() API

v3 -> v4:

  - Change copyright from 2013 to 2018
  - Coding style fixes
---
 grub-core/loader/riscv/linux.c | 351 +
 include/grub/riscv32/linux.h   |  41 +
 include/grub/riscv64/linux.h   |  43 +
 3 files changed, 435 insertions(+)
 create mode 100644 grub-core/loader/riscv/linux.c
 create mode 100644 include/grub/riscv32/linux.h
 create mode 100644 include/grub/riscv64/linux.h

diff --git a/grub-core/loader/riscv/linux.c b/grub-core/loader/riscv/linux.c
new file mode 100644
index 0..fc8c508c8
--- /dev/null
+++ b/grub-core/loader/riscv/linux.c
@@ -0,0 +1,351 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see .
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#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;
+
+grub_err_t
+grub_arch_efi_linux_check_image (struct linux_riscv_kernel_header * lh)
+{
+  if (lh->magic != GRUB_LINUX_RISCV_MAGIC_SIGNATURE)
+return grub_error(GRUB_ERR_BAD_OS, "invalid magic number");
+
+  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);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+finalize_params_linux (void)
+{
+  int node, retval;
+
+  void *fdt;
+
+  fdt = grub_fdt_load (0x400);
+
+  if (!fdt)
+goto failure;
+
+  node = grub_fdt_find_subnode (fdt, 0, "chosen");
+  if (node < 0)
+node = grub_fdt_add_subnode (fdt, 0, "chosen");
+
+  if (node < 1)
+goto failure;
+
+  /* Set initrd info */
+  if (initrd_start && initrd_end > initrd_start)
+{
+  grub_dprintf ("linux", "Initrd @ %p-%p\n",
+   (void *) initrd_start, (void *) initrd_end);
+
+  retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start",
+   initrd_start);
+  if (retval)
+   goto failure;
+  retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end",
+   initrd_end);
+  if (retval)
+   goto failure;
+}
+
+  if (grub_fdt_install() != GRUB_ERR_NONE)
+goto failure;
+
+  return GRUB_ERR_NONE;
+
+ failure:
+  grub_fdt_unload();
+  return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT");
+}
+
+grub_err_t
+grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
+{
+  grub_efi_memory_mapped_device_path_t *mempath;
+  grub_efi_handle_t image_handle;
+  grub_efi_boot_services_t *b;
+  grub_efi_status_t status;
+  grub_efi_loaded_image_t *loaded_image;
+  int len;
+
+  mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t));
+  if (!mempath)
+return grub_errno;
+
+  mempath[0].header.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE;
+  mempath[0].header.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE;
+  mempath[0].header.length = grub_cpu_to_le16_compile_time (sizeof (*mempath));
+  mempath[0].memory_type = GRUB_EFI_LOADER_DATA;
+  mempath[0].start_address = addr;
+  mempath[0].end_address = addr + size;
+
+  mempath[1].header.type = GRUB_EFI_END_DEVICE_PATH_TYPE;
+  mempath[1].header.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
+  mempath[1].header.length = sizeof (grub_efi_device_path_t);
+
+  b = grub_efi_system_table->boot_services;
+  status = b->load_image (0, grub_efi_image_handle,
+  

[PATCH v4 09/10] RISC-V: Add to build system

2018-11-25 Thread Alexander Graf
This patch adds support for RISC-V to the grub build system. With this
patch, I can successfully build grub on RISC-V as a UEFI application.

Signed-off-by: Alexander Graf 
Reviewed-by: Alistair Francis 
Reviewed-by: Bin Meng 
Tested-by: Bin Meng 

---

v2 -> v3:

  - Fix riscv32 target
---
 configure.ac | 28 ++--
 gentpl.py| 11 +++
 grub-core/Makefile.am| 12 
 grub-core/Makefile.core.def  | 29 +
 grub-core/commands/file.c| 14 +-
 grub-core/kern/compiler-rt.c |  6 --
 grub-core/kern/efi/mm.c  |  2 +-
 grub-core/kern/emu/cache.c   |  6 ++
 grub-core/kern/emu/cache_s.S |  1 +
 grub-core/kern/emu/lite.c|  2 ++
 grub-core/lib/efi/halt.c |  3 ++-
 grub-core/lib/setjmp.S   |  2 ++
 include/grub/compiler-rt.h   | 12 
 include/grub/efi/api.h   |  3 ++-
 include/grub/efi/efi.h   |  2 +-
 include/grub/misc.h  |  3 ++-
 include/grub/util/install.h  |  2 ++
 util/grub-install-common.c   |  2 ++
 util/grub-install.c  | 28 
 util/grub-mknetdir.c |  4 +++-
 util/grub-mkrescue.c | 16 +++-
 util/mkimage.c   | 32 
 22 files changed, 200 insertions(+), 20 deletions(-)

diff --git a/configure.ac b/configure.ac
index 5e63c4af3..2cc2ffb2c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -104,6 +104,12 @@ case "$target_cpu" in
   aarch64*)
target_cpu=arm64
;;
+  riscv32*)
+   target_cpu=riscv32
+   ;;
+  riscv64*)
+   target_cpu=riscv64
+   ;;
 esac
 
 # Specify the platform (such as firmware).
@@ -127,6 +133,8 @@ if test "x$with_platform" = x; then
 ia64-*) platform=efi ;;
 arm-*) platform=uboot ;;
 arm64-*) platform=efi ;;
+riscv32-*) platform=efi ;;
+riscv64-*) platform=efi ;;
 *)
   AC_MSG_WARN([unsupported CPU: "$target_cpu" - only building utilities])
   platform=none
@@ -174,6 +182,8 @@ case "$target_cpu"-"$platform" in
   arm-coreboot) ;;
   arm-efi) ;;
   arm64-efi) ;;
+  riscv32-efi) ;;
+  riscv64-efi) ;;
   *-emu) ;;
   *-none) ;;
   *) AC_MSG_ERROR([platform "$platform" is not supported for target CPU 
"$target_cpu"]) ;;
@@ -826,6 +836,16 @@ if test x"$platform" != xemu ; then
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
 [grub_cv_target_cc_soft_float="-mgeneral-regs-only"], 
[])
 fi
+if test "x$target_cpu" = xriscv32; then
+   CFLAGS="$TARGET_CFLAGS -march=rv32imac -mabi=ilp32 -Werror"
+   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
+[grub_cv_target_cc_soft_float="-march=rv32imac 
-mabi=ilp32"], [])
+fi
+if test "x$target_cpu" = xriscv64; then
+   CFLAGS="$TARGET_CFLAGS -march=rv64imac -mabi=lp64 -Werror"
+   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
+[grub_cv_target_cc_soft_float="-march=rv64imac 
-mabi=lp64"], [])
+fi
 if test "x$target_cpu" = xia64; then
CFLAGS="$TARGET_CFLAGS -mno-inline-float-divide -mno-inline-sqrt 
-Werror"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
@@ -1141,7 +1161,7 @@ AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)
 
 LDFLAGS="$TARGET_LDFLAGS"
 
-if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 ; 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"
@@ -1151,7 +1171,7 @@ if test "$target_cpu" = x86_64 || test "$target_cpu" = 
sparc64 ; then
   ])
   if test "x$grub_cv_cc_mcmodel" = xyes; then
 TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=large"
-  elif test "$target_cpu" = sparc64; then
+  elif test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64; then
 TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
   fi
 fi
@@ -1913,6 +1933,10 @@ AM_CONDITIONAL([COND_arm_coreboot], [test x$target_cpu = 
xarm -a x$platform = xc
 AM_CONDITIONAL([COND_arm_efi], [test x$target_cpu = xarm -a x$platform = xefi])
 AM_CONDITIONAL([COND_arm64], [test x$target_cpu = xarm64 ])
 AM_CONDITIONAL([COND_arm64_efi], [test x$target_cpu = xarm64 -a x$platform = 
xefi])
+AM_CONDITIONAL([COND_riscv32], [test x$target_cpu = xriscv32 ])
+AM_CONDITIONAL([COND_riscv64], [test x$target_cpu = xriscv64 ])
+AM_CONDITIONAL([COND_riscv32_efi], [test x$target_cpu = xriscv32 -a x$platform 
= xefi])
+AM_CONDITIONAL([COND_riscv64_efi], [test x$target_cpu = xriscv64 -a x$platform 
= xefi])
 
 AM_CONDITIONAL([COND_HOST_HURD], [test x$host_kernel = xhurd])
 AM_CONDITIONAL([COND_HOST_LINUX], [test x$host_kernel = xlinux])
diff --git a/gentpl.py b/gentpl.py
index da67965a4..996fda9d5 100644
--- a/gentpl.py
+++ b/gentpl.py
@@ -32,7 +32,7 @@ GRUB_PLATFORMS = [ "emu", "i

[PATCH v4 07/10] RISC-V: Add awareness for RISC-V reloations

2018-11-25 Thread Alexander Graf
This patch adds awareness of RISC-V relocations throughout the grub tools
as well as dynamic linkage and elf->PE relocation conversion support.

Signed-off-by: Alexander Graf 

---

v2 -> v3:

  - Fix riscv32 target

v3 -> v4:

  - Change copyright from 2013 to 2018
  - Add spec reference
---
 grub-core/kern/dl.c |   6 +-
 grub-core/kern/riscv/dl.c   | 340 
 include/grub/dl.h   |   6 +-
 util/grub-mkimagexx.c   | 268 ++
 util/grub-module-verifier.c |  56 
 5 files changed, 671 insertions(+), 5 deletions(-)
 create mode 100644 grub-core/kern/riscv/dl.c

diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
index f8d58f029..48eb5e7b6 100644
--- a/grub-core/kern/dl.c
+++ b/grub-core/kern/dl.c
@@ -225,7 +225,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
   unsigned i;
   const Elf_Shdr *s;
   grub_size_t tsize = 0, talign = 1;
-#if !defined (__i386__) && !defined (__x86_64__)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
   grub_size_t tramp;
   grub_size_t got;
   grub_err_t err;
@@ -241,7 +241,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
talign = s->sh_addralign;
 }
 
-#if !defined (__i386__) && !defined (__x86_64__)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
   err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
   if (err)
 return err;
@@ -304,7 +304,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
  mod->segment = seg;
}
 }
-#if !defined (__i386__) && !defined (__x86_64__)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   mod->trampptr = ptr;
diff --git a/grub-core/kern/riscv/dl.c b/grub-core/kern/riscv/dl.c
new file mode 100644
index 0..6fb8385ef
--- /dev/null
+++ b/grub-core/kern/riscv/dl.c
@@ -0,0 +1,340 @@
+/* dl.c - arch-dependent part of loadable module support */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see .
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/*
+ * Instructions and instruction encoding are documented in the RISC-V
+ * specification. This file is based on version 2.2:
+ *
+ * 
https://github.com/riscv/riscv-isa-manual/blob/master/release/riscv-spec-v2.2.pdf
+ */
+#define LDR 0x5850
+#define BR 0xd61f0200
+
+/*
+ * 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_DATA] != ELFDATA2LSB || e->e_machine != EM_RISCV)
+return grub_error (GRUB_ERR_BAD_OS,
+  N_("invalid arch-dependent ELF magic"));
+
+  return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/* Relocate symbols. */
+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;
+
+  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;
+  void *place;
+  grub_size_t sym_addr;
+
+  if (rel->r_offset >= seg->size)
+   return grub_error (GRUB_ERR_BAD_MODULE,
+  "reloc offset is out of 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 = (void *) ((grub_addr_t) seg->addr + rel->r_offset);
+
+  switch (ELF_R_TYPE (rel->r_info))
+   {
+   case R_RISCV_32:
+ {
+   grub_uint32_t *abs_place = place;
+
+   grub_dprintf ("dl", "  reloc_abs32 %p => 0x%016llx\n",
+ place, (unsigned long long) sym_addr);
+
+   *abs_place = (grub_uint32_t) sym_addr;
+ }
+ break;
+   case R_RISCV_64:
+ {
+   grub_size_t *abs_place = place;
+
+   grub_dprintf ("dl", "  reloc_abs64 %p => 

Re: [PATCH v3 09/10] RISC-V: Add to build system

2018-11-25 Thread Bin Meng
Hi Alex,

On Mon, Nov 26, 2018 at 7:06 AM Alexander Graf  wrote:
>
>
>
> On 18.11.18 12:41, Bin Meng wrote:
> > Hi Alex,
> >
> > On Thu, Nov 15, 2018 at 1:27 AM Alexander Graf  wrote:
> >>
> >> This patch adds support for RISC-V to the grub build system. With this
> >> patch, I can successfully build grub on RISC-V as a UEFI application.
> >>
> >> Signed-off-by: Alexander Graf 
> >> Reviewed-by: Alistair Francis 
> >>
> >> ---
> >>
> >> v2 -> v3:
> >>
> >>   - Fix riscv32 target
> >> ---
> >>  configure.ac | 28 ++--
> >>  gentpl.py| 11 +++
> >>  grub-core/Makefile.am| 12 
> >>  grub-core/Makefile.core.def  | 29 +
> >>  grub-core/commands/file.c| 14 +-
> >>  grub-core/kern/compiler-rt.c |  6 --
> >>  grub-core/kern/efi/mm.c  |  2 +-
> >>  grub-core/kern/emu/cache.c   |  6 ++
> >>  grub-core/kern/emu/cache_s.S |  1 +
> >>  grub-core/kern/emu/lite.c|  2 ++
> >>  grub-core/lib/efi/halt.c |  3 ++-
> >>  grub-core/lib/setjmp.S   |  2 ++
> >>  include/grub/compiler-rt.h   | 12 
> >>  include/grub/efi/api.h   |  3 ++-
> >>  include/grub/efi/efi.h   |  2 +-
> >>  include/grub/misc.h  |  3 ++-
> >>  include/grub/util/install.h  |  2 ++
> >>  util/grub-install-common.c   |  2 ++
> >>  util/grub-install.c  | 28 
> >>  util/grub-mknetdir.c |  4 +++-
> >>  util/grub-mkrescue.c | 16 +++-
> >>  util/mkimage.c   | 32 
> >>  22 files changed, 200 insertions(+), 20 deletions(-)
> >>
> >
> > Reviewed-by: Bin Meng 
> > Tested-by: Bin Meng 
> >
> > But please see one comment below:
> >
> >> diff --git a/configure.ac b/configure.ac
> >> index 5e63c4af3..2cc2ffb2c 100644
> >> --- a/configure.ac
> >> +++ b/configure.ac
> >> @@ -104,6 +104,12 @@ case "$target_cpu" in
> >>aarch64*)
> >> target_cpu=arm64
> >> ;;
> >> +  riscv32*)
> >> +   target_cpu=riscv32
> >> +   ;;
> >> +  riscv64*)
> >> +   target_cpu=riscv64
> >> +   ;;
> >>  esac
> >>
> >>  # Specify the platform (such as firmware).
> >> @@ -127,6 +133,8 @@ if test "x$with_platform" = x; then
> >>  ia64-*) platform=efi ;;
> >>  arm-*) platform=uboot ;;
> >>  arm64-*) platform=efi ;;
> >> +riscv32-*) platform=efi ;;
> >> +riscv64-*) platform=efi ;;
> >>  *)
> >>AC_MSG_WARN([unsupported CPU: "$target_cpu" - only building 
> >> utilities])
> >>platform=none
> >> @@ -174,6 +182,8 @@ case "$target_cpu"-"$platform" in
> >>arm-coreboot) ;;
> >>arm-efi) ;;
> >>arm64-efi) ;;
> >> +  riscv32-efi) ;;
> >> +  riscv64-efi) ;;
> >>*-emu) ;;
> >>*-none) ;;
> >>*) AC_MSG_ERROR([platform "$platform" is not supported for target CPU 
> >> "$target_cpu"]) ;;
> >> @@ -826,6 +836,16 @@ if test x"$platform" != xemu ; then
> >> AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
> >>  
> >> [grub_cv_target_cc_soft_float="-mgeneral-regs-only"], [])
> >>  fi
> >> +if test "x$target_cpu" = xriscv32; then
> >> +   CFLAGS="$TARGET_CFLAGS -march=rv32imac -mabi=ilp32 -Werror"
> >> +   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
> >> +[grub_cv_target_cc_soft_float="-march=rv32imac 
> >> -mabi=ilp32"], [])
> >> +fi
> >> +if test "x$target_cpu" = xriscv64; then
> >> +   CFLAGS="$TARGET_CFLAGS -march=rv64imac -mabi=lp64 -Werror"
> >> +   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
> >> +[grub_cv_target_cc_soft_float="-march=rv64imac 
> >> -mabi=lp64"], [])
> >> +fi
> >>  if test "x$target_cpu" = xia64; then
> >> CFLAGS="$TARGET_CFLAGS -mno-inline-float-divide -mno-inline-sqrt 
> >> -Werror"
> >> AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
> >> @@ -1141,7 +1161,7 @@ AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)
> >>
> >>  LDFLAGS="$TARGET_LDFLAGS"
> >>
> >> -if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 ; 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"
> >> @@ -1151,7 +1171,7 @@ if test "$target_cpu" = x86_64 || test "$target_cpu" 
> >> = sparc64 ; then
> >>])
> >>if test "x$grub_cv_cc_mcmodel" = xyes; then
> >>  TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=large"
> >> -  elif test "$target_cpu" = sparc64; then
> >> +  elif test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64; then
> >>  TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
> >
> > The patch does not indicate the code model explicitly for riscv32.
> > It's better we pass the -mcmodel=medlow (suitable 

Re: [PATCH V2 2/3] verifiers: Core TPM support

2018-11-25 Thread Matthew Garrett
On Tue, Nov 20, 2018 at 10:59 AM Matthew Garrett  wrote:
>
> On Mon, Nov 19, 2018 at 1:13 AM Daniel P. Smith  wrote:
> >
> > It would be great if the TPM commands that are using EFI protocol and
> > exposed to TPM command module be name spaced under efi, e.g.
> > grub_efi_tpm_log_event. As I lay in a TIS implementation, I can mimic a
> > similar set of tis name spaced functions that can be #ifdef/#else (or
> > any other mechanism GRUB maintainer's would prefer) switched between EFI
> > and TIS.
>
> I'm a little confused - if it's #ifdefed then surely there's no
> namespace collision (because only one implementation can be built at
> once)? If the goal is to allow one binary to support multiple
> implementations then that's not impossible, but it's going to require
> runtime registration of TPM callbacks rather than simply namespacing
> stuff.

Hey Daniel - just wanted to check in on this?

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


Re: [PATCH V2 2/3] verifiers: Core TPM support

2018-11-25 Thread Matthew Garrett
On Mon, Nov 26, 2018 at 4:47 PM Daniel Kiper  wrote:

> I have a feeling that both UEFI and TIS TPM implementations can coexists
> together even on UEFI platform. Of course, AIUI, UEFI TPM should be default
> if we play with TPM 2.0. TIS implementation should be used with TPM 1.2
> or if UEFI is buggy and its hooks does not work well with TPM 2.0.
> Does it make sense?

The implementation here should work fine with TPM 1.2, so the only
reason to avoid the UEFI implementation is if it's buggy - but given
current versions of Windows will use the TPM by default if it's
available, I'd be surprised if there's any significant bugs in the
wild. I'm not sure there's a real case where you'd have a TPM on a
UEFI system without having working firmware support, and if you don't
have the firmware support I don't know how you'd discover the TPM in
the first place (eg, if it's on SPI we'd need a full SPI
implementation in grub to talk to it)

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