aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/boot/boot.S13
-rw-r--r--src/bringelle.c7
-rw-r--r--src/core/paging.c98
-rw-r--r--src/core/paging.h17
-rw-r--r--src/core/scheduler.c0
-rw-r--r--src/core/scheduler.h5
-rw-r--r--src/linker.ld14
7 files changed, 135 insertions, 19 deletions
diff --git a/src/boot/boot.S b/src/boot/boot.S
index e436cbb..9777fbf 100644
--- a/src/boot/boot.S
+++ b/src/boot/boot.S
@@ -3,6 +3,8 @@
.extern bringelle
.extern gdt_memcpy
.extern mb_load_fb_tag
+.extern _bss_start
+.extern _bss_end
.set STACK_LOCATION, 0x1FFFFF
@@ -73,6 +75,17 @@ movl $STACK_LOCATION,%esp
mov $0x38, %eax
ltr %ax
+# Zeroing the .bss section
+mov $_bss_start, %eax
+mov $_bss_end, %ebx
+start_zeroing:
+ movb $0x0, (%eax)
+ cmp %eax, %ebx
+ je end_zeroing
+ inc %eax
+ jmp start_zeroing
+end_zeroing:
+
# Start kernel main function
call bringelle
diff --git a/src/bringelle.c b/src/bringelle.c
index b0ddec6..e29ffc5 100644
--- a/src/bringelle.c
+++ b/src/bringelle.c
@@ -27,6 +27,11 @@ void bringelle(){
print("Paging enable!\n");
print("Kernel started !");
+ paging_allocate(5);
+ print("\n");
+ paging_dump(1024,-1);
+ print("\n");
+
show_tics=1;
// Utask
print("Launch user task ");
@@ -54,6 +59,7 @@ void bringelle(){
while(1);
}
+
void clock(){
static int tic=0;
static int sec=0;
@@ -66,7 +72,6 @@ void clock(){
}
}
-
void page_fault(){
print("Page fault!");
}
diff --git a/src/core/paging.c b/src/core/paging.c
index b1f462f..c32a3bc 100644
--- a/src/core/paging.c
+++ b/src/core/paging.c
@@ -1,21 +1,43 @@
#include "paging.h"
+#include "libc/stdio.h"
+
+/// Use a bitmap to keep track of allocated pages
+char pages_status[PAGING_MAX_PAGES/8];
+/// Kernel page directory (ATTENTION need to be 4096)
+u32 k_pd[PAGING_MAX_DIR_ENTRY] __attribute__((aligned(4096)));
+/// Kernel page table
+u32 k_pt[PAGING_MAX_DIR_ENTRY][1024] __attribute__((aligned(4096)));
void paging_enable(){
- int *page_dir=(int*)PAGING_DIR_LOCATION;
- int *page_table=(int*)PAGING_TABLE_LOCATION;
+ // Init pages status
+ for(int i=0;i<PAGING_MAX_PAGES/8;i++)
+ pages_status[i]=0;
// Init page directory
- for(int i=0;i<1024;i++)
- page_dir[i]=0;
- page_dir[0]=(int)page_table;
- page_dir[0] |=7; // Permissions
+ for(int i=0;i<PAGING_MAX_DIR_ENTRY;i++){
+ k_pd[i]=0;
+ }
+ k_pd[0]=((int)&k_pt[0][0]);
+ k_pd[0]|=7; // Permissions
// Init page table 0
int addr_offset=0;
for(int i=0;i<1024;i++){
- page_table[i]=addr_offset;
- page_table[i]|=7; // Permission
- addr_offset+=4096; // 4Ko pages
+ k_pt[0][i]=addr_offset;
+ k_pt[0][i]|=7; // Permission
+ paging_set_usage(addr_offset,1); // Mark addr as used
+ addr_offset+=PAGING_PAGE_SIZE; // 4Ko pages
+ }
+
+ // Allow access to more ram
+ for(int i=1;i<PAGING_MAX_DIR_ENTRY;i++){
+ k_pd[i]=((int)&k_pt[i][0]);
+ k_pd[i]|=7; // Permissions
+ for(int j=0;j<1024;j++){
+ k_pt[i][j]=addr_offset;
+ k_pt[i][j]|=7; // Permission
+ addr_offset+=PAGING_PAGE_SIZE; // 4Ko pages
+ }
}
// Turns on paging
@@ -25,7 +47,63 @@ void paging_enable(){
"movl %%cr0, %%eax \n\t"
"orl %1, %%eax \n\t"
"movl %%eax, %%cr0 \n\t" // Turn on paging
- :: "i" (PAGING_DIR_LOCATION), "i" (PAGING_CR0_BIT)
+ :: "b" (k_pd), "i" (PAGING_CR0_BIT)
);
+}
+void paging_set_usage(int addr,char state){
+ char bytes=pages_status[addr/PAGING_PAGE_SIZE/8];
+ char bit=addr/PAGING_PAGE_SIZE%8;
+ if(state=0)
+ pages_status[addr/PAGING_PAGE_SIZE/8]=~(1<<bit)&bytes;
+ else
+ pages_status[addr/PAGING_PAGE_SIZE/8]=(1<<bit)|bytes;
+}
+
+void paging_dump(int min,int max){
+ for(int i=0;i<(PAGING_MAX_PAGES/8);i++){
+ for(int j=0;j<8;j++){
+ char status=(pages_status[i]>>j)&0x1;
+ if((i*8+j)>=min){
+ if((i*8+j)<max || max<0)
+ printi(status);
+ }
+ }
+ }
+}
+
+char* paging_allocate_next_page(){
+ for(int i=0;i<(PAGING_MAX_PAGES/8);i++){
+ char bytes=pages_status[i];
+ for(int j=0;j<8;j++){
+ char state=(bytes>>j)&1;
+ if(state!=1){
+ int page_id=i*8+j;
+ int page_addr=PAGING_PAGE_SIZE*page_id;
+ paging_set_usage(page_addr,1);
+ return((char*)page_addr);
+ }
+ }
+ }
+ print("Could not allocate anymore pages! Stopping...");
+ asm("hlt");
+}
+
+char *paging_allocate(int p){
+ int *page_dir=(int*)paging_allocate_next_page();
+ int *page_table;
+ int current_page_entry=0;
+ int current_dir_entry=0;
+ while(current_page_entry<p){
+ if(current_page_entry%1024 == 0){
+ current_dir_entry++;
+ page_table=(int*)paging_allocate_next_page();
+ page_dir[current_dir_entry]=(int)page_table;
+ }
+ int entry=current_page_entry%1024;
+ page_table[current_page_entry]=(int)paging_allocate_next_page();
+ current_page_entry++;
+ print("Jean\n");
+ }
+ print("end");
} \ No newline at end of file
diff --git a/src/core/paging.h b/src/core/paging.h
index 1e8f4e3..ad3671e 100644
--- a/src/core/paging.h
+++ b/src/core/paging.h
@@ -2,12 +2,25 @@
#define PAGING_H
#define PAGING_CR0_BIT 0x80000000
-#define PAGING_DIR_LOCATION 0x1000
-#define PAGING_TABLE_LOCATION 0x5000
+#define PAGING_PAGE_SIZE 4096
+#define PAGING_MAX_PAGES 2048
+#if PAGING_MAX_PAGES%1024>0
+ #define PAGING_MAX_DIR_ENTRY PAGING_MAX_PAGES/1024+1
+#else
+ #define PAGING_MAX_DIR_ENTRY PAGING_MAX_PAGES/1024
+#endif
/*
* Configure and enable paging
*/
void paging_enable();
+char* paging_allocate_next_page();
+void paging_set_usage(int addr,char state);
+/**
+ * Create a new page directory containing
+ * p pages.
+ */
+char *paging_allocate(int p);
+void paging_dump(int min,int max);
#endif \ No newline at end of file
diff --git a/src/core/scheduler.c b/src/core/scheduler.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/core/scheduler.c
diff --git a/src/core/scheduler.h b/src/core/scheduler.h
new file mode 100644
index 0000000..f15168b
--- /dev/null
+++ b/src/core/scheduler.h
@@ -0,0 +1,5 @@
+#ifndef SCHEDULER_H
+#define SCHEDULER_H
+
+
+#endif \ No newline at end of file
diff --git a/src/linker.ld b/src/linker.ld
index fdc7b1b..d281e81 100644
--- a/src/linker.ld
+++ b/src/linker.ld
@@ -5,23 +5,25 @@ OUTPUT_ARCH(i386)
SECTIONS {
. = 1M;
- .text :
+ .text : ALIGN(4)
{
*(.multiboot)
*(.text)
}
- .rodata :
+ .rodata : ALIGN(4)
{
*(.rodata)
}
- .data :
+ .data : ALIGN(4)
{
*(.data)
}
- .bss :
+ .bss : ALIGN(4)
{
- *(.bss)
- *(COMMON)
+ _bss_start = .;
+ *(.bss);
+ _bss_end = .;
+ *(COMMON);
}
} \ No newline at end of file