diff --git a/kernel/boot.asm b/kernel/boot.asm index 06a5926..ab54ac6 100644 --- a/kernel/boot.asm +++ b/kernel/boot.asm @@ -64,7 +64,6 @@ segmented_start: mov fs, cx mov esp, STACK_V+0x1000 ;ok, now we can access our data - ;Then the multiboot info structures are saved to local data variables. add ebx, VIRT_OFFSET push eax diff --git a/kernel/lang/vector.h b/kernel/lang/vector.h index 570fd42..e31284c 100644 --- a/kernel/lang/vector.h +++ b/kernel/lang/vector.h @@ -52,6 +52,9 @@ public: /* Direct access operators */ const type & operator[](unsigned int index) const; type & operator[](unsigned int index); + + /* Returns pointer to data */ + type **data(); }; template @@ -122,7 +125,6 @@ vector & vector::add(type elem) while (mySize >= myAllocated) grow(); myData[mySize++] = new type(elem); - kprintf("vector.h: added element %d @ 0x%x\n", mySize-1, myData[mySize-1]); return *this; } @@ -168,4 +170,11 @@ vector & vector::insert(type elem, unsigned int position) return *this; } + +template +type **vector::data() +{ + return myData; +} + #endif diff --git a/kernel/mm/vmm.c b/kernel/mm/vmm.c index 7e87caf..c78a24b 100644 --- a/kernel/mm/vmm.c +++ b/kernel/mm/vmm.c @@ -2,7 +2,7 @@ // Author: Josh Holtrop // Date: 09/30/03 // Rewritten from scratch: 12/23/03 -// Modified: 08/17/05 +// Modified: 09/12/05 #include "hos_defines.h" #include "kernel.h" @@ -157,11 +157,13 @@ void *kmalloc(u32_t size) if (size % VMM_MALLOC_GRANULARITY) size = size + VMM_MALLOC_GRANULARITY - (size % VMM_MALLOC_GRANULARITY); + /* added 09/12/05 by josh - fixed bug returning size 0 segments + multiple times */ + if (size < VMM_MALLOC_GRANULARITY) + size = VMM_MALLOC_GRANULARITY; void *attempt = vmm_getFreeChunk(size); if (attempt) { - if ((u32_t)attempt == 0xd000c5e4) - kprintf("vmm.c: attempt = 0x%x\n", attempt); k_leave_critical(); return attempt; } @@ -171,8 +173,6 @@ void *kmalloc(u32_t size) return NULL; //we could not get any more heap memory } attempt = vmm_getFreeChunk(size); - if ((u32_t)attempt == 0xd000c5e4) - kprintf("vmm.c: attempt = 0x%x\n", attempt); k_leave_critical(); return attempt; } @@ -485,8 +485,6 @@ HeapEntry_t *vmm_getUnusedEntry() { if (heapEntryQueues[VMM_HE_UNUSED].count < 5) { - kprintf(" heapEntryQueues[VMM_HE_UNUSED].count = %d\n", - heapEntryQueues[VMM_HE_UNUSED].count); HeapEntry_t *he = heapEntryQueues[VMM_HE_HOLE].head->next; HeapEntry_t *wilderness = he; while (he) @@ -515,7 +513,7 @@ HeapEntry_t *vmm_stripUnusedEntry() HeapEntry_t *he = heapEntryQueues[VMM_HE_UNUSED].head->next; heapEntryQueues[VMM_HE_UNUSED].head->next = he->next; if (! he->next) - kprintf(" he->next is NULL\n"); + k_check(1, "kernel panic: he->next is NULL\n"); ((HeapEntry_t *)he->next)->prev = he->prev; heapEntryQueues[VMM_HE_UNUSED].count--; he->next = 0; diff --git a/kernel/proc/hash.cpp b/kernel/proc/hash.cpp index 8b5c8a8..01e7f00 100644 --- a/kernel/proc/hash.cpp +++ b/kernel/proc/hash.cpp @@ -37,17 +37,11 @@ int hash::add(u32_t key, void *val) for (u32_t i = 0; i < myTable[hashed].size(); i++) if ( myTable[hashed][i].key == key ) return -1; // key exists already - kprintf("hash.cpp: %d (slot %d) -> %d\n", key, hashed, val); hash_entry_t he = {key, val}; - kprintf("hash.cpp: going to add {%d, %d} to vector %d\n", he.key, he.data, hashed); myTable[hashed].add(he); - kprintf("hash.cpp: finished adding %d -> %d\n", - myTable[hashed][myTable[hashed].size()-1].key, - myTable[hashed][myTable[hashed].size()-1].data); mySize++; if ( mySize > (hash_primes[mySizeIndex] << 1) ) // re-hash { - kprintf("re-hashing\n"); vector< vector > oldTable = myTable; mySizeIndex++; myTable = vector< vector >(); @@ -99,7 +93,7 @@ int hash::exists(u32_t key) u32_t hash::hash_key(u32_t key) { - return key % hash_primes[mySizeIndex]; + return ((key * 11) + mySizeIndex) % hash_primes[mySizeIndex]; } u32_t hash::size() diff --git a/kernel/proc/proc.cpp b/kernel/proc/proc.cpp index 4ae1cf6..5a78a93 100644 --- a/kernel/proc/proc.cpp +++ b/kernel/proc/proc.cpp @@ -27,9 +27,12 @@ extern "C" { u32_t cur_task = 0; u32_t n_processes = 0; tss_t tss0; +u32_t pid_base = 1024; +u32_t pid_index = 0; } hash *processes; +vector *pids; int proc_init() { @@ -37,54 +40,54 @@ int proc_init() memset(&tss0, 0, sizeof(tss_t)); tss0.ss0 = SEG_KERNEL_DATA; tss0.esp0 = VIRT_STACK_TOP; - kprintf("make new hash\n"); processes = new hash(); - vector< vector > test; - for (u32_t i = 0; i < 5; i++) - test.add(vector()); - for (u32_t i = 0; i < 5; i++) - for (u32_t j = 0; j < 3; j++) - test[i].add(i*10+j); - for (u32_t i = 0; i < 5; i++) - for (u32_t j = 0; j < 3; j++) - kprintf("<%d>", test[i][j]); - for (u32_t i = 0; i < 11; i++) - { - kprintf("add %d to hash\n", i); - processes->add(i, (void *) i); - } - kprintf("\n\n"); - processes->dump(); - kprintf("\n\n"); - for (u32_t i = 0; i < 11; i++) - { - kprintf("get %d from hash\n", i); - if (i != (u32_t)processes->get(i)) - kprintf(" !=: %d -> %d\n", i, processes->get(i)); - processes->remove(i); - } - kprintf("size: %d\n", processes->size()); - + process_t *proc = (process_t *)New(process_t); + proc->p_page_dir = read_cr3(); + proc->page_dir = (u32_t *)0xFFFFF000; + processes->add(0, proc); + pids = new vector(); + pids->add(0); return 0; } void proc_sched(int_stack_t *int_stack) { + u32_t new_pid_index = (pid_index + 1) % pids->size(); + switch_task(int_stack, (*pids)[new_pid_index]); + pid_index = new_pid_index; +} +u32_t get_pid() +{ + u32_t potential = pid_base; + while (processes->exists(potential)) + { + potential++; + if (potential == 0) + potential = PROC_MIN_USER_PID; + } + pid_base = potential + 1; + if (pid_base == 0) + pid_base = PROC_MIN_USER_PID; + return potential; } void switch_task(int_stack_t *int_stack, u32_t new_task) { -// memcpy((*processes)[cur_task]->int_stack, int_stack, sizeof(int_stack_t)); + memcpy(&((process_t *)processes->get(cur_task))->int_stack, + (void *) int_stack, + sizeof(int_stack_t)); cur_task = new_task; -// memcpy(int_stack, (*processes)[cur_task]->int_stack, sizeof(int_stack_t)); -// write_cr3((*processes)[cur_task]->p_page_dir); + memcpy(int_stack, &((process_t *)processes->get(cur_task))->int_stack, + sizeof(int_stack_t)); + write_cr3( ((process_t *)processes->get(cur_task))->p_page_dir ); } u32_t create_task(void *base, u32_t image_size, u32_t bss_size, void *entry) { - u32_t pid; -// processes[++n_processes] = create_process(base, image_size, bss_size, entry); + u32_t pid = get_pid(); + processes->add(pid, create_process(base, image_size, bss_size, entry)); + pids->add(pid); return pid; } diff --git a/kernel/proc/proc.h b/kernel/proc/proc.h index 948cb12..282f28b 100644 --- a/kernel/proc/proc.h +++ b/kernel/proc/proc.h @@ -13,6 +13,8 @@ extern "C" { #include "hos_defines.h" #include "kernel.h" +#define PROC_MIN_USER_PID 1024 + typedef struct { u32_t *page_dir; /* Page directory with physical PT addys */ @@ -67,6 +69,7 @@ typedef struct int proc_init(); void proc_sched(int_stack_t *int_stack); +u32_t get_pid(); void switch_task(int_stack_t *int_stack, u32_t new_task); u32_t create_task(void *base, u32_t image_size, u32_t bss_size, void *entry); process_t *create_process(void *base, u32_t image_size, u32_t bss_size, void *entry); diff --git a/rmmod/rmmod.asm b/rmmod/rmmod.asm index 4574578..c0d7f33 100644 --- a/rmmod/rmmod.asm +++ b/rmmod/rmmod.asm @@ -20,6 +20,9 @@ ; ebx = return address ; ecx = where to store real mode parameter table start: + jmp blah +blah: + jmp $ jmp 0:start_refreshed %include "conio.inc" @@ -29,6 +32,8 @@ start_refreshed: mov ax, cs ; 0 mov ds, ax mov es, ax + mov fs, ax + mov gs, ax mov ss, ax mov esp, 0x7000 mov [dat_rmadd], ecx