Tutorial 1

CSCC69

TA: Fatemeh Nargesian

Email: fnargesian@cs.toronto.edu

Introduction

• The aim of this tutorial is to familiarize you with environment you will be using for assignments

– Code reading component

– Making a minor change to the existing OS

• Try to get an understanding of the code

Introduction

• Components of the environment you will use

OS 161: the educational OS you will modify as assignments.

System 161: the machine simulator that OS 161 runs on.

GDB: a debugger

OS 161

• An educational OS developed at Harvard.

• A balance between giving students experience working on a real OS, and potentially making students feel the complexity of a fully fledged OS, such as Linux.

• OS 161 is quite small, and therefore it is much easier to develop an understanding of the entire code base.

• The source code distribution contains a full operating system source tree, including the kernel, libraries, various utilities (ls, cat, etc.).

• The OS 161 boots on the simulated machine in the same manner as a real system might boot on real hardware.

System 161

• System 161 simulates a "real" machine to run OS 161 on.

• The machine features a MIPS R2000/R3000 CPU including an MMU, but no floating point unit or cache.

These devices are much simpler than real hardware.

• Using a simulator allows debuggers access to the machine below the software architecture level as if debugging was built into the CPU chip.

• One major advantage is speed of reboot, rebooting real hardware takes minutes, and hence the development cycle can be frustratingly slow on real hardware.

GNU Debugger - GDB

• GDB allows you to set breakpoints to stop your program under certain conditions, inspect the state of your program when it stops, modify its state, and continue where it left off.

• It is a powerful aid to the debugging process - worth investing the time needed to learn it.

• GDB allows you to quickly find bugs that are very difficult to find with the typical printf style debugging.

• Details http://www.gnu.org/software/gdb/gdb.html.

Getting Started

• Obtaining and setting up the distribution

• Sys 161 is already installed for you.

– % set PATH = /local/packages/cs161/bin $PATH

– Logout and log back in.

• The OS 161 distribution can be copied from the class account into your home directory.

• In your home directory, create a directory in which you will do all your CSCC69 work.

– % mkdir ~/cscc69

• Change to your new directory.

– % cd ~/cscc69

Getting Started

• Copy the sources to this assignment into this directory

– % cp [cscc69 directory]/OS161/os161-1.11.tar.gz .

– % tar xzf os161-1.11.tar.gz

– % mv os161-1.11 os161

• You should now have a src directory to work on.

Code Reading Component

• You don’t need to understand every line of code, but you should have a rough idea of what some files do.

• We’ll provide you a set of questions based on the assignments that helps you acquire knowledge about the base code.

• You can consider using cscope: http://cscope.sourceforge.net/

Part1: Code Reading

The Top-level Directory

• The src directory contains the top-level directory of the OS 161. It contains a few files, and subdirectories containing distinct parts of

OS/161. The files are:

Makefile: this makefile builds the OS 161 distribution, including all the provided utilities. It does not build the operating system kernel.

configure: this is a configuration script, similar to autoconf, but not generated by autoconf. You don't need to understand it.

defs.mk: this file is generated by running ./configure. Unless something goes wrong, you shouldn't need to do anything with it.

defs.mk.sample: this is a sample defs.mk file in case something does go wrong with configure. If configure does fail, you can fix def.mk using the comments in this file.

The Top-level Directory

• src contains the following directories:

bin: contains the source code for the user-level utilities available on

OS 161. They are a subset of the typical unix /bin tools, e.g. cat, cp, ls.

Your focus should be on the kernel

• sources. You don't need a detailed programs on OS 161, they are not the kernel include files. Among understanding of the utilities in bin, library available on OS 161.

• sbin, lib and include libraries; However broad understanding of how they work

later.lib: the user-level library code for libc is here.

and where things are is useful.

sbin: contains the source code for the user-level system management utilities found in /sbin on a UNIX machine (e.g. halt, reboot, etc.)

testbin: these are pieces of test code.

The kern Subdirectory

• This directory and its subdirectories are where most of the action takes place. The only file in this directory is a Makefile. This Makefile only installs various header files. It does not actually build anything.

• Spend some time exploring the code.

The kern Subdirectory

kern/arch: This directory contains architecture-dependent code, which means code that is dependent on the architecture OS

161 runs on. Different machine architectures have their own specific architecturedependent directory. Currently, there is only one supported architecture, which is mips.

The kern Subdirectory

• kern/arch/mips/conf

– conf.arch: This tells the kernel config script where to find the machine-specific, low-level functions it needs (see mips/mips).

– Makefile.mips: Kernel Makefile; it copies this when you "config a kernel".

• kern/arch/mips/include

– These files are include files for the machinespecific constants and functions.

The kern Subdirectory

• kern/arch/mips/mips

– These are the low-level functions the kernel needs that are machine-dependent.

• kern/asst0

– This is the directory that contains framework code for one of the assignments at Harvard. You can safely ignore it.

The kern Subdirectory

• kern/compile

– This is where you build kernels. In the compile directory, you will find one subdirectory for each kernel you want to build. In a real installation, these will often correspond to things like a debug build, a profiling build, etc. In our world, each build directory will correspond to a programming assignment, e.g.,

Assignment1, Assignment2, etc. These directories are created when you configure a kernel (described later).

This directory and build organization is typical of UNIX installations and is not necessarily universal across all operating systems.

