Lecture 5 homework: Unix v6 on the PDP-11 -- Part I

Handed out: Wednesday, September 15, 2004
Due: Wednesday, September 22, 2004
Read: chapters 1, 2, 4, and pages 6-1 and 6-2 (till the comments about line 1559) of Lions' commentary and the corresponding source code (sheet 06, line 0612 through 0671).

Hand-In Procedure

You are to turn in this homework during lecture on Wednesday. Please write up your answers to the exercises below and hand them in to a 6.828 staff member by the end of the lecture.

Introdution

In this homework you will learn your way around some of the software you'll be using for the course. As you know, the course is split between studying the source code for the UNIX V6 kernel (which runs on PDP-11s) and applying the knowledge you gain in the writing of a small operating system for the Intel x86.

While the labs focus on working with the x86 simulator bochs, this homework introduces you to a PDP-11 simulator, a tool that will aid you in understanding Thompson and Ritchie's original Unix V6 code. When you are studying the kernel source throughout the course, knowing your way around the simulators and the kernel code will let you answer questions by inspecting the running system, or even modifying it and recompiling it. This should yield greater understanding of, and perhaps even a greater appreciation for, the code.

This homework steps through booting the Unix v6 kernel in the PDP-11 simulator, while you will focus on rebuilding the kernel and understanding it further in the next homework. The reference page has links to documents related to Unix v6 on the PDP-11, including the processor handbook covering the internals of the PDP-11 processor.

Acquiring the Software

We encourage you to do the homeworks using Athena, where we have built all the tools for you already.

Note: our software is only configured on the Linux Athena machines (i.e., not on the Suns). Use the uname command to make sure you are running on Linux.

To access them, run

athena% uname
Linux
athena% add 6.828 gnu
athena% 

UNIX V6 on the PDP-11

We will use a PDP-11 hardware simulator to provide the hardware on which UNIX expects to run. The full manual for the simulator is online. You may find it helpful for future reference.

First, you'll need your own copy of the UNIX disk images and a copy of the UNIX sources too, for later.

athena% cd ~
athena% mkdir 6.828
athena% cd 6.828
athena% cp -R /mit/6.828/sw/v6rk .
athena% cp -R /mit/6.828/sw/v6 .
athena% cd v6rk
athena% ls -l v6*
total 6043
-rw-rw-r--  1 rsc  rsc  2048000 Sep  1 21:57 v6doc
-rw-rw-r--  1 rsc  rsc  2060800 Sep  1 22:19 v6root
-rw-rw-r--  1 rsc  rsc  2048000 Sep  1 21:57 v6src
athena% 
There are three disks, named v6doc, v6root, and v6src.

To boot UNIX, start the simulator and type the following at the prompts.

athena% pdp11

PDP-11 simulator V2.3d
sim> attach rk0 v6root
sim> attach rk1 v6src
sim> attach rk2 v6doc
sim> boot rk0
The lines typed at the sim> prompts configure the machine being simulated. In particular, they attach three disks to the machine as devices rk0, rk1, and rk2 and then boot from disk rk0. You can abbreviate attach down to a and boot down to b.

Now the boot loader begins. You give it a kernel name and log in:

 @unix

login: root
# stty erase backspace
# stty kill control-U
# ls -l
total 70
drwxrwxr-x  2 bin      1104 May 14 00:47 bin
drwxrwxr-x  2 bin      1824 Aug 14 22:17 dev
drwxrwxr-x  2 bin       496 Aug 14 22:22 etc
drwxrwxr-x  2 bin       464 May 13 23:35 lib
drwxrwxr-x  2 bin        32 May 13 20:01 mnt
drwxrwxrwx  2 bin       272 Jul 18 09:19 tmp
-rw-rw-rw-  1 root    28854 Jul 18 09:18 unix
drwxrwxr-x 15 bin       240 Aug 14 22:19 usr
# 

The @ prompt is printed by the boot loader, asking for the name of the kernel file in the root directory. If you were trying a new kernel, you might install it as /unix.new rather than overwrite /unix, so that you could still boot the old kernel if it turned out your changes weren't for the best.

The stty commands set up your terminal. (Type real backspace and control-U characters.) You must type the first few lines perfectly! Until you run the first stty command, backspace will appear to work but not actually work -- although your xterm (or whatever) window is processing backspace properly, the UNIX V6 tty driver treats it as just another character until stty has done its magic. (The default settings are # for the erase character and @ for the line kill character.)

You may find it interesting to poke around. It's impressive how many tools are packed into that 2MB disk image. The shell command to change directories is chdir, not cd.

To get back out to the simulator prompt, type control-E. When you do that (actually, every time the simulation stops) the simulator prints information about the machine register state. The register values are printed in octal, and all the interaction with the simulator is in octal as well. (The computer world of 2003 has now settled on hexadecimal, but octal was the way of life in 1977. When we're talking about UNIX we'll assume octal, but when we talk about the x86 in the labs we'll assume hexadecimal.)

To reset the machine and boot again, run the commands

sim> reset
sim> boot rk0

Exercise 1. The PDP-11 simulator provides a register SR to allow you to pretend to set the switches that would be on the front of a real PDP-11. Run the command 'e sr' (e stands for examine) to look at the current value of SR. What is it? Run 'd sr 1' (d stands for deposit) to change the SR value. Boot again. What's different? Poke around in ~/6.828/v6/usr/sys to find where that came from. Why didn't you see it the first time you booted? (Hint: you ran the same kernel both times, so the answer is not that the switches caused a different kernel to be loaded.)

The debugger command 'e state' prints the entire state of the CPU, including the registers you've seen at the breakpoints and also various segmentation and device registers.

Exercise 2. Save a transcript of the following session. The UNIX kernel boot sequence starts at address 40 (octal). Run the command 'd break 40' to set a breakpoint (there's only one!) at address 40. Boot again. When the kernel stops at address 40, what is the next instruction it will execute? Run the command 's' (for step) to execute that one instruction. Now what is the PC set to?

Examine the next 20 bytes of instructions to run by executing 'e xxx-yyy' where xxx is the current value of the PC, and yyy is xxx plus 20. By default e prints the octal words it is examining. You can examine instructions with the -m flag. Try e -m xxx-yyy. Does each instruction occupy the same number of bytes in memory?

Run 't 5' to trace through those instructions. (You could also run s 5 times. s 5 would step through the 5 instructions but not print the register states after each one.) Clear the breakpoint by setting it to an innocuous value like the top of memory: d break 177777. Then let the simulation continue: c.

Hand in the transcript of your session, along with the answers to the three questions above.

Challenge! After letting the machine run for a second, type Control-E and look around. What's going on in the machine? How did it get there?

(Note: challenge questions are 100% optional. They are exercises we think would be fun (though often a fair amount of work) for students who are super excited about the material. You can answer the challenge questions, and we will correct your answers, but they have no effect whatsoever on your course grade.)

To close the simulator, type Control-E to get back to the prompt and then type q.

This completes the homework.