-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathsvsm.rs
More file actions
executable file
·736 lines (629 loc) · 25.8 KB
/
Copy pathsvsm.rs
File metadata and controls
executable file
·736 lines (629 loc) · 25.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright (c) 2022-2023 SUSE LLC
//
// Author: Joerg Roedel <jroedel@suse.de>
#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_main)]
extern crate alloc;
use bootdefs::kernel_launch::KernelLaunchInfo;
use bootdefs::kernel_launch::LOWMEM_END;
use bootdefs::platform::SvsmPlatformType;
use core::arch::global_asm;
use core::panic::PanicInfo;
use core::ptr::NonNull;
use svsm::address::{Address, PhysAddr, VirtAddr};
#[cfg(feature = "attest")]
use svsm::attest::AttestationDriver;
use svsm::boot_params::BootParamBox;
use svsm::boot_params::BootParams;
use svsm::console::install_console_logger;
use svsm::cpu::control_regs::{cr0_init, cr4_init};
use svsm::cpu::cpuid::dump_cpuid_table;
use svsm::cpu::features::{Feature, cpu_has_feat};
use svsm::cpu::gdt::GLOBAL_GDT;
use svsm::cpu::idt::svsm::{early_idt_init, idt_init};
use svsm::cpu::idt::{EARLY_IDT_ENTRIES, IDT, IdtEntry};
use svsm::cpu::percpu::{PERCPU_AREAS, PerCpu, cpu_idle_loop, this_cpu, try_this_cpu};
use svsm::cpu::shadow_stack::{
MODE_64BIT, S_CET, SCetFlags, set_cet_ss_enabled, shadow_stack_info,
};
use svsm::cpu::smp::start_secondary_cpus;
use svsm::cpu::sse::sse_init;
use svsm::debug::gdbstub::svsm_gdbstub::{debug_break, gdbstub_start};
use svsm::debug::stacktrace::print_stack;
use svsm::debug::symbols::init_symbols;
use svsm::enable_shadow_stacks;
#[cfg(feature = "virtio-drivers")]
use svsm::error::SvsmError;
use svsm::fs::{initialize_fs, populate_ram_fs};
use svsm::hyperv::hyperv_setup;
use svsm::kernel_region::expand_kernel_heap;
use svsm::kernel_region::new_kernel_region;
use svsm::mm::FixedAddressMappingRange;
use svsm::mm::PageBox;
use svsm::mm::TransitionPageTable;
use svsm::mm::alloc::{free_multiple_pages, memory_info, print_memory_info, root_mem_init};
use svsm::mm::global_memory::init_global_ranges;
use svsm::mm::init_kernel_mapping_info;
use svsm::mm::memory::init_memory_map;
use svsm::mm::pagetable::PageTable;
use svsm::mm::pagetable::paging_init;
use svsm::mm::ro_after_init::make_ro_after_init;
use svsm::mm::validate::init_valid_bitmap;
use svsm::mm::virtualrange::virt_log_usage;
use svsm::platform::PageValidateOp;
use svsm::platform::PlatformPageType;
use svsm::platform::SVSM_PLATFORM;
use svsm::platform::SvsmPlatform;
use svsm::platform::SvsmPlatformCell;
use svsm::platform::init_capabilities;
use svsm::platform::init_platform_type;
use svsm::sev::secrets_page_mut;
use svsm::svsm_paging::enumerate_early_boot_regions;
use svsm::svsm_paging::invalidate_early_boot_memory;
use svsm::task::{KernelThreadStartInfo, schedule_init, start_kernel_task};
use svsm::types::PAGE_SIZE;
use svsm::utils::MemoryRegion;
use svsm::utils::ScopedMut;
use svsm::utils::round_to_pages;
#[cfg(all(feature = "virtio-drivers", any(feature = "block", feature = "vsock")))]
use svsm::virtio::probe_mmio_slots;
#[cfg(all(feature = "vtpm-persist", not(feature = "vsock"), not(test)))]
use svsm::vtpm::MockTransport;
#[cfg(all(feature = "vtpm-persist", feature = "vsock", not(test)))]
use svsm::vtpm::VsockTransport;
#[cfg(all(feature = "vtpm", not(feature = "vtpm-persist"), not(test)))]
use svsm::vtpm::vtpm_init;
#[cfg(all(feature = "vtpm-persist", not(test)))]
use svsm::vtpm::{SealedBlobStore, VsockHostStore, VtpmBootMode, vtpm_init_sealed};
#[cfg(all(feature = "vtpm-persist", feature = "vsock", not(test)))]
use svsm::vtpm::{VSOCK_HOST_CID, VSOCK_TPM_PORT};
use alloc::string::String;
#[cfg(all(feature = "vtpm-persist", not(test)))]
use alloc::vec::Vec;
use release::COCONUT_VERSION;
#[cfg(feature = "attest")]
use kbs_types::Tee;
unsafe extern "C" {
static bsp_stack: u64;
static bsp_stack_end: u64;
}
/*
* Launch protocol:
*
* The boot loader will map and load the svsm binary image and jump to
* startup_64.
*
* %rdi Pointer to the KernelLaunchInfo structure
* %rsi Kernel stack pointer
* %rdx Kernel stack limit
* %rcx Kernel page tables
*/
global_asm!(
r#"
.section .text
.section ".startup.text","ax"
.code64
.globl startup_64
startup_64:
/* Upon entry, the stack pointer is correctly set but the initial page
* tables are not guaranteed to be set correctly. Switch to the
* correct page tables */
popq %r15
movq %r15, %cr3
/* Mark the next stack frame as the bottom frame */
xor %rbp, %rbp
/* Capture the start parameter registers from the stack. The platform
* type is in rax and not the stack. */
popq %rdi
movq %rax, %rsi
/* Capture the stack bounds into global variables. The stack limit
* (the low address) is the last value popped off the stack, and once
* it has been popped, the stack pointer will represent the stack base
* (the high address) */
leaq bsp_stack(%rip), %r15
popq %r14
movq %r14, (%r15)
leaq bsp_stack_end(%rip), %r15
movq %rsp, (%r15)
/*
* Make sure (%rsp + 8) is 16b-aligned when control is transferred
* to svsm_entry as required by the C calling convention for x86-64.
*/
call svsm_entry
int3
.bss
.align {PAGE_SIZE}
.globl bsp_stack
bsp_stack:
.quad 0
.globl bsp_stack_end
bsp_stack_end:
.quad 0
"#,
PAGE_SIZE = const PAGE_SIZE,
options(att_syntax)
);
/// # Safety
/// The launch info block must correctly specify the initial state of the
/// heap.
unsafe fn memory_init(
launch_info: &mut KernelLaunchInfo,
platform: &dyn SvsmPlatform,
platform_type: SvsmPlatformType,
) {
let mut full_kernel_region = None;
// Determine the size of the full kernel region as determined from the
// boot parameters, including the initial memory map.
let heap_adjust_pages: isize = if launch_info.vmsa_in_kernel_heap
&& (platform_type == SvsmPlatformType::Snp)
{
// On an SNP system, the VMSA might be located within the kernel heap
// area. If so, it is at the last page within the heap range, so the
// heap range must be reduced so there is no attempt to validate the
// VMSA page and so that the VMSA page is not reallocated. If this
// VMSA is present, then the heap cannot be dynamically expanded.
-1
} else {
// SAFETY: The launch info block is trusted to hold a valid virtual
// address for the boot parameters.
let boot_params =
unsafe { BootParams::new(VirtAddr::from(launch_info.boot_params_virt_addr)).unwrap() };
let kernel_region = boot_params
.find_kernel_region()
.expect("Failed to find memory region for SVSM kernel");
full_kernel_region = Some(kernel_region);
let region_end = u64::from(kernel_region.end());
((region_end - launch_info.kernel_region_phys_end) as usize / PAGE_SIZE) as isize
};
// Calculate the physical and virtual bounds of the kernel heap, which sits
// within the direct map.
let heap_vstart = launch_info.kernel_direct_map_vaddr + launch_info.heap_area_offset;
let heap_vaddr = VirtAddr::from(heap_vstart);
let heap_pstart =
PhysAddr::from(launch_info.kernel_region_phys_start + launch_info.heap_area_offset);
let mut heap_pend = PhysAddr::from(launch_info.kernel_region_phys_end);
let mut heap_allocated = launch_info.heap_area_allocated as usize;
let mut heap_length = (heap_pend - heap_pstart) / PAGE_SIZE;
// Reduce the heap size if required.
if heap_adjust_pages < 0 {
heap_length -= (-heap_adjust_pages) as usize;
assert!(heap_length >= heap_allocated);
}
// Unallocated heap memory has not already been accepted so it must be
// accepted here.
if heap_allocated < heap_length {
// SAFETY: the launch info is assumed to correctly reflect the set of
// pages that were accepted as part of the boot image.
unsafe {
platform
.validate_virtual_page_range(
MemoryRegion::new(
heap_vaddr + heap_allocated * PAGE_SIZE,
(heap_length - heap_allocated) * PAGE_SIZE,
),
PageValidateOp::Validate,
)
.expect("Failed to validate heap memory");
}
}
// Expand the heap if required.
if heap_adjust_pages > 0 {
// Call out to perform the expansion. Memory may be consumed from the
// heap as part of the expansion
// SAFETY: The caller guarantees that the inital state of the kernel
// heap is accurately captured by the launch information.
heap_allocated = unsafe {
expand_kernel_heap(
launch_info,
full_kernel_region.as_ref().unwrap().end(),
platform,
)
};
// Recalculate the heap size based on hoe much the kernel region
// was actually expanded. This may be less than the requested size
// if it was rounded down to a 2 MB boundary.
heap_pend = PhysAddr::from(launch_info.kernel_region_phys_end);
heap_length = (heap_pend - heap_pstart) / PAGE_SIZE;
}
let heap_vregion = MemoryRegion::new(heap_vaddr, heap_length * PAGE_SIZE);
// Establish the heap region as the fixed kernel mapping region.
let kernel_mapping =
FixedAddressMappingRange::new(heap_vregion.start(), heap_vregion.end(), heap_pstart);
init_kernel_mapping_info(kernel_mapping, None);
// Initialize the page heap itself.
root_mem_init(
heap_pstart,
heap_vregion.start(),
heap_length,
heap_allocated,
);
// Now that all memory in teh kernel range has been validated, initialize
// the valid memory bitmap.
init_valid_bitmap(new_kernel_region(launch_info), true)
.expect("Failed to allocate valid-bitmap");
}
fn boot_stack_info() {
let bs = this_cpu().get_current_stack();
log::info!("Boot stack @ {bs:#018x}");
}
/// Probes for VirtIO MMIO devices and initializes them.
///
/// # Returns
///
/// Returns Ok if initialization is successful or no virtio devices are found
/// Returns an error when a virtio device is found but its driver initialization fails.
#[cfg(feature = "virtio-drivers")]
fn initialize_virtio_mmio(_boot_params: &BootParams<'_>) -> Result<(), SvsmError> {
#[cfg(any(feature = "block", feature = "vsock"))]
let mut slots = probe_mmio_slots(_boot_params);
#[cfg(feature = "block")]
{
use svsm::block::virtio_blk::initialize_block;
initialize_block(&mut slots)?;
}
#[cfg(feature = "vsock")]
{
use svsm::vsock::virtio_vsock::initialize_vsock;
initialize_vsock(&mut slots)?;
}
Ok(())
}
/// # Safety
/// The caller must pass a valid pointer from the kernel heap as the launch
/// info pointer.
unsafe fn svsm_start(
li: *mut KernelLaunchInfo,
platform_type: SvsmPlatformType,
) -> Option<VirtAddr> {
// SAFETY: the caller guarantees the correctness of the launch info
// pointer.
let mut launch_info = unsafe { ScopedMut::<KernelLaunchInfo>::new(li).unwrap() };
init_platform_type(platform_type);
GLOBAL_GDT.load_selectors();
let mut early_idt = [IdtEntry::default(); EARLY_IDT_ENTRIES];
let mut idt = IDT::new(&mut early_idt);
// SAFETY: the IDT here will remain in scope until the full IDT is
// initialized later, and thus can safely be used as the early IDT.
unsafe {
early_idt_init(&mut idt);
}
// Capture the debug serial port before the launch info disappears from
// the address space.
let debug_serial_port = launch_info.debug_serial_port;
let mut platform_cell = SvsmPlatformCell::new(launch_info.suppress_svsm_interrupts);
let platform = platform_cell.platform_mut();
// SAFETY: the CPUID and secrets page addresses were allocated in the
// kernel heap and the addresses in the loader block are trusted.
unsafe {
platform.initialize_platform_page(
PlatformPageType::Cpuid,
VirtAddr::from(launch_info.cpuid_page),
);
platform.initialize_platform_page(
PlatformPageType::Secrets,
VirtAddr::from(launch_info.secrets_page),
);
}
cr0_init();
cr4_init();
install_console_logger("SVSM").expect("Console logger already initialized");
platform
.env_setup(debug_serial_port, launch_info.vtom.try_into().unwrap())
.expect("Early environment setup failed");
log::info!("CPU vendor {:?}", platform.get_cpu_vendor());
// Load symbol info now that there is a console
// SAFETY: the launch info here is the launch info passed by the boot
// environment.
unsafe {
init_symbols(&launch_info).expect("Could not initialize kernel symbols");
}
paging_init(platform, false).expect("Failed to initialize paging");
// SAFETY: THe launch info is assumed to correctly specify the initial
// state of memory.
unsafe {
memory_init(launch_info.as_mut(), platform, platform_type);
}
// SAFETY: the current page table was was placed into the kernel heap as
// part of the boot image and therefore it can be built into a PageBox.
let init_pgtable: PageBox<PageTable> = unsafe {
let page_table_ptr = (launch_info.kernel_page_table_vaddr as usize) as *mut PageTable;
PageBox::from_raw(NonNull::new(page_table_ptr).unwrap())
};
init_global_ranges();
// SAFETY: this is the first CPU, so there can be no other dependencies
// on multi-threaded access to the per-cpu areas.
let percpu_shared = unsafe { PERCPU_AREAS.create_new(0) };
let bsp_percpu = PerCpu::alloc(percpu_shared).expect("Failed to allocate BSP per-cpu data");
bsp_percpu
.setup(platform, init_pgtable)
.expect("Failed to setup BSP per-cpu area");
bsp_percpu
.setup_on_cpu(platform)
.expect("Failed to run percpu.setup_on_cpu()");
bsp_percpu.load();
// Now the stack unwinder can be used
// SAFETY: the stack addresses were initialized during kernel entry and
// are known to be correct at this point.
unsafe {
bsp_percpu.set_current_stack(MemoryRegion::from_addresses(
VirtAddr::from(bsp_stack),
VirtAddr::from(bsp_stack_end),
));
}
// SAFETY: the boot loader is trusted to allocate an appropriate virtual
// address for the IDT page.
unsafe {
idt_init(VirtAddr::from(launch_info.idt_vaddr), platform).expect("Failed to set up IDT");
}
initialize_fs();
// Idle task must be allocated after PerCPU data is mapped
// SAFETY: the pointer to the launch information is the correct start
// parameter for the startup routine.
let start_info = unsafe { KernelThreadStartInfo::new_unsafe(svsm_main, li as usize) };
bsp_percpu
.setup_bsp_idle_task(start_info)
.expect("Failed to allocate idle task for BSP");
platform
.env_setup_late(debug_serial_port)
.expect("Late environment setup failed");
dump_cpuid_table();
let mem_info = memory_info();
print_memory_info(&mem_info);
boot_stack_info();
platform
.configure_alternate_injection(launch_info.use_alternate_injection)
.expect("Alternate injection required but not available");
platform_cell.global_init();
sse_init();
bsp_percpu.get_top_of_shadow_stack()
}
/// # Safety
/// Thus function must only be called from the entry from stage 2, where
/// the launch info parameter is known to have been allocated from the kernel
/// heap.
#[unsafe(no_mangle)]
unsafe extern "C" fn svsm_entry(li: *mut KernelLaunchInfo, platform_type: SvsmPlatformType) -> ! {
// SAFETY: the caller ensures that the launch info pointer is a valid
// pointer.
let ssp_token = unsafe { svsm_start(li, platform_type) };
// Shadow stacks must be enabled once no further function returns are
// possible.
if cpu_has_feat(Feature::CetSS) {
set_cet_ss_enabled();
let ssp_token_addr = ssp_token.unwrap();
enable_shadow_stacks!(ssp_token_addr);
}
shadow_stack_info();
// SAFETY: there is no current task running on this processor yet, so
// initializing the scheduler is safe.
unsafe {
schedule_init();
}
unreachable!("SVSM entry point terminated unexpectedly");
}
fn free_init_bsp_stack() {
// SAFETY: the stack base and limit addresses were initialized when the
// kernel was first started.
let (stack_base, stack_end) =
unsafe { (VirtAddr::from(bsp_stack), VirtAddr::from(bsp_stack_end)) };
let stack_pages = round_to_pages(stack_end - stack_base);
free_multiple_pages(stack_base, stack_pages);
}
fn svsm_init(launch_info: &KernelLaunchInfo) {
// If required, the GDB stub can be started earlier, just after the console
// is initialised in svsm_start() above.
gdbstub_start(&**SVSM_PLATFORM).expect("Could not start GDB stub");
// Uncomment the line below if you want to wait for
// a remote GDB connection
//debug_break();
// Invalidate low-memory page tables if required for consistency.
if launch_info.lowmem_page_table_count != 0 {
SVSM_PLATFORM
.invalidate_lowmem_page_tables(
launch_info.lowmem_page_table_paddr,
launch_info.lowmem_page_table_count as usize,
)
.expect("failed to invalidate low-memory page tables");
}
// Validate low memory if the launch info indicates that it has not yet
// been validated.
if !launch_info.lowmem_validated {
// SAFETY: the launch information is trusted to represent the
// validation state of memory, thus memory can safely be validated if
// the launch info declares that it is necessary.
unsafe {
SVSM_PLATFORM
.validate_low_memory(LOWMEM_END.into(), false)
.expect("failed to validate low 640 KB");
}
}
// Free the BSP stack that was allocated for early initialization.
free_init_bsp_stack();
// Free platform pages that were allocated but are not needed by the
// current platform.
// SAFETY: the virtual addresses of these pages are guaranteed to be
// correct in the loader block, and the platform guarantees that freeing
// the page is safe if the underlying page is not needed.
unsafe {
SVSM_PLATFORM.free_unused_platform_page(
PlatformPageType::Cpuid,
VirtAddr::from(launch_info.cpuid_page),
);
SVSM_PLATFORM.free_unused_platform_page(
PlatformPageType::Secrets,
VirtAddr::from(launch_info.secrets_page),
);
}
SVSM_PLATFORM
.env_setup_svsm()
.expect("SVSM platform environment setup failed");
hyperv_setup().expect("failed to complete Hyper-V setup");
let boot_params =
// SAFETY: the address in the launch info is known to be correct.
unsafe { BootParamBox::new(VirtAddr::from(launch_info.boot_params_virt_addr)) }
.expect("Invalid boot parameters");
init_memory_map(&boot_params, launch_info).expect("Failed to init guest memory map");
populate_ram_fs(launch_info.kernel_fs_start, launch_info.kernel_fs_end)
.expect("Failed to unpack FS archive");
init_capabilities();
let cpus = boot_params
.load_cpu_info()
.expect("Failed to load ACPI tables");
// Create a transition page table for use during CPU startup.
let transition_page_table =
// SAFETY: the address of the initial kernel page tables supplied in
// the launch info is trusted to be correct.
unsafe { TransitionPageTable::new() }.expect("Failed to create transition page table");
start_secondary_cpus(&**SVSM_PLATFORM, &cpus, &transition_page_table);
// Make ro_after_init section read-only
make_ro_after_init().expect("Failed to make ro_after_init region read-only");
let kernel_region = new_kernel_region(launch_info);
let early_boot_regions = enumerate_early_boot_regions(&boot_params, launch_info);
invalidate_early_boot_memory(&**SVSM_PLATFORM, &boot_params, &early_boot_regions)
.expect("Failed to invalidate early boot memory");
if let Err(e) = SVSM_PLATFORM.prepare_fw(&boot_params, kernel_region) {
panic!("Failed to prepare guest FW: {e:#?}");
}
#[cfg(feature = "virtio-drivers")]
initialize_virtio_mmio(&boot_params).expect("Failed to initialize virtio-mmio drivers");
#[cfg(feature = "attest")]
{
let mut proxy = AttestationDriver::try_from(Tee::Snp).unwrap();
let _data = proxy.attest().unwrap();
// Nothing to do with data at the moment, simply print a success message.
log::info!("attestation successful");
}
// Mainline COCONUT-SVSM behaviour: ephemeral vTPM, no external
// dependencies, fresh seeds on every cold boot. This is the path
// upstream consumers get and the one preserved across the
// `vtpm-persist` feature being off.
#[cfg(all(feature = "vtpm", not(feature = "vtpm-persist"), not(test)))]
{
vtpm_init().expect("vtpm_init failed");
log::info!("VTPM: ephemeral init complete");
}
// Persistent vTPM path — opt-in via the `vtpm-persist` feature.
// Drives the Provision/Recover seal cycle against an off-CVM TPM
// endpoint (transport) and host-side blob store, both currently
// reached over VSOCK. The host helper must be reachable: a
// missing/unreachable endpoint is a hard error, NOT a silent
// fallback to ephemeral — silently downgrading would void the
// security model the seal cycle promises, which treats the
// physical TPM as a separate root of trust (an attacker who can
// break the VSOCK channel would otherwise harvest a fresh vTPM
// identity by triggering the fallback).
#[cfg(all(feature = "vtpm-persist", not(test)))]
{
let store = VsockHostStore::new(
VSOCK_HOST_CID,
VsockHostStore::DEFAULT_LOAD_PORT,
VsockHostStore::DEFAULT_SAVE_PORT,
);
let blob_opt = store
.load()
.expect("VTPM: sealed-store load failed (host helper unreachable?)");
let (boot_mode, blob_slice) = match blob_opt.as_deref() {
Some(b) => (VtpmBootMode::Recover, Some(b)),
None => (VtpmBootMode::Provision, None),
};
let vm_id: [u8; 16] = [0u8; 16]; // TODO: derive from IGVM guest_context
let transport = VsockTransport::new(VSOCK_HOST_CID, VSOCK_TPM_PORT);
// R1: vtpm_init_sealed now uses an out-param for the Vec<u8>; its
// return type is the register-passable `Result<(), SvsmReqError>`.
let mut maybe_blob: Option<Vec<u8>> = None;
vtpm_init_sealed(transport, boot_mode, vm_id, blob_slice, &mut maybe_blob)
.expect("VTPM: sealed init failed (host TPM endpoint unreachable?)");
if let Some(blob_bytes) = maybe_blob {
store
.save(&blob_bytes)
.expect("VTPM: sealed-store save failed");
log::info!(
"VTPM: SealedBlob ({} bytes) stored for next boot",
blob_bytes.len()
);
}
log::info!("VTPM: sealed init complete (mode={boot_mode:?})");
}
virt_log_usage();
if let Err(e) = SVSM_PLATFORM.launch_fw(&boot_params) {
panic!("Failed to launch FW: {e:?}");
}
#[cfg(test)]
{
if boot_params.has_qemu_testdev() {
crate::testutils::set_has_qemu_testdev();
}
if boot_params.has_test_iorequests() {
crate::testutils::set_has_test_iorequests();
}
let _ = start_kernel_task(
KernelThreadStartInfo::new(test_in_svsm_task, 0),
String::from("SVSM test task"),
);
}
#[cfg(not(test))]
{
use svsm::fs::opendir;
use svsm::requests::request_loop_start;
use svsm::task::exec_user;
match exec_user("/init", opendir("/").expect("Failed to find FS root")) {
Ok(_) => (),
Err(e) => log::info!("Failed to launch /init: {e:?}"),
}
// Start request processing on this CPU if required.
if SVSM_PLATFORM.start_svsm_request_loop() {
start_kernel_task(
KernelThreadStartInfo::new(request_loop_start, 0),
String::from("request-loop on CPU 0"),
)
.expect("Failed to launch request loop task");
}
}
}
/// # Safety
/// The caller is required to ensure that the start parameter is a pointer to
/// a valid `KernelLaunchInfo` structure that was allocated from the kernel
/// heap..
pub unsafe fn svsm_main(li: usize) {
// SAFETY: the caller takes responsibility for the correctness of the
// pointer.
let launch_info = unsafe {
PageBox::<KernelLaunchInfo>::from_raw(NonNull::new(li as *mut KernelLaunchInfo).unwrap())
};
svsm_init(&launch_info);
// The launch info is no lnoger used and can be freed now.
drop(launch_info);
cpu_idle_loop(0);
}
#[cfg(test)]
fn test_in_svsm_task(_context: usize) {
crate::test_main();
}
#[panic_handler]
fn panic(info: &PanicInfo<'_>) -> ! {
if let Some(mut secrets_page) = secrets_page_mut() {
secrets_page.clear_vmpck(0);
secrets_page.clear_vmpck(1);
secrets_page.clear_vmpck(2);
secrets_page.clear_vmpck(3);
}
if let Some(cpu) = try_this_cpu() {
log::error!(
"Panic on CPU[{}]! COCONUT-SVSM Version: {COCONUT_VERSION}",
cpu.get_cpu_index(),
);
} else {
log::error!("Panic on CPU[?]! COCONUT-SVSM Version: {COCONUT_VERSION}");
}
log::error!("Info: {info}");
print_stack(3);
debug_break();
// If we are running tests, notify qemu. Otherwise, simply
// terminate the guest.
#[cfg(all(test, test_in_svsm))]
crate::testing::exit(crate::testing::QEMUExitValue::Fail);
#[cfg(any(not(test), not(test_in_svsm)))]
svsm::platform::terminate();
}