Page 1 of 1
How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 6:27 am
by BMW
ATM, at the end of my kernel is this:
How can I let the CPU rest for a bit each time it loops?
I have googled it/searched the wiki but found no answer.
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 6:33 am
by halofreak1990
Use the assembly hlt instruction:
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 6:35 am
by Griwes
`pause`, or, if you want to make it sleep till interrupt - `hlt`.
I call this another RTFM question.
@up: this will stop it for good (until NMI), not "a bit each time it loops".
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 6:44 am
by BMW
Griwes wrote:`pause`, or, if you want to make it sleep till interrupt - `hlt`.
I call this another RTFM question.
@up: this will stop it for good (until NMI), not "a bit each time it loops".
Thanks.
Sorry my googling skills can't be as good as yours.
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 7:11 am
by halofreak1990
Griwes wrote:@up: this will stop it for good (until NMI), not "a bit each time it loops".
Well, that's what the
that he showed does, so I posted the equivalent asm code which puts the CPU to sleep
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 7:13 am
by Griwes
No, that's not what `for (;;) ;` does; `for (;;) ;` is busy loop (jmp, jmp, jmp...), and `cli; hlt` is CPU sleep state until NMI (or machine check, heh). How do you know whether OP asked about this so he can use it in code that isn't just busy loop that does nothing?
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 7:17 am
by Brendan
Hi,
BMW wrote:Griwes wrote:I call this another RTFM question.
Sorry my googling skills can't be as good as yours.
A minor point - Googling is different to reading the CPU's manual. If you're planning to get anywhere in OS development, you should have at least read enough of the CPU's manual to become familiar with the features it supports.
Note: You don't need to memorise the entire manual because you can look up more detailed information after you've realised you need a certain feature. The important thing is knowing which features the CPU supports, so that you know when you want more details about that feature. For example; simply reading "Chapter 2, System Architecture Overview
" of Intel's System Programming Guide would've been enough for you to have known about the existence and purpose of the HLT instruction; and this chapter is just an overview rather than anything detailed.
Cheers,
Brendan
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 10:41 am
by JAAman
i agree -- highly recommend that you at least read through intel 3a -- maybe vol 1 also if you don't really understand the CPU (vol 1 is a basic overview of the CPU for application developers, and doesn't go into depth at all about system level features, but may be a good overview if you need it first)
and of course having the manuals (and knowing where in them to find information) will be very helpful as you progress along
if you don't have them, you can download them in PDF form at the link in my signature (assuming they haven't been moved since i was there last)
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 3:17 pm
by zeusk
Why do you want just the NMI to wake the cpu up, why not just hlt in a loop. This is what my kernel's idle loop looks like:
(Preempt is disabled/IRQs are enabled)
Code: Select all
for (;;)
{
ThreadYield();
BSPIdleEnter();
do {
IRQDisable();
if (ThreadHighestPriority() <= CONFIG_IDLE_PRIORITY)
BSPIdle();
IRQEnable();
} while(ThreadHighestPriority() <= CONFIG_IDLE_PRIORITY);
BSPIdleExit();
}
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 3:31 pm
by rdos
The null-thread of RDOS:
Of course, interrupts are enabled, otherwise it is killing the core for good.
OTOH, on SMP, it looks slightly different
Code: Select all
null_start:
GetCore ; return core data into fs
null_loop:
test fs:ps_flags,PS_FLAG_SHUTDOWN ; test if shutdown is requested
jz null_hlt
push OFFSET null_start
call SaveCurrentThread ; save thread state
call UnlockCore
lock and fs:ps_flags,NOT PS_FLAG_SHUTDOWN
mov fs:ps_curr_thread,0
ShutdownCore ; put core in low-power mode
null_hlt:
hlt
jmp null_loop
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 4:06 pm
by BMW
Brendan wrote:Hi,
BMW wrote:Griwes wrote:I call this another RTFM question.
Sorry my googling skills can't be as good as yours.
A minor point - Googling is different to reading the CPU's manual. If you're planning to get anywhere in OS development, you should have at least read enough of the CPU's manual to become familiar with the features it supports.
Note: You don't need to memorise the entire manual because you can look up more detailed information after you've realised you need a certain feature. The important thing is knowing which features the CPU supports, so that you know when you want more details about that feature. For example; simply reading "Chapter 2, System Architecture Overview
" of Intel's System Programming Guide would've been enough for you to have known about the existence and purpose of the HLT instruction; and this chapter is just an overview rather than anything detailed.
Cheers,
Brendan
Ok, cool, I better read some.
I knew about the HLT instruction, I just had a misunerstanding, I thought that HLT stopped the CPU for good, but now I know that an interrupt wakes a HLT'd CPU.
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 4:51 pm
by Jezze
I just want to add a minor thing. When you are about to halt the cpu you are most likely in an interrupt handler already. After noticing that now is a good time to halt the cpu, instead of calling cli hlt directly a nice way is to let the iret return to a function that just does hlt but before returning you could switch the IF flag in the EFLAGS register that was pushed onto the stack. I just think that is more "clean".
Re: How do you halt the CPU during idle time?
Posted: Wed Jan 23, 2013 5:10 pm
by rdos
Jezze wrote:I just want to add a minor thing. When you are about to halt the cpu you are most likely in an interrupt handler already. After noticing that now is a good time to halt the cpu, instead of calling cli hlt directly a nice way is to let the iret return to a function that just does hlt but before returning you could switch the IF flag in the EFLAGS register that was pushed onto the stack. I just think that is more "clean".
Not necessarily. It is possible to have a lowest priority null thread that the scheduler picks when nothing else is ready to run. This thread could then decide to enter a deeper sleep-state itself (like in my example).
The reason to use hlt instead of just a busy-loop is that on many (all?) CPUs hlt will use less power than busy-looping. Also, the hlt will be exited directly when an interrupt occurs, and some work might be available.