implement mem api and fixes

This commit is contained in:
Matthieu Jolimaitre 2024-10-28 15:59:54 +01:00
parent 9ce30d2941
commit b013e95f2c
9 changed files with 393 additions and 16 deletions

View file

@ -47,15 +47,17 @@ impl Console {
}
}
}
self.vga.set_cursor_pos(self.cursor.pos());
}
pub fn correct(&mut self) {
self.cursor.set_col((self.cursor.col().max(1) - 1));
self.cursor.set_col(self.cursor.col().max(1) - 1);
self.vga.write_at(self.cursor.pos(), ' ');
self.vga.set_cursor_pos(self.cursor.pos());
}
}
struct Cursor {
pub struct Cursor {
line: usize,
column: usize,
width: usize,
@ -126,6 +128,6 @@ pub fn print_args(args: fmt::Arguments) {
use core::fmt::Write;
use x86_64::instructions::interrupts;
interrupts::without_interrupts(|| {
OUT.lock().write_fmt(args).unwrap();
out().write_fmt(args).unwrap();
});
}

View file

@ -1,4 +1,6 @@
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
use linked_list_allocator::{Heap, LockedHeap};
use spinning_top::{lock_api::MutexGuard, RawSpinlock};
use x86_64::{
registers::control::Cr3,
structures::paging::{
@ -60,7 +62,7 @@ unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
}
pub const HEAP_START: usize = 0x_4444_4444_0000;
pub const HEAP_SIZE: usize = 1_000 * 1024; // 1 MiB
pub const HEAP_SIZE: usize = 10_000 * 1024; // 10 MiB
pub fn init_heap(
mapper: &mut impl Mapper<Size4KiB>,
@ -83,16 +85,18 @@ pub fn init_heap(
unsafe { mapper.map_to(page, frame, flags, frame_allocator)?.flush() };
}
unsafe { ALLOCATOR.lock().init(HEAP_START as _, HEAP_SIZE) };
unsafe { alloc().init(HEAP_START as _, HEAP_SIZE) };
Ok(())
}
use linked_list_allocator::LockedHeap;
#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();
pub fn alloc<'l>() -> MutexGuard<'l, RawSpinlock, Heap> {
ALLOCATOR.lock()
}
pub unsafe fn translate_addr(addr: VirtAddr, physical_memory_start: VirtAddr) -> Option<PhysAddr> {
translate_addr_inner(addr, physical_memory_start)
}

View file

@ -74,9 +74,9 @@ pub enum Key {
#[derive(Debug, Default)]
pub struct KeyModState {
ctrl: bool,
shift: bool,
alt: bool,
pub ctrl: bool,
pub shift: bool,
pub alt: bool,
}
impl KeyModState {

View file

@ -1,3 +1,5 @@
use x86_64::instructions::{interrupts, port::Port};
pub const WIDTH: usize = 80;
pub const HEIGHT: usize = 25;
@ -13,4 +15,49 @@ impl Vga {
*Vga::BUFF.offset(index * 2 + 1) = 0xb;
}
}
pub fn set_cursor_pos(&self, pos: (usize, usize)) {
type Uk = u16;
let index = (pos.0 + pos.1 * WIDTH) as Uk + 1;
interrupts::without_interrupts(|| unsafe {
let mut cmd = Port::<Uk>::new(0x3D4);
let mut data = Port::<Uk>::new(0x3D5);
cmd.write(0x0F);
data.write(index & 0xFF);
cmd.write(0x0E);
data.write((index >> 8) & 0xFF);
});
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Color {
Black = 0,
Blue = 1,
Green = 2,
Cyan = 3,
Red = 4,
Magenta = 5,
Brown = 6,
LightGray = 7,
DarkGray = 8,
LightBlue = 9,
LightGreen = 10,
LightCyan = 11,
LightRed = 12,
Pink = 13,
Yellow = 14,
White = 15,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
struct ColorCode(u8);
impl ColorCode {
fn new(foreground: Color, background: Color) -> ColorCode {
ColorCode((background as u8) << 4 | (foreground as u8))
}
}

View file

@ -2,6 +2,7 @@
#![no_main]
#![feature(abi_x86_interrupt)]
#![feature(if_let_guard)]
#![feature(allocator_api)]
extern crate alloc;
mod dev;

View file

@ -1,7 +1,6 @@
use core::{cell::RefCell, mem};
use core::mem;
use alloc::{
rc::Rc,
string::{String, ToString},
vec::Vec,
};
@ -9,19 +8,25 @@ use blinkcast::alloc::channel;
use microlang::eval::{Context, Scope, Value};
use crate::{
dev::console::out,
hard::keyboard::{keyboard, Azerty, Code, Key, KeyModState, Qwerty},
dev::{console::out, memory::alloc},
hard::keyboard::{keyboard, Code, Key, KeyModState},
print, println,
};
use crate::hard::keyboard::Azerty as Keymap;
// use crate::hard::keyboard::Qwerty as Keymap;
pub fn main() {
let keymap = Azerty::default();
let keymap = Keymap::default();
let (tx, mut rx) = channel(1024);
let mut buff = String::new();
let mut state = KeyModState::default();
keyboard().keymap(keymap.clone());
keyboard().on_code(move |c| match state.translate(&keymap, c) {
Code::Down(Key::Char('d')) if state.ctrl => {
panic!("Exiting.");
}
Code::Down(Key::Char(c)) => {
print!("{c}");
if c == '\n' {
@ -93,4 +98,18 @@ fn prelude(context: &mut Context) {
.collect();
Ok(Value::array(values))
});
context.define_built("mem", |_args, _ctx| {
let size = alloc().size() as f64;
let used = alloc().used() as f64;
let free_frac = 1. - (used / size);
Ok(Value::object(
[
("size".into(), Value::Num(size)),
("used".into(), Value::Num(used)),
("free_frac".into(), Value::Num(free_frac)),
]
.into_iter(),
))
});
}