Skip to content

Commit

Permalink
more work on the kernel
Browse files Browse the repository at this point in the history
Twometer committed Dec 20, 2020
1 parent 740baad commit dfaeeb6
Showing 15 changed files with 191 additions and 48 deletions.
6 changes: 3 additions & 3 deletions kernel/arch/interrupts.cpp
Original file line number Diff line number Diff line change
@@ -246,13 +246,13 @@ void Interrupts::SetupIdt()
SetIdtEntry(46, TYPE_INTERRUPT_GATE, (unsigned long)irq14);
SetIdtEntry(47, TYPE_INTERRUPT_GATE, (unsigned long)irq15);

/* fill the IDT descriptor */
// build idt descriptor
unsigned long idt_ptr[2];
unsigned long idt_address = (unsigned long)IDT;
idt_ptr[0] = (sizeof(struct IDT_entry) * 256) + ((idt_address & 0xffff) << 16);
idt_ptr[1] = idt_address >> 16;

/* load the IDT */
// load idt
load_idt(idt_ptr);
}

@@ -292,7 +292,7 @@ void Interrupts::HandleException(unsigned int vector, struct interrupt_frame *fr

void Interrupts::HandleInterrupt(unsigned int interrupt)
{
for (int i = 0; i < entries.Size(); i++) {
for (size_t i = 0; i < entries.Size(); i++) {
InterruptHandlerEntry &entry = entries.At(i);
if (entry.interrupt == interrupt)
entry.handler->HandleInterrupt(interrupt);
20 changes: 20 additions & 0 deletions kernel/device/cmos.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <device/cmos.h>
#include <kernel/io.h>

using namespace Device;
using namespace Kernel;

#define PORT_ADDR 0x70
#define PORT_DATA 0x71

uint8_t CMOS::Read(uint8_t addr)
{
IO::Out8(PORT_ADDR, addr);
return IO::In8(PORT_DATA);
}

void CMOS::Write(uint8_t addr, uint8_t value)
{
IO::Out8(PORT_ADDR, addr);
IO::Out8(PORT_DATA, value);
}
5 changes: 4 additions & 1 deletion kernel/device/keyboard.cpp
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
#include <kernel/interrupts.h>
#include <kernel/io.h>
#include <device/keyboard.h>
#include <stdio.h>

using namespace Kernel;
using namespace Device;
@@ -75,7 +76,9 @@ void Keyboard::HandleInterrupt(unsigned int interrupt)

char chr = MapChar(scancode);
if (chr != 0)
Kernel::TTY::Write(&chr, 1);
{
putchar(chr);
}
}

void Keyboard::NewScancode(unsigned int scancode, char c)
8 changes: 8 additions & 0 deletions kernel/heap.cpp
Original file line number Diff line number Diff line change
@@ -45,6 +45,14 @@ extern "C"
return (allocation_map[a] & (0x1 << b)) == 0;
}

size_t get_free_heap() {
size_t freeHeap = 0;
for (size_t i = 0; i < ALLOCATION_MAP_ENTRIES; i++)
if (is_free(i))
freeHeap += HEAP_SEG_SIZE;
return freeHeap;
}

void set_free(size_t alloc_unit, bool free)
{
size_t a = alloc_unit / 8;
19 changes: 19 additions & 0 deletions kernel/include/device/cmos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _CMOS_H
#define _CMOS_H

#include <stdint.h>

namespace Device
{

class CMOS
{
public:
static uint8_t Read(uint8_t addr);

static void Write(uint8_t addr, uint8_t value);
};

} // namespace Device

#endif
1 change: 1 addition & 0 deletions kernel/include/device/devicemanager.h
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
#include "cpu.h"
#include "keyboard.h"
#include "pic.h"
#include "cmos.h"

namespace Device
{
2 changes: 1 addition & 1 deletion kernel/include/device/keyboard.h
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ namespace Device
{
private:
char scancode_map[128];

public:
void Initialize();

2 changes: 2 additions & 0 deletions kernel/include/kernel/heap.h
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@ extern "C"

void heap_init();

size_t get_free_heap();

void *kmalloc(size_t size);

void kfree(void *ptr);
23 changes: 15 additions & 8 deletions kernel/kernel.cpp
Original file line number Diff line number Diff line change
@@ -9,27 +9,34 @@ using namespace Device;

/* nekosys Kernel entry point */
extern "C" void nkmain() {
// Welcome message
Interrupts::Disable();

// Banner
TTY::Clear();
TTY::SetColor(0x0b);
printf("nekosys 0.01 <by Twometer>\n");
TTY::SetColor(0x0f);
TTY::SetColor(0x07);

// Init
printf("Initializing...\n");
uint16_t base_memory = (CMOS::Read(0x16) << 8) | CMOS::Read(0x15);
uint32_t ext_memory = (CMOS::Read(0x31) << 8 | CMOS::Read(0x30));

printf("Base Memory: %d KB\n", base_memory);
printf("Extended Memory: %d KB\n", ext_memory);

printf("Initializing heap...\n");
heap_init();

printf("Initializing devices...\n");
DeviceManager::Initialize();

// Set up environment
printf("Setting up interrupts...\n");
Interrupts::Disable();
Interrupts::SetupIdt();
DeviceManager::Initialize();

Interrupts::Enable();

// Dummy terminal
printf("Initialized.\n\n");
printf("$ ");

// Idle
Device::CPU::Halt();
}
27 changes: 0 additions & 27 deletions libc/cpplib.cpp

This file was deleted.

63 changes: 63 additions & 0 deletions libc/icxxabi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <stddef.h>
#include <stdlib.h>
#include "icxxabi.h"

extern "C" void __cxa_pure_virtual()
{
// Do nothing or print an error message.
}

atexitFuncEntry_t __atexitFuncs[ATEXIT_FUNC_MAX];
uarch_t __atexitFuncCount = 0;

void *__dso_handle = 0;

int __cxa_atexit(void (*f)(void *), void *objptr, void *dso){
if(__atexitFuncCount >= ATEXIT_FUNC_MAX){
return -1;
}
__atexitFuncs[__atexitFuncCount].destructorFunc = f;
__atexitFuncs[__atexitFuncCount].objPtr = objptr;
__atexitFuncs[__atexitFuncCount].dsoHandle = dso;
__atexitFuncCount++;
return 0;
}

void __cxa_finalize(void *f){
signed i = __atexitFuncCount;
if(!f){
while(i--){
if(__atexitFuncs[i].destructorFunc){
(*__atexitFuncs[i].destructorFunc)(__atexitFuncs[i].objPtr);
}
}
return;
}

for(; i >= 0; i--){
if(__atexitFuncs[i].destructorFunc == f){
(*__atexitFuncs[i].destructorFunc)(__atexitFuncs[i].objPtr);
__atexitFuncs[i].destructorFunc = 0;
}
}
}

void *operator new(size_t size)
{
return malloc(size);
}

void *operator new[](size_t size)
{
return malloc(size);
}

void operator delete(void *p)
{
free(p);
}

void operator delete[](void *p)
{
free(p);
}
25 changes: 25 additions & 0 deletions libc/icxxabi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef _ICXXABI_H
#define _ICXXABI_H
#define ATEXIT_FUNC_MAX 128

extern "C"
{

typedef unsigned uarch_t;

struct atexitFuncEntry_t
{
void (*destructorFunc)(void *);
void *objPtr;
void *dsoHandle;
};

extern void *__dso_handle;

int __cxa_atexit(void (*f)(void *), void *objptr, void *dso);
void __cxa_finalize(void *f);

void __cxa_pure_virtual();
};

#endif
1 change: 1 addition & 0 deletions libc/include/stdio.h
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ extern "C" {
int printf(const char* __restrict, ...);
int putchar(int);
int puts(const char*);
int getchar(void);

#ifdef __cplusplus
}
1 change: 1 addition & 0 deletions libc/stdio/getchar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <stdio.h>
36 changes: 28 additions & 8 deletions nekolib/nk/vector.h
Original file line number Diff line number Diff line change
@@ -12,15 +12,30 @@ namespace nk
{
private:
T *data = nullptr;
size_t index = 0;
size_t size = 0;
size_t capacity = 0;

public:
void Add(T obj)
{
EnsureCapacity(index + 1);
data[index] = obj;
index++;
EnsureCapacity(size + 1);
data[size] = obj;
size++;
}

void Remove(size_t idx)
{
for (size_t i = idx + 1; i < size; i++)
{
data[i - 1] = data[i];
}

size--;
}

const T &At(size_t index) const
{
return data[index];
}

T &At(size_t index)
@@ -30,26 +45,31 @@ namespace nk

size_t Size()
{
return index;
return size;
}

size_t Capacity()
{
return capacity;
}

~Vector()
{
free((void *)data);
}

private:
void EnsureCapacity(size_t min)
{
if (min > capacity)
{
size_t newCapacity = min * 2;
T *newData = (T*) malloc(sizeof(T) * newCapacity);
T *newData = (T *)malloc(sizeof(T) * newCapacity);
if (this->data != nullptr)
{
memcpy(newData, data, sizeof(T) * index);
memcpy(newData, data, sizeof(T) * size);
}
free((void*) this->data);
free((void *)this->data);
this->data = newData;
this->capacity = newCapacity;
}

0 comments on commit dfaeeb6

Please sign in to comment.