tboot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tboot.c: main implementation of helper functions used by kernel for
  4. * runtime support of Intel(R) Trusted Execution Technology
  5. *
  6. * Copyright (c) 2006-2009, Intel Corporation
  7. */
  8. #include <linux/init_task.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/export.h>
  11. #include <linux/delay.h>
  12. #include <linux/sched.h>
  13. #include <linux/init.h>
  14. #include <linux/dmar.h>
  15. #include <linux/cpu.h>
  16. #include <linux/pfn.h>
  17. #include <linux/mm.h>
  18. #include <linux/tboot.h>
  19. #include <linux/debugfs.h>
  20. #include <asm/realmode.h>
  21. #include <asm/processor.h>
  22. #include <asm/bootparam.h>
  23. #include <asm/pgalloc.h>
  24. #include <asm/fixmap.h>
  25. #include <asm/proto.h>
  26. #include <asm/setup.h>
  27. #include <asm/e820/api.h>
  28. #include <asm/io.h>
  29. #include "../realmode/rm/wakeup.h"
  30. /* Global pointer to shared data; NULL means no measured launch. */
  31. static struct tboot *tboot __read_mostly;
  32. /* timeout for APs (in secs) to enter wait-for-SIPI state during shutdown */
  33. #define AP_WAIT_TIMEOUT 1
  34. #undef pr_fmt
  35. #define pr_fmt(fmt) "tboot: " fmt
  36. static u8 tboot_uuid[16] __initdata = TBOOT_UUID;
  37. bool tboot_enabled(void)
  38. {
  39. return tboot != NULL;
  40. }
  41. /* noinline to prevent gcc from warning about dereferencing constant fixaddr */
  42. static noinline __init bool check_tboot_version(void)
  43. {
  44. if (memcmp(&tboot_uuid, &tboot->uuid, sizeof(tboot->uuid))) {
  45. pr_warn("tboot at 0x%llx is invalid\n", boot_params.tboot_addr);
  46. return false;
  47. }
  48. if (tboot->version < 5) {
  49. pr_warn("tboot version is invalid: %u\n", tboot->version);
  50. return false;
  51. }
  52. pr_info("found shared page at phys addr 0x%llx:\n",
  53. boot_params.tboot_addr);
  54. pr_debug("version: %d\n", tboot->version);
  55. pr_debug("log_addr: 0x%08x\n", tboot->log_addr);
  56. pr_debug("shutdown_entry: 0x%x\n", tboot->shutdown_entry);
  57. pr_debug("tboot_base: 0x%08x\n", tboot->tboot_base);
  58. pr_debug("tboot_size: 0x%x\n", tboot->tboot_size);
  59. return true;
  60. }
  61. void __init tboot_probe(void)
  62. {
  63. /* Look for valid page-aligned address for shared page. */
  64. if (!boot_params.tboot_addr)
  65. return;
  66. /*
  67. * also verify that it is mapped as we expect it before calling
  68. * set_fixmap(), to reduce chance of garbage value causing crash
  69. */
  70. if (!e820__mapped_any(boot_params.tboot_addr,
  71. boot_params.tboot_addr, E820_TYPE_RESERVED)) {
  72. pr_warn("non-0 tboot_addr but it is not of type E820_TYPE_RESERVED\n");
  73. return;
  74. }
  75. /* Map and check for tboot UUID. */
  76. set_fixmap(FIX_TBOOT_BASE, boot_params.tboot_addr);
  77. tboot = (void *)fix_to_virt(FIX_TBOOT_BASE);
  78. if (!check_tboot_version())
  79. tboot = NULL;
  80. }
  81. static pgd_t *tboot_pg_dir;
  82. static struct mm_struct tboot_mm = {
  83. .mm_mt = MTREE_INIT_EXT(mm_mt, MM_MT_FLAGS, tboot_mm.mmap_lock),
  84. .pgd = swapper_pg_dir,
  85. .mm_users = ATOMIC_INIT(2),
  86. .mm_count = ATOMIC_INIT(1),
  87. .write_protect_seq = SEQCNT_ZERO(tboot_mm.write_protect_seq),
  88. MMAP_LOCK_INITIALIZER(init_mm)
  89. .page_table_lock = __SPIN_LOCK_UNLOCKED(init_mm.page_table_lock),
  90. .mmlist = LIST_HEAD_INIT(init_mm.mmlist),
  91. };
  92. static inline void switch_to_tboot_pt(void)
  93. {
  94. write_cr3(virt_to_phys(tboot_pg_dir));
  95. }
  96. static int map_tboot_page(unsigned long vaddr, unsigned long pfn,
  97. pgprot_t prot)
  98. {
  99. pgd_t *pgd;
  100. p4d_t *p4d;
  101. pud_t *pud;
  102. pmd_t *pmd;
  103. pte_t *pte;
  104. pgd = pgd_offset(&tboot_mm, vaddr);
  105. p4d = p4d_alloc(&tboot_mm, pgd, vaddr);
  106. if (!p4d)
  107. return -1;
  108. pud = pud_alloc(&tboot_mm, p4d, vaddr);
  109. if (!pud)
  110. return -1;
  111. pmd = pmd_alloc(&tboot_mm, pud, vaddr);
  112. if (!pmd)
  113. return -1;
  114. pte = pte_alloc_map(&tboot_mm, pmd, vaddr);
  115. if (!pte)
  116. return -1;
  117. set_pte_at(&tboot_mm, vaddr, pte, pfn_pte(pfn, prot));
  118. pte_unmap(pte);
  119. /*
  120. * PTI poisons low addresses in the kernel page tables in the
  121. * name of making them unusable for userspace. To execute
  122. * code at such a low address, the poison must be cleared.
  123. *
  124. * Note: 'pgd' actually gets set in p4d_alloc() _or_
  125. * pud_alloc() depending on 4/5-level paging.
  126. */
  127. pgd->pgd &= ~_PAGE_NX;
  128. return 0;
  129. }
  130. static int map_tboot_pages(unsigned long vaddr, unsigned long start_pfn,
  131. unsigned long nr)
  132. {
  133. /* Reuse the original kernel mapping */
  134. tboot_pg_dir = pgd_alloc(&tboot_mm);
  135. if (!tboot_pg_dir)
  136. return -1;
  137. for (; nr > 0; nr--, vaddr += PAGE_SIZE, start_pfn++) {
  138. if (map_tboot_page(vaddr, start_pfn, PAGE_KERNEL_EXEC))
  139. return -1;
  140. }
  141. return 0;
  142. }
  143. static void tboot_create_trampoline(void)
  144. {
  145. u32 map_base, map_size;
  146. /* Create identity map for tboot shutdown code. */
  147. map_base = PFN_DOWN(tboot->tboot_base);
  148. map_size = PFN_UP(tboot->tboot_size);
  149. if (map_tboot_pages(map_base << PAGE_SHIFT, map_base, map_size))
  150. panic("tboot: Error mapping tboot pages (mfns) @ 0x%x, 0x%x\n",
  151. map_base, map_size);
  152. }
  153. #ifdef CONFIG_ACPI_SLEEP
  154. static void add_mac_region(phys_addr_t start, unsigned long size)
  155. {
  156. struct tboot_mac_region *mr;
  157. phys_addr_t end = start + size;
  158. if (tboot->num_mac_regions >= MAX_TB_MAC_REGIONS)
  159. panic("tboot: Too many MAC regions\n");
  160. if (start && size) {
  161. mr = &tboot->mac_regions[tboot->num_mac_regions++];
  162. mr->start = round_down(start, PAGE_SIZE);
  163. mr->size = round_up(end, PAGE_SIZE) - mr->start;
  164. }
  165. }
  166. static int tboot_setup_sleep(void)
  167. {
  168. int i;
  169. tboot->num_mac_regions = 0;
  170. for (i = 0; i < e820_table->nr_entries; i++) {
  171. if ((e820_table->entries[i].type != E820_TYPE_RAM)
  172. && (e820_table->entries[i].type != E820_TYPE_RESERVED_KERN))
  173. continue;
  174. add_mac_region(e820_table->entries[i].addr, e820_table->entries[i].size);
  175. }
  176. tboot->acpi_sinfo.kernel_s3_resume_vector =
  177. real_mode_header->wakeup_start;
  178. return 0;
  179. }
  180. #else /* no CONFIG_ACPI_SLEEP */
  181. static int tboot_setup_sleep(void)
  182. {
  183. /* S3 shutdown requested, but S3 not supported by the kernel... */
  184. BUG();
  185. return -1;
  186. }
  187. #endif
  188. void tboot_shutdown(u32 shutdown_type)
  189. {
  190. void (*shutdown)(void);
  191. if (!tboot_enabled())
  192. return;
  193. /*
  194. * if we're being called before the 1:1 mapping is set up then just
  195. * return and let the normal shutdown happen; this should only be
  196. * due to very early panic()
  197. */
  198. if (!tboot_pg_dir)
  199. return;
  200. /* if this is S3 then set regions to MAC */
  201. if (shutdown_type == TB_SHUTDOWN_S3)
  202. if (tboot_setup_sleep())
  203. return;
  204. tboot->shutdown_type = shutdown_type;
  205. switch_to_tboot_pt();
  206. shutdown = (void(*)(void))(unsigned long)tboot->shutdown_entry;
  207. shutdown();
  208. /* should not reach here */
  209. while (1)
  210. halt();
  211. }
  212. static void tboot_copy_fadt(const struct acpi_table_fadt *fadt)
  213. {
  214. #define TB_COPY_GAS(tbg, g) \
  215. tbg.space_id = g.space_id; \
  216. tbg.bit_width = g.bit_width; \
  217. tbg.bit_offset = g.bit_offset; \
  218. tbg.access_width = g.access_width; \
  219. tbg.address = g.address;
  220. TB_COPY_GAS(tboot->acpi_sinfo.pm1a_cnt_blk, fadt->xpm1a_control_block);
  221. TB_COPY_GAS(tboot->acpi_sinfo.pm1b_cnt_blk, fadt->xpm1b_control_block);
  222. TB_COPY_GAS(tboot->acpi_sinfo.pm1a_evt_blk, fadt->xpm1a_event_block);
  223. TB_COPY_GAS(tboot->acpi_sinfo.pm1b_evt_blk, fadt->xpm1b_event_block);
  224. /*
  225. * We need phys addr of waking vector, but can't use virt_to_phys() on
  226. * &acpi_gbl_FACS because it is ioremap'ed, so calc from FACS phys
  227. * addr.
  228. */
  229. tboot->acpi_sinfo.wakeup_vector = fadt->facs +
  230. offsetof(struct acpi_table_facs, firmware_waking_vector);
  231. }
  232. static int tboot_sleep(u8 sleep_state, u32 pm1a_control, u32 pm1b_control)
  233. {
  234. static u32 acpi_shutdown_map[ACPI_S_STATE_COUNT] = {
  235. /* S0,1,2: */ -1, -1, -1,
  236. /* S3: */ TB_SHUTDOWN_S3,
  237. /* S4: */ TB_SHUTDOWN_S4,
  238. /* S5: */ TB_SHUTDOWN_S5 };
  239. if (!tboot_enabled())
  240. return 0;
  241. tboot_copy_fadt(&acpi_gbl_FADT);
  242. tboot->acpi_sinfo.pm1a_cnt_val = pm1a_control;
  243. tboot->acpi_sinfo.pm1b_cnt_val = pm1b_control;
  244. /* we always use the 32b wakeup vector */
  245. tboot->acpi_sinfo.vector_width = 32;
  246. if (sleep_state >= ACPI_S_STATE_COUNT ||
  247. acpi_shutdown_map[sleep_state] == -1) {
  248. pr_warn("unsupported sleep state 0x%x\n", sleep_state);
  249. return -1;
  250. }
  251. tboot_shutdown(acpi_shutdown_map[sleep_state]);
  252. return 0;
  253. }
  254. static int tboot_extended_sleep(u8 sleep_state, u32 val_a, u32 val_b)
  255. {
  256. if (!tboot_enabled())
  257. return 0;
  258. pr_warn("tboot is not able to suspend on platforms with reduced hardware sleep (ACPIv5)");
  259. return -ENODEV;
  260. }
  261. static atomic_t ap_wfs_count;
  262. static int tboot_wait_for_aps(int num_aps)
  263. {
  264. unsigned long timeout;
  265. timeout = AP_WAIT_TIMEOUT*HZ;
  266. while (atomic_read((atomic_t *)&tboot->num_in_wfs) != num_aps &&
  267. timeout) {
  268. mdelay(1);
  269. timeout--;
  270. }
  271. if (timeout)
  272. pr_warn("tboot wait for APs timeout\n");
  273. return !(atomic_read((atomic_t *)&tboot->num_in_wfs) == num_aps);
  274. }
  275. static int tboot_dying_cpu(unsigned int cpu)
  276. {
  277. atomic_inc(&ap_wfs_count);
  278. if (num_online_cpus() == 1) {
  279. if (tboot_wait_for_aps(atomic_read(&ap_wfs_count)))
  280. return -EBUSY;
  281. }
  282. return 0;
  283. }
  284. #ifdef CONFIG_DEBUG_FS
  285. #define TBOOT_LOG_UUID { 0x26, 0x25, 0x19, 0xc0, 0x30, 0x6b, 0xb4, 0x4d, \
  286. 0x4c, 0x84, 0xa3, 0xe9, 0x53, 0xb8, 0x81, 0x74 }
  287. #define TBOOT_SERIAL_LOG_ADDR 0x60000
  288. #define TBOOT_SERIAL_LOG_SIZE 0x08000
  289. #define LOG_MAX_SIZE_OFF 16
  290. #define LOG_BUF_OFF 24
  291. static uint8_t tboot_log_uuid[16] = TBOOT_LOG_UUID;
  292. static ssize_t tboot_log_read(struct file *file, char __user *user_buf, size_t count, loff_t *ppos)
  293. {
  294. void __iomem *log_base;
  295. u8 log_uuid[16];
  296. u32 max_size;
  297. void *kbuf;
  298. int ret = -EFAULT;
  299. log_base = ioremap(TBOOT_SERIAL_LOG_ADDR, TBOOT_SERIAL_LOG_SIZE);
  300. if (!log_base)
  301. return ret;
  302. memcpy_fromio(log_uuid, log_base, sizeof(log_uuid));
  303. if (memcmp(&tboot_log_uuid, log_uuid, sizeof(log_uuid)))
  304. goto err_iounmap;
  305. max_size = readl(log_base + LOG_MAX_SIZE_OFF);
  306. if (*ppos >= max_size) {
  307. ret = 0;
  308. goto err_iounmap;
  309. }
  310. if (*ppos + count > max_size)
  311. count = max_size - *ppos;
  312. kbuf = kmalloc(count, GFP_KERNEL);
  313. if (!kbuf) {
  314. ret = -ENOMEM;
  315. goto err_iounmap;
  316. }
  317. memcpy_fromio(kbuf, log_base + LOG_BUF_OFF + *ppos, count);
  318. if (copy_to_user(user_buf, kbuf, count))
  319. goto err_kfree;
  320. *ppos += count;
  321. ret = count;
  322. err_kfree:
  323. kfree(kbuf);
  324. err_iounmap:
  325. iounmap(log_base);
  326. return ret;
  327. }
  328. static const struct file_operations tboot_log_fops = {
  329. .read = tboot_log_read,
  330. .llseek = default_llseek,
  331. };
  332. #endif /* CONFIG_DEBUG_FS */
  333. static __init int tboot_late_init(void)
  334. {
  335. if (!tboot_enabled())
  336. return 0;
  337. tboot_create_trampoline();
  338. atomic_set(&ap_wfs_count, 0);
  339. cpuhp_setup_state(CPUHP_AP_X86_TBOOT_DYING, "x86/tboot:dying", NULL,
  340. tboot_dying_cpu);
  341. #ifdef CONFIG_DEBUG_FS
  342. debugfs_create_file("tboot_log", S_IRUSR,
  343. arch_debugfs_dir, NULL, &tboot_log_fops);
  344. #endif
  345. acpi_os_set_prepare_sleep(&tboot_sleep);
  346. acpi_os_set_prepare_extended_sleep(&tboot_extended_sleep);
  347. return 0;
  348. }
  349. late_initcall(tboot_late_init);
  350. /*
  351. * TXT configuration registers (offsets from TXT_{PUB, PRIV}_CONFIG_REGS_BASE)
  352. */
  353. #define TXT_PUB_CONFIG_REGS_BASE 0xfed30000
  354. #define TXT_PRIV_CONFIG_REGS_BASE 0xfed20000
  355. /* # pages for each config regs space - used by fixmap */
  356. #define NR_TXT_CONFIG_PAGES ((TXT_PUB_CONFIG_REGS_BASE - \
  357. TXT_PRIV_CONFIG_REGS_BASE) >> PAGE_SHIFT)
  358. /* offsets from pub/priv config space */
  359. #define TXTCR_HEAP_BASE 0x0300
  360. #define TXTCR_HEAP_SIZE 0x0308
  361. #define SHA1_SIZE 20
  362. struct sha1_hash {
  363. u8 hash[SHA1_SIZE];
  364. };
  365. struct sinit_mle_data {
  366. u32 version; /* currently 6 */
  367. struct sha1_hash bios_acm_id;
  368. u32 edx_senter_flags;
  369. u64 mseg_valid;
  370. struct sha1_hash sinit_hash;
  371. struct sha1_hash mle_hash;
  372. struct sha1_hash stm_hash;
  373. struct sha1_hash lcp_policy_hash;
  374. u32 lcp_policy_control;
  375. u32 rlp_wakeup_addr;
  376. u32 reserved;
  377. u32 num_mdrs;
  378. u32 mdrs_off;
  379. u32 num_vtd_dmars;
  380. u32 vtd_dmars_off;
  381. } __packed;
  382. struct acpi_table_header *tboot_get_dmar_table(struct acpi_table_header *dmar_tbl)
  383. {
  384. void *heap_base, *heap_ptr, *config;
  385. if (!tboot_enabled())
  386. return dmar_tbl;
  387. /*
  388. * ACPI tables may not be DMA protected by tboot, so use DMAR copy
  389. * SINIT saved in SinitMleData in TXT heap (which is DMA protected)
  390. */
  391. /* map config space in order to get heap addr */
  392. config = ioremap(TXT_PUB_CONFIG_REGS_BASE, NR_TXT_CONFIG_PAGES *
  393. PAGE_SIZE);
  394. if (!config)
  395. return NULL;
  396. /* now map TXT heap */
  397. heap_base = ioremap(*(u64 *)(config + TXTCR_HEAP_BASE),
  398. *(u64 *)(config + TXTCR_HEAP_SIZE));
  399. iounmap(config);
  400. if (!heap_base)
  401. return NULL;
  402. /* walk heap to SinitMleData */
  403. /* skip BiosData */
  404. heap_ptr = heap_base + *(u64 *)heap_base;
  405. /* skip OsMleData */
  406. heap_ptr += *(u64 *)heap_ptr;
  407. /* skip OsSinitData */
  408. heap_ptr += *(u64 *)heap_ptr;
  409. /* now points to SinitMleDataSize; set to SinitMleData */
  410. heap_ptr += sizeof(u64);
  411. /* get addr of DMAR table */
  412. dmar_tbl = (struct acpi_table_header *)(heap_ptr +
  413. ((struct sinit_mle_data *)heap_ptr)->vtd_dmars_off -
  414. sizeof(u64));
  415. /* don't unmap heap because dmar.c needs access to this */
  416. return dmar_tbl;
  417. }