Skip to content

Commit

Permalink
Fix bugs, but fopen still crashes because we try to do interrupts whi…
Browse files Browse the repository at this point in the history
…le in an interrupt
  • Loading branch information
Twometer committed Jan 22, 2021
1 parent 7582fc1 commit ca4e688
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 30 deletions.
2 changes: 1 addition & 1 deletion kernel/fs/fat16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace FS
{
auto nkPath = nk::Path(path);
auto directory = LoadDirectory(nkPath.GetDirectoryPath());

bool eof = false;
for (size_t i = 0; i < 128; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion kernel/include/kernel/tasks/elfloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Kernel
static Thread *LoadElf(const ELF::Image &image);

private:
static void MapNewZeroedPage(Memory::PageDirectory *dir, vaddress_t vaddr);
static void *MapNewZeroedPages(Memory::PageDirectory *dir, vaddress_t vaddr, size_t sizeInBytes);
};

} // namespace Kernel
Expand Down
8 changes: 4 additions & 4 deletions kernel/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ extern "C"
vfs->Mount("/", fs);

printf("Loading startup app\n");
auto entry = vfs->GetFileMeta("/bin/hlwrld.app");
auto entry = vfs->GetFileMeta("/bin/shell.app");
if (!entry.IsValid())
{
Kernel::Panic("boot", "Startup app not found");
}

auto buf = new uint8_t[entry.size];
auto handle = vfs->Open("/bin/hlwrld.app");
auto handle = vfs->Open("/bin/shell.app");
vfs->Read(handle, 0, entry.size, buf);
vfs->Close(handle);

Expand All @@ -198,10 +198,10 @@ extern "C"
{
Kernel::Panic("boot", "Startup app not valid ELF");
}

auto startupApp = ElfLoader::LoadElf(image);

delete buf;
delete[] buf;

printf("Free kernel heap: %dKB/1024KB\n", get_free_heap() / 1024);

Expand Down
2 changes: 1 addition & 1 deletion kernel/syscallhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Kernel
Interrupts::AddHandler(0x80, this);
}

void SyscallHandler::HandleInterrupt(unsigned int, RegisterStates *regs)
void SyscallHandler::HandleInterrupt(unsigned int i, RegisterStates *regs)
{
// When the interrupt is called, ESP is saved to the regs state.
// At that point however, it is already the kernel's ESP0. The
Expand Down
3 changes: 2 additions & 1 deletion kernel/syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ uint32_t sys$$fclose(void *param)
uint32_t sys$$pagealloc(void *param)
{
int num = *(int *)param;
return (uint32_t)Thread::Current()->MapNewPage(num);
auto ptr = (uint32_t)Thread::Current()->MapNewPage(num);
return ptr;
}
42 changes: 24 additions & 18 deletions kernel/tasks/elfloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Kernel
return nullptr;

auto pagedir = new PageDirectory(*PageDirectory::GetKernelDir());
auto stackAddr = 0;
void *executableEndAddr = 0;

for (size_t i = 0; i < elfHeader->e_phnum; i++)
{
Expand All @@ -33,29 +33,25 @@ namespace Kernel
continue;

auto page = PAGE_ALIGN_DOWN(header.p_vaddr);
MapNewZeroedPage(pagedir, page);
executableEndAddr = MapNewZeroedPages(pagedir, page, header.p_filesz);

#if ELFLOADER_DEBUG
printf("elf_loader: Loading %d bytes from %x to %x (page %x)\n", header.p_filesz, header.p_offset, header.p_vaddr, PAGE_ALIGN_DOWN(header.p_vaddr));
#endif
memcpy((void *)header.p_vaddr, (void *)(image.GetData() + header.p_offset), header.p_filesz);

if (stackAddr < header.p_vaddr)
stackAddr = header.p_vaddr;
}

if (stackAddr == 0)
if (executableEndAddr == 0)
{
printf("elf_loader: error: No address loaded\n");
return nullptr;
}

auto alignedStack = PAGE_ALIGN_UP(stackAddr);
#if ELFLOADER_DEBUG
printf("elf_loader: Making a stack at %x\n", alignedStack);
printf("elf_loader: Making a stack at %x\n", executableEndAddr);
#endif
MapNewZeroedPage(pagedir, alignedStack);
auto stack = new Stack(alignedStack, 4096);
MapNewZeroedPages(pagedir, (vaddress_t)executableEndAddr, 4096);
auto stack = new Stack(executableEndAddr, 4096);
stack->Push(0x00);
stack->Push(0x00);

Expand All @@ -64,29 +60,39 @@ namespace Kernel
printf("elf_loader: Creating thread with ip=%x, sp=%x\n", elfHeader->e_entry, stack->GetStackPtr());
#endif
auto thread = Thread::CreateUserThread((ThreadMain)elfHeader->e_entry, pagedir, stack);
thread->SetFreeMemBase((uint32_t)(alignedStack + 4096));
thread->SetFreeMemBase((uint32_t)(executableEndAddr + 4096));

// Return to kernel
PageDirectory::GetKernelDir()->Load();

return thread;
}

