现在的位置: 首页 > 综合 > 正文

xen 添加hypercall

2013年10月03日 ⁄ 综合 ⁄ 共 2668字 ⁄ 字号 评论关闭

在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)

view plaincopy to clipboardprint?
#include <stdio.h>  
#include <stdlib.h>  
#include <errno.h>  
#include <sys/ioctl.h>  
#include <sys/types.h>  
#include <fcntl.h>  
#include <string.h>  
#include <xenctrl.h>  
#include <xen/sys/privcmd.h>  
int main(int argc, char *argv[])  
{  
    int fd, ret;  
   char * message;  
    if (argc != 2) {  
        printf("please put one parameter!/n");  
        return -1;  
    }  
   message = (char *) malloc(sizeof(char) * (strlen(argv[1])+1));  
   strcpy(message, argv[1]);  
    privcmd_hypercall_t hcall = {  
        __HYPERVISOR_print_string,  
        {message, 0, 0, 0, 0}  
    };  
    fd = open("/proc/xen/privcmd", O_RDWR);  
    if (fd < 0) {  
        perror("open");  
        exit(1);  
    } else 
        printf("fd = %d/n", fd);  
    ret = ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, &hcall);  
    printf("ret = %d/n", ret);  

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <xenctrl.h>
#include <xen/sys/privcmd.h>
int main(int argc, char *argv[])
{
    int fd, ret;
   char * message;
    if (argc != 2) {
        printf("please put one parameter!/n");
        return -1;
    }
   message = (char *) malloc(sizeof(char) * (strlen(argv[1])+1));
   strcpy(message, argv[1]);
    privcmd_hypercall_t hcall = {
        __HYPERVISOR_print_string,
        {message, 0, 0, 0, 0}
    };
    fd = open("/proc/xen/privcmd", O_RDWR);
    if (fd < 0) {
        perror("open");
        exit(1);
    } else
        printf("fd = %d/n", fd);
    ret = ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, &hcall);
    printf("ret = %d/n", ret);
}

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

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

 

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

 

OK!

抱歉!评论已关闭.