Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
asm 'hlt' question
#1
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.
o, you can't have a pony!
NOT YOURS!
http://www.b0g.org/wsnm/news.php?artc=17091
Reply
#2
Have you asked the question at #C/C++ on freenode? They might be able to help you better.
Reply
#3
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.
url=http://www.bbcbasic.org/]Visit www.bbcbasic.org for BBC BASIC[/url]
Reply
#4
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]
Reply
#5
Yeah, why exactly do you need to halt the processor? That'll force a cold reboot...
Reply
#6
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
Reply
#7
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.
o, you can't have a pony!
NOT YOURS!
http://www.b0g.org/wsnm/news.php?artc=17091
Reply
#8
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.
url=http://www.bbcbasic.org/]Visit www.bbcbasic.org for BBC BASIC[/url]
Reply
#9
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?
o, you can't have a pony!
NOT YOURS!
http://www.b0g.org/wsnm/news.php?artc=17091
Reply
#10
Update:

I have now created my kernel module, and it works Big Grin

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.
o, you can't have a pony!
NOT YOURS!
http://www.b0g.org/wsnm/news.php?artc=17091
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)