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
24 changes: 24 additions & 0 deletions src/arch/i686/paging.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
global load_page_directory
load_page_directory:
push ebp
mov ebp, esp

mov eax, [8+esp]
mov cr3, eax

mov esp, ebp
pop ebp
ret

global enable_paging
enable_paging:
push ebp
mov ebp, esp

mov eax, cr0
or eax, 0x80000000
mov cr0, eax

mov esp, ebp
pop ebp
ret
6 changes: 3 additions & 3 deletions src/elsos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#![no_std]
#![no_main]

mod tools;
mod ferramenta;
mod vga;
mod keyboard;
mod tty;
mod multiboot;
mod serial;
mod gdt;
mod memory;
mod libc;

use core::panic::PanicInfo;

Expand Down Expand Up @@ -44,8 +45,7 @@ pub extern "C" fn kernel_main(magic: u32, address: u32)
vga::cursor::Cursor::init(0, 15);
if multiboot::check_magic(magic) && multiboot::parse(address)
{
let mut alloc: memory::pageframe::Allocator = memory::pageframe::Allocator::new();
unsafe { alloc.read_grub_mmap(MULTIBOOT_MMAP, MULTIBOOT_MMAP_ENTRIES); }
unsafe { crate::memory::init(MULTIBOOT_MMAP, MULTIBOOT_MMAP_ENTRIES); }
init_serial();
logln!("\n");
logln!(" ::: :::::::: __ __ __ _ ____ ____ ");
Expand Down
23 changes: 20 additions & 3 deletions src/tools.rs → src/ferramenta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ pub fn outb(port: u32, value: u8)
}
}

pub fn get_bit_at(input: u8, n: u8) -> bool
pub fn get_bit_at(input: u32, n: u8) -> bool
{
if n < 8
if n < 32
{
return input & (1 << n) != 0;
}
Expand Down Expand Up @@ -132,7 +132,7 @@ macro_rules! page_index
{
($reg:expr) =>
{{
tools::divide_up($reg, 4096)
ferramenta::divide_up($reg, 4096)
}}
}

Expand Down Expand Up @@ -227,3 +227,20 @@ impl Bitmap
}
}
}

pub fn set_bit(var: &mut u32, value: bool, bit: u8)
{
if value
{
*var |= (value as u32) << bit;
}
else
{
*var &= !(!value as u32) << bit;
}
}

pub fn align(val: usize, bound: usize) -> usize
{
val + bound - 1 & !(bound - 1)
}
5 changes: 3 additions & 2 deletions src/gdt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// using https://wiki.osdev.org/Global_Descriptor_Table#Segment_Descriptor to fill the values

use core::mem::size_of;
use core::ffi::c_void;
use crate::libc;

#[repr(C, packed)]
struct gdt_ptr
Expand Down Expand Up @@ -102,7 +104,6 @@ static mut GDT_TABLE: gdt = gdt
extern "C"
{
fn _gdt_flush();
fn memcpy(dst: *const u8, src: *const u8, size: usize);
}

#[no_mangle]
Expand All @@ -117,7 +118,7 @@ pub fn init_gdt()
let size = size_of::<gdt>() - 1;
unsafe
{
memcpy(0x800 as *const u8, (&GDT_TABLE as *const _) as *const u8, size);
libc::memcpy(0x800 as *mut c_void, (&GDT_TABLE as *const _) as *const c_void, size);
_gp.limit = size as u16;
_gdt_flush();
}
Expand Down
4 changes: 2 additions & 2 deletions src/keyboard/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::tty;
use crate::tools;
use crate::ferramenta;

mod azerty;
mod qwerty;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub fn get_scancodes()
let mut scancode: u8 = 0;
loop
{
let new_scancode = tools::inb(KEYBOARD_DATA);
let new_scancode = ferramenta::inb(KEYBOARD_DATA);

if new_scancode == scancode
{
Expand Down
2 changes: 1 addition & 1 deletion src/libc/libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
void *memset(void *b, int c, size_t len);
void *memcpy(void *dst, const void *src, size_t n);
void *memmove(void *dst, const void *src, size_t len);
int memcmp(const void *s1, const void *s2, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);

#endif
1 change: 1 addition & 0 deletions src/libc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ extern "C"
{
pub fn memcpy(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void;
pub fn memmove(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void;
pub fn memset(dest: *mut c_void, val: usize, len: usize) -> *mut c_void;
}
58 changes: 38 additions & 20 deletions src/memory/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
pub mod pageframe;
mod pageframe;
mod pagetable;
mod page;

use core::ffi::c_void;
use crate::multiboot::MultibootTagMmap;

extern "C"
{
static _kernel_start: c_void;
static _kernel_end: c_void;
}
static PAGE_SIZE: usize = 4096;

pub fn get_mem_size(mmap: *const MultibootTagMmap, mmap_size: usize) -> usize
{
Expand All @@ -25,26 +22,47 @@ pub fn get_mem_size(mmap: *const MultibootTagMmap, mmap_size: usize) -> usize
}
if MEM_SIZE_BYTES > usize::MAX as u64
{
panic!("This version of ElsOS is in 32 bit, it only supports {}Mo of RAM, you have {}Mo installed", (usize::MAX / 1024) / 1024, (MEM_SIZE_BYTES / 1024) / 1024);
panic!("This version of elsOS is in 32 bit, it only supports {}MiB of RAM, you have {}MiB installed", (usize::MAX / 1024) / 1024, (MEM_SIZE_BYTES / 1024) / 1024);
}
return MEM_SIZE_BYTES as usize;
}
}

pub fn get_largest_mem_seg(mmap: *const MultibootTagMmap, mmap_size: usize) -> usize
pub fn init(mmap: *const MultibootTagMmap, mmap_size: usize)
{
let mut largest_free_mem_seg: usize = 0;
let mut largest_free_mem_seg_size: usize = 0;
let alloc: &mut pageframe::Allocator = pageframe::Allocator::shared();
alloc.read_grub_mmap(mmap, mmap_size);

let page_directory_addr = alloc.request_free_page();
let mut pt_manager = pagetable::Manager::new(page_directory_addr);

id_map(&mut pt_manager);
unsafe
{
for entry in (*mmap).entries(mmap_size)
{
if entry.len as usize > largest_free_mem_seg_size
{
largest_free_mem_seg_size = entry.len as usize;
largest_free_mem_seg = entry.addr as usize;
}
}
load_page_directory(page_directory_addr as *const page::DirectoryEntry);
enable_paging();
}
}

fn id_map(pt_manager: &mut pagetable::Manager)
{
for i in 0..1024
{
pt_manager.memory_map(i * PAGE_SIZE, i * PAGE_SIZE);
pageframe::Allocator::shared().lock_page(i);
}
largest_free_mem_seg
}

extern "C"
{
fn load_page_directory(address: *const page::DirectoryEntry);
fn enable_paging();
}

pub fn page_map_indexer(v_addr: usize) -> (usize, usize)
{
let pdindex = v_addr >> 22;
let ptindex = v_addr >> 12 & 0x03FF;

return (pdindex, ptindex);
}
Loading