/*
 * 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 <arch/rtl_irqs.h>
#include <arch/idle.h>
#include <arch/page.h>
#include <arch/context.h>
#include <deblin/vhal.h>
#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 <arch/memory.h>
#include <rtl_conf.h>
#include <rtl_tty.h>
#include <unistd.h>

unsigned long stack_idletask[100];
unsigned long mprot_prueba;

extern void start_kernel(void)
{
 int aux;
 
 SET_IDLE_STACK;
 
 ClearVHAL();
 
 // Here interrupts are disable
 

 DebugString("Start start_kernel function                       ");


 setup_arch();  // PIC programming.
                // Mask all interrupts.
		// Interrupts are mapped in 0x20-0x30 IDT entries

#if CONFIG_KERNEL_MEMORYPROT
 init_page();
 DebugString("Kernel Memory Protect Enable                      ");
#endif

#if CONFIG_CONTEXT_MEMORYPROT
 init_context();
 DebugString("Context Memory Protect Enable                     ");
#endif
 

#if CONFIG_RTL_GDBAGENT
 init_gdbagent();
#endif

#if _RTL_STARTBREAKPOINT 
 DebugString("Connect with remote target ...                  ");
 BREAKPOINT();
#endif


 DebugString("Start Memory Allocator Initialization           ");
 init_memory();
 
 rtl_allow_interrupts();

 DebugString("Start Clocks Initialization                     ");
 init_clocks();
 
 DebugString("Start Scheduler Initialization                  ");
 init_sched();
 
#if _RTL_POSIX_IO
 DebugString("Start PosixIO Initialization                   ");
 init_posixio();
#endif

 init_rt_terminal();

 close(STDOUT);
 STDOUT = open("/dev/rt_tty",0);

 
#if CONFIG_RTL_BAKERTEST
 // extern int init_baker_test(void);
 init_baker_test();
#else
 rtl_stop_interrupts();
 init_tasks();
 rtl_allow_interrupts();
#endif

 // This is the Idle Task

infinito:
 goto infinito;
 
}

