Skip to content

Commit

Permalink
Derive or implement Default and Debug in structs
Browse files Browse the repository at this point in the history
Derive or implement Default or Debug trait for a wide range of structures
or enums. This will help, among other things, to implement unit tests
in the future.

Signed-off-by: Carlos Bilbao <carlos.bilbao@amd.com>
  • Loading branch information
Zildj1an committed Aug 15, 2023
1 parent eed0c99 commit d303b34
Show file tree
Hide file tree
Showing 21 changed files with 188 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/acpi/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use alloc::vec::Vec;
use core::mem;
use log;

#[derive(Debug)]
#[derive(Debug, Default)]
#[repr(C, packed)]
struct RSDPDesc {
sig: [u8; 8],
Expand Down Expand Up @@ -44,7 +44,7 @@ impl RSDPDesc {
}
}

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Default)]
#[repr(C, packed)]
struct RawACPITableHeader {
sig: [u8; 4],
Expand All @@ -58,7 +58,7 @@ struct RawACPITableHeader {
compiler_rev: u32,
}

#[derive(Debug)]
#[derive(Debug, Default)]
#[allow(dead_code)]
struct ACPITableHeader {
sig: [u8; 4],
Expand Down
4 changes: 2 additions & 2 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub trait Address:
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct PhysAddr(InnerAddr);

Expand Down Expand Up @@ -143,7 +143,7 @@ impl ops::Add<InnerAddr> for PhysAddr {

impl Address for PhysAddr {}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct VirtAddr(InnerAddr);

Expand Down
15 changes: 13 additions & 2 deletions src/cpu/cpuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const SNP_CPUID_MAX_COUNT: usize = 64;

static CPUID_PAGE: ImmutAfterInitRef<SnpCpuidTable> = ImmutAfterInitRef::uninit();

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Default, Debug)]
#[repr(C, packed)]
pub struct SnpCpuidFn {
eax_in: u32,
Expand All @@ -25,7 +25,7 @@ pub struct SnpCpuidFn {
reserved_1: u64,
}

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
#[repr(C, packed)]
pub struct SnpCpuidTable {
count: u32,
Expand All @@ -34,6 +34,17 @@ pub struct SnpCpuidTable {
func: [SnpCpuidFn; SNP_CPUID_MAX_COUNT],
}

impl Default for SnpCpuidTable {
fn default() -> Self {
SnpCpuidTable {
count: Default::default(),
reserved_1: Default::default(),
reserved_2: Default::default(),
func: [SnpCpuidFn::default(); SNP_CPUID_MAX_COUNT],
}
}
}

pub fn register_cpuid_table(table: &'static SnpCpuidTable) {
CPUID_PAGE
.init_from_ref(table)
Expand Down
3 changes: 2 additions & 1 deletion src/cpu/idt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ pub const VC_VECTOR: usize = 29;
pub const _SX_VECTOR: usize = 30;

#[repr(C, packed)]
#[derive(Default, Debug)]
pub struct X86ExceptionContext {
pub regs: X86GeneralRegs,
pub vector: usize,
pub error_code: usize,
pub frame: X86InterruptFrame,
}

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Default, Debug)]
#[repr(C, packed)]
struct IdtEntry {
low: u64,
Expand Down
7 changes: 6 additions & 1 deletion src/cpu/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Author: Roy Hopkins <rhopkins@suse.de>

#[repr(C, packed)]
#[derive(Default, Debug)]
#[derive(Default, Debug, Clone)]
pub struct X86GeneralRegs {
pub r15: usize,
pub r14: usize,
Expand All @@ -24,6 +24,8 @@ pub struct X86GeneralRegs {
pub rax: usize,
}

impl Copy for X86GeneralRegs {}

#[repr(C, packed)]
#[derive(Default, Debug)]
pub struct X86SegmentRegs {
Expand All @@ -36,10 +38,13 @@ pub struct X86SegmentRegs {
}

#[repr(C, packed)]
#[derive(Default, Debug, Clone)]
pub struct X86InterruptFrame {
pub rip: usize,
pub cs: usize,
pub flags: usize,
pub rsp: usize,
pub ss: usize,
}

impl Copy for X86InterruptFrame {}
2 changes: 1 addition & 1 deletion src/cpu/tss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::address::VirtAddr;
pub const _IST_INVALID: usize = 0;
pub const IST_DF: usize = 1;

#[derive(Debug)]
#[derive(Debug, Default)]
#[repr(C, packed)]
pub struct X86Tss {
reserved1: u32,
Expand Down
3 changes: 2 additions & 1 deletion src/debug/stacktrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use core::arch::asm;
use core::mem;

#[cfg(feature = "enable-stacktrace")]
#[derive(Debug, Default)]
struct StackBounds {
bottom: VirtAddr,
top: VirtAddr,
Expand All @@ -31,7 +32,7 @@ impl StackBounds {
}
}

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Default)]
pub struct StackFrame {
pub rbp: VirtAddr,
pub rsp: VirtAddr,
Expand Down
12 changes: 8 additions & 4 deletions src/elf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ use core::fmt;
use core::matches;
use core::mem;

#[derive(Debug)]
#[derive(Debug, Default)]
pub enum ElfError {
#[default]
FileTooShort,

InvalidAddressRange,
Expand Down Expand Up @@ -182,7 +183,7 @@ pub type Elf64Xword = u64;
pub type Elf64Sxword = i64;
pub type Elf64char = u8;

#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq, Debug, Default)]
pub struct Elf64AddrRange {
pub vaddr_begin: Elf64Addr,
pub vaddr_end: Elf64Addr,
Expand Down Expand Up @@ -228,6 +229,7 @@ impl cmp::PartialOrd for Elf64AddrRange {
}
}

#[derive(Default, Debug)]
pub struct Elf64FileRange {
pub offset_begin: usize,
pub offset_end: usize,
Expand All @@ -249,6 +251,7 @@ impl convert::TryFrom<(Elf64Off, Elf64Xword)> for Elf64FileRange {
}
}

#[derive(Default, Debug)]
pub struct Elf64File<'a> {
elf_file_buf: &'a [u8],
elf_hdr: Elf64Hdr,
Expand Down Expand Up @@ -645,7 +648,7 @@ impl<'a> Elf64File<'a> {
}
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Elf64Hdr {
#[allow(unused)]
e_ident: [Elf64char; 16],
Expand Down Expand Up @@ -954,7 +957,7 @@ impl Elf64Shdr {
}
}

#[derive(Debug)]
#[derive(Debug, Default)]
struct Elf64LoadSegments {
segments: Vec<(Elf64AddrRange, Elf64Half)>,
}
Expand Down Expand Up @@ -1237,6 +1240,7 @@ impl<'a> Iterator for Elf64ImageLoadSegmentIterator<'a> {
}
}

#[derive(Debug, Default)]
struct Elf64Strtab<'a> {
strtab_buf: &'a [u8],
}
Expand Down
3 changes: 2 additions & 1 deletion src/fs/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ use packit::PackItError;
const MAX_FILENAME_LENGTH: usize = 64;
pub type FileName = FixedString<MAX_FILENAME_LENGTH>;

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Default)]
pub enum FsError {
#[default]
Inval,
FileExists,
FileNotFound,
Expand Down
2 changes: 1 addition & 1 deletion src/fs/ramfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use alloc::vec::Vec;

use core::cmp::{max, min};

#[derive(Debug)]
#[derive(Debug, Default)]
struct RawRamFile {
capacity: usize,
size: usize,
Expand Down
3 changes: 2 additions & 1 deletion src/fw_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ pub struct FwCfg<'a> {
driver: &'a dyn IOPort,
}

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Default)]
pub enum FwCfgError {
#[default]
// Could not find the appropriate file selector.
FileNotFound,
// Unexpected file size.
Expand Down
1 change: 1 addition & 0 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub trait IOPort {
}
}

#[derive(Default, Debug)]
pub struct DefaultIOPort {}

impl IOPort for DefaultIOPort {}
Expand Down
10 changes: 5 additions & 5 deletions src/mm/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ impl Page {
}
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct MemInfo {
total_pages: [usize; MAX_ORDER],
free_pages: [usize; MAX_ORDER],
}

#[derive(Debug)]
#[derive(Debug, Default)]
struct MemoryRegion {
start_phys: PhysAddr,
start_virt: VirtAddr,
Expand Down Expand Up @@ -819,7 +819,7 @@ pub fn memory_info() -> MemInfo {
ROOT_MEM.lock().memory_info()
}

#[derive(Debug)]
#[derive(Debug, Default)]
struct SlabPage {
vaddr: VirtAddr,
capacity: u16,
Expand Down Expand Up @@ -925,7 +925,7 @@ impl SlabPage {
}
}

#[derive(Debug)]
#[derive(Debug, Default)]
#[repr(align(16))]
struct SlabCommon {
item_size: u16,
Expand Down Expand Up @@ -1119,7 +1119,7 @@ impl SlabPageSlab {
}
}

#[derive(Debug)]
#[derive(Debug, Default)]
struct Slab {
common: SlabCommon,
}
Expand Down
11 changes: 10 additions & 1 deletion src/mm/pagetable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ bitflags! {
}

#[repr(C)]
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug, Default)]
pub struct PTEntry(PhysAddr);

