/**************************************************************************************
 Copyright (c) 2002 The UbixOS Project
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors
in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its
contributors may be used to endorse or promote products derived from this software without specific prior written permission.

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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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.

 $Id: main.c,v 1.1 2003/01/10 18:33:01 reddawg Exp $

**************************************************************************************/

#include <ubixos/gdt.h>
#include <ubixos/tss.h>
#include <ubixos/vitals.h>
#include <ubixos/idt.h>
#include <vmm/memory.h>
#include <vmm/paging.h>
#include <drivers/8259.h>

int main();

/************************************************************************

Function: void _start();
Description: Constructor For GDT
Notes: Conatins 12 Entries:
 
  00 - (0x00) - Dummy Entry
  01 - (0x08) - PL0 Code Segment
  02 - (0x10) - PL0 Data Segment
  03 - (0x18) - PL1 Code Segment
  04 - (0x20) - PL1 Data Segment
  05 - (0x28) - PL2 Code Segment
  06 - (0x30) - PL2 Data Segment
  07 - (0x38) - PL3 Code Segment
  08 - (0x40) - PL3 Data Segment
  09 - (0x48) - Dummy LDT
  10 - (0x50) - TSS1
  11 - (0x58) - TSS2

************************************************************************/  
descriptorTable(GDT,12) {
  {dummy:0},
  standardDescriptor(0x0, 0xFFFFF, (dCode + dRead + dBig + dBiglim + dConform)),
  standardDescriptor(0x0, 0xFFFFF, (dData + dWrite + dBig + dBiglim)),
  standardDescriptor(0x0, 0xFFFFF, (dCode + dRead + dBig + dBiglim + dDpl1 + dConform)),
  standardDescriptor(0x0, 0xFFFFF, (dData + dWrite + dBig + dBiglim + dDpl1)),
  standardDescriptor(0x0, 0xFFFFF, (dCode + dRead + dBig + dBiglim + dDpl2 + dConform)),
  standardDescriptor(0x0, 0xFFFFF, (dData + dWrite + dBig + dBiglim + dDpl2)),
  standardDescriptor(0x0, 0xFFFFF, (dCode + dRead + dBig + dBiglim + dDpl3 + dConform)),
  standardDescriptor(0x0, 0xFFFFF, (dData + dWrite + dBig + dBiglim + dDpl3)),
  standardDescriptor(0x0, 0xFFFFF, (dLdt)),
  standardDescriptor(0x6000, sizeof(struct tssStruct), (dTss)),
  standardDescriptor(0x2000, sizeof(struct tssStruct), (dTss)),
  };

struct {
  unsigned short limit __attribute__ ((packed));
  union descriptorTableunion *gdt __attribute__ ((packed));
  } loadGdt = { (12 * sizeof(union descriptorTableunion) - 1), GDT };

/************************************************************************

Function: void _start();
Description: This Function Is The Kernels Entry Point
Notes:

************************************************************************/
asm(
  ".globl _start      \n"
  "_start:            \n"
  "  lgdtl (loadGdt)  \n" // Load Our GDT Address Into Propper Register
  "  mov $0x10,%ax    \n" // Lets Set Up All Defaults
  "  mov %ax,%ds      \n" // Data Segment
  "  mov %ax,%es      \n" // ES
  "  mov %ax,%fs      \n" // FS
  "  mov %ax,%gs      \n" // GS
  "  mov %ax,%ss      \n" // SS
  "  mov $0x2FFFF,%esp\n" /* Top Address Of Kernel Stack
                             This Is First Address Before Kernel
                          */
  "  mov $0x48,%ax    \n" // Set Up Dummy LDT
  "  lldt %ax         \n" // Load Dummy LDT
  "  mov $0x50,%ax    \n" // Set Up Primary TSS
  "  ltr %ax          \n" // Loads Primary TSS
  "  call main        \n" // Jump To Entry Point Of Kernel
  "  hlt              \n" // Halt The System If We Happen To Get Here
  );

/************************************************************************

Function: int main();
Description: This Function Is The Kernels Main Loop
Notes: Hopefully I Have Systems Starting In The Correct Order

************************************************************************/
int main() {
  //char *buffer = 0xE4000000;
  //char *buffer = 0xA0000;
  //int i = 0x0;
  initVitals();        // Initialize The Vitals Subsystem (IN-KERNEL)
  initMemoryMap();     // Initialize The Memory Map
  initPagingSystem();  // Initialize Memory Paging
  init8259();          // Initialize PIC
  initIDT();           // Initialize The IDT
  initScheduler();     // Initialize Scheduler
  initFloppy();        //Initialize Floppy Controller
  /*
  while (1) {
    for (i=0;i<(1024*768);i++) {
      buffer[i] = 69;
      }
    for (i=(1024*768);i>=0;i--) {
      buffer[i] = 220;
      }
    }
  */
  asm("hlt");
  return(0);
  }