config/make.tmpl(%build_module): Support use of _rel libs in uselibs=
[AROS.git] / arch / ppc-chrp / exec / exec_init.c
blobaaf723bb2505897f59043c97705ef320c30c9548
1 #define DEBUG 1
3 #include <asm/mpc5200b.h>
4 #include <aros/config.h>
5 #include <aros/libcall.h>
6 #include <aros/asmcall.h>
7 #include <aros/arossupportbase.h>
8 #include <aros/symbolsets.h>
9 #include <exec/execbase.h>
10 #include <exec/types.h>
11 #include <exec/resident.h>
12 #include <dos/bptr.h>
13 #include <dos/dosextens.h>
14 #include <hardware/custom.h>
15 #include <hardware/intbits.h>
17 #include <proto/exec.h>
18 #include <proto/kernel.h>
20 #include <stdarg.h>
21 #include <strings.h>
22 #include <inttypes.h>
24 #include "etask.h"
25 #include "exec_intern.h"
26 #include "exec_util.h"
27 #include "intservers.h"
28 #include "memory.h"
29 #include "taskstorage.h"
31 D(extern void debugmem(void));
33 void exec_main(struct TagItem *msg, void *entry);
34 extern CONST_APTR Exec_FuncTable[];
35 extern ULONG Exec_15_MakeFunctions(APTR, CONST_APTR, CONST_APTR, struct ExecBase *);
36 void exec_DefaultTaskExit();
37 IPTR **exec_RomTagScanner(struct TagItem *msg, struct ExecBase *);
38 extern struct Library * PrepareAROSSupportBase (struct ExecBase *);
39 intptr_t krnGetTagData(Tag tagValue, intptr_t defaultVal, const struct TagItem *tagList);
41 struct ExecBase *priv_SysBase;
43 AROS_LD2(int, KrnBug,
44 AROS_LDA(const char *, format, A0),
45 AROS_LDA(va_list, args, A1),
46 APTR, kernelBase, 12, Kernel);
47 #undef bug
49 static inline void bug(const char *format, ...)
51 va_list args;
53 va_start(args, format);
54 /* Our KrnBug() ignores base address */
55 AROS_SLIB_ENTRY(KrnBug, Kernel, 12)(format, args, NULL);
56 va_end(args);
59 const char exec_name[] = "exec.library";
60 const char exec_idstring[] = "$VER: exec 41.11 (16.12.2000)\r\n";
61 const char exec_fastname[] = "System Memory";
63 const short exec_Version = 41;
64 const short exec_Revision = 11;
66 const struct __attribute__((section(".text"))) Resident Exec_resident =
68 RTC_MATCHWORD, /* Magic value used to find resident */
69 &Exec_resident, /* Points to Resident itself */
70 (APTR)&Exec_resident+1, /* Where could we find next Resident? */
71 0, /* There are no flags!! */
72 41, /* Version */
73 NT_LIBRARY, /* Type */
74 126, /* Very high startup priority. */
75 (STRPTR)exec_name, /* Pointer to name string */
76 (STRPTR)exec_idstring, /* Ditto */
77 exec_main /* Library initializer (for exec this value is irrelevant since we've jumped there at the begining to bring the system up */
80 THIS_PROGRAM_HANDLES_SYMBOLSETS
81 DEFINESET(INITLIB)
83 void exec_main(struct TagItem *msg, void *entry)
85 struct ExecBase *SysBase = NULL;
86 struct TaskStorageFreeSlot *tsfs;
87 struct Task *t;
88 uintptr_t lowmem = 0;
89 int i;
91 priv_SysBase = NULL;
93 D(bug("[exec] AROS for Efika5200B - The AROS Research OS\n"));
95 /* Prepare the exec base */
97 ULONG negsize = LIB_VECTSIZE; /* size of vector table */
98 CONST_APTR *fp = Exec_FuncTable; /* pointer to a function in the table */
100 D(bug("[exec] Preparing the ExecBase...\n"));
102 /* Calculate the size of the vector table */
103 while (*fp++ != (APTR) -1) negsize += LIB_VECTSIZE;
105 /* Align the offset for SysBase to the cache line */
106 negsize = (negsize + 31) & ~31;
108 /* Get the lowest usable memory location */
109 lowmem = 0x3000;
111 /* And now let's have the SysBase */
112 SysBase = (struct ExecBase *)(lowmem + negsize);
113 wrspr(SPRG5, SysBase);
114 *(struct ExecBase **)4UL = SysBase;
115 /* Store the SysBase for local use. It's ugly, it's dirty, it's a hack. */
116 priv_SysBase = SysBase;
117 lowmem = (lowmem + negsize + sizeof(struct IntExecBase) + 4095) & ~4095;
119 D(bug("[exec] ExecBase at %08x\n", SysBase));
121 D(bug("[exec] Clearing ExecBase\n"));
123 /* How about clearing most of ExecBase structure? */
124 bzero(&SysBase->IntVects[0], sizeof(struct IntExecBase) - offsetof(struct ExecBase, IntVects[0]));
126 SysBase->KickMemPtr = NULL;
127 SysBase->KickTagPtr = NULL;
128 SysBase->KickCheckSum = NULL;
130 SysBase->DispCount = 0;
131 SysBase->IdleCount = 0;
134 * Now everything is prepared to store ExecBase at the location 4UL and set
135 * it complement in ExecBase structure
138 D(bug("[exec] Initializing library...\n"));
140 SysBase->ChkBase = ~(ULONG)SysBase;
142 /* Store memory configuration */
143 SysBase->MaxLocMem = (IPTR)0; //locmem;
144 SysBase->MaxExtMem = (APTR)0; //extmem;
147 * Initialize exec lists. This is done through information table which consist
148 * of offset from begining of ExecBase and type of the list.
150 NEWLIST(&SysBase->MemList);
151 SysBase->MemList.lh_Type = NT_MEMORY;
152 NEWLIST(&SysBase->ResourceList);
153 SysBase->ResourceList.lh_Type = NT_RESOURCE;
154 NEWLIST(&SysBase->DeviceList);
155 SysBase->DeviceList.lh_Type = NT_DEVICE;
156 NEWLIST(&SysBase->LibList);
157 SysBase->LibList.lh_Type = NT_LIBRARY;
158 NEWLIST(&SysBase->PortList);
159 SysBase->PortList.lh_Type = NT_MSGPORT;
160 NEWLIST(&SysBase->TaskReady);
161 SysBase->TaskReady.lh_Type = NT_TASK;
162 NEWLIST(&SysBase->TaskWait);
163 SysBase->TaskWait.lh_Type = NT_TASK;
164 NEWLIST(&SysBase->IntrList);
165 SysBase->IntrList.lh_Type = NT_INTERRUPT;
166 NEWLIST(&SysBase->SemaphoreList);
167 SysBase->SemaphoreList.lh_Type = NT_SIGNALSEM;
168 NEWLIST(&SysBase->ex_MemHandlers);
170 for (i=0; i<5; i++)
172 NEWLIST(&SysBase->SoftInts[i].sh_List);
173 SysBase->SoftInts[i].sh_List.lh_Type = NT_SOFTINT;
177 * Exec.library initializer. Prepares exec.library for future use. All
178 * lists have to be initialized, some values from ROM are copied.
181 SysBase->TaskTrapCode = NULL; //exec_DefaultTrap;
182 SysBase->TaskExceptCode = NULL; //exec_DefaultTrap;
183 SysBase->TaskExitCode = exec_DefaultTaskExit;
184 SysBase->TaskSigAlloc = 0x0000ffff;
185 SysBase->TaskTrapAlloc = 0x8000;
187 /* Prepare values for execBase (like name, type, pri and other) */
189 SysBase->LibNode.lib_Node.ln_Type = NT_LIBRARY;
190 SysBase->LibNode.lib_Node.ln_Pri = 0;
191 SysBase->LibNode.lib_Node.ln_Name = (char *)exec_name;
192 SysBase->LibNode.lib_Flags = LIBF_CHANGED | LIBF_SUMUSED;
193 SysBase->LibNode.lib_PosSize = sizeof(struct IntExecBase);
194 SysBase->LibNode.lib_OpenCnt = 1;
195 SysBase->LibNode.lib_IdString = (char *)exec_idstring;
196 SysBase->LibNode.lib_Version = exec_Version;
197 SysBase->LibNode.lib_Revision = exec_Revision;
199 SysBase->Quantum = 4;
200 SysBase->Elapsed = 4;
201 SysBase->VBlankFrequency = 50;
202 SysBase->PowerSupplyFrequency = 1;
204 NEWLIST(&PrivExecBase(SysBase)->ResetHandlers);
205 NEWLIST(&PrivExecBase(SysBase)->AllocMemList);
207 #if AROS_MUNGWALL_DEBUG
209 * TODO: implement command line parsing instead of this awkward hack
210 * Or, even better, merge this init code with arch-independent one
212 PrivExecBase(SysBase)->IntFlags = EXECF_MungWall;
213 #endif
215 /* Build the jumptable */
216 SysBase->LibNode.lib_NegSize =
217 Exec_15_MakeFunctions(SysBase, Exec_FuncTable, NULL, SysBase);
219 SumLibrary((struct Library *)SysBase);
221 InitSemaphore(&PrivExecBase(SysBase)->MemListSem);
222 InitSemaphore(&PrivExecBase(SysBase)->LowMemSem);
224 PrivExecBase(SysBase)->TaskStorageSize = TASKSTORAGEPUDDLE;
225 NEWLIST(&PrivExecBase(SysBase)->TaskStorageSlots);
227 D(bug("[exec] Adding memory\n"));
229 AddMemList(0x07000000 - lowmem,
230 MEMF_FAST | MEMF_PUBLIC | MEMF_KICK | MEMF_LOCAL,
232 (APTR)lowmem,
233 (STRPTR)exec_fastname);
235 SumLibrary((struct Library *)SysBase);
237 Enqueue(&SysBase->LibList,&SysBase->LibNode.lib_Node);
239 SysBase->DebugAROSBase = PrepareAROSSupportBase(SysBase);
241 /* Scan for valid RomTags */
242 SysBase->ResModules = exec_RomTagScanner(msg, SysBase);
244 D(bug("[exec] InitCode(RTF_SINGLETASK)\n"));
245 InitCode(RTF_SINGLETASK, 0);
247 /* Install the interrupt servers */
248 for (i=0; i<16; i++)
250 if( (1<<i) & (INTF_PORTS|INTF_COPER|INTF_VERTB|INTF_EXTER|INTF_SETCLR))
252 struct Interrupt *is;
253 struct SoftIntList *sil;
254 is = AllocMem
256 sizeof(struct Interrupt) + sizeof(struct SoftIntList),
257 MEMF_CLEAR | MEMF_PUBLIC
259 if( is == NULL )
261 D(bug("[exec] ERROR: Cannot install Interrupt Servers!\n"));
263 sil = (struct SoftIntList *)((struct Interrupt *)is + 1);
265 is->is_Code = &IntServer;
266 is->is_Data = sil;
267 NEWLIST((struct List *)sil);
268 SetIntVector(i,is);
270 else
272 struct Interrupt *is;
273 switch (i)
275 case INTB_SOFTINT :
276 is = AllocMem
278 sizeof(struct Interrupt),
279 MEMF_CLEAR | MEMF_PUBLIC
281 if (is == NULL)
283 D(bug("[exec] Error: Cannot install Interrupt Servers!\n"));
284 // Alert(AT_DeadEnd | AN_IntrMem);
286 is->is_Node.ln_Type = NT_SOFTINT; //INTERRUPT;
287 is->is_Node.ln_Pri = 0;
288 is->is_Node.ln_Name = "SW Interrupt Dispatcher";
289 is->is_Data = NULL;
290 is->is_Code = (void *)SoftIntDispatch;
291 SetIntVector(i,is);
292 break;
297 /* Now it's time to calculate exec checksum. It will be used
298 * in future to distinguish whether we'd had proper execBase
299 * before restart */
301 UWORD sum=0, *ptr = &SysBase->SoftVer;
302 int i=((IPTR)&SysBase->IntVects[0] - (IPTR)&SysBase->SoftVer) / 2,
305 /* Calculate sum for every static part from SoftVer to ChkSum */
306 for (j=0;j < i;j++)
308 sum+=*(ptr++);
311 SysBase->ChkSum = ~sum;
314 tsfs = AllocMem(sizeof(struct TaskStorageFreeSlot), MEMF_PUBLIC|MEMF_CLEAR);
315 if (!tsfs)
317 D(bug("[exec] ERROR: Cannot create Task Storage!\n"));
319 tsfs->FreeSlot = 1;
320 AddHead((struct List *)&PrivExecBase(SysBase)->TaskStorageSlots, (struct Node *)tsfs);
322 /* Create boot task. Sigh, we actually create a Process sized Task,
323 since DOS needs to call things which think it has a Process and
324 we don't want to overwrite memory with something strange do we?
326 We do this until at least we can boot dos more cleanly.
329 struct MemList *ml;
331 ml = (struct MemList *)AllocMem(sizeof(struct MemList), MEMF_PUBLIC|MEMF_CLEAR);
332 t = (struct Task *) AllocMem(sizeof(struct Process), MEMF_PUBLIC|MEMF_CLEAR);
334 if( !ml || !t )
336 D(bug("[exec] ERROR: Cannot create Boot Task!\n"));
338 ml->ml_NumEntries = 1;
339 ml->ml_ME[0].me_Addr = t;
340 ml->ml_ME[0].me_Length = sizeof(struct Process);
342 NEWLIST(&t->tc_MemEntry);
343 NEWLIST(&((struct Process *)t)->pr_MsgPort.mp_MsgList);
345 /* It's the boot process that RunCommand()s the boot shell, so we
346 must have this list initialized */
347 NEWLIST((struct List *)&((struct Process *)t)->pr_LocalVars);
349 AddHead(&t->tc_MemEntry,&ml->ml_Node);
351 t->tc_Node.ln_Name = (char *)exec_name;
352 t->tc_Node.ln_Pri = 0;
353 t->tc_Node.ln_Type = NT_TASK;
354 t->tc_State = TS_RUN;
355 t->tc_SigAlloc = 0xFFFF;
356 t->tc_SPLower = 0; /* This is the system's stack */
357 t->tc_SPUpper = (APTR)~0UL;
358 t->tc_Flags |= TF_ETASK;
360 if (t->tc_Flags & TF_ETASK)
362 t->tc_UnionETask.tc_ETask = AllocVec
364 sizeof(struct IntETask),
365 MEMF_ANY|MEMF_CLEAR
368 if (!t->tc_UnionETask.tc_ETask)
370 D(bug("[exec] Not enough memory for first task\n"));
373 /* Initialise the ETask data. */
374 InitETask(t, t->tc_UnionETask.tc_ETask);
375 t->tc_UnionETask.tc_ETask->et_RegFrame = KrnCreateContext();
377 if (!t->tc_UnionETask.tc_ETask->et_RegFrame)
379 D(bug("[exec] Not enough memory for first task\n"));
383 SysBase->ThisTask = t;
386 D(bug("[exec] Done. SysBase->ThisTask = %08p\n", SysBase->ThisTask));
388 /* We now start up the interrupts */
389 Permit();
390 Enable();
392 D(debugmem());
394 /* Our housekeeper must have the largest possible priority */
395 t = NewCreateTask(TASKTAG_NAME , "Exec housekeeper",
396 TASKTAG_PRI , 127,
397 TASKTAG_PC , ServiceTask,
398 TASKTAG_TASKMSGPORT, &((struct IntExecBase *)SysBase)->ServicePort,
399 TASKTAG_ARG1 , SysBase,
400 TAG_DONE);
401 if (t)
403 set_call_libfuncs(SETNAME(INITLIB), 1, 1, SysBase);
405 D(bug("[exec] InitCode(RTF_COLDSTART)\n"));
406 InitCode(RTF_COLDSTART, 0);
409 D(bug("[exec] I should never get here...\n"));
414 * RomTag scanner.
416 * This function scans kernel for existing Resident modules. If two modules
417 * with the same name are found, the one with higher version or priority wins.
419 * After building list of kernel modules, the KickTagPtr and KickMemPtr are
420 * checksummed. If checksum is proper and all memory pointed in KickMemPtr may
421 * be allocated, then all modules from KickTagPtr are added to RT list
423 * Afterwards the proper RomTagList is created (see InitCode() for details) and
424 * memory after list and nodes is freed.
427 struct rt_node
429 struct Node node;
430 struct Resident *module;
434 void exec_DefaultTaskExit()
436 struct ExecBase *SysBase = priv_SysBase;
437 RemTask(SysBase->ThisTask);
440 IPTR **exec_RomTagScanner(struct TagItem *msg, struct ExecBase *SysBase)
442 struct List rtList; /* List of modules */
443 UWORD *ptr = (UWORD*)(krnGetTagData(KRN_KernelLowest, 0, msg) ^ 0xf8000000); /* Start looking here */
444 UWORD *maxptr = (UWORD*)(krnGetTagData(KRN_KernelHighest, 0, msg) ^ 0xf8000000);
445 struct Resident *res; /* module found */
447 int i;
448 IPTR **RomTag;
450 /* Initialize list */
451 NEWLIST(&rtList);
453 D(bug("[exec] Resident modules (addr: pri version name):\n"));
455 /* Look in whole kernel for resident modules */
458 /* Do we have RTC_MATCHWORD? */
459 if (*ptr == RTC_MATCHWORD)
461 /* Yes, assume we have Resident */
462 res = (struct Resident *)ptr;
464 /* Does rt_MatchTag point to Resident? */
465 if (res == res->rt_MatchTag)
467 /* Yes, it is Resident module */
468 struct rt_node *node;
470 /* Check if there is module with such name already */
471 node = (struct rt_node*)FindName(&rtList, res->rt_Name);
472 if (node)
474 /* Yes, there was such module. It it had lower pri then replace it */
475 if (node->node.ln_Pri <= res->rt_Pri)
477 /* If they have the same Pri but new one has higher Version, replace */
478 if ((node->node.ln_Pri == res->rt_Pri) &&
479 (node->module->rt_Version < res->rt_Version))
481 node->node.ln_Pri = res->rt_Pri;
482 node->module = res;
486 else
488 /* New module. Allocate some memory for it */
489 node = (struct rt_node *)
490 AllocMem(sizeof(struct rt_node),MEMF_PUBLIC|MEMF_CLEAR);
492 if (node)
494 node->node.ln_Name = (char *)res->rt_Name;
495 node->node.ln_Pri = res->rt_Pri;
496 node->module = res;
498 Enqueue(&rtList,(struct Node*)node);
501 ptr+=sizeof(struct Resident)/sizeof(UWORD);
502 continue;
506 /* Get next address... */
507 ptr++;
508 } while (ptr < maxptr);
511 * By now we have valid (and sorted) list of kernel resident modules.
513 * Now, we will have to analyze used-defined RomTags (via KickTagPtr and
514 * KickMemPtr)
516 #warning "TODO: Implement external modules!"
518 * Everything is done now. Allocate buffer for normal RomTag and convert
519 * list to RomTag
522 ListLength(&rtList,i); /* Get length of the list */
524 RomTag = AllocMem((i+1)*sizeof(IPTR),MEMF_PUBLIC | MEMF_CLEAR);
526 if (RomTag)
528 int j;
529 struct rt_node *n;
531 for (j=0; j<i; j++)
533 n = (struct rt_node *)RemHead(&rtList);
534 D(bug("[exec] + 0x%08lx: %4d %3d \"%s\"\n",
535 n->module,
536 n->node.ln_Pri,
537 n->module->rt_Version,
538 n->node.ln_Name));
539 RomTag[j] = (IPTR*)n->module;
540 FreeMem(n, sizeof(struct rt_node));
542 RomTag[i] = 0;
545 return RomTag;
548 AROS_LH1(struct ExecBase *, open,
549 AROS_LHA(ULONG, version, D0),
550 struct ExecBase *, SysBase, 1, Exec)
552 AROS_LIBFUNC_INIT
554 /* I have one more opener. */
555 SysBase->LibNode.lib_OpenCnt++;
556 return SysBase;
558 AROS_LIBFUNC_EXIT
561 AROS_LH0(BPTR, close,
562 struct ExecBase *, SysBase, 2, Exec)
564 AROS_LIBFUNC_INIT
566 /* I have one fewer opener. */
567 SysBase->LibNode.lib_OpenCnt--;
568 return 0;
569 AROS_LIBFUNC_EXIT
572 AROS_LH0I(int, null,
573 struct ExecBase *, SysBase, 4, Exec)
575 AROS_LIBFUNC_INIT
576 return 0;
577 AROS_LIBFUNC_EXIT
581 We temporarily redefine kprintf() so we use the real version in case
582 we have one of these two fn's called before AROSSupportBase is ready.
585 #undef kprintf
586 #undef rkprintf
587 #undef vkprintf
589 static int __kprintf(const UBYTE *fmt, ...)
591 va_list ap;
592 int result = 0;
594 va_start(ap,fmt);
595 result = AROS_SLIB_ENTRY(KrnBug, Kernel, 12)(fmt, ap, NULL);
596 va_end(ap);
598 return result;
601 static int __vkprintf(const UBYTE *fmt, va_list args)
603 return AROS_SLIB_ENTRY(KrnBug, Kernel, 12)(fmt, args, NULL);
606 static int __rkprintf(const STRPTR mainSystem, const STRPTR subSystem, int level, const UBYTE *fmt, ...)
608 va_list ap;
609 int result = 0;
611 va_start(ap,fmt);
612 result = AROS_SLIB_ENTRY(KrnBug, Kernel, 12)(fmt, ap, NULL);
613 va_end(ap);
615 return result;
618 struct Library * PrepareAROSSupportBase(struct ExecBase *SysBase)
620 struct AROSSupportBase *AROSSupportBase =
621 AllocMem(sizeof(struct AROSSupportBase), MEMF_CLEAR);
623 AROSSupportBase->kprintf = (void *)__kprintf;
624 AROSSupportBase->rkprintf = (void *)__rkprintf;
625 AROSSupportBase->vkprintf = (void *)__vkprintf;
627 return (struct Library *)AROSSupportBase;
630 void _aros_not_implemented(char *string) {}