License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 14:07:57 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2007-10-13 01:10:53 +00:00
|
|
|
* prepare to run common code
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
|
|
|
|
*/
|
|
|
|
|
2017-03-13 16:33:37 +00:00
|
|
|
#define DISABLE_BRANCH_PROFILING
|
2018-05-18 10:35:23 +00:00
|
|
|
|
|
|
|
/* cpu_feature_enabled() cannot be used this early */
|
|
|
|
#define USE_EARLY_PGTABLE_L5
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/linkage.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/percpu.h>
|
2008-01-30 12:30:19 +00:00
|
|
|
#include <linux/start_kernel.h>
|
2008-03-28 02:49:44 +00:00
|
|
|
#include <linux/io.h>
|
2010-08-25 20:39:17 +00:00
|
|
|
#include <linux/memblock.h>
|
2021-09-08 22:58:39 +00:00
|
|
|
#include <linux/cc_platform.h>
|
2020-06-09 04:32:42 +00:00
|
|
|
#include <linux/pgtable.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#include <asm/processor.h>
|
|
|
|
#include <asm/proto.h>
|
|
|
|
#include <asm/smp.h>
|
|
|
|
#include <asm/setup.h>
|
|
|
|
#include <asm/desc.h>
|
2007-05-02 17:27:07 +00:00
|
|
|
#include <asm/tlbflush.h>
|
2005-11-05 16:25:53 +00:00
|
|
|
#include <asm/sections.h>
|
2008-01-30 12:30:17 +00:00
|
|
|
#include <asm/kdebug.h>
|
2017-01-27 09:27:10 +00:00
|
|
|
#include <asm/e820/api.h>
|
2009-08-29 13:03:59 +00:00
|
|
|
#include <asm/bios_ebda.h>
|
2013-01-29 09:05:24 +00:00
|
|
|
#include <asm/bootparam_utils.h>
|
2012-12-21 07:44:30 +00:00
|
|
|
#include <asm/microcode.h>
|
2015-02-13 22:39:25 +00:00
|
|
|
#include <asm/kasan.h>
|
2018-09-20 02:58:28 +00:00
|
|
|
#include <asm/fixmap.h>
|
2020-09-07 13:15:34 +00:00
|
|
|
#include <asm/realmode.h>
|
2020-09-07 13:15:36 +00:00
|
|
|
#include <asm/extable.h>
|
|
|
|
#include <asm/trapnr.h>
|
2021-04-27 11:16:34 +00:00
|
|
|
#include <asm/sev.h>
|
2022-04-05 23:29:10 +00:00
|
|
|
#include <asm/tdx.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
/*
|
|
|
|
* Manage page tables very early on.
|
|
|
|
*/
|
|
|
|
extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES][PTRS_PER_PMD];
|
2017-06-06 11:31:26 +00:00
|
|
|
static unsigned int __initdata next_early_pgt;
|
2013-05-20 18:36:03 +00:00
|
|
|
pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
|
2018-02-14 11:16:51 +00:00
|
|
|
#ifdef CONFIG_X86_5LEVEL
|
2018-06-22 22:08:40 +00:00
|
|
|
unsigned int __pgtable_l5_enabled __ro_after_init;
|
2018-02-14 18:25:35 +00:00
|
|
|
unsigned int pgdir_shift __ro_after_init = 39;
|
2018-02-14 11:16:53 +00:00
|
|
|
EXPORT_SYMBOL(pgdir_shift);
|
2018-02-14 18:25:35 +00:00
|
|
|
unsigned int ptrs_per_p4d __ro_after_init = 1;
|
2018-02-14 11:16:53 +00:00
|
|
|
EXPORT_SYMBOL(ptrs_per_p4d);
|
2018-02-14 11:16:51 +00:00
|
|
|
#endif
|
|
|
|
|
2018-02-14 11:16:50 +00:00
|
|
|
#ifdef CONFIG_DYNAMIC_MEMORY_LAYOUT
|
2018-02-14 18:25:36 +00:00
|
|
|
unsigned long page_offset_base __ro_after_init = __PAGE_OFFSET_BASE_L4;
|
2018-02-14 11:16:50 +00:00
|
|
|
EXPORT_SYMBOL(page_offset_base);
|
2018-02-14 18:25:37 +00:00
|
|
|
unsigned long vmalloc_base __ro_after_init = __VMALLOC_BASE_L4;
|
2018-02-14 11:16:50 +00:00
|
|
|
EXPORT_SYMBOL(vmalloc_base);
|
2018-02-14 18:25:38 +00:00
|
|
|
unsigned long vmemmap_base __ro_after_init = __VMEMMAP_BASE_L4;
|
2018-02-14 11:16:50 +00:00
|
|
|
EXPORT_SYMBOL(vmemmap_base);
|
|
|
|
#endif
|
|
|
|
|
2020-09-07 13:15:30 +00:00
|
|
|
/*
|
|
|
|
* GDT used on the boot CPU before switching to virtual addresses.
|
|
|
|
*/
|
2023-07-12 03:30:05 +00:00
|
|
|
static struct desc_struct startup_gdt[GDT_ENTRIES] __initdata = {
|
2020-09-07 13:15:30 +00:00
|
|
|
[GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
|
|
|
|
[GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
|
|
|
|
[GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Address needs to be set at runtime because it references the startup_gdt
|
|
|
|
* while the kernel still uses a direct mapping.
|
|
|
|
*/
|
2023-07-12 03:30:05 +00:00
|
|
|
static struct desc_ptr startup_gdt_descr __initdata = {
|
2023-08-07 08:45:47 +00:00
|
|
|
.size = sizeof(startup_gdt)-1,
|
2020-09-07 13:15:30 +00:00
|
|
|
.address = 0,
|
|
|
|
};
|
|
|
|
|
2020-10-22 02:36:07 +00:00
|
|
|
#define __head __section(".head.text")
|
2017-06-16 11:30:24 +00:00
|
|
|
|
|
|
|
static void __head *fixup_pointer(void *ptr, unsigned long physaddr)
|
2017-06-06 11:31:26 +00:00
|
|
|
{
|
|
|
|
return ptr - (void *)_text + (void *)physaddr;
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:25:36 +00:00
|
|
|
static unsigned long __head *fixup_long(void *ptr, unsigned long physaddr)
|
|
|
|
{
|
|
|
|
return fixup_pointer(ptr, physaddr);
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:25:34 +00:00
|
|
|
#ifdef CONFIG_X86_5LEVEL
|
|
|
|
static unsigned int __head *fixup_int(void *ptr, unsigned long physaddr)
|
|
|
|
{
|
|
|
|
return fixup_pointer(ptr, physaddr);
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:25:39 +00:00
|
|
|
static bool __head check_la57_support(unsigned long physaddr)
|
2018-02-14 18:25:34 +00:00
|
|
|
{
|
2018-05-18 10:35:25 +00:00
|
|
|
/*
|
2021-03-18 14:28:01 +00:00
|
|
|
* 5-level paging is detected and enabled at kernel decompression
|
2018-05-18 10:35:25 +00:00
|
|
|
* stage. Only check if it has been enabled there.
|
|
|
|
*/
|
|
|
|
if (!(native_read_cr4() & X86_CR4_LA57))
|
2018-02-14 18:25:39 +00:00
|
|
|
return false;
|
2018-02-14 18:25:34 +00:00
|
|
|
|
2018-05-18 10:35:23 +00:00
|
|
|
*fixup_int(&__pgtable_l5_enabled, physaddr) = 1;
|
2018-02-14 18:25:35 +00:00
|
|
|
*fixup_int(&pgdir_shift, physaddr) = 48;
|
|
|
|
*fixup_int(&ptrs_per_p4d, physaddr) = 512;
|
2018-02-14 18:25:36 +00:00
|
|
|
*fixup_long(&page_offset_base, physaddr) = __PAGE_OFFSET_BASE_L5;
|
2018-02-14 18:25:37 +00:00
|
|
|
*fixup_long(&vmalloc_base, physaddr) = __VMALLOC_BASE_L5;
|
2018-02-14 18:25:38 +00:00
|
|
|
*fixup_long(&vmemmap_base, physaddr) = __VMEMMAP_BASE_L5;
|
2018-02-14 18:25:39 +00:00
|
|
|
|
|
|
|
return true;
|
2018-02-14 18:25:34 +00:00
|
|
|
}
|
|
|
|
#else
|
2018-02-14 18:25:39 +00:00
|
|
|
static bool __head check_la57_support(unsigned long physaddr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-14 18:25:34 +00:00
|
|
|
#endif
|
|
|
|
|
2022-02-11 16:23:50 +00:00
|
|
|
static unsigned long __head sme_postprocess_startup(struct boot_params *bp, pmdval_t *pmd)
|
2021-11-10 22:06:52 +00:00
|
|
|
{
|
|
|
|
unsigned long vaddr, vaddr_end;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Encrypt the kernel and related (if SME is active) */
|
|
|
|
sme_encrypt_kernel(bp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear the memory encryption mask from the .bss..decrypted section.
|
|
|
|
* The bss section will be memset to zero later in the initialization so
|
|
|
|
* there is no need to zero it after changing the memory encryption
|
|
|
|
* attribute.
|
|
|
|
*/
|
|
|
|
if (sme_get_me_mask()) {
|
|
|
|
vaddr = (unsigned long)__start_bss_decrypted;
|
|
|
|
vaddr_end = (unsigned long)__end_bss_decrypted;
|
2022-02-09 18:10:13 +00:00
|
|
|
|
2021-11-10 22:06:52 +00:00
|
|
|
for (; vaddr < vaddr_end; vaddr += PMD_SIZE) {
|
2022-02-09 18:10:13 +00:00
|
|
|
/*
|
|
|
|
* On SNP, transition the page to shared in the RMP table so that
|
|
|
|
* it is consistent with the page table attribute change.
|
|
|
|
*
|
|
|
|
* __start_bss_decrypted has a virtual address in the high range
|
|
|
|
* mapping (kernel .text). PVALIDATE, by way of
|
|
|
|
* early_snp_set_memory_shared(), requires a valid virtual
|
|
|
|
* address but the kernel is currently running off of the identity
|
|
|
|
* mapping so use __pa() to get a *currently* valid virtual address.
|
|
|
|
*/
|
|
|
|
early_snp_set_memory_shared(__pa(vaddr), __pa(vaddr), PTRS_PER_PMD);
|
|
|
|
|
2021-11-10 22:06:52 +00:00
|
|
|
i = pmd_index(vaddr);
|
|
|
|
pmd[i] -= sme_get_me_mask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the SME encryption mask (if SME is active) to be used as a
|
|
|
|
* modifier for the initial pgdir entry programmed into CR3.
|
|
|
|
*/
|
|
|
|
return sme_get_me_mask();
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:18:22 +00:00
|
|
|
/* Code in __startup_64() can be relocated during execution, but the compiler
|
|
|
|
* doesn't have to generate PC-relative relocations when accessing globals from
|
|
|
|
* that function. Clang actually does not generate them, which leads to
|
|
|
|
* boot-time crashes. To work around this problem, every global pointer must
|
|
|
|
* be adjusted using fixup_pointer().
|
|
|
|
*/
|
2017-07-17 21:10:35 +00:00
|
|
|
unsigned long __head __startup_64(unsigned long physaddr,
|
|
|
|
struct boot_params *bp)
|
2017-06-06 11:31:26 +00:00
|
|
|
{
|
2018-02-14 18:25:39 +00:00
|
|
|
unsigned long load_delta, *p;
|
2017-07-17 21:10:05 +00:00
|
|
|
unsigned long pgtable_flags;
|
2017-06-06 11:31:26 +00:00
|
|
|
pgdval_t *pgd;
|
2017-06-06 11:31:28 +00:00
|
|
|
p4dval_t *p4d;
|
2017-06-06 11:31:26 +00:00
|
|
|
pudval_t *pud;
|
|
|
|
pmdval_t *pmd, pmd_entry;
|
2018-05-09 09:18:22 +00:00
|
|
|
pteval_t *mask_ptr;
|
2018-02-14 18:25:39 +00:00
|
|
|
bool la57;
|
2017-06-06 11:31:26 +00:00
|
|
|
int i;
|
2017-08-16 19:08:08 +00:00
|
|
|
unsigned int *next_pgt_ptr;
|
2017-06-06 11:31:26 +00:00
|
|
|
|
2018-02-14 18:25:39 +00:00
|
|
|
la57 = check_la57_support(physaddr);
|
2018-02-14 18:25:34 +00:00
|
|
|
|
2017-06-06 11:31:26 +00:00
|
|
|
/* Is the address too large? */
|
|
|
|
if (physaddr >> MAX_PHYSMEM_BITS)
|
|
|
|
for (;;);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the delta between the address I am compiled to run at
|
|
|
|
* and the address I am actually running at.
|
|
|
|
*/
|
|
|
|
load_delta = physaddr - (unsigned long)(_text - __START_KERNEL_map);
|
|
|
|
|
|
|
|
/* Is the address not 2M aligned? */
|
2022-05-16 18:52:02 +00:00
|
|
|
if (load_delta & ~PMD_MASK)
|
2017-06-06 11:31:26 +00:00
|
|
|
for (;;);
|
|
|
|
|
2017-07-17 21:10:05 +00:00
|
|
|
/* Include the SME encryption mask in the fixup value */
|
|
|
|
load_delta += sme_get_me_mask();
|
|
|
|
|
2017-06-06 11:31:26 +00:00
|
|
|
/* Fixup the physical addresses in the page table */
|
|
|
|
|
2023-08-03 09:44:00 +00:00
|
|
|
pgd = fixup_pointer(early_top_pgt, physaddr);
|
2018-02-14 18:25:39 +00:00
|
|
|
p = pgd + pgd_index(__START_KERNEL_map);
|
|
|
|
if (la57)
|
|
|
|
*p = (unsigned long)level4_kernel_pgt;
|
|
|
|
else
|
|
|
|
*p = (unsigned long)level3_kernel_pgt;
|
|
|
|
*p += _PAGE_TABLE_NOENC - __START_KERNEL_map + load_delta;
|
|
|
|
|
|
|
|
if (la57) {
|
2023-08-03 09:44:00 +00:00
|
|
|
p4d = fixup_pointer(level4_kernel_pgt, physaddr);
|
2017-06-06 11:31:28 +00:00
|
|
|
p4d[511] += load_delta;
|
|
|
|
}
|
|
|
|
|
2023-08-03 09:44:00 +00:00
|
|
|
pud = fixup_pointer(level3_kernel_pgt, physaddr);
|
2017-06-06 11:31:26 +00:00
|
|
|
pud[510] += load_delta;
|
|
|
|
pud[511] += load_delta;
|
|
|
|
|
|
|
|
pmd = fixup_pointer(level2_fixmap_pgt, physaddr);
|
2018-09-20 02:58:28 +00:00
|
|
|
for (i = FIXMAP_PMD_TOP; i > FIXMAP_PMD_TOP - FIXMAP_PMD_NUM; i--)
|
|
|
|
pmd[i] += load_delta;
|
2017-06-06 11:31:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the identity mapping for the switchover. These
|
|
|
|
* entries should *NOT* have the global bit set! This also
|
|
|
|
* creates a bunch of nonsense entries but that is fine --
|
|
|
|
* it avoids problems around wraparound.
|
|
|
|
*/
|
|
|
|
|
2017-08-16 19:08:08 +00:00
|
|
|
next_pgt_ptr = fixup_pointer(&next_early_pgt, physaddr);
|
|
|
|
pud = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr);
|
|
|
|
pmd = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr);
|
2017-06-06 11:31:26 +00:00
|
|
|
|
2017-07-17 21:10:07 +00:00
|
|
|
pgtable_flags = _KERNPG_TABLE_NOENC + sme_get_me_mask();
|
2017-06-06 11:31:26 +00:00
|
|
|
|
2018-02-14 18:25:39 +00:00
|
|
|
if (la57) {
|
2019-06-20 11:24:22 +00:00
|
|
|
p4d = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++],
|
|
|
|
physaddr);
|
2017-06-06 11:31:28 +00:00
|
|
|
|
|
|
|
i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
|
2017-07-17 21:10:05 +00:00
|
|
|
pgd[i + 0] = (pgdval_t)p4d + pgtable_flags;
|
|
|
|
pgd[i + 1] = (pgdval_t)p4d + pgtable_flags;
|
2017-06-06 11:31:28 +00:00
|
|
|
|
2019-06-20 11:23:45 +00:00
|
|
|
i = physaddr >> P4D_SHIFT;
|
|
|
|
p4d[(i + 0) % PTRS_PER_P4D] = (pgdval_t)pud + pgtable_flags;
|
|
|
|
p4d[(i + 1) % PTRS_PER_P4D] = (pgdval_t)pud + pgtable_flags;
|
2017-06-06 11:31:28 +00:00
|
|
|
} else {
|
|
|
|
i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
|
2017-07-17 21:10:05 +00:00
|
|
|
pgd[i + 0] = (pgdval_t)pud + pgtable_flags;
|
|
|
|
pgd[i + 1] = (pgdval_t)pud + pgtable_flags;
|
2017-06-06 11:31:28 +00:00
|
|
|
}
|
2017-06-06 11:31:26 +00:00
|
|
|
|
2019-06-20 11:23:45 +00:00
|
|
|
i = physaddr >> PUD_SHIFT;
|
|
|
|
pud[(i + 0) % PTRS_PER_PUD] = (pudval_t)pmd + pgtable_flags;
|
|
|
|
pud[(i + 1) % PTRS_PER_PUD] = (pudval_t)pmd + pgtable_flags;
|
2017-06-06 11:31:26 +00:00
|
|
|
|
|
|
|
pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL;
|
2018-04-06 20:55:09 +00:00
|
|
|
/* Filter out unsupported __PAGE_KERNEL_* bits: */
|
2018-05-09 09:18:22 +00:00
|
|
|
mask_ptr = fixup_pointer(&__supported_pte_mask, physaddr);
|
|
|
|
pmd_entry &= *mask_ptr;
|
2017-07-17 21:10:05 +00:00
|
|
|
pmd_entry += sme_get_me_mask();
|
2017-06-06 11:31:26 +00:00
|
|
|
pmd_entry += physaddr;
|
|
|
|
|
|
|
|
for (i = 0; i < DIV_ROUND_UP(_end - _text, PMD_SIZE); i++) {
|
2019-06-20 11:23:45 +00:00
|
|
|
int idx = i + (physaddr >> PMD_SHIFT);
|
|
|
|
|
|
|
|
pmd[idx % PTRS_PER_PMD] = pmd_entry + i * PMD_SIZE;
|
2017-06-06 11:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fixup the kernel text+data virtual addresses. Note that
|
|
|
|
* we might write invalid pmds, when the kernel is relocated
|
|
|
|
* cleanup_highmap() fixes this up along with the mappings
|
|
|
|
* beyond _end.
|
x86/boot/64: Make level2_kernel_pgt pages invalid outside kernel area
Our hardware (UV aka Superdome Flex) has address ranges marked
reserved by the BIOS. Access to these ranges is caught as an error,
causing the BIOS to halt the system.
Initial page tables mapped a large range of physical addresses that
were not checked against the list of BIOS reserved addresses, and
sometimes included reserved addresses in part of the mapped range.
Including the reserved range in the map allowed processor speculative
accesses to the reserved range, triggering a BIOS halt.
Used early in booting, the page table level2_kernel_pgt addresses 1
GiB divided into 2 MiB pages, and it was set up to linearly map a full
1 GiB of physical addresses that included the physical address range
of the kernel image, as chosen by KASLR. But this also included a
large range of unused addresses on either side of the kernel image.
And unlike the kernel image's physical address range, this extra
mapped space was not checked against the BIOS tables of usable RAM
addresses. So there were times when the addresses chosen by KASLR
would result in processor accessible mappings of BIOS reserved
physical addresses.
The kernel code did not directly access any of this extra mapped
space, but having it mapped allowed the processor to issue speculative
accesses into reserved memory, causing system halts.
This was encountered somewhat rarely on a normal system boot, and much
more often when starting the crash kernel if "crashkernel=512M,high"
was specified on the command line (this heavily restricts the physical
address of the crash kernel, in our case usually within 1 GiB of
reserved space).
The solution is to invalidate the pages of this table outside the kernel
image's space before the page table is activated. It fixes this problem
on our hardware.
[ bp: Touchups. ]
Signed-off-by: Steve Wahl <steve.wahl@hpe.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Baoquan He <bhe@redhat.com>
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: dimitri.sivanich@hpe.com
Cc: Feng Tang <feng.tang@intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jordan Borgner <mail@jordan-borgner.de>
Cc: Juergen Gross <jgross@suse.com>
Cc: mike.travis@hpe.com
Cc: russ.anderson@hpe.com
Cc: stable@vger.kernel.org
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: x86-ml <x86@kernel.org>
Cc: Zhenzhong Duan <zhenzhong.duan@oracle.com>
Link: https://lkml.kernel.org/r/9c011ee51b081534a7a15065b1681d200298b530.1569358539.git.steve.wahl@hpe.com
2019-09-24 21:03:55 +00:00
|
|
|
*
|
|
|
|
* Only the region occupied by the kernel image has so far
|
|
|
|
* been checked against the table of usable memory regions
|
|
|
|
* provided by the firmware, so invalidate pages outside that
|
|
|
|
* region. A page table entry that maps to a reserved area of
|
|
|
|
* memory would allow processor speculation into that area,
|
|
|
|
* and on some hardware (particularly the UV platform) even
|
|
|
|
* speculative access to some reserved areas is caught as an
|
|
|
|
* error, causing the BIOS to halt the system.
|
2017-06-06 11:31:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
pmd = fixup_pointer(level2_kernel_pgt, physaddr);
|
x86/boot/64: Make level2_kernel_pgt pages invalid outside kernel area
Our hardware (UV aka Superdome Flex) has address ranges marked
reserved by the BIOS. Access to these ranges is caught as an error,
causing the BIOS to halt the system.
Initial page tables mapped a large range of physical addresses that
were not checked against the list of BIOS reserved addresses, and
sometimes included reserved addresses in part of the mapped range.
Including the reserved range in the map allowed processor speculative
accesses to the reserved range, triggering a BIOS halt.
Used early in booting, the page table level2_kernel_pgt addresses 1
GiB divided into 2 MiB pages, and it was set up to linearly map a full
1 GiB of physical addresses that included the physical address range
of the kernel image, as chosen by KASLR. But this also included a
large range of unused addresses on either side of the kernel image.
And unlike the kernel image's physical address range, this extra
mapped space was not checked against the BIOS tables of usable RAM
addresses. So there were times when the addresses chosen by KASLR
would result in processor accessible mappings of BIOS reserved
physical addresses.
The kernel code did not directly access any of this extra mapped
space, but having it mapped allowed the processor to issue speculative
accesses into reserved memory, causing system halts.
This was encountered somewhat rarely on a normal system boot, and much
more often when starting the crash kernel if "crashkernel=512M,high"
was specified on the command line (this heavily restricts the physical
address of the crash kernel, in our case usually within 1 GiB of
reserved space).
The solution is to invalidate the pages of this table outside the kernel
image's space before the page table is activated. It fixes this problem
on our hardware.
[ bp: Touchups. ]
Signed-off-by: Steve Wahl <steve.wahl@hpe.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Baoquan He <bhe@redhat.com>
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: dimitri.sivanich@hpe.com
Cc: Feng Tang <feng.tang@intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jordan Borgner <mail@jordan-borgner.de>
Cc: Juergen Gross <jgross@suse.com>
Cc: mike.travis@hpe.com
Cc: russ.anderson@hpe.com
Cc: stable@vger.kernel.org
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: x86-ml <x86@kernel.org>
Cc: Zhenzhong Duan <zhenzhong.duan@oracle.com>
Link: https://lkml.kernel.org/r/9c011ee51b081534a7a15065b1681d200298b530.1569358539.git.steve.wahl@hpe.com
2019-09-24 21:03:55 +00:00
|
|
|
|
|
|
|
/* invalidate pages before the kernel image */
|
|
|
|
for (i = 0; i < pmd_index((unsigned long)_text); i++)
|
|
|
|
pmd[i] &= ~_PAGE_PRESENT;
|
|
|
|
|
|
|
|
/* fixup pages that are part of the kernel image */
|
|
|
|
for (; i <= pmd_index((unsigned long)_end); i++)
|
2017-06-06 11:31:26 +00:00
|
|
|
if (pmd[i] & _PAGE_PRESENT)
|
|
|
|
pmd[i] += load_delta;
|
x86/boot/64: Make level2_kernel_pgt pages invalid outside kernel area
Our hardware (UV aka Superdome Flex) has address ranges marked
reserved by the BIOS. Access to these ranges is caught as an error,
causing the BIOS to halt the system.
Initial page tables mapped a large range of physical addresses that
were not checked against the list of BIOS reserved addresses, and
sometimes included reserved addresses in part of the mapped range.
Including the reserved range in the map allowed processor speculative
accesses to the reserved range, triggering a BIOS halt.
Used early in booting, the page table level2_kernel_pgt addresses 1
GiB divided into 2 MiB pages, and it was set up to linearly map a full
1 GiB of physical addresses that included the physical address range
of the kernel image, as chosen by KASLR. But this also included a
large range of unused addresses on either side of the kernel image.
And unlike the kernel image's physical address range, this extra
mapped space was not checked against the BIOS tables of usable RAM
addresses. So there were times when the addresses chosen by KASLR
would result in processor accessible mappings of BIOS reserved
physical addresses.
The kernel code did not directly access any of this extra mapped
space, but having it mapped allowed the processor to issue speculative
accesses into reserved memory, causing system halts.
This was encountered somewhat rarely on a normal system boot, and much
more often when starting the crash kernel if "crashkernel=512M,high"
was specified on the command line (this heavily restricts the physical
address of the crash kernel, in our case usually within 1 GiB of
reserved space).
The solution is to invalidate the pages of this table outside the kernel
image's space before the page table is activated. It fixes this problem
on our hardware.
[ bp: Touchups. ]
Signed-off-by: Steve Wahl <steve.wahl@hpe.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Baoquan He <bhe@redhat.com>
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: dimitri.sivanich@hpe.com
Cc: Feng Tang <feng.tang@intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jordan Borgner <mail@jordan-borgner.de>
Cc: Juergen Gross <jgross@suse.com>
Cc: mike.travis@hpe.com
Cc: russ.anderson@hpe.com
Cc: stable@vger.kernel.org
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: x86-ml <x86@kernel.org>
Cc: Zhenzhong Duan <zhenzhong.duan@oracle.com>
Link: https://lkml.kernel.org/r/9c011ee51b081534a7a15065b1681d200298b530.1569358539.git.steve.wahl@hpe.com
2019-09-24 21:03:55 +00:00
|
|
|
|
|
|
|
/* invalidate pages after the kernel image */
|
|
|
|
for (; i < PTRS_PER_PMD; i++)
|
|
|
|
pmd[i] &= ~_PAGE_PRESENT;
|
2017-06-06 11:31:26 +00:00
|
|
|
|
2017-07-17 21:10:05 +00:00
|
|
|
/*
|
|
|
|
* Fixup phys_base - remove the memory encryption mask to obtain
|
|
|
|
* the true physical address.
|
|
|
|
*/
|
2018-02-14 18:25:36 +00:00
|
|
|
*fixup_long(&phys_base, physaddr) += load_delta - sme_get_me_mask();
|
2017-07-17 21:10:05 +00:00
|
|
|
|
2021-11-10 22:06:52 +00:00
|
|
|
return sme_postprocess_startup(bp, pmd);
|
2017-07-17 21:10:05 +00:00
|
|
|
}
|
|
|
|
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
/* Wipe all early page tables except for the kernel symbol map */
|
|
|
|
static void __init reset_early_page_tables(void)
|
2007-05-02 17:27:07 +00:00
|
|
|
{
|
2017-06-06 11:31:27 +00:00
|
|
|
memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1));
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
next_early_pgt = 0;
|
2017-07-17 21:10:07 +00:00
|
|
|
write_cr3(__sme_pa_nodebug(early_top_pgt));
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new PMD entry */
|
2020-09-07 13:15:36 +00:00
|
|
|
bool __init __early_make_pgtable(unsigned long address, pmdval_t pmd)
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
{
|
|
|
|
unsigned long physaddr = address - __PAGE_OFFSET;
|
|
|
|
pgdval_t pgd, *pgd_p;
|
2017-06-06 11:31:28 +00:00
|
|
|
p4dval_t p4d, *p4d_p;
|
2013-01-24 20:19:53 +00:00
|
|
|
pudval_t pud, *pud_p;
|
2017-07-17 21:10:11 +00:00
|
|
|
pmdval_t *pmd_p;
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
/* Invalid address or early pgt is done ? */
|
2017-06-06 11:31:27 +00:00
|
|
|
if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt))
|
2020-09-07 13:15:36 +00:00
|
|
|
return false;
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
|
2013-01-24 20:19:53 +00:00
|
|
|
again:
|
2017-06-06 11:31:27 +00:00
|
|
|
pgd_p = &early_top_pgt[pgd_index(address)].pgd;
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
pgd = *pgd_p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The use of __START_KERNEL_map rather than __PAGE_OFFSET here is
|
|
|
|
* critical -- __PAGE_OFFSET would point us back into the dynamic
|
|
|
|
* range and we might end up looping forever...
|
|
|
|
*/
|
2018-05-18 10:35:24 +00:00
|
|
|
if (!pgtable_l5_enabled())
|
2017-06-06 11:31:28 +00:00
|
|
|
p4d_p = pgd_p;
|
|
|
|
else if (pgd)
|
|
|
|
p4d_p = (p4dval_t *)((pgd & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
|
|
|
|
else {
|
|
|
|
if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
|
|
|
|
reset_early_page_tables();
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
p4d_p = (p4dval_t *)early_dynamic_pgts[next_early_pgt++];
|
|
|
|
memset(p4d_p, 0, sizeof(*p4d_p) * PTRS_PER_P4D);
|
|
|
|
*pgd_p = (pgdval_t)p4d_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
|
|
|
|
}
|
|
|
|
p4d_p += p4d_index(address);
|
|
|
|
p4d = *p4d_p;
|
|
|
|
|
|
|
|
if (p4d)
|
|
|
|
pud_p = (pudval_t *)((p4d & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
|
2013-01-24 20:19:53 +00:00
|
|
|
else {
|
|
|
|
if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
reset_early_page_tables();
|
2013-01-24 20:19:53 +00:00
|
|
|
goto again;
|
|
|
|
}
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
|
|
|
|
pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++];
|
2016-02-09 13:44:54 +00:00
|
|
|
memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD);
|
2017-06-06 11:31:28 +00:00
|
|
|
*p4d_p = (p4dval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
}
|
2013-01-24 20:19:53 +00:00
|
|
|
pud_p += pud_index(address);
|
|
|
|
pud = *pud_p;
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
|
2013-01-24 20:19:53 +00:00
|
|
|
if (pud)
|
|
|
|
pmd_p = (pmdval_t *)((pud & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
|
|
|
|
else {
|
|
|
|
if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
|
|
|
|
reset_early_page_tables();
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
pmd_p = (pmdval_t *)early_dynamic_pgts[next_early_pgt++];
|
2016-02-09 13:44:54 +00:00
|
|
|
memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD);
|
2013-01-24 20:19:53 +00:00
|
|
|
*pud_p = (pudval_t)pmd_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
|
|
|
|
}
|
|
|
|
pmd_p[pmd_index(address)] = pmd;
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
|
2020-09-07 13:15:36 +00:00
|
|
|
return true;
|
2007-05-02 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 13:15:36 +00:00
|
|
|
static bool __init early_make_pgtable(unsigned long address)
|
2017-07-17 21:10:11 +00:00
|
|
|
{
|
|
|
|
unsigned long physaddr = address - __PAGE_OFFSET;
|
|
|
|
pmdval_t pmd;
|
|
|
|
|
|
|
|
pmd = (physaddr & PMD_MASK) + early_pmd_flags;
|
|
|
|
|
|
|
|
return __early_make_pgtable(address, pmd);
|
|
|
|
}
|
|
|
|
|
2020-09-07 13:15:36 +00:00
|
|
|
void __init do_early_exception(struct pt_regs *regs, int trapnr)
|
|
|
|
{
|
|
|
|
if (trapnr == X86_TRAP_PF &&
|
|
|
|
early_make_pgtable(native_read_cr2()))
|
|
|
|
return;
|
|
|
|
|
2020-09-08 12:38:16 +00:00
|
|
|
if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT) &&
|
|
|
|
trapnr == X86_TRAP_VC && handle_vc_boot_ghcb(regs))
|
|
|
|
return;
|
|
|
|
|
2022-04-05 23:29:27 +00:00
|
|
|
if (trapnr == X86_TRAP_VE && tdx_early_handle_ve(regs))
|
|
|
|
return;
|
|
|
|
|
2020-09-07 13:15:36 +00:00
|
|
|
early_fixup_exception(regs, trapnr);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Don't add a printk in there. printk relies on the PDA which is not initialized
|
|
|
|
yet. */
|
2022-06-30 07:14:39 +00:00
|
|
|
void __init clear_bss(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
memset(__bss_start, 0,
|
2005-11-05 16:25:53 +00:00
|
|
|
(unsigned long) __bss_stop - (unsigned long) __bss_start);
|
2022-06-30 07:14:40 +00:00
|
|
|
memset(__brk_base, 0,
|
|
|
|
(unsigned long) __brk_limit - (unsigned long) __brk_base);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 20:19:57 +00:00
|
|
|
static unsigned long get_cmd_line_ptr(void)
|
|
|
|
{
|
|
|
|
unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
|
|
|
|
|
2013-01-29 04:16:44 +00:00
|
|
|
cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32;
|
|
|
|
|
2013-01-24 20:19:57 +00:00
|
|
|
return cmd_line_ptr;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static void __init copy_bootdata(char *real_mode_data)
|
|
|
|
{
|
|
|
|
char * command_line;
|
2013-01-24 20:19:57 +00:00
|
|
|
unsigned long cmd_line_ptr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2017-07-17 21:10:11 +00:00
|
|
|
/*
|
|
|
|
* If SME is active, this will create decrypted mappings of the
|
|
|
|
* boot data in advance of the copy operations.
|
|
|
|
*/
|
|
|
|
sme_map_bootdata(real_mode_data);
|
|
|
|
|
2018-10-28 12:58:28 +00:00
|
|
|
memcpy(&boot_params, real_mode_data, sizeof(boot_params));
|
2013-01-29 09:05:24 +00:00
|
|
|
sanitize_boot_params(&boot_params);
|
2013-01-24 20:19:57 +00:00
|
|
|
cmd_line_ptr = get_cmd_line_ptr();
|
|
|
|
if (cmd_line_ptr) {
|
|
|
|
command_line = __va(cmd_line_ptr);
|
2007-10-16 00:13:22 +00:00
|
|
|
memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2017-07-17 21:10:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The old boot data is no longer needed and won't be reserved,
|
|
|
|
* freeing up that memory for use by the system. If SME is active,
|
|
|
|
* we need to remove the mappings that were created so that the
|
|
|
|
* memory doesn't remain mapped as decrypted.
|
|
|
|
*/
|
|
|
|
sme_unmap_bootdata(real_mode_data);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 23:49:33 +00:00
|
|
|
asmlinkage __visible void __init __noreturn x86_64_start_kernel(char * real_mode_data)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-02-21 12:45:16 +00:00
|
|
|
/*
|
|
|
|
* Build-time sanity checks on the kernel image and module
|
|
|
|
* area mappings. (these are purely build-time and produce no code)
|
|
|
|
*/
|
2013-03-04 20:16:17 +00:00
|
|
|
BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map);
|
|
|
|
BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE);
|
2008-02-21 12:45:16 +00:00
|
|
|
BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE);
|
2013-03-04 20:16:17 +00:00
|
|
|
BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0);
|
2008-02-21 12:45:16 +00:00
|
|
|
BUILD_BUG_ON((MODULES_VADDR & ~PMD_MASK) != 0);
|
|
|
|
BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
|
2018-02-14 11:16:53 +00:00
|
|
|
MAYBE_BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
|
2008-02-21 12:45:16 +00:00
|
|
|
(__START_KERNEL & PGDIR_MASK)));
|
2008-07-31 15:48:31 +00:00
|
|
|
BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END);
|
2008-02-21 12:45:16 +00:00
|
|
|
|
2014-10-24 22:58:08 +00:00
|
|
|
cr4_init_shadow();
|
|
|
|
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
/* Kill off the identity-map trampoline */
|
|
|
|
reset_early_page_tables();
|
|
|
|
|
2006-12-07 01:14:12 +00:00
|
|
|
clear_bss();
|
|
|
|
|
2021-12-17 15:48:29 +00:00
|
|
|
/*
|
|
|
|
* This needs to happen *before* kasan_early_init() because latter maps stuff
|
|
|
|
* into that page.
|
|
|
|
*/
|
2017-06-06 11:31:27 +00:00
|
|
|
clear_page(init_top_pgt);
|
2015-07-02 09:09:33 +00:00
|
|
|
|
2017-07-17 21:10:07 +00:00
|
|
|
/*
|
|
|
|
* SME support may update early_pmd_flags to include the memory
|
|
|
|
* encryption mask, so it needs to be called before anything
|
|
|
|
* that may generate a page fault.
|
|
|
|
*/
|
|
|
|
sme_early_init();
|
|
|
|
|
2015-07-02 09:09:34 +00:00
|
|
|
kasan_early_init();
|
|
|
|
|
2021-12-17 15:48:29 +00:00
|
|
|
/*
|
|
|
|
* Flush global TLB entries which could be left over from the trampoline page
|
|
|
|
* table.
|
|
|
|
*
|
|
|
|
* This needs to happen *after* kasan_early_init() as KASAN-enabled .configs
|
|
|
|
* instrument native_write_cr4() so KASAN must be initialized for that
|
|
|
|
* instrumentation to work.
|
|
|
|
*/
|
|
|
|
__native_tlb_flush_global(this_cpu_read(cpu_tlbstate.cr4));
|
|
|
|
|
2017-08-28 06:47:47 +00:00
|
|
|
idt_setup_early_handler();
|
2005-11-05 16:25:53 +00:00
|
|
|
|
2022-04-05 23:29:10 +00:00
|
|
|
/* Needed before cc_platform_has() can be used for TDX */
|
|
|
|
tdx_early_init();
|
|
|
|
|
2013-01-24 20:19:49 +00:00
|
|
|
copy_bootdata(__va(real_mode_data));
|
|
|
|
|
2012-12-21 07:44:30 +00:00
|
|
|
/*
|
|
|
|
* Load microcode early on BSP.
|
|
|
|
*/
|
|
|
|
load_ucode_bsp();
|
|
|
|
|
2017-06-06 11:31:27 +00:00
|
|
|
/* set init_top_pgt kernel high mapping*/
|
|
|
|
init_top_pgt[511] = early_top_pgt[511];
|
x86, 64bit: Use a #PF handler to materialize early mappings on demand
Linear mode (CR0.PG = 0) is mutually exclusive with 64-bit mode; all
64-bit code has to use page tables. This makes it awkward before we
have first set up properly all-covering page tables to access objects
that are outside the static kernel range.
So far we have dealt with that simply by mapping a fixed amount of
low memory, but that fails in at least two upcoming use cases:
1. We will support load and run kernel, struct boot_params, ramdisk,
command line, etc. above the 4 GiB mark.
2. need to access ramdisk early to get microcode to update that as
early possible.
We could use early_iomap to access them too, but it will make code to
messy and hard to be unified with 32 bit.
Hence, set up a #PF table and use a fixed number of buffers to set up
page tables on demand. If the buffers fill up then we simply flush
them and start over. These buffers are all in __initdata, so it does
not increase RAM usage at runtime.
Thus, with the help of the #PF handler, we can set the final kernel
mapping from blank, and switch to init_level4_pgt later.
During the switchover in head_64.S, before #PF handler is available,
we use three pages to handle kernel crossing 1G, 512G boundaries with
sharing page by playing games with page aliasing: the same page is
mapped twice in the higher-level tables with appropriate wraparound.
The kernel region itself will be properly mapped; other mappings may
be spurious.
early_make_pgtable is using kernel high mapping address to access pages
to set page table.
-v4: Add phys_base offset to make kexec happy, and add
init_mapping_kernel() - Yinghai
-v5: fix compiling with xen, and add back ident level3 and level2 for xen
also move back init_level4_pgt from BSS to DATA again.
because we have to clear it anyway. - Yinghai
-v6: switch to init_level4_pgt in init_mem_mapping. - Yinghai
-v7: remove not needed clear_page for init_level4_page
it is with fill 512,8,0 already in head_64.S - Yinghai
-v8: we need to keep that handler alive until init_mem_mapping and don't
let early_trap_init to trash that early #PF handler.
So split early_trap_pf_init out and move it down. - Yinghai
-v9: switchover only cover kernel space instead of 1G so could avoid
touch possible mem holes. - Yinghai
-v11: change far jmp back to far return to initial_code, that is needed
to fix failure that is reported by Konrad on AMD systems. - Yinghai
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1359058816-7615-12-git-send-email-yinghai@kernel.org
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-24 20:19:52 +00:00
|
|
|
|
2008-06-25 04:19:18 +00:00
|
|
|
x86_64_start_reservations(real_mode_data);
|
|
|
|
}
|
|
|
|
|
2023-04-12 23:49:33 +00:00
|
|
|
void __init __noreturn x86_64_start_reservations(char *real_mode_data)
|
2008-06-25 04:19:18 +00:00
|
|
|
{
|
2013-01-24 20:19:49 +00:00
|
|
|
/* version is always not zero if it is copied */
|
|
|
|
if (!boot_params.hdr.version)
|
|
|
|
copy_bootdata(__va(real_mode_data));
|
2008-01-30 12:30:46 +00:00
|
|
|
|
2016-04-14 00:04:34 +00:00
|
|
|
x86_early_init_platform_quirks();
|
2008-01-30 12:33:17 +00:00
|
|
|
|
2016-01-15 20:11:07 +00:00
|
|
|
switch (boot_params.hdr.hardware_subarch) {
|
|
|
|
case X86_SUBARCH_INTEL_MID:
|
|
|
|
x86_intel_mid_early_setup();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
start_kernel();
|
|
|
|
}
|
2020-09-07 13:15:30 +00:00
|
|
|
|
2020-09-07 13:15:34 +00:00
|
|
|
/*
|
|
|
|
* Data structures and code used for IDT setup in head_64.S. The bringup-IDT is
|
|
|
|
* used until the idt_table takes over. On the boot CPU this happens in
|
|
|
|
* x86_64_start_kernel(), on secondary CPUs in start_secondary(). In both cases
|
|
|
|
* this happens in the functions called from head_64.S.
|
|
|
|
*
|
|
|
|
* The idt_table can't be used that early because all the code modifying it is
|
|
|
|
* in idt.c and can be instrumented by tracing or KASAN, which both don't work
|
|
|
|
* during early CPU bringup. Also the idt_table has the runtime vectors
|
|
|
|
* configured which require certain CPU state to be setup already (like TSS),
|
|
|
|
* which also hasn't happened yet in early CPU bringup.
|
|
|
|
*/
|
|
|
|
static gate_desc bringup_idt_table[NUM_EXCEPTION_VECTORS] __page_aligned_data;
|
|
|
|
|
|
|
|
static struct desc_ptr bringup_idt_descr = {
|
|
|
|
.size = (NUM_EXCEPTION_VECTORS * sizeof(gate_desc)) - 1,
|
|
|
|
.address = 0, /* Set at runtime */
|
|
|
|
};
|
|
|
|
|
2020-09-08 12:35:17 +00:00
|
|
|
static void set_bringup_idt_handler(gate_desc *idt, int n, void *handler)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_AMD_MEM_ENCRYPT
|
|
|
|
struct idt_data data;
|
|
|
|
gate_desc desc;
|
|
|
|
|
|
|
|
init_idt_data(&data, n, handler);
|
|
|
|
idt_init_desc(&desc, &data);
|
|
|
|
native_write_idt_entry(idt, n, &desc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-07 13:15:34 +00:00
|
|
|
/* This runs while still in the direct mapping */
|
2023-07-12 03:30:06 +00:00
|
|
|
static void __head startup_64_load_idt(unsigned long physbase)
|
2020-09-07 13:15:34 +00:00
|
|
|
{
|
|
|
|
struct desc_ptr *desc = fixup_pointer(&bringup_idt_descr, physbase);
|
2020-09-08 12:35:17 +00:00
|
|
|
gate_desc *idt = fixup_pointer(bringup_idt_table, physbase);
|
|
|
|
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
|
|
|
|
void *handler;
|
|
|
|
|
|
|
|
/* VMM Communication Exception */
|
|
|
|
handler = fixup_pointer(vc_no_ghcb, physbase);
|
|
|
|
set_bringup_idt_handler(idt, X86_TRAP_VC, handler);
|
|
|
|
}
|
2020-09-07 13:15:34 +00:00
|
|
|
|
2020-09-08 12:35:17 +00:00
|
|
|
desc->address = (unsigned long)idt;
|
2020-09-07 13:15:34 +00:00
|
|
|
native_load_idt(desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is used when running on kernel addresses */
|
|
|
|
void early_setup_idt(void)
|
|
|
|
{
|
2020-09-08 12:38:16 +00:00
|
|
|
/* VMM Communication Exception */
|
2022-02-09 18:10:11 +00:00
|
|
|
if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
|
|
|
|
setup_ghcb();
|
2020-09-08 12:38:16 +00:00
|
|
|
set_bringup_idt_handler(bringup_idt_table, X86_TRAP_VC, vc_boot_ghcb);
|
2022-02-09 18:10:11 +00:00
|
|
|
}
|
2020-09-08 12:38:16 +00:00
|
|
|
|
2020-09-07 13:15:34 +00:00
|
|
|
bringup_idt_descr.address = (unsigned long)bringup_idt_table;
|
|
|
|
native_load_idt(&bringup_idt_descr);
|
|
|
|
}
|
|
|
|
|
2020-09-07 13:15:30 +00:00
|
|
|
/*
|
|
|
|
* Setup boot CPU state needed before kernel switches to virtual addresses.
|
|
|
|
*/
|
|
|
|
void __head startup_64_setup_env(unsigned long physbase)
|
|
|
|
{
|
|
|
|
/* Load GDT */
|
|
|
|
startup_gdt_descr.address = (unsigned long)fixup_pointer(startup_gdt, physbase);
|
|
|
|
native_load_gdt(&startup_gdt_descr);
|
|
|
|
|
|
|
|
/* New GDT is live - reload data segment registers */
|
|
|
|
asm volatile("movl %%eax, %%ds\n"
|
|
|
|
"movl %%eax, %%ss\n"
|
|
|
|
"movl %%eax, %%es\n" : : "a"(__KERNEL_DS) : "memory");
|
2020-09-07 13:15:34 +00:00
|
|
|
|
|
|
|
startup_64_load_idt(physbase);
|
2020-09-07 13:15:30 +00:00
|
|
|
}
|