Skip to content

Commit

Permalink
Add new mem command (#113)
Browse files Browse the repository at this point in the history
* Add mem command

* Increase heap size to 1MB

* Refactor hex formatting

* Rename variable in memory count

* Update changelog
  • Loading branch information
vinc authored Nov 10, 2020
1 parent 7839f44 commit 2cb0b83
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/acpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion src/kernel/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
}
6 changes: 3 additions & 3 deletions src/kernel/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/kernel/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
10 changes: 5 additions & 5 deletions src/kernel/net/rtl8139.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
Expand All @@ -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)]);
}
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/kernel/vga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,15 @@ 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) {
kernel::serial::print_fmt(format_args!("[hook] params={:?}, intermediates={:?}, ignore={:?}, char={:?}\n", params, intermediates, ignore, c));
}

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) {
Expand Down Expand Up @@ -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));
}
}

Expand Down
25 changes: 13 additions & 12 deletions src/user/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 13 additions & 0 deletions src/user/mem.rs
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions src/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/user/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down

0 comments on commit 2cb0b83

Please sign in to comment.