发布于 

MIT 6.828 HW3 system calls

Notes of MIT 6.828 HW3

Part One: System call tracing

Modify the syscall() function in syscall.c to print out a line for each system call invocation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void
syscall(void)
{
int num;
struct proc *curproc = myproc();
static char table[22][10] = {"0", "fork", "exit", "wait", "pipe", "read", "kill", "exec", "fstat", "chdir", "dup", "getpid", "sbrk", "sleep", "uptime", "open", "write", "mknod", "unlink", "link", "mkdir", "close"};

num = curproc->tf->eax;
if(num > 0 && num < NELEM(syscalls) && syscalls[num]) {
curproc->tf->eax = syscalls[num]();
cprintf("%s -> %d\n", table[num], curproc->tf->eax);
} else {
cprintf("%d %s: unknown sys call %d\n",
curproc->pid, curproc->name, num);
curproc->tf->eax = -1;
}
}

Part Two: Date system call

Add a date system call to xv6.

In user.h

1
2
3
4
5
// system calls
int fork(void);
/* ... */
int uptime(void);
int date(struct rtcdate*); // add this

In user.s

1
2
3
4
SYSCALL(fork)
; ...
SYSCALL(uptime)
SYSCALL(date) ; add this

In syscall.c

1
2
3
4
5
6
7
8
9
10
extern int sys_chdir(void);
/* ... */
extern int sys_uptime(void);
extern int sys_date(void); // add this

static int (*syscalls[])(void) = {
[SYS_fork] sys_fork,
/* ... */
[SYS_date] sys_date, // add this
};

In syscall.h

1
2
3
4
// System call numbers
#define SYS_fork 1
/* ... */
#define SYS_date 22 // add this

In sysproc.c

1
2
3
4
5
6
7
8
9
int
sys_date(void)
{
struct rtcdate *dt;
if (argptr(0, (void *)&dt, sizeof(*dt)) < 0)
return -1;
cmostime(dt);
return 0;
}

Finally, create a new file date.c, and add _date to the UPROGS definition in Makefile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "types.h"
#include "user.h"
#include "date.h"

int
main(int argc, char *argv[])
{
struct rtcdate r;

if (date(&r)) {
printf(2, "date failed\n");
exit();
}

printf(0, "%d-%d-%d %d:%d:%d\n", r.year, r.month, r.day, r.hour, r.minute, r.second);

exit();
}

And we can see the result in xv6 shell.

1
2
3
4
5
6
7
8
9
❯ make qemu-nox
qemu-system-i386 -nographic -drive file=fs.img,index=1,media=disk,format=raw -drive file=xv6.img,index=0,media=disk,format=raw -smp 2 -m 512
xv6...
cpu1: starting 1
cpu0: starting 0
sb: size 1000 nblocks 941 ninodes 200 nlog 30 logstart 2 inodestart 32 bmap start 58
init: starting sh
$ date
2022-3-30 3:2:12