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

Feature/usys transition #279

Open
wants to merge 2 commits into
base: riscv
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ entryother
initcode
initcode.out
kernelmemfs
mkfs
tools/mkfs
tools/usys
kernel/kernel
user/usys.S
.gdbinit
34 changes: 34 additions & 0 deletions ADDING_SYSCALLS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Adding Syscalls to XV6

The tools/usys.c programm uses the syscall.h to create the user/usys.S (See Makefile).
The following steps are necessary to add a syscall to xv6.

## Checklist

- update kernel/syscall.h to contain the new syscall name. Example:
- #define SYS_freepg 22
- update kernel/syscall.c. Example:
- extern uint64 sys_freepg(void);
- [SYS_freepg] sys_freepg,
- update kernel/defs.h. Example:
- Below //kalloc.c -> See assumes the the function freepg() is defined in kalloc.c
- int freepg(void);
- update kernel/sysproc.c. Example:
- uint64
- sys_freepg(void) { return freepg()};
- update user/user.h. Example:
- Below // system calls
- int freepg(void);

Remark: Before the change to usys.c you had to addionaly update user/usys.pl to reflect the changes.

## Ideas

- It would be good if I could reduce the places which are required to add a new system call. Ideally only 1-2 Files (kernel and user) should be updated. Best would be only one file.







23 changes: 15 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
K=kernel
U=user
T=tools


OBJS = \
$K/entry.o \
Expand Down Expand Up @@ -101,8 +103,13 @@ _%: %.o $(ULIB)
$(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
# Compile usys generator, this replaces the usys.pl script
$T/usys: $T/usys.c
gcc -Werror -Wall -I. -o $T/usys $T/usys.c

# Generate usys.S
$U/usys.S: $T/usys
$T/usys $U/usys.S

$U/usys.o : $U/usys.S
$(CC) $(CFLAGS) -c -o $U/usys.o $U/usys.S
Expand All @@ -113,8 +120,8 @@ $U/_forktest: $U/forktest.o $(ULIB)
$(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

mkfs/mkfs: mkfs/mkfs.c $K/fs.h $K/param.h
gcc -Werror -Wall -I. -o mkfs/mkfs mkfs/mkfs.c
$T/mkfs: $T/mkfs.c $K/fs.h $K/param.h
gcc -Werror -Wall -I. -o $T/mkfs $T/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
Expand All @@ -140,17 +147,17 @@ UPROGS=\
$U/_wc\
$U/_zombie\

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

-include kernel/*.d user/*.d

clean:
rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \
*/*.o */*.d */*.asm */*.sym \
$U/initcode $U/initcode.out $K/kernel fs.img \
mkfs/mkfs .gdbinit \
$U/usys.S \
$T/mkfs .gdbinit \
$T/usys $U/usys.S \
$(UPROGS)

# try to generate a unique GDB port
Expand Down
File renamed without changes.
55 changes: 55 additions & 0 deletions tools/usys.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_LINE_LENGTH 256

int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <output_file>\n", argv[0]);
return 1;
}

// Open syscall.h for reading
FILE *syscall_h = fopen("kernel/syscall.h", "r");
if (!syscall_h) {
fprintf(stderr, "Error opening syscall.h file\n");
return 1;
}

// Open the user-provided output file
FILE *output = fopen(argv[1], "w");
if (!output) {
fprintf(stderr, "Error creating %s file\n", argv[1]);
fclose(syscall_h);
return 1;
}

// Header in the output file
fprintf(output, "# generated by usys.c - do not edit\n#include \"kernel/syscall.h\"\n");

char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), syscall_h)) {
// Look for lines starting with "#define SYS_"
if (strncmp(line, "#define SYS_", 12) == 0) {
char syscall_name[50];

// Extract the system call name and number
if (sscanf(line, "#define SYS_%s", syscall_name) == 1) {
// Write the assembly stub for this system call, using the syscall name
fprintf(output, ".global %s\n", syscall_name);
fprintf(output, "%s:\n", syscall_name);
fprintf(output, " li a7, SYS_%s\n", syscall_name);
fprintf(output, " ecall\n"); // Make the syscall
fprintf(output, " ret\n"); // Return
}
}
}

fclose(syscall_h);
fclose(output);

printf("Assembly stubs successfully generated in %s.\n", argv[1]);

return 0;
}
38 changes: 0 additions & 38 deletions user/usys.pl

This file was deleted.