Skip to content

Commit

Permalink
Initial handout code for 2021.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Jan 27, 2021
1 parent 00bfb09 commit a9037c5
Show file tree
Hide file tree
Showing 26 changed files with 823 additions and 285 deletions.
26 changes: 15 additions & 11 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ all: $(QEMUIMAGEFILES)

# Place local configuration options, such as `CC=clang`, in
# `config.mk` so you don't have to list them every time.
-include config.mk
CONFIG ?= config.mk
-include $(CONFIG)

# `$(V)` controls whether the makefiles print verbose commands (the shell
# commands run by Make) or brief commands (like `COMPILE`).
Expand Down Expand Up @@ -33,6 +34,9 @@ QEMUOPT = -net none -parallel $(LOG) -smp $(NCPU)
ifeq ($(D),1)
QEMUOPT += -d int,cpu_reset,guest_errors -no-reboot
endif
ifneq ($(NOGDB),1)
QEMUGDB ?= -gdb tcp::12949
endif


# Sets of object files
Expand All @@ -47,7 +51,7 @@ KERNEL_OBJS = $(OBJDIR)/k-exception.ko \
$(OBJDIR)/k-ahci.ko $(OBJDIR)/k-chkfs.ko $(OBJDIR)/k-chkfsiter.ko \
$(OBJDIR)/k-memviewer.ko $(OBJDIR)/lib.ko $(OBJDIR)/k-initfs.ko

PROCESSES = $(patsubst %.cc,%,$(wildcard p-*.cc))
PROCESSES ?= $(patsubst %.cc,%,$(wildcard p-*.cc))

PROCESS_LIB_OBJS = $(OBJDIR)/lib.uo $(OBJDIR)/u-lib.uo $(OBJDIR)/crc32c.uo

Expand Down Expand Up @@ -107,7 +111,7 @@ $(OBJDIR)/bootentry.o: $(OBJDIR)/%.o: \
$(OBJDIR)/%.uo: %.cc $(BUILDSTAMPS)
$(call cxxcompile,$(CXXFLAGS) -O1 -DCHICKADEE_PROCESS -c $< -o $@,COMPILE $<)

$(OBJDIR)/%.uo: %.S $(OBJDIR)/u-asm.h $(KERNELBUILDSTAMPS)
$(OBJDIR)/%.uo: %.S $(OBJDIR)/u-asm.h $(BUILDSTAMPS)
$(call assemble,-O2 -c $< -o $@,ASSEMBLE $<)


Expand Down Expand Up @@ -150,7 +154,7 @@ $(OBJDIR)/kernel: $(OBJDIR)/kernel.full $(OBJDIR)/mkchickadeesymtab
$(OBJDIR)/%: $(OBJDIR)/%.full
$(call run,$(OBJDUMP) -C -S -j .text -j .ctors $< >$@.asm)
$(call run,$(NM) -n $< >$@.sym)
$(call run,$(OBJCOPY) -j .text -j .rodata -j .data -j .bss -j .ctors -j .init_array $<,STRIP,$@)
$(call run,$(QUIETOBJCOPY) -j .text -j .rodata -j .data -j .bss -j .ctors -j .init_array $<,STRIP,$@)

$(OBJDIR)/bootsector: $(BOOT_OBJS) boot.ld
$(call link,-T boot.ld -o $@.full $(BOOT_OBJS),LINK)
Expand All @@ -174,7 +178,7 @@ $(OBJDIR)/%.o: build/%.cc $(BUILDSTAMPS)
$(call run,$(HOSTCXX) $(CPPFLAGS) $(HOSTCXXFLAGS) $(DEPCFLAGS) -c -o $@,HOSTCOMPILE,$<)

$(OBJDIR)/mkchickadeefs: build/mkchickadeefs.cc $(BUILDSTAMPS)
$(call run,$(HOSTCXX) $(CPPFLAGS) $(HOSTCXXFLAGS) $(DEPCFLAGS) -o $@,HOSTCOMPILE,$<)
$(call run,$(HOSTCXX) $(CPPFLAGS) $(HOSTCXXFLAGS) $(DEPCFLAGS) -g -o $@,HOSTCOMPILE,$<)

CHICKADEEFSCK_OBJS = $(OBJDIR)/chickadeefsck.o \
$(OBJDIR)/journalreplayer.o \
Expand Down Expand Up @@ -212,12 +216,12 @@ QEMUIMG = -M q35 \

