Add support for "keyboard" input over the serial port.
[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 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, ,
21 VMware GSX Server}.
22
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.
30
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
33 projects.
34
35 @menu
36 * Getting Started::             
37 * Grading::                     
38 * License::                     
39 * Acknowledgements::            
40 * Trivia::                      
41 @end menu
42
43 @node Getting Started
44 @section Getting Started
45
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.
55
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
61 logged into.}
62 @example
63 set path = ( /usr/class/cs140/`uname -m`/bin $path )
64 @end example
65 @noindent
66 @strong{Notice that both @samp{`} are left single quotes or
67 ``backticks,'' not apostrophes (@samp{'}).}
68 It is a good idea to add this line to the @file{.cshrc} file
69 in your home directory.  Otherwise, you'll have to type it every time
70 you log in.
71
72 @menu
73 * Source Tree Overview::        
74 * Building Pintos::             
75 * Running Pintos::              
76 * Debugging versus Testing::    
77 @end menu
78
79 @node Source Tree Overview
80 @subsection Source Tree Overview
81
82 Now you can extract the source for Pintos into a directory named
83 @file{pintos/src}, by executing
84 @example
85 tar xzf /usr/class/cs140/pintos/pintos.tar.gz
86 @end example
87 Alternatively, fetch
88 @uref{http://@/www.stanford.edu/@/class/@/cs140/@/pintos/@/pintos.@/tar.gz}
89 and extract it in a similar way.
90
91 Let's take a look at what's inside.  Here's the directory structure
92 that you should see in @file{pintos/src}:
93
94 @table @file
95 @item threads/
96 Source code for the base kernel, which you will modify starting in
97 project 1.
98
99 @item userprog/
100 Source code for the user program loader, which you will modify
101 starting with project 2.
102
103 @item vm/
104 An almost empty directory.  You will implement virtual memory here in
105 project 3.
106
107 @item filesys/
108 Source code for a basic file system.  You will use this file system
109 starting with project 2, but you will not modify it until project 4.
110
111 @item devices/
112 Source code for I/O device interfacing: keyboard, timer, disk, etc.
113 You will modify the timer implementation in project 1.  Otherwise
114 you should have no need to change this code.
115
116 @item lib/
117 An implementation of a subset of the standard C library.  The code in
118 this directory is compiled into both the Pintos kernel and, starting
119 from project 2, user programs that run under it.  In both kernel code
120 and user programs, headers in this directory can be included using the
121 @code{#include <@dots{}>} notation.  You should have little need to
122 modify this code.
123
124 @item lib/kernel/
125 Parts of the C library that are included only in the Pintos kernel.
126 This also includes implementations of some data types that you are
127 free to use in your kernel code: bitmaps, doubly linked lists, and
128 hash tables.  In the kernel, headers in this
129 directory can be included using the @code{#include <@dots{}>}
130 notation.
131
132 @item lib/user/
133 Parts of the C library that are included only in Pintos user programs.
134 In user programs, headers in this directory can be included using the
135 @code{#include <@dots{}>} notation.
136
137 @item tests/
138 Tests for each project.  You can modify this code if it helps you test
139 your submission, but we will replace it with the originals before we run
140 the tests.
141
142 @item examples/
143 Example user programs for use starting with project 2.
144
145 @item misc/
146 @itemx utils/
147 These files may come in handy if you decide to try working with Pintos
148 away from the ITSS machines.  Otherwise, you can ignore them.
149 @end table
150
151 @node Building Pintos
152 @subsection Building Pintos
153
154 As the next step, build the source code supplied for
155 the first project.  First, @command{cd} into the @file{threads}
156 directory.  Then, issue the @samp{make} command.  This will create a
157 @file{build} directory under @file{threads}, populate it with a
158 @file{Makefile} and a few subdirectories, and then build the kernel
159 inside.  The entire build should take less than 30 seconds.
160
161 Watch the commands executed during the build.  On the Linux machines,
162 the ordinary system tools are used.  On a SPARC machine, special build
163 tools are used, whose names begin with @samp{i386-elf-}, e.g.@:
164 @code{i386-elf-gcc}, @code{i386-elf-ld}.  These are ``cross-compiler''
165 tools.  That is, the build is running on a SPARC machine (called the
166 @dfn{host}), but the result will run on a simulated 80@var{x}86 machine
167 (called the @dfn{target}).  The @samp{i386-elf-@var{program}} tools are
168 specially built for this configuration.
169
170 Following the build, the following are the interesting files in the
171 @file{build} directory:
172
173 @table @file
174 @item Makefile
175 A copy of @file{pintos/src/Makefile.build}.  It describes how to build
176 the kernel.  @xref{Adding Source Files}, for more information.
177
178 @item kernel.o
179 Object file for the entire kernel.  This is the result of linking
180 object files compiled from each individual kernel source file into a
181 single object file.  It contains debug information, so you can run
182 GDB (@pxref{GDB}) or @command{backtrace} (@pxref{Backtraces}) on it.
183
184 @item kernel.bin
185 Memory image of the kernel.  These are the exact bytes loaded into
186 memory to run the Pintos kernel.  To simplify loading, it is always
187 padded out with zero bytes up to an exact multiple of 4 kB in
188 size.
189
190 @item loader.bin
191 Memory image for the kernel loader, a small chunk of code written in
192 assembly language that reads the kernel from disk into memory and
193 starts it up.  It is exactly 512 bytes long, a size fixed by the
194 PC BIOS.
195
196 @item os.dsk
197 Disk image for the kernel, which is just @file{loader.bin} followed by
198 @file{kernel.bin}.  This file is used as a ``virtual disk'' by the
199 simulator.
200 @end table
201
202 Subdirectories of @file{build} contain object files (@file{.o}) and
203 dependency files (@file{.d}), both produced by the compiler.  The
204 dependency files tell @command{make} which source files need to be
205 recompiled when other source or header files are changed.
206
207 @node Running Pintos
208 @subsection Running Pintos
209
210 We've supplied a program for conveniently running Pintos in a simulator,
211 called @command{pintos}.  In the simplest case, you can invoke
212 @command{pintos} as @code{pintos @var{argument}@dots{}}.  Each
213 @var{argument} is passed to the Pintos kernel for it to act on.
214
215 Try it out.  First @command{cd} into the newly created @file{build}
216 directory.  Then issue the command @code{pintos run alarm-multiple},
217 which passes the arguments @code{run alarm-multiple} to the Pintos
218 kernel.  In these arguments, @command{run} instructs the kernel to run a
219 test and @code{alarm-multiple} is the test to run.
220
221 This command creates a @file{bochsrc.txt} file, which is needed for
222 running Bochs, and then invoke Bochs.  Bochs opens a new window that
223 represents the simulated machine's display, and a BIOS message briefly
224 flashes.  Then Pintos boots and runs the @code{alarm-multiple} test
225 program, which outputs a few screenfuls of text.  When it's done, you
226 can close Bochs by clicking on the ``Power'' button in the window's top
227 right corner, or rerun the whole process by clicking on the ``Reset''
228 button just to its left.  The other buttons are not very useful for our
229 purposes.
230
231 (If no window appeared at all, and you just got a terminal full of
232 corrupt-looking text, then you're probably logged in remotely and X
233 forwarding is not set up correctly.  In this case, you can fix your X
234 setup, or you can use the @option{-v} option to disable X output:
235 @code{pintos -v -- run alarm-multiple}.)
236
237 The text printed by Pintos inside Bochs probably went by too quickly to
238 read.  However, you've probably noticed by now that the same text was
239 displayed in the terminal you used to run @command{pintos}.  This is
240 because Pintos sends all output both to the VGA display and to the first
241 serial port, and by default the serial port is connected to Bochs's
242 @code{stdin} and @code{stdout}.  You can log serial output to a file by
243 redirecting at the
244 command line, e.g.@: @code{pintos run alarm-multiple > logfile}.
245
246 The @command{pintos} program offers several options for configuring the
247 simulator or the virtual hardware.  If you specify any options, they
248 must precede the commands passed to the Pintos kernel and be separated
249 from them by @option{--}, so that the whole command looks like
250 @code{pintos @var{option}@dots{} -- @var{argument}@dots{}}.  Invoke
251 @code{pintos} without any arguments to see a list of available options.
252 Options can select a simulator to use: the default is Bochs, but
253 @option{--qemu} selects qemu.  You can run the simulator
254 with a debugger (@pxref{GDB}).  You can set the amount of memory to give
255 the VM.  Finally, you can select how you want VM output to be displayed:
256 use @option{-v} to turn off the VGA display, @option{-t} to use your
257 terminal window as the VGA display instead of opening a new window
258 (Bochs only), or @option{-s} to suppress serial input from @code{stdin}
259 and output to @code{stdout}.
260
261 The Pintos kernel has commands and options other than @command{run}.
262 These are not very interesting for now, but you can see a list of them
263 using @option{-h}, e.g.@: @code{pintos -h}.
264
265 @node Debugging versus Testing
266 @subsection Debugging versus Testing
267
268 When you're debugging code, it's useful to be able to run a
269 program twice and have it do exactly the same thing.  On second and
270 later runs, you can make new observations without having to discard or
271 verify your old observations.  This property is called
272 ``reproducibility.''  One of the simulators that Pintos supports, Bochs,
273 can be set up for
274 reproducibility, and that's the way that @command{pintos} invokes it
275 by default.
276
277 Of course, a simulation can only be reproducible from one run to the
278 next if its input is the same each time.  For simulating an entire
279 computer, as we do, this means that every part of the computer must be
280 the same.  For example, you must use the same command-line argument, the
281 same disks, the same version
282 of Bochs, and you must not hit any keys on the keyboard (because you
283 could not be sure to hit them at exactly the same point each time)
284 during the runs.
285
286 While reproducibility is useful for debugging, it is a problem for
287 testing thread synchronization, an important part of most of the projects.  In
288 particular, when Bochs is set up for reproducibility, timer interrupts
289 will come at perfectly reproducible points, and therefore so will
290 thread switches.  That means that running the same test several times
291 doesn't give you any greater confidence in your code's correctness
292 than does running it only once.
293
294 So, to make your code easier to test, we've added a feature, called
295 ``jitter,'' to Bochs, that makes timer interrupts come at random
296 intervals, but in a perfectly predictable way.  In particular, if you
297 invoke @command{pintos} with the option @option{-j @var{seed}}, timer
298 interrupts will come at irregularly spaced intervals.  Within a single
299 @var{seed} value, execution will still be reproducible, but timer
300 behavior will change as @var{seed} is varied.  Thus, for the highest
301 degree of confidence you should test your code with many seed values.
302
303 On the other hand, when Bochs runs in reproducible mode, timings are not
304 realistic, meaning that a ``one-second'' delay may be much shorter or
305 even much longer than one second.  You can invoke @command{pintos} with
306 a different option, @option{-r}, to set up Bochs for realistic
307 timings, in which a one-second delay should take approximately one
308 second of real time.  Simulation in real-time mode is not reproducible,
309 and options @option{-j} and @option{-r} are mutually exclusive.
310
311 The qemu simulator is available as an
312 alternative to Bochs (use @option{--qemu} when invoking
313 @command{pintos}).  The qemu simulator is much faster than Bochs, but it
314 only supports real-time simulation and does not have a reproducible
315 mode.
316
317 @node Grading
318 @section Grading
319
320 We will grade your assignments based on test results and design quality,
321 each of which comprises 50% of your grade.
322
323 @menu
324 * Testing::                     
325 * Design::                      
326 @end menu
327
328 @node Testing
329 @subsection Testing
330
331 Your test result grade will be based on our tests.  Each project has
332 several tests, each of which has a name beginning with @file{tests}.
333 To completely test your submission, invoke @code{make check} from the
334 project @file{build} directory.  This will build and run each test and
335 print a ``pass'' or ``fail'' message for each one.  When a test fails,
336 @command{make check} also prints some details of the reason for failure.
337 After running all the tests, @command{make check} also prints a summary
338 of the test results.
339
340 For project 1, the tests will probably run faster in Bochs.  For the
341 rest of the projects, they will run much faster in qemu.
342 @command{make check} will select the faster simulator by default, but
343 you can override its choice by specifying @option{SIMULATOR=--bochs} or
344 @option{SIMULATOR=--qemu} on the @command{make} command line.
345
346 You can also run individual tests one at a time.  A given test @var{t}
347 writes its output to @file{@var{t}.output}, then a script scores the
348 output as ``pass'' or ``fail'' and writes the verdict to
349 @file{@var{t}.result}.  To run and grade a single test, @command{make}
350 the @file{.result} file explicitly from the @file{build} directory, e.g.@:
351 @code{make tests/threads/alarm-multiple.result}.  If @command{make} says
352 that the test result is up-to-date, but you want to re-run it anyway,
353 either run @code{make clean} or delete the @file{.output} file by hand.
354
355 By default, each test provides feedback only at completion, not during
356 its run.  If you prefer, you can observe the progress of each test by
357 specifying @option{VERBOSE=1} on the @command{make} command line, as in
358 @code{make check VERBOSE=1}.  You can also provide arbitrary options to the
359 @command{pintos} run by the tests with @option{PINTOSOPTS='@dots{}'},
360 e.g.@: @code{make check PINTOSOPTS='-j 1'} to select a jitter value of 1
361 (@pxref{Debugging Versus Testing}).
362
363 All of the tests and related files are in @file{pintos/src/tests}.
364 Before we test your submission, we will replace the contents of that
365 directory by a pristine, unmodified copy, to ensure that the correct
366 tests are used.  Thus, you can modify some of the tests if that helps in
367 debugging, but we will run the originals.
368
369 All software has bugs, so some of our tests may be flawed.  If you think
370 a test failure is a bug in the test, not a bug in your code,
371 please point it out.  We will look at it and fix it if necessary.
372
373 Please don't try to take advantage of our generosity in giving out our
374 test suite.  Your code has to work properly in the general case, not
375 just for the test cases we supply.  For example, it would be unacceptable
376 to explicitly base the kernel's behavior on the name of the running
377 test case.  Such attempts to side-step the test cases will receive no
378 credit.  If you think your solution may be in a gray area here, please
379 ask us about it.
380
381 @node Design
382 @subsection Design
383
384 We will judge your design based on the design document and the source
385 code that you submit.  We will read your entire design document and much
386 of your source code.  
387
388 Don't forget that the design document is 50% of your project grade.  It
389 is better to spend one or two hours writing a good design document than
390 it is to spend that time getting the last 5% of the points for tests and
391 then trying to rush through writing the design document in the last 15
392 minutes.
393
394 @menu
395 * Design Document::             
396 * Source Code::                 
397 @end menu
398
399 @node Design Document
400 @subsubsection Design Document
401
402 We provide a design document template for each project.  For each
403 significant part of a project, the template asks questions in four
404 areas: 
405
406 @table @strong
407 @item Data Structures
408
409 The instructions for this section are always the same:
410
411 @quotation
412 Copy here the declaration of each new or changed @code{struct} or
413 @code{struct} member, global or static variable, @code{typedef}, or
414 enumeration.  Identify the purpose of each in 25 words or less.
415 @end quotation
416
417 The first part is mechanical.  Just copy new or modified declarations
418 into the design document, to highlight for us the actual changes to data
419 structures.  Each declaration should include the comment that should
420 accompany it in the source code (see below).
421
422 We also ask for a very brief description of the purpose of each new or
423 changed data structure.  The limit of 25 words or less is a guideline
424 intended to save your time and avoid duplication with later areas.
425
426 @item Algorithms
427
428 This is where you tell us how your code works, through questions that
429 probe your understanding of your code.  We might not be able to easily
430 figure it out from the code, because many creative solutions exist for
431 most OS problems.  Help us out a little.
432
433 Your answers should be at a level below the high level description of
434 requirements given in the assignment.  We have read the assignment too,
435 so it is unnecessary to repeat or rephrase what is stated there.  On the
436 other hand, your answers should be at a level above the low level of the
437 code itself.  Don't give a line-by-line run-down of what your code does.
438 Instead, use your answers to explain how your code works to implement
439 the requirements.
440
441 @item Synchronization
442
443 An operating system kernel is a complex, multithreaded program, in which
444 synchronizing multiple threads can be difficult.  This section asks
445 about how you chose to synchronize this particular type of activity.
446
447 @item Rationale
448
449 Whereas the other sections primarily ask ``what'' and ``how,'' the
450 rationale section concentrates on ``why.''  This is where we ask you to
451 justify some design decisions, by explaining why the choices you made
452 are better than alternatives.  You may be able to state these in terms
453 of time and space complexity, which can be made as rough or informal
454 arguments (formal language or proofs are unnecessary).
455 @end table
456
457 An incomplete, evasive, or non-responsive design document or one that
458 strays from the template without good reason may be penalized.
459 Incorrect capitalization, punctuation, spelling, or grammar can also
460 cost points.  @xref{Project Documentation}, for a sample design document
461 for a fictitious project.
462
463 @node Source Code
464 @subsubsection Source Code
465
466 Your design will also be judged by looking at your source code.  We will
467 typically look at the differences between the original Pintos source
468 tree and your submission, based on the output of a command like
469 @code{diff -urpb pintos.orig pintos.submitted}.  We will try to match up your
470 description of the design with the code submitted.  Important
471 discrepancies between the description and the actual code will be
472 penalized, as will be any bugs we find by spot checks.
473
474 The most important aspects of source code design are those that specifically
475 relate to the operating system issues at stake in the project.  For
476 example, the organization of an inode is an important part of file
477 system design, so in the file system project a poorly designed inode
478 would lose points.  Other issues are much less important.  For
479 example, multiple Pintos design problems call for a ``priority
480 queue,'' that is, a dynamic collection from which the minimum (or
481 maximum) item can quickly be extracted.  Fast priority queues can be
482 implemented many ways, but we do not expect you to build a fancy data
483 structure even if it might improve performance.  Instead, you are
484 welcome to use a linked list (and Pintos even provides one with
485 convenient functions for sorting and finding minimums and maximums).
486
487 Pintos is written in a consistent style.  Make your additions and
488 modifications in existing Pintos source files blend in, not stick out.
489 In new source files, adopt the existing Pintos style by preference, but
490 make your code self-consistent at the very least.  There should not be a
491 patchwork of different styles that makes it obvious that three different
492 people wrote the code.  Use horizontal and vertical white space to make
493 code readable.  Add a brief comment on every structure, structure
494 member, global or static variable, and function definition.  Update
495 existing comments as you modify code.  Don't comment out or use the
496 preprocessor to ignore blocks of code (instead, remove it entirely).
497 Use assertions to document key invariants.  Decompose code into
498 functions for clarity.  Code that is difficult to understand because it
499 violates these or other ``common sense'' software engineering practices
500 will be penalized.
501
502 In the end, remember your audience.  Code is written primarily to be
503 read by humans.  It has to be acceptable to the compiler too, but the
504 compiler doesn't care about how it looks or how well it is written.
505
506 @node License
507 @section License
508
509 Pintos is distributed under a liberal license that allows free use,
510 modification, and distribution.  Students and others who work on Pintos
511 own the code that they write and may use it for any purpose.
512
513 In the context of Stanford's CS 140 course, please respect the spirit
514 and the letter of the honor code by refraining from reading any homework
515 solutions available online or elsewhere.  Reading the source code for
516 other operating system kernels, such as Linux or FreeBSD, is allowed,
517 but do not copy code from them literally.  Please cite the code that
518 inspired your own in your design documentation.
519
520 Pintos comes with NO WARRANTY, not even for MERCHANTABILITY or FITNESS
521 FOR A PARTICULAR PURPOSE.
522
523 The @file{LICENSE} file at the top level of the Pintos source
524 distribution has full details of the license and lack of warranty.
525
526 @node Acknowledgements
527 @section Acknowledgements
528
529 Pintos and this documentation were written by Ben Pfaff
530 @email{blp@@cs.stanford.edu}.
531
532 The GDB macros supplied with Pintos were written by Godmar Back
533 @email{gback@@cs.vt.edu}, and their documentation is adapted from his
534 work.
535
536 The original structure and form of Pintos was inspired by the Nachos
537 instructional operating system from the University of California,
538 Berkeley.
539
540 A few of the Pintos source files are derived from code used in the
541 Massachusetts Institute of Technology's 6.828 advanced operating systems
542 course.  These files bear the original MIT license notice.
543
544 The Pintos projects and documentation originated with those designed for
545 Nachos by current and former CS 140 teaching assistants at Stanford
546 University, including at least Yu Ping, Greg Hutchins, Kelly Shaw, Paul
547 Twohey, Sameer Qureshi, and John Rector.
548
549 Example code for monitors (@pxref{Monitors}) is
550 from classroom slides originally by Dawson Engler and updated by Mendel
551 Rosenblum.
552
553 @node Trivia
554 @section Trivia
555
556 Pintos originated as a replacement for Nachos with a similar design.
557 Since then Pintos has greatly diverged from the Nachos design.  Pintos
558 differs from Nachos in two important ways.  First, Pintos runs on real
559 or simulated 80@var{x}86 hardware, but Nachos runs as a process on a
560 host operating system.  Second, Pintos is written in C like most
561 real-world operating systems, but Nachos is written in C++.
562
563 Why the name ``Pintos''?  First, like nachos, pinto beans are a common
564 Mexican food.  Second, Pintos is small and a ``pint'' is a small amount.
565 Third, like drivers of the eponymous car, students are likely to have
566 trouble with blow-ups.