Skip to content

Commit

Permalink
Start work on interrupt handlers and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Twometer committed Dec 18, 2020
1 parent a6481c4 commit 7bd238d
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 17 deletions.
8 changes: 6 additions & 2 deletions Nekofile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ CCFLAGS = -std=gnu++11 -ffreestanding -mno-red-zone -fno-exceptions -nostdlib -W
[asm, multi]:
nasm -f {format=bin} {input} -o {output}

[cc, single]:
[kcc, single]:
neko-gcc {input} -o {output} -T kernel.ld {CCFLAGS} {include}

[cc, single]:
neko-gcc -c {input} -o {output} {CCFLAGS} {include}

; Targets
default:
% clean
Expand Down Expand Up @@ -44,7 +47,7 @@ kernel:
format: elf32
output: build/intermediate/~.o

[cc]
[kcc]
input: build/intermediate/*.o
kernel/**.cpp
kernel/**.c
Expand All @@ -53,5 +56,6 @@ kernel:

include: -I kernel/include
-I libc/include
-I nekolib

output: build/NEKOKRNL
6 changes: 5 additions & 1 deletion kernel/arch/interrupts.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "interrupts.h"
#include <kernel/interrupts.h>
#include <kernel/io.h>
#include <kernel/tty.h>
#include <device/keyboard.h>
Expand Down Expand Up @@ -295,5 +295,9 @@ void Interrupts::HandleException(unsigned int vector, struct interrupt_frame *fr

void Interrupts::HandleInterrupt(unsigned int interrupt)
{
}

void Interrupts::AddHandler(unsigned int interrupt, InterruptHandler handler)
{

}
2 changes: 2 additions & 0 deletions kernel/device/keyboard.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <kernel/tty.h>
#include <kernel/interrupts.h>
#include <device/keyboard.h>

using namespace Device;
Expand Down Expand Up @@ -61,6 +62,7 @@ void Keyboard::Initialize()
NewScancode(52, '.');
NewScancode(53, '-');
NewScancode(57, ' ');

}

void Keyboard::HandleInterrupt(unsigned int scancode)
Expand Down
4 changes: 2 additions & 2 deletions kernel/heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extern "C"
return (bytes + HEAP_SEG_SIZE - 1) / HEAP_SEG_SIZE;
}

void *malloc(size_t size)
void *kmalloc(size_t size)
{
size_t header_size = sizeof(heap_entry);
size_t total_size = size + header_size;
Expand Down Expand Up @@ -117,7 +117,7 @@ extern "C"
return data;
}

void free(void *ptr)
void kfree(void *ptr)
{
uint8_t *byte_ptr = (uint8_t *)ptr;
heap_entry *description = (heap_entry *)(byte_ptr - sizeof(heap_entry));
Expand Down
15 changes: 12 additions & 3 deletions kernel/include/kernel/heap.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#include <stddef.h>
#include <stdint.h>

void heap_init();
#ifdef __cplusplus
extern "C"
{
#endif

void* malloc(size_t size);
void heap_init();

void free(void *ptr);
void *kmalloc(size_t size);

void kfree(void *ptr);

#ifdef __cplusplus
}
#endif
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

struct IDT_entry
{
unsigned short int offset_lowerbits;
Expand All @@ -18,6 +19,10 @@ struct interrupt_frame

namespace Kernel
{
class InterruptHandler
{
virtual void Handle(unsigned int interrupt);
};

class Interrupts
{
Expand All @@ -33,6 +38,8 @@ namespace Kernel
static void SetIdtEntry(unsigned int interrupt, unsigned char type, unsigned long address);

static void HandleInterrupt(unsigned int interrupt);

static void AddHandler(unsigned int interrupt, InterruptHandler handler);
};

}; // namespace Kernel
8 changes: 2 additions & 6 deletions kernel/kernel.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
extern "C" {
#include <kernel/tty.h>
#include <kernel/heap.h>
#include <kernel/interrupts.h>
#include <device/keyboard.h>
#include <device/cpu.h>
#include <stdio.h>

#include "arch/interrupts.h"

using namespace Kernel;

/* nekosys Kernel entry point */
void nkmain() {
extern "C" void nkmain() {
// Welcome message
TTY::Clear();
TTY::SetColor(0x0b);
Expand All @@ -32,6 +30,4 @@ void nkmain() {
printf("$ ");

Device::CPU::Halt();
}

}
9 changes: 6 additions & 3 deletions libc/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
#define _STDLIB_H 1

#include <sys/cdefs.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif

__attribute__((__noreturn__))
void abort(void);
__attribute__((__noreturn__)) void abort(void);
void *malloc(size_t size);
void free(void *ptr);

#ifdef __cplusplus
}
Expand Down
14 changes: 14 additions & 0 deletions libc/stdlib/malloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdlib.h>
#include <kernel/heap.h>

// TODO thats not how it works lol

void *malloc(size_t size)
{
return kmalloc(size);
}

void free(void *ptr)
{
return kfree(ptr);
}
44 changes: 44 additions & 0 deletions nekolib/nk/vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <stdlib.h>

namespace nk
{

template <typename T>
class Vector
{
private:
T *data = nullptr;
int index = 0;
int capacity = 0;

public:
Vector()
{
EnsureCapacity(5);
}

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

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

} // namespace nk

0 comments on commit 7bd238d

Please sign in to comment.