MIT6.828 Fall2018 笔记 - Homework 3: xv6 system calls

Homework: xv6 system calls

我运行的时候出了一些问题,一直在Booting from Hard Disk...,后来发现是因为kernel.stab段以及之后的LMA都不对。修改kernel.ld的部分内容就能解决:

	/* Include debugging information in kernel memory */
	.stab : AT(LOADADDR(.rodata) + SIZEOF(.rodata)){

然后就可以正常运行了。

Part One: System call tracing

syscall.c文件里添加syscalls_name数组,并且修改syscall函数:

static char *syscalls_name[] = {
[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",
};

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]();
    cprintf("%s -> %d\n", syscalls_name[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

Makefile文件的UPROGS中添加 _date

新建文件date.c

#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");
    }
    printf(1, "%d-%d-%d %d:%d:%d\n", r.year, r.month, r.day, r.hour, r.minute, r.second);
    exit();
}

文件user.h中添加:

int date(struct rtcdate*);

文件usys.S中添加:

SYSCALL(date)

文件syscall.h中添加:

#define SYS_date   22

文件syscall.c中添加:

extern int sys_date(void);
// 往syscalls数组中添加:
[SYS_date]    sys_date,

文件sysproc.c中添加:

int sys_date(void) {
    struct rtcdate* r = 0;
    if (argptr(0, (char**)&r, sizeof(*r) < 0)) {
        return -1;
    }
    cmostime(r);
    return 0;
}

结果:

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
2020-4-2 12:27:48
$
posted @ 2020-04-02 20:59  hyuuko  阅读(982)  评论(2编辑  收藏  举报