If its correct to do so, can any one push me an example
Iret not in interrupt
-
HardEnough
Iret not in interrupt
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
?
If its correct to do so, can any one push me an example
-
Cjmovie
Re:Iret not in interrupt
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
.
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
-
bluecode
Re:Iret not in interrupt
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
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
-
HardEnough
Re:Iret not in interrupt
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.
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.
-
bluecode
Re:Iret not in interrupt
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:
When the processor is executing the process, it can only be interrupted by an interrupt (does this sentence make sense?
). 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.
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);
-
proxy
Re:Iret not in interrupt
i in fact use an iret as my kernel mode yield, here is is how it works:
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
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
-
Crazed123
Re:Iret not in interrupt
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.
-
proxy
Re:Iret not in interrupt
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
proxy
-
Crazed123
Re:Iret not in interrupt
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?
-
proxy
Re:Iret not in interrupt
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
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