How do you halt the CPU during idle time?

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
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

How do you halt the CPU during idle time?

Post by BMW »

ATM, at the end of my kernel is this:

Code: Select all

for(;;);
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.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
halofreak1990
Member
Member
Posts: 41
Joined: Thu Aug 09, 2012 5:10 am

Re: How do you halt the CPU during idle time?

Post by halofreak1990 »

Use the assembly hlt instruction:

Code: Select all

__asm cli
__asm hlt
<PixelToast> but i cant mouse

Porting is good if you want to port, not if you want maximum quality. -- sortie
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: How do you halt the CPU during idle time?

Post 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".
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
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: How do you halt the CPU during idle time?

Post 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.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
halofreak1990
Member
Member
Posts: 41
Joined: Thu Aug 09, 2012 5:10 am

Re: How do you halt the CPU during idle time?

Post 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

Code: Select all

for(;;);
that he showed does, so I posted the equivalent asm code which puts the CPU to sleep
<PixelToast> but i cant mouse

Porting is good if you want to port, not if you want maximum quality. -- sortie
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: How do you halt the CPU during idle time?

Post 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?
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
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How do you halt the CPU during idle time?

Post 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
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.
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: How do you halt the CPU during idle time?

Post 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)
zeusk
Posts: 16
Joined: Fri Sep 14, 2012 1:09 pm

Re: How do you halt the CPU during idle time?

Post 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();
	}
rdos
Member
Member
Posts: 3306
Joined: Wed Oct 01, 2008 1:55 pm

Re: How do you halt the CPU during idle time?

Post by rdos »

The null-thread of RDOS:

Code: Select all


null_loop:
    hlt
    jmp null_loop

Of course, interrupts are enabled, otherwise it is killing the core for good.

OTOH, on SMP, it looks slightly different :mrgreen:

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

User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: How do you halt the CPU during idle time?

Post 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.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: How do you halt the CPU during idle time?

Post 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".
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
rdos
Member
Member
Posts: 3306
Joined: Wed Oct 01, 2008 1:55 pm

Re: How do you halt the CPU during idle time?

Post 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.
Post Reply