Attempt to fix multiboot module support
[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                  int    ofs;
99                 // TODO: Handle this better by using MM_MapHW/MM_MapTemp
100                 // Adjust into higher half
101                 //mods[i].Start  += KERNEL_BASE;
102                 //mods[i].End    += KERNEL_BASE;
103                 mods[i].String += KERNEL_BASE;
104                 
105                 gaArch_BootModules[i].Size = mods[i].End - mods[i].Start;
106                 
107                 ofs = mods[i].Start&0xFFF;
108                 gaArch_BootModules[i].Base = (void*)( MM_MapHWPages(mods[i].Start,
109                         (gaArch_BootModules[i].Size+ofs+0xFFF) / 0x1000
110                         ) + ofs );
111                 //gaArch_BootModules[i].ArgString = MM_MapHW(mods[i].String, 1)
112                 // + (mods[i].String&0xFFF);
113                 
114                 //gaArch_BootModules[i].Base = (void *)mods[i].Start;
115                 gaArch_BootModules[i].ArgString = (char *)mods[i].String + KERNEL_BASE;
116         }
117         
118         // Pass on to Independent Loader
119         Log_Log("Arch", "Starting system");
120         System_Init(gsBootCmdLine);
121         
122         // Sleep forever (sleeping beauty)
123         for(;;)
124                 Threads_Sleep();
125         return 0;
126 }
127
128 void Arch_LoadBootModules(void)
129 {
130          int    i;
131         for( i = 0; i < giArch_NumBootModules; i ++ )
132         {
133                 Log_Debug("Arch", "Module %i: %p - %p 0x%x",
134                         i, gaArch_BootModules[i].ArgString,
135                         gaArch_BootModules[i].Base, gaArch_BootModules[i].Size
136                         );
137                 Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString);
138                 
139                 if( !Module_LoadMem( gaArch_BootModules[i].Base, gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString ) )
140                 {
141                         Log_Warning("Arch", "Unable to load module");
142                 }
143                 
144                 MM_UnmapHWPages(
145                         (tVAddr)gaArch_BootModules[i].Base,
146                         (gaArch_BootModules[i].Size + ((Uint)gaArch_BootModules[i].Base&0xFFF) + 0xFFF) >> 12
147                         );
148         }
149         Log_Log("Arch", "Boot modules loaded");
150         free( gaArch_BootModules );
151 }

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