BY JIM DUN ION
Full control of the 68000 chip requires knowledge of supervisor mode. Jim Dunion of debugger fame explains what it is, how to use it, and why it is important for ST programmers.
"I'm in charge here:"
- General Alexander Haig's statement to the press after President Reagan was shot by John Hinckley, Jr.
Exceptional circumstances call for exceptional actions by exceptional people, right? Al Haig certainly thought so, and so did the Motorola engineers who designed the 68000 family of microprocessors. When they built the chip, they included something called the "supervisor mode." To give you an idea of its importance, I quote from the first sentence of Motorola's Programmer's Reference Manual for the 68000:
"The M68000 executes instructions in one of two modes - user mode or supervisor mode."
Whether you know it or not, you're already familiar with user mode. That's what you're in when you run programs on the ST But what is supervisor mode, and how does it differ from user mode? More importantly: What good is it? Let's take a closer look.
WHAT IS SUPERVISOR MODE?
Think of the 68000 in your ST as a commercial airliner on which you can fly either coach or first class. Both classes share a lot of the airplane-wings, engines, etc.,- but passengers flying first class have access to certain privileges that the peasants in coach don't t. Now think about the registers in the 68000. Both user mode and supervisor mode have the
and how does it differ from
user mode? More importantly:
What good is it?
same eight data registers (D0-D7), the same seven address registers (A0-A6), the same program counter and the same condition code register. In user mode the programmer also has an address register A7 that is used as a user stack pointer. This is kind of like bathrooms on an aircraft. I mean, you wouldn't want first class passengers to have to use the same bathroom as coach passengers would you? Of course not. So, in supervisor mode there is a special supervisor stack pointer and a supervisor status register. Since the supervisor stack pointer is also called A7, it gets a little confusing. How will the processor know whether an instruction refers to the user stack pointer A7, or the supervisor stack pointer A7? Easy, bit 13 of the system (supervisor) status register-which is called the S bit-signals the processor. When the S bit is a 1, the processor is in supervisor mode and A7 refers to the supervisor stack pointer. When it is a 0, you're back in user mode and A7 is again the lowly user stack pointer. So far this seems simple enough, right? Two different stack pointers, an extra status register and a supervisor-mode bit. So what extra privileges do supervisor-mode passengers get that user mode passengers don't?
Karl Marx would have disliked the 68000. It seems to embody a lot of the struggle between the proletariat and the bourgeoisie. The privileged class wants to keep those privileges to itself and keep the underprivileged decidedly so. So in the 68000 world there is a simple rule: If you're in supervisor mode you can execute certain instructions that you can't execute while in user mode. Not surprisingly these are known as privileged instructions. It also shouldn't come as a shock that most of the privileged instructions are ones that would let a lowly user suddenly elevate himself to supervisor status. The privileged instructions are:
AND immediate to Status RegisterAs you may notice, most of these instructions operate on the status register and could therefore affect the S bit. The others could affect the smoothly running chip of state. (Sorry about that.) We don't want a user-mode program, which could conceivably have a bug in it, doing something that screws up the system and causes it to stop, reset or go off into some weird realm.
EOR immediate to Status Register
MOVE to Status Register
MOVE User Stack Pointer
OR immediate to Status Register
RTE (Return from Exception)
WHY USE SUPERVISOR MODE?
To look at what supervisor mode is used for, we first have to talk about exception processing. Exceptions in the 68000 world are nonstandard processing circumstances. In the 6502 world we called such situations interrupts. Exception processing is associated with interrupts, trap instructions, tracing and other exceptional conditions. The exception may be internally generated by an instruction or by an unusual condition which arises during the execution of an instruction (e.g., division by zero). Externally, exceptions can be caused by peripheral interrupts, by bus errors, or by a reset. And here's the kicker All exceptions on the 68000 are processed in the supervisor mode! Exception processing occurs in four main steps. In the first step, a copy is made of the status register. As soon as this is done, the S bit is asserted and, bingo, the processor is in the supervisor state. Now it can do anything.
The simple fact is that supervisor mode is used for writing operating systems and other systems software. In the early days of microprocessors there wasn't much of a need for things like supervisor mode. The program running was in total charge of everything that happened in the processor But a funny thing happened on the way to the ST. As micros became more sophisticated they started resembling large computers more and more. It's quite traditional in large systems to let the operating system handle stuff like I/O and interrupts. The logic here is based on Murphy's Law:
"If a user program can screw something up, sooner or later it will."
The best way to avoid OS pollution from user programs is to never give them the opportunity. And once you move from single-user to multi-user systems, you have to consider how to keep individual users from stepping on each other's toes.
The 68000 is an elegant architecture well suited to multi-user, multi-tasking types of applications. Remember, the 68000 is just one of a family of processors that Motorola supports. There are also an MC68010 and MC68012 which introduce even more elegance and a virtual machine concept. The 68012, for example, has an address space of two gigabytes, but a lot less physical memory is usually present. In a
first class passengers to have
to use the same bathroom as
coach passengers would you?
virtual memory system, a user program can be written as though it has a large amount of memory available, when in reality only a small amount of memory is physically present. When a reference is made to memory that isn't really there, an exception is generated and the operating system (sometimes known as the supervisor) loads in the proper memory from a secondary storage such as a hard disk. Even on the 68000 the supervisor state can be used by an external memory-management system. Any time the processor makes a memory reference, it classifies the type of reference being made. This is indicated by the three function-code output lines which allow external translation of addresses, control of access, and recognition of special processor states such as "interrupt acknowledge." The encoding of these lines is as follows:
Thus, theoretically, we could have different address spaces for the OS, for OS data, for user programs and for user data. Memory management on the ST isn't implemented like this, but who knows what's coming down the pike?
GETTING INTO SUPERVISOR MODE
Suppose you want to write a piece of system software, or perhaps change
what the operating system does. With the OS in ROM this may very well become
necessary at some point. The first step is to get into supervisor mode.
We know that we can't simply change the S bit in the status register, so
what do we do? Rule #1: To get into supervisor mode you have to be doing
exception processing. So we have to cause an exception.
|IntoSup||LEA||Dummy A1||;create pointer to Dummy|
|;instruction label in this|
|MOVE.L||A1,-(SP)||;Push this vector onto|
|MOVE.W||#8,-(SP)||;Now push the exception|
|;vector we want changed|
|;onto the stack|
|MOVE.W||#5,-(SP)||;Push value to tell GEM|
|;we want to change an ex-|
|TRAP||#13||;Call BIOS function|
|;handler This will result|
|;in the privilege violation|
|;vector to point to our|
|MOVE||USP,A0||;Now try a privileged|
|;instruction. If it works (a|
|;then we're already in|
|;supervisor mode. If it|
|;fails, a privilege violation|
|;will cause an exception|
|;and thus put us in Super-|
|;visor mode and control|
|;will be transferred to.|
|Dummy||"your code here"||;here|
HEADING TOWARDS THE FUTURE
With the 68000, we could set up an operating system in supervisor mode to multitask any number of auxiliary programs running in user mode. Back on the 6502 we had to resort to tricks like vertical blank interrupts to give us the ability to do multitasking. No more! GEM only does multitasking with installed desk accessories, but just wait. There are already replacement operating systems available that allow multitasking. Micro RTX from Beckemeyer Development Tools, for example, is a multitasking operating system kernel, and 4xFORTH, a FORTH-83 development system supports multitasking. Word is that OS-9, a UNIX-like multitasking operating system, is about ready to be released also.
Even though the ST is lightning quick, there are a number of utilities that it would be nice to have running concurrently. Print spoolers, concurrent spelling checkers, background file transfers. . . Hey, there's a whole new world waiting for us. So don't be afraid to, as Al Haig might say, "take charge."
LIST OF MANUFACTURERS:
Beckemeyer Development Tools
592 Jean St.-#304
Oakland, CA 94610
CIRCLE 336 ON READER SERVIcE CARD
650 Woodfleld Dr, Ste. 730
Schaumberg, IL 60195
CIRCLE 334 ON READER SERVICE CARD
148 Poca Fork Road
Elkview, WV 25071
$99.95 (without GEM calls)
$149.95 (accesses GEM)
CIRCLE 360 ON READER SERVICE CARD