arm64-stub.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013, 2014 Linaro Ltd; <[email protected]>
  4. *
  5. * This file implements the EFI boot stub for the arm64 kernel.
  6. * Adapted from ARM version by Mark Salter <[email protected]>
  7. */
  8. #include <linux/efi.h>
  9. #include <asm/efi.h>
  10. #include <asm/memory.h>
  11. #include <asm/sections.h>
  12. #include <asm/sysreg.h>
  13. #include "efistub.h"
  14. static bool system_needs_vamap(void)
  15. {
  16. const struct efi_smbios_type4_record *record;
  17. const u32 __aligned(1) *socid;
  18. const u8 *version;
  19. /*
  20. * Ampere eMAG, Altra, and Altra Max machines crash in SetTime() if
  21. * SetVirtualAddressMap() has not been called prior. Most Altra systems
  22. * can be identified by the SMCCC soc ID, which is conveniently exposed
  23. * via the type 4 SMBIOS records. Otherwise, test the processor version
  24. * field. eMAG systems all appear to have the processor version field
  25. * set to "eMAG".
  26. */
  27. record = (struct efi_smbios_type4_record *)efi_get_smbios_record(4);
  28. if (!record)
  29. return false;
  30. socid = (u32 *)record->processor_id;
  31. switch (*socid & 0xffff000f) {
  32. static char const altra[] = "Ampere(TM) Altra(TM) Processor";
  33. static char const emag[] = "eMAG";
  34. default:
  35. version = efi_get_smbios_string(&record->header, 4,
  36. processor_version);
  37. if (!version || (strncmp(version, altra, sizeof(altra) - 1) &&
  38. strncmp(version, emag, sizeof(emag) - 1)))
  39. break;
  40. fallthrough;
  41. case 0x0a160001: // Altra
  42. case 0x0a160002: // Altra Max
  43. efi_warn("Working around broken SetVirtualAddressMap()\n");
  44. return true;
  45. }
  46. return false;
  47. }
  48. efi_status_t check_platform_features(void)
  49. {
  50. u64 tg;
  51. /*
  52. * If we have 48 bits of VA space for TTBR0 mappings, we can map the
  53. * UEFI runtime regions 1:1 and so calling SetVirtualAddressMap() is
  54. * unnecessary.
  55. */
  56. if (VA_BITS_MIN >= 48 && !system_needs_vamap())
  57. efi_novamap = true;
  58. /* UEFI mandates support for 4 KB granularity, no need to check */
  59. if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
  60. return EFI_SUCCESS;
  61. tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_EL1_TGRAN_SHIFT) & 0xf;
  62. if (tg < ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MIN || tg > ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MAX) {
  63. if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
  64. efi_err("This 64 KB granular kernel is not supported by your CPU\n");
  65. else
  66. efi_err("This 16 KB granular kernel is not supported by your CPU\n");
  67. return EFI_UNSUPPORTED;
  68. }
  69. return EFI_SUCCESS;
  70. }
  71. /*
  72. * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
  73. * to provide space, and fail to zero it). Check for this condition by double
  74. * checking that the first and the last byte of the image are covered by the
  75. * same EFI memory map entry.
  76. */
  77. static bool check_image_region(u64 base, u64 size)
  78. {
  79. struct efi_boot_memmap *map;
  80. efi_status_t status;
  81. bool ret = false;
  82. int map_offset;
  83. status = efi_get_memory_map(&map, false);
  84. if (status != EFI_SUCCESS)
  85. return false;
  86. for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
  87. efi_memory_desc_t *md = (void *)map->map + map_offset;
  88. u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
  89. /*
  90. * Find the region that covers base, and return whether
  91. * it covers base+size bytes.
  92. */
  93. if (base >= md->phys_addr && base < end) {
  94. ret = (base + size) <= end;
  95. break;
  96. }
  97. }
  98. efi_bs_call(free_pool, map);
  99. return ret;
  100. }
  101. efi_status_t handle_kernel_image(unsigned long *image_addr,
  102. unsigned long *image_size,
  103. unsigned long *reserve_addr,
  104. unsigned long *reserve_size,
  105. efi_loaded_image_t *image,
  106. efi_handle_t image_handle)
  107. {
  108. efi_status_t status;
  109. unsigned long kernel_size, kernel_memsize = 0;
  110. u32 phys_seed = 0;
  111. /*
  112. * Although relocatable kernels can fix up the misalignment with
  113. * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are
  114. * subtly out of sync with those recorded in the vmlinux when kaslr is
  115. * disabled but the image required relocation anyway. Therefore retain
  116. * 2M alignment if KASLR was explicitly disabled, even if it was not
  117. * going to be activated to begin with.
  118. */
  119. u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
  120. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
  121. efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID;
  122. void *p;
  123. if (efi_nokaslr) {
  124. efi_info("KASLR disabled on kernel command line\n");
  125. } else if (efi_bs_call(handle_protocol, image_handle,
  126. &li_fixed_proto, &p) == EFI_SUCCESS) {
  127. efi_info("Image placement fixed by loader\n");
  128. } else {
  129. status = efi_get_random_bytes(sizeof(phys_seed),
  130. (u8 *)&phys_seed);
  131. if (status == EFI_NOT_FOUND) {
  132. efi_info("EFI_RNG_PROTOCOL unavailable\n");
  133. efi_nokaslr = true;
  134. } else if (status != EFI_SUCCESS) {
  135. efi_err("efi_get_random_bytes() failed (0x%lx)\n",
  136. status);
  137. efi_nokaslr = true;
  138. }
  139. }
  140. }
  141. if (image->image_base != _text)
  142. efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
  143. if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
  144. efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
  145. SEGMENT_ALIGN >> 10);
  146. kernel_size = _edata - _text;
  147. kernel_memsize = kernel_size + (_end - _edata);
  148. *reserve_size = kernel_memsize;
  149. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
  150. /*
  151. * If KASLR is enabled, and we have some randomness available,
  152. * locate the kernel at a randomized offset in physical memory.
  153. */
  154. status = efi_random_alloc(*reserve_size, min_kimg_align,
  155. reserve_addr, phys_seed);
  156. if (status != EFI_SUCCESS)
  157. efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
  158. } else {
  159. status = EFI_OUT_OF_RESOURCES;
  160. }
  161. if (status != EFI_SUCCESS) {
  162. if (!check_image_region((u64)_text, kernel_memsize)) {
  163. efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
  164. } else if (IS_ALIGNED((u64)_text, min_kimg_align)) {
  165. /*
  166. * Just execute from wherever we were loaded by the
  167. * UEFI PE/COFF loader if the alignment is suitable.
  168. */
  169. *image_addr = (u64)_text;
  170. *reserve_size = 0;
  171. return EFI_SUCCESS;
  172. }
  173. status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
  174. ULONG_MAX, min_kimg_align);
  175. if (status != EFI_SUCCESS) {
  176. efi_err("Failed to relocate kernel\n");
  177. *reserve_size = 0;
  178. return status;
  179. }
  180. }
  181. *image_addr = *reserve_addr;
  182. memcpy((void *)*image_addr, _text, kernel_size);
  183. return EFI_SUCCESS;
  184. }