Compare commits
2 Commits
397b1a2819
...
145ea40945
Author | SHA1 | Date | |
---|---|---|---|
145ea40945 | |||
16f797eeee |
@ -7,8 +7,6 @@ struct AddressSpace
|
||||
|
||||
void destroy();
|
||||
|
||||
void detach();
|
||||
|
||||
AddressSpace clone();
|
||||
|
||||
PageTable* get_pml4()
|
||||
@ -16,13 +14,6 @@ struct AddressSpace
|
||||
return m_pml4;
|
||||
}
|
||||
|
||||
bool is_cloned()
|
||||
{
|
||||
return *m_refs > 1;
|
||||
}
|
||||
|
||||
private:
|
||||
PageTable* m_pml4;
|
||||
|
||||
int* m_refs;
|
||||
};
|
@ -46,13 +46,18 @@ extern "C" void common_handler(Context* context)
|
||||
{
|
||||
kerrorln("Page fault in ring 3 (RIP %lx), while trying to access %lx, error code %ld", context->rip,
|
||||
context->cr2, context->error_code);
|
||||
|
||||
if (Scheduler::current_task())
|
||||
{
|
||||
kinfoln("Page fault ocurred while in task: %ld", Scheduler::current_task()->id);
|
||||
}
|
||||
|
||||
hang(); // FIXME: Remove this when multiple address spaces are working.
|
||||
kinfoln("Stack trace:");
|
||||
|
||||
StackTracer tracer(context->rbp);
|
||||
tracer.trace_with_ip(context->rip);
|
||||
|
||||
hang(); // FIXME: Remove this when multiple address spaces are working.
|
||||
|
||||
Scheduler::task_misbehave(context, -3);
|
||||
}
|
||||
}
|
||||
|
@ -4,27 +4,21 @@
|
||||
#include "log/Log.h"
|
||||
#include "memory/PMM.h"
|
||||
#include "memory/VMM.h"
|
||||
#include "misc/hang.h"
|
||||
#include "std/stdlib.h"
|
||||
#include "std/string.h"
|
||||
#include "utils/move.h"
|
||||
|
||||
AddressSpace AddressSpace::create()
|
||||
{
|
||||
AddressSpace result;
|
||||
result.m_pml4 = (PageTable*)PMM::request_page();
|
||||
result.m_refs = (int*)kmalloc(sizeof(int));
|
||||
*result.m_refs = 1;
|
||||
VMM::install_kernel_page_directory_into_address_space(result);
|
||||
return move(result);
|
||||
}
|
||||
|
||||
void AddressSpace::destroy()
|
||||
{
|
||||
if (is_cloned())
|
||||
{
|
||||
kdbgln("Will not destroy a cloned address space, I don't own it");
|
||||
(*m_refs)--;
|
||||
return;
|
||||
}
|
||||
uint64_t pages_freed = 0;
|
||||
for (int i = 0; i < 512; i++)
|
||||
{
|
||||
@ -78,26 +72,76 @@ void AddressSpace::destroy()
|
||||
pages_freed++;
|
||||
PMM::free_page(m_pml4);
|
||||
|
||||
kfree(m_refs);
|
||||
|
||||
kdbgln("Reclaimed %ld pages from address space!", pages_freed);
|
||||
}
|
||||
|
||||
void AddressSpace::detach()
|
||||
{
|
||||
if (!is_cloned()) return;
|
||||
(*m_refs)--;
|
||||
m_refs = (int*)kmalloc(sizeof(int));
|
||||
*m_refs = 1;
|
||||
m_pml4 = (PageTable*)PMM::request_page();
|
||||
VMM::install_kernel_page_directory_into_address_space(*this);
|
||||
}
|
||||
|
||||
AddressSpace AddressSpace::clone()
|
||||
AddressSpace AddressSpace::clone() // FIXME: Add out-of-memory checks to this function.
|
||||
{
|
||||
AddressSpace result;
|
||||
result.m_pml4 = m_pml4;
|
||||
result.m_refs = m_refs;
|
||||
*m_refs = *m_refs + 1;
|
||||
result.m_pml4 = (PageTable*)PMM::request_page();
|
||||
if (!result.m_pml4) return result;
|
||||
memcpy(result.m_pml4, m_pml4, PAGE_SIZE);
|
||||
for (int i = 0; i < 512; i++)
|
||||
{
|
||||
PageDirectoryEntry& pdp_pde = m_pml4->entries[i];
|
||||
PageDirectoryEntry& cloned_pdp_pde = result.m_pml4->entries[i];
|
||||
if (!pdp_pde.present) continue;
|
||||
if (pdp_pde.larger_pages)
|
||||
{
|
||||
void* cloned = PMM::request_page();
|
||||
memcpy(cloned, (void*)pdp_pde.get_address(), PAGE_SIZE);
|
||||
cloned_pdp_pde.set_address((uint64_t)cloned);
|
||||
continue;
|
||||
}
|
||||
PageTable* pdp = (PageTable*)pdp_pde.get_address();
|
||||
PageTable* cloned_pdp = (PageTable*)PMM::request_page();
|
||||
memcpy(cloned_pdp, pdp, PAGE_SIZE);
|
||||
cloned_pdp_pde.set_address((uint64_t)cloned_pdp);
|
||||
for (int j = 0; j < 511; j++) // skip the last page directory, it's the kernel one
|
||||
{
|
||||
PageDirectoryEntry& pd_pde = pdp->entries[j];
|
||||
PageDirectoryEntry& cloned_pd_pde = cloned_pdp->entries[j];
|
||||
if (!pd_pde.present) continue;
|
||||
if (pd_pde.larger_pages)
|
||||
{
|
||||
void* cloned = PMM::request_page();
|
||||
memcpy(cloned, (void*)pd_pde.get_address(), PAGE_SIZE);
|
||||
cloned_pd_pde.set_address((uint64_t)cloned);
|
||||
continue;
|
||||
}
|
||||
PageTable* pd = (PageTable*)pd_pde.get_address();
|
||||
PageTable* cloned_pd = (PageTable*)PMM::request_page();
|
||||
memcpy(cloned_pd, pd, PAGE_SIZE);
|
||||
cloned_pd_pde.set_address((uint64_t)cloned_pd);
|
||||
for (int k = 0; k < 512; k++)
|
||||
{
|
||||
PageDirectoryEntry& pt_pde = pd->entries[k];
|
||||
PageDirectoryEntry& cloned_pt_pde = cloned_pd->entries[k];
|
||||
if (!pt_pde.present) continue;
|
||||
if (pt_pde.larger_pages)
|
||||
{
|
||||
void* cloned = PMM::request_page();
|
||||
memcpy(cloned, (void*)pt_pde.get_address(), PAGE_SIZE);
|
||||
cloned_pt_pde.set_address((uint64_t)cloned);
|
||||
continue;
|
||||
}
|
||||
PageTable* pt = (PageTable*)pt_pde.get_address();
|
||||
PageTable* cloned_pt = (PageTable*)PMM::request_page();
|
||||
memcpy(cloned_pt, pt, PAGE_SIZE);
|
||||
cloned_pt_pde.set_address((uint64_t)cloned_pt);
|
||||
for (int l = 0; l < 512; l++)
|
||||
{
|
||||
PageDirectoryEntry& pde = pt->entries[l];
|
||||
PageDirectoryEntry& cloned_pde = cloned_pt->entries[l];
|
||||
if (!pde.present) continue;
|
||||
void* cloned = PMM::request_page();
|
||||
memcpy(cloned, (void*)pde.get_address(), PAGE_SIZE);
|
||||
cloned_pde.set_address((uint64_t)cloned);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
VMM::install_kernel_page_directory_into_address_space(result);
|
||||
return result;
|
||||
}
|
@ -87,7 +87,22 @@ char* strcat(char* dest, const char* src)
|
||||
|
||||
char* strstr(char* haystack, const char* needle)
|
||||
{
|
||||
return (char*)memmem(haystack, strlen(haystack), needle, strlen(needle));
|
||||
size_t needle_size = strlen(needle);
|
||||
size_t haystack_size = strlen(haystack);
|
||||
while (*haystack)
|
||||
{
|
||||
if (*haystack == *needle)
|
||||
{
|
||||
if (needle_size <= haystack_size)
|
||||
{
|
||||
if (!strncmp(haystack, needle, needle_size)) return haystack;
|
||||
}
|
||||
else { return NULL; }
|
||||
}
|
||||
haystack++;
|
||||
haystack_size--;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* memmem(void* haystack, size_t haystacklen, const void* needle, size_t needlelen)
|
||||
@ -100,7 +115,7 @@ void* memmem(void* haystack, size_t haystacklen, const void* needle, size_t need
|
||||
{
|
||||
if (needlelen <= haystacklen)
|
||||
{
|
||||
if (!memcmp(hs, nd, needlelen)) return (void*)const_cast<char*>(hs);
|
||||
if (!memcmp(hs, nd, needlelen)) return (void*)(const_cast<char*>(hs));
|
||||
}
|
||||
else { return NULL; }
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "memory/MemoryManager.h"
|
||||
#include "memory/PMM.h"
|
||||
#include "memory/VMM.h"
|
||||
#include "misc/hang.h"
|
||||
#include "std/stdlib.h"
|
||||
#include "std/string.h"
|
||||
#include "sys/Syscall.h"
|
||||
@ -13,8 +14,7 @@
|
||||
#include "thread/Scheduler.h"
|
||||
#include "utils/Addresses.h"
|
||||
|
||||
void sys_fork(Context* context) // FIXME: Even though both processes's address spaces are the same in content, writing
|
||||
// to one should not affect the other.
|
||||
void sys_fork(Context* context)
|
||||
{
|
||||
kinfoln("fork(): attempting fork");
|
||||
|
||||
@ -30,16 +30,10 @@ void sys_fork(Context* context) // FIXME: Even though both processes's address s
|
||||
}
|
||||
for (int i = 0; i < TASK_MAX_FDS; i++) { child->files[i] = parent->files[i]; }
|
||||
|
||||
size_t stack_bytes = get_top_of_stack(parent->allocated_stack, TASK_PAGES_IN_STACK) - parent->regs.rsp;
|
||||
|
||||
child->regs.rsp = get_top_of_stack(child->allocated_stack, TASK_PAGES_IN_STACK) - stack_bytes;
|
||||
|
||||
memcpy((void*)child->regs.rsp, (void*)parent->regs.rsp, stack_bytes);
|
||||
|
||||
child->regs.rsp += sizeof(uintptr_t) * 2; // I don't know why this is...
|
||||
|
||||
child->address_space = parent->address_space.clone();
|
||||
|
||||
child->regs.rsp += (2 * sizeof(uintptr_t));
|
||||
|
||||
child->regs.rax = 0;
|
||||
context->rax = child->id;
|
||||
|
||||
@ -87,20 +81,9 @@ void sys_exec(Context* context, const char* pathname)
|
||||
return;
|
||||
}
|
||||
|
||||
uint64_t allocated_stack = (uint64_t)MemoryManager::get_pages(TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER);
|
||||
if (!allocated_stack)
|
||||
{
|
||||
kfree(kpathname);
|
||||
context->rax = -ENOMEM;
|
||||
return;
|
||||
}
|
||||
|
||||
uint64_t allocated_stack_phys = VMM::get_physical(allocated_stack);
|
||||
|
||||
if ((uint64_t)memusage > PMM::get_free())
|
||||
{
|
||||
kfree(kpathname);
|
||||
MemoryManager::release_pages((void*)allocated_stack, TASK_PAGES_IN_STACK);
|
||||
context->rax = -ENOMEM;
|
||||
return;
|
||||
}
|
||||
@ -111,29 +94,12 @@ void sys_exec(Context* context, const char* pathname)
|
||||
Task* task = Scheduler::current_task();
|
||||
ASSERT(task);
|
||||
|
||||
if (task->address_space.is_cloned())
|
||||
{
|
||||
kdbgln("Detaching cloned address space, %p, %s", (void*)task->address_space.get_pml4(),
|
||||
task->address_space.is_cloned() ? "is cloned" : "is not cloned");
|
||||
task->address_space.detach();
|
||||
VMM::switch_to_user_address_space(task->address_space);
|
||||
kdbgln("Detached cloned address space, %p, %s", (void*)task->address_space.get_pml4(),
|
||||
task->address_space.is_cloned() ? "is cloned" : "is not cloned");
|
||||
}
|
||||
|
||||
// At this point, pretty much nothing can fail.
|
||||
|
||||
ELFImage* image = ELFLoader::load_elf_from_vfs(program);
|
||||
ASSERT(image); // If check_elf_image succeeded, load_elf_from_vfs MUST succeed, unless something has gone terribly
|
||||
// wrong.
|
||||
|
||||
task->allocated_stack = allocated_stack;
|
||||
|
||||
for (uint64_t i = 0; i < TASK_PAGES_IN_STACK; i++)
|
||||
{
|
||||
VMM::map(allocated_stack + (i * PAGE_SIZE), allocated_stack_phys + (i * PAGE_SIZE), MAP_READ_WRITE | MAP_USER);
|
||||
}
|
||||
|
||||
Scheduler::reset_task(task, image);
|
||||
|
||||
set_context_from_task(*task, context);
|
||||
|
@ -132,8 +132,8 @@ void Scheduler::load_user_task(const char* filename)
|
||||
new_task->user_task = true;
|
||||
new_task->regs.rip = image->entry;
|
||||
new_task->image = image;
|
||||
new_task->allocated_stack = (uint64_t)MemoryManager::get_pages(
|
||||
TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right?
|
||||
new_task->allocated_stack = (uint64_t)MemoryManager::get_pages_at(
|
||||
0x100000, TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right?
|
||||
new_task->regs.rsp = get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK);
|
||||
new_task->regs.cs = 0x18 | 0x03;
|
||||
new_task->regs.ss = 0x20 | 0x03;
|
||||
@ -185,8 +185,6 @@ void Scheduler::reap_task(Task* task)
|
||||
VMM::switch_to_user_address_space(exiting_task->address_space);
|
||||
}
|
||||
kinfoln("reaping task %ld, exited with code %ld", exiting_task->id, exiting_task->exit_status);
|
||||
if (exiting_task->allocated_stack)
|
||||
MemoryManager::release_pages((void*)exiting_task->allocated_stack, TASK_PAGES_IN_STACK);
|
||||
if (exiting_task->image) // FIXME: Also free pages the task has mmap-ed but not munmap-ed.
|
||||
{
|
||||
// ELFLoader::release_elf_image(exiting_task->image);
|
||||
|
Loading…
Reference in New Issue
Block a user