822bf70b124934d4941eff3928f706e901595378
[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 <pmemmap.h>
12
13 #define VGA_ERRORS      0
14
15 #define KERNEL_LOAD     0x100000        // 1MiB
16 #define MAX_ARGSTR_POS  (0x400000-0x2000)
17 #define MAX_PMEMMAP_ENTS        16
18
19 // === IMPORTS ===
20 extern char     gKernelEnd[];
21 extern void     MM_PreinitVirtual(void);
22 extern void     MM_Install(int NPMemRanges, tPMemMapEnt *PMemRanges);
23 extern void     MM_InstallVirtual(void);
24 extern int      Time_Setup(void);
25 extern int      ACPICA_Initialise(void);
26 // - Modules/Display/VESA
27 extern void     VBE_int_SetBootMode(Uint16 ModeID, const void *ModeInfo);
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                 ASSERT( mbInfo->CommandLine < 4*1024*1024 );
56                 gsBootCmdLine = (char*)(mbInfo->CommandLine + KERNEL_BASE);
57
58                 // Adjust Multiboot structure address
59                 mbInfo = (void*)( (tVAddr)MbInfoPtr + KERNEL_BASE );
60
61                 // Parse memory map
62                 // - mbInfo->Flags is checked in this function
63                 nPMemMapEnts = Multiboot_LoadMemoryMap(mbInfo, KERNEL_BASE, pmemmap, MAX_PMEMMAP_ENTS,
64                         KERNEL_LOAD, (tVAddr)&gKernelEnd - KERNEL_BASE);
65                 
66                 
67                 // Get video mode
68                 Debug("mbInfo->Flags = 0x%x", mbInfo->Flags);
69                 if( mbInfo->Flags & (1 << 11) )
70                 {
71                         // TODO: Ensure address is in mapped area
72                         ASSERT( mbInfo->vbe_mode_info < 4*1024*1024 );
73                         VBE_int_SetBootMode(mbInfo->vbe_mode, (void*)(mbInfo->vbe_mode_info + KERNEL_BASE));
74                 }
75
76                 break; }
77         
78         // Multiboot 2
79         case MULTIBOOT2_MAGIC:
80                 Panic("Multiboot 2 not yet supported");
81                 //MM_InstallMBoot2( MbInfo );   // Set up physical memory manager
82                 return 0;
83                 break;
84         
85         default:
86                 Panic("Multiboot magic invalid %08x, expected %08x or %08x\n",
87                         MbMagic, MULTIBOOT_MAGIC, MULTIBOOT2_MAGIC);
88                 return 0;
89         }
90         
91         // Set up physical memory manager
92         MM_Install(nPMemMapEnts, pmemmap);
93         
94         MM_InstallVirtual();    // Clean up virtual address space
95         Heap_Install();         // Create initial heap
96         Time_Setup();   // Initialise timing
97         
98         // Start Multitasking
99         Threads_Init();
100
101         #if USE_ACPICA
102         // Poke ACPICA
103         ACPICA_Initialise();
104         #endif
105
106         Log_Log("Arch", "Starting VFS...");
107         // Load Virtual Filesystem
108         VFS_Init();
109         
110         // Load initial modules
111         gaArch_BootModules = Multiboot_LoadModules(mbInfo, KERNEL_BASE, &giArch_NumBootModules);
112         
113         // Pass on to Independent Loader
114         Log_Log("Arch", "Starting system");
115         System_Init(gsBootCmdLine);
116         
117         // Sleep forever (sleeping beauty)
118         for(;;)
119                 Threads_Sleep();
120         return 0;
121 }
122
123 void Arch_LoadBootModules(void)
124 {
125         for( int i = 0; i < giArch_NumBootModules; i ++ )
126         {
127                 const tBootModule       *mod = &gaArch_BootModules[i];
128                 Log_Log("Arch", "Loading (%p[%P]+%x) '%s'",
129                         mod->Base, mod->PBase, mod->Size,
130                         mod->ArgString);
131                 
132                 if( !Module_LoadMem( mod->Base, mod->Size, mod->ArgString) ) {
133                         Log_Warning("Arch", "Unable to load module");
134                         continue ;
135                 }
136                 
137                 #if 0
138                 // Unmap and free
139                 int numPages = (mod->Size + ((tVAddr)mod->Base&0xFFF) + 0xFFF) >> 12;
140                 MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].Base, numPages );
141                 
142                 //for( int j = 0; j < numPages; j++ )
143                 //      MM_DerefPhys( mod->PBase + (j << 12) );
144                 
145                 if( (tVAddr) mod->ArgString > MAX_ARGSTR_POS )
146                         MM_UnmapHWPages( (tVAddr)mod->ArgString, 2 );
147                 #endif
148         }
149         Log_Log("Arch", "Boot modules loaded");
150         if( gaArch_BootModules )
151                 free( gaArch_BootModules );
152 }

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