Page 1 of 1

[Solved] Yield function with Trap Gate

Posted: Thu Jan 10, 2019 8:18 pm
by gardinirafael
Hi guys
I have a question about yield function that I need to create. My kernel has support to x86 and Arm Cortex M3 and both arch uses the same function to choose witch task needs to run.

In x86 the PIT calls the function that save, choose and restore context and the Arm doing the same using Systick and PendSV interrupts.

In the arm case make Yield functions is easy, I need fill a register with value and processor calls PendSV that save, choose etc...

So, my question is, can I make a trap gate with function that PIT calls and In my yield function just call int 0x79 or any of interruprs avaliable? like syscall

Is a good decision? or bad design?

I thought in this way because is very simple to implement.

Thanks.

Re: Yield function with Trap Gate

Posted: Thu Jan 10, 2019 9:47 pm
by kzinti
Wouldn't it make sense to just have the PIC interrupt handler call your thread_yield() function?

For example:

Code: Select all

static void pic_interrupt_handler()
{
    pic_eoi();         // end-of-interrupt
    thread_yield();    // call scheduler to yield the current thread
}

Re: Yield function with Trap Gate

Posted: Thu Jan 10, 2019 10:45 pm
by gardinirafael
kzinti wrote:Wouldn't it make sense to just have the PIC interrupt handler call your thread_yield() function?

For example:

Code: Select all

static void pic_interrupt_handler()
{
    pic_eoi();         // end-of-interrupt
    thread_yield();    // call scheduler to yield the current thread
}
Yes, I tried but for something reason my Yield function doesn't work without EOI, and eoi only be sent on IRQ scope right?
So I thought to use the same interrupt structure with trap gate thinking in reuse code but my function below probably wrong.

Your tip will be perfect if my function below works without EOI.

Code: Select all

.extern gos_task_schedule
.global gos_port_yield
gos_port_yield:
    cli
    push $0
    push $0
    pusha
    push %ds
    push %es
    push %fs
    push %gs
    movw $0x10, %ax
    movw %ax, %ds
    movw %ax, %es
    movw %ax, %fs
    movw %ax, %gs
    movl %esp, %eax
    push %eax
    call gos_task_schedule
	movl %eax, %esp
    /* ----         ----
	movb $0x20, %al
	out %al, $0x20
    */
    pop %gs
    pop %fs
    pop %es
    pop %ds
    popa
    addl $8, %esp
    sti
    retl

Re: Yield function with Trap Gate

Posted: Fri Jan 11, 2019 4:34 am
by Octocontrabass
gardinirafael wrote:Yes, I tried but for something reason my Yield function doesn't work without EOI, and eoi only be sent on IRQ scope right?\
Are you calling Yield in an IRQ handler before you do EOI? If so, that's why it's not working. IRQ handlers must send EOI before they yield.

Re: Yield function with Trap Gate

Posted: Fri Jan 11, 2019 7:14 am
by gardinirafael
Octocontrabass wrote:
gardinirafael wrote:Yes, I tried but for something reason my Yield function doesn't work without EOI, and eoi only be sent on IRQ scope right?\
Are you calling Yield in an IRQ handler before you do EOI? If so, that's why it's not working. IRQ handlers must send EOI before they yield.
Unfortunately, I'm not. I saw the kzinti code above and I didn't realize that I changed the order of calls. My EOI was being called after yield function, not before like both of you said.

So, now the function works. Thank you kzinti and Octocontrabass. =D> :D