2dca563503507ba58c154e27b861f371bd42aeb2
[tpg/acess2.git] / Kernel / arch / x86 / main.c
1 /*
2  * Acess 2
3  * x86 Kernel Main
4  * arch/x86/main.c
5  */
6 #include <acess.h>
7 #include <mboot.h>
8 #include <multiboot2.h>
9 #include <init.h>
10 #include <mm_virt.h>
11 #include <mp.h>
12
13 #define VGA_ERRORS      0
14
15 // === IMPORTS ===
16 extern void     Heap_Install(void);
17 extern void     Desctab_Install(void);
18 extern void     MM_PreinitVirtual(void);
19 extern void     MM_Install(tMBoot_Info *MBoot);
20 extern void MM_InstallVirtual(void);
21 extern void     Threads_Init(void);
22 extern int      Time_Setup(void);
23 extern Uint     Proc_Clone(Uint *Err, Uint Flags);
24 extern void     Threads_Sleep(void);
25 extern void     Threads_Exit(void);
26 // --- Core ---
27 extern void     System_Init(char *Commandline);
28
29 // === PROTOTYPES ===
30 void    Arch_LoadBootModules(void);
31
32 // === GLOBALS ===
33 char    *gsBootCmdLine = NULL;
34 struct {
35         void    *Base;
36         Uint    Size;
37         char    *ArgString;
38 }       *gaArch_BootModules;
39  int    giArch_NumBootModules = 0;
40
41 // === CODE ===
42 int kmain(Uint MbMagic, void *MbInfoPtr)
43 {
44          int    i;
45         tMBoot_Module   *mods;
46         tMBoot_Info     *mbInfo;
47         
48         Log("MbMagic = %08x", MbMagic);
49         Log("MbInfoPtr = %p", MbInfoPtr);
50         
51         // Set up non-boot info dependent stuff
52         Desctab_Install();      // Set up GDT and IDT
53         MM_PreinitVirtual();    // Initialise vital mappings
54         
55         switch(MbMagic)
56         {
57         // Multiboot 1
58         case MULTIBOOT_MAGIC:
59                 // Adjust Multiboot structure address
60                 mbInfo = (void*)( (Uint)MbInfoPtr + KERNEL_BASE );
61                 gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
62                 
63                 MM_Install( mbInfo );   // Set up physical memory manager
64                 break;
65         
66         // Multiboot 2
67         case MULTIBOOT2_MAGIC:
68                 Warning("Multiboot 2 Not yet supported");
69                 //MM_InstallMBoot2( MbInfo );   // Set up physical memory manager
70                 return 0;
71                 break;
72         
73         default:
74                 Panic("Multiboot magic invalid %08x, expected %08x or %08x\n",
75                         MbMagic, MULTIBOOT_MAGIC, MULTIBOOT2_MAGIC);
76                 return 0;
77         }
78         
79         MM_InstallVirtual();    // Clean up virtual address space
80         Heap_Install();         // Create initial heap
81         
82         //Log_Log("Arch", "Starting Multitasking...");
83         // Start Multitasking
84         Threads_Init();
85         
86         // Start Timers
87         Time_Setup();
88         
89         Log_Log("Arch", "Starting VFS...");
90         // Load Virtual Filesystem
91         VFS_Init();
92         
93         // Load initial modules
94         mods = (void*)( mbInfo->Modules + KERNEL_BASE );
95         giArch_NumBootModules = mbInfo->ModuleCount;
96         gaArch_BootModules = malloc( giArch_NumBootModules * sizeof(*gaArch_BootModules) );
97         for( i = 0; i < mbInfo->ModuleCount; i ++ )
98         {
99                 // Adjust into higher half
100                 mods[i].Start  += KERNEL_BASE;
101                 mods[i].End    += KERNEL_BASE;
102                 mods[i].String += KERNEL_BASE;
103                 
104                 gaArch_BootModules[i].Base = (void *)mods[i].Start;
105                 gaArch_BootModules[i].Size = mods[i].End - mods[i].Start;
106                 gaArch_BootModules[i].ArgString = (char *)mods[i].String;
107         }
108         
109         // Pass on to Independent Loader
110         Log_Log("Arch", "Starting system");
111         System_Init(gsBootCmdLine);
112         
113         // Sleep forever (sleeping beauty)
114         for(;;)
115                 Threads_Sleep();
116         return 0;
117 }
118
119 void Arch_LoadBootModules(void)
120 {
121          int    i;
122         for( i = 0; i < giArch_NumBootModules; i ++ )
123         {
124                 Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString);
125                 
126                 if( !Module_LoadMem( gaArch_BootModules[i].Base, gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString ) )
127                 {
128                         Log_Warning("Arch", "Unable to load module\n");
129                 }
130         }
131         Log_Log("Arch", "Boot modules loaded");
132         free( gaArch_BootModules );
133 }

UCC git Repository :: git.ucc.asn.au