xen 添加hypercall

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.