Added support for compiling C++ files. It isn't included for all
[AROS.git] / arch / all-mingw32 / kernel / kernel_startup.c
blobe20e30750c77b4ad11ce43f7a5b4ac3ffdff10b0
1 #include <aros/kernel.h>
2 #include <aros/multiboot.h>
3 #include <aros/symbolsets.h>
4 #include <exec/execbase.h>
5 #include <exec/resident.h>
6 #include <utility/tagitem.h>
7 #include <proto/arossupport.h>
8 #include <proto/exec.h>
10 #include <inttypes.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include "hostinterface.h"
15 #include "kernel_base.h"
16 #include "kernel_debug.h"
17 #include "kernel_romtags.h"
18 #include "kernel_mingw32.h"
20 #define D(x)
22 /* Some globals we can't live without */
23 struct HostInterface *HostIFace;
24 struct KernelInterface KernelIFace;
25 static const char *kernel_functions[];
27 /* rom startup */
28 int __startup startup(struct TagItem *msg, ULONG magic)
30 void *_stack = AROS_GET_SP;
31 void *hostlib;
32 char *errstr;
33 unsigned int i;
34 struct MemHeader *mh;
35 struct TagItem *tag, *tstate = msg;
36 struct HostInterface *hif = NULL;
37 struct mb_mmap *mmap = NULL;
38 ULONG memflags = MEMF_CHIP|MEMF_PUBLIC|MEMF_LOCAL|MEMF_KICK;
39 UWORD *ranges[] = {NULL, NULL, (UWORD *)-1};
41 /* Fail if we are ocassionally started from within AROS command line */
42 if (magic != AROS_BOOT_MAGIC)
43 return -1;
45 while ((tag = LibNextTagItem(&tstate)))
47 switch (tag->ti_Tag)
49 case KRN_KernelLowest:
50 ranges[0] = (UWORD *)tag->ti_Data;
51 break;
53 case KRN_KernelHighest:
54 ranges[1] = (UWORD *)tag->ti_Data;
55 break;
57 case KRN_MMAPAddress:
58 mmap = (struct mb_mmap *)tag->ti_Data;
59 break;
61 case KRN_KernelBss:
62 __clear_bss((struct KernelBSS *)tag->ti_Data);
63 break;
65 case KRN_HostInterface:
66 hif = (struct HostInterface *)tag->ti_Data;
67 break;
71 /* If there's no HostIFace, we can't even say anything */
72 if (!hif)
73 return -1;
75 /* Set globals only AFTER __clear_bss() */
76 BootMsg = msg;
77 HostIFace = hif;
79 /* Validate our HostInterface version */
80 if (strcmp(HostIFace->System, "Windows"))
81 return -1;
82 if (HostIFace->Version != HOSTINTERFACE_VERSION)
83 return -1;
85 /* Now we have debug output, we can bug() */
86 hostlib = HostIFace->hostlib_Open("Libs\\Host\\kernel.dll", &errstr);
87 if (!hostlib)
89 bug("[Kernel] Failed to load host-side module: %s\n", errstr);
90 HostIFace->hostlib_FreeErrorStr(errstr);
91 return -1;
94 for (i = 0; kernel_functions[i]; i++)
96 void *func = HostIFace->hostlib_GetPointer(hostlib, kernel_functions[i], &errstr);
98 if (!func)
100 bug("[Kernel] Failed to find symbol %s in host-side module: %s\n", kernel_functions[i], errstr);
101 HostIFace->hostlib_FreeErrorStr(errstr);
102 HostIFace->hostlib_Close(hostlib, NULL);
104 return -1;
106 ((void **)&KernelIFace)[i] = func;
109 /* Now we have core_alert(), krnDisplayAlert() works */
110 if ((!ranges[0]) || (!ranges[1]) || (!mmap))
112 krnPanic(KernelBase, "Not enough information from the bootstrap\n"
113 "Kickstart start 0x%p, end 0x%p\n"
114 "Memory map address: 0x%p",
115 ranges[0], ranges[1], mmap);
116 return -1;
119 #if __WORDSIZE > 32
121 * On x86-64 VirtualAlloc() still seems to return memory within 32-bit region.
122 * The documentation doesn't specify it explicitly, so we make a check, just in case.
123 * Looks like the lowest possible region is returned.
125 if (mmap->addr + mmap->len <= 0x80000000)
126 memflags |= MEMF_31BIT;
127 #endif
130 * Prepare the first mem header and hand it to PrepareExecBase to take SysBase live
131 * We know that memory map has only one RAM element.
133 D(bug("[Kernel] preparing first mem header\n"));
134 mh = (struct MemHeader *)mmap->addr;
135 krnCreateMemHeader("Normal RAM", -5, mh, mmap->len, memflags);
138 * TODO: this needs to be replaced by SysBase address validation.
139 * This will make KickTags and capture vectors working.
141 SysBase = NULL;
143 D(bug("[Kernel] calling krnPrepareExecBase(), mh_First = 0x%p, msg = 0x%p\n", mh->mh_First, msg));
144 if (!krnPrepareExecBase(ranges, mh, msg))
145 return -1;
147 D(bug("[Kernel] SysBase=0x%p, mh_First=0x%p\n", SysBase, mh->mh_First);)
150 * ROM memory header. This special memory header covers all ROM code and data sections
151 * so that TypeOfMem() will not return 0 for addresses pointing into the kickstart.
153 krnCreateROMHeader("Kickstart ROM", ranges[0], ranges[1]);
156 * Stack memory header. This special memory header covers a little part of the programs
157 * stack so that TypeOfMem() will not return 0 for addresses pointing into the stack
158 * during initialization.
160 krnCreateROMHeader("Boot stack", _stack - AROS_STACKSIZE, _stack);
162 D(bug("[Kernel] calling InitCode(RTF_SINGLETASK,0)\n"));
163 InitCode(RTF_SINGLETASK, 0);
164 InitCode(RTF_COLDSTART, 0);
166 krnPanic(KernelBase, "Failed to start up the system");
167 HostIFace->hostlib_Close(hostlib, NULL);
168 return 1;
171 /* Functions we want from Libs/Host/kernel.dll */
172 static const char *kernel_functions[] =
174 "core_init",
175 "core_raise",
176 "core_protect",
177 "core_putc",
178 "core_getc",
179 "core_alert",
180 "TrapVector",
181 "IRQVector",
182 "Ints_Enabled",
183 "Supervisor",
184 "Sleep_Mode",
185 "LastErrorPtr",
186 NULL
189 static int Platform_Init(struct KernelBase *KernelBase)
191 char *args;
193 /* Our VBlank is software-driven, we can ask the user what frequency to use. Default is 50 Hz. */
194 SysBase->VBlankFrequency = 50;
196 args = (char *)LibGetTagData(KRN_CmdLine, 0, BootMsg);
197 if (args)
199 char *s = strstr(args, "vblank=");
201 if (s)
202 SysBase->VBlankFrequency = atoi(&s[7]);
205 D(bug("[Kernel] initializing host-side kernel module, timer frequency is %u\n", SysBase->VBlankFrequency));
207 *KernelIFace.TrapVector = core_TrapHandler;
208 *KernelIFace.IRQVector = core_IRQHandler;
210 return KernelIFace.core_init(SysBase->VBlankFrequency);
213 ADD2INITLIB(Platform_Init, 10);