separate source into kernel/ user/ mkfs/

This commit is contained in:
Robert Morris 2019-06-11 09:57:14 -04:00
parent 91ba81110a
commit 5753553213
72 changed files with 157 additions and 296 deletions

View file

@ -1,6 +0,0 @@
set confirm off
python
gdb.execute("target remote localhost:26000")
gdb.execute("set architecture i386")
gdb.execute("symbol-file kernel")
gdb.execute("break *0x7c00")

View file

@ -1,4 +1,4 @@
set confirm off set confirm off
set architecture riscv set architecture riscv
target remote 127.0.0.1:1234 target remote 127.0.0.1:1234
symbol-file kernel symbol-file kernel/kernel

View file

@ -1,18 +0,0 @@
#if you would like to use gdb in 32bit mode, comment out lines 8 and 15, then uncomment
#the lines after. Note this will only work properly until 64bit mode is enabled in entry.S
python
gdb.execute("set architecture i386:x86-64:intel")
gdb.execute("target remote localhost:26000")
gdb.execute("symbol-file kernel")
gdb.execute("break start64")
#gdb.execute("break *0x7c00")
try:
gdb.execute("continue")
except:
pass
gdb.execute("disconnect")
gdb.execute("set architecture i386:x86-64")
#gdb.execute("set architecture i386")
gdb.execute("target remote localhost:26000")
gdb.execute("delete break 1")

1
.gitignore vendored
View file

@ -10,7 +10,6 @@ bootblock
entryother entryother
initcode initcode
initcode.out initcode.out
kernel
kernelmemfs kernelmemfs
mkfs mkfs
.gdbinit .gdbinit

148
Makefile
View file

