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.
`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".
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
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?
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
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)
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
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.
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".
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.