tools: config-create: drop line numbers in parse_iomem_tree()

Previously we defined the children of a MemRegion-Tree as set. This
caused the order of the children to be undefined as soon as they got
added to this structure. But the using function of parse_iomem_tree()
wanted the list of regions returned to be sorted like in the file, so we
had to maintain a separate list of line numbers.

By changing the set to a list, the order is always maintained and
because parse_iomem_tree() only adds to his private regions-list and
never inserts or reorders, this private structure will also always have
the same order as in the file. With this we can remove the
line numbers list. This structure was never use for anything else, so it
will make the code more readable.

Signed-off-by: Benjamin Block <bebl@mageta.org>
Reviewed-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: config-create: exclude kernel-area from mem-allocation

When allocating memory for jailhouse, we currently don't look at where
the kernel lives on the system. In case our `memmap` overlaps with this
area, the kernel will just ignore the kernel-cmd-option and out generated
configuration will be invalid.

To fix this, the script now looks out for any kernel-region while
parsing the tree from /proc/iomem and splits the parent-region on the
kernels boundaries. This region will then not be considered anymore and
could additionally be handled special in other cases in the future. The
config will contain it as extra memory-region with all rights.

Signed-off-by: Benjamin Block <bebl@mageta.org>
Reviewed-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: config-create: drop line numbers from IOMemRegionTree object

The line numbers are not used anymore, so drop the code dealing with
them.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: Detect HPET also in case of deeper nesting

Do not stop searching for the HPET on the first level of a reserved
region, it may hide deeper inside. Seen on an AMD-G series evaluation
system:

  fed00000-fed00fff : reserved
    fed00000-fed00fff : pnp 00:10
      fed00000-fed003ff : HPET 0

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Break up PERCPU_SIZE_SHIFT

Rename PERCPU_SIZE_SHIFT to PERCPU_SIZE_SHIFT_ASM, the currently
still manually defined version of this constant. PERCPU_SIZE_SHIFT is
now defined via a C expression and can thus only be used outside
assembler code.

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

Will be used for asm-defines soon.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: Add asm-defines.h generation

Adapted Linux kernel tools to generate asm-offset.h for Jailhouse.
This is meant to generate common defines (C structure member offsets
and other compile-time expressions) in a way accessible from the
assembly code.

Signed-off-by: Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
[Jan: moved to include/generated, added copyright headers, fix typo]
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Use asm-defines.h for struct per_cpu members

PERCPU_* defines are now generated from C sources rather than being
hard-coded, which makes the code less fragile. CHECK_ASSUMPTION
infrastructure is now gone.

Signed-off-by: Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
[Jan: remove split-up of percpu.h, use FIELD_SIZEOF]
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Remove struct farptr

This structure was previously used to reload CS register. As we switched
to the new switching method now, it is no longer needed.

Signed-off-by: Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Control interrupts in vendor-specific way

Jailhouse runs with GIF cleared on AMD, and simple 'sti' and 'cli'
aren't enough to enable and disable interrupts. This affects apic_clear()
which fails to reset IRR on AMD. To overcome this, former enable_irq()
and disable_irq() are now defined in svm.c/vmx.c in vendor-specific way.

Signed-off-by: Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
[Jan: convert documentation into doxygen format]
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Comments for apic_clear()

Added some comments for main steps that apic_clear() function
performs to restore APIC to the reset state.

Signed-off-by: Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: Explain asm-defines generation code origins

Explicitly mention code that was taken from Linux kernel in corresponding
source files' licensing headers.

Signed-off-by: Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: remove redundant test on error-code

The global error-state is checked before a call to cpu_init and checked
again in it. Because this happens under a lock and the global state is
never touched in cpu_init - or any sub-call -, the second check is
redundant and will always be true, thus remove it.

Signed-off-by: Benjamin Block <bebl@mageta.org>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
vtd: intremap: fix calculation of maximum amount of msi(x) vectors

Fix typo that prevented MSI-X remapping regions to get freed correctly.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Bounds check for apic_reserved_bits[] array

apic_accessing_reserved_bits() function doesn't sanitize 'reg' argument.
Malicious cell can use this fact to perform out-of-bounds read in
apic_reserved_bits[] array in host mode, which should never happen.

With this patch, all APIC register bits not listed in apic_reserved_bits[]
are considered reserved.

Signed-off-by: Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Fix mask of reserved bits for error LVT

Only bit 16 (mask) is writable, not bits 17 and 18. Was copied & pasted
from timer LVT.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Handle Extended APIC Register Space better

Extended APIC Register Space is AMD feature which was initially supported
in Jailhouse with a quick hack. It's fixed with this patch: now, the presence
of Extended APIC Register Space is determined as per APMv2, Sect. 16.3.4, and
the number of Extended Local Vector Tables is also determined in runtime.

As all of these is now done inside apic.c, and there is no need to keep
apic_reserved_bits[] public anymore.

Signed-off-by: Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: Mark hexdigit string static

ARM inmates have troubles processing a non-static version, and it also
makes no sense to have this constant on the stack.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
inmates: set BITS_PER_LONG for printk-core

printk-core.c uses BITS_PER_LONG and is included into inmate printk. In
order order to use that code correctly inmates need that define.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Use BITS_PER_LONG instead of hard-coded value

PERCPU_SIZE_SHIFT relied on sizeof(long) being 64 bit in size, which is always
true in Jailhouse but is also a potential breakage point. Fixed with
BITS_PER_LONG macro now.

Signed-off-by: Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>