Kernel - Fixing bugs exposed with `qemu -nographic`
[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
13 #define VGA_ERRORS      0
14
15 #define MAX_ARGSTR_POS  (0x400000-0x2000)
16
17 // === IMPORTS ===
18 extern void     Heap_Install(void);
19 extern void     Desctab_Install(void);
20 extern void     MM_PreinitVirtual(void);
21 extern void     MM_Install(tMBoot_Info *MBoot);
22 extern void     MM_InstallVirtual(void);
23 extern void     Threads_Init(void);
24 extern int      Time_Setup(void);
25 // --- Core ---
26 extern void     System_Init(char *Commandline);
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          int    i;
45         tMBoot_Module   *mods;
46         tMBoot_Info     *mbInfo;
47
48         LogF("Acess2 x86-"PLATFORM" v"EXPAND_STR(KERNEL_VERSION)"\r\n");
49         LogF(" Build %i, Git Hash %s\r\n", BUILD_NUM, gsGitHash);
50         
51         // Set up non-boot info dependent stuff
52         Desctab_Install();      // Set up GDT and IDT
53         MM_PreinitVirtual();    // Initialise virtual mappings
54         
55         switch(MbMagic)
56         {
57         // Multiboot 1
58         case MULTIBOOT_MAGIC:
59                 // Adjust Multiboot structure address
60                 mbInfo = (void*)( (Uint)MbInfoPtr + KERNEL_BASE );
61                 gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
62                 
63                 MM_Install( mbInfo );   // Set up physical memory manager
64                 break;
65         
66         // Multiboot 2
67         case MULTIBOOT2_MAGIC:
68                 Panic("Multiboot 2 not yet supported");
69                 //MM_InstallMBoot2( MbInfo );   // Set up physical memory manager
70                 return 0;
71                 break;
72         
73         default:
74                 Panic("Multiboot magic invalid %08x, expected %08x or %08x\n",
75                         MbMagic, MULTIBOOT_MAGIC, MULTIBOOT2_MAGIC);
76                 return 0;
77         }
78         
79         MM_InstallVirtual();    // Clean up virtual address space
80         Heap_Install();         // Create initial heap
81         
82         // Start Multitasking
83         Threads_Init();
84         
85         // Start Timers
86         Time_Setup();
87         
88         Log_Log("Arch", "Starting VFS...");
89         // Load Virtual Filesystem
90         VFS_Init();
91         
92         // Load initial modules
93         mods = (void*)( mbInfo->Modules + KERNEL_BASE );
94         giArch_NumBootModules = mbInfo->ModuleCount;
95         gaArch_BootModules = malloc( giArch_NumBootModules * sizeof(*gaArch_BootModules) );
96         for( i = 0; i < mbInfo->ModuleCount; i ++ )
97         {
98                  int    ofs;
99         
100                 Log_Log("Arch", "Multiboot Module at 0x%08x, 0x%08x bytes (String at 0x%08x)",
101                         mods[i].Start, mods[i].End-mods[i].Start, mods[i].String);
102         
103                 gaArch_BootModules[i].PBase = mods[i].Start;
104                 gaArch_BootModules[i].Size = mods[i].End - mods[i].Start;
105         
106                 // Always HW map the module data        
107                 ofs = mods[i].Start&0xFFF;
108                 gaArch_BootModules[i].Base = (void*)( MM_MapHWPages(mods[i].Start,
109                         (gaArch_BootModules[i].Size+ofs+0xFFF) / 0x1000
110                         ) + ofs );
111                 
112                 // Only map the string if needed
113                 if( (tVAddr)mods[i].String > MAX_ARGSTR_POS )
114                 {
115                         // Assumes the string is < 4096 bytes long)
116                         gaArch_BootModules[i].ArgString = (void*)(
117                                 MM_MapHWPages(mods[i].String, 2) + (mods[i].String&0xFFF)
118                                 );
119                 }
120                 else
121                         gaArch_BootModules[i].ArgString = (char *)mods[i].String + KERNEL_BASE;
122                 Log_Log("Arch", " - %s", gaArch_BootModules[i].ArgString);
123         }
124         
125         // Pass on to Independent Loader
126         Log_Log("Arch", "Starting system");
127         System_Init(gsBootCmdLine);
128         
129         // Sleep forever (sleeping beauty)
130         for(;;)
131                 Threads_Sleep();
132         return 0;
133 }
134
135 void Arch_LoadBootModules(void)
136 {
137          int    i, j, numPages;
138         for( i = 0; i < giArch_NumBootModules; i ++ )
139         {
140                 Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString);
141                 
142                 if( !Module_LoadMem( gaArch_BootModules[i].Base, gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString ) )
143                 {
144                         Log_Warning("Arch", "Unable to load module");
145                 }
146                 
147                 // Unmap and free
148                 numPages = (gaArch_BootModules[i].Size + ((Uint)gaArch_BootModules[i].Base&0xFFF) + 0xFFF) >> 12;
149                 MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].Base, numPages );
150                 
151                 for( j = 0; j < numPages; j++ )
152                         MM_DerefPhys( gaArch_BootModules[i].PBase + (j << 12) );
153                 
154                 if( (tVAddr) gaArch_BootModules[i].ArgString > MAX_ARGSTR_POS )
155                         MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].ArgString, 2 );
156         }
157         Log_Log("Arch", "Boot modules loaded");
158         free( gaArch_BootModules );
159 }

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