Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dhello20230928 #197

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
60fc1d7
Added shutdown system call, snprintf() function, and start of ps syst…
Make-IT-Wright Mar 8, 2023
a190d58
Added shutdown system call, snprintf() function, and start of ps syst…
Make-IT-Wright Mar 8, 2023
9308d59
Added shutdown system call, snprintf() function, and start of ps syst…
Make-IT-Wright Mar 8, 2023
8b332e4
Added linker script
Make-IT-Wright Mar 8, 2023
ee57661
Pass int argument to ps()
Make-IT-Wright Mar 8, 2023
a02bda3
Added !
Make-IT-Wright Mar 8, 2023
50d2a00
Implemented return of process information from kernel to calling process
Make-IT-Wright Mar 10, 2023
1f4b6c7
Document procInfo struct used to return information to calling program
Make-IT-Wright Mar 20, 2023
13901dd
Added simple long running program to help test ps
Make-IT-Wright Mar 20, 2023
68ad61b
Added simple long running program to help test ps
Make-IT-Wright Mar 20, 2023
9443b1e
Added procInfo struct used to return information to calling program
Make-IT-Wright Mar 20, 2023
61094c5
Added proc_ps to return information about processes to user program
Make-IT-Wright Mar 20, 2023
e485e5e
Added ps program make system call and output information about curren…
Make-IT-Wright Mar 20, 2023
b308925
Added kernel side of system call, ps
Make-IT-Wright Mar 20, 2023
47ac0cf
Defined struct procInfo used inside the kernel and by user program, ps
Make-IT-Wright Mar 20, 2023
498cab6
Defined ps system call
Make-IT-Wright Mar 20, 2023
69cb5b1
Merge pull request #1 from Make-IT-Wright/implement_ps
Make-IT-Wright Mar 20, 2023
1231319
reduced qemu memory to 256
Aug 30, 2023
19364ef
Added custom device, hello
Make-IT-Wright Sep 28, 2023
cb50200
Added custom device, hello
Make-IT-Wright Sep 28, 2023
1789551
Converted dhello to work like a pipe
cs2350 Oct 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 55 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ OBJS = \
ioapic.o\
kalloc.o\
kbd.o\
kshutdown.o\
lapic.o\
log.o\
main.o\
Expand All @@ -27,6 +28,7 @@ OBJS = \
uart.o\
vectors.o\
vm.o\
dhello.o\

# Cross-compiling (e.g., on Mac OS X)
# TOOLPREFIX = i386-jos-elf
Expand Down Expand Up @@ -76,11 +78,14 @@ AS = $(TOOLPREFIX)gas
LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump
CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer
CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -Og -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer -fno-delete-null-pointer-checks -std=gnu99
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
ASFLAGS = -m32 -gdwarf-2 -Wa,-divide
# FreeBSD ld wants ``elf_i386_fbsd''
LDFLAGS += -m $(shell $(LD) -V | grep elf_i386 2>/dev/null | head -n 1)
LIBGCC_A = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
FS=fs.img
ASM_INCLUDES=asm.h memlayout.h mmu.h

# Disable PIE when possible (for Ubuntu 16.10 toolchain)
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
Expand All @@ -90,6 +95,8 @@ ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),)
CFLAGS += -fno-pie -nopie
endif

all: xv6.img fs.img

xv6.img: bootblock kernel
dd if=/dev/zero of=xv6.img count=10000
dd if=bootblock of=xv6.img conv=notrunc
Expand All @@ -100,24 +107,24 @@ xv6memfs.img: bootblock kernelmemfs
dd if=bootblock of=xv6memfs.img conv=notrunc
dd if=kernelmemfs of=xv6memfs.img seek=1 conv=notrunc

bootblock: bootasm.S bootmain.c
bootblock: bootasm.S bootmain.c $(ASM_INCLUDES)
$(CC) $(CFLAGS) -fno-pic -O -nostdinc -I. -c bootmain.c
$(CC) $(CFLAGS) -fno-pic -nostdinc -I. -c bootasm.S
$(LD) $(LDFLAGS) -N -e start -Ttext 0x7C00 -o bootblock.o bootasm.o bootmain.o
$(OBJDUMP) -S bootblock.o > bootblock.asm
$(OBJCOPY) -S -O binary -j .text bootblock.o bootblock
./sign.pl bootblock

