VirtualBox Guest Additions:Guest 设备、显示调整、鼠标集成、无缝桌面与 HGCM 通信

来源:OSDev Wiki· 阅读原文 ↗

本文旨在描述 VirtualBox Guest Additions 中一些较为有用/较易实现的功能。

VirtualBox Guest 设备

Guest Additions 软件包通过 VM 提供的 PCI/mmio 设备运行。内存映射数据包、MMIO 端口写入和 IRQ 的组合使客户机能够向 VM 传达其功能支持,并使 VM 能够传达显示变化和鼠标移动等事件。该 PCI 设备的厂商 ID 为 0x80EE,与 VirtualBox 实现的 Bochs 显示适配器相同,设备 ID 为 0xCAFE。BAR0 是 MMIO 端口,BAR1 是包含该设备某些共享状态的内存区域,例如客户机希望接收哪些事件的位掩码。后者的格式对于本页描述的功能并不重要——你只需知道该区域可以解释为 32 位无符号整数数组,偏移量 3 包含 IRQ 掩码。

客户机与主机之间的通信通过内存中的数据包进行。这些数据包可以位于物理内存中的任何位置,且相对较短。读取或填充数据包的 MMIO 操作是同步的,因此设置一个页面来回传递是可行的。向 VM 发送消息的基本过程是准备一个数据包,然后将其(物理)地址写入 MMIO 端口。接收的工作方式相同,因为你必须准备一个带有请求类型的数据包并提供其物理地址;在 MMIO 端口写入完成后,如果请求成功,数据包将被填充相应的值。来自 VM 的通信主要由 PCI 设备中断线上的 IRQ 发起,随后应跟上适当的数据包请求,其中一个应为特殊的“确认事件”类型。

本页包含一些结构体定义,这些定义也可以从 VirtualBox 提供的头文件中获得,但那些头文件相当庞大,并假定它们与一套完善的系统头文件一起使用,因此我们将自行定义这些结构体。

以下各节中的伪代码示例将假定这些函数可用;请根据你的环境进行调整:

// Allocate one page of memory, providing its physical address as an output and virtual address as a return value.
void * allocate_physical_page(uint32_t * physical);
// Map a physical page into the virtual memory space.
void * map_physical_page(uint32_t physical);
// Install an interrupt request handler function.
void install_interrupt_handler(irq, int (*irq_function)(void));
// Write a 32-bit value to an MMIO port.
void outportl(port, value);
// Write a 16-bit value to an MMIO port.
void outports(port, value);
// Return an object describing the requested PCI device, if found.
pci_device_t pci_find(vendor,device);
// Read a field from the given PCI device
uint32_t pci_read_field(pci_device_t device, field, size);

初始化设备

在我们对 guest 设备做任何事情之前,我们需要向它介绍自己。当前版本的 VirtualBox 支持两种协议:1.03 和 1.04。我们将使用 1.03,即所谓的“遗留协议”,因为它稍微简单一些。

#define VBOX_VENDOR_ID 0x80EE
#define VBOX_DEVICE_ID 0xCAFE
#define VBOX_VMMDEV_VERSION 0x00010003
#define VBOX_REQUEST_HEADER_VERSION 0x10001

#define VBOX_REQUEST_GUEST_INFO 50

/* VBox Guest packet header */
struct vbox_header {
        uint32_t size; /* Size of the entire packet (including this header) */
        uint32_t version; /* Version; always VBOX_REQUEST_HEADER_VERSION */
        uint32_t requestType; /* Request code */
        int32_t  rc; /* This will get filled with the return code from the requset */
        uint32_t reserved1; /* These are unused */
        uint32_t reserved2;
};

/* VBox Guest Info packet (legacy) */
struct vbox_guest_info {
        struct vbox_header header;
        uint32_t version;
        uint32_t ostype;
};

static pci_device_t vbox_pci;
static int vbox_port;
static uint32_t * vbox_vmmdev;

