Page 1 of 1

Race conditions while writing a scheduler

Posted: Fri Apr 10, 2026 11:46 am
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

Re: Race conditions while writing a scheduler

Posted: Fri Apr 10, 2026 5:17 pm
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).