reimplmenets the previous iteration with a brand new custom build system

This commit is contained in:
2025-05-29 10:00:38 -04:00
commit e4f160e8b6
35 changed files with 1060 additions and 0 deletions

30
kernel/arch/boot.s Normal file
View File

@ -0,0 +1,30 @@
global loader ; entry symbol for ELF
extern kmain
MAGIC_NUMBER equ 0x1BADB002 ; magic number constant
FLAGS equ 0x3
CHECKSUM equ -(MAGIC_NUMBER + FLAGS) ; calculate the checksum
KERNEL_STACK_SIZE equ 4096
section .multiboot
align 4 ; code must be 4 byte aligned
dd MAGIC_NUMBER
dd FLAGS
dd CHECKSUM
section .bss
align 4
kernel_stack:
resb KERNEL_STACK_SIZE
section .text
loader:
mov esp, kernel_stack + KERNEL_STACK_SIZE
;mov eax, 0xCAFEBABE
call kmain
cli
loop: hlt
jmp loop

19
kernel/arch/gdt_entry.c Normal file
View File

@ -0,0 +1,19 @@
#include <kernel/x86/gdt.h>
uint64_t create_descriptor(uint32_t base, uint32_t limit, uint16_t flag)
{
uint64_t descriptor;
descriptor = limit & 0x000F0000;
descriptor |= (flag << 8) & 0x00F0FF00;
descriptor |= (base >> 16) & 0x000000FF;
descriptor |= base & 0xFF000000;
descriptor <<= 32;
descriptor |= base << 16;
descriptor |= limit & 0x0000FFFF;
return descriptor;
}

24
kernel/arch/io.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef ARCH_IO_H
#define ARCH_IO_H
/**
* outb:
* Sends the given data to the I/O port. Defined in io.s
*
* @param port The I/O port to send the data to
* @param data TThe data to send to the I/O port
*/
void outb(unsigned short port, unsigned char data);
/**
* inb:
* Read a byte from an I/O port
*
* @param port The address of the I/O port
* @return The read byte
*/
unsigned char inb(unsigned short port);
#endif

20
kernel/arch/io.s Normal file
View File

@ -0,0 +1,20 @@
global outb
; out b - send a byte to an I/O port
; stack: [esp + 8] the data byte
; [esp + 4] the I/O port
; [esp ] return address
outb:
mov al, [esp + 8]
mov dx, [esp + 4] ; move the address into dx
out dx, al ; send the data to the I/O port
ret ; return to the calling function
global inb
; inb - returns a byte from the given I/O port
; stack: [esp + 4] The address of the I/O port
; [esp ] The return address
inb:
mov dx, [esp + 4] ; move the address of the I/O port to the dx register
in al, dx ; read a byte from the I/O port and store it in the al register
ret ; return the read byte

28
kernel/arch/link.ld Normal file
View File

@ -0,0 +1,28 @@
ENTRY(loader)
SECTIONS
{
. = 1M;
.text BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
*(.text)
}
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}
.bss BLOCK(4K) : ALIGN(4K)
{
*(COMMON)
*(.bss)
}
}

123
kernel/arch/serial.c Normal file
View File

