/*
 * tasks.c
 *
 * Written by Vicente Esteve LLoret <viesllo@inf.upv.es>
 * Copyright (C) Feb, 2003 OCERA Consortium.
 *
 * This program 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 version 2.
 *
 * StandAlone RTLinux Initialization
 */

#include <rtl_tasks.h>
#include <rtl_sched.h>
#include <rtl_time.h>
#include <rtl_sema.h>
#include <rtl_sync.h>
#include <rtl_debug.h>
#include <rtl_posixio.h>
#include <rtl_archdep.h>
#include <rtl_conf.h>
#include <rtl_tty.h>
#include <unistd.h>

#ifdef CONFIG_XENO
#include <arch/hypervisor.h>
#endif
#ifdef CONFIG_XTRATUM
#include <arch/XtratuM_API.h>
#endif

unsigned long stack_idletask[100];
unsigned long mprot_prueba;

extern void start_kernel(void)
{
 
// SET_IDLE_STACK; 

 
 // Here interrupts are disable

#ifdef CONFIG_XTRATUM
 xm_write_domain("Setup_arch\n",13);
#endif
 setup_arch();  // PIC programming.
                // Mask all interrupts.
		// Interrupts are mapped in 0x20-0x30 IDT entries

#if CONFIG_KERNEL_MEMORYPROT
 init_page();
#endif

#if CONFIG_CONTEXT_MEMORYPROT
 init_context();
#endif
 

#if CONFIG_RTL_GDBAGENT
 init_gdbagent();
#endif

#if _RTL_STARTBREAKPOINT 
 BREAKPOINT();
#endif

#ifdef CONFIG_XTRATUM
 xm_write_domain("Init Memory\n",13);
#endif


 init_memory();
 rtl_allow_interrupts();

#ifdef CONFIG_XTRATUM
 xm_write_domain("Init Clocks\n",13);
#endif


 init_clocks();
 
#if _RTL_POSIX_IO
#ifdef CONFIG_XTRATUM
 xm_write_domain("Init PosixIO\n",13);
#endif
 init_posixio();
#endif

//
// Register Stdin /Stdout
//


#if DEVICE_I386_TERMINAL 
 init_i386_terminal();
 close(STDOUT);
 STDOUT = open("/dev/rt_tty",0);
#endif

#if DEVICE_I386_SERIAL_MOUSE
 init_i386_serial_mouse();
 close(STDMOUSE);
 STDMOUSE = open("/dev/rt_serial_mouse",0);
#endif
#if DEVICE_I386_PS2_MOUSE
 init_i386_ps2_mouse();
 close(STDMOUSE);
 STDMOUSE = open("/dev/rt_ps_mouse",0);
#endif


#if DEVICE_I386_KBD
 init_i386_kbd();
 close(STDIN);
 STDIN = open("/dev/rt_kbd",0);
#endif


#if DEVICE_SA1100_SERIAL 
 init_sa1100_serial();
 close(STDOUT);
 STDOUT = open("/dev/sa1100_serial",0);
#endif

#if DEVICE_XTRATUM_TERMINAL
  init_XtratuM_terminal();
  close(STDOUT);
  STDOUT = open("/dev/XtratuM_terminal",0);
#endif
#if DEVICE_XTRATUM_MOUSE
  init_XtratuM_mouse();
  close(STDMOUSE);
  STDMOUSE = open("/dev/XtratuM_mouse",0);
#endif
#if DEVICE_XTRATUM_KBD
  init_XtratuM_kbd();
  close(STDIN);
  STDIN = open("/dev/XtratuM_kbd",0);
#endif
 
#ifdef CONFIG_XTRATUM
 xm_write_domain("Init Sched\n",13);
#endif

 init_sched();
 
 rtl_stop_interrupts();
 init_tasks();
 rtl_allow_interrupts();

 // This is the Idle Task
infinito:

#if CONFIG_XENO
  for ( ; ; ) HYPERVISOR_yield();
#endif
#if CONFIG_XTRATUM
  for ( ; ; ) xm_suspend_domain();
#endif
  goto infinito;
 
}

