void start(uint32_t* modulep, void* physbase, void* physfree) { int i; struct smap_t { uint64_t base, length; uint32_t type; }__attribute__((packed)) *smap; // initially Mark all pages used mm_set(freePage,MEM_SIZE); while(modulep[0] != 0x9001) modulep += modulep[1]+2; for(smap = (struct smap_t*)(modulep+2); smap < (struct smap_t*)((char*)modulep+modulep[1]+2*4); ++smap) { if (smap->type == 1 /* memory */ && smap->length != 0) { // Mark the memory available in the free list pmmngr_init_region(smap->base, smap->length); } } /* we need to mark the kernel memory as used kernel starts from 200000 and end at 220000 */ /* so we will mark the memory till 400000 as used however we can keep it till 220000 as well */ pmmngr_uninit_region((uint64_t)0,(uint64_t)0x400000); // mark all kernel memory as used /* we need to map the kernel memory to the page table */ // initialize the page table set_page_table(&Page_Table); uint64_t *addr = (uint64_t*)physbase; /* we need to map the kernel memory to the page table */ for(addr = (uint64_t*)physbase; addr<=(uint64_t*)physfree; addr++) { vmmngr_map_page(addr,(uint64_t *)((uint64_t)addr + KERNEL_VIRTUAL_BASE),&Page_Table); } /* Map the Video memory This will map to single page 80*25*2 < 1 page */ vmmngr_map_page((uint64_t *)0xb8000,(uint64_t*)VIDEO_VIRTUAL_MEMORY,&Page_Table); // printf("Video Memory has been marked in virtual address space\n"); /* pass the pml4e value to cr3 register */ kernel_pml4e = (uint64_t *)ALIGN_DOWN((uint64_t)Page_Table.root); write_cr3(&Page_Table); init_video(); /********************************* KERNEL CREATION ********************************/ struct task_struct *pcb0 = (struct task_struct *)kmalloc(sizeof(struct task_struct)); //kernel pcb0->pml4e =(uint64_t)kernel_pml4e; // kernel's page table pcb0->pid = 0; // I'm kernel init process so pid 0 pcb0->iswait = 0; //not waiting for any one pcb0->stack =(uint64_t *)stack; //my stack is already created by prof :) process_count = 0; //at this point we don't have any other process in ready_queue so go ahead and create one and update this sleeping_process_count = 0; // at this point no body is sleeping // initialize processes queue for(i=0;i<100;i++) { zombie_queue[i] = 0; // no process is zombie } foreground_process = 3; // process with this pid will be foreground process // put them in the ready queue ready_queue[0] =(uint64_t ) pcb0; //kernel /* char fname[] = "bin/hello"; malloc_pcb(fname); char fname1[] = "bin/world"; malloc_pcb(fname1); char fname2[] = "bin/proc3"; malloc_pcb(fname2); char fname3[] = "bin/proc4"; malloc_pcb(fname3); */ /*************************************** Please change here fname ******************/ char fname4[] = "bin/printf"; malloc_pcb(fname4); idt_install(); __asm__("sti"); //init_context_switch(); asm volatile("mov $0x2b,%ax"); asm volatile("ltr %ax"); tarfs_init(); while(1); }
int _cdecl kmain (multiboot_info* bootinfo) { //! get kernel size passed from boot loader uint32_t kernelSize=0; _asm mov word ptr [kernelSize], dx //! make demo look nice :) DebugClrScr (0x13); DebugGotoXY (0,0); DebugSetColor (0x3F); DebugPrintf (" ~[ Physical Memory Manager Demo ]~ "); DebugGotoXY (0,24); DebugSetColor (0x3F); DebugPrintf (" "); DebugGotoXY (0,2); DebugSetColor (0x17); //! initialize hal hal_initialize (); //! enable interrupts and install exception handlers enable (); setvect (0,(void (__cdecl &)(void))divide_by_zero_fault); setvect (1,(void (__cdecl &)(void))single_step_trap); setvect (2,(void (__cdecl &)(void))nmi_trap); setvect (3,(void (__cdecl &)(void))breakpoint_trap); setvect (4,(void (__cdecl &)(void))overflow_trap); setvect (5,(void (__cdecl &)(void))bounds_check_fault); setvect (6,(void (__cdecl &)(void))invalid_opcode_fault); setvect (7,(void (__cdecl &)(void))no_device_fault); setvect (8,(void (__cdecl &)(void))double_fault_abort); setvect (10,(void (__cdecl &)(void))invalid_tss_fault); setvect (11,(void (__cdecl &)(void))no_segment_fault); setvect (12,(void (__cdecl &)(void))stack_fault); setvect (13,(void (__cdecl &)(void))general_protection_fault); setvect (14,(void (__cdecl &)(void))page_fault); setvect (16,(void (__cdecl &)(void))fpu_fault); setvect (17,(void (__cdecl &)(void))alignment_check_fault); setvect (18,(void (__cdecl &)(void))machine_check_abort); setvect (19,(void (__cdecl &)(void))simd_fpu_fault); //! get memory size in KB uint32_t memSize = 1024 + bootinfo->m_memoryLo + bootinfo->m_memoryHi*64; //! initialize the physical memory manager //! we place the memory bit map used by the PMM at the end of the kernel in memory pmmngr_init (memSize, 0x100000 + kernelSize*512); DebugPrintf("pmm initialized with %i KB physical memory; memLo: %i memHi: %i\n\n", memSize,bootinfo->m_memoryLo,bootinfo->m_memoryHi); DebugSetColor (0x19); DebugPrintf ("Physical Memory Map:\n"); memory_region* region = (memory_region*)0x1000; for (int i=0; i<15; ++i) { //! sanity check; if type is > 4 mark it reserved if (region[i].type>4) region[i].type=1; //! if start address is 0, there is no more entries, break out if (i>0 && region[i].startLo==0) break; //! display entry DebugPrintf ("region %i: start: 0x%x%x length (bytes): 0x%x%x type: %i (%s)\n", i, region[i].startHi, region[i].startLo, region[i].sizeHi,region[i].sizeLo, region[i].type, strMemoryTypes[region[i].type-1]); //! if region is avilable memory, initialize the region for use if (region[i].type==1) pmmngr_init_region (region[i].startLo, region[i].sizeLo); } //! deinit the region the kernel is in as its in use pmmngr_deinit_region (0x100000, kernelSize*512); DebugSetColor (0x17); DebugPrintf ("\npmm regions initialized: %i allocation blocks; used or reserved blocks: %i\nfree blocks: %i\n", pmmngr_get_block_count (), pmmngr_get_use_block_count (), pmmngr_get_free_block_count () ); //! allocating and deallocating memory examples... DebugSetColor (0x12); uint32_t* p = (uint32_t*)pmmngr_alloc_block (); DebugPrintf ("\np allocated at 0x%x", p); uint32_t* p2 = (uint32_t*)pmmngr_alloc_blocks (2); DebugPrintf ("\nallocated 2 blocks for p2 at 0x%x", p2); pmmngr_free_block (p); p = (uint32_t*)pmmngr_alloc_block (); DebugPrintf ("\nUnallocated p to free block 1. p is reallocated to 0x%x", p); pmmngr_free_block (p); pmmngr_free_blocks (p2, 2); return 0; }
int _k_main(struct multiboot_info *bootinfo) { int i; uint32_t memSize; struct memory_region* region; uint32_t *p,*p2; /*Initialize hardware drivers*/ hw_initialize(); //! install our exception handlers setvect (0, divide_by_zero_fault); setvect (1, single_step_trap); setvect (2, nmi_trap); setvect (3, breakpoint_trap); setvect (4, overflow_trap); setvect (5, bounds_check_fault); setvect (6, invalid_opcode_fault); setvect (7, no_device_fault); setvect (8, double_fault_abort); setvect (10, invalid_tss_fault); setvect (11, no_segment_fault); setvect (12, stack_fault); setvect (13, general_protection_fault); setvect (14, page_fault); setvect (16, fpu_fault); setvect (17, alignment_check_fault); setvect (18, machine_check_abort); setvect (19, simd_fpu_fault); ClrScr (0x13); GotoXY (0,0); SetColor (0x12); Puts ("\n zygote OS v0.01\n"); SetColor (0x17); /* Puts ("Enabled A20!\n"); Puts ("Initialized GDT and IDT!\n"); Puts ("Installed PIC, PIT, and exception handlers!\n"); Printf ("Cpu vender: %s \n", get_cpu_vender ());*/ //! get memory size in KB memSize = 1024 + bootinfo->m_memoryLo + bootinfo->m_memoryHi*64; //! initialize the physical memory manager //! we place the memory bit map used by the PMM at the end of the kernel in memory pmmngr_init (memSize, 0x100000); Printf("pmm initialized with %i KB physical memory; memLo: %i memHi: %i\n", memSize,bootinfo->m_memoryLo,bootinfo->m_memoryHi); SetColor (0x19); Printf ("Physical Memory Map:\n"); region = (struct memory_region*)0x1000; for (i=0; i<15; ++i) { //! sanity check; if type is > 4 mark it reserved if (region[i].type>4) region[i].type=1; //! if start address is 0, there is no more entries, break out if (i>0 && region[i].startLo==0) break; //! display entry Printf ("region %i: start: 0x%x%x length (bytes): 0x%x%x type: %i (%s)\n", i, region[i].startHi, region[i].startLo, region[i].sizeHi,region[i].sizeLo, region[i].type, strMemoryTypes[region[i].type-1]); //! if region is avilable memory, initialize the region for use if (region[i].type==1) pmmngr_init_region (region[i].startLo, region[i].sizeLo); } SetColor (0x17); Printf ("\npmm regions initialized: %i allocation blocks; used or reserved blocks: %i\nfree blocks: %i\n", pmmngr_get_block_count (), pmmngr_get_use_block_count (), pmmngr_get_free_block_count () ); //! allocating and deallocating memory examples... SetColor (0x12); p = (uint32_t*)pmmngr_alloc_block (); Printf ("\np allocated at 0x%x", p); p2 = (uint32_t*)pmmngr_alloc_blocks (2); Printf ("\nallocated 2 blocks for p2 at 0x%x", p2); pmmngr_free_block (p); p = (uint32_t*)pmmngr_alloc_block (); Printf ("\nUnallocated p to free block 1. p is reallocated to 0x%x", p); pmmngr_free_block (p); pmmngr_free_blocks (p2, 2); //To test divide by zero exception //i = 10/0; Puts ("\nHitting any key will fire the default handlers \n"); for(;;) { } };