Fixing handling of Multiboot modules
[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 #define MAX_ARGSTR_POS  (0x400000-0x2000)
16
17 // === IMPORTS ===
18 extern void     Heap_Install(void);
19 extern void     Desctab_Install(void);
20 extern void     MM_PreinitVirtual(void);
21 extern void     MM_Install(tMBoot_Info *MBoot);
22 extern void MM_InstallVirtual(void);
23 extern void     Threads_Init(void);
24 extern int      Time_Setup(void);
25 extern Uint     Proc_Clone(Uint *Err, Uint Flags);
26 extern void     Threads_Sleep(void);
27 // --- Core ---
28 extern void     System_Init(char *Commandline);
29
30 // === PROTOTYPES ===
31 void    Arch_LoadBootModules(void);
32
33 // === GLOBALS ===
34 char    *gsBootCmdLine = NULL;
35 struct {
36         void    *Base;
37         Uint    Size;
38         char    *ArgString;
39 }       *gaArch_BootModules;
40  int    giArch_NumBootModules = 0;
41
42 // === CODE ===
43 int kmain(Uint MbMagic, void *MbInfoPtr)
44 {
45          int    i;
46         tMBoot_Module   *mods;
47         tMBoot_Info     *mbInfo;
48         
49         Log("MbMagic = %08x", MbMagic);
50         Log("MbInfoPtr = %p", MbInfoPtr);
51         
52         // Set up non-boot info dependent stuff
53         Desctab_Install();      // Set up GDT and IDT
54         MM_PreinitVirtual();    // Initialise virtual mappings
55         
56         switch(MbMagic)
57         {
58         // Multiboot 1
59         case MULTIBOOT_MAGIC:
60                 // Adjust Multiboot structure address
61                 mbInfo = (void*)( (Uint)MbInfoPtr + KERNEL_BASE );
62                 gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
63                 
64                 MM_Install( mbInfo );   // Set up physical memory manager
65                 break;
66         
67         // Multiboot 2
68         case MULTIBOOT2_MAGIC:
69                 Warning("Multiboot 2 Not yet supported");
70                 //MM_InstallMBoot2( MbInfo );   // Set up physical memory manager
71                 return 0;
72                 break;
73         
74         default:
75                 Panic("Multiboot magic invalid %08x, expected %08x or %08x\n",
76                         MbMagic, MULTIBOOT_MAGIC, MULTIBOOT2_MAGIC);
77                 return 0;
78         }
79         
80         MM_InstallVirtual();    // Clean up virtual address space
81         Heap_Install();         // Create initial heap
82         
83         //Log_Log("Arch", "Starting Multitasking...");
84         // Start Multitasking
85         Threads_Init();
86         
87         // Start Timers
88         Time_Setup();
89         
90         Log_Log("Arch", "Starting VFS...");
91         // Load Virtual Filesystem
92         VFS_Init();
93         
94         // Load initial modules
95         mods = (void*)( mbInfo->Modules + KERNEL_BASE );
96         giArch_NumBootModules = mbInfo->ModuleCount;
97         gaArch_BootModules = malloc( giArch_NumBootModules * sizeof(*gaArch_BootModules) );
98         for( i = 0; i < mbInfo->ModuleCount; i ++ )
99         {
100                  int    ofs;
101                 // TODO: Handle this better by using MM_MapHW/MM_MapTemp
102                 // Adjust into higher half
103                 //mods[i].Start  += KERNEL_BASE;
104                 //mods[i].End    += KERNEL_BASE;
105                 //mods[i].String += KERNEL_BASE;
106                 
107                 gaArch_BootModules[i].Size = mods[i].End - mods[i].Start;
108                 
109                 ofs = mods[i].Start&0xFFF;
110                 gaArch_BootModules[i].Base = (void*)( MM_MapHWPages(mods[i].Start,
111                         (gaArch_BootModules[i].Size+ofs+0xFFF) / 0x1000
112                         ) + ofs );
113                 //gaArch_BootModules[i].ArgString = MM_MapHW(mods[i].String, 1)
114                 // + (mods[i].String&0xFFF);
115                 
116                 if( (tVAddr)mods[i].String > MAX_ARGSTR_POS )
117                 {
118                         gaArch_BootModules[i].ArgString = (void*)(
119                                 MM_MapHWPages((tVAddr)mods[i].String, 2)
120                                 + ((tVAddr)mods[i].String&0xFFF)
121                                 );
122                 }
123                 else
124                         gaArch_BootModules[i].ArgString = (char *)mods[i].String + KERNEL_BASE;
125         }
126         
127         // Pass on to Independent Loader
128         Log_Log("Arch", "Starting system");
129         System_Init(gsBootCmdLine);
130         
131         // Sleep forever (sleeping beauty)
132         for(;;)
133                 Threads_Sleep();
134         return 0;
135 }
136
137 void Arch_LoadBootModules(void)
138 {
139          int    i;
140         for( i = 0; i < giArch_NumBootModules; i ++ )
141         {
142                 Log_Debug("Arch", "Module %i: %p - %p 0x%x",
143                         i, gaArch_BootModules[i].ArgString,
144                         gaArch_BootModules[i].Base, gaArch_BootModules[i].Size
145                         );
146                 Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString);
147                 
148                 if( !Module_LoadMem( gaArch_BootModules[i].Base, gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString ) )
149                 {
150                         Log_Warning("Arch", "Unable to load module");
151                 }
152                 
153                 MM_UnmapHWPages(
154                         (tVAddr)gaArch_BootModules[i].Base,
155                         (gaArch_BootModules[i].Size + ((Uint)gaArch_BootModules[i].Base&0xFFF) + 0xFFF) >> 12
156                         );
157                 
158                 if( (tVAddr) gaArch_BootModules[i].ArgString > MAX_ARGSTR_POS )
159                         MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].ArgString, 2 );
160         }
161         Log_Log("Arch", "Boot modules loaded");
162         free( gaArch_BootModules );
163 }

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