diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6e49ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +obj/ +bin/ diff --git a/bin/os b/bin/os index f89cea2..8790395 100755 Binary files a/bin/os and b/bin/os differ diff --git a/bochs.txt b/bochs.txt new file mode 100644 index 0000000..b4d164d --- /dev/null +++ b/bochs.txt @@ -0,0 +1,3 @@ +vga: extension=vgahw +display_library: x, options="gui_debug" + diff --git a/image.hdd b/image.hdd index 6a15664..b879f01 100644 Binary files a/image.hdd and b/image.hdd differ diff --git a/obj/src/int.c.d b/obj/src/int.c.d index 29e5732..6d7ae4b 100644 --- a/obj/src/int.c.d +++ b/obj/src/int.c.d @@ -1 +1,2 @@ -obj/src/int.c.o: src/int.c +obj/src/int.c.o: src/int.c src/common.h +src/common.h: diff --git a/obj/src/int.c.o b/obj/src/int.c.o index 90a1722..505fbc1 100644 Binary files a/obj/src/int.c.o and b/obj/src/int.c.o differ diff --git a/obj/src/main.c.d b/obj/src/main.c.d index 008772b..b94abe3 100644 --- a/obj/src/main.c.d +++ b/obj/src/main.c.d @@ -1,2 +1,5 @@ -obj/src/main.c.o: src/main.c src/../include/limine.h +obj/src/main.c.o: src/main.c src/../include/limine.h src/common.h \ + src/print.h src/../include/limine.h: +src/common.h: +src/print.h: diff --git a/obj/src/main.c.o b/obj/src/main.c.o index fd9ee5f..3d4f34c 100644 Binary files a/obj/src/main.c.o and b/obj/src/main.c.o differ diff --git a/src/acpi.c b/src/acpi.c new file mode 100644 index 0000000..1c2992b --- /dev/null +++ b/src/acpi.c @@ -0,0 +1,52 @@ +#include "common.h" + +// root system description pointer, points +pstruct acpi_rsdp_header { + u8 signature[8]; + u8 checksum; + u8 OEMID[6]; + u8 revision; + u32 addr32_depr; + // v2 + u32 len; + u64 addr; + u8 extended_checksum; + u8 resv[3]; +}; + +#define RSDT_SIGNATURE "RSDT" +#define XSDT_SIGNATURE "XSDT" + +struct acpi_sdt_header { + u8 signature[4]; + u32 len; + u8 revision; + u8 checksum; + u8 OEMID[6]; + u8 OEM_table_id[8]; + u32 OEM_revision; + u32 creator_id; + u32 creator_revision; +}; + +struct apci_rsdt { + struct acpi_sdt_header header; + u32 addrs[]; +}; + +struct xsdt { + struct acpi_rsdp_header header; + u64 addrs[]; +}; + + +bool rsdp_validate(u8* data, size_t len) { + u32 sum_to_zero = 0; + for(size_t i = 0; i < len; i++) { + sum_to_zero += data[i]; + } + + return (sum_to_zero & 0xFF) == 0; +} + + diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..1ff02c0 --- /dev/null +++ b/src/common.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include +#include + +#define pstruct struct __attribute__((packed)) + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; + diff --git a/src/etc.h b/src/etc.h new file mode 100644 index 0000000..5eb4847 --- /dev/null +++ b/src/etc.h @@ -0,0 +1,12 @@ +#pragma once + +#include "common.h" + +void* memcpy(void* restrict dst, const void* src, size_t n); +void* memset(void* s, int c, size_t n); +void* memmove(void* dst, const void* src, size_t n); +int memcmp(const void* s1, const void* s2, size_t n); + +static int isprint(int ch) { + return (u8)(ch-' ') <= ('~'-' '); +} diff --git a/src/font.h b/src/font.h new file mode 100644 index 0000000..142a3fa --- /dev/null +++ b/src/font.h @@ -0,0 +1,124 @@ +#pragma once + +#include "common.h" + +/* credit due to blit-fonts + +ISC License +Copyright (c) 2018 Andrew Reece + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +*/ + +#define FONT_WIDTH 5 +#define FONT_HEIGHT 6 + +static u32 font[] = { + 0x0, + 0x8021084, + 0x294a, + 0x15f52bea, + 0x8fa38be, + 0x33a22e60, + 0x2e94d8a2, + 0x1084, + 0x10421088, + 0x4421082, + 0xa23880, + 0x471000, + 0x4420000, + 0x70000, + 0xc600000, + 0x2222200, + 0x1d3ad72e, + 0x3e4214c4, + 0x3e22222e, + 0x1d18320f, + 0x210fc888, + 0x1d183c3f, + 0x1d17844c, + 0x222221f, + 0x1d18ba2e, + 0x210f463e, + 0xc6018c0, + 0x4401000, + 0x10411100, + 0xe03800, + 0x4441040, + 0x802322e, + 0x3c1ef62e, + 0x231fc544, + 0x1f18be2f, + 0x3c10862e, + 0x1f18c62f, + 0x3e10bc3f, + 0x210bc3f, + 0x1d1c843e, + 0x2318fe31, + 0x3e42109f, + 0xc94211f, + 0x23149d31, + 0x3e108421, + 0x231ad6bb, + 0x239cd671, + 0x1d18c62e, + 0x217c62f, + 0x30eac62e, + 0x2297c62f, + 0x1d141a2e, + 0x842109f, + 0x1d18c631, + 0x8454631, + 0x375ad631, + 0x22a21151, + 0x8421151, + 0x3e22221f, + 0x1842108c, + 0x20820820, + 0xc421086, + 0x4544, + 0x3e000000, + 0x82, + 0x1c97b000, + 0xe949c21, + 0x1c10b800, + 0x1c94b908, + 0x1c549800, + 0x2211c4c, + 0xe87252e, + 0x12949c21, + 0xc210040, + 0x6210802, + 0x12519521, + 0xc210842, + 0x235aac00, + 0x12949c00, + 0xc949800, + 0x213a526, + 0x3087252e, + 0x2149800, + 0xe837000, + 0xc213c42, + 0xe94a400, + 0x464a400, + 0x155ac400, + 0x36426c00, + 0xe872529, + 0x1e223c00, + 0x1843188c, + 0x8421084, + 0xc463086, + 0x6d800, + 0x3fffffff, +}; diff --git a/src/int.c b/src/int.c index c9a249c..3620f31 100644 --- a/src/int.c +++ b/src/int.c @@ -1,15 +1,15 @@ #include -#define pstruct struct __attribute__((packed)) +#include "common.h" pstruct int_desc { - uint16_t addr_low; - uint16_t selector; - uint8_t ist; - uint8_t flags; - uint16_t addr_mid; - uint32_t addr_high; - uint32_t resv; + u16 addr_low; + u16 selector; + u8 ist; + u8 flags; + u16 addr_mid; + u32 addr_high; + u32 resv; }; enum INT_DESC_FLAGS { @@ -24,9 +24,9 @@ enum INT_DESC_FLAGS { struct int_desc idt[256]; -void set_idt(uint8_t vect, void* handler, uint8_t dpl) { +void set_idt(u8 vect, void* handler, u8 dpl) { struct int_desc* ent = &idt[vect]; - uint64_t addr = (uint64_t) handler; + u64 addr = (u64) handler; ent->addr_low = addr & 0xFFFF; ent->addr_mid = (addr >> 16) & 0xFFFF; ent->addr_mid = addr >> 32; @@ -37,8 +37,8 @@ void set_idt(uint8_t vect, void* handler, uint8_t dpl) { } pstruct idtr { - uint16_t limit; - uint64_t base; + u16 limit; + u64 base; }; #define IDTR_LIMIT (sizeof(struct int_desc)*256 - 1) @@ -46,25 +46,25 @@ pstruct idtr { void load_idt(void* addr) { struct idtr ir = (struct idtr) { .limit = IDTR_LIMIT, - .base = (uint64_t) addr, + .base = (u64) addr, }; asm volatile("lidt %0" :: "m"(ir)); } -extern void draw(uint8_t vect); +extern void draw(u8 vect); struct cpu_ctx { - uint64_t r15, r14, r13, r12, r11, r10, r9, r8, rdi, rsi, rdx, rcx, rbx, rax; + u64 r15, r14, r13, r12, r11, r10, r9, r8, rdi, rsi, rdx, rcx, rbx, rax; - uint64_t vect; - uint64_t err; + u64 vect; + u64 err; - uint64_t iret_rip; - uint64_t iret_cs; - uint64_t iret_flags; - uint64_t iret_rsp; - uint64_t iret_ss; + u64 iret_rip; + u64 iret_cs; + u64 iret_flags; + u64 iret_rsp; + u64 iret_ss; }; struct cpu_ctx* interrupt_dispatch(struct cpu_ctx* ctx) { diff --git a/src/main.c b/src/main.c index 2e75e1b..5100806 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,16 @@ #include #include #include -#include "../include/limine.h" -// credit owed to https://wiki.osdev.org/Limine_Bare_Bones + +#include "../include/limine.h" +#include "common.h" +#include "print.h" + +// credit owed to https://wiki.osdev.org/Limine_Bare_Bones for the limine setup code __attribute__((used, section(".limine_requests"))) -static volatile uint64_t limine_base_revision[] = LIMINE_BASE_REVISION(4); +static volatile u64 limine_base_revision[] = LIMINE_BASE_REVISION(4); __attribute__((used, section(".limine_requests"))) static volatile struct limine_framebuffer_request fb_req = { @@ -15,14 +19,14 @@ static volatile struct limine_framebuffer_request fb_req = { }; __attribute__((used, section(".limine_requests_start"))) -static volatile uint64_t limine_requests_start_marker[] = LIMINE_REQUESTS_START_MARKER; +static volatile u64 limine_requests_start_marker[] = LIMINE_REQUESTS_START_MARKER; __attribute__((used, section(".limine_requests_end"))) -static volatile uint64_t limine_requests_end_marker[] = LIMINE_REQUESTS_END_MARKER; +static volatile u64 limine_requests_end_marker[] = LIMINE_REQUESTS_END_MARKER; void* memcpy(void* restrict dst, const void* src, size_t n) { - uint8_t* restrict pdst = (uint8_t* restrict)dst; - const uint8_t* restrict psrc = (const uint8_t* restrict)src; + u8* restrict pdst = (u8* restrict)dst; + const u8* restrict psrc = (const u8* restrict)src; for(size_t i = 0; i < n; i++) { pdst[i] = psrc[i]; @@ -32,18 +36,18 @@ void* memcpy(void* restrict dst, const void* src, size_t n) { } void* memset(void* s, int c, size_t n) { - uint8_t* p = (uint8_t*) s; + u8* p = (u8*) s; for(size_t i = 0; i < n; i++) { - p[i] = (uint8_t)c; + p[i] = (u8)c; } return s; } void* memmove(void* dst, const void* src, size_t n) { - uint8_t* restrict pdst = (uint8_t* restrict)dst; - const uint8_t* restrict psrc = (const uint8_t* restrict)src; + u8* restrict pdst = (u8* restrict)dst; + const u8* restrict psrc = (const u8* restrict)src; if(src > dst) { for(size_t i = 0; i < n; i++) { @@ -59,8 +63,8 @@ void* memmove(void* dst, const void* src, size_t n) { } int memcmp(const void* s1, const void* s2, size_t n) { - const uint8_t* p1 = (const uint8_t* )s1; - const uint8_t* p2 = (const uint8_t* )s2; + const u8* p1 = (const u8* )s1; + const u8* p2 = (const u8* )s2; for(size_t i = 0; i < n; i++) { if(p1[i] != p2[i]) { @@ -80,10 +84,10 @@ static void hang() { struct limine_framebuffer* fb; -void draw(uint8_t vect) { +void draw(u8 vect) { for(size_t y = 0; y < fb->height; y++) { for(size_t x = 0; x < fb->width; x++) { - volatile uint32_t* fb_ptr = fb->address; + volatile u32* fb_ptr = fb->address; fb_ptr[y * fb->width + x] = ( (x + y) * 0x00010000 | (x ^ y) * 0x00000100 | @@ -108,11 +112,10 @@ void kmain() { init_idt(); - for(size_t i = 0; ~i & (1<<30); i++) { - draw(i&0xFF); - } + print_init(fb); + + print("TEST THIS\nMessage for me please Mr Kernel"); - hang(); } diff --git a/src/print.c b/src/print.c new file mode 100644 index 0000000..f071bd4 --- /dev/null +++ b/src/print.c @@ -0,0 +1,87 @@ +#include "etc.h" +#include "font.h" + +#include "../include/limine.h" + +u32 bg = 0x00000000; +u32 fg = 0xFFFFFFFF; +static size_t row; +static size_t col; +static size_t term_width, term_height; +static size_t fb_width, fb_height; +static volatile u32* fb_scr; + + +#define TAB_STOP 4 + +// buffer at each edge for readability +#define TERM_WIDTH (FONT_WIDTH+2) +#define TERM_HEIGHT (FONT_HEIGHT+2) + + +void print_init(struct limine_framebuffer* fb) { + term_width = fb->width / TERM_WIDTH; + term_height = fb->height / TERM_HEIGHT; + fb_width = fb->width; + fb_height = fb->height; + fb_scr = fb->address; +} + +static void scrollback() { + memmove((void*)fb_scr, (void*)(fb_scr + fb_width*TERM_HEIGHT), fb_width*(fb_height-1)*sizeof(u32)); +} + +void put(int ch) { + ch = (unsigned char)ch; + + if (!isprint(ch)) { + if (ch == '\n') { + col = 0; + row++; + } else if (ch == '\t') { + size_t target = (col + TAB_STOP) & ~(TAB_STOP - 1); + if (target >= term_width) { + col = 0; + row++; + } else { + while (col < target) + put(' '); + } + } + return; + } + + if (col >= term_width) { + col = 0; + row++; + } + + if (row >= term_height) { + scrollback(); + row = term_height - 1; + } + + size_t idx = ch - ' '; + u32 glyph = font[idx]; + + size_t px = col * TERM_WIDTH; + size_t py = row * TERM_HEIGHT; + + for (size_t y = 0; y < TERM_HEIGHT; y++) { + for (size_t x = 0; x < TERM_WIDTH; x++) { + size_t off = (py + y) * fb_width + (px + x); + bool padding = (y == 0 || y == TERM_HEIGHT-1 || x == 0 || x == TERM_WIDTH-1); + fb_scr[off] = ((~glyph & 1) | padding )? bg : (x^y * off + y*term_width); + glyph >>= !padding; + } + } + + col++; +} + + +void print(const char* msg) { + while(*msg) { + put(*(msg++)); + } +} diff --git a/src/print.h b/src/print.h new file mode 100644 index 0000000..d7dd1c2 --- /dev/null +++ b/src/print.h @@ -0,0 +1,10 @@ +#pragma once + + +struct limine_framebuffer; + +void put(int ch); + +void print(const char* msg); + +void print_init(struct limine_framebuffer* fb); diff --git a/src/rep.pl b/src/rep.pl new file mode 100644 index 0000000..13bce89 --- /dev/null +++ b/src/rep.pl @@ -0,0 +1,5 @@ +while(<>) { + s/\buint(..?)_t\b/u\1/g; + s/\bint(..?)_t\b/i\1/g; + print; +}