Skip to content

Commit

Permalink
cargo fmt fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KouweiLee committed Sep 11, 2023
1 parent 86391c3 commit e7cb198
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 69 deletions.
1 change: 0 additions & 1 deletion api/arceos_posix_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,3 @@ pub use imp::pthread::mutex::{
};
#[cfg(feature = "multitask")]
pub use imp::pthread::{sys_pthread_create, sys_pthread_exit, sys_pthread_join, sys_pthread_self};

8 changes: 4 additions & 4 deletions modules/axhal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ pub use self::platform::platform_init;
#[cfg(feature = "smp")]
pub use self::platform::platform_init_secondary;

/// A cmdline buf for x86_64
///
/// The Multiboot information structure may be placed anywhere in memory by the boot loader,
/// A cmdline buf for x86_64
///
/// The Multiboot information structure may be placed anywhere in memory by the boot loader,
/// so we should save cmdline in a buf before this memory is set free
pub static mut COMLINE_BUF:[u8; 256] = [0; 256];
pub static mut COMLINE_BUF: [u8; 256] = [0; 256];
2 changes: 1 addition & 1 deletion modules/axhal/src/platform/aarch64_qemu_virt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) unsafe extern "C" fn rust_entry(cpu_id: usize, dtb: usize) {
crate::mem::clear_bss();
crate::arch::set_exception_vector_base(exception_vector_base as usize);
crate::arch::write_page_table_root0(0.into()); // disable low address access
unsafe {
unsafe {
dtb::init(crate::mem::phys_to_virt(dtb.into()).as_ptr());
}
crate::cpu::init_primary(cpu_id);
Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/src/platform/riscv64_qemu_virt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ unsafe extern "C" fn rust_entry(cpu_id: usize, dtb: usize) {
crate::mem::clear_bss();
crate::cpu::init_primary(cpu_id);
crate::arch::set_trap_vector_base(trap_vector_base as usize);
unsafe {
unsafe {
dtb::init(crate::mem::phys_to_virt(dtb.into()).as_ptr());
}
rust_main(cpu_id, dtb);
Expand Down
4 changes: 2 additions & 2 deletions modules/axhal/src/platform/x86_pc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ unsafe extern "C" fn rust_entry(magic: usize, mbi: usize) {
self::uart16550::init();
self::dtables::init_primary();
self::time::init_early();
let mbi = mbi as *const u32;
let mbi = mbi as *const u32;
let flag = mbi.read();
if (flag & (1 << 2)) > 0 {
let cmdline = *mbi.add(4) as *const u8; // cmdline的物理地址
let mut len = 0;
while cmdline.add(len).read() != 0 {
COMLINE_BUF[len] = cmdline.add(len).read();
COMLINE_BUF[len] = cmdline.add(len).read();
len += 1;
}
}
Expand Down
54 changes: 26 additions & 28 deletions modules/axruntime/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
extern crate alloc;
use alloc::vec::Vec;
use core::{ptr, usize};
use core::ffi::c_char;
use core::{ptr, usize};

/// argv for C main function
#[allow(non_upper_case_globals)]
pub static mut argv: *mut *mut c_char = ptr::null_mut();
#[allow(non_upper_case_globals)]
static mut inner_argv: Vec<*mut c_char> = Vec::new();

/// A pointer pointing to OUR_ENVIRON
/// A pointer pointing to OUR_ENVIRON
#[allow(non_upper_case_globals)]
#[no_mangle]
pub static mut environ: *mut *mut c_char = ptr::null_mut();
pub static mut environ: *mut *mut c_char = ptr::null_mut();

/// Save environment variables
pub static mut OUR_ENVIRON: Vec<*mut c_char> = Vec::new();

pub(crate) unsafe fn init_argv(args: Vec<&str>) {
for arg in args {
let len = arg.len();
let arg = arg.as_ptr();
let buf = buf_alloc(len+1);
for i in 0..len {
*buf.add(i) = *arg.add(i) as i8;
}
*buf.add(len) = 0;
inner_argv.push(buf);
}
inner_argv.push(ptr::null_mut());
argv = inner_argv.as_mut_ptr();
for arg in args {
let len = arg.len();
let arg = arg.as_ptr();
let buf = buf_alloc(len + 1);
for i in 0..len {
*buf.add(i) = *arg.add(i) as i8;
}
*buf.add(len) = 0;
inner_argv.push(buf);
}
inner_argv.push(ptr::null_mut());
argv = inner_argv.as_mut_ptr();
}

/// Generate an iterator for environment variables
Expand All @@ -56,25 +56,23 @@ const CTRL_BLK_SIZE: usize = core::mem::size_of::<MemoryControlBlock>();

unsafe fn buf_alloc(size: usize) -> *mut c_char {
let layout = core::alloc::Layout::from_size_align(size + CTRL_BLK_SIZE, 8).unwrap();
// allocate for buf to meet free function
let alloc_ptr = alloc::alloc::alloc(layout).cast::<MemoryControlBlock>();
assert!(!alloc_ptr.is_null(), "alloc failed");
alloc_ptr.write(MemoryControlBlock { size });
alloc_ptr.add(1).cast()
// allocate for buf to meet free function
let alloc_ptr = alloc::alloc::alloc(layout).cast::<MemoryControlBlock>();
assert!(!alloc_ptr.is_null(), "alloc failed");
alloc_ptr.write(MemoryControlBlock { size });
alloc_ptr.add(1).cast()
}

pub(crate) unsafe fn boot_add_environ(
env: &str,
) {
pub(crate) unsafe fn boot_add_environ(env: &str) {
let ptr = env.as_ptr() as *const i8;
let size = env.len() + 1; // 算上/0
if size == 1 {
return;
}
if size == 1 {
return;
}
let buf = buf_alloc(size);
for i in 0..size-1 {
for i in 0..size - 1 {
core::ptr::write(buf.add(i), *ptr.add(i));
}
core::ptr::write(buf.add(size - 1), 0);
OUR_ENVIRON.push(buf);
}
}
48 changes: 25 additions & 23 deletions modules/axruntime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extern crate alloc;
#[cfg(feature = "alloc")]
mod env;
#[cfg(feature = "alloc")]
pub use self::env::{environ, OUR_ENVIRON, environ_iter, argv};
pub use self::env::{argv, environ, environ_iter, OUR_ENVIRON};
#[cfg(feature = "alloc")]
use self::env::{boot_add_environ, init_argv};
use core::ffi::{c_char, c_int};
Expand Down Expand Up @@ -198,47 +198,49 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! {
while !is_init_ok() {
core::hint::spin_loop();
}
// environ initialization
// environ initialization
#[cfg(feature = "alloc")]
unsafe {
use alloc::vec::Vec;
let mut boot_str = if cfg!(any(target_arch = "x86", target_arch = "x86_64")) {
let cmdline_buf: &[u8] = &axhal::COMLINE_BUF;
let mut len = 0;
for c in cmdline_buf.iter() {
if *c == 0 {
break;
}
len += 1;
}
core::str::from_utf8(&cmdline_buf[..len]).unwrap()
let cmdline_buf: &[u8] = &axhal::COMLINE_BUF;
let mut len = 0;
for c in cmdline_buf.iter() {
if *c == 0 {
break;
}
len += 1;
}
core::str::from_utf8(&cmdline_buf[..len]).unwrap()
} else {
dtb::get_node("chosen").unwrap().prop("bootargs").str()
};
(_, boot_str) = match boot_str.split_once(';') {
Some((a, b)) => (a, b),
None => ("", "")
};
(_, boot_str) = match boot_str.split_once(';') {
Some((a, b)) => (a, b),
None => ("", ""),
};
let (args, envs) = match boot_str.split_once(';') {
Some((a, e)) => (a, e),
None => ("", ""),
None => ("", ""),
};
let envs: Vec<&str> = envs.split(" ").collect();
for i in envs {
boot_add_environ(i);
}
OUR_ENVIRON.push(core::ptr::null_mut());
environ = OUR_ENVIRON.as_mut_ptr();
// set up argvs
let args: Vec<&str> = args.split(" ").filter(|i| i.len() != 0).collect();
let argc = args.len() as c_int;
init_argv(args);
// set up argvs
let args: Vec<&str> = args.split(" ").filter(|i| i.len() != 0).collect();
let argc = args.len() as c_int;
init_argv(args);

main(argc, argv);
main(argc, argv);
}

#[cfg(not(feature = "alloc"))]
unsafe { main(0, core::ptr::null_mut()) };
#[cfg(not(feature = "alloc"))]
unsafe {
main(0, core::ptr::null_mut())
};

#[cfg(feature = "multitask")]
axtask::exit(0);
Expand Down
9 changes: 4 additions & 5 deletions ulib/axlibc/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use arceos_posix_api::{OUR_ENVIRON, environ, environ_iter};
use arceos_posix_api::{environ, environ_iter, OUR_ENVIRON};
use core::ffi::{c_char, c_int, c_void};

use crate::malloc::{malloc, free};
use crate::malloc::{free, malloc};

unsafe fn find_env(search: *const c_char) -> Option<(usize, *mut c_char)> {
for (i, mut item) in environ_iter().enumerate() {
Expand Down Expand Up @@ -108,7 +108,6 @@ pub unsafe extern "C" fn setenv(
0
}


/// unset an environ variable
#[no_mangle]
pub unsafe extern "C" fn unsetenv(key: *const c_char) -> c_int {
Expand Down Expand Up @@ -143,6 +142,6 @@ pub unsafe extern "C" fn unsetenv(key: *const c_char) -> c_int {
#[no_mangle]
pub unsafe extern "C" fn getenv(name: *const c_char) -> *mut c_char {
find_env(name)
.map(|val| val.1)
.unwrap_or(core::ptr::null_mut())
.map(|val| val.1)
.unwrap_or(core::ptr::null_mut())
}
8 changes: 4 additions & 4 deletions ulib/axlibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ pub use self::sys::sysconf;
pub use self::time::{clock_gettime, nanosleep};
pub use self::unistd::{abort, exit, getpid};

#[cfg(feature = "alloc")]
pub use self::malloc::{free, malloc};
#[cfg(feature = "alloc")]
pub use self::strftime::strftime;
#[cfg(feature = "alloc")]
pub use self::env::{getenv, setenv, unsetenv};
#[cfg(feature = "fd")]
pub use self::fd_ops::{ax_fcntl, close, dup, dup2, dup3};
#[cfg(feature = "alloc")]
pub use self::malloc::{free, malloc};
#[cfg(feature = "alloc")]
pub use self::strftime::strftime;

#[cfg(feature = "fs")]
pub use self::fs::{ax_open, fstat, getcwd, lseek, lstat, rename, stat};
Expand Down

0 comments on commit e7cb198

Please sign in to comment.