Windows magic. (paging and virtual 8086 mode) (Windows Workshop) (Column)
by Clifton Karnes
Last time, I talked about the 80x86 family of CPUs and how its architecture has made much of what Windows does possible. This month, I'm going to talk about how Windows exploits 386 enhanced mode. Specifically, I want to discuss how Windows multitasks DOS and Windows applications and how it uses virtual memory.
The 386 and its successors, the 486 and Pentium, have two features that Windows uses to great advantage: virtual 8086 mode and paging. In virtual 8086 mode, also called V86 mode, the CPU emulates several 8086 machines in real mode, and it can multitask these machines. In V86 mode, each DOS program thinks it's running in its own 8086-based PC. It's V86 mode that gives Windows the power to multitask DOS applications so well, and that's one of the things that's accounted for Windows' success.
The other important 386 feature--paging--takes the protected-mode addressing scheme we discussed last month a step further. As you may recall, in protected mode, segment addresses are supplied by selectors, instead of actual segment addresses themselves. Using paging, a selector can refer to an address that's in physical memory, or it can refer to a 4K page of memory that's been swapped to disk. Paging is the key to virtual memory, and, although it sounds complex, it usually works like a charm.
But how does Windows juggle all these elements and get them to work together? That question isn't so easy to answer, but here's the big picture. Before proceeding, though, I want to mention two excellent books that have helped me unravel some of Windows' most closely kept secrets. The first is Windows Internals by Matt Pietrek (Ad- dison-Wesley), which gives a byte-level look at how Windows does what it does. The other is Microsoft's 80386/ 80486 Programming Guide by Ross P. Nelson, which explores the 80386 chip's architecture in detail.
Back to our story. In enhanced mode, the real Windows operating system is something called the Virtual Machine Manager, or VMM for short. The VMM is a 32-bit, protected-mode, preemptive multitasking operating system. The VMM's sole job is to create, manage, and destroy virtual machines (VMs).
The first VM the VMM creates is the system VM, which runs all Windows applications. When you start a DOS session under Windows, the VMM creates a separate DOS VM, which runs in the 80386's V86 mode. For each additional DOS session, the VMM creates a new VM.
The VMM allocates CPU resources to each VM, and it's ironic that while the VMM multitasks preemptively, the system VM, which runs all the Windows apps, multitasks nonpreemptively. Windows, as you may know, employs what is euphemistically called cooperative multitasking. In cooperative multitasking, each application gets as much CPU time as it's willing to take. For a program to be a good citizen in Windows, it needs to take as little time as possible. In a preemptive multitasking system, on the other hand, the operating system decides how much time each task gets. At the highest level, the VMM decides how much time each VM gets, and because of this, all Windows programs, taken as a group, are multitasked preemptively. But individually, Windows programs have to work inside a single VM, and they have to work cooperatively. Obviously, preemptive multitasking is better than cooperative multitasking.
I want to discuss paging by beginning with how Windows manages memory in general. When a Windows program needs memory (to load itself, for example), it requests the memory from Windows. If Windows has a chunk of memory in its pool--called the global heap--large enough for the program, it gives the memory to the program. If it doesn't have a chunk of memory available, it rearranges the programs already in memory and consolidates the free space. This is something like defragmenting a hard disk, except that it takes place in memory. If that still can't free enough memory, Windows takes a 4K page of memory from a program that hasn't been run in a while and pages this memory to disk. This paging, which is also called swapping, turns your hard disk into a holding area for memory. As far as the application whose memory was paged to disk is concerned, its code and data are still in physical memory. Windows manages all this stuff behind the scenes, so the applications themselves never have to worry about it.
When you create a permanent swap file, you're telling Windows exactly how much disk space it can use for paging. This magic is possible because of the 386 chip's paging mechanism, which the 286 doesn't support, which is why virtual memory isn't available in standard (286) mode.
The best thing about V86 mode and paging is that you never have to think about them to benefit from what they do. If you're still running Windows in standard mode and you have a 386 or better CPU, turn on the light and switch to 386 enhanced mode. You'll be glad you did.