How does a computer restart itself?

491

177

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?

Seth Carnegie

Posted 2011-06-08T19:04:08.707

Reputation: 3 999

I'm quite familiar with the low-level reboot process, but I'm having trouble trying to figure out exactly what you want and frame it for you. Can you be any more specific? – Brian Knoblauch – 2010-07-28T20:19:24.470

1@Brian if your looking for more detail I'd like to know what the operating system tells the bios that make it a reboot over a shutdown. – jer.salamon – 2010-07-28T20:59:48.407

8@jer.salamon: They're actually completely different paths. Shutting down invokes a ACPI routine which sends a signal to the ATX power supply which turns off the main voltage rails. – Ignacio Vazquez-Abrams – 2010-07-28T21:08:45.627

@Brian, I was really looking for what jer.salamon was asking.

@jer.salamon and @Ignacio Vazquez-Abrams thanks! I used to think that reboot is some smart way of telling the machine not to doze off while doing all the same things as shutdown. – Knight Samar – 2010-07-28T21:27:54.497

@Brian/Anyone do you know of documentation on this? – jer.salamon – 2010-07-28T22:16:41.407

@jer.salamon and all, a friend gave me this to refer http://www.howstuffworks.com/bios.htm/printable

– Knight Samar – 2010-07-30T19:17:08.673

Nice Whitepaper on this page...http://msdn.microsoft.com/en-us/windows/hardware/gg463386.aspx

– Moab – 2011-03-17T16:07:43.530

13This is controlled through the BIOS -- if memory serves, keywords of interest are APM and APIC – pst – 2011-06-08T19:05:48.363

119It never really turns "off" on a restart. – Moab – 2011-06-08T19:15:32.007

1Moab, if you expand your comment with a bit more detail and make it an answer, it might get accepted. Maybe mention the keyboard controller? – CarlF – 2011-06-08T19:24:42.793

10

There are no stupid questions. No, there maybe are -- but I think this one's actually very interesting. Featured as a question of the week candidate.

– slhck – 2011-06-08T20:01:19.360

3The processor just leaves what it's doing and runs through the start-up sequence which I suppose is in ROM. In the days pre 5VSB, computers could truly be Off, no voltage coming out any ports, and perhaps computers couldn't be woken up(like WOL)..but even then, a restart didn't turn them off. – barlop – 2011-06-08T20:25:34.107

4I used to telework on a remote server which was basically just a PC without screen or keyboard in the company's datacentre. My server was never normally turned off at all, but sometimes I'd reboot it from my end using WinXP Start->TurnOffComputer->Restart, to sort out instabilities caused by testing my own code. I'd often work at times of my own choosing, when the datacentre was unmanned. It was very tiresome what I accidentally selected Turn Off as the final option, and had to wait until two days later for an operator to go and press the on switch for me! – FumbleFingers – 2011-06-08T23:00:48.563

2@Fumblefingers thank you for horrifying me with an example of the intensely boring travail we will all be hearing with the new generation of oldies that are computer literate. Gone are the war stories and how far they had to walk to go to the toilet or bath. Now it's this. – barlop – 2011-06-09T03:12:59.273

@barlop: Too true! You young turks will have a much easier time of it! I really did start with paper tape though - punching 0's and 1's. But we were better off than the previous generation, who according to Dilbert's ole fart had to make do with the letter O :-) – FumbleFingers – 2011-06-09T03:20:08.893

@FumbleFingers I'm more scared of future oldies telling their old memories of Win98 or WinXP, you've given me a taste of what it might be like. I thought i'd never hear it, but you provided a window to the future. Also, your description of a rather current kind of situation, is written like a problem solved long ago with new technology. On second thoughts, it is solved, but was solved long ago. even 13 years ago I remember it being spoken of, of waking a computer up via modem. Not much has happened in 10 years for example. – barlop – 2011-06-09T09:23:20.657

In many devices there is a tiny computer that is always on, which switches the big one on and off as needed. – starblue – 2011-06-09T12:46:52.293

jcrawfordor's answer below is awesome, but I have to admit I'd love if there were a more layman's answer as well. – grautur – 2011-06-09T20:45:45.050

You should consider reading Charles Petzold's Code: The Hidden Language of Computers. He walks you through basic computer design and how some of this is handled. A very readable, low level compsci book.

– uSlackr – 2011-09-16T19:11:01.010

Answers

599

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.

jcrawfordor

Posted 2011-06-08T19:04:08.707

Reputation: 15 203

@jcrawfordor, Is a hard restart actually possible in theory? – Pacerier – 2015-06-05T05:19:14.933

3+1. Very informative & useful answer. Just one point - PS_ON is active low ie switched to 0V rather than +5VSB to turn the main outputs on. – MikeJ-UK – 2011-06-09T08:23:53.960

I have submitted you answer to hacker news. http://news.ycombinator.com/item?id=2637438 Thank you for this great explanation!

– Trufa – 2011-06-09T15:29:24.087

Would love an accompanying flowchart diagram :) Great answer! – Julius A – 2011-06-09T15:55:47.737

2For USB devices triggering a boot, some BIOSes have an option to wake the computer on a double-click or keypress. Also, some Mac keyboards have Power buttons (a tradition going all the way back to the original). – Stuart P. Bentley – 2011-06-09T16:41:52.640

