0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/init_task.h>
0010 #include <linux/spinlock.h>
0011 #include <linux/export.h>
0012 #include <linux/delay.h>
0013 #include <linux/sched.h>
0014 #include <linux/init.h>
0015 #include <linux/dmar.h>
0016 #include <linux/cpu.h>
0017 #include <linux/pfn.h>
0018 #include <linux/mm.h>
0019 #include <linux/tboot.h>
0020 #include <linux/debugfs.h>
0021
0022 #include <asm/realmode.h>
0023 #include <asm/processor.h>
0024 #include <asm/bootparam.h>
0025 #include <asm/pgalloc.h>
0026 #include <asm/fixmap.h>
0027 #include <asm/proto.h>
0028 #include <asm/setup.h>
0029 #include <asm/e820/api.h>
0030 #include <asm/io.h>
0031
0032 #include "../realmode/rm/wakeup.h"
0033
0034
0035 static struct tboot *tboot __read_mostly;
0036
0037
0038 #define AP_WAIT_TIMEOUT 1
0039
0040 #undef pr_fmt
0041 #define pr_fmt(fmt) "tboot: " fmt
0042
0043 static u8 tboot_uuid[16] __initdata = TBOOT_UUID;
0044
0045 bool tboot_enabled(void)
0046 {
0047 return tboot != NULL;
0048 }
0049
0050
0051 static noinline __init bool check_tboot_version(void)
0052 {
0053 if (memcmp(&tboot_uuid, &tboot->uuid, sizeof(tboot->uuid))) {
0054 pr_warn("tboot at 0x%llx is invalid\n", boot_params.tboot_addr);
0055 return false;
0056 }
0057
0058 if (tboot->version < 5) {
0059 pr_warn("tboot version is invalid: %u\n", tboot->version);
0060 return false;
0061 }
0062
0063 pr_info("found shared page at phys addr 0x%llx:\n",
0064 boot_params.tboot_addr);
0065 pr_debug("version: %d\n", tboot->version);
0066 pr_debug("log_addr: 0x%08x\n", tboot->log_addr);
0067 pr_debug("shutdown_entry: 0x%x\n", tboot->shutdown_entry);
0068 pr_debug("tboot_base: 0x%08x\n", tboot->tboot_base);
0069 pr_debug("tboot_size: 0x%x\n", tboot->tboot_size);
0070
0071 return true;
0072 }
0073
0074 void __init tboot_probe(void)
0075 {
0076
0077 if (!boot_params.tboot_addr)
0078 return;
0079
0080
0081
0082
0083 if (!e820__mapped_any(boot_params.tboot_addr,
0084 boot_params.tboot_addr, E820_TYPE_RESERVED)) {
0085 pr_warn("non-0 tboot_addr but it is not of type E820_TYPE_RESERVED\n");
0086 return;
0087 }
0088
0089
0090 set_fixmap(FIX_TBOOT_BASE, boot_params.tboot_addr);
0091 tboot = (void *)fix_to_virt(FIX_TBOOT_BASE);
0092 if (!check_tboot_version())
0093 tboot = NULL;
0094 }
0095
0096 static pgd_t *tboot_pg_dir;
0097 static struct mm_struct tboot_mm = {
0098 .mm_rb = RB_ROOT,
0099 .pgd = swapper_pg_dir,
0100 .mm_users = ATOMIC_INIT(2),
0101 .mm_count = ATOMIC_INIT(1),
0102 .write_protect_seq = SEQCNT_ZERO(tboot_mm.write_protect_seq),
0103 MMAP_LOCK_INITIALIZER(init_mm)
0104 .page_table_lock = __SPIN_LOCK_UNLOCKED(init_mm.page_table_lock),
0105 .mmlist = LIST_HEAD_INIT(init_mm.mmlist),
0106 };
0107
0108 static inline void switch_to_tboot_pt(void)
0109 {
0110 write_cr3(virt_to_phys(tboot_pg_dir));
0111 }
0112
0113 static int map_tboot_page(unsigned long vaddr, unsigned long pfn,
0114 pgprot_t prot)
0115 {
0116 pgd_t *pgd;
0117 p4d_t *p4d;
0118 pud_t *pud;
0119 pmd_t *pmd;
0120 pte_t *pte;
0121
0122 pgd = pgd_offset(&tboot_mm, vaddr);
0123 p4d = p4d_alloc(&tboot_mm, pgd, vaddr);
0124 if (!p4d)
0125 return -1;
0126 pud = pud_alloc(&tboot_mm, p4d, vaddr);
0127 if (!pud)
0128 return -1;
0129 pmd = pmd_alloc(&tboot_mm, pud, vaddr);
0130 if (!pmd)
0131 return -1;
0132 pte = pte_alloc_map(&tboot_mm, pmd, vaddr);
0133 if (!pte)
0134 return -1;
0135 set_pte_at(&tboot_mm, vaddr, pte, pfn_pte(pfn, prot));
0136 pte_unmap(pte);
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146 pgd->pgd &= ~_PAGE_NX;
0147
0148 return 0;
0149 }
0150
0151 static int map_tboot_pages(unsigned long vaddr, unsigned long start_pfn,
0152 unsigned long nr)
0153 {
0154
0155 tboot_pg_dir = pgd_alloc(&tboot_mm);
0156 if (!tboot_pg_dir)
0157 return -1;
0158
0159 for (; nr > 0; nr--, vaddr += PAGE_SIZE, start_pfn++) {
0160 if (map_tboot_page(vaddr, start_pfn, PAGE_KERNEL_EXEC))
0161 return -1;
0162 }
0163
0164 return 0;
0165 }
0166
0167 static void tboot_create_trampoline(void)
0168 {
0169 u32 map_base, map_size;
0170
0171
0172 map_base = PFN_DOWN(tboot->tboot_base);
0173 map_size = PFN_UP(tboot->tboot_size);
0174 if (map_tboot_pages(map_base << PAGE_SHIFT, map_base, map_size))
0175 panic("tboot: Error mapping tboot pages (mfns) @ 0x%x, 0x%x\n",
0176 map_base, map_size);
0177 }
0178
0179 #ifdef CONFIG_ACPI_SLEEP
0180
0181 static void add_mac_region(phys_addr_t start, unsigned long size)
0182 {
0183 struct tboot_mac_region *mr;
0184 phys_addr_t end = start + size;
0185
0186 if (tboot->num_mac_regions >= MAX_TB_MAC_REGIONS)
0187 panic("tboot: Too many MAC regions\n");
0188
0189 if (start && size) {
0190 mr = &tboot->mac_regions[tboot->num_mac_regions++];
0191 mr->start = round_down(start, PAGE_SIZE);
0192 mr->size = round_up(end, PAGE_SIZE) - mr->start;
0193 }
0194 }
0195
0196 static int tboot_setup_sleep(void)
0197 {
0198 int i;
0199
0200 tboot->num_mac_regions = 0;
0201
0202 for (i = 0; i < e820_table->nr_entries; i++) {
0203 if ((e820_table->entries[i].type != E820_TYPE_RAM)
0204 && (e820_table->entries[i].type != E820_TYPE_RESERVED_KERN))
0205 continue;
0206
0207 add_mac_region(e820_table->entries[i].addr, e820_table->entries[i].size);
0208 }
0209
0210 tboot->acpi_sinfo.kernel_s3_resume_vector =
0211 real_mode_header->wakeup_start;
0212
0213 return 0;
0214 }
0215
0216 #else
0217
0218 static int tboot_setup_sleep(void)
0219 {
0220
0221 BUG();
0222 return -1;
0223 }
0224
0225 #endif
0226
0227 void tboot_shutdown(u32 shutdown_type)
0228 {
0229 void (*shutdown)(void);
0230
0231 if (!tboot_enabled())
0232 return;
0233
0234
0235
0236
0237
0238
0239 if (!tboot_pg_dir)
0240 return;
0241
0242
0243 if (shutdown_type == TB_SHUTDOWN_S3)
0244 if (tboot_setup_sleep())
0245 return;
0246
0247 tboot->shutdown_type = shutdown_type;
0248
0249 switch_to_tboot_pt();
0250
0251 shutdown = (void(*)(void))(unsigned long)tboot->shutdown_entry;
0252 shutdown();
0253
0254
0255 while (1)
0256 halt();
0257 }
0258
0259 static void tboot_copy_fadt(const struct acpi_table_fadt *fadt)
0260 {
0261 #define TB_COPY_GAS(tbg, g) \
0262 tbg.space_id = g.space_id; \
0263 tbg.bit_width = g.bit_width; \
0264 tbg.bit_offset = g.bit_offset; \
0265 tbg.access_width = g.access_width; \
0266 tbg.address = g.address;
0267
0268 TB_COPY_GAS(tboot->acpi_sinfo.pm1a_cnt_blk, fadt->xpm1a_control_block);
0269 TB_COPY_GAS(tboot->acpi_sinfo.pm1b_cnt_blk, fadt->xpm1b_control_block);
0270 TB_COPY_GAS(tboot->acpi_sinfo.pm1a_evt_blk, fadt->xpm1a_event_block);
0271 TB_COPY_GAS(tboot->acpi_sinfo.pm1b_evt_blk, fadt->xpm1b_event_block);
0272
0273
0274
0275
0276
0277
0278 tboot->acpi_sinfo.wakeup_vector = fadt->facs +
0279 offsetof(struct acpi_table_facs, firmware_waking_vector);
0280 }
0281
0282 static int tboot_sleep(u8 sleep_state, u32 pm1a_control, u32 pm1b_control)
0283 {
0284 static u32 acpi_shutdown_map[ACPI_S_STATE_COUNT] = {
0285 -1, -1, -1,
0286 TB_SHUTDOWN_S3,
0287 TB_SHUTDOWN_S4,
0288 TB_SHUTDOWN_S5 };
0289
0290 if (!tboot_enabled())
0291 return 0;
0292
0293 tboot_copy_fadt(&acpi_gbl_FADT);
0294 tboot->acpi_sinfo.pm1a_cnt_val = pm1a_control;
0295 tboot->acpi_sinfo.pm1b_cnt_val = pm1b_control;
0296
0297 tboot->acpi_sinfo.vector_width = 32;
0298
0299 if (sleep_state >= ACPI_S_STATE_COUNT ||
0300 acpi_shutdown_map[sleep_state] == -1) {
0301 pr_warn("unsupported sleep state 0x%x\n", sleep_state);
0302 return -1;
0303 }
0304
0305 tboot_shutdown(acpi_shutdown_map[sleep_state]);
0306 return 0;
0307 }
0308
0309 static int tboot_extended_sleep(u8 sleep_state, u32 val_a, u32 val_b)
0310 {
0311 if (!tboot_enabled())
0312 return 0;
0313
0314 pr_warn("tboot is not able to suspend on platforms with reduced hardware sleep (ACPIv5)");
0315 return -ENODEV;
0316 }
0317
0318 static atomic_t ap_wfs_count;
0319
0320 static int tboot_wait_for_aps(int num_aps)
0321 {
0322 unsigned long timeout;
0323
0324 timeout = AP_WAIT_TIMEOUT*HZ;
0325 while (atomic_read((atomic_t *)&tboot->num_in_wfs) != num_aps &&
0326 timeout) {
0327 mdelay(1);
0328 timeout--;
0329 }
0330
0331 if (timeout)
0332 pr_warn("tboot wait for APs timeout\n");
0333
0334 return !(atomic_read((atomic_t *)&tboot->num_in_wfs) == num_aps);
0335 }
0336
0337 static int tboot_dying_cpu(unsigned int cpu)
0338 {
0339 atomic_inc(&ap_wfs_count);
0340 if (num_online_cpus() == 1) {
0341 if (tboot_wait_for_aps(atomic_read(&ap_wfs_count)))
0342 return -EBUSY;
0343 }
0344 return 0;
0345 }
0346
0347 #ifdef CONFIG_DEBUG_FS
0348
0349 #define TBOOT_LOG_UUID { 0x26, 0x25, 0x19, 0xc0, 0x30, 0x6b, 0xb4, 0x4d, \
0350 0x4c, 0x84, 0xa3, 0xe9, 0x53, 0xb8, 0x81, 0x74 }
0351
0352 #define TBOOT_SERIAL_LOG_ADDR 0x60000
0353 #define TBOOT_SERIAL_LOG_SIZE 0x08000
0354 #define LOG_MAX_SIZE_OFF 16
0355 #define LOG_BUF_OFF 24
0356
0357 static uint8_t tboot_log_uuid[16] = TBOOT_LOG_UUID;
0358
0359 static ssize_t tboot_log_read(struct file *file, char __user *user_buf, size_t count, loff_t *ppos)
0360 {
0361 void __iomem *log_base;
0362 u8 log_uuid[16];
0363 u32 max_size;
0364 void *kbuf;
0365 int ret = -EFAULT;
0366
0367 log_base = ioremap(TBOOT_SERIAL_LOG_ADDR, TBOOT_SERIAL_LOG_SIZE);
0368 if (!log_base)
0369 return ret;
0370
0371 memcpy_fromio(log_uuid, log_base, sizeof(log_uuid));
0372 if (memcmp(&tboot_log_uuid, log_uuid, sizeof(log_uuid)))
0373 goto err_iounmap;
0374
0375 max_size = readl(log_base + LOG_MAX_SIZE_OFF);
0376 if (*ppos >= max_size) {
0377 ret = 0;
0378 goto err_iounmap;
0379 }
0380
0381 if (*ppos + count > max_size)
0382 count = max_size - *ppos;
0383
0384 kbuf = kmalloc(count, GFP_KERNEL);
0385 if (!kbuf) {
0386 ret = -ENOMEM;
0387 goto err_iounmap;
0388 }
0389
0390 memcpy_fromio(kbuf, log_base + LOG_BUF_OFF + *ppos, count);
0391 if (copy_to_user(user_buf, kbuf, count))
0392 goto err_kfree;
0393
0394 *ppos += count;
0395
0396 ret = count;
0397
0398 err_kfree:
0399 kfree(kbuf);
0400
0401 err_iounmap:
0402 iounmap(log_base);
0403
0404 return ret;
0405 }
0406
0407 static const struct file_operations tboot_log_fops = {
0408 .read = tboot_log_read,
0409 .llseek = default_llseek,
0410 };
0411
0412 #endif
0413
0414 static __init int tboot_late_init(void)
0415 {
0416 if (!tboot_enabled())
0417 return 0;
0418
0419 tboot_create_trampoline();
0420
0421 atomic_set(&ap_wfs_count, 0);
0422 cpuhp_setup_state(CPUHP_AP_X86_TBOOT_DYING, "x86/tboot:dying", NULL,
0423 tboot_dying_cpu);
0424 #ifdef CONFIG_DEBUG_FS
0425 debugfs_create_file("tboot_log", S_IRUSR,
0426 arch_debugfs_dir, NULL, &tboot_log_fops);
0427 #endif
0428
0429 acpi_os_set_prepare_sleep(&tboot_sleep);
0430 acpi_os_set_prepare_extended_sleep(&tboot_extended_sleep);
0431 return 0;
0432 }
0433
0434 late_initcall(tboot_late_init);
0435
0436
0437
0438
0439
0440 #define TXT_PUB_CONFIG_REGS_BASE 0xfed30000
0441 #define TXT_PRIV_CONFIG_REGS_BASE 0xfed20000
0442
0443
0444 #define NR_TXT_CONFIG_PAGES ((TXT_PUB_CONFIG_REGS_BASE - \
0445 TXT_PRIV_CONFIG_REGS_BASE) >> PAGE_SHIFT)
0446
0447
0448 #define TXTCR_HEAP_BASE 0x0300
0449 #define TXTCR_HEAP_SIZE 0x0308
0450
0451 #define SHA1_SIZE 20
0452
0453 struct sha1_hash {
0454 u8 hash[SHA1_SIZE];
0455 };
0456
0457 struct sinit_mle_data {
0458 u32 version;
0459 struct sha1_hash bios_acm_id;
0460 u32 edx_senter_flags;
0461 u64 mseg_valid;
0462 struct sha1_hash sinit_hash;
0463 struct sha1_hash mle_hash;
0464 struct sha1_hash stm_hash;
0465 struct sha1_hash lcp_policy_hash;
0466 u32 lcp_policy_control;
0467 u32 rlp_wakeup_addr;
0468 u32 reserved;
0469 u32 num_mdrs;
0470 u32 mdrs_off;
0471 u32 num_vtd_dmars;
0472 u32 vtd_dmars_off;
0473 } __packed;
0474
0475 struct acpi_table_header *tboot_get_dmar_table(struct acpi_table_header *dmar_tbl)
0476 {
0477 void *heap_base, *heap_ptr, *config;
0478
0479 if (!tboot_enabled())
0480 return dmar_tbl;
0481
0482
0483
0484
0485
0486
0487
0488 config = ioremap(TXT_PUB_CONFIG_REGS_BASE, NR_TXT_CONFIG_PAGES *
0489 PAGE_SIZE);
0490 if (!config)
0491 return NULL;
0492
0493
0494 heap_base = ioremap(*(u64 *)(config + TXTCR_HEAP_BASE),
0495 *(u64 *)(config + TXTCR_HEAP_SIZE));
0496 iounmap(config);
0497 if (!heap_base)
0498 return NULL;
0499
0500
0501
0502 heap_ptr = heap_base + *(u64 *)heap_base;
0503
0504 heap_ptr += *(u64 *)heap_ptr;
0505
0506 heap_ptr += *(u64 *)heap_ptr;
0507
0508 heap_ptr += sizeof(u64);
0509
0510 dmar_tbl = (struct acpi_table_header *)(heap_ptr +
0511 ((struct sinit_mle_data *)heap_ptr)->vtd_dmars_off -
0512 sizeof(u64));
0513
0514
0515
0516 return dmar_tbl;
0517 }