void ElfLoader::MapNewZeroedPage(PageDirectory *dir, vaddress_t vaddr)
void *ElfLoader::MapNewZeroedPages(PageDirectory *dir, vaddress_t vaddr, size_t sizeInBytes)
{
if (!IS_PAGE_ALIGNED(vaddr))
{
printf("elf_loader: error: Can't make zeroed page at non-aligned address %x\n", vaddr);
return;
return nullptr;
}

auto numPages = (int)PAGE_ALIGN_UP(sizeInBytes) / PAGE_SIZE;

#if ELFLOADER_DEBUG
printf("elf_loader: Making new zeroed page at virtual %x\n", vaddr);
printf("elf_loader: Making %d new zeroed pages at virtual %x\n", numPages, vaddr);
#endif

auto pageframe = PageManager::GetInstance()->AllocPageframe();
dir->MapPage(pageframe, vaddr, PAGE_BIT_ALLOW_USER | PAGE_BIT_READ_WRITE);
memset(vaddr, 0, PAGE_SIZE);
vaddress_t a = vaddr;
for (size_t i = 0; i < numPages; i++)
{
auto pageframe = PageManager::GetInstance()->AllocPageframe();
dir->MapPage(pageframe, a, PAGE_BIT_ALLOW_USER | PAGE_BIT_READ_WRITE);
a += PAGE_SIZE;
}


memset(vaddr, 0, sizeInBytes);
return a;
}

} // namespace Kernel
8 changes: 5 additions & 3 deletions libc/liballoc/liballoc_neko.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <sys/syscall.h>
#include <stdio.h>

int liballoc_lock()
{
Expand All @@ -10,12 +11,13 @@ int liballoc_unlock()
return 0;
}

void* liballoc_alloc(int pages)
void *liballoc_alloc(int pages)
{
return (void*) syscall(SYS_PAGEALLOC, &pages);
int page = syscall(SYS_PAGEALLOC, &pages);
return (void *)page;
}

int liballoc_free(void* ptr, int pages)
int liballoc_free(void *ptr, int pages)
{
// TODO
}
3 changes: 2 additions & 1 deletion libneko/nk/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ namespace nk
{
if (path[i] == '/')
{
if (temp.Length() > 0)
if (temp.Length() > 0) {
parts->Add(temp);
}
temp = String();
}
else
Expand Down
1 change: 1 addition & 0 deletions libneko/nk/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <string.h>
#include <stddef.h>
#include <stdio.h>

namespace nk
{
Expand Down
1 change: 1 addition & 0 deletions libneko/nk/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace nk
{
size_t newCapacity = min * 2;
T *newData = (T *)malloc(sizeof(T) * newCapacity);
memset(newData, 0, sizeof(T) * newCapacity);
if (this->data != nullptr)
{
memcpy(newData, data, sizeof(T) * size);
Expand Down
18 changes: 18 additions & 0 deletions userland/shell/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
#include <stdio.h>
#include <stdint.h>

int main(int argc, char **argv)
{
printf("Opening file...\n");
FILE *file = fopen("/home/neko/test.txt", "r");
printf("opened\n");

fseek(file, 0, SEEK_END);
size_t filesize = ftell(file);
printf("Test file has size %d.\n", filesize);

uint8_t *data = new uint8_t[filesize + 1];
data[filesize] = 0;

fread(data, 1, filesize, file);

printf("File test contents: %s\n", data);

fclose(file);
delete[] data;
return 0;
}

0 comments on commit ca4e688

Please sign in to comment.