Skip to content

Commit

Permalink
mmu working fine now yeaaa
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryand1234 committed May 17, 2024
1 parent 5ccc324 commit 607548d
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 55 deletions.
13 changes: 12 additions & 1 deletion kernel/arch/i386/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ SECTIONS
*(.bss)
}

end = .;
. = ALIGN(4096);
.page_directory : {
__page_directory = .;
. += 4096;
}
.page_tables : {
__page_tables_start = .;
. += 4096 * 4;
}

/* The compiler may produce other sections, put them in the proper place in
in this file, if you'd like to include them in the final kernel. */
}
}
8 changes: 6 additions & 2 deletions kernel/include/arch/i386/x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct idtdesc {
#define INTGATE 0x8E
#define TRAPGATE 0xEF


#define NUM_PAGE_TABLES 4

#define KERN_PDIR 0x00001000
#define KERN_STACK 0x0009FFF0
Expand All @@ -54,10 +54,14 @@ struct idtdesc {
#define KERN_HEAP 0x20000000
#define KERN_HEAP_LIM 0x70000000


extern char end;
#define KERN_PG_1 0x400000
#define KERN_PG_1_LIM 0x800000


#define USER_OFFSET 0x40000000
#define USER_STACK 0xE0000000

#define VADDR_PD_OFFSET(addr) ((addr) & 0xFFC00000) >> 22
#define VADDR_PT_OFFSET(addr) ((addr) & 0x003FF000) >> 12
#define VADDR_PG_OFFSET(addr) (addr) & 0x00000FFF
Expand Down
2 changes: 1 addition & 1 deletion kernel/include/mmu/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define MMU_ALLOC_H

#include<stdint.h>
void *ksbrk(uint32_t);
void *ksbrk(uint16_t);
void *kmalloc(uint32_t);
void kfree(void *);

Expand Down
2 changes: 1 addition & 1 deletion kernel/include/mmu/vmm.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct page_directory *pd_create(void);
int pd_destroy(struct page_directory *);
struct page_directory *pd_copy(struct page_directory *pdFather);

int pd0_add_page(char*, char*, char*);
int pd0_add_page(char*, char*, int);

int pd_ad_page(char*, char*, int, struct page_directory *);
int pd_remove_page(char*);
Expand Down
6 changes: 3 additions & 3 deletions kernel/kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ void kernel_main(void) {
// isrs_install();
printf("Terminal initialization compelete\n");
printf("Tesing formating, %c \t%d %s\n", 'Q', 1234, "Hello world from format");
memory_init(2048);
/*
memory_init(20);

char* test = (char*) kmalloc(20);
printf("Memory Address %d\n", test);*/
printf("Memory Address %d\n", test);
divide_by_zero();
printf("DONE");
}
Expand Down
48 changes: 39 additions & 9 deletions kernel/mmu/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,35 @@ void kfree(void *v_addr)
printf("KFREEEEEEEE\n");

}
void *ksbrk(uint32_t n)
void *ksbrk(uint16_t n)
{
//printf("BREakkkkkk\n");
printf("BREakkkkkk\n");

struct kmalloc_header* chunk = (struct kmalloc_header *) kern_heap;
struct kmalloc_header* chunk;
char *p_addr;
uint32_t i;
printf("KSBRK: N: %d\n", n);

if((kern_heap + (n*PAGESIZE)) > (char*) KERN_HEAP_LIM) {
printf("PANIC ksbrk(): no memory left\n");
return (char *) -1;
}

chunk = (struct kmalloc_header *) kern_heap;

for(i = 0; i < n; i++) {
p_addr = get_page_frame();
printf("P ADDR: %d\n", p_addr);
if((uint32_t)(p_addr) < 0) {
printf("PANIC ksbrk(): no page left\n");
return (char*) -1;
}
pd0_add_page(kern_heap, p_addr, 0);
kern_heap += PAGESIZE;
}

chunk->size = PAGESIZE*n;
chunk->used = 0;
return chunk;
}
void *kmalloc(uint32_t size)
Expand All @@ -27,15 +51,21 @@ void *ksbrk(uint32_t n)
realsize = KMALLOC_MINSIZE;
}

