10/27: Weensy Memory-Mapped I/O
This exercise aims to teach you about system call implementation, argument checking, and memory mapping in an operating system kernel.
Check out the kernel3x
directory of your cs61-exercises
repository
(you’ll need to pull first). This directory contains a tiny operating
system built around a ramdisk, which is a model of a normal operating
system’s buffer cache. The ramdisk is stored in kernel memory in the
ramdisk
character array. The kernel first fills the initial portion
of the ramdisk with some data. Then the p-reader
process repeatedly
reads from the ramdisk by calling the sys_read
system call and
prints what it finds.
Part 0
Look at the code, particularly p-reader.c
, and understand how it
works.
Note: This weensy OS has kernel isolation (the kernel’s memory is
protected from processes by the page table), but not full process
isolation (all processes share the same page table,
kernel_pagetable
).
Part 1: Write system call
Uncomment the body of p-writer.c
. Now your kernel will fail to
compile, because the sys_write
system call is missing. So add it!
- Add a system call number for write to
lib.h
. - Add
sys_write
toprocess.h
, basing it offsys_read
. - Add a handler for your write system call to
kernel.c
.
When you’re done, the output of p-reader
should eventually become
all “61”.
Enter your answers on the survey
Part 2: System call checking
Our implementation of sys_read
doesn’t check its arguments. As a
result, a process can kill the kernel by calling sys_read
(and
likely sys_write
too).
First, verify this: change either p-reader
or p-writer
to kill
the kernel (the OS should crash in some way).
Second, fix it: change sys_read
and sys_write
to validate their
arguments to provide kernel isolation. A full check will involve some
arithmetic and some calls to virtual_memory_lookup
.
Enter your answers on the survey
Part 3: Memory mapping
Uncomment the body of p-mapreader.c
. Now your kernel will fail to
compile, because the sys_mmap
system call is missing. So add it!
- Add a system call number for mmap, and a definition for
PROT_READ
, tolib.h
. - Add
sys_mmap
toprocess.h
. The argument order is described inp-mapreader.c
. You don’t need to handle anaddr
ofNULL
(the user must always provide a validaddr
). - Add a handler for your mmap system call to
kernel.c
. (You don’t need to validate arguments at first, but once you’ve got it working, do go back and validate arguments.)
When you’re done, your output should alternate between read
and
mapread
lines, each printing the same thing (and both gradually
becoming all “61”).
Enter your answers on the survey
Part 4: Memory mapping for writes
Uncomment the body of p-mapwriter.c
, and re-comment the body of
p-writer.c
. Then add support for read/write memory mapping!
- Add a definition for
PROT_WRITE
tolib.h
. - Update your mmap system call so that the mapping is read/write only if
PROT_WRITE
is provided.
When you’re done, the read
and mapread
lines should gradually
become all “61”, because p-mapwriter
is writing into the ramdisk.
Enter your answers on the survey