Skip to content

Commit

Permalink
fix cargo clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
wangrunji0408 committed Mar 1, 2021
1 parent ddde5ae commit 9722caf
Show file tree
Hide file tree
Showing 20 changed files with 89 additions and 81 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ make rootfs
Run native Linux program (Busybox):

```sh
cargo run --release -p linux-loader /bin/busybox [args]
cargo run --release -p linux-loader -- /bin/busybox [args]
```

Run native Zircon program (shell):

```sh
cargo run --release -p zircon-loader prebuilt/zircon/x64
cargo run --release -p zircon-loader -- prebuilt/zircon/x64
```

Run Linux shell on bare-metal (zCore):
Expand Down
10 changes: 5 additions & 5 deletions kernel-hal-bare/src/arch/riscv.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use kernel_hal::{PageTableTrait, PhysAddr, VirtAddr};
use kernel_hal::{PageTableTrait, PhysAddr, VirtAddr, Result};
use riscv::addr::Page;
use riscv::paging::{PageTableFlags as PTF, *};
use riscv::register::satp;
Expand Down Expand Up @@ -46,7 +46,7 @@ impl PageTableImpl {
impl PageTableTrait for PageTableImpl {
/// Map the page of `vaddr` to the frame of `paddr` with `flags`.
#[export_name = "hal_pt_map"]
fn map(&mut self, vaddr: VirtAddr, paddr: PhysAddr, flags: MMUFlags) -> Result<(), ()> {
fn map(&mut self, vaddr: VirtAddr, paddr: PhysAddr, flags: MMUFlags) -> Result<()> {
let mut pt = self.get();
let page = Page::of_addr(vaddr);
let frame = riscv::addr::Frame::of_addr(riscv::addr::PhysAddr::new(paddr));
Expand All @@ -59,7 +59,7 @@ impl PageTableTrait for PageTableImpl {

/// Unmap the page of `vaddr`.
#[export_name = "hal_pt_unmap"]
fn unmap(&mut self, vaddr: VirtAddr) -> Result<(), ()> {
fn unmap(&mut self, vaddr: VirtAddr) -> Result<()> {
let mut pt = self.get();
let page = Page::of_addr(riscv::addr::VirtAddr::new(vaddr));
pt.unmap(page).unwrap().1.flush();
Expand All @@ -69,7 +69,7 @@ impl PageTableTrait for PageTableImpl {

/// Change the `flags` of the page of `vaddr`.
#[export_name = "hal_pt_protect"]
fn protect(&mut self, vaddr: VirtAddr, flags: MMUFlags) -> Result<(), ()> {
fn protect(&mut self, vaddr: VirtAddr, flags: MMUFlags) -> Result<()> {
let mut pt = self.get();
let page = Page::of_addr(riscv::addr::VirtAddr::new(vaddr));
pt.update_flags(page, flags.to_ptf()).unwrap().flush();
Expand All @@ -79,7 +79,7 @@ impl PageTableTrait for PageTableImpl {

/// Query the physical address which the page of `vaddr` maps to.
#[export_name = "hal_pt_query"]
fn query(&mut self, vaddr: VirtAddr) -> Result<riscv::addr::PhysAddr, ()> {
fn query(&mut self, vaddr: VirtAddr) -> Result<riscv::addr::PhysAddr> {
let mut pt = self.get();
let page = Page::of_addr(riscv::addr::VirtAddr::new(vaddr));
let res = pt.ref_entry(page);
Expand Down
16 changes: 8 additions & 8 deletions kernel-hal-bare/src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {
core::ptr::NonNull,
core::time::Duration,
git_version::git_version,
kernel_hal::PageTableTrait,
kernel_hal::{Result, HalError, PageTableTrait},
rcore_console::{Console, ConsoleOnGraphic, DrawTarget, Pixel, Rgb888, Size},
spin::Mutex,
uart_16550::SerialPort,
Expand Down Expand Up @@ -64,7 +64,7 @@ impl PageTableImpl {
impl PageTableTrait for PageTableImpl {
/// Map the page of `vaddr` to the frame of `paddr` with `flags`.
#[export_name = "hal_pt_map"]
fn map(&mut self, vaddr: VirtAddr, paddr: PhysAddr, flags: MMUFlags) -> Result<(), ()> {
fn map(&mut self, vaddr: VirtAddr, paddr: PhysAddr, flags: MMUFlags) -> Result<()> {
let mut pt = self.get();
unsafe {
pt.map_to_with_table_flags(
Expand All @@ -89,7 +89,7 @@ impl PageTableTrait for PageTableImpl {

/// Unmap the page of `vaddr`.
#[export_name = "hal_pt_unmap"]
fn unmap(&mut self, vaddr: VirtAddr) -> Result<(), ()> {
fn unmap(&mut self, vaddr: VirtAddr) -> Result<()> {
let mut pt = self.get();
let page =
Page::<Size4KiB>::from_start_address(x86_64::VirtAddr::new(vaddr as u64)).unwrap();
Expand All @@ -113,15 +113,15 @@ impl PageTableTrait for PageTableImpl {
"unmap failed: {:x?} err={:x?} in {:#x?}",
vaddr, err, self.root_paddr
);
return Err(());
return Err(HalError);
}
}
Ok(())
}

/// Change the `flags` of the page of `vaddr`.
#[export_name = "hal_pt_protect"]
fn protect(&mut self, vaddr: VirtAddr, flags: MMUFlags) -> Result<(), ()> {
fn protect(&mut self, vaddr: VirtAddr, flags: MMUFlags) -> Result<()> {
let mut pt = self.get();
let page =
Page::<Size4KiB>::from_start_address(x86_64::VirtAddr::new(vaddr as u64)).unwrap();
Expand All @@ -134,11 +134,11 @@ impl PageTableTrait for PageTableImpl {

/// Query the physical address which the page of `vaddr` maps to.
#[export_name = "hal_pt_query"]
fn query(&mut self, vaddr: VirtAddr) -> Result<PhysAddr, ()> {
fn query(&mut self, vaddr: VirtAddr) -> Result<PhysAddr> {
let pt = self.get();
let ret = pt
.translate_addr(x86_64::VirtAddr::new(vaddr as u64))
.map(|addr| addr.as_u64() as PhysAddr).ok_or(());
.map(|addr| addr.as_u64() as PhysAddr).ok_or(HalError);
trace!("query: {:x?} => {:x?}", vaddr, ret);
ret
}
Expand Down Expand Up @@ -236,7 +236,7 @@ struct Framebuffer {
impl DrawTarget<Rgb888> for Framebuffer {
type Error = core::convert::Infallible;

fn draw_pixel(&mut self, item: Pixel<Rgb888>) -> Result<(), Self::Error> {
fn draw_pixel(&mut self, item: Pixel<Rgb888>) -> core::result::Result<(), Self::Error> {
let idx = (item.0.x as u32 + item.0.y as u32 * self.width) as usize;
self.buf[idx] = unsafe { core::mem::transmute(item.1) };
Ok(())
Expand Down
20 changes: 10 additions & 10 deletions kernel-hal-unix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl PageTable {
impl PageTableTrait for PageTable {
/// Map the page of `vaddr` to the frame of `paddr` with `flags`.
#[export_name = "hal_pt_map"]
fn map(&mut self, vaddr: VirtAddr, paddr: PhysAddr, flags: MMUFlags) -> Result<(), ()> {
fn map(&mut self, vaddr: VirtAddr, paddr: PhysAddr, flags: MMUFlags) -> Result<()> {
debug_assert!(page_aligned(vaddr));
debug_assert!(page_aligned(paddr));
let prot = flags.to_mmap_prot();
Expand All @@ -97,13 +97,13 @@ impl PageTableTrait for PageTable {

/// Unmap the page of `vaddr`.
#[export_name = "hal_pt_unmap"]
fn unmap(&mut self, vaddr: VirtAddr) -> Result<(), ()> {
fn unmap(&mut self, vaddr: VirtAddr) -> Result<()> {
self.unmap_cont(vaddr, 1)
}

/// Change the `flags` of the page of `vaddr`.
#[export_name = "hal_pt_protect"]
fn protect(&mut self, vaddr: VirtAddr, flags: MMUFlags) -> Result<(), ()> {
fn protect(&mut self, vaddr: VirtAddr, flags: MMUFlags) -> Result<()> {
debug_assert!(page_aligned(vaddr));
let prot = flags.to_mmap_prot();
let ret = unsafe { libc::mprotect(vaddr as _, PAGE_SIZE, prot) };
Expand All @@ -113,7 +113,7 @@ impl PageTableTrait for PageTable {

/// Query the physical address which the page of `vaddr` maps to.
#[export_name = "hal_pt_query"]
fn query(&mut self, vaddr: VirtAddr) -> Result<PhysAddr, ()> {
fn query(&mut self, vaddr: VirtAddr) -> Result<PhysAddr> {
debug_assert!(page_aligned(vaddr));
unimplemented!()
}
Expand All @@ -125,7 +125,7 @@ impl PageTableTrait for PageTable {
}

#[export_name = "hal_pt_unmap_cont"]
fn unmap_cont(&mut self, vaddr: VirtAddr, pages: usize) -> Result<(), ()> {
fn unmap_cont(&mut self, vaddr: VirtAddr, pages: usize) -> Result<()> {
if pages == 0 {
return Ok(());
}
Expand All @@ -142,7 +142,7 @@ pub struct PhysFrame {
}

impl Debug for PhysFrame {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::result::Result<(), std::fmt::Error> {
write!(f, "PhysFrame({:#x})", self.paddr)
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ impl Drop for PhysFrame {

fn phys_to_virt(paddr: PhysAddr) -> VirtAddr {
/// Map physical memory from here.
const PMEM_BASE: VirtAddr = 0x8_00000000;
const PMEM_BASE: VirtAddr = 0x8_0000_0000;

PMEM_BASE + paddr
}
Expand Down Expand Up @@ -247,7 +247,7 @@ fn page_aligned(x: VirtAddr) -> bool {
x % PAGE_SIZE == 0
}

const PMEM_SIZE: usize = 0x400_00000; // 1GiB
const PMEM_SIZE: usize = 0x4000_0000; // 1GiB

lazy_static! {
static ref FRAME_FILE: File = create_pmem_file();
Expand Down Expand Up @@ -302,11 +302,11 @@ fn mmap(fd: libc::c_int, offset: usize, len: usize, vaddr: VirtAddr, prot: libc:
}

trait FlagsExt {
fn to_mmap_prot(self) -> libc::c_int;
fn to_mmap_prot(&self) -> libc::c_int;
}

impl FlagsExt for MMUFlags {
fn to_mmap_prot(self) -> libc::c_int {
fn to_mmap_prot(&self) -> libc::c_int {
let mut flags = 0;
if self.contains(MMUFlags::READ) {
flags |= libc::PROT_READ;
Expand Down
31 changes: 19 additions & 12 deletions kernel-hal/src/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ use core::time::Duration;

type ThreadId = usize;

/// The error type which is returned from HAL functions.
#[derive(Debug)]
pub struct HalError;

/// The result type returned by HAL functions.
pub type Result<T> = core::result::Result<T, HalError>;

#[repr(C)]
pub struct Thread {
id: ThreadId,
Expand Down Expand Up @@ -49,16 +56,16 @@ pub fn context_run(_context: &mut UserContext) {

pub trait PageTableTrait: Sync + Send {
/// Map the page of `vaddr` to the frame of `paddr` with `flags`.
fn map(&mut self, _vaddr: VirtAddr, _paddr: PhysAddr, _flags: MMUFlags) -> Result<(), ()>;
fn map(&mut self, _vaddr: VirtAddr, _paddr: PhysAddr, _flags: MMUFlags) -> Result<()>;

/// Unmap the page of `vaddr`.
fn unmap(&mut self, _vaddr: VirtAddr) -> Result<(), ()>;
fn unmap(&mut self, _vaddr: VirtAddr) -> Result<()>;

/// Change the `flags` of the page of `vaddr`.
fn protect(&mut self, _vaddr: VirtAddr, _flags: MMUFlags) -> Result<(), ()>;
fn protect(&mut self, _vaddr: VirtAddr, _flags: MMUFlags) -> Result<()>;

/// Query the physical address which the page of `vaddr` maps to.
fn query(&mut self, _vaddr: VirtAddr) -> Result<PhysAddr, ()>;
fn query(&mut self, _vaddr: VirtAddr) -> Result<PhysAddr>;

/// Get the physical address of root page table.
fn table_phys(&self) -> PhysAddr;
Expand All @@ -68,7 +75,7 @@ pub trait PageTableTrait: Sync + Send {
mut vaddr: VirtAddr,
paddrs: &[PhysAddr],
flags: MMUFlags,
) -> Result<(), ()> {
) -> Result<()> {
for &paddr in paddrs {
self.map(vaddr, paddr, flags)?;
vaddr += PAGE_SIZE;
Expand All @@ -82,7 +89,7 @@ pub trait PageTableTrait: Sync + Send {
paddr: PhysAddr,
pages: usize,
flags: MMUFlags,
) -> Result<(), ()> {
) -> Result<()> {
for i in 0..pages {
let paddr = paddr + i * PAGE_SIZE;
self.map(vaddr, paddr, flags)?;
Expand All @@ -91,7 +98,7 @@ pub trait PageTableTrait: Sync + Send {
Ok(())
}

fn unmap_cont(&mut self, vaddr: VirtAddr, pages: usize) -> Result<(), ()> {
fn unmap_cont(&mut self, vaddr: VirtAddr, pages: usize) -> Result<()> {
for i in 0..pages {
self.unmap(vaddr + i * PAGE_SIZE)?;
}
Expand Down Expand Up @@ -126,25 +133,25 @@ impl PageTableTrait for PageTable {
/// Map the page of `vaddr` to the frame of `paddr` with `flags`.
#[linkage = "weak"]
#[export_name = "hal_pt_map"]
fn map(&mut self, _vaddr: VirtAddr, _paddr: PhysAddr, _flags: MMUFlags) -> Result<(), ()> {
fn map(&mut self, _vaddr: VirtAddr, _paddr: PhysAddr, _flags: MMUFlags) -> Result<()> {
unimplemented!()
}
/// Unmap the page of `vaddr`.
#[linkage = "weak"]
#[export_name = "hal_pt_unmap"]
fn unmap(&mut self, _vaddr: VirtAddr) -> Result<(), ()> {
fn unmap(&mut self, _vaddr: VirtAddr) -> Result<()> {
unimplemented!()
}
/// Change the `flags` of the page of `vaddr`.
#[linkage = "weak"]
#[export_name = "hal_pt_protect"]
fn protect(&mut self, _vaddr: VirtAddr, _flags: MMUFlags) -> Result<(), ()> {
fn protect(&mut self, _vaddr: VirtAddr, _flags: MMUFlags) -> Result<()> {
unimplemented!()
}
/// Query the physical address which the page of `vaddr` maps to.
#[linkage = "weak"]
#[export_name = "hal_pt_query"]
fn query(&mut self, _vaddr: VirtAddr) -> Result<PhysAddr, ()> {
fn query(&mut self, _vaddr: VirtAddr) -> Result<PhysAddr> {
unimplemented!()
}
/// Get the physical address of root page table.
Expand All @@ -155,7 +162,7 @@ impl PageTableTrait for PageTable {
}
#[linkage = "weak"]
#[export_name = "hal_pt_unmap_cont"]
fn unmap_cont(&mut self, vaddr: VirtAddr, pages: usize) -> Result<(), ()> {
fn unmap_cont(&mut self, vaddr: VirtAddr, pages: usize) -> Result<()> {
for i in 0..pages {
self.unmap(vaddr + i * PAGE_SIZE)?;
}
Expand Down
14 changes: 7 additions & 7 deletions linux-object/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ impl TryFrom<&str> for FileDesc {
}
}

impl Into<usize> for FileDesc {
fn into(self) -> usize {
self.0 as _
impl From<FileDesc> for usize {
fn from(f: FileDesc) -> Self {
f.0 as _
}
}

impl Into<i32> for FileDesc {
fn into(self) -> i32 {
self.0
impl From<FileDesc> for i32 {
fn from(f: FileDesc) -> Self {
f.0
}
}

Expand Down Expand Up @@ -221,7 +221,7 @@ pub fn split_path(path: &str) -> (&str, &str) {
let mut split = path.trim_end_matches('/').rsplitn(2, '/');
let file_name = split.next().unwrap();
let mut dir_path = split.next().unwrap_or(".");
if dir_path == "" {
if dir_path.is_empty() {
dir_path = "/";
}
(dir_path, file_name)
Expand Down
1 change: 1 addition & 0 deletions linux-object/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![no_std]
#![deny(warnings, unsafe_code, missing_docs)]
#![allow(clippy::upper_case_acronyms)]
#![feature(bool_to_option)]

extern crate alloc;
Expand Down
Loading

0 comments on commit 9722caf

Please sign in to comment.