entryother: entryother.S
entryother: entryother.S $(ASM_INCLUDES)
$(CC) $(CFLAGS) -fno-pic -nostdinc -I. -c entryother.S
$(LD) $(LDFLAGS) -N -e start -Ttext 0x7000 -o bootblockother.o entryother.o
$(OBJCOPY) -S -O binary -j .text bootblockother.o entryother
$(OBJDUMP) -S bootblockother.o > entryother.asm

initcode: initcode.S
initcode: initcode.S $(ASM_INCLUDES)
$(CC) $(CFLAGS) -nostdinc -I. -c initcode.S
$(LD) $(LDFLAGS) -N -e start -Ttext 0 -o initcode.out initcode.o
$(OBJCOPY) -S -O binary initcode.out initcode
$(OBJCOPY) -S -j .text -O binary initcode.out initcode
$(OBJDUMP) -S initcode.o > initcode.asm

kernel: $(OBJS) entry.o entryother initcode kernel.ld
Expand Down Expand Up @@ -145,18 +152,20 @@ vectors.S: vectors.pl

ULIB = ulib.o usys.o printf.o umalloc.o

usys.o: usys.S syscall.h traps.h

_%: %.o $(ULIB)
$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $@ $^
$(LD) $(LDFLAGS) -T program.ld --gc-sections -o $@ $^ $(LIBGCC_A)
$(OBJDUMP) -S $@ > $*.asm
$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym

_forktest: 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 _forktest forktest.o ulib.o usys.o
$(LD) $(LDFLAGS) -T program.ld -o _forktest forktest.o ulib.o usys.o
$(OBJDUMP) -S _forktest > forktest.asm

mkfs: mkfs.c fs.h
mkfs: mkfs.c fs.h param.h
gcc -Werror -Wall -o mkfs mkfs.c

# Prevent deletion of intermediate files, e.g. cat.o, after first build, so
Expand All @@ -181,10 +190,26 @@ UPROGS=\
_usertests\
_wc\
_zombie\
_shutdown\
_mycat2\
_snprintftest\
_ps\
_longRunner\
_testhello\

fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)

fs-%-as-init.img: _% mkfs README $(UPROGS)
rm -rf temp-$<
mkdir -p temp-$<
for filename in README $(UPROGS) _$<; do \
ln -s ../$$filename ./temp-$<; \
done
rm ./temp-$</_init
ln -s ../$< ./temp-$</_init
cd temp-$< && ../mkfs ../$@ $(UPROGS)

-include *.d

clean:
Expand Down Expand Up @@ -217,27 +242,27 @@ QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
then echo "-gdb tcp::$(GDBPORT)"; \
else echo "-s -p $(GDBPORT)"; fi)
ifndef CPUS
CPUS := 2
CPUS := 1
endif
QEMUOPTS = -drive file=fs.img,index=1,media=disk,format=raw -drive file=xv6.img,index=0,media=disk,format=raw -smp $(CPUS) -m 512 $(QEMUEXTRA)
QEMUOPTS = -device isa-debug-exit -drive file=$(FS),index=1,media=disk,format=raw -drive file=xv6.img,index=0,media=disk,format=raw -smp $(CPUS) -m 256 $(QEMUEXTRA)

qemu: fs.img xv6.img
$(QEMU) -serial mon:stdio $(QEMUOPTS)
qemu: $(FS) xv6.img
$(QEMU) -serial mon:stdio $(QEMUOPTS) || true

qemu-memfs: xv6memfs.img
$(QEMU) -drive file=xv6memfs.img,index=0,media=disk,format=raw -smp $(CPUS) -m 256
$(QEMU) -drive file=xv6memfs.img,index=0,media=disk,format=raw -smp $(CPUS) -m 256 || true

