xv6-----system calls

本文整理于网上,非原创,,,

注:如果编译错误,请首先检查复制的代码是否包含中文字符,'"()等等。

大部分来自于洋大神哥的:http://yalongyang.com/2012/10/xv6-add-system-call/  

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

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

 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 -> %dn", name, proc->tf->eax);  
80       }else {  
81        cprintf("%d %s: unknown sys call %dn",  
82                proc->pid, proc->name, num);  
83        proc->tf->eax = -1;  
84       }  
85     }  

输出参数,

在fetchint和fetchStr的return正确数前

分别加入

1 cprintf("argu: %d
", *ip);   
1 cprintf("argu: %s
", *pp);

重新编译启动xv6时,会出现下图所示

就说明说明成功了。

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

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

步骤:

1. syscall.h添加

1 #define SYS_halt 22  

2. syscall.c 添加

1 extern int sys_halt(void); 

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

[SYS_halt]    sys_halt,  

 

3. Makefile中的UPROGS 后添加 _halt

  注意换行开头不能有空格,用Tab键进行缩进,否则会报错!

 

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

 

5. sysproc.c中添加sys_halt定义

 

1     int  
2     sys_halt(void)  
3     {  
4         char *p = "Shutdown";  
5         for( ; *p; p++)  
6             outb(0x8900, *p);  
7         return 08     }  

 

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

 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 }

大功告成!

注:重新编译运行xv6后,输入halt,有可能会出现下图所示错误

这是qemu更新版本的原因,不是实现错误,代码实现到这里就可以了,效果不必要达到。

 

原文地址:https://www.cnblogs.com/tjulym/p/4973976.html