Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-05-22 Thread Alexandre Ghiti



On 19/05/2023 23:55, Palmer Dabbelt wrote:

On Fri, 19 May 2023 14:48:59 PDT (-0700), sch...@linux-m68k.org wrote:

On Mai 19 2023, Alexandre Ghiti wrote:


I have tested the following patch successfully, can you give it a try
while I make sure this is the only place I forgot to add the -fno-pie
flag?

diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index fbdccc21418a..153864e4f399 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -23,6 +23,10 @@ ifdef CONFIG_FTRACE
 CFLAGS_REMOVE_alternative.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_cpufeature.o = $(CC_FLAGS_FTRACE)
 endif
+ifdef CONFIG_RELOCATABLE
+CFLAGS_alternative.o += -fno-pie
+CFLAGS_cpufeature.o += -fno-pie
+endif
 ifdef CONFIG_KASAN
 KASAN_SANITIZE_alternative.o := n
 KASAN_SANITIZE_cpufeature.o := n


I can confirm that this fixes the crash.


Thanks.  Alex: can you send a patch?



I don't think this patch alone will work, all the code in early 
alternatives must be compiled with -fno-pie, but I'm a bit scared that's 
a "big" constraint. For now, I see 2 solutions:


- Document somewhere the fact that anything called from early 
alternatives must be compiled with -fno-pie
- Or relocate once with physical address, call early alternatives, and 
then do the final virtual relocation


Both options can be cumbersome in their own way, if anyone has an 
opinion, I'd be happy to discuss that :)





Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-05-19 Thread Palmer Dabbelt

On Fri, 19 May 2023 14:48:59 PDT (-0700), sch...@linux-m68k.org wrote:

On Mai 19 2023, Alexandre Ghiti wrote:


I have tested the following patch successfully, can you give it a try
while I make sure this is the only place I forgot to add the -fno-pie
flag?

diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index fbdccc21418a..153864e4f399 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -23,6 +23,10 @@ ifdef CONFIG_FTRACE
 CFLAGS_REMOVE_alternative.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_cpufeature.o = $(CC_FLAGS_FTRACE)
 endif
+ifdef CONFIG_RELOCATABLE
+CFLAGS_alternative.o += -fno-pie
+CFLAGS_cpufeature.o += -fno-pie
+endif
 ifdef CONFIG_KASAN
 KASAN_SANITIZE_alternative.o := n
 KASAN_SANITIZE_cpufeature.o := n


I can confirm that this fixes the crash.


Thanks.  Alex: can you send a patch?


Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-05-19 Thread Andreas Schwab
On Mai 19 2023, Alexandre Ghiti wrote:

> I have tested the following patch successfully, can you give it a try
> while I make sure this is the only place I forgot to add the -fno-pie
> flag?
>
> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
> index fbdccc21418a..153864e4f399 100644
> --- a/arch/riscv/kernel/Makefile
> +++ b/arch/riscv/kernel/Makefile
> @@ -23,6 +23,10 @@ ifdef CONFIG_FTRACE
>  CFLAGS_REMOVE_alternative.o = $(CC_FLAGS_FTRACE)
>  CFLAGS_REMOVE_cpufeature.o = $(CC_FLAGS_FTRACE)
>  endif
> +ifdef CONFIG_RELOCATABLE
> +CFLAGS_alternative.o += -fno-pie
> +CFLAGS_cpufeature.o += -fno-pie
> +endif
>  ifdef CONFIG_KASAN
>  KASAN_SANITIZE_alternative.o := n
>  KASAN_SANITIZE_cpufeature.o := n

I can confirm that this fixes the crash.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."


Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-05-19 Thread Alexandre Ghiti

On 5/11/23 20:18, Andreas Schwab wrote:

On Mai 09 2023, Alexandre Ghiti wrote:


On 5/9/23 21:07, Andreas Schwab wrote:

That does not work with UEFI booting:

Loading Linux 6.4.0-rc1-1.g668187d-default ...
Loading initial ramdisk ...
Unhandled exception: Instruction access fault
EPC: 80016d56 RA: 8020334e TVAL: 007f80016d56
EPC: 002d1d56 RA: 004be34e reloc adjusted
Unhandled exception: Load access fault
EPC: fff462d4 RA: fff462d0 TVAL: 80016d56
EPC: 802012d4 RA: 802012d0 reloc adjusted

Code: c825 8e0d 05b3 40b4 d0ef 0636 7493 ffe4 (d783 0004)
UEFI image [0xfe65e000:0xfe6e3fff] '/efi\boot\bootriscv64.efi'
UEFI image [0xdaa82000:0xdcc2afff]


