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

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