views:

275

answers:

2

I have a simple bootloader, which initializes and prepares SDRAM. Then it loads an application from the Flash and starts it at some address in the RAM. After the application has finished its execution, the system does restart. There is no system stack.

Now, I would like this bootloader receives control back after an application finished its execution. The bootloader (let's call it OS) must also read an application's return code.

How can an application return a value to the calling OS and how the calling OS gets control back? I suppose, it can be done using interrupts - OS has a special resident function joined with some interrupt and every application just calls this interrupt at the end of its own execution. But how can a return code be read by OS if there is no system stack?

+2  A: 

One solution is for the program to write its exit code at a fixed, known location in memory - the "OS" can then read it.

anon
Damn, I was 27 seconds too slow :(
Andreas Bonini
+3  A: 

Normally you would leave a return code in one or more registers, but since you're in control, you can leave it wherever you like!

When an application is interrupted, the interrupt handling routine needs to save the application's state somewhere, which will probably mean copying from shadow registers to a predefined location in memory.

If an application surrenders control back to the OS (through a software interrupt / sytem call) then you need to define your own calling convention for which registers arguments are placed in, and the event handler needs to follow this before passing control back to the OS. You probably want to make the calling convention match up with that of your c compiler as much as possible, to keep things easy for yourself.

Autopulated