Skip to content

Commit ffe9c28

Browse files
author
Margo
committed
Add exercises.
1 parent 9c290a5 commit ffe9c28

34 files changed

+4036
-0
lines changed

kernel4x/.gdbinit

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
init-if-undefined $target_set = 0
2+
if $target_set == 0
3+
set arch i386:x86-64
4+
target remote localhost:1234
5+
file obj/kernel.full
6+
add-symbol-file obj/bootsector.full 0x7c00
7+
add-symbol-file obj/p-alloc.full 0x100000
8+
add-symbol-file obj/p-recurse.full 0x100000
9+
add-symbol-file obj/p-fork.full 0x100000
10+
add-symbol-file obj/p-panic.full 0x100000
11+
source build/functions.gdb
12+
display/5i $pc
13+
set $target_set = 1
14+
end

kernel4x/.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*~
2+
core*
3+
log.txt
4+
os[0-9]*.img
5+
obj
6+
weensyos1
7+
weensyos1.tar.gz

kernel4x/BRANCHES.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Here is the code we worked through in class for OS02. Each successive
2+
modification is checked in to a git branch. You should be able to
3+
examine a specific branch with `git checkout origin/BRANCHNAME`, and
4+
to see differences with `git diff master origin/v02` and similar
5+
commands. Remember to return to the master branch with
6+
`git checkout master` when you're done.
7+
8+
* master: initial OS02.
9+
* v01: Evil: alloc attacks the kernel with a bad argument.
10+
* v02: Good: kernel checks its arguments more carefully.
11+
* v03: Evil: alloc attacks the kernel with another bad argument.
12+
* v04: Good: kernel checks its arguments even more carefully.
13+
* v05: Kernel adds automatic stack page allocation (for recurse).
14+
* v06: Evil: recurse tries to kill the kernel.
15+
* v07: Good: kernel checks stack page allocation.
16+
* v08: A version of fork.
17+
* v09: Good: Kernel starts checking argument to panic system call.
18+
* v10: Good: Kernel completes checking argument to panic system call.

kernel4x/COPYRIGHT

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
The files in this directory are:
2+
3+
/*
4+
* Copyright (C) 2006-2012 Eddie Kohler
5+
* Copyright (C) 2005 Regents of the University of California
6+
* Copyright (C) 1997 Massachusetts Institute of Technology
7+
*
8+
* This software is being provided by the copyright holders under the
9+
* following license. By obtaining, using and/or copying this software,
10+
* you agree that you have read, understood, and will comply with the
11+
* following terms and conditions:
12+
*
13+
* Permission to use, copy, modify, distribute, and sell this software
14+
* and its documentation for any purpose and without fee or royalty is
15+
* hereby granted, provided that the full text of this NOTICE appears on
16+
* ALL copies of the software and documentation or portions thereof,
17+
* including modifications, that you make.
18+
*
19+
* THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
20+
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
21+
* BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
22+
* WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
23+
* THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
24+
* THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT
25+
* HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE OR
26+
* DOCUMENTATION.
27+
*
28+
* The name and trademarks of copyright holders may NOT be used in
29+
* advertising or publicity pertaining to the software without specific,
30+
* written prior permission. Title to copyright in this software and any
31+
* associated documentation will at all times remain with copyright
32+
* holders. See the file AUTHORS which should have accompanied this software
33+
* for a list of all copyright holders.
34+
*
35+
* This file may be derived from previously copyrighted software. This
36+
* copyright applies only to those changes made by the copyright
37+
* holders listed in the AUTHORS file. The rest of this file is covered by
38+
* the copyright notices, if any, listed below.
39+
*/

