summaryrefslogtreecommitdiff
path: root/kernel/kernel.c
blob: 4c7227b8728125ca91632253b770454b99e93640 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "kernel.h"

#include "serial.h"
#include "asm_pic.h"
#include "multiboot.h"
#include "acpi.h"
#include "gdt.h"
#include "interrupts.h"

#include "mem.h"
#include "vmem.h"

//-- clean below headers --//
#include "apic.h"
#include "kernel/scheduler.h"

#include "driver/timer.h"
#include "driver/keyboard.h"
#include "driver/mouse.h"

#include "syscalls.h"
#include "fifo.h"
#include "asm_smp.h"
#include "asm_x86.h"
#include "ringbuffer.h"
#include "driver/screen.h"
#include "smp.h"

#include "fs/fs.h"
#include "kmalloc.h"
#include "driver/vesa.h"

/* F00L 0S Entry point (called directly from asm/multiboot.asm */
void kernel_main(uint32_t eax,uint32_t ebx)
{
    // -- COM1 -- //
    serial_init();
    klog("Communication Port (COM1) initialized.");

    // -- PR & VERSION BANNER -- //
    klog("======================================");
    klog("F00L- 0S / The Fool's Operating System");
    klog("(C) 2018 / Michal Idziorek (m.i@gmx.at)");
    klog("Compiled on: %s at %s",__DATE__,__TIME__);
    klog("Version: git-commit: %s",GIT_REVISION);
    klog("======================================");

    // -- DISABLE LEGACY PIC -- //
    klog("Remapping & Disabling Programmable Interrupt Controller (PIC) ...");
    fixme("io_wait & spurious interrupts");
    asm_pic_setup();

    // -- GET CONFIGS -- //
    klog("Read Multiboot Structures ...");
    multiboot_information *cfg_multiboot;
    cfg_multiboot=multiboot_read(eax, ebx,false);
    
    klog("Read Advanced Power Configuration Interface (ACPI) Structures ...");
    acpi_information cfg_acpi;    
    bool acpi_found=acpi_fill(&cfg_acpi);
    fixme("try to read (legacy) multiprocessor mp strucutres (see: xxx/mp.c)");
    if(!acpi_found) kpanic("We Currently rely on ACPI Structures Sorry!");

    // -- GDT -- //
    klog("Global Descriptor Table (GDT) init ...");
    gdt_init();

    // -- IVT -- //
    klog("Interrupt Vector Table (IVT) init ...");
    interrupts_init(0x08);
    interrupts_install();
    fixme("register interrupt callback funcs (instead hardcoded dispatcher)");

    // -- APIC -- //
    klog("Advanced Programmable Interrupt Controller (APIC) config ...");
    apic_init(&cfg_acpi);
    ioapic_config();
    apic_enable();
    apic_init_timer(3);// freq in HZ

    // -- MEMORY MANAGEMENT -- //
    klog("Memory init ... ");
    mem_init(cfg_multiboot);
    
    klog("Vritual Memory / Paging init ... ");
    fixme("do not disable anymore on context switching!");
    fixme("write convenneint management funcs as: mapCPU, mapKErnel, map USerspace..");
    fixme("move stack and guard with empty pages!");

    vmem_init(0, // this is hardcoded to first 32megs anyway 
             (uint32_t)cfg_multiboot->framebuffer_addr,
	      cfg_acpi.local_apic_address,
              cfg_acpi.io_apic_address);

    struct pdirectory_struct *dir=vmem_kernel_dir();
    x86_set_page_directory(dir);
    x86_paging_enable();

    // temporary TODO
    uint32_t *cpu_mem=0x8000000; //1024 pages from here on are mapped per cpu for testing! TODO: dynamic!
    *cpu_mem=apic_id();

    // -- RAM IMAGE -- //
    klog("Ram Filesystem init ... ");
    fs_mount(cfg_multiboot);

    // -- VESA -- //
    klog("Video Electronics Standards Association (VESA) init ... "); // TODO check if text or fb?
    uint32_t addr=kballoc(1);
    fs_content("/binfont.bin",addr,0x100); // copy font (0x100 bytes) to memory.
    vesa_init(cfg_multiboot->vbe_control_info,cfg_multiboot->vbe_mode_info,addr);

    // -- STDIN/STDOUT -- //
    klog("stdin/stdout init ...");
    uint32_t sstdin  = syscall_open("stdin",0,0);   // stdin  0
    uint32_t sstdout = syscall_open("term",0,0);    // stdout 1
    uint32_t sstderr = syscall_open("stderr",0,0);  // stderr 2

    klog("Keyboard init ...");
    keyboard_init(0); 

    klog("Mouse init ...");
    mouse_init();

    klog("Symmetric Multi Processing (SMP) start ... ");
    smp_start_aps(&cfg_acpi);

    klog("Programmable Interval Timer (PIT) init ...");
    uint64_t unixtime=timer_init();
    klog("Unix Time = %u seconds",unixtime);

    klog("Unlock application processors ... ");
    asm_smp_unlock();

    PutFont('X', 100,200, 0x00ffff,0xff00ff); // TODO temporary!

    klog("Enable Interrupts ... ");
    x86_sti(); // this will start processing hardware interrupts

    // now just wait until our scheduler kicks in.
    while(1)asm("hlt");
}