static void vbox_guest_init(void) {
    /* Find the guest device */
    pci_device_t vbox_pci = pci_find(VBOX_VENDOR_ID, VBOX_DEVICE_ID);
    
    /* BAR0 is the IO port. */
    vbox_port = pci_read_field(vbox_pci, PCI_BAR0, 4) & 0xFFFFFFFC;

    /* BAR1 is the memory-mapped "vmmdevmem" area. */
    vbox_vmmdev = map_physical_page(pci_read_field(vbox_pci, PCI_BAR1, 4) & 0xFFFFFFF0);

    /* Allocate some space for our Guest Info packet */
    uint32_t guest_info_phys;
    struct vbox_guest_info * guest_info = allocate_physical_page(&guest_info_phys);

    /* Populate the packet */
    guest_info->header.size = sizeof(struct vbox_guest_info);
    guest_info->header.version = VBOX_REQUEST_HEADER_VERSION;
    guest_info->header.requestType = VBOX_REQUEST_GUEST_INFO;
    guest_info->header.rc = 0;
    guest_info->header.reserved1 = 0;
    guest_info->header.reserved2 = 0;
    guest_info->version = VBOX_VMMDEV_VERSION;
    guest_info->ostype = 0; /* 0 = Unknown (32-bit); we don't need to lie about being another OS here */

    /* And send it to the VM */
    outportl(vbox_port, guest_info_phys);

    /* (We could check the return value here as well) */
}


自动调整客户机显示大小

我们将要了解的 Guest Additions 的第一个功能是“自动调整客户机显示大小”,它将允许你的操作系统获知在显示适配器上使用的最佳分辨率。VirtualBox 显示适配器本身基于 Bochs/Qemu 显示适配器,尽管它使用不同的 PCI 厂商和设备 ID。它允许设置任何分辨率(在较旧版本中,要求宽度为 4 的倍数)。启用此功能后,我们可以在主机窗口大小改变时接收中断,并且查询 guest 设备将告诉我们应将显示设置为什么分辨率。

我们将需要定义一些新的数据包,安装一个中断处理程序,并在 VMMDevMem 空间中设置一些位。

#define VBOX_REQUEST_ACK_EVENTS 41
#define VBOX_REQUEST_GET_DISPLAY_CHANGE 51
#define VBOX_REQUEST_SET_GUEST_CAPS 55

/* VBox Guest Capabilities packet */
struct vbox_guest_caps {
        struct vbox_header header;
        uint32_t caps;
};

/* VBox Acknowledge Events packet */
struct vbox_ack_events {
        struct vbox_header header;
        uint32_t events;
};

/* VBox GetDisplayChange packet */
struct vbox_display_change {
        struct vbox_header header;
        uint32_t xres;
        uint32_t yres;
        uint32_t bpp;
        uint32_t eventack;
};

/* We'll use separate pages for our packets for simplicity. */
static uint32_t vbox_display_phys;
static uint32_t vbox_ack_phys;
static vbox_display_change * vbox_display;
static vbox_ack_events * vbox_ack;

/* Adjust as necessary for your interrupt handling. */
static int vbox_irq_handler(struct regs * r) {
    if (!vbox_vmmdev[2]) return 0; /* Nothing to process? Maybe this interrupt was from something sharing the line. */

    vbox_ack->events = vbox_vmmdev[2];
    outportl(vbox_port, vbox_ack_phys); /* Acknowledge events */

    outportl(vbox_port, vbox_display_phys); /* Request display change information. */

    /* vbox_display now has information on our display size. If it changed we can tell our display driver to update. */
    set_new_graphics_mode_maybe(vbox_display->xres, vbox_display->yres, vbox_display->bpp);

    /* You probably want to make sure you have a way of informing your userspace that the
       display resolution is changing, so your window manager or whatever updates. */

    /* Don't forget to acknowledge the interrupt itself if you need to. */
    return 1;
}

