Files
bsb2/kernel/main.cc

138 lines
4.0 KiB
C++

#include "./arch/lapic.h"
#include "./debug/output.h"
#include "./types.h" // This is actually used
#include "arch/core_interrupt.h"
#include "memory/page.h"
#include "memory/pageframealloc.h"
#include "memory/pagetable.h"
#include "object/outputstream.h"
#include "syscall/handler.h"
#include "./device/textstream.h"
#include "./debug/copystream.h"
#include "./device/serialstream.h"
// Separate title window on first line (for simplicity at scrolling)
void PageFrameAllocator::init();
static TextStream tout(0, CGA::COLUMNS, 0, 1);
SerialStream sout;
TextStream dout(0, 80, 17, 25);
CopyStream copystream(&dout, &sout);
OutputStream* copyout = &copystream;
#include "./arch/core.h"
#include "./arch/ioapic.h"
#include "./device/ps2controller.h"
#include "./interrupt/guard.h"
#include "./boot/multiboot/data.h"
#include "./sync/semaphore.h"
#include "./memory/pageframealloc.h"
//Semaphore koutsem(1);
//TextStream kout(0, 80, 1, 17, true);
// Applications
#include "./user/app1/appl.h"
#include "./user/app2/kappl.h"
//alignas(4096) four_lvl_paging_t paging_tree;
//four_lvl_paging_t paging_tree;
alignas(4096) pagetable_t identity_table[KERNEL_MEMORY_BORDER/4096/512];
//static const uint32_t NUM_APPS = 9;
//Application apps[NUM_APPS];
//static KeyboardApplication kapp;
// Main function
extern "C" int main() {
PageFrameAllocator::stats();
Multiboot::Module* initrd = Multiboot::getModule(0);
DBG << "initrd address: " << hex << initrd->getStartAddress() << endl << "initrd size: " << initrd->getSize() << endl;
//memcpy((void *)0x4000000, initrd->getStartAddress(), initrd->getSize());
//mark_pageframes(0x4000000, 0x4000000 + initrd->getSize(), false);
write_identity_map(identity_table, KERNEL_MEMORY_BORDER);
//create_basic_page_table(&paging_tree, identity_table);
//load_cr3((void*)&paging_tree.l4);
tout.reset();
tout.setPos(33, 0);
tout << OS_NAME << " (2.x)" << flush;
//// double faults correctly
//volatile uint16_t test1 = 0;
//volatile uint16_t test = 8/test1;
// Initialize IOAPIC
IOAPIC::init();
// Init timer (1000us = 1ms)
LAPIC::Timer::setup(10000);
// Activate Keyboard
PS2Controller::init();
Syscall::init();
// Enter Level 1/2
Guarded g = Guard::enter();
uint32_t (*apps_header)[1024] = (uint32_t (*)[1024])Multiboot::getModule(0)->getStartAddress();
uint64_t offset = 1;
for(uint16_t i = 1; i <= (*apps_header)[0]; i++){
uint64_t appsize = (*apps_header)[i];
uintptr_t appstart = (uintptr_t)apps_header+(offset<<12);
DBG << "app " << i << " size " << appsize << " at " << appstart << endl;
Thread* thread_ptr= new Thread(false, (void*)0x4000000, (void*)appstart, (appsize/4096)+1); //TODO fix edgecase on size=4096
g.vault().thread_list[thread_ptr->id] = thread_ptr;
g.vault().scheduler.ready(thread_ptr); //TODO fix edgecase on size=4096
offset += 1+(appsize/4096);
}
//Thread* thread_ptr = new Thread(false, (void*)0x4000000, (void*)0x4001000, 2);
//g.vault().scheduler.ready(thread_ptr);
//g.vault().thread_list[thread_ptr->id] = thread_ptr;
//for(void* p=Multiboot::getModule(0)->getStartAddress(); p<Multiboot::getModule(0)->getEndAddress(); p=(void*)((uintptr_t)p+4096)){
// DBG << "module at: " << hex << p << endl;
// g.vault().scheduler.ready(new Thread(false, p));
//}
//g.vault().scheduler.ready(new Thread(false, (void*)0x4001000));
//for (uint32_t i = 0; i < NUM_APPS; ++i) {
// g.vault().scheduler.ready(&(apps[i]));
//}
//g.vault().scheduler.ready(&kapp);
// Enable Interrupts
Core::Interrupt::enable();
/* Activate timer
* Be careful:
* If enabled early and interrupts are enabled, it might start scheduling
* (resume in epilogue) and dispatch the first app, never returning back to
* main and finish initialization.
* Therefore activate the LAPIC timer in level 1/2 after initialization
* (just before schedule())
*/
LAPIC::Timer::activate();
DBG_VERBOSE << "Schedule..." << endl;
// Schedule first app
g.vault().scheduler.schedule();
return 0;
}