The kern Subdirectory

kern/conf: config is a shell script that takes a config file, like

Assignment1, and creates the corresponding build directory. Later, in order to build a kernel, you will do the following:

– % cd kern/conf

– % ./config Assignment0

– % cd ../compile/Assignment0

– % make depend

– % make

• This will create the Assignment0 build directory and then actually build a kernel in it.

– Note that you should specify the complete pathname ./config when you configure OS 161. strange results!

The kern Subdirectory

kern/include: These are the include files that the kernel needs. The kern subdirectory contains include files that are visible not only to the operating system itself, but also to userlevel programs.

kern/main: This is where the kernel is initialized and where the kernel main function is implemented.

The kern Subdirectory

kern/userprog: This is where to add code to create and manage user level processes. As it stands now, OS 161 runs only kernel threads; there is no support for user level code.

kern/userprog: This is where to add code to create and manage user level processes. As it stands now, OS 161 runs only kernel threads; there is no support for user level code.

The kern Subdirectory

kern/vm: This directory is also fairly vacant.

Virtual memory would be mostly implemented in here.

kern/fs: The file system implementation has two subdirectories.

kern/fs/vfs: This is the file-system independent layer (vfs stands for "Virtual File System"). It establishes a framework into which you can add new file systems easily. You will want to review vfs.h and vnode.h before looking at this directory.

The kern Subdirectory

kern/fs/sfs: This is the simple file system that

OS 161 contains by default.

kern/dev: This is where all the low level device management code is stored. You can safely ignore most of this directory.

Part 2: Assignment 0

Building a Kernel

• You will now build and install a kernel.

• You first have to configure your source tree.

– % cd ~/cscc69/src

– % ./configure --ostree=$HOME/cscc69/root

– The builds and creates the files needed to run your kernel and puts them in $HOME/cscc69/root

• Now you must configure the kernel itself.

– % cd ~/cscc69/src/kern/conf

– % ./config Assignment0

– This creates Assignment0 build directory in kern/compile

• The next task is to build the kernel.

– % cd ../compile/Assignment0

– % make depend

– % make

Building a Kernel

• Now install the kernel

– % make install

– Every time that you’re making any changes to the kernel, you need to build and install the kernel.

• In addition to the kernel, you have to build the user-level utilities.

– % cd ~/cscc69/src

– % make

Running your Kernel

• If you have made it this far, your have built and installed the entire OS.

• Now it is time to run it.

• Copy the default simulator configuration file into the root directory

– % cp /local/packages/cs161/bin/sys161.conf.sample sys161.conf

• Change to the root directory of your OS.

– % cd ~/cscc69/root

• Now run system 161 (the machine simulator) on your kernel.

– % sys161 kernel

• Power of the machine by typing p at the prompt.

Using GDB

• To debug OS161 you must use a version of GDB configured to understand OS161 and MIPS. This is called cs161-gdb. This version of GDB has been patched to be able to communicate with your kernel through System 161.

• you need to make sure that you are debugging the operating system, not the machine simulator.

– % cs161-gdb kernel

Using GDB

• You will require two windows for the following portion. Be sure both your run window and your debug window are on the same machine. Run the kernel in gdb by first running the kernel and then attaching to it to gdb.

– (in the run window)

– % cd ~/cs161/root

– % sys161 –w kernel

• Use the -w option to tell System/161 to wait for a debugger connection

– % cd ~/cscc69/root

– % os161-gdb kernel

Using GDB

– (gdb) target remote unix:.sockets/gdb

– (gdb) break menu

– (gdb) c

• [gdb will stop at menu() ...]

– (gdb) detach

– (gdb) quit

• http://www.cdf.utoronto.ca/~csc369h/fall/docs/StartingWithOS161.shtml

#usinggdb

• http://cgi.cse.unsw.edu.au/~cs3231/doc/gdb_tut.php

Modifying your Kernel

• We will now go through the steps required to modify and rebuild your kernel.

• We will add a new file to the sources. The file contains a function we will call from existing code. We need to add the rebuild again.

• Simple assignment:

– Create a file called main/hello.c. In this file, write a function called hello that uses kprintf() to print "Hello World\n".

– Edit main/main.c and add a call (in a suitable place) to hello().

Modifying your Kernel

• Since we added new file to the kernel code, we need to add it to the kernel configuration in order to build it. Edit kern/conf/conf.kern

appropriately to include hello.c.

• When we change the kernel config, we need to re-configure the kernel again.

– % cd ~/cscc69/src/kern/conf

– % ./config Assignment0

Modifying your Kernel

• Now we can rebuild the kernel.

– % cd ../compile/Assignment0

– % make depend

– % make

– % make install

OS/161 base system version 1.07

The Task

Copyright (c) 2000, 2001, 2002, 2003 President and Fellows of Harvard College. All

• rights reserved.

Follow the instruction above instructions to

Cpu is MIPS r2000/r3000 add the given file to the operating system.

Device probe...

Once you have found (using GDB) and fixed emu0 at lamebus0 the bugs, you have completed the assignment. ltimer0 at lamebus0

Make sure you see the Hello World!!! output beep0 at ltimer0 just prior to the menu prompt.

lrandom0 at lamebus0 random0 at lrandom0 lser0 at lamebus0 con0 at lser0 pseudorand0 (virtual)

Hello World!!!OS/161 kernel [? for menu]:

Questions