kernel4x/GNUmakefile

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
IMAGE = os02.img
2+
all: $(IMAGE)
3+
4+
# '$(V)' controls whether the lab makefiles print verbose commands (the
5+
# actual shell commands run by Make), as well as the "overview" commands
6+
# (such as '+ cc lib/readline.c').
7+
#
8+
# For overview commands only, run 'make all'.
9+
# For overview and verbose commands, run 'make V=1 all'.
10+
V = 0
11+
ifeq ($(V),1)
12+
compile = $(CC) $(CPPFLAGS) $(CFLAGS) $(DEPCFLAGS) $(1)
13+
link = $(LD) $(LDFLAGS) $(1)
14+
run = $(1) $(3)
15+
else
16+
compile = @/bin/echo " " $(2) $< && $(CC) $(CPPFLAGS) $(CFLAGS) $(DEPCFLAGS) $(1)
17+
link = @/bin/echo " " $(2) $(patsubst %.full,%,$@) && $(LD) $(LDFLAGS) $(1)
18+
run = @$(if $(2),/bin/echo " " $(2) $(3) &&,) $(1) $(3)
19+
endif
20+
21+
-include build/rules.mk
22+
23+
24+
# Object sets
25+
26+
BOOT_OBJS = $(OBJDIR)/bootstart.o $(OBJDIR)/boot.o
27+
28+
KERNEL_OBJS = $(OBJDIR)/k-exception.o $(OBJDIR)/kernel.o \
29+
$(OBJDIR)/k-hardware.o $(OBJDIR)/k-loader.o $(OBJDIR)/lib.o \
30+
$(OBJDIR)/k-malloc.o \
31+
$(if $(wildcard k-secret.c),$(OBJDIR)/k-secret.o,)
32+
KERNEL_LINKER_FILES = link/kernel.ld link/shared.ld
33+
34+
PROCESS_BINARIES = $(OBJDIR)/p-alloc $(OBJDIR)/p-recurse $(OBJDIR)/p-fork \
35+
$(OBJDIR)/p-panic
36+
PROCESS_BINARIES_FULL = $(patsubst %,%.full,$(PROCESS_BINARIES))
37+
PROCESS_LIB_OBJS = $(OBJDIR)/lib.o $(OBJDIR)/process.o
38+
PROCESS_OBJS = $(patsubst %,%.o,$(PROCESS_BINARIES)) $(PROCESS_LIB_OBJS)
39+
PROCESS_LINKER_FILES = link/process.ld link/shared.ld
40+
41+
42+
# Generic rules for making object files
43+
44+
$(PROCESS_OBJS): $(OBJDIR)/%.o: %.c $(BUILDSTAMPS)
45+
$(call compile,-O1 -DWEENSYOS_PROCESS -c $< -o $@,COMPILE)
46+
47+
$(OBJDIR)/%.o: %.c $(BUILDSTAMPS)
48+
$(call compile,-DWEENSYOS_KERNEL -c $< -o $@,COMPILE)
49+
50+
$(OBJDIR)/boot.o: $(OBJDIR)/%.o: boot.c $(BUILDSTAMPS)
51+
$(call compile,-Os -fomit-frame-pointer -c $< -o $@,COMPILE)
52+
53+
$(OBJDIR)/%.o: %.S $(BUILDSTAMPS)
54+
$(call compile,-c $< -o $@,ASSEMBLE)
55+
56+
57+
# Specific rules for WeensyOS
58+
59+
$(OBJDIR)/kernel.full: $(KERNEL_OBJS) $(PROCESS_BINARIES) $(KERNEL_LINKER_FILES)
60+
$(call link,-T $(KERNEL_LINKER_FILES) -nostdlib -o $@ $(KERNEL_OBJS) -b binary $(PROCESS_BINARIES),LINK)
61+
62+
$(PROCESS_BINARIES_FULL): \
63+
$(OBJDIR)/p-%.full: $(OBJDIR)/p-%.o $(PROCESS_LIB_OBJS) $(PROCESS_LINKER_FILES)
64+
$(call link,-T $(PROCESS_LINKER_FILES) -nostdlib -o $@ $< $(PROCESS_LIB_OBJS),LINK)
65+
66+
$(OBJDIR)/%: $(OBJDIR)/%.full
67+
$(call run,$(OBJDUMP) -S $< >$@.asm)
68+
$(call run,$(NM) -n $< >$@.sym)
69+
$(call run,$(OBJCOPY) -j .text -j .rodata -j .data -j .bss $<,STRIP,$@)
70+
71+
$(OBJDIR)/bootsector: $(BOOT_OBJS) link/boot.ld link/shared.ld
72+
$(call link,-T link/boot.ld link/shared.ld -nostdlib -o $@.full $(BOOT_OBJS),LINK)
73+
$(call run,$(OBJDUMP) -S $@.full >$@.asm)
74+
$(call run,$(NM) -n $@.full >$@.sym)
75+
$(call run,$(OBJCOPY) -S -O binary -j .text $@.full $@)
76+
77+
$(OBJDIR)/mkbootdisk: build/mkbootdisk.c $(BUILDSTAMPS)
78+
$(call run,$(HOSTCC) -I. -o $(OBJDIR)/mkbootdisk,HOSTCOMPILE,build/mkbootdisk.c)
79+
80+
$(OBJDIR)/k-binaries.c: GNUmakefile build/mkkbinaries.sh
81+
$(call run,$(SHELL) build/mkkbinaries.sh -c $(PROCESS_BINARIES) >,CREATE,$@)
82+
$(OBJDIR)/k-binaries.h: GNUmakefile build/mkkbinaries.sh
83+
$(call run,$(SHELL) build/mkkbinaries.sh -h $(PROCESS_BINARIES) >,CREATE,$@)
84+
$(OBJDIR)/k-loader.o: $(OBJDIR)/k-binaries.c
85+
$(OBJDIR)/k-malloc.o: $(OBJDIR)/k-binaries.c
86+
$(OBJDIR)/kernel.o $(OBJDIR)/k-hardware.o: $(OBJDIR)/k-binaries.h
87+
88+
os02.img: $(OBJDIR)/mkbootdisk $(OBJDIR)/bootsector $(OBJDIR)/kernel
89+
$(call run,$(OBJDIR)/mkbootdisk $(OBJDIR)/bootsector $(OBJDIR)/kernel > $@,CREATE $@)
90+
91+
92+
run-%: run-qemu-%
93+
@:
94+
95+
run-qemu-%: run-$(QEMUDISPLAY)-%
96+
@:
97+
98+
run-graphic-%: %.img check-qemu
99+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) $(QEMUIMG),QEMU $<)
100+
101+
run-console-%: %.img check-qemu
102+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -curses $(QEMUIMG),QEMU $<)
103+
104+
run-monitor-%: %.img check-qemu
105+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -monitor stdio $(QEMUIMG),QEMU $<)
106+
107+
run-gdb-%: run-gdb-$(QEMUDISPLAY)-%
108+
@:
109+
110+
run-gdb-graphic-%: %.img check-qemu
111+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -S -gdb tcp::1234 $(QEMUIMG) &,QEMU $<)
112+
$(call run,gdb -x .gdbinit,GDB)
113+
114+
run-gdb-console-%: %.img check-qemu
115+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -curses -S -gdb tcp::1234 $(QEMUIMG),QEMU $<)
116+
117+
run: run-qemu-$(basename $(IMAGE))
118+
run-qemu: run-qemu-$(basename $(IMAGE))
119+
run-graphic: run-graphic-$(basename $(IMAGE))
120+
run-console: run-console-$(basename $(IMAGE))
121+
run-monitor: run-monitor-$(basename $(IMAGE))
122+
run-quit: run-quit-$(basename $(IMAGE))
123+
run-gdb: run-gdb-$(basename $(IMAGE))
124+
run-gdb-graphic: run-gdb-graphic-$(basename $(IMAGE))
125+
run-gdb-console: run-gdb-console-$(basename $(IMAGE))
126+
run-graphic-gdb: run-gdb-graphic-$(basename $(IMAGE))
127+
run-console-gdb: run-gdb-console-$(basename $(IMAGE))
128+
129+
130+
# Kill all my qemus
131+
kill:
132+
-killall -u $$(whoami) $(QEMU)
133+
@sleep 0.2; if ps -U $$(whoami) | grep $(QEMU) >/dev/null; then killall -9 -u $$(whoami) $(QEMU); fi

