matos/core/main.c

425 lines
13 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"
2021-10-03 23:19:00 +02:00
#include "ata.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"
2021-11-05 23:02:23 +01:00
#include "kernel.h"
2021-10-07 21:23:32 +02:00
#include "keyboard.h"
2018-08-09 22:19:34 +02:00
#include "klibc.h"
2018-08-06 18:41:45 +02:00
#include "mem.h"
2021-10-30 00:28:31 +02:00
#include "mmuContext.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"
2021-10-30 15:43:40 +02:00
#include "process.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"
2021-11-03 23:54:36 +01:00
#include "swintr.h"
2021-11-05 23:02:23 +01:00
#include "thread.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));
2021-10-30 14:18:21 +02:00
threadYield();
2020-05-03 14:45:26 +02:00
}
}
2021-11-05 23:02:23 +01:00
/**
* Make sure the program is in a valid ELF format, map it into memory,
* and return the address of its entry point (ie _start function)
*
* @return 0 when the program is not a valid ELF
*/
static uaddr_t loadElfProg(const char *prog)
{
int i;
/**
* Typedefs, constants and structure definitions as given by the ELF
* standard specifications.
*/
typedef unsigned long Elf32_Addr;
typedef unsigned long Elf32_Word;
typedef unsigned short Elf32_Half;
typedef unsigned long Elf32_Off;
// typedef signed long Elf32_Sword;
/* Elf identification */
#define EI_NIDENT 16
typedef struct {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
} __attribute__((packed)) Elf32_Ehdr_t;
/* e_ident value */
#define ELFMAG0 0x7f
#define ELFMAG1 'E'
#define ELFMAG2 'L'
#define ELFMAG3 'F'
/* e_ident offsets */
#define EI_MAG0 0
#define EI_MAG1 1
#define EI_MAG2 2
#define EI_MAG3 3
#define EI_CLASS 4
#define EI_DATA 5
#define EI_VERSION 6
#define EI_PAD 7
/* e_ident[EI_CLASS] */
#define ELFCLASSNONE 0
#define ELFCLASS32 1
#define ELFCLASS64 2
/* e_ident[EI_DATA] */
#define ELFDATANONE 0
#define ELFDATA2LSB 1
#define ELFDATA2MSB 2
/* e_type */
#define ET_NONE 0 /* No file type */
#define ET_REL 1 /* Relocatable file */
#define ET_EXEC 2 /* Executable file */
#define ET_DYN 3 /* Shared object file */
#define ET_CORE 4 /* Core file */
#define ET_LOPROC 0xff00 /* Processor-specific */
#define ET_HIPROC 0xffff /* Processor-specific */
/* e_machine */
#define EM_NONE 0 /* No machine */
#define EM_M32 1 /* AT&T WE 32100 */
#define EM_SPARC 2 /* SPARC */
#define EM_386 3 /* Intel 80386 */
#define EM_68K 4 /* Motorola 68000 */
#define EM_88K 5 /* Motorola 88000 */
#define EM_860 7 /* Intel 80860 */
#define EM_MIPS 8 /* MIPS RS3000 */
/* e_version */
#define EV_NONE 0 /* invalid version */
#define EV_CURRENT 1 /* current version */
typedef struct {
Elf32_Word p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
Elf32_Word p_filesz;
Elf32_Word p_memsz;
Elf32_Word p_flags;
Elf32_Word p_align;
} __attribute__((packed)) Elf32_Phdr_t;
/* Reserved segment types p_type */
#define PT_NULL 0
#define PT_LOAD 1
#define PT_DYNAMIC 2
#define PT_INTERP 3
#define PT_NOTE 4
#define PT_SHLIB 5
#define PT_PHDR 6
#define PT_LOPROC 0x70000000
#define PT_HIPROC 0x7fffffff
/* p_flags */
#define PF_X 1
#define PF_W 2
#define PF_R 4
Elf32_Ehdr_t *elf_hdr = (Elf32_Ehdr_t *)prog;
Elf32_Phdr_t *elf_phdrs;
/* Macro to check expected values for some fields in the ELF header */
#define ELF_CHECK(hdr, field, expected_value) \
({ \
if ((hdr)->field != (expected_value)) { \
printf("ELF prog : for %s, expected %x, got %x\n", #field, \
(unsigned)(expected_value), (unsigned)(hdr)->field); \
return 0; \
} \
})
ELF_CHECK(elf_hdr, e_ident[EI_MAG0], ELFMAG0);
ELF_CHECK(elf_hdr, e_ident[EI_MAG1], ELFMAG1);
ELF_CHECK(elf_hdr, e_ident[EI_MAG2], ELFMAG2);
ELF_CHECK(elf_hdr, e_ident[EI_MAG3], ELFMAG3);
ELF_CHECK(elf_hdr, e_ident[EI_CLASS], ELFCLASS32);
ELF_CHECK(elf_hdr, e_ident[EI_DATA], ELFDATA2LSB);
ELF_CHECK(elf_hdr, e_type, ET_EXEC);
ELF_CHECK(elf_hdr, e_version, EV_CURRENT);
/* Get the begining of the program header table */
elf_phdrs = (Elf32_Phdr_t *)(prog + elf_hdr->e_phoff);
/* Map the program segment in R/W mode. To make things clean, we
should iterate over the sections, not the program header */
for (i = 0; i < elf_hdr->e_phnum; i++) {
uaddr_t uaddr;
/* Ignore the empty program headers that are not marked "LOAD" */
if (elf_phdrs[i].p_type != PT_LOAD) {
continue;
}
if (elf_phdrs[i].p_vaddr < PAGING_BASE_USER_ADDRESS) {
printf("User program has an incorrect address 0x%x\n", elf_phdrs[i].p_vaddr);
return (uaddr_t)NULL;
}
/* Map pages of physical memory into user space */
for (uaddr = ALIGN_DOWN(elf_phdrs[i].p_vaddr, PAGE_SIZE);
uaddr < elf_phdrs[i].p_vaddr + elf_phdrs[i].p_memsz; uaddr += PAGE_SIZE) {
paddr_t ppage;
ppage = allocPhyPage(1);
assert(pageMap(uaddr, ppage,
PAGING_MEM_USER | PAGING_MEM_WRITE | PAGING_MEM_READ) == 0);
unrefPhyPage(ppage);
}
/* Copy segment into memory */
memcpy((void *)elf_phdrs[i].p_vaddr, (void *)(prog + elf_phdrs[i].p_offset),
elf_phdrs[i].p_filesz);
}
return elf_hdr->e_entry;
}
void loadUserSpace()
{
struct ata_partition *part = ATAGetPartition(0);
if (part == NULL) {
printf("No user partition found\n");
return;
}
char *buf = malloc(26 * 512);
if (buf == NULL) {
printf("ENOMEM\n");
return;
}
if (ATAReadPartitionSector(part, 0, 26, buf)) {
printf("Fail to read from disk\n");
return;
}
struct process *proc = processCreate("UserSpace");
threadChangeCurrentContext(processGetMMUContext(proc));
uaddr_t prog = loadElfProg(buf);
if (prog == (uaddr_t)NULL) {
free(buf);
return;
}
// Alloc user stack
uaddr_t stackTop = 0xfffffffc;
uaddr_t stackBottom = ALIGN_DOWN(stackTop, PAGE_SIZE);
paddr_t stackPhy = allocPhyPage(1);
assert(pageMap(stackBottom, stackPhy,
PAGING_MEM_USER | PAGING_MEM_WRITE | PAGING_MEM_READ) == 0);
unrefPhyPage(stackPhy);
threadCreateUser("UserProg", proc, prog, 0, 0, stackTop);
processUnref(proc);
threadChangeCurrentContext(NULL);
free(buf);
}
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;
2021-11-05 23:02:23 +01:00
int memMapAvailable = 0;
2021-01-23 00:42:09 +01:00
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);
2021-10-25 21:29:02 +02:00
printf("[Setup] 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
2021-10-18 20:39:08 +02:00
// https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Boot-information-format
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)) {
2021-10-25 21:29:02 +02:00
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)) {
2021-11-05 23:02:23 +01:00
int disk = mbi->boot_device >> 24 & 0xff;
2021-10-18 20:39:08 +02:00
int part1 = mbi->boot_device >> 16 & 0xff;
int part2 = mbi->boot_device >> 8 & 0xff;
int part3 = mbi->boot_device & 0xff;
2021-11-05 23:02:23 +01:00
printf("boot_device = 0x%x (0x0 for floppy 0x80 for disk) part %d %d %d\n", disk,
part1, part2, part3);
}
if (CHECK_FLAG(mbi->flags, 3)) {
2021-10-18 20:39:08 +02:00
if (mbi->mods_count > 0) {
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-10-18 20:39:08 +02:00
if (CHECK_FLAG(mbi->flags, 9)) {
printf("Loaded by %s. ", mbi->boot_loader_name);
}
/* Is the command line passed? */
if (CHECK_FLAG(mbi->flags, 2)) {
printf("cmdline = %s\n", (char *)mbi->cmdline);
}
if (CHECK_FLAG(mbi->flags, 12)) {
2021-11-05 23:02:23 +01:00
printf("Re configure 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);
VGAConfigure(mbi->framebuffer_addr, mbi->framebuffer_width,
mbi->framebuffer_height);
2021-10-18 20:39:08 +02:00
}
}
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-10-25 21:29:02 +02:00
printf("[Setup] 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-10-17 15:37:20 +02:00
printf(" -> skipping\n");
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-11-05 23:02:23 +01:00
printf("%d pages used by kernel(0x%x->0x%x))",
(lastUsedByMem - firstUsedByMem) / PAGE_SIZE, firstUsedByMem, lastUsedByMem);
printf(" (%d pages for MM)\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-10-25 21:29:02 +02:00
printf("[Setup] Pagination\n");
2021-04-10 00:24:02 +02:00
pagingSetup(firstUsedByMem, lastUsedByMem);
2021-10-25 21:29:02 +02:00
VGAMap();
2018-08-06 18:41:45 +02:00
2018-11-08 22:08:27 +01:00
2021-10-25 21:29:02 +02:00
printf("[Setup] 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
2021-10-25 21:29:02 +02:00
printf("[Setup] Serial link (115200)\n");
2018-11-08 22:08:27 +01:00
serialSetup(115200);
2021-01-25 14:00:06 +01:00
2021-10-25 21:29:02 +02:00
printf("[Setup] allocation system\n");
2021-10-28 00:32:35 +02:00
areaInit(firstUsedByMem, lastUsedByMem, _stack_bottom, _stack_top);
2021-10-17 15:37:20 +02:00
2021-11-06 00:13:40 +01:00
printf("[Setup] IRQ handlers\n");
keyboardSetup();
irqSetRoutineWrapped(IRQ_KEYBOARD, keyboard_do_irq);
2021-10-30 00:28:31 +02:00
mmuContextSetup();
2021-10-17 15:37:20 +02:00
cpu_context_subsystem_setup();
2021-01-25 14:00:06 +01:00
2021-10-25 21:29:02 +02:00
printf("[Setup] thread system\n");
2021-10-30 14:18:21 +02:00
threadSetup(_stack_bottom, (_stack_top - _stack_bottom + 1));
threadCreate("idle ", idleThread, NULL);
2021-10-30 15:43:40 +02:00
processSetup();
2021-11-03 23:54:36 +01:00
syscallSetup();
2021-01-25 14:00:06 +01:00
2020-04-23 00:49:09 +02:00
irqSetRoutine(IRQ_TIMER, pit_handler);
2021-10-03 23:19:00 +02:00
ATAInit();
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
2021-11-05 23:02:23 +01:00
loadUserSpace();
2020-04-23 23:40:16 +02:00
// There is no real caller behind this point
// So finish this by ourself
2021-10-30 14:18:21 +02:00
threadExit();
2018-07-20 15:41:58 +02:00
}