Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ arm_gic = { git = "https://github.com/Byte-OS/arm_gic" }
[target.'cfg(target_arch = "loongarch64")'.dependencies]
spin = { version = "0.9.8", features = ["mutex"] }
loongarch64 = {git = "https://github.com/Godones/loongArch64",package = "loongArch64" }
volatile = "0.3"
115 changes: 98 additions & 17 deletions arch/src/loongarch64/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,74 @@ use spin::Mutex;

use crate::VIRT_ADDR_START;

const UART_ADDR: usize = 0x01FE001E0 | VIRT_ADDR_START;
const UART_ADDR: usize = 0x1fe001e0 | VIRT_ADDR_START;

static COM1: Mutex<Uart> = Mutex::new(Uart::new(UART_ADDR));
use bitflags::*;
use volatile::{ReadOnly, Volatile, WriteOnly};

bitflags! {
#[derive(Copy, Clone)]
/// InterruptEnableRegister
pub struct IER: u8 {
const RX_AVAILABLE = 1 << 0;
const TX_EMPTY = 1 << 1;
}
#[derive(Copy, Clone)]
/// LineStatusRegister
pub struct LSR: u8 {
const DATA_AVAILABLE = 1 << 0;
const THR_EMPTY = 1 << 5;
}
#[derive(Copy, Clone)]
/// Model Control Register
pub struct MCR: u8 {
const DATA_TERMINAL_READY = 1 << 0;
const REQUEST_TO_SEND = 1 << 1;
const AUX_OUTPUT1 = 1 << 2;
const AUX_OUTPUT2 = 1 << 3;
}
}

#[repr(C)]
#[allow(dead_code)]
struct ReadWithoutDLAB {
/// receiver buffer register
pub rbr: ReadOnly<u8>,
/// interrupt enable register
pub ier: Volatile<IER>,
/// interrupt identification register
pub iir: ReadOnly<u8>,
/// line control register
pub lcr: Volatile<u8>,
/// model control register
pub mcr: Volatile<MCR>,
/// line status register
pub lsr: ReadOnly<LSR>,
/// ignore MSR
_padding1: ReadOnly<u8>,
/// ignore SCR
_padding2: ReadOnly<u8>,
}

#[repr(C)]
#[allow(dead_code)]
struct WriteWithoutDLAB {
/// transmitter holding register
pub thr: WriteOnly<u8>,
/// interrupt enable register
pub ier: Volatile<IER>,
/// ignore FCR
_padding0: ReadOnly<u8>,
/// line control register
pub lcr: Volatile<u8>,
/// modem control register
pub mcr: Volatile<MCR>,
/// line status register
pub lsr: ReadOnly<LSR>,
/// ignore other registers
_padding1: ReadOnly<u16>,
}

