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

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