init.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/io.h>
  3. #include <linux/slab.h>
  4. #include <linux/memblock.h>
  5. #include <linux/mem_encrypt.h>
  6. #include <asm/set_memory.h>
  7. #include <asm/pgtable.h>
  8. #include <asm/realmode.h>
  9. #include <asm/tlbflush.h>
  10. struct real_mode_header *real_mode_header;
  11. u32 *trampoline_cr4_features;
  12. /* Hold the pgd entry used on booting additional CPUs */
  13. pgd_t trampoline_pgd_entry;
  14. void __init set_real_mode_mem(phys_addr_t mem, size_t size)
  15. {
  16. void *base = __va(mem);
  17. real_mode_header = (struct real_mode_header *) base;
  18. }
  19. void __init reserve_real_mode(void)
  20. {
  21. phys_addr_t mem;
  22. size_t size = real_mode_size_needed();
  23. if (!size)
  24. return;
  25. WARN_ON(slab_is_available());
  26. /* Has to be under 1M so we can execute real-mode AP code. */
  27. mem = memblock_find_in_range(0, 1<<20, size, PAGE_SIZE);
  28. if (!mem) {
  29. pr_info("No sub-1M memory is available for the trampoline\n");
  30. return;
  31. }
  32. memblock_reserve(mem, size);
  33. set_real_mode_mem(mem, size);
  34. }
  35. static void __init setup_real_mode(void)
  36. {
  37. u16 real_mode_seg;
  38. const u32 *rel;
  39. u32 count;
  40. unsigned char *base;
  41. unsigned long phys_base;
  42. struct trampoline_header *trampoline_header;
  43. size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
  44. #ifdef CONFIG_X86_64
  45. u64 *trampoline_pgd;
  46. u64 efer;
  47. #endif
  48. base = (unsigned char *)real_mode_header;
  49. /*
  50. * If SME is active, the trampoline area will need to be in
  51. * decrypted memory in order to bring up other processors
  52. * successfully. This is not needed for SEV.
  53. */
  54. if (sme_active())
  55. set_memory_decrypted((unsigned long)base, size >> PAGE_SHIFT);
  56. memcpy(base, real_mode_blob, size);
  57. phys_base = __pa(base);
  58. real_mode_seg = phys_base >> 4;
  59. rel = (u32 *) real_mode_relocs;
  60. /* 16-bit segment relocations. */
  61. count = *rel++;
  62. while (count--) {
  63. u16 *seg = (u16 *) (base + *rel++);
  64. *seg = real_mode_seg;
  65. }
  66. /* 32-bit linear relocations. */
  67. count = *rel++;
  68. while (count--) {
  69. u32 *ptr = (u32 *) (base + *rel++);
  70. *ptr += phys_base;
  71. }
  72. /* Must be perfomed *after* relocation. */
  73. trampoline_header = (struct trampoline_header *)
  74. __va(real_mode_header->trampoline_header);
  75. #ifdef CONFIG_X86_32
  76. trampoline_header->start = __pa_symbol(startup_32_smp);
  77. trampoline_header->gdt_limit = __BOOT_DS + 7;
  78. trampoline_header->gdt_base = __pa_symbol(boot_gdt);
  79. #else
  80. /*
  81. * Some AMD processors will #GP(0) if EFER.LMA is set in WRMSR
  82. * so we need to mask it out.
  83. */
  84. rdmsrl(MSR_EFER, efer);
  85. trampoline_header->efer = efer & ~EFER_LMA;
  86. trampoline_header->start = (u64) secondary_startup_64;
  87. trampoline_cr4_features = &trampoline_header->cr4;
  88. *trampoline_cr4_features = mmu_cr4_features;
  89. trampoline_header->flags = 0;
  90. if (sme_active())
  91. trampoline_header->flags |= TH_FLAGS_SME_ACTIVE;
  92. trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);
  93. trampoline_pgd[0] = trampoline_pgd_entry.pgd;
  94. trampoline_pgd[511] = init_top_pgt[511].pgd;
  95. #endif
  96. }
  97. /*
  98. * reserve_real_mode() gets called very early, to guarantee the
  99. * availability of low memory. This is before the proper kernel page
  100. * tables are set up, so we cannot set page permissions in that
  101. * function. Also trampoline code will be executed by APs so we
  102. * need to mark it executable at do_pre_smp_initcalls() at least,
  103. * thus run it as a early_initcall().
  104. */
  105. static void __init set_real_mode_permissions(void)
  106. {
  107. unsigned char *base = (unsigned char *) real_mode_header;
  108. size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
  109. size_t ro_size =
  110. PAGE_ALIGN(real_mode_header->ro_end) -
  111. __pa(base);
  112. size_t text_size =
  113. PAGE_ALIGN(real_mode_header->ro_end) -
  114. real_mode_header->text_start;
  115. unsigned long text_start =
  116. (unsigned long) __va(real_mode_header->text_start);
  117. set_memory_nx((unsigned long) base, size >> PAGE_SHIFT);
  118. set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT);
  119. set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT);
  120. }
  121. static int __init init_real_mode(void)
  122. {
  123. if (!real_mode_header)
  124. panic("Real mode trampoline was not allocated");
  125. setup_real_mode();
  126. set_real_mode_permissions();
  127. return 0;
  128. }
  129. early_initcall(init_real_mode);