x86/boot/KASLR: Parse ACPI table and limit KASLR to choosing immovable memory
v13 cover x86/boot/KASLR: Parse ACPI table and limit KASLR to choosing immovable memory
***Background:
People reported that KASLR may randomly choose some positions which are located in movable memory regions. This will break memory hotplug feature and make the movable memory chosen by KASLR can't be removed.
***Solutions:
Get the information of memory hot-remove, then KASLR will know the right regions. Information about memory hot-remove is in ACPI tables, which will be parsed after start_kernel(), so that KASLR can't get the information.
Somebody suggest to add a kernel parameter to specify the immovable memory so that limit KASLR in these regions. Then I make a PATCHSET. After several versions, Ingo gave a suggestion:
https://www.mail-archive.com/[email protected]/msg1634024.html
Follow Ingo's suggestion, imitate the ACPI code to parse the ACPI tables, so that the kaslr can get necessary memory information in ACPI tables.
I think ACPI code is an independent part, so imitate the codes and functions to 'compressed/' directory, so that kaslr won't influence the initialization of ACPI.
PATCH 1/6 Copy kstrtoull() to boot/string.c
PATCH 2/6 Add get_acpi_rsdp() to parse RSDP in cmdline from KEXEC PATCH 3/6 Add efi_get_rsdp_addr() to find RSDP from EFI table when
booting from EFI.
PATCH 4/6 Add bios_get_rsdp_addr() to search RSDP in memory when EFI
table not found.
PATCH 5/6 Compute SRAT from RSDP and walk SRAT to store the immovable
memory regions.
PATCH 6/6 Calculate the intersection between memory regions from e820/efi
memory table and immovable memory regions. Limit KASLR to choosing these regions for randomization.
v1->v2:
- Simplify some code.
Follow Baoquan He's suggestion:
- Reuse the head file of acpi code.
v2->v3:
- Test in more conditions, so remove the 'RFC' tag.
- Change some comments.
v3->v4:
Follow Thomas Gleixner's suggestion:
- Put the whole efi related function into #define CONFIG_EFI and return false in the other stub.
v4->v5:
Follow Dou Liyang's suggestion:
- Add more comments about some functions based on kernel code.
- Change some typo in comments.
- Clean useless variable.
- Add check for the boundary of array.
- Add check for 'movable_node' parameter
v5->v6:
Follow Baoquan He's suggestion:
- Change some log.
- Add the check for acpi_rsdp
- Change some code logical to make code clear
v6->v7:
Follow Rafael's suggestion:
- Add more comments and patch log.
Follow test robot's suggestion:
- Add "static" tag for function
v7-v8:
Follow Kees Cook's suggestion:
- Use mem_overlaps() to check memory region.
- Use #ifdef in the definition of function.
v8-v9:
Follow Boris' suggestion:
- Change code style.
- Splite PATCH 1/3 to more path.
- Introduce some new function
- Use existing function to rework some code
Follow Masayoshi's suggetion:
- Make code more readable
v9->v10:
Follow Baoquan's suggestion:
- Change some log
- Merge last two patch together.
v10->v11:
Follow Boris' suggestion:
- Link kstrtoull() instead of copying it.
- Drop the useless wrapped function.
v11->v12:
Follow Boris' suggestion:
- Change patch log and code comments.
- Add 'CONFIG_EARLY_PARSE_RSDP' to make code easy to read
- Put strtoull() to misc.c
Follow Masa's suggestion:
- Remove the detection for 'movable_node'
- Change the code logical about cmdline_find_option()
v12->v13:
Follow Boris' suggestion:
- Copy kstrtoull() to boot/string.c
Follow Masa's suggestion:
- Change some code logical
Follow Baoquan's suggestion:
- Add tag to disable export symbol
Any comments will be welcome.
Chao Fan (6):
x86/boot: Introduce kstrtoull() to boot directory instead of
simple_strtoull()
x86/boot: Introduce get_acpi_rsdp() to parse RSDP in cmdline from
KEXEC
x86/boot: Introduce efi_get_rsdp_addr() to find RSDP from EFI table x86/boot: Introduce bios_get_rsdp_addr() to search RSDP in memory x86/boot: Parse SRAT from RSDP and store immovable memory x86/boot/KASLR: Limit KASLR to extracting kernel in immovable memory
arch/x86/Kconfig | 10 + arch/x86/boot/compressed/Makefile | 2 + arch/x86/boot/compressed/acpi.c | 322 ++++++++++++++++++++++++++++++ arch/x86/boot/compressed/kaslr.c | 79 ++++++-- arch/x86/boot/compressed/misc.h | 21 ++ arch/x86/boot/string.c | 137 +++++++++++++ arch/x86/boot/string.h | 2 + 7 files changed, 558 insertions(+), 15 deletions(-) create mode 100644 arch/x86/boot/compressed/acpi.c
Signature · 1 line
2.19.2
PATCH v13 1/6 x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Introduce kstrtoull() from lib/kstrtox.c to boot directory so that code in boot/ can use kstrtoull() and the old simple_strtoull() can be replaced.
--- arch/x86/boot/string.c | 137 +++++++++++++++++++++++++++++++++++++++++ arch/x86/boot/string.h | 2 + 2 files changed, 139 insertions(+)
arch/x86/boot/string.c +137 -0
@@ -12,7 +12,10 @@* Very basic string functions */ +#define _LINUX_CTYPE_H#include <linux/types.h> +#include <linux/kernel.h> +#include <linux/errno.h> #include <asm/asm.h> #include "ctype.h" #include "string.h"
@@ -187,3 +190,137 @@ char *strchr(const char *s, int c)return NULL ;return (char *)s; } + +static inline u64 div_u64_rem (u64 dividend,u32 divisor,u32 *remainder) +{ +union { +u64 v64; +u32 v32[2 ]; + } d = { dividend }; +u32 upper; + + upper = d.v32[1 ]; + d.v32[1 ] =0 ; +if (upper >= divisor) { + d.v32[1 ] = upper / divisor; + upper %= divisor; + } + asm ("divl %2" :"=a" (d.v32[0 ]),"=d" (*remainder) : +"rm" (divisor),"0" (d.v32[0 ]),"1" (upper)); +return d.v64; +} + +static inline u64 div_u64 (u64 dividend,u32 divisor) +{ +u32 remainder; +return div_u64_rem (dividend, divisor, &remainder); +} + +static inline char _tolower (const char c) +{ +return c |0x20 ; +} + +const char *_parse_integer_fixup_radix (const char *s,unsigned int *base) +{ +if (*base ==0 ) { +if (s[0 ] =='0' ) { +if (_tolower (s[1 ]) =='x' &&isxdigit (s[2 ])) + *base =16 ; +else + *base =8 ; + }else + *base =10 ; + } +if (*base ==16 && s[0 ] =='0' &&_tolower (s[1 ]) =='x' ) + s +=2 ; +return s; +} + +/* +* Convert non-negative integer string representation in explicitly given radix +* to an integer. +* Return number of characters consumed maybe or-ed with overflow bit. +* If overflow occurs, result integer (incorrect) is still returned. +* +* Don't you dare use this function. +*/ +unsigned int _parse_integer (const char *s,unsigned int base,unsigned long long *p) +{ +unsigned long long res; +unsigned int rv; + + res =0 ; + rv =0 ; +while (1 ) { +unsigned int c = *s; +unsigned int lc = c |0x20 ;/* don't tolower() this line */ +unsigned int val; + +if ('0' <= c && c <='9' ) + val = c -'0' ; +else if ('a' <= lc && lc <='f' ) + val = lc -'a' +10 ; +else +break ; + +if (val >= base) +break ; +/* +* Check for overflow only if we are within range of +* it in the max base we support (16) +*/ +if (unlikely (res & (~0ull <<60 ))) { +if (res >div_u64 (ULLONG_MAX - val, base)) + rv |=KSTRTOX_OVERFLOW ; + } + res = res * base + val; + rv++; + s++; + } + *p = res; +return rv; +} + +static int _kstrtoull (const char *s,unsigned int base,unsigned long long *res) +{ +unsigned long long _res; +unsigned int rv; + + s =_parse_integer_fixup_radix (s, &base); + rv =_parse_integer (s, base, &_res); +if (rv &KSTRTOX_OVERFLOW ) +return -ERANGE ; +if (rv ==0 ) +return -EINVAL ; + s += rv; +if (*s =='\n' ) + s++; +if (*s) +return -EINVAL ; + *res = _res; +return 0 ; +} + +/** +* kstrtoull - convert a string to an unsigned long long +* @s: The start of the string. The string must be null-terminated, and may also +* include a single newline before its terminating null. The first character +* may also be a plus sign, but not a minus sign. +* @base: The number base to use. The maximum supported base is 16. If base is +* given as 0, then the base of the string is automatically detected with the +* conventional semantics - If it begins with 0x the number will be parsed as a +* hexadecimal (case insensitive), if it otherwise begins with 0, it will be +* parsed as an octal number. Otherwise it will be parsed as a decimal. +* @res: Where to write the result of the conversion on success. +* +* Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. +* Used as a replacement for the obsolete simple_strtoull. Return code must +* be checked. +*/ +int kstrtoull (const char *s,unsigned int base,unsigned long long *res) +{ +if (s[0 ] =='+' ) + s++; +return _kstrtoull (s, base, res); +}
arch/x86/boot/string.h +2 -0
@@ -29,4 +29,6 @@ extern unsigned int atou(const char *s);extern unsigned long long simple_strtoull (const char *cp,char **endp,unsigned int base); +int kstrtoull (const char *s,unsigned int base,unsigned long long *res); +#define KSTRTOX_OVERFLOW (1U << 31)#endif /* BOOT_STRING_H */
Signature · 1 line
2.19.2
Re: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Quoted 4 lines · Chao Fan
Introduce
kstrtoull()fromlib/kstrtox.cto boot directory so that code in boot/ can usekstrtoull()and the oldsimple_strtoull()can be replaced.
Hi all,
Thanks for Boris, Baoquan and Masa's help, this PATCHSET has proceeded to this step. With the talking in community, the key problem has been turned from ACPI issue to kstrtoull() issue.
In this version, following the suggestion of Boris, I copy the kstrtoull() to boot/string.c
But from last week, I was working on kstrtoull() issue in different methods and try many times, there are several methods:
1. Copy kstrtoull() to boot/string.c
2. Include kstrtoull() to boot/string.c.
3. Use existing simple_strtoull() for now, and proceed to include kstrtoull() as a next work.
For method 1, PATCH is here, it's easy and clear.
For method 2, I tried and met many ifdeffery problems:
First, the conflict of ctype.h header file.
Second, redefine of isdigit() and memcpy() and other functions. Third, 'CONFIG_MODVERSIONS=y' causes ld error
Forth, div_u64 causes make error.
Thanks to Baoquan and Masa, these problems have been fixed. I success to make, but more warning issues is waiting to be fixed. And I think several weeks are needed to handle the warning issue since I am not so familiar to fix these problems.
But it blocked the main job for a long time, so if people think the method 1 is not so good, I think method 3 is a choice.
Thanks,
Chao Fan
Quoted 173 lines
Signed-off-by Chao Fan <[email protected]>---
arch/x86/boot/string.c| 137 +++++++++++++++++++++++++++++++++++++++++arch/x86/boot/string.h| 2 +2 files changed, 139 insertions(+)arch/x86/boot/string.c +137
-0@@ -12,7 +12,10 @@* Very basic string functions */ +#define _LINUX_CTYPE_H#include <linux/types.h> +#include <linux/kernel.h> +#include <linux/errno.h> #include <asm/asm.h> #include "ctype.h" #include "string.h" @@ -187,3 +190,137 @@ char *strchr(const char *s, int c)return NULL ;return (char *)s; } + +static inline u64 div_u64_rem (u64 dividend,u32 divisor,u32 *remainder) +{ +union { +u64 v64; +u32 v32[2 ]; + } d = { dividend }; +u32 upper; + + upper = d.v32[1 ]; + d.v32[1 ] =0 ; +if (upper >= divisor) { + d.v32[1 ] = upper / divisor; + upper %= divisor; + } + asm ("divl %2" :"=a" (d.v32[0 ]),"=d" (*remainder) : +"rm" (divisor),"0" (d.v32[0 ]),"1" (upper)); +return d.v64; +} + +static inline u64 div_u64 (u64 dividend,u32 divisor) +{ +u32 remainder; +return div_u64_rem (dividend, divisor, &remainder); +} + +static inline char _tolower (const char c) +{ +return c |0x20 ; +} + +const char *_parse_integer_fixup_radix (const char *s,unsigned int *base) +{ +if (*base ==0 ) { +if (s[0 ] =='0' ) { +if (_tolower (s[1 ]) =='x' &&isxdigit (s[2 ])) + *base =16 ; +else + *base =8 ; + }else + *base =10 ; + } +if (*base ==16 && s[0 ] =='0' &&_tolower (s[1 ]) =='x' ) + s +=2 ; +return s; +} + +/* +* Convert non-negative integer string representation in explicitly given radix +* to an integer. +* Return number of characters consumed maybe or-ed with overflow bit. +* If overflow occurs, result integer (incorrect) is still returned. +* +* Don't you dare use this function. +*/ +unsigned int _parse_integer (const char *s,unsigned int base,unsigned long long *p) +{ +unsigned long long res; +unsigned int rv; + + res =0 ; + rv =0 ; +while (1 ) { +unsigned int c = *s; +unsigned int lc = c |0x20 ;/* don't tolower() this line */ +unsigned int val; + +if ('0' <= c && c <='9' ) + val = c -'0' ; +else if ('a' <= lc && lc <='f' ) + val = lc -'a' +10 ; +else +break ; + +if (val >= base) +break ; +/* +* Check for overflow only if we are within range of +* it in the max base we support (16) +*/ +if (unlikely (res & (~0ull <<60 ))) { +if (res >div_u64 (ULLONG_MAX - val, base)) + rv |=KSTRTOX_OVERFLOW ; + } + res = res * base + val; + rv++; + s++; + } + *p = res; +return rv; +} + +static int _kstrtoull (const char *s,unsigned int base,unsigned long long *res) +{ +unsigned long long _res; +unsigned int rv; + + s =_parse_integer_fixup_radix (s, &base); + rv =_parse_integer (s, base, &_res); +if (rv &KSTRTOX_OVERFLOW ) +return -ERANGE ; +if (rv ==0 ) +return -EINVAL ; + s += rv; +if (*s =='\n' ) + s++; +if (*s) +return -EINVAL ; + *res = _res; +return 0 ; +} + +/** +* kstrtoull - convert a string to an unsigned long long +* @s: The start of the string. The string must be null-terminated, and may also +* include a single newline before its terminating null. The first character +* may also be a plus sign, but not a minus sign. +* @base: The number base to use. The maximum supported base is 16. If base is +* given as 0, then the base of the string is automatically detected with the +* conventional semantics - If it begins with 0x the number will be parsed as a +* hexadecimal (case insensitive), if it otherwise begins with 0, it will be +* parsed as an octal number. Otherwise it will be parsed as a decimal. +* @res: Where to write the result of the conversion on success. +* +* Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. +* Used as a replacement for the obsolete simple_strtoull. Return code must +* be checked. +*/ +int kstrtoull (const char *s,unsigned int base,unsigned long long *res) +{ +if (s[0 ] =='+' ) + s++; +return _kstrtoull (s, base, res); +}arch/x86/boot/string.h +2
-0@@ -29,4 +29,6 @@ extern unsigned int atou(const char *s);extern unsigned long long simple_strtoull (const char *cp,char **endp,unsigned int base); +int kstrtoull (const char *s,unsigned int base,unsigned long long *res); +#define KSTRTOX_OVERFLOW (1U << 31)#endif /* BOOT_STRING_H */ Signature · 1 line
2.19.2
Re: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Quoted 8 lines · Chao Fan
Quoted 4 lines · Chao Fan
Introduce
kstrtoull()fromlib/kstrtox.cto boot directory so that code in boot/ can usekstrtoull()and the oldsimple_strtoull()can be replaced.Hi all,
Quoted 11 lines
Thanks for Boris, Baoquan and Masa's help, this PATCHSET has proceeded to this step. With the talking in community, the key problem has been turned from ACPI issue to
kstrtoull()issue.
In this version, following the suggestion of Boris, I copy thekstrtoull()toboot/string.c
But from last week, I was working onkstrtoull()issue in different methods and try many times, there are several methods:
1. Copykstrtoull()toboot/string.c
2. Includekstrtoull()toboot/string.c.
3. Use existingsimple_strtoull()for now, and proceed to includekstrtoull()as a next work.
If can incalude it to boot/string.c, that's surely the best. Since we don't need to worry about update kstrtoull() update from /lib/kstrtox.c. Currently simple_strtoull() is called in arch/x86/boot/compressed/kaslr.c and arch/x86/boot/early_serial_console.c, if not easy to include kstrtoull() to boot/string.c, copying it is also fine.
Surely, using the old simple_strtoull() is fine too, we can take its replacement into TODO list. This fix has blocked KASLR&hotplug combination long time, now we have to ask customsers to add 'nokaslr' always if they want to do memory hot add/remove on bare metal system.
See what other reviewers will say.
Thanks
Baoquan
Borislav Petkov
maintainerRe: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Quoted 2 lines · Baoquan He
Surely, using the old
simple_strtoull()is fine too, we can take its replacement into TODO list.
Yes, that would be nice.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Quoted 5 lines · Borislav Petkov
Quoted 2 lines · Baoquan He
Surely, using the old
simple_strtoull()is fine too, we can take its replacement into TODO list.Yes, that would be nice.
Thanks, then I can use simple_strtoull() for now.
Thanks,
Chao Fan
Quoted 8 lines
Signature · 5 lines
-- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
Borislav Petkov
maintainerRe: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Quoted 8 lines · Chao Fan
Quoted 5 lines · Borislav Petkov
Quoted 2 lines · Baoquan He
Surely, using the old
simple_strtoull()is fine too, we can take its replacement into TODO list.Yes, that would be nice.
Thanks, then I can use
simple_strtoull()for now.
I don't understand - patch 1 in your v13 has kstrtoull() added to boot/string.c. Why are you reverting back to simple_strtoull()?
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
In reply to Borislav PetkovQuoted 5 lines · Borislav Petkov
Quoted 2 lines · Baoquan He
Surely, using the old
simple_strtoull()is fine too, we can take its replacement into TODO list.Yes, that would be nice.
Oops, I thought you agree with what Baoquan said, so I send the new version and use simple_strtoull() back.
Thanks,
Chao Fan
Quoted 8 lines
Signature · 5 lines
-- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
Borislav Petkov
maintainerRe: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Quoted 2 lines · Chao Fan
Oops, I thought you agree with what Baoquan said, so I send the new version and use
simple_strtoull()back.
I could've misunderstood him. Lemme repeat what I mean:
- we should make kstrtoull() work because it is the right function to use. Which means, include this too:
https://lkml.kernel.org/r/[email protected]
- we should gradually start replacing simple_strtoull() in arch/x86/boot/ and eventually get rid of it. But that's for the TODO list, to take care of later.
- Take our time and stop with the hurrying and do the whole thing nice and clean.
Ok?
Or have I forgotten something?
If not, I'll continue looking at your v13.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Chao Fan
Oops, I thought you agree with what Baoquan said, so I send the new version and use
simple_strtoull()back.I could've misunderstood him. Lemme repeat what I mean:
Quoted 9 lines
- we should make
kstrtoull()work because it is the right function to use. Which means, include this too:https://lkml.kernel.org/r/[email protected]
- we should gradually start replacing
simple_strtoull()inarch/x86/boot/and eventually get rid of it. But that's for the TODO list, to take care of later.
OK, got it.
And I tested your solution(rename div_u64 as __div_u64, and div_u64_rem as __div_u64_rem)for the issue from test rebot, that works. Thank you very much.
Thanks,
Chao Fan
Quoted 18 lines
- Take our time and stop with the hurrying and do the whole thing nice and clean.
Ok?
Or have I forgotten something?
If not, I'll continue looking at your v13.
Thx.
Signature · 4 lines
Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
Re: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
In reply to Chao FanQuoted 1 line · Chao Fan
Introduce
kstrtoull()fromlib/kstrtox.cto boot directory so that code
It's not introducing kstrtoull(), just copying kstrtoull() from lib/kstrtox.c to boot.
Quoted 178 lines
in boot/ can use
kstrtoull()and the oldsimple_strtoull()can be replaced.Signed-off-by Chao Fan <[email protected]>--- arch/x86/boot/string.c | 137 +++++++++++++++++++++++++++++++++++++++++ arch/x86/boot/string.h | 2 + 2 files changed, 139 insertions(+)arch/x86/boot/string.c +137
-0@@ -12,7 +12,10 @@* Very basic string functions */ +#define _LINUX_CTYPE_H#include <linux/types.h> +#include <linux/kernel.h> +#include <linux/errno.h> #include <asm/asm.h> #include "ctype.h" #include "string.h" @@ -187,3 +190,137 @@ char *strchr(const char *s, int c)return NULL ;return (char *)s; } + +static inline u64 div_u64_rem (u64 dividend,u32 divisor,u32 *remainder) +{ +union { +u64 v64; +u32 v32[2 ]; + } d = { dividend }; +u32 upper; + + upper = d.v32[1 ]; + d.v32[1 ] =0 ; +if (upper >= divisor) { + d.v32[1 ] = upper / divisor; + upper %= divisor; + } + asm ("divl %2" :"=a" (d.v32[0 ]),"=d" (*remainder) : +"rm" (divisor),"0" (d.v32[0 ]),"1" (upper)); +return d.v64; +} + +static inline u64 div_u64 (u64 dividend,u32 divisor) +{ +u32 remainder; +return div_u64_rem (dividend, divisor, &remainder); +} + +static inline char _tolower (const char c) +{ +return c |0x20 ; +} + +const char *_parse_integer_fixup_radix (const char *s,unsigned int *base) +{ +if (*base ==0 ) { +if (s[0 ] =='0' ) { +if (_tolower (s[1 ]) =='x' &&isxdigit (s[2 ])) + *base =16 ; +else + *base =8 ; + }else + *base =10 ; + } +if (*base ==16 && s[0 ] =='0' &&_tolower (s[1 ]) =='x' ) + s +=2 ; +return s; +} + +/* +* Convert non-negative integer string representation in explicitly given radix +* to an integer. +* Return number of characters consumed maybe or-ed with overflow bit. +* If overflow occurs, result integer (incorrect) is still returned. +* +* Don't you dare use this function. +*/ +unsigned int _parse_integer (const char *s,unsigned int base,unsigned long long *p) +{ +unsigned long long res; +unsigned int rv; + + res =0 ; + rv =0 ; +while (1 ) { +unsigned int c = *s; +unsigned int lc = c |0x20 ;/* don't tolower() this line */ +unsigned int val; + +if ('0' <= c && c <='9' ) + val = c -'0' ; +else if ('a' <= lc && lc <='f' ) + val = lc -'a' +10 ; +else +break ; + +if (val >= base) +break ; +/* +* Check for overflow only if we are within range of +* it in the max base we support (16) +*/ +if (unlikely (res & (~0ull <<60 ))) { +if (res >div_u64 (ULLONG_MAX - val, base)) + rv |=KSTRTOX_OVERFLOW ; + } + res = res * base + val; + rv++; + s++; + } + *p = res; +return rv; +} + +static int _kstrtoull (const char *s,unsigned int base,unsigned long long *res) +{ +unsigned long long _res; +unsigned int rv; + + s =_parse_integer_fixup_radix (s, &base); + rv =_parse_integer (s, base, &_res); +if (rv &KSTRTOX_OVERFLOW ) +return -ERANGE ; +if (rv ==0 ) +return -EINVAL ; + s += rv; +if (*s =='\n' ) + s++; +if (*s) +return -EINVAL ; + *res = _res; +return 0 ; +} + +/** +* kstrtoull - convert a string to an unsigned long long +* @s: The start of the string. The string must be null-terminated, and may also +* include a single newline before its terminating null. The first character +* may also be a plus sign, but not a minus sign. +* @base: The number base to use. The maximum supported base is 16. If base is +* given as 0, then the base of the string is automatically detected with the +* conventional semantics - If it begins with 0x the number will be parsed as a +* hexadecimal (case insensitive), if it otherwise begins with 0, it will be +* parsed as an octal number. Otherwise it will be parsed as a decimal. +* @res: Where to write the result of the conversion on success. +* +* Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. +* Used as a replacement for the obsolete simple_strtoull. Return code must +* be checked. +*/ +int kstrtoull (const char *s,unsigned int base,unsigned long long *res) +{ +if (s[0 ] =='+' ) + s++; +return _kstrtoull (s, base, res); +}arch/x86/boot/string.h +2
-0@@ -29,4 +29,6 @@ extern unsigned int atou(const char *s);extern unsigned long long simple_strtoull (const char *cp,char **endp,unsigned int base); +int kstrtoull (const char *s,unsigned int base,unsigned long long *res); +#define KSTRTOX_OVERFLOW (1U << 31)#endif /* BOOT_STRING_H */ Signature · 1 line
2.19.2
Re: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Quoted 5 lines · Baoquan He
Quoted 1 line · Chao Fan
Introduce
kstrtoull()fromlib/kstrtox.cto boot directory so that codeIt's not introducing
kstrtoull(), just copyingkstrtoull()fromlib/kstrtox.cto boot.
Oops, maybe I misunderstand 'introduce'.
Thanks,
Chao Fan
Quoted 181 lines
Quoted 179 lines
in boot/ can use
kstrtoull()and the oldsimple_strtoull()can be replaced.Signed-off-by Chao Fan <[email protected]>--- arch/x86/boot/string.c | 137 +++++++++++++++++++++++++++++++++++++++++ arch/x86/boot/string.h | 2 + 2 files changed, 139 insertions(+)arch/x86/boot/string.c +137
-0@@ -12,7 +12,10 @@* Very basic string functions */ +#define _LINUX_CTYPE_H#include <linux/types.h> +#include <linux/kernel.h> +#include <linux/errno.h> #include <asm/asm.h> #include "ctype.h" #include "string.h" @@ -187,3 +190,137 @@ char *strchr(const char *s, int c)return NULL ;return (char *)s; } + +static inline u64 div_u64_rem (u64 dividend,u32 divisor,u32 *remainder) +{ +union { +u64 v64; +u32 v32[2 ]; + } d = { dividend }; +u32 upper; + + upper = d.v32[1 ]; + d.v32[1 ] =0 ; +if (upper >= divisor) { + d.v32[1 ] = upper / divisor; + upper %= divisor; + } + asm ("divl %2" :"=a" (d.v32[0 ]),"=d" (*remainder) : +"rm" (divisor),"0" (d.v32[0 ]),"1" (upper)); +return d.v64; +} + +static inline u64 div_u64 (u64 dividend,u32 divisor) +{ +u32 remainder; +return div_u64_rem (dividend, divisor, &remainder); +} + +static inline char _tolower (const char c) +{ +return c |0x20 ; +} + +const char *_parse_integer_fixup_radix (const char *s,unsigned int *base) +{ +if (*base ==0 ) { +if (s[0 ] =='0' ) { +if (_tolower (s[1 ]) =='x' &&isxdigit (s[2 ])) + *base =16 ; +else + *base =8 ; + }else + *base =10 ; + } +if (*base ==16 && s[0 ] =='0' &&_tolower (s[1 ]) =='x' ) + s +=2 ; +return s; +} + +/* +* Convert non-negative integer string representation in explicitly given radix +* to an integer. +* Return number of characters consumed maybe or-ed with overflow bit. +* If overflow occurs, result integer (incorrect) is still returned. +* +* Don't you dare use this function. +*/ +unsigned int _parse_integer (const char *s,unsigned int base,unsigned long long *p) +{ +unsigned long long res; +unsigned int rv; + + res =0 ; + rv =0 ; +while (1 ) { +unsigned int c = *s; +unsigned int lc = c |0x20 ;/* don't tolower() this line */ +unsigned int val; + +if ('0' <= c && c <='9' ) + val = c -'0' ; +else if ('a' <= lc && lc <='f' ) + val = lc -'a' +10 ; +else +break ; + +if (val >= base) +break ; +/* +* Check for overflow only if we are within range of +* it in the max base we support (16) +*/ +if (unlikely (res & (~0ull <<60 ))) { +if (res >div_u64 (ULLONG_MAX - val, base)) + rv |=KSTRTOX_OVERFLOW ; + } + res = res * base + val; + rv++; + s++; + } + *p = res; +return rv; +} + +static int _kstrtoull (const char *s,unsigned int base,unsigned long long *res) +{ +unsigned long long _res; +unsigned int rv; + + s =_parse_integer_fixup_radix (s, &base); + rv =_parse_integer (s, base, &_res); +if (rv &KSTRTOX_OVERFLOW ) +return -ERANGE ; +if (rv ==0 ) +return -EINVAL ; + s += rv; +if (*s =='\n' ) + s++; +if (*s) +return -EINVAL ; + *res = _res; +return 0 ; +} + +/** +* kstrtoull - convert a string to an unsigned long long +* @s: The start of the string. The string must be null-terminated, and may also +* include a single newline before its terminating null. The first character +* may also be a plus sign, but not a minus sign. +* @base: The number base to use. The maximum supported base is 16. If base is +* given as 0, then the base of the string is automatically detected with the +* conventional semantics - If it begins with 0x the number will be parsed as a +* hexadecimal (case insensitive), if it otherwise begins with 0, it will be +* parsed as an octal number. Otherwise it will be parsed as a decimal. +* @res: Where to write the result of the conversion on success. +* +* Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. +* Used as a replacement for the obsolete simple_strtoull. Return code must +* be checked. +*/ +int kstrtoull (const char *s,unsigned int base,unsigned long long *res) +{ +if (s[0 ] =='+' ) + s++; +return _kstrtoull (s, base, res); +}arch/x86/boot/string.h +2
-0@@ -29,4 +29,6 @@ extern unsigned int atou(const char *s);extern unsigned long long simple_strtoull (const char *cp,char **endp,unsigned int base); +int kstrtoull (const char *s,unsigned int base,unsigned long long *res); +#define KSTRTOX_OVERFLOW (1U << 31)#endif /* BOOT_STRING_H */ Signature · 1 line
2.19.2
Borislav Petkov
maintainerRe: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
In reply to Chao FanQuoted 38 lines · Chao Fan
Introduce
kstrtoull()fromlib/kstrtox.cto boot directory so that code in boot/ can usekstrtoull()and the oldsimple_strtoull()can be replaced.Signed-off-by Chao Fan <[email protected]>--- arch/x86/boot/string.c | 137 +++++++++++++++++++++++++++++++++++++++++ arch/x86/boot/string.h | 2 + 2 files changed, 139 insertions(+)arch/x86/boot/string.c +12
-0@@ -12,7 +12,10 @@* Very basic string functions */ +#define _LINUX_CTYPE_H#include <linux/types.h> +#include <linux/kernel.h> +#include <linux/errno.h> #include <asm/asm.h> #include "ctype.h" #include "string.h" @@ -187,3 +190,137 @@ char *strchr(const char *s, int c)return NULL ;return (char *)s; } + +static inline u64 div_u64_rem (u64 dividend,u32 divisor,u32 *remainder) +{ +union { +u64 v64; +u32 v32[2 ]; + } d = { dividend }; +u32 upper; +
arch/x86/boot/string.c char *strchr(const char *s, int c) · line 211
Quoted 10 lines
arch/x86/boot/string.c +10
-0+ upper = d.v32[1 ]; + d.v32[1 ] =0 ; +if (upper >= divisor) { + d.v32[1 ] = upper / divisor; + upper %= divisor; + } + asm ("divl %2" :"=a" (d.v32[0 ]),"=d" (*remainder) : +"rm" (divisor),"0" (d.v32[0 ]),"1" (upper)); +return d.v64; +}
Why aren't you using the simple one from include/linux/math64.h ?
Rest looks ok.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Quoted 12 lines
Quoted 12 lines
arch/x86/boot/string.c +12
-0+static inline u64 div_u64_rem (u64 dividend,u32 divisor,u32 *remainder) +{ +union { +u64 v64; +u32 v32[2 ]; + } d = { dividend }; +u32 upper; + + upper = d.v32[1 ]; + d.v32[1 ] =0 ; +if (upper >= divisor) { + d.v32[1 ] = upper / divisor;
Quoted 8 lines
arch/x86/boot/string.c char *strchr(const char *s, int c) · line 211
Quoted 6 lines
arch/x86/boot/string.c +6
-0+ upper %= divisor; + } + asm ("divl %2" :"=a" (d.v32[0 ]),"=d" (*remainder) : +"rm" (divisor),"0" (d.v32[0 ]),"1" (upper)); +return d.v64; +}Why aren't you using the simple one from
include/linux/math64.h?
Cant't use div_u64() in math64.h directly, because that will cause ld error.
Even in 64-bit environment, arch/x86/boot/*.o will be built as 32-bit binary. In 64-bit binary, the dividend is handled as 64-bit value, that's OK, but in 32-bit binary, that's wrong. So it's necessary to separate the dividend to upper and lower in 32-bit binary. That's why copy the needed div_u64() here.
So, when copying kstrtoull(), we should copy div_u64().
When trying to include lib/kstrtox.c, even if other error in make period are solved, we will also meet this error. We should also copy div_u64 also. And then cover the math64.h in boot/string.c, and hanlde the other error and warining who comes together.
So just like Baoquan said, it's a good choice to use simple_strtoull() for now.
Thanks,
Chao Fan
Quoted 10 lines
Rest looks ok.
Signature · 4 lines
Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
Borislav Petkov
maintainerRe: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Quoted 1 line · Chao Fan
Cant't use
div_u64()inmath64.hdirectly, because that will cause ld error.
Ok, I see it now.
Quoted 5 lines
Even in 64-bit environment,
arch/x86/boot/*.owill be built as 32-bit binary. In 64-bit binary, the dividend is handled as 64-bit value, that's OK, but in 32-bit binary, that's wrong. So it's necessary to separate the dividend to upper and lower in 32-bit binary. That's why copy the neededdiv_u64()here.
You could've written this in the commit message. This is an important piece of information.
Quoted 2 lines
So just like Baoquan said, it's a good choice to use
simple_strtoull()for now.
So, we tried a little, we encountered a problem and now we're giving up? Is that what you're trying to tell me?
Please explain what are we in a hurry for and why aren't we doing it right?
Btw, one more thing I noticed:
Review
Quoted 11 lines
(quoted diff) +11
-0+static acpi_physical_address get_acpi_rsdp(void) +{ +#ifdef CONFIG_KEXEC + unsigned long long res; + int len = 0; + char val[MAX_ADDRESS_LENGTH+1]; + + len = cmdline_find_option("acpi_rsdp", val, MAX_ADDRESS_LENGTH+1); + if (len > 0) { + val[len] = 0; + return (acpi_physical_address)kstrtoull(val, 16, &res);
This would've never worked because the parsed integer gets returned in res but you're returning the kstrtoull() return value, which is
"Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error."
So this could not have been tested successfully, if at all...
So let's please slow down, think about it good, do it right and test it properly before sending next time, ok?
Thanks.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.kbuild test robot
build botRe: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
In reply to Chao FanThank you for the patch! Yet something to improve:
Hi Chao,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v4.20-rc6 next-20181214]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Chao-Fan/x86-boot-KASLR-Parse-ACPI-table-and-limit-KASLR-to-choosing-immovable-memory/20181214-082218
config: i386-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
# save the attached .config to linux build tree
make ARCH=i386 All error/warnings (new ones prefixed by >>):
In file included from include/linux/kernel.h:207:0,
from arch/x86/boot/string.c:17:
>> arch/x86/include/asm/div64.h:61:21: error: redefinition of 'div_u64_rem'
#define div_u64_rem div_u64_rem
^
>> arch/x86/boot/string.c:194:19: note: in expansion of macro 'div_u64_rem' static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
^~~~~~~~~~~
arch/x86/include/asm/div64.h:43:19: note: previous definition of 'div_u64_rem' was here
static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
^~~~~~~~~~~
--
In file included from include/linux/kernel.h:207:0,
from arch/x86/boot/compressed/../string.c:17,
from arch/x86/boot/compressed/string.c:11:
>> arch/x86/include/asm/div64.h:61:21: error: redefinition of 'div_u64_rem'
#define div_u64_rem div_u64_rem
^
>> arch/x86/boot/compressed/../string.c:194:19: note: in expansion of macro 'div_u64_rem' static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
^~~~~~~~~~~
arch/x86/include/asm/div64.h:43:19: note: previous definition of 'div_u64_rem' was here
static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
^~~~~~~~~~~
vim +/div_u64_rem +61 arch/x86/include/asm/div64.h
428c5a23 include/asm-x86/div64.h Chris Snook 2007-10-20 42
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 43 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 44 {
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 45 union {
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 46 u64 v64;
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 47 u32 v32[2];
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 48 } d = { dividend };
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 49 u32 upper;
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 50
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 51 upper = d.v32[1];
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 52 d.v32[1] = 0;
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 53 if (upper >= divisor) {
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 54 d.v32[1] = upper / divisor;
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 55 upper %= divisor;
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 56 }
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 57 asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) :
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 58 "rm" (divisor), "0" (d.v32[0]), "1" (upper));
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 59 return d.v64;
2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 60 }2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 @61 #define div_u64_rem div_u64_rem2418f4f2 include/asm-x86/div64.h Roman Zippel 2008-05-01 62
:::::: The code at line 61 was first introduced by commit
:::::: 2418f4f28f8467b92a6177af32d05737ebf6206c introduce explicit signed/unsigned 64bit divide
:::::: TO: Roman Zippel <[email protected]> :::::: CC: Linus Torvalds <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Borislav Petkov
maintainerRe: [PATCH v13 1/6] x86/boot: Introduce kstrtoull() to boot directory instead of simple_strtoull()
Quoted 12 lines · kbuild test robot
Hi Chao,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v4.20-rc6 next-20181214]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]url: https://github.com/0day-ci/linux/commits/Chao-Fan/x86-boot-KASLR-Parse-ACPI-table-and-limit-KASLR-to-choosing-immovable-memory/20181214-082218
config: i386-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0 reproduce:
Quoted 8 lines
# save the attached .config to linux build tree make ARCH=i386All error/warnings (new ones prefixed by >>):
In file included from
include/linux/kernel.h:207:0,from arch/x86/boot/string.c:17:Quoted 1 line
Quoted 1 line
arch/x86/include/asm/div64.h:61:21: error: redefinition of 'div_u64_rem'
Thanks for the report. We need something like this I guess:
---
arch/x86/boot/string.c +2 -2
@@ -191,7 +191,7 @@ char *strchr(const char *s, int c)return (char *)s; } -static inline u64 div_u64_rem (u64 dividend,u32 divisor,u32 *remainder) +static inline u64 __div_u64_rem (u64 dividend,u32 divisor,u32 *remainder) {union {u64 v64;
@@ -213,7 +213,7 @@ static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)static inline u64 div_u64 (u64 dividend,u32 divisor) {u32 remainder; -return div_u64_rem (dividend, divisor, &remainder); +return __div_u64_rem (dividend, divisor, &remainder); }static inline char _tolower (const char c)
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.PATCH v13 2/6 x86/boot: Introduce get_acpi_rsdp() to parse RSDP in cmdline from KEXEC
In reply to Chao FanMemory information in SRAT is necessary to fix the conflict between KASLR and memory-hotremove.
ACPI SRAT (System/Static Resource Affinity Table) shows the details about memory ranges, including ranges of memory provided by hot-added memory devices. SRAT is introduced by Root System Description Pointer(RSDP). So RSDP should be found firstly.
When booting form KEXEC/EFI/BIOS, the methods to find RSDP are different. When booting from KEXEC, 'acpi_rsdp' may have been added to cmdline, so parse cmdline to find RSDP.
Since 'RANDOMIZE_BASE' && 'MEMORY_HOTREMOVE' is needed, introduce 'CONFIG_EARLY_PARSE_RSDP' to make ifdeffery clear.
--- arch/x86/Kconfig | 10 ++++++++++ arch/x86/boot/compressed/acpi.c | 30 ++++++++++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 6 ++++++ 3 files changed, 46 insertions(+) create mode 100644 arch/x86/boot/compressed/acpi.c
arch/x86/Kconfig +10 -0
@@ -2149,6 +2149,16 @@ config X86_NEED_RELOCS def_bool y depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) +config EARLY_PARSE_RSDP + bool "Parse RSDP pointer on compressed period for KASLR" + def_bool y + depends on RANDOMIZE_BASE && MEMORY_HOTREMOVE + help + This option parses RSDP in compressed period. Works + for KASLR to get memory information from SRAT table and choose + immovable memory to extract kernel. + Say Y if you want to use both KASLR and memory-hotremove. + config PHYSICAL_ALIGN hex "Alignment value to which kernel should be aligned" default "0x200000"
arch/x86/boot/compressed/acpi.c new file +30 -0
@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +#define BOOT_CTYPE_H +#include "misc.h" +#include "error.h" + +#include <linux/efi.h> +#include <linux/numa.h> +#include <linux/acpi.h> +#include <asm/efi.h> + +#define STATIC +#include <linux/decompress/mm.h> + +#include "../string.h" + +static acpi_physical_address get_acpi_rsdp (void ) +{ +#ifdef CONFIG_KEXEC +unsigned long long res; +int len =0 ; +char val[MAX_ADDRESS_LENGTH +1 ]; + + len =cmdline_find_option ("acpi_rsdp" , val,MAX_ADDRESS_LENGTH +1 ); +if (len >0 ) { + val[len] =0 ; +return (acpi_physical_address )kstrtoull (val,16 , &res); + } +return 0 ; +#endif +}
arch/x86/boot/compressed/misc.h +6 -0
@@ -116,3 +116,9 @@ static inline void console_init(void)void set_sev_encryption_mask (void );#endif + +/* acpi.c */ +#ifdef CONFIG_EARLY_PARSE_RSDP +/* Max length of 64-bit hex address string is 18, prefix "0x" + 16 hex digit. */ +#define MAX_ADDRESS_LENGTH 18 +#endif
Signature · 1 line
2.19.2
Re: [PATCH v13 2/6] x86/boot: Introduce get_acpi_rsdp() to parse RSDP in cmdline from KEXEC
Hi Chao,
Great work! Let me say some trivial comments.
Quoted 64 lines · Chao Fan
Memory information in SRAT is necessary to fix the conflict between KASLR and memory-hotremove.
ACPI SRAT (System/Static Resource Affinity Table) shows the details about memory ranges, including ranges of memory provided by hot-added memory devices. SRAT is introduced by Root System Description Pointer(RSDP). So RSDP should be found firstly.
When booting form KEXEC/EFI/BIOS, the methods to find RSDP are different. When booting from KEXEC, '
acpi_rsdp' may have been added to cmdline, so parse cmdline to find RSDP.Since '
RANDOMIZE_BASE' && 'MEMORY_HOTREMOVE' is needed, introduce 'CONFIG_EARLY_PARSE_RSDP' to make ifdeffery clear.Signed-off-by Chao Fan <[email protected]>--- arch/x86/Kconfig | 10 ++++++++++ arch/x86/boot/compressed/acpi.c | 30 ++++++++++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 6 ++++++ 3 files changed, 46 insertions(+) create mode 100644 arch/x86/boot/compressed/acpi.carch/x86/Kconfig +10
-0@@ -2149,6 +2149,16 @@ config X86_NEED_RELOCS def_bool y depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) +config EARLY_PARSE_RSDP + bool "Parse RSDP pointer on compressed period for KASLR" + def_bool y + depends on RANDOMIZE_BASE && MEMORY_HOTREMOVE + help + This option parses RSDP in compressed period. Works + for KASLR to get memory information from SRAT table and choose + immovable memory to extract kernel. + Say Y if you want to use both KASLR and memory-hotremove. + config PHYSICAL_ALIGN hex "Alignment value to which kernel should be aligned" default "0x200000"arch/x86/boot/compressed/acpi.c new file +14
-0@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +#define BOOT_CTYPE_H +#include "misc.h" +#include "error.h" + +#include <linux/efi.h> +#include <linux/numa.h> +#include <linux/acpi.h> +#include <asm/efi.h> + +#define STATIC +#include <linux/decompress/mm.h> + +#include "../string.h"
arch/x86/boot/compressed/acpi.c line 23
Quoted 10 lines
arch/x86/boot/compressed/acpi.c new file +9
-0+ +static acpi_physical_address get_acpi_rsdp (void ) +{ +#ifdef CONFIG_KEXEC +unsigned long long res; +int len =0 ; +char val[MAX_ADDRESS_LENGTH +1 ]; + + len =cmdline_find_option ("acpi_rsdp" , val,MAX_ADDRESS_LENGTH +1 );
sizeof() is better here.
len = cmdline_find_option("acpi_rsdp", val, sizeof(var));arch/x86/boot/compressed/acpi.c line 25
Quoted 2 lines
arch/x86/boot/compressed/acpi.c new file +2
-0+if (len >0 ) { + val[len] =0 ;
'\0' should be fine here not 0.
val[len] = '\0';
Quoted 23 lines
arch/x86/boot/compressed/acpi.c new file +5
-0+ return (acpi_physical_address)kstrtoull(val, 16, &res); + } +return 0 ; +#endif +}arch/x86/boot/compressed/misc.h +6
-0@@ -116,3 +116,9 @@ static inline void console_init(void)void set_sev_encryption_mask (void );#endif + +/* acpi.c */ +#ifdef CONFIG_EARLY_PARSE_RSDP +/* Max length of 64-bit hex address string is 18, prefix "0x" + 16 hex digit. */ +#define MAX_ADDRESS_LENGTH 18 +#endif Signature · 1 line
2.19.2
Borislav Petkov
maintainerRe: [PATCH v13 2/6] x86/boot: Introduce get_acpi_rsdp() to parse RSDP in cmdline from KEXEC
Quoted 5 lines · Masayoshi Mizuma
arch/x86/boot/compressed/acpi.c line 23
Quoted 1 line
arch/x86/boot/compressed/acpi.c new file +1
-0+ len =cmdline_find_option ("acpi_rsdp" , val,MAX_ADDRESS_LENGTH +1 );
sizeof()is better here.len = cmdline_find_option("acpi_rsdp", val, sizeof(var));
Why is it better?
That makes you go look for the "val" variable and see what it's size is. MAX_ADDRESS_LENGTH+1 is OTOH explicit.
Quoted 7 lines
arch/x86/boot/compressed/acpi.c line 25
Quoted 3 lines
arch/x86/boot/compressed/acpi.c new file +2
-0+if (len >0 ) { + val[len] =0 ;'\0' should be fine here not 0.
val[len] = '\0';
Yes.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [PATCH v13 2/6] x86/boot: Introduce get_acpi_rsdp() to parse RSDP in cmdline from KEXEC
Quoted 11 lines · Borislav Petkov
Quoted 5 lines · Masayoshi Mizuma
arch/x86/boot/compressed/acpi.c line 23
Quoted 1 line
arch/x86/boot/compressed/acpi.c new file +1
-0+ len =cmdline_find_option ("acpi_rsdp" , val,MAX_ADDRESS_LENGTH +1 );
sizeof()is better here.len = cmdline_find_option("acpi_rsdp", val, sizeof(var));Why is it better?
That makes you go look for the "val" variable and see what it's size is. MAX_ADDRESS_LENGTH+1 is OTOH explicit.
Ah, thanks, make sense.
- Masa
Re: [PATCH v13 2/6] x86/boot: Introduce get_acpi_rsdp() to parse RSDP in cmdline from KEXEC
In reply to Masayoshi MizumaQuoted 3 lines · Masayoshi Mizuma
Hi Chao,
Great work! Let me say some trivial comments.
Thanks for your help, any comments will be welcome.
Quoted 79 lines · Chao Fan
Quoted 64 lines
Memory information in SRAT is necessary to fix the conflict between KASLR and memory-hotremove.
ACPI SRAT (System/Static Resource Affinity Table) shows the details about memory ranges, including ranges of memory provided by hot-added memory devices. SRAT is introduced by Root System Description Pointer(RSDP). So RSDP should be found firstly.
When booting form KEXEC/EFI/BIOS, the methods to find RSDP are different. When booting from KEXEC, '
acpi_rsdp' may have been added to cmdline, so parse cmdline to find RSDP.Since '
RANDOMIZE_BASE' && 'MEMORY_HOTREMOVE' is needed, introduce 'CONFIG_EARLY_PARSE_RSDP' to make ifdeffery clear.Signed-off-by Chao Fan <[email protected]>--- arch/x86/Kconfig | 10 ++++++++++ arch/x86/boot/compressed/acpi.c | 30 ++++++++++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 6 ++++++ 3 files changed, 46 insertions(+) create mode 100644 arch/x86/boot/compressed/acpi.carch/x86/Kconfig +10
-0@@ -2149,6 +2149,16 @@ config X86_NEED_RELOCS def_bool y depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) +config EARLY_PARSE_RSDP + bool "Parse RSDP pointer on compressed period for KASLR" + def_bool y + depends on RANDOMIZE_BASE && MEMORY_HOTREMOVE + help + This option parses RSDP in compressed period. Works + for KASLR to get memory information from SRAT table and choose + immovable memory to extract kernel. + Say Y if you want to use both KASLR and memory-hotremove. + config PHYSICAL_ALIGN hex "Alignment value to which kernel should be aligned" default "0x200000"arch/x86/boot/compressed/acpi.c new file +14
-0@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +#define BOOT_CTYPE_H +#include "misc.h" +#include "error.h" + +#include <linux/efi.h> +#include <linux/numa.h> +#include <linux/acpi.h> +#include <asm/efi.h> + +#define STATIC +#include <linux/decompress/mm.h> + +#include "../string.h" arch/x86/boot/compressed/acpi.c line 23
Quoted 10 lines
arch/x86/boot/compressed/acpi.c new file +9
-0+ +static acpi_physical_address get_acpi_rsdp (void ) +{ +#ifdef CONFIG_KEXEC +unsigned long long res; +int len =0 ; +char val[MAX_ADDRESS_LENGTH +1 ]; + + len =cmdline_find_option ("acpi_rsdp" , val,MAX_ADDRESS_LENGTH +1 );
sizeof()is better here.
Quoted 8 lines
len = cmdline_find_option("acpi_rsdp", val, sizeof(var));arch/x86/boot/compressed/acpi.c line 25
Quoted 2 lines
arch/x86/boot/compressed/acpi.c new file +2
-0+if (len >0 ) { + val[len] =0 ;'\0' should be fine here not 0.
val[len] = '\0';
Will change it.
Thanks,
Chao Fan
Quoted 26 lines
Quoted 24 lines
arch/x86/boot/compressed/acpi.c new file +5
-0+ return (acpi_physical_address)kstrtoull(val, 16, &res); + } +return 0 ; +#endif +}arch/x86/boot/compressed/misc.h +6
-0@@ -116,3 +116,9 @@ static inline void console_init(void)void set_sev_encryption_mask (void );#endif + +/* acpi.c */ +#ifdef CONFIG_EARLY_PARSE_RSDP +/* Max length of 64-bit hex address string is 18, prefix "0x" + 16 hex digit. */ +#define MAX_ADDRESS_LENGTH 18 +#endif Signature · 1 line
2.19.2
Borislav Petkov
maintainerRe: [PATCH v13 2/6] x86/boot: Introduce get_acpi_rsdp() to parse RSDP in cmdline from KEXEC
In reply to Chao FanQuoted 28 lines · Chao Fan
Memory information in SRAT is necessary to fix the conflict between KASLR and memory-hotremove.
ACPI SRAT (System/Static Resource Affinity Table) shows the details about memory ranges, including ranges of memory provided by hot-added memory devices. SRAT is introduced by Root System Description Pointer(RSDP). So RSDP should be found firstly.
When booting form KEXEC/EFI/BIOS, the methods to find RSDP are different. When booting from KEXEC, '
acpi_rsdp' may have been added to cmdline, so parse cmdline to find RSDP.Since '
RANDOMIZE_BASE' && 'MEMORY_HOTREMOVE' is needed, introduce 'CONFIG_EARLY_PARSE_RSDP' to make ifdeffery clear.Signed-off-by Chao Fan <[email protected]>--- arch/x86/Kconfig | 10 ++++++++++ arch/x86/boot/compressed/acpi.c | 30 ++++++++++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 6 ++++++ 3 files changed, 46 insertions(+) create mode 100644 arch/x86/boot/compressed/acpi.carch/x86/Kconfig +0
-0
arch/x86/Kconfig config X86_NEED_RELOCS · line 2152
Quoted 4 lines
arch/x86/Kconfig +1
-0@@ -2149,6 +2149,16 @@ config X86_NEED_RELOCS def_bool y depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) +config EARLY_PARSE_RSDP
That should be EARLY_SRAT_PARSE or so
arch/x86/Kconfig config X86_NEED_RELOCS · line 2153
Quoted 1 line
arch/x86/Kconfig +1
-0+ bool "Parse RSDP pointer on compressed period for KASLR"
and that should be
bool "Early SRAT table parsing"
arch/x86/Kconfig config X86_NEED_RELOCS · line 2157
Quoted 4 lines
arch/x86/Kconfig +4
-0+ def_bool y + depends on RANDOMIZE_BASE && MEMORY_HOTREMOVE + help + This option parses RSDP in compressed period. Works
do s/compressed period/compressed stage/g
I think "compressed stage" or "compressed boot stage" is more precise.
arch/x86/Kconfig config X86_NEED_RELOCS · line 2160
Quoted 3 lines
arch/x86/Kconfig +3
-0+ for KASLR to get memory information from SRAT table and choose + immovable memory to extract kernel. + Say Y if you want to use both KASLR and memory-hotremove.
This help text needs to explain what the functionality is for. "This option parses RSDP in compressed period" doesn't tell a whole lot to the normal user wondering whether she should enable this or not. You need to explain whether people would need this or not so should start along the lines of:
"This option enables early SRAT parsing so that memory hot remove ranges do not overlap with KASLR chosen ranges..."
Basically, the "***Background" in your 0th message.
Quoted 21 lines
arch/x86/Kconfig +1
-0+ config PHYSICAL_ALIGN hex "Alignment value to which kernel should be aligned" default "0x200000"arch/x86/boot/compressed/acpi.c new file +11
-0@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +#define BOOT_CTYPE_H +#include "misc.h" +#include "error.h" + +#include <linux/efi.h> +#include <linux/numa.h> +#include <linux/acpi.h> +#include <asm/efi.h> + +#define STATIC
arch/x86/boot/compressed/acpi.c line 21
Quoted 10 lines
arch/x86/boot/compressed/acpi.c new file +10
-0+#include <linux/decompress/mm.h> + +#include "../string.h" + +static acpi_physical_address get_acpi_rsdp (void ) +{ +#ifdef CONFIG_KEXEC +unsigned long long res; +int len =0 ; +char val[MAX_ADDRESS_LENGTH +1 ];
Please sort function local variables declaration in a reverse christmas tree order:
<type A> longest_variable_name; <type B> shorter_var_name; <type C> even_shorter; <type D> i;
arch/x86/boot/compressed/acpi.c line 23
Quoted 2 lines
arch/x86/boot/compressed/acpi.c new file +2
-0+ + len =cmdline_find_option ("acpi_rsdp" , val,MAX_ADDRESS_LENGTH +1 );
That MAX_ADDRESS_LENGTH define is used only here, right? So put it at the top in this file - no need to have it in a header.
arch/x86/boot/compressed/acpi.c line 29
Quoted 6 lines
arch/x86/boot/compressed/acpi.c new file +6
-0+if (len >0 ) { + val[len] =0 ; +return (acpi_physical_address )kstrtoull (val,16 , &res); + } +return 0 ; +#endif
Those two lines need to be the other way around:
#endif return 0;
because in the !CONFIG_KEXEC case that function won't return anything. gcc is smart enough to optimize that function away so that there's no build error but still.
Ditto for efi_get_rsdp_addr() in your next patch.
Quoted 7 lines
arch/x86/boot/compressed/acpi.c new file +1
-0+}arch/x86/boot/compressed/misc.h +0
-0
arch/x86/boot/compressed/misc.h static inline void console_init(void) · line 124
Quoted 9 lines
arch/x86/boot/compressed/misc.h +6
-0@@ -116,3 +116,9 @@ static inline void console_init(void)void set_sev_encryption_mask (void );#endif + +/* acpi.c */ +#ifdef CONFIG_EARLY_PARSE_RSDP +/* Max length of 64-bit hex address string is 18, prefix "0x" + 16 hex digit. */ +#define MAX_ADDRESS_LENGTH 18 +#endif
Yeah, just put that define in acpi.c.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [PATCH v13 2/6] x86/boot: Introduce get_acpi_rsdp() to parse RSDP in cmdline from KEXEC
Quoted 61 lines · Borislav Petkov
Quoted 28 lines · Chao Fan
Memory information in SRAT is necessary to fix the conflict between KASLR and memory-hotremove.
ACPI SRAT (System/Static Resource Affinity Table) shows the details about memory ranges, including ranges of memory provided by hot-added memory devices. SRAT is introduced by Root System Description Pointer(RSDP). So RSDP should be found firstly.
When booting form KEXEC/EFI/BIOS, the methods to find RSDP are different. When booting from KEXEC, '
acpi_rsdp' may have been added to cmdline, so parse cmdline to find RSDP.Since '
RANDOMIZE_BASE' && 'MEMORY_HOTREMOVE' is needed, introduce 'CONFIG_EARLY_PARSE_RSDP' to make ifdeffery clear.Signed-off-by Chao Fan <[email protected]>--- arch/x86/Kconfig | 10 ++++++++++ arch/x86/boot/compressed/acpi.c | 30 ++++++++++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 6 ++++++ 3 files changed, 46 insertions(+) create mode 100644 arch/x86/boot/compressed/acpi.carch/x86/Kconfig +0
-0arch/x86/Kconfig config X86_NEED_RELOCS · line 2152
Quoted 4 lines
arch/x86/Kconfig +1
-0@@ -2149,6 +2149,16 @@ config X86_NEED_RELOCS def_bool y depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) +config EARLY_PARSE_RSDPThat should be
EARLY_SRAT_PARSEor soarch/x86/Kconfig config X86_NEED_RELOCS · line 2153
Quoted 1 line
arch/x86/Kconfig +1
-0+ bool "Parse RSDP pointer on compressed period for KASLR"and that should be
bool "Early SRAT table parsing"
arch/x86/Kconfig config X86_NEED_RELOCS · line 2157
Quoted 4 lines
arch/x86/Kconfig +4
-0+ def_bool y + depends on RANDOMIZE_BASE && MEMORY_HOTREMOVE + help + This option parses RSDP in compressed period. Worksdo s/compressed period/compressed stage/g
I think "compressed stage" or "compressed boot stage" is more precise.
arch/x86/Kconfig config X86_NEED_RELOCS · line 2160
Quoted 3 lines
arch/x86/Kconfig +3
-0+ for KASLR to get memory information from SRAT table and choose + immovable memory to extract kernel. + Say Y if you want to use both KASLR and memory-hotremove.This help text needs to explain what the functionality is for. "This option parses RSDP in compressed period" doesn't tell a whole lot to the normal user wondering whether she should enable this or not. You need to explain whether people would need this or not so should start along the lines of:
Quoted 5 lines
"This option enables early SRAT parsing so that memory hot remove ranges do not overlap with KASLR chosen ranges..."
Basically, the "***Background" in your 0th message.
Thanks, I will rewrite this part.
Quoted 35 lines
Quoted 21 lines
arch/x86/Kconfig +1
-0+ config PHYSICAL_ALIGN hex "Alignment value to which kernel should be aligned" default "0x200000"arch/x86/boot/compressed/acpi.c new file +11
-0@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +#define BOOT_CTYPE_H +#include "misc.h" +#include "error.h" + +#include <linux/efi.h> +#include <linux/numa.h> +#include <linux/acpi.h> +#include <asm/efi.h> + +#define STATIC arch/x86/boot/compressed/acpi.c line 21
Quoted 10 lines
arch/x86/boot/compressed/acpi.c new file +10
-0+#include <linux/decompress/mm.h> + +#include "../string.h" + +static acpi_physical_address get_acpi_rsdp (void ) +{ +#ifdef CONFIG_KEXEC +unsigned long long res; +int len =0 ; +char val[MAX_ADDRESS_LENGTH +1 ];Please sort function local variables declaration in a reverse christmas tree order:
Quoted 4 lines
<type A> longest_variable_name; <type B> shorter_var_name; <type C> even_shorter; <type D> i;
I will change the position and check other places like this.
Quoted 16 lines
arch/x86/boot/compressed/acpi.c line 23
Quoted 3 lines
arch/x86/boot/compressed/acpi.c new file +2
-0+ + len =cmdline_find_option ("acpi_rsdp" , val,MAX_ADDRESS_LENGTH +1 );That
MAX_ADDRESS_LENGTHdefine is used only here, right? So put it at the top in this file - no need to have it in a header.arch/x86/boot/compressed/acpi.c line 29
Quoted 6 lines
arch/x86/boot/compressed/acpi.c new file +6
-0+if (len >0 ) { + val[len] =0 ; +return (acpi_physical_address )kstrtoull (val,16 , &res); + } +return 0 ; +#endif Those two lines need to be the other way around:
Quoted 8 lines
#endif return 0;because in the !CONFIG_KEXEC case that function won't return anything. gcc is smart enough to optimize that function away so that there's no build error but still.
Ditto for
efi_get_rsdp_addr()in your next patch.
Yes, I will change all places like this.
Thanks,
Chao Fan
Quoted 27 lines
Quoted 8 lines
arch/x86/boot/compressed/acpi.c new file +1
-0+}arch/x86/boot/compressed/misc.h +0
-0arch/x86/boot/compressed/misc.h static inline void console_init(void) · line 124
Quoted 9 lines
arch/x86/boot/compressed/misc.h +6
-0@@ -116,3 +116,9 @@ static inline void console_init(void)void set_sev_encryption_mask (void );#endif + +/* acpi.c */ +#ifdef CONFIG_EARLY_PARSE_RSDP +/* Max length of 64-bit hex address string is 18, prefix "0x" + 16 hex digit. */ +#define MAX_ADDRESS_LENGTH 18 +#endif Yeah, just put that define in
acpi.c.Signature · 4 lines
Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
PATCH v13 3/6 x86/boot: Introduce efi_get_rsdp_addr() to find RSDP from EFI table
In reply to Chao FanMemory information in SRAT is necessary to fix the conflict between KASLR and memory-hotremove. So RSDP and SRAT should be parsed.
When booting form KEXEC/EFI/BIOS, the methods to compute RSDP are different. When booting from EFI, EFI table points to RSDP. So parse the EFI table and find the RSDP.
--- arch/x86/boot/compressed/acpi.c | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+)
arch/x86/boot/compressed/acpi.c +79 -0
@@ -28,3 +28,82 @@ static acpi_physical_address get_acpi_rsdp(void)return 0 ;#endif } + +/* Search EFI table for RSDP. */ +static acpi_physical_address efi_get_rsdp_addr (void ) +{ +#ifdef CONFIG_EFI +acpi_physical_address rsdp_addr =0 ; +efi_system_table_t *systab; +struct efi_info *e; +bool efi_64; +char *sig; +int size; +int i; + + e = &boot_params->efi_info; + sig = (char *)&e->efi_loader_signature; + +if (!strncmp (sig,EFI64_LOADER_SIGNATURE ,4 )) + efi_64 = true; +else if (!strncmp (sig,EFI32_LOADER_SIGNATURE ,4 )) + efi_64 = false; +else { +debug_putstr ("Wrong EFI loader signature.\n" ); +return 0 ; + } + +/* Get systab from boot params. Based on efi_init(). */ +#ifdef CONFIG_X86_64 + systab = (efi_system_table_t *)(e->efi_systab | ((__u64 )e->efi_systab_hi<<32 )); +#else +if (e->efi_systab_hi || e->efi_memmap_hi) { +debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" ); +return 0 ; + } + systab = (efi_system_table_t *)e->efi_systab; +#endif + +if (!systab) +return 0 ; + +/* +* Get EFI tables from systab. Based on efi_config_init() and +* efi_config_parse_tables(). +*/ + size = efi_64 ?sizeof (efi_config_table_64_t) : +sizeof (efi_config_table_32_t); + +for (i =0 ; i < systab->nr_tables; i++) { +void *config_tables; +unsigned long table; +efi_guid_t guid; + + config_tables = (void *)(systab->tables + size * i); +if (efi_64) { +efi_config_table_64_t *tmp_table; + + tmp_table = (efi_config_table_64_t *)config_tables; + guid = tmp_table->guid; + table = tmp_table->table; + +if (!IS_ENABLED (CONFIG_X86_64 ) && table >>32 ) { +debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" ); +return 0 ; + } + }else { +efi_config_table_32_t *tmp_table; + + tmp_table = (efi_config_table_32_t *)config_tables; + guid = tmp_table->guid; + table = tmp_table->table; + } + +if (!(efi_guidcmp (guid,ACPI_TABLE_GUID ))) + rsdp_addr = (acpi_physical_address )table; +else if (!(efi_guidcmp (guid,ACPI_20_TABLE_GUID ))) +return (acpi_physical_address )table; + } +return rsdp_addr; +#endif +}
Signature · 1 line
2.19.2
Borislav Petkov
maintainerRe: [PATCH v13 3/6] x86/boot: Introduce efi_get_rsdp_addr() to find RSDP from EFI table
Quoted 34 lines · Chao Fan
Memory information in SRAT is necessary to fix the conflict between KASLR and memory-hotremove. So RSDP and SRAT should be parsed.
When booting form KEXEC/EFI/BIOS, the methods to compute RSDP are different. When booting from EFI, EFI table points to RSDP. So parse the EFI table and find the RSDP.
Signed-off-by Chao Fan <[email protected]>--- arch/x86/boot/compressed/acpi.c | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+)arch/x86/boot/compressed/acpi.c +14
-0@@ -28,3 +28,82 @@ static acpi_physical_address get_acpi_rsdp(void)return 0 ;#endif } + +/* Search EFI table for RSDP. */ +static acpi_physical_address efi_get_rsdp_addr (void ) +{ +#ifdef CONFIG_EFI +acpi_physical_address rsdp_addr =0 ; +efi_system_table_t *systab; +struct efi_info *e; +bool efi_64; +char *sig; +int size; +int i; + + e = &boot_params->efi_info;
arch/x86/boot/compressed/acpi.c static acpi_physical_address get_acpi_rsdp(void) · line 54
Quoted 10 lines
arch/x86/boot/compressed/acpi.c +10
-0+ sig = (char *)&e->efi_loader_signature; + +if (!strncmp (sig,EFI64_LOADER_SIGNATURE ,4 )) + efi_64 = true; +else if (!strncmp (sig,EFI32_LOADER_SIGNATURE ,4 )) + efi_64 = false; +else { +debug_putstr ("Wrong EFI loader signature.\n" ); +return 0 ; + }
Use curly braces for all three branches above.
Quoted 27 lines
arch/x86/boot/compressed/acpi.c +27
-0+ +/* Get systab from boot params. Based on efi_init(). */ +#ifdef CONFIG_X86_64 + systab = (efi_system_table_t *)(e->efi_systab | ((__u64 )e->efi_systab_hi<<32 )); +#else +if (e->efi_systab_hi || e->efi_memmap_hi) { +debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" ); +return 0 ; + } + systab = (efi_system_table_t *)e->efi_systab; +#endif + +if (!systab) +return 0 ; + +/* +* Get EFI tables from systab. Based on efi_config_init() and +* efi_config_parse_tables(). +*/ + size = efi_64 ?sizeof (efi_config_table_64_t) : +sizeof (efi_config_table_32_t); + +for (i =0 ; i < systab->nr_tables; i++) { +void *config_tables; +unsigned long table; +efi_guid_t guid; +
arch/x86/boot/compressed/acpi.c static acpi_physical_address get_acpi_rsdp(void) · line 91
Quoted 10 lines
arch/x86/boot/compressed/acpi.c +10
-0+ config_tables = (void *)(systab->tables + size * i); +if (efi_64) { +efi_config_table_64_t *tmp_table; + + tmp_table = (efi_config_table_64_t *)config_tables; + guid = tmp_table->guid; + table = tmp_table->table; + +if (!IS_ENABLED (CONFIG_X86_64 ) && table >>32 ) { +debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" );
Do s/system/config/ here so that this error message is different from the one above.
Quoted 7 lines
arch/x86/boot/compressed/acpi.c +7
-0+return 0 ; + } + }else { +efi_config_table_32_t *tmp_table; + + tmp_table = (efi_config_table_32_t *)config_tables; + guid = tmp_table->guid;
arch/x86/boot/compressed/acpi.c static acpi_physical_address get_acpi_rsdp(void) · line 108
Quoted 10 lines
arch/x86/boot/compressed/acpi.c +10
-0+ table = tmp_table->table; + } + +if (!(efi_guidcmp (guid,ACPI_TABLE_GUID ))) + rsdp_addr = (acpi_physical_address )table; +else if (!(efi_guidcmp (guid,ACPI_20_TABLE_GUID ))) +return (acpi_physical_address )table; + } +return rsdp_addr; +#endif
} #endif return rsdp_addr; }
IOW, you have:
static acpi_physical_address efi_get_rsdp_addr(void)
{
acpi_physical_address rsdp_addr = 0;
#ifdef CONFIG_EFI
...
#endif
return rsdp_addr;
}Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [PATCH v13 3/6] x86/boot: Introduce efi_get_rsdp_addr() to find RSDP from EFI table
Quoted 39 lines · Borislav Petkov
Quoted 38 lines · Chao Fan
Memory information in SRAT is necessary to fix the conflict between KASLR and memory-hotremove. So RSDP and SRAT should be parsed.
When booting form KEXEC/EFI/BIOS, the methods to compute RSDP are different. When booting from EFI, EFI table points to RSDP. So parse the EFI table and find the RSDP.
Signed-off-by Chao Fan <[email protected]>--- arch/x86/boot/compressed/acpi.c | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+)arch/x86/boot/compressed/acpi.c +18
-0@@ -28,3 +28,82 @@ static acpi_physical_address get_acpi_rsdp(void)return 0 ;#endif } + +/* Search EFI table for RSDP. */ +static acpi_physical_address efi_get_rsdp_addr (void ) +{ +#ifdef CONFIG_EFI +acpi_physical_address rsdp_addr =0 ; +efi_system_table_t *systab; +struct efi_info *e; +bool efi_64; +char *sig; +int size; +int i; + + e = &boot_params->efi_info; + sig = (char *)&e->efi_loader_signature; + +if (!strncmp (sig,EFI64_LOADER_SIGNATURE ,4 )) + efi_64 = true;
Quoted 9 lines
arch/x86/boot/compressed/acpi.c static acpi_physical_address get_acpi_rsdp(void) · line 54
Quoted 6 lines
arch/x86/boot/compressed/acpi.c +6
-0+else if (!strncmp (sig,EFI32_LOADER_SIGNATURE ,4 )) + efi_64 = false; +else { +debug_putstr ("Wrong EFI loader signature.\n" ); +return 0 ; + }Use curly braces for all three branches above.
OK, I will add in the first and second.
Quoted 32 lines
Quoted 32 lines
arch/x86/boot/compressed/acpi.c +32
-0+ +/* Get systab from boot params. Based on efi_init(). */ +#ifdef CONFIG_X86_64 + systab = (efi_system_table_t *)(e->efi_systab | ((__u64 )e->efi_systab_hi<<32 )); +#else +if (e->efi_systab_hi || e->efi_memmap_hi) { +debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" ); +return 0 ; + } + systab = (efi_system_table_t *)e->efi_systab; +#endif + +if (!systab) +return 0 ; + +/* +* Get EFI tables from systab. Based on efi_config_init() and +* efi_config_parse_tables(). +*/ + size = efi_64 ?sizeof (efi_config_table_64_t) : +sizeof (efi_config_table_32_t); + +for (i =0 ; i < systab->nr_tables; i++) { +void *config_tables; +unsigned long table; +efi_guid_t guid; + + config_tables = (void *)(systab->tables + size * i); +if (efi_64) { +efi_config_table_64_t *tmp_table; + + tmp_table = (efi_config_table_64_t *)config_tables;
Quoted 8 lines
arch/x86/boot/compressed/acpi.c static acpi_physical_address get_acpi_rsdp(void) · line 91
Quoted 5 lines
arch/x86/boot/compressed/acpi.c +5
-0+ guid = tmp_table->guid; + table = tmp_table->table; + +if (!IS_ENABLED (CONFIG_X86_64 ) && table >>32 ) { +debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" );Do s/system/config/ here so that this error message is different from the one above.
Yes, will change it.
Quoted 26 lines
Quoted 8 lines
arch/x86/boot/compressed/acpi.c +7
-0+return 0 ; + } + }else { +efi_config_table_32_t *tmp_table; + + tmp_table = (efi_config_table_32_t *)config_tables; + guid = tmp_table->guid;arch/x86/boot/compressed/acpi.c static acpi_physical_address get_acpi_rsdp(void) · line 108
Quoted 10 lines
arch/x86/boot/compressed/acpi.c +10
-0+ table = tmp_table->table; + } + +if (!(efi_guidcmp (guid,ACPI_TABLE_GUID ))) + rsdp_addr = (acpi_physical_address )table; +else if (!(efi_guidcmp (guid,ACPI_20_TABLE_GUID ))) +return (acpi_physical_address )table; + } +return rsdp_addr; +#endif } #endif return rsdp_addr; }IOW, you have:
Quoted 8 lines
static
acpi_physical_addressefi_get_rsdp_addr(void){ acpi_physical_address rsdp_addr = 0; #ifdef CONFIG_EFI ... #endif return rsdp_addr; }
Make sense. Thanks.
Thanks,
Chao Fan
Quoted 8 lines
Signature · 5 lines
-- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
Re: [PATCH v13 3/6] x86/boot: Introduce efi_get_rsdp_addr() to find RSDP from EFI table
In reply to Chao FanQuoted 60 lines · Chao Fan
Memory information in SRAT is necessary to fix the conflict between KASLR and memory-hotremove. So RSDP and SRAT should be parsed.
When booting form KEXEC/EFI/BIOS, the methods to compute RSDP are different. When booting from EFI, EFI table points to RSDP. So parse the EFI table and find the RSDP.
Signed-off-by Chao Fan <[email protected]>--- arch/x86/boot/compressed/acpi.c | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+)arch/x86/boot/compressed/acpi.c +40
-0@@ -28,3 +28,82 @@ static acpi_physical_address get_acpi_rsdp(void)return 0 ;#endif } + +/* Search EFI table for RSDP. */ +static acpi_physical_address efi_get_rsdp_addr (void ) +{ +#ifdef CONFIG_EFI +acpi_physical_address rsdp_addr =0 ; +efi_system_table_t *systab; +struct efi_info *e; +bool efi_64; +char *sig; +int size; +int i; + + e = &boot_params->efi_info; + sig = (char *)&e->efi_loader_signature; + +if (!strncmp (sig,EFI64_LOADER_SIGNATURE ,4 )) + efi_64 = true; +else if (!strncmp (sig,EFI32_LOADER_SIGNATURE ,4 )) + efi_64 = false; +else { +debug_putstr ("Wrong EFI loader signature.\n" ); +return 0 ; + } + +/* Get systab from boot params. Based on efi_init(). */ +#ifdef CONFIG_X86_64 + systab = (efi_system_table_t *)(e->efi_systab | ((__u64 )e->efi_systab_hi<<32 )); +#else +if (e->efi_systab_hi || e->efi_memmap_hi) { +debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" ); +return 0 ; + } + systab = (efi_system_table_t *)e->efi_systab; +#endif + +if (!systab) +return 0 ; + +/*
arch/x86/boot/compressed/acpi.c static acpi_physical_address get_acpi_rsdp(void) · line 79
Quoted 10 lines
arch/x86/boot/compressed/acpi.c +9
-0+* Get EFI tables from systab. Based on efi_config_init() and +* efi_config_parse_tables(). +*/ + size = efi_64 ?sizeof (efi_config_table_64_t) : +sizeof (efi_config_table_32_t); + +for (i =0 ; i < systab->nr_tables; i++) { +void *config_tables; +unsigned long table;
u64 table;
Otherwise, the following build warning happen in ARCH=i386.
===arch/x86/boot/compressed/acpi.c:96:44: warning: right shift count >= width of type [-Wshift-count-overflow]
if (!IS_ENABLED(CONFIG_X86_64) && table >> 32) {===
Thanks,
Masa
PATCH v13 4/6 x86/boot: Introduce bios_get_rsdp_addr() to search RSDP in memory
In reply to Chao FanTo fix the conflict between KASLR and memory-hotremove, memory Memory information in SRAT table is necessary to fix the conflict between KASLR and memory-hotremove. So RSDP and SRAT should be parsed.
When booting form KEXEC/EFI/BIOS, the methods to compute RSDP are different. When booting from BIOS, there is no variable who can point to RSDP directly, so scan memory for the RSDP and verify RSDP by signature and checksum.
--- arch/x86/boot/compressed/acpi.c | 85 +++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)
arch/x86/boot/compressed/acpi.c +85 -0
@@ -107,3 +107,88 @@ static acpi_physical_address efi_get_rsdp_addr(void)return rsdp_addr;#endif } + +static u8 compute_checksum (u8 *buffer,u32 length) +{ +u8 *end = buffer + length; +u8 sum =0 ; + +while (buffer < end) + sum += *(buffer++); + +return sum; +} + +/* Search a block of memory for the RSDP signature. */ +static u8 *scan_mem_for_rsdp (u8 *start,u32 length) +{ +struct acpi_table_rsdp *rsdp; +u8 *address; +u8 *end; + + end = start + length; + +/* Search from given start address for the requested length */ +for (address = start; address < end; address +=ACPI_RSDP_SCAN_STEP ) { +/* +* Both RSDP signature and checksum must be correct. +* Note: Sometimes there exists more than one RSDP in memory; +* the valid RSDP has a valid checksum, all others have an +* invalid checksum. +*/ + rsdp = (struct acpi_table_rsdp *)address; + +/* BAD Signature */ +if (!ACPI_VALIDATE_RSDP_SIG (rsdp->signature)) +continue ; + +/* Check the standard checksum */ +if (compute_checksum ((u8 *) rsdp,ACPI_RSDP_CHECKSUM_LENGTH )) +continue ; + +/* Check extended checksum if table version >= 2 */ +if ((rsdp->revision >=2 ) && + (compute_checksum ((u8 *) rsdp,ACPI_RSDP_XCHECKSUM_LENGTH ))) +continue ; + +/* Signature and checksum valid, we have found a real RSDP */ +return address; + } +return NULL ; +} + +/* Search RSDP address, based on acpi_find_root_pointer(). */ +static acpi_physical_address bios_get_rsdp_addr (void ) +{ +u8 *table_ptr; +u32 address; +u8 *rsdp; + +/* Get the location of the Extended BIOS Data Area (EBDA) */ + table_ptr = (u8 *)ACPI_EBDA_PTR_LOCATION ; + *(u32 *)(void *)&address = *(u16 *)(void *)table_ptr; + address <<=4 ; + table_ptr = (u8 *)(long )address; + +/* +* Search EBDA paragraphs (EBDA is required to be a minimum of +* 1K length) +*/ +if (address >0x400 ) { + rsdp =scan_mem_for_rsdp (table_ptr,ACPI_EBDA_WINDOW_SIZE ); +if (rsdp) { + address += (u32 )ACPI_PTR_DIFF (rsdp, table_ptr); +return (acpi_physical_address )address; + } + } + + table_ptr = (u8 *)ACPI_HI_RSDP_WINDOW_BASE ; + rsdp =scan_mem_for_rsdp (table_ptr,ACPI_HI_RSDP_WINDOW_SIZE ); + +/* Search upper memory: 16-byte boundaries in E0000h-FFFFFh */ +if (rsdp) { + address = (u32)(ACPI_HI_RSDP_WINDOW_BASE + +ACPI_PTR_DIFF (rsdp, table_ptr)); +return (acpi_physical_address )address; + } +}
Signature · 1 line
2.19.2
PATCH v13 5/6 x86/boot: Parse SRAT from RSDP and store immovable memory
In reply to Chao FanSRAT should be parsed by RSDP to fix the conflict between KASLR and memory-hotremove, then find the immovable memory regions and store them in an array called immovable_mem[]. With immovable_mem[], KASLR can avoid to extract kernel to specific regions.
--- arch/x86/boot/compressed/Makefile | 2 + arch/x86/boot/compressed/acpi.c | 128 ++++++++++++++++++++++++++++++ arch/x86/boot/compressed/kaslr.c | 4 - arch/x86/boot/compressed/misc.h | 15 ++++ 4 files changed, 145 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/Makefile +2 -0
@@ -84,6 +84,8 @@ ifdef CONFIG_X86_64 vmlinux-objs-y += $(obj)/pgtable_64.o endif +vmlinux-objs-$(CONFIG_EARLY_PARSE_RSDP) += $(obj)/acpi.o + $(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zone vmlinux-objs-$(CONFIG_EFI_STUB) += $(obj)/eboot.o $(obj)/efi_stub_$(BITS).o \
arch/x86/boot/compressed/acpi.c +128 -0
137 lines not on this page · raw message
arch/x86/boot/compressed/kaslr.c +0 -4
@@ -87,10 +87,6 @@ static unsigned long get_boot_seed(void)#define KASLR_COMPRESSED_BOOT #include "../../lib/kaslr.c" -struct mem_vector { -unsigned long long start; -unsigned long long size; -};/* Only supporting at most 4 unusable memmap regions with kaslr */ #define MAX_MEMMAP_REGIONS 4
arch/x86/boot/compressed/misc.h +15 -0
@@ -77,6 +77,11 @@ void choose_random_location(unsigned long input,unsigned long *output, unsigned long output_size, unsigned long *virt_addr); +struct mem_vector { +unsigned long long start; +unsigned long long size; +}; +/* cpuflags.c */ bool has_cpuflag (int flag);#else
@@ -118,7 +123,17 @@ void set_sev_encryption_mask(void);#endif /* acpi.c */ +#ifdef CONFIG_RANDOMIZE_BASE +/* Amount of immovable memory regions */ +int num_immovable_mem; +#endif +#ifdef CONFIG_EARLY_PARSE_RSDP +void get_immovable_mem (void );/* Max length of 64-bit hex address string is 18, prefix "0x" + 16 hex digit. */ #define MAX_ADDRESS_LENGTH 18 +#else +static void get_immovable_mem (void ) +{ +}#endif
Signature · 1 line
2.19.2
PATCH v13 6/6 x86/boot/KASLR: Limit KASLR to extracting kernel in immovable memory
In reply to Chao FanKASLR randomly chooses some positions which may locate in movable memory regions. It will break memory hotplug feature and make the movable memory chosen by KASLR practically immovable.
The solution is to limit KASLR to choose memory regions in immovable node according to SRAT tables.
When CONFIG_EARLY_PARSE_RSDP is enabled, walk through SRAT to get the information of immovable memory so that KASLR knows where should be chosen for randomization.
Rename process_mem_region() as __process_mem_region() and name new function as process_mem_region().
--- arch/x86/boot/compressed/kaslr.c | 75 +++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/kaslr.c +64 -11
@@ -97,6 +97,11 @@ static bool memmap_too_large;/* Store memory limit specified by "mem=nn[KMG]" or "memmap=nn[KMG]" */ static unsigned long long mem_limit =ULLONG_MAX ; +#ifdef CONFIG_EARLY_PARSE_RSDP +/* The immovable memory regions */ +extern struct mem_vector immovable_mem[MAX_NUMNODES *2 ]; +#endif +enum mem_avoid_index {MEM_AVOID_ZO_RANGE =0 ,
@@ -413,6 +418,9 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,/* Mark the memmap regions we need to avoid */ handle_mem_options (); +/* Mark the immovable regions we need to choose */ +get_immovable_mem (); +#ifdef CONFIG_X86_VERBOSE_BOOTUP /* Make sure video RAM can be used. */ add_identity_map (0 ,PMD_SIZE );
@@ -568,9 +576,9 @@ static unsigned long slots_fetch_random(void)return 0 ; } -static void process_mem_region (struct mem_vector *entry, -unsigned long minimum, -unsigned long image_size) +static void __process_mem_region (struct mem_vector *entry, +unsigned long minimum, +unsigned long image_size) {struct mem_vector region, overlap;unsigned long start_orig, end;
@@ -646,6 +654,57 @@ static void process_mem_region(struct mem_vector *entry, } } +static bool process_mem_region (struct mem_vector *region, +unsigned long long minimum, +unsigned long long image_size) +{ +int i; +/* +* If no immovable memory found, or MEMORY_HOTREMOVE disabled, +* walk all the regions, so use region directly. +*/ +if (!num_immovable_mem) { +__process_mem_region (region, minimum, image_size); + +if (slot_area_index ==MAX_SLOT_AREA ) { +debug_putstr ("Aborted e820/efi memmap scan (slot_areas full)!\n" ); +return 1 ; + } +return 0 ; + } + +#ifdef CONFIG_EARLY_PARSE_RSDP +/* +* If immovable memory found, filter the intersection between +* immovable memory and region to __process_mem_region(). +* Otherwise, go on old code. +*/ +for (i =0 ; i < num_immovable_mem; i++) { +struct mem_vector entry; +unsigned long long start, end, entry_end, region_end; + +if (!mem_overlaps (region, &immovable_mem[i])) +continue ; + + start = immovable_mem[i].start; + end = start + immovable_mem[i].size; + region_end = region->start + region->size; + + entry.start =clamp (region->start, start, end); + entry_end =clamp (region_end, start, end); + entry.size = entry_end - entry.start; + +__process_mem_region (&entry, minimum, image_size); + +if (slot_area_index ==MAX_SLOT_AREA ) { +debug_putstr ("Aborted e820/efi memmap scan (slot_areas full)!\n" ); +return 1 ; + } + } +return 0 ; +#endif +} +#ifdef CONFIG_EFI /* * Returns true if mirror region found (and must have been processed
@@ -711,11 +770,8 @@ process_efi_entries(unsigned long minimum, unsigned long image_size) region.start = md->phys_addr; region.size = md->num_pages <<EFI_PAGE_SHIFT ; -process_mem_region (®ion, minimum, image_size); -if (slot_area_index ==MAX_SLOT_AREA ) { -debug_putstr ("Aborted EFI scan (slot_areas full)!\n" ); +if (process_mem_region (®ion, minimum, image_size))break ; - } }return true; }
@@ -742,11 +798,8 @@ static void process_e820_entries(unsigned long minimum,continue ; region.start = entry->addr; region.size = entry->size; -process_mem_region (®ion, minimum, image_size); -if (slot_area_index ==MAX_SLOT_AREA ) { -debug_putstr ("Aborted e820 scan (slot_areas full)!\n" ); +if (process_mem_region (®ion, minimum, image_size))break ; - } } }
Signature · 1 line
2.19.2