run: run-$(QEMUDISPLAY)
@:
run-graphic: $(QEMUIMAGEFILES) check-qemu
@echo '* Run `gdb -x build/chickadee.gdb` to connect gdb to qemu.' 1>&2
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -gdb tcp::12949 $(QEMUIMG),QEMU $<)
run-console: $(QEMUIMAGEFILES) check-qemu-console
@echo '* Run `gdb -x build/chickadee.gdb` to connect gdb to qemu.' 1>&2
$(call run,$(QEMU) $(QEMUOPT) -curses -gdb tcp::12949 $(QEMUIMG),QEMU $<)
run-gdb-report:
@if test "$(QEMUGDB)" = "-gdb tcp::12949"; then echo '* Run `gdb -x build/weensyos.gdb` to connect gdb to qemu.' 1>&2; fi
run-graphic: $(QEMUIMAGEFILES) check-qemu run-gdb-report
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) $(QEMUGDB) $(QEMUIMG),QEMU $<)
run-console: $(QEMUIMAGEFILES) check-qemu-console run-gdb-report
$(call run,$(QEMU) $(QEMUOPT) -curses $(QEMUGDB) $(QEMUIMG),QEMU $<)
run-monitor: $(QEMUIMAGEFILES) check-qemu
$(call run,$(QEMU_PRELOAD) $(QEMU) $(QEMUOPT) -monitor stdio $(QEMUIMG),QEMU $<)
run-gdb: run-gdb-$(QEMUDISPLAY)
Expand Down
57 changes: 45 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ Quickstart: `make run` or `make run-PROGRAM`
Make targets
------------

Run `make NCPU=N run` to run with `N` virtual CPUs (default is 2).
`make NCPU=N run` will run the OS with `N` virtual CPUs (default is 2). Close
the QEMU window, or type `q` inside it, to exit the OS.

Run `make SAN=1 run` to run with sanitizers.
`make run-console` will run the OS in the console window.

Normally Chickadee’s debug log is written to `log.txt`. Run `make LOG=stdio
run` to redirect the debug log to the standard output, or `make
LOG=file:FILENAME run` to redirect it to `FILENAME`.
`make SAN=1 run` to run with sanitizers enabled.

Normally Chickadee’s debug log is written to `log.txt`. `make LOG=stdio run`
will redirect the debug log to the standard output, and `make
LOG=file:FILENAME run` will redirect it to `FILENAME`.

Run `make D=1 run` to ask QEMU to print verbose information about interrupts
and CPU resets to the standard error. This setting will also cause QEMU to
Expand All @@ -29,6 +32,36 @@ default is `alloc`.
Troubleshooting
---------------

There are several ways to kill a recalcitrant QEMU (for instance, if your
OS has become unresponsive).

* If QEMU is running in its own graphical window, then close the window. This
will kill the embedded OS.

* If QEMU is running in a terminal window (in Docker, for instance), then
press `Alt-2`. This will bring up the QEMU Monitor, which looks like this:

```
compat_monitor0 console
QEMU 4.2.0 monitor - type 'help' for more information
(qemu)
```
Type `quit` and hit Return to kill the embedded OS and return to your
shell. If this leaves the terminal looking funny, enter the `reset` shell
command to restore it.
If `Alt-2` does not work, you may need to configure your terminal to
properly send the Alt key. For instance, on Mac OS X’s Terminal, go to
Terminal > Preferences > Keyboard and select “Use Option as Meta key”. You
can also configure a special keyboard shortcut that sends the `Escape 2`
sequence.
Run `make run-gdb` to start up the OS with support for GDB debugging. This
will start the OS, but not GDB. You must run `gdb -x build/weensyos.gdb` to
connect to the running emulator; when GDB connects, it will stop the OS and
wait for instructions.
If you experience runtime errors involving `obj/libqemu-nograb.so.1`, put
`QEMU_PRELOAD_LIBRARY=` in `config.mk`. This disables a shim we use that
prevents QEMU from grabbing the mouse.
Expand All @@ -41,7 +74,7 @@ Source files
| File | Description |
| --------------- | ---------------------------- |
| `types.h` | Type definitions |
| `lib.hh/cc` | Chickadee C library |
| `lib.hh/cc` | C library |
| `x86-64.h` | x86-64 hardware definitions |
| `elf.h` | ELF64 structures |
Expand Down Expand Up @@ -103,11 +136,11 @@ The main output of the build process is a disk image,
could conceivably boot on real hardware! The build process also
produces other files that can be useful to examine.
| File | Description |
| ---------------------------- | ------------------------------------ |
| `obj/kernel.asm` | Kernel assembly (with addresses) |
| `obj/kernel.sym` | Kernel defined symbols |
| `obj/p-allocator.asm`, `sym` | Same for process binaries |
| File | Description |
| -------------------------- | ------------------------------------ |
| `obj/kernel.asm` | Kernel assembly (with addresses) |
| `obj/kernel.sym` | Kernel defined symbols |
| `obj/p-PROCESS.asm`, `sym` | Same for process binaries |
[CS 161]: https://read.seas.harvard.edu/cs161/2020/
[CS 161]: https://read.seas.harvard.edu/cs161/2021/
[triple fault]: https://en.wikipedia.org/wiki/Triple_fault
6 changes: 3 additions & 3 deletions bootentry.S
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ init_boot_pagetable:
rep stosl

