/*****************************************************************************************
 Copyright (c) 2002-2004 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$

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

#include <sys/gdt.h>
#include <sys/video.h>
#include <sys/idt.h>
#include <sys/tss.h>
#include <vmm/vmm.h>
#include <ubixos/vitals.h>
#include <ubixos/exec.h>
#include <ubixos/kpanic.h>
#include <ubixos/sched.h>
#include <ubixos/idletask.h>
#include <ubixos/time.h>
#include <vfs/mount.h>
#include <lib/kprintf.h>
#include <lib/kmalloc.h>
#include <lib/bioscall.h>
#include <isa/8259.h>
#include <isa/pit.h>
#include <isa/fdc.h>
#include <isa/atkbd.h>
#include <isa/ne2k.h>
#include <vfs/vfs.h>
#include <ubixfs/ubixfs.h>
#include <pci/pci.h>
#include <pci/hd.h>
#include <sde/sde.h>
#include <devfs/devfs.h>
#include <net/net.h>
#include <ubixos/smp.h>
#include <mpi/mpi.h>

char kernelStack[8192]; // Stack Space For Our Kernel

ubixDescriptorTable(ubixGDT,8) {
  {dummy:0},
  ubixStandardDescriptor(0x0, 0xFFFFF, (dCode + dRead + dBig + dBiglim)),
  ubixStandardDescriptor(0x0, 0xFFFFF, (dData + dWrite + dBig + dBiglim)),
  ubixStandardDescriptor(0x0, 0xFFFFF, (dLdt)),
  ubixStandardDescriptor(0x4200, (sizeof(struct tssStruct)), (dTss)),
  ubixStandardDescriptor(0x0, 0xFFFFF, (dCode + dWrite + dBig + dBiglim + dDpl3)),
  ubixStandardDescriptor(0x0, 0xFFFFF, (dData + dWrite + dBig + dBiglim + dDpl3)),
  ubixStandardDescriptor(0x4200, (sizeof(struct tssStruct)), (dTss)),
  };
struct {
  unsigned short limit __attribute__ ((packed));
  union descriptorTableUnion *gdt __attribute__ ((packed));
  } loadGDT = { (8 * sizeof(union descriptorTableUnion) - 1), ubixGDT };

/*****************************************************************************************
 Desc: This is the entry point into the os where all of the kernels sub systems are 
       started up. 

 Notes: 

*****************************************************************************************/
int main(int argc,char **argv) {

  clearScreen(); /* Do A Clear Screen Just To Make The TEXT Buffer Nice And Empty */
  //smpInit();     /* Initialize SMP */

  /* Initialize Virtual Memory Manager */
  if (vmmInit() != 0x0) {
    kpanic("Error: Initializing VMM Subsystem.\n");
    }

  /* Initialize System Vital Sub System */
  if (initVitals() != 0x0) {
    kpanic("Error: Initializing vitals\n");
    }

  /* Initialize VFS(Virtual File System) Layer */
  if (vfsInit() != 0x0) {
    kpanic("Error: Initializing VFS\n");
    }

  /* Initialize The Systems 8259 Controller/IRQ Handling */
  if (init8259() != 0x0) {
    kpanic("Error: Initializing 8259\n");
    }

  /* Initialize The IDT Subsystem */
  if (idtInit() != 0x0) {
    kpanic("Error: Initializing IDT\n");
    }

  /* Initialize The Kernel Scheduler */
  if (schedInit() != 0x0) {
    kpanic("Error: Initializing: Scheduler\n");
    }

  /* Initialize The Systems Timer */
  if (pitInit(1000) != 0x0) {
    kpanic("Error: Initializing PIT\n");
    }

  /* Initialize The Systems Keyboard */
  if (keyboardInit() != 0x0) {
    kpanic("Error: Initializing Keyboard\n");
    }

  /* Initialize PCI Sub System */
  /*
  if (pciInit() != 0x0) {
    kpanic("Error: Initializing PCI\n");
    }
  */

  /* Sart Time */
  if (timeInit() != 0x0) {
    kpanic("Error: Initializing TIME\n");
    }

  /* Initialize Networking Subsystem */
  if (netInit() != 0x0) {
    kpanic("Error: Initializing Networking Subsystem\n");
    }

  /* Initialize The NE2000 NIC Device */
  if (ne2kInit(0x240) != 0x0) {
    kpanic("Error: Initializing NE2000\n");
    }

  if (devFSEnable() != 0x0) {
    kpanic("Error: Enabling devFS\n");
    }
  enableUbixFS();
  fdcInit();
  //initHardDisk();
  if (mount(0x0,0x0,0x0,0x0,"sys","rw") != 0x0) {
    kprintf("Problem Mounting sys Mount Point\n");
    }
  if (mount(0x0,0x0,0x1,0x0,"tmp","rw") != 0x0) {
    kprintf("Problem Mounting tmp Mount Point\n");
    }
  /*
  if (mount(0x1,0x1,0x0,0x0,"hd","rw") != 0x0) {
    kprintf("Problem Mounting HD Mount Point\n");
    }
  */
  execThread(idleTask,(uInt32)(kmalloc(0x2000)+0x2000),0x0);
  execFile("init",0x0,0x0,0x0);
  //execFile("shell",0x0,0x0,0x0);
  kprintf("Free Pages: [%i]\n",freePages); 
  kprintf("MemoryMap:  [0x%X]\n",vmmMemoryMap);
  kprintf("Starting Os\n");
  kprintf("argc: %i:%i\n",argc,argv[0]);
  irqEnable(0x0);
  sched();
  return(0x0);
  }

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

