BarryServer : Git

All the code for all my projects
// BarryServer : Git / Orion / blob / master / main.c

// Related

Orion

Barry Keyboard/Mouse drivers + POSIX names for structs 1628fcf (2 years, 4 months ago)
/*
 * This file contains the entrance point for the Kernel proper.  The code here
 * is called by the bootloader when it finishes.  The main routine is
 * responsible for setting up memory and starting the processes running.
 * This file is also responsible for performing per-CPU setups, which is done
 * before the Kernel starts multi-tasking.
 */

#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mount.h>
#include "mem/frame.h"
#include "mem/paging.h"
#include "mem/heap.h"
#include "mem/mem.h"
#include "proc/proc.h"
#include "task/task.h"
#include "vfs/vfs.h"
#include "drivers/drivers.h"
#include "net/net.h"
#include "io.h"
#include "screen.h"
#include "spinlock.h"
#include "multiboot.h"

extern char _bss[], _end[];

uintptr_t initialStack;
void *initrd;

/* Setup current CPU */
void
cpu_load(void)
{
	/* Initialise SSE */
	asm volatile(
		"mov %cr0, %eax;"
		"andl $0xfffffffb, %eax;"
		"orl $0x2, %eax;"
		"mov %eax, %cr0;"
		"mov %cr4, %eax;"
		"orl $0x600, %eax;"
		"mov %eax, %cr4;"
	);

	/* Initialise CR4.PGE */
	asm volatile(
		"mov %cr4, %eax;"
		"orl $0x10, %eax;"
		"mov %eax, %cr4;"
	);

	/* Tables */
	cpu_load_idt();
	cpu_load_gdt();
}

/* Kernel main function */
void
kmain(uint32_t esp, struct MultibootInfo *mbinfo)
{
	/* Disable the cursor */
	outb(0x3D4, 0x0A);
        outb(0x3D5, 0x20);

	kprintf("Starting the Orion Operating System");
	void *ebda = (void *) (*((uint16_t *) 0x040E) << 4);
	initrd = (void *) *((uint32_t *) mbinfo->modsAddr);

	/* Zero-fill the BSS */
	memset(_bss, 0, _end - _bss);

	/* Setup frame allocator */
	init_frames(mbinfo->mmapLen, (void *) mbinfo->mmapAddr);

	/* Processor startup */
	initialStack = esp;
	init_idt();
	init_gdt();
	init_pic();
	cpu_load();
	init_multicore(ebda);

	/* Check if 64-bit CPU */
	uint32_t lm;
	asm("cpuid" : "=d" (lm) : "a" (0x80000001));
	if ((lm >> 29) & 1)
		kprintf("CPU supports 64-bit mode");

	/* Initialise paging */
	init_paging();
	/* Initialise Multitasking */
	init_tasking();
	/* Initialise VFS */
	init_vfs();
	register_exception(14, page_fault_handler);
	/* Search for devices */
	init_drivers();
	init_tty();
	init_kbd();
//	init_mouse();
	/* Initialise Networking */
//	init_net();

	/* Mount root disk */
	mkdir("root", 0);
	mount("/dev/hda", "/root", "Ext2FS", MS_RDONLY, NULL);
	chroot("/root");
	chdir("/");
	mount("devfs", "/dev", "DevFS", 0, NULL);
	mount("procfs", "/proc", "ProcFS", 0, NULL);

	char *argv[] = { "init", NULL };
	execve("/bin/init", argv, NULL);
	panic("Could not run init");
}