环境

  1. 拉取仓库
1
2
git clone https://gitee.com/tjucs/xv6-public
cd xv6-public
  1. 安装qemu
1
2
sudo apt update
sudo apt install qemu-system-x86

PART 1

  1. 修改syscall.c,添加系统调用跟踪和 date 系统调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 在extern int sys_uptime(void)后添加
extern int sys_date(void);

// 在 static int (*syscalls[])(void)之前添加
static char *syscall_names[] = {
[SYS_fork] "fork",
[SYS_exit] "exit",
[SYS_wait] "wait",
[SYS_pipe] "pipe",
[SYS_read] "read",
[SYS_kill] "kill",
[SYS_exec] "exec",
[SYS_fstat] "fstat",
[SYS_chdir] "chdir",
[SYS_dup] "dup",
[SYS_getpid] "getpid",
[SYS_sbrk] "sbrk",
[SYS_sleep] "sleep",
[SYS_uptime] "uptime",
[SYS_open] "open",
[SYS_write] "write",
[SYS_mknod] "mknod",
[SYS_unlink] "unlink",
[SYS_link] "link",
[SYS_mkdir] "mkdir",
[SYS_close] "close",
[SYS_date] "date",
};

// 在[SYS_close] sys_close后添加
[SYS_date] sys_date,

// 修改 syscall() 函数
void
syscall(void)
{
int num;
struct proc *curproc = myproc();

num = curproc->tf->eax;
if(num > 0 && num < NELEM(syscalls) && syscalls[num]) {
curproc->tf->eax = syscalls[num]();

// 打印系统调用名称和返回值
if (num < NELEM(syscall_names) && syscall_names[num]) {
cprintf("%s -> %d\n", syscall_names[num], curproc->tf->eax);
}
} else {
cprintf("%d %s: unknown sys call %d\n",
curproc->pid, curproc->name, num);
curproc->tf->eax = -1;
}
}
  1. 修改syscall.h
1
2
// 在末尾添加
#define SYS_date 22
  1. 更改sysproc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 在sys_uptime 函数之后添加
int
sys_date(void)
{
struct rtcdate *r;

// 获取用户空间传入的指针参数
if(argptr(0, (char**)&r, sizeof(*r)) < 0)
return -1;

// 调用 cmostime 获取当前时间
cmostime(r);
return 0;
}

Part 2

  1. 创建date.c用户程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "types.h"
#include "stat.h"
#include "user.h"
#include "date.h"

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

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

// 不使用 %02d,直接用 %d
printf(1, "Date: %d-%d-%d %d:%d:%d\n",
r.year, r.month, r.day, r.hour, r.minute, r.second);

exit();
}
  1. 更改makefile
1
2
# 在_cat\ 后面添加
_date\
  1. 更改user.h
1
2
// 在 uptime(void); 后面添加
int date(struct rtcdate *);
  1. 修改 syscall.h:添加系统调用号
1
#define SYS_date   22
  1. 修改 syscall.c:添加系统调用入口
1
2
3
4
5
6
7
8
// 添加外部声明
extern int sys_date(void);

// 在 syscalls[] 数组中添加
[SYS_date] sys_date,

// 在 syscall_names[] 数组中添加(用于 Part 1 跟踪)
[SYS_date] "date",
  1. 在sysproc.c中实现系统调用函数
1
2
3
4
5
6
7
8
9
10
11
int
sys_date(void)
{
struct rtcdate *r;

if(argptr(0, (char**)&r, sizeof(*r)) < 0)
return -1;

cmostime(r);
return 0;
}
  1. 修改 usys.S :添加用户态陷门入口
1
SYSCALL(date)
  1. 编译运行
1
2
make clean
make qemu-nox

然后测试

1
date

最后Ctrl+A,在按x退出QEMU

选做(dup2)

  1. 更改syscall.h,添加系统调用号
1
#define SYS_dup2   23
  1. user.h中添加用户态声明
1
2
3
4
5
6
7
int dup2(int, int);
````

3. usys.S中添加入口

```S
SYSCALL(dup2)
  1. syscall.c中添加入口
1
2
3
4
5
6
7
extern int sys_dup2(void);

// syscalls[] 数组中添加:
[SYS_dup2] sys_dup2,

// syscall_names[] 数组中添加:
[SYS_dup2] "dup2",
  1. 在sysproc.c中实现sys_dup2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int
sys_dup2(void)
{
int oldfd, newfd;
struct file *of, *nf;
struct proc *curproc = myproc();

if(argint(0, &oldfd) < 0 || argint(1, &newfd) < 0)
return -1;

if(oldfd < 0 || oldfd >= NOFILE || curproc->ofile[oldfd] == 0)
return -1;

if(oldfd == newfd)
return newfd;

if(newfd < 0 || newfd >= NOFILE)
return -1;

of = curproc->ofile[oldfd];

if(curproc->ofile[newfd] != 0) {
fileclose(curproc->ofile[newfd]);
curproc->ofile[newfd] = 0;
}

nf = of;
filedup(nf);
curproc->ofile[newfd] = nf;

return newfd;
}
  1. 修改sh.c中runcmd函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
case REDIR:
rcmd = (struct redircmd*)cmd;
int fd;
close(rcmd->fd);
if((fd = open(rcmd->file, rcmd->mode)) < 0){
printf(2, "open %s failed\n", rcmd->file);
exit();
}
if(fd != rcmd->fd){
if(dup2(fd, rcmd->fd) < 0){
printf(2, "dup2 failed\n");
exit();
}
close(fd);
}
runcmd(rcmd->cmd);
break;