Merge branch 'master' of git://localhost/acess2
[tpg/acess2.git] / KernelLand / Kernel / arch / x86_64 / main.c
1 /*
2  * Acess2 Kernel x86_64
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Kernel C entrypoint
7  */
8 #include <acess.h>
9 #include <mboot.h>
10 #include <init.h>
11 #include <archinit.h>
12 #include <pmemmap.h>
13
14 // === CONSTANTS ===
15 #define KERNEL_LOAD     0x100000
16 #define MAX_PMEMMAP_ENTS        16
17 #define MAX_ARGSTR_POS  (0x200000-0x2000)
18
19 // === IMPORTS ===
20 extern void     Desctab_Init(void);
21 extern void     MM_InitVirt(void);
22 extern void     Heap_Install(void);
23 extern int      Time_Setup(void);
24
25 extern char     gKernelEnd[];
26
27 // === PROTOTYPES ===
28 void    kmain(Uint MbMagic, void *MbInfoPtr);
29
30 // === GLOBALS ==
31 char    *gsBootCmdLine = NULL;
32 tBootModule     *gaArch_BootModules;
33  int    giArch_NumBootModules = 0;
34
35 // === CODE ===
36 void kmain(Uint MbMagic, void *MbInfoPtr)
37 {
38         tMBoot_Info     *mbInfo;
39         tPMemMapEnt     pmemmap[MAX_PMEMMAP_ENTS];
40          int    nPMemMapEnts;
41
42         LogF("%s\r\n", gsBuildInfo);
43         
44         Desctab_Init();
45
46         MM_InitVirt();
47         *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'C';
48         
49         switch(MbMagic)
50         {
51         // Multiboot 1
52         case MULTIBOOT_MAGIC:
53                 // Adjust Multiboot structure address
54                 mbInfo = (void*)( (Uint)MbInfoPtr + KERNEL_BASE );
55                 gsBootCmdLine = (char*)( (Uint)mbInfo->CommandLine + KERNEL_BASE);
56                 nPMemMapEnts = Multiboot_LoadMemoryMap(mbInfo, KERNEL_BASE, pmemmap, MAX_PMEMMAP_ENTS,
57                         KERNEL_LOAD, (tVAddr)&gKernelEnd - KERNEL_BASE
58                         );
59                 break;
60         default:
61                 Panic("Multiboot magic invalid %08x, expected %08x\n",
62                         MbMagic, MULTIBOOT_MAGIC);
63                 return ;
64         }
65         
66         MM_InitPhys( nPMemMapEnts, pmemmap );   // Set up physical memory manager
67         Log("gsBootCmdLine = '%s'", gsBootCmdLine);
68         
69         switch(MbMagic)
70         {
71         case MULTIBOOT_MAGIC:
72                 MM_RefPhys( mbInfo->CommandLine );
73                 break;
74         }
75
76         *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'D';
77         Heap_Install();
78         
79         *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'E';
80         Threads_Init();
81         
82         Time_Setup();
83         *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'F';
84         
85         // Load Virtual Filesystem
86         Log_Log("Arch", "Starting VFS...");
87         VFS_Init();
88         
89         // Multiboot_InitFramebuffer(mbInfo);
90
91         gaArch_BootModules = Multiboot_LoadModules(mbInfo, KERNEL_BASE, &giArch_NumBootModules);
92         
93         *(Uint16*)(KERNEL_BASE|0xB8000) = 0x1F00|'Z';
94         
95         // Pass on to Independent Loader
96         Log_Log("Arch", "Starting system");
97         System_Init(gsBootCmdLine);
98         
99         // Sleep forever (sleeping beauty)
100         for(;;)
101                 Threads_Sleep();
102 }
103
104 void Arch_LoadBootModules(void)
105 {
106          int    i, j, numPages;
107         Log("gsBootCmdLine = '%s'", gsBootCmdLine);
108         for( i = 0; i < giArch_NumBootModules; i ++ )
109         {
110                 Log_Log("Arch", "Loading '%s'", gaArch_BootModules[i].ArgString);
111                 
112                 if( !Module_LoadMem( gaArch_BootModules[i].Base,
113                         gaArch_BootModules[i].Size, gaArch_BootModules[i].ArgString
114                         ) )
115                 {
116                         Log_Warning("Arch", "Unable to load module");
117                 }
118                 
119                 // Unmap and free
120                 numPages = (gaArch_BootModules[i].Size + ((Uint)gaArch_BootModules[i].Base&0xFFF) + 0xFFF) >> 12;
121                 MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].Base, numPages );
122                 
123                 for( j = 0; j < numPages; j++ )
124                         MM_DerefPhys( gaArch_BootModules[i].PBase + (j << 12) );
125                 
126                 if( (tVAddr) gaArch_BootModules[i].ArgString < KERNEL_BASE )
127                         MM_UnmapHWPages( (tVAddr)gaArch_BootModules[i].ArgString, 2 );
128         }
129         Log_Log("Arch", "Boot modules loaded");
130         if( gaArch_BootModules )
131                 free( gaArch_BootModules );
132 }
133
134 void StartupPrint(const char *String)
135 {
136         
137 }

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