static void vbox_guest_init(void) {
    ...

    /* Install an interrupt handler. */
    int irq = pci_read_field(vbox_pci, PCI_INTERRUPT_LINE, 1);
    install_interrupt_handler(irq, vbox_irq_handler);

    ...

    /* We need to tell the VM that we support this capability. The Guest Capabilities request tells the VM about our ability to support seamless and auto-resize modes. */
    uint32_t guest_caps_phys;
    struct vbox_guest_caps * guest_caps = allocate_physical_page(&guest_caps_phys);

    guest_caps->header.size = sizeof(struct vbox_guest_caps);
    guest_caps->header.version = VBOX_REQUEST_HEADER_VERSION;
    guest_caps->header.requestType = VBOX_REQUEST_SET_GUEST_CAPS;
    guest_caps->header.rc = 0;
    guest_caps->header.reserved1 = 0;
    guest_caps->header.reserved2 = 0;
    guest_caps->caps = 1 << 2; /* set bit 2, which indicates we support "graphics" (auto-resize guest display). */
    outportl(vbox_port, guest_caps_phys);

    /* We'll also set up the packets we'll use later for AcknowledgeEvents and GetDisplayChange */
    vbox_ack = allocate_physical_page(&vbox_ack_phys);
    vbox_ack->header.size = sizeof(struct vbox_ack_events);
    vbox_ack->header.version = VBOX_REQUEST_HEADER_VERSION;
    vbox_ack->header.requestType = VBOX_REQUEST_ACK_EVENTS;
    vbox_ack->header.rc = 0;
    vbox_ack->header.reserved1 = 0;
    vbox_ack->header.reserved2 = 0;
    vbox_ack->events = 0;

    vbox_display = allocate_physical_page(&vbox_display_phys);
    vbox_display->header.size = sizeof(struct vbox_display_change);
    vbox_display->header.version = VBOX_REQUEST_HEADER_VERSION;
    vbox_display->header.requestType = VBOX_REQUEST_GET_DISPLAY_CHANGE;
    vbox_display->header.rc = 0;
    vbox_display->header.reserved1 = 0;
    vbox_display->header.reserved2 = 0;
    vbox_display->xres = 0;
    vbox_display->yres = 0;
    vbox_display->bpp = 0;
    vbox_display->eventack = 1;

    /* Finally, we need to enable interrupts for the capabilities we've advertised. We're just going to enable all of them. */
    vbox_vmmdev[3] = 0xFFFFFFFF;
}

鼠标集成

鼠标集成使用绝对坐标系提供鼠标位置信息。不过,它不提供鼠标按钮信息,鼠标按钮信息仍通过标准 PS/2(或 USB)鼠标设备传递。如果你的操作系统支持 USB 设备,鼠标集成可以通过 USB 平板设备实现,而不是通过本文描述的机制。

鼠标集成完全通过 guest 设备运行。一旦启用,鼠标移动将通过鼠标数据包请求和相应的中断发送到客户机。需要注意的是,这些数据包中坐标的格式基于 0 到 0xFFFF 的范围,需要缩放到显示分辨率。为什么采用这种方法而不是使用实际的像素坐标(缩放或不缩放)尚不清楚。

#define VBOX_REQUEST_GET_MOUSE 1
#define VBOX_REQUEST_SET_MOUSE 2

/* The Mouse packet is used both to advertise our guest capabilities and to receive mouse movements. */
struct vbox_mouse_absolute {
        struct vbox_header header;
        uint32_t features;
        int32_t x;
        int32_t y;
};

static uint32_t vbox_mouse_phys;
static vbox_mouse_absolute * vbox_mouse;

static int vbox_irq_handler(struct regs * r) {
    ...

    outportl(vbox_port, vbox_mouse_phys);

    ...

    /* The mouse coordinates are scaled to the range (0x0,0xFFFF) independently in each dimension, so let's convert to pixels.
       If you prefer to have a more accurate mouse (subpixel support, etc.) you can convert to something else. */
    unsigned int x = ((unsigned int)vbox_mouse->x * display_resolution_width) / 0xFFFF;
    unsigned int y = ((unsigned int)vbox_mouse->y * display_resolution_height) / 0xFFFF;
    do_something_useful_with_mouse_coordinates(x,y);
    
    ...
}

