Skip to content

Commit

Permalink
Add linux_object::fs::rcore_fs_wrapper
Browse files Browse the repository at this point in the history
And add directory zCore to cargo workspace
  • Loading branch information
equation314 committed Oct 25, 2021
1 parent 26dd63f commit 806c9d1
Show file tree
Hide file tree
Showing 26 changed files with 171 additions and 89 deletions.
51 changes: 22 additions & 29 deletions .github/workflows/rustc20210727.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
with:
submodules: 'recursive'
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand All @@ -27,13 +25,9 @@ jobs:
- name: Clippy LibOS
run: cargo clippy --all-features
- name: Clippy x86_64 bare-metal
run: |
cd zCore
make clippy ARCH=x86_64
run: cd zCore && make clippy ARCH=x86_64
- name: Clippy riscv64 bare-metal
run: |
cd zCore
make clippy ARCH=riscv64 LINUX=1
run: cd zCore && make clippy ARCH=riscv64 LINUX=1

build:
runs-on: ${{ matrix.os }}
Expand All @@ -48,13 +42,20 @@ jobs:
with:
profile: minimal
toolchain: nightly-2021-07-27
components: rust-src
- name: Build
run: cargo build
- name: Build zCore
run: |
cd zCore
make build ARCH=x86_64
components: rust-src, llvm-tools-preview
- uses: actions-rs/[email protected]
with:
crate: cargo-binutils
version: latest
use-tool-cache: true
- name: Build linux LibOS
run: cargo build --features "linux libos"
- name: Build zircon LibOS
run: cargo build --features "zircon libos"
- name: Build x86_64 bare-metal
run: cd zCore && make build ARCH=x86_64
- name: Build riscv64 bare-metal
run: cd zCore && make build ARCH=riscv64 LINUX=1

build-aarch64:
runs-on: ubuntu-20.04
Expand Down Expand Up @@ -101,10 +102,6 @@ jobs:
run: make rootfs
- name: Test
run: cargo test --no-fail-fast
# uses: actions-rs/cargo@v1
# with:
# command: test
# args: --all-features --no-fail-fast
env:
CARGO_INCREMENTAL: '0'
RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort'
Expand All @@ -130,9 +127,6 @@ jobs:
- uses: actions/checkout@v2
- name: Run benchmarks
run: cargo bench
# uses: actions-rs/cargo@v1
# with:
# command: bench

core-test:
runs-on: ubuntu-20.04
Expand Down Expand Up @@ -166,8 +160,6 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
with:
submodules: 'recursive'
- name: Pull prebuilt images
run: git lfs pull -I prebuilt/zircon/x64/core-tests.zbi,prebuilt/zircon/x64/libzircon-libos.so,prebuilt/zircon/x64/userboot-libos.so
- uses: actions-rs/toolchain@v1
Expand All @@ -185,8 +177,6 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
with:
submodules: 'recursive'
- name: Pull prebuilt images
run: git lfs pull -I prebuilt/linux/libc-libos.so
- name: Install musl toolchain
Expand Down Expand Up @@ -262,13 +252,16 @@ jobs:
with:
profile: minimal
toolchain: nightly-2021-07-27
components: rust-src
components: rust-src, llvm-tools-preview
- uses: actions-rs/[email protected]
with:
crate: cargo-binutils
version: latest
use-tool-cache: true
- name: Install cargo tools and qemu-system-riscv64
run: |
sudo apt update
sudo apt install qemu-utils
cargo install cargo-binutils
rustup component add llvm-tools-preview
wget https://github.com/rcore-os/qemu-prebuilt/releases/download/5.2.0-riscv64/qemu-system-riscv64.tar.xz > /dev/null
tar xJf qemu-system-riscv64.tar.xz && sudo cp qemu-system-riscv64 /usr/local/bin
wget https://github.com/rcore-os/qemu-prebuilt/releases/download/qemu-share/qemu-share.tar.xz > /dev/null
Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ members = [
"linux-loader",
"kernel-hal",
"drivers",
"zCore",
]

exclude = [
"zircon-user",
"zCore",
"rboot",
]

[profile.release]
lto = true
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ make rootfs
##### step2: Compile&Run native Linux program (Busybox) in libos mode:

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