impl PTEntry {
Expand Down Expand Up @@ -158,10 +158,18 @@ impl PTEntry {
}

#[repr(C)]
#[derive(Debug)]
pub struct PTPage {
entries: [PTEntry; ENTRY_COUNT],
}

impl Default for PTPage {
fn default() -> Self {
let entries = [PTEntry::default(); ENTRY_COUNT];
PTPage { entries }
}
}

impl Index<usize> for PTPage {
type Output = PTEntry;

Expand All @@ -184,6 +192,7 @@ pub enum Mapping<'a> {
}

#[repr(C)]
#[derive(Default, Debug)]
pub struct PageTable {
root: PTPage,
}
Expand Down
2 changes: 1 addition & 1 deletion src/mm/virtualrange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::{
pub const VIRT_ALIGN_4K: usize = PAGE_SHIFT - 12;
pub const VIRT_ALIGN_2M: usize = PAGE_SHIFT_2M - 12;

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct VirtualRange {
start_virt: VirtAddr,
page_count: usize,
Expand Down
1 change: 1 addition & 0 deletions src/protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod errors;

use crate::sev::vmsa::{GuestVMExit, VMSA};

#[derive(Debug, Default)]
pub struct RequestParams {
pub guest_exit_code: GuestVMExit,
sev_features: u64,
Expand Down
3 changes: 3 additions & 0 deletions src/sev/ghcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,19 @@ const OFF_VERSION: u16 = 0xffa;
const OFF_USAGE: u16 = 0xffc;

#[repr(C, packed)]
#[derive(Debug, Default)]
pub struct PageStateChangeHeader {
cur_entry: u16,
end_entry: u16,
reserved: u32,
}

#[derive(Debug, Default)]
pub enum PageStateChangeOp {
PscPrivate,
PscShared,
PscPsmash,
#[default]
PscUnsmash,
}

Expand Down
Loading

0 comments on commit d303b34

Please sign in to comment.