Skip to content

Commit 0b040b7

Browse files
committed
Shell1X exercise.
1 parent 0596421 commit 0b040b7

29 files changed

+3870
-0
lines changed

shell1x/.gdbinit

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
init-if-undefined $loaded = 0
2+
if $loaded == 0
3+
set $loaded = 1
4+
set arch i386:x86-64
5+
file obj/kernel.full
6+
add-symbol-file obj/bootsector.full 0x7c00
7+
add-symbol-file obj/p-pipewriter.full 0x100000
8+
add-symbol-file obj/p-pipereader.full 0x140000
9+
target remote localhost:1234
10+
source build/functions.gdb
11+
display/5i $pc
12+
13+
# Your initialization commands here (if you want)
14+
end

shell1x/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*~
2+
core*
3+
log.txt
4+
*.img
5+
obj

shell1x/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-2016 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+
*/

shell1x/GNUmakefile

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
IMAGE = pipeos.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+
KERNEL_LINKER_FILES = link/kernel.ld link/shared.ld
31+
32+
PROCESS_BINARIES = $(OBJDIR)/p-pipewriter $(OBJDIR)/p-pipereader
33+
PROCESS_BINARIES_FULL = $(patsubst %,%.full,$(PROCESS_BINARIES))
34+
PROCESS_LIB_OBJS = $(OBJDIR)/lib.o $(OBJDIR)/process.o
35+
PROCESS_OBJS = $(patsubst %,%.o,$(PROCESS_BINARIES)) $(PROCESS_LIB_OBJS)
36+
37+
38+
# Generic rules for making object files
39+
40+
$(PROCESS_OBJS): $(OBJDIR)/%.o: %.c $(BUILDSTAMPS)
41+
$(call compile,-O1 -DWEENSYOS_PROCESS -c $< -o $@,COMPILE)
42+
43+
$(OBJDIR)/%.o: %.c $(BUILDSTAMPS)
44+
$(call compile,-DWEENSYOS_KERNEL -c $< -o $@,COMPILE)
45+
46+
$(OBJDIR)/boot.o: $(OBJDIR)/%.o: boot.c $(BUILDSTAMPS)
47+
$(call compile,-Os -fomit-frame-pointer -c $< -o $@,COMPILE)
48+
49+
$(OBJDIR)/%.o: %.S $(BUILDSTAMPS)
50+
$(call compile,-c $< -o $@,ASSEMBLE)
51+
52+
53+
# Specific rules for WeensyOS
54+
55+
$(OBJDIR)/kernel.full: $(KERNEL_OBJS) $(PROCESS_BINARIES) $(KERNEL_LINKER_FILES)
56+
$(call link,-T $(KERNEL_LINKER_FILES) -nostdlib -o $@ $(KERNEL_OBJS) -b binary $(PROCESS_BINARIES),LINK)
57+
58+
$(PROCESS_BINARIES_FULL): \
59+
$(OBJDIR)/p-%.full: $(OBJDIR)/p-%.o $(PROCESS_LIB_OBJS) \
60+
link/p-%.ld link/shared.ld
61+
$(call link,-T link/p-$*.ld link/shared.ld -nostdlib -o $@ $< $(PROCESS_LIB_OBJS),LINK)
62+
63+
$(OBJDIR)/%: $(OBJDIR)/%.full
64+
$(call run,$(OBJDUMP) -S $< >$@.asm)
65+
$(call run,$(NM) -n $< >$@.sym)
66+
$(call run,$(OBJCOPY) -j .text -j .rodata -j .data -j .bss $<,STRIP,$@)
67+
68+
$(OBJDIR)/bootsector: $(BOOT_OBJS) link/boot.ld link/shared.ld
69+
$(call link,-T link/boot.ld link/shared.ld -nostdlib -o $@.full $(BOOT_OBJS),LINK)
70+
$(call run,$(OBJDUMP) -S $@.full >$@.asm)
71+
$(call run,$(NM) -n $@.full >$@.sym)
72+
$(call run,$(OBJCOPY) -S -O binary -j .text $@.full $@)
73+
74+
$(OBJDIR)/mkbootdisk: build/mkbootdisk.c $(BUILDSTAMPS)
75+
$(call run,$(HOSTCC) -I. -o $(OBJDIR)/mkbootdisk,HOSTCOMPILE,build/mkbootdisk.c)
76+
77+
pipeos.img: $(OBJDIR)/mkbootdisk $(OBJDIR)/bootsector $(OBJDIR)/kernel
78+
$(call run,$(OBJDIR)/mkbootdisk $(OBJDIR)/bootsector $(OBJDIR)/kernel > $@,CREATE $@)
79+
80+
81+
run-%: run-qemu-%
82+
@:
83+
84+
run-qemu-%: run-$(QEMUDISPLAY)-%
85+
@:
86+
87+
run-graphic-%: %.img check-qemu
88+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) $(QEMUIMG),QEMU $<)
89+
90+
run-console-%: %.img check-qemu
91+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -curses $(QEMUIMG),QEMU $<)
92+
93+
run-monitor-%: %.img check-qemu
94+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -monitor stdio $(QEMUIMG),QEMU $<)
95+
96+
run-gdb-%: run-gdb-$(QEMUDISPLAY)-%
97+
@:
98+
99+
run-gdb-graphic-%: %.img check-qemu
100+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -gdb tcp::1234 $(QEMUIMG) &,QEMU $<)
101+
$(call run,sleep 0.5; gdb -x .gdbinit,GDB)
102+
103+
run-gdb-console-%: %.img check-qemu
104+
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -curses -gdb tcp::1234 $(QEMUIMG),QEMU $<)
105+
106+
run: run-qemu-$(basename $(IMAGE))
107+
run-qemu: run-qemu-$(basename $(IMAGE))
108+
run-graphic: run-graphic-$(basename $(IMAGE))
109+
run-console: run-console-$(basename $(IMAGE))
110+
run-monitor: run-monitor-$(basename $(IMAGE))
111+
run-quit: run-quit-$(basename $(IMAGE))
112+
run-gdb: run-gdb-$(basename $(IMAGE))
113+
run-gdb-graphic: run-gdb-graphic-$(basename $(IMAGE))
114+
run-gdb-console: run-gdb-console-$(basename $(IMAGE))
115+
run-graphic-gdb: run-gdb-graphic-$(basename $(IMAGE))
116+
run-console-gdb: run-gdb-console-$(basename $(IMAGE))
117+
118+
119+
# Kill all my qemus
120+
kill:
121+
-killall -u $$(whoami) $(QEMU)
122+
@sleep 0.2; if ps -U $$(whoami) | grep $(QEMU) >/dev/null; then killall -9 -u $$(whoami) $(QEMU); fi

shell1x/README.md

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
OS01
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)