You can add `--features graphic` as cargo arguments to show the graphical output (with [sdl2](https://www.libsdl.org) installed).
You can also add the feature `graphic` to show the graphical output (with [sdl2](https://www.libsdl.org) installed).

To debug, set `LOG` environment variable to one of `error`, `warn`, `info`, `debug`, `trace`.

#### Run native Zircon program (shell) in zircon-libos mode:

#### step1: Compile and Run Zircon shell

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

To debug, set `LOG` environment variable to one of `error`, `warn`, `info`, `debug`, `trace`.

### Run zcore in bare-metal mode
#### Run Linux shell in linux-bare-metal mode:
##### step1: Prepare Alpine Linux rootfs:
Expand Down
10 changes: 6 additions & 4 deletions drivers/src/irq/riscv_intc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,26 @@ impl IrqScheme for Intc {

fn mask(&self, cause: usize) -> DeviceResult {
unsafe {
Ok(match cause {
match cause {
S_SOFT => sie::clear_ssoft(),
S_TIMER => sie::clear_stimer(),
S_EXT => sie::clear_sext(),
_ => return Err(DeviceError::InvalidParam),
})
}
}
Ok(())
}

fn unmask(&self, cause: usize) -> DeviceResult {
unsafe {
Ok(match cause {
match cause {
S_SOFT => sie::set_ssoft(),
S_TIMER => sie::set_stimer(),
S_EXT => sie::set_sext(),
_ => return Err(DeviceError::InvalidParam),
})
}
}
Ok(())
}

fn register_handler(&self, cause: usize, handler: IrqHandler) -> DeviceResult {
Expand Down
6 changes: 4 additions & 2 deletions drivers/src/irq/riscv_plic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,17 @@ impl IrqScheme for Plic {

fn mask(&self, irq_num: usize) -> DeviceResult {
if self.is_valid_irq(irq_num) {
Ok(self.inner.lock().toggle(irq_num, false))
self.inner.lock().toggle(irq_num, false);
Ok(())
} else {
Err(DeviceError::InvalidParam)
}
}

fn unmask(&self, irq_num: usize) -> DeviceResult {
if self.is_valid_irq(irq_num) {
Ok(self.inner.lock().toggle(irq_num, true))
self.inner.lock().toggle(irq_num, true);
Ok(())
} else {
Err(DeviceError::InvalidParam)
}
Expand Down
4 changes: 2 additions & 2 deletions kernel-hal/src/bare/arch/riscv/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ pub(super) fn init() -> DeviceResult {
.expect("IRQ device 'riscv-intc' not initialized!");
irq.register_handler(
ScauseIntCode::SupervisorSoft as _,
Box::new(|| super::trap::super_soft()),
Box::new(super::trap::super_soft),
)?;
irq.register_handler(
ScauseIntCode::SupervisorTimer as _,
Box::new(|| super::trap::super_timer()),
Box::new(super::trap::super_timer),
)?;
irq.unmask(ScauseIntCode::SupervisorSoft as _)?;
irq.unmask(ScauseIntCode::SupervisorTimer as _)?;
Expand Down
5 changes: 1 addition & 4 deletions kernel-hal/src/bare/arch/x86_64/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ pub(super) fn init() -> DeviceResult {
drivers::all_uart().first_unwrap().upcast(),
)?;
irq.unmask(trap::X86_ISA_IRQ_COM1)?;
irq.register_local_apic_handler(
trap::X86_INT_APIC_TIMER,
Box::new(|| crate::timer::timer_tick()),
)?;
irq.register_local_apic_handler(trap::X86_INT_APIC_TIMER, Box::new(crate::timer::timer_tick))?;
drivers::add_device(Device::Irq(irq));

#[cfg(feature = "graphic")]
Expand Down
1 change: 1 addition & 0 deletions kernel-hal/src/bare/arch/x86_64/trap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(dead_code)]
#![allow(clippy::identity_op)]

use trapframe::TrapFrame;

Expand Down
2 changes: 1 addition & 1 deletion kernel-hal/src/hal_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ hal_fn_def! {
*x = r as _;
}
} else {
static mut SEED: u64 = 0xdeadbeef_cafebabe;
static mut SEED: u64 = 0xdead_beef_cafe_babe;
for x in buf.iter_mut() {
unsafe {
// from musl
Expand Down
1 change: 1 addition & 0 deletions linux-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ default = ["libos"]
libos = ["kernel-hal/libos", "zircon-object/aspace-separate"]

[dev-dependencies]
env_logger = "0.9"
async-std = { version = "1.10", features = ["attributes"] }
rcore-fs-hostfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "7c232ec" }
15 changes: 15 additions & 0 deletions linux-loader/examples/linux-libos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::env;
use std::path::Path;

#[async_std::main]
async fn main() {
env_logger::init();
kernel_hal::init();
let args = env::args().skip(1).collect();
let envs = vec!["PATH=/usr/sbin:/usr/bin:/sbin:/bin".into()];
let rootfs_path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("../rootfs");
let hostfs = rcore_fs_hostfs::HostFS::new(rootfs_path);
let proc = linux_loader::run(args, envs, hostfs);
let code = proc.wait_for_exit().await;
std::process::exit(code as i32);
}
2 changes: 1 addition & 1 deletion linux-loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async fn handler_user_trap(thread: &CurrentThread, cx: &mut UserContext) -> ZxRe
// UserContext
#[cfg(target_arch = "riscv64")]
{
let trap_num = kernel_hal::context::fetch_trap_num(&cx);
let trap_num = kernel_hal::context::fetch_trap_num(cx);
let is_interrupt = ((trap_num >> (core::mem::size_of::<usize>() * 8 - 1)) & 1) == 1;
let trap_num = trap_num & 0xfff;
if is_interrupt {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions linux-object/src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Linux file objects
mod devfs;
mod device;
mod file;
mod ioctl;
mod pipe;
mod pseudo;
mod stdio;

pub mod rcore_fs_wrapper;

use alloc::{boxed::Box, string::ToString, sync::Arc, vec::Vec};
use core::convert::TryFrom;

Expand All @@ -26,7 +27,6 @@ use self::{devfs::RandomINode, pseudo::Pseudo};
use crate::error::{LxError, LxResult};
use crate::process::LinuxProcess;

pub use device::MemBuf;
pub use file::{File, OpenFlags, SeekFrom};
pub use pipe::Pipe;
pub use rcore_fs::vfs;
Expand Down
67 changes: 67 additions & 0 deletions linux-object/src/fs/rcore_fs_wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//! Device wrappers that implement `rcore_fs::dev::Device`, which can loaded
//! file systems on (e.g. `rcore_fs_sfs::SimpleFileSystem::open()`).
use alloc::sync::Arc;

extern crate rcore_fs;

use kernel_hal::drivers::scheme::BlockScheme;
use rcore_fs::dev::{BlockDevice, DevError, Device, Result};
use spin::RwLock;

/// A naive LRU cache layer for `BlockDevice`, re-exported from `rcore-fs`.
pub use rcore_fs::dev::block_cache::BlockCache;

/// Memory buffer for device.
pub struct MemBuf(RwLock<&'static mut [u8]>);

impl MemBuf {
/// create a [`MemBuf`] struct.
pub fn new(buf: &'static mut [u8]) -> Self {
MemBuf(RwLock::new(buf))
}
}

impl Device for MemBuf {
fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> {
let slice = self.0.read();
let len = buf.len().min(slice.len() - offset);
buf[..len].copy_from_slice(&slice[offset..offset + len]);
Ok(len)
}
fn write_at(&self, offset: usize, buf: &[u8]) -> Result<usize> {
let mut slice = self.0.write();
let len = buf.len().min(slice.len() - offset);
slice[offset..offset + len].copy_from_slice(&buf[..len]);
Ok(len)
}
fn sync(&self) -> Result<()> {
Ok(())
}
}

/// Block device implements [`BlockScheme`].
pub struct Block(Arc<dyn BlockScheme>);

impl Block {
/// create a [`Block`] struct.
pub fn new(block: Arc<dyn BlockScheme>) -> Self {
Self(block)
}
}

impl BlockDevice for Block {
const BLOCK_SIZE_LOG2: u8 = 9; // 512

fn read_at(&self, block_id: usize, buf: &mut [u8]) -> Result<()> {
self.0.read_block(block_id, buf).map_err(|_| DevError)
}

fn write_at(&self, block_id: usize, buf: &[u8]) -> Result<()> {
self.0.write_block(block_id, buf).map_err(|_| DevError)
}

fn sync(&self) -> Result<()> {
self.0.flush().map_err(|_| DevError)
}
}
2 changes: 1 addition & 1 deletion linux-syscall/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ impl RegExt for GeneralRegs {
entry, sp
);
GeneralRegs {
sp: sp,
sp,
a0: arg1,
a1: arg2,
..Default::default()
Expand Down
4 changes: 2 additions & 2 deletions scripts/libc-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def print_cases(cases, file=None):
print(case, file=file)


subprocess.run("cd ../zCore && cargo build --release --features 'libos linux'",
subprocess.run("cd .. && cargo build -p zcore --release --features 'libos linux'",
shell=True, check=True)

for path in sorted(glob.glob("../rootfs/libc-test/src/*/*.exe")):
Expand All @@ -35,7 +35,7 @@ def print_cases(cases, file=None):
continue
try:
time_start = time.time()
subprocess.run("cd ../zCore && ./target/release/zcore " + path,
subprocess.run("cd .. && ./target/release/zcore " + path,
shell=True, timeout=TIMEOUT, check=True)
time_end = time.time()
passed.add(path)
Expand Down
Loading

0 comments on commit 806c9d1

Please sign in to comment.