I need more details please, as I have a UEFI bootflow and it works great
(KASLR is based on a relocatable kernel and works fine in UEFI too).

It also crashes without UEFI.  Disabling CONFIG_RELOCATABLE fixes that.
This was tested on the HiFive Unmatched board.
The kernel image I tested is available from
.  The
same kernel with CONFIG_RELOCATABLE disabled is available from
.



I have tested the following patch successfully, can you give it a try 
while I make sure this is the only place I forgot to add the -fno-pie flag?


diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index fbdccc21418a..153864e4f399 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -23,6 +23,10 @@ ifdef CONFIG_FTRACE
 CFLAGS_REMOVE_alternative.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_cpufeature.o = $(CC_FLAGS_FTRACE)
 endif
+ifdef CONFIG_RELOCATABLE
+CFLAGS_alternative.o += -fno-pie
+CFLAGS_cpufeature.o += -fno-pie
+endif
 ifdef CONFIG_KASAN
 KASAN_SANITIZE_alternative.o := n
 KASAN_SANITIZE_cpufeature.o := n

Thanks

Alex



Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-05-12 Thread Palmer Dabbelt

On Thu, 11 May 2023 11:18:23 PDT (-0700), sch...@linux-m68k.org wrote:

On Mai 09 2023, Alexandre Ghiti wrote:


On 5/9/23 21:07, Andreas Schwab wrote:

That does not work with UEFI booting:

Loading Linux 6.4.0-rc1-1.g668187d-default ...
Loading initial ramdisk ...
Unhandled exception: Instruction access fault
EPC: 80016d56 RA: 8020334e TVAL: 007f80016d56
EPC: 002d1d56 RA: 004be34e reloc adjusted
Unhandled exception: Load access fault
EPC: fff462d4 RA: fff462d0 TVAL: 80016d56
EPC: 802012d4 RA: 802012d0 reloc adjusted

Code: c825 8e0d 05b3 40b4 d0ef 0636 7493 ffe4 (d783 0004)
UEFI image [0xfe65e000:0xfe6e3fff] '/efi\boot\bootriscv64.efi'
UEFI image [0xdaa82000:0xdcc2afff]



I need more details please, as I have a UEFI bootflow and it works great
(KASLR is based on a relocatable kernel and works fine in UEFI too).


It also crashes without UEFI.  Disabling CONFIG_RELOCATABLE fixes that.
This was tested on the HiFive Unmatched board.
The kernel image I tested is available from
.  The
same kernel with CONFIG_RELOCATABLE disabled is available from
.


Sorry I missed this earlier, there's been some other reports of boot 
failures on rc1 showing up but those were all a lot more vague.  Just 
setting CONFIG_RELOCATABLE=y doesn't manifest a boot failure on QEMU on 
my end and I don't have an UNmatched floating around.


Alex says he's going to look into it (and IIRC he has my Unmatched...).


Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-05-11 Thread Andreas Schwab
On Mai 09 2023, Alexandre Ghiti wrote:

> On 5/9/23 21:07, Andreas Schwab wrote:
>> That does not work with UEFI booting:
>>
>> Loading Linux 6.4.0-rc1-1.g668187d-default ...
>> Loading initial ramdisk ...
>> Unhandled exception: Instruction access fault
>> EPC: 80016d56 RA: 8020334e TVAL: 007f80016d56
>> EPC: 002d1d56 RA: 004be34e reloc adjusted
>> Unhandled exception: Load access fault
>> EPC: fff462d4 RA: fff462d0 TVAL: 80016d56
>> EPC: 802012d4 RA: 802012d0 reloc adjusted
>>
>> Code: c825 8e0d 05b3 40b4 d0ef 0636 7493 ffe4 (d783 0004)
>> UEFI image [0xfe65e000:0xfe6e3fff] 
>> '/efi\boot\bootriscv64.efi'
>> UEFI image [0xdaa82000:0xdcc2afff]
>>
>
> I need more details please, as I have a UEFI bootflow and it works great
> (KASLR is based on a relocatable kernel and works fine in UEFI too).

It also crashes without UEFI.  Disabling CONFIG_RELOCATABLE fixes that.
This was tested on the HiFive Unmatched board.
The kernel image I tested is available from
.  The
same kernel with CONFIG_RELOCATABLE disabled is available from
.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."


Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-05-09 Thread Alexandre Ghiti

On 5/9/23 21:07, Andreas Schwab wrote:

