Page 1 of 1
Strange problem with scheduler task switching
Posted: Sun Oct 24, 2021 12:47 pm
by YDeeps1
I have managed to put together a task scheduler which allows you to schedule tasks (processes for now) which will jump to that process code (all code is linked with the OS, no ring 3 yet) and save all registers upon a timer interrupt and run the scheduler code. The thing works well, however I am having problems trying to get the stack to work. At first I tried simply simply allocating around 2KB with my simple heap allocator and then upon the process being scheduled, move the top of the stack into the esp and jump to the process and once the process yields control through a timer, moves the dedicated kernel stack pointer back into esp and run some quick C++ code to figure out which process to jump to next (and yes, the code does release the stack frame, not like that matters though since the kernel esp would go back to the original each time). My problem is that after around 2 seconds, undefined behaviour happens (sometimes I get a general protection fault, sometimes nothing happens and sometimes the struct containing process data gets corrupted and filled with random data) which leads me to believe there is some strange overwriting and code execution going on.
I'm looking for advice on the best way (or if I'm doing something wrong) for implementing a process stack and hopefully fixing this issue because after many nights I ran out of ideas. If you need anymore information feel free to ask (I do not just want to throw code at people).
Thank you!
Re: Strange problem with scheduler task switching
Posted: Sun Oct 24, 2021 2:19 pm
by pvc
2 KiB is a very little storage for a stack, unless you're running very restricted test code or targeting some kind of small, embedded system. Too little IMO. It would be very easy to overflow it with any bigger object or recursive algorithm.
Re: Strange problem with scheduler task switching
Posted: Sun Oct 24, 2021 5:38 pm
by YDeeps1
pvc wrote:2 KiB is a very little storage for a stack, unless you're running very restricted test code or targeting some kind of small, embedded system. Too little IMO. It would be very easy to overflow it with any bigger object or recursive algorithm.
You might be right, but the problem occurs when the stack isn't even used by the program! I wrote a simple assembly function to just infinitely loop and all hell breaks loose after around 200 ticks from the PIT.
Re: Strange problem with scheduler task switching
Posted: Sun Oct 24, 2021 7:44 pm
by deadmutex
Are you in 64-bit mode? If so, then keep in mind that the stack is aligned to a 16-byte boundary before SS:RSP is pushed upon an interrupt.
Re: Strange problem with scheduler task switching
Posted: Sun Oct 24, 2021 11:53 pm
by Octocontrabass
YDeeps1 wrote:moves the dedicated kernel stack pointer back into esp
Hold on a minute. In the "one kernel stack per thread" design, there is no dedicated kernel stack. Interrupts and system calls are handled on the current task's ring 0 stack, and switching to a different task is switching to a different ring 0 stack. When you eventually get code running in ring 3, how many ring 0 stacks do you plan to have?
YDeeps1 wrote:(I do not just want to throw code at people)
Got a link to your code?
Re: Strange problem with scheduler task switching
Posted: Wed Oct 27, 2021 10:42 am
by YDeeps1
deadmutex wrote:Are you in 64-bit mode? If so, then keep in mind that the stack is aligned to a 16-byte boundary before SS:RSP is pushed upon an interrupt.
I'm in 32 bit mode.
Re: Strange problem with scheduler task switching
Posted: Wed Oct 27, 2021 11:46 am
by deadmutex
YDeeps1 wrote:pvc wrote:2 KiB is a very little storage for a stack, unless you're running very restricted test code or targeting some kind of small, embedded system. Too little IMO. It would be very easy to overflow it with any bigger object or recursive algorithm.
You might be right, but the problem occurs when the stack isn't even used by the program! I wrote a simple assembly function to just infinitely loop and all hell breaks loose after around 200 ticks from the PIT.
In 32-bit mode, the processor still pushes at least 12 bytes to the stack upon an interrupt (it may push up to 24 bytes upon an exception.) Are you using 'iret' when your timer handler finishes?
Re: Strange problem with scheduler task switching
Posted: Wed Oct 27, 2021 12:06 pm
by YDeeps1
deadmutex wrote:YDeeps1 wrote:pvc wrote:2 KiB is a very little storage for a stack, unless you're running very restricted test code or targeting some kind of small, embedded system. Too little IMO. It would be very easy to overflow it with any bigger object or recursive algorithm.
You might be right, but the problem occurs when the stack isn't even used by the program! I wrote a simple assembly function to just infinitely loop and all hell breaks loose after around 200 ticks from the PIT.
In 32-bit mode, the processor still pushes at least 12 bytes to the stack upon an interrupt (it may push up to 24 bytes upon an exception.) Are you using 'iret' when your timer handler finishes?
Yes! In fact I overwrite the return address and instead add the address for the main scheduler function, just so I don't have to do the far jumps myself.
Re: Strange problem with scheduler task switching
Posted: Wed Oct 27, 2021 1:13 pm
by YDeeps1
I'm doing a little debugging of my own right now to figure out different issues which likely link to this issue.
Re: Strange problem with scheduler task switching
Posted: Thu Oct 28, 2021 6:33 pm
by YDeeps1
Yeah I managed to fix it myself. I would explain how I got to the solution but I went through so many steps I can't remember
All I can say is the moral of the story is to not stay focused on one specific issue and explore to see if something else is causing that issue.