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

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