xref: /linux/arch/x86/kernel/head32.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2700efc1bSEric W. Biederman /*
3700efc1bSEric W. Biederman  *  linux/arch/i386/kernel/head32.c -- prepare to run common code
4700efc1bSEric W. Biederman  *
5700efc1bSEric W. Biederman  *  Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6700efc1bSEric W. Biederman  *  Copyright (C) 2007 Eric Biederman <ebiederm@xmission.com>
7700efc1bSEric W. Biederman  */
8700efc1bSEric W. Biederman 
9700efc1bSEric W. Biederman #include <linux/init.h>
10700efc1bSEric W. Biederman #include <linux/start_kernel.h>
11c967da6aSYinghai Lu #include <linux/mm.h>
1272d7c3b3SYinghai Lu #include <linux/memblock.h>
13700efc1bSEric W. Biederman 
1487e81786SThomas Gleixner #include <asm/desc.h>
15a4c81cf6SYinghai Lu #include <asm/setup.h>
16a4c81cf6SYinghai Lu #include <asm/sections.h>
1766441bd3SIngo Molnar #include <asm/e820/api.h>
18816c25e7SThomas Gleixner #include <asm/page.h>
19de934103SThomas Gleixner #include <asm/apic.h>
20de934103SThomas Gleixner #include <asm/io_apic.h>
2147a3d5daSThomas Gleixner #include <asm/bios_ebda.h>
22*0b62f6cbSThomas Gleixner #include <asm/microcode.h>
23b40827faSBorislav Petkov #include <asm/tlbflush.h>
245dcd14ecSH. Peter Anvin #include <asm/bootparam_utils.h>
2547a3d5daSThomas Gleixner 
i386_default_early_setup(void)2647a3d5daSThomas Gleixner static void __init i386_default_early_setup(void)
2747a3d5daSThomas Gleixner {
28421f91d2SUwe Kleine-König 	/* Initialize 32bit specific setup functions */
2947a3d5daSThomas Gleixner 	x86_init.resources.reserve_resources = i386_reserve_resources;
3047a3d5daSThomas Gleixner 	x86_init.mpparse.setup_ioapic_ids = setup_ioapic_ids_from_mpc;
3147a3d5daSThomas Gleixner }
32a4c81cf6SYinghai Lu 
334c585af7SThomas Gleixner #ifdef CONFIG_MICROCODE_INITRD32
344c585af7SThomas Gleixner unsigned long __initdata initrd_start_early;
354c585af7SThomas Gleixner static pte_t __initdata *initrd_pl2p_start, *initrd_pl2p_end;
364c585af7SThomas Gleixner 
zap_early_initrd_mapping(void)374c585af7SThomas Gleixner static void zap_early_initrd_mapping(void)
384c585af7SThomas Gleixner {
394c585af7SThomas Gleixner 	pte_t *pl2p = initrd_pl2p_start;
404c585af7SThomas Gleixner 
414c585af7SThomas Gleixner 	for (; pl2p < initrd_pl2p_end; pl2p++) {
424c585af7SThomas Gleixner 		*pl2p = (pte_t){ .pte = 0 };
434c585af7SThomas Gleixner 
444c585af7SThomas Gleixner 		if (!IS_ENABLED(CONFIG_X86_PAE))
454c585af7SThomas Gleixner 			*(pl2p + ((PAGE_OFFSET >> PGDIR_SHIFT))) = (pte_t) {.pte = 0};
464c585af7SThomas Gleixner 	}
474c585af7SThomas Gleixner }
484c585af7SThomas Gleixner #else
zap_early_initrd_mapping(void)494c585af7SThomas Gleixner static inline void zap_early_initrd_mapping(void) { }
504c585af7SThomas Gleixner #endif
514c585af7SThomas Gleixner 
i386_start_kernel(void)524208d2d7SJosh Poimboeuf asmlinkage __visible void __init __noreturn i386_start_kernel(void)
53700efc1bSEric W. Biederman {
549c48c096SThomas Gleixner 	/* Make sure IDT is set up before any exception happens */
5587e81786SThomas Gleixner 	idt_setup_early_handler();
5687e81786SThomas Gleixner 
57*0b62f6cbSThomas Gleixner 	load_ucode_bsp();
584c585af7SThomas Gleixner 	zap_early_initrd_mapping();
594c585af7SThomas Gleixner 
609c48c096SThomas Gleixner 	cr4_init_shadow();
619c48c096SThomas Gleixner 
625dcd14ecSH. Peter Anvin 	sanitize_boot_params(&boot_params);
635dcd14ecSH. Peter Anvin 
648d152e7aSLuis R. Rodriguez 	x86_early_init_platform_quirks();
658d152e7aSLuis R. Rodriguez 
6647a3d5daSThomas Gleixner 	/* Call the subarch specific early setup function */
6747a3d5daSThomas Gleixner 	switch (boot_params.hdr.hardware_subarch) {
68712b6aa8SKuppuswamy Sathyanarayanan 	case X86_SUBARCH_INTEL_MID:
69712b6aa8SKuppuswamy Sathyanarayanan 		x86_intel_mid_early_setup();
703f4110a4SThomas Gleixner 		break;
71c751e17bSThomas Gleixner 	case X86_SUBARCH_CE4100:
72c751e17bSThomas Gleixner 		x86_ce4100_early_setup();
73c751e17bSThomas Gleixner 		break;
7447a3d5daSThomas Gleixner 	default:
7547a3d5daSThomas Gleixner 		i386_default_early_setup();
7647a3d5daSThomas Gleixner 		break;
7747a3d5daSThomas Gleixner 	}
78a4c81cf6SYinghai Lu 
79700efc1bSEric W. Biederman 	start_kernel();
80700efc1bSEric W. Biederman }
811e620f9bSBoris Ostrovsky 
821e620f9bSBoris Ostrovsky /*
831e620f9bSBoris Ostrovsky  * Initialize page tables.  This creates a PDE and a set of page
841e620f9bSBoris Ostrovsky  * tables, which are located immediately beyond __brk_base.  The variable
851e620f9bSBoris Ostrovsky  * _brk_end is set up to point to the first "safe" location.
861e620f9bSBoris Ostrovsky  * Mappings are created both at virtual address 0 (identity mapping)
871e620f9bSBoris Ostrovsky  * and PAGE_OFFSET for up to _end.
881e620f9bSBoris Ostrovsky  *
891e620f9bSBoris Ostrovsky  * In PAE mode initial_page_table is statically defined to contain
901e620f9bSBoris Ostrovsky  * enough entries to cover the VMSPLIT option (that is the top 1, 2 or 3
911e620f9bSBoris Ostrovsky  * entries). The identity mapping is handled by pointing two PGD entries
921e620f9bSBoris Ostrovsky  * to the first kernel PMD. Note the upper half of each PMD or PTE are
931e620f9bSBoris Ostrovsky  * always zero at this stage.
941e620f9bSBoris Ostrovsky  */
95a62f4ca1SThomas Gleixner #ifdef CONFIG_X86_PAE
96a62f4ca1SThomas Gleixner typedef pmd_t			pl2_t;
97a62f4ca1SThomas Gleixner #define pl2_base		initial_pg_pmd
98a62f4ca1SThomas Gleixner #define SET_PL2(val)		{ .pmd = (val), }
99a62f4ca1SThomas Gleixner #else
100a62f4ca1SThomas Gleixner typedef pgd_t			pl2_t;
101a62f4ca1SThomas Gleixner #define pl2_base		initial_page_table
102a62f4ca1SThomas Gleixner #define SET_PL2(val)		{ .pgd = (val), }
103a62f4ca1SThomas Gleixner #endif
104242db758SThomas Gleixner 
init_map(pte_t pte,pte_t ** ptep,pl2_t ** pl2p,const unsigned long limit)10569ba866dSThomas Gleixner static __init __no_stack_protector pte_t init_map(pte_t pte, pte_t **ptep, pl2_t **pl2p,
10669ba866dSThomas Gleixner 						  const unsigned long limit)
10769ba866dSThomas Gleixner {
10869ba866dSThomas Gleixner 	while ((pte.pte & PTE_PFN_MASK) < limit) {
10969ba866dSThomas Gleixner 		pl2_t pl2 = SET_PL2((unsigned long)*ptep | PDE_IDENT_ATTR);
11069ba866dSThomas Gleixner 		int i;
11169ba866dSThomas Gleixner 
11269ba866dSThomas Gleixner 		**pl2p = pl2;
11369ba866dSThomas Gleixner 		if (!IS_ENABLED(CONFIG_X86_PAE)) {
11469ba866dSThomas Gleixner 			/* Kernel PDE entry */
11569ba866dSThomas Gleixner 			*(*pl2p + ((PAGE_OFFSET >> PGDIR_SHIFT))) = pl2;
11669ba866dSThomas Gleixner 		}
11769ba866dSThomas Gleixner 
11869ba866dSThomas Gleixner 		for (i = 0; i < PTRS_PER_PTE; i++) {
11969ba866dSThomas Gleixner 			**ptep = pte;
12069ba866dSThomas Gleixner 			pte.pte += PAGE_SIZE;
12169ba866dSThomas Gleixner 			(*ptep)++;
12269ba866dSThomas Gleixner 		}
12369ba866dSThomas Gleixner 		(*pl2p)++;
12469ba866dSThomas Gleixner 	}
12569ba866dSThomas Gleixner 	return pte;
12669ba866dSThomas Gleixner }
12769ba866dSThomas Gleixner 
mk_early_pgtbl_32(void)128242db758SThomas Gleixner void __init __no_stack_protector mk_early_pgtbl_32(void)
1291e620f9bSBoris Ostrovsky {
1301e620f9bSBoris Ostrovsky 	/* Enough space to fit pagetables for the low memory linear map */
1314c585af7SThomas Gleixner 	unsigned long limit = __pa_nodebug(_end) + (PAGE_TABLE_SIZE(LOWMEM_PAGES) << PAGE_SHIFT);
132a62f4ca1SThomas Gleixner 	pte_t pte, *ptep = (pte_t *)__pa_nodebug(__brk_base);
1334c585af7SThomas Gleixner 	struct boot_params __maybe_unused *params;
134a62f4ca1SThomas Gleixner 	pl2_t *pl2p = (pl2_t *)__pa_nodebug(pl2_base);
135a62f4ca1SThomas Gleixner 	unsigned long *ptr;
1361e620f9bSBoris Ostrovsky 
1371e620f9bSBoris Ostrovsky 	pte.pte = PTE_IDENT_ATTR;
13869ba866dSThomas Gleixner 	pte = init_map(pte, &ptep, &pl2p, limit);
1391e620f9bSBoris Ostrovsky 
1401e2dd572SThomas Gleixner 	ptr = (unsigned long *)__pa_nodebug(&max_pfn_mapped);
1411e620f9bSBoris Ostrovsky 	/* Can't use pte_pfn() since it's a call with CONFIG_PARAVIRT */
1421e620f9bSBoris Ostrovsky 	*ptr = (pte.pte & PTE_PFN_MASK) >> PAGE_SHIFT;
1431e620f9bSBoris Ostrovsky 
1441e2dd572SThomas Gleixner 	ptr = (unsigned long *)__pa_nodebug(&_brk_end);
1451e620f9bSBoris Ostrovsky 	*ptr = (unsigned long)ptep + PAGE_OFFSET;
1464c585af7SThomas Gleixner 
1474c585af7SThomas Gleixner #ifdef CONFIG_MICROCODE_INITRD32
1484c585af7SThomas Gleixner 	/* Running on a hypervisor? */
1494c585af7SThomas Gleixner 	if (native_cpuid_ecx(1) & BIT(31))
1504c585af7SThomas Gleixner 		return;
1514c585af7SThomas Gleixner 
1524c585af7SThomas Gleixner 	params = (struct boot_params *)__pa_nodebug(&boot_params);
1534c585af7SThomas Gleixner 	if (!params->hdr.ramdisk_size || !params->hdr.ramdisk_image)
1544c585af7SThomas Gleixner 		return;
1554c585af7SThomas Gleixner 
1564c585af7SThomas Gleixner 	/* Save the virtual start address */
1574c585af7SThomas Gleixner 	ptr = (unsigned long *)__pa_nodebug(&initrd_start_early);
1584c585af7SThomas Gleixner 	*ptr = (pte.pte & PTE_PFN_MASK) + PAGE_OFFSET;
1594c585af7SThomas Gleixner 	*ptr += ((unsigned long)params->hdr.ramdisk_image) & ~PAGE_MASK;
1604c585af7SThomas Gleixner 
1614c585af7SThomas Gleixner 	/* Save PLP2 for cleanup */
1624c585af7SThomas Gleixner 	ptr = (unsigned long *)__pa_nodebug(&initrd_pl2p_start);
1634c585af7SThomas Gleixner 	*ptr = (unsigned long)pl2p + PAGE_OFFSET;
1644c585af7SThomas Gleixner 
1654c585af7SThomas Gleixner 	limit = (unsigned long)params->hdr.ramdisk_image;
1664c585af7SThomas Gleixner 	pte.pte = PTE_IDENT_ATTR | PFN_ALIGN(limit);
1674c585af7SThomas Gleixner 	limit = (unsigned long)params->hdr.ramdisk_image + params->hdr.ramdisk_size;
1684c585af7SThomas Gleixner 
1694c585af7SThomas Gleixner 	init_map(pte, &ptep, &pl2p, limit);
1704c585af7SThomas Gleixner 
1714c585af7SThomas Gleixner 	ptr = (unsigned long *)__pa_nodebug(&initrd_pl2p_end);
1724c585af7SThomas Gleixner 	*ptr = (unsigned long)pl2p + PAGE_OFFSET;
1734c585af7SThomas Gleixner #endif
1741e620f9bSBoris Ostrovsky }
175