Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / all-mingw32 / kernel / kernel_init.c
blob482bdf9e5dd24078d4eb4f246de6d7a034288751
1 #define DEBUG 0
3 #include <inttypes.h>
4 #include <aros/symbolsets.h>
5 #include <exec/lists.h>
6 #include <memory.h>
7 #include <exec/resident.h>
8 #include <exec/memheaderext.h>
9 #include <utility/tagitem.h>
11 #include <proto/exec.h>
12 #include <proto/alib.h>
13 #include <proto/kernel.h>
15 #include <aros/kernel.h>
16 #include <aros/debug.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <stdlib.h>
22 #include "cpucontext.h"
23 #include "kernel_intern.h"
24 #include LC_LIBDEFS_FILE
26 extern struct ExecBase * PrepareExecBase(struct MemHeader *);
27 extern ULONG ** Exec_RomTagScanner(struct ExecBase*,UWORD**);
29 static struct TagItem *BootMsg;
30 struct HostInterface *HostIFace;
31 struct KernelInterface KernelIFace;
32 APTR KernelBase = NULL;
33 /* static char cmdLine[200]; TODO */
35 #undef kprintf
36 #undef rkprintf
37 #undef vkprintf
39 int mykprintf(const UBYTE * fmt, ...)
41 va_list args;
42 int r;
44 va_start(args, fmt);
45 if (SysBase)
46 Forbid();
47 r = HostIFace->VKPrintF(fmt, args);
48 if (SysBase)
49 Permit();
50 va_end(args);
51 return r;
54 int myvkprintf (const UBYTE *fmt, va_list args)
56 int res;
58 if (SysBase)
59 Forbid();
60 res = HostIFace->VKPrintF(fmt, args);
61 if (SysBase)
62 Permit();
63 return res;
66 int myrkprintf(const STRPTR foo, const STRPTR bar, int baz, const UBYTE * fmt, ...)
68 va_list args;
69 int r;
71 va_start(args, fmt);
72 if (SysBase)
73 Forbid();
74 r = HostIFace->VKPrintF(fmt, args);
75 if (SysBase)
76 Permit();
77 va_end(args);
78 return r;
81 void __clear_bss(struct TagItem *msg)
83 struct KernelBSS *bss;
85 bss = (struct KernelBSS *)krnGetTagData(KRN_KernelBss, 0, msg);
87 if (bss)
89 while (bss->addr)
91 bzero((void*)bss->addr, bss->len);
92 bss++;
97 AROS_LH0I(struct TagItem *, KrnGetBootInfo,
98 struct KernelBase *, KernelBase, 1, Kernel)
100 AROS_LIBFUNC_INIT
102 return BootMsg;
104 AROS_LIBFUNC_EXIT
107 AROS_LH0I(void *, KrnCreateContext,
108 struct KernelBase *, KernelBase, 10, Kernel)
110 AROS_LIBFUNC_INIT
112 return AllocMem(sizeof(struct AROSCPUContext), MEMF_PUBLIC|MEMF_CLEAR);
114 AROS_LIBFUNC_EXIT
117 AROS_LH1I(void, KrnDeleteContext,
118 AROS_LHA(void *, context, A0),
119 struct KernelBase *, KernelBase, 10, Kernel)
121 AROS_LIBFUNC_INIT
123 FreeMem(context, sizeof(struct AROSCPUContext));
125 AROS_LIBFUNC_EXIT
128 /* auto init */
129 static int Kernel_Init(LIBBASETYPEPTR kBase)
131 int i;
133 KernelBase = kBase;
134 D(mykprintf("[Kernel] init (KernelBase=%p)\n", kBase));
135 D(mykprintf("[Kernel] -1 : %p -2 : %p\n", *((APTR*)(((APTR*)kBase)-1)),*((APTR*)(((APTR*)kBase)-2))));
136 for (i=0; i < EXCEPTIONS_NUM; i++)
137 NEWLIST(&kBase->kb_Exceptions[i]);
139 for (i=0; i < INTERRUPTS_NUM; i++)
140 NEWLIST(&kBase->kb_Interrupts[i]);
141 D(mykprintf("[Kernel] KrnGetBootInfo yields %p\n",Kernel_KrnGetBootInfo()));
142 return 1;
145 ADD2INITLIB(Kernel_Init, 0)
147 char *kernel_functions[] = {
148 "core_init",
149 "core_intr_disable",
150 "core_intr_enable",
151 "core_syscall",
152 "core_is_super",
153 NULL
156 /* rom startup */
159 //make this the entry point
160 //int startup(struct TagItem *msg) __attribute__ ((section (".aros.init")));
162 int __startup startup(struct TagItem *msg)
164 void *hostlib;
165 char *errstr;
166 unsigned long badsyms;
167 struct MemHeader *mh;
169 BootMsg = msg;
171 void * klo = (void*)krnGetTagData(KRN_KernelLowest, 0, msg);
172 void * khi = (void*)krnGetTagData(KRN_KernelHighest, 0, msg);
173 void * memory = (void*)krnGetTagData(KRN_MEMLower, 0, msg);
174 void * memupper = krnGetTagData(KRN_MEMUpper, 0, msg);
175 HostIFace = (struct HostInterface *)krnGetTagData(KRN_HostInterface, 0, msg);
177 hostlib = HostIFace->HostLib_Open("Libs\\Host\\kernel.dll", &errstr);
178 if (!hostlib) {
179 mykprintf("[Kernel] failed to load host-side module: %s\n", errstr);
180 HostIFace->HostLib_FreeErrorStr(errstr);
181 return -1;
183 badsyms = HostIFace->HostLib_GetInterface(hostlib, kernel_functions, &KernelIFace);
184 if (badsyms) {
185 mykprintf("[Kernel] failed to resolve %lu symbols\n", badsyms);
186 HostIFace->HostLib_Close(hostlib, NULL);
187 return -1;
190 mykprintf("[Kernel] preparing first mem header\n");
192 /* Prepare the first mem header and hand it to PrepareExecBase to take SysBase live */
193 mh = memory;
194 mh->mh_Node.ln_Type = NT_MEMORY;
195 mh->mh_Node.ln_Name = "chip memory";
196 mh->mh_Node.ln_Pri = -5;
197 mh->mh_Attributes = MEMF_CHIP | MEMF_PUBLIC | MEMF_LOCAL | MEMF_24BITDMA | MEMF_KICK;
198 mh->mh_First = memory + MEMHEADER_TOTAL;
199 mh->mh_First->mc_Next = NULL;
200 mh->mh_First->mc_Bytes = memupper - memory + 1 - MEMHEADER_TOTAL;
201 mh->mh_Lower = memory;
202 mh->mh_Upper = memupper;
203 mh->mh_Free = mh->mh_First->mc_Bytes;
205 mykprintf("[Kernel] calling PrepareExecBase@%p mh_First=%p\n",PrepareExecBase,mh->mh_First);
207 * FIXME: This routine is part of exec.library, however it doesn't have an LVO
208 * (it can't have one because exec.library is not initialized yet) and is called
209 * only from here. Probably the code should be reorganized
211 SysBase = PrepareExecBase(mh);
212 mykprintf("[Kernel] SysBase=%p mhFirst=%p\n",SysBase,mh->mh_First);
214 /* ROM memory header. This special memory header covers all ROM code and data sections
215 * so that TypeOfMem() will not return 0 for addresses pointing into the kernel.
217 if ((mh = (struct MemHeader *)AllocMem(sizeof(struct MemHeader), MEMF_PUBLIC)))
219 mh->mh_Node.ln_Type = NT_MEMORY;
220 mh->mh_Node.ln_Name = "rom memory";
221 mh->mh_Node.ln_Pri = -128;
222 mh->mh_Attributes = MEMF_KICK;
223 mh->mh_First = NULL;
224 mh->mh_Lower = klo;
225 mh->mh_Upper = khi;
226 mh->mh_Free = 0; /* Never allocate from this chunk! */
227 Enqueue(&SysBase->MemList, &mh->mh_Node);
230 ((struct AROSSupportBase *)(SysBase->DebugAROSBase))->kprintf = mykprintf;
231 ((struct AROSSupportBase *)(SysBase->DebugAROSBase))->rkprintf = myrkprintf;
232 ((struct AROSSupportBase *)(SysBase->DebugAROSBase))->vkprintf = myvkprintf;
234 mykprintf("[Kernel] calling Exec_RomTagScanner@%p\n",Exec_RomTagScanner);
235 UWORD * ranges[] = {klo,khi,(UWORD *)~0};
237 * FIXME: Cross-module call again
239 SysBase->ResModules = Exec_RomTagScanner(SysBase,ranges);
241 mykprintf("[Kernel] initializing host-side kernel module\n");
242 if (!KernelIFace.core_init(SysBase->VBlankFrequency, &SysBase, &KernelBase)) {
243 mykprintf("[Kernel] Failed to initialize!\n");
244 return -1;
247 mykprintf("[Kernel] calling InitCode(RTF_SINGLETASK,0)\n");
248 InitCode(RTF_SINGLETASK, 0);
250 mykprintf("leaving startup!\n");
251 HostIFace->HostLib_Close(hostlib, NULL);
252 return 1;
256 struct TagItem *krnNextTagItem(const struct TagItem **tagListPtr)
258 if (!(*tagListPtr)) return 0;
260 while(1)
262 switch((*tagListPtr)->ti_Tag)
264 case TAG_MORE:
265 if (!((*tagListPtr) = (struct TagItem *)(*tagListPtr)->ti_Data))
266 return NULL;
267 continue;
268 case TAG_IGNORE:
269 break;
271 case TAG_END:
272 (*tagListPtr) = 0;
273 return NULL;
275 case TAG_SKIP:
276 (*tagListPtr) += (*tagListPtr)->ti_Data + 1;
277 continue;
279 default:
280 return (struct TagItem *)(*tagListPtr)++;
284 (*tagListPtr)++;
288 struct TagItem *krnFindTagItem(Tag tagValue, const struct TagItem *tagList)
290 struct TagItem *tag;
291 const struct TagItem *tagptr = tagList;
293 while((tag = krnNextTagItem(&tagptr)))
295 if (tag->ti_Tag == tagValue)
296 return tag;
299 return 0;
302 IPTR krnGetTagData(Tag tagValue, intptr_t defaultVal, const struct TagItem *tagList)
304 struct TagItem *ti = 0;
306 if (tagList && (ti = krnFindTagItem(tagValue, tagList)))
307 return ti->ti_Data;
309 return defaultVal;