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

Shared memory system call #199

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 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
bf92898
Added system call to attach shared memory to calling process
Make-IT-Wright Oct 26, 2023
e483d54
Added nice, flock, funlock, priority based scheduler
Make-IT-Wright Nov 7, 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
76 changes: 56 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 Down Expand Up @@ -76,11 +77,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 +94,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 +106,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 +151,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 +189,28 @@ UPROGS=\
_usertests\
_wc\
_zombie\
_shutdown\
_mycat2\
_snprintftest\
_ps\
_longRunner\
_testshm\
_nice\
_testsched\

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 +243,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 +276,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 +300,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
42 changes: 42 additions & 0 deletions defs.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef defs_H
#define defs_H
#include "param.h"
struct buf;
struct context;
struct file;
Expand All @@ -22,6 +25,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 +162,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 +195,38 @@ void switchkvm(void);
int copyout(pde_t*, uint, void*, uint);
void clearpteu(pde_t *pgdir, char *uva);

struct procInfo;

int filelock(struct file*);
int fileunlock(struct file*);
void fsemaphore_lock(struct inode*);
void fsemaphore_unlock(struct inode*);

/// @brief This fuction changes the priority of the procee with
/// targetPID to targetPriority.
/// @param targetPID The index of the process data structure to change
/// @param targetPriority the new priority
/// @return the priority of chnage dprocess before the change
int proc_nice(int targetPID, int targetPriority) ;

/// @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);

/// @brief Call this function to obtain a pointer to shared memory.
/// @param storeBuffer_p Addess of memory where a pointer to the shared memory
/// should be stored. FYI, pass by reference and pass by pointer are the same
/// thing.
/// @return zero upon success and -1 otherwise
int proc_attachSharedMemory(char **storeBuffer_p);

// number of elements in fixed-size array
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))

#endif // defs_H
21 changes: 21 additions & 0 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ struct {
struct file file[NFILE];
} ftable;

int filelock(struct file* fp) {
if(fp->ip == 0 || fp->ip->ref < 1) {
return -1;
}

fsemaphore_lock(fp->ip);
return 0;
}

int fileunlock(struct file* fp)
{
int status = -1;

if(fp->ip != 0 && holdingsleep(&fp->ip->flock) && !(fp->ip->ref < 1)) {
fsemaphore_unlock(fp->ip);
status = 0;
}

return status;
}

void
fileinit(void)
{
Expand Down
1 change: 1 addition & 0 deletions file.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct inode {
uint dev; // Device number
uint inum; // Inode number
int ref; // Reference count
struct sleeplock flock; // use to implement file based semaphore
struct sleeplock lock; // protects everything below here
int valid; // inode has been read from disk?

Expand Down
20 changes: 20 additions & 0 deletions fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@
#include "buf.h"
#include "file.h"

// Lock the given inode (treat it as a semphore).
void
fsemaphore_lock(struct inode *ip)
{
if(ip == 0 || ip->ref < 1)
panic("flock");

acquiresleep(&ip->flock);
}

// Unlock the given inode (that isbeing treated as a semaphore).
void
fsemaphore_unlock(struct inode *ip)
{
if(ip == 0 || !holdingsleep(&ip->flock) || ip->ref < 1)
panic("funlock");

releasesleep(&ip->flock);
}

#define min(a, b) ((a) < (b) ? (a) : (b))
static void itrunc(struct inode*);
// there should be one superblock per disk device, but we run with
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);
}
24 changes: 24 additions & 0 deletions longRunner.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "types.h"
#include "stat.h"
#include "user.h"

static volatile int counter = 0;

int loop(int n)
{
for(int i = n * 4096; i > 0; --i) {
for(int j = n * 4096; j > 0; --j) {
counter += 1;
}
}
return counter;
}

int
main(int argc, char *argv[])
{
for(int j = 3; j > 0; --j) {
loop(j);
}
exit();
}
Loading