// //The main entry of OS.When the OS kernel is loaded into memory and all hardware //context is initialized OK,Hello China OS's kernel will run from here. //This is a never finish(infinite) routine and will never end unless powers off //the system or reboot the system. //It's main functions are initializing all system level objects and modules,loading kernel //mode hardware drivers,loading external function modules(such as GUI and network),then //creating several kernel thread(s) and entering a dead loop. //But the dead loop codes only run a short time since the kernel thread(s) will be //scheduled once system clock occurs and the dead loop will end. // void __OS_Entry() { __KERNEL_THREAD_OBJECT* lpIdleThread = NULL; __KERNEL_THREAD_OBJECT* lpShellThread = NULL; #ifdef __CFG_USE_EOS __KERNEL_THREAD_OBJECT* lpUserThread = NULL; #endif DWORD dwIndex = 0; CHAR strInfo[64]; char* pszErrorMsg = "INIT: OK"; //Print out welcome message. //Please note the output should put here that before the System.BeginInitialization routine, //since it may cause the interrupt enable,which will lead the failure of system initialization. /*ClearScreen(); PrintStr(pszStartMsg1); PrintStr(pszStartMsg2); GotoHome(); ChangeLine(); PrintStr(pszHelpInfo); //Print out help information. GotoHome(); ChangeLine();*/ //Prepare the OS initialization environment.It's worth noting that even the System //object self is not initialized yet. if(!System.BeginInitialize((__COMMON_OBJECT*)&System)) { pszErrorMsg = "INIT ERROR: System.BeginInitialization routine failed."; goto __TERMINAL; } //Initialize memory management object.This object must be initialized before any other //system level objects since it's function maybe required by them. if(!AnySizeBuffer.Initialize(&AnySizeBuffer)) { pszErrorMsg = "INIT ERROR: Failed to initialize AnySizeBuffer object."; goto __TERMINAL; } #ifdef __I386__ // load display device dwIndex = 0; while(DisplayEntryArray[dwIndex]) { if(!DisplayManager.LoadDisplay(DisplayEntryArray[dwIndex])) //Failed to load. { //sprintf(strInfo,"Failed to load the %dth driver.",dwIndex); //Show an error. //PrintLine(strInfo); } dwIndex ++; //Continue to load. } CD_InitDisplay(0); #endif ClearScreen(); PrintStr(pszStartMsg1); PrintStr(pszStartMsg2); GotoHome(); ChangeLine(); PrintStr(pszHelpInfo); //Print out help information. GotoHome(); ChangeLine(); #ifdef __CFG_SYS_VMM //Enable VMM. *(__PDE*)PD_START = NULL_PDE; //Set the first page directory entry to NULL,to indicate //this location is not initialized yet. #endif //******************************************************************************** // //The following code initializes system level global objects. // //******************************************************************************** #ifdef __CFG_SYS_VMM //Should enable virtual memory model. lpVirtualMemoryMgr = (__VIRTUAL_MEMORY_MANAGER*)ObjectManager.CreateObject(&ObjectManager, NULL, OBJECT_TYPE_VIRTUAL_MEMORY_MANAGER); //Create virtual memory manager object. if(NULL == lpVirtualMemoryMgr) //Failed to create this object. { pszErrorMsg = "INIT ERROR: Can not create VirtualMemoryManager object."; goto __TERMINAL; } if(!lpVirtualMemoryMgr->Initialize((__COMMON_OBJECT*)lpVirtualMemoryMgr)) { pszErrorMsg = "INIT ERROR: Can not initialize VirtualMemoryManager object."; goto __TERMINAL; } #endif //Initialize Kernel Thread Manager object. if(!KernelThreadManager.Initialize((__COMMON_OBJECT*)&KernelThreadManager)) { pszErrorMsg = "INIT ERROR: Can not initialize KernelThreadManager object."; goto __TERMINAL; } //Initialize System object. if(!System.Initialize((__COMMON_OBJECT*)&System)) { pszErrorMsg = "INIT ERROR: Can not initialize System object."; goto __TERMINAL; } //Set the general interrupt handler,after this action,interrupt and //exceptions in system can be handled by System object. //This routine must be called earlier than any modules who will use //system call. #ifdef __I386__ SetGeneralIntHandler(GeneralIntHandler); #endif #ifdef __CFG_SYS_VMM //Initialize PageFrmaeManager object. if(!PageFrameManager.Initialize((__COMMON_OBJECT*)&PageFrameManager, (LPVOID)0x02000000, (LPVOID)0x09FFFFFF)) { pszErrorMsg = "INIT ERROR: Can not initialize PageFrameManager object."; goto __TERMINAL; } #endif //Device Driver Framework related global functions. #ifdef __CFG_SYS_DDF //Initialize IOManager object. if(!IOManager.Initialize((__COMMON_OBJECT*)&IOManager)) { pszErrorMsg = "INIT ERROR: Can not initialize IOManager object."; goto __TERMINAL; } //Initialize DeviceManager object. if(!DeviceManager.Initialize(&DeviceManager)) { pszErrorMsg = "INIT ERROR: Can not initialize DeviceManager object."; goto __TERMINAL; } #endif //Initialize CPU statistics object. #ifdef __CFG_SYS_CPUSTAT if(!StatCpuObject.Initialize(&StatCpuObject)) { pszErrorMsg = "INIT ERROR: Can not initialize StatCpuObject."; goto __TERMINAL; } #endif //Enable the virtual memory management mechanism if __CFG_SYS_VMM flag is defined. #ifdef __CFG_SYS_VMM EnableVMM(); #endif //******************************************************************************** // //The following code loads all inline device drivers,and external drivers implemented as //external module. // //******************************************************************************** #ifdef __CFG_SYS_DDF dwIndex = 0; while(DriverEntryArray[dwIndex]) { if(!IOManager.LoadDriver(DriverEntryArray[dwIndex])) //Failed to load. { _hx_sprintf(strInfo,"Failed to load the %dth driver.",dwIndex); //Show an error. PrintLine(strInfo); } dwIndex ++; //Continue to load. } #endif //Initialize Console object if necessary. #ifdef __CFG_SYS_CONSOLE if(!Console.Initialize(&Console)) { pszErrorMsg = "INIT ERROR: Can not initialize Console object."; goto __TERMINAL; } #endif //BISStartup(); //******************************************************************************** // //The following code creates all system level kernel threads. // //******************************************************************************** //The first one is IDLE thread,which will be scheduled when no thread need to schedule, //so it's priority is the lowest one in system,which is PRIORITY_LEVEL_LOWEST. //Also need to mention that this thread is mandatory and without any switch to turn off //it. lpIdleThread = KernelThreadManager.CreateKernelThread( (__COMMON_OBJECT*)&KernelThreadManager, 0, KERNEL_THREAD_STATUS_READY, PRIORITY_LEVEL_LOWEST, SystemIdle, NULL, NULL, "IDLE"); if(NULL == lpIdleThread) { pszErrorMsg = "INIT ERROR: Can not create SystemIdle kernel thread."; goto __TERMINAL; } //Create statistics kernel thread. #ifdef __CFG_SYS_CPUSTAT lpStatKernelThread = KernelThreadManager.CreateKernelThread( (__COMMON_OBJECT*)&KernelThreadManager, 0, KERNEL_THREAD_STATUS_READY, PRIORITY_LEVEL_HIGH, //With high priority. StatThreadRoutine, NULL, NULL, "CPU STAT"); if(NULL == lpStatKernelThread) { pszErrorMsg = "INIT ERROR: Can not create CPU_Stat kernel thread."; goto __TERMINAL; } #endif //Create shell thread.The shell thread's implementation code resides in shell.cpp //file in shell directory. #ifdef __CFG_SYS_SHELL //Shell can be eleminated by turn off this switch. if(NULL == ModuleMgr.ShellEntry) //Use default shell. { lpShellThread = KernelThreadManager.CreateKernelThread( //Create shell thread. (__COMMON_OBJECT*)&KernelThreadManager, 0, KERNEL_THREAD_STATUS_READY, PRIORITY_LEVEL_HIGH, ShellEntryPoint, NULL, NULL, "SHELL"); if(NULL == lpShellThread) { pszErrorMsg = "INIT ERROR: Can not create Shell kernel thread."; goto __TERMINAL; } } else //Use other kernel module specified shell. { lpShellThread = KernelThreadManager.CreateKernelThread( //Create shell thread. (__COMMON_OBJECT*)&KernelThreadManager, 0, KERNEL_THREAD_STATUS_READY, PRIORITY_LEVEL_HIGH, ModuleMgr.ShellEntry, NULL, NULL, "SHELL"); if(NULL == lpShellThread) { pszErrorMsg = "INIT ERROR: Can not create Shell kernel thread."; goto __TERMINAL; } } g_lpShellThread = lpShellThread; //Initialize the shell thread global variable. //Print out the default system prompt,which can be changed by 'sysname' command. //strcpy(&HostName[0],"[system-view]"); #endif //Initialize DeviceInputManager object. if(!DeviceInputManager.Initialize((__COMMON_OBJECT*)&DeviceInputManager, NULL, (__COMMON_OBJECT*)lpShellThread)) { pszErrorMsg = "INIT ERROR: Can not initialize DeviceInputManager object."; goto __TERMINAL; } //******************************************************************************** // //The following code creates user's main kernel thread if used as EOS. // //******************************************************************************** //Create user kernel thread. #ifdef __CFG_USE_EOS lpUserThread = KernelThreadManager.CreateKernelThread( //Create shell thread. (__COMMON_OBJECT*)&KernelThreadManager, 0, KERNEL_THREAD_STATUS_READY, __HCNMAIN_PRIORITY, _HCNMain, NULL, NULL, __HCNMAIN_NAME); if(NULL == lpUserThread) { pszErrorMsg = "INIT ERROR: Can not create User_Main kernel thread."; goto __TERMINAL; } #endif //If log debugging functions is enabled. #ifdef __CFG_SYS_LOGCAT DebugManager.Initialize(&DebugManager); lpLogcatDaemonThread = KernelThreadManager.CreateKernelThread( //Create logcat daemon thread. (__COMMON_OBJECT*)&KernelThreadManager, 0, KERNEL_THREAD_STATUS_READY, PRIORITY_LEVEL_NORMAL, LogcatDaemon, NULL, NULL, "Logcat Daemon"); if(NULL == lpLogcatDaemonThread) { pszErrorMsg = "INIT ERROR: Can not create Logcat_Daemon object."; goto __TERMINAL; } #endif //__CFG_SYS_LOGCAT. #ifdef __CFG_NET_IPv4 //IPv4 network protocol is enabled. if(!IPv4_Entry(NULL)) { pszErrorMsg = "INIT ERROR: Can not initialize IPv4 protocol function."; goto __TERMINAL; } if(!InitializeEthernetIf()) { pszErrorMsg = "INIT ERROR: Can not initialize Ethernet interface."; goto __TERMINAL; } #endif System.EndInitialize((__COMMON_OBJECT*)&System); //Enter a dead loop to wait for the scheduling of kernel threads. DeadLoop(); //The following code will never be executed if corrected. __TERMINAL: GotoHome(); ChangeLine(); PrintLine(pszErrorMsg); //Show error msg. DeadLoop(); }
// //The main entry of OS.When the OS kernel is loaded into memory and all hardware //context is initialized OK,Hello China OS's kernel will run from here. //This is a never finish(infinite) routine and will never end unless powers off //the system or reboot the system. //It's main functions are initializing all system level objects and modules,loading kernel //mode hardware drivers,loading external function modules(such as GUI and network),then //creating several kernel thread(s) and entering a dead loop. //But the dead loop codes only run a short time since the kernel thread(s) will be //scheduled once system clock occurs and the dead loop will end. // void __OS_Entry() { __KERNEL_THREAD_OBJECT* lpIdleThread = NULL; __KERNEL_THREAD_OBJECT* lpShellThread = NULL; #ifdef __CFG_USE_EOS __KERNEL_THREAD_OBJECT* lpUserThread = NULL; #endif DWORD dwIndex = 0; CHAR strInfo[64]; //Prepare the OS initialization environment.It's worth noting that even the System //object self is not initialized yet. if(!System.BeginInitialize((__COMMON_OBJECT*)&System)) { goto __TERMINAL; } //Initialize memory management object.This object must be initialized before any other //system level objects since it's function maybe required by them. if(!AnySizeBuffer.Initialize(&AnySizeBuffer)) { goto __TERMINAL; } //Print out welcome message. ClearScreen(); PrintStr(pszStartMsg1); PrintStr(pszStartMsg2); GotoHome(); ChangeLine(); PrintStr(pszHelpInfo); //Print out help information. GotoHome(); ChangeLine(); #ifdef __CFG_SYS_VMM //Enable VMM. *(__PDE*)PD_START = NULL_PDE; //Set the first page directory entry to NULL,to indicate //this location is not initialized yet. #endif //******************************************************************************** // //The following code initializes system level global objects. // //******************************************************************************** #ifdef __CFG_SYS_VMM //Should enable virtual memory model. lpVirtualMemoryMgr = (__VIRTUAL_MEMORY_MANAGER*)ObjectManager.CreateObject(&ObjectManager, NULL, OBJECT_TYPE_VIRTUAL_MEMORY_MANAGER); //Create virtual memory manager object. if(NULL == lpVirtualMemoryMgr) //Failed to create this object. { goto __TERMINAL; } if(!lpVirtualMemoryMgr->Initialize((__COMMON_OBJECT*)lpVirtualMemoryMgr)) { goto __TERMINAL; } #endif //Initialize Kernel Thread Manager object. if(!KernelThreadManager.Initialize((__COMMON_OBJECT*)&KernelThreadManager)) { goto __TERMINAL; } //Initialize System object. if(!System.Initialize((__COMMON_OBJECT*)&System)) { goto __TERMINAL; } //Set the general interrupt handler,after this action,interrupt and //exceptions in system can be handled by System object. //This routine must be called earlier than any modules who will use //system call. #ifdef __I386__ SetGeneralIntHandler(GeneralIntHandler); #endif #ifdef __CFG_SYS_VMM //Initialize PageFrmaeManager object. if(!PageFrameManager.Initialize((__COMMON_OBJECT*)&PageFrameManager, (LPVOID)0x02000000, (LPVOID)0x09FFFFFF)) { goto __TERMINAL; } #endif //Device Driver Framework related global functions. #ifdef __CFG_SYS_DDF //Initialize IOManager object. if(!IOManager.Initialize((__COMMON_OBJECT*)&IOManager)) { goto __TERMINAL; } //Initialize DeviceManager object. if(!DeviceManager.Initialize(&DeviceManager)) { goto __TERMINAL; } #endif //Initialize CPU statistics object. #ifdef __CFG_SYS_CPUSTAT if(!StatCpuObject.Initialize(&StatCpuObject)) { goto __TERMINAL; } #endif //Enable the virtual memory management mechanism if __CFG_SYS_VMM flag is defined. #ifdef __CFG_SYS_VMM EnableVMM(); #endif //******************************************************************************** // //The following code loads all inline device drivers,and external drivers implemented as //external module. // //******************************************************************************** #ifdef __CFG_SYS_DDF dwIndex = 0; while(DriverEntryArray[dwIndex]) { if(!IOManager.LoadDriver(DriverEntryArray[dwIndex])) //Failed to load. { sprintf(strInfo,"Failed to load the %dth driver.",dwIndex); //Show an error. PrintLine(strInfo); } dwIndex ++; //Continue to load. } //Initialize module manager object. //if(!ModuleMgr.Initialize(&ModuleMgr)) //{ // PrintLine(" Can not initialize module manager object."); // goto __TERMINAL; //} #endif //Initialize Console object if necessary. #ifdef __CFG_SYS_CONSOLE if(!Console.Initialize(&Console)) { PrintLine("Can not initialize Console object."); } else { //PrintLine("Initialize Console object successfully."); } #endif //Load external modules. //ModuleMgr.LoadExternalMod("MODCFG.INI"); //MODCFG.INI is the module's configure file. //PrintLine("Load external module is disabled for debugging."); //******************************************************************************** // //The following code creates all system level kernel threads. // //******************************************************************************** //The first one is IDLE thread,which will be scheduled when no thread need to schedule, //so it's priority is the lowest one in system,which is PRIORITY_LEVEL_LOWEST. //Also need to mention that this thread is mandatory and without any switch to turn off //it. lpIdleThread = KernelThreadManager.CreateKernelThread( (__COMMON_OBJECT*)&KernelThreadManager, 0, KERNEL_THREAD_STATUS_READY, PRIORITY_LEVEL_LOWEST, SystemIdle, NULL, NULL, "IDLE"); if(NULL == lpIdleThread) { //PrintLine("Can not create idle kernel thread,please restart the system."); __ERROR_HANDLER(ERROR_LEVEL_FATAL,0,NULL); goto __TERMINAL; } //Create statistics kernel thread. #ifdef __CFG_SYS_CPUSTAT lpStatKernelThread = KernelThreadManager.CreateKernelThread( (__COMMON_OBJECT*)&KernelThreadManager, 0, KERNEL_THREAD_STATUS_READY, PRIORITY_LEVEL_HIGH, //With high priority. StatThreadRoutine, NULL, NULL, "CPU STAT"); if(NULL == lpStatKernelThread) { //PrintLine("Can not create idle kernel thread,please restart the system."); __ERROR_HANDLER(ERROR_LEVEL_FATAL,0,NULL); goto __TERMINAL; } #endif //Create shell thread.The shell thread's implementation code resides in shell.cpp //file in shell directory. #ifdef __CFG_SYS_SHELL //Shell can be eleminated by turn off this switch. if(NULL == ModuleMgr.ShellEntry) //Use default shell. { lpShellThread = KernelThreadManager.CreateKernelThread( //Create shell thread. (__COMMON_OBJECT*)&KernelThreadManager, 0, KERNEL_THREAD_STATUS_READY, PRIORITY_LEVEL_NORMAL, ShellEntryPoint, NULL, NULL, "SHELL"); if(NULL == lpShellThread) { __ERROR_HANDLER(ERROR_LEVEL_FATAL,0,NULL); goto __TERMINAL; } } else //Use other kernel module specified shell. { lpShellThread = KernelThreadManager.CreateKernelThread( //Create shell thread. (__COMMON_OBJECT*)&KernelThreadManager, 0, KERNEL_THREAD_STATUS_READY, PRIORITY_LEVEL_NORMAL, ModuleMgr.ShellEntry, NULL, NULL, "SHELL"); if(NULL == lpShellThread) { __ERROR_HANDLER(ERROR_LEVEL_FATAL,0,NULL); goto __TERMINAL; } } g_lpShellThread = lpShellThread; //Initialize the shell thread global variable. //Print out the default system prompt,which can be changed by 'sysname' command. strcpy(&HostName[0],"[system-view]"); #endif //Initialize DeviceInputManager object. if(!DeviceInputManager.Initialize((__COMMON_OBJECT*)&DeviceInputManager, NULL, (__COMMON_OBJECT*)lpShellThread)) { __ERROR_HANDLER(ERROR_LEVEL_FATAL,0,NULL); goto __TERMINAL; } //******************************************************************************** // //The following code creates user's main kernel thread if used as EOS. // //******************************************************************************** //Create user kernel thread. #ifdef __CFG_USE_EOS lpUserThread = KernelThreadManager.CreateKernelThread( //Create shell thread. (__COMMON_OBJECT*)&KernelThreadManager, 0, KERNEL_THREAD_STATUS_READY, __HCNMAIN_PRIORITY, _HCNMain, NULL, NULL, __HCNMAIN_NAME); if(NULL == lpUserThread) { __ERROR_HANDLER(ERROR_LEVEL_FATAL,0,NULL); goto __TERMINAL; } #endif //Finish OS initialization phase by calling EndInitialize routine of System object,coresponding the //BeginInitialize routine call before initialization. System.EndInitialize((__COMMON_OBJECT*)&System); //Enter a dead loop to wait for the scheduling of kernel threads. DeadLoop(); //The following code will never be executed if corrected. __TERMINAL: GotoHome(); ChangeLine(); __ERROR_HANDLER(ERROR_LEVEL_FATAL,0,"Initializing process failed!"); DeadLoop(); }