# set up boot page table
# 0x1000: L1 page table; entries 0, 256, and 511 point to:
# 0x2000: L2 page table; entries 0 and 510 map 1st 1GB of physmem
# This weird setup is the minimal page table that maps all of
# 0x1000: L4 page table; entries 0, 256, and 511 point to:
# 0x2000: L3 page table; entries 0 and 510 map 1st 1GB of physmem
# This is the minimal page table that maps all of
# low-canonical, high-canonical, and kernel-text addresses to
# the first 1GB of physical memory.
movl $BOOT_PAGETABLE, %edi
Expand Down
11 changes: 11 additions & 0 deletions build/quietobjcopy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#! /bin/sh

onexit () {
test -n "$tmp" && rm -f "$tmp"
}
trap onexit 0
tmp=`mktemp /tmp/objcopy.XXXXXX`
"$@" 2> $tmp
v=$?
cat $tmp | grep -v "empty loadable segment" 1>&2
exit $v
4 changes: 3 additions & 1 deletion build/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ LDFLAGS := $(LDFLAGS) -Os --gc-sections -z max-page-size=0x1000 \
-static -nostdlib -nostartfiles
LDFLAGS += $(shell $(LD) -m elf_x86_64 --help >/dev/null 2>&1 && echo -m elf_x86_64)

QUIETOBJCOPY = sh build/quietobjcopy.sh $(OBJCOPY)


# Dependencies
DEPSDIR := .deps
Expand Down Expand Up @@ -154,7 +156,7 @@ always:
# These targets don't correspond to files
.PHONY: all always clean realclean distclean cleanfs fsck \
run run-graphic run-console run-monitor \
run-gdb run-gdb-graphic run-gdb-console \
run-gdb run-gdb-graphic run-gdb-console run-gdb-report \
check-qemu-console check-qemu kill \
run-% run-graphic-% run-console-% run-monitor-% \
run-gdb-% run-gdb-graphic-% run-gdb-console-%
Expand Down
102 changes: 102 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
FROM ubuntu:focal

# set environment variables for tzdata
ARG TZ=America/New_York
ENV TZ ${TZ}

# include manual pages and documentation
RUN DEBIAN_FRONTEND=noninteractive apt-get update && yes | unminimize

# install GCC-related packages
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install \
binutils-doc \
cpp-doc \
gcc-doc \
g++ \
g++-multilib \
gdb \
gdb-doc \
glibc-doc \
libblas-dev \
liblapack-dev \
liblapack-doc \
libstdc++-10-doc \
make \
make-doc

# install clang-related packages
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install \
clang \
clang-10-doc \
lldb

# install qemu for pset 4 (sadly, this pulls in a lot of crap)
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install \
qemu-system-x86

# install programs used for system exploration
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install \
blktrace \
linux-tools-generic \
strace \
tcpdump

# install interactive programs (emacs, vim, nano, man, sudo, etc.)
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install \
bc \
curl \
dc \
emacs-nox \
git \
git-doc \
man \
micro \
nano \
sudo \
vim \
wget

# set up libraries
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install \
libreadline-dev \
locales \
wamerican

# install programs used for networking
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install \
dnsutils \
inetutils-ping \
net-tools \
netcat \
telnet \
traceroute

# set up default locale
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8

# remove unneeded .deb files
RUN rm -r /var/lib/apt/lists/*

# set up passwordless sudo for user cs61-user
RUN useradd -m -s /bin/bash cs61-user && \
echo "cs61-user ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/cs61-init

# create binary reporting version of dockerfile
RUN (echo '#\!/bin/sh'; echo 'echo 5') > /usr/bin/cs61-docker-version; chmod ugo+rx,u+w,go-w /usr/bin/cs61-docker-version

# git build arguments
ARG USER=CS61\ User
ARG [email protected]

# configure your environment
USER cs61-user
RUN git config --global user.name "${USER}" && \
git config --global user.email "${EMAIL}" && \
(echo "(custom-set-variables"; echo " '(c-basic-offset 4)"; echo " '(indent-tabs-mode nil))") > ~/.emacs && \
(echo "set expandtab"; echo "set shiftwidth=4"; echo "set softtabstop=4") > ~/.vimrc && \
echo "add-auto-load-safe-path ~" > ~/.gdbinit

WORKDIR /home/cs61-user

# Initial version of this Dockerfile by Todd Morrill, CS 61 DCE student
Loading

0 comments on commit a9037c5

Please sign in to comment.