How can a computer restart itself? After it's off, how does it tell itself to come back on again? What kind of software is it that can do this?
tl;dr: power states in your computer are controlled by an implementation of ACPI (advanced configuration and power interface). At the end of a shutdown process, your operating system sets an ACPI command indicating that the computer should reboot. In response, the motherboard resets all components using their respective reset commands or lines, and then follows the bootstrap process. The motherboard never actually turns off, it only resets various components and then behaves as if the power button has just been pressed.
Long and rambling but (in my opinion) more interesting answer:
Soft Power and how it works
In the olden days (well, okay, to a college student like me the '90s was a long time ago), we had AT (Advanced Technology) motherboards with AT power management. The AT power system was very, very simple. The power button on your computer was a hardware toggle (probably in the back of the case) and your 120vac input went right through it. It physically turned the power to your power supply on and off, and when this switch was in the Off position everything in your computer was completely dead (this made the CMOS battery very important, because without it there was no power supply to keep the hardware clock ticking). Because the power switch was a physical mechanism, there was no software way to turn power on and off. Windows would show the famous "It is now safe to turn off your computer" message because, although everything was parked and ready to turn off, it was not possible for the OS to actually flip the power switch. This configuration was sometimes referred to as hard power, because it's all hardware.
Nowadays things are different, because of the wonders of ATX motherboards and ATX power (that's Advanced Technology eXtended if you're keeping track). Along with a number of other advances (mini-DIN PS/2, anyone?), ATX brought soft power. Soft power means that power to the computer can be controlled by software. This brought a few import changes:
- Standby power: you may have seen a "5v SB" or "5v standby" connector labeled in power supply pinouts. The standby power supply is a 5v line to your motherboard that is always on, even when the computer is turned off. This is why it is important to unplug or turn off a PSU hard switch (if present) when servicing modern computers, because even when it's off you could potentially short the 5v SB and damage the motherboard. This is also why CMOS batteries aren't really as important anymore - the 5v SB is used to replace the CMOS battery whenever the power supply has mains power, so the CMOS battery is only used when you unplug the computer entirely. The 5v SB line importantly allows components of your computer (most importantly the BIOS and the network adapters) to keep running some simple software even when the computer is turned off.
- Intelligent power supply control. If you look at a pinout for your power supply's motherboard (P1) connector, you'll notice two pins typically labeled PS_ON and PS_RDY. These stand for "power supply on" and "power supply ready". If you like to experiment, take a power supply not in a computer, plug it in, and carefully short a ground line (one of the black wires) to the PS_ON line (the green wire). The power supply will visibly turn on, with the fan spinning up. The components of the motherboard running off of +5v SB actually turn your power supply on and off by connecting power to the PS_ON pin. Because there are some capacitors and other components in the power supply that take a moment to charge up, the voltages from the power supply's main outputs may not be stable immediately after the PSU turns on. This is what the PS_RDY pin is for, it comes on when the power supply's internal logic determines that the power supply is "ready" and will provide stable power. The motherboard waits until PS_RDY is on to continue booting.
So, your power switch no longer "turns on" the computer. Instead, it's connected to your motherboard's basic controllers, which detect that the button has been pressed and execute a number of steps to ready the system, including lighting up PS_ON so that power will be available. The power button isn't the only way to trigger the startup process, devices on your expansion bus can also do so. This is important because your ethernet network adapters actually stay on when your computer is off and look for a very specific packet often referred to as the "Magic packet." If they detect this packet addressed to their MAC address, they will trigger the startup process. This is how "Wake-on-LAN" (WoL) works. The clock can also initiate a boot (most BIOS allow you to set a time that the computer should boot each day), and USB and FireWire devices can trigger a boot, although I'm not aware of any implementation of this.
Understanding Power Control
Well, I explain the Soft Power thing both because I think it's interesting (always a key reason that I explain things) and because it allows you to understand how the power and running/off state of your computer are all controlled by software. In most current computers, this software system is an implementation of the Advanced Configuration and Power Interface, or ACPI. ACPI is a standardized, unified system allowing software to control your computer's power system. You may have heard of the ACPI power states. The basic mechanism of power control is these "power states", your operating system switches through power modes by preparing for the switch (the shutdown/hibernate processes that occur prior to power actually flipping off), and then commanding the motherboard to switch power states. The power states look like this:
- G0: Working (your computer's "on" state)
- G1: Sleeping (your computer's standby states, divided in to the S substates)
- S1: power to CPU and RAM remains on, but CPU is not executing instructions. Peripheral devices are powered off.
- S2: CPU powered off, RAM maintained
- S3: All components powered down except for RAM and devices that will trigger a resume (keyboard). When you tell your OS to "Sleep", it will stop processes and then enter this mode.
- S4: Hibernation. Absolutely everything is turned off. When you tell your operating system to Hibernate, it stops processes, saves the contents of RAM to disk, and then enters this mode.
- G2: Soft Off. this is your computer's "off" state. Power is off to everything except for devices that can trigger a boot.
- G3: Mechanical off.
How reset actually happens
You'll notice that reboot is not one of these states. So what actually happens when your computer when it reboots? The answer may be surprising, because from a power management perspective it's almost nothing. There is an ACPI reset command. When you tell your operating system to reboot, it follows its normal shutdown process (stops all your processes, performs a bit of maintenance, dismounts your file systems, etc), and then as a final step, instead of sending the machine to power state G2 (as it would if you had simply told it to Shut Down) it sets the Reset command. This is generally referred to as the "Reset register", because like most of the ACPI interface it's just an address that a specific value should be written to in order to request a reset. I'll quote the 2.0 specification on what it does:
The optional ACPI reset mechanism specifies a standard mechanism that provides a complete system reset. When implemented, this mechanism must reset the entire system. This includes processors, core logic, all buses, and all peripherals. From an OSPM perspective, asserting the reset mechanism is the logical equivalent to power cycling the machine. Upon gaining control after a reset, OSPM will perform actions in like manner to a cold boot.
So, when the reset register is set, a few things happen in sequence.
- All logic is reset. This means sending the respective reset commands to various bits of hardware including the CPU, memory controller, peripheral controllers, etc. In most cases this simply means lighting up a physical RST wire, as AndrejaKo showed up above.
- The computer is then bootstrapped. This is the "perform actions in like manner to a cold boot" part. The motherboard performs the same steps as it would if the power supply had just become ready after the power button being pressed.
The end effect of these two steps (which actually break down in to a lot more steps) is that it looks to everything just like the computer just booted, but the power was actually on the whole time. This means less time required to shut down and start up (since you don't have to wait for the power supply to become ready), and importantly allows the bootup to be initiated by the operating system shutting down. This means that another startup trigger doesn't need to be used (WoL etc), and allows you to use Reboot as an effective way to reset the system remotely, when you don't have a way to trigger boot.
That was a long answer. But hey, hopefully you know more about computer power management now. I certainly learned some things researching this.
Here's a starting point:
Chips usually don't turn off and then turn on. Instead, there's a reset line which will bring processor in so-called reset state when usually all memory is cleared and processor looks like it's just been powered on. While that pin is held high (or low, depending on the processor), the processor is in reset. Once the pin is released, it will continue booting normally, just as if it was turned on for the first time. The point of this is that there's no power cut itself.
So how does this scale to larger systems like modern PCs? Well modern computers are made out of computers which are sometimes made out of computers themselves. So when you set computer to reset, "computers" which make the computer will start saving their states (if the reset is controlled), or just have their reset pins pulled.
Some processors and microcontrollers (which are miniature self-contained computers, usually 20 years behind the modern desktop computers) can reset themselves using internal switches. As I said, once the signal which generates reset is gone, the computer will start up. So the premise in the question isn't completely right. Computer doesn't know when to turn on. In know when it needs to be "off" or in reset and when the signal which keeps it there is gone, it will turn on.
This behavior may look strange on modern computer which can be set to turn on at specific time or over network and so on. As I said computers are made of computers. So while the main processor may be off, there could be numerous other chips and microcontrollers inside which are on. The most obvious case is the real-time clock which is often battery powered. It can then turn on other chips which will turn on other chips and the chain reaction goes until the whole computer is on. On today's computers, there's a PSU line called +5 VDC Standby Voltage. It provides around 50 mW power to various devices that are on when computer is "off".
A little bit of trivia: Reset pin on Intel 386 EX processor is pin number 110.
On Intel i7-900 it's land number AL39.
I hope that someone will be able to provide an answer which will explain how things work from the high level point of view, since it's quite complicated system.
This blog post describes how Linux triggers a reboot.
Linux has a bunch of different ways to reset an x86. Some of them are 32-bit only and so I'm just going to ignore them because honestly just what are you doing with your life. Also, they're horrible. So, that leaves us with five of them.
kbd - reboot via the keyboard controller. The original IBM PC had the CPU reset line tied to the keyboard controller. Writing the appropriate magic value pulses the line and the machine resets. This is all very straightforward, except for the fact that modern machines don't have keyboard controllers (they're actually part of the embedded controller) and even more modern machines don't even pretend to have a keyboard controller. Now, embedded controllers run software. And, as we all know, software is dreadful. But, worse, the software on the embedded controller has been written by BIOS authors. So clearly any pretence that this ever works is some kind of elaborate fiction. Some machines are very picky about hardware being in the exact state that Windows would program. Some machines work 9 times out of 10 and then lock up due to some odd timing issue. And others simply don't work at all. Hurrah!
triple - attempt to generate a triple fault. This is done by loading an empty interrupt descriptor table and then calling int(3). The interrupt fails (there's no IDT), the fault handler fails (there's no IDT) and the CPU enters a condition which should, in theory, then trigger a reset. Except there doesn't seem to be a requirement that this happen and it just doesn't work on a bunch of machines.
pci - not actually pci. Traditional PCI config space access is achieved by writing a 32 bit value to io port 0xcf8 to identify the bus, device, function and config register. Port 0xcfc then contains the register in question. But if you write the appropriate pair of magic values to 0xcf9, the machine will reboot. Spectacular! And not standardised in any way (certainly not part of the PCI spec), so different chipsets may have different requirements. Booo.
efi - EFI runtime services provide an entry point to reboot the machine. It usually even works! As long as EFI runtime services are working at all, which may be a stretch.
acpi - Recent versions of the ACPI spec let you provide an address (typically memory or system IO space) and a value to write there. The idea is that writing the value to the address resets the system. It turns out that doing so often fails. It's also impossible to represent the PCI reboot method via ACPI, because the PCI reboot method requires a pair of values and ACPI only gives you one.
It frobs an I/O location which pulls a data line low which tells the CPU that it should stop whatever it's doing and start running code from a certain location in the BIOS.
Back in the old days before power management, computers could still restart themselves, of course. (Anybody remember when a frozen program meant you had to use Ctrl+Alt+Delete to reboot the computer?)
On my old 486, the assembly language command
JMP FFFF:0000 (ie, set the CPU's Instruction Pointer to said address) would cause the whole computer to restart. In other words, FFFF:0000 addresses a location in BIOS that has instructions for what the computer should do when it first starts. I suspect the Reset Pin described by AndrejaKo's answer, or reset buttons in the pre-power-management days, would also force the Instruction Pointer to the same address.
A Google search for JMP FFFF:0000 reveals many interesting pages about this.
There is also a thing named the watchdog. This device serves as a dead-man's switch. The computer has to signal to the watchdog every say minute that it is still alive. When the computer crashes, e.g. by running into an endless loop, it will fail to signal to the watchdog it is still running as intended, on which occasion the watchdog will perform a hardware reset. This was demonstrated in the popular tv series of the terminator, where a robot was knocked out by a high voltage power surge. it would reset itself in 2 minutes.
Back on the old IBM PC-1 the keyboard controller, oddly enough, handled reboot. IBM embedded a small microprocessor to handle the keyboard, and it had some spare I/O lines, so they used one of the lines to drive the reset line of the main CPU. A command sent to the keyboard controller would result in the CPU being reset as if power had just been switched on.
I'm guessing this tradition continued well into the "AT" era, and there may be vestiges of it remaining in ACPI today.
Added: There's an interesting detail about the above reset scheme. During the early boot sequence the code looked for a particular pattern in RAM that might have been set by the previously executing code. If this code was present some of the POST (power-on self-test) diagnostics were skipped. The pattern would only be present on a "warm" boot.