That does not work with UEFI booting:

Loading Linux 6.4.0-rc1-1.g668187d-default ...
Loading initial ramdisk ...
Unhandled exception: Instruction access fault
EPC: 80016d56 RA: 8020334e TVAL: 007f80016d56
EPC: 002d1d56 RA: 004be34e reloc adjusted
Unhandled exception: Load access fault
EPC: fff462d4 RA: fff462d0 TVAL: 80016d56
EPC: 802012d4 RA: 802012d0 reloc adjusted

Code: c825 8e0d 05b3 40b4 d0ef 0636 7493 ffe4 (d783 0004)
UEFI image [0xfe65e000:0xfe6e3fff] '/efi\boot\bootriscv64.efi'
UEFI image [0xdaa82000:0xdcc2afff]



I need more details please, as I have a UEFI bootflow and it works great 
(KASLR is based on a relocatable kernel and works fine in UEFI too).


Thanks,

Alex



Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-05-09 Thread Andreas Schwab
That does not work with UEFI booting:

Loading Linux 6.4.0-rc1-1.g668187d-default ...
Loading initial ramdisk ...
Unhandled exception: Instruction access fault
EPC: 80016d56 RA: 8020334e TVAL: 007f80016d56
EPC: 002d1d56 RA: 004be34e reloc adjusted
Unhandled exception: Load access fault
EPC: fff462d4 RA: fff462d0 TVAL: 80016d56
EPC: 802012d4 RA: 802012d0 reloc adjusted

Code: c825 8e0d 05b3 40b4 d0ef 0636 7493 ffe4 (d783 0004)
UEFI image [0xfe65e000:0xfe6e3fff] '/efi\boot\bootriscv64.efi'
UEFI image [0xdaa82000:0xdcc2afff]

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."


Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-03-24 Thread Alexandre Ghiti

Hi Nick,

On 3/22/23 19:25, Nick Desaulniers wrote:

On Fri, Feb 24, 2023 at 7:58 AM Björn Töpel  wrote:

Alexandre Ghiti  writes:


+cc linux-kbuild, llvm, Nathan, Nick

On 2/15/23 15:36, Alexandre Ghiti wrote:

From: Alexandre Ghiti 


I tried a lot of things, but I struggle to understand, does anyone have
any idea? FYI, the same problem happens with LLVM.

Off the top of my head, no idea.

(Maybe as a follow up to this series, I wonder if pursuing
ARCH_HAS_RELR for ARCH=riscv is worthwhile?)



IIUC, the goal for using RELR is to reduce the size of a kernel image: 
right now, this is not my priority, but I'll add that to my todo list 
because that may be useful to distros.






Don't ask me *why*, but adding --emit-relocs to your linker flags solves
"the NULL .rela.dyn" both for GCC and LLVM.

The downside is that you end up with a bunch of .rela cruft in your
vmlinux.

There was a patch just this week to use $(OBJCOPY) to strip these from
vmlinux (for x86). Looks like x86 uses --emit-relocs for KASLR:
https://lore.kernel.org/lkml/20230320121006.4863-1-petr.pa...@suse.com/



That's nice, that would be an interesting intermediate step until we 
find the issue here as I believe it is important to have the relocations 
in the init section to save memory.


Thanks for your answer Nick, really appreciated,

Alex




Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-03-23 Thread Fangrui Song
On Wed, Mar 22, 2023 at 11:26 AM Nick Desaulniers
 wrote:
>
> On Fri, Feb 24, 2023 at 7:58 AM Björn Töpel  wrote:
> >
> > Alexandre Ghiti  writes:
> >
> > > +cc linux-kbuild, llvm, Nathan, Nick
> > >
> > > On 2/15/23 15:36, Alexandre Ghiti wrote:
> > >> From: Alexandre Ghiti 
> > >>
> > > I tried a lot of things, but I struggle to understand, does anyone have
> > > any idea? FYI, the same problem happens with LLVM.
>
> Off the top of my head, no idea.
>
> (Maybe as a follow up to this series, I wonder if pursuing
> ARCH_HAS_RELR for ARCH=riscv is worthwhile?)

(I had thought about this for my own fun, but the currently only
implementation arch/arm64/kernel/head.S uses assembly.
Every port needs to write some assembly for the same task, which is a pity.
In FreeBSD rtld, glibc, and musl, DT_RELR code is target-independent.)


