I am not very knowledgeable about very low-level programming so I wanted to have a pet project to explore it – I am attempting to write something, that would hopefully be considered an OS.
Having written an assembly bootloader, I have proceeded to try to write a kernel in C, here is my code:
//kernel.c
#include "terminal.h"
#include "terminal_backend.h"
void _start(void) {
Terminal* terminal = getTerminalInstance();
T_clearScreen(terminal);
for (;;) {}
...
}
//terminal.h
#pragma once
#include <stdint.h>
struct Terminal;
typedef struct Terminal Terminal;
//TBptr = Terminal backend function pointer
typedef void (*TBptr_moveCursor)(Terminal* t, uint16_t x, uint16_t y);
typedef void (*TBptr_getCursorPosition)(Terminal* t, uint16_t* x, uint16_t* y);
typedef void (*TBptr_clearScreen)(Terminal* t);
typedef void (*TBptr_putCharacter)(Terminal* t, uint32_t ch);
typedef void (*TBptr_getSize)(Terminal* t, uint16_t *w, uint16_t *h);
struct Terminal
{
TBptr_moveCursor TF_moveCursor;
TBptr_getCursorPosition TF_getCursorPosition;
TBptr_clearScreen TF_clearScreen;
TBptr_putCharacter TF_putCharacter;
TBptr_getSize TF_getSize;
void* backend_data;
};
...
//fragment of terminal.c
...
void T_clearScreen(Terminal* terminal)
{
terminal->TF_clearScreen(terminal);
}
...
//fragment of terminal_backend.c
static void primitiveClearScreen(Terminal* terminal)
{
for (;;) {}
...
}
...
static Terminal functionTable = {
.TF_moveCursor = primitiveMoveCursor,
.TF_getCursorPosition = primitiveGetCursorPosition,
.TF_clearScreen = primitiveClearScreen,
.TF_putCharacter = primitivePutCharacter,
.TF_getSize = primitiveGetSize
};
Terminal* getTerminalInstance(void)
{
return &functionTable;
}
The issue is that T_clearScreen(Terminal* terminal)
gets called but the infinite loop in primitiveClearScreen(Terminal* terminal)
does not get executed. It seems to me that the struct didn’t get properly configured and so couldn’t make the proper call to the function adresses. Compiler does not complain about the code. What I expected to happen is that this code should produce statically allocated struct Terminal with pointers to functions that I have implemented in terminal_backend.c and by calling getTerminalInstance()
we get the singleton instance adress and can now call the T_clearScreen(terminal)
.
This also might be caused by process I did to the kernel and other files, but I am hoping that it is not the problem and it’s much simpler. I am still going to describe it in case that is.
The process of getting the C code into the floppy.bin (my virtual floppy) is that I paste the LOAD headers from ELF format into the floppy. Then I tell the bootloader to paste the binary into the proper locations + 0x40000 offset (to align with tutorial I am following). I suppose it could mess with addresses, so I consider it as a possible cause. The “proper locations” are in range from 0x0000 to 0x2000 – which I thought as too low, which is also a reason for including the offset.
I use QEMU, here is command that I use to run the floppy:
qemu-system-x86_64 -fda floppy.bin -cpu core2duo