↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

init.c
/*
EPOS
http://www.atanaslaskov.com/epos/

FILE:         init.c
DESCRIPTION:  Kernel initialization
 
BSD LICENSE
Copyright (c) 2006, Atanas Laskov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "init.h"

#include "..\gdt\gdt.h"
#include "..\idt\int.h"
#include "..\paging\paging.h"
#include "..\access\access.h"

#include "..\debug\txtout.h"
#include "..\debug\keybrd.h"
#include "..\debug\debug.h"

#include "..\service\service.h"
#include "..\memory\memory.h"
#include "..\process\process.h"

char s[4*1024];

#define LOW_BYTE(n) (n & 0x00ff)
#define HIGH_BYTE(n) ((n>>8)&0x00ff)

/*
    Kernel initialization failed ?
*/

void khalt() {

    // Share the bad news
    //
    puts("\n\nFatal error has occured during kernel initialization.");
    puts("\nPress any key to restart...");
    getc();

    reboot();
}

/*
    Kernel initialization
*/

void kinit(void)
{
    unsigned i;
    unsigned char fault1, fault2;
    unsigned long size;

    // Start by printing out EPOS version
    //
    puts("\nEPOS: Experimental Protected-mode Operating System");
    puts("\nEPOS: Kernel version is ");
    putd(access_get_major()); puts(".");
    putd(access_get_minor()); puts(" build "); putd(access_get_build());
   
    puts("\nEPOS: Copyright (C) Atanas Laskov");
    puts("\n");
   
    PrintMedium(); // boot medium
    puts("\nKRN: Starting KERNEL initialization...");

    // A20 must be enabled for correct memory access in protected-mode
    //
    enableA20();
    puts("\nKRN: A20 enabled");
   
    gdt_linear_setb(0x500, 0x00);
    gdt_linear_setb(0x100000+0x500, 0xA6);
   
    if(gdt_linear_getb(0x500)==0xA6)
        puts("\nKRN: ENABLE A20 FAILED !");

    // Initialize interrupt handlers
    //
    initvectors();
    puts("\nKRN: Interrupt subsystem initialized");

    // Initialize GDT
    //
    gdtInit();
    puts("\nKRN: GDT management subsystem initialized");

    // Setup trap/falt/abort interrupt vectors
    // internal kernel debugger
    //
    initdebug();
    puts("\nKRN: Debug subsystem initialized");

    // Initialize memory manager
    //
    memInit();
    puts("\nKRN: Memory management subsystem initialized");

    // Assign higher interrupt numbers to hardware IRQs
    //
    access_out(0x20, 0x11); //ICW1
    access_out(0xA0, 0x11);
    access_out(0x21, KRNL_FIRST_IRQ);   //ICW2
    access_out(0xA1, KRNL_FIRST_IRQ+8);
    access_out(0x21, 4);    //ICW3
    access_out(0xA1, 2);
    access_out(0x21, 1);    //ICW4
    access_out(0xA1, 1);

    // Disable all IRQs (temporarily)
    //
    access_out(0x21, 0xFF);
    access_out(0xA1, 0xFF);

    // Initialize paging
    //
    initPaging();
    puts("\nKRN: Paging initialized");

    // Setup kernel service interrupt
    // NOTE: The process manager uses "IPC" in the service
    // sub-system so it must me initialised first
    //
    srvinit();
    puts("\nKRN: Service subsystem initialized");

    // Initialize process manager
    //
    tmInit();
    puts("\nKRN: Process management subsystem initialized");

    // Setup keyboard interrupt
    kbdinit();

    // Re-enable IRQs
    //
    access_out(0x21, 0x0);  // enable Keyboard, FDC, slave PIC, timer
    access_out(0xA1, 0x0);  // enable all
    access_sti;

    // Print memory status
    //
    puts("\nKRN: "); putd( memGetFreePhysicalMemorySize()/0x100000 );
    puts("MB unallocated memory left");

    // Initialize IRQ manager (don't move this)
    //
    k_irqInit();

    // Setup hardware timer
    //
    access_cli;
    SetTimerFrequency(KRNL_TIMER_HZ);
    access_sti;

    // Start the "ancestor" process. It will read the boot config file
    // and it will start all executables it finds there (e.g. the shell)
    //
    StartAncestor();

    // Jump to the first task (i.e. the ancesstor process)
    //
    pageDirectorySwitch(PAGING_DIR_PROCESS0 + tmGetCrProcessID());
    tmFirstProcess();
}

/*
    Enable A20 memory gate. A20 must be enabled for correct
    memory access in protected-mode
*/


void enableA20()
{
    unsigned char state;

    while(1){ state = access_in(0x64); if((state & 0x01)==0) break;}
    while(1){ state = access_in(0x64); if((state & 0x01)==0) break;}
    access_out(0x60, 0xD7);
    while(1){ state = access_in(0x64); if((state & 0x01)==0) break;}

    state = access_in(0x92);
    state = state | 2;
    access_out(0x92, state);
}

/*
    Initialize hardware timer
*/

void SetTimerFrequency(unsigned long hz)
{
  unsigned short word;

  word = 1193180 / hz;

  access_out(0x43, 0x34 /*0x3c /*0x36*/);
  access_out(0x40, LOW_BYTE(word));
  access_out(0x40, HIGH_BYTE(word));
}

/*
    Start anccestor porcess
*/

void StartAncestor()
{
    PID h;
    unsigned char* src;
    unsigned char* dst;
    unsigned long size;
    int i;

    // Form memory pointers
    //
    src  = (char*)((unsigned long)access_get_ancestor_start_seg()
        * 0x10+(unsigned long)(access_get_ancestor_start_ofs()));
   
    size = ((unsigned long)access_get_ancestor_end_seg()
        * 0x10+(unsigned long)(access_get_ancestor_end_ofs()));
   
    size -= (unsigned long)src;
    src  = src - 0x600;

    // Print useful stuff :-)
    //
    puts("\nKRN: Ancestor size is "); putd(size/1024); puts("KB");
    puts("\nKRN: Starting Ancestor process...");

    // Start it
    tmCreateProcess(&h, size, "ANCESTOR");
    dst = (char*)(tmGetProcessMembase(h)-0x600);
    kMemCpy(dst, src, size);
    tmLiveProcess(h);
}

/*
    Print out boot medium
*/

void PrintMedium()
{
    puts("\nKRN: Booting from ");
    switch(access_get_bootup_medium()) {
    case 0:
        puts("3.14' fdd");
        break;
    default:
        // Ok, not exactly a long list.
        puts("unknown medium");
        break;
    }
    puts(" ("); putd(access_get_bootup_medium()); puts(")");
}