|
| 1 | +// Copyright (c) 2021 Intel Corporation |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: BSD-2-Clause-Patent |
| 4 | + |
| 5 | +#![no_std] |
| 6 | +#![cfg_attr(test, no_main)] |
| 7 | +// The `custom_test_frameworks` feature allows the use of `#[test_case]` and `#![test_runner]`. |
| 8 | +// Any function, const, or static can be annotated with `#[test_case]` causing it to be aggregated |
| 9 | +// (like #[test]) and be passed to the test runner determined by the `#![test_runner]` crate |
| 10 | +// attribute. |
| 11 | +#![feature(custom_test_frameworks)] |
| 12 | +#![test_runner(test_runner)] |
| 13 | +// Reexport the test harness main function under a different symbol. |
| 14 | +#![reexport_test_harness_main = "test_main"] |
| 15 | + |
| 16 | +#[cfg(test)] |
| 17 | +use bootloader::{boot_info, entry_point, BootInfo}; |
| 18 | +#[cfg(test)] |
| 19 | +use core::ops::Deref; |
| 20 | +#[cfg(test)] |
| 21 | +use test_runner_client::{init_heap, serial_println, test_runner}; |
| 22 | + |
| 23 | +#[cfg(test)] |
| 24 | +entry_point!(kernel_main); |
| 25 | + |
| 26 | +#[cfg(test)] |
| 27 | +fn kernel_main(boot_info: &'static mut BootInfo) -> ! { |
| 28 | + // turn the screen gray |
| 29 | + if let Some(framebuffer) = boot_info.framebuffer.as_mut() { |
| 30 | + for byte in framebuffer.buffer_mut() { |
| 31 | + *byte = 0x90; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + let memoryregions = boot_info.memory_regions.deref(); |
| 36 | + let offset = boot_info.physical_memory_offset.into_option().unwrap(); |
| 37 | + |
| 38 | + for usable in memoryregions.iter() { |
| 39 | + if usable.kind == boot_info::MemoryRegionKind::Usable { |
| 40 | + init_heap((usable.start + offset) as usize, 0x100000); |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + serial_println!("Start to execute test cases..."); |
| 46 | + test_main(); |
| 47 | + panic!("Unexpected return from test_main()!!!"); |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(test)] |
| 51 | +mod tests { |
| 52 | + use td_layout::runtime; |
| 53 | + use td_paging::{ |
| 54 | + create_mapping, reserve_page, PAGE_SIZE_DEFAULT, PAGE_TABLE_SIZE, PHYS_VIRT_OFFSET, |
| 55 | + }; |
| 56 | + use x86_64::{ |
| 57 | + structures::paging::{OffsetPageTable, PageTable}, |
| 58 | + PhysAddr, VirtAddr, |
| 59 | + }; |
| 60 | + |
| 61 | + /// Build page table to map guest physical addres range [0, system_memory_size), the top page table |
| 62 | + /// page will be hosted at guest physical address `page_table_memory_base`. |
| 63 | + pub fn setup_paging(page_table_memory_base: u64, system_memory_size: u64) { |
| 64 | + let mut pt = unsafe { |
| 65 | + OffsetPageTable::new( |
| 66 | + &mut *(page_table_memory_base as *mut PageTable), |
| 67 | + VirtAddr::new(PHYS_VIRT_OFFSET as u64), |
| 68 | + ) |
| 69 | + }; |
| 70 | + |
| 71 | + if page_table_memory_base > system_memory_size |
| 72 | + || page_table_memory_base < runtime::TD_PAYLOAD_PAGE_TABLE_BASE |
| 73 | + || page_table_memory_base > runtime::TD_PAYLOAD_PAGE_TABLE_BASE + PAGE_TABLE_SIZE as u64 |
| 74 | + || runtime::TD_PAYLOAD_PAGE_TABLE_BASE + PAGE_TABLE_SIZE as u64 > system_memory_size |
| 75 | + { |
| 76 | + panic!( |
| 77 | + "invalid parameters (0x{:x}, 0x{:x} to setup_paging()", |
| 78 | + page_table_memory_base, system_memory_size |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + reserve_page(page_table_memory_base); |
| 83 | + |
| 84 | + // TODO: make this work. More work is needed to enable paging, basically need to duplicate |
| 85 | + // tdshim::memory::setup_paging() |
| 86 | + /* |
| 87 | + create_mapping( |
| 88 | + &mut pt, |
| 89 | + PhysAddr::new(0), |
| 90 | + VirtAddr::new(0), |
| 91 | + PAGE_SIZE_DEFAULT as u64, |
| 92 | + system_memory_size, |
| 93 | + ); |
| 94 | + page_table::cr3_write(); |
| 95 | + */ |
| 96 | + } |
| 97 | + |
| 98 | + #[test_case] |
| 99 | + fn test_create_paging() { |
| 100 | + td_paging::init(); |
| 101 | + setup_paging( |
| 102 | + runtime::TD_PAYLOAD_PAGE_TABLE_BASE + 0x1000, |
| 103 | + runtime::TD_PAYLOAD_PAGE_TABLE_BASE + PAGE_TABLE_SIZE as u64, |
| 104 | + ); |
| 105 | + |
| 106 | + // TODO: add test cases for create_mapping_with_flags(), set_page_flags() |
| 107 | + } |
| 108 | +} |
0 commit comments