Function: _start();
Description: This Is The Kernels Main Entry Point From Here We Must
             Re-Setup Our GDT And Set All Registers To Their Default
             Values So We Are Sure Of No Error When The OS Boots Up Also
             We Have To Quickly Set Up Our Interrupt Table Because We Do
             Not Have Any Fault Protection Yet

Notes:

02/17/03 - I'm Unhappy With The Infinite Loop I Decided To Do If The Call
           To Main Returns I Think I Shall Change It To A Kernel Panic
           At A Later Date Then Reboot The Machine

************************************************************************/
void _start(char *args, ...) {
  int    argc;
  char **argv;
  argv = &args;
  argc = * (int *) (argv - 1);
  asm ("pushl $2; popf");
  asm volatile(
    "lgdtl (loadGDT)        \n"
    "movw $0x10,%%ax        \n" // Select Ring 0 Data Segment
    "movw %%ax,%%ds         \n" // Set Default Segment
    "movw %%ax,%%es         \n" //   ""           ""
    "movw %%ax,%%fs         \n" //   ""           ""
    "movw %%ax,%%gs         \n" //   ""           ""
    "movw %%ax,%%ss         \n" //   ""           ""
    "movl $0x2000,%%esp     \n" // Set Default Stack Pointer Its The End Of First Page
    "movl $0x2000,%%ebp     \n"
    "mov $0x18,%%ax         \n" // Set Up Dummy LDT
    "lldt %%ax              \n" // Load The Dummy LDT
    "mov $0x20,%%ax         \n" // Set Up Dummy TSS
    "ltr %%ax               \n" // Load The Dummy TSS
    "ljmp $0x08,$next       \n"
    "next:                  \n"
    :
    : "r" (ubixGDT), "p" (kernelStack+8192)
    : "%eax"
    );
  main(argc,argv);
  kpanic("We Should Not Get This Far\n");
  }

/***

 $Log$
 Revision 1.29  2004/05/25 15:50:43  reddawg
 mpiSpam() test

 Revision 1.28  2004/05/23 01:10:35  reddawg
 Fixes: Started to fix re-entrancy issues many more to look into

 Revision 1.27  2004/05/21 21:15:04  reddawg
 Fixed a few bugs which prevented the system from loadin

 Revision 1.26  2004/05/20 22:54:02  reddawg
 Cleaned Up Warrnings

 Revision 1.25  2004/05/19 17:28:28  reddawg
 Added the correct endTask Procedure

 Revision 1.24  2004/05/19 04:07:42  reddawg
 kmalloc(size,pid) no more it is no kmalloc(size); the way it should of been

 Revision 1.23  2004/05/19 03:35:02  reddawg
 Fixed A Few Ordering Issues In The Service Startup Routine

 Revision 1.22  2004/05/19 01:21:29  reddawg
 Tweaked

 Revision 1.21  2004/05/18 10:45:41  reddawg
 Bug Fix

 Revision 1.20  2004/05/10 02:23:24  reddawg
 Minor Changes To Source Code To Prepare It For Open Source Release

 Revision 1.19  2004/05/08 19:06:24  reddawg
 Going to impliment FSTAB

 Revision 1.18  2004/05/04 23:38:24  reddawg
 make clean all install

 Revision 1.17  2004/05/04 23:31:01  reddawg
 Ok Mark Cleaned Up Boot This

 Revision 1.16  2004/05/02 14:34:21  reddawg
 SMP Support Thanks To Some Help From Slowcoder now to make it interface
 well with the rest of the kernel

 Revision 1.15  2004/04/30 17:04:23  reddawg
 Ok GUI is back on

 Revision 1.14  2004/04/30 16:58:37  reddawg
 Turned On PCI Detections

 Revision 1.13  2004/04/30 16:29:34  reddawg
 Turned Off Gui

 Revision 1.12  2004/04/30 13:40:34  reddawg
 Just doing spring cleaning for the new members

 Revision 1.7  2004/04/26 22:27:36  reddawg
 Now mounts our harddrive I need to either impliment fstab or do something similar

 Revision 1.6  2004/04/25 04:35:50  reddawg
 Minor bug fixes to set default partition information

 Revision 1.107  2004/04/13 16:36:33  reddawg
 Changed our copyright, it is all now under a BSD-Style license

 END
 ***/