@ -0,0 +1,123 @@
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <kernel/serial.h>
#include "io.h"
/* I/O ports */
/**
* All the I/O ports are calculated relative to their base.
* The ports (COM1, COM2, etc) have their ports in the same order,
* but they start at different values.
*/
#define SERIAL_COM1_BASE 0x3F8
#define SERIAL_DATA_PORT(base) (base)
#define SERIAL_FIFO_COMMAND_PORT(base) (base + 2)
#define SERIAL_LINE_COMMAND_PORT(base) (base + 3)
#define SERIAL_MODEM_COMMAND_PORT(base) (base + 4)
#define SERIAL_LINE_STATUS_PORT(base) (base + 5)
/* I/O port commands */
/**
* SERIAL_LINE_ENABLE_DLAB:
* Tells the serial port to expect first the highest 8 bits on the data port,
* then the lowest 8 bits will follow
*/
#define SERIAL_LINE_ENABLE_DLAB 0x80
void serial_configure_baud_rate(unsigned short com, unsigned short divisor)
{
outb(SERIAL_LINE_COMMAND_PORT(com),
SERIAL_LINE_ENABLE_DLAB);
outb(SERIAL_DATA_PORT(com),
(divisor >> 8) & 0x00FF);
outb(SERIAL_DATA_PORT(com),
divisor & 0x00FF);
}
void serial_configure_line(unsigned short com)
{
/**
* Bit: | 7 | 6 | 5 4 3 | 2 | 1 0 |
* Content: | d | b | prty | s | dl |
* d - Enabled (d = 1) or disables (d = 0) DLAB
* b - If break control is enabled (b = 1) or disabled (b = 0)
* prty - The number of parity bits to use
* s - The number of stop bits to use (s = 0 equals 1, s = 1 equals 1.5 or 2)
* dl - Describes the length of the data
* Value: | 0 | 0 | 0 0 0 | 0 | 1 1 | = 0x03
*/
outb(SERIAL_LINE_COMMAND_PORT(com), 0x03);
}
void serial_configure_buffers(unsigned short com)
{
/**
* Bit: | 7 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* Content: | lvl | bs | r | dma | clt | clr | e |
* lvl - How many bytes should be stored in the FIFO buffers
* bs - If the buffers should be 16 or 64 bytes large
* r - Reserved for future use
* dma - How the serial port data should be accessed
* clt - Clear the transmission FIFO buffer
* clr - Clear the receiver FIFO buffer
* e - If the FIFO buffer should be enabled or not
* Value: | 1 1 | 0 | 0 | 0 | 1 | 1 | 1 |
*/
outb(SERIAL_FIFO_COMMAND_PORT(com), 0xC7);
}
void serial_configure_modem(unsigned short com)
{
/**
* Bit: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* Content: | r | r | af | lb | ao2 | ao1 | rts | dtr |
* r - Reserved
* af - Autoflow control enabled
* lb - Loopback mode (used for debugging serial ports)
* ao2 - Auxiliary output 2, used for receiving interrupts
* ao1 - Auxiliary output 1
* rts - Ready to transmit
* dtr - Data Terminal ready
* Value: | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
*/
outb(SERIAL_MODEM_COMMAND_PORT(com), 0x03);
}
int serial_is_transmit_fifo_empty(unsigned int com)
{
/* 0x20 = 0010 0000 */
return inb(SERIAL_LINE_STATUS_PORT(com)) & 0x20;
}
void serial_initialize(void)
{
serial_configure_baud_rate(SERIAL_COM1_BASE, 2);
serial_configure_line(SERIAL_COM1_BASE);
serial_configure_buffers(SERIAL_COM1_BASE);
serial_configure_modem(SERIAL_COM1_BASE);
}
void serial_write_byte(unsigned short com, char c)
{
while (!serial_is_transmit_fifo_empty(com));
outb(SERIAL_DATA_PORT(com), c);
}
void serial_write(const char* buf, size_t len)
{
for (size_t i = 0; i < len; i++)
serial_write_byte(SERIAL_COM1_BASE, buf[i]);
}
void serial_writestring(const char* buf)
{
serial_write(buf, strlen(buf));
}

131
kernel/arch/tty.c Normal file
View File