struct kmalloc_header *chunk = (struct kmalloc_header*) KERN_HEAP;
printf("Size required: %d\n", realsize);
struct kmalloc_header *chunk, *other;
chunk = (struct kmalloc_header*) &end;
while(chunk->used || chunk->size < realsize)
{
//printf("chunk: %d, %d\n", chunk->used, chunk->size);
if(chunk->size == 0) {
printf("PANIC: kmalloc(): corrupted chunk on %d with null size (heap %d). System halt", chunk, kern_heap);
asm("hlt");
return 0;
}
chunk = (struct kmalloc_header*) ((char*)chunk + chunk->size);
printf("C: %d, S: %d, U: %d, h: %d", chunk, chunk->used, chunk->size, kern_heap);
if(chunk == (struct kmalloc_header*) KERN_HEAP)
if(chunk == (struct kmalloc_header*) kern_heap)
{
if((int)ksbrk(realsize/PAGESIZE) < 0)
chunk = (ksbrk((realsize/PAGESIZE)+1));
if((int)chunk < 0)
{
printf("No memory left %d, heap: %d", chunk, kern_heap);
asm("hlt");
Expand All @@ -53,7 +83,7 @@ void *ksbrk(uint32_t n)
{
chunk->used = 1;
} else {
struct kmalloc_header* other = (struct kmalloc_header*)((char*) chunk+ realsize);
other = (struct kmalloc_header*)((char*) chunk+ realsize);
other->size = (uint32_t) (chunk-size - realsize);
other->used = 0;
chunk->size = realsize;
Expand Down
84 changes: 46 additions & 38 deletions kernel/mmu/vmm.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#include<mmu/vmm.h>
#include<stdio.h>
extern uint32_t __page_directory;
extern uint32_t __page_tables_start;
char *kern_heap;
struct list_head kern_free_vm;
uint32_t *pd0 = (uint32_t *) KERN_PDIR;
char *pg0 = (char*) 0;
uint32_t *pd0 = (uint32_t *) ( &__page_directory);
char *pg0 = ((uint32_t *)&__page_tables_start);
char *pg1 = (char*) KERN_PG_1;
char *pg1_end = (char*) KERN_PG_1_LIM;
uint8_t mem_bitmap[RAM_MAXPAGE /8];



uint32_t kmalloc_used = 0;

char* get_page_frame(void)
Expand Down Expand Up @@ -119,8 +123,8 @@ extern uint32_t read_cr0(void);
void memory_init(uint32_t high_mem)
{
int pg, pg_limit;
struct vm_area *p, *pd;

struct vm_area *p, *pm;
uint32_t i;
pg_limit = (high_mem * 1024) / PAGESIZE;

for(pg = 0; pg < pg_limit/8; pg++){
Expand All @@ -130,51 +134,32 @@ extern uint32_t read_cr0(void);
for(pg = pg_limit/8; pg < RAM_MAXPAGE /8; pg++){
mem_bitmap[pg] = 0xFF;
}

/*for(pg = PAGE(0x0); pg < (uint32_t)(PAGE((uint32_t) pg1_end)); pg++)
/*
for(pg = PAGE(0x0); pg < (uint32_t)(PAGE((uint32_t) pg1_end)); pg++)
{
set_page_frame_used(pg);
}
*/
/* pd[0] = ((uint32_t) pg0 | (PG_PRESENT | PG_WRITE | PG_4MB));
pd[1] = ((uint32_t) pg1 | (PG_PRESENT | PG_WRITE | PG_4MB));
pd0[0] = ((uint32_t) pg0 | (PG_PRESENT | PG_WRITE | PG_4MB));
pd0[1] = ((uint32_t) pg1 | (PG_PRESENT | PG_WRITE | PG_4MB));
for(i = 2; i < 1023; i++)
{
pd0[i] = ((uint32_t) pg1 + PAGESIZE*i (PG_PRESENT | PG_WRITE));
pd0[i] = ((uint32_t) pg1 + PAGESIZE*i | (PG_PRESENT | PG_WRITE));
}
pd0[1023] = ((uint32_t) pd0 | (PG_PRESENT | PG_WRITE));*/
uint32_t *page_dir = (uint32_t *) 0x9C000;
uint32_t *page_table = (uint32_t *) 0x9D000;
uint32_t address = 0;
uint32_t i;
for(i = 0; i < 1024; i++)
{
page_table[i] = address | 3;
address = address + 4096;
};

page_dir[0] = page_table;
page_dir[1] = page_dir[0] | 3;
for(i = 1; i < 1024; i++)
{
page_dir[i] = 0 | 3;
};
/*asm volatile(" mov %0, %%eax \n \
mov %%eax, %%cr3 \n \
mov %%cr4, %%eax \n \
or %2, %%eax \n \
mov %%eax, %%cr4 \n \
mov %%cr0, %%eax \n \
or %1, %%eax \n \
mov %%eax, %%cr0":: "m"(pd0), "i"(PAGING_FLAG), "i"(PSE_FLAG));*/
write_cr3(page_dir);
pd0[1023] = ((uint32_t) pd0 | (PG_PRESENT | PG_WRITE));
*/
for (int i = 0; i < 1024; i++) {
pg0[i] = (i * 0x1000) | 3; // Present, read/write
}
pd0[0] = ((uint32_t) pg0) | 3;
write_cr3(pd0);
write_cr0(read_cr0() | 0x000000000);
//asm("hlt");

kern_heap = (char*) KERN_HEAP;
// ksbrk(1);
kern_heap = &end;
ksbrk(1);

p = (struct vm_area*) kmalloc(sizeof(struct vm_area));
p->vm_start = (char*) KERN_PG_HEAP;
Expand Down Expand Up @@ -218,4 +203,27 @@ extern uint32_t read_cr0(void);
}*/
int pd0_add_page(char *v_addr, char *p_addr, int flags)
{
uint32_t *pde;
uint32_t *pte;

if (v_addr > (char *) USER_OFFSET) {
printf("ERROR: pd0_add_page(): %d is not in kernel space !\n", v_addr);
return 0;
}

/* On verifie que la table de page est bien presente */
pde = (uint32_t *) (0xFFFFF000 | (((uint32_t) v_addr & 0xFFC00000) >> 20));
if ((*pde & PG_PRESENT) == 0) {
//error
printf("\nERROR: %d\n", *pde);
// return 0;
}

/* Modification de l'entree dans la table de page */
pte = (uint32_t *) (0xFFC00000 | (((uint32_t) v_addr & 0xFFFFF000) >> 10));
*pte = ((uint32_t) p_addr) | (PG_PRESENT | PG_WRITE | flags);
set_page_frame_used(p_addr);
return 0;
}

0 comments on commit 607548d

Please sign in to comment.