Aside from any other problems with this code, I am not sure if this is really what you want for the
sleep() function anyway.
Why do I say this? because you generally aren't going to be sleeping in the kernel at all, and even if you did, you wouldn't want it to busy-wait - you would use
HLT to wait for the clock ticks, and count those. Even then, though, there are few if any situations where you would want to actually halt the kernel outright, if only because there are usually going to processes to run - whether kernel or user - while waiting for whatever you might try to sleep on.
Conversely, the
sleep() function for userspace processes is a system call out to the scheduler, which should move the sleeping thread to a sleep queue and reschedule whatever other processes might still be active (or exhausting those, to a null process which simply loops on
HLT). Since clock ticks should invoke the scheduler first and foremost (assuming preemption is being used), it is the scheduler's job to track the timing and move the awakened threads back into the running queue, not the function's. See
this earlier thread for my on one way to do this.
Either way, while this will work to sleep the kernel, it isn't what your going to want, at least not once you have a scheduler running. I suppose I can see this version being useful as a stepping stone towards that, though.