Parse ACPI table and limit KASLR to choosing immovable memory
v16 cover 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 make the movable memory chosen by KASLR can't be removed.
***Solutions:
Get the information of memory hot-remove, then KASLR knows the right regions. Information about memory-hotremove is in ACPI SRAT, which will be parsed after start_kernel(), so KASLR can't get the information.
Someone suggested to add a kernel parameter to specify the immovable memory then 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.
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/7 Copy kstrtoull() to boot/string.c to instead of using
old simple_strtoull()
PATCH 2/7 Introduce get_acpi_rsdp() to parse RSDP in cmdline from KEXEC PATCH 3/6 Introduce efi_get_rsdp_addr() to find RSDP from EFI table when
booting from EFI.
PATCH 4/7 Introduce bios_get_rsdp_addr() to search RSDP in memory when
booting from BIOS
PATCH 5/7 Parse RSDP and fill in boot_params->acpi_rsdp_addr before
KASLR.
PATCH 6/7 Compute SRAT from RSDP and walk SRAT to store the immovable
memory regions and store the immovable memory regions.
PATCH 7/7 Calculate the intersection between memory regions from e820/efi
memory table and immovable memory regions. Limit KASLR to choosing these regions for randomization.
***Tests:
Test it in QEMU guest with 10 NUMA nodes, every node has 512M memory. Use earlyprintk with debug_putaddr() to display the immovable memory regions when KASLR walks all memory. Compare the result with dmesg. If they are same, the parsing job works well.
Here are test cases:
- x86_64 machine booting from EFI
- x86_64 machine booting from BIOS
- i386 machine booting from BIOS
- According to '/sys/firmware/efi/systab', add 'acpi_rsdp=' to cmdline to imitate case for get_acpi_rsdp()
- Add 'acpi=off' to cmdline to test the detecting for cmdline.
- Add debug_putaddr to test the value of boot_params->acpi_rsdp_addr
v15->v16:
- Splite the parsing of RSDP.
- Clean ACPI_* functions as local type cast
Follow Kairui Song's suggestion:
- Add code to parse boot_params->acpi_rsdp_addr
Follow Boris' suggetsion:
- Drop CONFIG_EARLY_SRAT_PARSE and build acpi.o
- Change get_acpi_srat_table() type and clean type cast.
- Change num_immovable_mem as static variable
- Change count_immovable_mem_regions() type to int.
v14->v15:
Follow Boris' suggestion:
- Fix the usage of kstrtoull()
- Add more test case
Follow Ingo's suggestion:
- Clear the typecast
- Change some variable name
- If no efi systab found, use error() to halt
Follow Baoquan's suggestion:
- Rebase from tip/master to tip/x86/boot
v13->v14:
Follow Baoquan's suggestion:
- Use simple_strtoull() for now.
Follow Boris' suggestion:
- Put 'return 0' out of 'ifdef' to make sure function return.
Follow Masa's suggestion:
- Change the end of string as '\0'
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
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()
v10->v11:
Follow Boris' suggestion:
- Link kstrtoull() instead of copying it.
- Drop the useless wrapped function.
v9->v10:
Follow Baoquan's suggestion:
- Change some log
- Merge last two patch together.
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
v7-v8:
Follow Kees Cook's suggestion:
- Use mem_overlaps() to check memory region.
- Use #ifdef in the definition of function.
v6->v7:
Follow Rafael's suggestion:
- Add more comments and patch log.
Follow test robot's suggestion:
- Add "static" tag for function
v5->v6:
Follow Baoquan He's suggestion:
- Change some log.
- Add the check for acpi_rsdp
- Change some code logical to make code clear
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
v3->v4:
Follow Thomas Gleixner's suggestion:
- Put the whole efi related function into #define CONFIG_EFI and return false in the other stub.
v2->v3:
- Test in more conditions, so remove the 'RFC' tag.
- Change some comments.
v1->v2:
- Simplify some code.
Follow Baoquan He's suggestion:
- Reuse the head file of acpi code.
Any comments will be welcome.
Chao Fan (7):
x86/boot: Copy kstrtoull() to boot/string.c 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: Early parse RSDP and fill in boot_params x86/boot: Parse SRAT address from RSDP and store immovable memory x86/boot/KASLR: Limit KASLR to extracting kernel in immovable memory
arch/x86/boot/compressed/Makefile | 2 + arch/x86/boot/compressed/acpi.c | 341 ++++++++++++++++++++++++++++++ arch/x86/boot/compressed/kaslr.c | 76 +++++-- arch/x86/boot/compressed/misc.c | 7 + arch/x86/boot/compressed/misc.h | 22 ++ arch/x86/boot/string.c | 141 ++++++++++++ arch/x86/boot/string.h | 1 + 7 files changed, 575 insertions(+), 15 deletions(-) create mode 100644 arch/x86/boot/compressed/acpi.c
Signature · 1 line
2.20.1
PATCH v16 1/7 x86/boot: Copy kstrtoull() to boot/string.c instead of simple_strtoull()
Copy kstrtoull() and necessary functions from lib/kstrtox.c to boot/string.c so that code in boot/ can use kstrtoull() and the old simple_strtoull() can be replaced.
In boot/string.c, using div_u64() from math64.h directly will cause the dividend handled as 64-bit value and bring ld error. The solution is to separate the dividend to upper and lower in boot/string.o. So copy the useful div_u64() and div_u64_rem() to boot/string.c also. To avoid redefinition in i386, rename them as __div_u64() and __div_u64_rem().
--- arch/x86/boot/string.c | 141 +++++++++++++++++++++++++++++++++++++++++ arch/x86/boot/string.h | 1 + 2 files changed, 142 insertions(+)
arch/x86/boot/string.c +141 -0
@@ -13,10 +13,14 @@*/ #include <linux/types.h> +#include <linux/kernel.h> +#include <linux/errno.h> #include <asm/asm.h> #include "ctype.h" #include "string.h" +#define KSTRTOX_OVERFLOW (1U << 31) +/* * Undef these macros so that the functions that we provide * here will have the correct names regardless of how string.h
@@ -187,3 +191,140 @@ 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 ; +} + +static 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. +*/ +static 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 +1 -0
@@ -29,4 +29,5 @@ 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);#endif /* BOOT_STRING_H */
Signature · 1 line
2.20.1
tip-bot for Chao Fan
merge bot[tip:x86/boot] x86/boot: Copy kstrtoull() to boot/string.c
x86/boot: Copy kstrtoull() to boot/string.c
Commit-ID: de50ce20cd05da4d1a7e5709a12fc23bc0b66be9 Gitweb: https://git.kernel.org/tip/de50ce20cd05da4d1a7e5709a12fc23bc0b66be9 Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:44 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:54 +0100
x86/boot: Copy kstrtoull() to boot/string.c
Copy kstrtoull() and the other necessary functions from lib/kstrtox.c to boot/string.c so that code in boot/ can use kstrtoull() and the old simple_strtoull() can gradually be phased out.
Using div_u64() from math64.h directly will cause the dividend to be handled as a 64-bit value and cause the infamous __divdi3 linker error due to gcc trying to use its library function for the 64-bit division.
Therefore, separate the dividend into an upper and lower part.
[ bp: Rewrite commit message. ]
--- arch/x86/boot/string.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++++ arch/x86/boot/string.h | 1 + 2 files changed, 142 insertions(+)
arch/x86/boot/string.c +141 -0
@@ -13,10 +13,14 @@*/ #include <linux/types.h> +#include <linux/kernel.h> +#include <linux/errno.h> #include <asm/asm.h> #include "ctype.h" #include "string.h" +#define KSTRTOX_OVERFLOW (1U << 31) +/* * Undef these macros so that the functions that we provide * here will have the correct names regardless of how string.h
@@ -187,3 +191,140 @@ 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 ; +} + +static 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. +*/ +static 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 +1 -0
@@ -29,4 +29,5 @@ 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);#endif /* BOOT_STRING_H */
PATCH v16 2/7 x86/boot: Introduce get_acpi_rsdp() to parse RSDP in cmdline from KEXEC
In reply to Chao FanKASLR may randomly choose some positions which are located in movable memory regions. This will make the movable memory chosen by KASLR can't be removed.
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.
--- arch/x86/boot/compressed/acpi.c | 32 ++++++++++++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 3 +++ 2 files changed, 35 insertions(+) create mode 100644 arch/x86/boot/compressed/acpi.c
arch/x86/boot/compressed/acpi.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +#define BOOT_CTYPE_H +#include "misc.h" +#include "error.h" +#include "../string.h" + +#ifdef CONFIG_ACPI + +/* +* Max length of 64-bit hex address string is 19, prefix "0x" + 16 hex +* digits, and '\0' for termination. +*/ +#define MAX_ADDR_LEN 19 + +static acpi_physical_address get_acpi_rsdp (void ) +{ +acpi_physical_address addr =0 ; + +#ifdef CONFIG_KEXEC +char val[MAX_ADDR_LEN ] = { }; +int ret; + + ret =cmdline_find_option ("acpi_rsdp" , val,MAX_ADDR_LEN ); +if (ret <0 ) +return 0 ; + +if (kstrtoull (val,16 , &addr)) +return 0 ; +#endif +return addr; +} +#endif /* CONFIG_ACPI */
arch/x86/boot/compressed/misc.h +3 -0
@@ -25,6 +25,9 @@#include <asm/bootparam.h> #include <asm/bootparam_utils.h> +#define BOOT_CTYPE_H +#include <linux/acpi.h> +#define BOOT_BOOT_H #include "../ctype.h"
Signature · 1 line
2.20.1
tip-bot for Chao Fan
merge bot[tip:x86/boot] x86/boot: Add "acpi_rsdp=" early parsing
x86/boot: Add "acpi_rsdp=" early parsing
Commit-ID: 3c98e71b42a7d0c14639ccb565ea315f3c887f92 Gitweb: https://git.kernel.org/tip/3c98e71b42a7d0c14639ccb565ea315f3c887f92 Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:45 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:54 +0100
x86/boot: Add "acpi_rsdp=" early parsing
KASLR may randomly choose offsets which are located in movable memory regions resulting in the movable memory becoming immovable.
The ACPI SRAT (System/Static Resource Affinity Table) describes memory ranges including ranges of memory provided by hot-added memory devices. In order to access SRAT, one needs the Root System Description Pointer (RSDP) with which to find the Root/Extended System Description Table (R/XSDT) which then contains the system description tables of which SRAT is one of.
In case the RSDP address has been passed on the command line (kexec-ing a second kernel) parse it from there.
[ bp: Rewrite the commit message and cleanup the code. ]
--- arch/x86/boot/compressed/acpi.c | 32 ++++++++++++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 3 +++ 2 files changed, 35 insertions(+)
arch/x86/boot/compressed/acpi.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +#define BOOT_CTYPE_H +#include "misc.h" +#include "error.h" +#include "../string.h" + +#ifdef CONFIG_ACPI + +/* +* Max length of 64-bit hex address string is 19, prefix "0x" + 16 hex +* digits, and '\0' for termination. +*/ +#define MAX_ADDR_LEN 19 + +static acpi_physical_address get_acpi_rsdp (void ) +{ +acpi_physical_address addr =0 ; + +#ifdef CONFIG_KEXEC +char val[MAX_ADDR_LEN ] = { }; +int ret; + + ret =cmdline_find_option ("acpi_rsdp" , val,MAX_ADDR_LEN ); +if (ret <0 ) +return 0 ; + +if (kstrtoull (val,16 , &addr)) +return 0 ; +#endif +return addr; +} +#endif /* CONFIG_ACPI */
arch/x86/boot/compressed/misc.h +3 -0
@@ -25,6 +25,9 @@#include <asm/bootparam.h> #include <asm/bootparam_utils.h> +#define BOOT_CTYPE_H +#include <linux/acpi.h> +#define BOOT_BOOT_H #include "../ctype.h"
PATCH v16 3/7 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 | 82 +++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)
arch/x86/boot/compressed/acpi.c +82 -0
@@ -4,6 +4,9 @@#include "error.h" #include "../string.h" +#include <linux/efi.h> +#include <asm/efi.h> +#ifdef CONFIG_ACPI /*
@@ -29,4 +32,83 @@ static acpi_physical_address get_acpi_rsdp(void)#endif return addr; } + +/* Search EFI table for RSDP. */ +static acpi_physical_address efi_get_rsdp_addr (void ) +{ +acpi_physical_address rsdp_addr =0 ; + +#ifdef CONFIG_EFI +efi_system_table_t *systab; +struct efi_info *ei; +bool efi_64; +int size, i; +char *sig; + + ei = &boot_params->efi_info; + sig = (char *)&ei->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 *)(ei->efi_systab | ((__u64 )ei->efi_systab_hi<<32 )); +#else +if (ei->efi_systab_hi || ei->efi_memmap_hi) { +debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" ); +return 0 ; + } + systab = (efi_system_table_t *)ei->efi_systab; +#endif + +if (!systab) +error ("EFI system table not found." ); + +/* +* 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++) { +acpi_physical_address table; +void *config_tables; +efi_guid_t guid; + + config_tables = (void *)(systab->tables + size * i); +if (efi_64) { +efi_config_table_64_t *tmp_table; + + tmp_table = 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 config table located above 4GB.\n" ); +return 0 ; + } + }else { +efi_config_table_32_t *tmp_table; + + tmp_table = config_tables; + guid = tmp_table->guid; + table = tmp_table->table; + } + +if (!(efi_guidcmp (guid,ACPI_TABLE_GUID ))) + rsdp_addr = table; +else if (!(efi_guidcmp (guid,ACPI_20_TABLE_GUID ))) +return table; + } +#endif +return rsdp_addr; +}#endif /* CONFIG_ACPI */
Signature · 1 line
2.20.1
tip-bot for Chao Fan
merge bot[tip:x86/boot] x86/boot: Search for RSDP in the EFI tables
x86/boot: Search for RSDP in the EFI tables
Commit-ID: 33f0df8d843deb9ec24116dcd79a40ca0ea8e8a9 Gitweb: https://git.kernel.org/tip/33f0df8d843deb9ec24116dcd79a40ca0ea8e8a9 Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:46 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:54 +0100
x86/boot: Search for RSDP in the EFI tables
The immovable memory ranges information in the SRAT table is necessary to fix the issue of KASLR not paying attention to movable memory regions when selecting the offset. Therefore, SRAT needs to be parsed.
Depending on the boot: KEXEC/EFI/BIOS, the methods to compute RSDP are different. When booting from EFI, the EFI table points to the RSDP. So iterate over the EFI system tables in order to find the RSDP.
[ bp:
- Heavily massage commit message
- Trim comments
- Move the CONFIG_ACPI ifdeffery into the Makefile.
]
--- arch/x86/boot/compressed/acpi.c | 81 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-)
arch/x86/boot/compressed/acpi.c +79 -2
@@ -4,7 +4,8 @@#include "error.h" #include "../string.h" -#ifdef CONFIG_ACPI +#include <linux/efi.h> +#include <asm/efi.h> /* * Max length of 64-bit hex address string is 19, prefix "0x" + 16 hex
@@ -29,4 +30,80 @@ static acpi_physical_address get_acpi_rsdp(void)#endif return addr; } -#endif /* CONFIG_ACPI */ + +/* Search EFI system tables for RSDP. */ +static acpi_physical_address efi_get_rsdp_addr (void ) +{ +acpi_physical_address rsdp_addr =0 ; + +#ifdef CONFIG_EFI +efi_system_table_t *systab; +struct efi_info *ei; +bool efi_64; +int size, i; +char *sig; + + ei = &boot_params->efi_info; + sig = (char *)&ei->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. */ +#ifdef CONFIG_X86_64 + systab = (efi_system_table_t *)(ei->efi_systab | ((__u64 )ei->efi_systab_hi<<32 )); +#else +if (ei->efi_systab_hi || ei->efi_memmap_hi) { +debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" ); +return 0 ; + } + systab = (efi_system_table_t *)ei->efi_systab; +#endif +if (!systab) +error ("EFI system table not found." ); + +/* +* Get EFI tables from systab. +*/ + size = efi_64 ?sizeof (efi_config_table_64_t) : +sizeof (efi_config_table_32_t); + +for (i =0 ; i < systab->nr_tables; i++) { +acpi_physical_address table; +void *config_tables; +efi_guid_t guid; + + config_tables = (void *)(systab->tables + size * i); +if (efi_64) { +efi_config_table_64_t *tmp_table; + + tmp_table = 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 config table located above 4GB.\n" ); +return 0 ; + } + }else { +efi_config_table_32_t *tmp_table; + + tmp_table = config_tables; + guid = tmp_table->guid; + table = tmp_table->table; + } + +if (!(efi_guidcmp (guid,ACPI_TABLE_GUID ))) + rsdp_addr = table; +else if (!(efi_guidcmp (guid,ACPI_20_TABLE_GUID ))) +return table; + } +#endif +return rsdp_addr; +}
PATCH v16 4/7 x86/boot: Introduce bios_get_rsdp_addr() to search RSDP in memory
In reply to Chao FanMemory 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 | 83 +++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+)
arch/x86/boot/compressed/acpi.c +83 -0
@@ -111,4 +111,87 @@ static acpi_physical_address efi_get_rsdp_addr(void)#endif return rsdp_addr; } + +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, *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 ) +{ +unsigned long address; +u8 *table_ptr, *rsdp; + +/* Get the location of the Extended BIOS Data Area (EBDA) */ + table_ptr = (u8 *)ACPI_EBDA_PTR_LOCATION ; + address = *(u16 *)table_ptr; + address <<=4 ; + table_ptr = (u8 *)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 = (unsigned long )rsdp; +return address; + } + } + +/* Search upper memory: 16-byte boundaries in E0000h-FFFFFh */ + table_ptr = (u8 *)ACPI_HI_RSDP_WINDOW_BASE ; + rsdp =scan_mem_for_rsdp (table_ptr,ACPI_HI_RSDP_WINDOW_SIZE ); +if (rsdp) { + address = (unsigned long )rsdp; +return address; + } + +return 0 ; +}#endif /* CONFIG_ACPI */
Signature · 1 line
2.20.1
tip-bot for Chao Fan
merge bot[tip:x86/boot] x86/boot: Search for RSDP in memory
x86/boot: Search for RSDP in memory
Commit-ID: 93a209aaaad495d7d0bc9b6186a4495934f70402 Gitweb: https://git.kernel.org/tip/93a209aaaad495d7d0bc9b6186a4495934f70402 Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:47 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:55 +0100
x86/boot: Search for RSDP in memory
Scan memory (EBDA) for the RSDP and verify RSDP by signature and checksum.
[ bp:
- Trim commit message.
- Simplify bios_get_rsdp_addr() and cleanup mad casting.
]
--- arch/x86/boot/compressed/acpi.c | 77 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+)
arch/x86/boot/compressed/acpi.c +77 -0
@@ -107,3 +107,80 @@ static acpi_physical_address efi_get_rsdp_addr(void)#endif return rsdp_addr; } + +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, *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 in EBDA. */ +static acpi_physical_address bios_get_rsdp_addr (void ) +{ +unsigned long address; +u8 *rsdp; + +/* Get the location of the Extended BIOS Data Area (EBDA) */ + address = *(u16 *)ACPI_EBDA_PTR_LOCATION ; + address <<=4 ; + +/* +* Search EBDA paragraphs (EBDA is required to be a minimum of +* 1K length) +*/ +if (address >0x400 ) { + rsdp =scan_mem_for_rsdp ((u8 *)address,ACPI_EBDA_WINDOW_SIZE ); +if (rsdp) +return (acpi_physical_address )(unsigned long )rsdp; + } + +/* Search upper memory: 16-byte boundaries in E0000h-FFFFFh */ + rsdp =scan_mem_for_rsdp ((u8 *)ACPI_HI_RSDP_WINDOW_BASE , +ACPI_HI_RSDP_WINDOW_SIZE ); +if (rsdp) +return (acpi_physical_address )(unsigned long )rsdp; + +return 0 ; +}
PATCH v16 5/7 x86/boot: Early parse RSDP and fill in boot_params
In reply to Chao FanRSDP is needed by both KASLR, so parse it early and fill it in boot_params before KASLR code.
RSDP is needed by lots of other code, it would be easier to have it always built-in instead of a long "depends on" line in Kconfig.
--- arch/x86/boot/compressed/Makefile | 2 ++ arch/x86/boot/compressed/acpi.c | 22 ++++++++++++++++++++++ arch/x86/boot/compressed/misc.c | 7 +++++++ arch/x86/boot/compressed/misc.h | 7 +++++++ 4 files changed, 38 insertions(+)
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-y += $(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 +22 -0
@@ -194,4 +194,26 @@ static acpi_physical_address bios_get_rsdp_addr(void) return 0; } + +/* +* Very early parse RSDP, based on acpi_os_get_root_pointer(). +* Return address of RSDP on success, 0 on failed to parse RSDP. +*/ +acpi_physical_address get_rsdp_addr (void ) +{ +acpi_physical_address pa; + + pa =get_acpi_rsdp (); + +if (!pa) + pa = boot_params->acpi_rsdp_addr; + +if (!pa) + pa =efi_get_rsdp_addr (); + +if (!pa) + pa =bios_get_rsdp_addr (); + +return pa; +}#endif /* CONFIG_ACPI */
arch/x86/boot/compressed/misc.c +7 -0
@@ -382,6 +382,13 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,debug_putaddr (trampoline_32bit);#endif +/* +* Very early parse RSDP and fill in boot_params. +* Early parsing is only executed once, and later anyone who +* needs RSDP can gain RSDP address by boot_params but not parse again. +*/ + boot_params->acpi_rsdp_addr =get_rsdp_addr (); +/* * The memory hole needed for the kernel is the larger of either * the entire decompressed kernel plus relocation table, or the
arch/x86/boot/compressed/misc.h +7 -0
@@ -119,3 +119,10 @@ static inline void console_init(void)void set_sev_encryption_mask (void );#endif + +/* acpi.c */ +#ifdef CONFIG_ACPI +acpi_physical_address get_rsdp_addr (void ); +#else +static inline acpi_physical_address get_rsdp_addr (void ) {return 0 ; } +#endif
Signature · 1 line
2.20.1
Re: [PATCH v16 5/7] x86/boot: Early parse RSDP and fill in boot_params
On Wed, Jan 23, 2019 at 07:08:48PM +0800, Chao Fan wrote:
[...]
Quoted 6 lines
#endif /*
CONFIG_ACPI*/arch/x86/boot/compressed/misc.c +0
-0
arch/x86/boot/compressed/misc.c asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, · line 391
Quoted 10 lines
arch/x86/boot/compressed/misc.c +7
-0@@ -382,6 +382,13 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,debug_putaddr (trampoline_32bit);#endif +/* +* Very early parse RSDP and fill in boot_params. +* Early parsing is only executed once, and later anyone who +* needs RSDP can gain RSDP address by boot_params but not parse again. +*/ + boot_params->acpi_rsdp_addr =get_rsdp_addr (); +
Hi Boris and Kairui,
I splite my v15 PATCH 5/6 to two patches, this v16 5/7 is new added. Since as Boris said much code such as Kairui's need RSDP where is not related to KASLR, but in my v15 version, the function entry is only in kaslr.c. So I expose get_rsdp_addr() as not static, and fill in boot_params without #ifdef KASLR issue ot MEMORY_HOTREMOVE and so on. More code can gain the RSDP by boot_params.
Since in KALSR, we need RSDP to parse SRAT and gain memroy information, so I put boot_params->acpi_rsdp_addr = get_rsdp_addr(); just before choose_random_location(), in which KASLR works. Then in this version v16 PATCH 6/7, only try to gain RSDP by boot_params and not parse again.
So I think this method can cover other people's usage, such as Kairui Song, how do you think this change.
Thanks,
Chao Fan
Quoted 21 lines
arch/x86/boot/compressed/misc.c +0
-0/* * The memory hole needed for the kernel is the larger of either * the entire decompressed kernel plus relocation table, or the arch/x86/boot/compressed/misc.h +7
-0@@ -119,3 +119,10 @@ static inline void console_init(void)void set_sev_encryption_mask (void );#endif + +/* acpi.c */ +#ifdef CONFIG_ACPI +acpi_physical_address get_rsdp_addr (void ); +#else +static inline acpi_physical_address get_rsdp_addr (void ) {return 0 ; } +#endif Signature · 1 line
2.20.1
tip-bot for Chao Fan
merge bot[tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
In reply to Chao Fanx86/boot: Early parse RSDP and save it in boot_params
Commit-ID: 3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Gitweb: https://git.kernel.org/tip/3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:48 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:55 +0100
x86/boot: Early parse RSDP and save it in boot_params
The RSDP is needed by KASLR so parse it early and save it in boot_params.acpi_rsdp_addr, before KASLR setup runs.
RSDP is needed by other kernel facilities so have the parsing code built-in instead of a long "depends on" line in Kconfig.
[ bp:
- Trim commit message and comments
- Add CONFIG_ACPI dependency in the Makefile
- Move ->acpi_rsdp_addr assignment with the rest of boot_params massaging in extract_kernel().
]
--- arch/x86/boot/compressed/Makefile | 2 ++ arch/x86/boot/compressed/acpi.c | 19 +++++++++++++++++++ arch/x86/boot/compressed/misc.c | 3 +++ arch/x86/boot/compressed/misc.h | 7 +++++++ 4 files changed, 31 insertions(+)
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_ACPI) += $(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 +19 -0
@@ -184,3 +184,22 @@ static acpi_physical_address bios_get_rsdp_addr(void)return 0 ; } + +/* Return RSDP address on success, otherwise 0. */ +acpi_physical_address get_rsdp_addr (void ) +{ +acpi_physical_address pa; + + pa =get_acpi_rsdp (); + +if (!pa) + pa = boot_params->acpi_rsdp_addr; + +if (!pa) + pa =efi_get_rsdp_addr (); + +if (!pa) + pa =bios_get_rsdp_addr (); + +return pa; +}
arch/x86/boot/compressed/misc.c +3 -0
@@ -351,6 +351,9 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,/* Clear flags intended for solely in-kernel use. */ boot_params->hdr.loadflags &= ~KASLR_FLAG ; +/* Save RSDP address for later use. */ + boot_params->acpi_rsdp_addr =get_rsdp_addr (); +sanitize_boot_params (boot_params);if (boot_params->screen_info.orig_video_mode ==7 ) {
arch/x86/boot/compressed/misc.h +7 -0
@@ -119,3 +119,10 @@ static inline void console_init(void)void set_sev_encryption_mask (void );#endif + +/* acpi.c */ +#ifdef CONFIG_ACPI +acpi_physical_address get_rsdp_addr (void ); +#else +static inline acpi_physical_address get_rsdp_addr (void ) {return 0 ; } +#endif
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Hi,
Quoted 12 lines · tip-bot for Chao Fan
Commit-ID: 3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Gitweb: https://git.kernel.org/tip/3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:48 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:55 +0100x86/boot: Early parse RSDP and save it in
boot_paramsThe RSDP is needed by KASLR so parse it early and save it in boot_params.acpi_rsdp_addr, before KASLR setup runs.
Quoted 9 lines
RSDP is needed by other kernel facilities so have the parsing code built-in instead of a long "depends on" line in
Kconfig.[ bp:
- Trim commit message and comments
- AddCONFIG_ACPIdependency in theMakefile
- Move ->acpi_rsdp_addr assignment with the rest ofboot_paramsmassaging inextract_kernel().
]
This patch causes x86_64 boots with efi32 bios to crash early and hard (before any console output is seen).
Sample qemu command line:
qemu-system-x86_64 -kernel arch/x86/boot/bzImage -M q35 -cpu SandyBridge \ -no-reboot -snapshot -smp 4 \ -bios OVMF-pure-efi-32.fd \ -m 1G \ -usb -device usb-storage,drive=d0 \ -drive file=rootfs.squashfs,if=none,id=d0,format=raw \ --append 'earlycon=uart8250,io,0x3f8,9600n8 root=/dev/sda rootwait panic=-1 console=ttyS0' \ -nographic
This is with qemu 3.1, though details other than "-bios OVMF-pure-efi-32.fd" don't really matter.
Files used above are available from https://github.com/groeck/linux-build-test/.
The patch can not be reverted alone, but
Revert "x86/boot/KASLR: Limit KASLR to extract the kernel in immovable memory only" Revert "x86/boot: Parse SRAT table and count immovable memory regions" Revert "x86/boot: Early parse RSDP and save it in boot_params"
fixes the problem.
Bisect log attached below.
Guenter
---
# bad: [a46228f6598a191e25ca160870f20c3cd1743d13] Add linux-next specific files for 20190208 # good: [8834f5600cf3c8db365e18a3d5cac2c2780c81e5] Linux 5.0-rc5
git bisect start 'HEAD' 'v5.0-rc5'
# good: [d5ee02f33ec5ade9c9afb9d45bfc4f5cb1375e58] Merge remote-tracking branch 'spi-nor/spi-nor/next' git bisect good d5ee02f33ec5ade9c9afb9d45bfc4f5cb1375e58
# good: [f34bff29873986d260d7a491fb6f543d12fbe7cd] Merge remote-tracking branch 'devicetree/for-next' git bisect good f34bff29873986d260d7a491fb6f543d12fbe7cd
# bad: [68182cb929ec6a676655887cbfb12b09662c1e5b] Merge remote-tracking branch 'icc/icc-next' git bisect bad 68182cb929ec6a676655887cbfb12b09662c1e5b
# bad: [0a5c6189664123753c298d90dec3a5a6b907f06f] Merge remote-tracking branch 'driver-core/driver-core-next' git bisect bad 0a5c6189664123753c298d90dec3a5a6b907f06f
# bad: [e2a43b09bfc24369719437b5f8f703c5d7003af2] Merge remote-tracking branch 'tip/auto-latest' git bisect bad e2a43b09bfc24369719437b5f8f703c5d7003af2
# good: [ba1ef96ac73d2a9851dc64d0a991ac8699aa6a22] Merge branch 'timers/core'
git bisect good ba1ef96ac73d2a9851dc64d0a991ac8699aa6a22
# good: [473a78a7bbeb7c77b81b596095f59ce4fbbd49dc] spi-atmel: support inter-word delay
git bisect good 473a78a7bbeb7c77b81b596095f59ce4fbbd49dc
# bad: [132dc31173c99bd252f6a54e3a9fba90f7417a43] Merge branch 'x86/cleanups'
git bisect bad 132dc31173c99bd252f6a54e3a9fba90f7417a43
# bad: [6eb09d138a331121740a4cf7458ab317979d9a17] Merge branch 'x86/boot'
git bisect bad 6eb09d138a331121740a4cf7458ab317979d9a17
# good: [e2c403557ebbcd1f0052e1ae51cfaff596cf259d] Merge branch 'x86/asm'
git bisect good e2c403557ebbcd1f0052e1ae51cfaff596cf259d
# good: [33f0df8d843deb9ec24116dcd79a40ca0ea8e8a9] x86/boot: Search for RSDP in the EFI tables git bisect good 33f0df8d843deb9ec24116dcd79a40ca0ea8e8a9
# bad: [3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce] x86/boot: Early parse RSDP and save it in boot_params git bisect bad 3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce
# good: [93a209aaaad495d7d0bc9b6186a4495934f70402] x86/boot: Search for RSDP in memory
git bisect good 93a209aaaad495d7d0bc9b6186a4495934f70402
# first bad commit: [3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce] x86/boot: Early parse RSDP and save it in boot_params
Borislav Petkov
maintainerRe: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 44 lines · Guenter Roeck
Hi,
Quoted 12 lines · tip-bot for Chao Fan
Commit-ID: 3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Gitweb: https://git.kernel.org/tip/3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:48 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:55 +0100x86/boot: Early parse RSDP and save it in
boot_paramsThe RSDP is needed by KASLR so parse it early and save it in boot_params.acpi_rsdp_addr, before KASLR setup runs.
Quoted 9 lines
RSDP is needed by other kernel facilities so have the parsing code built-in instead of a long "depends on" line in
Kconfig.[ bp:
- Trim commit message and comments
- AddCONFIG_ACPIdependency in theMakefile
- Move ->acpi_rsdp_addr assignment with the rest ofboot_paramsmassaging inextract_kernel().
]This patch causes
x86_64boots with efi32 bios to crash early and hard (before any console output is seen).Sample qemu command line:
qemu-system-x86_64 -kernel arch/x86/boot/bzImage -M q35 -cpu SandyBridge \ -no-reboot -snapshot -smp 4 \ -bios OVMF-pure-efi-32.fd \ -m 1G \ -usb -device usb-storage,drive=d0 \ -drive file=rootfs.squashfs,if=none,id=d0,format=raw \ --append 'earlycon=uart8250,io,0x3f8,9600n8 root=/dev/sda rootwait panic=-1 console=ttyS0' \ -nographicThis is with qemu 3.1, though details other than "-bios OVMF-pure-efi-32.fd" don't really matter.
Files used above are available from https://github.com/groeck/linux-build-test/.
Quoted 7 lines
The patch can not be reverted alone, but
Revert "x86/boot/KASLR: Limit KASLR to extract the kernel in immovable memory only" Revert "x86/boot: Parse SRAT table and count immovable memory regions" Revert "x86/boot: Early parse RSDP and save it in
boot_params"fixes the problem.
Does it per chance at least continue booting if you return 0 and early from efi_get_rsdp_addr() ?
Chao, pls take a look.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 47 lines · Borislav Petkov
Quoted 46 lines · Guenter Roeck
Hi,
Quoted 12 lines · tip-bot for Chao Fan
Commit-ID: 3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Gitweb: https://git.kernel.org/tip/3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:48 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:55 +0100x86/boot: Early parse RSDP and save it in
boot_paramsThe RSDP is needed by KASLR so parse it early and save it in boot_params.acpi_rsdp_addr, before KASLR setup runs.
Quoted 9 lines
RSDP is needed by other kernel facilities so have the parsing code built-in instead of a long "depends on" line in
Kconfig.[ bp:
- Trim commit message and comments
- AddCONFIG_ACPIdependency in theMakefile
- Move ->acpi_rsdp_addr assignment with the rest ofboot_paramsmassaging inextract_kernel().
]This patch causes
x86_64boots with efi32 bios to crash early and hard (before any console output is seen).Sample qemu command line:
qemu-system-x86_64 -kernel arch/x86/boot/bzImage -M q35 -cpu SandyBridge \ -no-reboot -snapshot -smp 4 \ -bios OVMF-pure-efi-32.fd \ -m 1G \ -usb -device usb-storage,drive=d0 \ -drive file=rootfs.squashfs,if=none,id=d0,format=raw \ --append 'earlycon=uart8250,io,0x3f8,9600n8 root=/dev/sda rootwait panic=-1 console=ttyS0' \ -nographicThis is with qemu 3.1, though details other than "-bios OVMF-pure-efi-32.fd" don't really matter.
Files used above are available from https://github.com/groeck/linux-build-test/.
The patch can not be reverted alone, but
Quoted 9 lines
Quoted 5 lines
Revert "x86/boot/KASLR: Limit KASLR to extract the kernel in immovable memory only" Revert "x86/boot: Parse SRAT table and count immovable memory regions" Revert "x86/boot: Early parse RSDP and save it in
boot_params"fixes the problem.
Does it per chance at least continue booting if you return 0 and early from
efi_get_rsdp_addr()?
Yes, the kernel boots if I comment out that function and have it return 0.
Guenter
Quoted 9 lines
Chao, pls take a look.
Thx.
Signature · 4 lines
Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
Borislav Petkov
maintainerRe: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 1 line · Guenter Roeck
Yes, the kernel boots if I comment out that function and have it return 0.
Thanks, this localizes the issue significantly.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Borislav Petkov
maintainerRe: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Guenter Roeck
Yes, the kernel boots if I comment out that function and have it return 0.
Thanks, this localizes the issue significantly.
Some observations:
} else {
efi_config_table_32_t *tmp_table;
tmp_table = config_tables;
guid = tmp_table->guid; <--- *
table = tmp_table->table;
}It blows up at that tmp_table->guid deref above. Singlestepping through it with gdb shows:
# arch/x86/boot/compressed/acpi.c:114: guid = tmp_table->guid; movq (%rdi), %rax # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid movq 8(%rdi), %rsi # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid # arch/x86/boot/compressed/acpi.c:115: table = tmp_table->table; movl 16(%rdi), %r10d # MEM[(struct efi_config_table_32_t *)config_tables_37].table, table jmp .L30 #
and %rdi has:
rdi 0x630646870
which is an address above 4G but we're using a 32-bit EFI BIOS.
Which begs the question whether EFI system tables can even be mapped at something above 4G with a 32-bit EFI and whether that could work ok. Hmm.
Lemme add Ard and mfleming for insight here.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 28 lines · Borislav Petkov
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Guenter Roeck
Yes, the kernel boots if I comment out that function and have it return 0.
Thanks, this localizes the issue significantly.
Some observations:
} else { efi_config_table_32_t *tmp_table; tmp_table = config_tables; guid = tmp_table->guid; <--- * table = tmp_table->table; }It blows up at that
tmp_table->guidderef above. Singlestepping through it with gdb shows:# arch/x86/boot/compressed/acpi.c:114: guid = tmp_table->guid; movq (%rdi), %rax # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid movq 8(%rdi), %rsi # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid # arch/x86/boot/compressed/acpi.c:115: table = tmp_table->table; movl 16(%rdi), %r10d # MEM[(struct efi_config_table_32_t *)config_tables_37].table, table jmp .L30 #and %rdi has:
Quoted 7 lines
rdi 0x630646870which is an address above 4G but we're using a 32-bit EFI BIOS.
Which begs the question whether EFI system tables can even be mapped at something above 4G with a 32-bit EFI and whether that could work ok. Hmm.
Thanks for your help. As you said, I am not wure whether 32-bit EFI can map table address above 4G.
If the map method has problem, I think adding a check here can work.
Thanks,
Chao Fan
Quoted 12 lines
Lemme add Ard and mfleming for insight here.
Thx.
Signature · 4 lines
Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
In reply to Borislav PetkovQuoted 31 lines · Borislav Petkov
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Guenter Roeck
Yes, the kernel boots if I comment out that function and have it return 0.
Thanks, this localizes the issue significantly.
Some observations:
} else { efi_config_table_32_t *tmp_table; tmp_table = config_tables; guid = tmp_table->guid; <--- * table = tmp_table->table; }It blows up at that
tmp_table->guidderef above. Singlestepping through it with gdb shows:# arch/x86/boot/compressed/acpi.c:114: guid = tmp_table->guid; movq (%rdi), %rax # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid movq 8(%rdi), %rsi # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid#
arch/x86/boot/compressed/acpi.c:115: table =tmp_table->table;
movl 16(%rdi), %r10d # MEM[(structefi_config_table_32_t*)config_tables_37].table, table
jmp .L30 #and %rdi has:
rdi 0x630646870
Quoted 8 lines
which is an address above 4G but we're using a 32-bit EFI BIOS.
Which begs the question whether EFI system tables can even be mapped at something above 4G with a 32-bit EFI and whether that could work ok. Hmm.
Lemme add Ard and mfleming for insight here.
-ENOCONTEXT, but let me try in any case:
linux/efi.h has
typedef struct {
efi_guid_t guid;
u32 table;
} efi_config_table_32_t;
so if we end up with more than 32 bits set in table, there is something seriously wrong.
The size of efi_config_table_32_t deviates from efi_config_table_64_t, so you will have to ensure that you are using the correct stride when iterating over config_tables.
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 50 lines · Ard Biesheuvel
Quoted 31 lines · Borislav Petkov
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Guenter Roeck
Yes, the kernel boots if I comment out that function and have it return 0.
Thanks, this localizes the issue significantly.
Some observations:
} else { efi_config_table_32_t *tmp_table; tmp_table = config_tables; guid = tmp_table->guid; <--- * table = tmp_table->table; }It blows up at that
tmp_table->guidderef above. Singlestepping through it with gdb shows:# arch/x86/boot/compressed/acpi.c:114: guid = tmp_table->guid; movq (%rdi), %rax # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid movq 8(%rdi), %rsi # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid#
arch/x86/boot/compressed/acpi.c:115: table =tmp_table->table;
movl 16(%rdi), %r10d # MEM[(structefi_config_table_32_t*)config_tables_37].table, table
jmp .L30 #and %rdi has:
rdi 0x630646870Quoted 8 lines
which is an address above 4G but we're using a 32-bit EFI BIOS.
Which begs the question whether EFI system tables can even be mapped at something above 4G with a 32-bit EFI and whether that could work ok. Hmm.
Lemme add Ard and mfleming for insight here.
-ENOCONTEXT, but let me try in any case:
linux/efi.hhastypedef struct {
efi_guid_tguid;
u32 table;
}efi_config_table_32_t;
Quoted 6 lines
so if we end up with more than 32 bits set in table, there is something seriously wrong.
The size of
efi_config_table_32_tdeviates fromefi_config_table_64_t, so you will have to ensure that you are using the correct stride when iterating overconfig_tables.
Here I use signature to judge it.
If the signature is EFI64_LOADER_SIGNATURE, I will use efi_config_table_64_t, if the signature is EFI32_LOADER_SIGNATURE, I will use efi_config_table_32_t. But the efi32 whose signature is EFI32_LOADER_SIGNATURE points to a address above 4G, I am not sure whether this is normal and works well.
Thanks,
Chao Fan
Quoted 2 lines
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 59 lines · Chao Fan
Quoted 50 lines · Ard Biesheuvel
Quoted 31 lines · Borislav Petkov
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Guenter Roeck
Yes, the kernel boots if I comment out that function and have it return 0.
Thanks, this localizes the issue significantly.
Some observations:
} else { efi_config_table_32_t *tmp_table; tmp_table = config_tables; guid = tmp_table->guid; <--- * table = tmp_table->table; }It blows up at that
tmp_table->guidderef above. Singlestepping through it with gdb shows:# arch/x86/boot/compressed/acpi.c:114: guid = tmp_table->guid; movq (%rdi), %rax # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid movq 8(%rdi), %rsi # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid#
arch/x86/boot/compressed/acpi.c:115: table =tmp_table->table;
movl 16(%rdi), %r10d # MEM[(structefi_config_table_32_t*)config_tables_37].table, table
jmp .L30 #and %rdi has:
rdi 0x630646870Quoted 8 lines
which is an address above 4G but we're using a 32-bit EFI BIOS.
Which begs the question whether EFI system tables can even be mapped at something above 4G with a 32-bit EFI and whether that could work ok. Hmm.
Lemme add Ard and mfleming for insight here.
-ENOCONTEXT, but let me try in any case:
linux/efi.hhastypedef struct {
efi_guid_tguid;
u32 table;
}efi_config_table_32_t;Quoted 6 lines
so if we end up with more than 32 bits set in table, there is something seriously wrong.
The size of
efi_config_table_32_tdeviates fromefi_config_table_64_t, so you will have to ensure that you are using the correct stride when iterating overconfig_tables.
Quoted 6 lines
Here I use signature to judge it.
If the signature isEFI64_LOADER_SIGNATURE, I will useefi_config_table_64_t, if the signature isEFI32_LOADER_SIGNATURE, I will useefi_config_table_32_t. But the efi32 whose signature isEFI32_LOADER_SIGNATUREpoints to a address above 4G, I am not sure whether this is normal and works well.
This is impossible. The 'table' member of efi_config_table_32_t is only 32 bits wide, so how can it contain an address over 4 GB ?
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 67 lines · Ard Biesheuvel
Quoted 59 lines · Chao Fan
Quoted 50 lines · Ard Biesheuvel
Quoted 31 lines · Borislav Petkov
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Guenter Roeck
Yes, the kernel boots if I comment out that function and have it return 0.
Thanks, this localizes the issue significantly.
Some observations:
} else { efi_config_table_32_t *tmp_table; tmp_table = config_tables; guid = tmp_table->guid; <--- * table = tmp_table->table; }It blows up at that
tmp_table->guidderef above. Singlestepping through it with gdb shows:# arch/x86/boot/compressed/acpi.c:114: guid = tmp_table->guid; movq (%rdi), %rax # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid movq 8(%rdi), %rsi # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid#
arch/x86/boot/compressed/acpi.c:115: table =tmp_table->table;
movl 16(%rdi), %r10d # MEM[(structefi_config_table_32_t*)config_tables_37].table, table
jmp .L30 #and %rdi has:
rdi 0x630646870Quoted 8 lines
which is an address above 4G but we're using a 32-bit EFI BIOS.
Which begs the question whether EFI system tables can even be mapped at something above 4G with a 32-bit EFI and whether that could work ok. Hmm.
Lemme add Ard and mfleming for insight here.
-ENOCONTEXT, but let me try in any case:
linux/efi.hhastypedef struct {
efi_guid_tguid;
u32 table;
}efi_config_table_32_t;Quoted 6 lines
so if we end up with more than 32 bits set in table, there is something seriously wrong.
The size of
efi_config_table_32_tdeviates fromefi_config_table_64_t, so you will have to ensure that you are using the correct stride when iterating overconfig_tables.Quoted 6 lines
Here I use signature to judge it.
If the signature isEFI64_LOADER_SIGNATURE, I will useefi_config_table_64_t, if the signature isEFI32_LOADER_SIGNATURE, I will useefi_config_table_32_t. But the efi32 whose signature isEFI32_LOADER_SIGNATUREpoints to a address above 4G, I am not sure whether this is normal and works well.
Quoted 2 lines
This is impossible. The 'table' member of
efi_config_table_32_tis only 32 bits wide, so how can it contain an address over 4 GB ?
Maybe I mislead you. In my code, I need to find the eficonfig_table_*. After that, I should type cast it to right efi_config_table_32_t or efi_config_table_64_t.
Then my judgment is to compare its efi_info->efi_loader_signature. If it's EFI64_LOADER_SIGNATURE, I will type cast it to efi_config_table_64_t. If it's EFI32_LOADER_SIGNATURE, I will type cast it to efi_config_table_32_t.
But here is a issue, its signature matches EFI32_LOADER_SIGNATURE, but it's table member is above 4G, but I use efi_config_table_32_t. That cause a problem.
Thanks,
Chao Fan
Quoted 2 lines
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 76 lines · Chao Fan
Quoted 67 lines · Ard Biesheuvel
Quoted 59 lines · Chao Fan
Quoted 50 lines · Ard Biesheuvel
Quoted 31 lines · Borislav Petkov
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Guenter Roeck
Yes, the kernel boots if I comment out that function and have it return 0.
Thanks, this localizes the issue significantly.
Some observations:
} else { efi_config_table_32_t *tmp_table; tmp_table = config_tables; guid = tmp_table->guid; <--- * table = tmp_table->table; }It blows up at that
tmp_table->guidderef above. Singlestepping through it with gdb shows:# arch/x86/boot/compressed/acpi.c:114: guid = tmp_table->guid; movq (%rdi), %rax # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid movq 8(%rdi), %rsi # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid#
arch/x86/boot/compressed/acpi.c:115: table =tmp_table->table;
movl 16(%rdi), %r10d # MEM[(structefi_config_table_32_t*)config_tables_37].table, table
jmp .L30 #and %rdi has:
rdi 0x630646870Quoted 8 lines
which is an address above 4G but we're using a 32-bit EFI BIOS.
Which begs the question whether EFI system tables can even be mapped at something above 4G with a 32-bit EFI and whether that could work ok. Hmm.
Lemme add Ard and mfleming for insight here.
-ENOCONTEXT, but let me try in any case:
linux/efi.hhastypedef struct {
efi_guid_tguid;
u32 table;
}efi_config_table_32_t;Quoted 6 lines
so if we end up with more than 32 bits set in table, there is something seriously wrong.
The size of
efi_config_table_32_tdeviates fromefi_config_table_64_t, so you will have to ensure that you are using the correct stride when iterating overconfig_tables.Quoted 6 lines
Here I use signature to judge it.
If the signature isEFI64_LOADER_SIGNATURE, I will useefi_config_table_64_t, if the signature isEFI32_LOADER_SIGNATURE, I will useefi_config_table_32_t. But the efi32 whose signature isEFI32_LOADER_SIGNATUREpoints to a address above 4G, I am not sure whether this is normal and works well.Quoted 2 lines
This is impossible. The 'table' member of
efi_config_table_32_tis only 32 bits wide, so how can it contain an address over 4 GB ?Maybe I mislead you. In my code, I need to find the eficonfig_table_*. After that, I should type cast it to right
efi_config_table_32_torefi_config_table_64_t.
Quoted 7 lines
Then my judgment is to compare its
efi_info->efi_loader_signature. If it'sEFI64_LOADER_SIGNATURE, I will type cast it toefi_config_table_64_t. If it'sEFI32_LOADER_SIGNATURE, I will type cast it toefi_config_table_32_t.But here is a issue, its signature matches
EFI32_LOADER_SIGNATURE, but it's table member is above 4G, but I useefi_config_table_32_t. That cause a problem.
That still does not explain how 'table' can assume a value > 4 GB after assigning the contents of a u32 to it.
Borislav Petkov
maintainerRe: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 2 lines · Ard Biesheuvel
That still does not explain how 'table' can assume a value > 4 GB after assigning the contents of a u32 to it.
See efi_get_rsdp_addr() in tip/master and especially that systable address computation:
#ifdef CONFIG_X86_64
systab = (efi_system_table_t *)(ei->efi_systab | ((__u64)ei->efi_systab_hi<<32));
#else
if (ei->efi_systab_hi || ei->efi_memmap_hi) {
debug_putstr("Error getting RSDP address: EFI system table located above 4GB.\n");
return 0;
}
systab = (efi_system_table_t *)ei->efi_systab;#endif
... config_tables = (void *)(systab->tables + size * i);
It is hard to debug that early but I managed to singlestep it last night to this deref:
} else {
efi_config_table_32_t *tmp_table;
tmp_table = config_tables;
guid = tmp_table->guid;
^^^^^^^^^^^^^^
table = tmp_table->table;which in asm is:
# arch/x86/boot/compressed/acpi.c:114: guid = tmp_table->guid;
#NO_APP
movq (%rdi), %rax # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid movq 8(%rdi), %rsi # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid # arch/x86/boot/compressed/acpi.c:115: table = tmp_table->table; movl 16(%rdi), %r10d # MEM[(struct efi_config_table_32_t *)config_tables_37].table, table
and %rdi has 0x630646870 so either we got the wrong address or qemu mapped it above 4G...
It is only an observation for now though...
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 43 lines · Borislav Petkov
Quoted 2 lines · Ard Biesheuvel
That still does not explain how 'table' can assume a value > 4 GB after assigning the contents of a u32 to it.
See
efi_get_rsdp_addr()in tip/master and especially that systable address computation:#ifdef CONFIG_X86_64 systab = (efi_system_table_t *)(ei->efi_systab | ((__u64)ei->efi_systab_hi<<32)); #else if (ei->efi_systab_hi || ei->efi_memmap_hi) { debug_putstr("Error getting RSDP address: EFI system table located above 4GB.\n"); return 0; } systab = (efi_system_table_t *)ei->efi_systab;#endif
... config_tables = (void *)(systab->tables + size * i);It is hard to debug that early but I managed to singlestep it last night to this deref:
} else { efi_config_table_32_t *tmp_table; tmp_table = config_tables; guid = tmp_table->guid; ^^^^^^^^^^^^^^ table = tmp_table->table;which in asm is:
#
arch/x86/boot/compressed/acpi.c:114: guid =tmp_table->guid;
#NO_APPmovq (%rdi), %rax # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid movq 8(%rdi), %rsi # MEM[(struct efi_config_table_32_t *)config_tables_37].guid, guid # arch/x86/boot/compressed/acpi.c:115: table = tmp_table->table; movl 16(%rdi), %r10d # MEM[(struct efi_config_table_32_t *)config_tables_37].table, table
Quoted 5 lines
and %rdi has
0x630646870so either we got the wrong address or qemu mapped it above 4G...It is only an observation for now though...
I think the problem is that efi_system_table_t is native, so if you want to access a 32-bit EFI system table from a 64-bit build, you will need to use efi_system_table_32_t explicitly.
Borislav Petkov
maintainerRe: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
In reply to Borislav PetkovQuoted 1 line · Borislav Petkov
It is only an observation for now though...
Ok, now I did this:
for (i = 0; i < systab->nr_tables; i++) {
acpi_physical_address table;
void *config_tables;
efi_guid_t guid;
if ((unsigned long)systab->tables >> 32) <---
asm volatile("1:\n jmp 1b");
config_tables = (void *)(systab->tables + size * i);It did stop at the endless loop there which would mean that systab->tables is simply pointing somewhere into the weeds.
Asm is:
---
# arch/x86/boot/compressed/acpi.c:96: asm volatile("1:\n jmp 1b");
#APP
# 96 "arch/x86/boot/compressed/acpi.c" 1
1: jmp 1b # 0 "" 2 #NO_APP movq 112(%rcx), %rdi # systab_147->tables, _31 .L29: # arch/x86/boot/compressed/acpi.c:98: config_tables = (void *)(systab->tables + size * i); addq %r9, %rdi # ivtmp.64, config_tables
# arch/x86/boot/compressed/acpi.c:104: guid = tmp_table->guid;
---
and RDI is
rdi 0x630646870
in gdb.
systab itself gets computed to something low: 0x3fedb010.
Hmmm. Could it be that that EDK BIOS image is buggy?
[ 0.000000] efi: EFI v2.70 by EDK II [ 0.000000] efi: SMBIOS=0x3feb9000 ACPI=0x3fee7000 ACPI 2.0=0x3fee7014 MEMATTR=0x3f524110
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 16 lines · Borislav Petkov
Quoted 1 line · Borislav Petkov
It is only an observation for now though...
Ok, now I did this:
for (i = 0; i < systab->nr_tables; i++) { acpi_physical_address table; void *config_tables; efi_guid_t guid; if ((unsigned long)systab->tables >> 32) <--- asm volatile("1:\n jmp 1b"); config_tables = (void *)(systab->tables + size * i);
Quoted 3 lines
It did stop at the endless loop there which would mean that
systab->tablesis simply pointing somewhere into the weeds.
As I pointed out in my previous reply, systab will be the wrong type on 32-bit firmware, it needs to be efi_system_table_32_t
Quoted 35 lines
Asm is:
---
#arch/x86/boot/compressed/acpi.c:96: asmvolatile("1:\n jmp 1b");
#APP
# 96 "arch/x86/boot/compressed/acpi.c" 11: jmp 1b # 0 "" 2 #NO_APP movq 112(%rcx), %rdi # systab_147->tables, _31.L29:
#arch/x86/boot/compressed/acpi.c:98:config_tables= (void *)(systab->tables + size * i);
addq %r9, %rdi # ivtmp.64,config_tables
#arch/x86/boot/compressed/acpi.c:104: guid =tmp_table->guid;
---and RDI is
rdi
0x630646870in gdb.
systab itself gets computed to something low:
0x3fedb010.Hmmm. Could it be that that EDK BIOS image is buggy?
[ 0.000000] efi: EFI v2.70 by EDK II [ 0.000000] efi: SMBIOS=0x3feb9000 ACPI=0x3fee7000 ACPI 2.0=0x3fee7014 MEMATTR=0x3f524110--
Regards/Gruss,
Boris.Good mailing practices for 400: avoid top-posting and trim the reply.
Borislav Petkov
maintainerRe: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 2 lines · Ard Biesheuvel
As I pointed out in my previous reply, systab will be the wrong type on 32-bit firmware, it needs to be
efi_system_table_32_t
Yeah, that seems to work. It boots now and it says:
[ 0.000000] efi: No EFI runtime due to 32/64-bit mismatch with kernel
Do you think we should mirror that behavoir early too, so that people don't get any ideas?
Or should we limit that only to the RDSP address computation and anything else where kernel and EFI bitness mismatch should be disabled?
Thanks Ard!
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 5 lines · Borislav Petkov
Quoted 2 lines · Ard Biesheuvel
As I pointed out in my previous reply, systab will be the wrong type on 32-bit firmware, it needs to be
efi_system_table_32_t
Quoted 7 lines
Yeah, that seems to work. It boots now and it says:
[ 0.000000] efi: No EFI runtime due to 32/64-bit mismatch with kernelDo you think we should mirror that behavoir early too, so that people don't get any ideas?
It you have CONFIG_EFI_MIXED enabled, you can in fact use 32-bit UEFI runtime services from 64-bit Linux, so just using the tables should be fine as well, and I don't think we should hide that behind a Kconfig option. (Note that ACPI defines its table layouts without regard for architecture bitness, so this is just about the minimal EFI table parsing that is required to get at the RDSP)
Quoted 10 lines
Or should we limit that only to the RDSP address computation and anything else where kernel and EFI bitness mismatch should be disabled?
Thanks Ard!
--
Regards/Gruss,
Boris.Good mailing practices for 400: avoid top-posting and trim the reply.
Borislav Petkov
maintainerRe: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 6 lines · Ard Biesheuvel
It you have
CONFIG_EFI_MIXEDenabled, you can in fact use 32-bit UEFI runtime services from 64-bit Linux, so just using the tables should be fine as well, and I don't think we should hide that behind aKconfigoption. (Note that ACPI defines its table layouts without regard for architecture bitness, so this is just about the minimal EFI table parsing that is required to get at the RDSP)
Ok, understood.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Borislav Petkov
maintainerRe: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
In reply to Ard BiesheuvelQuoted 2 lines · Ard Biesheuvel
As I pointed out in my previous reply, systab will be the wrong type on 32-bit firmware, it needs to be
efi_system_table_32_t
This seems to work. Chao, please run it too.
Thx.
---
arch/x86/boot/compressed/acpi.c +24 -10
@@ -50,7 +50,8 @@ static acpi_physical_address efi_get_rsdp_addr(void)acpi_physical_address rsdp_addr =0 ;#ifdef CONFIG_EFI -efi_system_table_t *systab; +unsigned long systab, systab_tables, config_tables; +unsigned int nr_tables;struct efi_info *ei;bool efi_64;int size, i;
@@ -70,13 +71,13 @@ static acpi_physical_address efi_get_rsdp_addr(void)/* Get systab from boot params. */ #ifdef CONFIG_X86_64 - systab = (efi_system_table_t *)(ei->efi_systab | ((__u64 )ei->efi_systab_hi<<32 )); + systab = ei->efi_systab | ((__u64 )ei->efi_systab_hi <<32 );#else if (ei->efi_systab_hi || ei->efi_memmap_hi) {debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" );return 0 ; } - systab = (efi_system_table_t *)ei->efi_systab; + systab = ei->efi_systab;#endif if (!systab)error ("EFI system table not found." );
@@ -87,16 +88,30 @@ static acpi_physical_address efi_get_rsdp_addr(void) size = efi_64 ?sizeof (efi_config_table_64_t) :sizeof (efi_config_table_32_t); -for (i =0 ; i < systab->nr_tables; i++) { +if (efi_64) { +efi_system_table_64_t *st_tbl = (efi_system_table_64_t *)systab; + + config_tables = st_tbl->tables; + nr_tables = st_tbl->nr_tables; + }else { +efi_system_table_32_t *st_tbl = (efi_system_table_32_t *)systab; + + config_tables = st_tbl->tables; + nr_tables = st_tbl->nr_tables; + } + +if (!config_tables) +error ("EFI config tables not found." ); + +for (i =0 ; i < nr_tables; i++) {acpi_physical_address table; -void *config_tables;efi_guid_t guid; - config_tables = (void *)(systab->tables + size * i); + config_tables += size * i; +if (efi_64) { -efi_config_table_64_t *tmp_table; +efi_config_table_64_t *tmp_table = (efi_config_table_64_t *)config_tables; - tmp_table = config_tables; guid = tmp_table->guid; table = tmp_table->table;
@@ -105,9 +120,8 @@ static acpi_physical_address efi_get_rsdp_addr(void)return 0 ; } }else { -efi_config_table_32_t *tmp_table; +efi_config_table_32_t *tmp_table = (efi_config_table_32_t *)config_tables; - tmp_table = config_tables; guid = tmp_table->guid; table = tmp_table->table; }
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 5 lines · Borislav Petkov
Quoted 2 lines · Ard Biesheuvel
As I pointed out in my previous reply, systab will be the wrong type on 32-bit firmware, it needs to be
efi_system_table_32_tThis seems to work. Chao, please run it too.
Ah, I will try and test more then reply to mail list.
Thanks,
Chao Fan
Quoted 89 lines
Thx.
---
arch/x86/boot/compressed/acpi.c +24
-10@@ -50,7 +50,8 @@ static acpi_physical_address efi_get_rsdp_addr(void)acpi_physical_address rsdp_addr =0 ;#ifdef CONFIG_EFI -efi_system_table_t *systab; +unsigned long systab, systab_tables, config_tables; +unsigned int nr_tables;struct efi_info *ei;bool efi_64;int size, i;@@ -70,13 +71,13 @@ static acpi_physical_address efi_get_rsdp_addr(void)/* Get systab from boot params. */ #ifdef CONFIG_X86_64 - systab = (efi_system_table_t *)(ei->efi_systab | ((__u64 )ei->efi_systab_hi<<32 )); + systab = ei->efi_systab | ((__u64 )ei->efi_systab_hi <<32 );#else if (ei->efi_systab_hi || ei->efi_memmap_hi) {debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" );return 0 ; } - systab = (efi_system_table_t *)ei->efi_systab; + systab = ei->efi_systab;#endif if (!systab)error ("EFI system table not found." );@@ -87,16 +88,30 @@ static acpi_physical_address efi_get_rsdp_addr(void) size = efi_64 ?sizeof (efi_config_table_64_t) :sizeof (efi_config_table_32_t); -for (i =0 ; i < systab->nr_tables; i++) { +if (efi_64) { +efi_system_table_64_t *st_tbl = (efi_system_table_64_t *)systab; + + config_tables = st_tbl->tables; + nr_tables = st_tbl->nr_tables; + }else { +efi_system_table_32_t *st_tbl = (efi_system_table_32_t *)systab; + + config_tables = st_tbl->tables; + nr_tables = st_tbl->nr_tables; + } + +if (!config_tables) +error ("EFI config tables not found." ); + +for (i =0 ; i < nr_tables; i++) {acpi_physical_address table; -void *config_tables;efi_guid_t guid; - config_tables = (void *)(systab->tables + size * i); + config_tables += size * i; +if (efi_64) { -efi_config_table_64_t *tmp_table; +efi_config_table_64_t *tmp_table = (efi_config_table_64_t *)config_tables; - tmp_table = config_tables; guid = tmp_table->guid; table = tmp_table->table;@@ -105,9 +120,8 @@ static acpi_physical_address efi_get_rsdp_addr(void)return 0 ; } }else { -efi_config_table_32_t *tmp_table; +efi_config_table_32_t *tmp_table = (efi_config_table_32_t *)config_tables; - tmp_table = config_tables; guid = tmp_table->guid; table = tmp_table->table; }Signature · 5 lines
-- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
In reply to Borislav PetkovQuoted 5 lines · Borislav Petkov
Quoted 2 lines · Ard Biesheuvel
As I pointed out in my previous reply, systab will be the wrong type on 32-bit firmware, it needs to be
efi_system_table_32_tThis seems to work. Chao, please run it too.
Yes, your PATCH really works well. I tried both efi32 OVMF and efi64 OVMF, all boot.
Thanks,
Chao Fan
Quoted 89 lines
Thx.
---
arch/x86/boot/compressed/acpi.c +24
-10@@ -50,7 +50,8 @@ static acpi_physical_address efi_get_rsdp_addr(void)acpi_physical_address rsdp_addr =0 ;#ifdef CONFIG_EFI -efi_system_table_t *systab; +unsigned long systab, systab_tables, config_tables; +unsigned int nr_tables;struct efi_info *ei;bool efi_64;int size, i;@@ -70,13 +71,13 @@ static acpi_physical_address efi_get_rsdp_addr(void)/* Get systab from boot params. */ #ifdef CONFIG_X86_64 - systab = (efi_system_table_t *)(ei->efi_systab | ((__u64 )ei->efi_systab_hi<<32 )); + systab = ei->efi_systab | ((__u64 )ei->efi_systab_hi <<32 );#else if (ei->efi_systab_hi || ei->efi_memmap_hi) {debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" );return 0 ; } - systab = (efi_system_table_t *)ei->efi_systab; + systab = ei->efi_systab;#endif if (!systab)error ("EFI system table not found." );@@ -87,16 +88,30 @@ static acpi_physical_address efi_get_rsdp_addr(void) size = efi_64 ?sizeof (efi_config_table_64_t) :sizeof (efi_config_table_32_t); -for (i =0 ; i < systab->nr_tables; i++) { +if (efi_64) { +efi_system_table_64_t *st_tbl = (efi_system_table_64_t *)systab; + + config_tables = st_tbl->tables; + nr_tables = st_tbl->nr_tables; + }else { +efi_system_table_32_t *st_tbl = (efi_system_table_32_t *)systab; + + config_tables = st_tbl->tables; + nr_tables = st_tbl->nr_tables; + } + +if (!config_tables) +error ("EFI config tables not found." ); + +for (i =0 ; i < nr_tables; i++) {acpi_physical_address table; -void *config_tables;efi_guid_t guid; - config_tables = (void *)(systab->tables + size * i); + config_tables += size * i; +if (efi_64) { -efi_config_table_64_t *tmp_table; +efi_config_table_64_t *tmp_table = (efi_config_table_64_t *)config_tables; - tmp_table = config_tables; guid = tmp_table->guid; table = tmp_table->table;@@ -105,9 +120,8 @@ static acpi_physical_address efi_get_rsdp_addr(void)return 0 ; } }else { -efi_config_table_32_t *tmp_table; +efi_config_table_32_t *tmp_table = (efi_config_table_32_t *)config_tables; - tmp_table = config_tables; guid = tmp_table->guid; table = tmp_table->table; }Signature · 5 lines
-- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
Boris Petkov
maintainerRe: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 2 lines · Chao Fan
Yes, your PATCH really works well. I tried both efi32 OVMF and efi64 OVMF, all boot.
What about the real hardware you are normally testing on? Boots there too?
Thx.
Signature · 1 line
Sent from a small device: formatting sux and brevity is inevitable.
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 5 lines · Boris Petkov
Quoted 2 lines · Chao Fan
Yes, your PATCH really works well. I tried both efi32 OVMF and efi64 OVMF, all boot.
What about the real hardware you are normally testing on? Boots there too?
I testet it in a real EFI machine, it booted, but from command:
Quoted 1 line
cat
/sys/firmware/efi/fw_platform_size
64
We can see it's a 64-bit EFI firmware, and there is no 32-bit EFI firmware available.
Thanks,
Chao Fan
Quoted 7 lines
Thx.
Signature · 1 line
Sent from a small device: formatting sux and brevity is inevitable.
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 6 lines · Chao Fan
Quoted 4 lines · Boris Petkov
Quoted 2 lines · Chao Fan
Yes, your PATCH really works well. I tried both efi32 OVMF and efi64 OVMF, all boot.
Quoted 8 lines
Quoted 1 line
What about the real hardware you are normally testing on? Boots there too?
I testet it in a real EFI machine, it booted, but from command:
Quoted 1 line
cat
/sys/firmware/efi/fw_platform_size64
We can see it's a 64-bit EFI firmware, and there is no 32-bit EFI firmware available.
Did you try booting 32-bit OVMF from 64-bit QEMU?
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 9 lines · Ard Biesheuvel
Quoted 8 lines · Chao Fan
Quoted 5 lines · Boris Petkov
Quoted 2 lines · Chao Fan
Yes, your PATCH really works well. I tried both efi32 OVMF and efi64 OVMF, all boot.
What about the real hardware you are normally testing on? Boots there too?
Quoted 8 lines
Quoted 6 lines
I testet it in a real EFI machine, it booted, but from command:
Quoted 1 line
cat
/sys/firmware/efi/fw_platform_size64
We can see it's a 64-bit EFI firmware, and there is no 32-bit EFI firmware available.
Did you try booting 32-bit OVMF from 64-bit QEMU?
Yes, I tried the 32-bit OVMF and rootfs from Guenter. Used as suggested by Boris:
qemu-system-x86_64 -kernel /home/cfan/code/tip/arch/x86/boot/bzImage -M q35 -cpu SandyBridge \
-no-reboot -snapshot -smp 1 \
-bios OVMF-pure-efi-32.fd \
-net nic,model=rtl8139,macaddr=12:34:56:78:12:34 \
-usb -device usb-storage,drive=d0 \
-m 1G \
-drive file=rootfs.squashfs,if=none,id=d0,format=raw \
--append 'earlycon=uart8250,io,0x3f8,9600n8 root=/dev/sda rootwait panic=-1 earlyprintk=ttyS0,115200,keep console=ttyS0,115200' \
-monitor pty \
-serial stdio
Since I used to watch the log, so I just change the "serial file:log" to "serial stdio".
Thanks,
Chao Fan
Quoted 2 lines
Borislav Petkov
maintainerRe: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 1 line · Chao Fan
Yes, I tried the 32-bit OVMF and rootfs from Guenter.
Ok, thanks.
Final version of the fix coming up...
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 6 lines · Borislav Petkov
Quoted 1 line · Chao Fan
Yes, I tried the 32-bit OVMF and rootfs from Guenter.
Ok, thanks.
Final version of the fix coming up...
Thanks so much for your help.
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: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
In reply to Borislav PetkovQuoted 51 lines · Borislav Petkov
Quoted 50 lines · Guenter Roeck
Hi,
Quoted 12 lines · tip-bot for Chao Fan
Commit-ID: 3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Gitweb: https://git.kernel.org/tip/3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:48 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:55 +0100x86/boot: Early parse RSDP and save it in
boot_paramsThe RSDP is needed by KASLR so parse it early and save it in boot_params.acpi_rsdp_addr, before KASLR setup runs.
Quoted 9 lines
RSDP is needed by other kernel facilities so have the parsing code built-in instead of a long "depends on" line in
Kconfig.[ bp:
- Trim commit message and comments
- AddCONFIG_ACPIdependency in theMakefile
- Move ->acpi_rsdp_addr assignment with the rest ofboot_paramsmassaging inextract_kernel().
]This patch causes
x86_64boots with efi32 bios to crash early and hard (before any console output is seen).Sample qemu command line:
qemu-system-x86_64 -kernel arch/x86/boot/bzImage -M q35 -cpu SandyBridge \ -no-reboot -snapshot -smp 4 \ -bios OVMF-pure-efi-32.fd \ -m 1G \ -usb -device usb-storage,drive=d0 \ -drive file=rootfs.squashfs,if=none,id=d0,format=raw \ --append 'earlycon=uart8250,io,0x3f8,9600n8 root=/dev/sda rootwait panic=-1 console=ttyS0' \ -nographicThis is with qemu 3.1, though details other than "-bios OVMF-pure-efi-32.fd" don't really matter.
Files used above are available from https://github.com/groeck/linux-build-test/.
The patch can not be reverted alone, but
Revert "x86/boot/KASLR: Limit KASLR to extract the kernel in immovable memory only" Revert "x86/boot: Parse SRAT table and count immovable memory regions" Revert "x86/boot: Early parse RSDP and save it in
boot_params"
Quoted 6 lines
Quoted 1 line
fixes the problem.
Does it per chance at least continue booting if you return 0 and early from
efi_get_rsdp_addr()?Chao, pls take a look.
Sorry for delay, last several days were holiday, so I was away from keyboard. I will take a look.
Thanks,
Chao Fan
Quoted 10 lines
Thx.
Signature · 4 lines
Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
In reply to Guenter RoeckQuoted 39 lines · Guenter Roeck
Hi,
Quoted 12 lines · tip-bot for Chao Fan
Commit-ID: 3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Gitweb: https://git.kernel.org/tip/3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:48 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:55 +0100x86/boot: Early parse RSDP and save it in
boot_paramsThe RSDP is needed by KASLR so parse it early and save it in boot_params.acpi_rsdp_addr, before KASLR setup runs.
Quoted 9 lines
RSDP is needed by other kernel facilities so have the parsing code built-in instead of a long "depends on" line in
Kconfig.[ bp:
- Trim commit message and comments
- AddCONFIG_ACPIdependency in theMakefile
- Move ->acpi_rsdp_addr assignment with the rest ofboot_paramsmassaging inextract_kernel().
]This patch causes
x86_64boots with efi32 bios to crash early and hard (before any console output is seen).Sample qemu command line:
qemu-system-x86_64 -kernel arch/x86/boot/bzImage -M q35 -cpu SandyBridge \ -no-reboot -snapshot -smp 4 \ -bios OVMF-pure-efi-32.fd \ -m 1G \ -usb -device usb-storage,drive=d0 \ -drive file=rootfs.squashfs,if=none,id=d0,format=raw \ --append 'earlycon=uart8250,io,0x3f8,9600n8 root=/dev/sda rootwait panic=-1 console=ttyS0' \ -nographic
Quoted 2 lines
This is with qemu 3.1, though details other than "-bios OVMF-pure-efi-32.fd" don't really matter.
Yes, I download your ovmf file in linux-build-test/rootfs/firmware/ from your link. There are two files, efi32 and efi64. Then I try to run:
BIOS=/usr/share/edk2/ovmf/OVMF-pure-efi-32.fd
qemu-system-x86_64 \
-bios $BIOS \
-cdrom /var/lib/libvirt/images/iso/Fedora-Workstation-Live-x86_64-29-1.2.iso \
-hda /var/lib/libvirt/images/fedora-efi32.qcow2 \
-enable-kvm \
-m 5G \
-serial stdio
It can't boot, but if I changed as:
BIOS=/usr/share/edk2/ovmf/OVMF-pure-efi-64.fd
Then the machine booted. So I am not sure how to use this OVMF-pure-efi-32.fd. If there is somethins wrong in my understand, please let me know.
Thanks,
Chao Fan
Quoted 48 lines
Files used above are available from https://github.com/groeck/linux-build-test/.
The patch can not be reverted alone, but
Revert "x86/boot/KASLR: Limit KASLR to extract the kernel in immovable memory only" Revert "x86/boot: Parse SRAT table and count immovable memory regions" Revert "x86/boot: Early parse RSDP and save it in
boot_params"fixes the problem.
Bisect log attached below.
Guenter
---
# bad: [a46228f6598a191e25ca160870f20c3cd1743d13] Add linux-next specific files for 20190208 # good: [8834f5600cf3c8db365e18a3d5cac2c2780c81e5] Linux 5.0-rc5
git bisect start 'HEAD' 'v5.0-rc5'
# good: [d5ee02f33ec5ade9c9afb9d45bfc4f5cb1375e58] Merge remote-tracking branch 'spi-nor/spi-nor/next' git bisect goodd5ee02f33ec5ade9c9afb9d45bfc4f5cb1375e58
# good: [f34bff29873986d260d7a491fb6f543d12fbe7cd] Merge remote-tracking branch 'devicetree/for-next' git bisect goodf34bff29873986d260d7a491fb6f543d12fbe7cd
# bad: [68182cb929ec6a676655887cbfb12b09662c1e5b] Merge remote-tracking branch 'icc/icc-next' git bisect bad68182cb929ec6a676655887cbfb12b09662c1e5b
# bad: [0a5c6189664123753c298d90dec3a5a6b907f06f] Merge remote-tracking branch 'driver-core/driver-core-next' git bisect bad0a5c6189664123753c298d90dec3a5a6b907f06f
# bad: [e2a43b09bfc24369719437b5f8f703c5d7003af2] Merge remote-tracking branch 'tip/auto-latest' git bisect bade2a43b09bfc24369719437b5f8f703c5d7003af2
# good: [ba1ef96ac73d2a9851dc64d0a991ac8699aa6a22] Merge branch 'timers/core'
git bisect goodba1ef96ac73d2a9851dc64d0a991ac8699aa6a22
# good: [473a78a7bbeb7c77b81b596095f59ce4fbbd49dc] spi-atmel: support inter-word delay
git bisect good473a78a7bbeb7c77b81b596095f59ce4fbbd49dc
# bad: [132dc31173c99bd252f6a54e3a9fba90f7417a43] Merge branch 'x86/cleanups'
git bisect bad132dc31173c99bd252f6a54e3a9fba90f7417a43
# bad: [6eb09d138a331121740a4cf7458ab317979d9a17] Merge branch 'x86/boot'
git bisect bad6eb09d138a331121740a4cf7458ab317979d9a17
# good: [e2c403557ebbcd1f0052e1ae51cfaff596cf259d] Merge branch 'x86/asm'
git bisect goode2c403557ebbcd1f0052e1ae51cfaff596cf259d
# good: [33f0df8d843deb9ec24116dcd79a40ca0ea8e8a9] x86/boot: Search for RSDP in the EFI tables git bisect good33f0df8d843deb9ec24116dcd79a40ca0ea8e8a9
# bad: [3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce] x86/boot: Early parse RSDP and save it inboot_paramsgit bisect bad3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce
# good: [93a209aaaad495d7d0bc9b6186a4495934f70402] x86/boot: Search for RSDP in memory
git bisect good93a209aaaad495d7d0bc9b6186a4495934f70402# first bad commit: [3a63f70bf4c3a17f5d9c9bf3bc3288a23bdfefce] x86/boot: Early parse RSDP and save it in boot_params
Borislav Petkov
maintainerRe: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 2 lines · Chao Fan
Yes, I download your ovmf file in linux-build-test/rootfs/firmware/ from your link. There are two files, efi32 and efi64. Then I try to run:
You need both OVMF-pure-efi-32.fd and rootfs.squashfs from the repo. And then this works here. Just put the script below and the two files in a directory and point to the kernel where you're building with -kernel.
---
#!/bin/bash
qemu-system-x86_64 -kernel ~/kernel/linux/arch/x86/boot/bzImage -M q35 -cpu SandyBridge \
-no-reboot -snapshot -smp 1 \
-gdb tcp::1235 \
-bios OVMF-pure-efi-32.fd \
-m 1G \
-net nic,model=rtl8139,macaddr=12:34:56:78:12:34 \
-usb -device usb-storage,drive=d0 \
-drive file=rootfs.squashfs,if=none,id=d0,format=raw \
--append 'earlycon=uart8250,io,0x3f8,9600n8 root=/dev/sda rootwait panic=-1 earlyprintk=ttyS0,115200,keep console=ttyS0,115200' \
-monitor pty \
-serial file:logI've added some options for debugging.
Signature · 4 lines
Regards/Gruss,
Boris.
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
Quoted 23 lines · Borislav Petkov
Quoted 2 lines · Chao Fan
Yes, I download your ovmf file in linux-build-test/rootfs/firmware/ from your link. There are two files, efi32 and efi64. Then I try to run:
You need both OVMF-pure-efi-32.fd and rootfs.squashfs from the repo. And then this works here. Just put the script below and the two files in a directory and point to the kernel where you're building with -kernel.
---
#!/bin/bashqemu-system-x86_64 -kernel ~/kernel/linux/arch/x86/boot/bzImage -M q35 -cpu SandyBridge \ -no-reboot -snapshot -smp 1 \ -gdb tcp::1235 \ -bios OVMF-pure-efi-32.fd \ -m 1G \ -net nic,model=rtl8139,macaddr=12:34:56:78:12:34 \ -usb -device usb-storage,drive=d0 \ -drive file=rootfs.squashfs,if=none,id=d0,format=raw \ --append 'earlycon=uart8250,io,0x3f8,9600n8 root=/dev/sda rootwait panic=-1 earlyprintk=ttyS0,115200,keep console=ttyS0,115200' \ -monitor pty \ -serial file:log
Quoted 1 line
I've added some options for debugging.
So many thanks!
Since I learn much about how to debug kernel code before start_kernel(). I only tried to debug code after start_kernel(). Seems you can debug the code in compressed code.
Thanks,
Chao Fan
Quoted 8 lines
Signature · 5 lines
-- Regards/Gruss, Boris. SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
Re: [tip:x86/boot] x86/boot: Early parse RSDP and save it in boot_params
In reply to Borislav PetkovQuoted 23 lines · Borislav Petkov
Quoted 2 lines · Chao Fan
Yes, I download your ovmf file in linux-build-test/rootfs/firmware/ from your link. There are two files, efi32 and efi64. Then I try to run:
You need both OVMF-pure-efi-32.fd and rootfs.squashfs from the repo. And then this works here. Just put the script below and the two files in a directory and point to the kernel where you're building with -kernel.
---
#!/bin/bashqemu-system-x86_64 -kernel ~/kernel/linux/arch/x86/boot/bzImage -M q35 -cpu SandyBridge \ -no-reboot -snapshot -smp 1 \ -gdb tcp::1235 \ -bios OVMF-pure-efi-32.fd \ -m 1G \ -net nic,model=rtl8139,macaddr=12:34:56:78:12:34 \ -usb -device usb-storage,drive=d0 \ -drive file=rootfs.squashfs,if=none,id=d0,format=raw \ --append 'earlycon=uart8250,io,0x3f8,9600n8 root=/dev/sda rootwait panic=-1 earlyprintk=ttyS0,115200,keep console=ttyS0,115200' \ -monitor pty \ -serial file:log
Quoted 2 lines
I've added some options for debugging.
If you use my rootfs, you can also add "noreboot" as boot parameter and it won't reboot but stay in a shell. You can also use any of the other root file systems in the repository - it doesn't have to be squashfs.
The whole point of booting with OVMF-pure-efi-32.fd is just that. I have other test runs which boot with the 64 bit efi version. The idea is to test as many combinations as possible. This is also why this specific boot is from USB drive and with squashfs/q35/SandyBridge.
Thanks,
Guenter
tip-bot for Borislav Petkov
merge bot[tip:x86/boot] x86/boot: Correct RSDP parsing with 32-bit EFI
In reply to Guenter Roeckx86/boot: Correct RSDP parsing with 32-bit EFI
Commit-ID: 022442cbec93433ac260a417ab89a960ae6058f8 Gitweb: https://git.kernel.org/tip/022442cbec93433ac260a417ab89a960ae6058f8 Author: Borislav Petkov <[email protected]> AuthorDate: Mon, 11 Feb 2019 12:19:45 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Wed, 13 Feb 2019 09:59:35 +0100
x86/boot: Correct RSDP parsing with 32-bit EFI
Guenter Roeck reported triple faults of a 64-bit VM using a 32-bit OVMF EFI image. After some singlestepping of the image in gdb, it turned out that some of the EFI config tables were at bogus addresses. Which, as Ard pointed out, results from using the wrong efi_config_table typedef.
So switch all EFI table pointers to unsigned longs and convert them to the proper typedef only when accessing them. This way, the proper table type is being used.
Shorten variable names, while at it.
--- arch/x86/boot/compressed/acpi.c | 50 +++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 19 deletions(-)
arch/x86/boot/compressed/acpi.c +31 -19
@@ -50,7 +50,8 @@ static acpi_physical_address efi_get_rsdp_addr(void)acpi_physical_address rsdp_addr =0 ;#ifdef CONFIG_EFI -efi_system_table_t *systab; +unsigned long systab, systab_tables, config_tables; +unsigned int nr_tables;struct efi_info *ei;bool efi_64;int size, i;
@@ -70,46 +71,57 @@ static acpi_physical_address efi_get_rsdp_addr(void)/* Get systab from boot params. */ #ifdef CONFIG_X86_64 - systab = (efi_system_table_t *)(ei->efi_systab | ((__u64 )ei->efi_systab_hi<<32 )); + systab = ei->efi_systab | ((__u64 )ei->efi_systab_hi <<32 );#else if (ei->efi_systab_hi || ei->efi_memmap_hi) {debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" );return 0 ; } - systab = (efi_system_table_t *)ei->efi_systab; + systab = ei->efi_systab;#endif if (!systab)error ("EFI system table not found." ); -/* -* Get EFI tables from systab. -*/ - size = efi_64 ?sizeof (efi_config_table_64_t) : -sizeof (efi_config_table_32_t); +/* Handle EFI bitness properly */ +if (efi_64) { +efi_system_table_64_t *stbl = (efi_system_table_64_t *)systab; + + config_tables = stbl->tables; + nr_tables = stbl->nr_tables; + size =sizeof (efi_config_table_64_t); + }else { +efi_system_table_32_t *stbl = (efi_system_table_32_t *)systab; -for (i =0 ; i < systab->nr_tables; i++) { + config_tables = stbl->tables; + nr_tables = stbl->nr_tables; + size =sizeof (efi_config_table_32_t); + } + +if (!config_tables) +error ("EFI config tables not found." ); + +/* Get EFI tables from systab. */ +for (i =0 ; i < nr_tables; i++) {acpi_physical_address table; -void *config_tables;efi_guid_t guid; - config_tables = (void *)(systab->tables + size * i); + config_tables += size * i; +if (efi_64) { -efi_config_table_64_t *tmp_table; +efi_config_table_64_t *tbl = (efi_config_table_64_t *)config_tables; - tmp_table = config_tables; - guid = tmp_table->guid; - table = tmp_table->table; + guid = tbl->guid; + table = tbl->table;if (!IS_ENABLED (CONFIG_X86_64 ) && table >>32 ) {debug_putstr ("Error getting RSDP address: EFI config table located above 4GB.\n" );return 0 ; } }else { -efi_config_table_32_t *tmp_table; +efi_config_table_32_t *tbl = (efi_config_table_32_t *)config_tables; - tmp_table = config_tables; - guid = tmp_table->guid; - table = tmp_table->table; + guid = tbl->guid; + table = tbl->table; }if (!(efi_guidcmp (guid,ACPI_TABLE_GUID )))
tip-bot for Borislav Petkov
merge bot[tip:x86/boot] x86/boot: Correct RSDP parsing with 32-bit EFI
In reply to Guenter Roeckx86/boot: Correct RSDP parsing with 32-bit EFI
Commit-ID: f9d230e893e864f13ce5ded9a49990fd024bfed5 Gitweb: https://git.kernel.org/tip/f9d230e893e864f13ce5ded9a49990fd024bfed5 Author: Borislav Petkov <[email protected]> AuthorDate: Mon, 11 Feb 2019 12:19:45 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Wed, 13 Feb 2019 12:19:05 +0100
x86/boot: Correct RSDP parsing with 32-bit EFI
Guenter Roeck reported triple faults of a 64-bit VM using a 32-bit OVMF EFI image. After some singlestepping of the image in gdb, it turned out that some of the EFI config tables were at bogus addresses. Which, as Ard pointed out, results from using the wrong efi_config_table typedef.
So switch all EFI table pointers to unsigned longs and convert them to the proper typedef only when accessing them. This way, the proper table type is being used.
Shorten variable names, while at it.
--- arch/x86/boot/compressed/acpi.c | 50 +++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 19 deletions(-)
arch/x86/boot/compressed/acpi.c +31 -19
@@ -50,7 +50,8 @@ static acpi_physical_address efi_get_rsdp_addr(void)acpi_physical_address rsdp_addr =0 ;#ifdef CONFIG_EFI -efi_system_table_t *systab; +unsigned long systab, systab_tables, config_tables; +unsigned int nr_tables;struct efi_info *ei;bool efi_64;int size, i;
@@ -70,46 +71,57 @@ static acpi_physical_address efi_get_rsdp_addr(void)/* Get systab from boot params. */ #ifdef CONFIG_X86_64 - systab = (efi_system_table_t *)(ei->efi_systab | ((__u64 )ei->efi_systab_hi<<32 )); + systab = ei->efi_systab | ((__u64 )ei->efi_systab_hi <<32 );#else if (ei->efi_systab_hi || ei->efi_memmap_hi) {debug_putstr ("Error getting RSDP address: EFI system table located above 4GB.\n" );return 0 ; } - systab = (efi_system_table_t *)ei->efi_systab; + systab = ei->efi_systab;#endif if (!systab)error ("EFI system table not found." ); -/* -* Get EFI tables from systab. -*/ - size = efi_64 ?sizeof (efi_config_table_64_t) : -sizeof (efi_config_table_32_t); +/* Handle EFI bitness properly */ +if (efi_64) { +efi_system_table_64_t *stbl = (efi_system_table_64_t *)systab; + + config_tables = stbl->tables; + nr_tables = stbl->nr_tables; + size =sizeof (efi_config_table_64_t); + }else { +efi_system_table_32_t *stbl = (efi_system_table_32_t *)systab; -for (i =0 ; i < systab->nr_tables; i++) { + config_tables = stbl->tables; + nr_tables = stbl->nr_tables; + size =sizeof (efi_config_table_32_t); + } + +if (!config_tables) +error ("EFI config tables not found." ); + +/* Get EFI tables from systab. */ +for (i =0 ; i < nr_tables; i++) {acpi_physical_address table; -void *config_tables;efi_guid_t guid; - config_tables = (void *)(systab->tables + size * i); + config_tables += size; +if (efi_64) { -efi_config_table_64_t *tmp_table; +efi_config_table_64_t *tbl = (efi_config_table_64_t *)config_tables; - tmp_table = config_tables; - guid = tmp_table->guid; - table = tmp_table->table; + guid = tbl->guid; + table = tbl->table;if (!IS_ENABLED (CONFIG_X86_64 ) && table >>32 ) {debug_putstr ("Error getting RSDP address: EFI config table located above 4GB.\n" );return 0 ; } }else { -efi_config_table_32_t *tmp_table; +efi_config_table_32_t *tbl = (efi_config_table_32_t *)config_tables; - tmp_table = config_tables; - guid = tmp_table->guid; - table = tmp_table->table; + guid = tbl->guid; + table = tbl->table; }if (!(efi_guidcmp (guid,ACPI_TABLE_GUID )))
PATCH v16 6/7 x86/boot: Parse SRAT address 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 filter the immovable memory regions and accoring to the memory information, KASLR can avoid to extract kernel to movable memory regions.
--- arch/x86/boot/compressed/acpi.c | 122 +++++++++++++++++++++++++++++++ arch/x86/boot/compressed/kaslr.c | 4 - arch/x86/boot/compressed/misc.h | 11 +++ 3 files changed, 133 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/acpi.c +122 -0
@@ -4,9 +4,23 @@#include "error.h" #include "../string.h" +#include <linux/numa.h> #include <linux/efi.h> #include <asm/efi.h> +/* +* Longest parameter of 'acpi=' is 'copy_dsdt', plus an extra '\0' +* for termination. +*/ +#define MAX_ACPI_ARG_LENGTH 10 + +/* +* Information of immovable memory regions. +* Max amount of memory regions is MAX_NUMNODES*2, so need such array +* to place immovable memory regions if all of the memory is immovable. +*/ +struct mem_vector immovable_mem[MAX_NUMNODES *2 ]; +#ifdef CONFIG_ACPI /*
@@ -217,3 +231,111 @@ acpi_physical_address get_rsdp_addr(void)return pa; }#endif /* CONFIG_ACPI */ + +#if defined(CONFIG_RANDOMIZE_BASE ) && defined(CONFIG_MEMORY_HOTREMOVE ) +/* Compute SRAT address from RSDP. */ +static unsigned long get_acpi_srat_table (void ) +{ +unsigned long root_table, acpi_table; +struct acpi_table_header *header; +struct acpi_table_rsdp *rsdp; +u32 num_entries, size, len; +char arg[10 ]; +u8 *entry; + + rsdp = (struct acpi_table_rsdp *)(long )boot_params->acpi_rsdp_addr; +if (!rsdp) +return 0 ; + +/* Get ACPI root table from RSDP.*/ +if (!(cmdline_find_option ("acpi" , arg,sizeof (arg)) ==4 && + !strncmp (arg,"rsdt" ,4 )) && + rsdp->xsdt_physical_address && + rsdp->revision >1 ) { + root_table = rsdp->xsdt_physical_address; + size =ACPI_XSDT_ENTRY_SIZE ; + }else { + root_table = rsdp->rsdt_physical_address; + size =ACPI_RSDT_ENTRY_SIZE ; + } + +if (!root_table) +return 0 ; + + header = (struct acpi_table_header *)root_table; + len = header->length; +if (len <sizeof (struct acpi_table_header ) + size) +return 0 ; + + num_entries = (len -sizeof (struct acpi_table_header )) / size; + entry = (u8 *)(root_table +sizeof (struct acpi_table_header )); + +while (num_entries--) { +if (size ==ACPI_RSDT_ENTRY_SIZE ) + acpi_table = *(u32 *)entry; +else + acpi_table = *(u64 *)entry; + +if (acpi_table) { + header = (struct acpi_table_header *)acpi_table; + +if (ACPI_COMPARE_NAME (header->signature,ACPI_SIG_SRAT )) +return acpi_table; + } + entry += size; + } +return 0 ; +} + +/** +* count_immovable_mem_regions - early parse SRAT, filter immovable +* memory regions +* +* Return amount of immovable memory regions on success, 0 on +* - Too many immovable memory regions +* - ACPI off or no SRAT found +* - No immovable memory region found +*/ +int count_immovable_mem_regions (void ) +{ +unsigned long table_addr, table_end, table; +struct acpi_subtable_header *sub_table; +struct acpi_table_header *table_header; +char arg[MAX_ACPI_ARG_LENGTH ]; +int num =0 ; + +if (cmdline_find_option ("acpi" , arg,sizeof (arg)) ==3 && + !strncmp (arg,"off" ,3 )) +return 0 ; + + table_addr =get_acpi_srat_table (); +if (!table_addr) +return 0 ; + + table_header = (struct acpi_table_header *)table_addr; + table_end = table_addr + table_header->length; + table = table_addr +sizeof (struct acpi_table_srat ); + +while (table +sizeof (struct acpi_subtable_header ) < table_end) { + sub_table = (struct acpi_subtable_header *)table; +if (sub_table->type ==ACPI_SRAT_TYPE_MEMORY_AFFINITY ) { +struct acpi_srat_mem_affinity *ma; + + ma = (struct acpi_srat_mem_affinity *)sub_table; +if (!(ma->flags &ACPI_SRAT_MEM_HOT_PLUGGABLE ) && + ma->length) { + immovable_mem[num].start = ma->base_address; + immovable_mem[num].size = ma->length; + num++; + } + +if (num >=MAX_NUMNODES *2 ) { +debug_putstr ("Too many immovable memory regions, aborting.\n" ); +return 0 ; + } + } + table += sub_table->length; + } +return num; +} +#endif /* CONFIG_RANDOMIZE_BASE && CONFIG_MEMORY_HOTREMOVE */
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 +11 -0
@@ -80,6 +80,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
@@ -126,3 +131,9 @@ acpi_physical_address get_rsdp_addr(void);#else static inline acpi_physical_address get_rsdp_addr (void ) {return 0 ; }#endif + +#if defined(CONFIG_RANDOMIZE_BASE ) && defined(CONFIG_MEMORY_HOTREMOVE ) +int count_immovable_mem_regions (void ); +#else +static inline int count_immovable_mem_regions (void ) {return 0 ; } +#endif
Signature · 1 line
2.20.1
tip-bot for Chao Fan
merge bot[tip:x86/boot] x86/boot: Parse SRAT table and count immovable memory regions
x86/boot: Parse SRAT table and count immovable memory regions
Commit-ID: 02a3e3cdb7f12fb8589e4a2e85d23c9e6544b54a Gitweb: https://git.kernel.org/tip/02a3e3cdb7f12fb8589e4a2e85d23c9e6544b54a Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:49 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:55 +0100
x86/boot: Parse SRAT table and count immovable memory regions
Parse SRAT for the immovable memory regions and use that information to control which offset KASLR selects so that it doesn't overlap with any movable region.
[ bp:
- Move struct mem_vector where it is visible so that it builds.
- Correct comments.
- Rewrite commit message.
]
--- arch/x86/boot/compressed/acpi.c | 121 +++++++++++++++++++++++++++++++++++++++ arch/x86/boot/compressed/kaslr.c | 4 -- arch/x86/boot/compressed/misc.h | 10 ++++ 3 files changed, 131 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/acpi.c +121 -0
@@ -4,9 +4,22 @@#include "error.h" #include "../string.h" +#include <linux/numa.h> #include <linux/efi.h> #include <asm/efi.h> +/* +* Longest parameter of 'acpi=' is 'copy_dsdt', plus an extra '\0' +* for termination. +*/ +#define MAX_ACPI_ARG_LENGTH 10 + +/* +* Immovable memory regions representation. Max amount of memory regions is +* MAX_NUMNODES*2. +*/ +struct mem_vector immovable_mem[MAX_NUMNODES *2 ]; +/* * Max length of 64-bit hex address string is 19, prefix "0x" + 16 hex * digits, and '\0' for termination.
@@ -203,3 +216,111 @@ acpi_physical_address get_rsdp_addr(void)return pa; } + +#if defined(CONFIG_RANDOMIZE_BASE ) && defined(CONFIG_MEMORY_HOTREMOVE ) +/* Compute SRAT address from RSDP. */ +static unsigned long get_acpi_srat_table (void ) +{ +unsigned long root_table, acpi_table; +struct acpi_table_header *header; +struct acpi_table_rsdp *rsdp; +u32 num_entries, size, len; +char arg[10 ]; +u8 *entry; + + rsdp = (struct acpi_table_rsdp *)(long )boot_params->acpi_rsdp_addr; +if (!rsdp) +return 0 ; + +/* Get ACPI root table from RSDP.*/ +if (!(cmdline_find_option ("acpi" , arg,sizeof (arg)) ==4 && + !strncmp (arg,"rsdt" ,4 )) && + rsdp->xsdt_physical_address && + rsdp->revision >1 ) { + root_table = rsdp->xsdt_physical_address; + size =ACPI_XSDT_ENTRY_SIZE ; + }else { + root_table = rsdp->rsdt_physical_address; + size =ACPI_RSDT_ENTRY_SIZE ; + } + +if (!root_table) +return 0 ; + + header = (struct acpi_table_header *)root_table; + len = header->length; +if (len <sizeof (struct acpi_table_header ) + size) +return 0 ; + + num_entries = (len -sizeof (struct acpi_table_header )) / size; + entry = (u8 *)(root_table +sizeof (struct acpi_table_header )); + +while (num_entries--) { +if (size ==ACPI_RSDT_ENTRY_SIZE ) + acpi_table = *(u32 *)entry; +else + acpi_table = *(u64 *)entry; + +if (acpi_table) { + header = (struct acpi_table_header *)acpi_table; + +if (ACPI_COMPARE_NAME (header->signature,ACPI_SIG_SRAT )) +return acpi_table; + } + entry += size; + } +return 0 ; +} + +/** +* count_immovable_mem_regions - Parse SRAT and cache the immovable +* memory regions into the immovable_mem array. +* +* Return the number of immovable memory regions on success, 0 on failure: +* +* - Too many immovable memory regions +* - ACPI off or no SRAT found +* - No immovable memory region found. +*/ +int count_immovable_mem_regions (void ) +{ +unsigned long table_addr, table_end, table; +struct acpi_subtable_header *sub_table; +struct acpi_table_header *table_header; +char arg[MAX_ACPI_ARG_LENGTH ]; +int num =0 ; + +if (cmdline_find_option ("acpi" , arg,sizeof (arg)) ==3 && + !strncmp (arg,"off" ,3 )) +return 0 ; + + table_addr =get_acpi_srat_table (); +if (!table_addr) +return 0 ; + + table_header = (struct acpi_table_header *)table_addr; + table_end = table_addr + table_header->length; + table = table_addr +sizeof (struct acpi_table_srat ); + +while (table +sizeof (struct acpi_subtable_header ) < table_end) { + sub_table = (struct acpi_subtable_header *)table; +if (sub_table->type ==ACPI_SRAT_TYPE_MEMORY_AFFINITY ) { +struct acpi_srat_mem_affinity *ma; + + ma = (struct acpi_srat_mem_affinity *)sub_table; +if (!(ma->flags &ACPI_SRAT_MEM_HOT_PLUGGABLE ) && ma->length) { + immovable_mem[num].start = ma->base_address; + immovable_mem[num].size = ma->length; + num++; + } + +if (num >=MAX_NUMNODES *2 ) { +debug_putstr ("Too many immovable memory regions, aborting.\n" ); +return 0 ; + } + } + table += sub_table->length; + } +return num; +} +#endif /* CONFIG_RANDOMIZE_BASE && CONFIG_MEMORY_HOTREMOVE */
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 +10 -0
@@ -72,6 +72,10 @@ int cmdline_find_option(const char *option, char *buffer, int bufsize);int cmdline_find_option_bool (const char *option);#endif +struct mem_vector { +unsigned long long start; +unsigned long long size; +};#if CONFIG_RANDOMIZE_BASE /* kaslr.c */
@@ -126,3 +130,9 @@ acpi_physical_address get_rsdp_addr(void);#else static inline acpi_physical_address get_rsdp_addr (void ) {return 0 ; }#endif + +#if defined(CONFIG_RANDOMIZE_BASE ) && defined(CONFIG_MEMORY_HOTREMOVE ) +int count_immovable_mem_regions (void ); +#else +static inline int count_immovable_mem_regions (void ) {return 0 ; } +#endif
PATCH v16 7/7 x86/boot/KASLR: Limit KASLR to extracting kernel in immovable memory
In reply to Chao FanKASLR may randomly choose some positions which are located in movable memory regions. It will break memory hotplug feature and make the movable memory chosen by KASLR can't be removed.
The solution is to limit KASLR to choose memory regions in immovable node according to SRAT tables.
When CONFIG_EARLY_SRAT_PARSE is enabled, walk through SRAT to get the information of immovable memory so that KASLR knows where should be chosen for randomization.
--- arch/x86/boot/compressed/kaslr.c | 72 +++++++++++++++++++++++++++----- arch/x86/boot/compressed/misc.h | 1 + 2 files changed, 62 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/kaslr.c +61 -11
@@ -97,6 +97,8 @@ 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 ; +/* Amount of immovable memory regions */ +static int num_immovable_mem;enum mem_avoid_index {MEM_AVOID_ZO_RANGE =0 ,
@@ -413,6 +415,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 */ + num_immovable_mem =count_immovable_mem_regions (); +#ifdef CONFIG_X86_VERBOSE_BOOTUP /* Make sure video RAM can be used. */ add_identity_map (0 ,PMD_SIZE );
@@ -568,9 +573,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 +651,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_MEMORY_HOTREMOVE +/* +* 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++) { +unsigned long long start, end, entry_end, region_end; +struct mem_vector entry; + +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 when walking immovable regions(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 +767,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 +795,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 ; - } } }
arch/x86/boot/compressed/misc.h +1 -0
@@ -133,6 +133,7 @@ static inline acpi_physical_address get_rsdp_addr(void) { return 0; }#endif #if defined(CONFIG_RANDOMIZE_BASE ) && defined(CONFIG_MEMORY_HOTREMOVE ) +extern struct mem_vector immovable_mem[MAX_NUMNODES *2 ];int count_immovable_mem_regions (void );#else static inline int count_immovable_mem_regions (void ) {return 0 ; }
Signature · 1 line
2.20.1
tip-bot for Chao Fan
merge bot[tip:x86/boot] x86/boot/KASLR: Limit KASLR to extract the kernel in immovable memory only
x86/boot/KASLR: Limit KASLR to extract the kernel in immovable memory only
Commit-ID: 690eaa53205769fd5c0c36e3aa737a615135f969 Gitweb: https://git.kernel.org/tip/690eaa53205769fd5c0c36e3aa737a615135f969 Author: Chao Fan <[email protected]> AuthorDate: Wed, 23 Jan 2019 19:08:50 +0800 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:52:55 +0100
x86/boot/KASLR: Limit KASLR to extract the kernel in immovable memory only
KASLR may randomly choose a range which is located in movable memory regions. As a result, this will break memory hotplug and make the movable memory chosen by KASLR immovable.
Therefore, limit KASLR to choose memory regions in the immovable range after consulting the SRAT table.
[ bp:
- Rewrite commit message.
- Trim comments.
]
--- arch/x86/boot/compressed/kaslr.c | 71 +++++++++++++++++++++++++++++++++------- arch/x86/boot/compressed/misc.h | 1 + 2 files changed, 61 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/kaslr.c +60 -11
@@ -97,6 +97,8 @@ 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 ; +/* Number of immovable memory regions */ +static int num_immovable_mem;enum mem_avoid_index {MEM_AVOID_ZO_RANGE =0 ,
@@ -413,6 +415,9 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,/* Mark the memmap regions we need to avoid */ handle_mem_options (); +/* Enumerate the immovable memory regions */ + num_immovable_mem =count_immovable_mem_regions (); +#ifdef CONFIG_X86_VERBOSE_BOOTUP /* Make sure video RAM can be used. */ add_identity_map (0 ,PMD_SIZE );
@@ -568,9 +573,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 +651,56 @@ 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, +* 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_MEMORY_HOTREMOVE +/* +* If immovable memory found, filter the intersection between +* immovable memory and @region. +*/ +for (i =0 ; i < num_immovable_mem; i++) { +unsigned long long start, end, entry_end, region_end; +struct mem_vector entry; + +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 when walking immovable regions(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 +766,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 +794,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 ; - } } }
arch/x86/boot/compressed/misc.h +1 -0
@@ -132,6 +132,7 @@ static inline acpi_physical_address get_rsdp_addr(void) { return 0; }#endif #if defined(CONFIG_RANDOMIZE_BASE ) && defined(CONFIG_MEMORY_HOTREMOVE ) +extern struct mem_vector immovable_mem[MAX_NUMNODES *2 ];int count_immovable_mem_regions (void );#else static inline int count_immovable_mem_regions (void ) {return 0 ; }
Borislav Petkov
maintainerQuoted 6 lines · Chao Fan
PATCH 1/7 Copy
kstrtoull()toboot/string.cto instead of using
oldsimple_strtoull()
PATCH 2/7 Introduceget_acpi_rsdp()to parse RSDP in cmdline from KEXEC PATCH 3/6 Introduceefi_get_rsdp_addr()to find RSDP from EFI table when
booting from EFI.
PATCH 4/7 Introducebios_get_rsdp_addr()to search RSDP in memory when
Quoted 8 lines
booting from BIOS
PATCH 5/7 Parse RSDP and fill inboot_params->acpi_rsdp_addrbefore
KASLR.
PATCH 6/7 Compute SRAT from RSDP and walk SRAT to store the immovable
memory regions and store the immovable memory regions.
PATCH 7/7 Calculate the intersection between memory regions from e820/efi
memory table and immovable memory regions. Limit KASLR to choosing these regions for randomization.
Ok, I've massaged the whole pile and fixed a couple of things that sprang at me, see each commit message for details.
Please run it and check whether I haven't broken anything:
https://git.kernel.org/pub/scm/linux/kernel/git/bp/bp.git/log/?h=tip-x86-boot
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Quoted 15 lines · Borislav Petkov
Quoted 6 lines · Chao Fan
PATCH 1/7 Copy
kstrtoull()toboot/string.cto instead of using
oldsimple_strtoull()
PATCH 2/7 Introduceget_acpi_rsdp()to parse RSDP in cmdline from KEXEC PATCH 3/6 Introduceefi_get_rsdp_addr()to find RSDP from EFI table when
booting from EFI.
PATCH 4/7 Introducebios_get_rsdp_addr()to search RSDP in memory whenQuoted 8 lines
booting from BIOS
PATCH 5/7 Parse RSDP and fill inboot_params->acpi_rsdp_addrbefore
KASLR.
PATCH 6/7 Compute SRAT from RSDP and walk SRAT to store the immovable
memory regions and store the immovable memory regions.
PATCH 7/7 Calculate the intersection between memory regions from e820/efi
memory table and immovable memory regions. Limit KASLR to choosing these regions for randomization.
Hi Boris,
Sorry for delay.
Quoted 3 lines
Ok, I've massaged the whole pile and fixed a couple of things that sprang at me, see each commit message for details.
Thanks for your fix, your change looks good.
Quoted 4 lines
Please run it and check whether I haven't broken anything:
https://git.kernel.org/pub/scm/linux/kernel/git/bp/bp.git/log/?h=tip-x86-boot
I clone your branch and test some cases. Except the build issue of cmdline_find_option(), PATCHSET works well.
Build in x86_64 and i386.
Tested EFI/BIOS environment in x86_64 and BIOS environment in i386. Tested the 'acpi_rsdp=' and 'boot_params->acpi_rsdp_addr' issue, all work well.
Thanks,
Chao Fan
Quoted 10 lines
Thx.
Signature · 4 lines
Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.
Borislav Petkov
maintainerPATCH 1/1 x86/boot: Build the command line parsing code unconditionally (was: Re: [PATCH v16 0/7] Parse ACPI table and limit KASLR to choosing immovable memory)
Quoted 6 lines · Chao Fan
I clone your branch and test some cases. Except the build issue of
cmdline_find_option(), PATCHSET works well.
Build inx86_64and i386.
Tested EFI/BIOS environment inx86_64and BIOS environment in i386. Tested the 'acpi_rsdp=' and 'boot_params->acpi_rsdp_addr' issue, all work well.
Thanks.
Here's a fix for the build issue below, pushing a new branch.
---
From: Borislav Petkov <[email protected]>
Subject: [PATCH] x86/boot: Build the command line parsing code unconditionally
Just drop the three-item ifdeffery and build it in unconditionally. Early cmdline parsing is needed more often than not.
--- arch/x86/boot/compressed/cmdline.c | 4 ---- 1 file changed, 4 deletions(-)
arch/x86/boot/compressed/cmdline.c +0 -4
@@ -1,8 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include "misc.h" -#if CONFIG_EARLY_PRINTK ||CONFIG_RANDOMIZE_BASE ||CONFIG_X86_5LEVEL -static unsigned long fs;static inline void set_fs (unsigned long seg) {
@@ -30,5 +28,3 @@ int cmdline_find_option_bool(const char *option) {return __cmdline_find_option_bool (get_cmd_line_ptr (), option); } - -#endif
Signature · 7 lines
2.19.1
--
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.tip-bot for Borislav Petkov
merge bot[tip:x86/boot] x86/boot: Build the command line parsing code unconditionally
x86/boot: Build the command line parsing code unconditionally
Commit-ID: ac09c5f43cf613939850cc38d7a34ae6556016ba Gitweb: https://git.kernel.org/tip/ac09c5f43cf613939850cc38d7a34ae6556016ba Author: Borislav Petkov <[email protected]> AuthorDate: Wed, 30 Jan 2019 11:19:37 +0100 Committer: Borislav Petkov <[email protected]> CommitDate: Fri, 1 Feb 2019 11:51:01 +0100
x86/boot: Build the command line parsing code unconditionally
Just drop the three-item ifdeffery and build it in unconditionally. Early cmdline parsing is needed more often than not.
--- arch/x86/boot/compressed/cmdline.c | 4 ---- 1 file changed, 4 deletions(-)
arch/x86/boot/compressed/cmdline.c +0 -4
@@ -1,8 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include "misc.h" -#if CONFIG_EARLY_PRINTK ||CONFIG_RANDOMIZE_BASE ||CONFIG_X86_5LEVEL -static unsigned long fs;static inline void set_fs (unsigned long seg) {
@@ -30,5 +28,3 @@ int cmdline_find_option_bool(const char *option) {return __cmdline_find_option_bool (get_cmd_line_ptr (), option); } - -#endif