@ -0,0 +1,131 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <kernel/tty.h>
#include "vga.h"
#include "io.h"
/* I/O ports */
#define VGA_COMMAND_PORT 0x3D4
#define VGA_DATA_PORT 0x3D5
/* I/O port commands */
#define VGA_HIGH_BYTE_COMMAND 14
#define VGA_LOW_BYTE_COMMAND 15
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
static uint16_t* const VGA_MEMORY = (uint16_t*) 0xB8000;
static size_t terminal_row;
static size_t terminal_column;
static uint8_t terminal_colour;
static struct vga_entry* terminal_buffer;
struct vga_entry {
char c;
unsigned char colour;
} __attribute__((packed));
void terminal_set_colour(uint8_t colour)
{
terminal_colour = colour;
}
void terminal_clear(void)
{
for (size_t y = 0; y < VGA_HEIGHT; y++)
for (size_t x = 0; x < VGA_WIDTH; x++)
terminal_write_cell((y * VGA_WIDTH) + x, ' ');
terminal_move_cursor(0);
}
void terminal_initialize(void)
{
terminal_row = 0;
terminal_column = 0;
terminal_buffer = (struct vga_entry*) VGA_MEMORY;
terminal_set_colour(vga_entry_colour(VGA_COLOUR_LIGHT_GREY, VGA_COLOUR_BLACK));
terminal_clear();
}
void terminal_move_cursor(unsigned short pos)
{
if (terminal_row >= VGA_HEIGHT)
terminal_scroll_screen();
outb(VGA_COMMAND_PORT, VGA_HIGH_BYTE_COMMAND);
outb(VGA_DATA_PORT, ((pos >> 8) & 0x00FF));
outb(VGA_COMMAND_PORT, VGA_LOW_BYTE_COMMAND);
outb(VGA_DATA_PORT, pos & 0x00FF);
}
inline size_t terminal_get_pos(void)
{return (terminal_row * VGA_WIDTH) + terminal_column;}
void terminal_write_cell(unsigned int i, char c)
{
struct vga_entry entry;
entry.c = c;
entry.colour = terminal_colour;
terminal_buffer[i] = entry;
}
void terminal_write_character(unsigned int i, char c)
{
switch(c) {
case '\n':
terminal_row += 1;
__attribute__((fallthrough));
case '\r':
terminal_column = 0;
terminal_move_cursor(terminal_get_pos());
break;
default:
terminal_write_cell(i, c);
terminal_column += 1;
if (terminal_column >= VGA_WIDTH) {
terminal_column = 0;
terminal_row += 1;
}
terminal_move_cursor(terminal_get_pos());
}
}
void terminal_write(const char* buf, size_t len)
{
for (size_t i = 0; i < len; i++)
terminal_write_character(terminal_get_pos(), buf[i]);
}
void terminal_writestring(const char* buf)
{
terminal_write(buf, strlen(buf));
}
void terminal_scroll_screen(void)
{
size_t y, x;
for (y = 0; y < VGA_HEIGHT -1; y++) {
for (x = 0; x < VGA_WIDTH; x++) {
terminal_buffer[(y * VGA_WIDTH) + x] =
terminal_buffer[((y + 1) * VGA_WIDTH) + x];
}
}
for (x = 0; x < VGA_WIDTH; x++)
terminal_write_cell(((VGA_HEIGHT - 1) * VGA_WIDTH) + x, ' ');
terminal_row -= 1;
terminal_column = 0;
terminal_move_cursor(terminal_get_pos());
}

30
kernel/arch/vga.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef ARCH_VGA_H
#define ARCH_VGA_H
#include <stdint.h>
typedef enum vga_colour {
VGA_COLOUR_BLACK = 0,
VGA_COLOUR_BLUE = 1,
VGA_COLOUR_GREEN = 2,
VGA_COLOUR_CYAN = 3,
VGA_COLOUR_RED = 4,
VGA_COLOUR_MAGENTA = 5,
VGA_COLOUR_BROWN = 6,
VGA_COLOUR_LIGHT_GREY = 7,
VGA_COLOUR_DARK_GREY = 8,
VGA_COLOUR_LIGHT_BLUE = 9,
VGA_COLOUR_LIGHT_GREEN = 10,
VGA_COLOUR_LIGHT_CYAN = 11,
VGA_COLOUR_LIGHT_RED = 12,
VGA_COLOUR_LIGHT_MAGENTA = 13,
VGA_COLOUR_LIGHT_BROWN = 14,
VGA_COLOUR_WHITE = 15,
} vga_colour_t;
static inline uint8_t vga_entry_colour(vga_colour_t fg, vga_colour_t bg) {
return fg | bg << 4;
}
#endif

View File

@ -0,0 +1,12 @@
#ifndef _KERNEL_KERNEL_H
#define _KERNEL_KERNEL_H
void kerror(const char*);
void kwarn(const char*);
void kinfo(const char*);
#endif

