6601adccbb1c65536b3cda1d976bb52724823c6b
[pintos-anon] / doc / intro.texi
1 @node Introduction
2 @chapter Introduction
3
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.
10
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}.
21
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.
29
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
32 projects.
33
34 @menu
35 * Getting Started::             
36 * Grading::                     
37 * Legal and Ethical Issues::    
38 * Acknowledgements::            
39 * Trivia::                      
40 @end menu
41
42 @node Getting Started
43 @section Getting Started
44
45 To get started, you'll have to log into a machine that Pintos can be
46 built on.  
47 @localmachines{}
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}).
52
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}
55 environment.  
56 @localpathsetup{}
57
58 @menu
59 * Source Tree Overview::        
60 * Building Pintos::             
61 * Running Pintos::              
62 * Debugging versus Testing::    
63 @end menu
64
65 @node Source Tree Overview
66 @subsection Source Tree Overview
67
68 Now you can extract the source for Pintos into a directory named
69 @file{pintos/src}, by executing
70 @example
71 zcat @value{localpintostarpath} | tar x
72 @end example
73 Alternatively, fetch
74 @uref{@value{localpintoshttppath}}
75 and extract it in a similar way.
76
77 Let's take a look at what's inside.  Here's the directory structure
78 that you should see in @file{pintos/src}:
79
80 @table @file
81 @item threads/
82 Source code for the base kernel, which you will modify starting in
83 project 1.
84
85 @item userprog/
86 Source code for the user program loader, which you will modify
87 starting with project 2.
88
89 @item vm/
90 An almost empty directory.  You will implement virtual memory here in
91 project 3.
92
93 @item filesys/
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.
96
97 @item devices/
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.
101
102 @item lib/
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
108 modify this code.
109
110 @item lib/kernel/
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{}>}
116 notation.
117
118 @item lib/user/
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.
122
123 @item tests/
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
126 the tests.
127
128 @item examples/
129 Example user programs for use starting with project 2.
130
131 @item misc/
132 @itemx utils/
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.
135 @end table
136
137 @node Building Pintos
138 @subsection Building Pintos
139
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.
146
147 @localcrossbuild{}
148
149 Following the build, the following are the interesting files in the
150 @file{build} directory:
151
152 @table @file
153 @item Makefile
154 A copy of @file{pintos/src/Makefile.build}.  It describes how to build
155 the kernel.  @xref{Adding Source Files}, for more information.
156
157 @item kernel.o
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.
162
163 @item kernel.bin
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
167 size.
168
169 @item loader.bin
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
173 PC BIOS.
174
175 @item os.dsk
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
178 simulator.
179 @end table
180
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.
185
186 @node Running Pintos
187 @subsection Running Pintos
188
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.
193
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.
199
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
208 purposes.
209
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}.)
215
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
222 redirecting at the
223 command line, e.g.@: @code{pintos run alarm-multiple > logfile}.
224
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}.
239
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}.
243
244 @node Debugging versus Testing
245 @subsection Debugging versus Testing
246
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,
252 can be set up for
253 reproducibility, and that's the way that @command{pintos} invokes it
254 by default.
255
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)
263 during the runs.
264
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.
272
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.
281
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.
289
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
294 mode.
295
296 @node Grading
297 @section Grading
298
299 We will grade your assignments based on test results and design quality,
300 each of which comprises 50% of your grade.
301
302 @menu
303 * Testing::                     
304 * Design::                      
305 @end menu
306
307 @node Testing
308 @subsection Testing
309
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
317 of the test results.
318
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.
324
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.
333
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}).
341
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.
347
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.
351
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
358 ask us about it.
359
360 @node Design
361 @subsection Design
362
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
365 of your source code.  
366
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
372 minutes.
373
374 @menu
375 * Design Document::             
376 * Source Code::                 
377 @end menu
378
379 @node Design Document
380 @subsubsection Design Document
381
382 We provide a design document template for each project.  For each
383 significant part of a project, the template asks questions in four
384 areas: 
385
386 @table @strong
387 @item Data Structures
388
389 The instructions for this section are always the same:
390
391 @quotation
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.
395 @end quotation
396
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).
401
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.
405
406 @item Algorithms
407
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.
412
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
419 the requirements.
420
421 @item Synchronization
422
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.
426
427 @item Rationale
428
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).
435 @end table
436
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.
442
443 @node Source Code
444 @subsubsection Source Code
445
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.
453
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).
466
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
475 definition.  Update
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
481 will be penalized.
482
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.
486
487 @node Legal and Ethical Issues
488 @section Legal and Ethical Issues
489
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.
496
497 @localhonorcodepolicy{}
498
499 @node Acknowledgements
500 @section Acknowledgements
501
502 The Pintos core and this documentation were originally written by Ben
503 Pfaff @email{blp@@cs.stanford.edu}.
504
505 Additional features were contributed by Anthony Romano
506 @email{chz@@vt.edu}.
507
508 The GDB macros supplied with Pintos were written by Godmar Back
509 @email{gback@@cs.vt.edu}, and their documentation is adapted from his
510 work.
511
512 The original structure and form of Pintos was inspired by the Nachos
513 instructional operating system from the University of California,
514 Berkeley (@bibref{Christopher}).
515
516 A few of the Pintos source files are derived from code used in the
517 Massachusetts Institute of Technology's 6.828 advanced operating systems
518 course.  These files bear the original MIT license notice.
519
520 The Pintos projects and documentation originated with those designed for
521 Nachos by current and former CS 140 teaching assistants at Stanford
522 University, including at least Yu Ping, Greg Hutchins, Kelly Shaw, Paul
523 Twohey, Sameer Qureshi, and John Rector.
524
525 Example code for monitors (@pxref{Monitors}) is
526 from classroom slides originally by Dawson Engler and updated by Mendel
527 Rosenblum.
528
529 @localcredits{}
530
531 @node Trivia
532 @section Trivia
533
534 Pintos originated as a replacement for Nachos with a similar design.
535 Since then Pintos has greatly diverged from the Nachos design.  Pintos
536 differs from Nachos in two important ways.  First, Pintos runs on real
537 or simulated 80@var{x}86 hardware, but Nachos runs as a process on a
538 host operating system.  Second, Pintos is written in C like most
539 real-world operating systems, but Nachos is written in C++.
540
541 Why the name ``Pintos''?  First, like nachos, pinto beans are a common
542 Mexican food.  Second, Pintos is small and a ``pint'' is a small amount.
543 Third, like drivers of the eponymous car, students are likely to have
544 trouble with blow-ups.