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 Desctab_Install(void);
24 extern void MM_PreinitVirtual(void);
25 extern void MM_Install(int NPMemRanges, tPMemMapEnt *PMemRanges);
26 extern void MM_InstallVirtual(void);
27 extern void Threads_Init(void);
28 extern int Time_Setup(void);
30 extern void System_Init(char *Commandline);
33 int kmain(Uint MbMagic, void *MbInfoPtr);
36 char *gsBootCmdLine = NULL;
42 } *gaArch_BootModules;
43 int giArch_NumBootModules = 0;
46 int kmain(Uint MbMagic, void *MbInfoPtr)
50 tPMemMapEnt pmemmap[MAX_PMEMMAP_ENTS];
53 LogF("Acess2 x86-"PLATFORM" v"EXPAND_STR(KERNEL_VERSION)"\r\n");
54 LogF(" Build %i, Git Hash %s\r\n", BUILD_NUM, gsGitHash);
56 MM_PreinitVirtual(); // Initialise virtual mappings
63 case MULTIBOOT_MAGIC: {
64 // TODO: Handle when this isn't in the mapped area
65 gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
67 tMBoot_MMapEnt *ent = (void*)mbInfo->MMapAddr;
68 tMBoot_MMapEnt *last = (void*)(mbInfo->MMapAddr + mbInfo->MMapLength);
70 // Build up memory map
72 while( ent < last && nPMemMapEnts < MAX_PMEMMAP_ENTS )
74 tPMemMapEnt *nent = &pmemmap[nPMemMapEnts];
75 nent->Start = ent->Base;
76 nent->Length = ent->Length;
80 nent->Type = PMEMTYPE_FREE;
83 nent->Type = PMEMTYPE_RESERVED;
89 ent = (void*)( (tVAddr)ent + ent->Size + 4 );
93 nPMemMapEnts = PMemMap_ValidateMap(pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS);
94 // TODO: Error handling
96 // Replace kernel with PMEMTYPE_USED
97 nPMemMapEnts = PMemMap_MarkRangeUsed(
98 pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS,
99 KERNEL_LOAD, (tVAddr)&gKernelEnd - KERNEL_LOAD - KERNEL_BASE
102 // Replace modules with PMEMTYPE_USED
103 nPMemMapEnts = PMemMap_MarkRangeUsed(pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS,
104 mbInfo->Modules, mbInfo->ModuleCount*sizeof(*mods)
106 mods = (void*)mbInfo->Modules;
107 for( int i = 0; i < mbInfo->ModuleCount; i ++ )
109 nPMemMapEnts = PMemMap_MarkRangeUsed(
110 pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS,
111 mods->Start, mods->End - mods->Start
115 // Debug - Output map
116 PMemMap_DumpBlocks(pmemmap, nPMemMapEnts);
118 // Adjust Multiboot structure address
119 mbInfo = (void*)( (Uint)MbInfoPtr + KERNEL_BASE );
124 case MULTIBOOT2_MAGIC:
125 Panic("Multiboot 2 not yet supported");
126 //MM_InstallMBoot2( MbInfo ); // Set up physical memory manager
131 Panic("Multiboot magic invalid %08x, expected %08x or %08x\n",
132 MbMagic, MULTIBOOT_MAGIC, MULTIBOOT2_MAGIC);
136 // Set up physical memory manager
137 MM_Install(nPMemMapEnts, pmemmap);
139 MM_InstallVirtual(); // Clean up virtual address space
140 Heap_Install(); // Create initial heap
142 // Start Multitasking
148 Log_Log("Arch", "Starting VFS...");
149 // Load Virtual Filesystem
152 // Load initial modules
153 mods = (void*)( mbInfo->Modules + KERNEL_BASE );
154 giArch_NumBootModules = mbInfo->ModuleCount;
155 gaArch_BootModules = malloc( giArch_NumBootModules * sizeof(*gaArch_BootModules) );
156 for( int i = 0; i < mbInfo->ModuleCount; i ++ )
160 Log_Log("Arch", "Multiboot Module at 0x%08x, 0x%08x bytes (String at 0x%08x)",
161 mods[i].Start, mods[i].End-mods[i].Start, mods[i].String);
163 gaArch_BootModules[i].PBase = mods[i].Start;
164 gaArch_BootModules[i].Size = mods[i].End - mods[i].Start;
166 // Always HW map the module data
167 ofs = mods[i].Start&0xFFF;
168 gaArch_BootModules[i].Base = (void*)( MM_MapHWPages(mods[i].Start,
169 (gaArch_BootModules[i].Size+ofs+0xFFF) / 0x1000
172 // Only map the string if needed
173 if( (tVAddr)mods[i].String > MAX_ARGSTR_POS )
175 // Assumes the string is < 4096 bytes long)
176 gaArch_BootModules[i].ArgString = (void*)(
177 MM_MapHWPages(mods[i].String, 2) + (mods[i].String&0xFFF)
181 gaArch_BootModules[i].ArgString = (char *)mods[i].String + KERNEL_BASE;
184 // Pass on to Independent Loader
185 Log_Log("Arch", "Starting system");
186 System_Init(gsBootCmdLine);
188 // Sleep forever (sleeping beauty)
194 void Arch_LoadBootModules(void)
197 for( i = 0; i < giArch_NumBootModules; i ++ )
199 Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString);
201 if( !Module_LoadMem( gaArch_BootModules[i].Base, gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString ) )
203 Log_Warning("Arch", "Unable to load module");
207 numPages = (gaArch_BootModules[i].Size + ((Uint)gaArch_BootModules[i].Base&0xFFF) + 0xFFF) >> 12;
208 MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].Base, numPages );
210 for( j = 0; j < numPages; j++ )
211 MM_DerefPhys( gaArch_BootModules[i].PBase + (j << 12) );
213 if( (tVAddr) gaArch_BootModules[i].ArgString > MAX_ARGSTR_POS )
214 MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].ArgString, 2 );
216 Log_Log("Arch", "Boot modules loaded");
217 free( gaArch_BootModules );