1pub mod config;
7
8#[cfg(guest_arch = "aarch64")]
9use aarch64 as arch;
10#[cfg(guest_arch = "x86_64")]
11use x86_64 as arch;
12
13pub use arch::CONFIG_BLOB_GPA_BASE;
14pub use arch::IMAGE_SIZE;
15pub use arch::load;
16
17use guid::Guid;
18use thiserror::Error;
19use zerocopy::FromBytes;
20use zerocopy::Immutable;
21use zerocopy::IntoBytes;
22use zerocopy::KnownLayout;
23
24const SEC_FIRMWARE_VOLUME_OFFSET: u64 = 0x005E0000;
29
30fn expand_3byte_integer(size: [u8; 3]) -> u64 {
32 ((size[2] as u64) << 16) + ((size[1] as u64) << 8) + size[0] as u64
33}
34
35const fn signature_16(v: &[u8; 2]) -> u16 {
36 v[0] as u16 | (v[1] as u16) << 8
37}
38
39const fn signature_32(v: &[u8; 4]) -> u32 {
40 v[0] as u32 | (v[1] as u32) << 8 | (v[2] as u32) << 16 | (v[3] as u32) << 24
41}
42
43const IMAGE_DOS_SIGNATURE: u16 = 0x5A4D; const IMAGE_NT_SIGNATURE: u32 = 0x00004550; const TE_IMAGE_HEADER_SIGNATURE: u16 = signature_16(b"VZ");
46const EFI_FVH_SIGNATURE: u32 = signature_32(b"_FVH");
47
48#[repr(C)]
49#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
50struct ImageDosHeader {
51 e_magic: u16, e_cblp: u16, e_cp: u16, e_crlc: u16, e_cparhdr: u16, e_minalloc: u16, e_maxalloc: u16, e_ss: u16, e_sp: u16, e_csum: u16, e_ip: u16, e_cs: u16, e_lfarlc: u16, e_ovno: u16, e_res: [u16; 4], e_oemid: u16, e_oeminfo: u16, e_res2: [u16; 10], e_lfanew: i32, }
71
72#[repr(C)]
73#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
74struct TeImageHeader {
75 signature: u16,
76 machine: u16,
77 number_of_sections: u8,
78 subsystem: u8,
79 stripped_size: u16,
80 address_of_entry_point: u32,
81 base_of_code: u32,
82 image_base: u64,
83 data_directory: [ImageDataDirectory; 2],
84}
85
86#[repr(C)]
87#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
88struct ImageNtHeaders32 {
89 signature: u32,
90 file_header: ImageFileHeader,
91 optional_header: ImageOptionalHeader32,
92}
93
94#[repr(C)]
95#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
96struct ImageFileHeader {
97 machine: u16,
98 number_of_sections: u16,
99 time_date_stamp: u32,
100 pointer_to_symbol_table: u32,
101 number_of_symbols: u32,
102 size_of_optional_header: u16,
103 characteristics: u16,
104}
105
106#[repr(C)]
107#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
108struct ImageOptionalHeader32 {
109 magic: u16,
110 major_linker_version: u8,
111 minor_linker_version: u8,
112 size_of_code: u32,
113 size_of_initialized_data: u32,
114 size_of_uninitialized_data: u32,
115 address_of_entry_point: u32,
116 base_of_code: u32,
117 base_of_data: u32,
118 image_base: u32,
119 section_alignment: u32,
120 file_alignment: u32,
121 major_operating_system_version: u16,
122 minor_operating_system_version: u16,
123 major_image_version: u16,
124 minor_image_version: u16,
125 major_subsystem_version: u16,
126 minor_subsystem_version: u16,
127 win32_version_value: u32,
128 size_of_image: u32,
129 size_of_headers: u32,
130 check_sum: u32,
131 subsystem: u16,
132 dll_characteristics: u16,
133 size_of_stack_reserve: u32,
134 size_of_stack_commit: u32,
135 size_of_heap_reserve: u32,
136 size_of_heap_commit: u32,
137 loader_flags: u32,
138 number_of_rva_and_sizes: u32,
139 data_directory: [ImageDataDirectory; 16],
140}
141
142#[repr(C)]
143#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
144struct ImageDataDirectory {
145 virtual_address: u32,
146 size: u32,
147}
148
149fn pe_get_entry_point_offset(pe32_data: &[u8]) -> Option<u32> {
150 let dos_header = ImageDosHeader::read_from_prefix(pe32_data).ok()?.0; let nt_headers_offset = if dos_header.e_magic == IMAGE_DOS_SIGNATURE {
152 dos_header.e_lfanew as usize
154 } else {
155 0
157 };
158
159 let signature = u32::read_from_prefix(&pe32_data[nt_headers_offset..])
160 .ok()?
161 .0; if signature as u16 == TE_IMAGE_HEADER_SIGNATURE {
166 let te = TeImageHeader::read_from_prefix(&pe32_data[nt_headers_offset..])
167 .ok()?
168 .0; Some(te.address_of_entry_point + size_of_val(&te) as u32 - te.stripped_size as u32)
170 } else if signature == IMAGE_NT_SIGNATURE {
171 let pe = ImageNtHeaders32::read_from_prefix(&pe32_data[nt_headers_offset..])
172 .ok()?
173 .0; Some(pe.optional_header.address_of_entry_point)
175 } else {
176 None
177 }
178}
179
180#[repr(C)]
181#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
182struct EFI_FIRMWARE_VOLUME_HEADER {
183 zero_vector: [u8; 16],
184 file_system_guid: Guid,
185 fv_length: u64,
186 signature: u32,
187 attributes: u32,
188 header_length: u16,
189 checksum: u16,
190 ext_header_offset: u16,
191 reserved: u8,
192 revision: u8,
193}
194
195#[repr(C)]
196#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
197struct EFI_FFS_FILE_HEADER {
198 name: Guid,
199 integrity_check: u16,
200 typ: u8,
201 attributes: u8,
202 size: [u8; 3],
203 state: u8,
204}
205
206const EFI_FV_FILETYPE_SECURITY_CORE: u8 = 3;
207
208#[repr(C)]
209#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
210struct EFI_COMMON_SECTION_HEADER {
211 size: [u8; 3],
212 typ: u8,
213}
214
215const EFI_SECTION_PE32: u8 = 0x10;
216
217fn get_sec_entry_point_offset(image: &[u8]) -> Option<u64> {
219 let mut image_offset = SEC_FIRMWARE_VOLUME_OFFSET;
221
222 let fvh = EFI_FIRMWARE_VOLUME_HEADER::read_from_prefix(&image[image_offset as usize..])
224 .ok()?
225 .0; if fvh.signature != EFI_FVH_SIGNATURE {
227 return None;
228 }
229
230 image_offset += fvh.header_length as u64;
232
233 let mut sec_core_file_header = None;
235 let mut volume_offset = 0;
236 while volume_offset < fvh.fv_length {
237 let new_volume_offset = (volume_offset + 7) & !7;
238 if new_volume_offset > volume_offset {
239 image_offset += new_volume_offset - volume_offset;
240 volume_offset = new_volume_offset;
241 }
242 let fh = EFI_FFS_FILE_HEADER::read_from_prefix(&image[image_offset as usize..])
243 .ok()?
244 .0; if fh.typ == EFI_FV_FILETYPE_SECURITY_CORE {
246 sec_core_file_header = Some(fh);
247 break;
248 }
249
250 image_offset += expand_3byte_integer(fh.size);
251 volume_offset += expand_3byte_integer(fh.size);
252 }
253
254 let sec_core_file_header = sec_core_file_header?;
256 let sec_core_file_size = expand_3byte_integer(sec_core_file_header.size);
257
258 image_offset += size_of::<EFI_FFS_FILE_HEADER>() as u64;
260 volume_offset += size_of::<EFI_FFS_FILE_HEADER>() as u64;
261
262 let mut file_offset = volume_offset;
264 while file_offset < sec_core_file_size {
265 let new_file_offset = (file_offset + 3) & !3;
269 if new_file_offset > file_offset {
270 image_offset += new_file_offset - file_offset;
271 volume_offset += new_file_offset - file_offset;
272 file_offset += new_file_offset - file_offset;
273 }
274
275 let sh = EFI_COMMON_SECTION_HEADER::read_from_prefix(&image[image_offset as usize..])
276 .ok()?
277 .0; if sh.typ == EFI_SECTION_PE32 {
279 let pe_offset = pe_get_entry_point_offset(
280 &image[image_offset as usize + size_of::<EFI_COMMON_SECTION_HEADER>()..],
281 )?;
282 image_offset += size_of::<EFI_COMMON_SECTION_HEADER>() as u64 + pe_offset as u64;
283 break;
284 }
285 image_offset += expand_3byte_integer(sh.size);
286 volume_offset += expand_3byte_integer(sh.size);
287 file_offset += expand_3byte_integer(sh.size);
288 }
289
290 Some(image_offset)
291}
292
293mod igvm {
295 use zerocopy::FromBytes;
296
297 use zerocopy::Immutable;
298 use zerocopy::IntoBytes;
299 use zerocopy::KnownLayout;
300
301 #[repr(C)]
303 #[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
304 pub struct UEFI_IGVM_PARAMETER_INFO {
305 pub parameter_page_count: u32,
306 pub cpuid_pages_offset: u32,
307 pub vp_context_page_number: u64,
308 pub loader_block_offset: u32,
309 pub command_line_offset: u32,
310 pub command_line_page_count: u32,
311 pub memory_map_offset: u32,
312 pub memory_map_page_count: u32,
313 pub madt_offset: u32,
314 pub madt_page_count: u32,
315 pub srat_offset: u32,
316 pub srat_page_count: u32,
317 pub maximum_processor_count: u32,
318 pub uefi_memory_map_offset: u32,
319 pub uefi_memory_map_page_count: u32,
320 }
321
322 pub const UEFI_IGVM_LOADER_BLOCK_NUMBER_OF_PROCESSORS_FIELD_OFFSET: usize = 0;
323}
324
325#[derive(Debug, Error)]
326pub enum Error {
327 #[error("Firmware size invalid")]
328 InvalidImageSize,
329 #[error("Unable to find SEC volume entry point")]
330 NoSecEntryPoint,
331 #[error("Invalid shared gpa boundary")]
332 InvalidSharedGpaBoundary,
333 #[error("Invalid config type")]
334 InvalidConfigType(String),
335 #[error("Importer error")]
336 Importer(#[source] anyhow::Error),
337}
338
339#[derive(Debug)]
340pub enum ConfigType {
341 ConfigBlob(config::Blob),
342 Igvm,
343 None,
344}
345
346#[derive(Debug)]
347pub struct LoadInfo {
348 pub firmware_base: u64,
350 pub firmware_size: u64,
352 pub total_size: u64,
355}
356
357pub mod x86_64 {
358 use super::ConfigType;
359 use super::Error;
360 use super::LoadInfo;
361 use crate::common::DEFAULT_GDT_SIZE;
362 use crate::common::import_default_gdt;
363 use crate::cpuid::HV_PSP_CPUID_PAGE;
364 use crate::importer::BootPageAcceptance;
365 use crate::importer::IgvmParameterType;
366 use crate::importer::ImageLoad;
367 use crate::importer::IsolationType;
368 use crate::importer::StartupMemoryType;
369 use crate::importer::X86Register;
370 use crate::uefi::SEC_FIRMWARE_VOLUME_OFFSET;
371 use crate::uefi::get_sec_entry_point_offset;
372 use hvdef::HV_PAGE_SIZE;
373 use page_table::IdentityMapSize;
374 use page_table::x64::align_up_to_page_size;
375 use page_table::x64::build_page_tables_64;
376 use zerocopy::FromZeros;
377 use zerocopy::IntoBytes;
378
379 pub const IMAGE_SIZE: u64 = 0x00600000; const IMAGE_GPA_BASE: u64 = 0x100000; const PAGE_TABLE_GPA_BASE: u64 = IMAGE_GPA_BASE + IMAGE_SIZE; const PAGE_TABLE_SIZE: u64 = HV_PAGE_SIZE * 6;
383 const GDT_GPA_BASE: u64 = PAGE_TABLE_GPA_BASE + PAGE_TABLE_SIZE; const MISC_PAGES_GPA_BASE: u64 = GDT_GPA_BASE + DEFAULT_GDT_SIZE; const MISC_PAGES_SIZE: u64 = HV_PAGE_SIZE * 2;
386 pub const CONFIG_BLOB_GPA_BASE: u64 = MISC_PAGES_GPA_BASE + MISC_PAGES_SIZE; pub fn load(
390 importer: &mut dyn ImageLoad<X86Register>,
391 image: &[u8],
392 config: ConfigType,
393 ) -> Result<LoadInfo, Error> {
394 if image.len() != IMAGE_SIZE as usize {
395 return Err(Error::InvalidImageSize);
396 }
397
398 let sec_entry_point = get_sec_entry_point_offset(image).ok_or(Error::NoSecEntryPoint)?;
399
400 let isolation = importer.isolation_config();
401
402 let (page_tables, shared_vis_page_tables) =
408 if isolation.isolation_type == IsolationType::Snp && !isolation.paravisor_present {
409 if let ConfigType::ConfigBlob(_) = config {
410 return Err(Error::InvalidConfigType(
411 "Enlightened UEFI must use IGVM parameters".into(),
412 ));
413 }
414
415 let shared_vis_page_table_gpa = CONFIG_BLOB_GPA_BASE + HV_PAGE_SIZE;
416 let shared_gpa_boundary_bits = isolation
417 .shared_gpa_boundary_bits
418 .ok_or(Error::InvalidSharedGpaBoundary)?;
419 let shared_gpa_boundary = 1 << shared_gpa_boundary_bits;
420
421 let shared_vis_page_tables = build_page_tables_64(
425 shared_vis_page_table_gpa,
426 shared_gpa_boundary,
427 IdentityMapSize::Size4Gb,
428 None,
429 );
430
431 let page_tables = build_page_tables_64(
432 PAGE_TABLE_GPA_BASE,
433 0,
434 IdentityMapSize::Size4Gb,
435 Some((shared_vis_page_table_gpa, shared_gpa_boundary)),
436 );
437
438 (page_tables, Some(shared_vis_page_tables))
439 } else {
440 let page_tables =
441 build_page_tables_64(PAGE_TABLE_GPA_BASE, 0, IdentityMapSize::Size4Gb, None);
442
443 (page_tables, None)
444 };
445
446 assert_eq!(page_tables.len(), PAGE_TABLE_SIZE as usize);
448
449 let image_page_count = image.len() as u64 / HV_PAGE_SIZE;
451 importer
452 .import_pages(
453 IMAGE_GPA_BASE / HV_PAGE_SIZE,
454 image_page_count,
455 "uefi-image",
456 BootPageAcceptance::Exclusive,
457 image,
458 )
459 .map_err(Error::Importer)?;
460
461 let mut total_page_count = IMAGE_GPA_BASE / HV_PAGE_SIZE + image_page_count;
462
463 importer
464 .import_pages(
465 PAGE_TABLE_GPA_BASE / HV_PAGE_SIZE,
466 PAGE_TABLE_SIZE / HV_PAGE_SIZE,
467 "uefi-page-tables",
468 BootPageAcceptance::Exclusive,
469 &page_tables,
470 )
471 .map_err(Error::Importer)?;
472
473 total_page_count += PAGE_TABLE_SIZE / HV_PAGE_SIZE;
474
475 assert_eq!(DEFAULT_GDT_SIZE, HV_PAGE_SIZE);
477 import_default_gdt(importer, GDT_GPA_BASE / HV_PAGE_SIZE).map_err(Error::Importer)?;
478 total_page_count += DEFAULT_GDT_SIZE / HV_PAGE_SIZE;
479
480 importer
482 .import_pages(
483 MISC_PAGES_GPA_BASE / HV_PAGE_SIZE,
484 MISC_PAGES_SIZE / HV_PAGE_SIZE,
485 "uefi-misc-pages",
486 BootPageAcceptance::Exclusive,
487 &[],
488 )
489 .map_err(Error::Importer)?;
490
491 total_page_count += MISC_PAGES_SIZE / HV_PAGE_SIZE;
492
493 match config {
496 ConfigType::Igvm => {
497 total_page_count += set_igvm_parameters(
498 importer,
499 CONFIG_BLOB_GPA_BASE / HV_PAGE_SIZE,
500 match isolation.isolation_type {
501 IsolationType::Snp => shared_vis_page_tables
502 .as_ref()
503 .expect("should be shared vis page tables"),
504 _ => &[],
505 },
506 )?
507 }
508 ConfigType::ConfigBlob(config) => {
509 let data = config.complete();
510 assert!(!data.is_empty());
511 let config_blob_page_count = (data.len() as u64).div_ceil(HV_PAGE_SIZE);
512 importer
513 .import_pages(
514 CONFIG_BLOB_GPA_BASE / HV_PAGE_SIZE,
515 config_blob_page_count,
516 "uefi-config-blob",
517 BootPageAcceptance::Exclusive,
518 &data,
519 )
520 .map_err(Error::Importer)?;
521
522 total_page_count += config_blob_page_count;
523 }
524 ConfigType::None => {}
525 }
526
527 importer
530 .verify_startup_memory_available(0, total_page_count, StartupMemoryType::Ram)
531 .map_err(Error::Importer)?;
532
533 let mut import_reg = |register| {
534 importer
535 .import_vp_register(register)
536 .map_err(Error::Importer)
537 };
538
539 import_reg(X86Register::Cr0(
541 x86defs::X64_CR0_PG | x86defs::X64_CR0_NE | x86defs::X64_CR0_MP | x86defs::X64_CR0_PE,
542 ))?;
543
544 import_reg(X86Register::Cr3(PAGE_TABLE_GPA_BASE))?;
546
547 import_reg(X86Register::Cr4(
549 x86defs::X64_CR4_PAE
550 | x86defs::X64_CR4_MCE
551 | x86defs::X64_CR4_FXSR
552 | x86defs::X64_CR4_XMMEXCPT,
553 ))?;
554
555 import_reg(X86Register::Efer(
557 x86defs::X64_EFER_LMA | x86defs::X64_EFER_LME | x86defs::X64_EFER_NXE,
558 ))?;
559
560 import_reg(X86Register::Pat(x86defs::X86X_MSR_DEFAULT_PAT))?;
562
563 import_reg(X86Register::Rbp(
566 IMAGE_GPA_BASE + SEC_FIRMWARE_VOLUME_OFFSET,
567 ))?;
568
569 import_reg(X86Register::Rip(IMAGE_GPA_BASE + sec_entry_point))?;
571
572 let isolation_cpuid = isolation.get_cpuid();
574
575 import_reg(X86Register::R8(isolation_cpuid.eax as u64))?;
576 import_reg(X86Register::R9(isolation_cpuid.ebx as u64))?;
577 import_reg(X86Register::R10(isolation_cpuid.ecx as u64))?;
578 import_reg(X86Register::R11(isolation_cpuid.edx as u64))?;
579
580 import_reg(X86Register::MtrrDefType(0xc00))?;
582 import_reg(X86Register::MtrrFix64k00000(0x0606060606060606))?;
583 import_reg(X86Register::MtrrFix16k80000(0x0606060606060606))?;
584
585 Ok(LoadInfo {
586 firmware_base: IMAGE_GPA_BASE,
587 firmware_size: image.len() as u64,
588 total_size: total_page_count * HV_PAGE_SIZE,
589 })
590 }
591
592 struct PageAllocator {
594 base: u32,
595 total_count: u32,
596 }
597
598 impl PageAllocator {
599 fn new(base: u32) -> PageAllocator {
601 PageAllocator {
602 base,
603 total_count: 0,
604 }
605 }
606
607 fn allocate(&mut self, count: u32) -> u32 {
609 let allocation = self.base + self.total_count;
610 self.total_count += count;
611
612 allocation
613 }
614
615 fn total(&self) -> u32 {
617 self.total_count
618 }
619 }
620
621 fn set_igvm_parameters(
624 importer: &mut dyn ImageLoad<X86Register>,
625 config_area_base_page: u64,
626 shared_visibility_page_tables: &[u8],
627 ) -> Result<u64, Error> {
628 let mut parameter_info = super::igvm::UEFI_IGVM_PARAMETER_INFO::new_zeroed();
629
630 let mut allocator = PageAllocator::new(0);
633 allocator.allocate(1);
634
635 let table_page_count = 20;
639
640 let page_table_page_count =
643 align_up_to_page_size(shared_visibility_page_tables.len() as u64) / HV_PAGE_SIZE;
644 let page_table_offset = allocator.allocate(page_table_page_count as u32);
645 parameter_info.loader_block_offset = allocator.allocate(1);
646
647 let command_line_page_count = 1;
648 parameter_info.command_line_offset = allocator.allocate(command_line_page_count);
649 parameter_info.command_line_page_count = command_line_page_count;
650
651 parameter_info.memory_map_offset = allocator.allocate(table_page_count);
652 parameter_info.memory_map_page_count = table_page_count;
653
654 parameter_info.madt_offset = allocator.allocate(table_page_count);
655 parameter_info.madt_page_count = table_page_count;
656
657 parameter_info.srat_offset = allocator.allocate(table_page_count);
658 parameter_info.srat_page_count = table_page_count;
659
660 parameter_info.uefi_memory_map_offset = allocator.allocate(table_page_count);
663 parameter_info.uefi_memory_map_page_count = table_page_count;
664
665 let isolation = importer.isolation_config();
667 if isolation.isolation_type == IsolationType::Snp {
668 if isolation.paravisor_present {
670 return Err(Error::InvalidConfigType(
671 "IGVM ConfigType specified but paravisor is present.".into(),
672 ));
673 }
674
675 importer
678 .import_vp_register(X86Register::R12(config_area_base_page * HV_PAGE_SIZE))
679 .map_err(Error::Importer)?;
680
681 parameter_info.cpuid_pages_offset = allocator.allocate(2);
686
687 let cpuid_page = create_snp_cpuid_page();
688
689 importer
690 .import_pages(
691 config_area_base_page + parameter_info.cpuid_pages_offset as u64,
692 1,
693 "uefi-cpuid-page",
694 BootPageAcceptance::CpuidPage,
695 cpuid_page.as_bytes(),
696 )
697 .map_err(Error::Importer)?;
698
699 importer
700 .import_pages(
701 config_area_base_page + parameter_info.cpuid_pages_offset as u64 + 1,
702 1,
703 "uefi-cpuid-extended-page",
704 BootPageAcceptance::CpuidExtendedStatePage,
705 &[],
706 )
707 .map_err(Error::Importer)?;
708
709 let vp_context_page_number = config_area_base_page + allocator.total() as u64;
718 importer
719 .set_vp_context_page(vp_context_page_number)
720 .map_err(Error::Importer)?;
721
722 parameter_info.vp_context_page_number = vp_context_page_number;
723 } else {
724 parameter_info.vp_context_page_number = 0xfffff;
729 }
730
731 parameter_info.parameter_page_count = allocator.total();
733
734 importer
735 .import_pages(
736 config_area_base_page,
737 1,
738 "uefi-config-base-page",
739 BootPageAcceptance::Exclusive,
740 parameter_info.as_bytes(),
741 )
742 .map_err(Error::Importer)?;
743
744 importer
745 .import_pages(
746 config_area_base_page + parameter_info.uefi_memory_map_offset as u64,
747 parameter_info.uefi_memory_map_page_count as u64,
748 "uefi-memory-map-scratch",
749 BootPageAcceptance::ExclusiveUnmeasured,
750 &[],
751 )
752 .map_err(Error::Importer)?;
753
754 let loader_block = importer
755 .create_parameter_area(
756 config_area_base_page + parameter_info.loader_block_offset as u64,
757 1,
758 "uefi-loader-block",
759 )
760 .map_err(Error::Importer)?;
761 importer
762 .import_parameter(
763 loader_block,
764 super::igvm::UEFI_IGVM_LOADER_BLOCK_NUMBER_OF_PROCESSORS_FIELD_OFFSET as u32,
765 IgvmParameterType::VpCount,
766 )
767 .map_err(Error::Importer)?;
768
769 let command_line = importer
770 .create_parameter_area(
771 config_area_base_page + parameter_info.command_line_offset as u64,
772 parameter_info.command_line_page_count,
773 "uefi-command-line",
774 )
775 .map_err(Error::Importer)?;
776 importer
777 .import_parameter(command_line, 0, IgvmParameterType::CommandLine)
778 .map_err(Error::Importer)?;
779
780 let memory_map = importer
781 .create_parameter_area(
782 config_area_base_page + parameter_info.memory_map_offset as u64,
783 parameter_info.memory_map_page_count,
784 "uefi-memory-map",
785 )
786 .map_err(Error::Importer)?;
787 importer
788 .import_parameter(memory_map, 0, IgvmParameterType::MemoryMap)
789 .map_err(Error::Importer)?;
790
791 let madt = importer
792 .create_parameter_area(
793 config_area_base_page + parameter_info.madt_offset as u64,
794 parameter_info.madt_page_count,
795 "uefi-madt",
796 )
797 .map_err(Error::Importer)?;
798 importer
799 .import_parameter(madt, 0, IgvmParameterType::Madt)
800 .map_err(Error::Importer)?;
801
802 let srat = importer
803 .create_parameter_area(
804 config_area_base_page + parameter_info.srat_offset as u64,
805 parameter_info.srat_page_count,
806 "uefi-srat",
807 )
808 .map_err(Error::Importer)?;
809 importer
810 .import_parameter(srat, 0, IgvmParameterType::Srat)
811 .map_err(Error::Importer)?;
812
813 if page_table_page_count != 0 {
814 importer
815 .import_pages(
816 config_area_base_page + page_table_offset as u64,
817 page_table_page_count,
818 "uefi-igvm-page-tables",
819 BootPageAcceptance::Exclusive,
820 shared_visibility_page_tables,
821 )
822 .map_err(Error::Importer)?;
823 }
824
825 Ok(allocator.total() as u64)
826 }
827
828 fn create_snp_cpuid_page() -> HV_PSP_CPUID_PAGE {
830 let mut cpuid_page = HV_PSP_CPUID_PAGE::default();
831
832 for (i, required_leaf) in crate::cpuid::SNP_REQUIRED_CPUID_LEAF_LIST_UEFI
833 .iter()
834 .enumerate()
835 {
836 cpuid_page.cpuid_leaf_info[i].eax_in = required_leaf.eax;
837 cpuid_page.cpuid_leaf_info[i].eax_out = required_leaf.ecx;
838 cpuid_page.count += 1;
839 }
840
841 cpuid_page
842 }
843}
844
845pub mod aarch64 {
846 use super::ConfigType;
847 use super::Error;
848 use super::LoadInfo;
849 use crate::importer::Aarch64Register;
850 use crate::importer::BootPageAcceptance;
851 use crate::importer::ImageLoad;
852 use aarch64defs::Cpsr64;
853 use hvdef::HV_PAGE_SIZE;
854
855 use zerocopy::IntoBytes;
856
857 pub const IMAGE_SIZE: u64 = 0x800000;
858 pub const CONFIG_BLOB_GPA_BASE: u64 = 0x824000;
859
860 pub fn load(
862 importer: &mut dyn ImageLoad<Aarch64Register>,
863 image: &[u8],
864 config: ConfigType,
865 ) -> Result<LoadInfo, Error> {
866 if image.len() != IMAGE_SIZE as usize {
867 return Err(Error::InvalidImageSize);
868 }
869
870 const BYTES_2MB: u64 = 0x200000;
871
872 let image_size = (image.len() as u64 + BYTES_2MB - 1) & !(BYTES_2MB - 1);
873 importer
874 .import_pages(
875 0,
876 image_size / HV_PAGE_SIZE,
877 "uefi-image",
878 BootPageAcceptance::Exclusive,
879 image,
880 )
881 .map_err(Error::Importer)?;
882
883 let stack_offset = image_size;
885 let stack_size = 32 * HV_PAGE_SIZE;
886 let stack_end = stack_offset + stack_size;
887 importer
888 .import_pages(
889 stack_offset / HV_PAGE_SIZE,
890 stack_size / HV_PAGE_SIZE,
891 "uefi-stack",
892 BootPageAcceptance::Exclusive,
893 &[],
894 )
895 .map_err(Error::Importer)?;
896
897 let page_table_offset = stack_end;
899 let page_tables = page_tables(page_table_offset, 1 << 30 );
900 importer
901 .import_pages(
902 page_table_offset / HV_PAGE_SIZE,
903 page_tables.as_bytes().len() as u64 / HV_PAGE_SIZE,
904 "uefi-page-tables",
905 BootPageAcceptance::Exclusive,
906 page_tables.as_bytes(),
907 )
908 .map_err(Error::Importer)?;
909
910 let blob_offset = CONFIG_BLOB_GPA_BASE;
911
912 let blob_size = match config {
914 ConfigType::ConfigBlob(blob) => {
915 let blob = blob.complete();
916 let blob_size = (blob.len() as u64 + HV_PAGE_SIZE - 1) & !(HV_PAGE_SIZE - 1);
917 importer
918 .import_pages(
919 blob_offset / HV_PAGE_SIZE,
920 blob_size / HV_PAGE_SIZE,
921 "uefi-config-blob",
922 BootPageAcceptance::Exclusive,
923 &blob,
924 )
925 .map_err(Error::Importer)?;
926
927 blob_size
928 }
929 ConfigType::None => 0,
930 ConfigType::Igvm => {
931 return Err(Error::InvalidConfigType("igvm not supported".to_owned()));
932 }
933 };
934
935 let total_size = blob_offset + blob_size;
936
937 let mut import_reg = |reg| importer.import_vp_register(reg).map_err(Error::Importer);
938
939 import_reg(Aarch64Register::Cpsr(
940 Cpsr64::new().with_sp(true).with_el(1).into(),
941 ))?;
942 import_reg(Aarch64Register::X0(0x1000))?;
943 import_reg(Aarch64Register::Pc(0x1000))?;
944 import_reg(Aarch64Register::X1(stack_end))?;
945
946 import_reg(Aarch64Register::Ttbr0El1(page_table_offset))?;
947
948 const ARM64_MAIR_CACHE_WBWA: u64 = 0xff;
950 const ARM64_MAIR_CACHE_NC: u64 = 0x00;
951 const ARM64_MAIR_CACHE_WTNA: u64 = 0xaa;
952 const ARM64_MAIR_CACHE_WC: u64 = 0x44;
953
954 import_reg(Aarch64Register::MairEl1(
955 ARM64_MAIR_CACHE_WBWA
956 | (ARM64_MAIR_CACHE_NC << 8)
957 | (ARM64_MAIR_CACHE_WTNA << 16)
958 | (ARM64_MAIR_CACHE_WC << 24)
959 | (ARM64_MAIR_CACHE_WBWA << 32)
960 | (ARM64_MAIR_CACHE_NC << 40)
961 | (ARM64_MAIR_CACHE_WTNA << 48)
962 | (ARM64_MAIR_CACHE_WC << 56),
963 ))?;
964
965 const ARM64_SCTLR_M: u64 = 0x00000001;
967 const ARM64_SCTLR_C: u64 = 0x00000004;
968 const ARM64_SCTLR_RES1_11: u64 = 0x00000800;
969 const ARM64_SCTLR_I: u64 = 0x00001000;
970 const ARM64_SCTLR_RES1_20: u64 = 0x00100000;
971 const ARM64_SCTLR_RES1_22: u64 = 0x00400000;
972 const ARM64_SCTLR_RES1_23: u64 = 0x00800000;
973 const ARM64_SCTLR_RES1_28: u64 = 0x10000000;
974 const ARM64_SCTLR_RES1_29: u64 = 0x20000000;
975
976 import_reg(Aarch64Register::SctlrEl1(
977 ARM64_SCTLR_M
978 | ARM64_SCTLR_C
979 | ARM64_SCTLR_I
980 | ARM64_SCTLR_RES1_11
981 | ARM64_SCTLR_RES1_20
982 | ARM64_SCTLR_RES1_22
983 | ARM64_SCTLR_RES1_23
984 | ARM64_SCTLR_RES1_28
985 | ARM64_SCTLR_RES1_29,
986 ))?;
987
988 const ARM64_TCR_IRGN0_WBWA: u64 = 0x0000000000000100;
991 const ARM64_TCR_ORGN0_WBWA: u64 = 0x0000000000000400;
992 const ARM64_TCR_SH0_INNER_SHARED: u64 = 0x0000000000003000;
993 const ARM64_TCR_TG0_4K: u64 = 0x0000000000000000;
994 const ARM64_TCR_EPD1: u64 = 0x0000000000800000;
995 const ARM64_TCR_T0SZ_SHIFT: u32 = 0;
996 const ARM64_TCR_T1SZ_SHIFT: u32 = 16;
997
998 import_reg(Aarch64Register::TcrEl1(
999 ARM64_TCR_EPD1
1000 | ARM64_TCR_TG0_4K
1001 | ARM64_TCR_SH0_INNER_SHARED
1002 | ARM64_TCR_ORGN0_WBWA
1003 | ARM64_TCR_IRGN0_WBWA
1004 | (16 << ARM64_TCR_T0SZ_SHIFT)
1005 | (16 << ARM64_TCR_T1SZ_SHIFT),
1006 ))?;
1007
1008 Ok(LoadInfo {
1009 firmware_base: 0,
1010 firmware_size: image.len() as u64,
1011 total_size,
1012 })
1013 }
1014
1015 const PTE_VALID: u64 = 1 << 0;
1016 const PTE_NOT_LARGE: u64 = 1 << 1;
1017 const PTE_MAIR_WB: u64 = 0 << 2;
1018 const PTE_MAIR_UC: u64 = 1 << 2;
1019 const PTE_SHARABILITY_INNER: u64 = 3 << 8;
1020 const PTE_ACCESSED: u64 = 1 << 10;
1021 const PTE_USER_NX: u64 = 1 << 54;
1022
1023 fn large_leaf_entry(normal: bool, address: u64) -> u64 {
1024 address
1025 | PTE_VALID
1026 | PTE_ACCESSED
1027 | PTE_SHARABILITY_INNER
1028 | PTE_USER_NX
1029 | if normal { PTE_MAIR_WB } else { PTE_MAIR_UC }
1030 }
1031
1032 fn non_leaf_entry(address: u64) -> u64 {
1033 address | PTE_VALID | PTE_NOT_LARGE
1034 }
1035
1036 fn leaf_entry(normal: bool, address: u64) -> u64 {
1037 address
1038 | PTE_VALID
1039 | PTE_ACCESSED
1040 | PTE_NOT_LARGE
1041 | PTE_SHARABILITY_INNER
1042 | PTE_USER_NX
1043 | if normal { PTE_MAIR_WB } else { PTE_MAIR_UC }
1044 }
1045
1046 fn table_index(va: u64, level: u32) -> usize {
1047 let index = va >> (9 * (3 - level) + 12);
1048 let index = index & ((1 << 9) - 1);
1049 index as usize
1050 }
1051
1052 fn page_tables(address: u64, end_of_ram: u64) -> Vec<[u64; 512]> {
1053 const PT_SIZE: u64 = 4096;
1054 const VA_4GB: u64 = 1 << 32;
1055 const VA_1GB: u64 = 1 << 30;
1056 const VA_2MB: u64 = 2 << 20;
1057 const VA_4KB: u64 = 4 << 10;
1058
1059 let mut buffer = vec![[0u64; PT_SIZE as usize / 8]; 4];
1060 let [level0, level1, level2, level3] = buffer.as_mut_slice() else {
1061 unreachable!()
1062 };
1063
1064 level0[0] = non_leaf_entry(address + PT_SIZE);
1074
1075 let mut normal = true;
1080 let mut va = 0;
1081 let mut end_va = end_of_ram;
1082 while va < VA_4GB {
1083 if normal && va == end_va {
1087 normal = false;
1088 end_va = VA_4GB;
1089 continue;
1090 }
1091
1092 let level1_index = table_index(va, 1);
1094 if level1[level1_index] & PTE_VALID == 0
1095 && ((va & (VA_1GB - 1)) == 0)
1096 && (end_va - va >= VA_1GB)
1097 {
1098 level1[level1_index] = large_leaf_entry(normal, va);
1099 va += VA_1GB;
1100 continue;
1101 }
1102
1103 if level1[level1_index] & PTE_VALID == 0 {
1108 level1[level1_index] = non_leaf_entry(address + PT_SIZE * 2);
1109 }
1110
1111 let level2_index = table_index(va, 2);
1115 if level2[level2_index] & PTE_VALID == 0
1116 && ((va & (VA_2MB - 1)) == 0)
1117 && (end_va - va >= VA_2MB)
1118 {
1119 level2[level2_index] = large_leaf_entry(normal, va);
1120 va += VA_2MB;
1121 continue;
1122 }
1123
1124 if level2[level2_index] & PTE_VALID == 0 {
1129 level2[level2_index] = non_leaf_entry(address + PT_SIZE * 3);
1130 }
1131
1132 let level3_index = table_index(va, 3);
1133 level3[level3_index] = leaf_entry(normal, va);
1134 va += VA_4KB;
1135 }
1136
1137 buffer
1138 }
1139}