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