Kernel/x86 - Multiboot module cleanup
[tpg/acess2.git] / KernelLand / 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 <pmemmap.h>
12 #include <semaphore.h>
13
14 #define VGA_ERRORS      0
15
16 #define KERNEL_LOAD     0x100000        // 1MiB
17 #define MAX_ARGSTR_POS  (0x400000-0x2000)
18 #define MAX_PMEMMAP_ENTS        16
19
20 // === IMPORTS ===
21 extern char     gKernelEnd[];
22 extern void     MM_PreinitVirtual(void);
23 extern void     MM_Install(int NPMemRanges, tPMemMapEnt *PMemRanges);
24 extern void     MM_InstallVirtual(void);
25 extern int      Time_Setup(void);
26 extern int      ACPICA_Initialise(void);
27 // - Modules/Display/VESA
28 extern void     VBE_int_SetBootMode(Uint16 ModeID, const void *ModeInfo);
29
30 // === PROTOTYPES ===
31  int    kmain(Uint MbMagic, void *MbInfoPtr);
32
33 // === GLOBALS ===
34 char    *gsBootCmdLine = NULL;
35 tBootModule     *gaArch_BootModules;
36  int    giArch_NumBootModules = 0;
37
38 // === CODE ===
39 int kmain(Uint MbMagic, void *MbInfoPtr)
40 {
41         tMBoot_Info     *mbInfo;
42         tPMemMapEnt     pmemmap[MAX_PMEMMAP_ENTS];
43          int    nPMemMapEnts;
44
45         LogF("%s\r\n", gsBuildInfo);
46         
47         MM_PreinitVirtual();    // Initialise virtual mappings
48
49         mbInfo = MbInfoPtr;     
50
51         switch(MbMagic)
52         {
53         // Multiboot 1
54         case MULTIBOOT_MAGIC: {
55                 // TODO: Handle when this isn't in the mapped area
56                 ASSERT( mbInfo->CommandLine < 4*1024*1024 );
57                 gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
58
59                 // Adjust Multiboot structure address
60                 mbInfo = (void*)( (tVAddr)MbInfoPtr + KERNEL_BASE );
61
62                 // Parse memory map
63                 // - mbInfo->Flags is checked in this function
64                 nPMemMapEnts = Multiboot_LoadMemoryMap(mbInfo, KERNEL_BASE, pmemmap, MAX_PMEMMAP_ENTS,
65                         KERNEL_LOAD, (tVAddr)&gKernelEnd - KERNEL_BASE);
66                 
67                 
68                 // Get video mode
69                 Debug("mbInfo->Flags = 0x%x", mbInfo->Flags);
70                 if( mbInfo->Flags & (1 << 11) )
71                 {
72                         // TODO: Ensure address is in mapped area
73                         ASSERT( mbInfo->vbe_mode_info < 4*1024*1024 );
74                         VBE_int_SetBootMode(mbInfo->vbe_mode, (void*)(mbInfo->vbe_mode_info + KERNEL_BASE));
75                 }
76
77                 break; }
78         
79         // Multiboot 2
80         case MULTIBOOT2_MAGIC:
81                 Panic("Multiboot 2 not yet supported");
82                 //MM_InstallMBoot2( MbInfo );   // Set up physical memory manager
83                 return 0;
84                 break;
85         
86         default:
87                 Panic("Multiboot magic invalid %08x, expected %08x or %08x\n",
88                         MbMagic, MULTIBOOT_MAGIC, MULTIBOOT2_MAGIC);
89                 return 0;
90         }
91
92         // Set up physical memory manager
93         MM_Install(nPMemMapEnts, pmemmap);
94         
95         MM_InstallVirtual();    // Clean up virtual address space
96         Heap_Install();         // Create initial heap
97         Time_Setup();   // Initialise timing
98         
99         // Start Multitasking
100         Threads_Init();
101
102         #if USE_ACPICA
103         // Poke ACPICA
104         ACPICA_Initialise();
105         #endif
106
107         Log_Log("Arch", "Starting VFS...");
108         // Load Virtual Filesystem
109         VFS_Init();
110         
111         // Load initial modules
112         gaArch_BootModules = Multiboot_LoadModules(mbInfo, KERNEL_BASE, &giArch_NumBootModules);
113         
114         // Pass on to Independent Loader
115         Log_Log("Arch", "Starting system");
116         System_Init(gsBootCmdLine);
117         
118         // Sleep forever (sleeping beauty)
119         for(;;)
120                 Threads_Sleep();
121         return 0;
122 }
123
124 void Arch_LoadBootModules(void)
125 {
126         for( int i = 0; i < giArch_NumBootModules; i ++ )
127         {
128                 const tBootModule       *mod = &gaArch_BootModules[i];
129                 Log_Log("Arch", "Loading (%p[%P]+%x) '%s'",
130                         mod->Base, mod->PBase, mod->Size,
131                         mod->ArgString);
132                 
133                 if( !Module_LoadMem( mod->Base, mod->Size, mod->ArgString) ) {
134                         Log_Warning("Arch", "Unable to load module");
135                         continue ;
136                 }
137         }
138         Log_Log("Arch", "Boot modules loaded");
139         Multiboot_FreeModules(giArch_NumBootModules, gaArch_BootModules);
140         giArch_NumBootModules = 0;
141         gaArch_BootModules = NULL;
142 }

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