qemu-nox: fs.img xv6.img
$(QEMU) -nographic $(QEMUOPTS)
qemu-nox: $(FS) xv6.img
$(QEMU) -nographic $(QEMUOPTS) || true

.gdbinit: .gdbinit.tmpl
sed "s/localhost:1234/localhost:$(GDBPORT)/" < $^ > $@

qemu-gdb: fs.img xv6.img .gdbinit
qemu-gdb: fs.img xv6.img .gdbinit $(FS)
@echo "*** Now run 'gdb'." 1>&2
$(QEMU) -serial mon:stdio $(QEMUOPTS) -S $(QEMUGDB)

qemu-nox-gdb: fs.img xv6.img .gdbinit
qemu-nox-gdb: fs.img xv6.img .gdbinit $(FS)
@echo "*** Now run 'gdb'." 1>&2
$(QEMU) -nographic $(QEMUOPTS) -S $(QEMUGDB)

Expand All @@ -250,9 +275,10 @@ qemu-nox-gdb: fs.img xv6.img .gdbinit
EXTRA=\
mkfs.c ulib.c user.h cat.c echo.c forktest.c grep.c kill.c\
ln.c ls.c mkdir.c rm.c stressfs.c usertests.c wc.c zombie.c\
printf.c umalloc.c\
printf.c umalloc.c \
README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\
.gdbinit.tmpl gdbutil\
kernel.ld

