Skip to content

Commit

Permalink
* Made the kernel higher half
Browse files Browse the repository at this point in the history
* Implemented Physical memory manager
* Implemented a dynamic memory allocator
* Implemented Paging
* Implemented Virtual filesystem
* Implemented PCI
* Added two new data structures list_t and gentere_t
* Added 32bit and 16bit varients of inb and outb
* Updated link.ld
* Splitted loader.s into multiboot.s and loader.asm
* Updated Makefile
* Added a new header for math operations
* Updated exception handler
* Added new function printE
* Updated string.c
* Added a function to PANIC the kernel
* Updated vga.h
* Updated sysfetch function to include info about paging and kheap
* Bumped kernel version to 2.0.0
  • Loading branch information
Arun007coder committed May 15, 2022
1 parent 9113925 commit 6486376
Show file tree
Hide file tree
Showing 35 changed files with 2,451 additions and 102 deletions.
25 changes: 20 additions & 5 deletions docs/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ Second rewrite of the SectorOS project. Written in C and assembly.
* string stdlib
* Vga color changing
* IDT and GDT functional
* Paging
* malloc
* A basic shell
* Virtual filesystem

## TODO

* Implement driver for serial port, keyboard, mouse and other devices
* Implement VFS
* Implement memory management
* Implement paging
* Implement driver for ata, mouse and other devices
* Implement Tasking
* Implement Networking
* Implement EXT2 filesystem
* Implement ELF loader
* etc...

