tags:

views:

81

answers:

2

I'm writing a small bootloader for an x86 based PC. The problem is that the CPU is somehow still active after executing these instructions:

sti
hlt

sti is supposed to disable interrupts for the next instruction
hlt is supposed to completely halt the processor

Since they're used together, I assumed they would just 'freeze' the computer. But when I assemble it and mount it in VirtualBox as a floppy image, my CPU jumps to 100%.

What am I doing wrong?

+4  A: 

I think you are a bit confused about both of these commands.

The sti command enables interrupts and the cli command disables them.

The halt state is similar to an idle loop, so it doesn't suspend the processor.

Here are some links that may help you: Info on the STI/CLI commands: http://en.wikipedia.org/wiki/STI_%28x86_instruction%29

Info on the x86 instructions: http://en.wikipedia.org/wiki/X86_instruction_listings From here there is a link to the hlt command that may help.

If you suspend the processor, how do you wake it up again, if you disable the interrupts?

James Black
That's the point - the computer is ready to be turned off at that point.
George Edison
Perfect. I replaced the STI instruction with the CLI one and it works perfectly now!
George Edison
+2  A: 

I would like to add a comment about 'cli' as I have been bitten by this a couple of times in the past. The 'cli' instruction does not block all interrupts--it only blocks the maskable ones. Conceivably, the system could still be woken up due to a non-maskable interrupt (NMI).

As one of the comments indicates that the computer is ready to be turned off, I expect that there are no other threads/processes/tasks ready to run in the system (otherwise an NMI could conceivably lead to a reschedule). For the scenario you describe an NMI is unlikely; however depending upon your level of paranoia of things going wrong, you may wish to add a loop to guard against the possibility.

sysSuspend:
    cli
    hlt
    jmp sysSuspend
Sparky
That's a good point. Thanks.
George Edison