diff mbox series

[Crash-utility] RISCV64: Use va_kernel_pa_offset in VTOP()

Message ID 20230724040649.220279-1-suagrfillet@gmail.com (mailing list archive)
State Superseded
Headers show
Series [Crash-utility] RISCV64: Use va_kernel_pa_offset in VTOP() | expand

Checks

Context Check Description
conchuod/tree_selection fail Failed to apply to next/pending-fixes, riscv/for-next or riscv/master

Commit Message

Song Shuai July 24, 2023, 4:06 a.m. UTC
Since RISC-V Linux v6.4, the commit 3335068f8721 ("riscv: Use
PUD/P4D/PGD pages for the linear mapping") changes the
phys_ram_base from the kernel_map.phys_addr to the start of DRAM.

The Crash's VTOP() still uses phys_ram_base and kernel_map.virt_addr
to translate kernel virtual address, that made Crash boot failed with
Linux v6.4 and later version.

Let Linux export kernel_map.va_kernel_pa_offset in v6.5 and Crash can
use "va_kernel_pa_offset" to translate the kernel virtual address in
VTOP() correctly.

Signed-off-by: Song Shuai <suagrfillet@gmail.com>
---
You can check/test the Linux changes from this link:
https://github.com/sugarfillet/linux/commits/6.5-rc3-crash

And I'll send the Linux changes to riscv/for-next If you're ok with this patch.
---
 defs.h    |  4 ++--
 riscv64.c | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)

Comments

Conor Dooley July 24, 2023, 7:41 a.m. UTC | #1
Hey,