> >
> > Don't ask me *why*, but adding --emit-relocs to your linker flags solves
> > "the NULL .rela.dyn" both for GCC and LLVM.
> >
> > The downside is that you end up with a bunch of .rela cruft in your
> > vmlinux.
>
> There was a patch just this week to use $(OBJCOPY) to strip these from
> vmlinux (for x86). Looks like x86 uses --emit-relocs for KASLR:
> https://lore.kernel.org/lkml/20230320121006.4863-1-petr.pa...@suse.com/
> --
> Thanks,
> ~Nick Desaulniers
>


-- 
宋方睿


Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-03-22 Thread Nick Desaulniers
On Fri, Feb 24, 2023 at 7:58 AM Björn Töpel  wrote:
>
> Alexandre Ghiti  writes:
>
> > +cc linux-kbuild, llvm, Nathan, Nick
> >
> > On 2/15/23 15:36, Alexandre Ghiti wrote:
> >> From: Alexandre Ghiti 
> >>
> > I tried a lot of things, but I struggle to understand, does anyone have
> > any idea? FYI, the same problem happens with LLVM.

Off the top of my head, no idea.

(Maybe as a follow up to this series, I wonder if pursuing
ARCH_HAS_RELR for ARCH=riscv is worthwhile?)

>
> Don't ask me *why*, but adding --emit-relocs to your linker flags solves
> "the NULL .rela.dyn" both for GCC and LLVM.
>
> The downside is that you end up with a bunch of .rela cruft in your
> vmlinux.

There was a patch just this week to use $(OBJCOPY) to strip these from
vmlinux (for x86). Looks like x86 uses --emit-relocs for KASLR:
https://lore.kernel.org/lkml/20230320121006.4863-1-petr.pa...@suse.com/
-- 
Thanks,
~Nick Desaulniers


Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-03-22 Thread Alexandre Ghiti

@linux-kbuild: Does anyone has an idea to solve this?

Thanks!

On 2/22/23 13:29, Alexandre Ghiti wrote:

+cc linux-kbuild, llvm, Nathan, Nick

On 2/15/23 15:36, Alexandre Ghiti wrote:

From: Alexandre Ghiti 

This config allows to compile 64b kernel as PIE and to relocate it at
any virtual address at runtime: this paves the way to KASLR.
Runtime relocation is possible since relocation metadata are embedded 
into

the kernel.

Note that relocating at runtime introduces an overhead even if the
kernel is loaded at the same address it was linked at and that the 
compiler

options are those used in arm64 which uses the same RELA relocation
format.

Signed-off-by: Alexandre Ghiti 
---
  arch/riscv/Kconfig  | 14 +
  arch/riscv/Makefile |  7 +++--
  arch/riscv/kernel/efi-header.S  |  6 ++--
  arch/riscv/kernel/vmlinux.lds.S | 10 --
  arch/riscv/mm/Makefile  |  4 +++
  arch/riscv/mm/init.c    | 54 -
  6 files changed, 87 insertions(+), 8 deletions(-)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index e2b656043abf..e0ee7ce4b2e3 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -544,6 +544,20 @@ config COMPAT
      If you want to execute 32-bit userspace applications, say Y.
  +config RELOCATABLE
+    bool "Build a relocatable kernel"
+    depends on MMU && 64BIT && !XIP_KERNEL
+    help
+  This builds a kernel as a Position Independent Executable 
(PIE),
+  which retains all relocation metadata required to relocate 
the
+  kernel binary at runtime to a different virtual address 
than the

+  address it was linked at.
+  Since RISCV uses the RELA relocation format, this requires a
+  relocation pass at runtime even if the kernel is loaded at 
the

+  same address it was linked at.
+
+  If unsure, say N.
+
  endmenu # "Kernel features"
    menu "Boot options"
diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index 82153960ac00..97c34136b027 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -7,9 +7,12 @@
  #
    OBJCOPYFLAGS    := -O binary
-LDFLAGS_vmlinux :=
+ifeq ($(CONFIG_RELOCATABLE),y)
+    LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro
+    KBUILD_CFLAGS += -fPIE
+endif
  ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
-    LDFLAGS_vmlinux := --no-relax
+    LDFLAGS_vmlinux += --no-relax
  KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
  CC_FLAGS_FTRACE := -fpatchable-function-entry=8
  endif
diff --git a/arch/riscv/kernel/efi-header.S 
b/arch/riscv/kernel/efi-header.S

index 8e733aa48ba6..f7ee09c4f12d 100644
--- a/arch/riscv/kernel/efi-header.S
+++ b/arch/riscv/kernel/efi-header.S
@@ -33,7 +33,7 @@ optional_header:
  .byte    0x02    // MajorLinkerVersion
  .byte    0x14    // MinorLinkerVersion
  .long    __pecoff_text_end - efi_header_end    // SizeOfCode
