Advertisement
lfed255-dev

Paste From Computer

Aug 1st, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.41 KB | None | 0 0
  1. // heap.c
  2. #include <depthos/paging.h>
  3. #include <depthos/pgm.h>
  4. #include "depthos/heap.h"
  5. #include <depthos/console.h>
  6.  
  7.  
  8.  
  9. extern uint32_t end;
  10.  
  11. //page_t *free_pages = NULL;
  12. //size_t total_page_count = 0;
  13.  
  14. extern page_t heap_mem_pgt[1024] __align(4096);
  15. extern page_t heap_blks_pgt[1024] __align(4096);
  16. extern page_t proc_dirs_pgt[1024] __align(4096); /* 769 */ // for processes
  17. extern page_t proc_tbs_pgt[1024] __align(4096); /* 769 */ // for processes
  18.  
  19. extern pagetb_t end_pgt __align(4096);
  20.  
  21. // pagetb_t _heap_mem_pgtb;
  22. // size_t _heap_mem_pgtb_cidx;
  23. // pagetb_t _heap_blks_pgtb;
  24. // size_t _heap_blks_pgtb_cidx;
  25.  
  26.  
  27. uint32_t kend = (uint32_t)&end;
  28. uint32_t glob_mem_start;
  29. uint32_t glob_mem_cur;
  30.  
  31. void* glob_get_mem(size_t count) {
  32. //printk("Alloc\n");
  33. uint32_t tmp = glob_mem_cur;
  34. glob_mem_cur += count;
  35. return (void*)tmp;
  36. }
  37.  
  38. heap_t kheap;
  39. heap_pgmem_alloc_state_t kheap_pgmem_astate;
  40. heap_blk_alloc_state_t kheap_blk_astate;
  41.  
  42. void __kheap_init() {
  43. glob_mem_start = kend;
  44. glob_mem_cur = glob_mem_start;
  45. kheap.pgmem_astate = &kheap_pgmem_astate;
  46. kheap.blk_astate = &kheap_blk_astate;
  47. kheap.root = __heap_allocmaj(kheap,kheap.blk_astate);
  48. if (kheap.root == NULL) {
  49. panic(__func__,"kheap.root is empty");
  50. }
  51. }
  52. static uint32_t cur_pgmaj_offset = 0x0;
  53. static uint32_t cur_pgmin_offset = 0x0;
  54.  
  55.  
  56.  
  57. dheap_major_blk_t __heap_allocmaj(dheap_t h,heap_alloc_state_t *s) {
  58. if (s->maj_cidx >= (1024/2) - 1) {
  59. return NULL;
  60. }
  61. if(s->maj_offset == 4096 - 1) { s->maj_offset = 0; s->maj_cidx++; }
  62. dheap_major_blk_t blk;
  63. blk = parse_page(&s->pgtb[s->maj_cidx]).frame + s->maj_offset;
  64.  
  65. s->maj_offset += sizeof(heap_major_blk_t);
  66.  
  67.  
  68. s->maj_offset = maj_offset;
  69.  
  70. return (dheap_major_blk_t)blk;
  71.  
  72. }
  73. dheap_minor_blk_t __heap_allocmin(dheap_t h, heap_alloc_state_t *s) {
  74. if (s->min_cidx >= (1024/2) - 1) {
  75. return NULL;
  76. }
  77. if(s->min_offset == 4096 - 1) { s->min_offset = 0; s->maj_cidx++; }
  78. dheap_main_blk_t blk;
  79. blk = parse_page(&s->pgtb[s->min_cidx]).frame + s->min_offset;
  80.  
  81. s->min_offset += sizeof(heap_major_blk_t);
  82.  
  83.  
  84. s->min_offset = min_offset;
  85.  
  86. return (dheap_minor_blk_t)blk;
  87.  
  88. }
  89.  
  90. void* __kheap_alloc(heap_t* heap,size_t sz) {
  91. dheap_major_blk_t root = heap->root;
  92. dheap_major_blk_t **curmaj;
  93. dheap_minor_blk_t **curmin;
  94. dheap
  95. if (root.firstblk == NULL) {
  96. root.firstblk = __heap_allocmin(heap,heap->blk_astate);
  97. if (root.firstblk == NULL) {
  98. panic(__func__,"error allocation the first memory block");
  99. }
  100. curmin = root.firstblk;
  101. }
  102. curmaj = &root;
  103. while(*curmaj != NULL) {
  104. curmin = &curmaj->firstblk;
  105. if(curmin == NULL) {
  106. (*curmaj)->firstblk = __heap_allocmin(heap,heap->blk_astate);
  107. }
  108. while(*curmin != NULL) {
  109. if(*curmin->busy) { goto nextmin; }
  110.  
  111. if(*curmin->)
  112.  
  113. nextmin:;
  114. curmin = &((*curmin)->next);
  115. }
  116. if(*curmin == NULL) {
  117. *curmin = __heap_allocmin(heap,heap->blk_astate);
  118. *curmin->ctrl_addr = heap->good_newaddr;
  119. heap->good_newaddr += sz;
  120. *curmin->sz = sz;
  121. }
  122.  
  123. nextmaj:;
  124. curmaj = &((*curmaj)->next);
  125. }
  126. }
  127.  
  128. // heap.h
  129. #pragma once
  130.  
  131. #include <depthos/stdtypes.h>
  132.  
  133. #define HEAP_MAGIC 0x7971D91F // 2037504287
  134.  
  135. struct __heap_major_blk;
  136.  
  137. typedef struct __heap_minor_blk {
  138. short int magic;
  139. short int sz;
  140. uint32_t ctrl_addr;
  141. bool busy;
  142.  
  143.  
  144. struct __heap_minor_blk *next;
  145. struct __heap_minor_blk *prev;
  146. struct __heap_major_blk *parent;
  147.  
  148. }heap_minor_blk_t,*dheap_minor_blk_t;
  149.  
  150. typedef struct __heap_major_blk {
  151. dheap_minor_blk_t firstblk;
  152.  
  153. uint16_t cnt_pg;
  154. uint16_t usage;
  155. uint16_t countblk;
  156. bool busy;
  157.  
  158. struct __heap_major_blk *next;
  159. struct __heap_major_blk *prev;
  160. }heap_major_blk_t,*dheap_major_blk_t;
  161.  
  162. typedef struct __heap_blk_alloc_state {
  163. pagetb_t pgtb;
  164. unsigned short int maj_offset;
  165. unsigned short int min_offset;
  166. unsigned short int min_cidx;
  167. unsigned short int maj_cidx;
  168. }heap_blk_alloc_state_t;
  169.  
  170. typedef struct __heap_pgmem_alloc_state {
  171. pagetb_t pgtb;
  172. unsigned short int cidx;
  173. unsigned short int coffset;
  174. }heap_pgmem_alloc_state_t;
  175.  
  176.  
  177.  
  178. typedef struct __heap {
  179.  
  180. // page's information
  181.  
  182. heap_pgmem_alloc_state_t *pgmem_astate;
  183.  
  184. // blocks information
  185.  
  186. heap_blk_alloc_state_t *blk_astate;
  187.  
  188. uint32_t good_newaddr;
  189.  
  190. dheap_major_blk_t *root;
  191.  
  192. }heap_t,*dheap_t;
  193.  
  194. // paging.c
  195.  
  196. #include <depthos/paging.h>
  197.  
  198. #include <depthos/idt.h>
  199.  
  200. #include <depthos/stdbits.h>
  201.  
  202. int paging_enabled = 0;
  203.  
  204. pde_t kernel_pgd[1024] __align(4096);
  205.  
  206. pde_t *cur_pgd __align(4096);
  207.  
  208. page_t kernel_pgt[1024] __align(4096); /* 768 */
  209. page_t heap_mem_pgt[1024] __align(4096);
  210. page_t heap_blks_pgt[1024] __align(4096);
  211. page_t proc_dirs_pgt[1024] __align(4096); /* 769 */ // for processes
  212. page_t proc_tbs_pgt[1024] __align(4096); /* 769 */ // for processes
  213. pagetb_t end_pgt __align(4096);
  214.  
  215. #define page_offset(a) (((uint32_t)a) & 0xfff)
  216. #define page_index(a) ((((uint32_t)a) >>12) & 0x3ff)
  217. #define table_index(a) (((uint32_t)a) >> 22)
  218. int pde_index(uint32_t addr){
  219. return addr >> 22;
  220. }
  221.  
  222. uintptr_t pte_index(uint32_t addr){
  223. return (uintptr_t) ((addr / 4096) % 1024);
  224. }
  225.  
  226. void activate_pgd(pagedir_t pgd){
  227. cur_pgd = pgd;
  228. __asm __volatile ("mov %0, %%cr3"::"r"((int)pgd - (int)VIRT_BASE));
  229. }
  230.  
  231. pagedir_t __save_pgd(void){
  232. uint32_t ret;
  233. __asm __volatile ("mov %%cr3, %0":"=r"(ret));
  234. return (pagedir_t) (ret + VIRT_BASE);
  235. }
  236.  
  237. pagedir_t activate_pgd_save(pagedir_t pgd){
  238. cur_pgd = pgd;
  239. pagedir_t ret = __save_pgd();
  240. __asm __volatile ("mov %0, %%cr3"::"r"((int)pgd - (int)VIRT_BASE));
  241. return ret;
  242. }
  243.  
  244.  
  245. void* get_paddr(pagedir_t dir,void *vaddr) {
  246. if (!paging_enabled)
  247. return (void*)(vaddr - VIRT_BASE);
  248. pde_t *pdes = dir, pde;
  249. page_t *ptes;
  250. page_t page;
  251. uint32_t addr;
  252. int pdi = pde_index((uint32_t)vaddr);
  253. int pti = pte_index((uint32_t)vaddr);
  254.  
  255. pde = pdes[pdi];
  256. pde >>= PDE_ADDR_SHIFT;
  257. pde <<= 12;
  258. if(pde == 0)
  259. return NULL;
  260. pde += VIRT_BASE;
  261. ptes = (page_t*)pde;
  262. page = ptes[pti];
  263.  
  264.  
  265. page >>= PTE_ADDR_SHIFT;
  266. page <<=12;
  267.  
  268. if (page == 0)
  269. return NULL;
  270. addr = page;
  271. addr += page_offset(vaddr);
  272.  
  273.  
  274. return (void*)addr;
  275. }
  276.  
  277. #undef page_offset
  278. #undef page_index
  279. #undef table_index
  280.  
  281.  
  282. void turn_page(page_t* p) {
  283. *p &= ((!(*p << PTE_PRESENT_SHIFT)) << PTE_PRESENT_SHIFT);
  284. }
  285.  
  286. page_t* get_page(pagedir_t pgd,uint32_t vaddr) {
  287. int ipde, ipte;
  288.  
  289. pde_t *pdes = pgd, pde;
  290.  
  291. page_t *ptes;
  292.  
  293. ipde = pde_index(vaddr);
  294. ipte = pte_index(vaddr);
  295.  
  296. pde = pdes[ipde];
  297. pde >>= PDE_ADDR_SHIFT;
  298. pde <<= 12;
  299. if (pde == 0)
  300. return NULL;
  301. pde += VIRT_BASE;
  302.  
  303. ptes = (page_t*)pde;
  304. return &ptes[ipte];
  305. }
  306. // 0000100 << 2
  307. // int i = 0000000;
  308. // i = 0000100;
  309. // i >>= 2;
  310.  
  311. page_t make_pte(uint32_t paddr,int user,int rw) {
  312. paddr = paddr >> 12;
  313.  
  314. return (0x0 |
  315. (1 << PTE_PRESENT_SHIFT) |
  316. (rw << PTE_RW_SHIFT) |
  317. (user << PTE_USER_SHIFT) |
  318. (1 << PTE_WRITETHRU_SHIFT) |
  319. (0 << PTE_CACHE_SHIFT) |
  320. (0 << PTE_ACCESS_SHIFT) |
  321. (0 << PTE_DIRTY_SHIFT) &
  322. (PTE_ZERO_MASK) |
  323. (0 << PTE_GLOB_SHIFT) &
  324. (PTE_AVAIL_MASK) |
  325. (paddr << PTE_ADDR_SHIFT));
  326.  
  327. }
  328.  
  329. pde_t make_pde(uint32_t paddr,int user,int rw) {
  330.  
  331. paddr = paddr >> 12;
  332.  
  333. return 0x0 |
  334. (1 << PDE_PRESENT_SHIFT) |
  335. (rw << PDE_RW_SHIFT) |
  336. (user << PDE_USER_SHIFT) |
  337. (1 << PDE_WRITETHRU_SHIFT) |
  338. (0 << PDE_CACHE_SHIFT) |
  339. (0 << PDE_ACCESS_SHIFT) |
  340. (0 << PDE_ZERO_SHIFT) |
  341. (0 << PDE_SIZE_SHIFT) |
  342. (0 << PDE_IGNORE_SHIFT) &
  343. (PDE_AVAIL_MASK) |
  344. (paddr << PDE_ADDR_SHIFT);
  345. }
  346.  
  347. void enable_paging() {
  348. #define SET_PGBIT(cr0) (cr0 = cr0 | 0x80000000)
  349. #define CLEAR_PSEBIT(cr4) (cr4 = cr4 & 0xffffffef)
  350.  
  351. uint32_t cr0, cr4;
  352.  
  353. __asm __volatile ( "mov %%cr4, %0" : "=r"(cr4) :);
  354. CLEAR_PSEBIT(cr4);
  355. __asm __volatile ( "mov %0, %%cr4" : :"r"(cr4) :);
  356. __asm __volatile ( "mov %%cr0, %0" :"=r"(cr0) ::);
  357. SET_PGBIT(cr0);
  358. __asm __volatile ( "mov %0,%%cr0" ::"r"(cr0) :);
  359.  
  360.  
  361. paging_enabled = 1;
  362.  
  363. #undef SET_PGBIT
  364. #undef CLEAR_PSEBIT
  365. }
  366.  
  367. #define getPfState(r) \
  368. int present = r.err_code & 0x1; \
  369. int rw = r.err_code & 0x2; \
  370. int us = r.err_code & 0x4; \
  371. int reserved = r.err_code & 0x8; \
  372. int id = r.err_code & 0x10; \
  373.  
  374.  
  375. void __noreturn __do_fatal_pf(regs_t r) {
  376. while(1);
  377. }
  378.  
  379. void __do_soft_pf(regs_t r) {
  380. __do_fatal_pf(r);
  381. return;
  382. }
  383.  
  384. void __do_pf(regs_t r)
  385. {
  386. uint32_t cr2;
  387. __asm __volatile ("mov %%cr2, %0":"=r"(cr2));
  388. printk("page fault: [0x%x] ",cr2);
  389.  
  390. #if 1
  391. getPfState(r);
  392. printk("( .pres = %d, .rw = %d, .supervisor = %d, .reserved = %d, .id = %d )",present,rw,us,reserved,id);
  393. #endif
  394.  
  395. if(present == 0) {
  396. __do_soft_pf(r);
  397. }
  398. else {
  399. __do_fatal_pf(r);
  400. }
  401. }
  402.  
  403.  
  404. #undef getPfState
  405.  
  406. void paging_init() {
  407. size_t i,j;
  408.  
  409. __asm __volatile ("cli");
  410.  
  411. for(i = 0 * 1024 * 1024; i < 1 * 4 * 1024 * 1024; i += 4096){
  412. page_t pg = make_pte(i,0,1);
  413. //mod_log(__func__,"alloc pages ( kernel ) - [%d](%d)",pte_index(i),i);
  414. kernel_pgt[pte_index(i)] = pg;
  415.  
  416. // turn_page(&pg);
  417. }
  418. for(; i < 3 * 4 * 1024 * 1024; i += 4096){
  419. page_t pg = make_pte(i,0,1);
  420. //mod_log(__func__,"alloc pages ( heap blks ) - [%d](%d)",pte_index(i),i);
  421. heap_blks_pgt[pte_index(i)] = pg;
  422.  
  423. // turn_page(&pg);
  424. }
  425. for(; i < 2 * 4 * 1024 * 1024; i += 4096){
  426. page_t pg = make_pte(i,0,1);
  427. //mod_log(__func__,"alloc pages ( heap mem ) - [%d](%d)",pte_index(i),i);
  428. heap_mem_pgt[pte_index(i)] = pg;
  429.  
  430. // turn_page(&pg);
  431. }
  432.  
  433.  
  434.  
  435. //change_page(&kernel_pgt[0],0,0,1);
  436.  
  437. // printk("%x || %x - %x = %x\n", (int)kernel_pgt,(int)kernel_pgt,(int)VIRT_BASE,(uint32_t)((int)kernel_pgt - (int)VIRT_BASE));
  438. kernel_pgd[pde_index(VIRT_BASE + 0 * 4 * 1024 * 1024)] = make_pde((uint32_t)(((int)kernel_pgt - (int)VIRT_BASE)),0,1);
  439. kernel_pgd[pde_index(VIRT_BASE + 1 * 4 * 1024 * 1024)] = make_pde((uint32_t)(((int)heap_blks_pgt - (int)VIRT_BASE)),0,1);
  440. kernel_pgd[pde_index(VIRT_BASE + 2 * 4 * 1024 * 1024)] = make_pde((uint32_t)(((int)heap_mem_pgt - (int)VIRT_BASE)),0,1);
  441. // change_page
  442. end_pgt = heap_blks_pgt;
  443. activate_pgd(kernel_pgd);
  444.  
  445. reg_intr(14,__do_pf);
  446.  
  447. enable_paging();
  448.  
  449. print_mod("vmem initialized",MOD_OK);
  450.  
  451. __asm __volatile ("sti");
  452. }
  453.  
  454.  
  455.  
  456. pageinfo_t parse_page(page_t* pg) {
  457. pageinfo_t pgi;
  458.  
  459. pgi.pg = pg;
  460.  
  461. // printk("0x%x,0x%x\n",pgi.pg,pg);
  462.  
  463.  
  464.  
  465. //
  466. // >> 6 = 101000000
  467.  
  468. pgi.pres = getbit(*pg,PTE_PRESENT_SHIFT); // & 0x1;
  469. pgi.rw = getbit(*pg,PTE_RW_SHIFT); //& 0x2;
  470. pgi.us = getbit(*pg,PTE_USER_SHIFT); //& 0x4;
  471. pgi.pwt = getbit(*pg,PTE_WRITETHRU_SHIFT); //& 0x8;
  472. pgi.pcd = getbit(*pg,PTE_CACHE_SHIFT); //& 0x10;
  473. pgi.accessed = getbit(*pg,PTE_ACCESS_SHIFT); //& 0x20;
  474. pgi.dirty = getbit(*pg,PTE_DIRTY_SHIFT);//& 0x40;
  475. pgi.pat = getbit(*pg,PTE_ZERO_SHIFT); //& 80;
  476. pgi.glob = getbit(*pg,PTE_GLOB_SHIFT); //& 0x100;
  477. pgi.frame = *pg & 0xFFFFF000;
  478.  
  479. return pgi;
  480. }
  481.  
  482. void change_page(page_t* pg,int pres,int rw,int us) {
  483. // printk("vals - %d,%d,%d\n",pgi.pres,pgi.rw,pgi.us);
  484. // printk("old - %d - %d - %d\n",getbit(*pgi.pg,PTE_PRESENT_SHIFT),getbit(*pgi.pg,PTE_RW_SHIFT),getbit(*pgi.pg,PTE_USER_SHIFT));
  485. setbit(pg,PTE_PRESENT_SHIFT,pres);
  486. setbit(pg,PTE_RW_SHIFT,rw);
  487. setbit(pg,PTE_USER_SHIFT,us);
  488.  
  489.  
  490. // *pgi.pg = (pgi.pres << PTE_PRESENT_SHIFT) | (*pgi.pg & (~(1 << PTE_PRESENT_SHIFT)));
  491. // *pgi.pg = (pgi.rw << PTE_RW_SHIFT) | (*pgi.pg & (~(1 << PTE_RW_SHIFT)));
  492. // *pgi.pg = (pgi.us << PTE_USER_SHIFT) | (*pgi.pg & (~(1 << PTE_USER_SHIFT)));
  493.  
  494. // printk("new - %d - %d - %d\n",getbit(*pgi.pg,PTE_PRESENT_SHIFT),getbit(*pgi.pg,PTE_RW_SHIFT),getbit(*pgi.pg,PTE_USER_SHIFT));
  495. // printk("%d",pg);
  496. }
  497.  
  498. // console.c
  499.  
  500. #pragma once
  501.  
  502. #include <depthos/stdarg.h>
  503. #include <depthos/stdio.h>
  504. #include <depthos/stdtypes.h>
  505. #include <depthos/ports.h>
  506.  
  507.  
  508. #define BLACK_COLOR 0
  509. #define BLUE_COLOR 1
  510. #define GREEN_COLOR 2
  511. #define WWBLUE_COLOR 3
  512. #define RED_COLOR 4
  513. #define PURPLE_COLOR 5
  514. #define BROWN_COLOR 6
  515. #define WGRAY_COLOR 7
  516. #define BGRAY_COLOR 8
  517. #define WBLUE_COLOR 9
  518. #define WGREEN_COLOR 10
  519. #define WWWBLUE_COLOR 11
  520. #define PINK_COLOR 12
  521. #define WPURPLE_COLOR 13
  522. #define WBROWN_COLOR 14
  523. #define WHITE_COLOR 15
  524.  
  525. enum {
  526. MOD_OK,
  527. MOD_ERR
  528. };
  529.  
  530.  
  531.  
  532. void console_init(int s,int l,int b,int f);
  533. void console_movec(int x,int y);
  534. void console_flushc();
  535.  
  536. void console_clear();
  537. void console_flushs();
  538.  
  539. void console_putchara(unsigned char c,uint8_t a);
  540. void console_putchar(unsigned char c);
  541.  
  542.  
  543. void console_write(const char* buf);
  544. void console_writea(const char* buf,uint8_t a);
  545. void console_write_hex(uint32_t v);
  546. void console_write_dec(uint32_t v);
  547. void console_write_int(uint32_t v, unsigned base);
  548. void console_read();
  549.  
  550. void console_write_color(const char* buf,int8_t b,int8_t f);
  551. void console_putchar_color(unsigned char c,int8_t b,int8_t f);
  552.  
  553. void print_mod(char* buf,int m);
  554. //char *mlog_s;
  555.  
  556.  
  557. void mod_loga(char* file, int line, char *mod,char *msg,...);
  558. #define mod_log(mod,msg,...) mod_loga(__FILE__,__LINE__,mod,msg, ##__VA_ARGS__);
  559. #define panic(mod,msg,...) \
  560. mod_log(mod,msg,...) \
  561. while(1) {}
  562.  
  563. void register_console(void (*output)(void *context, const char *data, size_t sz),
  564. void *context);
  565.  
  566. void printk(const char *fmt, ...);
  567. void vprintk(const char *fmt, va_list ap);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement