kaslr_64.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This code is used on x86_64 to create page table identity mappings on
  4. * demand by building up a new set of page tables (or appending to the
  5. * existing ones), and then switching over to them when ready.
  6. *
  7. * Copyright (C) 2015-2016 Yinghai Lu
  8. * Copyright (C) 2016 Kees Cook
  9. */
  10. /*
  11. * Since we're dealing with identity mappings, physical and virtual
  12. * addresses are the same, so override these defines which are ultimately
  13. * used by the headers in misc.h.
  14. */
  15. #define __pa(x) ((unsigned long)(x))
  16. #define __va(x) ((void *)((unsigned long)(x)))
  17. /* No PAGE_TABLE_ISOLATION support needed either: */
  18. #undef CONFIG_PAGE_TABLE_ISOLATION
  19. #include "misc.h"
  20. /* These actually do the work of building the kernel identity maps. */
  21. #include <asm/init.h>
  22. #include <asm/pgtable.h>
  23. /* Use the static base for this part of the boot process */
  24. #undef __PAGE_OFFSET
  25. #define __PAGE_OFFSET __PAGE_OFFSET_BASE
  26. #include "../../mm/ident_map.c"
  27. /* Used by pgtable.h asm code to force instruction serialization. */
  28. unsigned long __force_order;
  29. /* Used to track our page table allocation area. */
  30. struct alloc_pgt_data {
  31. unsigned char *pgt_buf;
  32. unsigned long pgt_buf_size;
  33. unsigned long pgt_buf_offset;
  34. };
  35. /*
  36. * Allocates space for a page table entry, using struct alloc_pgt_data
  37. * above. Besides the local callers, this is used as the allocation
  38. * callback in mapping_info below.
  39. */
  40. static void *alloc_pgt_page(void *context)
  41. {
  42. struct alloc_pgt_data *pages = (struct alloc_pgt_data *)context;
  43. unsigned char *entry;
  44. /* Validate there is space available for a new page. */
  45. if (pages->pgt_buf_offset >= pages->pgt_buf_size) {
  46. debug_putstr("out of pgt_buf in " __FILE__ "!?\n");
  47. debug_putaddr(pages->pgt_buf_offset);
  48. debug_putaddr(pages->pgt_buf_size);
  49. return NULL;
  50. }
  51. entry = pages->pgt_buf + pages->pgt_buf_offset;
  52. pages->pgt_buf_offset += PAGE_SIZE;
  53. return entry;
  54. }
  55. /* Used to track our allocated page tables. */
  56. static struct alloc_pgt_data pgt_data;
  57. /* The top level page table entry pointer. */
  58. static unsigned long top_level_pgt;
  59. phys_addr_t physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
  60. /*
  61. * Mapping information structure passed to kernel_ident_mapping_init().
  62. * Due to relocation, pointers must be assigned at run time not build time.
  63. */
  64. static struct x86_mapping_info mapping_info;
  65. /* Locates and clears a region for a new top level page table. */
  66. void initialize_identity_maps(void)
  67. {
  68. /* If running as an SEV guest, the encryption mask is required. */
  69. set_sev_encryption_mask();
  70. /* Exclude the encryption mask from __PHYSICAL_MASK */
  71. physical_mask &= ~sme_me_mask;
  72. /* Init mapping_info with run-time function/buffer pointers. */
  73. mapping_info.alloc_pgt_page = alloc_pgt_page;
  74. mapping_info.context = &pgt_data;
  75. mapping_info.page_flag = __PAGE_KERNEL_LARGE_EXEC | sme_me_mask;
  76. mapping_info.kernpg_flag = _KERNPG_TABLE;
  77. /*
  78. * It should be impossible for this not to already be true,
  79. * but since calling this a second time would rewind the other
  80. * counters, let's just make sure this is reset too.
  81. */
  82. pgt_data.pgt_buf_offset = 0;
  83. /*
  84. * If we came here via startup_32(), cr3 will be _pgtable already
  85. * and we must append to the existing area instead of entirely
  86. * overwriting it.
  87. *
  88. * With 5-level paging, we use '_pgtable' to allocate the p4d page table,
  89. * the top-level page table is allocated separately.
  90. *
  91. * p4d_offset(top_level_pgt, 0) would cover both the 4- and 5-level
  92. * cases. On 4-level paging it's equal to 'top_level_pgt'.
  93. */
  94. top_level_pgt = read_cr3_pa();
  95. if (p4d_offset((pgd_t *)top_level_pgt, 0) == (p4d_t *)_pgtable) {
  96. debug_putstr("booted via startup_32()\n");
  97. pgt_data.pgt_buf = _pgtable + BOOT_INIT_PGT_SIZE;
  98. pgt_data.pgt_buf_size = BOOT_PGT_SIZE - BOOT_INIT_PGT_SIZE;
  99. memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size);
  100. } else {
  101. debug_putstr("booted via startup_64()\n");
  102. pgt_data.pgt_buf = _pgtable;
  103. pgt_data.pgt_buf_size = BOOT_PGT_SIZE;
  104. memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size);
  105. top_level_pgt = (unsigned long)alloc_pgt_page(&pgt_data);
  106. }
  107. }
  108. /*
  109. * Adds the specified range to what will become the new identity mappings.
  110. * Once all ranges have been added, the new mapping is activated by calling
  111. * finalize_identity_maps() below.
  112. */
  113. void add_identity_map(unsigned long start, unsigned long size)
  114. {
  115. unsigned long end = start + size;
  116. /* Align boundary to 2M. */
  117. start = round_down(start, PMD_SIZE);
  118. end = round_up(end, PMD_SIZE);
  119. if (start >= end)
  120. return;
  121. /* Build the mapping. */
  122. kernel_ident_mapping_init(&mapping_info, (pgd_t *)top_level_pgt,
  123. start, end);
  124. }
  125. /*
  126. * This switches the page tables to the new level4 that has been built
  127. * via calls to add_identity_map() above. If booted via startup_32(),
  128. * this is effectively a no-op.
  129. */
  130. void finalize_identity_maps(void)
  131. {
  132. write_cr3(top_level_pgt);
  133. }