Page 1 of 1

Iret not in interrupt

Posted: Tue Oct 18, 2005 2:23 pm
by HardEnough
Can i use the iret instruction not in an interrupt, just to do context switching knowing that i push the correct parameters on the stack ?
If its correct to do so, can any one push me an example ;) ?

Re:Iret not in interrupt

Posted: Tue Oct 18, 2005 2:33 pm
by Cjmovie
I don't have an example, but...
You have to have some way of knowing when to start the next task. I'm not saying it MUST be an interrupt, but that it is the only way (that I know of...) to do task-switching (/thread switching/etc.).

For instance, the only other way I could think of is a cooperative multi-tasking system, where each process tells the kernel when it is ready to give up some time. But if, while coding a program, you forget to make (even 1!) call to the kernel to give up time....your kernel never gets control, and the whole system dies :).

Re:Iret not in interrupt

Posted: Tue Oct 18, 2005 2:40 pm
by bluecode
hi,

if you want to switch from cpl0 to cpl3 the stack should look like this (all 32Bit Data):
ss3 ;STACK-TOP
esp3
eflags
cs
eip ;STACK-BOTTOM
The esp before the task-switch must then point to eip. If you're not switching rings, then you just don't push ss and esp.

@Cjmovie: The initial task-switch is not interrupt triggered ;-)

Re:Iret not in interrupt

Posted: Tue Oct 18, 2005 2:59 pm
by HardEnough
So i don't need to use ints to do switching (from cpl0->cpl3) ?
what i wanted to do is:
not to force context switching when returning from interrupt, but create a function that do context switching when needed.

so if i'm in cpl0 and wanted to switch from process1(cpl3) to process2(cpl3), i can do so without being in isr, right ? just by using iret without being in isr.

Re:Iret not in interrupt

Posted: Tue Oct 18, 2005 3:19 pm
by bluecode
no, you can't use iret from cpl3. If you do so, the processor will raise a GPF (General Protection Fault). You just use it in the kernel. So your kernel initialization function does something like this:

Code: Select all

init_kernel();
/* Create Process address space and kernel stack from a multiboot module */
create_process(&multiboot_module);
/* Switch to the task */
do_task_switch(&multiboot_module);
When the processor is executing the process, it can only be interrupted by an interrupt (does this sentence make sense? 8) ). The Interrupt switches back to the kernel and the kernel then switches the address space and the kernel-level stack, does an iret and resumes another process.

Re:Iret not in interrupt

Posted: Tue Oct 18, 2005 3:21 pm
by proxy
i in fact use an iret as my kernel mode yield, here is is how it works:

Code: Select all

/*------------------------------------------------------------------------------
// Name: _kernel_yield
// Desc: this basically fakes an interrupt frame on the kernel stack
//       we trash eax in the process, but this is ok because c-calling 
//       convention allows eax to be trashed on function calls
//----------------------------------------------------------------------------*/
_kernel_yield:
   /* now has eip of one instruction after call _kernel_yield */
   pop %eax
   
   pushf         /* push eflags */
   push   %cs      /* push cs */
   push   %eax   /* push eip */
   push   $0x00   /* interrupt 0 */
   push   $0x00   /* 0 means software for us */
   
   /* we now look like a normal iret stack, lets continue just like a 
      normal int $0 */
   jmp _irq_call

Code: Select all

#define SAVE_ALL_REGS   \
   push %ds         ;\
   push %es         ;\
   push %fs         ;\
   push %gs         ;\
   pusha
   
#define RESTORE_ALL_REGS \
   popa            ;\
   pop %gs            ;\
   pop %fs            ;\
   pop %es            ;\
   pop %ds
   
   
#define SWITCH_KMODE_DATASEG\
   mov $0x10, %ebx         ;\
   mov %bx, %ds         ;\
   mov %bx, %es         ;\
   mov %bx, %fs         ;\
   mov %bx, %gs         ;\
      
/*------------------------------------------------------------------------------
// Name: _irq_call
//----------------------------------------------------------------------------*/
_irq_call:
   SAVE_ALL_REGS
   
   push %esp
   
   /* change to all kernel mode selectors */
   SWITCH_KMODE_DATASEG
   
   call irq_handler
   
   add $4, %esp
   mov %eax, %esp
   
   RESTORE_ALL_REGS
   
   /* compensate for error code and int number pushed earlier */
   add $8, %esp   
   iret

as you can see i build what looks like an interrupt 0 (except i set one minor detail so my task switcher knows not to do an eoi) and then just jump to my normal interrupt handler stub.

proxy

Re:Iret not in interrupt

Posted: Wed Oct 19, 2005 9:00 pm
by Crazed123
If you just want a way to context/thread switch without having to wait for a timer IRQ or the like, I really just recommend a software interrupt. You call it with a thread/process identifier, the hardware sets you up an interrupt frame, your software interrupt handler does the switch, irets, and you've switched threads. To me it seems simpler than trying to muck around with the stack manually, because once you have very basic things like exceptions and no-action IRQ handlers implemented the overriding likelihood is that you've already developed good code to talk to an interrupt frame, and using good code over again is a Good Thing.

Re:Iret not in interrupt

Posted: Wed Oct 19, 2005 9:38 pm
by proxy
true, but at the same time, i hardly find a pop, a few pushes and a jmp complicated at all. also, there could be hidden overhead in actually doing an int over doing the same thing manually. It really needs to be looked into to be sure.

proxy

Re:Iret not in interrupt

Posted: Thu Oct 20, 2005 9:08 am
by Crazed123
Looking at your code, why don't you just set up an interrupt stack frame on the stack, configure it to return to where you want and then do the iret without actually calling an IRQ handler?

Re:Iret not in interrupt

Posted: Thu Oct 20, 2005 1:11 pm
by proxy
because to do that, i would have to invoke the scheduler anyway like the normal irq 0 does and would involve much of the same work. Also, i wanted perfect symetry when task switch occurs whether it was a yield or an irq0, it is simpler to manage this way.

I could hypothetically drop the SWITCH_KMODE_DATASEG in the yield as I am already in kernel mode when doing this yield, but i find it more intuiative to just make it look like an irq0 just occured, it's a matter of preference i suppose.

proxy