0557760e1a3dcfa1badb0ccfbd8f01292995fb5a
[tpg/acess2.git] / 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 extern Uint     Proc_Clone(Uint *Err, Uint Flags);
26 extern void     Threads_Sleep(void);
27 // --- Core ---
28 extern void     System_Init(char *Commandline);
29
30 // === PROTOTYPES ===
31 void    Arch_LoadBootModules(void);
32
33 // === GLOBALS ===
34 char    *gsBootCmdLine = NULL;
35 struct {
36         Uint32  PBase;
37         void    *Base;
38         Uint    Size;
39         char    *ArgString;
40 }       *gaArch_BootModules;
41  int    giArch_NumBootModules = 0;
42
43 // === CODE ===
44 int kmain(Uint MbMagic, void *MbInfoPtr)
45 {
46          int    i;
47         tMBoot_Module   *mods;
48         tMBoot_Info     *mbInfo;
49         
50         Log("MbMagic = %08x", MbMagic);
51         Log("MbInfoPtr = %p", MbInfoPtr);
52         
53         // Set up non-boot info dependent stuff
54         Desctab_Install();      // Set up GDT and IDT
55         MM_PreinitVirtual();    // Initialise virtual mappings
56         
57         switch(MbMagic)
58         {
59         // Multiboot 1
60         case MULTIBOOT_MAGIC:
61                 // Adjust Multiboot structure address
62                 mbInfo = (void*)( (Uint)MbInfoPtr + KERNEL_BASE );
63                 gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
64                 
65                 MM_Install( mbInfo );   // Set up physical memory manager
66                 break;
67         
68         // Multiboot 2
69         case MULTIBOOT2_MAGIC:
70                 Panic("Multiboot 2 not yet supported");
71                 //MM_InstallMBoot2( MbInfo );   // Set up physical memory manager
72                 return 0;
73                 break;
74         
75         default:
76                 Panic("Multiboot magic invalid %08x, expected %08x or %08x\n",
77                         MbMagic, MULTIBOOT_MAGIC, MULTIBOOT2_MAGIC);
78                 return 0;
79         }
80         
81         MM_InstallVirtual();    // Clean up virtual address space
82         Heap_Install();         // Create initial heap
83         
84         // Start Timers
85         Time_Setup();
86         
87         //Log_Log("Arch", "Starting Multitasking...");
88         // Start Multitasking
89         Threads_Init();
90         
91         Log_Log("Arch", "Starting VFS...");
92         // Load Virtual Filesystem
93         VFS_Init();
94         
95         // Load initial modules
96         mods = (void*)( mbInfo->Modules + KERNEL_BASE );
97         giArch_NumBootModules = mbInfo->ModuleCount;
98         gaArch_BootModules = malloc( giArch_NumBootModules * sizeof(*gaArch_BootModules) );
99         for( i = 0; i < mbInfo->ModuleCount; i ++ )
100         {
101                  int    ofs;
102         
103                 Log_Log("Arch", "Multiboot Module at 0x%08x, 0x%08x bytes (String at 0x%08x)",
104                         mods[i].Start, mods[i].End-mods[i].Start, mods[i].String);
105         
106                 gaArch_BootModules[i].PBase = mods[i].Start;
107                 gaArch_BootModules[i].Size = mods[i].End - mods[i].Start;
108         
109                 // Always HW map the module data        
110                 ofs = mods[i].Start&0xFFF;
111                 gaArch_BootModules[i].Base = (void*)( MM_MapHWPages(mods[i].Start,
112                         (gaArch_BootModules[i].Size+ofs+0xFFF) / 0x1000
113                         ) + ofs );
114                 
115                 // Only map the string if needed
116                 if( (tVAddr)mods[i].String > MAX_ARGSTR_POS )
117                 {
118                         // Assumes the string is < 4096 bytes long)
119                         gaArch_BootModules[i].ArgString = (void*)(
120                                 MM_MapHWPages(mods[i].String, 2) + (mods[i].String&0xFFF)
121                                 );
122                 }
123                 else
124                         gaArch_BootModules[i].ArgString = (char *)mods[i].String + KERNEL_BASE;
125         }
126         
127         // Pass on to Independent Loader
128         Log_Log("Arch", "Starting system");
129         System_Init(gsBootCmdLine);
130         
131         // Sleep forever (sleeping beauty)
132         for(;;)
133                 Threads_Sleep();
134         return 0;
135 }
136
137 void Arch_LoadBootModules(void)
138 {
139          int    i, j, numPages;
140         for( i = 0; i < giArch_NumBootModules; i ++ )
141         {
142                 Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString);
143                 
144                 if( !Module_LoadMem( gaArch_BootModules[i].Base, gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString ) )
145                 {
146                         Log_Warning("Arch", "Unable to load module");
147                 }
148                 
149                 // Unmap and free
150                 numPages = (gaArch_BootModules[i].Size + ((Uint)gaArch_BootModules[i].Base&0xFFF) + 0xFFF) >> 12;
151                 MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].Base, numPages );
152                 
153                 for( j = 0; j < numPages; j++ )
154                         MM_DerefPhys( gaArch_BootModules[i].PBase + (j << 12) );
155                 
156                 if( (tVAddr) gaArch_BootModules[i].ArgString > MAX_ARGSTR_POS )
157                         MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].ArgString, 2 );
158         }
159         Log_Log("Arch", "Boot modules loaded");
160         free( gaArch_BootModules );
161 }

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