-    .long    __pecoff_data_virt_size    // 
SizeOfInitializedData
+    .long    __pecoff_data_virt_end - __pecoff_text_end    // 
SizeOfInitializedData

  .long    0    // SizeOfUninitializedData
  .long    __efistub_efi_pe_entry - _start    // 
AddressOfEntryPoint

  .long    efi_header_end - _start    // BaseOfCode
@@ -91,9 +91,9 @@ section_table:
  IMAGE_SCN_MEM_EXECUTE    // Characteristics
    .ascii    ".data\0\0\0"
-    .long    __pecoff_data_virt_size    // VirtualSize
+    .long    __pecoff_data_virt_end - __pecoff_text_end    // 
VirtualSize

  .long    __pecoff_text_end - _start    // VirtualAddress
-    .long    __pecoff_data_raw_size    // SizeOfRawData
+    .long    __pecoff_data_raw_end - __pecoff_text_end    // 
SizeOfRawData

  .long    __pecoff_text_end - _start    // PointerToRawData
    .long    0    // PointerToRelocations
diff --git a/arch/riscv/kernel/vmlinux.lds.S 
b/arch/riscv/kernel/vmlinux.lds.S

index 4e6c88aa4d87..8be2de3be08c 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -122,9 +122,15 @@ SECTIONS
  *(.sdata*)
  }
  +    .rela.dyn : ALIGN(8) {
+    __rela_dyn_start = .;
+    *(.rela .rela*)
+    __rela_dyn_end = .;
+    }
+



So I realized those relocations would be better in the init section so 
we can get rid of them at some point. So I tried the following:


diff --git a/arch/riscv/kernel/vmlinux.lds.S 
b/arch/riscv/kernel/vmlinux.lds.S

index 7ac215467fd5..6111023a89ef 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -93,6 +93,12 @@ SECTIONS
    *(.rel.dyn*)
    }

+   .rela.dyn : ALIGN(8) {
+   __rela_dyn_start = .;
+   *(.rela .rela*)
+   __rela_dyn_end = .;
+   }
+
    __init_data_end = .;

    . = ALIGN(8);
@@ -119,12 +125,6 @@ SECTIONS
    *(.sdata*)
    }

-   .rela.dyn 

Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-02-24 Thread Björn Töpel
Alexandre Ghiti  writes:

