Merge branch 'master' of github.com:thepowersgang/acess2
[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 tBootModule     *gaArch_BootModules;
34  int    giArch_NumBootModules = 0;
35
36 // === CODE ===
37 int kmain(Uint MbMagic, void *MbInfoPtr)
38 {
39         tMBoot_Info     *mbInfo;
40         tPMemMapEnt     pmemmap[MAX_PMEMMAP_ENTS];
41          int    nPMemMapEnts;
42
43         LogF("%s\r\n", gsBuildInfo);
44         
45         MM_PreinitVirtual();    // Initialise virtual mappings
46
47         mbInfo = MbInfoPtr;     
48
49         switch(MbMagic)
50         {
51         // Multiboot 1
52         case MULTIBOOT_MAGIC: {
53                 // TODO: Handle when this isn't in the mapped area
54                 gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
55
56                 // Adjust Multiboot structure address
57                 mbInfo = (void*)( (tVAddr)MbInfoPtr + KERNEL_BASE );
58
59                 nPMemMapEnts = Multiboot_LoadMemoryMap(mbInfo, KERNEL_BASE, pmemmap, MAX_PMEMMAP_ENTS,
60                         KERNEL_LOAD, (tVAddr)&gKernelEnd - KERNEL_BASE);
61
62                 break; }
63         
64         // Multiboot 2
65         case MULTIBOOT2_MAGIC:
66                 Panic("Multiboot 2 not yet supported");
67                 //MM_InstallMBoot2( MbInfo );   // Set up physical memory manager
68                 return 0;
69                 break;
70         
71         default:
72                 Panic("Multiboot magic invalid %08x, expected %08x or %08x\n",
73                         MbMagic, MULTIBOOT_MAGIC, MULTIBOOT2_MAGIC);
74                 return 0;
75         }
76         
77         // Set up physical memory manager
78         MM_Install(nPMemMapEnts, pmemmap);
79         
80         MM_InstallVirtual();    // Clean up virtual address space
81         Heap_Install();         // Create initial heap
82         
83         // Start Multitasking
84         Threads_Init();
85         
86         // Start Timers
87         Time_Setup();
88         
89         Log_Log("Arch", "Starting VFS...");
90         // Load Virtual Filesystem
91         VFS_Init();
92         
93         // Load initial modules
94         gaArch_BootModules = Multiboot_LoadModules(mbInfo, KERNEL_BASE, &giArch_NumBootModules);
95         
96         // Pass on to Independent Loader
97         Log_Log("Arch", "Starting system");
98         System_Init(gsBootCmdLine);
99         
100         // Sleep forever (sleeping beauty)
101         for(;;)
102                 Threads_Sleep();
103         return 0;
104 }
105
106 void Arch_LoadBootModules(void)
107 {
108          int    i, j, numPages;
109         for( i = 0; i < giArch_NumBootModules; i ++ )
110         {
111                 Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString);
112                 
113                 if( !Module_LoadMem( gaArch_BootModules[i].Base,
114                         gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString
115                         ) )
116                 {
117                         Log_Warning("Arch", "Unable to load module");
118                 }
119                 
120                 // Unmap and free
121                 numPages = (gaArch_BootModules[i].Size + ((Uint)gaArch_BootModules[i].Base&0xFFF) + 0xFFF) >> 12;
122                 MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].Base, numPages );
123                 
124                 for( j = 0; j < numPages; j++ )
125                         MM_DerefPhys( gaArch_BootModules[i].PBase + (j << 12) );
126                 
127                 if( (tVAddr) gaArch_BootModules[i].ArgString > MAX_ARGSTR_POS )
128                         MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].ArgString, 2 );
129         }
130         Log_Log("Arch", "Boot modules loaded");
131         if( gaArch_BootModules )
132                 free( gaArch_BootModules );
133 }

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