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
| commit d3959ba050e8d4fcbd764415b0bac71be5a2013b Author: Panda Jiang <3160104094@zju.edu.cn> Date: Fri Nov 29 07:43:01 2024 +0000
read pflash.img ok Signed-off-by: Panda Jiang <3160104094@zju.edu.cn>
diff --git a/arceos/tour/h_2_0/src/main.rs b/arceos/tour/h_2_0/src/main.rs index 0ae2f0e..d7b6d6a 100644
@@ -6,14 +6,14 @@ extern crate log; #[macro_use] extern crate alloc; extern crate axstd as std; +use alloc::string::String; use alloc::string::ToString; -use riscv_vcpu::AxVCpuExitReason; use axerrno::{ax_err_type, AxResult}; use memory_addr::VirtAddr; -use alloc::string::String; -use std::fs::File; -use riscv_vcpu::RISCVVCpu; +use riscv_vcpu::AxVCpuExitReason; use riscv_vcpu::AxVCpuExitReason::NestedPageFault; +use riscv_vcpu::RISCVVCpu; +use std::fs::File; const VM_ASPACE_BASE: usize = 0x0; const VM_ASPACE_SIZE: usize = 0x7fff_ffff_f000; @@ -21,8 +21,8 @@ const PHY_MEM_START: usize = 0x8000_0000; const PHY_MEM_SIZE: usize = 0x100_0000; const KERNEL_BASE: usize = 0x8020_0000; -use axmm::AddrSpace; use axhal::paging::MappingFlags; +use axmm::AddrSpace; #[no_mangle] fn main() { @@ -36,40 +36,47 @@ fn main() { // Physical memory region. Full access flags. let mapping_flags = MappingFlags::from_bits(0xf).unwrap(); - aspace.map_alloc(PHY_MEM_START.into(), PHY_MEM_SIZE, mapping_flags, true).unwrap(); + aspace + .map_alloc(PHY_MEM_START.into(), PHY_MEM_SIZE, mapping_flags, true) + .unwrap(); // Load corresponding images for VM. info!("VM created success, loading images..."); let image_fname = "/sbin/u_3_0_riscv64-qemu-virt.bin"; - load_vm_image(image_fname.to_string(), KERNEL_BASE.into(), &aspace).expect("Failed to load VM images"); + load_vm_image(image_fname.to_string(), KERNEL_BASE.into(), &aspace) + .expect("Failed to load VM images"); // Create VCpus. let mut arch_vcpu = RISCVVCpu::init(); // Setup VCpus. - info!("bsp_entry: {:#x}; ept: {:#x}", KERNEL_BASE, aspace.page_table_root()); + info!( + "bsp_entry: {:#x}; ept: {:#x}", + KERNEL_BASE, + aspace.page_table_root() + ); arch_vcpu.set_entry(KERNEL_BASE.into()).unwrap(); arch_vcpu.set_ept_root(aspace.page_table_root()).unwrap(); loop { match vcpu_run(&mut arch_vcpu) { Ok(exit_reason) => match exit_reason { - AxVCpuExitReason::Nothing => {}, - NestedPageFault{addr, access_flags} => { + AxVCpuExitReason::Nothing => {} + NestedPageFault { addr, access_flags } => { debug!("addr {:#x} access {:#x}", addr, access_flags); assert_eq!(addr, 0x2200_0000.into(), "Now we ONLY handle pflash#2."); - let mapping_flags = MappingFlags::from_bits(0xf).unwrap(); - // Passthrough-Mode - let _ = aspace.map_linear(addr, addr.as_usize().into(), 4096, mapping_flags); + // let mapping_flags = MappingFlags::from_bits(0xf).unwrap(); + // // Passthrough-Mode + // let _ = aspace.map_linear(addr, addr.as_usize().into(), 4096, mapping_flags); /* // Emulator-Mode // Pretend to load file to fill buffer. - let buf = "pfld"; + */ + // let buf = "pfld"; aspace.map_alloc(addr, 4096, mapping_flags, true); - aspace.write(addr, buf.as_bytes()); - */ - }, + load_file("/sbin/pflash.img", addr, &aspace).unwrap() + } _ => { panic!("Unhandled VM-Exit: {:?}", exit_reason); } @@ -81,6 +88,24 @@ fn main() { } } +fn load_file(file_path: &str, image_file_load_addr: VirtAddr, aspace: &AddrSpace) -> AxResult { + use std::io::{BufReader, Read}; + let (ffile, size) = open_image_file(file_path)?; + let mut file = BufReader::new(ffile); + let load_region = aspace + .translated_byte_buffer(image_file_load_addr, 4096) + .expect("failed to translate kernel image load address"); + for buffer in load_region { + file.read_exact(buffer).map_err(|err| { + ax_err_type!( + Io, + format!("Failed in reading from file {}, err {:?}", file_path, err) + ) + })? + } + Ok(()) +} + fn load_vm_image(image_path: String, image_load_gpa: VirtAddr, aspace: &AddrSpace) -> AxResult { use std::io::{BufReader, Read}; let (image_file, image_size) = open_image_file(image_path.as_str())?; @@ -103,7 +128,7 @@ fn load_vm_image(image_path: String, image_load_gpa: VirtAddr, aspace: &AddrSpac } fn vcpu_run(arch_vcpu: &mut RISCVVCpu) -> AxResult<AxVCpuExitReason> { - use axhal::arch::{local_irq_save_and_disable, local_irq_restore}; + use axhal::arch::{local_irq_restore, local_irq_save_and_disable}; let flags = local_irq_save_and_disable(); let ret = arch_vcpu.run(); local_irq_restore(flags);
|