Page 1 of 1

Exit routine?

Posted: Sun Jan 25, 2004 11:04 pm
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!

Re:Exit routine?

Posted: Mon Jan 26, 2004 3:22 am
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 ...)

Re:Exit routine?

Posted: Mon Jan 26, 2004 5:57 pm
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

Re:Exit routine?

Posted: Tue Jan 27, 2004 3:58 am
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 ...

Re:Exit routine?

Posted: Sun Feb 01, 2004 11:44 am
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?

Re:Exit routine?

Posted: Sun Feb 01, 2004 3:23 pm
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 ...