View File

@ -0,0 +1,91 @@
#include <stdint.h>
#ifndef _KERNEL_SERIAL_H
#define _KERNEL_SERIAL_H
/**
* serial_initialize:
* Initializes the serial port for writing.
*/
void serial_initialize(void);
/**
* serial_configure_baud_rate:
* Sets the speed of the data being sent.
* The argument is a divisor of that number, hence the speed becomes
* (speed / divisor) bits/s.
*
* @param com The COM port to configure
* @param divisor The divisor
*/
void serial_configure_baud_rate(unsigned short com, unsigned short divisor);
/**
* serial_configure_line:
* Configures the line of the given serial port. The port is set to have a
* data length of 8 bits, no parity bits, one stop bit and break control
* disabled.
*
* @param com The serial port to configure
*/
void serial_configure_line(unsigned short com);
/**
* serial_configure_buffers:
* Configures the buffer of the given serial port. The port is set to
* enable FIFO, clear the receiver and transmission FIFO queues,
* and to use 14 bytes as a size of the queue.
*
* @param com The serial port to configure
*/
void serial_configure_buffers(unsigned short com);
/**
* serial_configure_modem:
* Configures the modem of the given serial port.
* We don't need interrupts, and just need RTS and DTS.
*
* @param com The serial port to configure
*/
void serial_configure_modem(unsigned short com);
/**
* serial_is_transmit_fifo_empty:
* Checks whether the transmit FIFO queue is empty or not for the given COM port.
*
* @param com The COM port
*
* @return 0 if the transmit FIFO queue is not empty
* 1 if the transmit FIFO queue is empty
*/
int serial_is_transmit_fifo_empty(unsigned int com);
/**
* serial_write_byte:
* Writes a byte to the serial port, and adhering to the FIFO queue,
* ensures bytes aren't overwritten
*
* @param com The serial port
* @param c The character to write
*/
void serial_write_byte(unsigned short com, char c);
/**
* serial_write:
* Writes to the serial output (DEFAULTS TO COM1)
*
* @param buf The character buffer
* @param len The length of the buffer
*/
void serial_write(const char* buf, size_t len);
/**
* serial_writestring:
* Writes toe the serial output (DEFAULT TO COM1)
*
* @param buf The character buffer
*/
void serial_writestring(const char* buf);
#endif

View File

@ -0,0 +1,80 @@
#ifndef _KERNEL_TTY_H
#define _KERNEL_TTY_H
#include <stddef.h>
/**
* terminal_clear:
* Clears the terminal
*/
void terminal_clear(void);
/**
* terminal_initialize:
* Initialize the framebuffer variables.
*/
void terminal_initialize(void);
/**
* terminal_move_cursor:
* Moves the cursor of the framebuffer to the given position
*
* @param pos The new position of the cursor
*/
void terminal_move_cursor(unsigned short pos);
/**
* terminal_get_pos:
* Calculates the position of the cursor
*
* @return The position
*/
size_t terminal_get_pos(void);
/**
* terminal_write_cell:
* Writes a character with the given foreground and background to the framebuffer
*
* @param i The location in the framebuffer
* @param c The character
* @param fg The foreground colour
* @param bg The background colour
*/
void terminal_write_cell(unsigned int i, char c);
/**
* terminal_write_character:
* Parses the given character, enacts any edge cases, and passes
* the rest of the write information down to terminal_write_cell
*
* @param i The location in the terminal_buffer
* @param c The character
* @param fg The foreground colour
* @param bg The background colour
*/
void terminal_write_character(unsigned int i, char c);
/**
* terminal_write:
* Writes the content of the buffer to the framebuffer
*
* @param buf Buffer containing text to write to the framebuffer
* @param len Length of the buffer
*/
void terminal_write(const char* buf, size_t len);
/**
* terminal_writestring:
* Writes the content of the buffer to the framebuffer
*
* @param buf Buffer containing text to write to the framebuffer
*/
void terminal_writestring(const char* buf);
/**
* terminal_scroll_screen:
* Scrolls the terminal screen up one.
*/
void terminal_scroll_screen(void);
#endif

View File

@ -0,0 +1,60 @@
#include <stdint.h>
#ifndef ARCH_I386_GDT_H
#define ARCH_I386_GDT_H
// Each define here is for a specific flag in the descriptor.
// Refer to the intel documentation for a description of what each one does.
#define SEG_DESCTYPE(x) ((x) << 0x04) // Descriptor type (0 for system, 1 for code/data)
#define SEG_PRES(x) ((x) << 0x07) // Present
#define SEG_SAVL(x) ((x) << 0x0C) // Available for system use
#define SEG_LONG(x) ((x) << 0x0D) // Long mode
#define SEG_SIZE(x) ((x) << 0x0E) // Size (0 for 16-bit, 1 for 32)
#define SEG_GRAN(x) ((x) << 0x0F) // Granularity (0 for 1B - 1MB, 1 for 4KB - 4GB)
#define SEG_PRIV(x) (((x) & 0x03) << 0x05) // Set privilege level (0 - 3)
#define SEG_DATA_RD 0x00 // Read-Only
#define SEG_DATA_RDA 0x01 // Read-Only, accessed
#define SEG_DATA_RDWR 0x02 // Read/Write
#define SEG_DATA_RDWRA 0x03 // Read/Write, accessed
#define SEG_DATA_RDEXPD 0x04 // Read-Only, expand-down
#define SEG_DATA_RDEXPDA 0x05 // Read-Only, expand-down, accessed
#define SEG_DATA_RDWREXPD 0x06 // Read/Write, expand-down
#define SEG_DATA_RDWREXPDA 0x07 // Read/Write, expand-down, accessed
#define SEG_CODE_EX 0x08 // Execute-Only
#define SEG_CODE_EXA 0x09 // Execute-Only, accessed
#define SEG_CODE_EXRD 0x0A // Execute/Read
#define SEG_CODE_EXRDA 0x0B // Execute/Read, accessed
#define SEG_CODE_EXC 0x0C // Execute-Only, conforming
#define SEG_CODE_EXCA 0x0D // Execute-Only, conforming, accessed
#define SEG_CODE_EXRDC 0x0E // Execute/Read, conforming
#define SEG_CODE_EXRDCA 0x0F // Execute/Read, conforming, accessed
#define GDT_CODE_PL0 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \
SEG_PRIV(0) | SEG_CODE_EXRD
#define GDT_DATA_PL0 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \
SEG_PRIV(0) | SEG_DATA_RDWR
#define GDT_CODE_PL3 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \
SEG_PRIV(3) | SEG_CODE_EXRD
#define GDT_DATA_PL3 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \
SEG_PRIV(3) | SEG_DATA_RDWR
void setGdt(unsigned short limit, uint64_t* base);
void reloadSegments();
uint64_t create_descriptor(uint32_t base, uint32_t limit, uint16_t flag);
#endif

38
kernel/klog.c Normal file
View File

@ -0,0 +1,38 @@
#include <stddef.h>
#include <kernel/_kernel.h>
#include <kernel/serial.h>
enum log_mode {
LOG_ERR,
LOG_WARN,
LOG_INFO,
};
void klog(const char* buf, enum log_mode mode)
{
switch(mode) {
case LOG_ERR:
serial_writestring("ERROR: ");
break;
case LOG_WARN:
serial_writestring("WARNING: ");
break;
case LOG_INFO:
serial_writestring("INFO: ");
break;
}
serial_writestring(buf);
serial_writestring("\n");
}
void kerror(const char* buf)
{klog(buf, LOG_ERR);}
void kwarn(const char* buf)
{klog(buf, LOG_WARN);}
void kinfo(const char* buf)
{klog(buf, LOG_INFO);}

17
kernel/kmain.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
#include <kernel/tty.h>
#include <kernel/serial.h>
void kmain(void)
{
terminal_initialize();
serial_initialize();
terminal_writestring("test");
serial_writestring("test!");
printf("test..");
}