Syscall

文章目录

  • Syscall
    • 一、实验要求
      • Part One: System call tracing
      • Part Two: Date system call
    • 二、实验操作
      • Part one
      • Part Two

request

一、实验要求

Part One: System call tracing

Your first task is to modify the xv6 kernel to print out a line for each system call invocation. It is enough to print the name of the system call and the return value; you don’t need to print the system call arguments.
When you’re done, you should see output like this when booting xv6:

...
fork -> 2
exec -> 0
open -> 3
close -> 0
$write -> 1write -> 1

That’s init forking and execing sh, sh making sure only two file descriptors are open, and sh writing the $ prompt. (Note: the output of the shell and the system call trace are intermixed, because the shell uses the write syscall to print its output.)

Hint: modify the syscall() function in syscall.c.
Optional challenge: print the system call arguments.

Part Two: Date system call

Your second task is to add a new system call to xv6. The main point of the exercise is for you to see some of the different pieces of the system call machinery. Your new system call will get the current UTC time and return it to the user program. You may want to use the helper function, cmostime() (defined in lapic.c), to read the real time clock. date.h contains the definition of the struct rtcdate struct, which you will provide as an argument to cmostime() as a pointer.

You should create a user-level program that calls your new date system call; here’s some source you should put in 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");exit();}// your code to print the time in any format you like...exit();
}

In order to make your new date program available to run from the xv6 shell, add _date to the UPROGS definition in Makefile.

Your strategy for making a date system call should be to clone all of the pieces of code that are specific to some existing system call, for example the “uptime” system call. You should grep for uptime in all the source files, using *grep -n uptime .[chS].

When you’re done, typing date to an xv6 shell prompt should print the current UTC time.

Write down a few words of explanation for each of the files you had to modify in the process of creating your date system call.

Optional challenge: add a dup2() system call and modify the shell to use it.
*syscall.h:

二、实验操作

Part one

要实现系统调调用时打印系统调用的名称和参数,我们需要修改syscall.csyscalll.h文件

然后我们先看一下syscall.c的代码

// System call numbers
#define SYS_fork    1
#define SYS_exit    2
#define SYS_wait    3
#define SYS_pipe    4
#define SYS_read    5
#define SYS_kill    6
#define SYS_exec    7
#define SYS_fstat   8
#define SYS_chdir   9
#define SYS_dup    10
#define SYS_getpid 11
#define SYS_sbrk   12
#define SYS_sleep  13
#define SYS_uptime 14
#define SYS_open   15
#define SYS_write  16
#define SYS_mknod  17
#define SYS_unlink 18
#define SYS_link   19
#define SYS_mkdir  20
#define SYS_close  21

不难看出,这段代码是对xv6 21个常见系统调用的宏定义,我们要想实现打印调用名称和参数,可以在syscall.c设置和系统调用的相同的编号和名称相对应的数组

#include "x86.h"
#include "syscall.h"// 以下为添加的内容
static char SYS_call_names[][6] = {[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"};
// 以上为添加的内容

然后除此之外还需要修改syscall()函数,使其实现当系统调用时打印对应的名称和编号

$make qemu (或make qemu-nox)
//make qemu-nox是将qemu固定在linux下,在串口终端中启动Qemu

运行结果如下图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C5kxiRPK-1571208580659)(partone.png)]

void syscall(void)
····curproc ->tf->eax = syscall[numm]();
// 以下为添加的内容cprintf("\tSYS_call: %s\tid: %d\n", SYS_call_names[num], num);
// 以上为添加的内容
}
else
···

到这里第一个小部分就完成了,这部分其实很简单就是在syscall调用函数是加入了一句打印代码使其实现功能

  • 在这没学过c语言的可能会疑惑是printf的基本语法,我在下面简单介绍一下:
    首先在这里的**cprintf()我认为和printf()**的基本用法是一样的。
    printf()函数调用格式:printf(“格式化字符串”,输出表列)。格式化字符串包含三种对象,为:
    (1)字符串常量
    (2)格式化控制字符串
    (3)转义字符
    字符串常量原样输出,在显示中起提示作用。输出表列中给出了各个输出项,要求格式控制字符串和各输出项在数量和类型上应该一一对应。其中格式控制字符串是以%开头的字符串,在%后面跟有各种格式控制符,以说明输出数据的类型、宽度、精度等。
    格式化控制字符串:
    %[标志][最小宽度][.精度][类型长度]类型
    示例:

    printf("%5d\n",1000);               //默认右对齐,左边补空格output:
    ` `1000
    

    常用转义字符:
    \n :回车符
    \t :横向制表符
    \v:纵向制表符

Part Two

  • 在各种文件里添加第22个系统调用sys_date
    1.syscall.h:

     #define SYS_date 22

    2.syscall.c:

    [SYS_date]  " date"
    };···
    extern int sys_date(void);···
    [SYS_date] sys_date

    3.user.h文件添加用户态函数

    int sleep(int);
    int uptime(void);
    // 以下为添加内容
    int date(struct rtcdate*);
    // 以上为添加内容

    4.usys.S添加用户态函数的实现

    SYSCALL(date)
    

    5.sysproc.c添加系统调用函数

    int
    sys_date(struct rtcdate *r)
    {if (argptr(0, (void *)&r, sizeof(*r)) < 0)return -1;cmostime(r);  //从cmos中获取时间return 0;}

至此,就完成了添加系统调用函数 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");exit();}// your code to print the time in any format you like...printf(1, "%d-%d %d %d:%d:%d\n", r.month, r.day, r.year, r.hour, r.minute, r.second);//输出格式可以随意定义exit();
}

6.最后不要忘了在MakeFile添加UPROGS对应的命令:

