Race conditions while writing a scheduler

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
User avatar
zevvi
Posts: 22
Joined: Wed May 28, 2025 8:20 am
GitHub: https://sr.ht/~zevvi

Race conditions while writing a scheduler

Post by zevvi »

I am trying to write a scheduler but every time I run it the error changes (it switches between 3-4 different errors) and each small change (printf, moving code, etc.) changes the result completely.
It seems to be race conditions, as adding a delay (e.g. a busy-loop) shifts which error appears.

The errors include:
- General protection faults (always with eip=0x0, suggesting a null/corrupt function pointer)
- Page faults (sometimes at the same address, sometimes complete garbage)
- Divide by zero
- Random serial output and VGA going completely black

The source: git.sr.ht/~zevvi/aoposii, the code is mainly in kernel/scheduler.c
nullplan
Member
Member
Posts: 2036
Joined: Wed Aug 30, 2017 8:24 am

Re: Race conditions while writing a scheduler

Post by nullplan »

When I hear issues like yours in an OS kernel, I check the interrupt code first. In your case, the interrupt handlers are not differentiating between exceptions with error code and those without. In exceptions with error code, you need to remove the error code from stack before running iret.

As a side note, the GPF handler looks for EIP in the wrong place. You are just reading a random stack value that is definitely never the EIP you actually want. You pass the registers to some of those handler functions, so that is what you should do with the GPF handler as well, and then read out the EIP from the interrupt frame (for that, you need to enhance your registers struct first).
Carpe diem!
Post Reply