x86: Also flush caches of CPU calling flush_root_cell_cpu_caches

Currently, we flush the caller's caches only inconsistently via
vmx_root_cell_shrink, but we fail to do this on cell destruction and we
do not flush TLBs of the caller at all.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Make vtd_remove_device_from_cell more robust against non-existent devices

Do not crash if we call vtd_remove_device_from_cell for a device that is
not added to a cell, and there is even no corresponding context table.
This allows to use vtd_remove_device_from_cell e.g. for rollbacks of
half-done configurations after an error occurred.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Move vtd_remove_device_from_cell

Move vtd_remove_device_from_cell unmodified. We will need it earlier.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Fold vmx/vtd_root_cell_shrink into vmx/vtd_cell_init

This primarily fixes a regression of 46ab6c2f1e: Due to that reordering,
we were first adding devices to a new cell, then removing them from the
root cell which effectively disabled them in the context table.

Analyzing the content of vmx/vtd_root_cell_shrink, we are better off
folding them into the corresponding cell_init functions. We fix the
ordering issue while doing this.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core/configs: Clean up config structure alignment/packing

Instead of spreading aligned(1), we rather need to pack all config
structures and also the containers we define in the config files
themselves. Clean this up, also dropping the now unneeded padding from
jailhouse_cell_desc.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: Replace shrinking_set with root_cell.cpu_set in cell_create

Let's make it clear: the set we shrink is always the one of the root cell.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: Factor out cell_destroy_internal

We will reuse it to roll back failed cell creations.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Guard vtd_add_device_to_cell against addition of existing devices

Avoid that we add an already registered device to a cell, specifically
that we report this to the console. This case can happen soon when
rolling back failed cell creations.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Remove some unneeded local caching of cell->config

No hot-paths, we can save the local variables.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: Introduce arch_config_commit

This function primarily serves to split IOMMU activation from related
root cell and memory region mapping setup. This is required for generic
memory region mapping but also helps to consistently flush the affected
caches after configuration changes.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Pull root cell initialization into vmx/vtd_init

Provides a micro-simplification regarding error handling and belongs
together conceptually anyway.

Some constant name shortening is required to avoid line wrappings.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: Generic memory region mapping with rollback on error for cell creation

Pull the memory region mappings that currently happen in vmx and vtd
into generic code paths. This allows us to properly roll back on errors
during cell creation.

We now perform the arch-specific cell initialization first, then
transfer CPUs and finally remap the memory regions. For the rollback, we
can simply use the infrastructure available for cell destruction, both
at generic level as well as inside vmx/vtd.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: Add BIT_MASK macro and document BYTE_MASK

BIT_MASK will help defining constants according to hardware
specifications when bits [m:n] (m > n) form a field in a register or
data structure entry. Document also the BYTE_MASK macro at this chance.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Fix reading of vtd fault-recording register offset

The capability register is 64 bit wide.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Simplify vtd_get_fault_rec_reg_addr

The logic remains readable when written in a single statement.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Refactor vtd constant definitions

Make use of BIT_MASK and refactor the constant definitions used for vtd
into a consistent form that is more easily verifiable against the spec.
Drop unneeded constants.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core/configs: Change IRQ line access control modeling

Change the configuration file to manage access to IRQ lines at IRQ chip
level. Each IRQ chip config entry consists of a unique identifier,
typically the chip's MMIO address, and a bitmap controlling access to
individual IRQs lines of that chip. This will simplify access control
checks to IRQ chips.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Add support for Mod 1 to MMIO parser

The only difference to Mod 2 is the length of the displacement: 1
instead of 4 bytes.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Add support for IOAPIC access control

This adds basic access control to the IOAPIC. Based on the IRQ chip
configuration, we permit or deny writing to redirection table entries.
This needs to be integrated with interrupt remapping later on.

We furthermore allow reads from other valid IOAPIC registers but deny
any other write accesses.

EOI writing is currently passed through. This will have to be
virtualized as well when interrupt remapping is added.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
configs: Make use of IOAPIC moderation

Remove the IOAPIC page from the memory regions and add the IOAPIC as
irqchip instead.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>