Add new `mem` command (#113)

* Add mem command

* Increase heap size to 1MB

* Refactor hex formatting

* Rename variable in memory count

* Update changelog
This commit is contained in:
Vincent Ollivier 2020-11-10 22:11:33 +01:00 committed by GitHub
parent 7839f44438
commit 2cb0b83dee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 58 additions and 29 deletions

View File

@ -1,6 +1,7 @@
# Changelog
## Unreleased
- Add new `mem` command ([#113](https://github.com/vinc/moros/pull/113))
- Add new `date` and `env` commands ([#112](https://github.com/vinc/moros/pull/112))
- Add ACPI shutdown ([#111](https://github.com/vinc/moros/pull/111))
- Improve text editor ([#109](https://github.com/vinc/moros/pull/109))

View File

@ -55,7 +55,7 @@ pub fn shutdown() {
*/
pm1a_control_block = read_fadt::<u32>(sdt.physical_address, FADT::Pm1aControlBlock);
//log!("ACPI Found PM1a Control Block: 0x{:x}\n", pm1a_control_block);
//log!("ACPI Found PM1a Control Block: {:#X}\n", pm1a_control_block);
}
}
match &acpi.dsdt {

View File

@ -10,7 +10,7 @@ use x86_64::structures::paging::{FrameAllocator, Mapper, Page, PageTableFlags, S
use x86_64::VirtAddr;
pub const HEAP_START: usize = 0x_4444_4444_0000;
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB
pub const HEAP_SIZE: usize = 1 << 20; // 1 MB
#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();
@ -105,3 +105,15 @@ impl core::ops::DerefMut for PhysBuf {
unsafe { alloc::slice::from_raw_parts_mut(vec.as_mut_ptr(), vec.len()) }
}
}
pub fn size() -> usize {
ALLOCATOR.lock().size()
}
pub fn used() -> usize {
ALLOCATOR.lock().used()
}
pub fn free() -> usize {
ALLOCATOR.lock().free()
}

View File

@ -39,11 +39,11 @@ pub fn init() {
let res = unsafe {
port.read()
};
print!("[{:.6}] keyboard: identify 0x{:X}\n", kernel::clock::uptime(), res);
print!("[{:.6}] keyboard: identify {:#X}\n", kernel::clock::uptime(), res);
let res = unsafe {
port.read()
};
print!("[{:.6}] keyboard: identify 0x{:X}\n", kernel::clock::uptime(), res);
print!("[{:.6}] keyboard: identify {:#X}\n", kernel::clock::uptime(), res);
// Self-test
let res = unsafe {
@ -59,7 +59,7 @@ pub fn init() {
if res == 0xAA { // 0xAA == Passed, 0xFC or 0xFD == Failed, 0xFE == Resend
print!("[{:.6}] keyboard: self test passed\n", kernel::clock::uptime());
} else {
print!("[{:.6}] keyboard: self test failed (0x{:X})\n", kernel::clock::uptime(), res);
print!("[{:.6}] keyboard: self test failed ({:#X})\n", kernel::clock::uptime(), res);
}
// Switch to scancode set 2

View File

@ -8,14 +8,14 @@ use x86_64::{PhysAddr, VirtAddr};
static mut PHYS_MEM_OFFSET: u64 = 0;
pub fn init(boot_info: &'static BootInfo) {
let mut mem_total = 0;
let mut memory_size = 0;
for region in boot_info.memory_map.iter() {
let start_addr = region.range.start_addr();
let end_addr = region.range.end_addr();
mem_total += end_addr - start_addr;
log!("MEM [0x{:016X}-0x{:016X}] {:?}\n", start_addr, end_addr, region.region_type);
memory_size += end_addr - start_addr;
log!("MEM [{:#016X}-{:#016X}] {:?}\n", start_addr, end_addr, region.region_type);
}
log!("MEM {} KB\n", mem_total >> 10);
log!("MEM {} KB\n", memory_size >> 10);
unsafe { PHYS_MEM_OFFSET = boot_info.physical_memory_offset; }

View File

@ -238,11 +238,11 @@ impl<'a> Device<'a> for RTL8139 {
if self.debug_mode {
print!("------------------------------------------------------------------\n");
print!("[{:.6}] NET RTL8139 receiving packet:\n\n", kernel::clock::uptime());
//print!("Command Register: 0x{:02X}\n", cmd);
//print!("Interrupt Status Register: 0x{:02X}\n", isr);
//print!("Command Register: {:#02X}\n", cmd);
//print!("Interrupt Status Register: {:#02X}\n", isr);
//print!("CAPR: {}\n", capr);
//print!("CBR: {}\n", cbr);
//print!("Header: 0x{:04X}\n", header);
//print!("Header: {:#04X}\n", header);
}
if header & ROK != ROK {
unsafe { self.ports.capr.write(cbr) };
@ -254,7 +254,7 @@ impl<'a> Device<'a> for RTL8139 {
let len = n - 4;
if self.debug_mode {
print!("size: {} bytes", len);
//print!("CRC: 0x{:08X}\n", crc);
//print!("CRC: {:#08X}\n", crc);
//print!("RX Offset: {}\n", offset);
user::hex::print_hex(&self.rx_buffer[(offset + 4)..(offset + n)]);
}
@ -284,7 +284,7 @@ impl<'a> Device<'a> for RTL8139 {
print!("------------------------------------------------------------------\n");
print!("[{:.6}] NET RTL8139 transmitting packet:\n\n", kernel::clock::uptime());
//print!("TX Buffer: {}\n", self.tx_id);
//print!("Interrupt Status Register: 0x{:02X}\n", isr);
//print!("Interrupt Status Register: {:#02X}\n", isr);
}
let tx = TxToken {

View File

@ -252,7 +252,7 @@ impl vte::Perform for Writer {
fn execute(&mut self, byte: u8) {
self.write_byte(byte);
kernel::serial::print_fmt(format_args!("[execute] {:02x}\n", byte));
kernel::serial::print_fmt(format_args!("[execute] {:#02X}\n", byte));
}
fn hook(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, c: char) {
@ -260,7 +260,7 @@ impl vte::Perform for Writer {
}
fn put(&mut self, byte: u8) {
kernel::serial::print_fmt(format_args!("[put] {:02x}\n", byte));
kernel::serial::print_fmt(format_args!("[put] {:#02X}\n", byte));
}
fn unhook(&mut self) {
@ -297,7 +297,7 @@ impl vte::Perform for Writer {
}
fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8) {
kernel::serial::print_fmt(format_args!("[esc_dispatch] intermediates={:?}, ignore={:?}, byte={:02x}\n", intermediates, ignore, byte));
kernel::serial::print_fmt(format_args!("[esc_dispatch] intermediates={:?}, ignore={:?}, byte={:#02X}\n", intermediates, ignore, byte));
}
}

View File

@ -3,38 +3,39 @@ use alloc::vec::Vec;
pub fn main(args: &[&str]) -> user::shell::ExitCode {
if args.len() != 3 {
print!("Usage: copy <source> <dest>\n");
return user::shell::ExitCode::CommandError;
}
let from = args[1];
let to = args[2];
let source = args[1];
let dest = args[2];
if to.starts_with("/dev") || to.starts_with("/sys") {
print!("Permission denied to write to '{}'\n", to);
if dest.starts_with("/dev") || dest.starts_with("/sys") {
print!("Permission denied to write to '{}'\n", dest);
return user::shell::ExitCode::CommandError;
}
if let Some(file_from) = kernel::fs::File::open(from) {
if let Some(mut file_to) = kernel::fs::File::create(to) {
let filesize = file_from.size();
if let Some(source_file) = kernel::fs::File::open(source) {
if let Some(mut dest_file) = kernel::fs::File::create(dest) {
let filesize = source_file.size();
let mut buf = Vec::with_capacity(filesize);
buf.resize(filesize, 0);
file_from.read(&mut buf);
match file_to.write(&buf) {
source_file.read(&mut buf);
match dest_file.write(&buf) {
Ok(()) => {
user::shell::ExitCode::CommandSuccessful
},
Err(()) => {
print!("Could not write to '{}'\n", to);
print!("Could not write to '{}'\n", dest);
user::shell::ExitCode::CommandError
}
}
} else {
print!("Permission denied to write to '{}'\n", to);
print!("Permission denied to write to '{}'\n", dest);
user::shell::ExitCode::CommandError
}
} else {
print!("File not found '{}'\n", from);
print!("File not found '{}'\n", source);
user::shell::ExitCode::CommandError
}
}

13
src/user/mem.rs Normal file
View File

@ -0,0 +1,13 @@
use crate::{kernel, print, user};
use crate::kernel::console::Style;
use alloc::string::ToString;
pub fn main(_args: &[&str]) -> user::shell::ExitCode {
let width = kernel::allocator::size().to_string().len();
let color = Style::color("LightCyan");
let reset = Style::reset();
print!("{}Size:{} {:width$}\n", color, reset, kernel::allocator::size(), width = width);
print!("{}Used:{} {:width$}\n", color, reset, kernel::allocator::used(), width = width);
print!("{}Free:{} {:width$}\n", color, reset, kernel::allocator::free(), width = width);
user::shell::ExitCode::CommandSuccessful
}

View File

@ -17,6 +17,7 @@ pub mod http;
pub mod install;
pub mod ip;
pub mod list;
pub mod mem;
pub mod net;
pub mod print;
pub mod r#move;

View File

@ -423,6 +423,7 @@ impl Shell {
"colors" => user::colors::main(&args),
"disk" => user::disk::main(&args),
"user" => user::user::main(&args),
"mem" | "memory" => user::mem::main(&args),
_ => ExitCode::CommandUnknown,
}
}