intel_txt: Intel(R) TXT and tboot kernel support
Subsystems (9)
RFC PATCH v2 1/1 intel_txt: Intel(R) TXT and tboot kernel support
Linux support for Intel(R) Trusted Execution Technology.
arch/x86/configs/i386_defconfig | 1 arch/x86/configs/x86_64_defconfig | 1 arch/x86/include/asm/bootparam.h | 1 arch/x86/include/asm/fixmap.h | 3 arch/x86/include/asm/tboot.h | 152 +++++++++++++++ arch/x86/kernel/Makefile | 1 arch/x86/kernel/reboot.c | 18 + arch/x86/kernel/setup.c | 4 arch/x86/kernel/smpboot.c | 6 arch/x86/kernel/tboot.c | 371 ++++++++++++++++++++++++++++++++++++++ drivers/acpi/acpica/hwsleep.c | 34 +++ drivers/acpi/sleep.c | 3 drivers/pci/dmar.c | 6 kernel/cpu.c | 6 kernel/power/disk.c | 2 security/Kconfig | 17 + 16 files changed, 622 insertions(+), 4 deletions(-)
Signed-off-by: Shane Wang <[email protected]> Signed-off-by: Joseph Cihula <[email protected]> Signed-off-by: Gang Wei <[email protected]>
---
arch/x86/configs/i386_defconfig renamed from linux.trees.git/arch/x86/configs/i386_defconfig +0 -0
@@ -51,6 +51,7 @@ CONFIG_X86_BIOS_REBOOT=y
CONFIG_X86_TRAMPOLINE=yCONFIG_KTIME_SCALAR=y CONFIG_DEFCONFIG_LIST="/lib/modules/$UNAME_RELEASE/.config" +# CONFIG_INTEL_TXT is not set
#
# General setup
arch/x86/configs/x86_64_defconfig renamed from linux.trees.git/arch/x86/configs/x86_64_defconfig +0 -0
@@ -52,6 +52,7 @@ CONFIG_X86_BIOS_REBOOT=y
CONFIG_X86_TRAMPOLINE=y
# CONFIG_KTIME_SCALAR is not set CONFIG_DEFCONFIG_LIST="/lib/modules/$UNAME_RELEASE/.config" +# CONFIG_INTEL_TXT is not set
#
# General setup
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +0 -0
@@ -62,6 +62,7 @@ struct setup_header {
__u32 payload_offset; __u32 payload_length; __u64 setup_data;
+ __u32 tboot_shared_addr;
} __attribute__((packed));
struct sys_desc_table {
arch/x86/include/asm/fixmap.h renamed from linux.trees.git/arch/x86/include/asm/fixmap.h +0 -0
@@ -132,6 +132,9 @@ enum fixed_addresses {
#ifdef CONFIG_X86_32
FIX_WP_TEST,
#endif
+#ifdef CONFIG_INTEL_TXT
+ FIX_TBOOT_SHARED_BASE,
+#endif
__end_of_fixed_addresses
};
arch/x86/include/asm/tboot.h renamed from linux.trees.git/arch/x86/include/asm/tboot.h +152 -0
152 lines not on this page · raw message
arch/x86/kernel/Makefile renamed from linux.trees.git/arch/x86/kernel/Makefile +0 -0
@@ -47,6 +47,7 @@ obj-$(CONFIG_X86_DS) += ds.o
obj-$(CONFIG_X86_32) += tls.o
obj-$(CONFIG_IA32_EMULATION) += tls.o
obj-y += step.o
+obj-$(CONFIG_INTEL_TXT) += tboot.o
obj-$(CONFIG_STACKTRACE) += stacktrace.o
obj-y += cpu/
obj-y += acpi/
arch/x86/kernel/reboot.c renamed from linux.trees.git/arch/x86/kernel/reboot.c +0 -0
@@ -24,6 +24,8 @@
# include <asm/iommu.h> #endif
+#include <asm/tboot.h>
+
/*
* Power off function, if any
*/
@@ -435,6 +437,8 @@ static void native_machine_emergency_res
if (reboot_emergency) emergency_vmx_disable_all();
+ tboot_shutdown(TB_SHUTDOWN_REBOOT);
+
/* Tell the BIOS if we want cold or warm reboot */ *((unsigned short *)__va(0x472)) = reboot_mode;
@@ -500,11 +504,17 @@ static void native_machine_emergency_res
void native_machine_shutdown(void)
{
- /* Stop the cpus and apics */
#ifdef CONFIG_SMP
-
/* The boot cpu is always logical cpu 0 */ int reboot_cpu_id = 0;
+#endif
+
+ /* TXT requires VMX to be off for all shutdowns */
+ if (tboot_in_measured_env())
+ emergency_vmx_disable_all();
+
+ /* Stop the cpus and apics */
+#ifdef CONFIG_SMP
#ifdef CONFIG_X86_32 /* See if there has been given a command line override */ @@ -561,6 +571,8 @@ static void native_machine_halt(void) /* stop other cpus and apics */ machine_shutdown();
+ tboot_shutdown(TB_SHUTDOWN_HALT);
+
/* stop this cpu */ stop_this_cpu(NULL); } @@ -572,6 +584,8 @@ static void native_machine_power_off(voi machine_shutdown(); pm_power_off(); }
+ /* a fallback in case there is no PM info available */
+ tboot_shutdown(TB_SHUTDOWN_HALT);
}
struct machine_ops machine_ops = {
arch/x86/kernel/setup.c renamed from linux.trees.git/arch/x86/kernel/setup.c +0 -0
@@ -137,6 +137,8 @@ struct boot_params __initdata boot_param
struct boot_params boot_params;
#endif
+#include <asm/tboot.h>
+
/*
* Machine setup..
*/
@@ -940,6 +942,8 @@ void __init setup_arch(char **cmdline_p)
paravirt_pagetable_setup_done(swapper_pg_dir); paravirt_post_allocator_init();
+ tboot_probe();
+
#ifdef CONFIG_X86_64 map_vsyscall();
#endif
arch/x86/kernel/smpboot.c renamed from linux.trees.git/arch/x86/kernel/smpboot.c +0 -0
@@ -62,6 +62,7 @@
#include <asm/vmi.h>
#include <asm/apic.h>
#include <asm/setup.h>
+#include <asm/tboot.h>
#include <asm/uv/uv.h>
#include <linux/mc146818rtc.h>
@@ -1313,7 +1314,10 @@ void play_dead_common(void) void native_play_dead(void)
{
play_dead_common();- wbinvd_halt();
+ if (tboot_in_measured_env())
+ tboot_shutdown(TB_SHUTDOWN_WFS);
+ else
+ wbinvd_halt();
}
#else /* ... !CONFIG_HOTPLUG_CPU */
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +371 -0
371 lines not on this page · raw message
drivers/acpi/acpica/hwsleep.c renamed from linux.trees.git/drivers/acpi/acpica/hwsleep.c +0 -0
@@ -45,6 +45,7 @@
#include <acpi/acpi.h>
#include "accommon.h"
#include "actables.h"
+#include <asm/tboot.h>
#define _COMPONENT ACPI_HARDWAREACPI_MODULE_NAME("hwsleep")
@@ -333,6 +334,39 @@ acpi_status asmlinkage acpi_enter_sleep_
PM1Acontrol |= sleep_enable_reg_info->access_bit_mask; PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
+#ifdef CONFIG_INTEL_TXT
+#define TB_COPY_GAS(tbg, g) \
+ tbg.space_id = g.space_id; \ + tbg.bit_width = g.bit_width; \ + tbg.bit_offset = g.bit_offset; \ + tbg.access_width = g.access_width; \
+ tbg.address = g.address;
+
+ if (tboot_in_measured_env()) {
+ TB_COPY_GAS(tboot_shared->acpi_sinfo.pm1a_cnt_blk,
+ acpi_gbl_FADT.xpm1a_control_block);
+ TB_COPY_GAS(tboot_shared->acpi_sinfo.pm1b_cnt_blk,
+ acpi_gbl_FADT.xpm1b_control_block);
+ TB_COPY_GAS(tboot_shared->acpi_sinfo.pm1a_evt_blk,
+ acpi_gbl_FADT.xpm1a_event_block);
+ TB_COPY_GAS(tboot_shared->acpi_sinfo.pm1b_evt_blk,
+ acpi_gbl_FADT.xpm1b_event_block);
+ tboot_shared->acpi_sinfo.pm1a_cnt_val = PM1Acontrol;
+ tboot_shared->acpi_sinfo.pm1b_cnt_val = PM1Bcontrol;
+ /* we need phys addr of waking vector, but can't use
+ virt_to_phys() on &acpi_gbl_FACS because it is ioremap'ed,
+ so calc from FACS phys addr */
+ tboot_shared->acpi_sinfo.wakeup_vector = acpi_gbl_FADT.facs +
+ ((void *)&acpi_gbl_FACS->firmware_waking_vector -
+ (void *)acpi_gbl_FACS);
+ tboot_shared->acpi_sinfo.vector_width = 32;
+ tboot_shared->acpi_sinfo.kernel_s3_resume_vector =
+ acpi_wakeup_address;
+
+ tboot_sleep(sleep_state);
+ }
+#endif
+
/* Write #2: SLP_TYP + SLP_EN */ ACPI_FLUSH_CPU_CACHE();
drivers/acpi/sleep.c renamed from linux.trees.git/drivers/acpi/sleep.c +0 -0
@@ -22,6 +22,7 @@
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>
#include "sleep.h"
+#include <asm/tboot.h>
u8 sleep_states[ACPI_S_STATE_COUNT];
static u32 acpi_target_sleep_state = ACPI_STATE_S0; @@ -246,6 +247,8 @@ static int acpi_suspend_enter(suspend_st
break; }
+ tboot_sx_resume(acpi_state);
+
/* If ACPI is not enabled by the BIOS, we need to enable it here. */ if (set_sci_en_on_resume) acpi_set_register(ACPI_BITREG_SCI_ENABLE, 1);
drivers/pci/dmar.c renamed from linux.trees.git/drivers/pci/dmar.c +0 -0
@@ -33,6 +33,7 @@
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
+#include <asm/tboot.h>
#undef PREFIX
#define PREFIX "DMAR:"
@@ -319,6 +320,11 @@ parse_dmar_table(void)
*/ dmar_table_detect();
+ /* ACPI tables may not be DMA protected by tboot, so use DMAR copy */
+ /* SINIT saved in SinitMleData in TXT heap (which is DMA protected) */
+ if (tboot_in_measured_env())
+ dmar_tbl = tboot_get_dmar_table();
+
dmar = (struct acpi_table_dmar *)dmar_tbl; if (!dmar) return -ENODEV;
kernel/cpu.c renamed from linux.trees.git/kernel/cpu.c +0 -0
@@ -14,6 +14,7 @@
#include <linux/kthread.h>
#include <linux/stop_machine.h>
#include <linux/mutex.h>
+#include <asm/tboot.h>
#ifdef CONFIG_SMP
/* Serializes the updates to cpu_online_mask, cpu_present_mask */ @@ -379,7 +380,7 @@ static cpumask_var_t frozen_cpus;
int disable_nonboot_cpus(void)
{
- int cpu, first_cpu, error;
+ int cpu, first_cpu, error, num_cpus = 0;
error = stop_machine_create();
if (error)
@@ -394,6 +395,7 @@ int disable_nonboot_cpus(void)
for_each_online_cpu(cpu) {
if (cpu == first_cpu)
continue;
+ num_cpus++;
error = _cpu_down(cpu, 1);
if (!error) {
cpumask_set_cpu(cpu, frozen_cpus);
@@ -404,6 +406,8 @@ int disable_nonboot_cpus(void)
break;
}
}
+ /* ensure all CPUs have gone into wait-for-SIPI */
+ tboot_wait_for_aps(num_cpus);
if (!error) {
BUG_ON(num_online_cpus() > 1);
/* Make sure the CPUs won't be enabled by someone else */kernel/power/disk.c renamed from linux.trees.git/kernel/power/disk.c +0 -0
@@ -22,6 +22,7 @@
#include <linux/console.h>
#include <linux/cpu.h>
#include <linux/freezer.h>
+#include <asm/tboot.h>
#include "power.h"
@@ -308,6 +309,7 @@ int hibernation_snapshot(int platform_mo
} Enable_cpus: enable_nonboot_cpus(); + tboot_sx_resume(ACPI_STATE_S4); Finish: platform_finish(platform_mode);
Resume_devices:
security/Kconfig renamed from linux.trees.git/security/Kconfig +0 -0
@@ -132,6 +132,23 @@ config SECURITY_DEFAULT_MMAP_MIN_ADDR
/proc/sys/vm/mmap_min_addr tunable.
+config INTEL_TXT
+ bool "Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)" + depends on EXPERIMENTAL && X86 + help + This option enables support for booting the kernel with + the Trusted Boot (tboot) module. This will utilize + Intel(R) Trusted Execution Technology to perform a + measured launch of the kernel. If the system does not + support Intel(R) TXT, this will have no effect. + + See <http://www.intel.com/technology/security/> for more + information about Intel(R) TXT. + And see <http://tboot.sourceforge.net> for more information + about tboot.
+
+ If you are unsure as to whether this is required, answer N.
+
source security/selinux/Kconfig
source security/smack/Kconfig
Joseph Cihula <[email protected]> writes:
Quick review.
Overall it doesn't seem too bad and looks clean and reasonable overall, but some details probably need to be fixed.
Quoted 8 lines
diff -uprN ../linux.trees.git/arch/x86/configs/i386_defconfig
./arch/x86/configs/i386_defconfigarch/x86/configs/i386_defconfig renamed from linux.trees.git/arch/x86/configs/i386_defconfig +0
-0@@ -51,6 +51,7 @@ CONFIG_X86_BIOS_REBOOT=y
CONFIG_X86_TRAMPOLINE=yCONFIG_KTIME_SCALAR=yCONFIG_DEFCONFIG_LIST="/lib/modules/$UNAME_RELEASE/.config" +#CONFIG_INTEL_TXTis not set
Normally random patches shouldn't update defconfigs, maintainers do that. Just drop these hunks.
arch/x86/include/asm/bootparam.h struct setup_header { · line 66
Quoted 10 lines
# General setup
diff -uprN../linux.trees.git/arch/x86/include/asm/bootparam.h./arch/x86/include/asm/bootparam.harch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +1
-0@@ -62,6 +62,7 @@ struct setup_header {__u32 payload_offset;__u32 payload_length;__u64 setup_data; +__u32 tboot_shared_addr; } __attribute__((packed));
These changes should be documented in Documentation/x86/zero-page.txt
arch/x86/include/asm/bootparam.h struct setup_header {
Quoted 8 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +8
-0+/* GAS - Generic Address Structure (ACPI 2.0+) */ +struct tboot_acpi_generic_address { +u8 space_id; +u8 bit_width; +u8 bit_offset; +u8 access_width; +u64 address; +} __attribute__ ((__packed__ ));
Why not use the existing structure from ACPI?
arch/x86/include/asm/bootparam.h
Quoted 8 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +8
-0+struct tboot_acpi_sleep_info { +struct tboot_acpi_generic_address pm1a_cnt_blk; +struct tboot_acpi_generic_address pm1b_cnt_blk; +struct tboot_acpi_generic_address pm1a_evt_blk; +struct tboot_acpi_generic_address pm1b_evt_blk; +u16 pm1a_cnt_val; +u16 pm1b_cnt_val; +u64 wakeup_vector;
So that vector can be >4GB but the tshared data structure not?
arch/x86/include/asm/bootparam.h
Quoted 3 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +3
-0+u32 vector_width; +u64 kernel_s3_resume_vector; +} __attribute__ ((__packed__));
In general it might be better to put ACPI defined structures somewhere in the acpi includes, separated from Linux defined types.
A reference to the spec in a comment would be also useful.
Quoted 11 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +11
-0+ +struct tboot_shared { +/* version 3+ fields: */ +struct tboot_uuid uuid;/* TBOOT_SHARED_UUID */ +u32 version;/* Version number: 5 is current */ +u32 log_addr;/* physical addr of tb_log_t log */ +u32 shutdown_entry;/* entry point for tboot shutdown */ +u32 shutdown_type;/* type of shutdown (TB_SHUTDOWN_*) */ +struct tboot_acpi_sleep_info + acpi_sinfo;/* where kernel put acpi sleep info in Sx */ +u32 tboot_base;/* starting addr for tboot */
arch/x86/include/asm/bootparam.h
Quoted 10 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +10
-0+u32 tboot_size;/* size of tboot */ +u8 num_mac_regions;/* number mem regions to MAC on S3 */ +/* contig regions memory to MAC on S3 */ +struct tboot_mac_region mac_regions[MAX_TB_MAC_REGIONS ]; +/* version 4+ fields: */ +/* populated by tboot; will be encrypted */ +u8 s3_key[TB_KEY_SIZE ]; +/* version 5+ fields: */ +u8 reserved_align[3 ];/* used to 4byte-align num_in_wfs */ +u32 num_in_wfs;/* number of processors in wait-for-SIPI */
You could use __attribute__ aligned instead
Quoted 6 lines
*((unsigned short *)__va(0x472)) = reboot_mode;arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +0
-0@@ -500,11 +504,17 @@ static void native_machine_emergency_resvoid native_machine_shutdown (void ) {
arch/x86/include/asm/bootparam.h static void native_machine_emergency_res · line 514
Quoted 10 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +5
-2-/* Stop the cpus and apics */ #ifdef CONFIG_SMP -/* The boot cpu is always logical cpu 0 */ int reboot_cpu_id =0 ; +#endif + +/* TXT requires VMX to be off for all shutdowns */ +if (tboot_in_measured_env ()) +emergency_vmx_disable_all ();
I thought KVM did that already in its shutdown handler? Why is this needed again?
arch/x86/include/asm/bootparam.h static void native_machine_emergency_res
Quoted 9 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +9
-0+#include <asm/pgalloc.h> +#include <asm/processor.h> +#include <asm/bootparam.h> +#include <asm/setup.h> +#include <asm/io.h> +#include <asm/tboot.h> + +/* Global pointer to shared data; NULL means no measured launch. */ +struct tboot_shared *tboot_shared;
Should be __read_mostly probably
arch/x86/include/asm/bootparam.h
Quoted 2 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +2
-0+ +static spinlock_t pgtable_lock;
Locks should have comments describing what they protect and possibly required locking order if applicable.
Also should be using DEFINE_SPINLOCK (or maybe DEFINE_RAW_SPINLOCK, are all these contexts lockdep safe?)
arch/x86/include/asm/bootparam.h
Quoted 8 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +8
-0+void __init tboot_probe (void ) +{ +/* Look for valid page-aligned address for shared page. */ +if (boot_params.hdr.tboot_shared_addr ==0 ) +return ; + +/* Map and check for tboot UUID. */ +set_fixmap (FIX_TBOOT_SHARED_BASE , boot_params.hdr.tboot_shared_addr);
I would do a quick check here if this actually points to something in the e820 map. Just to be paranoid and avoid crashes for junk input data.
arch/x86/include/asm/bootparam.h
Quoted 6 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +6
-0+ tboot_shared =NULL ; +return ; + } +if (tboot_shared->version <5 ) { +printk (KERN_WARNING "tboot_shared version is invalid: %u\n" , + tboot_shared->version);
This would likely benefit from a more instructive prefix that points what wentg wrong. TXT: ? "Measured-launch:"? TBOOT: ? Similar for other messages.
arch/x86/include/asm/bootparam.h
Quoted 4 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +4
-0+return ; + } + +spin_lock_init (&pgtable_lock);
Ok drop that and use DEFINE_*_SPINLOCK
arch/x86/include/asm/bootparam.h
Quoted 9 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +9
-0+printk (KERN_INFO "TBOOT: found shared page at phys addr 0x%lx:\n" , + (unsigned long )boot_params.hdr.tboot_shared_addr); +printk (KERN_DEBUG " version: %d\n" , tboot_shared->version); +printk (KERN_DEBUG " log_addr: 0x%08x\n" , tboot_shared->log_addr); +printk (KERN_DEBUG " shutdown_entry: 0x%x\n" , + tboot_shared->shutdown_entry); +printk (KERN_DEBUG " tboot_base: 0x%08x\n" , tboot_shared->tboot_base); +printk (KERN_DEBUG " tboot_size: 0x%x\n" , tboot_shared->tboot_size); +}
Seem to be a lot more dependencies on all this stuff being < 4GB. Was that intended?
arch/x86/include/asm/bootparam.h
Quoted 7 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +7
-0+static pgd_t *tboot_pg_dir; +static u32 map_base, map_size; +static struct mm_struct tboot_mm =INIT_MM (tboot_mm); + +static inline void switch_to_tboot_pt (void ) +{ +wbinvd ();
Is the wbinvd really needed? If yes there should be a comment.
arch/x86/include/asm/bootparam.h
Quoted 1 line
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +1
-0+native_write_cr3 (virt_to_phys (tboot_pg_dir));
This means it explodes with paravirt, right? Do you have a check early for that?
Quoted 18 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +18
-0+static void clean_up_tboot_mapping (void ) +{ +pgd_t *pgd; +pud_t *pud; +pmd_t *pmd; +pte_t *pte; +unsigned long vaddr, i, j; + +if (!tboot_in_measured_env ()) +return ; + +/* The following loop is to release the pages allocated for building +* tboot page table. It releases pte pages, then pmd pages, then pud +* pages and finally the pgd page by using variable i to control +* the process, where +* i = 0 denotes to release pte, +* i = 1 denotes to release pmd, +* i = 2 denotes to release pud,
arch/x86/include/asm/bootparam.h
Quoted 10 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +10
-0+* and i > 2 denotes to release pgd +*/ + i =0 ; +while (tboot_pg_dir) { +for (j =0 , vaddr = map_base <<PAGE_SHIFT ; + j < map_size; + j++, vaddr +=PAGE_SIZE ) { +if (i >2 ) { +if (tboot_pg_dir) +pgd_free (&tboot_mm, tboot_pg_dir);
pgd_free() does a lot of things, some of them likely specific to user mappings. Have you checked if that is all ok?
arch/x86/include/asm/bootparam.h
Quoted 5 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +5
-0+ tboot_pg_dir =0 ; +break ; + }else { + pgd =pgd_offset (&tboot_mm, vaddr); +if ((!pgd) || (!pgd_present (*pgd)))
The NULL checks here and below are all not needed. *_offset never returns NULL.
arch/x86/include/asm/bootparam.h
Quoted 3 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +3
-0+ +/* release pte */ + pte =pte_offset_kernel (pmd,0 );
So you assume the pte is in lowmem here..
Quoted 16 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +16
-0+if (pte) +pte_free_kernel (&tboot_mm, pte); +pmd_clear (pmd); + } + i++; + } +} + +static int map_page_for_tboot (unsigned long vaddr,unsigned long pfn, +pgprot_t prot) +{ +pgd_t *pgd; +pud_t *pud; +pmd_t *pmd; +pte_t *pte; +
arch/x86/include/asm/bootparam.h
Quoted 10 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +10
-0+ pgd =pgd_offset (&tboot_mm, vaddr); + pud =pud_alloc (&tboot_mm, pgd, vaddr); +if (!pud) +return -1 ; + pmd =pmd_alloc (&tboot_mm, pud, vaddr); +if (!pmd) +return -1 ; + pte =pte_alloc_map (&tboot_mm, pmd, vaddr); +if (!pte) +return -1 ;
... but not here. That seems inconsistent and might cause crashes with highpte on 32bit. better to make sure none of them are in highmem.
arch/x86/include/asm/bootparam.h
Quoted 10 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +10
-0+set_pte_at (&tboot_mm, vaddr, pte,pfn_pte (pfn, prot)); +pte_unmap (pte); +return 0 ; +} + +static int map_pages_for_tboot (unsigned long vaddr,unsigned long start_pfn, +unsigned long nr) +{ +/* Reuse the original kernel mapping */ + tboot_pg_dir =pgd_alloc (&tboot_mm);
That puts the PGD into the pgd_list and then pageattr.c will actually walk that list and change ptes, assuming it's a standard kernel mapping. Can you tolerate that? It seems dangerous. Better to use a pgd_alloc_kernel(). There's none, but you could add one.
Quoted 4 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +4
-0+#include "acpi/realmode/wakeup.h" +#include <asm/trampoline.h> + +void tboot_shutdown (u32 shutdown_type)
arch/x86/include/asm/bootparam.h
Quoted 10 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +10
-0+{ +if (!tboot_in_measured_env ()) +return ; + +/* only create the table once */ +spin_lock (&pgtable_lock); +if (!tboot_pg_dir) { +/* page alloc routines need interrupts enabled else they */ +/* generate debug warnings; we'll disable them afterwards */ +local_irq_enable ();
Sorry but that doesn't work. It's still not safe to sleep with a spinlock and there are other counters e.g. on preempt kernels.
It's probably not too bad in your case because there shouldn't be multiple callers of tboot and probably also only in single CPU contexts, but all the debugging infrastructure won't like it.
So likely you can just drop the spinlock. Perhaps add a WARN_ON() somewhere for parallel entry using a simple flag.
Also I'm not sure it's a good idea to enable interrupts so deep down in shutdown/suspend, it might confuse other code too. Better don't do it.
Quoted 7 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +7
-0+PFN_UP (WAKEUP_SIZE ) <<PAGE_SHIFT ; +/* AP trampoline code */ + tboot_shared->mac_regions[1 ].start = +PFN_PHYS (PFN_DOWN (virt_to_phys (trampoline_base))); + tboot_shared->mac_regions[1 ].size = +PFN_UP (TRAMPOLINE_SIZE ) <<PAGE_SHIFT ; +/* kernel code + data + bss */
arch/x86/include/asm/bootparam.h
Quoted 10 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +10
-0+ tboot_shared->mac_regions[2 ].start = +PFN_PHYS (PFN_DOWN (virt_to_phys (&_text))); + tboot_shared->mac_regions[2 ].size = +PFN_PHYS (PFN_UP (virt_to_phys (&_end))) - +PFN_PHYS (PFN_DOWN (virt_to_phys (&_text))); + } + + tboot_shared->shutdown_type = shutdown_type; + +switch_to_tboot_pt ();
What happens with the NMIs? (nmi watchdog etc.) Are they all disabled at this point? Machine checks probably too.
It might be safer to load a special IDT too that just returns.
arch/x86/include/asm/bootparam.h
Quoted 4 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +4
-0+ + ((void (*)(void ))(unsigned long )tboot_shared->shutdown_entry)(); + +BUG ();/* should not reach here */
The bug will explode because the kernel is gone.
Quoted 9 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +9
-0+} + +void tboot_sleep (u8 sleep_state) +{ +uint32_t shutdown_type; + +switch (sleep_state) { +case ACPI_STATE_S3 : + shutdown_type =TB_SHUTDOWN_S3 ;
arch/x86/include/asm/bootparam.h
Quoted 10 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +10
-0+break ; +case ACPI_STATE_S4 : + shutdown_type =TB_SHUTDOWN_S4 ; +break ; +case ACPI_STATE_S5 : + shutdown_type =TB_SHUTDOWN_S5 ; +break ; +default : +return ; + }
That's a simple table lookup, could be done much shorter this way.
arch/x86/include/asm/bootparam.h
Quoted 2 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +2
-0+#define TXT_PUB_CONFIG_REGS_BASE 0xfed30000 +#define TXT_PRIV_CONFIG_REGS_BASE 0xfed20000
Really no way to discover that?
arch/x86/include/asm/bootparam.h
Quoted 8 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +8
-0+ *(u64 *)(config + TXTCR_HEAP_SIZE)); +iounmap (config); +if (heap_base ==NULL ) +return NULL ; + +/* walk heap to SinitMleData */ +/* skip BiosData */ + heap_ptr = heap_base + *(uint64_t *)heap_base;
Normally using u64. i would add some sanity checks for out of bounds etc. Normally it's a good idea to sanity check anything coming from the BIOS.
Also isn't this reinventing ACPI table parser code? Perhaps the ACPI code could be used directly instead. or is this too early?
Quoted 10 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +10
-0+ tbg.access_width = g.access_width; \ + tbg.address = g.address; + +if (tboot_in_measured_env ()) { +TB_COPY_GAS (tboot_shared->acpi_sinfo.pm1a_cnt_blk, + acpi_gbl_FADT.xpm1a_control_block); +TB_COPY_GAS (tboot_shared->acpi_sinfo.pm1b_cnt_blk, + acpi_gbl_FADT.xpm1b_control_block); +TB_COPY_GAS (tboot_shared->acpi_sinfo.pm1a_evt_blk, + acpi_gbl_FADT.xpm1a_event_block);
arch/x86/include/asm/bootparam.h
Quoted 10 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +10
-0+TB_COPY_GAS (tboot_shared->acpi_sinfo.pm1b_evt_blk, + acpi_gbl_FADT.xpm1b_event_block); + tboot_shared->acpi_sinfo.pm1a_cnt_val = PM1Acontrol; + tboot_shared->acpi_sinfo.pm1b_cnt_val = PM1Bcontrol; +/* we need phys addr of waking vector, but can't use +virt_to_phys() on &acpi_gbl_FACS because it is ioremap'ed, +so calc from FACS phys addr */ + tboot_shared->acpi_sinfo.wakeup_vector = acpi_gbl_FADT.facs + + ((void *)&acpi_gbl_FACS->firmware_waking_vector - + (void *)acpi_gbl_FACS);
That's an opencoded offsetof ?
And you assign the address of the field in the FACS, not the value?
arch/x86/include/asm/bootparam.h int disable_nonboot_cpus(void) · line 410
Quoted 6 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +2
-0@@ -404,6 +406,8 @@ int disable_nonboot_cpus(void)break ; } } +/* ensure all CPUs have gone into wait-for-SIPI */ +tboot_wait_for_aps (num_cpus);
That looks like something that should be done generically. Wait for the other CPUs to be "dead".
arch/x86/include/asm/bootparam.h int disable_nonboot_cpus(void)
Quoted 10 lines
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +8
-0+config INTEL_TXT + bool "Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)" + depends on EXPERIMENTAL && X86 +help + This option enables support for booting the kernel with + the Trusted Boot (tboot) module. This will utilize + Intel(R) Trusted Execution Technology to perform a + measured launch of the kernel.
One-two sentences here what value a a measured launch brings to the user.
arch/x86/include/asm/bootparam.h
Quoted 9 lines
If the system does not
arch/x86/include/asm/bootparam.h renamed from linux.trees.git/arch/x86/include/asm/bootparam.h +8
-0+ support Intel(R) TXT, this will have no effect. + + See <http://www.intel.com/technology/security/> for more + information about Intel(R) TXT. + And see <http://tboot.sourceforge.net> for more information + about tboot. + + If you are unsure as to whether this is required, answer N.
Putting the howto you had in the intro somewhere in Documentation/* and adding a pointer to it here would be a good idea.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only.
Re: [RFC v2][PATCH 1/1] intel_txt: Intel(R) TXT and tboot kernel support II
Quoted 7 lines · Andi Kleen
Quoted 7 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +7
-0+set_pte_at (&tboot_mm, vaddr, pte,pfn_pte (pfn, prot)); +pte_unmap (pte); +return 0 ; +} + +static int map_pages_for_tboot (unsigned long vaddr,unsigned long start_pfn, +unsigned long nr)
Quoted 8 lines
arch/x86/kernel/tboot.c line 186
Quoted 3 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +3
-0+{ +/* Reuse the original kernel mapping */ + tboot_pg_dir =pgd_alloc (&tboot_mm);That puts the PGD into the
pgd_listand thenpageattr.cwill actually walk that list and change ptes, assuming it's a standard kernel mapping. Can you tolerate that? It seems dangerous. Better to use apgd_alloc_kernel(). There's none, but you could add one.
Thought some more about this. I think you're ok on 64bit at least because the kernel mappings are elsewhere from the identity map and keeping them in sync with pageattr makes sense and avoids illegal cache attribute aliases.
But on 32bit it could be potentially a problem in general. e.g. what happens when the tboot shared page is in the area the kernel is running? You would crash during the window where you run in that pgd.
It would be probably safer to use a low memory trampoline supplied by the kernel too that then loads the new pgd.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only.
Quoted 3 lines · Andi Kleen
This means it explodes with paravirt, right? Do you have a check early for that?
The tboot stuff should only be run on bare hardware; if there's virtualization under the kernel, then TXT should be happening at that level.
J
Quoted 7 lines · Jeremy Fitzhardinge
Quoted 3 lines · Andi Kleen
This means it explodes with paravirt, right? Do you have a check early for that?
The tboot stuff should only be run on bare hardware; if there's virtualization under the kernel, then TXT should be happening at that level.
Yes I know, but still this should be enforced.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only.
Quoted 5 lines
From: Andi Kleen [mailto:[email protected]]
Sent: Wednesday, April 22, 2009 1:40 PMQuoted 1 line · Jeremy Fitzhardinge
Andi Kleen wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
This means it explodes with paravirt, right? Do you have a check early for that?
The tboot stuff should only be run on bare hardware; if there's virtualization under the kernel, then TXT should be happening at that level.
Yes I know, but still this should be enforced.
This code will only be executed if the tboot_shared page is found, which should not be the case if the kernel is running paravirtualized. But I'm planning to change it to just write_cr3() to be more consistent with other cases where we don't try to specifically exclude paravirt hooks.
Joe
Quoted 14 lines
From: Andi Kleen [mailto:[email protected]]
Sent: Saturday, April 18, 2009 3:03 AMJoseph Cihula <[email protected]> writes:
Quick review.
Overall it doesn't seem too bad and looks clean and reasonable overall, but some details probably need to be fixed.
Quoted 1 line
diff -uprN ../linux.trees.git/arch/x86/configs/i386_defconfig
./arch/x86/configs/i386_defconfigQuoted 1 line
--- ../linux.trees.git/arch/x86/configs/i386_defconfig 2009-03-29 12:12:13.000000000 -
0700
Quoted 9 lines
Quoted 6 lines
+++
./arch/x86/configs/i386_defconfig2009-03-30 13:03:26.000000000 -0700(quoted diff) +0
-0@@ -51,6 +51,7 @@ CONFIG_X86_BIOS_REBOOT=y
CONFIG_X86_TRAMPOLINE=yCONFIG_KTIME_SCALAR=yCONFIG_DEFCONFIG_LIST="/lib/modules/$UNAME_RELEASE/.config" +#CONFIG_INTEL_TXTis not setNormally random patches shouldn't update defconfigs, maintainers do that. Just drop these hunks.
Will do.
Quoted 5 lines
Quoted 2 lines
# General setup
diff -uprN../linux.trees.git/arch/x86/include/asm/bootparam.h
./arch/x86/include/asm/bootparam.hQuoted 1 line
---
../linux.trees.git/arch/x86/include/asm/bootparam.h2009-03-29 12:12:13.000000000 -0700
Quoted 9 lines
Review struct setup_header { · line 66
Quoted 7 lines
+++
./arch/x86/include/asm/bootparam.h2009-03-30 13:03:25.000000000 -0700(quoted diff) +1
-0@@ -62,6 +62,7 @@ struct setup_header { __u32 payload_offset; __u32 payload_length; __u64 setup_data; + __u32 tboot_shared_addr; } __attribute__((packed));These changes should be documented in
Documentation/x86/zero-page.txt
Per Peter Anvin's advice, this field will move to struct boot_params (and changed to be 64b) and we will doc it in zero-page.txt.
Quoted 10 lines
arch/x86/include/asm/tboot.h line 62
Quoted 8 lines
arch/x86/include/asm/tboot.h renamed from linux.trees.git/arch/x86/include/asm/tboot.h +8
-0+/* GAS - Generic Address Structure (ACPI 2.0+) */ +struct tboot_acpi_generic_address { +u8 space_id; +u8 bit_width; +u8 bit_offset; +u8 access_width; +u64 address; +} __attribute__ ((__packed__ ));Why not use the existing structure from ACPI?
This is the ACPI structure, but just not the Linux type definition of it. This is because tboot needs to work with multiple target kernels/VMMs and so it must define its parameters "independently".
Quoted 10 lines
arch/x86/include/asm/tboot.h line 71
Quoted 8 lines
arch/x86/include/asm/tboot.h renamed from linux.trees.git/arch/x86/include/asm/tboot.h +8
-0+struct tboot_acpi_sleep_info { +struct tboot_acpi_generic_address pm1a_cnt_blk; +struct tboot_acpi_generic_address pm1b_cnt_blk; +struct tboot_acpi_generic_address pm1a_evt_blk; +struct tboot_acpi_generic_address pm1b_evt_blk; +u16 pm1a_cnt_val; +u16 pm1b_cnt_val; +u64 wakeup_vector;So that vector can be >4GB but the tshared data structure not?
Neither can currently be 64b in practice, though now both types will be 64b to allow for future "expansion".
Quoted 6 lines
arch/x86/include/asm/tboot.h line 74
Quoted 3 lines
arch/x86/include/asm/tboot.h renamed from linux.trees.git/arch/x86/include/asm/tboot.h +3
-0+u32 vector_width; +u64 kernel_s3_resume_vector; +} __attribute__ ((__packed__));In general it might be better to put ACPI defined structures somewhere in the acpi includes, separated from Linux defined types.
Per above, tboot needs to define its interface independently of any specific kernel or VMM.
Quoted 1 line
A reference to the spec in a comment would be also useful.
OK.
Quoted 21 lines
Quoted 10 lines
arch/x86/include/asm/tboot.h renamed from linux.trees.git/arch/x86/include/asm/tboot.h +10
-0+struct tboot_shared { +/* version 3+ fields: */ +struct tboot_uuid uuid;/* TBOOT_SHARED_UUID */ +u32 version;/* Version number: 5 is current */ +u32 log_addr;/* physical addr of tb_log_t log */ +u32 shutdown_entry;/* entry point for tboot shutdown */ +u32 shutdown_type;/* type of shutdown (TB_SHUTDOWN_*) */ +struct tboot_acpi_sleep_info + acpi_sinfo;/* where kernel put acpi sleep info in Sx */ +u32 tboot_base;/* starting addr for tboot */ arch/x86/include/asm/tboot.h line 95
Quoted 10 lines
arch/x86/include/asm/tboot.h renamed from linux.trees.git/arch/x86/include/asm/tboot.h +10
-0+u32 tboot_size;/* size of tboot */ +u8 num_mac_regions;/* number mem regions to MAC on S3 */ +/* contig regions memory to MAC on S3 */ +struct tboot_mac_region mac_regions[MAX_TB_MAC_REGIONS ]; +/* version 4+ fields: */ +/* populated by tboot; will be encrypted */ +u8 s3_key[TB_KEY_SIZE ]; +/* version 5+ fields: */ +u8 reserved_align[3 ];/* used to 4byte-align num_in_wfs */ +u32 num_in_wfs;/* number of processors in wait-for-SIPI */
Quoted 1 line
You could use
__attribute__aligned instead
I prefer to keep the padding explicit, for wider compiler compatibility as well as for potential later use.
Quoted 4 lines
Quoted 4 lines
*((unsigned short *)__va(0x472)) = reboot_mode;(quoted diff) +0
-0@@ -500,11 +504,17 @@ static void native_machine_emergency_res
Quoted 14 lines
Review static void native_machine_emergency_res · line 514
Quoted 12 lines
(quoted diff) +5
-2void native_machine_shutdown(void) { - /* Stop the cpus and apics */ #ifdef CONFIG_SMP - /* The boot cpu is always logical cpu 0 */ int reboot_cpu_id = 0; +#endif + + /* TXT requires VMX to be off for all shutdowns */ + if (tboot_in_measured_env()) + emergency_vmx_disable_all();I thought KVM did that already in its shutdown handler? Why is this needed again?
It does in most shutdown cases, but not for reboot.
Quoted 11 lines
arch/x86/kernel/tboot.c line 37
Quoted 9 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +9
-0+#include <asm/pgalloc.h> +#include <asm/processor.h> +#include <asm/bootparam.h> +#include <asm/setup.h> +#include <asm/io.h> +#include <asm/tboot.h> + +/* Global pointer to shared data; NULL means no measured launch. */ +struct tboot_shared *tboot_shared;Should be
__read_mostlyprobably
Will change.
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static spinlock_t pgtable_lock;Locks should have comments describing what they protect and possibly required locking order if applicable.
Will be removed (see below).
Quoted 2 lines
Also should be using
DEFINE_SPINLOCK(or maybeDEFINE_RAW_SPINLOCK, are all these contexts lockdep safe?)
Ditto on removal.
Quoted 4 lines
Quoted 4 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +4
-0+void __init tboot_probe (void ) +{ +/* Look for valid page-aligned address for shared page. */ +if (boot_params.hdr.tboot_shared_addr ==0 )
Quoted 8 lines
arch/x86/kernel/tboot.c line 48
Quoted 4 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +4
-0+return ; + +/* Map and check for tboot UUID. */ +set_fixmap (FIX_TBOOT_SHARED_BASE , boot_params.hdr.tboot_shared_addr);I would do a quick check here if this actually points to something in the e820 map. Just to be paranoid and avoid crashes for junk input data.
Will do.
Quoted 9 lines
arch/x86/kernel/tboot.c line 60
Quoted 6 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +6
-0+ tboot_shared =NULL ; +return ; + } +if (tboot_shared->version <5 ) { +printk (KERN_WARNING "tboot_shared version is invalid: %u\n" , + tboot_shared->version);This would likely benefit from a more instructive prefix that points what wentg wrong. TXT: ? "Measured-launch:"? TBOOT: ? Similar for other messages.
Will do.
Quoted 6 lines
arch/x86/kernel/tboot.c line 65
Quoted 4 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +4
-0+return ; + } + +spin_lock_init (&pgtable_lock);Ok drop that and use DEFINE_*_SPINLOCK
Will be removed per below.
Quoted 11 lines
arch/x86/kernel/tboot.c line 75
Quoted 9 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +9
-0+printk (KERN_INFO "TBOOT: found shared page at phys addr 0x%lx:\n" , + (unsigned long )boot_params.hdr.tboot_shared_addr); +printk (KERN_DEBUG " version: %d\n" , tboot_shared->version); +printk (KERN_DEBUG " log_addr: 0x%08x\n" , tboot_shared->log_addr); +printk (KERN_DEBUG " shutdown_entry: 0x%x\n" , + tboot_shared->shutdown_entry); +printk (KERN_DEBUG " tboot_base: 0x%08x\n" , tboot_shared->tboot_base); +printk (KERN_DEBUG " tboot_size: 0x%x\n" , tboot_shared->tboot_size); +}Seem to be a lot more dependencies on all this stuff being < 4GB. Was that intended?
Yes--Intel TXT components (SINIT, MLE, config space, etc.) are defined to be <4GB.
Quoted 9 lines
arch/x86/kernel/tboot.c line 83
Quoted 7 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +7
-0+static pgd_t *tboot_pg_dir; +static u32 map_base, map_size; +static struct mm_struct tboot_mm =INIT_MM (tboot_mm); + +static inline void switch_to_tboot_pt (void ) +{ +wbinvd ();Is the wbinvd really needed? If yes there should be a comment.
It was needed to ensure that all of the page table creation writes were getting flushed before paging got disabled by tboot. Without it we were getting a PREEMPT SMP DEBUG_PAGEALLOC warning. I will add a comment to this effect.
Quoted 4 lines
Quoted 1 line
+ native_write_cr3(virt_to_phys(tboot_pg_dir));
This means it explodes with paravirt, right? Do you have a check early for that?
This code will only be executed if the tboot_shared page is found, which should not be the case if the kernel is running paravirtualized. But I will change it to just write_cr3() to be more consistent with other cases where we don't try to specifically exclude paravirt hooks.
Quoted 23 lines
Quoted 23 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +23
-0+static void clean_up_tboot_mapping (void ) +{ +pgd_t *pgd; +pud_t *pud; +pmd_t *pmd; +pte_t *pte; +unsigned long vaddr, i, j; + +if (!tboot_in_measured_env ()) +return ; + +/* The following loop is to release the pages allocated for building +* tboot page table. It releases pte pages, then pmd pages, then pud +* pages and finally the pgd page by using variable i to control +* the process, where +* i = 0 denotes to release pte, +* i = 1 denotes to release pmd, +* i = 2 denotes to release pud, +* and i > 2 denotes to release pgd +*/ + i =0 ; +while (tboot_pg_dir) { + for (j = 0, vaddr = map_base << PAGE_SHIFT;
Quoted 8 lines
arch/x86/kernel/tboot.c line 114
Quoted 5 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +5
-0+ j < map_size; + j++, vaddr += PAGE_SIZE) { +if (i >2 ) { +if (tboot_pg_dir) +pgd_free (&tboot_mm, tboot_pg_dir);
pgd_free()does a lot of things, some of them likely specific to user mappings. Have you checked if that is all ok?
This is being removed; see below.
Quoted 7 lines
arch/x86/kernel/tboot.c line 119
Quoted 5 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +5
-0+ tboot_pg_dir =0 ; +break ; + }else { + pgd =pgd_offset (&tboot_mm, vaddr); +if ((!pgd) || (!pgd_present (*pgd)))The NULL checks here and below are all not needed.
*_offsetnever returns NULL.
Removed.
Quoted 5 lines
arch/x86/kernel/tboot.c line 150
Quoted 3 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +3
-0+ +/* release pte */ + pte =pte_offset_kernel (pmd,0 );So you assume the pte is in lowmem here..
See below for change to allocation (and removal of de-allocation).
Quoted 21 lines
Quoted 21 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +21
-0+if (pte) +pte_free_kernel (&tboot_mm, pte); +pmd_clear (pmd); + } + i++; + } +} + +static int map_page_for_tboot (unsigned long vaddr,unsigned long pfn, +pgprot_t prot) +{ +pgd_t *pgd; +pud_t *pud; +pmd_t *pmd; +pte_t *pte; + + pgd =pgd_offset (&tboot_mm, vaddr); + pud =pud_alloc (&tboot_mm, pgd, vaddr); +if (!pud) +return -1 ; + pmd =pmd_alloc (&tboot_mm, pud, vaddr);
Quoted 8 lines
arch/x86/kernel/tboot.c line 176
Quoted 5 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +5
-0+if (!pmd) +return -1 ; + pte =pte_alloc_map (&tboot_mm, pmd, vaddr); +if (!pte) +return -1 ;... but not here. That seems inconsistent and might cause crashes with highpte on 32bit. better to make sure none of them are in highmem.
See below for change to allocation (and removal of de-allocation).
Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +7
-0+set_pte_at (&tboot_mm, vaddr, pte,pfn_pte (pfn, prot)); +pte_unmap (pte); +return 0 ; +} + +static int map_pages_for_tboot (unsigned long vaddr,unsigned long start_pfn, +unsigned long nr)
Quoted 9 lines
arch/x86/kernel/tboot.c line 186
Quoted 3 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +3
-0+{ +/* Reuse the original kernel mapping */ + tboot_pg_dir =pgd_alloc (&tboot_mm);That puts the PGD into the
pgd_listand thenpageattr.cwill actually walk that list and change ptes, assuming it's a standard kernel mapping. Can you tolerate that? It seems dangerous. Better to use apgd_alloc_kernel(). There's none, but you could add one.
from a followup email of Andi's:
Quoted 5 lines
Thought some more about this. I think you're ok on 64bit at least because the kernel mappings are elsewhere from the identity map and keeping them in sync with pageattr makes sense and avoids illegal cache attribute aliases.
Quoted 7 lines
But on 32bit it could be potentially a problem in general. e.g. what happens when the tboot shared page is in the area the kernel is running? You would crash during the window where you run in that pgd.
It would be probably safer to use a low memory trampoline supplied by the kernel too that then loads the new pgd.
I don't think that I understand the issue. The tboot physical addr range will always be <4GB and thus will it's virtual 1:1 mapping. Immediately after switching to this pgd the CPU calls into tboot which will then disable paging and do its thing. Can you elaborate on what the exact issue would be?
Quoted 18 lines
Quoted 4 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +4
-0+#include "acpi/realmode/wakeup.h" +#include <asm/trampoline.h> + +void tboot_shutdown (u32 shutdown_type)arch/x86/kernel/tboot.c line 211
Quoted 10 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +10
-0+{ +if (!tboot_in_measured_env ()) +return ; + +/* only create the table once */ +spin_lock (&pgtable_lock); +if (!tboot_pg_dir) { +/* page alloc routines need interrupts enabled else they */ +/* generate debug warnings; we'll disable them afterwards */ +local_irq_enable ();Sorry but that doesn't work. It's still not safe to sleep with a spinlock and there are other counters e.g. on preempt kernels.
Quoted 3 lines
It's probably not too bad in your case because there shouldn't be multiple callers of tboot and probably also only in single CPU contexts, but all the debugging infrastructure won't like it.
The debug infrastructure is why this was added--we were getting warnings during page table allocation that interrupts were disabled. This cause all shutdown cases to complete without warnings.
But now that you mention it, I think it will be better to create the map during early boot and then we can skip the spinlock, leave interrupts disabled, and skip the free as well.
Quoted 2 lines
So likely you can just drop the spinlock. Perhaps add a
WARN_ON()somewhere for parallel entry using a simple flag.
Per above, we will allocate early and then don't need the spinlock.
Quoted 17 lines
Also I'm not sure it's a good idea to enable interrupts so deep down in shutdown/suspend, it might confuse other code too. Better don't do it.
Quoted 12 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +12
-0+PFN_UP (WAKEUP_SIZE ) <<PAGE_SHIFT ; +/* AP trampoline code */ + tboot_shared->mac_regions[1 ].start = +PFN_PHYS (PFN_DOWN (virt_to_phys (trampoline_base))); + tboot_shared->mac_regions[1 ].size = +PFN_UP (TRAMPOLINE_SIZE ) <<PAGE_SHIFT ; +/* kernel code + data + bss */ + tboot_shared->mac_regions[2 ].start = +PFN_PHYS (PFN_DOWN (virt_to_phys (&_text))); + tboot_shared->mac_regions[2 ].size = +PFN_PHYS (PFN_UP (virt_to_phys (&_end))) - +PFN_PHYS (PFN_DOWN (virt_to_phys (&_text)));
Quoted 8 lines
arch/x86/kernel/tboot.c line 251
Quoted 5 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +5
-0+ } + + tboot_shared->shutdown_type = shutdown_type; + +switch_to_tboot_pt ();What happens with the NMIs? (nmi watchdog etc.) Are they all disabled at this point? Machine checks probably too.
The TXT-related code doesn't disable them. Doesn't Linux disable them before shutting down?
Quoted 1 line
It might be safer to load a special IDT too that just returns.
Tboot will install a minimal IDT but it will handle all interrupts by forcing a reboot.
Quoted 6 lines
arch/x86/kernel/tboot.c line 255
Quoted 4 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +4
-0+ + ((void (*)(void ))(unsigned long )tboot_shared->shutdown_entry)(); + +BUG ();/* should not reach here */ The bug will explode because the kernel is gone.
We can probably remove the BUG(), since the call itself can't fail and by the time the tboot code could get into a bad state that caused an un-matched ret, the kernel's page table would be long gone and it would never get back here. But as any execution beyond the call would be catastrophic, it is actually desirable that things blow up rather than fail insecurely--so I'm inclined to leave it or replace it with something equally fatal (?).
Quoted 13 lines
Quoted 13 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +13
-0+} + +void tboot_sleep (u8 sleep_state) +{ +uint32_t shutdown_type; + +switch (sleep_state) { +case ACPI_STATE_S3 : + shutdown_type =TB_SHUTDOWN_S3 ; +break ; +case ACPI_STATE_S4 : + shutdown_type =TB_SHUTDOWN_S4 ; +break ;
Quoted 8 lines
arch/x86/kernel/tboot.c line 274
Quoted 6 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +6
-0+case ACPI_STATE_S5 : + shutdown_type =TB_SHUTDOWN_S5 ; +break ; +default : +return ; + }That's a simple table lookup, could be done much shorter this way.
Changed.
Quoted 4 lines
arch/x86/kernel/tboot.c line 293
Quoted 2 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +2
-0+#define TXT_PUB_CONFIG_REGS_BASE 0xfed30000 +#define TXT_PRIV_CONFIG_REGS_BASE 0xfed20000Really no way to discover that?
These are defined as fixed addrs on all TXT platforms.
Quoted 11 lines
arch/x86/kernel/tboot.c line 349
Quoted 8 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +8
-0+ *(u64 *)(config + TXTCR_HEAP_SIZE)); +iounmap (config); +if (heap_base ==NULL ) +return NULL ; + +/* walk heap to SinitMleData */ +/* skip BiosData */ + heap_ptr = heap_base + *(uint64_t *)heap_base;Normally using u64. i would add some sanity checks for out of bounds etc. Normally it's a good idea to sanity check anything coming from the BIOS.
Changed to u64. This data was already validated by tboot and should not have been altered since then.
Quoted 2 lines
Also isn't this reinventing ACPI table parser code? Perhaps the ACPI code could be used directly instead. or is this too early?
This is a copy of the ACPI table that was made during the launch, placed into DMA-protected memory, and validated for correctness. So that is why we want to get it from here.
Quoted 21 lines
Quoted 10 lines
(quoted diff) +10
-0+ tbg.access_width = g.access_width; \ + tbg.address = g.address; + + if (tboot_in_measured_env()) { + TB_COPY_GAS(tboot_shared->acpi_sinfo.pm1a_cnt_blk, + acpi_gbl_FADT.xpm1a_control_block); + TB_COPY_GAS(tboot_shared->acpi_sinfo.pm1b_cnt_blk, + acpi_gbl_FADT.xpm1b_control_block); + TB_COPY_GAS(tboot_shared->acpi_sinfo.pm1a_evt_blk, + acpi_gbl_FADT.xpm1a_event_block);Review
Quoted 10 lines
(quoted diff) +10
-0+ TB_COPY_GAS(tboot_shared->acpi_sinfo.pm1b_evt_blk, + acpi_gbl_FADT.xpm1b_event_block); + tboot_shared->acpi_sinfo.pm1a_cnt_val = PM1Acontrol; + tboot_shared->acpi_sinfo.pm1b_cnt_val = PM1Bcontrol; + /* we need phys addr of waking vector, but can't use + virt_to_phys() on &acpi_gbl_FACS because it is ioremap'ed, + so calc from FACS phys addr */ + tboot_shared->acpi_sinfo.wakeup_vector = acpi_gbl_FADT.facs + + ((void *)&acpi_gbl_FACS->firmware_waking_vector - + (void *)acpi_gbl_FACS);
Quoted 1 line
That's an opencoded offsetof ?
I'm not sure I understand what you mean. But the resulting pointer will get checked for validity in tboot before it is used.
Quoted 1 line
And you assign the address of the field in the FACS, not the value?
Right--the address is the place that BIOS will look for the resume vector.
Quoted 9 lines
Review int disable_nonboot_cpus(void) · line 410
Quoted 6 lines
(quoted diff) +2
-0@@ -404,6 +406,8 @@ int disable_nonboot_cpus(void) break; } } + /* ensure all CPUs have gone into wait-for-SIPI */ + tboot_wait_for_aps(num_cpus);That looks like something that should be done generically. Wait for the other CPUs to be "dead".
The reason for this is because we really need to wait for tboot to put them into VMX mini-guests before we continue. There is enough additional overhead on the call into tboot and then into these guests to require this wait instead of just using the kernel's notion of dead (i.e. when they call into tboot_shutdown()).
Quoted 10 lines
Review int disable_nonboot_cpus(void)
Quoted 8 lines
(quoted diff) +8
-0+config INTEL_TXT + bool "Enable Intel(R) Trusted Execution Technology (Intel(R) TXT)" + depends on EXPERIMENTAL && X86 + help + This option enables support for booting the kernel with + the Trusted Boot (tboot) module. This will utilize + Intel(R) Trusted Execution Technology to perform a + measured launch of the kernel.One-two sentences here what value a a measured launch brings to the user.
Done.
Quoted 4 lines
Quoted 4 lines
If the system does not
(quoted diff) +3
-0+ support Intel(R) TXT, this will have no effect. + + See <http://www.intel.com/technology/security/> for more
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ information about Intel(R) TXT. + And see <http://tboot.sourceforge.net> for more information + about tboot. + + If you are unsure as to whether this is required, answer N.Putting the howto you had in the intro somewhere in
Documentation/*and adding a pointer to it here would be a good idea.
OK.
Quoted 5 lines
-Andi
--
[email protected] -- Speaking for myself only.
Thanks for your comments.
Joe & Shane
Quoted 11 lines · Joseph
Quoted 10 lines
arch/x86/include/asm/tboot.h line 62
Quoted 8 lines
arch/x86/include/asm/tboot.h renamed from linux.trees.git/arch/x86/include/asm/tboot.h +8
-0+/* GAS - Generic Address Structure (ACPI 2.0+) */ +struct tboot_acpi_generic_address { +u8 space_id; +u8 bit_width; +u8 bit_offset; +u8 access_width; +u64 address; +} __attribute__ ((__packed__ ));Why not use the existing structure from ACPI?
Quoted 1 line
This is the ACPI structure, but just not the Linux type definition of it. This is because tboot needs to work with multiple target kernels/VMMs and so it must define its parameters "independently".
But it's the same type. Or are you saying that include file is shared with another project? Why can't tboot include the ACPICA include files? If it's shared with another project there should be a clear comment about that.
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
arch/x86/include/asm/tboot.h renamed from linux.trees.git/arch/x86/include/asm/tboot.h +8
-0+struct tboot_acpi_sleep_info { +struct tboot_acpi_generic_address pm1a_cnt_blk; +struct tboot_acpi_generic_address pm1b_cnt_blk; +struct tboot_acpi_generic_address pm1a_evt_blk; +struct tboot_acpi_generic_address pm1b_evt_blk; +u16 pm1a_cnt_val; +u16 pm1b_cnt_val; +u64 wakeup_vector;
Quoted 3 lines
Quoted 1 line
So that vector can be >4GB but the tshared data structure not?
Neither can currently be 64b in practice, though now both types will be 64b to allow for future "expansion".
Seems pointless when legacy implementations like this one cannot deal with that.
Quoted 14 lines
Quoted 13 lines
Review
Quoted 11 lines
- /* Stop the cpus and apics */
#ifdefCONFIG_SMP(quoted diff) +5
-1- /* The boot cpu is always logical cpu 0 */ int reboot_cpu_id = 0; +#endif + + /* TXT requires VMX to be off for all shutdowns */ + if (tboot_in_measured_env()) + emergency_vmx_disable_all();I thought KVM did that already in its shutdown handler? Why is this needed again?
Quoted 1 line
It does in most shutdown cases, but not for reboot.
I think you should fix KVM then, not hack around it here.
Quoted 1 line
It was needed to ensure that all of the page table creation writes were getting flushed before paging got disabled by tboot.
Hmm weird. Normally caches should be physical anyways.
Quoted 1 line
Without it we were getting a PREEMPT SMP
DEBUG_PAGEALLOCwarning.
Sounds strange. Is that back by some requirement in the SDM? Perhaps it's just a race of some sort and you're masking it by wbinvd being very slow.
Quoted 1 line
I will add a comment to this effect.
I would double check that. Does a udelay(10) fix it too?
Quoted 3 lines
Quoted 1 line
for that?
This code will only be executed if the
tboot_sharedpage is found, which should not be the case if the kernel is running paravirtualized. But I will change it to justwrite_cr3()to be more consistent with other cases where we don't try to specifically exclude paravirt hooks.
I would also recommend to a add an explicit check for para virtualization somewhere and bail out.
Quoted 19 lines
Quoted 8 lines
arch/x86/kernel/tboot.c line 186
Quoted 2 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +2
-0+/* Reuse the original kernel mapping */ + tboot_pg_dir =pgd_alloc (&tboot_mm);That puts the PGD into the
pgd_listand thenpageattr.cwill actually walk that list and change ptes, assuming it's a standard kernel mapping. Can you tolerate that? It seems dangerous. Better to use apgd_alloc_kernel(). There's none, but you could add one.from a followup email of Andi's:
Quoted 10 lines
Thought some more about this. I think you're ok on 64bit at least because the kernel mappings are elsewhere from the identity map and keeping them in sync with pageattr makes sense and avoids illegal cache attribute aliases.
But on 32bit it could be potentially a problem in general. e.g. what happens when the tboot shared page is in the area the kernel is running? You would crash during the window where you run in that pgd.
Quoted 4 lines
Quoted 2 lines
It would be probably safer to use a low memory trampoline supplied by the kernel too that then loads the new pgd.
I don't think that I understand the issue. The tboot physical addr range will always be <4GB and thus will it's virtual 1:1 mapping. Immediately after switching to this pgd the CPU calls into tboot which will then disable paging and do its thing. Can you elaborate on what the exact issue would be?
On 32bit the kernel direct mapping is virtually < 4GB. So when tboot happens to put some data into the usual 0xc0000... range the kernel lives in you would have conflicting mappings for the short period you're still executing in the kernel mapping.
Quoted 4 lines
Quoted 2 lines
So likely you can just drop the spinlock. Perhaps add a
WARN_ON()somewhere for parallel entry using a simple flag.Per above, we will allocate early and then don't need the spinlock.
Just make sure early panics before your initialization are covered.
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +4
-0+PFN_PHYS (PFN_DOWN (virt_to_phys (&_text))); + tboot_shared->mac_regions[2 ].size = +PFN_PHYS (PFN_UP (virt_to_phys (&_end))) - +PFN_PHYS (PFN_DOWN (virt_to_phys (&_text)));Quoted 8 lines
arch/x86/kernel/tboot.c line 251
Quoted 5 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +5
-0+ } + + tboot_shared->shutdown_type = shutdown_type; + +switch_to_tboot_pt ();What happens with the NMIs? (nmi watchdog etc.) Are they all disabled at this point? Machine checks probably too.
Quoted 1 line
The TXT-related code doesn't disable them. Doesn't Linux disable them before shutting down?
No, but the direct shutdown code tends to load a special IDT
Thinking of it the BIOS/ACPI shutdowns might be buggy this way.
But your case is worse than any of them because you likely need longer so the race window is larger.
I would recommend to load a special IDT before entering a special context to avoid all that.
Quoted 1 line
We can probably remove the
BUG(), since the call itself can't fail and by the time the tboot code could get into a bad state that caused an un-matched ret, the kernel's page table would be long gone and it would never get back here. But as any execution beyond the call would be catastrophic, it is actually desirable that things blow up rather than fail insecurely--so I'm inclined to leave it or replace it with something equally fatal (?).
It'll be a triple fault, these tend to be hard to debug. Better just loop.
Quoted 4 lines
Quoted 2 lines
Normally using u64. i would add some sanity checks for out of bounds etc. Normally it's a good idea to sanity check anything coming from the BIOS.
Changed to u64. This data was already validated by tboot and should not have been altered since then.
Assuming nothing ever corrupts memory?
What happens when it's wrong?
Quoted 5 lines
Quoted 3 lines
Also isn't this reinventing ACPI table parser code? Perhaps the ACPI code could be used directly instead. or is this too early?
This is a copy of the ACPI table that was made during the launch, placed into DMA-protected memory, and validated for correctness. So that is why we want to get it from here.
Still seems like a lot of reinvention. It would be better if you could integrate that with ACPI more cleanly.
Quoted 11 lines
Quoted 9 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ tboot_shared->acpi_sinfo.pm1b_cnt_val = PM1Bcontrol; + /* we need phys addr of waking vector, but can't use + virt_to_phys() on &acpi_gbl_FACS because it is ioremap'ed, + so calc from FACS phys addr */ + tboot_shared->acpi_sinfo.wakeup_vector = acpi_gbl_FADT.facs + + ((void *)&acpi_gbl_FACS->firmware_waking_vector - + (void *)acpi_gbl_FACS);That's an opencoded offsetof ?
I'm not sure I understand what you mean.
I meant it should use offsetof(), not opencoding it.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only.
Quoted 15 lines
From: Andi Kleen [mailto:[email protected]]
Sent: Saturday, April 25, 2009 8:56 AMQuoted 11 lines · Joseph
Quoted 10 lines
arch/x86/include/asm/tboot.h line 62
Quoted 8 lines
arch/x86/include/asm/tboot.h renamed from linux.trees.git/arch/x86/include/asm/tboot.h +8
-0+/* GAS - Generic Address Structure (ACPI 2.0+) */ +struct tboot_acpi_generic_address { +u8 space_id; +u8 bit_width; +u8 bit_offset; +u8 access_width; +u64 address; +} __attribute__ ((__packed__ ));Why not use the existing structure from ACPI?
Quoted 8 lines
Quoted 1 line
This is the ACPI structure, but just not the Linux type definition of it. This is because
tboot needs to work with multiple target kernels/VMMs and so it must define its parameters "independently".
But it's the same type. Or are you saying that include file is shared with another project? Why can't tboot include the ACPICA include files? If it's shared with another project there should be a clear comment about that.
It's the same type today, but what if Linux adds a field to the structure or the new ACPI spec changes it? That would cause a silent failure with a tboot built for the older version. This header file is a Linux-ified version of the one in the tboot project that also gets copied into any other VMM/kernel that uses tboot (e.g. Xen), so I'd prefer to keep this definition and do explicit field population in the code.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
arch/x86/include/asm/tboot.h renamed from linux.trees.git/arch/x86/include/asm/tboot.h +8
-0+struct tboot_acpi_sleep_info { +struct tboot_acpi_generic_address pm1a_cnt_blk; +struct tboot_acpi_generic_address pm1b_cnt_blk; +struct tboot_acpi_generic_address pm1a_evt_blk; +struct tboot_acpi_generic_address pm1b_evt_blk; +u16 pm1a_cnt_val; +u16 pm1b_cnt_val; +u64 wakeup_vector;
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
So that vector can be >4GB but the tshared data structure not?
Neither can currently be 64b in practice, though now both types will be 64b to allow for
future "expansion".
Seems pointless when legacy implementations like this one cannot deal with that.
The wakeup vector limitation is not TXT architectural but rather a limitation of the current tboot code. So making it 64b will allow a future tboot without such limitations to use the same structure.
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Review
Quoted 10 lines
- /* Stop the cpus and apics */
#ifdefCONFIG_SMP(quoted diff) +5
-1- /* The boot cpu is always logical cpu 0 */ int reboot_cpu_id = 0; +#endif + + /* TXT requires VMX to be off for all shutdowns */ + if (tboot_in_measured_env()) + emergency_vmx_disable_all();I thought KVM did that already in its shutdown handler? Why is this needed again?
Quoted 3 lines
Quoted 1 line
It does in most shutdown cases, but not for reboot.
I think you should fix KVM then, not hack around it here.
OK, we found the issue in KVM and will submit that as a separate patch.
Quoted 10 lines
Quoted 1 line
It was needed to ensure that all of the page table creation writes were getting flushed
before paging got disabled by tboot.
Hmm weird. Normally caches should be physical anyways.
Quoted 1 line
Without it we were getting a PREEMPT SMP
DEBUG_PAGEALLOCwarning.Sounds strange. Is that back by some requirement in the SDM? Perhaps it's just a race of some sort and you're masking it by wbinvd being very slow.
Adding wbinvd was suggested by Venki Pallipadi. But now that the pagetable allocation has been moved, this may no longer be necessary. I have removed it and there doesn't seem to be any issues.
Quoted 4 lines
Quoted 1 line
I will add a comment to this effect.
I would double check that. Does a udelay(10) fix it too?
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
for that?
This code will only be executed if the
tboot_sharedpage is found, which should not be thecase if the kernel is running paravirtualized. But I will change it to just
write_cr3()to be more consistent with other cases where we don't try to specifically exclude paravirt hooks.I would also recommend to a add an explicit check for para virtualization somewhere and bail out.
I'll add that to the tboot_probe() fn.
Quoted 23 lines
Quoted 19 lines
Quoted 8 lines
arch/x86/kernel/tboot.c line 186
Quoted 2 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +2
-0+/* Reuse the original kernel mapping */ + tboot_pg_dir =pgd_alloc (&tboot_mm);That puts the PGD into the
pgd_listand thenpageattr.cwill actually walk that list and change ptes, assuming it's a standard kernel mapping. Can you tolerate that? It seems dangerous. Better to use apgd_alloc_kernel(). There's none, but you could add one.from a followup email of Andi's:
Quoted 10 lines
Thought some more about this. I think you're ok on 64bit at least because the kernel mappings are elsewhere from the identity map and keeping them in sync with pageattr makes sense and avoids illegal cache attribute aliases.
But on 32bit it could be potentially a problem in general. e.g. what happens when the tboot shared page is in the area the kernel is running? You would crash during the window where you run in that pgd.
Quoted 4 lines
Quoted 2 lines
It would be probably safer to use a low memory trampoline supplied by the kernel too that then loads the new pgd.
I don't think that I understand the issue. The tboot physical addr range will always be
Quoted 8 lines
<4GB and thus will it's virtual 1:1 mapping. Immediately after switching to this pgd the CPU calls into tboot which will then disable paging and do its thing. Can you elaborate on what the exact issue would be?
On 32bit the kernel direct mapping is virtually < 4GB. So when tboot happens to put some data into the usual
0xc0000... range the kernel lives in you would have conflicting mappings for the short period you're still executing in the kernel mapping.
tboot lives at a fixed address of 0x0800000 (8MB) and marks its region of memory as E820_UNUSABLE to avoid the kernel creating any mappings to it. We have used the current method on 32b kernels and even enabled CONFIG_HIGHPTE and there is no conflict. Under what configurations would this issue arise?
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
So likely you can just drop the spinlock. Perhaps add a
WARN_ON()somewhere for parallel entry using a simple flag.Per above, we will allocate early and then don't need the spinlock.
Just make sure early panics before your initialization are covered.
Will do.
Quoted 17 lines
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +4
-0+PFN_PHYS (PFN_DOWN (virt_to_phys (&_text))); + tboot_shared->mac_regions[2 ].size = +PFN_PHYS (PFN_UP (virt_to_phys (&_end))) - +PFN_PHYS (PFN_DOWN (virt_to_phys (&_text)));Quoted 8 lines
arch/x86/kernel/tboot.c line 251
Quoted 5 lines
arch/x86/kernel/tboot.c renamed from linux.trees.git/arch/x86/kernel/tboot.c +5
-0+ } + + tboot_shared->shutdown_type = shutdown_type; + +switch_to_tboot_pt ();What happens with the NMIs? (nmi watchdog etc.) Are they all disabled at this point? Machine checks probably too.
Quoted 1 line
The TXT-related code doesn't disable them. Doesn't Linux disable them before shutting down?
No, but the direct shutdown code tends to load a special IDT
Quoted 7 lines
Thinking of it the BIOS/ACPI shutdowns might be buggy this way.
But your case is worse than any of them because you likely need longer so the race window is larger.
I would recommend to load a special IDT before entering a special context to avoid all that.
Since the tboot code will install an IDT just after entry to the shutdown code, I think that the window would be extremely small (since the 1:1 pagetable we switch to includes the kernel mappings).
Quoted 7 lines
Quoted 1 line
We can probably remove the
BUG(), since the call itself can't fail and by the time the tbootcode could get into a bad state that caused an un-matched ret, the kernel's page table would be long gone and it would never get back here. But as any execution beyond the call would be catastrophic, it is actually desirable that things blow up rather than fail insecurely--so I'm inclined to leave it or replace it with something equally fatal (?).
It'll be a triple fault, these tend to be hard to debug. Better just loop.
OK.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Normally using u64. i would add some sanity checks for out of bounds etc. Normally it's a good idea to sanity check anything coming from the BIOS.
Changed to u64. This data was already validated by tboot and should not have been altered
since then.
Assuming nothing ever corrupts memory?
What happens when it's wrong?
Then it would really be no different than if something, after the kernel started booting, corrupted the kernel's memory. Is there some function that kernel code calls to validate pointers before it uses them?
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Also isn't this reinventing ACPI table parser code? Perhaps the ACPI code could be used directly instead. or is this too early?
This is a copy of the ACPI table that was made during the launch, placed into DMA-protected
memory, and validated for correctness. So that is why we want to get it from here.
Still seems like a lot of reinvention. It would be better if you could integrate that with ACPI more cleanly.
This copy of the table is done by the TXT launch process. All that this code is doing is locating it and pointing the IOMMU code at it. I don't think that I can make it any simpler.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
(quoted diff) +7
-0+ tboot_shared->acpi_sinfo.pm1b_cnt_val = PM1Bcontrol; + /* we need phys addr of waking vector, but can't use + virt_to_phys() on &acpi_gbl_FACS because it is ioremap'ed, + so calc from FACS phys addr */ + tboot_shared->acpi_sinfo.wakeup_vector = acpi_gbl_FADT.facs + + ((void *)&acpi_gbl_FACS->firmware_waking_vector - + (void *)acpi_gbl_FACS);
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
That's an opencoded offsetof ?
I'm not sure I understand what you mean.
I meant it should use
offsetof(), not opencoding it.
OK, I see.