_big\
# 以下为添加内容_date\
# 以上为添加内容
  • 到此就完成了所有程序的修改,然后我们运行
make qemu
//然后输入date
$date
  • 但是结果却并不和预期一样
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y0R4MoKv-1571208580662)(parttwo.png)]
    然后我们发现其实这是之前第一部分系统调用造成的,虽然没有达到我们的预期但是却能够清晰的显现每个输出的系统调用命令
    然后我们把第一部分的系统调用注释掉就好了
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dCvkrWaf-1571208580664)(partthree.png)]
    最后成功的实现了我们的预期!

xv6 syscall实验相关推荐

  1. 深大操作系统:xv6 综合实验一:copy on write,slab,信号量,调度与系统调用

    目录 前言 1. xv6 hello world 程序 2. 调度时间片的修改 3. 信号量 4. slab 内核内存管理 5. copy on write 总结 前言 这绝对是我做过的最难的实验没有 ...

  2. xv6 - lab0 - 实验环境

    xv6 实验环境 为了能够在RISC-V模拟器环境中实验XV6操作系统,我需要配置的工具软件有:QEMU 5.1+, GDB 8.3+, GCC, and Binutils. 1 配置环境 1)系统环 ...

  3. 操作系统实验Lab 1:Xv6 and Unix utilities(MIT 6.S081 FALL 2020)

    Lab 1 Xv6 and Unix utilities 实验要求链接 Boot xv6 (easy) 实验目的 切换到 xv6-labs-2020 代码的 util 分支,并利用 QEMU 模拟器启 ...

  4. 6.S081-Lab: Xv6 and Unix utilities 操作系统实验

    学习操作系统的时候,买了一本人民邮电出版社的-Operating Systems: Three Easy Pieces 中译版(操作系统导论). 从书中学到了很多关于操作系统的思想,出现一个问题,思考 ...

  5. 深大操作系统实验四:磁盘 Inode 读取,盘块读取,软硬链接,磁盘挂载

    目录 前言 1. 目录树构建 2. Inode 与物理盘块读取 3. 软硬链接 4. 内存磁盘 cache 5. 磁盘挂载(选做) 6. open 与 fopen(选做) 7. 文件指针游标(选做) ...

  6. MIT6.S081简单总结

    在复盘MIT6.828的时候,偶然看到了一个将MIT6.S081的视频翻译成文字的gitbook:https://mit-public-courses-cn-translatio.gitbook.io ...

  7. 操作系统-MIT6.S081学习

    操作系统 Lec01 1.2 操作系统结构 以距型表示: 硬件资源包括:CPU.内存.磁盘.网卡,在底层 架构上层,需要运行各种的应用程序,例如 文本编译器(vi).c编译器(cc),cli 存在的 ...

  8. XV6实验-Lab1 Syscalls

    文章目录 EXERCISE 0 EXERCISE 1 System Call Tracing 目的 提示 EXERCISE 2 Sysinfo 目的 提示 EXERCISE 0 阅读xv6 book第 ...

  9. 操作系统实验三·xv6系统增添系统调用和用户级应用程序

    xv6系统增添系统调用和用户级应用程序 1.实验目的 2.实验内容 3. 实验环境 4. 程序设计和实现 4.1系统修改 4.2 编译运行 1.实验目的 准备xv6环境,向xv6添加一个新的系统调用, ...

  10. 操作系统实验五·xv6系统内存布局修改·栈空间位置变更与栈不足时扩充

    xv6系统内存布局修改·栈空间位置变更与栈不足时扩充 1.实验目的 2.实验内容 3. 实验手册 4. 实验环境 5. 程序设计和实现 5.1系统函数修改 5.2 编译运行 6. 实验结果和分析 1. ...

最新文章

  1. 剔除Intellij中Mybatis的Mapper自动注入警告
  2. sys no attribute argv
  3. 不是程序员学python有什么用-程序员的基本功:为什么非要用Python,其他语言不好吗?...
  4. kafka--storm--mongodb
  5. docker单一部署jenkins
  6. VTK:图片之RTAnalyticSource
  7. 基于vue-cli的webpack配置优化
  8. mockwebserver java_在Java中使用WireMock和SOAP Web服务
  9. [python + debug] set()操作对象的元素为字符串,则结果随机排序,使用sorted()函数以固定顺序
  10. [20180801]insert导致死锁.txt
  11. 计算机专业在医院的工作,探析计算机在医院工作的应用
  12. python3的print()函数的用法图文讲解
  13. 根据excel模板导出多sheet且生成条形码或二维码插入excel指定位置中
  14. 苹果新园区最新内部效果图流出
  15. python中plot函数参数_Python的 plot函数和绘图参数设置
  16. 我的Go+语言初体验——Demo游戏体验篇(直男程序员的真实体验)
  17. 人工智能ai思维_人工智能系统如何学习创造性思维
  18. SM敏捷实践经验总结
  19. 合工大宣城校区计算机老师,合肥工业大学计算机与信息学院导师教师师资介绍简介-△李春华...
  20. 禁道具和连携击败姜世离的两种战术

热门文章

  1. 电脑win10黑苹果双系统
  2. 第十二届蓝桥杯模拟赛Python组(第一期)
  3. 计算机无法安装新字体,xp系统电脑安装新字体后无法使用的解决方法
  4. 全国30省市分地区能源结构(煤炭占比)2003-2019年
  5. JetBrains DataGrip 2018.2.3中文破解版 含jar文件注册码激活教程(转)
  6. 一套鼠标键盘操作两台电脑的方法
  7. CSDN下载积分的获取方式
  8. MODIS数据介绍及下载
  9. 公开课丨苏杰新著来袭,十年之后再谈人人都是产品经理!
  10. 数学建模-非线性优化模型