TODO
[tpg/acess2.git] / KernelLand / 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 #include <pmemmap.h>
13
14 #define VGA_ERRORS      0
15
16 #define KERNEL_LOAD     0x100000        // 1MiB
17 #define MAX_ARGSTR_POS  (0x400000-0x2000)
18 #define MAX_PMEMMAP_ENTS        16
19
20 // === IMPORTS ===
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);
27
28 // === PROTOTYPES ===
29  int    kmain(Uint MbMagic, void *MbInfoPtr);
30
31 // === GLOBALS ===
32 char    *gsBootCmdLine = NULL;
33 struct {
34         Uint32  PBase;
35         void    *Base;
36         Uint    Size;
37         char    *ArgString;
38 }       *gaArch_BootModules;
39  int    giArch_NumBootModules = 0;
40
41 // === CODE ===
42 int kmain(Uint MbMagic, void *MbInfoPtr)
43 {
44         tMBoot_Module   *mods;
45         tMBoot_Info     *mbInfo;
46         tPMemMapEnt     pmemmap[MAX_PMEMMAP_ENTS];
47          int    nPMemMapEnts;
48
49         LogF("%s\r\n", gsBuildInfo);
50         
51         MM_PreinitVirtual();    // Initialise virtual mappings
52
53         mbInfo = MbInfoPtr;     
54
55         switch(MbMagic)
56         {
57         // Multiboot 1
58         case MULTIBOOT_MAGIC: {
59                 // TODO: Handle when this isn't in the mapped area
60                 gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
61                 
62                 tMBoot_MMapEnt  *ent = (void*)mbInfo->MMapAddr;
63                 tMBoot_MMapEnt  *last = (void*)(mbInfo->MMapAddr + mbInfo->MMapLength);
64                 
65                 // Build up memory map
66                 nPMemMapEnts = 0;
67                 while( ent < last && nPMemMapEnts < MAX_PMEMMAP_ENTS )
68                 {
69                         tPMemMapEnt     *nent = &pmemmap[nPMemMapEnts];
70                         nent->Start = ent->Base;
71                         nent->Length = ent->Length;
72                         switch(ent->Type)
73                         {
74                         case 1:
75                                 nent->Type = PMEMTYPE_FREE;
76                                 break;
77                         default:
78                                 nent->Type = PMEMTYPE_RESERVED;
79                                 break;
80                         }
81                         nent->NUMADomain = 0;
82                         
83                         nPMemMapEnts ++;
84                         ent = (void*)( (tVAddr)ent + ent->Size + 4 );
85                 }
86
87                 // Ensure it's valid
88                 nPMemMapEnts = PMemMap_ValidateMap(pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS);
89                 // TODO: Error handling
90
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
95                         );
96
97                 // Replace modules with PMEMTYPE_USED
98                 nPMemMapEnts = PMemMap_MarkRangeUsed(pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS,
99                         mbInfo->Modules, mbInfo->ModuleCount*sizeof(*mods)
100                         );
101                 mods = (void*)mbInfo->Modules;
102                 for( int i = 0; i < mbInfo->ModuleCount; i ++ )
103                 {
104                         nPMemMapEnts = PMemMap_MarkRangeUsed(
105                                 pmemmap, nPMemMapEnts, MAX_PMEMMAP_ENTS,
106                                 mods->Start, mods->End - mods->Start
107                                 );
108                 }
109                 
110                 // Debug - Output map
111                 PMemMap_DumpBlocks(pmemmap, nPMemMapEnts);
112
113                 // Adjust Multiboot structure address
114                 mbInfo = (void*)( (Uint)MbInfoPtr + KERNEL_BASE );
115                 
116                 break; }
117         
118         // Multiboot 2
119         case MULTIBOOT2_MAGIC:
120                 Panic("Multiboot 2 not yet supported");
121                 //MM_InstallMBoot2( MbInfo );   // Set up physical memory manager
122                 return 0;
123                 break;
124         
125         default:
126                 Panic("Multiboot magic invalid %08x, expected %08x or %08x\n",
127                         MbMagic, MULTIBOOT_MAGIC, MULTIBOOT2_MAGIC);
128                 return 0;
129         }
130         
131         // Set up physical memory manager
132         MM_Install(nPMemMapEnts, pmemmap);
133         
134         MM_InstallVirtual();    // Clean up virtual address space
135         Heap_Install();         // Create initial heap
136         
137         // Start Multitasking
138         Threads_Init();
139         
140         // Start Timers
141         Time_Setup();
142         
143         Log_Log("Arch", "Starting VFS...");
144         // Load Virtual Filesystem
145         VFS_Init();
146         
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 ++ )
152         {
153                  int    ofs;
154         
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);
157         
158                 gaArch_BootModules[i].PBase = mods[i].Start;
159                 gaArch_BootModules[i].Size = mods[i].End - mods[i].Start;
160         
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
165                         ) + ofs );
166                 
167                 // Only map the string if needed
168                 if( (tVAddr)mods[i].String > MAX_ARGSTR_POS )
169                 {
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)
173                                 );
174                 }
175                 else
176                         gaArch_BootModules[i].ArgString = (char *)mods[i].String + KERNEL_BASE;
177         }
178         
179         // Pass on to Independent Loader
180         Log_Log("Arch", "Starting system");
181         System_Init(gsBootCmdLine);
182         
183         // Sleep forever (sleeping beauty)
184         for(;;)
185                 Threads_Sleep();
186         return 0;
187 }
188
189 void Arch_LoadBootModules(void)
190 {
191          int    i, j, numPages;
192         for( i = 0; i < giArch_NumBootModules; i ++ )
193         {
194                 Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString);
195                 
196                 if( !Module_LoadMem( gaArch_BootModules[i].Base, gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString ) )
197                 {
198                         Log_Warning("Arch", "Unable to load module");
199                 }
200                 
201                 // Unmap and free
202                 numPages = (gaArch_BootModules[i].Size + ((Uint)gaArch_BootModules[i].Base&0xFFF) + 0xFFF) >> 12;
203                 MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].Base, numPages );
204                 
205                 for( j = 0; j < numPages; j++ )
206                         MM_DerefPhys( gaArch_BootModules[i].PBase + (j << 12) );
207                 
208                 if( (tVAddr) gaArch_BootModules[i].ArgString > MAX_ARGSTR_POS )
209                         MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].ArgString, 2 );
210         }
211         Log_Log("Arch", "Boot modules loaded");
212         free( gaArch_BootModules );
213 }

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