dist:
rm -rf dist
Expand All @@ -273,14 +299,23 @@ dist-test:
cp dist/* dist-test
cd dist-test; $(MAKE) print
cd dist-test; $(MAKE) bochs || true
cd dist-test; $(MAKE) qemu
cd dist-test; $(MAKE) qemu-nox

# update this rule (change rev#) when it is time to
# make a new revision.
tar:
rm -rf /tmp/xv6
mkdir -p /tmp/xv6
cp dist/* dist/.gdbinit.tmpl /tmp/xv6
(cd /tmp; tar cf - xv6) | gzip >xv6-rev10.tar.gz # the next one will be 10 (9/17)
(cd /tmp; tar cf - xv6) | gzip >xv6-rev10-uva1.tar.gz

SUBMIT_FILENAME=xv6-submission-$(shell date +%Y%m%d%H%M%S).tar.gz

submit:
@tar -zcf $(SUBMIT_FILENAME) *.c *.h *.S *.ld Makefile $(wildcard *.txt) $(wildcard *.md) $(EXTRA) $(FILES)
@echo "Created $(SUBMIT_FILENAME); please upload and submit this file."


.PHONY: dist-test dist

.DELETE_ON_ERROR: 1
2 changes: 1 addition & 1 deletion console.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ panic(char *s)
//PAGEBREAK: 50
#define BACKSPACE 0x100
#define CRTPORT 0x3d4
static ushort *crt = (ushort*)P2V(0xb8000); // CGA memory
static ushort *crt = (ushort*)P2V_C(0xb8000); // CGA memory

static void
cgaputc(int c)
Expand Down
19 changes: 19 additions & 0 deletions defs.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "param.h"
struct buf;
struct context;
struct file;
Expand All @@ -22,6 +23,9 @@ void cprintf(char*, ...);
void consoleintr(int(*)(void));
void panic(char*) __attribute__((noreturn));

// dhello.c
void helloinit(void);

// exec.c
int exec(char*, char**);

Expand Down Expand Up @@ -156,6 +160,9 @@ int fetchint(uint, int*);
int fetchstr(uint, char**);
void syscall(void);

// shutdown.c
void shutdown(void);

// timer.c
void timerinit(void);

Expand Down Expand Up @@ -186,5 +193,17 @@ void switchkvm(void);
int copyout(pde_t*, uint, void*, uint);
void clearpteu(pde_t *pgdir, char *uva);

struct procInfo;

/// @brief Call this function to obtain information about existing
/// processes.
/// @param count : the maximum number of elements storable in procInfoArray
/// @param procInfoArray : an array of struct procInfo able to store at least
/// count elements.
/// @return The number of struct procInfo structures stored in procInfoArray
/// by the kernel. This number may be less than count, and if it is, elements
/// at indexes >= count may contain uninitialized memory.
int proc_ps(int count, struct procInfo* procInfoArray);

// number of elements in fixed-size array
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
97 changes: 97 additions & 0 deletions dhello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "types.h"
#include "defs.h"
#include "param.h"
#include "traps.h"
#include "spinlock.h"
#include "sleeplock.h"
#include "fs.h"
#include "file.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "x86.h"

#define BUFFERSIZE (8192)

struct hello_pipe {
struct spinlock lock;
char data[BUFFERSIZE];
uint nread; // number of bytes read
uint nwrite; // number of bytes written
};

// Storage for one pipe like buffer
static struct hello_pipe s_hello_pipe;

int
s_hello_pipewrite(struct hello_pipe *p, char *addr, int n)
{
int i;

acquire(&p->lock);
for(i = 0; i < n; i++){
while(p->nwrite == p->nread + BUFFERSIZE){ //DOC: pipewrite-full
if(myproc()->killed){
release(&p->lock);
return -1;
}
wakeup(&p->nread);
sleep(&p->nwrite, &p->lock); //DOC: pipewrite-sleep
}
p->data[p->nwrite++ % BUFFERSIZE] = addr[i];
}
wakeup(&p->nread); //DOC: pipewrite-wakeup1
release(&p->lock);
return n;
}

int
s_hello_piperead(struct hello_pipe *p, char *addr, int n)
{
int i;

acquire(&p->lock);
while(p->nread == p->nwrite){ //DOC: pipe-empty
if(myproc()->killed){
release(&p->lock);
return -1;
}
sleep(&p->nread, &p->lock); //DOC: piperead-sleep
}
for(i = 0; i < n; i++){ //DOC: piperead-copy
if(p->nread == p->nwrite)
break;
addr[i] = p->data[p->nread++ % BUFFERSIZE];
}
wakeup(&p->nwrite); //DOC: piperead-wakeup
release(&p->lock);
return i;
}

int
helloread(struct inode *ip, char *dst, int n) {
iunlock(ip);
n = s_hello_piperead(&s_hello_pipe, dst, n);
ilock(ip);
return n;
}

int
hellowrite(struct inode *ip, char *src, int n) {
iunlock(ip);
n = s_hello_pipewrite(&s_hello_pipe, src, n);
ilock(ip);
return n;
}


void
helloinit(void)
{
//initlock(&cons.lock, "hello");

devsw[HELLO].write = hellowrite;
devsw[HELLO].read = helloread;
//cons.locking = 1;
}

6 changes: 6 additions & 0 deletions file.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ struct devsw {
extern struct devsw devsw[];

#define CONSOLE 1

// This is te Major number for the new custom hello devices
// it probably should not be defiend in file.h, but that's
// where CONSOLE is defined, so we will follow the same
// practice.
#define HELLO 7
7 changes: 7 additions & 0 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ main(void)
dup(0); // stdout
dup(0); // stderr

mkdir("dev");
// Only 10 device major numbers are allowed by param.h
if(open("dev/hello", O_RDWR) < 0){
mknod("dev/hello", 7, 1); // 7 is major number, 1 is minor number
open("dev/hello", O_RDWR);
}

for(;;){
printf(1, "init: starting sh\n");
pid = fork();
Expand Down
1 change: 1 addition & 0 deletions kalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void
freerange(void *vstart, void *vend)
{
char *p;
if (vend < vstart) panic("freerange");
p = (char*)PGROUNDUP((uint)vstart);
for(; p + PGSIZE <= (char*)vend; p += PGSIZE)
kfree(p);
Expand Down
14 changes: 14 additions & 0 deletions kshutdown.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* Kernel part of shutdown/poweroff support */

#include "types.h"
#include "x86.h"

void
shutdown(void)
{
/*
This only works in QEMU and assumes QEMU was run
with -device isa-debug-exit
*/
outb(0x501, 0x0);
}
Loading