efi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common EFI (Extensible Firmware Interface) support functions
  4. * Based on Extensible Firmware Interface Specification version 1.0
  5. *
  6. * Copyright (C) 1999 VA Linux Systems
  7. * Copyright (C) 1999 Walt Drummond <[email protected]>
  8. * Copyright (C) 1999-2002 Hewlett-Packard Co.
  9. * David Mosberger-Tang <[email protected]>
  10. * Stephane Eranian <[email protected]>
  11. * Copyright (C) 2005-2008 Intel Co.
  12. * Fenghua Yu <[email protected]>
  13. * Bibo Mao <[email protected]>
  14. * Chandramouli Narayanan <[email protected]>
  15. * Huang Ying <[email protected]>
  16. * Copyright (C) 2013 SuSE Labs
  17. * Borislav Petkov <[email protected]> - runtime services VA mapping
  18. *
  19. * Copied from efi_32.c to eliminate the duplicated code between EFI
  20. * 32/64 support code. --ying 2007-10-26
  21. *
  22. * All EFI Runtime Services are not implemented yet as EFI only
  23. * supports physical mode addressing on SoftSDV. This is to be fixed
  24. * in a future version. --drummond 1999-07-20
  25. *
  26. * Implemented EFI runtime services and virtual mode calls. --davidm
  27. *
  28. * Goutham Rao: <[email protected]>
  29. * Skip non-WB memory and ignore empty memory ranges.
  30. */
  31. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  32. #include <linux/kernel.h>
  33. #include <linux/init.h>
  34. #include <linux/efi.h>
  35. #include <linux/efi-bgrt.h>
  36. #include <linux/export.h>
  37. #include <linux/memblock.h>
  38. #include <linux/slab.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/time.h>
  42. #include <linux/io.h>
  43. #include <linux/reboot.h>
  44. #include <linux/bcd.h>
  45. #include <asm/setup.h>
  46. #include <asm/efi.h>
  47. #include <asm/e820/api.h>
  48. #include <asm/time.h>
  49. #include <asm/tlbflush.h>
  50. #include <asm/x86_init.h>
  51. #include <asm/uv/uv.h>
  52. static unsigned long efi_systab_phys __initdata;
  53. static unsigned long prop_phys = EFI_INVALID_TABLE_ADDR;
  54. static unsigned long uga_phys = EFI_INVALID_TABLE_ADDR;
  55. static unsigned long efi_runtime, efi_nr_tables;
  56. unsigned long efi_fw_vendor, efi_config_table;
  57. static const efi_config_table_type_t arch_tables[] __initconst = {
  58. {EFI_PROPERTIES_TABLE_GUID, &prop_phys, "PROP" },
  59. {UGA_IO_PROTOCOL_GUID, &uga_phys, "UGA" },
  60. #ifdef CONFIG_X86_UV
  61. {UV_SYSTEM_TABLE_GUID, &uv_systab_phys, "UVsystab" },
  62. #endif
  63. {},
  64. };
  65. static const unsigned long * const efi_tables[] = {
  66. &efi.acpi,
  67. &efi.acpi20,
  68. &efi.smbios,
  69. &efi.smbios3,
  70. &uga_phys,
  71. #ifdef CONFIG_X86_UV
  72. &uv_systab_phys,
  73. #endif
  74. &efi_fw_vendor,
  75. &efi_runtime,
  76. &efi_config_table,
  77. &efi.esrt,
  78. &prop_phys,
  79. &efi_mem_attr_table,
  80. #ifdef CONFIG_EFI_RCI2_TABLE
  81. &rci2_table_phys,
  82. #endif
  83. &efi.tpm_log,
  84. &efi.tpm_final_log,
  85. &efi_rng_seed,
  86. #ifdef CONFIG_LOAD_UEFI_KEYS
  87. &efi.mokvar_table,
  88. #endif
  89. #ifdef CONFIG_EFI_COCO_SECRET
  90. &efi.coco_secret,
  91. #endif
  92. };
  93. u64 efi_setup; /* efi setup_data physical address */
  94. static int add_efi_memmap __initdata;
  95. static int __init setup_add_efi_memmap(char *arg)
  96. {
  97. add_efi_memmap = 1;
  98. return 0;
  99. }
  100. early_param("add_efi_memmap", setup_add_efi_memmap);
  101. /*
  102. * Tell the kernel about the EFI memory map. This might include
  103. * more than the max 128 entries that can fit in the passed in e820
  104. * legacy (zeropage) memory map, but the kernel's e820 table can hold
  105. * E820_MAX_ENTRIES.
  106. */
  107. static void __init do_add_efi_memmap(void)
  108. {
  109. efi_memory_desc_t *md;
  110. if (!efi_enabled(EFI_MEMMAP))
  111. return;
  112. for_each_efi_memory_desc(md) {
  113. unsigned long long start = md->phys_addr;
  114. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  115. int e820_type;
  116. switch (md->type) {
  117. case EFI_LOADER_CODE:
  118. case EFI_LOADER_DATA:
  119. case EFI_BOOT_SERVICES_CODE:
  120. case EFI_BOOT_SERVICES_DATA:
  121. case EFI_CONVENTIONAL_MEMORY:
  122. if (efi_soft_reserve_enabled()
  123. && (md->attribute & EFI_MEMORY_SP))
  124. e820_type = E820_TYPE_SOFT_RESERVED;
  125. else if (md->attribute & EFI_MEMORY_WB)
  126. e820_type = E820_TYPE_RAM;
  127. else
  128. e820_type = E820_TYPE_RESERVED;
  129. break;
  130. case EFI_ACPI_RECLAIM_MEMORY:
  131. e820_type = E820_TYPE_ACPI;
  132. break;
  133. case EFI_ACPI_MEMORY_NVS:
  134. e820_type = E820_TYPE_NVS;
  135. break;
  136. case EFI_UNUSABLE_MEMORY:
  137. e820_type = E820_TYPE_UNUSABLE;
  138. break;
  139. case EFI_PERSISTENT_MEMORY:
  140. e820_type = E820_TYPE_PMEM;
  141. break;
  142. default:
  143. /*
  144. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  145. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  146. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  147. */
  148. e820_type = E820_TYPE_RESERVED;
  149. break;
  150. }
  151. e820__range_add(start, size, e820_type);
  152. }
  153. e820__update_table(e820_table);
  154. }
  155. /*
  156. * Given add_efi_memmap defaults to 0 and there is no alternative
  157. * e820 mechanism for soft-reserved memory, import the full EFI memory
  158. * map if soft reservations are present and enabled. Otherwise, the
  159. * mechanism to disable the kernel's consideration of EFI_MEMORY_SP is
  160. * the efi=nosoftreserve option.
  161. */
  162. static bool do_efi_soft_reserve(void)
  163. {
  164. efi_memory_desc_t *md;
  165. if (!efi_enabled(EFI_MEMMAP))
  166. return false;
  167. if (!efi_soft_reserve_enabled())
  168. return false;
  169. for_each_efi_memory_desc(md)
  170. if (md->type == EFI_CONVENTIONAL_MEMORY &&
  171. (md->attribute & EFI_MEMORY_SP))
  172. return true;
  173. return false;
  174. }
  175. int __init efi_memblock_x86_reserve_range(void)
  176. {
  177. struct efi_info *e = &boot_params.efi_info;
  178. struct efi_memory_map_data data;
  179. phys_addr_t pmap;
  180. int rv;
  181. if (efi_enabled(EFI_PARAVIRT))
  182. return 0;
  183. /* Can't handle firmware tables above 4GB on i386 */
  184. if (IS_ENABLED(CONFIG_X86_32) && e->efi_memmap_hi > 0) {
  185. pr_err("Memory map is above 4GB, disabling EFI.\n");
  186. return -EINVAL;
  187. }
  188. pmap = (phys_addr_t)(e->efi_memmap | ((u64)e->efi_memmap_hi << 32));
  189. data.phys_map = pmap;
  190. data.size = e->efi_memmap_size;
  191. data.desc_size = e->efi_memdesc_size;
  192. data.desc_version = e->efi_memdesc_version;
  193. rv = efi_memmap_init_early(&data);
  194. if (rv)
  195. return rv;
  196. if (add_efi_memmap || do_efi_soft_reserve())
  197. do_add_efi_memmap();
  198. efi_fake_memmap_early();
  199. WARN(efi.memmap.desc_version != 1,
  200. "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
  201. efi.memmap.desc_version);
  202. memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
  203. set_bit(EFI_PRESERVE_BS_REGIONS, &efi.flags);
  204. return 0;
  205. }
  206. #define OVERFLOW_ADDR_SHIFT (64 - EFI_PAGE_SHIFT)
  207. #define OVERFLOW_ADDR_MASK (U64_MAX << OVERFLOW_ADDR_SHIFT)
  208. #define U64_HIGH_BIT (~(U64_MAX >> 1))
  209. static bool __init efi_memmap_entry_valid(const efi_memory_desc_t *md, int i)
  210. {
  211. u64 end = (md->num_pages << EFI_PAGE_SHIFT) + md->phys_addr - 1;
  212. u64 end_hi = 0;
  213. char buf[64];
  214. if (md->num_pages == 0) {
  215. end = 0;
  216. } else if (md->num_pages > EFI_PAGES_MAX ||
  217. EFI_PAGES_MAX - md->num_pages <
  218. (md->phys_addr >> EFI_PAGE_SHIFT)) {
  219. end_hi = (md->num_pages & OVERFLOW_ADDR_MASK)
  220. >> OVERFLOW_ADDR_SHIFT;
  221. if ((md->phys_addr & U64_HIGH_BIT) && !(end & U64_HIGH_BIT))
  222. end_hi += 1;
  223. } else {
  224. return true;
  225. }
  226. pr_warn_once(FW_BUG "Invalid EFI memory map entries:\n");
  227. if (end_hi) {
  228. pr_warn("mem%02u: %s range=[0x%016llx-0x%llx%016llx] (invalid)\n",
  229. i, efi_md_typeattr_format(buf, sizeof(buf), md),
  230. md->phys_addr, end_hi, end);
  231. } else {
  232. pr_warn("mem%02u: %s range=[0x%016llx-0x%016llx] (invalid)\n",
  233. i, efi_md_typeattr_format(buf, sizeof(buf), md),
  234. md->phys_addr, end);
  235. }
  236. return false;
  237. }
  238. static void __init efi_clean_memmap(void)
  239. {
  240. efi_memory_desc_t *out = efi.memmap.map;
  241. const efi_memory_desc_t *in = out;
  242. const efi_memory_desc_t *end = efi.memmap.map_end;
  243. int i, n_removal;
  244. for (i = n_removal = 0; in < end; i++) {
  245. if (efi_memmap_entry_valid(in, i)) {
  246. if (out != in)
  247. memcpy(out, in, efi.memmap.desc_size);
  248. out = (void *)out + efi.memmap.desc_size;
  249. } else {
  250. n_removal++;
  251. }
  252. in = (void *)in + efi.memmap.desc_size;
  253. }
  254. if (n_removal > 0) {
  255. struct efi_memory_map_data data = {
  256. .phys_map = efi.memmap.phys_map,
  257. .desc_version = efi.memmap.desc_version,
  258. .desc_size = efi.memmap.desc_size,
  259. .size = efi.memmap.desc_size * (efi.memmap.nr_map - n_removal),
  260. .flags = 0,
  261. };
  262. pr_warn("Removing %d invalid memory map entries.\n", n_removal);
  263. efi_memmap_install(&data);
  264. }
  265. }
  266. void __init efi_print_memmap(void)
  267. {
  268. efi_memory_desc_t *md;
  269. int i = 0;
  270. for_each_efi_memory_desc(md) {
  271. char buf[64];
  272. pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n",
  273. i++, efi_md_typeattr_format(buf, sizeof(buf), md),
  274. md->phys_addr,
  275. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1,
  276. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  277. }
  278. }
  279. static int __init efi_systab_init(unsigned long phys)
  280. {
  281. int size = efi_enabled(EFI_64BIT) ? sizeof(efi_system_table_64_t)
  282. : sizeof(efi_system_table_32_t);
  283. const efi_table_hdr_t *hdr;
  284. bool over4g = false;
  285. void *p;
  286. int ret;
  287. hdr = p = early_memremap_ro(phys, size);
  288. if (p == NULL) {
  289. pr_err("Couldn't map the system table!\n");
  290. return -ENOMEM;
  291. }
  292. ret = efi_systab_check_header(hdr, 1);
  293. if (ret) {
  294. early_memunmap(p, size);
  295. return ret;
  296. }
  297. if (efi_enabled(EFI_64BIT)) {
  298. const efi_system_table_64_t *systab64 = p;
  299. efi_runtime = systab64->runtime;
  300. over4g = systab64->runtime > U32_MAX;
  301. if (efi_setup) {
  302. struct efi_setup_data *data;
  303. data = early_memremap_ro(efi_setup, sizeof(*data));
  304. if (!data) {
  305. early_memunmap(p, size);
  306. return -ENOMEM;
  307. }
  308. efi_fw_vendor = (unsigned long)data->fw_vendor;
  309. efi_config_table = (unsigned long)data->tables;
  310. over4g |= data->fw_vendor > U32_MAX ||
  311. data->tables > U32_MAX;
  312. early_memunmap(data, sizeof(*data));
  313. } else {
  314. efi_fw_vendor = systab64->fw_vendor;
  315. efi_config_table = systab64->tables;
  316. over4g |= systab64->fw_vendor > U32_MAX ||
  317. systab64->tables > U32_MAX;
  318. }
  319. efi_nr_tables = systab64->nr_tables;
  320. } else {
  321. const efi_system_table_32_t *systab32 = p;
  322. efi_fw_vendor = systab32->fw_vendor;
  323. efi_runtime = systab32->runtime;
  324. efi_config_table = systab32->tables;
  325. efi_nr_tables = systab32->nr_tables;
  326. }
  327. efi.runtime_version = hdr->revision;
  328. efi_systab_report_header(hdr, efi_fw_vendor);
  329. early_memunmap(p, size);
  330. if (IS_ENABLED(CONFIG_X86_32) && over4g) {
  331. pr_err("EFI data located above 4GB, disabling EFI.\n");
  332. return -EINVAL;
  333. }
  334. return 0;
  335. }
  336. static int __init efi_config_init(const efi_config_table_type_t *arch_tables)
  337. {
  338. void *config_tables;
  339. int sz, ret;
  340. if (efi_nr_tables == 0)
  341. return 0;
  342. if (efi_enabled(EFI_64BIT))
  343. sz = sizeof(efi_config_table_64_t);
  344. else
  345. sz = sizeof(efi_config_table_32_t);
  346. /*
  347. * Let's see what config tables the firmware passed to us.
  348. */
  349. config_tables = early_memremap(efi_config_table, efi_nr_tables * sz);
  350. if (config_tables == NULL) {
  351. pr_err("Could not map Configuration table!\n");
  352. return -ENOMEM;
  353. }
  354. ret = efi_config_parse_tables(config_tables, efi_nr_tables,
  355. arch_tables);
  356. early_memunmap(config_tables, efi_nr_tables * sz);
  357. return ret;
  358. }
  359. void __init efi_init(void)
  360. {
  361. if (IS_ENABLED(CONFIG_X86_32) &&
  362. (boot_params.efi_info.efi_systab_hi ||
  363. boot_params.efi_info.efi_memmap_hi)) {
  364. pr_info("Table located above 4GB, disabling EFI.\n");
  365. return;
  366. }
  367. efi_systab_phys = boot_params.efi_info.efi_systab |
  368. ((__u64)boot_params.efi_info.efi_systab_hi << 32);
  369. if (efi_systab_init(efi_systab_phys))
  370. return;
  371. if (efi_reuse_config(efi_config_table, efi_nr_tables))
  372. return;
  373. if (efi_config_init(arch_tables))
  374. return;
  375. /*
  376. * Note: We currently don't support runtime services on an EFI
  377. * that doesn't match the kernel 32/64-bit mode.
  378. */
  379. if (!efi_runtime_supported())
  380. pr_err("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  381. if (!efi_runtime_supported() || efi_runtime_disabled()) {
  382. efi_memmap_unmap();
  383. return;
  384. }
  385. /* Parse the EFI Properties table if it exists */
  386. if (prop_phys != EFI_INVALID_TABLE_ADDR) {
  387. efi_properties_table_t *tbl;
  388. tbl = early_memremap_ro(prop_phys, sizeof(*tbl));
  389. if (tbl == NULL) {
  390. pr_err("Could not map Properties table!\n");
  391. } else {
  392. if (tbl->memory_protection_attribute &
  393. EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
  394. set_bit(EFI_NX_PE_DATA, &efi.flags);
  395. early_memunmap(tbl, sizeof(*tbl));
  396. }
  397. }
  398. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  399. efi_clean_memmap();
  400. if (efi_enabled(EFI_DBG))
  401. efi_print_memmap();
  402. }
  403. /* Merge contiguous regions of the same type and attribute */
  404. static void __init efi_merge_regions(void)
  405. {
  406. efi_memory_desc_t *md, *prev_md = NULL;
  407. for_each_efi_memory_desc(md) {
  408. u64 prev_size;
  409. if (!prev_md) {
  410. prev_md = md;
  411. continue;
  412. }
  413. if (prev_md->type != md->type ||
  414. prev_md->attribute != md->attribute) {
  415. prev_md = md;
  416. continue;
  417. }
  418. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  419. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  420. prev_md->num_pages += md->num_pages;
  421. md->type = EFI_RESERVED_TYPE;
  422. md->attribute = 0;
  423. continue;
  424. }
  425. prev_md = md;
  426. }
  427. }
  428. static void *realloc_pages(void *old_memmap, int old_shift)
  429. {
  430. void *ret;
  431. ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
  432. if (!ret)
  433. goto out;
  434. /*
  435. * A first-time allocation doesn't have anything to copy.
  436. */
  437. if (!old_memmap)
  438. return ret;
  439. memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
  440. out:
  441. free_pages((unsigned long)old_memmap, old_shift);
  442. return ret;
  443. }
  444. /*
  445. * Iterate the EFI memory map in reverse order because the regions
  446. * will be mapped top-down. The end result is the same as if we had
  447. * mapped things forward, but doesn't require us to change the
  448. * existing implementation of efi_map_region().
  449. */
  450. static inline void *efi_map_next_entry_reverse(void *entry)
  451. {
  452. /* Initial call */
  453. if (!entry)
  454. return efi.memmap.map_end - efi.memmap.desc_size;
  455. entry -= efi.memmap.desc_size;
  456. if (entry < efi.memmap.map)
  457. return NULL;
  458. return entry;
  459. }
  460. /*
  461. * efi_map_next_entry - Return the next EFI memory map descriptor
  462. * @entry: Previous EFI memory map descriptor
  463. *
  464. * This is a helper function to iterate over the EFI memory map, which
  465. * we do in different orders depending on the current configuration.
  466. *
  467. * To begin traversing the memory map @entry must be %NULL.
  468. *
  469. * Returns %NULL when we reach the end of the memory map.
  470. */
  471. static void *efi_map_next_entry(void *entry)
  472. {
  473. if (efi_enabled(EFI_64BIT)) {
  474. /*
  475. * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
  476. * config table feature requires us to map all entries
  477. * in the same order as they appear in the EFI memory
  478. * map. That is to say, entry N must have a lower
  479. * virtual address than entry N+1. This is because the
  480. * firmware toolchain leaves relative references in
  481. * the code/data sections, which are split and become
  482. * separate EFI memory regions. Mapping things
  483. * out-of-order leads to the firmware accessing
  484. * unmapped addresses.
  485. *
  486. * Since we need to map things this way whether or not
  487. * the kernel actually makes use of
  488. * EFI_PROPERTIES_TABLE, let's just switch to this
  489. * scheme by default for 64-bit.
  490. */
  491. return efi_map_next_entry_reverse(entry);
  492. }
  493. /* Initial call */
  494. if (!entry)
  495. return efi.memmap.map;
  496. entry += efi.memmap.desc_size;
  497. if (entry >= efi.memmap.map_end)
  498. return NULL;
  499. return entry;
  500. }
  501. static bool should_map_region(efi_memory_desc_t *md)
  502. {
  503. /*
  504. * Runtime regions always require runtime mappings (obviously).
  505. */
  506. if (md->attribute & EFI_MEMORY_RUNTIME)
  507. return true;
  508. /*
  509. * 32-bit EFI doesn't suffer from the bug that requires us to
  510. * reserve boot services regions, and mixed mode support
  511. * doesn't exist for 32-bit kernels.
  512. */
  513. if (IS_ENABLED(CONFIG_X86_32))
  514. return false;
  515. /*
  516. * EFI specific purpose memory may be reserved by default
  517. * depending on kernel config and boot options.
  518. */
  519. if (md->type == EFI_CONVENTIONAL_MEMORY &&
  520. efi_soft_reserve_enabled() &&
  521. (md->attribute & EFI_MEMORY_SP))
  522. return false;
  523. /*
  524. * Map all of RAM so that we can access arguments in the 1:1
  525. * mapping when making EFI runtime calls.
  526. */
  527. if (efi_is_mixed()) {
  528. if (md->type == EFI_CONVENTIONAL_MEMORY ||
  529. md->type == EFI_LOADER_DATA ||
  530. md->type == EFI_LOADER_CODE)
  531. return true;
  532. }
  533. /*
  534. * Map boot services regions as a workaround for buggy
  535. * firmware that accesses them even when they shouldn't.
  536. *
  537. * See efi_{reserve,free}_boot_services().
  538. */
  539. if (md->type == EFI_BOOT_SERVICES_CODE ||
  540. md->type == EFI_BOOT_SERVICES_DATA)
  541. return true;
  542. return false;
  543. }
  544. /*
  545. * Map the efi memory ranges of the runtime services and update new_mmap with
  546. * virtual addresses.
  547. */
  548. static void * __init efi_map_regions(int *count, int *pg_shift)
  549. {
  550. void *p, *new_memmap = NULL;
  551. unsigned long left = 0;
  552. unsigned long desc_size;
  553. efi_memory_desc_t *md;
  554. desc_size = efi.memmap.desc_size;
  555. p = NULL;
  556. while ((p = efi_map_next_entry(p))) {
  557. md = p;
  558. if (!should_map_region(md))
  559. continue;
  560. efi_map_region(md);
  561. if (left < desc_size) {
  562. new_memmap = realloc_pages(new_memmap, *pg_shift);
  563. if (!new_memmap)
  564. return NULL;
  565. left += PAGE_SIZE << *pg_shift;
  566. (*pg_shift)++;
  567. }
  568. memcpy(new_memmap + (*count * desc_size), md, desc_size);
  569. left -= desc_size;
  570. (*count)++;
  571. }
  572. return new_memmap;
  573. }
  574. static void __init kexec_enter_virtual_mode(void)
  575. {
  576. #ifdef CONFIG_KEXEC_CORE
  577. efi_memory_desc_t *md;
  578. unsigned int num_pages;
  579. /*
  580. * We don't do virtual mode, since we don't do runtime services, on
  581. * non-native EFI.
  582. */
  583. if (efi_is_mixed()) {
  584. efi_memmap_unmap();
  585. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  586. return;
  587. }
  588. if (efi_alloc_page_tables()) {
  589. pr_err("Failed to allocate EFI page tables\n");
  590. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  591. return;
  592. }
  593. /*
  594. * Map efi regions which were passed via setup_data. The virt_addr is a
  595. * fixed addr which was used in first kernel of a kexec boot.
  596. */
  597. for_each_efi_memory_desc(md)
  598. efi_map_region_fixed(md); /* FIXME: add error handling */
  599. /*
  600. * Unregister the early EFI memmap from efi_init() and install
  601. * the new EFI memory map.
  602. */
  603. efi_memmap_unmap();
  604. if (efi_memmap_init_late(efi.memmap.phys_map,
  605. efi.memmap.desc_size * efi.memmap.nr_map)) {
  606. pr_err("Failed to remap late EFI memory map\n");
  607. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  608. return;
  609. }
  610. num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
  611. num_pages >>= PAGE_SHIFT;
  612. if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) {
  613. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  614. return;
  615. }
  616. efi_sync_low_kernel_mappings();
  617. efi_native_runtime_setup();
  618. #endif
  619. }
  620. /*
  621. * This function will switch the EFI runtime services to virtual mode.
  622. * Essentially, we look through the EFI memmap and map every region that
  623. * has the runtime attribute bit set in its memory descriptor into the
  624. * efi_pgd page table.
  625. *
  626. * The new method does a pagetable switch in a preemption-safe manner
  627. * so that we're in a different address space when calling a runtime
  628. * function. For function arguments passing we do copy the PUDs of the
  629. * kernel page table into efi_pgd prior to each call.
  630. *
  631. * Specially for kexec boot, efi runtime maps in previous kernel should
  632. * be passed in via setup_data. In that case runtime ranges will be mapped
  633. * to the same virtual addresses as the first kernel, see
  634. * kexec_enter_virtual_mode().
  635. */
  636. static void __init __efi_enter_virtual_mode(void)
  637. {
  638. int count = 0, pg_shift = 0;
  639. void *new_memmap = NULL;
  640. efi_status_t status;
  641. unsigned long pa;
  642. if (efi_alloc_page_tables()) {
  643. pr_err("Failed to allocate EFI page tables\n");
  644. goto err;
  645. }
  646. efi_merge_regions();
  647. new_memmap = efi_map_regions(&count, &pg_shift);
  648. if (!new_memmap) {
  649. pr_err("Error reallocating memory, EFI runtime non-functional!\n");
  650. goto err;
  651. }
  652. pa = __pa(new_memmap);
  653. /*
  654. * Unregister the early EFI memmap from efi_init() and install
  655. * the new EFI memory map that we are about to pass to the
  656. * firmware via SetVirtualAddressMap().
  657. */
  658. efi_memmap_unmap();
  659. if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
  660. pr_err("Failed to remap late EFI memory map\n");
  661. goto err;
  662. }
  663. if (efi_enabled(EFI_DBG)) {
  664. pr_info("EFI runtime memory map:\n");
  665. efi_print_memmap();
  666. }
  667. if (efi_setup_page_tables(pa, 1 << pg_shift))
  668. goto err;
  669. efi_sync_low_kernel_mappings();
  670. status = efi_set_virtual_address_map(efi.memmap.desc_size * count,
  671. efi.memmap.desc_size,
  672. efi.memmap.desc_version,
  673. (efi_memory_desc_t *)pa,
  674. efi_systab_phys);
  675. if (status != EFI_SUCCESS) {
  676. pr_err("Unable to switch EFI into virtual mode (status=%lx)!\n",
  677. status);
  678. goto err;
  679. }
  680. efi_check_for_embedded_firmwares();
  681. efi_free_boot_services();
  682. if (!efi_is_mixed())
  683. efi_native_runtime_setup();
  684. else
  685. efi_thunk_runtime_setup();
  686. /*
  687. * Apply more restrictive page table mapping attributes now that
  688. * SVAM() has been called and the firmware has performed all
  689. * necessary relocation fixups for the new virtual addresses.
  690. */
  691. efi_runtime_update_mappings();
  692. /* clean DUMMY object */
  693. efi_delete_dummy_variable();
  694. return;
  695. err:
  696. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  697. }
  698. void __init efi_enter_virtual_mode(void)
  699. {
  700. if (efi_enabled(EFI_PARAVIRT))
  701. return;
  702. efi.runtime = (efi_runtime_services_t *)efi_runtime;
  703. if (efi_setup)
  704. kexec_enter_virtual_mode();
  705. else
  706. __efi_enter_virtual_mode();
  707. efi_dump_pagetable();
  708. }
  709. bool efi_is_table_address(unsigned long phys_addr)
  710. {
  711. unsigned int i;
  712. if (phys_addr == EFI_INVALID_TABLE_ADDR)
  713. return false;
  714. for (i = 0; i < ARRAY_SIZE(efi_tables); i++)
  715. if (*(efi_tables[i]) == phys_addr)
  716. return true;
  717. return false;
  718. }
  719. char *efi_systab_show_arch(char *str)
  720. {
  721. if (uga_phys != EFI_INVALID_TABLE_ADDR)
  722. str += sprintf(str, "UGA=0x%lx\n", uga_phys);
  723. return str;
  724. }
  725. #define EFI_FIELD(var) efi_ ## var
  726. #define EFI_ATTR_SHOW(name) \
  727. static ssize_t name##_show(struct kobject *kobj, \
  728. struct kobj_attribute *attr, char *buf) \
  729. { \
  730. return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
  731. }
  732. EFI_ATTR_SHOW(fw_vendor);
  733. EFI_ATTR_SHOW(runtime);
  734. EFI_ATTR_SHOW(config_table);
  735. struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
  736. struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
  737. struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
  738. umode_t efi_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
  739. {
  740. if (attr == &efi_attr_fw_vendor.attr) {
  741. if (efi_enabled(EFI_PARAVIRT) ||
  742. efi_fw_vendor == EFI_INVALID_TABLE_ADDR)
  743. return 0;
  744. } else if (attr == &efi_attr_runtime.attr) {
  745. if (efi_runtime == EFI_INVALID_TABLE_ADDR)
  746. return 0;
  747. } else if (attr == &efi_attr_config_table.attr) {
  748. if (efi_config_table == EFI_INVALID_TABLE_ADDR)
  749. return 0;
  750. }
  751. return attr->mode;
  752. }