From 2d51463c56e8f1437b9c64907bc32e5557987ce7 Mon Sep 17 00:00:00 2001 From: Garen Tyler Date: Sun, 22 Oct 2023 13:43:27 -0600 Subject: [PATCH] restructure makefile into multiple files --- .gitignore | 3 +- Makefile | 84 +++++++++++------------------------ kernel/Makefile | 88 +++++++++++++++++++++++++++++++++++++ kernel/buf.h | 4 ++ kernel/defs.h | 2 + kernel/elf.h | 2 + kernel/file.h | 3 ++ kernel/fs.h | 1 + {user => kernel}/initcode.S | 0 kernel/proc.h | 5 +++ kernel/riscv.h | 1 + kernel/sleeplock.h | 4 ++ kernel/spinlock.h | 3 ++ kernel/stat.h | 2 + kernel/virtio.h | 2 + mkfs/Makefile | 9 ++++ mkfs/mkfs.c | 8 ++-- user/Makefile | 62 ++++++++++++++++++++++++++ user/printf.c | 6 +-- user/ulib.c | 8 ++-- user/umalloc.c | 8 ++-- user/usys.pl | 2 +- 22 files changed, 232 insertions(+), 75 deletions(-) create mode 100644 kernel/Makefile rename {user => kernel}/initcode.S (100%) create mode 100644 mkfs/Makefile create mode 100644 user/Makefile diff --git a/.gitignore b/.gitignore index 26ea3e7..0aad3f7 100644 --- a/.gitignore +++ b/.gitignore @@ -6,13 +6,14 @@ _* *.asm *.sym *.img +*.gch vectors.S bootblock entryother initcode initcode.out kernelmemfs -mkfs +mkfs/mkfs kernel/kernel user/usys.S .gdbinit diff --git a/Makefile b/Makefile index 80d3fe8..b03527c 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,8 @@ K=kernel -R=$K/rustkernel +M=mkfs U=user P=programs -OBJS = \ - $K/entry.o \ - $K/vm.o \ - $K/proc.o \ - $K/swtch.o \ - $K/trampoline.o \ - $K/bio.o \ - $K/fs.o \ - $K/log.o \ - $K/file.o \ - $K/pipe.o \ - $K/exec.o \ - $K/sysfile.o \ - $K/kernelvec.o \ - $K/virtio_disk.o - # riscv64-unknown-elf- or riscv64-linux-gnu- # perhaps in /opt/riscv/bin #TOOLPREFIX = @@ -51,7 +35,6 @@ CFLAGS += -mcmodel=medany CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax CFLAGS += -I. CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector) -TARGET_TRIPLE = riscv64gc-unknown-none-elf # Disable PIE when possible (for Ubuntu 16.10 toolchain) ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),) @@ -63,49 +46,35 @@ endif LDFLAGS = -z max-page-size=4096 -$K/kernel: $(OBJS) $K/kernel.ld $U/initcode $R/src - cargo +nightly -Z unstable-options -C $R build --release -# $(OBJDUMP) -S $R/target/$(TARGET_TRIPLE)/release/librustkernel.a > $R/target/$(TARGET_TRIPLE)/release/librustkernel.asm - $(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS) $R/target/$(TARGET_TRIPLE)/release/librustkernel.a - $(OBJDUMP) -S $K/kernel > $K/kernel.asm - $(OBJDUMP) -t $K/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $K/kernel.sym +.PHONY: build kernel ulib mkfs clean -$U/initcode: $U/initcode.S - $(CC) $(CFLAGS) -march=rv64g -nostdinc -I. -Ikernel -c $U/initcode.S -o $U/initcode.o - $(LD) $(LDFLAGS) -N -e start -Ttext 0 -o $U/initcode.out $U/initcode.o - $(OBJCOPY) -S -O binary $U/initcode.out $U/initcode - $(OBJDUMP) -S $U/initcode.o > $U/initcode.asm +build: kernel fs.img -tags: $(OBJS) _init - etags *.S *.c +kernel: + $(MAKE) -C $K -ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o +ulib: + $(MAKE) -C $U -# %.a: %.rs -# $(RUSTC) $(RUSTFLAGS) --crate-type staticlib -o $@ -L $K $^ +mkfs: + $(MAKE) -C $M + +USERLIBS = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o %.o: %.c *.h - $(CC) $(CFLAGS) -c $^ + $(CC) $(CFLAGS) -c $< -_%: %.o $(ULIB) - $(LD) $(LDFLAGS) -T $U/user.ld -o $@ $^ - $(OBJDUMP) -S $@ > $*.asm - $(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym +_%: %.o ulib + $(LD) $(LDFLAGS) -T $U/user.ld -o $@ $< $(USERLIBS) + # $(OBJDUMP) -S $@ > $*.asm + # $(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym -$U/usys.S : $U/usys.pl - perl $U/usys.pl > $U/usys.S - -$U/usys.o : $U/usys.S - $(CC) $(CFLAGS) -c -o $U/usys.o $U/usys.S - -$U/_forktest: $U/forktest.o $(ULIB) +$U/_forktest: $U/forktest.o ulib # forktest has less library code linked in - needs to be small # in order to be able to max out the proc table. $(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $U/_forktest $U/forktest.o $U/ulib.o $U/usys.o - $(OBJDUMP) -S $U/_forktest > $U/forktest.asm + # $(OBJDUMP) -S $U/_forktest > $U/forktest.asm -mkfs/mkfs: mkfs/mkfs.c $K/fs.h $K/param.h - gcc -Werror -Wall -I. -o mkfs/mkfs mkfs/mkfs.c # Prevent deletion of intermediate files, e.g. cat.o, after first build, so # that disk image changes after first build are persistent until clean. More @@ -132,19 +101,18 @@ UPROGS=\ $P/_zombie\ $P/_shutdown\ -fs.img: mkfs/mkfs README $(UPROGS) +fs.img: mkfs README $(UPROGS) mkfs/mkfs fs.img README $(UPROGS) -include kernel/*.d user/*.d clean: + $(MAKE) -C $K clean + $(MAKE) -C $M clean + $(MAKE) -C $U clean rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \ - */*.o */*.a */*.d */*.asm */*.sym \ - $U/initcode $U/initcode.out $K/kernel fs.img \ - mkfs/mkfs .gdbinit \ - $U/usys.S \ + */*.o */*.a */*.d */*.asm */*.sym fs.img .gdbinit \ $(UPROGS) - cargo +nightly -Z unstable-options -C $R clean # try to generate a unique GDB port GDBPORT = $(shell expr `id -u` % 5000 + 25000) @@ -156,18 +124,18 @@ ifndef CPUS CPUS := 3 endif -QEMUOPTS = -machine virt -bios none -kernel $K/kernel -m 128M -smp $(CPUS) -nographic +QEMUOPTS = -machine virt -bios none -kernel kernel/kernel -m 128M -smp $(CPUS) -nographic QEMUOPTS += -global virtio-mmio.force-legacy=false QEMUOPTS += -drive file=fs.img,if=none,format=raw,id=x0 QEMUOPTS += -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 -qemu: $K/kernel fs.img +qemu: kernel/kernel fs.img $(QEMU) $(QEMUOPTS) .gdbinit: .gdbinit.tmpl-riscv sed "s/:1234/:$(GDBPORT)/" < $^ > $@ -qemu-gdb: $K/kernel .gdbinit fs.img +qemu-gdb: kernel/kernel .gdbinit fs.img @echo "*** Now run 'gdb' in another window." 1>&2 $(QEMU) $(QEMUOPTS) -S $(QEMUGDB) diff --git a/kernel/Makefile b/kernel/Makefile new file mode 100644 index 0000000..db0f8a7 --- /dev/null +++ b/kernel/Makefile @@ -0,0 +1,88 @@ +R=rustkernel + +KERNEL_SOURCES = \ + entry.c \ + vm.c \ + proc.c \ + swtch.c \ + trampoline.c \ + bio.c \ + fs.c \ + log.c \ + file.c \ + pipe.c \ + exec.c \ + sysfile.c \ + kernelvec.c \ + virtio_disk.c + +OBJS = $(KERNEL_SOURCES:%.c=%.o) + +# riscv64-unknown-elf- or riscv64-linux-gnu- +# perhaps in /opt/riscv/bin +#TOOLPREFIX = + +# Try to infer the correct TOOLPREFIX if not set +ifndef TOOLPREFIX +TOOLPREFIX := $(shell if riscv64-unknown-elf-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \ + then echo 'riscv64-unknown-elf-'; \ + elif riscv64-linux-gnu-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \ + then echo 'riscv64-linux-gnu-'; \ + elif riscv64-unknown-linux-gnu-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \ + then echo 'riscv64-unknown-linux-gnu-'; \ + else echo "***" 1>&2; \ + echo "*** Error: Couldn't find a riscv64 version of GCC/binutils." 1>&2; \ + echo "*** To turn off this error, run 'gmake TOOLPREFIX= ...'." 1>&2; \ + echo "***" 1>&2; exit 1; fi) +endif + +CC = $(TOOLPREFIX)gcc +LD = $(TOOLPREFIX)ld +OBJCOPY = $(TOOLPREFIX)objcopy +# OBJDUMP = $(TOOLPREFIX)objdump + +CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2 +CFLAGS += -MD +CFLAGS += -mcmodel=medany +CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax +CFLAGS += -I. +CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector) + +TARGET_TRIPLE = riscv64gc-unknown-none-elf +RUST_LIB = $R/target/$(TARGET_TRIPLE)/release/librustkernel.a + +# Disable PIE when possible (for Ubuntu 16.10 toolchain) +ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),) +CFLAGS += -fno-pie -no-pie +endif +ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),) +CFLAGS += -fno-pie -nopie +endif + +LDFLAGS = -z max-page-size=4096 + +.PHONY: clean + +kernel: $(OBJS) kernel.ld initcode $(RUST_LIB) + $(LD) $(LDFLAGS) -T kernel.ld -o kernel $(OBJS) $(RUST_LIB) + # $(OBJDUMP) -S kernel > kernel.asm + # $(OBJDUMP) -t kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > kernel.sym + +$(RUST_LIB): $(shell find $R/src -type f) $R/Cargo.toml + cargo +nightly -Z unstable-options -C $R build --release + # $(OBJDUMP) -S $(RUST_LIB) > $(RUST_LIB).asm + +initcode: initcode.S + $(CC) $(CFLAGS) -march=rv64g -nostdinc -I. -Ikernel -c initcode.S -o initcode.o + $(LD) $(LDFLAGS) -N -e start -Ttext 0 -o initcode.out initcode.o + $(OBJCOPY) -S -O binary initcode.out initcode + # $(OBJDUMP) -S initcode.o > initcode.asm + +%.o: %.c *.h + $(CC) $(CFLAGS) -c $< + +clean: + rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \ + *.o *.a *.d *.asm *.sym *.gch \ + initcode initcode.out kernel + cargo +nightly -Z unstable-options -C $R clean diff --git a/kernel/buf.h b/kernel/buf.h index 4616e9e..a3c4300 100644 --- a/kernel/buf.h +++ b/kernel/buf.h @@ -1,3 +1,7 @@ +#include "types.h" +#include "param.h" +#include "sleeplock.h" + struct buf { int valid; // has data been read from disk? int disk; // does disk "own" buf? diff --git a/kernel/defs.h b/kernel/defs.h index fff6729..5151a7e 100644 --- a/kernel/defs.h +++ b/kernel/defs.h @@ -1,4 +1,6 @@ #pragma once +#include "types.h" +#include "riscv.h" struct buf; struct context; diff --git a/kernel/elf.h b/kernel/elf.h index 84555fa..f616b9e 100644 --- a/kernel/elf.h +++ b/kernel/elf.h @@ -1,3 +1,5 @@ +#include "types.h" + // Format of an ELF executable file #define ELF_MAGIC 0x464C457FU // "\x7FELF" in little endian diff --git a/kernel/file.h b/kernel/file.h index b076d1d..fca7e6c 100644 --- a/kernel/file.h +++ b/kernel/file.h @@ -1,3 +1,6 @@ +#include "types.h" +#include "param.h" + struct file { enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE } type; int ref; // reference count diff --git a/kernel/fs.h b/kernel/fs.h index 139dcc9..6d967a4 100644 --- a/kernel/fs.h +++ b/kernel/fs.h @@ -1,6 +1,7 @@ // On-disk file system format. // Both the kernel and user programs use this header file. +#include "types.h" #define ROOTINO 1 // root i-number #define BSIZE 1024 // block size diff --git a/user/initcode.S b/kernel/initcode.S similarity index 100% rename from user/initcode.S rename to kernel/initcode.S diff --git a/kernel/proc.h b/kernel/proc.h index 46c59df..4ec8078 100644 --- a/kernel/proc.h +++ b/kernel/proc.h @@ -1,3 +1,8 @@ +#include "types.h" +#include "param.h" +#include "riscv.h" +#include "spinlock.h" + // Saved registers for kernel context switches. struct context { uint64 ra; diff --git a/kernel/riscv.h b/kernel/riscv.h index b68d134..b4dc11e 100644 --- a/kernel/riscv.h +++ b/kernel/riscv.h @@ -1,4 +1,5 @@ #ifndef __ASSEMBLER__ +#pragma once #include "./types.h" diff --git a/kernel/sleeplock.h b/kernel/sleeplock.h index 110e6f3..951be20 100644 --- a/kernel/sleeplock.h +++ b/kernel/sleeplock.h @@ -1,3 +1,7 @@ +#include "types.h" +#include "spinlock.h" +#pragma once + // Long-term locks for processes struct sleeplock { uint locked; // Is the lock held? diff --git a/kernel/spinlock.h b/kernel/spinlock.h index 4392820..d4c1d65 100644 --- a/kernel/spinlock.h +++ b/kernel/spinlock.h @@ -1,3 +1,6 @@ +#include "types.h" +#pragma once + // Mutual exclusion lock. struct spinlock { uint locked; // Is the lock held? diff --git a/kernel/stat.h b/kernel/stat.h index 19543af..54e2ef1 100644 --- a/kernel/stat.h +++ b/kernel/stat.h @@ -1,3 +1,5 @@ +#include "types.h" + #define T_DIR 1 // Directory #define T_FILE 2 // File #define T_DEVICE 3 // Device diff --git a/kernel/virtio.h b/kernel/virtio.h index 96272b4..ac8a5cd 100644 --- a/kernel/virtio.h +++ b/kernel/virtio.h @@ -7,6 +7,8 @@ // https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.pdf // +#include "types.h" + // virtio mmio control registers, mapped starting at 0x10001000. // from qemu virtio_mmio.h #define VIRTIO_MMIO_MAGIC_VALUE 0x000 // 0x74726976 diff --git a/mkfs/Makefile b/mkfs/Makefile new file mode 100644 index 0000000..1b661aa --- /dev/null +++ b/mkfs/Makefile @@ -0,0 +1,9 @@ +K=../kernel + +.PHONY: clean + +mkfs: mkfs.c $K/fs.h $K/param.h + gcc -Werror -Wall -I. -o mkfs mkfs.c + +clean: + rm -f mkfs diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c index c7585b1..a2ec1a0 100644 --- a/mkfs/mkfs.c +++ b/mkfs/mkfs.c @@ -6,10 +6,10 @@ #include #define stat xv6_stat // avoid clash with host struct stat -#include "kernel/types.h" -#include "kernel/fs.h" -#include "kernel/stat.h" -#include "kernel/param.h" +#include "../kernel/types.h" +#include "../kernel/fs.h" +#include "../kernel/stat.h" +#include "../kernel/param.h" #ifndef static_assert #define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0) diff --git a/user/Makefile b/user/Makefile new file mode 100644 index 0000000..9471128 --- /dev/null +++ b/user/Makefile @@ -0,0 +1,62 @@ +U=user +P=programs + +# riscv64-unknown-elf- or riscv64-linux-gnu- +# perhaps in /opt/riscv/bin +#TOOLPREFIX = + +# Try to infer the correct TOOLPREFIX if not set +ifndef TOOLPREFIX +TOOLPREFIX := $(shell if riscv64-unknown-elf-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \ + then echo 'riscv64-unknown-elf-'; \ + elif riscv64-linux-gnu-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \ + then echo 'riscv64-linux-gnu-'; \ + elif riscv64-unknown-linux-gnu-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \ + then echo 'riscv64-unknown-linux-gnu-'; \ + else echo "***" 1>&2; \ + echo "*** Error: Couldn't find a riscv64 version of GCC/binutils." 1>&2; \ + echo "*** To turn off this error, run 'gmake TOOLPREFIX= ...'." 1>&2; \ + echo "***" 1>&2; exit 1; fi) +endif + +CC = $(TOOLPREFIX)gcc +AS = $(TOOLPREFIX)gas +LD = $(TOOLPREFIX)ld +OBJCOPY = $(TOOLPREFIX)objcopy +OBJDUMP = $(TOOLPREFIX)objdump + +CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2 +CFLAGS += -MD +CFLAGS += -mcmodel=medany +CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax +CFLAGS += -I. +CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector) + +# Disable PIE when possible (for Ubuntu 16.10 toolchain) +ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),) +CFLAGS += -fno-pie -no-pie +endif +ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),) +CFLAGS += -fno-pie -nopie +endif + +LDFLAGS = -z max-page-size=4096 + +.PHONY: build clean + +ULIB = ulib.o usys.o printf.o umalloc.o + +build: $(ULIB) + +%.o: %.c *.h + $(CC) $(CFLAGS) -c $< + +usys.S : usys.pl + perl usys.pl > usys.S + +usys.o : usys.S + $(CC) $(CFLAGS) -c -o usys.o usys.S + +clean: + rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \ + *.o *.a *.d *.asm *.sym usys.S diff --git a/user/printf.c b/user/printf.c index 5c5c782..fa13ee2 100644 --- a/user/printf.c +++ b/user/printf.c @@ -1,6 +1,6 @@ -#include "kernel/types.h" -#include "kernel/stat.h" -#include "user/user.h" +#include "../kernel/types.h" +#include "../kernel/stat.h" +#include "user.h" #include diff --git a/user/ulib.c b/user/ulib.c index c7b66c4..9089326 100644 --- a/user/ulib.c +++ b/user/ulib.c @@ -1,7 +1,7 @@ -#include "kernel/types.h" -#include "kernel/stat.h" -#include "kernel/fcntl.h" -#include "user/user.h" +#include "../kernel/types.h" +#include "../kernel/stat.h" +#include "../kernel/fcntl.h" +#include "user.h" // // wrapper so that it's OK if main() does not call exit(). diff --git a/user/umalloc.c b/user/umalloc.c index 2092a32..7d72000 100644 --- a/user/umalloc.c +++ b/user/umalloc.c @@ -1,7 +1,7 @@ -#include "kernel/types.h" -#include "kernel/stat.h" -#include "user/user.h" -#include "kernel/param.h" +#include "../kernel/types.h" +#include "../kernel/stat.h" +#include "../kernel/param.h" +#include "user.h" // Memory allocator by Kernighan and Ritchie, // The C programming Language, 2nd ed. Section 8.7. diff --git a/user/usys.pl b/user/usys.pl index 461806c..d6fc93c 100755 --- a/user/usys.pl +++ b/user/usys.pl @@ -4,7 +4,7 @@ print "# generated by usys.pl - do not edit\n"; -print "#include \"kernel/syscall.h\"\n"; +print "#include \"../kernel/syscall.h\"\n"; sub entry { my $name = shift;