pgtable_64.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "misc.h"
  3. #include <asm/bootparam.h>
  4. #include <asm/bootparam_utils.h>
  5. #include <asm/e820/types.h>
  6. #include <asm/processor.h>
  7. #include "pgtable.h"
  8. #include "../string.h"
  9. #include "efi.h"
  10. #define BIOS_START_MIN 0x20000U /* 128K, less than this is insane */
  11. #define BIOS_START_MAX 0x9f000U /* 640K, absolute maximum */
  12. #ifdef CONFIG_X86_5LEVEL
  13. /* __pgtable_l5_enabled needs to be in .data to avoid being cleared along with .bss */
  14. unsigned int __section(".data") __pgtable_l5_enabled;
  15. unsigned int __section(".data") pgdir_shift = 39;
  16. unsigned int __section(".data") ptrs_per_p4d = 1;
  17. #endif
  18. /* Buffer to preserve trampoline memory */
  19. static char trampoline_save[TRAMPOLINE_32BIT_SIZE];
  20. /*
  21. * Trampoline address will be printed by extract_kernel() for debugging
  22. * purposes.
  23. *
  24. * Avoid putting the pointer into .bss as it will be cleared between
  25. * configure_5level_paging() and extract_kernel().
  26. */
  27. unsigned long *trampoline_32bit __section(".data");
  28. int cmdline_find_option_bool(const char *option);
  29. static unsigned long find_trampoline_placement(void)
  30. {
  31. unsigned long bios_start = 0, ebda_start = 0;
  32. struct boot_e820_entry *entry;
  33. char *signature;
  34. int i;
  35. /*
  36. * Find a suitable spot for the trampoline.
  37. * This code is based on reserve_bios_regions().
  38. */
  39. /*
  40. * EFI systems may not provide legacy ROM. The memory may not be mapped
  41. * at all.
  42. *
  43. * Only look for values in the legacy ROM for non-EFI system.
  44. */
  45. signature = (char *)&boot_params_ptr->efi_info.efi_loader_signature;
  46. if (strncmp(signature, EFI32_LOADER_SIGNATURE, 4) &&
  47. strncmp(signature, EFI64_LOADER_SIGNATURE, 4)) {
  48. ebda_start = *(unsigned short *)0x40e << 4;
  49. bios_start = *(unsigned short *)0x413 << 10;
  50. }
  51. if (bios_start < BIOS_START_MIN || bios_start > BIOS_START_MAX)
  52. bios_start = BIOS_START_MAX;
  53. if (ebda_start > BIOS_START_MIN && ebda_start < bios_start)
  54. bios_start = ebda_start;
  55. bios_start = round_down(bios_start, PAGE_SIZE);
  56. /* Find the first usable memory region under bios_start. */
  57. for (i = boot_params_ptr->e820_entries - 1; i >= 0; i--) {
  58. unsigned long new = bios_start;
  59. entry = &boot_params_ptr->e820_table[i];
  60. /* Skip all entries above bios_start. */
  61. if (bios_start <= entry->addr)
  62. continue;
  63. /* Skip non-RAM entries. */
  64. if (entry->type != E820_TYPE_RAM)
  65. continue;
  66. /* Adjust bios_start to the end of the entry if needed. */
  67. if (bios_start > entry->addr + entry->size)
  68. new = entry->addr + entry->size;
  69. /* Keep bios_start page-aligned. */
  70. new = round_down(new, PAGE_SIZE);
  71. /* Skip the entry if it's too small. */
  72. if (new - TRAMPOLINE_32BIT_SIZE < entry->addr)
  73. continue;
  74. /* Protect against underflow. */
  75. if (new - TRAMPOLINE_32BIT_SIZE > bios_start)
  76. break;
  77. bios_start = new;
  78. break;
  79. }
  80. /* Place the trampoline just below the end of low memory */
  81. return bios_start - TRAMPOLINE_32BIT_SIZE;
  82. }
  83. asmlinkage void configure_5level_paging(struct boot_params *bp, void *pgtable)
  84. {
  85. void (*toggle_la57)(void *cr3);
  86. bool l5_required = false;
  87. /* Initialize boot_params. Required for cmdline_find_option_bool(). */
  88. sanitize_boot_params(bp);
  89. boot_params_ptr = bp;
  90. /*
  91. * Check if LA57 is desired and supported.
  92. *
  93. * There are several parts to the check:
  94. * - if the kernel supports 5-level paging: CONFIG_X86_5LEVEL=y
  95. * - if user asked to disable 5-level paging: no5lvl in cmdline
  96. * - if the machine supports 5-level paging:
  97. * + CPUID leaf 7 is supported
  98. * + the leaf has the feature bit set
  99. *
  100. * That's substitute for boot_cpu_has() in early boot code.
  101. */
  102. if (IS_ENABLED(CONFIG_X86_5LEVEL) &&
  103. !cmdline_find_option_bool("no5lvl") &&
  104. native_cpuid_eax(0) >= 7 &&
  105. (native_cpuid_ecx(7) & (1 << (X86_FEATURE_LA57 & 31)))) {
  106. l5_required = true;
  107. /* Initialize variables for 5-level paging */
  108. __pgtable_l5_enabled = 1;
  109. pgdir_shift = 48;
  110. ptrs_per_p4d = 512;
  111. }
  112. /*
  113. * The trampoline will not be used if the paging mode is already set to
  114. * the desired one.
  115. */
  116. if (l5_required == !!(native_read_cr4() & X86_CR4_LA57))
  117. return;
  118. trampoline_32bit = (unsigned long *)find_trampoline_placement();
  119. /* Preserve trampoline memory */
  120. memcpy(trampoline_save, trampoline_32bit, TRAMPOLINE_32BIT_SIZE);
  121. /* Clear trampoline memory first */
  122. memset(trampoline_32bit, 0, TRAMPOLINE_32BIT_SIZE);
  123. /* Copy trampoline code in place */
  124. toggle_la57 = memcpy(trampoline_32bit +
  125. TRAMPOLINE_32BIT_CODE_OFFSET / sizeof(unsigned long),
  126. &trampoline_32bit_src, TRAMPOLINE_32BIT_CODE_SIZE);
  127. /*
  128. * Avoid the need for a stack in the 32-bit trampoline code, by using
  129. * LJMP rather than LRET to return back to long mode. LJMP takes an
  130. * immediate absolute address, which needs to be adjusted based on the
  131. * placement of the trampoline.
  132. */
  133. *(u32 *)((u8 *)toggle_la57 + trampoline_ljmp_imm_offset) +=
  134. (unsigned long)toggle_la57;
  135. /*
  136. * The code below prepares page table in trampoline memory.
  137. *
  138. * The new page table will be used by trampoline code for switching
  139. * from 4- to 5-level paging or vice versa.
  140. */
  141. if (l5_required) {
  142. /*
  143. * For 4- to 5-level paging transition, set up current CR3 as
  144. * the first and the only entry in a new top-level page table.
  145. */
  146. *trampoline_32bit = __native_read_cr3() | _PAGE_TABLE_NOENC;
  147. } else {
  148. unsigned long src;
  149. /*
  150. * For 5- to 4-level paging transition, copy page table pointed
  151. * by first entry in the current top-level page table as our
  152. * new top-level page table.
  153. *
  154. * We cannot just point to the page table from trampoline as it
  155. * may be above 4G.
  156. */
  157. src = *(unsigned long *)__native_read_cr3() & PAGE_MASK;
  158. memcpy(trampoline_32bit, (void *)src, PAGE_SIZE);
  159. }
  160. toggle_la57(trampoline_32bit);
  161. /*
  162. * Move the top level page table out of trampoline memory.
  163. */
  164. memcpy(pgtable, trampoline_32bit, PAGE_SIZE);
  165. native_write_cr3((unsigned long)pgtable);
  166. /* Restore trampoline memory */
  167. memcpy(trampoline_32bit, trampoline_save, TRAMPOLINE_32BIT_SIZE);
  168. }