2012-02-29 07:36:11 -05:00
|
|
|
/*******************************************************************************
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-02-29 07:36:11 -05:00
|
|
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
2012-02-29 07:36:11 -05:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
kernel.cpp
|
2012-02-29 07:36:11 -05:00
|
|
|
The main kernel initialization routine. Configures hardware and starts an
|
|
|
|
initial process from the init ramdisk, allowing a full operating system.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-02-29 07:36:11 -05:00
|
|
|
*******************************************************************************/
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
#include "platform.h"
|
|
|
|
#include <libmaxsi/memory.h>
|
|
|
|
#include <libmaxsi/string.h>
|
|
|
|
#include <libmaxsi/format.h>
|
|
|
|
#include "log.h"
|
|
|
|
#include "panic.h"
|
2012-03-07 12:04:59 -05:00
|
|
|
#include "kernelinfo.h"
|
2012-02-29 18:15:28 -05:00
|
|
|
#include "x86-family/gdt.h"
|
2011-08-05 08:25:00 -04:00
|
|
|
#include "time.h"
|
|
|
|
#include "keyboard.h"
|
|
|
|
#include "multiboot.h"
|
|
|
|
#include "memorymanagement.h"
|
2011-11-07 08:36:35 -05:00
|
|
|
#include "thread.h"
|
2011-09-21 14:52:29 -04:00
|
|
|
#include "process.h"
|
2011-08-05 08:25:00 -04:00
|
|
|
#include "scheduler.h"
|
|
|
|
#include "syscall.h"
|
|
|
|
#include "pci.h"
|
2012-03-17 10:18:03 -04:00
|
|
|
#include "com.h"
|
2011-08-05 08:25:00 -04:00
|
|
|
#include "uart.h"
|
2012-01-22 10:48:57 -05:00
|
|
|
#include "terminal.h"
|
2011-08-05 08:25:00 -04:00
|
|
|
#include "serialterminal.h"
|
|
|
|
#include "vgaterminal.h"
|
2011-08-22 18:29:59 -04:00
|
|
|
#include "elf.h"
|
2011-08-27 14:07:08 -04:00
|
|
|
#include "initrd.h"
|
2011-10-26 18:05:20 -04:00
|
|
|
#include "vga.h"
|
|
|
|
#include "sound.h"
|
2011-11-16 02:37:29 -05:00
|
|
|
#include "io.h"
|
|
|
|
#include "pipe.h"
|
2011-11-17 17:31:41 -05:00
|
|
|
#include "filesystem.h"
|
|
|
|
#include "mount.h"
|
2011-11-20 18:02:53 -05:00
|
|
|
#include "directory.h"
|
2011-11-30 17:30:14 -05:00
|
|
|
#include "interrupt.h"
|
2012-03-17 10:11:35 -04:00
|
|
|
#include "fs/devfs.h"
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
using namespace Maxsi;
|
|
|
|
|
Fixed the horrible 'nofoo' bug!
When compiled with gcc 4.6.1, 32-bit Sortix would triple fault during
early boot: When the TLB is being flushed, somehow a garbage value had
sneaked into Sortix::Memory::currentdir, and a non-page aligned (and
garbage) page directory is loaded. (Triple fault, here we come!)
However, adding a volatile addr_t foo after the currentdir variable
actually caused the system to boot correctly - the garbage was written
into that variable instead. To debug the problem, I set the foo value
to 0: as long as !foo (hence the name nofoo) everything was alright.
After closer examination I found that the initrd open code wrote to a
pointer supplied by kernel.cpp. The element pointed to was on the
stack. Worse, its address was the same as currentdir (now foo).
Indeed, the stack had gone into the kernel's data segment!
Turns out that this gcc configuration stores variables in the data
segment in the reverse order they are defined in, whereas previous
compilers did the opposite. The hack used to set up the stack during
early boot relied on this (now obviously incorrect) fact.
In effect, the stack was initialized to the end of the stack, not
the start of it: completely ignoring all the nice stack space
allocated in kernel.cpp.
I did not see that one coming.
2011-12-24 21:33:12 -05:00
|
|
|
// Keep the stack size aligned with $CPU/base.s
|
2012-03-17 15:17:12 -04:00
|
|
|
extern "C" { size_t stack[64*1024 / sizeof(size_t)] = {0}; }
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
namespace Sortix {
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
void DoMaxsiLogo()
|
|
|
|
{
|
|
|
|
Log::Print("\e[37;41m\e[2J"); // Make the background color red.
|
|
|
|
Log::Print(" _ \n");
|
|
|
|
Log::Print(" / \\ \n");
|
|
|
|
Log::Print(" /\\ /\\ / \\ \n");
|
|
|
|
Log::Print(" / \\ / \\ | | \n");
|
|
|
|
Log::Print(" / \\/ \\ | | \n");
|
|
|
|
Log::Print(" | O O \\_______________________ / | \n");
|
|
|
|
Log::Print(" | | \n");
|
|
|
|
Log::Print(" | \\_______/ / \n");
|
|
|
|
Log::Print(" \\ / \n");
|
|
|
|
Log::Print(" ------ --------------- ---/ \n");
|
|
|
|
Log::Print(" / \\ / \\ \n");
|
|
|
|
Log::Print(" / \\ / \\ \n");
|
|
|
|
Log::Print(" / \\ / \\ \n");
|
|
|
|
Log::Print(" /_____________\\ /____________\\ \n");
|
|
|
|
Log::Print(" \n");
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
void DoWelcome()
|
|
|
|
{
|
|
|
|
DoMaxsiLogo();
|
|
|
|
Log::Print(" BOOTING OPERATING SYSTEM... ");
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
extern "C" void KernelInit(unsigned long magic, multiboot_info_t* bootinfo)
|
|
|
|
{
|
|
|
|
// Initialize system calls.
|
|
|
|
Syscall::Init();
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Detect and initialize any serial COM ports in the system.
|
|
|
|
COM::EarlyInit();
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the default terminal.
|
|
|
|
VGATerminal::Init();
|
|
|
|
Maxsi::Format::Callback logcallback = VGATerminal::Print;
|
|
|
|
void* logpointer = NULL;
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the kernel log.
|
|
|
|
Log::Init(logcallback, logpointer);
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Display the boot welcome screen.
|
|
|
|
DoWelcome();
|
2012-03-17 10:18:03 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
if ( !bootinfo )
|
|
|
|
{
|
|
|
|
Panic("The bootinfo structure was NULL. Are your bootloader "
|
|
|
|
"multiboot compliant?");
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
addr_t initrd = NULL;
|
|
|
|
size_t initrdsize = 0;
|
2011-11-30 17:30:14 -05:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
uint32_t* modules = (uint32_t*) bootinfo->mods_addr;
|
|
|
|
for ( uint32_t i = 0; i < bootinfo->mods_count; i++ )
|
|
|
|
{
|
|
|
|
initrdsize = modules[2*i+1] - modules[2*i+0];
|
|
|
|
initrd = (addr_t) modules[2*i+0];
|
|
|
|
break;
|
|
|
|
}
|
2011-11-30 17:30:14 -05:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
if ( !initrd ) { PanicF("No init ramdisk provided"); }
|
2011-08-27 14:07:08 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
Memory::RegisterInitRDSize(initrdsize);
|
2011-11-30 17:30:14 -05:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize paging and virtual memory.
|
|
|
|
Memory::Init(bootinfo);
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the GDT and TSS structures.
|
|
|
|
GDT::Init();
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the interrupt handler table and enable interrupts.
|
|
|
|
Interrupt::Init();
|
2011-10-06 04:09:08 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the kernel heap.
|
|
|
|
Maxsi::Memory::Init();
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the list of kernel devices.
|
|
|
|
DeviceFS::Init();
|
2012-03-17 10:11:35 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the COM ports.
|
|
|
|
COM::Init();
|
2012-03-17 10:18:03 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the keyboard.
|
|
|
|
Keyboard::Init();
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the terminal.
|
|
|
|
Terminal::Init();
|
2012-01-22 10:48:57 -05:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the VGA driver.
|
|
|
|
VGA::Init();
|
2011-10-26 18:05:20 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the sound driver.
|
|
|
|
Sound::Init();
|
2011-10-26 18:05:20 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the process system.
|
|
|
|
Process::Init();
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the thread system.
|
|
|
|
Thread::Init();
|
2011-09-15 16:38:40 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the IO system.
|
|
|
|
IO::Init();
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the pipe system.
|
|
|
|
Pipe::Init();
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the filesystem system.
|
|
|
|
FileSystem::Init();
|
2011-11-17 17:31:41 -05:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the directory system.
|
|
|
|
Directory::Init();
|
2011-11-20 18:02:53 -05:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the mount system.
|
|
|
|
Mount::Init();
|
2011-11-17 17:31:41 -05:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the scheduler.
|
|
|
|
Scheduler::Init();
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Initialize the kernel information query syscall.
|
|
|
|
Info::Init();
|
2012-03-07 12:04:59 -05:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Set up the initial ram disk.
|
|
|
|
InitRD::Init(initrd, initrdsize);
|
2011-08-27 14:07:08 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Search for PCI devices and load their drivers.
|
|
|
|
PCI::Init();
|
2012-01-08 08:20:39 -05:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Alright, now the system's drivers are loaded and initialized. It is
|
|
|
|
// time to load the initial user-space programs and start execution of
|
|
|
|
// the actual operating system.
|
2011-09-21 14:52:29 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
byte* program;
|
|
|
|
size_t programsize;
|
2011-09-21 14:52:29 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Create an address space for the idle process.
|
|
|
|
addr_t idleaddrspace = Memory::Fork();
|
|
|
|
if ( !idleaddrspace ) { Panic("could not fork an idle process address space"); }
|
2011-09-21 14:52:29 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Create an address space for the initial process.
|
|
|
|
addr_t initaddrspace = Memory::Fork();
|
|
|
|
if ( !initaddrspace ) { Panic("could not fork an initial process address space"); }
|
2011-09-21 14:52:29 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Create the system idle process.
|
|
|
|
Process* idle = new Process;
|
|
|
|
if ( !idle ) { Panic("could not allocate idle process"); }
|
|
|
|
idle->addrspace = idleaddrspace;
|
|
|
|
Memory::SwitchAddressSpace(idleaddrspace);
|
|
|
|
Scheduler::SetDummyThreadOwner(idle);
|
|
|
|
program = InitRD::Open("idle", &programsize);
|
|
|
|
if ( program == NULL ) { PanicF("initrd did not contain 'idle'"); }
|
|
|
|
addr_t idlestart = ELF::Construct(idle, program, programsize);
|
|
|
|
if ( !idlestart ) { Panic("could not construct ELF image for idle process"); }
|
|
|
|
Thread* idlethread = CreateThread(idlestart);
|
|
|
|
if ( !idlethread ) { Panic("could not create thread for the idle process"); }
|
|
|
|
Scheduler::SetIdleThread(idlethread);
|
2011-09-21 14:52:29 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Create the initial process.
|
|
|
|
Process* init = new Process;
|
|
|
|
if ( !init ) { Panic("could not allocate init process"); }
|
|
|
|
init->addrspace = initaddrspace;
|
|
|
|
Memory::SwitchAddressSpace(initaddrspace);
|
|
|
|
Scheduler::SetDummyThreadOwner(init);
|
|
|
|
program = InitRD::Open("init", &programsize);
|
|
|
|
if ( program == NULL ) { PanicF("initrd did not contain 'init'"); }
|
|
|
|
addr_t initstart = ELF::Construct(init, program, programsize);
|
|
|
|
if ( !initstart ) { Panic("could not construct ELF image for init process"); }
|
|
|
|
Thread* initthread = CreateThread(initstart);
|
|
|
|
if ( !initthread ) { Panic("could not create thread for the init process"); }
|
|
|
|
Scheduler::SetInitProcess(init);
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Lastly set up the timer driver and we are ready to run the OS.
|
|
|
|
Time::Init();
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-03-17 15:11:37 -04:00
|
|
|
// Run the OS.
|
|
|
|
Scheduler::MainLoop();
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
2012-03-17 15:11:37 -04:00
|
|
|
|
|
|
|
} // namespace Sortix
|