HhhOS/arch/x86_64/kernel.c
2021-08-23 14:05:04 +03:00

211 lines
7.4 KiB
C

#include <types.h>
#include <stivale2.h>
#include <std/inline.h>
#include <std/util.h>
#include <stdbool.h>
#include <std/stdio.h>
#include <drivers/idt/isr.h>
#include <drivers/keyboard/keyboard.h>
#include <drivers/pic/pic.h>
#include <drivers/device/device.h>
#include <drivers/ide/ide.h>
#include <framebuffer.h>
#include <psf.h>
#include <paging.h>
#include <mem/mem.h>
static u8 stack[16384];
bool is_running = true;
int keyboard_descriptor = 0;
int terminal_descriptor = 0;
// stivale2 uses a linked list of tags for both communicating TO the
// bootloader, or receiving info FROM it. More information about these tags
// is found in the stivale2 specification.
// We are now going to define a framebuffer header tag, which is mandatory when
// using the stivale2 terminal.
// This tag tells the bootloader that we want a graphical framebuffer instead
// of a CGA-compatible text mode. Omitting this tag will make the bootloader
// default to text mode, if available.
static struct stivale2_header_tag_framebuffer framebuffer_hdr_tag = {
// Same as above.
.tag = {
.identifier = STIVALE2_HEADER_TAG_FRAMEBUFFER_ID,
.next = 0
},
// set all the framebuffer specifics to 0 for it to pick the best it can.
.framebuffer_width = 800,
.framebuffer_height = 600,
.framebuffer_bpp = 32
};
// The stivale2 specification says we need to define a "header structure".
// This structure needs to reside in the .stivale2hdr ELF section in order
// for the bootloader to find it. We use this __attribute__ directive to
// tell the compiler to put the following structure in said section.
__attribute__((section(".stivale2hdr"), used))
static struct stivale2_header stivale_hdr = {
// The entry_point member is used to specify an alternative entry
// point that the bootloader should jump to instead of the executable's
// ELF entry point. We do not care about that so we leave it zeroed.
.entry_point = 0,
// Let's tell the bootloader where our stack is.
// We need to add the sizeof(stack) since in x86(_64) the stack grows
// downwards.
.stack = (uintptr_t)stack + sizeof(stack),
// Bit 1, if set, causes the bootloader to return to us pointers in the
// higher half, which we likely want.
.flags = (1 << 1),
// This header structure is the root of the linked list of header tags and
// points to the first one in the linked list.
.tags = (uintptr_t)&framebuffer_hdr_tag
};
void *stivale2_get_tag(struct stivale2_struct *stivale2_struct, u64 id) {
struct stivale2_tag *current_tag = (void *)stivale2_struct->tags;
for (;;) {
// If the tag pointer is NULL (end of linked list), we did not find
// the tag. Return NULL to signal this.
if (current_tag == NULL) {
return NULL;
}
// Check whether the identifier matches. If it does, return a pointer
// to the matching tag.
if (current_tag->identifier == id) {
return current_tag;
}
// Get a pointer to the next tag in the linked list and repeat.
current_tag = (void *)current_tag->next;
}
}
framebuffer_t *framebuffer;
extern usize _kernel_start;
extern usize _kernel_end;
struct test_struct {
unsigned int something;
int hello;
};
void _start(struct stivale2_struct *stivale2_struct) {
struct stivale2_struct_tag_framebuffer *tag_fb = stivale2_get_tag(stivale2_struct, STIVALE2_STRUCT_TAG_FRAMEBUFFER_ID);
struct stivale2_struct_tag_memmap *tag_mmap = stivale2_get_tag(stivale2_struct, STIVALE2_STRUCT_TAG_MEMMAP_ID);
if (tag_fb == NULL || tag_mmap == NULL) {
printf("Requested stivale2 tags were not found, hanging...");
for (;;) {
asm ("hlt");
}
}
framebuffer->address = (u8*)tag_fb->framebuffer_addr;
framebuffer->width = tag_fb->framebuffer_width;
framebuffer->height = tag_fb->framebuffer_height;
framebuffer->depth = tag_fb->framebuffer_bpp;
framebuffer->pitch = tag_fb->framebuffer_pitch;
framebuffer->pixelwidth = tag_fb->framebuffer_bpp / 8;
terminal_descriptor = terminal_initialize(framebuffer);
printf("Terminal initialized, descriptor: %d\n", terminal_descriptor);
printf("Framebuffer | addr: %#lX, width: %d, height: %d, depth: %d, pitch: %d\n", (usize)framebuffer->address, framebuffer->width, framebuffer->height, framebuffer->depth, framebuffer->pitch);
u64 total_memory = 0;
for (u64 i = 0; i < tag_mmap->entries; i++) {
struct stivale2_mmap_entry entry = tag_mmap->memmap[i];
char *entry_name;
switch (entry.type) {
case STIVALE2_MMAP_USABLE: entry_name = "Usable"; break;
case STIVALE2_MMAP_RESERVED: entry_name = "Reserved"; break;
case STIVALE2_MMAP_ACPI_RECLAIMABLE: entry_name = "ACPI Reclaimable"; break;
case STIVALE2_MMAP_ACPI_NVS: entry_name = "ACPI NVS"; break;
case STIVALE2_MMAP_BAD_MEMORY: entry_name = "Bad Memory"; break;
case STIVALE2_MMAP_BOOTLOADER_RECLAIMABLE: entry_name = "Bootloader Reclaimable"; break;
case STIVALE2_MMAP_KERNEL_AND_MODULES: entry_name = "Kernel and Modules"; break;
case STIVALE2_MMAP_FRAMEBUFFER: entry_name = "Framebuffer"; break;
default: entry_name = "Unknown";
}
total_memory += entry.length;
printf("Memory map entry | name: %s, base: %#lX, length: %ld KiB\n", entry_name, entry.base, entry.length / 1024);
}
printf("Total memory: %ld KiB\n", total_memory / 1024);
//printf("\"\"\"Used\"\"\" memory: %ld B\n", (_kernel_end - _kernel_start));
printf("Preparing interrupts... ");
pic_init();
isr_install();
printf("done\n");
/*
printf("Preparing IDE driver... \n");
printf("Searching for IDE devices... \n");
ide_init();
*/
fillrect(framebuffer, 750, 550, 25, 25, 0x00FFD5FF);
keyboard_descriptor = keyboard_init();
printf("Keyboard ready, descriptor: %d\n", keyboard_descriptor);
printf("Are interrupts enabled? ");
if (are_interrupts_enabled() == 1) {
printf("yes\n");
} else {
printf("no\n");
}
usize start_mem_addr = (usize)_start;
printf("_start memory address: %#lX\n", start_mem_addr);
mem_populate_blocks(start_mem_addr + 0x42069);
/* TODO: only enable this in debug (or just handle testing in a better way) */
if (mem_test_mm()) {
printf("memory manager test passed\n");
}
printf("----------\n");
printf("hello yes");
while (is_running) {
printf("\n> ");
while (true) {
usize scancode = (usize)read(keyboard_descriptor, NULL);
if (!scancode) continue;
bool special = true;
bool finished = false;
switch (scancode) {
case 72:
terminal_row--;
break;
case 75:
terminal_column--;
break;
case 77:
terminal_column++;
break;
case 80:
terminal_row++;
break;
case 0x1C:
finished = true;
break;
default:
special = false;
}
if (finished) break;
if (!special) putchar(keyboard_get_key_from_scancode(scancode));
terminal_updatecursor();
}
}
}