pagetable.c 4.0 KB

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