Exit routine?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
St8ic

Exit routine?

Post by St8ic »

Can anyone give me the steps (or code? ;D) required to terminate a program and returning control to the kernel? How did you do it?

Thanks!
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Exit routine?

Post by Pype.Clicker »

well, that basically depend on what resources your program has opened, etc.
however, steps common to all multi-address-space implementations should be:
- wait until noone is no longer waiting for this process (for instance, defer the death of a process which has summonned an I/O operation that isn't completed)
- walk the list of threads and free any thread-specific resources you have (stack segments, TSS, whatever)
- walk the list of virtual memory areas and apply the proper memory_release() policy on the page frames mapped through these areas (i.e. you might wish to simply ignore the kernel-space pages, free some user-level frames and countdown the amount of references for the shared user-level frames ...)
- release any kernel-hold structures that were defining your process (pid table entries, file handlers, whatever ...)
St8ic

Re:Exit routine?

Post by St8ic »

I just need the code for terminating the program. My OS is the simplest one ever. No multi-tasking, no memory management (yet), no nothing. I tried the following, but it still doesn't work.

Code: Select all

mov ax,cx
push ax
xor ax,ax
push ax
iret
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Exit routine?

Post by Pype.Clicker »

the common technique is to call an interrupt (system call) to tell the kernel 'okay, my program is complete' ... from there, it'll be up to the kernel to clean up things.

Code: Select all

user_main:
       call do_stuff
       mov eax,return_code
       int END_OF_PROGRAM_INTERRUPT_NUMBER
the "end of program" interrupt handler should of course *not* return to the caller... instead, you could send a signal to the parent process (but you don't have signals nor process yet ;p )
or have a "chain" of programs, and return to the one who exec()uted the current program.

Code: Select all

struct job {
   struct job *parent;
   char *pname;
   int flags;
   cpu_context suspended; // include stack pointer and target address
   resources *res; // anything your program has allocated...
};
when you receive the 'end of program', just release resources if any, then resume the parent job (for instance by patching the return address and stack pointers from your current interrupt frame and then performing an IRET)

If you have no 'parent' job, you can simply enter a 'while(1) halt();' loop in end_of_program handler ...
St8ic

Re:Exit routine?

Post by St8ic »

I've been having troubles setting up an IDT (I was using Deviator as a model...maybe Darklife can help me port?), so is there any other way I could set up an exit routine?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Exit routine?

Post by Pype.Clicker »

before you have IDT set up and working, i don't think it's wise to launch user programs anyway ... if the program misbehave (and user programs eventually do), your kernel will be unable to handle the exception the CPU will fire and the system will reset ...
Post Reply