MAJOR RESTRUCTURE

This commit is contained in:
2024-12-28 21:23:58 +01:00
parent 12b07a9f70
commit 2e0b030324
81 changed files with 1168 additions and 579 deletions
+28
View File
@@ -0,0 +1,28 @@
#ifndef _CPU_IDT_H
#define _CPU_IDT_H
#include <stdint.h>
#define IDT_ENTRIES 256
#define TRAP_GATE_FLAGS 0x8f // P = 1, DPL = 00, S = 0, Type = 1111 (32-bit Trap gate)
#define INT_GATE_FLAGS 0x8e // P = 1, DPL = 00, S = 0, Type = 1110 (32-bit Int gate)
#define USR_INT_GATE_FLAGS 0xee // P = 1, DPL = 11, S = 0, Type = 1110 (32-bit Int gate)
typedef struct {
uint16_t offsetLow;
uint16_t selector;
uint8_t zero;
uint8_t typeAttr;
uint16_t offsetHigh;
} __attribute__ ((packed)) idtEntry_t;
typedef struct {
uint16_t limit;
uint32_t base;
} __attribute__ ((packed)) idtp_t;
void idtSetGate(uint8_t i, void* handler, uint8_t flags);
void setupIdt();
#endif
+27
View File
@@ -0,0 +1,27 @@
#ifndef _CPU_ISR_H
#define _CPU_ISR_H
#include <stdint.h>
typedef struct {
// Pushed by OS
uint32_t esp, ebx, ecx, edx, esi, edi, ebp;
uint32_t ds, es, fs, gs, eax;
uint32_t interrupt, error;
// Pushed by CPU
uint32_t eip, cs, eflags, user_esp, user_ss;
} intFrame_t;
typedef void (*HandlerPtr)(intFrame_t* iframe);
void setupIsrs();
void registerIrq(uint8_t id, HandlerPtr handler);
int handleInterrupt(intFrame_t iframe);
// ISR Table
extern void* asm_isrRedirectTable[48];
// Int 0x80 - Syscall
extern void isr128();
#endif
+19
View File
@@ -0,0 +1,19 @@
#ifndef _CPU_SYSTEM_H
#define _CPU_SYSTEM_H
#include <stdint.h>
void cpuid(uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx);
void outb(uint16_t port, uint8_t data);
void outw(uint16_t port, uint16_t data);
void outl(uint16_t port, uint32_t data);
uint8_t inb(uint16_t port);
uint16_t inw(uint16_t port);
uint32_t inl(uint16_t port);
void ioWait();
void panic();
#endif
+14
View File
@@ -0,0 +1,14 @@
#ifndef _DRIVERS_DISK_H
#define _DRIVERS_DISK_H
#include <stdint.h>
#define PIO_PORT 0x1f0
#define READ_COMMAND 0x20
#define WRITE_COMMAND 0x30
#define CACHE_FLUSH 0xE7
void diskRead(uint32_t lbaAddress, uint8_t sectorCount, char* dst);
void diskWrite(uint32_t lbaAddress, uint8_t sectorCount, char* src);
#endif
+48
View File
@@ -0,0 +1,48 @@
#ifndef _DRIVERS_KEYBOARD_H
#define _DRIVERS_KEYBOARD_H
#include <cpu/isr.h>
// Special keys
#define LEFT_SHIFT_PRESS 0x2a
#define LEFT_SHIFT_RELEASE (LEFT_SHIFT_PRESS | 0x80)
#define RIGHT_SHIFT_PRESS 0x36
#define RIGHT_SHIFT_RELEASE (RIGHT_SHIFT_PRESS | 0x80)
#define LEFT_CTRL_PRESS 0x1D
#define LEFT_CTRL_RELEASE (LEFT_CTRL_PRESS | 0x80)
#define RIGHT_CTRL_PRESS 0x1D
#define RIGHT_CTRL_RELEASE (RIGHT_CTRL_PRESS | 0x80)
#define LEFT_ALT_PRESS 0x38
#define LEFT_ALT_RELEASE (LEFT_ALT_PRESS | 0x80)
#define RIGHT_ALT_PRESS 0x38
#define RIGHT_ALT_RELEASE (RIGHT_ALT_PRESS | 0x80)
// Basic US Keyboard layout
#define US_KEYBOARD {\
{\
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',\
'\t','q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',\
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'','`',\
0, '\\','z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0,\
'*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
'7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.',\
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0\
},\
{\
0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t',\
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', 0, 'A', 'S',\
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0, '|', 'Z', 'X', 'C', 'V',\
'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.',\
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0\
}\
}
typedef struct {
char character;
int shift;
} keyboardState_t;
void keyboardHandler(intFrame_t*);
#endif
+14
View File
@@ -0,0 +1,14 @@
#ifndef _DRIVERS_PCSPK_H
#define _DRIVERS_PCSPK_H
#include <sys/notes.h>
#include <stdint.h>
#define PC_SPEAKER 0x61
void enableSpeaker();
void disableSpeaker();
void playNote(note_t note, uint32_t ms);
void rest(uint32_t ms);
#endif
+25
View File
@@ -0,0 +1,25 @@
#ifndef _DRIVERS_RTC_H
#define _DRIVERS_RTC_H
#include <cpu/isr.h>
#include <stdint.h>
#define CMOS_ADDRESS 0x70
#define CMOS_DATA 0x71
typedef struct {
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t day;
uint8_t month;
uint16_t year;
} __attribute__ ((packed)) datetime_t;
void rtcHandler(intFrame_t*);
int cmosUpdateProgress();
uint8_t getRtcRegister(uint8_t reg);
void enableRtc();
void disableRtc();
#endif
+23
View File
@@ -0,0 +1,23 @@
#ifndef _DRIVERS_SERIAL_H
#define _DRIVERS_SERIAL_H
#include <stdarg.h>
#define PORT_COM1 0x3f8
#define PORT_COM2 0x2f8
#define PORT_COM3 0x3e8
#define PORT_COM4 0x2e8
// IRQ4 -> COM1, IRQ3 -> COM2
#define SERIAL_IRQ 4
int serialEnable(int port);
int setupSerial();
void serialWrite(int port, char c);
char serialRead(int port);
char serialReadAsync(int port);
int debugf(const char* fmt, ...);
int vdebugf(const char* fmt, va_list va);
#endif
+14
View File
@@ -0,0 +1,14 @@
#ifndef _DRIVERS_TIMER_H
#define _DRIVERS_TIMER_H
#include <cpu/isr.h>
#include <stdint.h>
#define CHANNEL_0 0x40 // Offset it by 1 or 2 for ch1 and ch2
#define COMMAND 0x43
#define INPUT_CLK 1193182
void timerHandler(intFrame_t*);
void setPitPhase(uint8_t channel, uint8_t opMode, uint16_t hz);
#endif
+83
View File
@@ -0,0 +1,83 @@
#ifndef _FS_FS_H
#define _FS_FS_H
/*
* For info, read the fs-spec.txt document inside of docs/
*/
#include <stdint.h>
// FS Definitions
#define FS_SECTOR_SIZE 512
#define FS_BLOCK_SIZE 4096
// Conversion Macros (beatiful)
#define BYTES2BLOCKS(_n) ( (_n) == 0 ? 0 : \
(_n) <= (FS_BLOCK_SIZE) ? 1 : \
((_n) / FS_BLOCK_SIZE) + (((_n) % FS_BLOCK_SIZE) > 0 ? 1 : 0) )
#define BYTES2SECTORS(_n) ( (_n) == 0 ? 0 : \
(_n) <= (FS_SECTOR_SIZE) ? 1 : \
((_n) / FS_SECTOR_SIZE) + (((_n) % FS_SECTOR_SIZE) > 0 ? 1 : 0) )
// Filetypes
#define FT_FILE 0
#define FT_DIR 1
// FS Structures
// TODO: datetime_t?
typedef struct {
uint8_t sectors[FS_BLOCK_SIZE / FS_SECTOR_SIZE][FS_SECTOR_SIZE];
} __attribute__ ((packed)) bootblock_t;
// Superblock - Padded to 4096 Bytes
typedef struct {
// Constant data
uint16_t deviceId;
uint16_t extentsPerIndirect;
uint32_t maxFileSize;
uint16_t inodesPerSector;
// INode data
uint32_t inodeCount;
uint32_t inodeBitmapBlock;
uint32_t inodesStart;
uint32_t inodeBlockCount;
// Data data :)
uint32_t dataBitmapBlock;
uint32_t dataStart;
uint32_t dataBlockCount;
// Other
uint32_t rootInodePtr;
uint32_t firstUnreservedInode;
} __attribute__ ((packed)) superblock_t;
// Extent - 8 Bytes
typedef struct {
uint32_t block;
uint32_t length;
} __attribute__ ((packed)) extent_t;
// INode - 64 Bytes
typedef struct {
uint32_t id;
uint8_t type;
uint32_t sizeBytes; // Size in bytes
uint32_t sizeSectors; // Size in sectors
uint8_t mode; // Access permisions
extent_t extent[4];
uint32_t singleIndirect;
uint32_t doubleIndirect;
uint8_t reserved[10];
} __attribute__ ((packed)) inode_t;
typedef struct {
char name[60];
uint32_t id;
} __attribute__ ((packed)) dirEntry_t;
#endif
+22
View File
@@ -0,0 +1,22 @@
#ifndef _FS_KERNFS_H
#define _FS_KERNFS_H
#include <fs/fs.h>
#include <stdint.h>
#include <stdio.h>
#define MAX_OPEN_FILES 256
typedef struct {
FILE* openFileTable;
uint32_t openFiles;
} fsInfo_t;
extern superblock_t* impl_superblock;
extern fsInfo_t* fsInfo;
inode_t* getInode(char* path, dirEntry_t* dentries);
uint8_t loadFile(inode_t* inode, void* dest);
void initOpenFileTable();
#endif
+61
View File
@@ -0,0 +1,61 @@
#ifndef _MEMORY_ADDRESSES_H
#define _MEMORY_ADDRESSES_H
/*
* Memory layout of addresses 0x0000 - 0x12000
* 0x0000-0x0fff - Probably shouldn't mess with this? idk it feels wrong
* 0x1000-0x17ff - Free
* 0x1800-0x1fff - Global Variables
* 0x2000-0x2fff - Larger structs
* 0x3000-0x3fff - Filesystem Info
* 0x4000-0x4fff - Font area
* 0x6000-0x6fff - VBE info area
* 0x7000-0x7bff - SMAP Area
* 0x7c00-0x8bff - Bootblock
* 0x8c00-0x9bff - Superblock
* 0x9c00-0xafff - Free
* 0xb000-0xbfff - Bootloader first INode
* 0xc000-0xcfff - Scratch INode block
*/
// Global Variables 0x1800-0x1fff
#define SLEEP_TIMER 0x1800
#define CURRENT_PAGE_DIR_ADDR 0x1804
#define PHYS_MEM_MAX_BLOCKS 0x1808
#define PHYS_MEM_USED_BLOCKS 0x180C
// Some structs 0x2000-0x2fff
#define CONSOLE_INFO_LOC 0x2000
#define KEYBOARD_STATE_LOC 0x2100
#define DATETIME_LOC 0x2200
// Filesystem Area 0x3000 - 0x3fff
#define FS_INFO_LOC 0x3000
// Font area 0x4000 - 0x4fff
#define FONT_LOC 0x4000
// VBE Info Area 0x6000 - 0x6fff
#define MODE_INFO_BLOCK_LOC 0x6000 // 512 Bytes
// Smap Area 0x7000 - 0x7bff
#define SMAP_ENTRY_COUNT 0x7000
#define SMAP_ENTRIES 0x7004
// Bootblock 0x7c00-0x9bff
#define BOOTBLOCK_LOC 0x7c00
// Superblock 0x9c00-0xabff
#define SUPERBLOCK_LOC 0x8c00
// First INode from bootloader
#define BOOT_FIRST_INODE_LOC 0xb000
#define SCRATCH_BLOCK_LOC 0xc000
// Other addresses
#define KERNEL_LOC 0x100000
#define KERNEL_MALLOC_AREA 0x300000
#define MEMMAP_AREA 0x30000
#define OPEN_FILE_LOC 0xd000
#endif
+27
View File
@@ -0,0 +1,27 @@
#ifndef _MEMORY_MALLOC_H
#define _MEMORY_MALLOC_H
#include <memory/virtual.h>
#include <memory/physical.h>
#include <stdbool.h>
#include <stdint.h>
// Singly linked list!
typedef struct mallocNode_s {
uint32_t size;
bool free;
struct mallocNode_s* next;
} mallocNode_t;
extern mallocNode_t* mallocListHead;
extern uint32_t mallocVirtualAddr;
extern uint32_t mallocPhysicalAddr;
extern uint32_t mallocPages;
void mallocInit(uint32_t size);
void mallocSplit(mallocNode_t* node, uint32_t size);
void* mallocNextNode(uint32_t size);
void mallocMergeFree();
void mallocFree(void* ptr);
#endif
+33
View File
@@ -0,0 +1,33 @@
#ifndef _MEMORY_PHYSICAL_H
#define _MEMORY_PHYSICAL_H
#include <memory/addresses.h>
#include <string.h>
#include <stdint.h>
#define BLOCK_SIZE 4096 // 4kB
#define BLOCKS_PER_BYTE 8
typedef struct smapEntry_s {
uint64_t baseAddress;
uint64_t length;
uint32_t type;
uint32_t acpiAttr;
} __attribute__ ((packed)) smapEntry_t;
extern uint32_t* memoryMap;
extern uint32_t maxBlocks;
extern uint32_t usedBlocks;
void setBlock(uint32_t block);
void unsetBlock(uint32_t block);
uint8_t testBlock(uint32_t block);
int32_t findFreeBlocks(uint32_t blockCount);
void setupPhysicalMemoryManager(uint32_t startAddress, uint32_t size);
void initMemoryRegion(uint32_t baseAddress, uint32_t size);
void deinitMemoryRegion(uint32_t baseAddress, uint32_t size);
void* allocBlocks(uint32_t blockCount);
void freeBlocks(void* address, uint32_t blockCount);
#endif
+70
View File
@@ -0,0 +1,70 @@
#ifndef _MEMORY_VIRTUAL_H
#define _MEMORY_VIRTUAL_H
#include <memory/physical.h>
#include <stdint.h>
#define PAGES_PER_TABLE 1024
#define TABLES_PER_DIRECTORY 1024
#define PAGE_SIZE 4096
#define PD_INDEX(_vAddress) ((_vAddress) >> 22)
#define PT_INDEX(_vAddress) (((_vAddress) >> 12) & 0x3ff)
#define PAGE_PHYS_ADDRESS(_dirEntry) (*(_dirEntry) & ~ 0xfff)
#define SET_ATTR(_entry, _attr) (*(_entry) |= (_attr))
#define CLEAR_ATTR(_entry, _attr) (*(_entry) &= ~(_attr))
#define TEST_ATTR(_entry, _attr) (*(_entry) & (_attr))
#define SET_FRAME(_entry, _address) (*(_entry) = (*(_entry) & ~0xfffff000) | (_address))
typedef uint32_t ptEntry_t;
typedef uint32_t pdEntry_t;
typedef enum {
PTE_PRESENT = 0x01,
PTE_READ_WRITE = 0x02,
PTE_USER = 0x04,
PTE_WRITE_THROUGH = 0x08,
PTE_CACHE_DISABLE = 0x10,
PTE_ACCESSED = 0x20,
PTE_DIRTY = 0x40,
PTE_PAT = 0x80,
PTE_GLOBAL = 0x100,
PTE_FRAME = 0x7ffff000,
} PAGE_TABLE_FLAGS;
typedef enum {
PDE_PRESENT = 0x01,
PDE_READ_WRITE = 0x02,
PDE_USER = 0x04,
PDE_WRITE_THROUGH = 0x08,
PDE_CACHE_DISABLE = 0x10,
PDE_ACCESSED = 0x20,
PDE_DIRTY = 0x40, // Only with 4MB pages
PDE_PAGE_SIZE = 0x80, // Is page 4MB
PDE_GLOBAL = 0x100,
PDE_PAT = 0x2000,
PDE_FRAME = 0x7ffff000,
} PAGE_DIR_FLAGS;
typedef struct {
ptEntry_t entries[PAGES_PER_TABLE];
} pageTable_t;
typedef struct {
pdEntry_t entries[TABLES_PER_DIRECTORY];
} pageDirectory_t;
extern pageDirectory_t* currentPd;
ptEntry_t* getPtEntry(pageTable_t* pt, uint32_t virtualAddress);
pdEntry_t* getPdEntry(pageDirectory_t* pd, uint32_t virtualAddress);
ptEntry_t* getPage(uint32_t virtualAddress);
void* allocPage(ptEntry_t* page);
void freePage(ptEntry_t* page);
uint8_t setPageDirectory(pageDirectory_t* pd);
void flushTlbEntry(uint32_t virtualAddress);
uint8_t mapPage(void* physicalAddr, void* virtualAddr);
void unmapPage(void* virtualAddress);
uint8_t setupVirtualMemoryManager();
#endif
+72
View File
@@ -0,0 +1,72 @@
#ifndef _SCREEN_GFXMODE_H
#define _SCREEN_GFXMODE_H
#include <memory/addresses.h>
#include <stdint.h>
#define BG_COLOR 0x00010b17
#define FG_COLOR 0x00ebddf4
typedef struct modeInfoBlock_s {
// Required by all VBE revisions
uint16_t modeAttr;
uint8_t windowAAtrr;
uint8_t windowBAttr;
uint16_t windowGranularity;
uint16_t windowSize;
uint16_t windowASegment;
uint16_t windowBSegment;
uint32_t windowFunctionPtr;
uint16_t bytesPerScanline;
// Required by VBE 1.2+
uint16_t xRes;
uint16_t yRes;
uint8_t xCharSize;
uint8_t yCharSize;
uint8_t planeCount;
uint8_t bpp;
uint8_t bankCount;
uint8_t memoryModel;
uint8_t bankSize;
uint8_t imagePageCount;
uint8_t reserved1;
// Direct color fields
uint8_t redMaskSize;
uint8_t redFieldPos;
uint8_t greenMaskSize;
uint8_t greenFieldPos;
uint8_t blueMaskSize;
uint8_t blueFieldPos;
uint8_t reservedMaskSize;
uint8_t reservedFieldPos;
uint8_t directColorModeInfo;
// Required by VBE 2.0+
uint32_t physicalBasePtr;
uint32_t reserved2;
uint16_t reserved3;
// Required by VBE 3.0+
uint16_t linearBytesPerScanline;
uint8_t bankImagePageCount;
uint8_t linearImagePageCount;
uint8_t linearRedMaskSize;
uint8_t linearRedFieldPos;
uint8_t linearGreenMaskSize;
uint8_t linearGreenFieldPos;
uint8_t linearBlueMaskSize;
uint8_t linearBlueFieldPos;
uint8_t linearReservedMaskSize;
uint8_t linearReservedFieldPos;
uint32_t maxPixelClock;
uint8_t reserved4[190];
} __attribute__ ((packed)) modeInfoBlock_t;
extern modeInfoBlock_t* gfxMode;
uint32_t convertColor(uint32_t color);
#endif
+18
View File
@@ -0,0 +1,18 @@
#ifndef _SCREEN_GRAPHICS_H
#define _SCREEN_GRAPHICS_H
#include <stdint.h>
#define rgb(_r,_g,_b) ( ((_r) << 16 | (_g) << 8 | (_b)) & 0x00ffffff )
void drawPixel(int32_t x, int32_t y, uint32_t color);
void drawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color);
void drawRect(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color);
void drawTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, uint32_t color);
void drawCircle(int32_t x, int32_t y, uint16_t radius, uint32_t color);
void boundaryFill(int32_t x, int32_t y, uint32_t fillColor, uint32_t boundaryColor);
void fillRect(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color);
void fillTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, uint32_t color);
void fillCircle(int32_t x, int32_t y, uint16_t radius, uint32_t color);
#endif
+18
View File
@@ -0,0 +1,18 @@
#ifndef _SCREEN_TEXT_H
#define _SCREEN_TEXT_H
#include <stdint.h>
typedef union {
struct {
uint8_t width;
uint8_t height;
};
uint8_t data[4096];
} __attribute__ ((packed)) font_t;
void drawChar(char c, uint32_t x, uint32_t y, uint32_t fgColor, uint32_t bgColor);
void scroll();
void clearScreen(uint32_t bgColor);
#endif
+23
View File
@@ -0,0 +1,23 @@
#ifndef _SCREEN_TTY_H
#define _SCREEN_TTY_H
#include <stdint.h>
#include <stdbool.h>
struct console {
uint32_t x; // X Position of cursor in characters
uint32_t y; // Y Position of cursor in characters
uint32_t bg; // Printed character background
uint32_t fg; // Printed character foreground
uint32_t width; // Width in characters
uint32_t height; // Height in characters
char cursorChar; // Character to use as a cursor
bool hideCursor; // Should we display the cursor
}
extern struct console* console;
int32_t consoleWrite(const void* buf, uint32_t len);
void toggleCursor();
#endif
+19
View File
@@ -0,0 +1,19 @@
#ifndef _SYSCALLS_SYSCALLS_H
#define _SYSCALLS_SYSCALLS_H
#include <cpu/isr.h>
int sys_exit (intFrame_t* iframe);
int sys_read (intFrame_t* iframe);
int sys_write (intFrame_t* iframe);
int sys_open (intFrame_t* iframe);
int sys_close (intFrame_t* iframe);
int sys_seek (intFrame_t* iframe);
int sys_malloc (intFrame_t* iframe);
int sys_free (intFrame_t* iframe);
int sys_sleep (intFrame_t* iframe);
// Main syscall handler/dispatcher
int syscallHandler(intFrame_t* iframe);
#endif