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

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