core: Account for 64-bit overflow in open-coded div_u64_u64

We run into an endless loop when trying to divide something which has
bit 63 set. In this case, tmp_div will overrun and never become larger
than the dividend. Detect this case and terminate the inner loop
properly.

Reported-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
driver: Catch unconfigured CPUs earlier

As the driver allocates per-CPU buffers according to the system
configuration, calling into the hypervisor with a CPU outside that range
can cause a crash instead of a proper error return. Catch this case
earlier, already in the driver.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Revert "core: Validate CPU ID before using it"

This reverts commit c1eb94e35d03a71bb528fe1a6ab5304e840f91b0.

This attempt to protect us from out-of-bounds CPU IDs is futile: already
arch_entry requires a usable per-cpu data structure to save and - in
case of an error - restore Linux state. It's the driver's duty to
prevent hypervisor invocations on out-of-bounds CPUs.

Moreover the patch contained a regression as it passed an uninitialized
member of cpu_data to cpu_id_valid.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: add memory flag ROOTSHARED and use it

This patch introduces a memory flag to express shared memory with the
root cell. Memory regions with that flag will stay mapped in the root
cell on non-root-cell creation and do not need to be mapped back into
the root cell on non-root-cell destruction.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86 apic: make sure to only send valid irqs

struct apic_irq_message has a valid field, make sure to call
apic_send_irq only in cases where this bit is set. This protects us
against sending invalid interrupts during handover when the Linux setup
should ever be broken.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
[Jan: extended commit description]
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
apic: extract irq validation logic from iommu_map_interrupt

Introducing a new function apic_irq_dest_in_cell that makes sure the
destination cpu of the interrupt belongs to the given cell.
So far this code was part of iommu_map_interrupt but these checks are
required in other places as well.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
[Jan: trivial formatting adjustments]
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
pci: move define from pci.c to general pci header

This prepares for using the define in other C-files.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
pci: ignore PCI bridge violations in Linux to allow bus rescans

When adding new devices Linux will access the PCI root bridge. Introduce
a HACK that will ignore the access and not treat it as a violation.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
driver, pci: add functions to add/del devices to/from root-cell

This patch introduces functions to add or remove PCI devices in the root
cell. The functions are not called yet, the patch prepares for virtual
PCI devices.
The original idea was to remove PCI devices from Linux on cell create
and re-add them on cell destroy. But in this case Linux could reprogram
the BARs so we ignore regular PCI devices for now.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
driver: add/remove virtual PCI devices to/from root-cell

Introduce a new PCI device class and make Linux discover these virtual
PCI devices on enable and remove them on disable.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: cell-config: add field to PCI device struct to refer to memory

Add shmem_region to struct jailhouse_pci_device to be able to refer to
the shared memory region used by the virtual PCI device used for
inter-cell communication.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
pci: apic: make function non-static

Make the function pci_translate_msi_vector available for code outside of
arch/x86/pci.c.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
pci: add a bunch of defines to pci.h

Add a few general defines to pci.h. They will be used in a later commit
by the ivshmem virtual PCI device but are not specific to it.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
pci: add list for virtual PCI devices to cell, makes lookup faster

Add a list of virtual PCI devices to a cell, that way we do need to go
through all PCI devices when looking for virtual ones. This lookup is on
the mmio path, which should be fast.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
pci: refactor error path of pci_cell_init()

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: add load barrier for serialization

Introduce a memory load barrier function.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
pci_ivshmem: add PCI shared memory device

This patch adds support for a virtio shared memory device which can be
used for inter-cell communication.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
inmates, configs: add ivshmem virtual PCI device demo code and config

New demo to demonstrate ivshmem virtual PCI devices for shared memory
inter-cell communication.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
[Jan: adjusted region location]
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
configs: qemu: add a virtual PCI device to qemu config

This adds a virtual PCI device of type ivshmem to the root-cell on qemu.
Starting a cell with an ivshmem at the same bdf, refering to the same
memory will allow the two cells to connect.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
[Jan: adjusted memory regions to avoid overlappings]
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
docs: add documentation for inter-cell communication

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>