@ -1,29 +1,33 @@
K=kernel
U=user
OBJS = \ OBJS = \
start.o \ $K/entry.o \
console.o \ $K/start.o \
uart.o \ $K/console.o \
kalloc.o \ $K/uart.o \
spinlock.o \ $K/kalloc.o \
string.o \ $K/spinlock.o \
main.o \ $K/string.o \
vm.o \ $K/main.o \
proc.o \ $K/vm.o \
swtch.o \ $K/proc.o \
trampoline.o \ $K/swtch.o \
trap.o \ $K/trampoline.o \
syscall.o \ $K/trap.o \
sysproc.o \ $K/syscall.o \
bio.o \ $K/sysproc.o \
fs.o \ $K/bio.o \
log.o \ $K/fs.o \
sleeplock.o \ $K/log.o \
file.o \ $K/sleeplock.o \
pipe.o \ $K/file.o \
ramdisk.o \ $K/pipe.o \
exec.o \ $K/ramdisk.o \
sysfile.o \ $K/exec.o \
kernelvec.o \ $K/sysfile.o \
plic.o $K/kernelvec.o \
$K/plic.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
@ -53,6 +57,7 @@ OBJDUMP = $(TOOLPREFIX)objdump
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb
CFLAGS += -mcmodel=medany CFLAGS += -mcmodel=medany
CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax 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) 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) # Disable PIE when possible (for Ubuntu 16.10 toolchain)
@ -65,41 +70,41 @@ endif
LDFLAGS = -z max-page-size=4096 LDFLAGS = -z max-page-size=4096
kernel: $(OBJS) entry.o kernel.ld initcode $K/kernel: $(OBJS) $K/kernel.ld $U/initcode
$(LD) $(LDFLAGS) -T kernel.ld -o kernel entry.o $(OBJS) $(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS)
$(OBJDUMP) -S kernel > kernel.asm $(OBJDUMP) -S $K/kernel > $K/kernel.asm
$(OBJDUMP) -t kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > kernel.sym $(OBJDUMP) -t $K/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $K/kernel.sym
initcode: initcode.S $U/initcode: $U/initcode.S
$(CC) $(CFLAGS) -nostdinc -I. -c initcode.S $(CC) $(CFLAGS) -nostdinc -I. -Ikernel -c $U/initcode.S -o $U/initcode.o
$(LD) $(LDFLAGS) -N -e start -Ttext 0 -o initcode.out initcode.o $(LD) $(LDFLAGS) -N -e start -Ttext 0 -o $U/initcode.out $U/initcode.o
$(OBJCOPY) -S -O binary initcode.out initcode $(OBJCOPY) -S -O binary $U/initcode.out $U/initcode
$(OBJDUMP) -S initcode.o > initcode.asm $(OBJDUMP) -S $U/initcode.o > $U/initcode.asm
tags: $(OBJS) entryother.S _init tags: $(OBJS) _init
etags *.S *.c etags *.S *.c
vectors.S: vectors.pl ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o
./vectors.pl > vectors.S
ULIB = ulib.o usys.o printf.o umalloc.o
_%: %.o $(ULIB) _%: %.o $(ULIB)
$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $@ $^ $(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $@ $^
$(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
usys.S : usys.pl $U/usys.S : $U/usys.pl
perl ./usys.pl > usys.S perl $U/usys.pl > $U/usys.S
_forktest: forktest.o $(ULIB) $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 _forktest forktest.o ulib.o usys.o $(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $U/_forktest $U/forktest.o $U/ulib.o $U/usys.o
$(OBJDUMP) -S _forktest > forktest.asm $(OBJDUMP) -S $U/_forktest > $U/forktest.asm
mkfs: mkfs.c fs.h mkfs/mkfs: mkfs/mkfs.c $K/fs.h
gcc -Werror -Wall -o mkfs mkfs.c 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
@ -108,32 +113,33 @@ mkfs: mkfs.c fs.h
.PRECIOUS: %.o .PRECIOUS: %.o
UPROGS=\ UPROGS=\
_cat\ $U/_cat\
_echo\ $U/_echo\
_forktest\ $U/_forktest\
_grep\ $U/_grep\
_init\ $U/_init\
_kill\ $U/_kill\
_ln\ $U/_ln\
_ls\ $U/_ls\
_mkdir\ $U/_mkdir\
_rm\ $U/_rm\
_sh\ $U/_sh\
_stressfs\ $U/_stressfs\
_usertests\ $U/_usertests\
_wc\ $U/_wc\
_zombie\ $U/_zombie\
fs.img: mkfs README $(UPROGS) fs.img: mkfs/mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS) mkfs/mkfs fs.img README $(UPROGS)
-include *.d -include *.d
clean: clean:
rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \ rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \
*.o *.d *.asm *.sym vectors.S bootblock entryother \ */*.o */*.d */*.asm */*.sym \
initcode initcode.out kernel fs.img kernelmemfs \ $U/initcode $U/initcode.out $K/kernel fs.img \
mkfs .gdbinit \ mkfs/mkfs .gdbinit \
$U/usys.S \
$(UPROGS) $(UPROGS)
# make a printout # make a printout
@ -155,23 +161,19 @@ QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
ifndef CPUS ifndef CPUS
CPUS := 3 CPUS := 3
endif endif
QEMUOPTS = -machine virt -kernel kernel -m 3G -smp $(CPUS) -nographic QEMUOPTS = -machine virt -kernel $K/kernel -m 3G -smp $(CPUS) -nographic
QEMUOPTS += -initrd fs.img QEMUOPTS += -initrd fs.img
qemu: kernel fs.img qemu: $K/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: kernel .gdbinit fs.img qemu-gdb: $K/kernel .gdbinit fs.img
@echo "*** Now run 'gdb'." 1>&2 @echo "*** Now run 'gdb'." 1>&2
$(QEMU) $(QEMUOPTS) -S $(QEMUGDB) $(QEMU) $(QEMUOPTS) -S $(QEMUGDB)
qemu-nox-gdb: fs.img kernel .gdbinit
@echo "*** Now run 'gdb'." 1>&2
$(QEMU) -nographic $(QEMUOPTS) -S $(QEMUGDB)
# CUT HERE # CUT HERE
# prepare dist for students # prepare dist for students
# after running make dist, probably want to # after running make dist, probably want to

View file

@ -1,76 +0,0 @@
// The I/O APIC manages hardware interrupts for an SMP system.
// http://www.intel.com/design/chipsets/datashts/29056601.pdf
// See also picirq.c.
#include "types.h"
#include "defs.h"
#include "memlayout.h"
#include "traps.h"
#define IOAPIC 0xFEC00000 // Default physical address of IO APIC
#define REG_ID 0x00 // Register index: ID
#define REG_VER 0x01 // Register index: version
#define REG_TABLE 0x10 // Redirection table base
// The redirection table starts at REG_TABLE and uses
// two registers to configure each interrupt.
// The first (low) register in a pair contains configuration bits.
// The second (high) register contains a bitmask telling which
// CPUs can serve that interrupt.
#define INT_DISABLED 0x00010000 // Interrupt disabled
#define INT_LEVEL 0x00008000 // Level-triggered (vs edge-)
#define INT_ACTIVELOW 0x00002000 // Active low (vs high)
#define INT_LOGICAL 0x00000800 // Destination is CPU id (vs APIC ID)
volatile struct ioapic *ioapic;
// IO APIC MMIO structure: write reg, then read or write data.
struct ioapic {
uint reg;
uint pad[3];
uint data;
};
static uint
ioapicread(int reg)
{
ioapic->reg = reg;
return ioapic->data;
}
static void
ioapicwrite(int reg, uint data)
{
ioapic->reg = reg;
ioapic->data = data;
}
void
ioapicinit(void)
{
int i, id, maxintr;
ioapic = P2V((volatile struct ioapic*)IOAPIC);
maxintr = (ioapicread(REG_VER) >> 16) & 0xFF;
id = ioapicread(REG_ID) >> 24;
if(id != ioapicid)
cprintf("ioapicinit: id isn't equal to ioapicid; not a MP\n");
// Mark all interrupts edge-triggered, active high, disabled,
// and not routed to any CPUs.
for(i = 0; i <= maxintr; i++){
ioapicwrite(REG_TABLE+2*i, INT_DISABLED | (T_IRQ0 + i));
ioapicwrite(REG_TABLE+2*i+1, 0);
}
}
void
ioapicenable(int irq, int cpunum)
{
// Mark interrupt edge-triggered, active high,
// enabled, and routed to the given cpunum,
// which happens to be that cpu's APIC ID.
ioapicwrite(REG_TABLE+2*irq, T_IRQ0 + irq);
ioapicwrite(REG_TABLE+2*irq+1, cpunum << 24);
}

View file

View file

View file

View file

View file

View file

View file

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 "types.h" #include "kernel/types.h"
#include "fs.h" #include "kernel/fs.h"
#include "stat.h" #include "kernel/stat.h"
#include "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)
@ -129,7 +129,14 @@ main(int argc, char *argv[])
iappend(rootino, &de, sizeof(de)); iappend(rootino, &de, sizeof(de));
for(i = 2; i < argc; i++){ for(i = 2; i < argc; i++){
assert(index(argv[i], '/') == 0); // get rid of "user/"
char *shortname;
if(strncmp(argv[i], "user/", 5) == 0)
shortname = argv[i] + 5;
else
shortname = argv[i];
assert(index(shortname, '/') == 0);
if((fd = open(argv[i], 0)) < 0){ if((fd = open(argv[i], 0)) < 0){
perror(argv[i]); perror(argv[i]);
@ -140,14 +147,14 @@ main(int argc, char *argv[])
// The binaries are named _rm, _cat, etc. to keep the // The binaries are named _rm, _cat, etc. to keep the
// build operating system from trying to execute them // build operating system from trying to execute them
// in place of system binaries like rm and cat. // in place of system binaries like rm and cat.
if(argv[i][0] == '_') if(shortname[0] == '_')
++argv[i]; shortname += 1;
inum = ialloc(T_FILE); inum = ialloc(T_FILE);
bzero(&de, sizeof(de)); bzero(&de, sizeof(de));
de.inum = xshort(inum); de.inum = xshort(inum);
strncpy(de.name, argv[i], DIRSIZ); strncpy(de.name, shortname, DIRSIZ);
iappend(rootino, &de, sizeof(de)); iappend(rootino, &de, sizeof(de));
while((cc = read(fd, buf, sizeof(buf))) > 0) while((cc = read(fd, buf, sizeof(buf))) > 0)

View file

@ -1,6 +1,6 @@
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
char buf[512]; char buf[512];

View file

@ -1,6 +1,6 @@
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
int int
main(int argc, char *argv[]) main(int argc, char *argv[])

View file

@ -1,9 +1,9 @@
// Test that fork fails gracefully. // Test that fork fails gracefully.
// Tiny executable so that the limit can be filling the proc table. // Tiny executable so that the limit can be filling the proc table.
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
#define N 1000 #define N 1000

View file

@ -1,8 +1,8 @@
// Simple grep. Only supports ^ . * $ operators. // Simple grep. Only supports ^ . * $ operators.
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
char buf[1024]; char buf[1024];
int match(char*, char*); int match(char*, char*);

View file

@ -1,9 +1,9 @@
// init: The initial user-level program // init: The initial user-level program
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
#include "fcntl.h" #include "kernel/fcntl.h"
char *argv[] = { "sh", 0 }; char *argv[] = { "sh", 0 };

View file

@ -1,6 +1,6 @@
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
int int
main(int argc, char **argv) main(int argc, char **argv)

View file

@ -1,6 +1,6 @@
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
int int
main(int argc, char *argv[]) main(int argc, char *argv[])

View file

@ -1,7 +1,7 @@
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
#include "fs.h" #include "kernel/fs.h"
char* char*
fmtname(char *path) fmtname(char *path)

View file

@ -1,6 +1,6 @@
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
int int
main(int argc, char *argv[]) main(int argc, char *argv[])

View file

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

View file

@ -1,6 +1,6 @@
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
int int
main(int argc, char *argv[]) main(int argc, char *argv[])

View file

@ -1,8 +1,8 @@
// Shell. // Shell.
#include "types.h" #include "kernel/types.h"
#include "user.h" #include "user/user.h"
#include "fcntl.h" #include "kernel/fcntl.h"
// Parsed command representation // Parsed command representation
#define EXEC 1 #define EXEC 1

View file

@ -7,11 +7,11 @@
// for (i = 0; i < 40000; i++) // for (i = 0; i < 40000; i++)
// asm volatile(""); // asm volatile("");
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
#include "fs.h" #include "kernel/fs.h"
#include "fcntl.h" #include "kernel/fcntl.h"
int int
main(int argc, char *argv[]) main(int argc, char *argv[])

View file

@ -1,7 +1,7 @@
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "fcntl.h" #include "kernel/fcntl.h"
#include "user.h" #include "user/user.h"
char* char*
strcpy(char *s, const char *t) strcpy(char *s, const char *t)

View file

@ -1,7 +1,7 @@
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
#include "param.h" #include "kernel/param.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

View file

@ -1,11 +1,11 @@
#include "param.h" #include "kernel/param.h"
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
#include "fs.h" #include "kernel/fs.h"
#include "fcntl.h" #include "kernel/fcntl.h"
#include "syscall.h" #include "kernel/syscall.h"
#include "memlayout.h" #include "kernel/memlayout.h"
char buf[8192]; char buf[8192];
char name[3]; char name[3];

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 \"syscall.h\"\n"; print "#include \"kernel/syscall.h\"\n";
sub entry { sub entry {
my $name = shift; my $name = shift;

View file

@ -1,6 +1,6 @@
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
char buf[512]; char buf[512];

View file

@ -1,9 +1,9 @@
// Create a zombie process that // Create a zombie process that
// must be reparented at exit. // must be reparented at exit.
#include "types.h" #include "kernel/types.h"
#include "stat.h" #include "kernel/stat.h"
#include "user.h" #include "user/user.h"
int int
main(void) main(void)

View file

@ -1,47 +0,0 @@
#!/usr/bin/perl -w
# Generate vectors.S, the trap/interrupt entry points.
# There has to be one entry point per interrupt number
# since otherwise there's no way for trap() to discover
# the interrupt number.
print "# generated by vectors.pl - do not edit\n";
print "# handlers\n";
print ".globl alltraps\n";
for(my $i = 0; $i < 256; $i++){
print ".globl vector$i\n";
print "vector$i:\n";
if(!($i == 8 || ($i >= 10 && $i <= 14) || $i == 17)){
print " push \$0\n";
}
print " push \$$i\n";
print " jmp alltraps\n";
}
print "\n# vector table\n";
print ".data\n";
print ".globl vectors\n";
print "vectors:\n";
for(my $i = 0; $i < 256; $i++){
print " .quad vector$i\n";
}
# sample output:
# # handlers
# .globl alltraps
# .globl vector0
# vector0:
# push $0
# push $0
# jmp alltraps
# ...
#
# # vector table
# .data
# .globl vectors
# vectors:
# .quad vector0
# .quad vector1
# .quad vector2
# ...