xv6系统实现System call,本文所实现的功能基于Vmware ubuntu14.04虚拟机
1、配置环境:
[1]安装虚拟机
[2]sudo apt-get install git
[3]git clone git://github.com/mit-pdos/xv6-public.git
[4]sudo apt-get install libsdl1.2-dev
[5]sudo apt-get install qemu
[6]cd xv6-public
[7]make qemu
运行结果如下图所示:
2、要求:在进行系统调用时,打印出系统调用的名字和返回值。
系统调用函数syscall()在syscall.c文件中,只需要修改syscall()函数即可,即在syscall()函数内添加对应的printf语句。
void
syscall(void)
{
int num;
num = proc->tf->eax;
if(num > 0 && num < NELEM(syscalls) && syscalls[num]) {
proc->tf->eax = syscalls[num]();
switch (num) {
case SYS_fork:
cprintf("fork -> ");
break;
case SYS_exit:
cprintf("exit -> ");
break;
case SYS_wait:
cprintf("wait -> ");
break;
case SYS_pipe:
cprintf("pipe -> ");
break;
case SYS_read:
cprintf("read -> ");
break;
case SYS_kill:
cprintf("kill -> ");
break;
case SYS_exec:
cprintf("exec -> ");
break;
case SYS_fstat:
cprintf("fstat -> ");
break;
case SYS_chdir:
cprintf("chdir -> ");
break;
case SYS_dup:
cprintf("dup -> ");
break;
case SYS_getpid:
cprintf("getpid -> ");
break;
case SYS_sbrk:
cprintf("sbrk -> ");
break;
case SYS_sleep:
cprintf("sleep -> ");
break;
case SYS_uptime:
cprintf("uptime -> ");
break;
case SYS_open:
cprintf("open -> ");
break;
case SYS_write:
cprintf("write -> ");
break;
case SYS_mknod:
cprintf("mknod -> ");
break;
case SYS_unlink:
cprintf("unlink -> ");
break;
case SYS_link:
cprintf("link -> ");
break;\
case SYS_mkdir:
cprintf("mkdir -> ");
break;
case SYS_close:
cprintf("close -> ");
break;
default:
panic("should never get here\n");
}
cprintf("%d\n", proc->tf->eax);
} else {
cprintf("%d %s: unknown sys call %d\n",
proc->pid, proc->name, num);
proc->tf->eax = -1;
}
}
再次运行qemu,出现如图所示结果:
3、要求:在xv6系统中添加并实现一个date系统调用,用以输出当前的UTC时间。
要实现date系统调用主要是添加系统调用号和添加对应的系统调用函数,具体过程可以仿照uptime系统调用的实现。
1) 使用grep命令筛选出出现uptime字样的文件和文件中所在行号,以便仿照uptime系统调用实现date系统调用:
2) 在syscall.c中添加系统调用函数的外部声明,共有两处地方需要添加:
3) 在syscall.h中添加系统调用号:
4) 在sysproc.c中添加系统调用函数sys_date()的实现:
5) 在user.h中添加用户态函数的定义:
6) 在usys.S中添加用户态函数的实现:
7) 由于我们还需要在用户空间来对内核提供的系统命令进行调用,新建用户程序date.c文件,写入以下代码:
8) 修改Makefile:
运行qemu ,调用date: