Reminders about things that make an operating system possible. Extra flags SYS: running in system mode if set, user mode if not. VM: clear this flag to turn off virtual memory translation if SYS is not set, certain things don't work: it is impossible to change the SYS or VM flags trying to access certain registers (such as VM base and limit) causes an "error" executing privileged instructions (such as HALT) causes an "error" all enforced by CPU hardware, usually in the instruction decoder. When system starts up, SYS is set. SYS can be turned off when everything is ready to go, but then nobody will be able to deliberately turn it on again. What are those "errors"? Interrupts, just like segmentation fault, keyboard keypress, countdown timer. What is an interrupt? Hardware signal directly to CPU. When signalled: automatic switch to SYS mode save PC so interrupted program is can be continued later. in fact, save all registers and flags, so interrupted program can be continued as though nothing had happened. automatic jump to special address: take address stored in Interrupt Vector Base Register add to it number indicating which interrupt occurred set PC = contents of that memory location (interrupt vector is an array containing the addresses of the specuial functions for handling interrupts) Interrupt Vector Base Register can only be changed in SYS mode, so it doesn't provide a way for users to hijack the system. Special return-from-interrupt instruction to reverse all of those actions when interrupt processing is complete. Where do all the registers get saved? Interrupt might have been a segmentation fault caused by the stack being full. Trying to save them on the stack would be a disaster. Back to VM hardware. Instead of two pairs of base and limit registers (one for addresses beginning with 1 (stack) and one for addresses beginning with 0 (statics)), have four pairs. This creates four segments of the virtual address space, depending on first two bits of any virtual address: 11 System Stack - special small area used for the stack when in SYS mode. Correct design of OS ensures this never gets over-filled 10 System Static - where the OS code and global data structures are stored 01 User Stack - just as in the old model. All user programs are now started with SP = 0x7FFFFFFF instead of 0xFFFFFFFF. 00 User Static - just as in the old model. Any attempt to access memory addresses that begin with 1 when not in SYS mode automatically produce segmentation fault interrupt, so all OS data is protected but instantly available when an interrupt causes mode change. Make sure interrupt vector is stored in the system area. The only way to turn SYS mode back on is to cause an interrupt, and then the protected OS code takes over immediately and can decide what to do. But when an interrupt occurs, the stack pointer SP will be useless, it will contain an address in the User Stack segment. So there are always 2 different SP registers, the System Stack Pointer and the User Stack Pointer, automatically selected by hardware according to the SYS flag. OS now has its own stack, which is switched to instantly when an interrupt occurs. So now saving all the registers on the stack during an interrupt does make sense. Multiprocessing. There are some number of user programs ready to run. Each has its own allocation of memory for User Stack and User Static, and therefore its own values to store in two of the pairs of VM base and limit registers. Suppose one of them is running now, somehow. Sensibly, the OS put a reasonable value in the countdown interrupt timer before letting it start. Eventually that timer ticks away and causes a timer interrupt. All the program's register and flag values go onto the system stack and the OS timer handler is called. The OS copies all of that "volatile state" plus the values from the VM translation registers into a special area in the System Statics segment that it set aside for that one program. A PCB, Process Control Block. Then it chooses the PCB for a different program, and overwrites the values on the system stack with the values from this new PCB. It also reloads the VM registers with the values from this PCB. Reset the countdown timer, and do a simple return-from-interrupt instruction. It now appears to the new program as though it had just returned from an unusually long interrupt, and it is back to running as though nothing had happened. Except of course that it has no way of telling that this has happened at all. This is how timesharing works. How to start a new program (process). An existing running program "asks" the OS to start a sub-process. OS creates a new PCB, allocates enough physical memory for the new programs stack and statics, copies program from disc into that static area. Then put suitable initial values for registers (such as SP=0x7FFFFFFF, PC=wherever it stored the program code) into the new PCB, along with VM base and limit values correct for the allocated memory. Then nothing, just return from interrupt and let the requesting program (parent process) continue. Eventually the timer will expire, eventually the new PCB will be selected to run in the perfectly usual way. It will seem to have been running all along. How to stop a process (exit() in unix). Set a flag marking the process for death, and reduce the countdown timer to zero. Normal timer interrupt occurs immediately. Instead of saving the precess' volatile state in its PCB as usual, OS just abandons it. Erase the PCB, take back the memory used for it, and select another PCB to be reloaded in the normal way. How does a running program "ask" the OS to do something? The IBM BIOS use of an intel INT instruction is completely lame.