mirror of https://github.com/E-Almqvist/eOS
commit
8c7b94116f
@ -0,0 +1,23 @@ |
||||
[bits 32] ; 32-bit mode |
||||
|
||||
PAGING_ENABLE_FLAG equ 0x80000001 |
||||
PAGE_DIRECTORY_ADDR equ 0xffffffff ; TODO: change me to something good |
||||
|
||||
global enable_paging_registers ; make the SR "global" so that we can access it in the kernel etc |
||||
enable_paging_registers: |
||||
push eax |
||||
mov eax, PAGE_DIRECTORY_ADDR ; Move the address of the |
||||
; page register (page directory) into eax |
||||
; (Using eax as a middle-man register) |
||||
mov cr3, eax ; Put the address into the cr3 register (required by the MMU) |
||||
|
||||
mov eax, cr0 ; eax as a middle-man register (again) |
||||
or eax, PAGING_ENABLE_FLAG ; perform the OR operation on eax (ex: 0b01 or 0b10 = 0b11) |
||||
; This is needed to enable paging (set the flag as "enabled") |
||||
|
||||
mov cr0, eax ; Move it into cr0 to finally enable paging |
||||
|
||||
pop eax |
||||
ret ; return to last location |
||||
|
||||
|
@ -1,2 +1,102 @@ |
||||
#include "memory.h" |
||||
#include "../drivers/vga.h" |
||||
|
||||
// https://wiki.osdev.org/Page_Frame_Allocation
|
||||
// page = block
|
||||
|
||||
// MAX_BLOCK_SIZE_IN_BITS/8 bytes
|
||||
// i.e. : bit i of byte n define status of block 8n+i
|
||||
// block = 8n+i
|
||||
|
||||
// in 32-bit mode, we have access to 2^32 blocks
|
||||
// which is (2^32)*BLOCK_SIZE blocks
|
||||
// and with a blocksize of 1024, we git around 4.3 trillion blocks
|
||||
// which is more than enough
|
||||
|
||||
#define CHECK_BITMAP(map, idx) ((map) & (1<<(idx))) |
||||
|
||||
#define BLOCK_TO_MEMP(idx) (pointer)(PM_MEM_START + (idx*BLOCK_SIZE)) |
||||
|
||||
#define MEMP_TO_BLOCK(memp) (uint)((memp - PM_MEM_START)/BLOCK_SIZE) |
||||
|
||||
static int bitmap = 0;
|
||||
static uint last_block; |
||||
void mod_bitmap(uint bit, uint bflag) { |
||||
// create a bitmask that will be applied to the bitmap
|
||||
int bitmask = 1 << bit; |
||||
|
||||
// apply the bitmask, resulting in the bit:n bit will set
|
||||
// set to bflag
|
||||
bitmap = (((bitmap & ~bitmask)) | (bflag << bit)); |
||||
} |
||||
|
||||
|
||||
pointer block_alloc(uint blockidx) { |
||||
int block_bflag; |
||||
block_bflag = CHECK_BITMAP(bitmap, blockidx); |
||||
|
||||
if( block_bflag == BM_FREE ) { // check if block is free
|
||||
println("Allocating block...", DEFAULT_COLOR); |
||||
|
||||
mod_bitmap(blockidx, 1); |
||||
last_block = blockidx; |
||||
|
||||
return BLOCK_TO_MEMP(blockidx);
|
||||
} else { |
||||
println("[ERROR] Attemped to allocate non-free block.", 0x0c); |
||||
|
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
void block_free(uint blockidx) { |
||||
println("Dealloc block...", DEFAULT_COLOR);
|
||||
mod_bitmap(blockidx, BM_FREE); |
||||
last_block = blockidx; |
||||
} |
||||
|
||||
uint find_free(uint block_count) { |
||||
// TODO: find a free start block to allocate
|
||||
// loop through bitmap
|
||||
// check if range is free
|
||||
} |
||||
|
||||
bool check_block_range(uint start, uint end) { |
||||
bool allowed = true;
|
||||
|
||||
uint idx; |
||||
for(idx = start; idx <= end; idx++) { |
||||
if( CHECK_BITMAP(bitmap, idx) != BM_FREE ) |
||||
allowed = false; |
||||
break; |
||||
} |
||||
|
||||
return allowed; |
||||
} |
||||
|
||||
void pm_alloc_range(ulong start, ulong end, bool force) { |
||||
uint idx_start; |
||||
uint idx_end; |
||||
|
||||
ulong d_addr = end - start; // memory size
|
||||
uint num_blocks = (d_addr/BLOCK_SIZE) + 1; // amount of blocks to be allocated
|
||||
|
||||
uint start_block = MEMP_TO_BLOCK(start); // start idx, end = start_block + num_blocks - 1
|
||||
bool allowed = true && check_block_range(start_block, start_block + num_blocks - 1); |
||||
|
||||
// allocate (if permitted)
|
||||
if( allowed ) { |
||||
uint idx; |
||||
for(idx=start_block; idx <= start_block + num_blocks - 1; idx++)
|
||||
block_alloc(idx); |
||||
|
||||
return; |
||||
} else { |
||||
println("[ERROR] Tried to allocate memory range without permission!", 0x0c); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
pointer pm_malloc(uint block_count) { |
||||
|
||||
} |
||||
|
@ -1,2 +1,25 @@ |
||||
char* malloc(unsigned int size); |
||||
void mfree(char* p); |
||||
#include "../lib/types.h" |
||||
|
||||
#define BLOCK_SIZE 1024 // 1 KiB
|
||||
#define MAX_BLOCK_COUNT 32 // placeholder
|
||||
#define MEMSIZE_TO_BLOCKS(n) ((n*1024)/BLOCK_SIZE) |
||||
|
||||
#define BM_FREE 0 |
||||
|
||||
#define PM_MEM_START 0x000000 // start of memory
|
||||
#define PM_MEM_END 0xb7000 // end of memory TODO: fix me/change or something
|
||||
|
||||
// void init_pmm(uint map_addr, uint bsize); // Initialize physical memory manager
|
||||
|
||||
void mod_bitmap(uint bit, uint flag); |
||||
|
||||
pointer block_alloc(uint blockidx); // allocate a block
|
||||
void block_free(uint blockidx); // free a block
|
||||
|
||||
uint find_free(uint block_count); |
||||
|
||||
bool check_block_range(uint start, uint end); |
||||
void pm_alloc_range(ulong start, ulong end, bool force); // allocate a range of memory
|
||||
|
||||
pointer pm_malloc(uint block_count); // allocate some blocks
|
||||
void pm_free(int* p); // free a var (if allocated with pm_malloc)
|
||||
|
@ -0,0 +1,24 @@ |
||||
#include "paging.h" |
||||
#include "../drivers/vga.h" |
||||
|
||||
void enable_paging() { |
||||
println("Enabling paging...", DEFAULT_COLOR); |
||||
|
||||
// extern int enable_paging_registers(); // Call the assembly SR
|
||||
// enable_paging_registers(); // and enable paging
|
||||
|
||||
return; |
||||
} |
||||
|
||||
// Page Entry struct
|
||||
//struct page_entry {
|
||||
// unsigned int index;
|
||||
// unsigned int start_addr;
|
||||
//} page_table[PAGE_TABLE_SIZE];
|
||||
//
|
||||
//int get_phys_addr(int virt_addr) {
|
||||
// int index = virt_addr / PAGE_SIZE; // page index for the virtual address
|
||||
// int offset = virt_addr % PAGE_SIZE; // actual physical offset for the address
|
||||
//
|
||||
// return page_table[index].start_addr + offset;
|
||||
//}
|
@ -0,0 +1,9 @@ |
||||
#define PAGE_SIZE 100 |
||||
#define PAGE_TABLE_SIZE 128 |
||||
|
||||
void enable_paging(); |
||||
|
||||
// struct page_entry;
|
||||
|
||||
// char** heap_alloc(unsigned int size); // Process heap allocation
|
||||
// int get_phys_addr(int virt_addr);
|
@ -0,0 +1,17 @@ |
||||
#include "conv.h" |
||||
#include "../drivers/vga.h" |
||||
|
||||
char* int_to_str(int i, char* buf) { |
||||
ulong num = (ulong)i; // convert to ulong
|
||||
uint len = ulong_len(num); // number of digits
|
||||
|
||||
*(buf+len) = '\0'; // add a "end-of-string" at the end
|
||||
|
||||
int j; |
||||
for(j = 0; j < len; j++) // iterate over each digit and assign it to the buffer
|
||||
// super dangerous memory write
|
||||
println("char!", DEFAULT_COLOR); |
||||
*(buf+j) = (char)(ndigit(num, len-1-j) + ASCII_OFFSET); // apply the ascii offset so that i becomes a char
|
||||
|
||||
return buf; |
||||
} |
@ -0,0 +1,6 @@ |
||||
#include "types.h" |
||||
#include "util.h" |
||||
|
||||
#define ASCII_OFFSET 0x30 |
||||
|
||||
char* int_to_str(int i, char* buf); |
@ -0,0 +1,10 @@ |
||||
#include "math.h" |
||||
|
||||
long pow(int num, uint expon) { |
||||
long prod = 1; |
||||
while(expon > 0)
|
||||
prod *= num; |
||||
expon--; |
||||
|
||||
return prod; |
||||
} |
@ -0,0 +1,3 @@ |
||||
#include "types.h" |
||||
|
||||
long pow(int, uint); |
@ -1,15 +0,0 @@ |
||||
#include "strf.h" |
||||
|
||||
#define int_offset 48 |
||||
// 0:48 - 9:57
|
||||
|
||||
char* int_to_str(int i, char* strbuf) { |
||||
if( i == 0 ) { |
||||
return (char*)(int_offset); |
||||
} else { |
||||
char cbuf; |
||||
cbuf = (char)((i % 10) + int_offset); |
||||
|
||||
return int_to_str(i / 10, strbuf + cbuf); |
||||
} |
||||
} |
@ -1 +0,0 @@ |
||||
char* int_to_str(int i, char* strbuf); |
@ -0,0 +1,6 @@ |
||||
typedef unsigned int uint; |
||||
typedef unsigned long ulong; |
||||
typedef unsigned long* pointer; |
||||
typedef int bool; |
||||
#define true 1 |
||||
#define false 0 |
@ -0,0 +1,16 @@ |
||||
#include "util.h" |
||||
#include "math.h" |
||||
|
||||
uint ulong_len(ulong n) { // get the digit length of a number
|
||||
int len = 0; |
||||
while (n != 0) { |
||||
n = n / 10; |
||||
++len; |
||||
} |
||||
return len; |
||||
} |
||||
|
||||
uint ndigit(ulong n, uint i) { // OBS: index order is reversed
|
||||
long den = pow(10, i); |
||||
return (n/den) % 10; |
||||
} |
@ -0,0 +1,4 @@ |
||||
#include "types.h" |
||||
|
||||
uint ulong_len(ulong n); |
||||
uint ndigit(ulong n, uint i); |
Loading…
Reference in new issue