Kernel - Cleaned up MM_AllocDMA/_MapHWPages/_GetPhysAddr
[tpg/acess2.git] / KernelLand / Kernel / arch / x86_64 / main.c
1 /*
2  * Acess2 Kernel x86_64
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Kernel C entrypoint
7  */
8 #include <acess.h>
9 #include <mboot.h>
10 #include <init.h>
11 #include <archinit.h>
12 #include <pmemmap.h>
13
14 // === CONSTANTS ===
15 #define KERNEL_LOAD     0x100000
16 #define MAX_PMEMMAP_ENTS        16
17 #define MAX_ARGSTR_POS  (0x200000-0x2000)
18
19 // === IMPORTS ===
20 extern void     Desctab_Init(void);
21 extern void     MM_InitVirt(void);
22 extern int      Time_Setup(void);
23
24 extern char     gKernelEnd[];
25
26 // === PROTOTYPES ===
27 void    kmain(Uint MbMagic, void *MbInfoPtr);
28
29 // === GLOBALS ==
30 char    *gsBootCmdLine = NULL;
31 tBootModule     *gaArch_BootModules;
32  int    giArch_NumBootModules = 0;
33
34 // === CODE ===
35 void kmain(Uint MbMagic, void *MbInfoPtr)
36 {
37         tMBoot_Info     *mbInfo;
38         tPMemMapEnt     pmemmap[MAX_PMEMMAP_ENTS];
39          int    nPMemMapEnts;
40
41         LogF("%s\r\n", gsBuildInfo);
42         
43         Desctab_Init();
44
45         MM_InitVirt();
46         *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'C';
47         
48         switch(MbMagic)
49         {
50         // Multiboot 1
51         case MULTIBOOT_MAGIC:
52                 // Adjust Multiboot structure address
53                 mbInfo = (void*)( (Uint)MbInfoPtr + KERNEL_BASE );
54                 gsBootCmdLine = (char*)( (Uint)mbInfo->CommandLine + KERNEL_BASE);
55                 nPMemMapEnts = Multiboot_LoadMemoryMap(mbInfo, KERNEL_BASE, pmemmap, MAX_PMEMMAP_ENTS,
56                         KERNEL_LOAD, (tVAddr)&gKernelEnd - KERNEL_BASE
57                         );
58                 break;
59         default:
60                 Panic("Multiboot magic invalid %08x, expected %08x\n",
61                         MbMagic, MULTIBOOT_MAGIC);
62                 return ;
63         }
64         
65         MM_InitPhys( nPMemMapEnts, pmemmap );   // Set up physical memory manager
66         Log("gsBootCmdLine = '%s'", gsBootCmdLine);
67         
68         switch(MbMagic)
69         {
70         case MULTIBOOT_MAGIC:
71                 MM_RefPhys( mbInfo->CommandLine );
72                 break;
73         }
74
75         *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'D';
76         Heap_Install();
77         
78         *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'E';
79         Threads_Init();
80         
81         Time_Setup();
82         *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'F';
83         
84         // Load Virtual Filesystem
85         Log_Log("Arch", "Starting VFS...");
86         VFS_Init();
87         
88         // Multiboot_InitFramebuffer(mbInfo);
89
90         gaArch_BootModules = Multiboot_LoadModules(mbInfo, KERNEL_BASE, &giArch_NumBootModules);
91         
92         *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'Z';
93         
94         // Pass on to Independent Loader
95         Log_Log("Arch", "Starting system");
96         System_Init(gsBootCmdLine);
97         
98         // Sleep forever (sleeping beauty)
99         for(;;)
100                 Threads_Sleep();
101 }
102
103 void Arch_LoadBootModules(void)
104 {
105          int    i, j, numPages;
106         Log("gsBootCmdLine = '%s'", gsBootCmdLine);
107         for( i = 0; i < giArch_NumBootModules; i ++ )
108         {
109                 Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString);
110                 
111                 if( !Module_LoadMem( gaArch_BootModules[i].Base,
112                         gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString
113                         ) )
114                 {
115                         Log_Warning("Arch", "Unable to load module");
116                 }
117                 
118                 // Unmap and free
119                 numPages = (gaArch_BootModules[i].Size + ((Uint)gaArch_BootModules[i].Base&0xFFF) + 0xFFF) >> 12;
120                 MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].Base, numPages );
121                 
122                 for( j = 0; j < numPages; j++ )
123                         MM_DerefPhys( gaArch_BootModules[i].PBase + (j << 12) );
124                 
125                 if( (tVAddr) gaArch_BootModules[i].ArgString < KERNEL_BASE )
126                         MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].ArgString, 2 );
127         }
128         Log_Log("Arch", "Boot modules loaded");
129         if( gaArch_BootModules )
130                 free( gaArch_BootModules );
131 }
132
133 void StartupPrint(const char *String)
134 {
135         
136 }

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