head32.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/i386/kernel/head32.c -- prepare to run common code
  4. *
  5. * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
  6. * Copyright (C) 2007 Eric Biederman <ebiederm@xmission.com>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/start_kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/memblock.h>
  12. #include <asm/desc.h>
  13. #include <asm/setup.h>
  14. #include <asm/sections.h>
  15. #include <asm/e820/api.h>
  16. #include <asm/page.h>
  17. #include <asm/apic.h>
  18. #include <asm/io_apic.h>
  19. #include <asm/bios_ebda.h>
  20. #include <asm/microcode.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/bootparam_utils.h>
  23. static void __init i386_default_early_setup(void)
  24. {
  25. /* Initialize 32bit specific setup functions */
  26. x86_init.resources.reserve_resources = i386_reserve_resources;
  27. x86_init.mpparse.setup_ioapic_ids = setup_ioapic_ids_from_mpc;
  28. }
  29. #ifdef CONFIG_MICROCODE_INITRD32
  30. unsigned long __initdata initrd_start_early;
  31. static pte_t __initdata *initrd_pl2p_start, *initrd_pl2p_end;
  32. static void zap_early_initrd_mapping(void)
  33. {
  34. pte_t *pl2p = initrd_pl2p_start;
  35. for (; pl2p < initrd_pl2p_end; pl2p++) {
  36. *pl2p = (pte_t){ .pte = 0 };
  37. if (!IS_ENABLED(CONFIG_X86_PAE))
  38. *(pl2p + ((PAGE_OFFSET >> PGDIR_SHIFT))) = (pte_t) {.pte = 0};
  39. }
  40. }
  41. #else
  42. static inline void zap_early_initrd_mapping(void) { }
  43. #endif
  44. asmlinkage __visible void __init __noreturn i386_start_kernel(void)
  45. {
  46. /* Make sure IDT is set up before any exception happens */
  47. idt_setup_early_handler();
  48. load_ucode_bsp();
  49. zap_early_initrd_mapping();
  50. cr4_init_shadow();
  51. sanitize_boot_params(&boot_params);
  52. x86_early_init_platform_quirks();
  53. /* Call the subarch specific early setup function */
  54. switch (boot_params.hdr.hardware_subarch) {
  55. case X86_SUBARCH_INTEL_MID:
  56. x86_intel_mid_early_setup();
  57. break;
  58. case X86_SUBARCH_CE4100:
  59. x86_ce4100_early_setup();
  60. break;
  61. default:
  62. i386_default_early_setup();
  63. break;
  64. }
  65. start_kernel();
  66. }
  67. /*
  68. * Initialize page tables. This creates a PDE and a set of page
  69. * tables, which are located immediately beyond __brk_base. The variable
  70. * _brk_end is set up to point to the first "safe" location.
  71. * Mappings are created both at virtual address 0 (identity mapping)
  72. * and PAGE_OFFSET for up to _end.
  73. *
  74. * In PAE mode initial_page_table is statically defined to contain
  75. * enough entries to cover the VMSPLIT option (that is the top 1, 2 or 3
  76. * entries). The identity mapping is handled by pointing two PGD entries
  77. * to the first kernel PMD. Note the upper half of each PMD or PTE are
  78. * always zero at this stage.
  79. */
  80. #ifdef CONFIG_X86_PAE
  81. typedef pmd_t pl2_t;
  82. #define pl2_base initial_pg_pmd
  83. #define SET_PL2(val) { .pmd = (val), }
  84. #else
  85. typedef pgd_t pl2_t;
  86. #define pl2_base initial_page_table
  87. #define SET_PL2(val) { .pgd = (val), }
  88. #endif
  89. static __init __no_stack_protector pte_t init_map(pte_t pte, pte_t **ptep, pl2_t **pl2p,
  90. const unsigned long limit)
  91. {
  92. while ((pte.pte & PTE_PFN_MASK) < limit) {
  93. pl2_t pl2 = SET_PL2((unsigned long)*ptep | PDE_IDENT_ATTR);
  94. int i;
  95. **pl2p = pl2;
  96. if (!IS_ENABLED(CONFIG_X86_PAE)) {
  97. /* Kernel PDE entry */
  98. *(*pl2p + ((PAGE_OFFSET >> PGDIR_SHIFT))) = pl2;
  99. }
  100. for (i = 0; i < PTRS_PER_PTE; i++) {
  101. **ptep = pte;
  102. pte.pte += PAGE_SIZE;
  103. (*ptep)++;
  104. }
  105. (*pl2p)++;
  106. }
  107. return pte;
  108. }
  109. void __init __no_stack_protector mk_early_pgtbl_32(void)
  110. {
  111. /* Enough space to fit pagetables for the low memory linear map */
  112. unsigned long limit = __pa_nodebug(_end) + (PAGE_TABLE_SIZE(LOWMEM_PAGES) << PAGE_SHIFT);
  113. pte_t pte, *ptep = (pte_t *)__pa_nodebug(__brk_base);
  114. struct boot_params __maybe_unused *params;
  115. pl2_t *pl2p = (pl2_t *)__pa_nodebug(pl2_base);
  116. unsigned long *ptr;
  117. pte.pte = PTE_IDENT_ATTR;
  118. pte = init_map(pte, &ptep, &pl2p, limit);
  119. ptr = (unsigned long *)__pa_nodebug(&max_pfn_mapped);
  120. /* Can't use pte_pfn() since it's a call with CONFIG_PARAVIRT */
  121. *ptr = (pte.pte & PTE_PFN_MASK) >> PAGE_SHIFT;
  122. ptr = (unsigned long *)__pa_nodebug(&_brk_end);
  123. *ptr = (unsigned long)ptep + PAGE_OFFSET;
  124. #ifdef CONFIG_MICROCODE_INITRD32
  125. /* Running on a hypervisor? */
  126. if (native_cpuid_ecx(1) & BIT(31))
  127. return;
  128. params = (struct boot_params *)__pa_nodebug(&boot_params);
  129. if (!params->hdr.ramdisk_size || !params->hdr.ramdisk_image)
  130. return;
  131. /* Save the virtual start address */
  132. ptr = (unsigned long *)__pa_nodebug(&initrd_start_early);
  133. *ptr = (pte.pte & PTE_PFN_MASK) + PAGE_OFFSET;
  134. *ptr += ((unsigned long)params->hdr.ramdisk_image) & ~PAGE_MASK;
  135. /* Save PLP2 for cleanup */
  136. ptr = (unsigned long *)__pa_nodebug(&initrd_pl2p_start);
  137. *ptr = (unsigned long)pl2p + PAGE_OFFSET;
  138. limit = (unsigned long)params->hdr.ramdisk_image;
  139. pte.pte = PTE_IDENT_ATTR | PFN_ALIGN(limit);
  140. limit = (unsigned long)params->hdr.ramdisk_image + params->hdr.ramdisk_size;
  141. init_map(pte, &ptep, &pl2p, limit);
  142. ptr = (unsigned long *)__pa_nodebug(&initrd_pl2p_end);
  143. *ptr = (unsigned long)pl2p + PAGE_OFFSET;
  144. #endif
  145. }