xv6修改之输出系统调用名称&添加halt系统调用

这篇博客介绍了如何修改xv6操作系统,使其在执行系统调用时输出调用名称,并添加了halt系统调用。在syscall.c中,为每个系统调用添加了相应的名称输出。同时,详细描述了添加halt系统调用的步骤,包括更新syscall.h、syscall.c、Makefile、usys.S以及sysproc.c,并创建halt.c文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(原文章手抖 误删。。。重新发的)
大部分来自于洋大神哥的:http://yalongyang.com/2012/10/xv6-add-system-call/

题目一:为系统调用添加输出

在syscall.c中的,syscall函数改为

[cpp]  view plain copy
  1. void  
  2. syscall(void){  
  3.    
  4.   int num;  
  5.   num= proc->tf->eax;  
  6.  if(num > 0 && num < NELEM(syscalls) &&syscalls[num]) {  
  7.    proc->tf->eax = syscalls[num]();  
  8.    char* name;  
  9.    switch(num){  
  10.        case 1:  
  11.               name= "fork";  
  12.               break;  
  13.        case 2:  
  14.               name= "exit";  
  15.               break;  
  16.        case 3:  
  17.               name= "wait";  
  18.               break;  
  19.        case 4:  
  20.               name= "pipe";  
  21.               break;  
  22.        case 5:  
  23.               name= "read";  
  24.               break;  
  25.        case 6:  
  26.               name= "kill";  
  27.               break;  
  28.        case 7:  
  29.               name= "exec";  
  30.               break;  
  31.        case 8:  
  32.               name= "fstat";        
  33.               break;  
  34.        case 9:  
  35.               name= "chdir";      
  36.               break;  
  37.        case 10:  
  38.               name= "dup";  
  39.               break;  
  40.        case 11:  
  41.               name= "getpid";     
  42.               break;  
  43.        case 12:  
  44.               name= "sbrk";        
  45.               break;  
  46.        case 13:  
  47.               name= "sleep";       
  48.               break;  
  49.        case 14:  
  50.               name= "uptime";  
  51.               break;  
  52.        case 15:  
  53.               name= "open";      
  54.               break;  
  55.        case 16:  
  56.               name= "write";      
  57.               break;  
  58.        case 17:  
  59.               name= "mknod";    
  60.               break;    
  61.        case 18:  
  62.               name= "unlink";     
  63.               break;    
  64.        case 19:  
  65.               name= "link";        
  66.               break;    
  67.        case 20:  
  68.               name= "medir";     
  69.               break;  
  70.        case 21:  
  71.               name= "close";       
  72.               break;  
  73.        case 22:  
  74.               name= "halt";  
  75.               break;  
  76.        default:  
  77.               panic("Wrong");  
  78.     }  
  79.    cprintf("%s -> %d\n", name, proc->tf->eax);  
  80.   }else {  
  81.    cprintf("%d %s: unknown sys call %d\n",  
  82.            proc->pid, proc->name, num);  
  83.    proc->tf->eax = -1;  
  84.   }  
  85. }  


 

输出参数,

在fetchint和fetchStr的return正确数前

分别加入

[cpp]  view plain copy
  1. cprintf(”argu: %d\n”, *ip);      

[cpp]  view plain copy
  1. cprintf(”argu: %s\n”, *pp);   

题目二:为系统添加halt系统调用

模仿uptime系统调用,在xv6 pdf 搜索。加入halt系统调用。

步骤:

1.      syscall.h添加

[cpp]  view plain copy
  1. #define SYS_halt 22  


2.      syscall.c 添加

[cpp]  view plain copy
  1. extern int sys_halt(void);  


并在static int (*syscalls[])(void)中添加

[cpp]  view plain copy
  1. [SYS_halt]    sys_halt,  


 

3.      Makefile中的UPROGS 后添加 _halt\

4.      usys.S中添加SYSCALL(halt)

5. sysproc.c中添加sys_halt定义

int
sys_halt(void)
{
	char *p = "Shutdown";
   	for( ; *p; p++)
		outb(0x8900, *p);
}


6.      添加halt.c文件,加一句 halt声明

[cpp]  view plain copy
  1. #include"types.h"  
  2. #include"stat.h"  
  3. #include"user.h"  
  4. int halt();  
  5.    
  6. int  
  7. main(int argc,char *argv[])  
  8. {  
  9.   halt();  
  10.   return 0; 
  11. }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值