Merge branch 'master' of github.com:thepowersgang/acess2
[tpg/acess2.git] / KernelLand / Kernel / arch / x86 / main.c
index 2106b6a..b6ac974 100644 (file)
@@ -9,21 +9,21 @@
 #include <init.h>
 #include <mm_virt.h>
 #include <mp.h>
+#include <pmemmap.h>
 
 #define        VGA_ERRORS      0
 
+#define KERNEL_LOAD    0x100000        // 1MiB
 #define MAX_ARGSTR_POS (0x400000-0x2000)
+#define MAX_PMEMMAP_ENTS       16
 
 // === IMPORTS ===
+extern char    gKernelEnd[];
 extern void    Heap_Install(void);
-extern void    Desctab_Install(void);
 extern void    MM_PreinitVirtual(void);
-extern void    MM_Install(tMBoot_Info *MBoot);
+extern void    MM_Install(int NPMemRanges, tPMemMapEnt *PMemRanges);
 extern void    MM_InstallVirtual(void);
-extern void    Threads_Init(void);
 extern int     Time_Setup(void);
-// --- Core ---
-extern void    System_Init(char *Commandline);
 
 // === PROTOTYPES ===
  int   kmain(Uint MbMagic, void *MbInfoPtr);
@@ -41,29 +41,79 @@ struct {
 // === CODE ===
 int kmain(Uint MbMagic, void *MbInfoPtr)
 {
-        int    i;
        tMBoot_Module   *mods;
        tMBoot_Info     *mbInfo;
+       tPMemMapEnt     pmemmap[MAX_PMEMMAP_ENTS];
+        int    nPMemMapEnts;
 
-       LogF("Acess2 x86-"PLATFORM" v"EXPAND_STR(KERNEL_VERSION)"\n");
-       LogF(" Build %i, Git Hash %s\n", BUILD_NUM, gsGitHash);
+       LogF("%s\r\n", gsBuildInfo);
        
-       Log("MbMagic = %08x, MbInfoPtr = %p", MbMagic, MbInfoPtr);
-       
-       // Set up non-boot info dependent stuff
-       Desctab_Install();      // Set up GDT and IDT
        MM_PreinitVirtual();    // Initialise virtual mappings
-       
+
+       mbInfo = MbInfoPtr;     
+
        switch(MbMagic)
        {
        // Multiboot 1
-       case MULTIBOOT_MAGIC:
+       case MULTIBOOT_MAGIC: {
+               // TODO: Handle when this isn't in the mapped area
+               gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
+               
+               tMBoot_MMapEnt  *ent = (void*)mbInfo->MMapAddr;
+               tMBoot_MMapEnt  *last = (void*)(mbInfo->MMapAddr + mbInfo->MMapLength);
+               
+               // Build up memory map
+               nPMemMapEnts = 0;
+               while( ent < last && nPMemMapEnts < MAX_PMEMMAP_ENTS )
+               {
+                       tPMemMapEnt     *nent = &pmemmap[nPMemMapEnts];
+                       nent->Start = ent->Base;
+                       nent->Length = ent->Length;
+                       switch(ent->Type)
+                       {
+                       case 1:
+                               nent->Type = PMEMTYPE_FREE;
+                               break;
+                       default:
+                               nent->Type = PMEMTYPE_RESERVED;
+                               break;
+                       }
+                       nent->NUMADomain = 0;
+                       
+                       nPMemMapEnts ++;
+                       ent = (void*)( (tVAddr)ent + ent->Size + 4 );
+               }
+
+               // Ensure it's valid
+               nPMemMapEnts = PMemMap_ValidateMap(pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS);
+               // TODO: Error handling
+
+               // Replace kernel with PMEMTYPE_USED
+               nPMemMapEnts = PMemMap_MarkRangeUsed(
+                       pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS,
+                       KERNEL_LOAD, (tVAddr)&gKernelEnd - KERNEL_LOAD - KERNEL_BASE
+                       );
+
+               // Replace modules with PMEMTYPE_USED
+               nPMemMapEnts = PMemMap_MarkRangeUsed(pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS,
+                       mbInfo->Modules, mbInfo->ModuleCount*sizeof(*mods)
+                       );
+               mods = (void*)mbInfo->Modules;
+               for( int i = 0; i < mbInfo->ModuleCount; i ++ )
+               {
+                       nPMemMapEnts = PMemMap_MarkRangeUsed(
+                               pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS,
+                               mods->Start, mods->End - mods->Start
+                               );
+               }
+               
+               // Debug - Output map
+               PMemMap_DumpBlocks(pmemmap, nPMemMapEnts);
+
                // Adjust Multiboot structure address
                mbInfo = (void*)( (Uint)MbInfoPtr + KERNEL_BASE );
-               gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
                
-               MM_Install( mbInfo );   // Set up physical memory manager
-               break;
+               break; }
        
        // Multiboot 2
        case MULTIBOOT2_MAGIC:
@@ -78,6 +128,9 @@ int kmain(Uint MbMagic, void *MbInfoPtr)
                return 0;
        }
        
+       // Set up physical memory manager
+       MM_Install(nPMemMapEnts, pmemmap);
+       
        MM_InstallVirtual();    // Clean up virtual address space
        Heap_Install();         // Create initial heap
        
@@ -95,7 +148,7 @@ int kmain(Uint MbMagic, void *MbInfoPtr)
        mods = (void*)( mbInfo->Modules + KERNEL_BASE );
        giArch_NumBootModules = mbInfo->ModuleCount;
        gaArch_BootModules = malloc( giArch_NumBootModules * sizeof(*gaArch_BootModules) );
-       for( i = 0; i < mbInfo->ModuleCount; i ++ )
+       for( int i = 0; i < mbInfo->ModuleCount; i ++ )
        {
                 int    ofs;
        

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