天天看點

Xen添加hypercall擷取page_fault次數

1. 首先注冊一個hypercall調用号。

xen/include/public/xen.h

 #define __HYPERVISOR_kexec_op             37

+#define __HYPERVISOR_print                   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(

    unsigned long op, unsigned arg1, XEN_GUEST_HANDLE(void) uarg);

+extern int

+do_print(void);

4.在/xen/arch/x86/traps.c 中

添加一個全局變量

long mynr_page_fault=0;

在do_page_fault()函數中統計次數,每執行一次,mynr_page_fault加1.

自定義函數

int do_print(void)

{

    printk("#####################test begin/n");

    printk("nr_page_fault: %ld/n",mynr_page_fault);

    return 1;

}

然後make dist、make install,制作初始化啟動盤。

5、編輯一個print.c檔案,在使用者空間測試用。

#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 fd, ret;       

    privcmd_hypercall_t hcall = {  

        __HYPERVISOR_print_string,

        {0,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);  

}

6、測試

[[email protected] ~] gcc -o a print.c

[[email protected] ~] ./a

[[email protected] ~] xm dm|tail -n 1