init.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <linux/io.h>
  2. #include <linux/slab.h>
  3. #include <linux/memblock.h>
  4. #include <asm/cacheflush.h>
  5. #include <asm/pgtable.h>
  6. #include <asm/realmode.h>
  7. #include <asm/tlbflush.h>
  8. struct real_mode_header *real_mode_header;
  9. u32 *trampoline_cr4_features;
  10. /* Hold the pgd entry used on booting additional CPUs */
  11. pgd_t trampoline_pgd_entry;
  12. void __init set_real_mode_mem(phys_addr_t mem, size_t size)
  13. {
  14. void *base = __va(mem);
  15. real_mode_header = (struct real_mode_header *) base;
  16. printk(KERN_DEBUG "Base memory trampoline at [%p] %llx size %zu\n",
  17. base, (unsigned long long)mem, size);
  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. memcpy(base, real_mode_blob, size);
  50. phys_base = __pa(base);
  51. real_mode_seg = phys_base >> 4;
  52. rel = (u32 *) real_mode_relocs;
  53. /* 16-bit segment relocations. */
  54. count = *rel++;
  55. while (count--) {
  56. u16 *seg = (u16 *) (base + *rel++);
  57. *seg = real_mode_seg;
  58. }
  59. /* 32-bit linear relocations. */
  60. count = *rel++;
  61. while (count--) {
  62. u32 *ptr = (u32 *) (base + *rel++);
  63. *ptr += phys_base;
  64. }
  65. /* Must be perfomed *after* relocation. */
  66. trampoline_header = (struct trampoline_header *)
  67. __va(real_mode_header->trampoline_header);
  68. #ifdef CONFIG_X86_32
  69. trampoline_header->start = __pa_symbol(startup_32_smp);
  70. trampoline_header->gdt_limit = __BOOT_DS + 7;
  71. trampoline_header->gdt_base = __pa_symbol(boot_gdt);
  72. #else
  73. /*
  74. * Some AMD processors will #GP(0) if EFER.LMA is set in WRMSR
  75. * so we need to mask it out.
  76. */
  77. rdmsrl(MSR_EFER, efer);
  78. trampoline_header->efer = efer & ~EFER_LMA;
  79. trampoline_header->start = (u64) secondary_startup_64;
  80. trampoline_cr4_features = &trampoline_header->cr4;
  81. *trampoline_cr4_features = mmu_cr4_features;
  82. trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);
  83. trampoline_pgd[0] = trampoline_pgd_entry.pgd;
  84. trampoline_pgd[511] = init_level4_pgt[511].pgd;
  85. #endif
  86. }
  87. /*
  88. * reserve_real_mode() gets called very early, to guarantee the
  89. * availability of low memory. This is before the proper kernel page
  90. * tables are set up, so we cannot set page permissions in that
  91. * function. Also trampoline code will be executed by APs so we
  92. * need to mark it executable at do_pre_smp_initcalls() at least,
  93. * thus run it as a early_initcall().
  94. */
  95. static void __init set_real_mode_permissions(void)
  96. {
  97. unsigned char *base = (unsigned char *) real_mode_header;
  98. size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
  99. size_t ro_size =
  100. PAGE_ALIGN(real_mode_header->ro_end) -
  101. __pa(base);
  102. size_t text_size =
  103. PAGE_ALIGN(real_mode_header->ro_end) -
  104. real_mode_header->text_start;
  105. unsigned long text_start =
  106. (unsigned long) __va(real_mode_header->text_start);
  107. set_memory_nx((unsigned long) base, size >> PAGE_SHIFT);
  108. set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT);
  109. set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT);
  110. }
  111. static int __init init_real_mode(void)
  112. {
  113. if (!real_mode_header)
  114. panic("Real mode trampoline was not allocated");
  115. setup_real_mode();
  116. set_real_mode_permissions();
  117. return 0;
  118. }
  119. early_initcall(init_real_mode);