static void vbox_guest_init(void) {
    ...

    vbox_mouse = allocate_physical_page(&vbox_mouse_phys);
    vbox_mouse->header.size = sizeof(struct vbox_mouse_absolute);
    vbox_mouse->header.version = VBOX_REQUEST_HEADER_VERSION;
    vbox_mouse->header.requestType = VBOX_REQUEST_SET_MOUSE;
    vbox_mouse->header.rc = 0;
    vbox_mouse->header.reserved1 = 0;
    vbox_mouse->header.reserved2 = 0;
    vbox_mouse->features = (1 << 0) | (1 << 4); /* bit 0 says "guest supports (and wants) absolute mouse"; bit 4 says we'll query absolute positions on interrupts */
    vbox_mouse->x = 0;
    vbox_mouse->y = 0;
    outportl(vbox_port, vbox_mouse_phys);

    vbox_mouse->header.requestType = VBOX_REQUEST_GET_MOUSE; /* Change the packet to a Get packet for use in the interrupt handler. */

    ...
}

无缝桌面

VirtualBox 中的无缝桌面模式通过向虚拟机发送一组矩形来工作,这些矩形表示桌面上非背景的可见区域。主机创建一个无边框的最大化窗口,并根据客户机提供的矩形对其进行整形,从而产生无缝桌面的外观。你可以将其与一个从你的 GUI 接收窗口边界信息的驱动程序结合起来。注意,无缝模式在(也许需要?)自动调整客户机显示大小支持下效果最佳。

#define VBOX_REQUEST_SET_VISIBLE_REGION 71

struct vbox_rtrect {
    int32_t xLeft;
    int32_t yTop;
    int32_t xRight;
    int32_t yBottom;
}

struct vbox_visibleregion {
    struct vbox_header header;
    uint32_t count;
    struct vbox_rtrect rect[];
}

static uint32_t vbox_visibleregion_phys;
static vbox_visibleregion * vbox_visibleregion;

...

static void vbox_set_visible_region(uint32_t count, ...) {
    vbox_visibleregion = allocate_physical_page(&vbox_visibleregion_phys);
    vbox_visibleregion->header.size = sizeof(struct vbox_visibleregion) + sizeof(vbox_rtrect) * count;
    vbox_visibleregion->header.version = VBOX_REQUEST_HEADER_VERSION;
    vbox_visibleregion->header.requestType = VBOX_REQUEST_SET_VISIBLE_REGION;
    vbox_visibleregion->header.rc = 0;
    vbox_visibleregion->header.reserved1 = 0;
    vbox_visibleregion->header.reserved2 = 0;
    vbox_visibleregion->count = count;

    /* Set each of the rectangles... */
    vbox_visibleregion->rect[i].xLeft = ...;
    /* etc */
    
    outportl(vbox_port, vbox_visibleregion_phys);
}

还有一个使用(未文档化的)主机-客户机通信接口的无缝模式附加接口。就无缝模式而言,此功能仅通信是否已请求无缝模式,并且对于其运行并非必需。

VirtualBox 代码库中还有线索表明,曾计划实现直接的客户机-主机窗口映射功能,但尚未实现。这大概可以让客户机为每个窗口提供单独的纹理,可能还包括窗口标题和图标信息……

指针形状

指针形状功能允许你向主机发送鼠标光标纹理,从而省去你自己在软件中绘制它的需要。此功能与无缝模式结合使用时特别有用,因为它允许你的光标在可见区域之外渲染。此功能与绝对鼠标功能配合工作,因此无需向主机告知鼠标光标位置,因为它已经知道。

共享文件夹和剪贴板

虚拟 Guest Additions PCI 设备提供对主机上称为 Host-Guest Communication Manager (HGCM) 的服务的访问。该服务负责为共享文件夹、共享剪贴板提供功能,并对客户机本身进行配置更改。

对通信管理器的所有请求都使用相同的数据包头,并且数据包类型字段使用 5 个值:

