Stack me up

Posted by ALB42 on 13. Januar 2013No Comments

When I started to bring FreePascal to AROS I had no idea how this should be especially the startup code was something like magic. The startup codes are mostly in Assembler, I used much i386 and x64 Assembler, even machine code already (created an compiler to machine code). But for this you have to know about the inner system of aros and freepascal and additionally this assembler uses a rather strange syntax AT&T, ok when look right to it the intel syntax is strange… but I’m more used to it.

So I just looked into the different i386 implementations for linux, ppc for amiga and tried to guess how it should look like, in principle it was just a lucky shot. And I was rather surprise that the startup worked, just the finish was nearly impossible, for me this was more or less obvious because I do not know what AROS expect on the stack after the program.

The simple approach was DosExit, a call in dos.library which ends the process directly, but because its such a hard end it leaves some memory leaks open, especially the dos.library stays open (of course it must be open to call this DosExit). The conseqeuence was when calling very often after a while you get some strange effects like „out of memory“ even there are some hundred megabytes free.

Finally I got disturbed by it and try to find a different solution.

First I tried what happen when I just leave the program directly again, seems everything works nicely.. so it seems Aros expect the same stack as started, so more or less its just a CDECL call of „start“ with CommandLine, CommandLineLength and Execbase as arguments. As in the copied startup code of linux I saved the stack pointer called my program restored the stack pointer. Sadly this did not work, on „ret“ it looked like it jumped back into my fpc program and not back to  aros system., so for me it looked like the stack is not at right position. I compared the stack entry by entry, its exactly the saem before and after my routine, so this was not my problem. Of course I didn’t noticed this directly, this would be too easy. First I tried 3 days to debug this situation why my stack could be wrong or destroyed or whatever and thats all without a real debugger and without output (writeln does not work at this position of course). In germany we say  „I was barking at the wrong tree.“

Ok back to problem, I noticed that the stack is completely the same, so what else could it be, of course the other registers. So I added pushad/popad (in AT&T syntax its pushal/popal) and its working I was really surprised about this. It seems that the starting of a program in AROS is really just like call a subroutine… which was not obvious for me because I guessed you get a new process so also get a clean set of registers.

I’m not really sure if I saved now enough… how about SSE registers? FPU registers? until now fpu seems always be empty, sse register difficult to say. Much more interresting will be for x64 if one day I will also bring fpc to there (its my wish) there is no pushal/popal and much registers.

Nevertheless its working now, included in the next release.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert