Skip to content

Commit cac6191

Browse files
Alexandra Iordachealexandruag
authored andcommitted
docs: more usage examples
Signed-off-by: Alexandra Iordache <aghecen@amazon.com>
1 parent f4c6507 commit cac6191

File tree

7 files changed

+118
-4
lines changed

7 files changed

+118
-4
lines changed

coverage_config_aarch64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 80.4,
2+
"coverage_score": 80.8,
33
"exclude_path": "",
44
"crate_features": ""
55
}

src/cmdline/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl fmt::Display for Error {
4040
}
4141

4242
/// Specialized [`Result`] type for command line operations.
43+
///
4344
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
4445
pub type Result<T> = result::Result<T, Error>;
4546

src/configurator/aarch64/fdt.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ impl BootConfigurator for FdtBootConfigurator {
5656
///
5757
/// * `params` - boot parameters containing the FDT.
5858
/// * `guest_memory` - guest's physical memory.
59+
///
60+
/// # Examples
61+
///
62+
/// ```rust
63+
/// # extern crate vm_memory;
64+
/// # use linux_loader::configurator::{BootConfigurator, BootParams};
65+
/// # use linux_loader::configurator::fdt::FdtBootConfigurator;
66+
/// # use vm_memory::{Address, ByteValued, GuestMemory, GuestMemoryMmap, GuestAddress};
67+
/// # #[derive(Clone, Copy, Default)]
68+
/// # struct FdtPlaceholder([u8; 0x20]);
69+
/// # unsafe impl ByteValued for FdtPlaceholder {}
70+
/// # fn create_guest_memory() -> GuestMemoryMmap {
71+
/// # GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (0x100_0000 as usize))]).unwrap()
72+
/// # }
73+
/// # fn create_fdt(guest_memory: &GuestMemoryMmap) -> (FdtPlaceholder, GuestAddress) {
74+
/// # let last_addr = guest_memory.last_addr().raw_value();
75+
/// # (FdtPlaceholder([0u8; 0x20]), GuestAddress(last_addr - 0x20u64))
76+
/// # }
77+
/// # fn main() {
78+
/// let guest_memory = create_guest_memory();
79+
/// let (fdt, fdt_addr) = create_fdt(&guest_memory);
80+
/// FdtBootConfigurator::write_bootparams::<GuestMemoryMmap>(
81+
/// BootParams::new::<FdtPlaceholder>(&fdt, fdt_addr),
82+
/// &guest_memory,
83+
/// ).unwrap();
84+
/// # }
85+
/// ```
5986
fn write_bootparams<M>(params: BootParams, guest_memory: &M) -> Result<()>
6087
where
6188
M: GuestMemory,

src/configurator/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ impl fmt::Display for Error {
8484
}
8585
}
8686

87-
/// A specialized `Result` type for the boot configurator.
87+
/// Specialized [`Result`] type for the boot configurator.
88+
///
89+
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
8890
pub type Result<T> = std::result::Result<T, Error>;
8991

9092
/// Trait that defines interfaces for building (TBD) and configuring boot parameters.

