Skip to content

Commit 6486376

Browse files
committed
* Made the kernel higher half
* 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
1 parent 9113925 commit 6486376

35 files changed

+2451
-102
lines changed

docs/Readme.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ Second rewrite of the SectorOS project. Written in C and assembly.
99
* string stdlib
1010
* Vga color changing
1111
* IDT and GDT functional
12+
* Paging
13+
* malloc
14+
* A basic shell
15+
* Virtual filesystem
1216

1317
## TODO
1418

15-
* Implement driver for serial port, keyboard, mouse and other devices
16-
* Implement VFS
17-
* Implement memory management
18-
* Implement paging
19+
* Implement driver for ata, mouse and other devices
20+
* Implement Tasking
21+
* Implement Networking
22+
* Implement EXT2 filesystem
23+
* Implement ELF loader
1924
* etc...
2025

2126
## Build
@@ -33,4 +38,14 @@ make -f src/Makefile iso
3338
To run the project, run the following command:
3439
```bash
3540
make -f src/Makefile run
36-
```
41+
```
42+
43+
## Resources
44+
45+
The resources used in this project are:
46+
47+
* [osdev wiki](https://wiki.osdev.org)
48+
* [osdev forums](https://forum.osdev.org)
49+
* [JamesM's kernel development tutorials](http://www.jamesmolloy.co.uk/tutorial_html/)
50+
* [szhou42/osdev](https://github.com/szhou42/osdev)
51+
* [SectorOS](https://github.com/arun007coder/SectorOS)

src/Include/gentree.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef __GENTREE_H_
2+
#define __GENTREE_H_
3+
4+
#include "list.h"
5+
#include "kheap.h"
6+
#include "system.h"
7+
8+
typedef struct gentree_node
9+
{
10+
list_t *children;
11+
void *data;
12+
}gentree_node_t;
13+
14+
typedef struct gentree
15+
{
16+
gentree_node_t *root;
17+
}gentree_t;
18+
19+
gentree_t* gentree_create();
20+
21+
gentree_node_t* gentree_node_create(void *data);
22+
gentree_node_t* gentree_insert(gentree_t *tree, gentree_node_t *subroot, void *data);
23+
gentree_node_t* gentree_findParent(gentree_t *tree, gentree_node_t *remove_node, int* child_index);
24+
gentree_node_t* gentree_findParent_recur(gentree_t *tree, gentree_node_t *remove_node, gentree_node_t *subroot, int *child_index);
25+
26+
void gentree_remove(gentree_t *tree, gentree_node_t *remove_node);
27+
void tree2list_recur(gentree_node_t *subroot, list_t *list);
28+
void tree2list(gentree_t *tree, list_t *list);
29+
void tree2array(gentree_t *tree, void **array, int *size);
30+
void tree2array_recur(gentree_node_t *subroot, void **array, int *size);
31+
32+
#endif

src/Include/kheap.h

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Based on code from http://www.jamesmolloy.co.uk/tutorial_html/4.-The%20Heap.html
2+
#ifndef _KHEAP_H_
3+
#define _KHEAP_H_
4+
5+
#include "system.h"
6+
#include "printf.h"
7+
#include "string.h"
8+
#include "vga.h"
9+
#include "paging.h"
10+
11+
#define KHEAP_START (void*)0xC0400000
12+
#define KHEAP_INITIAL_SIZE 48 * MB
13+
#define KHEAP_MAX_ADDRESS (void*)0xCFFFFFFF
14+
#define HEAP_MIN_SIZE 4 * MB
15+
16+
#define PAGE_SIZE 4096
17+
#define OVERHEAD (sizeof(struct memory_block) + sizeof(uint32_t))
18+
19+
typedef struct memory_block
20+
{
21+
struct memory_block* next;
22+
struct memory_block* prev;
23+
uint32_t size;
24+
} memory_block_t;
25+
26+
extern bool Kheap_enabled;
27+
28+
/*
29+
* if heap is initialized then uss the dynamic memory allocator else use the placement memory allocator
30+
* @param size The size of the memory to be allocated
31+
* @param align Should the memory be aligned?
32+
* @param paddr Should the allocated memory be given a physical address?
33+
*/
34+
uint32_t kmalloc_int(uint32_t size, int align, uint32_t* paddr);
35+
36+
/*
37+
* continuous kmalloc which can be used before the heap is initialised
38+
* @param size The size of the memory to be allocated
39+
* @param align Should the memory be aligned?
40+
* @param paddr Should the allocated memory be given a physical address?
41+
*/
42+
void* kmalloc_c(uint32_t size, int align, uint32_t* paddr);
43+
44+
/*
45+
* Wrapper for kmalloc_int with align = 1
46+
* @param size The size of the memory to be allocated
47+
*/
48+
void* kmalloc_a(uint32_t size);
49+
50+
/*
51+
* Wrapper for kmalloc_int with align = 0 and paddr = paddr
52+
* @param size The size of the memory to be allocated
53+
* @param paddr Should the allocated memory be given a physical address?
54+
*/
55+
uint32_t kmalloc_p(uint32_t size, uint32_t* paddr);
56+
57+
/*
58+
* Wrapper for kmalloc_int with align = 1 and paddr = paddr
59+
* @param size The size of the memory to be allocated
60+
* @param paddr Should the allocated memory be given a physical address?
61+
*/
62+
uint32_t kmalloc_ap(uint32_t size, uint32_t* paddr);
63+
64+
/*
65+
* Wrapper for kmalloc_int with align = 0 and paddr = 0
66+
* @param size The size of the memory to be allocated
67+
*/
68+
void *kmalloc(uint32_t size);
69+
70+
/*
71+
* A function to free a block of memory previously allocated with kmalloc
72+
* @param ptr The pointer to the memory to be freed
73+
*/
74+
void kfree(void* ptr);
75+
76+
/*
77+
* A function to reallocate a block of memory previously allocated with kmalloc
78+
* @param ptr The pointer to the memory to be reallocated
79+
* @param size The size of the memory to be reallocated
80+
*/
81+
void* krealloc(void* ptr, uint32_t size);
82+
83+
void* kcalloc(uint32_t size, uint32_t num);
84+
85+
void print_db();
86+
87+
int doesItFit(memory_block_t* block, uint32_t size);
88+
89+
void setFree(uint32_t *size, int x);
90+
91+
void removeFromList(memory_block_t* block);
92+
void addToList(memory_block_t* block);
93+
94+
int isBetter(memory_block_t* block1, memory_block_t* block2);
95+
memory_block_t* bestFit(uint32_t size);
96+
memory_block_t* getPrevBlock(memory_block_t* block);
97+
memory_block_t* getNextBlock(memory_block_t* block);
98+
99+
uint32_t getRealSize(uint32_t size);
100+
uint32_t getSbrkSize(uint32_t size);
101+
102+
int isFree(memory_block_t* block);
103+
int isEnd(memory_block_t* block);
104+
105+
void init_kheap(void* start, void* end, void* max);
106+
107+
void* malloc(uint32_t size);
108+
void* realloc(void* ptr, uint32_t size);
109+
void* calloc(uint32_t size, uint32_t num);
110+
111+
void free(void* ptr);
112+
#endif

src/Include/list.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef __LIST_H__
2+
#define __LIST_H__
3+
4+
#include "system.h"
5+
6+
typedef struct listnode
7+
{
8+
void *val;
9+
struct listnode *next;
10+
struct listnode *prev;
11+
} listnode_t;
12+
13+
typedef struct list
14+
{
15+
listnode_t *head;
16+
listnode_t *tail;
17+
uint32_t size;
18+
} list_t;
19+
20+
#define foreach(t, list) for(listnode_t * t = list->head; t != NULL; t = t->next)
21+
22+
list_t *list_create();
23+
uint32_t list_size(list_t *list);
24+
25+
listnode_t* list_insert_front(list_t *list, void *val);
26+
void list_insert_back(list_t *list, void *val);
27+
28+
void * list_remove_node(list_t * list, listnode_t * node);
29+
void * list_remove_front(list_t * list);
30+
void * list_remove_back(list_t * list);
31+
32+
void list_push(list_t * list, void * val);
33+
listnode_t * list_pop(list_t * list);
34+
35+
void list_enqueue(list_t * list, void * val);
36+
listnode_t * list_dequeue(list_t * list);
37+
38+
void * list_peek_front(list_t * list);
39+
void * list_peek_back(list_t * list);
40+
41+
void list_destroy(list_t * list);
42+
void listnode_destroy(listnode_t * node);
43+
44+
int list_contain(list_t * list, void * val);
45+
46+
listnode_t * list_get_node_by_index(list_t * list, int index);
47+
void * list_remove_by_index(list_t * list, int index);
48+
49+
50+
#endif

src/Include/math.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef __MATH_H
2+
#define __MATH_H
3+
4+
#define abs(a) (((a) < 0)?-(a):(a))
5+
#define max(a,b) (((a) > (b)) ? (a) : (b))
6+
#define min(a,b) (((a) < (b)) ? (a) : (b))
7+
#define sign(x) ((x < 0) ? -1 :((x > 0) ? 1 : 0))
8+
9+
#endif

src/Include/paging.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#ifndef __PAGING_H_
2+
#define __PAGING_H_
3+
4+
#include "system.h"
5+
#include "isr.h"
6+
#include "pmm.h"
7+
#include "math.h"
8+
9+
#define PAGE_SIZE 4096
10+
11+
#define IS_ALIGN(addr) ((((uint32_t)(addr)) | 0xFFFFF000) == 0)
12+
#define PAGE_ALIGN(addr) ((((uint32_t)(addr)) & 0xFFFFF000) + 0x1000)
13+
14+
#define PAGEDIR_INDEX(vaddr) (((uint32_t)vaddr) >> 22)
15+
#define PAGETBL_INDEX(vaddr) ((((uint32_t)vaddr) >>12) & 0x3ff)
16+
#define PAGEFRAME_INDEX(vaddr) (((uint32_t)vaddr) & 0xfff)
17+
18+
#define SET_PGBIT(cr0) (cr0 = cr0 | 0x80000000)
19+
#define CLEAR_PSEBIT(cr4) (cr4 = cr4 & 0xffffffef)
20+
21+
typedef struct page_dir_entry
22+
{
23+
uint32_t present :1;
24+
uint32_t rw :1;
25+
uint32_t user :1;
26+
uint32_t write_through :1;
27+
uint32_t cache_disable :1;
28+
uint32_t accessed :1;
29+
uint32_t dirty :1;
30+
uint32_t page_size :1;
31+
uint32_t global :1;
32+
uint32_t avail :3;
33+
uint32_t frame :20;
34+
}page_dir_entry_t;
35+
36+
typedef struct page_table_entry
37+
{
38+
uint32_t present :1;
39+
uint32_t rw :1;
40+
uint32_t user :1;
41+
uint32_t reserved :2;
42+
uint32_t accessed :1;
43+
uint32_t dirty :1;
44+
uint32_t reserved2 :2;
45+
uint32_t avail :3;
46+
uint32_t frame :20;
47+
}page_table_entry_t;
48+
49+
typedef struct page_table
50+
{
51+
page_table_entry_t pages[1024];
52+
}page_table_t;
53+
54+
typedef struct page_directory
55+
{
56+
page_dir_entry_t tables[1024];
57+
page_table_t* ref_tables[1024];
58+
}page_directory_t;
59+
60+
extern int paging_enabled;
61+
62+
extern page_directory_t* TEMP_PAGE_DIR;
63+
64+
extern uint8_t * pmm_bitmap;
65+
extern uint32_t pmm_bitmap_size;
66+
67+
extern page_directory_t* kernel_page_dir;
68+
69+
void* virt2phys(page_directory_t* dir, void* vaddr);
70+
void* d_kmalloc(uint32_t size, int align);
71+
72+
void alloc_region(page_directory_t * dir, uint32_t start_vaddr, uint32_t end_vaddr, int iden_map, int is_kernel, int is_writable);
73+
void free_region(page_directory_t * dir, uint32_t start_vaddr, uint32_t end_vaddr, int free);
74+
75+
void alloc_page(page_directory_t * dir, uint32_t vaddr, uint32_t frame, int is_kernel, int is_writable);
76+
void free_page(page_directory_t * dir, uint32_t vaddr, int free);
77+
78+
void init_paging();
79+
80+
void switch_page_dir(page_directory_t* dir, uint32_t paddr);
81+
82+
void enable_paging();
83+
84+
void* ksbrk(uint32_t size);
85+
86+
void copy_page_dir(page_directory_t* dest, page_directory_t* src);
87+
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);
88+
89+
void page_fault_handler(registers_t regs);
90+
91+
#endif

0 commit comments

Comments
 (0)