matos/core/main.c

170 lines
5.5 KiB
C
Raw Normal View History

2019-04-11 22:34:20 +02:00
#include "alloc.h"
2021-08-09 09:26:10 +02:00
#include "allocArea.h"
2018-07-20 15:41:58 +02:00
#include "exception.h"
#include "gdt.h"
#include "idt.h"
#include "interrupt.h"
#include "io.h"
#include "irq.h"
2018-08-09 22:19:34 +02:00
#include "klibc.h"
2020-04-23 00:49:09 +02:00
#include "kthread.h"
2018-08-06 18:41:45 +02:00
#include "mem.h"
#include "multiboot.h"
2018-11-11 23:04:23 +01:00
#include "paging.h"
2018-07-20 15:41:58 +02:00
#include "pit.h"
2018-11-08 21:11:45 +01:00
#include "serial.h"
2020-04-23 00:49:09 +02:00
#include "stack.h"
#include "stdarg.h"
2018-11-08 22:08:27 +01:00
#ifdef RUN_TEST
#include "test.h"
#endif
2020-04-27 23:08:36 +02:00
#include "time.h"
2018-11-11 23:04:23 +01:00
#include "types.h"
2018-07-20 15:41:58 +02:00
#include "vga.h"
2018-08-09 22:19:34 +02:00
#define CHECK_FLAG(flags, bit) ((flags) & (1 << (bit)))
2018-07-20 15:41:58 +02:00
void idleThread(void *arg)
{
2020-04-23 00:49:09 +02:00
(void)arg;
2020-05-03 14:45:26 +02:00
while (1) {
2021-01-25 14:00:06 +01:00
VGAPrintf(GREEN, BLACK, 0, VGA_HEIGHT - 1, "%d", (jiffies / HZ));
2020-05-03 23:13:17 +02:00
kthreadYield();
2020-05-03 14:45:26 +02:00
}
}
2018-08-09 22:19:34 +02:00
// Multiboot information available here :
// https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#kernel_002ec
// https://www.gnu.org/software/grub/manual/multiboot/html_node/Boot-information-format.html#Boot%20information%20format
void kmain(unsigned long magic, unsigned long addr)
2018-07-20 15:41:58 +02:00
{
2021-01-23 00:42:09 +01:00
unsigned long upperMemKB = 0;
int memMapAvailable = 0;
paddr_t lastUsedByMem;
2021-04-10 00:24:02 +02:00
paddr_t firstUsedByMem;
2021-01-22 22:59:45 +01:00
2018-11-08 22:08:27 +01:00
VGASetup(BLACK, GREEN);
printf("Setting up Interruptions\n");
2018-07-20 15:41:58 +02:00
gdtSetup();
idtSetup();
irqSetup();
2020-04-27 23:08:36 +02:00
pitSetup(HZ);
2018-07-20 15:41:58 +02:00
2019-04-15 23:19:14 +02:00
if (magic == MULTIBOOT_BOOTLOADER_MAGIC) { // Get loaded by Grub with mutliboot version 1
multiboot_info_t *mbi = (multiboot_info_t *)addr;
/* Are mem_* valid? */
2018-08-09 22:19:34 +02:00
if (CHECK_FLAG(mbi->flags, 0)) {
printf("mem_lower = %dKiB mem_upper %dKiB\n", mbi->mem_lower, mbi->mem_upper);
2021-01-23 00:42:09 +01:00
upperMemKB = mbi->mem_upper;
}
/* Is boot_device valid? */
2018-08-09 22:19:34 +02:00
if (CHECK_FLAG(mbi->flags, 1)) {
2018-08-06 22:28:45 +02:00
printf("boot_device = %d\n", mbi->boot_device);
}
/* Is the command line passed? */
2018-08-09 22:19:34 +02:00
if (CHECK_FLAG(mbi->flags, 2)) {
2018-08-06 22:28:45 +02:00
printf("cmdline = %s\n", (char *)mbi->cmdline);
}
if (CHECK_FLAG(mbi->flags, 3)) {
multiboot_module_t *mod;
uint32_t i;
printf("mods_count = %d, mods_addr = 0x%x\n", (int)mbi->mods_count,
(int)mbi->mods_addr);
for (i = 0, mod = (multiboot_module_t *)mbi->mods_addr; i < mbi->mods_count;
i++, mod++)
printf(" mod_start = 0x%x, mod_end = 0x%x, cmdline = %s\n",
(unsigned)mod->mod_start, (unsigned)mod->mod_end, (char *)mod->cmdline);
}
if (CHECK_FLAG(mbi->flags, 6)) {
2021-01-23 00:42:09 +01:00
memMapAvailable = 1;
}
2021-04-10 00:24:02 +02:00
printf("Framebuffer from 0x%llx size (%dx%d) pitch %d bpp %d\n", mbi->framebuffer_addr, mbi->framebuffer_width, mbi->framebuffer_height, mbi->framebuffer_pitch, mbi->framebuffer_bpp);
}
2021-01-23 00:42:09 +01:00
if (upperMemKB == 0) {
2018-08-06 22:28:45 +02:00
printf("Cannot get upper phy mem bound. Using default value 32MB\n");
2021-01-23 00:42:09 +01:00
upperMemKB = 32 * 1024;
2018-08-06 18:41:45 +02:00
}
2018-11-08 22:08:27 +01:00
2021-01-23 00:42:09 +01:00
printf("Setting up Mem\n");
2021-04-10 00:24:02 +02:00
memSetup(upperMemKB, &firstUsedByMem, &lastUsedByMem);
2021-01-23 00:42:09 +01:00
if (memMapAvailable) {
multiboot_info_t *mbi = (multiboot_info_t *)addr;
struct multiboot_mmap_entry *mmap = (struct multiboot_mmap_entry *)mbi->mmap_addr;
uint size = mbi->mmap_length / sizeof(struct multiboot_mmap_entry);
2021-01-25 14:00:06 +01:00
2021-01-23 00:42:09 +01:00
pr_devel("mmap buffer at 0x%x with %d entries\n", mbi->mmap_addr, size);
2021-01-25 14:00:06 +01:00
2021-01-23 00:42:09 +01:00
for (uint i = 0; i < size; i++) {
2021-01-23 21:21:13 +01:00
printf(" base_addr 0x%llx, length = 0x%llx, type = 0x%x\n", mmap[i].addr,
mmap[i].len, (uint32_t)mmap[i].type);
// Consider low memory taken (https://wiki.osdev.org/Detecting_Memory_(x86). For
// example by VGA
// Turns out linux and windows do the same !
// https://lore.kernel.org/lkml/MWHPR21MB159330952629D36EEDE706B3D7379@MWHPR21MB1593.namprd21.prod.outlook.com/
if (mmap[i].addr < 0x100000) {
2021-04-10 00:24:02 +02:00
continue;
}
2021-01-23 21:51:00 +01:00
memAddBank(max(mmap[i].addr, (multiboot_uint64_t)lastUsedByMem),
mmap[i].addr + mmap[i].len, mmap[i].type == MULTIBOOT_MEMORY_AVAILABLE);
2021-01-23 00:42:09 +01:00
}
} else {
printf("Cannot get memory Mapping information, using default value\n");
memAddBank(lastUsedByMem, upperMemKB * 1024, 1);
}
2021-04-10 00:24:02 +02:00
printf("%d pages taken by kernel(0x%x->0x%x))\n", (lastUsedByMem - firstUsedByMem)/PAGE_SIZE, firstUsedByMem, lastUsedByMem);
printf("with %d pages taken for memory management\n", (lastUsedByMem - (paddr_t)&__ld_kernel_end)/PAGE_SIZE);
2021-01-25 14:00:06 +01:00
2018-11-12 18:06:46 +01:00
#ifdef RUN_TEST
testPhymem();
#endif
2021-01-23 00:42:09 +01:00
printf("Setting up Pagination\n");
2021-04-10 00:24:02 +02:00
pagingSetup(firstUsedByMem, lastUsedByMem);
for (paddr_t i = VGA_ADDR; i < VGA_ADDR + VGA_WIDTH * VGA_HEIGHT * sizeof(short) ; i += PAGE_SIZE) {
pageMap(i, i, PAGING_MEM_WRITE);
}
2018-08-06 18:41:45 +02:00
2018-08-06 22:28:45 +02:00
printf("Setting up IRQ handlers\n");
2018-07-20 15:41:58 +02:00
irqSetRoutine(IRQ_KEYBOARD, keyboard_handler);
2018-11-08 22:08:27 +01:00
2018-08-06 22:28:45 +02:00
printf("Enabling HW interrupts\n");
2018-07-20 15:41:58 +02:00
// Enabling the HW interrupts
2020-04-29 23:07:01 +02:00
exceptionSetup();
2018-07-20 15:41:58 +02:00
asm volatile("sti\n");
2018-11-08 22:08:27 +01:00
printf("Setting up Serial link (115200)\n");
serialSetup(115200);
2021-01-25 14:00:06 +01:00
printf("Setting up allocation system\n");
2021-08-09 09:26:10 +02:00
areaInit();
//allocSetup();
2021-01-25 14:00:06 +01:00
printf("Setting up thread system\n");
2020-04-23 00:49:09 +02:00
kthreadSetup(_stack_bottom, (_stack_top - _stack_bottom + 1));
2020-05-03 14:45:26 +02:00
kthreadCreate("idle ", idleThread, NULL);
2021-01-25 14:00:06 +01:00
2020-04-23 00:49:09 +02:00
irqSetRoutine(IRQ_TIMER, pit_handler);
2018-11-08 22:08:27 +01:00
#ifdef RUN_TEST
run_test();
#endif
2021-01-23 21:51:00 +01:00
printf("\nSystem init done: ");
{
uint free, used;
memGetStat(&free, &used);
printf("%dKB free %dKB Used\n", free * (PAGE_SIZE / 1024), used * (PAGE_SIZE / 1024));
}
2018-11-08 21:11:45 +01:00
2020-04-23 23:40:16 +02:00
// There is no real caller behind this point
// So finish this by ourself
2020-04-24 00:12:12 +02:00
kthreadExit();
2018-07-20 15:41:58 +02:00
}