How to release a stack with the context switch?

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
cmpxchg64
Posts: 12
Joined: Thu Dec 24, 2015 8:46 am
Libera.chat IRC: fi-matbah

How to release a stack with the context switch?

Post by cmpxchg64 »

I now found out why my OS corrupts the stack in SMP mode:
The task_save_state() function stores context and then clears the task->is_running flag, which allows the use of the stack after a context switch in a parallel task_schedule().
BUT: The scheduler/interrupt handler is still running on top of the old context, which results in the same stack being used by two tasks. (only ring0->ring0 CS'es for now)

What strategy do you use to avoid this problem?
I cannot clear the task->is_running flag until the scheduler has finished execution: An idea would be to construct an artificial second context which switches to some function like context_switch(task* prev_task, context* real_ctx) using a stack on top of the new stack, which would clear the flag and then continue to the context of the task selected by the scheduler.
Are there other, possibly more simple ways to handle this?

Cheers cmpxchg64
cmpxchg64
Posts: 12
Joined: Thu Dec 24, 2015 8:46 am
Libera.chat IRC: fi-matbah

Solution!

Post by cmpxchg64 »

After a litte thinking I now have found an even simpler solution, which should be pretty obvious: clear task->is_running and then perform the context switch without ever touching the stack again.
In practice this works by loading the esp of the new context into a register, then clearing the is_running flag and then doing the longjmp (which has to be implemented without a function call).
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: How to release a stack with the context switch?

Post by onlyonemac »

It is generally considered bad form to change the topic subject halfway through a thread. However, if you want to highlight the fact that you've solved your problem, you can edit the first post and change the subject by adding "[SOLVED] " in front - this is the standard way to mark a thread as solved, so that firstly you won't keep receiving replies from people who don't read the whole thread before replying and secondly others can find your solution more easily if they are having the same problem.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How to release a stack with the context switch?

Post by Combuster »

cmpxchg64 wrote:What strategy do you use to avoid this problem?
The scheduler has its own per-core stack. That way I also know exactly where a suspended process' registers are without having to know what the scheduler stack does.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply