diff --git a/makefile b/makefile index 431f544..954fa00 100644 --- a/makefile +++ b/makefile @@ -1,16 +1,18 @@ build: prepare copygrub nasm -felf32 src/bootload/boot.asm -o build/boot.o # entrypoint for grub + nasm -felf32 src/misc/interrupts.asm -o build/intasm.o # interrupt assembly part #nasm -felf32 src/kernel/driverdriver.asm -o build/drivdriv.o # driver code for drivers gcc -c src/kernel/entry.c -o build/kernel.o -ffreestanding -O2 -Wall -Wextra -m32 # kernel gcc -c src/kernel/stdlib.c -o build/stdlib.o -ffreestanding -O2 -Wall -Wextra -m32 # stdlib gcc -c src/kernel/graphics.c -o build/graphics.o -ffreestanding -O2 -Wall -Wextra -m32 # VGA graphics gcc -c src/kernel/tty.c -o build/tty.o -ffreestanding -O2 -Wall -Wextra -m32 # tty + gcc -c src/kernel/interrupts.c -o build/interrupts.o -ffreestanding -O2 -Wall -Wextra -m32 # interrupts, C side gcc -c initrd/vga/main.c -o build/initrddir/vga_graph -ffreestanding -O2 -Wall -Wextra -m32 # initrd/vga driver tar -czf build/initrd build/initrddir/* # build initrd - gcc -z noexecstack -m32 -T linker.ld -o build/linked -ffreestanding -O2 -nostdlib build/tty.o build/graphics.o build/boot.o build/kernel.o build/stdlib.o -fno-pie -fno-pic -no-pie # link + gcc -z noexecstack -m32 -T linker.ld -o build/linked -ffreestanding -O2 -nostdlib build/interrupts.o build/intasm.o build/tty.o build/graphics.o build/boot.o build/kernel.o build/stdlib.o -fno-pie -fno-pic -no-pie # link cp build/linked build/grub/boot/kernel # copy kernel over to grub template cp build/initrd build/grub/boot/JPOS.initrd # copy initrd to grub template diff --git a/src/headers/graphics.h b/src/headers/graphics.h index 3b6bcaf..3ef9131 100644 --- a/src/headers/graphics.h +++ b/src/headers/graphics.h @@ -3,184 +3,32 @@ #include "stdlib.h" #include "tty.h" -// these values are for 320*200*256 typedef struct { uint8_t r; uint8_t g; uint8_t b; } VGA_pixel; -static const VGA_pixel VGA_colors_black = { - 0x00, - 0x00, - 0x00 -}; -static const VGA_pixel VGA_colors_white = { - 0xFF, - 0xFF, - 0xFF -}; +extern const VGA_pixel VGA_colors_black; +extern const VGA_pixel VGA_colors_white; +extern const uint8_t VGA_basic8x8font[128][8]; +extern uint8_t VGA_palette[256][3]; +extern uint8_t* VGA_fbuf; +extern uint8_t VGA_values[]; +extern size_t VGA_sizex; +extern size_t VGA_sizey; +extern size_t VGA_rsx; +extern size_t VGA_rsy; + -static uint8_t VGA_basic8x8font[128][8] = { - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0000 (nul) - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0001 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0002 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0003 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0004 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0005 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0006 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0007 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0008 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0009 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000A - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000B - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000C - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000D - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000E - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000F - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0010 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0011 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0012 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0013 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0014 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0015 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0016 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0017 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0018 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0019 - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001A - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001B - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001C - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001D - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001E - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001F - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0020 (space) - { 0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00}, // U+0021 (!) - { 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0022 (") - { 0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36, 0x00}, // U+0023 (#) - { 0x0C, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x0C, 0x00}, // U+0024 ($) - { 0x00, 0x63, 0x33, 0x18, 0x0C, 0x66, 0x63, 0x00}, // U+0025 (%) - { 0x1C, 0x36, 0x1C, 0x6E, 0x3B, 0x33, 0x6E, 0x00}, // U+0026 (&) - { 0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0027 (') - { 0x18, 0x0C, 0x06, 0x06, 0x06, 0x0C, 0x18, 0x00}, // U+0028 (() - { 0x06, 0x0C, 0x18, 0x18, 0x18, 0x0C, 0x06, 0x00}, // U+0029 ()) - { 0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00}, // U+002A (*) - { 0x00, 0x0C, 0x0C, 0x3F, 0x0C, 0x0C, 0x00, 0x00}, // U+002B (+) - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x06}, // U+002C (,) - { 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00}, // U+002D (-) - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x00}, // U+002E (.) - { 0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00}, // U+002F (/) - { 0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00}, // U+0030 (0) - { 0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00}, // U+0031 (1) - { 0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00}, // U+0032 (2) - { 0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00}, // U+0033 (3) - { 0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00}, // U+0034 (4) - { 0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00}, // U+0035 (5) - { 0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00}, // U+0036 (6) - { 0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00}, // U+0037 (7) - { 0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00}, // U+0038 (8) - { 0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00}, // U+0039 (9) - { 0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x00}, // U+003A (:) - { 0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x06}, // U+003B (;) - { 0x18, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x18, 0x00}, // U+003C (<) - { 0x00, 0x00, 0x3F, 0x00, 0x00, 0x3F, 0x00, 0x00}, // U+003D (=) - { 0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06, 0x00}, // U+003E (>) - { 0x1E, 0x33, 0x30, 0x18, 0x0C, 0x00, 0x0C, 0x00}, // U+003F (?) - { 0x3E, 0x63, 0x7B, 0x7B, 0x7B, 0x03, 0x1E, 0x00}, // U+0040 (@) - { 0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00}, // U+0041 (A) - { 0x3F, 0x66, 0x66, 0x3E, 0x66, 0x66, 0x3F, 0x00}, // U+0042 (B) - { 0x3C, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3C, 0x00}, // U+0043 (C) - { 0x1F, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1F, 0x00}, // U+0044 (D) - { 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x46, 0x7F, 0x00}, // U+0045 (E) - { 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x06, 0x0F, 0x00}, // U+0046 (F) - { 0x3C, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7C, 0x00}, // U+0047 (G) - { 0x33, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x33, 0x00}, // U+0048 (H) - { 0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0049 (I) - { 0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E, 0x00}, // U+004A (J) - { 0x67, 0x66, 0x36, 0x1E, 0x36, 0x66, 0x67, 0x00}, // U+004B (K) - { 0x0F, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7F, 0x00}, // U+004C (L) - { 0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, 0x00}, // U+004D (M) - { 0x63, 0x67, 0x6F, 0x7B, 0x73, 0x63, 0x63, 0x00}, // U+004E (N) - { 0x1C, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00}, // U+004F (O) - { 0x3F, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x0F, 0x00}, // U+0050 (P) - { 0x1E, 0x33, 0x33, 0x33, 0x3B, 0x1E, 0x38, 0x00}, // U+0051 (Q) - { 0x3F, 0x66, 0x66, 0x3E, 0x36, 0x66, 0x67, 0x00}, // U+0052 (R) - { 0x1E, 0x33, 0x07, 0x0E, 0x38, 0x33, 0x1E, 0x00}, // U+0053 (S) - { 0x3F, 0x2D, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0054 (T) - { 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3F, 0x00}, // U+0055 (U) - { 0x33, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00}, // U+0056 (V) - { 0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00}, // U+0057 (W) - { 0x63, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00}, // U+0058 (X) - { 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x0C, 0x1E, 0x00}, // U+0059 (Y) - { 0x7F, 0x63, 0x31, 0x18, 0x4C, 0x66, 0x7F, 0x00}, // U+005A (Z) - { 0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1E, 0x00}, // U+005B ([) - { 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00}, // U+005C (\) - { 0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00}, // U+005D (]) - { 0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00}, // U+005E (^) - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}, // U+005F (_) - { 0x0C, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0060 (`) - { 0x00, 0x00, 0x1E, 0x30, 0x3E, 0x33, 0x6E, 0x00}, // U+0061 (a) - { 0x07, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3B, 0x00}, // U+0062 (b) - { 0x00, 0x00, 0x1E, 0x33, 0x03, 0x33, 0x1E, 0x00}, // U+0063 (c) - { 0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6E, 0x00}, // U+0064 (d) - { 0x00, 0x00, 0x1E, 0x33, 0x3f, 0x03, 0x1E, 0x00}, // U+0065 (e) - { 0x1C, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0F, 0x00}, // U+0066 (f) - { 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x1F}, // U+0067 (g) - { 0x07, 0x06, 0x36, 0x6E, 0x66, 0x66, 0x67, 0x00}, // U+0068 (h) - { 0x0C, 0x00, 0x0E, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0069 (i) - { 0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E}, // U+006A (j) - { 0x07, 0x06, 0x66, 0x36, 0x1E, 0x36, 0x67, 0x00}, // U+006B (k) - { 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+006C (l) - { 0x00, 0x00, 0x33, 0x7F, 0x7F, 0x6B, 0x63, 0x00}, // U+006D (m) - { 0x00, 0x00, 0x1F, 0x33, 0x33, 0x33, 0x33, 0x00}, // U+006E (n) - { 0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00}, // U+006F (o) - { 0x00, 0x00, 0x3B, 0x66, 0x66, 0x3E, 0x06, 0x0F}, // U+0070 (p) - { 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x78}, // U+0071 (q) - { 0x00, 0x00, 0x3B, 0x6E, 0x66, 0x06, 0x0F, 0x00}, // U+0072 (r) - { 0x00, 0x00, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x00}, // U+0073 (s) - { 0x08, 0x0C, 0x3E, 0x0C, 0x0C, 0x2C, 0x18, 0x00}, // U+0074 (t) - { 0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6E, 0x00}, // U+0075 (u) - { 0x00, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00}, // U+0076 (v) - { 0x00, 0x00, 0x63, 0x6B, 0x7F, 0x7F, 0x36, 0x00}, // U+0077 (w) - { 0x00, 0x00, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x00}, // U+0078 (x) - { 0x00, 0x00, 0x33, 0x33, 0x33, 0x3E, 0x30, 0x1F}, // U+0079 (y) - { 0x00, 0x00, 0x3F, 0x19, 0x0C, 0x26, 0x3F, 0x00}, // U+007A (z) - { 0x38, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x38, 0x00}, // U+007B ({) - { 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00}, // U+007C (|) - { 0x07, 0x0C, 0x0C, 0x38, 0x0C, 0x0C, 0x07, 0x00}, // U+007D (}) - { 0x6E, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+007E (~) - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // U+007F -}; -static uint8_t VGA_palette[256][3]; void VGA_setchar(virtualConsole* con, uint8_t x, uint8_t y, char val); void VGA_renderchar(virtualConsole* con); void _VGA_renchar(uint8_t x, uint8_t y, char val, uint8_t fcolor[3], uint8_t bcolor[3]); -static uint8_t* VGA_fbuf = (unsigned char*)0xA0000; -static uint8_t VGA_values[] = { // THIS. THIS IS CANCER. - // MISC - 0x63, - - // SEQ - 0x03, 0x01, 0x0F, 0x00, 0x0E, - - // CRTC - 0x5F,0x4F,0x50,0x82,0x54,0x80,0xBF,0x1F, - 0x00,0x41,0x00,0x00,0x00,0x00,0x00,0x00, - 0x9C,0x0E,0x8F,0x28,0x40,0x96,0xB9,0xA3, - 0xFF, - - // GC - 0x00,0x00,0x00,0x00,0x00,0x40,0x05,0x0F,0xFF, - - // AC - 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, - 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F, - 0x41,0x00,0x0F,0x00,0x00 -}; void VGA_switch(uint8_t *regs); inline size_t VGA_offset(uint16_t x, uint16_t y) { - return x+(y*320); + return x+(y*VGA_rsx); } void VGA_setcol(uint8_t index, uint8_t r, uint8_t g, uint8_t b); void VGA_setpal(uint8_t palette[256][3]); void VGA_setpix(uint16_t x, uint16_t y, VGA_pixel pix); -void VGA_kstrrend(virtualConsole* con); \ No newline at end of file +void VGA_kstrrend(virtualConsole* con); +void VGA_clear(VGA_pixel color); \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 2ae2979..0df0755 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -20,6 +20,25 @@ #define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */ #define ICW4_SFNM 0x10 /* Special fully nested (not) */ #define CASCADE_IRQ 2 +#define IDT_MAX_DESCRIPTORS 32 + +typedef struct { + uint16_t isr_low; // The lower 16 bits of the ISR's address + uint16_t kernel_cs; // The GDT segment selector that the CPU will load into CS before calling the ISR + uint8_t reserved; // Set to zero + uint8_t attributes; // Type and attributes; see the IDT page + uint16_t isr_high; // The higher 16 bits of the ISR's address +} __attribute__((packed)) idt_entry_t; +typedef struct { + uint16_t limit; + uint32_t base; +} __attribute__((packed)) idtr_t; +typedef struct { + uint32_t ds; + uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; + uint32_t int_no, err_code; + uint32_t eip, cs, eflags, useresp, ss; +} INT_registers_t; inline void PIC_sendEOI(uint8_t irq) { if (irq >= 8) { // slave also needs EOI @@ -27,4 +46,17 @@ inline void PIC_sendEOI(uint8_t irq) { } outb(PIC1_COMMAND,PIC_EOI); } -extern void PIC_remap(int offset1, int offset2); \ No newline at end of file +void PIC_remap(int offset1, int offset2); +__attribute__((noreturn)) +void INT_exhand(INT_registers_t regs); +void INT_IRQ(uint8_t IRQ, INT_registers_t regs); +void idt_set_descriptor(uint8_t vector, void* isr, uint8_t flags); +void idt_init(); + +__attribute__((aligned(0x10))) +extern idt_entry_t idt[256]; +extern idtr_t idtr; +extern void* isr_stub_table[]; +extern void* irq_stub_table[]; +extern uint8_t INT_vectors[IDT_MAX_DESCRIPTORS]; +extern char *exception_messages[]; \ No newline at end of file diff --git a/src/headers/stdlib.h b/src/headers/stdlib.h index aaf48a1..0817088 100644 --- a/src/headers/stdlib.h +++ b/src/headers/stdlib.h @@ -4,7 +4,10 @@ uint32_t strlen32(char* str); void strrev32(char* str); uint8_t uitoa32(uint32_t x, char* str); +void strcon(char* x, char* y); void memcopy(void* dest, void* src, size_t size); +void memset(void* dest, size_t size, uint8_t value); +__attribute__((noreturn)) void kpanic(char* msg); static inline void outb(uint16_t port, uint8_t val) { __asm__ volatile ( "outb %b0, %w1" : : "a"(val), "Nd"(port) : "memory"); diff --git a/src/headers/tty.h b/src/headers/tty.h index 4d5f79a..126c766 100644 --- a/src/headers/tty.h +++ b/src/headers/tty.h @@ -1,14 +1,20 @@ #pragma once #include "stddef.h" +#define CON_sizex 40 +#define CON_sizey 25 + typedef struct { void* FU_kupdbuf; char kstdout[8192]; uint16_t kstdout_count; uint8_t FU_forecolor[3]; uint8_t FU_backcolor[3]; - char charbuf[40][25]; + char charbuf[CON_sizex][CON_sizey]; uint8_t enableRendering; + + uint16_t keyBuf[1024]; + size_t keyBufSize; } virtualConsole; typedef void (*FU_kupdbuf_t)(virtualConsole*); @@ -16,8 +22,9 @@ typedef void (*FU_kupdbuf_t)(virtualConsole*); extern virtualConsole consoles[consolesAmount]; extern uint8_t currentConsole; -void con_kputchar(virtualConsole* con, char x); +void con_kputchar(virtualConsole* con, char x, uint8_t newrender); void con_kputstr(virtualConsole* con, char* x); void con_init(virtualConsole* con); void kputchar(char x); -void kputstr(char* x); \ No newline at end of file +void kputstr(char* x); +void con_render(); \ No newline at end of file diff --git a/src/kernel/entry.c b/src/kernel/entry.c index 7d4d334..d67c0a1 100644 --- a/src/kernel/entry.c +++ b/src/kernel/entry.c @@ -3,6 +3,7 @@ #include "../headers/stdlib.h" #include "../headers/graphics.h" #include "../headers/tty.h" +#include "../headers/interrupts.h" struct multiboot_info { uint32_t total_size; @@ -41,11 +42,7 @@ void kernel_main(struct multiboot_info* header, uint32_t magic) { VGA_switch(VGA_values); kputstr("OK\n"); - for (uint16_t x=0;x<320;x++) { - for (uint16_t y=0;y<200;y++) { - VGA_setpix(x,y,(VGA_pixel){0xFF,0xFF,0xFF}); - } - } + VGA_clear((VGA_pixel){0xFF,0xFF,0xFF}); kputstr("Setting up stdout rendering: "); for (uint8_t i=0;i) + { 0x1E, 0x33, 0x30, 0x18, 0x0C, 0x00, 0x0C, 0x00}, // U+003F (?) + { 0x3E, 0x63, 0x7B, 0x7B, 0x7B, 0x03, 0x1E, 0x00}, // U+0040 (@) + { 0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00}, // U+0041 (A) + { 0x3F, 0x66, 0x66, 0x3E, 0x66, 0x66, 0x3F, 0x00}, // U+0042 (B) + { 0x3C, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3C, 0x00}, // U+0043 (C) + { 0x1F, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1F, 0x00}, // U+0044 (D) + { 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x46, 0x7F, 0x00}, // U+0045 (E) + { 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x06, 0x0F, 0x00}, // U+0046 (F) + { 0x3C, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7C, 0x00}, // U+0047 (G) + { 0x33, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x33, 0x00}, // U+0048 (H) + { 0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0049 (I) + { 0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E, 0x00}, // U+004A (J) + { 0x67, 0x66, 0x36, 0x1E, 0x36, 0x66, 0x67, 0x00}, // U+004B (K) + { 0x0F, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7F, 0x00}, // U+004C (L) + { 0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, 0x00}, // U+004D (M) + { 0x63, 0x67, 0x6F, 0x7B, 0x73, 0x63, 0x63, 0x00}, // U+004E (N) + { 0x1C, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00}, // U+004F (O) + { 0x3F, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x0F, 0x00}, // U+0050 (P) + { 0x1E, 0x33, 0x33, 0x33, 0x3B, 0x1E, 0x38, 0x00}, // U+0051 (Q) + { 0x3F, 0x66, 0x66, 0x3E, 0x36, 0x66, 0x67, 0x00}, // U+0052 (R) + { 0x1E, 0x33, 0x07, 0x0E, 0x38, 0x33, 0x1E, 0x00}, // U+0053 (S) + { 0x3F, 0x2D, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0054 (T) + { 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3F, 0x00}, // U+0055 (U) + { 0x33, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00}, // U+0056 (V) + { 0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00}, // U+0057 (W) + { 0x63, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00}, // U+0058 (X) + { 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x0C, 0x1E, 0x00}, // U+0059 (Y) + { 0x7F, 0x63, 0x31, 0x18, 0x4C, 0x66, 0x7F, 0x00}, // U+005A (Z) + { 0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1E, 0x00}, // U+005B ([) + { 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00}, // U+005C (\) + { 0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00}, // U+005D (]) + { 0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00}, // U+005E (^) + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}, // U+005F (_) + { 0x0C, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0060 (`) + { 0x00, 0x00, 0x1E, 0x30, 0x3E, 0x33, 0x6E, 0x00}, // U+0061 (a) + { 0x07, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3B, 0x00}, // U+0062 (b) + { 0x00, 0x00, 0x1E, 0x33, 0x03, 0x33, 0x1E, 0x00}, // U+0063 (c) + { 0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6E, 0x00}, // U+0064 (d) + { 0x00, 0x00, 0x1E, 0x33, 0x3f, 0x03, 0x1E, 0x00}, // U+0065 (e) + { 0x1C, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0F, 0x00}, // U+0066 (f) + { 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x1F}, // U+0067 (g) + { 0x07, 0x06, 0x36, 0x6E, 0x66, 0x66, 0x67, 0x00}, // U+0068 (h) + { 0x0C, 0x00, 0x0E, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0069 (i) + { 0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E}, // U+006A (j) + { 0x07, 0x06, 0x66, 0x36, 0x1E, 0x36, 0x67, 0x00}, // U+006B (k) + { 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+006C (l) + { 0x00, 0x00, 0x33, 0x7F, 0x7F, 0x6B, 0x63, 0x00}, // U+006D (m) + { 0x00, 0x00, 0x1F, 0x33, 0x33, 0x33, 0x33, 0x00}, // U+006E (n) + { 0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00}, // U+006F (o) + { 0x00, 0x00, 0x3B, 0x66, 0x66, 0x3E, 0x06, 0x0F}, // U+0070 (p) + { 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x78}, // U+0071 (q) + { 0x00, 0x00, 0x3B, 0x6E, 0x66, 0x06, 0x0F, 0x00}, // U+0072 (r) + { 0x00, 0x00, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x00}, // U+0073 (s) + { 0x08, 0x0C, 0x3E, 0x0C, 0x0C, 0x2C, 0x18, 0x00}, // U+0074 (t) + { 0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6E, 0x00}, // U+0075 (u) + { 0x00, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00}, // U+0076 (v) + { 0x00, 0x00, 0x63, 0x6B, 0x7F, 0x7F, 0x36, 0x00}, // U+0077 (w) + { 0x00, 0x00, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x00}, // U+0078 (x) + { 0x00, 0x00, 0x33, 0x33, 0x33, 0x3E, 0x30, 0x1F}, // U+0079 (y) + { 0x00, 0x00, 0x3F, 0x19, 0x0C, 0x26, 0x3F, 0x00}, // U+007A (z) + { 0x38, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x38, 0x00}, // U+007B ({) + { 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00}, // U+007C (|) + { 0x07, 0x0C, 0x0C, 0x38, 0x0C, 0x0C, 0x07, 0x00}, // U+007D (}) + { 0x6E, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+007E (~) + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // U+007F +}; +uint8_t* VGA_fbuf = (uint8_t*)0xA0000; +uint8_t VGA_values[] = { // THIS. THIS IS CANCER. + // MISC + 0x63, + + // SEQ + 0x03, 0x01, 0x0F, 0x00, 0x0E, + + // CRTC + 0x5F,0x4F,0x50,0x82,0x54,0x80,0xBF,0x1F, + 0x00,0x41,0x00,0x00,0x00,0x00,0x00,0x00, + 0x9C,0x0E,0x8F,0x28,0x40,0x96,0xB9,0xA3, + 0xFF, + + // GC + 0x00,0x00,0x00,0x00,0x00,0x40,0x05,0x0F,0xFF, + + // AC + 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, + 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F, + 0x41,0x00,0x0F,0x00,0x00 +}; +size_t VGA_sizex = 320; +size_t VGA_sizey = 200; +size_t VGA_rsx = 320; +size_t VGA_rsy = 200; inline uint8_t _VGA_nearestmul(uint8_t val, uint8_t multiple) { if (val%multiple == 0) {return val;} return val + multiple - (val%multiple); } void _VGA_renchar(uint8_t x, uint8_t y, char val, uint8_t fcolor[3], uint8_t bcolor[3]) { - if (val > 127) { // out of bounds, no ascii character + if ((unsigned char)val > 127) { // out of bounds, no ascii character return; } for (uint8_t px=0;px<8;px++) { for (uint8_t py=0;py<8;py++) { - VGA_setpix(x*8+px,y*8+py,(VGA_basic8x8font[val][py] >> px) & 1 ? *(VGA_pixel*)fcolor : *(VGA_pixel*)bcolor); + size_t exX,exY; + exX = CON_sizex*8; + exY = CON_sizey*8; + if (exX == VGA_sizex && exY == VGA_sizey) { + VGA_setpix(x*8+px,y*8+py,(VGA_basic8x8font[(unsigned char)val][py] >> px) & 1 ? *(VGA_pixel*)fcolor : *(VGA_pixel*)bcolor); + } else { // calculate the difference and based on that scale the font. + double nX,nY,sX,sY; + nX = VGA_sizex/CON_sizex; // example: 320/40 = 8, the size of our font. perfect! + nY = VGA_sizey/CON_sizey; // same here: 200/25 = 8 + exX = 8; // maybe change this later incase the font ever is transitioned to a dynamic one. + exY = 8; + sX = nX/exX; + sY = nY/exY; + + for (size_t i=0;i> px) & 1 ? *(VGA_pixel*)fcolor : *(VGA_pixel*)bcolor); + } + } + } } } } void VGA_renderchar(virtualConsole* con) { - for (uint8_t x=0;x<40;x++) { - for (uint8_t y=0;y<25;y++) { + for (size_t x=0;xcharbuf[x][y], con->FU_forecolor, con->FU_backcolor); } } @@ -111,18 +299,19 @@ void VGA_setchar(virtualConsole* con, uint8_t x, uint8_t y, char val) { con->charbuf[x][y] = val; } void VGA_kstrrend(virtualConsole* con) { + VGA_clear((VGA_pixel){0x00,0x00,0x00}); size_t count = con->kstdout_count; char* buf = con->kstdout; - while (count > (40*25)) { - count -= (40*25); - buf += (40*25); + while (count > (CON_sizex*CON_sizey)) { + count -= (CON_sizex*CON_sizey); + buf += (CON_sizex*CON_sizey); } uint16_t x = 0; uint16_t y = 0; for (uint16_t i=0;i= 40) { + if (x >= CON_sizex) { x = 0; y ++; } @@ -158,4 +347,22 @@ void VGA_setpix(uint16_t x, uint16_t y, VGA_pixel pix) { } } VGA_fbuf[VGA_offset(x,y)] = i; +} +void VGA_clear(VGA_pixel pix) { + uint16_t i; // color index finding code from setpix. + // first we round each r, g and b value to the nearest multiple of 0x33 + uint8_t color[3] = {pix.r, pix.g, pix.b}; + color[0] = _VGA_nearestmul(color[0], 0x33); + color[1] = _VGA_nearestmul(color[1], 0x33); + color[2] = _VGA_nearestmul(color[2], 0x33); + // then find the matching index in palette + for (i=0;i<256;i++) { + if (VGA_palette[i][0] == color[0] && + VGA_palette[i][1] == color[1] && + VGA_palette[i][2] == color[2] + ) { + break; + } + } + memset(VGA_fbuf, VGA_rsx*VGA_rsy, i); } \ No newline at end of file diff --git a/src/kernel/interrupts.c b/src/kernel/interrupts.c index b66e744..a79ea21 100644 --- a/src/kernel/interrupts.c +++ b/src/kernel/interrupts.c @@ -1,5 +1,46 @@ #include "../headers/interrupts.h" +__attribute__((aligned(0x10))) +idt_entry_t idt[256] = {}; +idtr_t idtr = {}; +char *exception_messages[] = { + "Division By Zero", + "Debug", + "Non Maskable Interrupt", + "Breakpoint", + "Into Detected Overflow", + "Out of Bounds", + "Invalid Opcode", + "No Coprocessor", + + "Double Fault", + "Coprocessor Segment Overrun", + "Bad TSS", + "Segment Not Present", + "Stack Fault", + "General Protection Fault", + "Page Fault", + "Unknown Interrupt", + + "Coprocessor Fault", + "Alignment Check", + "Machine Check", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved" +}; + void PIC_remap(int offset1, int offset2) { outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); // starts the initialization sequence (in cascade mode) outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4); @@ -14,4 +55,33 @@ void PIC_remap(int offset1, int offset2) { // Unmask both PICs. outb(PIC1_DATA, 0); outb(PIC2_DATA, 0); +} +void idt_set_descriptor(uint8_t vector, void* isr, uint8_t flags) { + idt_entry_t* descriptor = &idt[vector]; + + descriptor->isr_low = (uint32_t)isr & 0xFFFF; + descriptor->kernel_cs = 0x08; + descriptor->attributes = flags; + descriptor->isr_high = (uint32_t)isr >> 16; + descriptor->reserved = 0; +} +void INT_exhand(INT_registers_t regs) { + char msg[128] = "Exception occured.\nCode: "; + char temp[8] = ""; + uitoa32(regs.int_no, temp); + strcon(msg,temp); + kpanic(msg); +} +void INT_IRQ(uint8_t IRQ, INT_registers_t regs) { + +} +void idt_init() { + idtr.base = (uint32_t)&idt[0]; + idtr.limit = (uint16_t)sizeof(idt_entry_t) * IDT_MAX_DESCRIPTORS - 1; + + for (uint8_t vector = 0; vector < 32; vector++) { + idt_set_descriptor(vector, isr_stub_table[vector], 0x8E); + } + + __asm__ volatile ("lidt %0" : : "m"(idtr)); // load the new IDT } \ No newline at end of file diff --git a/src/kernel/stdlib.c b/src/kernel/stdlib.c index 6c1ca8b..fb1b145 100644 --- a/src/kernel/stdlib.c +++ b/src/kernel/stdlib.c @@ -30,8 +30,9 @@ void memcopy(void* dest, void* src, size_t size) { } } void kpanic(char* msg) { - for (uint16_t x=0;x<320;x++) { - for (uint16_t y=0;y<200;y++) { + asm volatile("cli"); + for (uint16_t x=0;xkstdout_count == 8192) { for (uint32_t i=0;i<8191;i++) { con->kstdout[i] = con->kstdout[i+1]; @@ -13,17 +13,16 @@ void con_kputchar(virtualConsole* con, char x) { con->kstdout_count --; } con->kstdout[con->kstdout_count++] = x; - if (&consoles[currentConsole] == con && con->enableRendering) { // current console - if (con->FU_kupdbuf != 0x00) { - ((FU_kupdbuf_t)con->FU_kupdbuf)(con); - } else { - kpanic("virtualConsole failure:\nFU_kupdbuf is 0x00."); - } - } + if (newrender && &consoles[currentConsole] == con && con->enableRendering) { + con_render(); + } } void con_kputstr(virtualConsole* con, char* x) { while (*x != 0) { - con_kputchar(con, *x++); + con_kputchar(con, *x++, 0); + } + if (&consoles[currentConsole] == con && con->enableRendering) { // current console + con_render(); } } void con_init(virtualConsole* con) { @@ -36,10 +35,19 @@ void con_init(virtualConsole* con) { con->FU_backcolor[1] = 0x00; con->FU_backcolor[2] = 0x00; con->enableRendering = 0; + con->keyBufSize = 0; } void kputchar(char x) { - con_kputchar(&consoles[currentConsole], x); + con_kputchar(&consoles[currentConsole], x, 1); } void kputstr(char* x) { con_kputstr(&consoles[currentConsole], x); +} +void con_render() { + virtualConsole* con = &consoles[currentConsole]; + if (con->FU_kupdbuf != 0x00) { + ((FU_kupdbuf_t)con->FU_kupdbuf)(con); + } else { + kpanic("virtualConsole failure:\nFU_kupdbuf is 0x00."); + } } \ No newline at end of file diff --git a/src/misc/interrupts.asm b/src/misc/interrupts.asm new file mode 100644 index 0000000..e8769bd --- /dev/null +++ b/src/misc/interrupts.asm @@ -0,0 +1,125 @@ +extern INT_exhand +extern INT_IRQ + +%macro isr_err_stub 1 +isr_stub_%+%1: + push 0 + push %1 + + pusha + mov ax, ds + push eax + mov ax, 0x08 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + + call INT_exhand + + pop eax + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + popa + add esp, 8 + sti + iret +%endmacro + +%macro isr_no_err_stub 1 +isr_stub_%+%1: + ;call INT_exhand + sti + iret +%endmacro + +%macro irq_stub 1 +irq_stub_%+%1: + push 0 + push %1 + + pusha + mov ax, ds + push eax + mov ax, 0x08 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + call INT_IRQ + pop ebx + mov ds, bx + mov es, bx + mov fs, bx + mov gs, bx + popa + add esp, 8 +%endmacro + +isr_no_err_stub 0 +isr_no_err_stub 1 +isr_no_err_stub 2 +isr_no_err_stub 3 +isr_no_err_stub 4 +isr_no_err_stub 5 +isr_no_err_stub 6 +isr_no_err_stub 7 +isr_err_stub 8 +isr_no_err_stub 9 +isr_err_stub 10 +isr_err_stub 11 +isr_err_stub 12 +isr_err_stub 13 +isr_err_stub 14 +isr_no_err_stub 15 +isr_no_err_stub 16 +isr_err_stub 17 +isr_no_err_stub 18 +isr_no_err_stub 19 +isr_no_err_stub 20 +isr_no_err_stub 21 +isr_no_err_stub 22 +isr_no_err_stub 23 +isr_no_err_stub 24 +isr_no_err_stub 25 +isr_no_err_stub 26 +isr_no_err_stub 27 +isr_no_err_stub 28 +isr_no_err_stub 29 +isr_err_stub 30 +isr_no_err_stub 31 + +irq_stub 0 +irq_stub 1 +irq_stub 2 +irq_stub 3 +irq_stub 4 +irq_stub 5 +irq_stub 6 +irq_stub 7 +irq_stub 8 +irq_stub 9 +irq_stub 10 +irq_stub 11 +irq_stub 12 +irq_stub 13 +irq_stub 14 +irq_stub 15 + +global isr_stub_table +isr_stub_table: +%assign i 0 +%rep 32 + dd isr_stub_%+i +%assign i i+1 +%endrep + +global irq_stub_table +irq_stub_table: +%assign i 0 +%rep 16 + dd irq_stub_%+i +%assign i i+1 +%endrep \ No newline at end of file