restructure makefile into multiple files

This commit is contained in:
Garen Tyler 2023-10-22 13:43:27 -06:00
parent 16924f75fd
commit 2d51463c56
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
22 changed files with 232 additions and 75 deletions

3
.gitignore vendored
View File

@ -6,13 +6,14 @@ _*
*.asm *.asm
*.sym *.sym
*.img *.img
*.gch
vectors.S vectors.S
bootblock bootblock
entryother entryother
initcode initcode
initcode.out initcode.out
kernelmemfs kernelmemfs
mkfs mkfs/mkfs
kernel/kernel kernel/kernel
user/usys.S user/usys.S
.gdbinit .gdbinit

View File

@ -1,24 +1,8 @@
K=kernel K=kernel
R=$K/rustkernel M=mkfs
U=user U=user
P=programs 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- # riscv64-unknown-elf- or riscv64-linux-gnu-
# perhaps in /opt/riscv/bin # perhaps in /opt/riscv/bin
#TOOLPREFIX = #TOOLPREFIX =
@ -51,7 +35,6 @@ CFLAGS += -mcmodel=medany
CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
CFLAGS += -I. CFLAGS += -I.
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector) 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) # Disable PIE when possible (for Ubuntu 16.10 toolchain)
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),) ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
@ -63,49 +46,35 @@ endif
LDFLAGS = -z max-page-size=4096 LDFLAGS = -z max-page-size=4096
$K/kernel: $(OBJS) $K/kernel.ld $U/initcode $R/src .PHONY: build kernel ulib mkfs clean
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
$U/initcode: $U/initcode.S build: kernel fs.img
$(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
tags: $(OBJS) _init kernel:
etags *.S *.c $(MAKE) -C $K
ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o ulib:
$(MAKE) -C $U
# %.a: %.rs mkfs:
# $(RUSTC) $(RUSTFLAGS) --crate-type staticlib -o $@ -L $K $^ $(MAKE) -C $M
USERLIBS = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o
%.o: %.c *.h %.o: %.c *.h
$(CC) $(CFLAGS) -c $^ $(CC) $(CFLAGS) -c $<
_%: %.o $(ULIB) _%: %.o ulib
$(LD) $(LDFLAGS) -T $U/user.ld -o $@ $^ $(LD) $(LDFLAGS) -T $U/user.ld -o $@ $< $(USERLIBS)
$(OBJDUMP) -S $@ > $*.asm # $(OBJDUMP) -S $@ > $*.asm
$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym # $(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym
$U/usys.S : $U/usys.pl $U/_forktest: $U/forktest.o ulib
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)
# forktest has less library code linked in - needs to be small # forktest has less library code linked in - needs to be small
# in order to be able to max out the proc table. # 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 $(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 # 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 # that disk image changes after first build are persistent until clean. More
@ -132,19 +101,18 @@ UPROGS=\
$P/_zombie\ $P/_zombie\
$P/_shutdown\ $P/_shutdown\
fs.img: mkfs/mkfs README $(UPROGS) fs.img: mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS) mkfs/mkfs fs.img README $(UPROGS)
-include kernel/*.d user/*.d -include kernel/*.d user/*.d
clean: clean:
$(MAKE) -C $K clean
$(MAKE) -C $M clean
$(MAKE) -C $U clean
rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \ rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \
*/*.o */*.a */*.d */*.asm */*.sym \ */*.o */*.a */*.d */*.asm */*.sym fs.img .gdbinit \
$U/initcode $U/initcode.out $K/kernel fs.img \
mkfs/mkfs .gdbinit \
$U/usys.S \
$(UPROGS) $(UPROGS)
cargo +nightly -Z unstable-options -C $R clean
# try to generate a unique GDB port # try to generate a unique GDB port
GDBPORT = $(shell expr `id -u` % 5000 + 25000) GDBPORT = $(shell expr `id -u` % 5000 + 25000)
@ -156,18 +124,18 @@ ifndef CPUS
CPUS := 3 CPUS := 3
endif 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 += -global virtio-mmio.force-legacy=false
QEMUOPTS += -drive file=fs.img,if=none,format=raw,id=x0 QEMUOPTS += -drive file=fs.img,if=none,format=raw,id=x0
QEMUOPTS += -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 QEMUOPTS += -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0
qemu: $K/kernel fs.img qemu: kernel/kernel fs.img
$(QEMU) $(QEMUOPTS) $(QEMU) $(QEMUOPTS)
.gdbinit: .gdbinit.tmpl-riscv .gdbinit: .gdbinit.tmpl-riscv
sed "s/:1234/:$(GDBPORT)/" < $^ > $@ 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 @echo "*** Now run 'gdb' in another window." 1>&2
$(QEMU) $(QEMUOPTS) -S $(QEMUGDB) $(QEMU) $(QEMUOPTS) -S $(QEMUGDB)

88
kernel/Makefile Normal file
View File

@ -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

View File

@ -1,3 +1,7 @@
#include "types.h"
#include "param.h"
#include "sleeplock.h"
struct buf { struct buf {
int valid; // has data been read from disk? int valid; // has data been read from disk?
int disk; // does disk "own" buf? int disk; // does disk "own" buf?

View File

@ -1,4 +1,6 @@
#pragma once #pragma once
#include "types.h"
#include "riscv.h"
struct buf; struct buf;
struct context; struct context;

View File

@ -1,3 +1,5 @@
#include "types.h"
// Format of an ELF executable file // Format of an ELF executable file
#define ELF_MAGIC 0x464C457FU // "\x7FELF" in little endian #define ELF_MAGIC 0x464C457FU // "\x7FELF" in little endian

View File

@ -1,3 +1,6 @@
#include "types.h"
#include "param.h"
struct file { struct file {
enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE } type; enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE } type;
int ref; // reference count int ref; // reference count

View File

@ -1,6 +1,7 @@
// On-disk file system format. // On-disk file system format.
// Both the kernel and user programs use this header file. // Both the kernel and user programs use this header file.
#include "types.h"
#define ROOTINO 1 // root i-number #define ROOTINO 1 // root i-number
#define BSIZE 1024 // block size #define BSIZE 1024 // block size

View File

@ -1,3 +1,8 @@
#include "types.h"
#include "param.h"
#include "riscv.h"
#include "spinlock.h"
// Saved registers for kernel context switches. // Saved registers for kernel context switches.
struct context { struct context {
uint64 ra; uint64 ra;

View File

@ -1,4 +1,5 @@
#ifndef __ASSEMBLER__ #ifndef __ASSEMBLER__
#pragma once
#include "./types.h" #include "./types.h"

View File

@ -1,3 +1,7 @@
#include "types.h"
#include "spinlock.h"
#pragma once
// Long-term locks for processes // Long-term locks for processes
struct sleeplock { struct sleeplock {
uint locked; // Is the lock held? uint locked; // Is the lock held?

View File

@ -1,3 +1,6 @@
#include "types.h"
#pragma once
// Mutual exclusion lock. // Mutual exclusion lock.
struct spinlock { struct spinlock {
uint locked; // Is the lock held? uint locked; // Is the lock held?

View File

@ -1,3 +1,5 @@
#include "types.h"
#define T_DIR 1 // Directory #define T_DIR 1 // Directory
#define T_FILE 2 // File #define T_FILE 2 // File
#define T_DEVICE 3 // Device #define T_DEVICE 3 // Device

View File

@ -7,6 +7,8 @@
// https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.pdf // https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.pdf
// //
#include "types.h"
// virtio mmio control registers, mapped starting at 0x10001000. // virtio mmio control registers, mapped starting at 0x10001000.
// from qemu virtio_mmio.h // from qemu virtio_mmio.h
#define VIRTIO_MMIO_MAGIC_VALUE 0x000 // 0x74726976 #define VIRTIO_MMIO_MAGIC_VALUE 0x000 // 0x74726976

9
mkfs/Makefile Normal file
View File

@ -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

View File

@ -6,10 +6,10 @@
#include <assert.h> #include <assert.h>
#define stat xv6_stat // avoid clash with host struct stat #define stat xv6_stat // avoid clash with host struct stat
#include "kernel/types.h" #include "../kernel/types.h"
#include "kernel/fs.h" #include "../kernel/fs.h"
#include "kernel/stat.h" #include "../kernel/stat.h"
#include "kernel/param.h" #include "../kernel/param.h"
#ifndef static_assert #ifndef static_assert
#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0) #define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)

62
user/Makefile Normal file
View File

@ -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

View File

@ -1,6 +1,6 @@
#include "kernel/types.h" #include "../kernel/types.h"
#include "kernel/stat.h" #include "../kernel/stat.h"
#include "user/user.h" #include "user.h"
#include <stdarg.h> #include <stdarg.h>

View File

@ -1,7 +1,7 @@
#include "kernel/types.h" #include "../kernel/types.h"
#include "kernel/stat.h" #include "../kernel/stat.h"
#include "kernel/fcntl.h" #include "../kernel/fcntl.h"
#include "user/user.h" #include "user.h"
// //
// wrapper so that it's OK if main() does not call exit(). // wrapper so that it's OK if main() does not call exit().

View File

@ -1,7 +1,7 @@
#include "kernel/types.h" #include "../kernel/types.h"
#include "kernel/stat.h" #include "../kernel/stat.h"
#include "user/user.h" #include "../kernel/param.h"
#include "kernel/param.h" #include "user.h"
// Memory allocator by Kernighan and Ritchie, // Memory allocator by Kernighan and Ritchie,
// The C programming Language, 2nd ed. Section 8.7. // The C programming Language, 2nd ed. Section 8.7.

View File

@ -4,7 +4,7 @@
print "# generated by usys.pl - do not edit\n"; print "# generated by usys.pl - do not edit\n";
print "#include \"kernel/syscall.h\"\n"; print "#include \"../kernel/syscall.h\"\n";
sub entry { sub entry {
my $name = shift; my $name = shift;