1 @node Introduction, Pintos Tour, Top, Top
4 Welcome to Pintos. Pintos is a simple operating system framework for
5 the 80@var{x}86 architecture. It supports kernel threads, loading and
6 running user programs, and a file system, but it implements all of
7 these in a very simple way. In the Pintos projects, you and your
8 project team will strengthen its support in all three of these areas.
9 You will also add a virtual memory implementation.
11 Pintos could, theoretically, run on a regular IBM-compatible PC.
12 Unfortunately, it is impractical to supply every CS 140 student
13 a dedicated PC for use with Pintos. Therefore, we will run Pintos projects
14 in a system simulator, that is, a program that simulates an 80@var{x}86
15 CPU and its peripheral devices accurately enough that unmodified operating
16 systems and software can run under it. In class we will use the
17 @uref{http://bochs.sourceforge.net, , Bochs} and
18 @uref{http://fabrice.bellard.free.fr/qemu/, ,
19 qemu} simulators. Pintos has also been tested with
20 @uref{http://www.vmware.com/products/server/gsx_features.html, ,
23 These projects are hard. CS 140 has a reputation of taking a lot of
24 time, and deservedly so. We will do what we can to reduce the workload, such
25 as providing a lot of support material, but there is plenty of
26 hard work that needs to be done. We welcome your
27 feedback. If you have suggestions on how we can reduce the unnecessary
28 overhead of assignments, cutting them down to the important underlying
29 issues, please let us know.
31 This chapter explains how to get started working with Pintos. You
32 should read the entire chapter before you start work on any of the
44 @section Getting Started
46 To get started, you'll have to log into a machine that Pintos can be
47 built on. The CS140 ``officially supported''
48 Pintos development machines are the machines in Sweet Hall managed by
49 Stanford ITSS, as described on the
50 @uref{http://www.stanford.edu/services/cluster/environs/sweet/, , ITSS
51 webpage}. You may use the Solaris or Linux machines. We will test your
52 code on these machines, and the instructions given here assume this
53 environment. However, Pintos and its supporting tools are portable
54 enough that it should build ``out of the box'' in other environments.
56 Once you've logged into one of these machines, either locally or
57 remotely, start out by adding our binaries directory to your @env{PATH}
58 environment. Under @command{csh}, Stanford's login shell, you can do so
59 with this command:@footnote{The term @samp{`uname -m`} expands to either
60 @file{sun4u} or @file{i686} according to the type of computer you're
63 set path = ( $path /usr/class/cs140/`uname -m`/bin )
66 (Notice that both @samp{`} are left single quotes or ``backticks.'')
67 It is a good idea to add this line to the @file{.cshrc} file
68 in your home directory. Otherwise, you'll have to type it every time
72 * Source Tree Overview::
75 * Debugging versus Testing::
78 @node Source Tree Overview
79 @subsection Source Tree Overview
81 Now you can extract the source for Pintos into a directory named
82 @file{pintos/src}, by executing
84 tar xzf /usr/class/cs140/pintos/pintos.tar.gz
87 @uref{http://@/www.stanford.edu/@/class/@/cs140/@/pintos/@/pintos.@/tar.gz}
88 and extract it in a similar way.
90 Let's take a look at what's inside. Here's the directory structure
91 that you should see in @file{pintos/src}:
95 Source code for the base kernel, which you will modify starting in
99 Source code for the user program loader, which you will modify
100 starting with project 2.
103 An almost empty directory. You will implement virtual memory here in
107 Source code for a basic file system. You will use this file system
108 starting with project 2, but you will not modify it until project 4.
111 Source code for I/O device interfacing: keyboard, timer, disk, etc.
112 You will modify the timer implementation in project 1. Otherwise
113 you should have no need to change this code.
116 An implementation of a subset of the standard C library. The code in
117 this directory is compiled into both the Pintos kernel and, starting
118 from project 2, user programs that run under it. In both kernel code
119 and user programs, headers in this directory can be included using the
120 @code{#include <@dots{}>} notation. You should have little need to
124 Parts of the C library that are included only in the Pintos kernel.
125 This also includes implementations of some data types that you are
126 free to use in your kernel code: bitmaps, doubly linked lists, and
127 hash tables. In the kernel, headers in this
128 directory can be included using the @code{#include <@dots{}>}
132 Parts of the C library that are included only in Pintos user programs.
133 In user programs, headers in this directory can be included using the
134 @code{#include <@dots{}>} notation.
137 Tests for each project. You can modify this code if it helps you test
138 your submission, but we will replace it with the originals before we run
142 Example user programs for use starting with project 2.
146 These files may come in handy if you decide to try working with Pintos
147 away from the ITSS machines. Otherwise, you can ignore them.
150 @node Building Pintos
151 @subsection Building Pintos
153 As the next step, build the source code supplied for
154 the first project. First, @command{cd} into the @file{threads}
155 directory. Then, issue the @samp{make} command. This will create a
156 @file{build} directory under @file{threads}, populate it with a
157 @file{Makefile} and a few subdirectories, and then build the kernel
158 inside. The entire build should take less than 30 seconds.
160 Watch the commands executed during the build. On the Linux machines,
161 the ordinary system tools are used. On a SPARC machine, special build
162 tools are used, whose names begin with @samp{i386-elf-}, e.g.@:
163 @code{i386-elf-gcc}, @code{i386-elf-ld}. These are ``cross-compiler''
164 tools. That is, the build is running on a SPARC machine (called the
165 @dfn{host}), but the result will run on a simulated 80@var{x}86 machine
166 (called the @dfn{target}). The @samp{i386-elf-@var{program}} tools are
167 specially built for this configuration.
169 Following the build, the following are the interesting files in the
170 @file{build} directory:
174 A copy of @file{pintos/src/Makefile.build}. It describes how to build
175 the kernel. @xref{Adding Source Files}, for more information.
178 Object file for the entire kernel. This is the result of linking
179 object files compiled from each individual kernel source file into a
180 single object file. It contains debug information, so you can run
181 @command{gdb} or @command{backtrace} (@pxref{Backtraces}) on it.
184 Memory image of the kernel. These are the exact bytes loaded into
185 memory to run the Pintos kernel. To simplify loading, it is always
186 padded out with zero bytes up to an exact multiple of 4 kB in
190 Memory image for the kernel loader, a small chunk of code written in
191 assembly language that reads the kernel from disk into memory and
192 starts it up. It is exactly 512 bytes long, a size fixed by the
196 Disk image for the kernel, which is just @file{loader.bin} followed by
197 @file{kernel.bin}. This file is used as a ``virtual disk'' by the
201 Subdirectories of @file{build} contain object files (@file{.o}) and
202 dependency files (@file{.d}), both produced by the compiler. The
203 dependency files tell @command{make} which source files need to be
204 recompiled when other source or header files are changed.
207 @subsection Running Pintos
209 We've supplied a program for conveniently running Pintos in a simulator,
210 called @command{pintos}. In the simplest case, you can invoke
211 @command{pintos} as @code{pintos @var{argument}@dots{}}. Each
212 @var{argument} is passed to the Pintos kernel for it to act on.
214 Try it out. First @command{cd} into the newly created @file{build}
215 directory. Then issue the command @code{pintos run alarm-multiple},
216 which passes the arguments @code{run alarm-multiple} to the Pintos
217 kernel. In these arguments, @command{run} instructs the kernel to run a
218 test and @code{alarm-multiple} is the test to run.
220 This command creates a @file{bochsrc.txt} file, which is needed for
221 running Bochs, and then invoke Bochs. Bochs opens a new window that
222 represents the simulated machine's display, and a BIOS message briefly
223 flashes. Then Pintos boots and runs the @code{alarm-multiple} test
224 program, which outputs a few screenfuls of text. When it's done, you
225 can close Bochs by clicking on the ``Power'' button in the window's top
226 right corner, or rerun the whole process by clicking on the ``Reset''
227 button just to its left. The other buttons are not very useful for our
230 (If no window appeared at all, and you just got a terminal full of
231 corrupt-looking text, then you're probably logged in remotely and X
232 forwarding is not set up correctly. In this case, you can fix your X
233 setup, or you can use the @option{-v} option to disable X output:
234 @code{pintos -v -- run alarm-multiple}.)
236 The text printed by Pintos inside Bochs probably went by too quickly to
237 read. However, you've probably noticed by now that the same text was
238 displayed in the terminal you used to run @command{pintos}. This is
239 because Pintos sends all output both to the VGA display and to the first
240 serial port, and by default the serial port is connected to Bochs's
241 @code{stdout}. You can log this output to a file by redirecting at the
242 command line, e.g.@: @code{pintos run alarm-multiple > logfile}.
244 The @command{pintos} program offers several options for configuring the
245 simulator or the virtual hardware. If you specify any options, they
246 must precede the commands passed to the Pintos kernel and be separated
247 from them by @option{--}, so that the whole command looks like
248 @code{pintos @var{option}@dots{} -- @var{argument}@dots{}}. Invoke
249 @code{pintos} without any arguments to see a list of available options.
250 Options can select a simulator to use: the default is Bochs, but on the
251 Linux machines @option{--qemu} selects qemu. You can run the simulator
252 with a debugger (@pxref{gdb}). You can set the amount of memory to give
253 the VM. Finally, you can select how you want VM output to be displayed:
254 use @option{-v} to turn off the VGA display, @option{-t} to use your
255 terminal window as the VGA display instead of opening a new window
256 (Bochs only), or @option{-s} to suppress the serial output to
259 The Pintos kernel has commands and options other than @command{run}.
260 These are not very interesting for now, but you can see a list of them
261 using @option{-h}, e.g.@: @code{pintos -h}.
263 @node Debugging versus Testing
264 @subsection Debugging versus Testing
266 When you're debugging code, it's useful to be able to be able to run a
267 program twice and have it do exactly the same thing. On second and
268 later runs, you can make new observations without having to discard or
269 verify your old observations. This property is called
270 ``reproducibility.'' The simulator we use by default, Bochs, can be set
272 reproducibility, and that's the way that @command{pintos} invokes it
275 Of course, a simulation can only be reproducible from one run to the
276 next if its input is the same each time. For simulating an entire
277 computer, as we do, this means that every part of the computer must be
278 the same. For example, you must use the same command-line argument, the
279 same disks, the same version
280 of Bochs, and you must not hit any keys on the keyboard (because you
281 could not be sure to hit them at exactly the same point each time)
284 While reproducibility is useful for debugging, it is a problem for
285 testing thread synchronization, an important part of most of the projects. In
286 particular, when Bochs is set up for reproducibility, timer interrupts
287 will come at perfectly reproducible points, and therefore so will
288 thread switches. That means that running the same test several times
289 doesn't give you any greater confidence in your code's correctness
290 than does running it only once.
292 So, to make your code easier to test, we've added a feature, called
293 ``jitter,'' to Bochs, that makes timer interrupts come at random
294 intervals, but in a perfectly predictable way. In particular, if you
295 invoke @command{pintos} with the option @option{-j @var{seed}}, timer
296 interrupts will come at irregularly spaced intervals. Within a single
297 @var{seed} value, execution will still be reproducible, but timer
298 behavior will change as @var{seed} is varied. Thus, for the highest
299 degree of confidence you should test your code with many seed values.
301 On the other hand, when Bochs runs in reproducible mode, timings are not
302 realistic, meaning that a ``one-second'' delay may be much shorter or
303 even much longer than one second. You can invoke @command{pintos} with
304 a different option, @option{-r}, to set up Bochs for realistic
305 timings, in which a one-second delay should take approximately one
306 second of real time. Simulation in real-time mode is not reproducible,
307 and options @option{-j} and @option{-r} are mutually exclusive.
309 On the Linux machines only, the qemu simulator is available as an
310 alternative to Bochs (use @option{--qemu} when invoking
311 @command{pintos}). The qemu simulator is much faster than Bochs, but it
312 only supports real-time simulation and does not have a reproducible
318 We will grade your assignments based on test results and design quality,
319 each of which comprises 50% of your grade.
329 Your test result grade will be based on our tests. Each project has
330 several tests, each of which has a name beginning with @file{tests}.
331 To completely test your submission, invoke @code{make check} from the
332 project @file{build} directory. This will build and run each test and
333 print a ``pass'' or ``fail'' message for each one. When a test fails,
334 @command{make check} also prints some details of the reason for failure.
335 After running all the tests, @command{make check} also prints a summary
338 For project 1, the tests will probably run faster in Bochs. For the
339 rest of the projects, they will probably run faster in qemu.
341 You can also run individual tests one at a time. A given test @var{t}
342 writes its output to @file{@var{t}.output}, then a script scores the
343 output as ``pass'' or ``fail'' and writes the verdict to
344 @file{@var{t}.result}. To run and grade a single test, @command{make}
345 the @file{.result} file explicitly from the @file{build} directory, e.g.@:
346 @code{make tests/threads/alarm-multiple.result}. If @command{make} says
347 that the test result is up-to-date, but you want to re-run it anyway,
348 either run @code{make clean} or delete the @file{.output} file by hand.
350 By default, each test provides feedback only at completion, not during
351 its run. If you prefer, you can observe the progress of each test by
352 specifying @option{VERBOSE=1} on the @command{make} command line, as in
353 @code{make check VERBOSE=1}. You can also provide arbitrary options to the
354 @command{pintos} run by the tests with @option{PINTOSOPTS='@dots{}'},
355 e.g.@: @code{make check PINTOSOPTS='--qemu'} to run the tests under
358 All of the tests and related files are in @file{pintos/src/tests}.
359 Before we test your submission, we will replace the contents of that
360 directory by a pristine, unmodified copy, to ensure that the correct
361 tests are used. Thus, you can modify some of the tests if that helps in
362 debugging, but we will run the originals.
364 All software has bugs, so some of our tests may be flawed. If you think
365 a test failure is a bug in the test, not a bug in your code,
366 please point it out. We will look at it and fix it if necessary.
368 Please don't try to take advantage of our generosity in giving out our
369 test suite. Your code has to work properly in the general case, not
370 just for the test cases we supply. For example, it would be unacceptable
371 to explicitly base the kernel's behavior on the name of the running
372 test case. Such attempts to side-step the test cases will receive no
373 credit. If you think your solution may be in a gray area here, please
379 We will judge your design based on the design document and the source
380 code that you submit. We will read your entire design document and much
383 We provide a design document template for each project. For each
384 significant part of a project, the template asks questions in four
385 areas: data structures, algorithms, synchronization, and rationale. An
386 incomplete design document or one that strays from the template without
387 good reason may be penalized. Incorrect capitalization, punctuation,
388 spelling, or grammar can also cost points. @xref{Project
389 Documentation}, for a sample design document for a fictitious project.
391 Design quality will also be judged based on your source code. We will
392 typically look at the differences between the original Pintos source
393 tree and your submission, based on the output of a command like
394 @code{diff -urpb pintos.orig pintos.submitted}. We will try to match up your
395 description of the design with the code submitted. Important
396 discrepancies between the description and the actual code will be
397 penalized, as will be any bugs we find by spot checks.
399 The most important aspects of design quality are those that specifically
400 relate to the operating system issues at stake in the project. For
401 example, the organization of an inode is an important part of file
402 system design, so in the file system project a poorly designed inode
403 would lose points. Other issues are much less important. For
404 example, multiple Pintos design problems call for a ``priority
405 queue,'' that is, a dynamic collection from which the minimum (or
406 maximum) item can quickly be extracted. Fast priority queues can be
407 implemented many ways, but we do not expect you to build a fancy data
408 structure even if it might improve performance. Instead, you are
409 welcome to use a linked list (and Pintos even provides one with
410 convenient functions for sorting and finding minimums and maximums).
412 Pintos is written in a consistent style. Make your additions and
413 modifications in existing Pintos source files blend in, not stick out.
414 In new source files, adopt the existing Pintos style by preference, but
415 make the self-consistent at the very least. Use horizontal and vertical
416 white space to make code readable. Add a comment to every structure,
417 structure member, global or static variable, and function definition.
418 Update existing comments as you modify code. Don't comment out or use
419 the preprocessor to ignore blocks of code. Use assertions to document
420 key invariants. Decompose code into functions for clarity. Code that
421 is difficult to understand because it violates these or other ``common
422 sense'' software engineering practices will be penalized.
424 In the end, remember your audience. Code is written primarily to be
425 read by humans. It has to be acceptable to the compiler too, but the
426 compiler doesn't care about how it looks or how well it is written.
431 Pintos is distributed under a liberal license that allows free use,
432 modification, and distribution. Students and others who work on Pintos
433 own the code that they write and may use it for any purpose.
435 In the context of Stanford's CS 140 course, please respect the spirit
436 and the letter of the honor code by refraining from reading any homework
437 solutions available online or elsewhere. Reading the source code for
438 other operating system kernels, such as Linux or FreeBSD, is allowed,
439 but do not copy code from them literally. Please cite the code that
440 inspired your own in your design documentation.
442 Pintos comes with NO WARRANTY, not even for MERCHANTABILITY or FITNESS
443 FOR A PARTICULAR PURPOSE.
445 The @file{LICENSE} file at the top level of the Pintos source
446 distribution has full details of the license and lack of warranty.
448 @node Acknowledgements
449 @section Acknowledgements
451 Pintos and this documentation were written by Ben Pfaff
452 @email{blp@@cs.stanford.edu}.
454 The original structure and form of Pintos was inspired by the Nachos
455 instructional operating system from the University of California,
456 Berkeley. A few of the source files were originally more-or-less
457 literal translations of the Nachos C++ code into C. These files bear
458 the original UCB license notice.
460 A few of the Pintos source files are derived from code used in the
461 Massachusetts Institute of Technology's 6.828 advanced operating systems
462 course. These files bear the original MIT license notice.
464 The Pintos projects and documentation originated with those designed for
465 Nachos by current and former CS140 teaching assistants at Stanford
466 University, including at least Yu Ping, Greg Hutchins, Kelly Shaw, Paul
467 Twohey, Sameer Qureshi, and John Rector. If you're not on this list but
468 should be, please let me know.
470 Example code for condition variables (@pxref{Condition Variables}) is
471 from classroom slides originally by Dawson Engler and updated by Mendel
477 Pintos originated as a replacement for Nachos with a similar design.
478 Since then Pintos has greatly diverged from the Nachos design. Pintos
479 differs from Nachos in two important ways. First, Pintos runs on real
480 or simulated 80@var{x}86 hardware, whereas Nachos runs as a process on a
481 host operating system. Second, like most real-world operating systems,
482 Pintos is written in C, whereas Nachos is written in C++.
484 Why the name ``Pintos''? First, like nachos, pinto beans are a common
485 Mexican food. Second, Pintos is small and a ``pint'' is a small amount.
486 Third, like drivers of the eponymous car, students are likely to have
487 trouble with blow-ups.