8 #include <multiboot2.h>
16 #define KERNEL_LOAD 0x100000 // 1MiB
17 #define MAX_ARGSTR_POS (0x400000-0x2000)
18 #define MAX_PMEMMAP_ENTS 16
21 extern char gKernelEnd[];
22 extern void Heap_Install(void);
23 extern void MM_PreinitVirtual(void);
24 extern void MM_Install(int NPMemRanges, tPMemMapEnt *PMemRanges);
25 extern void MM_InstallVirtual(void);
26 extern int Time_Setup(void);
29 int kmain(Uint MbMagic, void *MbInfoPtr);
32 char *gsBootCmdLine = NULL;
38 } *gaArch_BootModules;
39 int giArch_NumBootModules = 0;
42 int kmain(Uint MbMagic, void *MbInfoPtr)
46 tPMemMapEnt pmemmap[MAX_PMEMMAP_ENTS];
49 LogF("%s\r\n", gsBuildInfo);
51 MM_PreinitVirtual(); // Initialise virtual mappings
58 case MULTIBOOT_MAGIC: {
59 // TODO: Handle when this isn't in the mapped area
60 gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
62 tMBoot_MMapEnt *ent = (void*)mbInfo->MMapAddr;
63 tMBoot_MMapEnt *last = (void*)(mbInfo->MMapAddr + mbInfo->MMapLength);
65 // Build up memory map
67 while( ent < last && nPMemMapEnts < MAX_PMEMMAP_ENTS )
69 tPMemMapEnt *nent = &pmemmap[nPMemMapEnts];
70 nent->Start = ent->Base;
71 nent->Length = ent->Length;
75 nent->Type = PMEMTYPE_FREE;
78 nent->Type = PMEMTYPE_RESERVED;
84 ent = (void*)( (tVAddr)ent + ent->Size + 4 );
88 nPMemMapEnts = PMemMap_ValidateMap(pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS);
89 // TODO: Error handling
91 // Replace kernel with PMEMTYPE_USED
92 nPMemMapEnts = PMemMap_MarkRangeUsed(
93 pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS,
94 KERNEL_LOAD, (tVAddr)&gKernelEnd - KERNEL_LOAD - KERNEL_BASE
97 // Replace modules with PMEMTYPE_USED
98 nPMemMapEnts = PMemMap_MarkRangeUsed(pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS,
99 mbInfo->Modules, mbInfo->ModuleCount*sizeof(*mods)
101 mods = (void*)mbInfo->Modules;
102 for( int i = 0; i < mbInfo->ModuleCount; i ++ )
104 nPMemMapEnts = PMemMap_MarkRangeUsed(
105 pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS,
106 mods->Start, mods->End - mods->Start
110 // Debug - Output map
111 PMemMap_DumpBlocks(pmemmap, nPMemMapEnts);
113 // Adjust Multiboot structure address
114 mbInfo = (void*)( (Uint)MbInfoPtr + KERNEL_BASE );
119 case MULTIBOOT2_MAGIC:
120 Panic("Multiboot 2 not yet supported");
121 //MM_InstallMBoot2( MbInfo ); // Set up physical memory manager
126 Panic("Multiboot magic invalid %08x, expected %08x or %08x\n",
127 MbMagic, MULTIBOOT_MAGIC, MULTIBOOT2_MAGIC);
131 // Set up physical memory manager
132 MM_Install(nPMemMapEnts, pmemmap);
134 MM_InstallVirtual(); // Clean up virtual address space
135 Heap_Install(); // Create initial heap
137 // Start Multitasking
143 Log_Log("Arch", "Starting VFS...");
144 // Load Virtual Filesystem
147 // Load initial modules
148 mods = (void*)( mbInfo->Modules + KERNEL_BASE );
149 giArch_NumBootModules = mbInfo->ModuleCount;
150 gaArch_BootModules = malloc( giArch_NumBootModules * sizeof(*gaArch_BootModules) );
151 for( int i = 0; i < mbInfo->ModuleCount; i ++ )
155 Log_Log("Arch", "Multiboot Module at 0x%08x, 0x%08x bytes (String at 0x%08x)",
156 mods[i].Start, mods[i].End-mods[i].Start, mods[i].String);
158 gaArch_BootModules[i].PBase = mods[i].Start;
159 gaArch_BootModules[i].Size = mods[i].End - mods[i].Start;
161 // Always HW map the module data
162 ofs = mods[i].Start&0xFFF;
163 gaArch_BootModules[i].Base = (void*)( MM_MapHWPages(mods[i].Start,
164 (gaArch_BootModules[i].Size+ofs+0xFFF) / 0x1000
167 // Only map the string if needed
168 if( (tVAddr)mods[i].String > MAX_ARGSTR_POS )
170 // Assumes the string is < 4096 bytes long)
171 gaArch_BootModules[i].ArgString = (void*)(
172 MM_MapHWPages(mods[i].String, 2) + (mods[i].String&0xFFF)
176 gaArch_BootModules[i].ArgString = (char *)mods[i].String + KERNEL_BASE;
179 // Pass on to Independent Loader
180 Log_Log("Arch", "Starting system");
181 System_Init(gsBootCmdLine);
183 // Sleep forever (sleeping beauty)
189 void Arch_LoadBootModules(void)
192 for( i = 0; i < giArch_NumBootModules; i ++ )
194 Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString);
196 if( !Module_LoadMem( gaArch_BootModules[i].Base, gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString ) )
198 Log_Warning("Arch", "Unable to load module");
202 numPages = (gaArch_BootModules[i].Size + ((Uint)gaArch_BootModules[i].Base&0xFFF) + 0xFFF) >> 12;
203 MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].Base, numPages );
205 for( j = 0; j < numPages; j++ )
206 MM_DerefPhys( gaArch_BootModules[i].PBase + (j << 12) );
208 if( (tVAddr) gaArch_BootModules[i].ArgString > MAX_ARGSTR_POS )
209 MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].ArgString, 2 );
211 Log_Log("Arch", "Boot modules loaded");
212 free( gaArch_BootModules );