Skip to content

Commit

Permalink
vm_arm: move vpci code to dedicated module
Browse files Browse the repository at this point in the history
This calls vmm_pci_init() only when vpci is enabled.

Signed-off-by: Axel Heider <[email protected]>
  • Loading branch information
Axel Heider committed Jan 24, 2024
1 parent c372ff4 commit ec9db48
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 40 deletions.
9 changes: 8 additions & 1 deletion arm_vm_helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,18 @@ function(DeclareCAmkESARMVM init_component)
vm_src
${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/main.c
${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/fdt_manipulation.c
${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/crossvm.c
${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/modules/map_frame_hack.c
${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/modules/init_ram.c
)

if(VmPCISupport)
list(
APPEND
vm_src ${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/modules/vpci.c
${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/crossvm.c
)
endif()

if(VmVirtUart)
list(APPEND vm_src ${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/modules/vuart_init.c)
endif()
Expand Down
44 changes: 5 additions & 39 deletions components/VM_Arm/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@
#include <sel4vm/guest_irq_controller.h>

#include <sel4vmmplatsupport/drivers/virtio_con.h>

#include <sel4vmmplatsupport/arch/vusb.h>
#include <sel4vmmplatsupport/arch/vpci.h>
#include <sel4vmmplatsupport/guest_image.h>
#include <sel4vmmplatsupport/drivers/pci_helper.h>
#include <sel4vmmplatsupport/drivers/cross_vm_connection.h>
#include <sel4vmmplatsupport/arch/vusb.h>
#include <sel4vmmplatsupport/arch/guest_boot_init.h>
#include <sel4vmmplatsupport/arch/guest_reboot.h>
#include <sel4vmmplatsupport/arch/guest_vcpu_fault.h>
Expand Down Expand Up @@ -97,7 +93,6 @@ allocman_t *allocman;
seL4_CPtr _fault_endpoint;
irq_server_t *_irq_server;

vmm_pci_space_t *pci;
vmm_io_port_list_t *io_ports;
reboot_hooks_list_t reboot_hooks_list;

Expand Down Expand Up @@ -687,17 +682,6 @@ extern vmm_module_t __stop__vmm_module[];

static int install_vm_devices(vm_t *vm, const vm_config_t *vm_config)
{
int err;

/* Install virtual devices */
if (config_set(CONFIG_VM_PCI_SUPPORT)) {
err = vm_install_vpci(vm, io_ports, pci);
if (err) {
ZF_LOGE("Failed to install VPCI device");
return -1;
}
}

int max_vmm_modules = (int)(__stop__vmm_module - __start__vmm_module);
int num_vmm_modules = 0;
for (vmm_module_t *i = __start__vmm_module; i < __stop__vmm_module; i++) {
Expand All @@ -707,7 +691,6 @@ static int install_vm_devices(vm_t *vm, const vm_config_t *vm_config)
}

return 0;

}

static int route_irq(int irq_num, vm_vcpu_t *vcpu, irq_server_t *irq_server)
Expand Down Expand Up @@ -875,22 +858,11 @@ static int vm_dtb_finalize(vm_t *vm, const vm_config_t *vm_config)
assert(vm_config->generate_dtb);

if (config_set(CONFIG_VM_PCI_SUPPORT)) {
/* Modules can add PCI devices, so the PCI device tree node can be
* created only after all modules have been set up.
*/
int gic_offset = fdt_path_offset(fdt_ori, GIC_NODE_PATH);
if (gic_offset < 0) {
ZF_LOGE("Failed to find gic node from path: %s", GIC_NODE_PATH);
return -1;
}
int gic_phandle = fdt_get_phandle(fdt_ori, gic_offset);
if (0 == gic_phandle) {
ZF_LOGE("Failed to find phandle in gic node");
return -1;
}
int err = fdt_generate_vpci_node(vm, pci, gen_dtb_buf, gic_phandle);
extern int vpci_update_dtb(vm_t *vm, const vm_config_t *vm_config, void *dtb,
void *fdt, char const *gic_node);
int err = vpci_update_dtb(vm, vm_config, gen_dtb_buf, fdt_ori, GIC_NODE_PATH);
if (err) {
ZF_LOGE("Couldn't generate vpci_node (%d)", err);
ZF_LOGE("Couldn't generate VPCI device tree node (%d)\n", err);
return -1;
}
}
Expand Down Expand Up @@ -1191,12 +1163,6 @@ static int main_continued(void)
return -1;
}

err = vmm_pci_init(&pci);
if (err) {
ZF_LOGE("Failed to initialise vmm pci");
return err;
}

err = vmm_io_port_init(&io_ports, FREE_IOPORT_START);
if (err) {
ZF_LOGE("Failed to initialise VM ioports");
Expand Down
71 changes: 71 additions & 0 deletions components/VM_Arm/src/modules/vpci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2023, Hensoldt Cyber
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <vmlinux.h>
#include <camkes.h>

#include <sel4vmmplatsupport/ioports.h>
#include <sel4vmmplatsupport/arch/vpci.h>
#include <sel4vmmplatsupport/drivers/pci_helper.h>

#include <libfdt.h>

extern vmm_io_port_list_t *io_ports;

/* Can't make this static, because others uses it */
vmm_pci_space_t *pci = NULL;

/* Various other modules can register PCI devices. Update the device tree
* eventually.
*/
int vpci_update_dtb(vm_t *vm, const vm_config_t *vm_config, void *dtb,
void *fdt, char const *gic_node)
{
if (!vm_config->generate_dtb) {
return 0;
}

ZF_LOGF_IF(!fdt_ori, "fdt not set");

int gic_offset = fdt_path_offset(fdt, gic_node);
if (gic_offset < 0) {
ZF_LOGE("Failed to find gic node from path: %s", gic_node);
return -1;
}
int gic_phandle = fdt_get_phandle(fdt, gic_offset);
if (0 == gic_phandle) {
ZF_LOGE("Failed to find phandle in gic node (%d)", gic_phandle);
return -1;
}
int err = fdt_generate_vpci_node(vm, pci, dtb, gic_phandle);
if (err) {
ZF_LOGE("Couldn't generate vpci_node (%d)", err);
return -1;
}

}

static void vpci_init_module(vm_t *vm, void *cookie)
{
int err;

err = vmm_pci_init(&pci);
if (err) {
ZF_LOGE("Failed to initialise vmm pci (%d)", err);
return;
}

ZF_LOGF_IF(!io_ports, "io_ports not set");

err = vm_install_vpci(vm, io_ports, pci);
if (err) {
ZF_LOGE("Failed to install VPCI device (%d)", err);
return;
}

}

DEFINE_MODULE(vpci, NULL, vpci_init_module)

0 comments on commit ec9db48

Please sign in to comment.