2007-11-21
Linux virtual terminals and the X server
Linux virtual terminals have a simple conceptual model; you hit Alt-Fn and the kernel switches you to virtual terminal n. It even works with X, except that you have to remember to hit Ctrl-Alt-Fn when you're in X to switch out of it. What I suspect many people don't know is that in the case of X this simple conceptual model is a bit of a lie.
The issue is that text VTs and the X server VT are in different graphics modes, so when you switch back and forth something has to reprogram the graphics hardware into the right mode for the target VT. While you might think that this is done by the kernel, it is not; it is actually done by the X server, which is the only thing that knows how to do it.
(In the jargon, this is called 'modesetting'.)
There are both historical and engineering reasons that things are this way. Setting anything beyond text modes and some very basic graphics modes is chipset specific and often quite complex; since the X server had to deal with the complexity of the graphics card anyways, it was the natural place to stick yet more card-specific knowledge. In addition, many graphics cards can't be safely reprogrammed while something else is using the card, so to switch away from the X server's VT the X server has to stop anything else it might be doing to the card anyways.
(In general, PC class graphics hardware is not very good about letting multiple processes cleanly share the graphics hardware; it pretty much assumes that it is only going to be used by one thing at once.)
If your X server dies, it generally does not helpfully reprogram the graphics card back to text mode on the way down; instead the card is left in some unknown, random state. At least three things can happen if another X server is started:
- if you are really lucky, the new X server can reprogram the graphics card to a good mode for X, and then when it exits will program the card back to a good text mode.
- if you are less lucky, X works but when the X server exits it restores more or less the old broken state; your system works fine in X, but you can't switch to a readable text VT.
- if you are unlucky, the new X server can't even reprogram the card so that X works correctly. If you are really unlucky, the crash left the card in a state where trying to reprogram it will lock up your system.
(I've alluded to the situation before in passing.)
Technically all of this is not always true; there are some ways of running X so that it is in the same mode as the text virtual terminals, so no modesetting needs to be done and the kernel can just flip back and forth. However, these are rarely used on PC hardware because they're generally achingly slow.