Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // heap.c
- #include <depthos/paging.h>
- #include <depthos/pgm.h>
- #include "depthos/heap.h"
- #include <depthos/console.h>
- extern uint32_t end;
- //page_t *free_pages = NULL;
- //size_t total_page_count = 0;
- extern page_t heap_mem_pgt[1024] __align(4096);
- extern page_t heap_blks_pgt[1024] __align(4096);
- extern page_t proc_dirs_pgt[1024] __align(4096); /* 769 */ // for processes
- extern page_t proc_tbs_pgt[1024] __align(4096); /* 769 */ // for processes
- extern pagetb_t end_pgt __align(4096);
- // pagetb_t _heap_mem_pgtb;
- // size_t _heap_mem_pgtb_cidx;
- // pagetb_t _heap_blks_pgtb;
- // size_t _heap_blks_pgtb_cidx;
- uint32_t kend = (uint32_t)&end;
- uint32_t glob_mem_start;
- uint32_t glob_mem_cur;
- void* glob_get_mem(size_t count) {
- //printk("Alloc\n");
- uint32_t tmp = glob_mem_cur;
- glob_mem_cur += count;
- return (void*)tmp;
- }
- heap_t kheap;
- heap_pgmem_alloc_state_t kheap_pgmem_astate;
- heap_blk_alloc_state_t kheap_blk_astate;
- void __kheap_init() {
- glob_mem_start = kend;
- glob_mem_cur = glob_mem_start;
- kheap.pgmem_astate = &kheap_pgmem_astate;
- kheap.blk_astate = &kheap_blk_astate;
- kheap.root = __heap_allocmaj(kheap,kheap.blk_astate);
- if (kheap.root == NULL) {
- panic(__func__,"kheap.root is empty");
- }
- }
- static uint32_t cur_pgmaj_offset = 0x0;
- static uint32_t cur_pgmin_offset = 0x0;
- dheap_major_blk_t __heap_allocmaj(dheap_t h,heap_alloc_state_t *s) {
- if (s->maj_cidx >= (1024/2) - 1) {
- return NULL;
- }
- if(s->maj_offset == 4096 - 1) { s->maj_offset = 0; s->maj_cidx++; }
- dheap_major_blk_t blk;
- blk = parse_page(&s->pgtb[s->maj_cidx]).frame + s->maj_offset;
- s->maj_offset += sizeof(heap_major_blk_t);
- s->maj_offset = maj_offset;
- return (dheap_major_blk_t)blk;
- }
- dheap_minor_blk_t __heap_allocmin(dheap_t h, heap_alloc_state_t *s) {
- if (s->min_cidx >= (1024/2) - 1) {
- return NULL;
- }
- if(s->min_offset == 4096 - 1) { s->min_offset = 0; s->maj_cidx++; }
- dheap_main_blk_t blk;
- blk = parse_page(&s->pgtb[s->min_cidx]).frame + s->min_offset;
- s->min_offset += sizeof(heap_major_blk_t);
- s->min_offset = min_offset;
- return (dheap_minor_blk_t)blk;
- }
- void* __kheap_alloc(heap_t* heap,size_t sz) {
- dheap_major_blk_t root = heap->root;
- dheap_major_blk_t **curmaj;
- dheap_minor_blk_t **curmin;
- dheap
- if (root.firstblk == NULL) {
- root.firstblk = __heap_allocmin(heap,heap->blk_astate);
- if (root.firstblk == NULL) {
- panic(__func__,"error allocation the first memory block");
- }
- curmin = root.firstblk;
- }
- curmaj = &root;
- while(*curmaj != NULL) {
- curmin = &curmaj->firstblk;
- if(curmin == NULL) {
- (*curmaj)->firstblk = __heap_allocmin(heap,heap->blk_astate);
- }
- while(*curmin != NULL) {
- if(*curmin->busy) { goto nextmin; }
- if(*curmin->)
- nextmin:;
- curmin = &((*curmin)->next);
- }
- if(*curmin == NULL) {
- *curmin = __heap_allocmin(heap,heap->blk_astate);
- *curmin->ctrl_addr = heap->good_newaddr;
- heap->good_newaddr += sz;
- *curmin->sz = sz;
- }
- nextmaj:;
- curmaj = &((*curmaj)->next);
- }
- }
- // heap.h
- #pragma once
- #include <depthos/stdtypes.h>
- #define HEAP_MAGIC 0x7971D91F // 2037504287
- struct __heap_major_blk;
- typedef struct __heap_minor_blk {
- short int magic;
- short int sz;
- uint32_t ctrl_addr;
- bool busy;
- struct __heap_minor_blk *next;
- struct __heap_minor_blk *prev;
- struct __heap_major_blk *parent;
- }heap_minor_blk_t,*dheap_minor_blk_t;
- typedef struct __heap_major_blk {
- dheap_minor_blk_t firstblk;
- uint16_t cnt_pg;
- uint16_t usage;
- uint16_t countblk;
- bool busy;
- struct __heap_major_blk *next;
- struct __heap_major_blk *prev;
- }heap_major_blk_t,*dheap_major_blk_t;
- typedef struct __heap_blk_alloc_state {
- pagetb_t pgtb;
- unsigned short int maj_offset;
- unsigned short int min_offset;
- unsigned short int min_cidx;
- unsigned short int maj_cidx;
- }heap_blk_alloc_state_t;
- typedef struct __heap_pgmem_alloc_state {
- pagetb_t pgtb;
- unsigned short int cidx;
- unsigned short int coffset;
- }heap_pgmem_alloc_state_t;
- typedef struct __heap {
- // page's information
- heap_pgmem_alloc_state_t *pgmem_astate;
- // blocks information
- heap_blk_alloc_state_t *blk_astate;
- uint32_t good_newaddr;
- dheap_major_blk_t *root;
- }heap_t,*dheap_t;
- // paging.c
- #include <depthos/paging.h>
- #include <depthos/idt.h>
- #include <depthos/stdbits.h>
- int paging_enabled = 0;
- pde_t kernel_pgd[1024] __align(4096);
- pde_t *cur_pgd __align(4096);
- page_t kernel_pgt[1024] __align(4096); /* 768 */
- page_t heap_mem_pgt[1024] __align(4096);
- page_t heap_blks_pgt[1024] __align(4096);
- page_t proc_dirs_pgt[1024] __align(4096); /* 769 */ // for processes
- page_t proc_tbs_pgt[1024] __align(4096); /* 769 */ // for processes
- pagetb_t end_pgt __align(4096);
- #define page_offset(a) (((uint32_t)a) & 0xfff)
- #define page_index(a) ((((uint32_t)a) >>12) & 0x3ff)
- #define table_index(a) (((uint32_t)a) >> 22)
- int pde_index(uint32_t addr){
- return addr >> 22;
- }
- uintptr_t pte_index(uint32_t addr){
- return (uintptr_t) ((addr / 4096) % 1024);
- }
- void activate_pgd(pagedir_t pgd){
- cur_pgd = pgd;
- __asm __volatile ("mov %0, %%cr3"::"r"((int)pgd - (int)VIRT_BASE));
- }
- pagedir_t __save_pgd(void){
- uint32_t ret;
- __asm __volatile ("mov %%cr3, %0":"=r"(ret));
- return (pagedir_t) (ret + VIRT_BASE);
- }
- pagedir_t activate_pgd_save(pagedir_t pgd){
- cur_pgd = pgd;
- pagedir_t ret = __save_pgd();
- __asm __volatile ("mov %0, %%cr3"::"r"((int)pgd - (int)VIRT_BASE));
- return ret;
- }
- void* get_paddr(pagedir_t dir,void *vaddr) {
- if (!paging_enabled)
- return (void*)(vaddr - VIRT_BASE);
- pde_t *pdes = dir, pde;
- page_t *ptes;
- page_t page;
- uint32_t addr;
- int pdi = pde_index((uint32_t)vaddr);
- int pti = pte_index((uint32_t)vaddr);
- pde = pdes[pdi];
- pde >>= PDE_ADDR_SHIFT;
- pde <<= 12;
- if(pde == 0)
- return NULL;
- pde += VIRT_BASE;
- ptes = (page_t*)pde;
- page = ptes[pti];
- page >>= PTE_ADDR_SHIFT;
- page <<=12;
- if (page == 0)
- return NULL;
- addr = page;
- addr += page_offset(vaddr);
- return (void*)addr;
- }
- #undef page_offset
- #undef page_index
- #undef table_index
- void turn_page(page_t* p) {
- *p &= ((!(*p << PTE_PRESENT_SHIFT)) << PTE_PRESENT_SHIFT);
- }
- page_t* get_page(pagedir_t pgd,uint32_t vaddr) {
- int ipde, ipte;
- pde_t *pdes = pgd, pde;
- page_t *ptes;
- ipde = pde_index(vaddr);
- ipte = pte_index(vaddr);
- pde = pdes[ipde];
- pde >>= PDE_ADDR_SHIFT;
- pde <<= 12;
- if (pde == 0)
- return NULL;
- pde += VIRT_BASE;
- ptes = (page_t*)pde;
- return &ptes[ipte];
- }
- // 0000100 << 2
- // int i = 0000000;
- // i = 0000100;
- // i >>= 2;
- page_t make_pte(uint32_t paddr,int user,int rw) {
- paddr = paddr >> 12;
- return (0x0 |
- (1 << PTE_PRESENT_SHIFT) |
- (rw << PTE_RW_SHIFT) |
- (user << PTE_USER_SHIFT) |
- (1 << PTE_WRITETHRU_SHIFT) |
- (0 << PTE_CACHE_SHIFT) |
- (0 << PTE_ACCESS_SHIFT) |
- (0 << PTE_DIRTY_SHIFT) &
- (PTE_ZERO_MASK) |
- (0 << PTE_GLOB_SHIFT) &
- (PTE_AVAIL_MASK) |
- (paddr << PTE_ADDR_SHIFT));
- }
- pde_t make_pde(uint32_t paddr,int user,int rw) {
- paddr = paddr >> 12;
- return 0x0 |
- (1 << PDE_PRESENT_SHIFT) |
- (rw << PDE_RW_SHIFT) |
- (user << PDE_USER_SHIFT) |
- (1 << PDE_WRITETHRU_SHIFT) |
- (0 << PDE_CACHE_SHIFT) |
- (0 << PDE_ACCESS_SHIFT) |
- (0 << PDE_ZERO_SHIFT) |
- (0 << PDE_SIZE_SHIFT) |
- (0 << PDE_IGNORE_SHIFT) &
- (PDE_AVAIL_MASK) |
- (paddr << PDE_ADDR_SHIFT);
- }
- void enable_paging() {
- #define SET_PGBIT(cr0) (cr0 = cr0 | 0x80000000)
- #define CLEAR_PSEBIT(cr4) (cr4 = cr4 & 0xffffffef)
- uint32_t cr0, cr4;
- __asm __volatile ( "mov %%cr4, %0" : "=r"(cr4) :);
- CLEAR_PSEBIT(cr4);
- __asm __volatile ( "mov %0, %%cr4" : :"r"(cr4) :);
- __asm __volatile ( "mov %%cr0, %0" :"=r"(cr0) ::);
- SET_PGBIT(cr0);
- __asm __volatile ( "mov %0,%%cr0" ::"r"(cr0) :);
- paging_enabled = 1;
- #undef SET_PGBIT
- #undef CLEAR_PSEBIT
- }
- #define getPfState(r) \
- int present = r.err_code & 0x1; \
- int rw = r.err_code & 0x2; \
- int us = r.err_code & 0x4; \
- int reserved = r.err_code & 0x8; \
- int id = r.err_code & 0x10; \
- void __noreturn __do_fatal_pf(regs_t r) {
- while(1);
- }
- void __do_soft_pf(regs_t r) {
- __do_fatal_pf(r);
- return;
- }
- void __do_pf(regs_t r)
- {
- uint32_t cr2;
- __asm __volatile ("mov %%cr2, %0":"=r"(cr2));
- printk("page fault: [0x%x] ",cr2);
- #if 1
- getPfState(r);
- printk("( .pres = %d, .rw = %d, .supervisor = %d, .reserved = %d, .id = %d )",present,rw,us,reserved,id);
- #endif
- if(present == 0) {
- __do_soft_pf(r);
- }
- else {
- __do_fatal_pf(r);
- }
- }
- #undef getPfState
- void paging_init() {
- size_t i,j;
- __asm __volatile ("cli");
- for(i = 0 * 1024 * 1024; i < 1 * 4 * 1024 * 1024; i += 4096){
- page_t pg = make_pte(i,0,1);
- //mod_log(__func__,"alloc pages ( kernel ) - [%d](%d)",pte_index(i),i);
- kernel_pgt[pte_index(i)] = pg;
- // turn_page(&pg);
- }
- for(; i < 3 * 4 * 1024 * 1024; i += 4096){
- page_t pg = make_pte(i,0,1);
- //mod_log(__func__,"alloc pages ( heap blks ) - [%d](%d)",pte_index(i),i);
- heap_blks_pgt[pte_index(i)] = pg;
- // turn_page(&pg);
- }
- for(; i < 2 * 4 * 1024 * 1024; i += 4096){
- page_t pg = make_pte(i,0,1);
- //mod_log(__func__,"alloc pages ( heap mem ) - [%d](%d)",pte_index(i),i);
- heap_mem_pgt[pte_index(i)] = pg;
- // turn_page(&pg);
- }
- //change_page(&kernel_pgt[0],0,0,1);
- // printk("%x || %x - %x = %x\n", (int)kernel_pgt,(int)kernel_pgt,(int)VIRT_BASE,(uint32_t)((int)kernel_pgt - (int)VIRT_BASE));
- kernel_pgd[pde_index(VIRT_BASE + 0 * 4 * 1024 * 1024)] = make_pde((uint32_t)(((int)kernel_pgt - (int)VIRT_BASE)),0,1);
- kernel_pgd[pde_index(VIRT_BASE + 1 * 4 * 1024 * 1024)] = make_pde((uint32_t)(((int)heap_blks_pgt - (int)VIRT_BASE)),0,1);
- kernel_pgd[pde_index(VIRT_BASE + 2 * 4 * 1024 * 1024)] = make_pde((uint32_t)(((int)heap_mem_pgt - (int)VIRT_BASE)),0,1);
- // change_page
- end_pgt = heap_blks_pgt;
- activate_pgd(kernel_pgd);
- reg_intr(14,__do_pf);
- enable_paging();
- print_mod("vmem initialized",MOD_OK);
- __asm __volatile ("sti");
- }
- pageinfo_t parse_page(page_t* pg) {
- pageinfo_t pgi;
- pgi.pg = pg;
- // printk("0x%x,0x%x\n",pgi.pg,pg);
- //
- // >> 6 = 101000000
- pgi.pres = getbit(*pg,PTE_PRESENT_SHIFT); // & 0x1;
- pgi.rw = getbit(*pg,PTE_RW_SHIFT); //& 0x2;
- pgi.us = getbit(*pg,PTE_USER_SHIFT); //& 0x4;
- pgi.pwt = getbit(*pg,PTE_WRITETHRU_SHIFT); //& 0x8;
- pgi.pcd = getbit(*pg,PTE_CACHE_SHIFT); //& 0x10;
- pgi.accessed = getbit(*pg,PTE_ACCESS_SHIFT); //& 0x20;
- pgi.dirty = getbit(*pg,PTE_DIRTY_SHIFT);//& 0x40;
- pgi.pat = getbit(*pg,PTE_ZERO_SHIFT); //& 80;
- pgi.glob = getbit(*pg,PTE_GLOB_SHIFT); //& 0x100;
- pgi.frame = *pg & 0xFFFFF000;
- return pgi;
- }
- void change_page(page_t* pg,int pres,int rw,int us) {
- // printk("vals - %d,%d,%d\n",pgi.pres,pgi.rw,pgi.us);
- // printk("old - %d - %d - %d\n",getbit(*pgi.pg,PTE_PRESENT_SHIFT),getbit(*pgi.pg,PTE_RW_SHIFT),getbit(*pgi.pg,PTE_USER_SHIFT));
- setbit(pg,PTE_PRESENT_SHIFT,pres);
- setbit(pg,PTE_RW_SHIFT,rw);
- setbit(pg,PTE_USER_SHIFT,us);
- // *pgi.pg = (pgi.pres << PTE_PRESENT_SHIFT) | (*pgi.pg & (~(1 << PTE_PRESENT_SHIFT)));
- // *pgi.pg = (pgi.rw << PTE_RW_SHIFT) | (*pgi.pg & (~(1 << PTE_RW_SHIFT)));
- // *pgi.pg = (pgi.us << PTE_USER_SHIFT) | (*pgi.pg & (~(1 << PTE_USER_SHIFT)));
- // printk("new - %d - %d - %d\n",getbit(*pgi.pg,PTE_PRESENT_SHIFT),getbit(*pgi.pg,PTE_RW_SHIFT),getbit(*pgi.pg,PTE_USER_SHIFT));
- // printk("%d",pg);
- }
- // console.c
- #pragma once
- #include <depthos/stdarg.h>
- #include <depthos/stdio.h>
- #include <depthos/stdtypes.h>
- #include <depthos/ports.h>
- #define BLACK_COLOR 0
- #define BLUE_COLOR 1
- #define GREEN_COLOR 2
- #define WWBLUE_COLOR 3
- #define RED_COLOR 4
- #define PURPLE_COLOR 5
- #define BROWN_COLOR 6
- #define WGRAY_COLOR 7
- #define BGRAY_COLOR 8
- #define WBLUE_COLOR 9
- #define WGREEN_COLOR 10
- #define WWWBLUE_COLOR 11
- #define PINK_COLOR 12
- #define WPURPLE_COLOR 13
- #define WBROWN_COLOR 14
- #define WHITE_COLOR 15
- enum {
- MOD_OK,
- MOD_ERR
- };
- void console_init(int s,int l,int b,int f);
- void console_movec(int x,int y);
- void console_flushc();
- void console_clear();
- void console_flushs();
- void console_putchara(unsigned char c,uint8_t a);
- void console_putchar(unsigned char c);
- void console_write(const char* buf);
- void console_writea(const char* buf,uint8_t a);
- void console_write_hex(uint32_t v);
- void console_write_dec(uint32_t v);
- void console_write_int(uint32_t v, unsigned base);
- void console_read();
- void console_write_color(const char* buf,int8_t b,int8_t f);
- void console_putchar_color(unsigned char c,int8_t b,int8_t f);
- void print_mod(char* buf,int m);
- //char *mlog_s;
- void mod_loga(char* file, int line, char *mod,char *msg,...);
- #define mod_log(mod,msg,...) mod_loga(__FILE__,__LINE__,mod,msg, ##__VA_ARGS__);
- #define panic(mod,msg,...) \
- mod_log(mod,msg,...) \
- while(1) {}
- void register_console(void (*output)(void *context, const char *data, size_t sz),
- void *context);
- void printk(const char *fmt, ...);
- void vprintk(const char *fmt, va_list ap);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement