loader: fix most clippy warnings

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-06-15 12:03:59 +08:00
parent a6c45339e2
commit c1c31dbfe4
4 changed files with 54 additions and 53 deletions

View file

@ -26,7 +26,7 @@ impl KernelArguments {
} }
} }
pub fn iter(&self) -> KernelArgumentsIterator { pub fn iter(self) -> KernelArgumentsIterator {
KernelArgumentsIterator { KernelArgumentsIterator {
base: self.base, base: self.base,
size: self.size(), size: self.size(),
@ -34,7 +34,7 @@ impl KernelArguments {
} }
} }
pub fn size(&self) -> usize { pub fn size(self) -> usize {
let s = unsafe { self.base.add(2).read() * 4 }; let s = unsafe { self.base.add(2).read() * 4 };
s as usize s as usize
} }

View file

@ -7,7 +7,6 @@ impl Uart {
unsafe { unsafe {
// Wait until TXFULL is `0` // Wait until TXFULL is `0`
while self.base.add(1).read_volatile() != 0 { while self.base.add(1).read_volatile() != 0 {
()
} }
self.base.add(0).write_volatile(c as u32) self.base.add(0).write_volatile(c as u32)
}; };

View file

@ -109,11 +109,11 @@ impl Default for BootConfig {
BootConfig { BootConfig {
no_copy: false, no_copy: false,
debug: false, debug: false,
base_addr: 0 as *const usize, base_addr: core::ptr::null::<usize>(),
regions: Default::default(), regions: Default::default(),
sram_start: 0 as *mut usize, sram_start: core::ptr::null_mut::<usize>(),
sram_size: 0, sram_size: 0,
args: KernelArguments::new(0 as *const usize), args: KernelArguments::new(core::ptr::null::<usize>()),
init_size: 0, init_size: 0,
extra_pages: 0, extra_pages: 0,
runtime_page_tracker: Default::default(), runtime_page_tracker: Default::default(),
@ -159,6 +159,10 @@ impl MiniElfSection {
len as usize len as usize
} }
pub fn is_empty(&self) -> bool {
false
}
pub fn flags(&self) -> usize { pub fn flags(&self) -> usize {
let le_bytes = self.size_and_flags >> 24; let le_bytes = self.size_and_flags >> 24;
le_bytes as usize le_bytes as usize
@ -225,7 +229,7 @@ impl MiniElf {
// Ensure the pagetables are mapped as well // Ensure the pagetables are mapped as well
let pt_addr = allocator.alloc() as usize; let pt_addr = allocator.alloc() as usize;
allocator.map_page(satp, pt_addr, PAGE_TABLE_OFFSET + 0, FLG_R | FLG_W); allocator.map_page(satp, pt_addr, PAGE_TABLE_OFFSET, FLG_R | FLG_W);
// Allocate stack pages. // Allocate stack pages.
for i in 0..STACK_PAGE_COUNT { for i in 0..STACK_PAGE_COUNT {
@ -303,10 +307,10 @@ impl MiniElf {
page_addr = previous_addr & !(PAGE_SIZE - 1); page_addr = previous_addr & !(PAGE_SIZE - 1);
} }
let ref mut process = allocator.processes[pid as usize - 1]; let mut process = &mut allocator.processes[pid as usize - 1];
process.entrypoint = self.entry_point as usize; process.entrypoint = self.entry_point as usize;
process.sp = stack_addr; process.sp = stack_addr;
process.satp = 0x80000000 | ((pid as usize) << 22) | (satp_address >> 12); process.satp = 0x8000_0000 | ((pid as usize) << 22) | (satp_address >> 12);
allocated_bytes allocated_bytes
} }
@ -400,7 +404,7 @@ impl ProgramDescription {
// Ensure the pagetables are mapped as well // Ensure the pagetables are mapped as well
let pt_addr = allocator.alloc() as usize; let pt_addr = allocator.alloc() as usize;
allocator.map_page(satp, pt_addr, PAGE_TABLE_OFFSET + 0, FLG_R | FLG_W); allocator.map_page(satp, pt_addr, PAGE_TABLE_OFFSET, FLG_R | FLG_W);
// Allocate stack pages. // Allocate stack pages.
for i in 0..STACK_PAGE_COUNT { for i in 0..STACK_PAGE_COUNT {
@ -476,18 +480,18 @@ impl ProgramDescription {
// Our "earlyprintk" equivalent // Our "earlyprintk" equivalent
if cfg!(feature = "earlyprintk") && is_kernel { if cfg!(feature = "earlyprintk") && is_kernel {
allocator.map_page(satp, 0xF0002000, 0xffcf_0000, FLG_R | FLG_W); allocator.map_page(satp, 0xF000_2000, 0xffcf_0000, FLG_R | FLG_W);
allocator.change_owner(pid as XousPid, 0xF0002000); allocator.change_owner(pid as XousPid, 0xF000_2000);
} }
let ref mut process = allocator.processes[pid_idx]; let mut process = &mut allocator.processes[pid_idx];
process.entrypoint = self.entrypoint as usize; process.entrypoint = self.entrypoint as usize;
process.sp = stack_addr; process.sp = stack_addr;
process.satp = 0x80000000 | ((pid as usize) << 22) | (satp_address >> 12); process.satp = 0x8000_0000 | ((pid as usize) << 22) | (satp_address >> 12);
} }
} }
pub unsafe fn bzero<T>(mut sbss: *mut T, ebss: *mut T) unsafe fn bzero<T>(mut sbss: *mut T, ebss: *mut T)
where where
T: Copy, T: Copy,
{ {
@ -500,7 +504,7 @@ where
} }
/// Copy _count_ **bytes** from src to dest. /// Copy _count_ **bytes** from src to dest.
pub unsafe fn memcpy<T>(dest: *mut T, src: *const T, count: usize) unsafe fn memcpy<T>(dest: *mut T, src: *const T, count: usize)
where where
T: Copy, T: Copy,
{ {
@ -516,7 +520,7 @@ where
while offset < (count / mem::size_of::<T>()) { while offset < (count / mem::size_of::<T>()) {
dest.add(offset) dest.add(offset)
.write_volatile(src.add(offset).read_volatile()); .write_volatile(src.add(offset).read_volatile());
offset = offset + 1; offset += 1
} }
} }
@ -524,7 +528,7 @@ pub fn read_initial_config(cfg: &mut BootConfig) {
let args = cfg.args; let args = cfg.args;
let mut i = args.iter(); let mut i = args.iter();
let xarg = i.next().expect("couldn't read initial tag"); let xarg = i.next().expect("couldn't read initial tag");
if xarg.name != make_type!("XArg") || xarg.size != 20 { if xarg.name != u32::from_le_bytes(*b"XArg") || xarg.size != 20 {
panic!("XArg wasn't first tag, or was invalid size"); panic!("XArg wasn't first tag, or was invalid size");
} }
cfg.sram_start = xarg.data[2] as *mut usize; cfg.sram_start = xarg.data[2] as *mut usize;
@ -534,32 +538,32 @@ pub fn read_initial_config(cfg: &mut BootConfig) {
let mut init_seen = false; let mut init_seen = false;
for tag in i { for tag in i {
if tag.name == make_type!("MREx") { if tag.name == u32::from_le_bytes(*b"MREx") {
cfg.regions = unsafe { cfg.regions = unsafe {
slice::from_raw_parts( slice::from_raw_parts(
tag.data.as_ptr() as *const MemoryRegionExtra, tag.data.as_ptr() as *const MemoryRegionExtra,
tag.size as usize / mem::size_of::<MemoryRegionExtra>(), tag.size as usize / mem::size_of::<MemoryRegionExtra>(),
) )
}; };
} else if tag.name == make_type!("Bflg") { } else if tag.name == u32::from_le_bytes(*b"Bflg") {
let boot_flags = tag.data[0]; let boot_flags = tag.data[0];
if boot_flags & (1 << 0) != 0 { if boot_flags & 1 != 0 {
cfg.no_copy = true; cfg.no_copy = true;
} }
if boot_flags & (1 << 1) != 0 { if boot_flags & (1 << 1) != 0 {
cfg.base_addr = 0 as *const usize; cfg.base_addr = core::ptr::null::<usize>();
} }
if boot_flags & (1 << 2) != 0 { if boot_flags & (1 << 2) != 0 {
cfg.debug = true; cfg.debug = true;
} }
} else if tag.name == make_type!("XKrn") { } else if tag.name == u32::from_le_bytes(*b"XKrn") {
assert!(!kernel_seen, "kernel appears twice"); assert!(!kernel_seen, "kernel appears twice");
assert!( assert!(
tag.size as usize == mem::size_of::<ProgramDescription>(), tag.size as usize == mem::size_of::<ProgramDescription>(),
"invalid XKrn size" "invalid XKrn size"
); );
kernel_seen = true; kernel_seen = true;
} else if tag.name == make_type!("IniE") { } else if tag.name == u32::from_le_bytes(*b"IniE") {
assert!(tag.size >= 4, "invalid Init size"); assert!(tag.size >= 4, "invalid Init size");
init_seen = true; init_seen = true;
cfg.init_process_count += 1; cfg.init_process_count += 1;
@ -574,10 +578,10 @@ pub fn read_initial_config(cfg: &mut BootConfig) {
/// located at the end of memory space. /// located at the end of memory space.
fn copy_processes(cfg: &mut BootConfig) { fn copy_processes(cfg: &mut BootConfig) {
for tag in cfg.args.iter() { for tag in cfg.args.iter() {
if tag.name == make_type!("IniE") { if tag.name == u32::from_le_bytes(*b"IniE") {
let mut page_addr: usize = 0; let mut page_addr: usize = 0;
let mut previous_addr: usize = 0; let mut previous_addr: usize = 0;
let mut top = 0 as *mut u8; let mut top = core::ptr::null_mut::<u8>();
let inie = MiniElf::new(&tag); let inie = MiniElf::new(&tag);
let mut src_addr = unsafe { let mut src_addr = unsafe {
@ -717,7 +721,7 @@ fn copy_processes(cfg: &mut BootConfig) {
top.add(PAGE_SIZE as usize), top.add(PAGE_SIZE as usize),
) )
}; };
} else if tag.name == make_type!("XKrn") { } else if tag.name == u32::from_le_bytes(*b"XKrn") {
let prog = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) }; let prog = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) };
// TEXT SECTION // TEXT SECTION
@ -792,12 +796,10 @@ fn copy_processes(cfg: &mut BootConfig) {
impl BootConfig { impl BootConfig {
fn get_top(&self) -> *mut usize { fn get_top(&self) -> *mut usize {
let val = unsafe { let val = unsafe {
let t = self.sram_start.add( self.sram_start.add(
(self.sram_size - self.init_size - self.extra_pages * PAGE_SIZE) (self.sram_size - self.init_size - self.extra_pages * PAGE_SIZE)
/ mem::size_of::<usize>(), / mem::size_of::<usize>(),
); )
// println!("top address: {:08x}", t as usize);
t
}; };
assert!((val as usize) >= (self.sram_start as usize)); assert!((val as usize) >= (self.sram_start as usize));
assert!( assert!(
@ -874,11 +876,11 @@ impl BootConfig {
pub fn map_page_32(&mut self, root: &mut PageTable, phys: usize, virt: usize, flags: usize) { pub fn map_page_32(&mut self, root: &mut PageTable, phys: usize, virt: usize, flags: usize) {
let ppn1 = (phys >> 22) & ((1 << 12) - 1); let ppn1 = (phys >> 22) & ((1 << 12) - 1);
let ppn0 = (phys >> 12) & ((1 << 10) - 1); let ppn0 = (phys >> 12) & ((1 << 10) - 1);
let ppo = (phys >> 0) & ((1 << 12) - 1); let ppo = (phys) & ((1 << 12) - 1);
let vpn1 = (virt >> 22) & ((1 << 10) - 1); let vpn1 = (virt >> 22) & ((1 << 10) - 1);
let vpn0 = (virt >> 12) & ((1 << 10) - 1); let vpn0 = (virt >> 12) & ((1 << 10) - 1);
let vpo = (virt >> 0) & ((1 << 12) - 1); let vpo = (virt) & ((1 << 12) - 1);
assert!(ppn1 < 4096); assert!(ppn1 < 4096);
assert!(ppn0 < 1024); assert!(ppn0 < 1024);
@ -887,7 +889,7 @@ impl BootConfig {
assert!(vpn0 < 1024); assert!(vpn0 < 1024);
assert!(vpo < 4096); assert!(vpo < 4096);
let ref mut l1_pt = root.entries; let l1_pt = &mut root.entries;
let mut new_addr = 0; let mut new_addr = 0;
// Allocate a new level 1 pagetable entry if one doesn't exist. // Allocate a new level 1 pagetable entry if one doesn't exist.
@ -900,7 +902,7 @@ impl BootConfig {
let l0_pt_idx = let l0_pt_idx =
unsafe { &mut (*(((l1_pt[vpn1] << 2) & !((1 << 12) - 1)) as *mut PageTable)) }; unsafe { &mut (*(((l1_pt[vpn1] << 2) & !((1 << 12) - 1)) as *mut PageTable)) };
let ref mut l0_pt = l0_pt_idx.entries; let l0_pt = &mut l0_pt_idx.entries;
// Ensure the entry hasn't already been mapped. // Ensure the entry hasn't already been mapped.
// if l0_pt[vpn0] & 1 != 0 { // if l0_pt[vpn0] & 1 != 0 {
@ -1007,7 +1009,7 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
// where in heap this memory will go. // where in heap this memory will go.
let mut cfg = BootConfig { let mut cfg = BootConfig {
base_addr: args.base as *const usize, base_addr: args.base as *const usize,
args: args, args,
..Default::default() ..Default::default()
}; };
read_initial_config(&mut cfg); read_initial_config(&mut cfg);
@ -1101,7 +1103,7 @@ pub fn phase_2(cfg: &mut BootConfig) {
// page tables and mapping memory to them. // page tables and mapping memory to them.
let mut pid = 2; let mut pid = 2;
for tag in args.iter() { for tag in args.iter() {
if tag.name == make_type!("IniE") { if tag.name == u32::from_le_bytes(*b"IniE") {
let inie = MiniElf::new(&tag); let inie = MiniElf::new(&tag);
println!("Mapping program into memory"); println!("Mapping program into memory");
// let init = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) }; // let init = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) };
@ -1109,7 +1111,7 @@ pub fn phase_2(cfg: &mut BootConfig) {
// + (((init.data_size + init.bss_size) as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1)); // + (((init.data_size + init.bss_size) as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1));
process_offset -= inie.load(cfg, process_offset, pid); process_offset -= inie.load(cfg, process_offset, pid);
pid += 1; pid += 1;
} else if tag.name == make_type!("XKrn") { } else if tag.name == u32::from_le_bytes(*b"XKrn") {
println!("Mapping kernel into memory"); println!("Mapping kernel into memory");
let xkrn = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) }; let xkrn = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) };
let load_size_rounded = ((xkrn.text_size as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1)) let load_size_rounded = ((xkrn.text_size as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1))
@ -1149,13 +1151,13 @@ pub fn phase_2(cfg: &mut BootConfig) {
println!("PID1 pagetables:"); println!("PID1 pagetables:");
debug::print_pagetable(cfg.processes[0].satp); debug::print_pagetable(cfg.processes[0].satp);
println!(""); println!();
println!(""); println!();
for (_pid, process) in cfg.processes[1..].iter().enumerate() { for (_pid, process) in cfg.processes[1..].iter().enumerate() {
println!("PID{} pagetables:", _pid + 2); println!("PID{} pagetables:", _pid + 2);
debug::print_pagetable(process.satp); debug::print_pagetable(process.satp);
println!(""); println!();
println!(""); println!();
} }
println!( println!(
"Runtime Page Tracker: {} bytes", "Runtime Page Tracker: {} bytes",

View file

@ -129,7 +129,7 @@ fn parse_args_bin() {
let mut ka_iter = ka.iter(); let mut ka_iter = ka.iter();
let ka_first = ka_iter.next().expect("kernel args has no first tag"); let ka_first = ka_iter.next().expect("kernel args has no first tag");
assert_eq!(ka_first.name, make_type!("XArg"), "first tag was not valid"); assert_eq!(ka_first.name, u32::from_le_bytes(*b"XArg"), "first tag was not valid");
assert_eq!(ka_first.size, 20, "first tag had invalid size"); assert_eq!(ka_first.size, 20, "first tag had invalid size");
assert_eq!(ka_first.data[1], 1, "tag version number unexpected"); assert_eq!(ka_first.data[1], 1, "tag version number unexpected");
@ -167,7 +167,7 @@ fn read_initial_config() {
} }
fn read_word(satp: usize, virt: usize) -> Result<u32, &'static str> { fn read_word(satp: usize, virt: usize) -> Result<u32, &'static str> {
if satp & 0x80000000 != 0x80000000 { if satp & 0x8000_0000 != 0x8000_0000 {
return Err("satp valid bit isn't set"); return Err("satp valid bit isn't set");
} }
// let ppn1 = (phys >> 22) & ((1 << 12) - 1); // let ppn1 = (phys >> 22) & ((1 << 12) - 1);
@ -176,7 +176,7 @@ fn read_word(satp: usize, virt: usize) -> Result<u32, &'static str> {
let vpn1 = (virt >> 22) & ((1 << 10) - 1); let vpn1 = (virt >> 22) & ((1 << 10) - 1);
let vpn0 = (virt >> 12) & ((1 << 10) - 1); let vpn0 = (virt >> 12) & ((1 << 10) - 1);
let vpo = (virt >> 0) & ((1 << 12) - 1); let vpo = (virt) & ((1 << 12) - 1);
let l1_pt = unsafe { &mut (*((satp << 12) as *mut crate::PageTable)) }; let l1_pt = unsafe { &mut (*((satp << 12) as *mut crate::PageTable)) };
let l1_entry = l1_pt.entries[vpn1]; let l1_entry = l1_pt.entries[vpn1];
@ -196,7 +196,7 @@ fn read_word(satp: usize, virt: usize) -> Result<u32, &'static str> {
} }
fn read_byte(satp: usize, virt: usize) -> Result<u8, &'static str> { fn read_byte(satp: usize, virt: usize) -> Result<u8, &'static str> {
let word = read_word(satp, virt & 0xfffffffc)?; let word = read_word(satp, virt & 0xffff_fffc)?;
Ok(word.to_le_bytes()[virt & 3]) Ok(word.to_le_bytes()[virt & 3])
} }
@ -320,12 +320,12 @@ fn full_boot() {
let mut xkrn_inspected = false; let mut xkrn_inspected = false;
let mut init_index = 0; let mut init_index = 0;
for arg in env.cfg.args.iter() { for arg in env.cfg.args.iter() {
if arg.name == make_type!("XKrn") { if arg.name == u32::from_le_bytes(*b"XKrn") {
verify_kernel(&env.cfg, 0, &arg); verify_kernel(&env.cfg, 0, &arg);
assert!(xkrn_inspected == false, "multiple kernels found"); assert!(!xkrn_inspected, "multiple kernels found");
xkrn_inspected = true; xkrn_inspected = true;
println!("Kernel PASS"); println!("Kernel PASS");
} else if arg.name == make_type!("IniE") { } else if arg.name == u32::from_le_bytes(*b"IniE") {
init_index += 1; init_index += 1;
verify_program(&env.cfg, init_index, &arg); verify_program(&env.cfg, init_index, &arg);
println!("PID {} PASS", init_index + 1); println!("PID {} PASS", init_index + 1);
@ -348,12 +348,12 @@ fn spanning_section() {
let mut xkrn_inspected = false; let mut xkrn_inspected = false;
let mut init_index = 0; let mut init_index = 0;
for arg in env.cfg.args.iter() { for arg in env.cfg.args.iter() {
if arg.name == make_type!("XKrn") { if arg.name == u32::from_le_bytes(*b"XKrn") {
verify_kernel(&env.cfg, 0, &arg); verify_kernel(&env.cfg, 0, &arg);
assert!(xkrn_inspected == false, "multiple kernels found"); assert!(!xkrn_inspected, "multiple kernels found");
xkrn_inspected = true; xkrn_inspected = true;
println!("Kernel PASS"); println!("Kernel PASS");
} else if arg.name == make_type!("IniE") { } else if arg.name == u32::from_le_bytes(*b"IniE") {
init_index += 1; init_index += 1;
verify_program(&env.cfg, init_index, &arg); verify_program(&env.cfg, init_index, &arg);
println!("PID {} PASS", init_index + 1); println!("PID {} PASS", init_index + 1);
@ -406,5 +406,5 @@ pub unsafe extern "C" fn start_kernel(
_entrypoint: usize, _entrypoint: usize,
_stack: usize, _stack: usize,
) -> ! { ) -> ! {
loop {} panic!("not running natively");
} }