## Build
Expand All @@ -33,4 +38,14 @@ make -f src/Makefile iso
To run the project, run the following command:
```bash
make -f src/Makefile run
```
```

## Resources

The resources used in this project are:

* [osdev wiki](https://wiki.osdev.org)
* [osdev forums](https://forum.osdev.org)
* [JamesM's kernel development tutorials](http://www.jamesmolloy.co.uk/tutorial_html/)
* [szhou42/osdev](https://github.com/szhou42/osdev)
* [SectorOS](https://github.com/arun007coder/SectorOS)
32 changes: 32 additions & 0 deletions src/Include/gentree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef __GENTREE_H_
#define __GENTREE_H_

#include "list.h"
#include "kheap.h"
#include "system.h"

typedef struct gentree_node
{
list_t *children;
void *data;
}gentree_node_t;

typedef struct gentree
{
gentree_node_t *root;
}gentree_t;

gentree_t* gentree_create();

gentree_node_t* gentree_node_create(void *data);
gentree_node_t* gentree_insert(gentree_t *tree, gentree_node_t *subroot, void *data);
gentree_node_t* gentree_findParent(gentree_t *tree, gentree_node_t *remove_node, int* child_index);
gentree_node_t* gentree_findParent_recur(gentree_t *tree, gentree_node_t *remove_node, gentree_node_t *subroot, int *child_index);

void gentree_remove(gentree_t *tree, gentree_node_t *remove_node);
void tree2list_recur(gentree_node_t *subroot, list_t *list);
void tree2list(gentree_t *tree, list_t *list);
void tree2array(gentree_t *tree, void **array, int *size);
void tree2array_recur(gentree_node_t *subroot, void **array, int *size);

#endif
112 changes: 112 additions & 0 deletions src/Include/kheap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Based on code from http://www.jamesmolloy.co.uk/tutorial_html/4.-The%20Heap.html
#ifndef _KHEAP_H_
#define _KHEAP_H_

#include "system.h"
#include "printf.h"
#include "string.h"
#include "vga.h"
#include "paging.h"

#define KHEAP_START (void*)0xC0400000
#define KHEAP_INITIAL_SIZE 48 * MB
#define KHEAP_MAX_ADDRESS (void*)0xCFFFFFFF
#define HEAP_MIN_SIZE 4 * MB

#define PAGE_SIZE 4096
#define OVERHEAD (sizeof(struct memory_block) + sizeof(uint32_t))

typedef struct memory_block
{
struct memory_block* next;
struct memory_block* prev;
uint32_t size;
} memory_block_t;

extern bool Kheap_enabled;

/*
* if heap is initialized then uss the dynamic memory allocator else use the placement memory allocator
* @param size The size of the memory to be allocated
* @param align Should the memory be aligned?
* @param paddr Should the allocated memory be given a physical address?
*/
uint32_t kmalloc_int(uint32_t size, int align, uint32_t* paddr);

/*
* continuous kmalloc which can be used before the heap is initialised
* @param size The size of the memory to be allocated
* @param align Should the memory be aligned?
* @param paddr Should the allocated memory be given a physical address?
*/
void* kmalloc_c(uint32_t size, int align, uint32_t* paddr);

/*
* Wrapper for kmalloc_int with align = 1
* @param size The size of the memory to be allocated
*/
void* kmalloc_a(uint32_t size);

/*
* Wrapper for kmalloc_int with align = 0 and paddr = paddr
* @param size The size of the memory to be allocated
* @param paddr Should the allocated memory be given a physical address?
*/
uint32_t kmalloc_p(uint32_t size, uint32_t* paddr);

/*
* Wrapper for kmalloc_int with align = 1 and paddr = paddr
* @param size The size of the memory to be allocated
* @param paddr Should the allocated memory be given a physical address?
*/
uint32_t kmalloc_ap(uint32_t size, uint32_t* paddr);

/*
* Wrapper for kmalloc_int with align = 0 and paddr = 0
* @param size The size of the memory to be allocated
*/
void *kmalloc(uint32_t size);

/*
* A function to free a block of memory previously allocated with kmalloc
* @param ptr The pointer to the memory to be freed
*/
void kfree(void* ptr);

/*
* A function to reallocate a block of memory previously allocated with kmalloc
* @param ptr The pointer to the memory to be reallocated
* @param size The size of the memory to be reallocated
*/
void* krealloc(void* ptr, uint32_t size);

void* kcalloc(uint32_t size, uint32_t num);

void print_db();

int doesItFit(memory_block_t* block, uint32_t size);

void setFree(uint32_t *size, int x);

void removeFromList(memory_block_t* block);
void addToList(memory_block_t* block);

int isBetter(memory_block_t* block1, memory_block_t* block2);
memory_block_t* bestFit(uint32_t size);
memory_block_t* getPrevBlock(memory_block_t* block);
memory_block_t* getNextBlock(memory_block_t* block);

uint32_t getRealSize(uint32_t size);
uint32_t getSbrkSize(uint32_t size);

int isFree(memory_block_t* block);
int isEnd(memory_block_t* block);

void init_kheap(void* start, void* end, void* max);

void* malloc(uint32_t size);
void* realloc(void* ptr, uint32_t size);
void* calloc(uint32_t size, uint32_t num);

void free(void* ptr);
#endif
50 changes: 50 additions & 0 deletions src/Include/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef __LIST_H__
#define __LIST_H__

#include "system.h"

typedef struct listnode
{
void *val;
struct listnode *next;
struct listnode *prev;
} listnode_t;

typedef struct list
{
listnode_t *head;
listnode_t *tail;
uint32_t size;
} list_t;

#define foreach(t, list) for(listnode_t * t = list->head; t != NULL; t = t->next)

list_t *list_create();
uint32_t list_size(list_t *list);

listnode_t* list_insert_front(list_t *list, void *val);
void list_insert_back(list_t *list, void *val);

void * list_remove_node(list_t * list, listnode_t * node);
void * list_remove_front(list_t * list);
void * list_remove_back(list_t * list);

void list_push(list_t * list, void * val);
listnode_t * list_pop(list_t * list);

void list_enqueue(list_t * list, void * val);
listnode_t * list_dequeue(list_t * list);

void * list_peek_front(list_t * list);
void * list_peek_back(list_t * list);

void list_destroy(list_t * list);
void listnode_destroy(listnode_t * node);

int list_contain(list_t * list, void * val);

listnode_t * list_get_node_by_index(list_t * list, int index);
void * list_remove_by_index(list_t * list, int index);


#endif
9 changes: 9 additions & 0 deletions src/Include/math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __MATH_H
#define __MATH_H

#define abs(a) (((a) < 0)?-(a):(a))
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define sign(x) ((x < 0) ? -1 :((x > 0) ? 1 : 0))

#endif
91 changes: 91 additions & 0 deletions src/Include/paging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#ifndef __PAGING_H_
#define __PAGING_H_

#include "system.h"
#include "isr.h"
#include "pmm.h"
#include "math.h"

#define PAGE_SIZE 4096

#define IS_ALIGN(addr) ((((uint32_t)(addr)) | 0xFFFFF000) == 0)
#define PAGE_ALIGN(addr) ((((uint32_t)(addr)) & 0xFFFFF000) + 0x1000)

#define PAGEDIR_INDEX(vaddr) (((uint32_t)vaddr) >> 22)
#define PAGETBL_INDEX(vaddr) ((((uint32_t)vaddr) >>12) & 0x3ff)
#define PAGEFRAME_INDEX(vaddr) (((uint32_t)vaddr) & 0xfff)

#define SET_PGBIT(cr0) (cr0 = cr0 | 0x80000000)
#define CLEAR_PSEBIT(cr4) (cr4 = cr4 & 0xffffffef)

typedef struct page_dir_entry
{
uint32_t present :1;
uint32_t rw :1;
uint32_t user :1;
uint32_t write_through :1;
uint32_t cache_disable :1;
uint32_t accessed :1;
uint32_t dirty :1;
uint32_t page_size :1;
uint32_t global :1;
uint32_t avail :3;
uint32_t frame :20;
}page_dir_entry_t;

typedef struct page_table_entry
{
uint32_t present :1;
uint32_t rw :1;
uint32_t user :1;
uint32_t reserved :2;
uint32_t accessed :1;
uint32_t dirty :1;
uint32_t reserved2 :2;
uint32_t avail :3;
uint32_t frame :20;
}page_table_entry_t;

typedef struct page_table
{
page_table_entry_t pages[1024];
}page_table_t;

typedef struct page_directory
{
page_dir_entry_t tables[1024];
page_table_t* ref_tables[1024];
}page_directory_t;

extern int paging_enabled;

extern page_directory_t* TEMP_PAGE_DIR;

extern uint8_t * pmm_bitmap;
extern uint32_t pmm_bitmap_size;

extern page_directory_t* kernel_page_dir;

void* virt2phys(page_directory_t* dir, void* vaddr);
void* d_kmalloc(uint32_t size, int align);

void alloc_region(page_directory_t * dir, uint32_t start_vaddr, uint32_t end_vaddr, int iden_map, int is_kernel, int is_writable);
void free_region(page_directory_t * dir, uint32_t start_vaddr, uint32_t end_vaddr, int free);

void alloc_page(page_directory_t * dir, uint32_t vaddr, uint32_t frame, int is_kernel, int is_writable);
void free_page(page_directory_t * dir, uint32_t vaddr, int free);

void init_paging();

void switch_page_dir(page_directory_t* dir, uint32_t paddr);

void enable_paging();

void* ksbrk(uint32_t size);

void copy_page_dir(page_directory_t* dest, page_directory_t* src);
page_table_t* copy_page_table(page_directory_t * src_page_dir, page_directory_t * dst_page_dir, uint32_t page_dir_idx, page_table_t * src);

void page_fault_handler(registers_t regs);

#endif
Loading

0 comments on commit 6486376

Please sign in to comment.