4 #include <aros/symbolsets.h>
5 #include <exec/lists.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>
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 */
39 int mykprintf(const UBYTE
* fmt
, ...)
47 r
= HostIFace
->VKPrintF(fmt
, args
);
54 int myvkprintf (const UBYTE
*fmt
, va_list args
)
60 res
= HostIFace
->VKPrintF(fmt
, args
);
66 int myrkprintf(const STRPTR foo
, const STRPTR bar
, int baz
, const UBYTE
* fmt
, ...)
74 r
= HostIFace
->VKPrintF(fmt
, args
);
81 void __clear_bss(struct TagItem
*msg
)
83 struct KernelBSS
*bss
;
85 bss
= (struct KernelBSS
*)krnGetTagData(KRN_KernelBss
, 0, msg
);
91 bzero((void*)bss
->addr
, bss
->len
);
97 AROS_LH0I(struct TagItem
*, KrnGetBootInfo
,
98 struct KernelBase
*, KernelBase
, 1, Kernel
)
107 AROS_LH0I(void *, KrnCreateContext
,
108 struct KernelBase
*, KernelBase
, 10, Kernel
)
112 return AllocMem(sizeof(struct AROSCPUContext
), MEMF_PUBLIC
|MEMF_CLEAR
);
117 AROS_LH1I(void, KrnDeleteContext
,
118 AROS_LHA(void *, context
, A0
),
119 struct KernelBase
*, KernelBase
, 10, Kernel
)
123 FreeMem(context
, sizeof(struct AROSCPUContext
));
129 static int Kernel_Init(LIBBASETYPEPTR 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()));
145 ADD2INITLIB(Kernel_Init
, 0)
147 char *kernel_functions
[] = {
159 //make this the entry point
160 //int startup(struct TagItem *msg) __attribute__ ((section (".aros.init")));
162 int __startup
startup(struct TagItem
*msg
)
166 unsigned long badsyms
;
167 struct MemHeader
*mh
;
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
);
179 mykprintf("[Kernel] failed to load host-side module: %s\n", errstr
);
180 HostIFace
->HostLib_FreeErrorStr(errstr
);
183 badsyms
= HostIFace
->HostLib_GetInterface(hostlib
, kernel_functions
, &KernelIFace
);
185 mykprintf("[Kernel] failed to resolve %lu symbols\n", badsyms
);
186 HostIFace
->HostLib_Close(hostlib
, NULL
);
190 mykprintf("[Kernel] preparing first mem header\n");
192 /* Prepare the first mem header and hand it to PrepareExecBase to take SysBase live */
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
;
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");
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
);
256 struct TagItem
*krnNextTagItem(const struct TagItem
**tagListPtr
)
258 if (!(*tagListPtr
)) return 0;
262 switch((*tagListPtr
)->ti_Tag
)
265 if (!((*tagListPtr
) = (struct TagItem
*)(*tagListPtr
)->ti_Data
))
276 (*tagListPtr
) += (*tagListPtr
)->ti_Data
+ 1;
280 return (struct TagItem
*)(*tagListPtr
)++;
288 struct TagItem
*krnFindTagItem(Tag tagValue
, const struct TagItem
*tagList
)
291 const struct TagItem
*tagptr
= tagList
;
293 while((tag
= krnNextTagItem(&tagptr
)))
295 if (tag
->ti_Tag
== tagValue
)
302 IPTR
krnGetTagData(Tag tagValue
, intptr_t defaultVal
, const struct TagItem
*tagList
)
304 struct TagItem
*ti
= 0;
306 if (tagList
&& (ti
= krnFindTagItem(tagValue
, tagList
)))