Implement a proper block layer with partition support.
[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, that is, the exact bytes loaded into
165 memory to run the Pintos kernel.  This is just @file{kernel.o} with
166 debug information stripped out, which saves a lot of space, which in
167 turn keeps the kernel from bumping up against a @w{512 kB} size limit
168 imposed by the kernel loader's design.
169
170 @item loader.bin
171 Memory image for the kernel loader, a small chunk of code written in
172 assembly language that reads the kernel from disk into memory and
173 starts it up.  It is exactly 512 bytes long, a size fixed by the
174 PC BIOS.
175 @end table
176
177 Subdirectories of @file{build} contain object files (@file{.o}) and
178 dependency files (@file{.d}), both produced by the compiler.  The
179 dependency files tell @command{make} which source files need to be
180 recompiled when other source or header files are changed.
181
182 @node Running Pintos
183 @subsection Running Pintos
184
185 We've supplied a program for conveniently running Pintos in a simulator,
186 called @command{pintos}.  In the simplest case, you can invoke
187 @command{pintos} as @code{pintos @var{argument}@dots{}}.  Each
188 @var{argument} is passed to the Pintos kernel for it to act on.
189
190 Try it out.  First @command{cd} into the newly created @file{build}
191 directory.  Then issue the command @code{pintos run alarm-multiple},
192 which passes the arguments @code{run alarm-multiple} to the Pintos
193 kernel.  In these arguments, @command{run} instructs the kernel to run a
194 test and @code{alarm-multiple} is the test to run.
195
196 This command creates a @file{bochsrc.txt} file, which is needed for
197 running Bochs, and then invoke Bochs.  Bochs opens a new window that
198 represents the simulated machine's display, and a BIOS message briefly
199 flashes.  Then Pintos boots and runs the @code{alarm-multiple} test
200 program, which outputs a few screenfuls of text.  When it's done, you
201 can close Bochs by clicking on the ``Power'' button in the window's top
202 right corner, or rerun the whole process by clicking on the ``Reset''
203 button just to its left.  The other buttons are not very useful for our
204 purposes.
205
206 (If no window appeared at all, then you're probably logged in remotely and X
207 forwarding is not set up correctly.  In this case, you can fix your X
208 setup, or you can use the @option{-v} option to disable X output:
209 @code{pintos -v -- run alarm-multiple}.)
210
211 The text printed by Pintos inside Bochs probably went by too quickly to
212 read.  However, you've probably noticed by now that the same text was
213 displayed in the terminal you used to run @command{pintos}.  This is
214 because Pintos sends all output both to the VGA display and to the first
215 serial port, and by default the serial port is connected to Bochs's
216 @code{stdin} and @code{stdout}.  You can log serial output to a file by
217 redirecting at the
218 command line, e.g.@: @code{pintos run alarm-multiple > logfile}.
219
220 The @command{pintos} program offers several options for configuring the
221 simulator or the virtual hardware.  If you specify any options, they
222 must precede the commands passed to the Pintos kernel and be separated
223 from them by @option{--}, so that the whole command looks like
224 @code{pintos @var{option}@dots{} -- @var{argument}@dots{}}.  Invoke
225 @code{pintos} without any arguments to see a list of available options.
226 Options can select a simulator to use: the default is Bochs, but
227 @option{--qemu} selects QEMU.  You can run the simulator
228 with a debugger (@pxref{GDB}).  You can set the amount of memory to give
229 the VM.  Finally, you can select how you want VM output to be displayed:
230 use @option{-v} to turn off the VGA display, @option{-t} to use your
231 terminal window as the VGA display instead of opening a new window
232 (Bochs only), or @option{-s} to suppress serial input from @code{stdin}
233 and output to @code{stdout}.
234
235 The Pintos kernel has commands and options other than @command{run}.
236 These are not very interesting for now, but you can see a list of them
237 using @option{-h}, e.g.@: @code{pintos -h}.
238
239 @node Debugging versus Testing
240 @subsection Debugging versus Testing
241
242 When you're debugging code, it's useful to be able to run a
243 program twice and have it do exactly the same thing.  On second and
244 later runs, you can make new observations without having to discard or
245 verify your old observations.  This property is called
246 ``reproducibility.''  One of the simulators that Pintos supports, Bochs,
247 can be set up for
248 reproducibility, and that's the way that @command{pintos} invokes it
249 by default.
250
251 Of course, a simulation can only be reproducible from one run to the
252 next if its input is the same each time.  For simulating an entire
253 computer, as we do, this means that every part of the computer must be
254 the same.  For example, you must use the same command-line argument, the
255 same disks, the same version
256 of Bochs, and you must not hit any keys on the keyboard (because you
257 could not be sure to hit them at exactly the same point each time)
258 during the runs.
259
260 While reproducibility is useful for debugging, it is a problem for
261 testing thread synchronization, an important part of most of the projects.  In
262 particular, when Bochs is set up for reproducibility, timer interrupts
263 will come at perfectly reproducible points, and therefore so will
264 thread switches.  That means that running the same test several times
265 doesn't give you any greater confidence in your code's correctness
266 than does running it only once.
267
268 So, to make your code easier to test, we've added a feature, called
269 ``jitter,'' to Bochs, that makes timer interrupts come at random
270 intervals, but in a perfectly predictable way.  In particular, if you
271 invoke @command{pintos} with the option @option{-j @var{seed}}, timer
272 interrupts will come at irregularly spaced intervals.  Within a single
273 @var{seed} value, execution will still be reproducible, but timer
274 behavior will change as @var{seed} is varied.  Thus, for the highest
275 degree of confidence you should test your code with many seed values.
276
277 On the other hand, when Bochs runs in reproducible mode, timings are not
278 realistic, meaning that a ``one-second'' delay may be much shorter or
279 even much longer than one second.  You can invoke @command{pintos} with
280 a different option, @option{-r}, to set up Bochs for realistic
281 timings, in which a one-second delay should take approximately one
282 second of real time.  Simulation in real-time mode is not reproducible,
283 and options @option{-j} and @option{-r} are mutually exclusive.
284
285 The QEMU simulator is available as an
286 alternative to Bochs (use @option{--qemu} when invoking
287 @command{pintos}).  The QEMU simulator is much faster than Bochs, but it
288 only supports real-time simulation and does not have a reproducible
289 mode.
290
291 @node Grading
292 @section Grading
293
294 We will grade your assignments based on test results and design quality,
295 each of which comprises 50% of your grade.
296
297 @menu
298 * Testing::                     
299 * Design::                      
300 @end menu
301
302 @node Testing
303 @subsection Testing
304
305 Your test result grade will be based on our tests.  Each project has
306 several tests, each of which has a name beginning with @file{tests}.
307 To completely test your submission, invoke @code{make check} from the
308 project @file{build} directory.  This will build and run each test and
309 print a ``pass'' or ``fail'' message for each one.  When a test fails,
310 @command{make check} also prints some details of the reason for failure.
311 After running all the tests, @command{make check} also prints a summary
312 of the test results.
313
314 For project 1, the tests will probably run faster in Bochs.  For the
315 rest of the projects, they will run much faster in QEMU.
316 @command{make check} will select the faster simulator by default, but
317 you can override its choice by specifying @option{SIMULATOR=--bochs} or
318 @option{SIMULATOR=--qemu} on the @command{make} command line.
319
320 You can also run individual tests one at a time.  A given test @var{t}
321 writes its output to @file{@var{t}.output}, then a script scores the
322 output as ``pass'' or ``fail'' and writes the verdict to
323 @file{@var{t}.result}.  To run and grade a single test, @command{make}
324 the @file{.result} file explicitly from the @file{build} directory, e.g.@:
325 @code{make tests/threads/alarm-multiple.result}.  If @command{make} says
326 that the test result is up-to-date, but you want to re-run it anyway,
327 either run @code{make clean} or delete the @file{.output} file by hand.
328
329 By default, each test provides feedback only at completion, not during
330 its run.  If you prefer, you can observe the progress of each test by
331 specifying @option{VERBOSE=1} on the @command{make} command line, as in
332 @code{make check VERBOSE=1}.  You can also provide arbitrary options to the
333 @command{pintos} run by the tests with @option{PINTOSOPTS='@dots{}'},
334 e.g.@: @code{make check PINTOSOPTS='-j 1'} to select a jitter value of 1
335 (@pxref{Debugging versus Testing}).
336
337 All of the tests and related files are in @file{pintos/src/tests}.
338 Before we test your submission, we will replace the contents of that
339 directory by a pristine, unmodified copy, to ensure that the correct
340 tests are used.  Thus, you can modify some of the tests if that helps in
341 debugging, but we will run the originals.
342
343 All software has bugs, so some of our tests may be flawed.  If you think
344 a test failure is a bug in the test, not a bug in your code,
345 please point it out.  We will look at it and fix it if necessary.
346
347 Please don't try to take advantage of our generosity in giving out our
348 test suite.  Your code has to work properly in the general case, not
349 just for the test cases we supply.  For example, it would be unacceptable
350 to explicitly base the kernel's behavior on the name of the running
351 test case.  Such attempts to side-step the test cases will receive no
352 credit.  If you think your solution may be in a gray area here, please
353 ask us about it.
354
355 @node Design
356 @subsection Design
357
358 We will judge your design based on the design document and the source
359 code that you submit.  We will read your entire design document and much
360 of your source code.  
361
362 Don't forget that design quality, including the design document, is 50%
363 of your project grade.  It
364 is better to spend one or two hours writing a good design document than
365 it is to spend that time getting the last 5% of the points for tests and
366 then trying to rush through writing the design document in the last 15
367 minutes.
368
369 @menu
370 * Design Document::             
371 * Source Code::                 
372 @end menu
373
374 @node Design Document
375 @subsubsection Design Document
376
377 We provide a design document template for each project.  For each
378 significant part of a project, the template asks questions in four
379 areas: 
380
381 @table @strong
382 @item Data Structures
383
384 The instructions for this section are always the same:
385
386 @quotation
387 Copy here the declaration of each new or changed @code{struct} or
388 @code{struct} member, global or static variable, @code{typedef}, or
389 enumeration.  Identify the purpose of each in 25 words or less.
390 @end quotation
391
392 The first part is mechanical.  Just copy new or modified declarations
393 into the design document, to highlight for us the actual changes to data
394 structures.  Each declaration should include the comment that should
395 accompany it in the source code (see below).
396
397 We also ask for a very brief description of the purpose of each new or
398 changed data structure.  The limit of 25 words or less is a guideline
399 intended to save your time and avoid duplication with later areas.
400
401 @item Algorithms
402
403 This is where you tell us how your code works, through questions that
404 probe your understanding of your code.  We might not be able to easily
405 figure it out from the code, because many creative solutions exist for
406 most OS problems.  Help us out a little.
407
408 Your answers should be at a level below the high level description of
409 requirements given in the assignment.  We have read the assignment too,
410 so it is unnecessary to repeat or rephrase what is stated there.  On the
411 other hand, your answers should be at a level above the low level of the
412 code itself.  Don't give a line-by-line run-down of what your code does.
413 Instead, use your answers to explain how your code works to implement
414 the requirements.
415
416 @item Synchronization
417
418 An operating system kernel is a complex, multithreaded program, in which
419 synchronizing multiple threads can be difficult.  This section asks
420 about how you chose to synchronize this particular type of activity.
421
422 @item Rationale
423
424 Whereas the other sections primarily ask ``what'' and ``how,'' the
425 rationale section concentrates on ``why.''  This is where we ask you to
426 justify some design decisions, by explaining why the choices you made
427 are better than alternatives.  You may be able to state these in terms
428 of time and space complexity, which can be made as rough or informal
429 arguments (formal language or proofs are unnecessary).
430 @end table
431
432 An incomplete, evasive, or non-responsive design document or one that
433 strays from the template without good reason may be penalized.
434 Incorrect capitalization, punctuation, spelling, or grammar can also
435 cost points.  @xref{Project Documentation}, for a sample design document
436 for a fictitious project.
437
438 @node Source Code
439 @subsubsection Source Code
440
441 Your design will also be judged by looking at your source code.  We will
442 typically look at the differences between the original Pintos source
443 tree and your submission, based on the output of a command like
444 @code{diff -urpb pintos.orig pintos.submitted}.  We will try to match up your
445 description of the design with the code submitted.  Important
446 discrepancies between the description and the actual code will be
447 penalized, as will be any bugs we find by spot checks.
448
449 The most important aspects of source code design are those that specifically
450 relate to the operating system issues at stake in the project.  For
451 example, the organization of an inode is an important part of file
452 system design, so in the file system project a poorly designed inode
453 would lose points.  Other issues are much less important.  For
454 example, multiple Pintos design problems call for a ``priority
455 queue,'' that is, a dynamic collection from which the minimum (or
456 maximum) item can quickly be extracted.  Fast priority queues can be
457 implemented many ways, but we do not expect you to build a fancy data
458 structure even if it might improve performance.  Instead, you are
459 welcome to use a linked list (and Pintos even provides one with
460 convenient functions for sorting and finding minimums and maximums).
461
462 Pintos is written in a consistent style.  Make your additions and
463 modifications in existing Pintos source files blend in, not stick out.
464 In new source files, adopt the existing Pintos style by preference, but
465 make your code self-consistent at the very least.  There should not be a
466 patchwork of different styles that makes it obvious that three different
467 people wrote the code.  Use horizontal and vertical white space to make
468 code readable.  Add a brief comment on every structure, structure
469 member, global or static variable, typedef, enumeration, and function
470 definition.  Update
471 existing comments as you modify code.  Don't comment out or use the
472 preprocessor to ignore blocks of code (instead, remove it entirely).
473 Use assertions to document key invariants.  Decompose code into
474 functions for clarity.  Code that is difficult to understand because it
475 violates these or other ``common sense'' software engineering practices
476 will be penalized.
477
478 In the end, remember your audience.  Code is written primarily to be
479 read by humans.  It has to be acceptable to the compiler too, but the
480 compiler doesn't care about how it looks or how well it is written.
481
482 @node Legal and Ethical Issues
483 @section Legal and Ethical Issues
484
485 Pintos is distributed under a liberal license that allows free use,
486 modification, and distribution.  Students and others who work on Pintos
487 own the code that they write and may use it for any purpose.
488 Pintos comes with NO WARRANTY, not even for MERCHANTABILITY or FITNESS
489 FOR A PARTICULAR PURPOSE.
490 @xref{License}, for details of the license and lack of warranty.
491
492 @localhonorcodepolicy{}
493
494 @node Acknowledgements
495 @section Acknowledgements
496
497 The Pintos core and this documentation were originally written by Ben
498 Pfaff @email{blp@@cs.stanford.edu}.
499
500 Additional features were contributed by Anthony Romano
501 @email{chz@@vt.edu}.
502
503 The GDB macros supplied with Pintos were written by Godmar Back
504 @email{gback@@cs.vt.edu}, and their documentation is adapted from his
505 work.
506
507 The original structure and form of Pintos was inspired by the Nachos
508 instructional operating system from the University of California,
509 Berkeley (@bibref{Christopher}).
510
511 The Pintos projects and documentation originated with those designed for
512 Nachos by current and former CS 140 teaching assistants at Stanford
513 University, including at least Yu Ping, Greg Hutchins, Kelly Shaw, Paul
514 Twohey, Sameer Qureshi, and John Rector.
515
516 Example code for monitors (@pxref{Monitors}) is
517 from classroom slides originally by Dawson Engler and updated by Mendel
518 Rosenblum.
519
520 @localcredits{}
521
522 @node Trivia
523 @section Trivia
524
525 Pintos originated as a replacement for Nachos with a similar design.
526 Since then Pintos has greatly diverged from the Nachos design.  Pintos
527 differs from Nachos in two important ways.  First, Pintos runs on real
528 or simulated 80@var{x}86 hardware, but Nachos runs as a process on a
529 host operating system.  Second, Pintos is written in C like most
530 real-world operating systems, but Nachos is written in C++.
531
532 Why the name ``Pintos''?  First, like nachos, pinto beans are a common
533 Mexican food.  Second, Pintos is small and a ``pint'' is a small amount.
534 Third, like drivers of the eponymous car, students are likely to have
535 trouble with blow-ups.