值(十进制) 请求类型
60 连接
61 断开连接
62 调用函数(32 位)
63 调用函数(64 位)
64 取消请求

在标准 Guest Addition 头之后,每个通信管理器数据包(取消请求数据包除外)都有第二个头,具体取决于上面的请求类型。

连接

偏移量(十六进制) 长度 描述
0x20 4 位置类型
0x24 128 位置
0xA4 4 客户端 ID

位置字段包含要连接的库的名称。有 4 个预定义的库名称:VBoxSharedFolders、VBoxSharedClipboard、VBoxGuestPropSvc、VBoxSharedOpenGL

通过在位置字段中提供库名称,还可以访问其他库。

位置类型字段应设置为 1 以连接到其他库,设置为 2 以连接到预定义库之一。

客户端 ID 在请求完成后由主机填充。此值应用于对该服务的任何后续调用。

断开连接

偏移量(十六进制) 长度 描述
0x20 4 客户端 ID

调用函数

32 位和 64 位调用函数数据包都使用相同的服务头,后跟参数记录列表。32 位函数参数记录长度为 12 字节,64 位函数参数记录长度为 16 字节。

偏移量(十六进制) 长度 描述
0x20 4 类型
0x24 4 客户端 ID
0x28 4 函数代码
0x2c 4 参数计数
0x30 * 参数


32 位参数

偏移量(十六进制) 长度 描述
0x00 4 类型
0x04 8 值

64 位参数

偏移量(十六进制) 长度 描述
0x00 4 类型
0x04 12 值

参数类型

值(十进制) 请求类型
1 32 位值
2 64 位值
3 物理地址
4 线性地址
5 线性地址(仅主机到客户机)
6 线性地址(仅客户机到主机)
7 线性地址(由客户机预锁定)
8 线性地址(仅主机到客户机,由客户机预锁定)
9 线性地址(仅客户机到主机,由客户机预锁定)

物理地址和线性地址参数值具有以下结构:

偏移量(十六进制) 长度 描述
0x00 4 缓冲区长度
0x04 4/8 地址(32 位/64 位)

函数

值(十进制) 函数 参数
1 查询映射 3
2 查询映射名称 2
3 创建句柄 3
4 关闭句柄 2
5 读取 5
6 写入 5
7 锁定 5
8 列出目录 8
9 信息 5
11 删除 3
14 重命名 4
15 刷新 2
24 设置文件大小 3

查询映射

查询映射命令返回为此客户机配置的所有共享文件夹的列表。客户机传递的缓冲区将被填充映射记录,Count 参数将被设置为返回的记录数。每条记录的 Root 值将在后续命令中用于引用该共享文件夹。如果缓冲区大小不足以容纳所有记录,缓冲区指针大小字段将被设置为容纳整个映射记录列表所需的最小大小。默认情况下,主机返回的所有字符串均为 UCS2 编码。传递 UTF8 标志将强制所有请求以 UTF8 格式返回字符串。Automount Only 标志将仅返回在客户机配置中标记为 Automount 的共享文件夹。

编号 参数 类型
1 标志 32 位(0 - UCS2,1 - UTF8,2 - 仅自动挂载)
2 计数 32 位
3 缓冲区 指针

映射记录

偏移(十六进制) 长度 描述
0x00 4 标志(1 - 新增,2 - 已删除)
0x04 4 根


查询映射名称

查询映射名称命令将返回客户机配置中指定共享文件夹的名称。

编号 参数 类型
1 根 32 位
2 缓冲区 指针


其他内容

VirtualBox 还提供了一种写入其日志文件的机制,不过这不是通过客户机设备管理的。简单地向端口 0x504 写入字节即可产生日志条目。此功能的能力位为位 0。


另请参阅

外部链接

来源与翻译说明

本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。

原文:VirtualBox Guest Additions;离线修订号:28067。

原作者:OSDev Wiki 社区贡献者。原文及引用资料的版权与许可声明继续适用:版权声明 许可说明

评论

暂无评论