kernel4x/README.md

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
OS02
2+
=======
3+
4+
Type `make run` to run our OS using the QEMU emulator. We expect this
5+
to work only on CS50 Appliance and other Linux hosts. If you have
6+
problems, check out Troubleshooting below.
7+
8+
Running the OS
9+
--------------
10+
11+
There are several ways to run the OS.
12+
13+
* `make run`
14+
15+
Build the OS and pop up a QEMU window to run it. Close the QEMU
16+
window to exit the OS.
17+
18+
* `make run-console`
19+
20+
Build the OS and run QEMU in the current terminal window. Press
21+
Control-C in the terminal to exit the OS.
22+
23+
* `make run-gdb`
24+
25+
Build the OS and run it in a QEMU window under GDB. A QEMU window
26+
pops up, but the virtual machine immediately stops for debugging.
27+
Gdb runs in the terminal. You can set breakpoints on OS,
28+
bootloader, or process functions (for instance, try `b kernel` to
29+
break at the kernel's entry point) and then type `c` to run the
30+
virtual machine until a breakpoint hits. Use `si`, `x`, `info
31+
reg`, etc. to view machine state. We compile the kernel with
32+
debugging symbols; this means even commands like `next` will work,
33+
and you can see the source lines corresponding to individual
34+
instructions. Close the QEMU window to exit the OS, and enter `q`
35+
in gdb to exit it.
36+
37+
* `make run-gdb-console`
38+
39+
Same as `make run-gdb`, but runs QEMU in the current terminal
40+
window. You must run gdb yourself in a *different* terminal
41+
window. Run `gdb -x .gdbinit` from this directory.
42+
43+
In all of these run modes, QEMU also creates a file named `log.txt`.
44+
The code we hand out doesn't actually log anything yet, but you may
45+
find it useful to add your own calls to `log_printf` from the kernel.
46+
47+
Finally, run `make clean` to clean up your directory.
48+
49+
Source
50+
------
51+
52+
Real operating systems are big. We have tried to boil down this OS to
53+
a minimum, comment it to help you, and separate x86 specifics from
54+
more fundamental issues. Here is an overview of the code.
55+
56+
=== Important code ===
57+
58+
* `kernel.c`: The kernel. Uses functions declared and described in
59+
`kernel.h` and `lib.h`.
60+
* `p-*.c`: The applications.
61+
Use functions declared and described in `process.h` and `lib.h`.
62+
63+
=== Support code ===
64+
65+
You can read this if you're interested, but the important stuff is
66+
declared and described in the files listed above.
67+
68+
* `bootstart.S`, `boot.c`: The bootloader.
69+
* `k-hardware.c`: Functions that set up x86 hardware state using
70+
programmed I/O and memory-mapped I/O instructions.
71+
* `k-exception.S`: Kernel assembly code for handling exceptions
72+
(interrupts, traps, and faults).
73+
* `k-loader.c`: Kernel program loader, which loads processes from
74+
"image files" into memory.
75+
* `process.c`: Support code for applications.
76+
* `lib.c`, `lib.h`: Support code useful in both the kernel and
77+
applications.
78+
* `x86.h`: x86 hardware definitions, including functions that
79+
correspond to important x86 instructions.
80+
* `elf.h`: ELF support information. (ELF is a format used for
81+
executables.)
82+
83+
Build Files
84+
-----------
85+
86+
The main output of the build process is a disk image, `os01.img`. QEMU
87+
"boots" off this disk image, but it could also boot on real hardware!
88+
The build process also produces other files that you can look at and
89+
puts them in the `obj/` directory.
90+
91+
* `obj/kernel.asm`
92+
93+
This file is the output of `objdump -S` on the kernel. Use it to see
94+
the kernel's assembly code.
95+
96+
* `obj/kernel.sym`
97+
98+
This smaller file just lists all the kernel's symbols (i.e.,
99+
variable names).
100+
101+
* `obj/p-hello.asm`, `obj/p-welcome.sym`
102+
103+
Similar files are generated for process code.
104+
105+
Troubleshooting
106+
---------------
107+
108+
The OS runs using the QEMU full-system emulator. On CS50 Appliance,
109+
our makefiles will install QEMU for you. On your own Linux machine,
110+
you will need to install QEMU yourself. On Ubuntu-based hosts, run
111+
`sudo apt-get install qemu`. On Fedora-based hosts, run `sudo yum
112+
install qemu-system-x86`.
113+
114+
If Control-C doesn't work on your QEMU, make sure you are using an
115+
actual Control key. On some machines QEMU ignores key remappings (such
116+
as swapping Control and Caps Lock).
117+
118+
If Control-C still doesn't work on your QEMU -- for instance, because
119+
your OS kernel crashed with an assertion failure -- close it by
120+
running `make kill`.

0 commit comments

Comments
 (0)