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