Posts: 5
Threads: 1
Joined: Mar 2005
Hi, I'm looking for an explanation (yes, I've tried googling and #asm on freenode) on the 'hlt' instruction...
I'm running linux, using nasm and ld to compile my asm programs.
What I want is basically a idle loop, something like this:
run:
hlt
jmp run
but adding 'hlt' to any of my asm programs results in an 'Segmentation fault' error during runtime.
Posts: 4,307
Threads: 63
Joined: May 2003
Have you asked the question at #C/C++ on freenode? They might be able to help you better.
Posts: 8
Threads: 0
Joined: Feb 2005
Quote:What I want is basically a idle loop, something like this:
run:
hlt
jmp run
Assuming you don't mind calling a library routine, you probably need to do something like this:
Code: run:
push 0
call _sleep
jmp run
The sleep(0) routine is the usual way of releasing the processor for other tasks.
Richard.
Posts: 320
Threads: 9
Joined: Dec 2004
If I remember correctly, HLT is a ring0 instruction - userland progs are not allowed to use it.
-shiftLynx
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Posts: 1,439
Threads: 15
Joined: Apr 2003
Yeah, why exactly do you need to halt the processor? That'll force a cold reboot...
Posts: 4,307
Threads: 63
Joined: May 2003
Quote:Yeah, why exactly do you need to halt the processor? That'll force a cold reboot...
No, it doesnt "reboot" the system. All it does is "suspend"/"turns off" the CPU until an interrupt wakes it up. How does this help? Well for one, it makes your CPU run cooler. Heres a link I found.
http://www.hardware-one.com/reviews.asp?aid=103&page=1
Posts: 5
Threads: 1
Joined: Mar 2005
Quote:nightwalker Wrote:What I want is basically a idle loop, something like this:
run:
hlt
jmp run
Assuming you don't mind calling a library routine, you probably need to do something like this:
Code: run:
push 0
call _sleep
jmp run
The sleep(0) routine is the usual way of releasing the processor for other tasks.
Richard.
Thanks for the reply guys. Software cooling is what I'm looking for, not just cooling the CPU when it's idling, but forcibly causing it to halt every now and then, even when under full load...
Using sleep(0) I get this error... As I said earlier, I'm running linux using nasm as compiler... This sleep(0) call, does it require any special library?
Code: hello.asm:13: error: symbol `_sleep' undefined
*edit*
And note, I'm not sure about library calls, because if I do that, then maybe the OS or thread handler will see my program as idle, and allow other processes/threads to execute instead. I really want to force a CPU halt, a halt which lasts until an interrupt (or something similar) occurs. If this can't be done in userspace, I'll consider the possibilty of creating a kernel module for it.
Posts: 8
Threads: 0
Joined: Feb 2005
Quote:And note, I'm not sure about library calls, because if I do that, then maybe the OS or thread handler will see my program as idle, and allow other processes/threads to execute instead. I really want to force a CPU halt, a halt which lasts until an interrupt (or something similar) occurs. If this can't be done in userspace, I'll consider the possibilty of creating a kernel module for it.
Ah, I misunderstood. I thought you wanted other processes to run while your program is idle - that's what the sleep(0) would have done.
Incidentally sleep() is declared in unistd.h so I think you'll need to link with the unistd library to use it from your assembler code.
Richard.
Posts: 5
Threads: 1
Joined: Mar 2005
Quote:If I remember correctly, HLT is a ring0 instruction - userland progs are not allowed to use it.
-shiftLynx
So you mean I have to write a kernel module to be able to do a simple hlt instruction?
Posts: 5
Threads: 1
Joined: Mar 2005
Update:
I have now created my kernel module, and it works
Thanks to all of you, and shiftLynx, for providing the clue about hlt not being userspace executable.
The kernel module (cpuidle is it's name) works by adding itself to the kernel's workqueue, and performs a certain number of hlt's every time the timer ticks. The module provides an easy way of control via a proc file. A high number of hlt's makes the system laggy and inresponsive, however, devices (such as networking), aren't affected, since they use interrupts, which breaks the resting CPU state.
I have tested this on an Intel Pentium 3, running linux 2.6.11. I know this is not a linux forum, but if anyone would like to test the module out for me, I'd be grateful.
|