> +cc linux-kbuild, llvm, Nathan, Nick
>
> On 2/15/23 15:36, Alexandre Ghiti wrote:
>> From: Alexandre Ghiti 
>>
>> This config allows to compile 64b kernel as PIE and to relocate it at
>> any virtual address at runtime: this paves the way to KASLR.
>> Runtime relocation is possible since relocation metadata are embedded into
>> the kernel.
>>
>> Note that relocating at runtime introduces an overhead even if the
>> kernel is loaded at the same address it was linked at and that the compiler
>> options are those used in arm64 which uses the same RELA relocation
>> format.
>>
>> Signed-off-by: Alexandre Ghiti 
>> ---
>>   arch/riscv/Kconfig  | 14 +
>>   arch/riscv/Makefile |  7 +++--
>>   arch/riscv/kernel/efi-header.S  |  6 ++--
>>   arch/riscv/kernel/vmlinux.lds.S | 10 --
>>   arch/riscv/mm/Makefile  |  4 +++
>>   arch/riscv/mm/init.c| 54 -
>>   6 files changed, 87 insertions(+), 8 deletions(-)
>>
>> diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
>> index e2b656043abf..e0ee7ce4b2e3 100644
>> --- a/arch/riscv/Kconfig
>> +++ b/arch/riscv/Kconfig
>> @@ -544,6 +544,20 @@ config COMPAT
>>   
>>If you want to execute 32-bit userspace applications, say Y.
>>   
>> +config RELOCATABLE
>> +bool "Build a relocatable kernel"
>> +depends on MMU && 64BIT && !XIP_KERNEL
>> +help
>> +  This builds a kernel as a Position Independent Executable (PIE),
>> +  which retains all relocation metadata required to relocate the
>> +  kernel binary at runtime to a different virtual address than the
>> +  address it was linked at.
>> +  Since RISCV uses the RELA relocation format, this requires a
>> +  relocation pass at runtime even if the kernel is loaded at the
>> +  same address it was linked at.
>> +
>> +  If unsure, say N.
>> +
>>   endmenu # "Kernel features"
>>   
>>   menu "Boot options"
>> diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
>> index 82153960ac00..97c34136b027 100644
>> --- a/arch/riscv/Makefile
>> +++ b/arch/riscv/Makefile
>> @@ -7,9 +7,12 @@
>>   #
>>   
>>   OBJCOPYFLAGS:= -O binary
>> -LDFLAGS_vmlinux :=
>> +ifeq ($(CONFIG_RELOCATABLE),y)
>> +LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro
>> +KBUILD_CFLAGS += -fPIE
>> +endif
>>   ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
>> -LDFLAGS_vmlinux := --no-relax
>> +LDFLAGS_vmlinux += --no-relax
>>  KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
>>  CC_FLAGS_FTRACE := -fpatchable-function-entry=8
>>   endif
>> diff --git a/arch/riscv/kernel/efi-header.S b/arch/riscv/kernel/efi-header.S
>> index 8e733aa48ba6..f7ee09c4f12d 100644
>> --- a/arch/riscv/kernel/efi-header.S
>> +++ b/arch/riscv/kernel/efi-header.S
>> @@ -33,7 +33,7 @@ optional_header:
>>  .byte   0x02// MajorLinkerVersion
>>  .byte   0x14// MinorLinkerVersion
>>  .long   __pecoff_text_end - efi_header_end  // SizeOfCode
>> -.long   __pecoff_data_virt_size // SizeOfInitializedData
>> +.long   __pecoff_data_virt_end - __pecoff_text_end  // 
>> SizeOfInitializedData
>>  .long   0   // 
>> SizeOfUninitializedData
>>  .long   __efistub_efi_pe_entry - _start // AddressOfEntryPoint
>>  .long   efi_header_end - _start // BaseOfCode
>> @@ -91,9 +91,9 @@ section_table:
>>  IMAGE_SCN_MEM_EXECUTE   // Characteristics
>>   
>>  .ascii  ".data\0\0\0"
>> -.long   __pecoff_data_virt_size // VirtualSize
>> +.long   __pecoff_data_virt_end - __pecoff_text_end  // VirtualSize
>>  .long   __pecoff_text_end - _start  // VirtualAddress
>> -.long   __pecoff_data_raw_size  // SizeOfRawData
>> +.long   __pecoff_data_raw_end - __pecoff_text_end   // SizeOfRawData
>>  .long   __pecoff_text_end - _start  // PointerToRawData
>>   
>>  .long   0   // PointerToRelocations
>> diff --git a/arch/riscv/kernel/vmlinux.lds.S 
>> b/arch/riscv/kernel/vmlinux.lds.S
>> index 4e6c88aa4d87..8be2de3be08c 100644
>> --- a/arch/riscv/kernel/vmlinux.lds.S
>> +++ b/arch/riscv/kernel/vmlinux.lds.S
>> @@ -122,9 +122,15 @@ SECTIONS
>>  *(.sdata*)
>>  }
>>   
>> +.rela.dyn : ALIGN(8) {
>> +__rela_dyn_start = .;
>> +*(.rela .rela*)
>> +__rela_dyn_end = .;
>> +}
>> +
>
>
> So I realized those relocations would be better in the init section so 
> we can get rid of them at some point. So I tried the following:
>
> diff --git a/arch/riscv/kernel/vmlinux.lds.S 
> b/arch/riscv/kernel/vmlinux.lds.S
> index 7ac215467fd5..6111023a89ef 100644
> --- a/arch/riscv/kernel/vmlinux.lds.S
> +++ b/arch/riscv/kernel/vmlinux.lds.S
> 

