Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14759
  • 博文数量: 11
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 105
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-08 14:28
文章分类

全部博文(11)

文章存档

2013年(11)

我的朋友

分类: 虚拟化

2013-08-08 14:35:27

在linux系统中添加新的系统调用,一般需要三个步骤:
1.  注册新的系统调用号
2. 更新系统调用表
3.  添加新函数
在xen中添加一个 hypercall,类似于在linux中添加一个系统调. 基本上也是上面几个步骤。

现在举个具体的例子:
比如我们要在xen中添加一个打印消息的hypercall,参数有一个,类型为char*, 代表我们要打印的消息. 函数原型为:
do_print_string(char* message),xen中原有37个hypercall,因此我们添加的超级调用号为38.

 

1. 首先注册一个hypercall调用号。
xen/include/public/xen.h
 #define __HYPERVISOR_kexec_op             37
+#define __HYPERVISOR_print_string         38

2.更新系统调用表
/xen/arch/x86/x86_32/entry.S
ENTRY(hypercall_table)
        
   .long do_kexec_op
+  .long do_print_string

ENTRY(hypercall_args_table)

   .byte 2 /* do_kexec_op          */
+  .byte 1 /* do_print_string      */

3. 定义函数头文件
/xen/include/asm-x86/hypercall.h

extern int
do_kexec(
    unsigned long op, unsigned arg1, XEN_GUEST_HANDLE(void) uarg);

+extern int
+do_print_string(char * message);

4.定义函数(函数定义在合适的文件中,这个例子采用mm.c)
/xen/arch/x86/mm.c
int do_print_string(char * message)
{
    if(message)
      printk("The message is :/n%s/n", message);
    else printk("no message!/n");
    return 1;
}

OK.

重新编译安装

make dist

make install

重新制作img

mkinitrd -v -f initrd-2.6.18.8-xen.img 2.6.18.8-xen

重启

然后编辑一个.c file, 在用户空间测试新的hypercall.如下:

(xen提供了privcmd这个驱动文件,从而在3环(用户空间)可以利用ioctl来调用hypercall)

 


  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. #include   
  10. int main(int argc, char *argv[])  
  11. {  
  12.     int fd, ret;  
  13.    char * message;  
  14.     if (argc != 2) {  
  15.         printf("please put one parameter!/n");  
  16.         return -1;  
  17.     }  
  18.    message = (char *) malloc(sizeof(char) * (strlen(argv[1])+1));  
  19.    strcpy(message, argv[1]);  
  20.     privcmd_hypercall_t hcall = {  
  21.         __HYPERVISOR_print_string,  
  22.         {message, 0, 0, 0, 0}  
  23.     };  
  24.     fd = open("/proc/xen/privcmd", O_RDWR);  
  25.     if (fd < 0) {  
  26.         perror("open");  
  27.         exit(1);  
  28.     } else  
  29.         printf("fd = %d/n", fd);  
  30.     ret = ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, &hcall);  
  31.     printf("ret = %d/n", ret);  
  32. }  


 

也可以采用加载模块的形式,在内核空间之间调用hypercall来测试。

 

编译该文件,并测试如下:

 

 

查看日志文件,检测是否新的hypercall安装成功:(linux下的log 一般在/var/log/mesages,而xen下的日志采用xm dm命令查看)

 

hypercall_test

 

OK!

阅读(350) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~