xen 添加hypercall

来源:互联网 发布:傻瓜记账软件 编辑:程序博客网 时间:2024/05/20 04:28

在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
.byte 1

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

extern int
do_kexec(
    unsignedlong 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);
    elseprintk("no message!\n");
    return1;
}

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)

viewplaincopyto clipboardprint?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <errno.h>  
  4. #include <sys/ioctl.h>  
  5. #include <sys/types.h>  
  6. #include <fcntl.h>  
  7. #include <string.h>  
  8. #include <xenctrl.h>  
  9. #include <xen/sys/privcmd.h>  
  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!