Skip to content

Commit

Permalink
Fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryand1234 committed May 9, 2024
1 parent 36f128b commit 59ac8ff
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 39 deletions.
4 changes: 2 additions & 2 deletions kernel/include/ds/list.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef LIST_H
#define LIST_H
#ifndef DS_LIST_H
#define DS_LIST_H

struct list_head {
struct list_head *next, *prev;
Expand Down
14 changes: 7 additions & 7 deletions kernel/include/mmu/alloc.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifdef ALLOC_H
#define ALLOC_H
#ifndef MMU_ALLOC_H
#define MMU_ALLOC_H

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

extern "C" {
int kbsrk(uint32_t)
void *kmalloc(uint32_t);
void kfree(void *);
}

#endif
6 changes: 2 additions & 4 deletions kernel/include/mmu/vmm.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#ifndef VMM_H
#define VMM_H
#ifndef MMU_VMM_H
#define MMU_VMM_H


#include<mmu/alloc.h>
#include<ds/list.h>
#include<arch/i386/x86.h>
#include<stddef.h>

struct page {
char *v_addr;
char *p_addr;
Expand Down
7 changes: 4 additions & 3 deletions kernel/kernel/kernel.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include<stdio.h>
#include<kernel/tty.h>
#include<arch/i386/x86.h>
//#include<arch/i386/x86.h>
#include<mmu/vmm.h>
#include<mmu/alloc.h>

void divide_by_zero(void);

void kernel_main(void) {
Expand All @@ -12,7 +12,8 @@ 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");
char* test = kmalloc(20);
memory_init(2048);
char* test = (char*) kmalloc(20);
printf("Memory Address %d\n", test);
divide_by_zero();
printf("DONE");
Expand Down
21 changes: 10 additions & 11 deletions kernel/mmu/alloc.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include<mmu.h>
#include<mmu/vmm.h>
#include<stdio.h>

extern "C" {
#include<stdint.h>
void *kmalloc(uint32_t size)
{
if(size <= 0)
Expand All @@ -13,23 +12,23 @@ extern "C" {
uint32_t realsize = sizeof(struct kmalloc_header) + size;
if(realsize < KMALLOC_MINSIZE)
{
realsize = KMALLOCSIZE;
realsize = KMALLOC_MINSIZE;
}

chunk = (struct *kmalloc_header) KERN_HEAP;
struct kmalloc_header *chunk = (struct kmalloc_header*) KERN_HEAP;
while(chunk->used || chunk->size < realsize)
{
printf("chunk: %d, %d\n", chunk->used, chunk->size);
chunk = (struct *KMALLOC_HEADER) ((char*)chunk + chunk->size);
if(chunk == (struct *KMALLOC_HEADER) kern_heap)
chunk = (struct kmalloc_header*) ((char*)chunk + chunk->size);
if(chunk == (struct kmalloc_header*) kern_heap)
{
if((int)ksbrk(realsize/PAGESIZE) < 0)
{
printf("No memory left %d, heap: %d", chunk, kern_heap);
asm("hlt");
return 0;
}
} else if(chunk > (struct *KMALLOC_HEADER) kern_heap)
} else if(chunk > (struct kmalloc_header*) kern_heap)
{
printf("Got address %d while heap limit is %d", chunk, kern_heap);
asm("hlt");
Expand All @@ -41,8 +40,8 @@ extern "C" {
{
chunk->used = 1;
} else {
other = (struct *kmalloc_header)((chunk*) chunk + realsize)
other->size = chunk-size - realsize;
struct kmalloc_header* other = (struct kmalloc_header*)((char*) chunk+ realsize);
other->size = (uint32_t) (chunk-size - realsize);
other->used = 0;
chunk->size = realsize;
chunk->used = 1;
Expand All @@ -52,4 +51,4 @@ extern "C" {

return (char *) chunk + sizeof(struct kmalloc_header);
}
}

25 changes: 13 additions & 12 deletions kernel/mmu/vmm.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include<mmu/vmm.h>
#include<stdio.h>
#include<ds/list.h>
#include<mmu/alloc.h>
char *kern_heap;
struct list_head kern_free_vm;
uint32_t *pd0 = (uint32_t *) KERN_PDIR;
Expand Down Expand Up @@ -49,7 +46,7 @@
printf("PANIC: get_page_from_heap(): no memory left in page heap. System can't work\n");
}

area = list_first_entry(&kern_free_vm, struct m_area, list);
area = list_first_entry(&kern_free_vm, struct vm_area, list);
v_addr = area->vm_start;

area->vm_start += PAGESIZE;
Expand All @@ -70,7 +67,7 @@
return pg;
}

int release_page_from_heap(char *v_addr)
/* int release_page_from_heap(char *v_addr)
{
struct vm_area *next_area, *prev_area, *new_area;
char *p_addr;
Expand All @@ -86,8 +83,9 @@
pd_remove_page(v_addr);
list_for_each_entry(next_area, &kern_free_vm, list) {
if(next_area->vm_start > v_addr)
if(next_area->vm_start > v_addr){
break;
}
}
prev_area = list_entry(next_area->list.prev, struct vm_area, list);
Expand All @@ -113,7 +111,7 @@
return 0;
}

*/
void memory_init(uint32_t high_mem)
{
int pg, pg_limit;
Expand All @@ -122,11 +120,13 @@

pg_limit = (high_mem * 1024) / PAGESIZE;

for(pg = 0; pg < pg_limit/8; pg++)
for(pg = 0; pg < pg_limit/8; pg++){
mem_bitmap[pg] = 0;
}

for(pg = pg_limit/8; pg < RAM_MAXPAGE /8; pg++)
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++)
{
Expand All @@ -143,14 +143,15 @@
pd0[1023] = ((uint32_t) pd0 | (PG_PRESENT | PG_WRITE));*/

asm(" mov %0, %%eax \n \
asm volatile(" mov %0, %%eax \n \
mov %%eax, %%cr3 \n \
mov %%cr4, %%eax \n \
or %3, %%eax \n \
or %2, %%eax \n \
mov %%eax, %%cr4 \n \
mov %%cr0, %%eax \n \
or %1, %%eax \n \
mov %%eax, %%cr0":: "m"(pd0), "1"(PAGING_FLAG), "i"(PSE_FLAG));
mov %%eax, %%cr0":: "m"(pd0), "i"(PAGING_FLAG), "i"(PSE_FLAG));


kern_heap = (char*) KERN_HEAP;
ksbrk(1);
Expand Down

0 comments on commit 59ac8ff

Please sign in to comment.