pub struct Uart {
base_address: usize,
Expand All @@ -16,31 +81,43 @@ impl Uart {
pub const fn new(base_address: usize) -> Self {
Uart { base_address }
}
fn read_end(&mut self) -> &mut ReadWithoutDLAB {
unsafe { &mut *(self.base_address as *mut ReadWithoutDLAB) }
}

fn write_end(&mut self) -> &mut WriteWithoutDLAB {
unsafe { &mut *(self.base_address as *mut WriteWithoutDLAB) }
}

pub fn init(&mut self) {
let read_end = self.read_end();
let mut mcr = MCR::empty();
mcr |= MCR::DATA_TERMINAL_READY;
mcr |= MCR::REQUEST_TO_SEND;
mcr |= MCR::AUX_OUTPUT2;
read_end.mcr.write(mcr);
let ier = IER::RX_AVAILABLE;
read_end.ier.write(ier);
}

pub fn putchar(&mut self, c: u8) {
let ptr = self.base_address as *mut u8;
let write_end = self.write_end();
loop {
unsafe {
let c = ptr.add(5).read_volatile();
if c & (1 << 5) != 0 {
break;
}
if write_end.lsr.read().contains(LSR::THR_EMPTY) {
write_end.thr.write(c);
break;
}
}
unsafe {
ptr.add(0).write_volatile(c);
}
}

pub fn getchar(&mut self) -> Option<u8> {
let ptr = self.base_address as *mut u8;
let read_end = self.read_end();
let lsr = read_end.lsr.read();
unsafe {
if ptr.add(5).read_volatile() & 1 == 0 {
// The DR bit is 0, meaning no data
None
} else {
// The DR bit is 1, meaning data!
Some(ptr.add(0).read_volatile())
if lsr.contains(LSR::DATA_AVAILABLE) {// The DR bit is 1, meaning data!
Some(read_end.rbr.read())
} else { // The DR bit is 0, meaning no data
None
}
}
}
Expand All @@ -63,3 +140,7 @@ pub fn console_putchar(c: u8) {
pub fn console_getchar() -> Option<u8> {
COM1.lock().getchar()
}
///
pub fn console_init(){
COM1.lock().init();
}
3 changes: 2 additions & 1 deletion arch/src/loongarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod sigtrx;
mod timer;
mod trap;

pub use console::{console_getchar, console_putchar};
pub use console::{console_getchar, console_putchar,console_init};
pub use consts::*;
pub use context::TrapFrame;
#[cfg(feature = "kcontext")]
Expand All @@ -23,6 +23,7 @@ use crate::clear_bss;

pub fn rust_tmp_main(hart_id: usize) {
clear_bss();
console_init();
ArchInterface::init_logging();
ArchInterface::init_allocator();
trap::set_trap_vector_base();
Expand Down
6 changes: 4 additions & 2 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
pub const USER_STACK_SIZE: usize = 4096 * 20;
pub const KERNEL_STACK_SIZE: usize = 4096 *10;
pub const KERNEL_HEAP_SIZE: usize = 0x300_0000;
pub const USER_STACK_TOP: usize = 0x8000_0000;
pub const USER_MMAP_TOP: usize = 0x6000_0000;
pub const USER_STACK_TOP: usize = 0x13_0000_0000;
pub const USER_MMAP_TOP: usize = 0x11_0000_0000;

pub const DL_INTERP_OFFSET: usize = 0x15_0000_0000;

pub const MAX_FD:usize = 1024;
//pub const PAGE_SIZE: usize = 0x1000;
Expand Down
5 changes: 1 addition & 4 deletions ext4-fs-fuse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,8 @@ fn easy_fs_pack() -> std::io::Result<()> {
// write data to ext4
sb.ext4fs.ext4_file_write(inode as u64, 0, all_data.as_slice());
}

println!("app:----");
/* list apps
for app in root_dentry.clone().ls() {
println!("{}", app);
}*/
drop(root_dentry);
block_cache_sync_all();
Ok(())
Expand Down
141 changes: 131 additions & 10 deletions ext4-test-fuse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::mem;
use std::io::{Read, Seek, SeekFrom, Write};
use std::sync::{Arc, Once};
use std::sync::Mutex;
use vfs_defs::{Dentry, DentryState, DiskInodeType, File as OtherFile, FileInner, FileSystemType, SuperBlock, SuperBlockInner,OpenFlags};
use vfs_defs::{dcache_drop, dcache_lookup, Dentry, DentryState, DiskInodeType, File as OtherFile, FileInner, FileSystemType, OpenFlags, SuperBlock, SuperBlockInner,dcache_sync_call};
use system_result::{SysError,SysResult};
use device::BlockDevice;
use buffer::block_cache_sync_all;
Expand Down Expand Up @@ -51,10 +51,11 @@ impl BlockDevice for BlockFile {
}

fn main() {
easy_fs_pack().expect("Error when packing ext4!");
easy_fs_pack().expect("Error when packing ext4!");
//rv_pack().expect("Error when packing ext4!");
}
fn easy_fs_pack() -> std::io::Result<()> {

fn rv_pack() -> std::io::Result<()> {
let matches = App::new("Ext4 packer")
.arg(
Arg::with_name("source")
Expand Down Expand Up @@ -100,21 +101,137 @@ fn easy_fs_pack() -> std::io::Result<()> {
name_with_ext
})
.collect();

{ let mnt = root_dentry.create("mnt",DiskInodeType::Directory).unwrap();
let mut host_file = File::open(format!("{}{}", src_path, "mnt/test_mount")).unwrap();


for app in apps {
// load app data from host file system
println!("{}",app);
if app != String::from("user_shell"){
continue;
}
let mut host_file = File::open(format!("{}{}", src_path, app)).unwrap();
let mut all_data: Vec<u8> = Vec::new();
host_file.read_to_end(&mut all_data).unwrap();
// create a file in ext4
let den =mnt.create("test_mount",DiskInodeType::File).unwrap();
let den =root_dentry.create(app.as_str(),DiskInodeType::File).unwrap();
let inode = den.get_inode().unwrap().get_meta().ino;
// write data to ext4
sb.ext4fs.ext4_file_write(inode as u64, 0, all_data.as_slice());
}
{
let mut host_file = File::open(format!("{}{}", target_path, "initproc")).unwrap();
let mut all_data: Vec<u8> = Vec::new();
host_file.read_to_end(&mut all_data).unwrap();
// create a file in ext4
let den =root_dentry.create("initproc",DiskInodeType::File).unwrap();
let inode = den.get_inode().unwrap().get_meta().ino;
// write data to ext4
sb.ext4fs.ext4_file_write(inode as u64, 0, all_data.as_slice());
}
println!("app:----");
drop(root_dentry);
dcache_sync_call();
dcache_drop();
block_cache_sync_all();
Ok(())
}


fn easy_fs_pack() -> std::io::Result<()> {
let matches = App::new("Ext4 packer")
.arg(
Arg::with_name("source")
.short("s")
.long("source")
.takes_value(true)
.help("Executable source dir(with backslash)"),
)
.arg(
Arg::with_name("target")
.short("t")
.long("target")
.takes_value(true)
.help("Executable target dir(with backslash)"),
)
.get_matches();
let src_path = matches.value_of("source").unwrap();
let target_path = matches.value_of("target").unwrap();
println!("src_path = {}\ntarget_path = {}", src_path, target_path);
let block_file = Arc::new(BlockFile(Mutex::new({
let f = OpenOptions::new()
.read(true)
.write(true)
.create(true)
// .open("ext4.img")?;
.open(format!("{}{}", target_path, "fs.img"))?;
f.set_len(8192 * 1024 * 1024).unwrap();
f
})));


device::BLOCK_DEVICE.call_once(||block_file);
logger::init_logger();
vfs::init();
let root_dentry = get_root_dentry();
let sb = root_dentry.get_superblock().downcast_arc::<Ext4Superblock>().map_err(|_| SysError::ENOENT).unwrap();
let apps: Vec<_> = read_dir(src_path)
.unwrap()
.into_iter()
.map(|dir_entry| {
let mut name_with_ext = dir_entry.unwrap().file_name().into_string().unwrap();
// name_with_ext.drain(name_with_ext.find('.').unwrap()..name_with_ext.len());
name_with_ext
})
.collect();
let mnt = root_dentry.create("mnt",DiskInodeType::Directory).unwrap();
let lib = root_dentry.create("lib",DiskInodeType::Directory).unwrap();

let mntapps: Vec<_> = read_dir(format!("{}{}", src_path, "mnt/"))
.unwrap()
.into_iter()
.map(|dir_entry| {
let mut name_with_ext = dir_entry.unwrap().file_name().into_string().unwrap();
// name_with_ext.drain(name_with_ext.find('.').unwrap()..name_with_ext.len());
name_with_ext
})
.collect();
let libapps: Vec<_> = read_dir(format!("{}{}", src_path, "lib/"))
.unwrap()
.into_iter()
.map(|dir_entry| {
let mut name_with_ext = dir_entry.unwrap().file_name().into_string().unwrap();
// name_with_ext.drain(name_with_ext.find('.').unwrap()..name_with_ext.len());
name_with_ext
})
.collect();
for app in mntapps {
// load app data from host file system
println!("{}",app);
let mut host_file = File::open(format!("{}{}{}", src_path,"mnt/", app)).unwrap();
let mut all_data: Vec<u8> = Vec::new();
host_file.read_to_end(&mut all_data).unwrap();
// create a file in ext4
let den =mnt.create(app.as_str(),DiskInodeType::File).unwrap();
let inode = den.get_inode().unwrap().get_meta().ino;
// write data to ext4
sb.ext4fs.ext4_file_write(inode as u64, 0, all_data.as_slice());
}
for app in libapps {
// load app data from host file system
println!("{}",app);
let mut host_file = File::open(format!("{}{}{}", src_path,"lib/", app)).unwrap();
let mut all_data: Vec<u8> = Vec::new();
host_file.read_to_end(&mut all_data).unwrap();
// create a file in ext4
let den =lib.create(app.as_str(),DiskInodeType::File).unwrap();
let inode = den.get_inode().unwrap().get_meta().ino;
// write data to ext4
sb.ext4fs.ext4_file_write(inode as u64, 0, all_data.as_slice());
}
for app in apps {
// load app data from host file system
println!("{}",app);
if app == String::from("mnt"){
if app == String::from("mnt") || app == String::from("lib"){
continue;
}
let mut host_file = File::open(format!("{}{}", src_path, app)).unwrap();
Expand All @@ -127,7 +244,11 @@ fn easy_fs_pack() -> std::io::Result<()> {
sb.ext4fs.ext4_file_write(inode as u64, 0, all_data.as_slice());
}
println!("app:----");
drop(mnt);
drop(lib);
drop(root_dentry);
block_cache_sync_all();
dcache_sync_call();
dcache_drop();
block_cache_sync_all();
Ok(())
}
Loading
Loading