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 @value{coursenumber} 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/, , VMware Player}.
22 These projects are hard. @value{coursenumber} has a reputation of taking a lot of
23 time, and deservedly so. We will do what we can to reduce the workload, such
24 as providing a lot of support material, but there is plenty of
25 hard work that needs to be done. We welcome your
26 feedback. If you have suggestions on how we can reduce the unnecessary
27 overhead of assignments, cutting them down to the important underlying
28 issues, please let us know.
30 This chapter explains how to get started working with Pintos. You
31 should read the entire chapter before you start work on any of the
37 * Legal and Ethical Issues::
43 @section Getting Started
45 To get started, you'll have to log into a machine that Pintos can be
48 We will test your code on these machines, and the instructions given
49 here assume this environment. We cannot provide support for installing
50 and working on Pintos on your own machine, but we provide instructions
51 for doing so nonetheless (@pxref{Installing Pintos}).
53 Once you've logged into one of these machines, either locally or
54 remotely, start out by adding our binaries directory to your @env{PATH}
59 * Source Tree Overview::
62 * Debugging versus Testing::
65 @node Source Tree Overview
66 @subsection Source Tree Overview
68 Now you can extract the source for Pintos into a directory named
69 @file{pintos/src}, by executing
71 zcat @value{localpintostarpath} | tar x
74 @uref{@value{localpintoshttppath}}
75 and extract it in a similar way.
77 Let's take a look at what's inside. Here's the directory structure
78 that you should see in @file{pintos/src}:
82 Source code for the base kernel, which you will modify starting in
86 Source code for the user program loader, which you will modify
87 starting with project 2.
90 An almost empty directory. You will implement virtual memory here in
94 Source code for a basic file system. You will use this file system
95 starting with project 2, but you will not modify it until project 4.
98 Source code for I/O device interfacing: keyboard, timer, disk, etc.
99 You will modify the timer implementation in project 1. Otherwise
100 you should have no need to change this code.
103 An implementation of a subset of the standard C library. The code in
104 this directory is compiled into both the Pintos kernel and, starting
105 from project 2, user programs that run under it. In both kernel code
106 and user programs, headers in this directory can be included using the
107 @code{#include <@dots{}>} notation. You should have little need to
111 Parts of the C library that are included only in the Pintos kernel.
112 This also includes implementations of some data types that you are
113 free to use in your kernel code: bitmaps, doubly linked lists, and
114 hash tables. In the kernel, headers in this
115 directory can be included using the @code{#include <@dots{}>}
119 Parts of the C library that are included only in Pintos user programs.
120 In user programs, headers in this directory can be included using the
121 @code{#include <@dots{}>} notation.
124 Tests for each project. You can modify this code if it helps you test
125 your submission, but we will replace it with the originals before we run
129 Example user programs for use starting with project 2.
133 These files may come in handy if you decide to try working with Pintos
134 on your own machine. Otherwise, you can ignore them.
137 @node Building Pintos
138 @subsection Building Pintos
140 As the next step, build the source code supplied for
141 the first project. First, @command{cd} into the @file{threads}
142 directory. Then, issue the @samp{make} command. This will create a
143 @file{build} directory under @file{threads}, populate it with a
144 @file{Makefile} and a few subdirectories, and then build the kernel
145 inside. The entire build should take less than 30 seconds.
149 Following the build, the following are the interesting files in the
150 @file{build} directory:
154 A copy of @file{pintos/src/Makefile.build}. It describes how to build
155 the kernel. @xref{Adding Source Files}, for more information.
158 Object file for the entire kernel. This is the result of linking
159 object files compiled from each individual kernel source file into a
160 single object file. It contains debug information, so you can run
161 GDB (@pxref{GDB}) or @command{backtrace} (@pxref{Backtraces}) on it.
164 Memory image of the kernel. These are the exact bytes loaded into
165 memory to run the Pintos kernel. To simplify loading, it is always
166 padded out with zero bytes up to an exact multiple of 4 kB in
170 Memory image for the kernel loader, a small chunk of code written in
171 assembly language that reads the kernel from disk into memory and
172 starts it up. It is exactly 512 bytes long, a size fixed by the
176 Disk image for the kernel, which is just @file{loader.bin} followed by
177 @file{kernel.bin}. This file is used as a ``virtual disk'' by the
181 Subdirectories of @file{build} contain object files (@file{.o}) and
182 dependency files (@file{.d}), both produced by the compiler. The
183 dependency files tell @command{make} which source files need to be
184 recompiled when other source or header files are changed.
187 @subsection Running Pintos
189 We've supplied a program for conveniently running Pintos in a simulator,
190 called @command{pintos}. In the simplest case, you can invoke
191 @command{pintos} as @code{pintos @var{argument}@dots{}}. Each
192 @var{argument} is passed to the Pintos kernel for it to act on.
194 Try it out. First @command{cd} into the newly created @file{build}
195 directory. Then issue the command @code{pintos run alarm-multiple},
196 which passes the arguments @code{run alarm-multiple} to the Pintos
197 kernel. In these arguments, @command{run} instructs the kernel to run a
198 test and @code{alarm-multiple} is the test to run.
200 This command creates a @file{bochsrc.txt} file, which is needed for
201 running Bochs, and then invoke Bochs. Bochs opens a new window that
202 represents the simulated machine's display, and a BIOS message briefly
203 flashes. Then Pintos boots and runs the @code{alarm-multiple} test
204 program, which outputs a few screenfuls of text. When it's done, you
205 can close Bochs by clicking on the ``Power'' button in the window's top
206 right corner, or rerun the whole process by clicking on the ``Reset''
207 button just to its left. The other buttons are not very useful for our
210 (If no window appeared at all, and you just got a terminal full of
211 corrupt-looking text, then you're probably logged in remotely and X
212 forwarding is not set up correctly. In this case, you can fix your X
213 setup, or you can use the @option{-v} option to disable X output:
214 @code{pintos -v -- run alarm-multiple}.)
216 The text printed by Pintos inside Bochs probably went by too quickly to
217 read. However, you've probably noticed by now that the same text was
218 displayed in the terminal you used to run @command{pintos}. This is
219 because Pintos sends all output both to the VGA display and to the first
220 serial port, and by default the serial port is connected to Bochs's
221 @code{stdin} and @code{stdout}. You can log serial output to a file by
223 command line, e.g.@: @code{pintos run alarm-multiple > logfile}.
225 The @command{pintos} program offers several options for configuring the
226 simulator or the virtual hardware. If you specify any options, they
227 must precede the commands passed to the Pintos kernel and be separated
228 from them by @option{--}, so that the whole command looks like
229 @code{pintos @var{option}@dots{} -- @var{argument}@dots{}}. Invoke
230 @code{pintos} without any arguments to see a list of available options.
231 Options can select a simulator to use: the default is Bochs, but
232 @option{--qemu} selects QEMU. You can run the simulator
233 with a debugger (@pxref{GDB}). You can set the amount of memory to give
234 the VM. Finally, you can select how you want VM output to be displayed:
235 use @option{-v} to turn off the VGA display, @option{-t} to use your
236 terminal window as the VGA display instead of opening a new window
237 (Bochs only), or @option{-s} to suppress serial input from @code{stdin}
238 and output to @code{stdout}.
240 The Pintos kernel has commands and options other than @command{run}.
241 These are not very interesting for now, but you can see a list of them
242 using @option{-h}, e.g.@: @code{pintos -h}.
244 @node Debugging versus Testing
245 @subsection Debugging versus Testing
247 When you're debugging code, it's useful to be able to run a
248 program twice and have it do exactly the same thing. On second and
249 later runs, you can make new observations without having to discard or
250 verify your old observations. This property is called
251 ``reproducibility.'' One of the simulators that Pintos supports, Bochs,
253 reproducibility, and that's the way that @command{pintos} invokes it
256 Of course, a simulation can only be reproducible from one run to the
257 next if its input is the same each time. For simulating an entire
258 computer, as we do, this means that every part of the computer must be
259 the same. For example, you must use the same command-line argument, the
260 same disks, the same version
261 of Bochs, and you must not hit any keys on the keyboard (because you
262 could not be sure to hit them at exactly the same point each time)
265 While reproducibility is useful for debugging, it is a problem for
266 testing thread synchronization, an important part of most of the projects. In
267 particular, when Bochs is set up for reproducibility, timer interrupts
268 will come at perfectly reproducible points, and therefore so will
269 thread switches. That means that running the same test several times
270 doesn't give you any greater confidence in your code's correctness
271 than does running it only once.
273 So, to make your code easier to test, we've added a feature, called
274 ``jitter,'' to Bochs, that makes timer interrupts come at random
275 intervals, but in a perfectly predictable way. In particular, if you
276 invoke @command{pintos} with the option @option{-j @var{seed}}, timer
277 interrupts will come at irregularly spaced intervals. Within a single
278 @var{seed} value, execution will still be reproducible, but timer
279 behavior will change as @var{seed} is varied. Thus, for the highest
280 degree of confidence you should test your code with many seed values.
282 On the other hand, when Bochs runs in reproducible mode, timings are not
283 realistic, meaning that a ``one-second'' delay may be much shorter or
284 even much longer than one second. You can invoke @command{pintos} with
285 a different option, @option{-r}, to set up Bochs for realistic
286 timings, in which a one-second delay should take approximately one
287 second of real time. Simulation in real-time mode is not reproducible,
288 and options @option{-j} and @option{-r} are mutually exclusive.
290 The QEMU simulator is available as an
291 alternative to Bochs (use @option{--qemu} when invoking
292 @command{pintos}). The QEMU simulator is much faster than Bochs, but it
293 only supports real-time simulation and does not have a reproducible
299 We will grade your assignments based on test results and design quality,
300 each of which comprises 50% of your grade.
310 Your test result grade will be based on our tests. Each project has
311 several tests, each of which has a name beginning with @file{tests}.
312 To completely test your submission, invoke @code{make check} from the
313 project @file{build} directory. This will build and run each test and
314 print a ``pass'' or ``fail'' message for each one. When a test fails,
315 @command{make check} also prints some details of the reason for failure.
316 After running all the tests, @command{make check} also prints a summary
319 For project 1, the tests will probably run faster in Bochs. For the
320 rest of the projects, they will run much faster in QEMU.
321 @command{make check} will select the faster simulator by default, but
322 you can override its choice by specifying @option{SIMULATOR=--bochs} or
323 @option{SIMULATOR=--qemu} on the @command{make} command line.
325 You can also run individual tests one at a time. A given test @var{t}
326 writes its output to @file{@var{t}.output}, then a script scores the
327 output as ``pass'' or ``fail'' and writes the verdict to
328 @file{@var{t}.result}. To run and grade a single test, @command{make}
329 the @file{.result} file explicitly from the @file{build} directory, e.g.@:
330 @code{make tests/threads/alarm-multiple.result}. If @command{make} says
331 that the test result is up-to-date, but you want to re-run it anyway,
332 either run @code{make clean} or delete the @file{.output} file by hand.
334 By default, each test provides feedback only at completion, not during
335 its run. If you prefer, you can observe the progress of each test by
336 specifying @option{VERBOSE=1} on the @command{make} command line, as in
337 @code{make check VERBOSE=1}. You can also provide arbitrary options to the
338 @command{pintos} run by the tests with @option{PINTOSOPTS='@dots{}'},
339 e.g.@: @code{make check PINTOSOPTS='-j 1'} to select a jitter value of 1
340 (@pxref{Debugging versus Testing}).
342 All of the tests and related files are in @file{pintos/src/tests}.
343 Before we test your submission, we will replace the contents of that
344 directory by a pristine, unmodified copy, to ensure that the correct
345 tests are used. Thus, you can modify some of the tests if that helps in
346 debugging, but we will run the originals.
348 All software has bugs, so some of our tests may be flawed. If you think
349 a test failure is a bug in the test, not a bug in your code,
350 please point it out. We will look at it and fix it if necessary.
352 Please don't try to take advantage of our generosity in giving out our
353 test suite. Your code has to work properly in the general case, not
354 just for the test cases we supply. For example, it would be unacceptable
355 to explicitly base the kernel's behavior on the name of the running
356 test case. Such attempts to side-step the test cases will receive no
357 credit. If you think your solution may be in a gray area here, please
363 We will judge your design based on the design document and the source
364 code that you submit. We will read your entire design document and much
367 Don't forget that design quality, including the design document, is 50%
368 of your project grade. It
369 is better to spend one or two hours writing a good design document than
370 it is to spend that time getting the last 5% of the points for tests and
371 then trying to rush through writing the design document in the last 15
379 @node Design Document
380 @subsubsection Design Document
382 We provide a design document template for each project. For each
383 significant part of a project, the template asks questions in four
387 @item Data Structures
389 The instructions for this section are always the same:
392 Copy here the declaration of each new or changed @code{struct} or
393 @code{struct} member, global or static variable, @code{typedef}, or
394 enumeration. Identify the purpose of each in 25 words or less.
397 The first part is mechanical. Just copy new or modified declarations
398 into the design document, to highlight for us the actual changes to data
399 structures. Each declaration should include the comment that should
400 accompany it in the source code (see below).
402 We also ask for a very brief description of the purpose of each new or
403 changed data structure. The limit of 25 words or less is a guideline
404 intended to save your time and avoid duplication with later areas.
408 This is where you tell us how your code works, through questions that
409 probe your understanding of your code. We might not be able to easily
410 figure it out from the code, because many creative solutions exist for
411 most OS problems. Help us out a little.
413 Your answers should be at a level below the high level description of
414 requirements given in the assignment. We have read the assignment too,
415 so it is unnecessary to repeat or rephrase what is stated there. On the
416 other hand, your answers should be at a level above the low level of the
417 code itself. Don't give a line-by-line run-down of what your code does.
418 Instead, use your answers to explain how your code works to implement
421 @item Synchronization
423 An operating system kernel is a complex, multithreaded program, in which
424 synchronizing multiple threads can be difficult. This section asks
425 about how you chose to synchronize this particular type of activity.
429 Whereas the other sections primarily ask ``what'' and ``how,'' the
430 rationale section concentrates on ``why.'' This is where we ask you to
431 justify some design decisions, by explaining why the choices you made
432 are better than alternatives. You may be able to state these in terms
433 of time and space complexity, which can be made as rough or informal
434 arguments (formal language or proofs are unnecessary).
437 An incomplete, evasive, or non-responsive design document or one that
438 strays from the template without good reason may be penalized.
439 Incorrect capitalization, punctuation, spelling, or grammar can also
440 cost points. @xref{Project Documentation}, for a sample design document
441 for a fictitious project.
444 @subsubsection Source Code
446 Your design will also be judged by looking at your source code. We will
447 typically look at the differences between the original Pintos source
448 tree and your submission, based on the output of a command like
449 @code{diff -urpb pintos.orig pintos.submitted}. We will try to match up your
450 description of the design with the code submitted. Important
451 discrepancies between the description and the actual code will be
452 penalized, as will be any bugs we find by spot checks.
454 The most important aspects of source code design are those that specifically
455 relate to the operating system issues at stake in the project. For
456 example, the organization of an inode is an important part of file
457 system design, so in the file system project a poorly designed inode
458 would lose points. Other issues are much less important. For
459 example, multiple Pintos design problems call for a ``priority
460 queue,'' that is, a dynamic collection from which the minimum (or
461 maximum) item can quickly be extracted. Fast priority queues can be
462 implemented many ways, but we do not expect you to build a fancy data
463 structure even if it might improve performance. Instead, you are
464 welcome to use a linked list (and Pintos even provides one with
465 convenient functions for sorting and finding minimums and maximums).
467 Pintos is written in a consistent style. Make your additions and
468 modifications in existing Pintos source files blend in, not stick out.
469 In new source files, adopt the existing Pintos style by preference, but
470 make your code self-consistent at the very least. There should not be a
471 patchwork of different styles that makes it obvious that three different
472 people wrote the code. Use horizontal and vertical white space to make
473 code readable. Add a brief comment on every structure, structure
474 member, global or static variable, typedef, enumeration, and function
476 existing comments as you modify code. Don't comment out or use the
477 preprocessor to ignore blocks of code (instead, remove it entirely).
478 Use assertions to document key invariants. Decompose code into
479 functions for clarity. Code that is difficult to understand because it
480 violates these or other ``common sense'' software engineering practices
483 In the end, remember your audience. Code is written primarily to be
484 read by humans. It has to be acceptable to the compiler too, but the
485 compiler doesn't care about how it looks or how well it is written.
487 @node Legal and Ethical Issues
488 @section Legal and Ethical Issues
490 Pintos is distributed under a liberal license that allows free use,
491 modification, and distribution. Students and others who work on Pintos
492 own the code that they write and may use it for any purpose.
493 Pintos comes with NO WARRANTY, not even for MERCHANTABILITY or FITNESS
494 FOR A PARTICULAR PURPOSE.
495 @xref{License}, for details of the license and lack of warranty.
497 @localhonorcodepolicy{}
499 @node Acknowledgements
500 @section Acknowledgements
502 Pintos and this documentation were written by Ben Pfaff
503 @email{blp@@cs.stanford.edu}.
505 The GDB macros supplied with Pintos were written by Godmar Back
506 @email{gback@@cs.vt.edu}, and their documentation is adapted from his
509 The original structure and form of Pintos was inspired by the Nachos
510 instructional operating system from the University of California,
511 Berkeley (@bibref{Christopher}).
513 A few of the Pintos source files are derived from code used in the
514 Massachusetts Institute of Technology's 6.828 advanced operating systems
515 course. These files bear the original MIT license notice.
517 The Pintos projects and documentation originated with those designed for
518 Nachos by current and former CS 140 teaching assistants at Stanford
519 University, including at least Yu Ping, Greg Hutchins, Kelly Shaw, Paul
520 Twohey, Sameer Qureshi, and John Rector.
522 Example code for monitors (@pxref{Monitors}) is
523 from classroom slides originally by Dawson Engler and updated by Mendel
531 Pintos originated as a replacement for Nachos with a similar design.
532 Since then Pintos has greatly diverged from the Nachos design. Pintos
533 differs from Nachos in two important ways. First, Pintos runs on real
534 or simulated 80@var{x}86 hardware, but Nachos runs as a process on a
535 host operating system. Second, Pintos is written in C like most
536 real-world operating systems, but Nachos is written in C++.
538 Why the name ``Pintos''? First, like nachos, pinto beans are a common
539 Mexican food. Second, Pintos is small and a ``pint'' is a small amount.
540 Third, like drivers of the eponymous car, students are likely to have
541 trouble with blow-ups.