src/configurator/x86_64/linux.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,43 @@ impl BootConfigurator for LinuxBootConfigurator {
6666
/// and `modules` are unused.
6767
/// * `guest_memory` - guest's physical memory.
6868
///
69+
/// # Examples
70+
///
71+
/// ```rust
72+
/// # extern crate vm_memory;
73+
/// # use linux_loader::configurator::{BootConfigurator, BootParams};
74+
/// # use linux_loader::configurator::linux::LinuxBootConfigurator;
75+
/// # use linux_loader::loader::bootparam::boot_params;
76+
/// # use vm_memory::{Address, ByteValued, GuestMemory, GuestMemoryMmap, GuestAddress};
77+
/// # const KERNEL_BOOT_FLAG_MAGIC: u16 = 0xaa55;
78+
/// # const KERNEL_HDR_MAGIC: u32 = 0x53726448;
79+
/// # const KERNEL_LOADER_OTHER: u8 = 0xff;
80+
/// # const KERNEL_MIN_ALIGNMENT_BYTES: u32 = 0x1000000;
81+
/// # const MEM_SIZE: u64 = 0x100_0000;
82+
/// # fn create_guest_memory() -> GuestMemoryMmap {
83+
/// # GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap()
84+
/// # }
85+
/// fn build_bootparams() -> boot_params {
86+
/// let mut params = boot_params::default();
87+
/// params.hdr.boot_flag = KERNEL_BOOT_FLAG_MAGIC;
88+
/// params.hdr.header = KERNEL_HDR_MAGIC;
89+
/// params.hdr.kernel_alignment = KERNEL_MIN_ALIGNMENT_BYTES;
90+
/// params.hdr.type_of_loader = KERNEL_LOADER_OTHER;
91+
/// params
92+
/// }
93+
///
94+
/// fn main() {
95+
/// # let zero_page_addr = GuestAddress(0x30000);
96+
/// let guest_memory = create_guest_memory();
97+
/// let params = build_bootparams();
98+
/// let mut bootparams = BootParams::new::<boot_params>(&params, zero_page_addr);
99+
/// LinuxBootConfigurator::write_bootparams::<GuestMemoryMmap>(
100+
/// bootparams,
101+
/// &guest_memory,
102+
/// ).unwrap();
103+
/// }
104+
/// ```
105+
///
69106
/// [`boot_params`]: ../loader/bootparam/struct.boot_params.html
70107
fn write_bootparams<M>(params: BootParams, guest_memory: &M) -> Result<()>
71108
where

src/configurator/x86_64/pvh.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,49 @@ impl BootConfigurator for PvhBootConfigurator {
9191
/// [`hvm_start_info`]: ../loader/elf/start_info/struct.hvm_start_info.html
9292
/// [`hvm_memmap_table_entry`]: ../loader/elf/start_info/struct.hvm_memmap_table_entry.html
9393
/// [`hvm_modlist_entry`]: ../loader/elf/start_info/struct.hvm_modlist_entry.html
94+
///
95+
/// # Examples
96+
///
97+
/// ```rust
98+
/// # extern crate vm_memory;
99+
/// # use linux_loader::configurator::{BootConfigurator, BootParams};
100+
/// # use linux_loader::configurator::pvh::PvhBootConfigurator;
101+
/// # use linux_loader::loader::elf::start_info::{hvm_start_info, hvm_memmap_table_entry};
102+
/// # use vm_memory::{Address, ByteValued, GuestMemory, GuestMemoryMmap, GuestAddress};
103+
/// # const XEN_HVM_START_MAGIC_VALUE: u32 = 0x336ec578;
104+
/// # const MEM_SIZE: u64 = 0x100_0000;
105+
/// # const E820_RAM: u32 = 1;
106+
/// fn create_guest_memory() -> GuestMemoryMmap {
107+
/// GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap()
108+
/// }
109+
///
110+
/// fn build_boot_params() -> (hvm_start_info, Vec<hvm_memmap_table_entry>) {
111+
/// let mut start_info = hvm_start_info::default();
112+
/// let memmap_entry = hvm_memmap_table_entry {
113+
/// addr: 0x7000,
114+
/// size: 0,
115+
/// type_: E820_RAM,
116+
/// reserved: 0,
117+
/// };
118+
/// start_info.magic = XEN_HVM_START_MAGIC_VALUE;
119+
/// start_info.version = 1;
120+
/// start_info.nr_modules = 0;
121+
/// start_info.memmap_entries = 0;
122+
/// (start_info, vec![memmap_entry])
123+
/// }
124+
///
125+
/// fn main() {
126+
/// let guest_mem = create_guest_memory();
127+
/// let (mut start_info, memmap_entries) = build_boot_params();
128+
/// let start_info_addr = GuestAddress(0x6000);
129+
/// let memmap_addr = GuestAddress(0x7000);
130+
/// start_info.memmap_paddr = memmap_addr.raw_value();
131+
///
132+
/// let mut boot_params = BootParams::new::<hvm_start_info>(&start_info, start_info_addr);
133+
/// boot_params.set_sections::<hvm_memmap_table_entry>(&memmap_entries, memmap_addr);
134+
/// PvhBootConfigurator::write_bootparams::<GuestMemoryMmap>(boot_params, &guest_mem).unwrap();
135+
/// }
136+
/// ```
94137
fn write_bootparams<M>(params: BootParams, guest_memory: &M) -> Result<()>
95138
where
96139
M: GuestMemory,

src/loader/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
//! - [KernelLoader](trait.KernelLoader.html): load kernel image into guest memory.
1414
//! - [KernelLoaderResult](struct.KernelLoaderResult.html): structure passed to the VMM to assist
1515
//! zero page construction and boot environment setup.
16-
//! - [Elf](struct.Elf.html): elf image loader.
17-
//! - [BzImage](struct.BzImage.html): bzImage loader.
16+
//! - [Elf](elf/struct.Elf.html): elf image loader.
17+
//! - [BzImage](bzimage/struct.BzImage.html): bzImage loader.
18+
//! - [PE](pe/struct.PE.html): PE image loader.
1819
1920
extern crate vm_memory;
2021

@@ -34,6 +35,8 @@ use vm_memory::{Address, Bytes, GuestAddress, GuestMemory, GuestUsize};
3435
#[allow(missing_docs)]
3536
#[cfg_attr(feature = "cargo-clippy", allow(clippy::all))]
3637
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
38+
// Hide the autogenerated documentation for bindgen'ed sources.
39+
#[doc(hidden)]
3740
pub mod bootparam;
3841

3942
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
@@ -72,6 +75,7 @@ pub enum Error {
7275
}
7376

7477
/// A specialized [`Result`] type for the kernel loader.
78+
///
7579
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
7680
pub type Result<T> = std::result::Result<T, Error>;
7781

0 commit comments

Comments
 (0)