On Mon, Jul 24, 2023 at 12:06:49PM +0800, Song Shuai wrote:
> Since RISC-V Linux v6.4, the commit 3335068f8721 ("riscv: Use
> PUD/P4D/PGD pages for the linear mapping") changes the
> phys_ram_base from the kernel_map.phys_addr to the start of DRAM.
> 
> The Crash's VTOP() still uses phys_ram_base and kernel_map.virt_addr
> to translate kernel virtual address, that made Crash boot failed with
> Linux v6.4 and later version.
> 
> Let Linux export kernel_map.va_kernel_pa_offset in v6.5 and Crash can
> use "va_kernel_pa_offset" to translate the kernel virtual address in
> VTOP() correctly.
> 
> Signed-off-by: Song Shuai <suagrfillet@gmail.com>
> ---
> You can check/test the Linux changes from this link:
> https://github.com/sugarfillet/linux/commits/6.5-rc3-crash
> 
> And I'll send the Linux changes to riscv/for-next If you're ok with this patch.

If you want this to go into 6.5, you'll need to send it for riscv/fixes
instead. It sounds like a fix for this would need to go into 6.4 too,
no?
Alexandre Ghiti July 24, 2023, 8:13 a.m. UTC | #2
Hi Song,


On 24/07/2023 06:06, Song Shuai wrote:
> Since RISC-V Linux v6.4, the commit 3335068f8721 ("riscv: Use
> PUD/P4D/PGD pages for the linear mapping") changes the
> phys_ram_base from the kernel_map.phys_addr to the start of DRAM.


Maybe we could be more explicit here, kernel_map.phys_addr actually 
points to the physical start of the kernel so maybe something like that:

"changes phys_ram_base from the physical start of the kernel to the 
actual start of the DRAM."


>
> The Crash's VTOP() still uses phys_ram_base and kernel_map.virt_addr
> to translate kernel virtual address, that made Crash boot failed with
> Linux v6.4 and later version.
>
> Let Linux export kernel_map.va_kernel_pa_offset in v6.5 and Crash can
> use "va_kernel_pa_offset" to translate the kernel virtual address in
> VTOP() correctly.
>
> Signed-off-by: Song Shuai <suagrfillet@gmail.com>
> ---
> You can check/test the Linux changes from this link:
> https://github.com/sugarfillet/linux/commits/6.5-rc3-crash
>
> And I'll send the Linux changes to riscv/for-next If you're ok with this patch.
> ---
>   defs.h    |  4 ++--
>   riscv64.c | 22 ++++++++++++++++++++++
>   2 files changed, 24 insertions(+), 2 deletions(-)
>
> diff --git a/defs.h b/defs.h
> index 358f365..46b9857 100644
> --- a/defs.h
> +++ b/defs.h
> @@ -3662,8 +3662,7 @@ typedef signed int s32;
>   	ulong _X = X;									\
>   	(THIS_KERNEL_VERSION >= LINUX(5,13,0) &&					\
>   		(_X) >= machdep->machspec->kernel_link_addr) ?				\
> -		(((unsigned long)(_X)-(machdep->machspec->kernel_link_addr)) +		\
> -		 machdep->machspec->phys_base):						\
> +		((unsigned long)(_X)-(machdep->machspec->va_kernel_pa_offset)): 	\
>   		(((unsigned long)(_X)-(machdep->kvbase)) +				\
>   		 machdep->machspec->phys_base);						\
>   	})
> @@ -7021,6 +7020,7 @@ struct machine_specific {
>   	ulong modules_vaddr;
>   	ulong modules_end;
>   	ulong kernel_link_addr;
> +	ulong va_kernel_pa_offset;
>   
>   	ulong _page_present;
>   	ulong _page_read;
> diff --git a/riscv64.c b/riscv64.c
> index 6b9a688..b9e50b4 100644
> --- a/riscv64.c
> +++ b/riscv64.c
> @@ -418,6 +418,27 @@ error:
>   	error(FATAL, "cannot get vm layout\n");
>   }
>   
> +static void
> +riscv64_get_va_kernel_pa_offset(struct machine_specific *ms)
> +{
> +	unsigned long kernel_version = riscv64_get_kernel_version();
> +
> +	/*
> +	 * va_kernel_pa_offset is defined in Linux kernel since 6.5.
> +	 */
> +	if (kernel_version >= LINUX(6,5,0)) {
> +		char *string;
> +		if ((string = pc->read_vmcoreinfo("NUMBER(va_kernel_pa_offset)"))) {
> +			ms->va_kernel_pa_offset = htol(string, QUIET, NULL);
> +			free(string);
> +		} else
> +			error(FATAL, "cannot read va_kernel_pa_offset\n");
> +	} else if (kernel_version >= LINUX(6,4,0))
> +		error(FATAL, "cannot determine va_kernel_pa_offset since Linux 6.4\n");
> +	else
> +		ms->va_kernel_pa_offset = ms->kernel_link_addr - ms->phys_base;
> +}
> +
>   static int
>   riscv64_is_kvaddr(ulong vaddr)
>   {
> @@ -1352,6 +1373,7 @@ riscv64_init(int when)
>   		riscv64_get_struct_page_size(machdep->machspec);
>   		riscv64_get_va_bits(machdep->machspec);
>   		riscv64_get_va_range(machdep->machspec);
> +		riscv64_get_va_kernel_pa_offset(machdep->machspec);
>   
>   		pt_level_alloc(&machdep->pgd, "cannot malloc pgd space.");
>   		pt_level_alloc(&machdep->machspec->p4d, "cannot malloc p4d space.");


Would you mind giving me the instructions on how to reproduce the issue 
please? So that I can add that to our internal CI and avoid this type of 
breakage in the future.

Thanks,

Alex
Song Shuai July 24, 2023, 8:44 a.m. UTC | #3
在 2023/7/24 15:41, Conor Dooley 写道:
> Hey,
> 
> On Mon, Jul 24, 2023 at 12:06:49PM +0800, Song Shuai wrote:
>> Since RISC-V Linux v6.4, the commit 3335068f8721 ("riscv: Use
>> PUD/P4D/PGD pages for the linear mapping") changes the
>> phys_ram_base from the kernel_map.phys_addr to the start of DRAM.
>>
>> The Crash's VTOP() still uses phys_ram_base and kernel_map.virt_addr
>> to translate kernel virtual address, that made Crash boot failed with
>> Linux v6.4 and later version.
>>
>> Let Linux export kernel_map.va_kernel_pa_offset in v6.5 and Crash can
>> use "va_kernel_pa_offset" to translate the kernel virtual address in
>> VTOP() correctly.
>>
>> Signed-off-by: Song Shuai <suagrfillet@gmail.com>
>> ---
>> You can check/test the Linux changes from this link:
>> https://github.com/sugarfillet/linux/commits/6.5-rc3-crash
>>
>> And I'll send the Linux changes to riscv/for-next If you're ok with this patch.
> 
> If you want this to go into 6.5, you'll need to send it for riscv/fixes
> instead. It sounds like a fix for this would need to go into 6.4 too,
> no?
You're right, that should be riscv/fixes for 6.5 and this issue also 
need to be fixed in 6.4 stable.

How about waiting for Crash guys' comments on the introduction of the 
"va_kernel_pa_offset" in vmcoreinfo
and then determine which stable version should be taken in the first 
"if" of kernel_version.
>
Song Shuai July 24, 2023, 8:48 a.m. UTC | #4
在 2023/7/24 16:13, Alexandre Ghiti 写道:
> Hi Song,
> 
> 
> On 24/07/2023 06:06, Song Shuai wrote:
>> Since RISC-V Linux v6.4, the commit 3335068f8721 ("riscv: Use
>> PUD/P4D/PGD pages for the linear mapping") changes the
>> phys_ram_base from the kernel_map.phys_addr to the start of DRAM.
> 
> 
> Maybe we could be more explicit here, kernel_map.phys_addr actually 
> points to the physical start of the kernel so maybe something like that:
> 
> "changes phys_ram_base from the physical start of the kernel to the 
> actual start of the DRAM."
> 
ok,
> 
>>
>> The Crash's VTOP() still uses phys_ram_base and kernel_map.virt_addr
>> to translate kernel virtual address, that made Crash boot failed with
>> Linux v6.4 and later version.
>>
>> Let Linux export kernel_map.va_kernel_pa_offset in v6.5 and Crash can
>> use "va_kernel_pa_offset" to translate the kernel virtual address in
>> VTOP() correctly.
>>
>> Signed-off-by: Song Shuai <suagrfillet@gmail.com>
>> ---
>> You can check/test the Linux changes from this link:
>> https://github.com/sugarfillet/linux/commits/6.5-rc3-crash
>>
>> And I'll send the Linux changes to riscv/for-next If you're ok with 
>> this patch.
>> ---
>>   defs.h    |  4 ++--
>>   riscv64.c | 22 ++++++++++++++++++++++
>>   2 files changed, 24 insertions(+), 2 deletions(-)
>>
>> diff --git a/defs.h b/defs.h
>> index 358f365..46b9857 100644
>> --- a/defs.h
>> +++ b/defs.h
>> @@ -3662,8 +3662,7 @@ typedef signed int s32;
>>       ulong _X = X;                                    \
>>       (THIS_KERNEL_VERSION >= LINUX(5,13,0) &&                    \
>>           (_X) >= machdep->machspec->kernel_link_addr) ?                \
>> -        (((unsigned long)(_X)-(machdep->machspec->kernel_link_addr)) 
>> +        \
>> -         machdep->machspec->phys_base):                        \
>> +        ((unsigned 
>> long)(_X)-(machdep->machspec->va_kernel_pa_offset)):     \
>>           (((unsigned long)(_X)-(machdep->kvbase)) +                \
>>            machdep->machspec->phys_base);                        \
>>       })
>> @@ -7021,6 +7020,7 @@ struct machine_specific {
>>       ulong modules_vaddr;
>>       ulong modules_end;
>>       ulong kernel_link_addr;
>> +    ulong va_kernel_pa_offset;
>>       ulong _page_present;
>>       ulong _page_read;
>> diff --git a/riscv64.c b/riscv64.c
>> index 6b9a688..b9e50b4 100644
>> --- a/riscv64.c
>> +++ b/riscv64.c
>> @@ -418,6 +418,27 @@ error:
>>       error(FATAL, "cannot get vm layout\n");
>>   }
>> +static void
>> +riscv64_get_va_kernel_pa_offset(struct machine_specific *ms)
>> +{
>> +    unsigned long kernel_version = riscv64_get_kernel_version();
>> +
>> +    /*
>> +     * va_kernel_pa_offset is defined in Linux kernel since 6.5.
>> +     */
>> +    if (kernel_version >= LINUX(6,5,0)) {
>> +        char *string;
>> +        if ((string = 
>> pc->read_vmcoreinfo("NUMBER(va_kernel_pa_offset)"))) {
>> +            ms->va_kernel_pa_offset = htol(string, QUIET, NULL);
>> +            free(string);
>> +        } else
>> +            error(FATAL, "cannot read va_kernel_pa_offset\n");
>> +    } else if (kernel_version >= LINUX(6,4,0))
>> +        error(FATAL, "cannot determine va_kernel_pa_offset since 
>> Linux 6.4\n");
>> +    else
>> +        ms->va_kernel_pa_offset = ms->kernel_link_addr - ms->phys_base;
>> +}
>> +
>>   static int
>>   riscv64_is_kvaddr(ulong vaddr)
>>   {
>> @@ -1352,6 +1373,7 @@ riscv64_init(int when)
>>           riscv64_get_struct_page_size(machdep->machspec);
>>           riscv64_get_va_bits(machdep->machspec);
>>           riscv64_get_va_range(machdep->machspec);
>> +        riscv64_get_va_kernel_pa_offset(machdep->machspec);
>>           pt_level_alloc(&machdep->pgd, "cannot malloc pgd space.");
>>           pt_level_alloc(&machdep->machspec->p4d, "cannot malloc p4d 
>> space.");
> 
> 
> Would you mind giving me the instructions on how to reproduce the issue 
> please? So that I can add that to our internal CI and avoid this type of 
> breakage in the future.
> 
You can reproduce this issue via :

1. compile the Linux v6.4 or later version with Kdump support
2. generate the vmcore file via sysrq-trigger
3. start the Crash (crash-utility/crash:master) with namelist(vmlinux) 
and vmcore with optional "-d" option

Crash would boot failed with some incorrect infos (like: empty 
cpu_*_mask,utsname ) and some error like:
`crash: read error: kernel virtual address: ffffffff80ecb498  type: 
"linux_banner"`

> Thanks,
> 
> Alex
>
HAGIO KAZUHITO(萩尾 一仁) July 24, 2023, 9:22 a.m. UTC | #5
On 2023/07/24 17:44, Song Shuai wrote:
> 在 2023/7/24 15:41, Conor Dooley 写道:
>> Hey,
>>
>> On Mon, Jul 24, 2023 at 12:06:49PM +0800, Song Shuai wrote:
>>> Since RISC-V Linux v6.4, the commit 3335068f8721 ("riscv: Use
>>> PUD/P4D/PGD pages for the linear mapping") changes the
>>> phys_ram_base from the kernel_map.phys_addr to the start of DRAM.
>>>
>>> The Crash's VTOP() still uses phys_ram_base and kernel_map.virt_addr
>>> to translate kernel virtual address, that made Crash boot failed with
>>> Linux v6.4 and later version.
>>>
>>> Let Linux export kernel_map.va_kernel_pa_offset in v6.5 and Crash can
>>> use "va_kernel_pa_offset" to translate the kernel virtual address in
>>> VTOP() correctly.
>>>
>>> Signed-off-by: Song Shuai <suagrfillet@gmail.com>
>>> ---
>>> You can check/test the Linux changes from this link:
>>> https://github.com/sugarfillet/linux/commits/6.5-rc3-crash
>>>
>>> And I'll send the Linux changes to riscv/for-next If you're ok with 
>>> this patch.
>>
>> If you want this to go into 6.5, you'll need to send it for riscv/fixes
>> instead. It sounds like a fix for this would need to go into 6.4 too,
>> no?
> You're right, that should be riscv/fixes for 6.5 and this issue also 
> need to be fixed in 6.4 stable.
> 
> How about waiting for Crash guys' comments on the introduction of the 
> "va_kernel_pa_offset" in vmcoreinfo
> and then determine which stable version should be taken in the first 
> "if" of kernel_version.

I don't have any specific comment on this, it looks necessary and if 
it's accepted in vmcoreinfo, then we can accept a crash patch for it.

Thanks,
Kazu
HAGIO KAZUHITO(萩尾 一仁) Aug. 4, 2023, 4:42 a.m. UTC | #6
On 2023/07/24 13:06, Song Shuai wrote:
> Since RISC-V Linux v6.4, the commit 3335068f8721 ("riscv: Use
> PUD/P4D/PGD pages for the linear mapping") changes the
> phys_ram_base from the kernel_map.phys_addr to the start of DRAM.
> 
> The Crash's VTOP() still uses phys_ram_base and kernel_map.virt_addr
> to translate kernel virtual address, that made Crash boot failed with
> Linux v6.4 and later version.
> 
> Let Linux export kernel_map.va_kernel_pa_offset in v6.5 and Crash can
> use "va_kernel_pa_offset" to translate the kernel virtual address in
> VTOP() correctly.
> 
> Signed-off-by: Song Shuai <suagrfillet@gmail.com>
> ---
> You can check/test the Linux changes from this link:
> https://github.com/sugarfillet/linux/commits/6.5-rc3-crash
> 
> And I'll send the Linux changes to riscv/for-next If you're ok with this patch.
> ---
>   defs.h    |  4 ++--
>   riscv64.c | 22 ++++++++++++++++++++++
>   2 files changed, 24 insertions(+), 2 deletions(-)
> 
> diff --git a/defs.h b/defs.h
> index 358f365..46b9857 100644
> --- a/defs.h
> +++ b/defs.h
> @@ -3662,8 +3662,7 @@ typedef signed int s32;
>   	ulong _X = X;									\
>   	(THIS_KERNEL_VERSION >= LINUX(5,13,0) &&					\
>   		(_X) >= machdep->machspec->kernel_link_addr) ?				\
> -		(((unsigned long)(_X)-(machdep->machspec->kernel_link_addr)) +		\
> -		 machdep->machspec->phys_base):						\
> +		((unsigned long)(_X)-(machdep->machspec->va_kernel_pa_offset)): 	\
>   		(((unsigned long)(_X)-(machdep->kvbase)) +				\
>   		 machdep->machspec->phys_base);						\
>   	})
> @@ -7021,6 +7020,7 @@ struct machine_specific {
>   	ulong modules_vaddr;
>   	ulong modules_end;
>   	ulong kernel_link_addr;
> +	ulong va_kernel_pa_offset;
>   
>   	ulong _page_present;
>   	ulong _page_read;
> diff --git a/riscv64.c b/riscv64.c
> index 6b9a688..b9e50b4 100644
> --- a/riscv64.c
> +++ b/riscv64.c
> @@ -418,6 +418,27 @@ error:
>   	error(FATAL, "cannot get vm layout\n");
>   }
>   
> +static void
> +riscv64_get_va_kernel_pa_offset(struct machine_specific *ms)
> +{
> +	unsigned long kernel_version = riscv64_get_kernel_version();
> +
> +	/*
> +	 * va_kernel_pa_offset is defined in Linux kernel since 6.5.
> +	 */
> +	if (kernel_version >= LINUX(6,5,0)) {

The kernel patches look accepted, so for the crash patch detail,

I think this first version check is not necessary, we can just use the 
vmcoreinfo entry if available.  With it, backporting the kernel patches 
to e.g. 6.4.0 will also be supported.

Thanks,
Kazu

> +		char *string;
> +		if ((string = pc->read_vmcoreinfo("NUMBER(va_kernel_pa_offset)"))) {
> +			ms->va_kernel_pa_offset = htol(string, QUIET, NULL);
> +			free(string);
> +		} else
> +			error(FATAL, "cannot read va_kernel_pa_offset\n");
> +	} else if (kernel_version >= LINUX(6,4,0))
> +		error(FATAL, "cannot determine va_kernel_pa_offset since Linux 6.4\n");
> +	else
> +		ms->va_kernel_pa_offset = ms->kernel_link_addr - ms->phys_base;
> +}
> +
>   static int
>   riscv64_is_kvaddr(ulong vaddr)
>   {
> @@ -1352,6 +1373,7 @@ riscv64_init(int when)
>   		riscv64_get_struct_page_size(machdep->machspec);
>   		riscv64_get_va_bits(machdep->machspec);
>   		riscv64_get_va_range(machdep->machspec);
> +		riscv64_get_va_kernel_pa_offset(machdep->machspec);
>   
>   		pt_level_alloc(&machdep->pgd, "cannot malloc pgd space.");
>   		pt_level_alloc(&machdep->machspec->p4d, "cannot malloc p4d space.");
Song Shuai Aug. 4, 2023, 9:20 a.m. UTC | #7
在 2023/8/4 12:42, HAGIO KAZUHITO(萩尾 一仁) 写道:
> On 2023/07/24 13:06, Song Shuai wrote:
>> Since RISC-V Linux v6.4, the commit 3335068f8721 ("riscv: Use
>> PUD/P4D/PGD pages for the linear mapping") changes the
>> phys_ram_base from the kernel_map.phys_addr to the start of DRAM.
>>
>> The Crash's VTOP() still uses phys_ram_base and kernel_map.virt_addr
>> to translate kernel virtual address, that made Crash boot failed with
>> Linux v6.4 and later version.
>>
>> Let Linux export kernel_map.va_kernel_pa_offset in v6.5 and Crash can
>> use "va_kernel_pa_offset" to translate the kernel virtual address in
>> VTOP() correctly.
>>
>> Signed-off-by: Song Shuai <suagrfillet@gmail.com>
>> ---
>> You can check/test the Linux changes from this link:
>> https://github.com/sugarfillet/linux/commits/6.5-rc3-crash
>>
>> And I'll send the Linux changes to riscv/for-next If you're ok with this patch.
>> ---
>>    defs.h    |  4 ++--
>>    riscv64.c | 22 ++++++++++++++++++++++
>>    2 files changed, 24 insertions(+), 2 deletions(-)
>>
>> diff --git a/defs.h b/defs.h
>> index 358f365..46b9857 100644
>> --- a/defs.h
>> +++ b/defs.h
>> @@ -3662,8 +3662,7 @@ typedef signed int s32;
>>    	ulong _X = X;									\
>>    	(THIS_KERNEL_VERSION >= LINUX(5,13,0) &&					\
>>    		(_X) >= machdep->machspec->kernel_link_addr) ?				\
>> -		(((unsigned long)(_X)-(machdep->machspec->kernel_link_addr)) +		\
>> -		 machdep->machspec->phys_base):						\
>> +		((unsigned long)(_X)-(machdep->machspec->va_kernel_pa_offset)): 	\
>>    		(((unsigned long)(_X)-(machdep->kvbase)) +				\
>>    		 machdep->machspec->phys_base);						\
>>    	})
>> @@ -7021,6 +7020,7 @@ struct machine_specific {
>>    	ulong modules_vaddr;
>>    	ulong modules_end;
>>    	ulong kernel_link_addr;
>> +	ulong va_kernel_pa_offset;
>>    
>>    	ulong _page_present;
>>    	ulong _page_read;
>> diff --git a/riscv64.c b/riscv64.c
>> index 6b9a688..b9e50b4 100644
>> --- a/riscv64.c
>> +++ b/riscv64.c
>> @@ -418,6 +418,27 @@ error:
>>    	error(FATAL, "cannot get vm layout\n");
>>    }
>>    
>> +static void
>> +riscv64_get_va_kernel_pa_offset(struct machine_specific *ms)
>> +{
>> +	unsigned long kernel_version = riscv64_get_kernel_version();
>> +
>> +	/*
>> +	 * va_kernel_pa_offset is defined in Linux kernel since 6.5.
>> +	 */
>> +	if (kernel_version >= LINUX(6,5,0)) {
> 
> The kernel patches look accepted, so for the crash patch detail,
> 
> I think this first version check is not necessary, we can just use the
> vmcoreinfo entry if available.  With it, backporting the kernel patches
> to e.g. 6.4.0 will also be supported.
> 
ok, V2 is here:

https://lore.kernel.org/linux-riscv/20230804091559.3005820-1-suagrfillet@gmail.com/T/#u

> Thanks,
> Kazu
> 
>> +		char *string;
>> +		if ((string = pc->read_vmcoreinfo("NUMBER(va_kernel_pa_offset)"))) {
>> +			ms->va_kernel_pa_offset = htol(string, QUIET, NULL);
>> +			free(string);
>> +		} else
>> +			error(FATAL, "cannot read va_kernel_pa_offset\n");
>> +	} else if (kernel_version >= LINUX(6,4,0))
>> +		error(FATAL, "cannot determine va_kernel_pa_offset since Linux 6.4\n");
>> +	else
>> +		ms->va_kernel_pa_offset = ms->kernel_link_addr - ms->phys_base;
>> +}
>> +
>>    static int
>>    riscv64_is_kvaddr(ulong vaddr)
>>    {
>> @@ -1352,6 +1373,7 @@ riscv64_init(int when)
>>    		riscv64_get_struct_page_size(machdep->machspec);
>>    		riscv64_get_va_bits(machdep->machspec);
>>    		riscv64_get_va_range(machdep->machspec);
>> +		riscv64_get_va_kernel_pa_offset(machdep->machspec);
>>    
>>    		pt_level_alloc(&machdep->pgd, "cannot malloc pgd space.");
>>    		pt_level_alloc(&machdep->machspec->p4d, "cannot malloc p4d space.");
diff mbox series

Patch

diff --git a/defs.h b/defs.h
index 358f365..46b9857 100644
--- a/defs.h
+++ b/defs.h
@@ -3662,8 +3662,7 @@  typedef signed int s32;
 	ulong _X = X;									\
 	(THIS_KERNEL_VERSION >= LINUX(5,13,0) &&					\
 		(_X) >= machdep->machspec->kernel_link_addr) ?				\
-		(((unsigned long)(_X)-(machdep->machspec->kernel_link_addr)) +		\
-		 machdep->machspec->phys_base):						\
+		((unsigned long)(_X)-(machdep->machspec->va_kernel_pa_offset)): 	\
 		(((unsigned long)(_X)-(machdep->kvbase)) +				\
 		 machdep->machspec->phys_base);						\
 	})
@@ -7021,6 +7020,7 @@  struct machine_specific {
 	ulong modules_vaddr;
 	ulong modules_end;
 	ulong kernel_link_addr;
+	ulong va_kernel_pa_offset;
 
 	ulong _page_present;
 	ulong _page_read;
diff --git a/riscv64.c b/riscv64.c
index 6b9a688..b9e50b4 100644
--- a/riscv64.c
+++ b/riscv64.c
@@ -418,6 +418,27 @@  error:
 	error(FATAL, "cannot get vm layout\n");
 }
 
+static void
+riscv64_get_va_kernel_pa_offset(struct machine_specific *ms)
+{
+	unsigned long kernel_version = riscv64_get_kernel_version();
+
+	/*
+	 * va_kernel_pa_offset is defined in Linux kernel since 6.5.
+	 */
+	if (kernel_version >= LINUX(6,5,0)) {
+		char *string;
+		if ((string = pc->read_vmcoreinfo("NUMBER(va_kernel_pa_offset)"))) {
+			ms->va_kernel_pa_offset = htol(string, QUIET, NULL);
+			free(string);
+		} else
+			error(FATAL, "cannot read va_kernel_pa_offset\n");
+	} else if (kernel_version >= LINUX(6,4,0))
+		error(FATAL, "cannot determine va_kernel_pa_offset since Linux 6.4\n");
+	else
+		ms->va_kernel_pa_offset = ms->kernel_link_addr - ms->phys_base;
+}
+
 static int
 riscv64_is_kvaddr(ulong vaddr)
 {
@@ -1352,6 +1373,7 @@  riscv64_init(int when)
 		riscv64_get_struct_page_size(machdep->machspec);
 		riscv64_get_va_bits(machdep->machspec);
 		riscv64_get_va_range(machdep->machspec);
+		riscv64_get_va_kernel_pa_offset(machdep->machspec);
 
 		pt_level_alloc(&machdep->pgd, "cannot malloc pgd space.");
 		pt_level_alloc(&machdep->machspec->p4d, "cannot malloc p4d space.");