1Great answer, I certainly learned a lot. One question remains, though: from the motherboard's point of view, what is the difference between S4 and G2 states? Saving RAM to disk is done by the OS, so S4 sounds just like "soft off" for the hardware. – EMP – 2011-06-10T01:17:41.003

1I believe S4 is functionally equivalent to G2, but the difference is stored. Some BIOS appear to do a "quick POST" when starting from S4 but a full POST when starting from G2. I don't think there's any reason for this, though, other than that people want S4 resume to be fast. – jcrawfordor – 2011-06-10T01:21:40.673

Very good answer! I do have a question and a comment, though: I have heard about "S5". According to Wikipedia, this is the same as G2; is this true? Also, there are some laptop docking stations that hook up via USB and that include a power button. – Daniel H – 2011-06-10T04:02:38.120

1

That's actually a bit simplistic; there are (many) non-ACPI ways to reboot the computer, and sometimes ACPI alone doesn't work: http://superuser.com/questions/294681/how-does-a-computer-restart-itself/294778#294778

– Tobu – 2011-06-15T23:11:20.580

It seems like it's actually quite a bit more complicated than this, due to buggy ACPI implementations: http://mjg59.dreamwidth.org/3561.html

– thekidder – 2011-06-27T18:09:18.723

81

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.

386EX datasheet figure 2

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.

AndrejaKo

Posted 2011-06-08T19:04:08.707

Reputation: 16 459

Wow, thorough answer. – Seth Carnegie – 2011-06-08T20:35:45.087

13@Seth Carnegie It's not that great. I just touched the low level stuff that's true for most computers, from the one in a refrigerator to PC. I didn't even mention the ACPI, APM and other modern power systems or how on old PCs Intel 8042 microcontroller which controller the keyboard could reset the processor, how the POST works, reset vectors and many other things. – AndrejaKo – 2011-06-08T20:47:00.663

1@netvope Too much to imagine, I'd say. You probably have at least one in your mouse, one in keyboard, more than 10 for the most basic parts of the motherboard (even if we take into account highly integrated "chipsets"), maybe one in the power supply unit, At least one in each drive (more likely 2-3), several in each expansion card... Monitors are serious business too and my printer now has more RAM than my first desktop computer. – AndrejaKo – 2011-12-30T03:03:44.827

4

Talking about memory, the caches of some new HDDs and CPUs are now bigger than the RAM of the first computer I've used. We can now run Windows 95 entirely in the L2 cache and keep the whole system partition in the drive cache!

– netvope – 2011-12-30T03:57:12.510

27

This blog post describes how Linux triggers a reboot.

Excerpt:

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.

jabiko

Posted 2011-06-08T19:04:08.707

Reputation: 279

15This is relevant, but the answer would be more useful if you put an excerpt of the other page's info here. – calvinf – 2011-06-08T23:58:20.423

1tl;dr there are many ways to reboot; some boards are broken badly enough that pretend to be windows, a heuristic that involves a few tries and sleeping a bit, is the only way. – Tobu – 2011-06-15T23:02:19.290

10

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.

Ignacio Vazquez-Abrams

Posted 2011-06-08T19:04:08.707

Reputation: 100 516

...and that location is specified in the code that is passed to the reboot system call ? – Knight Samar – 2010-07-28T21:25:13.253

1@Knight Samar: No; the I/O location is generally an architecture constant, which the kernel knows. You can see this e.g. in the Linux reboot code, such as arch/x86/kernel/reboot.c . – sleske – 2010-07-28T22:19:57.967

does anyone have a good rescource for further reading on that matter? – matthias krull – 2010-07-29T11:19:35.083

@mugen.kenichi and all, a friend gave me this to refer http://www.howstuffworks.com/bios.htm/printable

– Knight Samar – 2010-07-30T19:17:28.207

I suggest https://superuser.com/a/347115/38062 .

– JdeBP – 2017-10-29T07:49:31.087

10

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.

krubo

Posted 2011-06-08T19:04:08.707

Reputation: 481

3You also needed to write a specific value to 0040:0072 to determine whether it was a cold (0x0000) or warm boot (0x1234). – Synetech – 2011-08-02T05:36:22.493

9

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.

Marcel

Posted 2011-06-08T19:04:08.707

Reputation: 91

2This is correct, but does not really answer the question. – sleske – 2011-06-09T15:00:25.110

4Are there watchdogs that will actually restart entire computer? I know that they are sometimes used for various microcontrollers inside the computer, but is there one on a whole computer level? My personal experience leads me to say no, but some evidence for or against would be welcome. – AndrejaKo – 2011-06-09T19:03:15.517

5@AndrejaKo: I used to work for a company that made communication servers (essentially blade servers with modems connected to them). They had a watchdog timer that was connected to the reset pins on the motherboard. The timer was bumped periodically by a background utility running on the motherboard, so if the processor hung, the watchdog would eventually push the reset button. It was tied to a circuit that made the phone line busy while the motherboard rebooted. – Robert Harvey – 2011-06-09T23:51:40.623

4

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.

Daniel R Hicks

Posted 2011-06-08T19:04:08.707

Reputation: 5 783