XEN添加和调用Hypercall (zz)

XEN添加和调用Hypercall (zz)

XEN添加和调用Hypercall (zz)

在linux系统中添加新的系统调用,一般需要三个步骤:

1. 注册新的系统调用号

2. 更新系统调用表

3. 添加新函数

在xen中添加一个 hypercall,类似于在linux中添加一个系统调. 基本上也是上面几个步骤。现在举个具体的例子:

比如我们要在xen中添加一个打印消息的hypercall,参数有一个,类型为char*, 代表我们要打印的消息. 函数原型为:

do_print_string(char* message)

1. 首先注册一个hypercall调用号。

xen/include/public/xen.h

#define __HYPERVISOR_kexec_op              37//原有超级调用

#define __HYPERVISOR_print_string          38//新增超级调用号为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%sn", message);

    else printk("no message!n");

    return 1;

}

重新编译安装

make dist

make install

重新制作img

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

重启

调用新Hypercall

然后编辑一个.c file, 在用户空间测试新的hypercall.如下:(xen提供了privcmd这个驱动文件,从而在3环(用户空间)可以利用ioctl来调用hypercall)

#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 = %dn", fd);

     ret = ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, &hcall);

     printf("ret = %dn", ret);

}

也可以采用加载模块的形式,在内核空间之间调用hypercall来测试。编译该文件,并测试如下:

gcc -o a hypercall_test.c

./a Hello!

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值