OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Dec 08, 2021 5:12 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Strange problem with scheduler task switching
PostPosted: Sun Oct 24, 2021 12:47 pm 
Offline
Member
Member

Joined: Tue Aug 31, 2021 7:25 am
Posts: 35
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!


Top
 Profile  
 
 Post subject: Re: Strange problem with scheduler task switching
PostPosted: Sun Oct 24, 2021 2:19 pm 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 188
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.


Top
 Profile  
 
 Post subject: Re: Strange problem with scheduler task switching
PostPosted: Sun Oct 24, 2021 5:38 pm 
Offline
Member
Member

Joined: Tue Aug 31, 2021 7:25 am
Posts: 35
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.


Top
 Profile  
 
 Post subject: Re: Strange problem with scheduler task switching
PostPosted: Sun Oct 24, 2021 7:44 pm 
Offline
Member
Member
User avatar

Joined: Wed Sep 28, 2005 11:00 pm
Posts: 82
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.


Top
 Profile  
 
 Post subject: Re: Strange problem with scheduler task switching
PostPosted: Sun Oct 24, 2021 11:53 pm 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 3500
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?


Top
 Profile  
 
 Post subject: Re: Strange problem with scheduler task switching
PostPosted: Wed Oct 27, 2021 10:42 am 
Offline
Member
Member

Joined: Tue Aug 31, 2021 7:25 am
Posts: 35
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.


Top
 Profile  
 
 Post subject: Re: Strange problem with scheduler task switching
PostPosted: Wed Oct 27, 2021 11:46 am 
Offline
Member
Member
User avatar

Joined: Wed Sep 28, 2005 11:00 pm
Posts: 82
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?


Top
 Profile  
 
 Post subject: Re: Strange problem with scheduler task switching
PostPosted: Wed Oct 27, 2021 12:06 pm 
Offline
Member
Member

Joined: Tue Aug 31, 2021 7:25 am
Posts: 35
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.


Top
 Profile  
 
 Post subject: Re: Strange problem with scheduler task switching
PostPosted: Wed Oct 27, 2021 1:13 pm 
Offline
Member
Member

Joined: Tue Aug 31, 2021 7:25 am
Posts: 35
I'm doing a little debugging of my own right now to figure out different issues which likely link to this issue.


Top
 Profile  
 
 Post subject: Re: Strange problem with scheduler task switching
PostPosted: Thu Oct 28, 2021 6:33 pm 
Offline
Member
Member

Joined: Tue Aug 31, 2021 7:25 am
Posts: 35
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 :lol:
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: klange and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group