Re: [PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-02-22 Thread Alexandre Ghiti

+cc linux-kbuild, llvm, Nathan, Nick

On 2/15/23 15:36, Alexandre Ghiti wrote:

From: Alexandre Ghiti 

This config allows to compile 64b kernel as PIE and to relocate it at
any virtual address at runtime: this paves the way to KASLR.
Runtime relocation is possible since relocation metadata are embedded into
the kernel.

Note that relocating at runtime introduces an overhead even if the
kernel is loaded at the same address it was linked at and that the compiler
options are those used in arm64 which uses the same RELA relocation
format.

Signed-off-by: Alexandre Ghiti 
---
  arch/riscv/Kconfig  | 14 +
  arch/riscv/Makefile |  7 +++--
  arch/riscv/kernel/efi-header.S  |  6 ++--
  arch/riscv/kernel/vmlinux.lds.S | 10 --
  arch/riscv/mm/Makefile  |  4 +++
  arch/riscv/mm/init.c| 54 -
  6 files changed, 87 insertions(+), 8 deletions(-)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index e2b656043abf..e0ee7ce4b2e3 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -544,6 +544,20 @@ config COMPAT
  
  	  If you want to execute 32-bit userspace applications, say Y.
  
+config RELOCATABLE

+   bool "Build a relocatable kernel"
+   depends on MMU && 64BIT && !XIP_KERNEL
+   help
+  This builds a kernel as a Position Independent Executable (PIE),
+  which retains all relocation metadata required to relocate the
+  kernel binary at runtime to a different virtual address than the
+  address it was linked at.
+  Since RISCV uses the RELA relocation format, this requires a
+  relocation pass at runtime even if the kernel is loaded at the
+  same address it was linked at.
+
+  If unsure, say N.
+
  endmenu # "Kernel features"
  
  menu "Boot options"

diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index 82153960ac00..97c34136b027 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -7,9 +7,12 @@
  #
  
  OBJCOPYFLAGS:= -O binary

-LDFLAGS_vmlinux :=
+ifeq ($(CONFIG_RELOCATABLE),y)
+   LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro
+   KBUILD_CFLAGS += -fPIE
+endif
  ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
-   LDFLAGS_vmlinux := --no-relax
+   LDFLAGS_vmlinux += --no-relax
KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
CC_FLAGS_FTRACE := -fpatchable-function-entry=8
  endif
diff --git a/arch/riscv/kernel/efi-header.S b/arch/riscv/kernel/efi-header.S
index 8e733aa48ba6..f7ee09c4f12d 100644
--- a/arch/riscv/kernel/efi-header.S
+++ b/arch/riscv/kernel/efi-header.S
@@ -33,7 +33,7 @@ optional_header:
.byte   0x02// MajorLinkerVersion
.byte   0x14// MinorLinkerVersion
.long   __pecoff_text_end - efi_header_end  // SizeOfCode
-   .long   __pecoff_data_virt_size // SizeOfInitializedData
+   .long   __pecoff_data_virt_end - __pecoff_text_end  // 
SizeOfInitializedData
.long   0   // 
SizeOfUninitializedData
.long   __efistub_efi_pe_entry - _start // AddressOfEntryPoint
.long   efi_header_end - _start // BaseOfCode
@@ -91,9 +91,9 @@ section_table:
IMAGE_SCN_MEM_EXECUTE   // Characteristics
  
  	.ascii	".data\0\0\0"

-   .long   __pecoff_data_virt_size // VirtualSize
+   .long   __pecoff_data_virt_end - __pecoff_text_end  // VirtualSize
.long   __pecoff_text_end - _start  // VirtualAddress
-   .long   __pecoff_data_raw_size  // SizeOfRawData
+   .long   __pecoff_data_raw_end - __pecoff_text_end   // SizeOfRawData
.long   __pecoff_text_end - _start  // PointerToRawData
  
  	.long	0	// PointerToRelocations

diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
index 4e6c88aa4d87..8be2de3be08c 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -122,9 +122,15 @@ SECTIONS
*(.sdata*)
}
  
+	.rela.dyn : ALIGN(8) {

+   __rela_dyn_start = .;
+   *(.rela .rela*)
+   __rela_dyn_end = .;
+   }
+



So I realized those relocations would be better in the init section so 
we can get rid of them at some point. So I tried the following:


diff --git a/arch/riscv/kernel/vmlinux.lds.S 
b/arch/riscv/kernel/vmlinux.lds.S

index 7ac215467fd5..6111023a89ef 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -93,6 +93,12 @@ SECTIONS
    *(.rel.dyn*)
    }

+   .rela.dyn : ALIGN(8) {
+   __rela_dyn_start = .;
+   *(.rela .rela*)
+   __rela_dyn_end = .;
+   }
+
    __init_data_end = .;

    . = ALIGN(8);
@@ -119,12 +125,6 @@ SECTIONS
    

[PATCH v8 1/3] riscv: Introduce CONFIG_RELOCATABLE

2023-02-15 Thread Alexandre Ghiti
From: Alexandre Ghiti 

This config allows to compile 64b kernel as PIE and to relocate it at
any virtual address at runtime: this paves the way to KASLR.
Runtime relocation is possible since relocation metadata are embedded into
the kernel.

Note that relocating at runtime introduces an overhead even if the
kernel is loaded at the same address it was linked at and that the compiler
options are those used in arm64 which uses the same RELA relocation
format.

Signed-off-by: Alexandre Ghiti 
---
 arch/riscv/Kconfig  | 14 +
 arch/riscv/Makefile |  7 +++--
 arch/riscv/kernel/efi-header.S  |  6 ++--
 arch/riscv/kernel/vmlinux.lds.S | 10 --
 arch/riscv/mm/Makefile  |  4 +++
 arch/riscv/mm/init.c| 54 -
 6 files changed, 87 insertions(+), 8 deletions(-)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index e2b656043abf..e0ee7ce4b2e3 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -544,6 +544,20 @@ config COMPAT
 
  If you want to execute 32-bit userspace applications, say Y.
 
+config RELOCATABLE
+   bool "Build a relocatable kernel"
+   depends on MMU && 64BIT && !XIP_KERNEL
+   help
+  This builds a kernel as a Position Independent Executable (PIE),
+  which retains all relocation metadata required to relocate the
+  kernel binary at runtime to a different virtual address than the
+  address it was linked at.
+  Since RISCV uses the RELA relocation format, this requires a
+  relocation pass at runtime even if the kernel is loaded at the
+  same address it was linked at.
+
+  If unsure, say N.
+
 endmenu # "Kernel features"
 
 menu "Boot options"
diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index 82153960ac00..97c34136b027 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -7,9 +7,12 @@
 #
 
 OBJCOPYFLAGS:= -O binary
-LDFLAGS_vmlinux :=
+ifeq ($(CONFIG_RELOCATABLE),y)
+   LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro
+   KBUILD_CFLAGS += -fPIE
+endif
 ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
-   LDFLAGS_vmlinux := --no-relax
+   LDFLAGS_vmlinux += --no-relax
KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
CC_FLAGS_FTRACE := -fpatchable-function-entry=8
 endif
diff --git a/arch/riscv/kernel/efi-header.S b/arch/riscv/kernel/efi-header.S
index 8e733aa48ba6..f7ee09c4f12d 100644
--- a/arch/riscv/kernel/efi-header.S
+++ b/arch/riscv/kernel/efi-header.S
@@ -33,7 +33,7 @@ optional_header:
.byte   0x02// MajorLinkerVersion
.byte   0x14// MinorLinkerVersion
.long   __pecoff_text_end - efi_header_end  // SizeOfCode
-   .long   __pecoff_data_virt_size // SizeOfInitializedData
+   .long   __pecoff_data_virt_end - __pecoff_text_end  // 
SizeOfInitializedData
.long   0   // 
SizeOfUninitializedData
.long   __efistub_efi_pe_entry - _start // AddressOfEntryPoint
.long   efi_header_end - _start // BaseOfCode
@@ -91,9 +91,9 @@ section_table:
IMAGE_SCN_MEM_EXECUTE   // Characteristics
 
.ascii  ".data\0\0\0"
-   .long   __pecoff_data_virt_size // VirtualSize
+   .long   __pecoff_data_virt_end - __pecoff_text_end  // VirtualSize
.long   __pecoff_text_end - _start  // VirtualAddress
-   .long   __pecoff_data_raw_size  // SizeOfRawData
+   .long   __pecoff_data_raw_end - __pecoff_text_end   // SizeOfRawData
.long   __pecoff_text_end - _start  // PointerToRawData
 
.long   0   // PointerToRelocations
diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
index 4e6c88aa4d87..8be2de3be08c 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -122,9 +122,15 @@ SECTIONS
*(.sdata*)
}
 
+   .rela.dyn : ALIGN(8) {
+   __rela_dyn_start = .;
+   *(.rela .rela*)
+   __rela_dyn_end = .;
+   }
+
 #ifdef CONFIG_EFI
.pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); }
-   __pecoff_data_raw_size = ABSOLUTE(. - __pecoff_text_end);
+   __pecoff_data_raw_end = ABSOLUTE(.);
 #endif
 
/* End of data section */
@@ -134,7 +140,7 @@ SECTIONS
 
 #ifdef CONFIG_EFI
. = ALIGN(PECOFF_SECTION_ALIGNMENT);
-   __pecoff_data_virt_size = ABSOLUTE(. - __pecoff_text_end);
+   __pecoff_data_virt_end = ABSOLUTE(.);
 #endif
_end = .;
 
diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile
index 2ac177c05352..b85e9e82f082 100644
--- a/arch/riscv/mm/Makefile
+++ b/arch/riscv/mm/Makefile
@@ -1,6 +1,10 @@
 # SPDX-License-Identifier: