Commit Graph

1143116 Commits

Author SHA1 Message Date
Eric Biggers
72c995ce88 UPSTREAM: crypto: algboss - compile out test-related code when tests disabled
When CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is set, the code in algboss.c
that handles CRYPTO_MSG_ALG_REGISTER is unnecessary, so make it be
compiled out.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Bug: 256875295
(cherry picked from commit 441cb1b730006bd2d636f72dc7f6e11a8a0ecce5)
Change-Id: I11ebf60e1915ad5d13bd16a26d6c2c0944b4c401
Signed-off-by: Eric Biggers <ebiggers@google.com>
2022-12-16 19:00:01 +00:00
Eric Biggers
93b8f2a16f UPSTREAM: crypto: kdf - silence noisy self-test
Make the kdf_sp800108 self-test only print a message on success when
fips_enabled, so that it's consistent with testmgr.c and doesn't spam
the kernel log with a message that isn't really important.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Bug: 256875295
(cherry picked from commit 790c4c9f532318e3fe8c6f0b498072abc80e1195)
Change-Id: Icb2c93c0ed51789fd425d36ebcaebf01af6b4c78
Signed-off-by: Eric Biggers <ebiggers@google.com>
2022-12-16 19:00:01 +00:00
Eric Biggers
4ce9b3423d UPSTREAM: crypto: kdf - skip self-test when tests disabled
Make kdf_sp800108 honor the CONFIG_CRYPTO_MANAGER_DISABLE_TESTS kconfig
option, so that it doesn't always waste time running its self-test.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Bug: 256875295
(cherry picked from commit 0bf365c0efdd8fc03cb82e381ea4d76196c66bc2)
Change-Id: Ib189f727a7bb1a231cc8ec4ca6450e685bd678f7
Signed-off-by: Eric Biggers <ebiggers@google.com>
2022-12-16 19:00:01 +00:00
Eric Biggers
1e15da5148 UPSTREAM: crypto: api - compile out crypto_boot_test_finished when tests disabled
The crypto_boot_test_finished static key is unnecessary when self-tests
are disabled in the kconfig, so optimize it out accordingly, along with
the entirety of crypto_start_tests().  This mainly avoids the overhead
of an unnecessary static_branch_enable() on every boot.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Bug: 256875295
(cherry picked from commit 06bd9c967eaac5484c31c3dc6dfbef6183819508)
Change-Id: I68eff9772dc219a8786bf410cb4e946052ea7811
Signed-off-by: Eric Biggers <ebiggers@google.com>
2022-12-16 19:00:01 +00:00
Eric Biggers
8c54209f4a UPSTREAM: crypto: algboss - optimize registration of internal algorithms
Since algboss always skips testing of algorithms with the
CRYPTO_ALG_INTERNAL flag, there is no need to go through the dance of
creating the test kthread, which creates a lot of overhead.  Instead, we
can just directly finish the algorithm registration, like is now done
when self-tests are disabled entirely.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Bug: 256875295
(cherry picked from commit 9cadd73adef1e1d53ea100f28e3e258698b92418)
Change-Id: I10f814cd6903d41265f69297d8568b43ec30012e
Signed-off-by: Eric Biggers <ebiggers@google.com>
2022-12-16 19:00:01 +00:00
Eric Biggers
6b510a1423 UPSTREAM: crypto: api - optimize algorithm registration when self-tests disabled
Currently, registering an algorithm with the crypto API always causes a
notification to be posted to the "cryptomgr", which then creates a
kthread to self-test the algorithm.  However, if self-tests are disabled
in the kconfig (as is the default option), then this kthread just
notifies waiters that the algorithm has been tested, then exits.

This causes a significant amount of overhead, especially in the kthread
creation and destruction, which is not necessary at all.  For example,
in a quick test I found that booting a "minimum" x86_64 kernel with all
the crypto options enabled (except for the self-tests) takes about 400ms
until PID 1 can start.  Of that, a full 13ms is spent just doing this
pointless dance, involving a kthread being created, run, and destroyed
over 200 times.  That's over 3% of the entire kernel start time.

Fix this by just skipping the creation of the test larval and the
posting of the registration notification entirely, when self-tests are
disabled.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Bug: 256875295
(cherry picked from commit a7008584ab19d2df05caa95634cd72bc41f4cad3)
Change-Id: Ia6be068618e9286c1be01415a6766ba2fa94fc0d
Signed-off-by: Eric Biggers <ebiggers@google.com>
2022-12-16 19:00:01 +00:00
Will Deacon
72cc19df8b ANDROID: KVM: arm64: Add support for non-cacheable mappings
Hypervisor vendor modules may need to create non-cacheable mappings in
the hypervisor stage-1 for interacting with devices such as IOMMUs.

Add support for this memory type to the KVM pgtable API and implement
it for both stage-1 and stage-2.

Bug: 244373730
Signed-off-by: Will Deacon <willdeacon@google.com>
Change-Id: I2f88db7fe47e16366018e3e48f30d09b299ae6e4
2022-12-15 18:32:35 +00:00
Will Deacon
e38c4149ed ANDROID: KVM: arm64: Don't filter out KVM_FUNC_MMIO_GUARD_MAP hypercalls
If a KVM_FUNC_MMIO_GUARD_MAP hypercall from a protected guest fails at
EL2 due to running out of page-table memory, the call is forwarded to
the host so that additional memory can be donated using the vCPU's
memcache.

Unfortunately, the host filters out these calls the hypervisor will
replay the guest's HVC instruction forever, making no progress because
it will fail each time.

Avoid filtering out KVM_FUNC_MMIO_GUARD_MAP, in the same way as we
handle the SHARE and UNSHARE hypercalls.

Bug: 262700476
Cc: Keir Fraser <keirf@google.com>
Signed-off-by: Will Deacon <willdeacon@google.com>
Change-Id: Idd14c6bc08a4232939676e3566b79cbc7c927a3a
2022-12-15 17:44:36 +00:00
Sebastian Ene
1af7ed3212 ANDROID: KVM: arm64: Coalesce host stage2 entries on ownership reclaim
This optimization allows us to re-create higher order block mappings in
the host stage2 pagetables after we teardown a guest VM.
When the host reclaims ownership during guest teardown, the page table
walker drops the refcount of the counted entries and clears out
unreferenced entries (refcount == 1). Clearing out the entry installs a
zero PTE. When the host stage2 receives a data abort because there is no
mapping associated, it will try to create the largest possible block
mapping from the founded leaf entry.
With the current patch, we increase the chances of finding a leaf entry
that has level < 3 if the requested region comes from a reclaimed torned
down VM memory. This has the advantage of reducing the TLB pressure at
host stage2.

To increase the coalescing chances, we modify the way we refcount page
table descriptors for host stage2:
- non-zero invalid PTEs
- any of the reserved-high bits(58-55) toogled
- non-default attribute mappings
- page table descriptors

Bug: 222044487
Test: dump the host stage2 pagetables and view the mapping
Signed-off-by: Sebastian Ene <sebastianene@google.com>
Change-Id: I90ff4ec2185e9a76d7ad17e77ef9bdd8ce3e8698
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:22 +00:00
Sebastian Ene
9a46f648c8 ANDROID: KVM: arm64: Move kvm_pte_table to the common header
In preparation for the coalescing algorithm implementation, move the
function which verifies if a page table entry is a tabel to the common
header.

Bug: 222044487
Change-Id: I4124b7727e91f61b8f0a7e44cd91403d09d83c3c
Signed-off-by: Sebastian Ene <sebastianene@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:22 +00:00
Sebastian Ene
d096d35445 ANDROID: KVM: arm64: Have different callbacks for PTE manipulation
Move the host specific code for PTE reference counting out of the
pagetable code and define a new structure that wraps all the PTE
manipulation callbacks. This structure will be passed during the
pagetable code initialization and it allows to register different
callback for [guest|host].

Bug: 222044487
Signed-off-by: Sebastian Ene <sebastianene@google.com>
Change-Id: I116e8322935762df2f2be6e8d51a3f0c140b3d36
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:22 +00:00
Sebastian Ene
c4714ab75d ANDROID: KVM: arm64: Move PTE attributes definitions to the common header
Make PTE attribute definitions available from kvm_pgtable.h and take
them out of the pagetable code. These attributes will be used later in
mem_protect.c to construct different masks during the PTE manipulation
callbacks.

Bug: 222044487
Signed-off-by: Sebastian Ene <sebastianene@google.com>
Change-Id: I2f7108815ef0fa536e7f3314762a412119400fe9
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:22 +00:00
Sebastian Ene
ca5e2707ba ANDROID: KVM: arm64: Split stage2_put_pte function
Refactor the code and add stage2_clear_pte(..) which removes the PTE
without dropping the refcount for an entry.

Bug: 222044487
Signed-off-by: Sebastian Ene <sebastianene@google.com>
Change-Id: Ia2cb47f2ffad6faa5c6b4ec8a37bcbe61be0bc2f
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:22 +00:00
Sebastian Ene
901f361acc ANDROID: KVM: arm64: Pass the pagetable struct as an argument to the freewalker
Extend the scope of the stage2_freewalker by passing the pgt instead of
the mm_ops callbacks. This will later be used by the stage2_pte_is_counted
function.

Bug: 222044487
Signed-off-by: Sebastian Ene <sebastianene@google.com>
Change-Id: I390661eb106cbdb863cbb1832e39ec155c439091
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:21 +00:00
Quentin Perret
ed83f265a5 ANDROID: KVM: arm64: Fix link with CONFIG_MODULES=n
The recently introduced support for pKVM modules has clearly lacked a
bit of testing with CONFIG_MODULES=n.

Make sure to not access __pkvm_modules_enabled when CONFIG_MODULES=n to
fix the link.

Bug: 244543039
Bug: 244373730
Reported-by: kernel test robot <lkp@intel.com>
Change-Id: I65c6ad7c07c2812b85dcd435b24b3444c0ce7f1e
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:21 +00:00
Quentin Perret
4287da7498 ANDROID: KVM: arm64: Fix build with CONFIG_MODULES=n
The recently introduced support for pKVM modules has clearly lacked a
bit of testing with CONFIG_MODULES=n.

Fix the broken __pkvm_init_module() stub to make the bots happy.

Bug: 244543039
Bug: 244373730
Reported-by: kernel test robot <lkp@intel.com>
Change-Id: Ie59cbf46442721de78ef51523debadc4c156530e
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:21 +00:00
Vincent Donnefort
ec51620fe1 ANDROID: KVM: arm64: Block module loading based on cmdline or HVC
Disable module loading, based on the cmdline option
kvm-arm.protected_modules. If enabled, the feature can later be
irreversibly disabled with the HVC __pkvm_close_module_registration().

Bug: 244543039
Bug: 244373730
Change-Id: I693a1db350ae15bcaab59103a68b60f087dcc6b4
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:21 +00:00
Vincent Donnefort
9896e587b0 ANDROID: KVM: arm64: Support unaligned fixmap in the nVHE hyp
Return the fixmap VA with the page offset, instead of the page base
address.

Bug: 244543039
Bug: 229972309
Change-Id: I40c342f84e3cd395156ef846f0434c028d4e3fa3
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:21 +00:00
Vincent Donnefort
e2eb8807e6 ANDROID: KVM: arm64: Add support for custom hypercall registration
When pKVM is in use, allow pKVM modules to register custom hypercall
handlers:

   * pkvm_register_el2_call(): Give a handler to the hypervisor and gets in
     return the newly registered hypercall number.

   * pkvm_el2_mod_call(): Call the previously registered hypercall handler.

There is a limit of 128 hypercalls that can be registered.

Bug: 244543039
Bug: 244373730
Change-Id: I3d6c89675efe5f65f6b53c3b45ae155d1a00164c
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:21 +00:00
Vincent Donnefort
a8f7fefd69 ANDROID: KVM: arm64: Return a token for a pKVM module registration
Later introduction of custom HVCs will require to store some data.
Conviniently, the only thing we need is the start addr (in the hyp VA) of
the text section. So use this value as a token to avoid having to store
it anywhere.

Bug: 244543039
Bug: 244373730
Change-Id: Idd0bcbbb36d189aa4932833ca5b40382c2cddb08
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:21 +00:00
Quentin Perret
fc7032826c ANDROID: KVM: arm64: Introduce hyp_protect_host_page()
Introduce a new helper allowing to map host-owned pages with reduced
permissions (e.g. RO) at stage-2.

Bug: 244543039
Bug: 244373730
Change-Id: I96f732eb044b1fcbdc3d7db51d406580f0142ad4
Signed-off-by: Quentin Perret <qperret@google.com>
[vdonnefort@: Fix for host_stage2_idmap_locked merge conflict]
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:20 +00:00
Quentin Perret
5b2287bddc ANDROID: KVM: arm64: Add a permission fault handler
In preparation for allowing to restrict host permissions at stage-2 for
certain pages, introduce some infrastructure allowing a pKVM module to
register a permission fault handler.

Bug: 244543039
Bug: 244373730
Change-Id: I8035c64969cc0ebb01c8936b1974b3bc103ba84f
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:20 +00:00
Quentin Perret
c0faf0f3e7 ANDROID: KVM: arm64: Introduce PKVM_PAGE_RESTRICTED_PROT
pKVM currently doesn't map pages with reduced permissions in stage2
page-tables, for both host and guests. But in preparation for allowing
that to happen, introduce a new page 'meta-state' that is set whenever a
page has non-default permissions. Introducing it ensures that e.g.
host-to-guest donations or shares will fail for a page that the host has
read-only, as the page will no longer be PKVM_PAGE_OWNED only.

Bug: 244543039
Bug: 244373730
Change-Id: Ib3b20c7edabafb9a305f000a37bbdb4bcc8fdbbb
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:20 +00:00
Quentin Perret
4f73cf46ab ANDROID: KVM: arm64: Expose kvm_flush_dcache_to_poc() in module_ops
Expose kvm_flush_dcache_to_poc() in the module_ops struct to allow CMOs
from pKVM modules.

Bug: 244543039
Bug: 244373730
Change-Id: I91d57a94effd2710d868591c6baf4a5672d149a4
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:20 +00:00
Quentin Perret
4d4c9f9829 ANDROID: KVM: arm64: Expose hyp fixmap helpers in module_ops
Expose the hyp_fixmap helpers in the module_ops struct to allow dynamic
mapping and unmapping of pages from pKVM modules.

Bug: 244543039
Bug: 244373730
Change-Id: I201db6044ed5eb4c2821a64a6b650b931dd2e389
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:20 +00:00
Quentin Perret
29dceaf3d5 ANDROID: KVM: arm64: Expose puts and putx64 in pKVM ABI
Expose the hyp_puts() and hyp_putx64() helpers in the module_ops struct
to allow logging messages on the UART from pKVM modules.

Bug: 244543039
Bug: 244373730
Change-Id: Ica578667297e5a1f94c370603c29482be89982a9
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:20 +00:00
Quentin Perret
b21ae4963d ANDROID: KVM: arm64: Add serial framework for pKVM
Debugging a hypervisor tends to be trickier than normal system code
such as the kernel. The lack of availability of a UART framework is a
significant contributor to that. In order to address this, introduce a
framework allowing to load serial drivers into the hypervisor.

Bug: 244543039
Bug: 244373730
Change-Id: I2e7a1fd9abc9d5aa9d95f1d271a997d54a8fd582
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:20 +00:00
Quentin Perret
c3db83c87f ANDROID: KVM: arm64: Expose __pkvm_create_private_mapping to pKVM modules
pKVM has an internal API allowing to create mappings in the 'private'
range of the hypervisor VA space for which there are no rules
constraining the VA-PA relation (hence comparable to the vmalloc area in
the kernel). This will be a useful API for hypervisor modules, so expose
it in the recently introduced module ops struct.

Bug: 244543039
Bug: 244373730
Change-Id: I2a8f958a02c3c3b9871224b65b00b207820a507a
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:19 +00:00
Quentin Perret
4deb454ebf ANDROID: KVM: arm64: Include .note.gnu.property in .hyp.rodata
Since .hyp.rodata sections of pKVM modules are emitted with SHT_MERGE,
ld.ldd feels free to attempt merging it with other sections.
Unfortunately, the pKVM module linker script doesn't always place them
in output sections, hence causing link failures:

  ld.lld: error: drivers/misc/pkvm-pl011/hyp/kvm_nvhe.tmp.o:(.hyp.rodata):
  offset is outside the section

In practice, ld.ldd only seems to attempt merging .note.gnu.property with
.hyp.rodata. To work around the problem, make sure to explicitely place
the .note.gnu.property in .hyp.rodata from the start, hence preventing
ld.ldd from trying to optimize further.

A preferable solution would be to teach ld.lld that merging pKVM modules
sections is a bad idea, or to make sure the sections are not emitted
with SHT_MERGE to begin with, but we couldn't find an obvious way to make
that happen. This workaround is nothing more than a pratical compromise.

Bug: 244543039
Reported-by: Will Deacon <will@kernel.org>
Suggested-by: Will Deacon <will@kernel.org>
Change-Id: Iae902bdfd21915f552e218515cd77881a95fef2d
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:19 +00:00
Quentin Perret
6e84750015 ANDROID: KVM: arm64: Allow loading modules to the pKVM hypervisor
All nVHE hypervisor code is currently required to be statically linked
into the kernel image. Sadly, scaling pKVM will inevitably require
running _some_ hardware-specific code in the hypervisor due to the
absence of architecture requirements regarding IOMMU implementations or
power management, for example.

In order to address this issue, introduce the ability to load modules
in the pKVM hypervisor at run-time. pKVM modules are expected to be
embedded inside kernel modules, and to be loaded into pKVM when their
kernel counterpart is loaded at EL1. pKVM module loading is defined as a
privileged operation -- all of them must be loaded while the host kernel
is still part of the trusted computing base.

Bug: 244543039
Bug: 244373730
Co-authored-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
Change-Id: If8e5d3ac0a2893c892aff09e5b51d3b8e14693f8
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:19 +00:00
Quentin Perret
32e2601b60 ANDROID: KVM: arm64: Refactor nvhe Makefile
Building and linking hypervisor modules into kernel modules involves
very similar problems to building and linking the KVM nVHE hypervisor
object into the kernel image. In order to re-use the same building
procedure, let's factor out and generalize the nVHE makefile in a way
that allows it to be re-used for module builds.

Bug: 244543039
Bug: 244373730
Change-Id: I89b2630f8cfd3ce5b624300fc277749be4fc9e04
Signed-off-by: Quentin Perret <qperret@google.com>
[vdonnefort@: Merge with updated nvhe/Makefile]
Signed-off-by: Vincent Donnefort <vdonnefort@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:19 +00:00
Quentin Perret
f347aa8c3d ANDROID: KVM: arm64: Make gen-hyprel emit delimiters
The start and end pointers of the hyp relocs section are currently
specified in the vmlinux linker script. In order to ease re-using the
same relocation procedure for hypervisor modules, emit the delimiters
from the generated hyp-reloc.S file directly.

Bug: 244543039
Bug: 244373730
Change-Id: I845af2d40e1dd13301069537c6325f6a6f381ce4
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:19 +00:00
Quentin Perret
a8d5d29e85 ANDROID: KVM: arm64: Move gen-hyprel into a tool directory
In order to allow re-use of the gen-hyprel tool to build hypervisor
modules in the future, move it up to the arm64 tools folder.

Bug: 244543039
Bug: 244373730
Change-Id: I188a2dac1acf4974213499970cc29552807497eb
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:19 +00:00
Vincent Donnefort
8a3e189fbd ANDROID: KVM: arm64: Add mapping removal interface for nVHE hyp
The new pkvm_remove_mappings() allows the caller to unmap a memory range
from the hypervisor.

This is to allow later introduction of in-hypervisor tracing.

Bug: 244543039
Bug: 229972309
Change-Id: I9edc7c2ae55c4b7f5d464d26ce3351b5fd4bf9f3
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:19 +00:00
Quentin Perret
d996511e43 ANDROID: arm64: patching: Add aarch64_addr_write()
The process of applying hypervisor relocations involves patching
addresses in the hypervisor object. In the existing KVM nVHE relocation
procedure, the relocations are applied early enough for write-permission
not to be a problem when touching e.g. the rodata section. But applying
relocations on hypervisor modules embedded in a kernel module proves
more challenging, as the kernel module loader will actively map text and
rodata sections read-only. In order to allow patching in those sections,
let's introduce a new helper function using the text fixmap to
temporarily map the relevant pages writable.

Bug: 244543039
Bug: 244373730
Change-Id: I9dcdade1927e5bc121db87bc950fb70a374c44cd
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:18 +00:00
Quentin Perret
378e0dbd91 ANDROID: arm64: patching: Refactor __aarch64_insn_write()
In order to allow the re-use of __arch64_insn_write() to fixup
relocations in pKVM modules, refactor it into a more abstract
__arch64_text_write() function which also takes a size as parameter.

No functional changes intended.

Bug: 244543039
Bug: 244373730
Change-Id: I60d789d8a4b1271deeb2f6ac6d3b7fc55bdbb465
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:18 +00:00
Mostafa Saleh
a9a4f4f958 ANDROID: KVM: arm64: Use correct pkvm owners type
Update host_stage2_set_owner_locked() and caller functions
to use enum pkvm_component_id instead of u8, as IDs are not
defined as const variables anymore. and all other functions
and structs use this enum consistently.

Use the correct max number of IDs as it is not calculated from
invalid PTEs anymore.

Test: MicrodroidHostTestCases
Bug: 255731794
Change-Id: Ib45dc34ff9ff20716adb79920f32875034587343
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:18 +00:00
Mostafa Saleh
e3262b342c ANDROID: KVM: arm64: s2mpu: S2MPU V9 code
Add S2MPU V9 code with current page table ops and version ops.
Most SMPT_* macros are now function of protection bits
To keep logic modification minimal and avoid duplicate code
SMPT and FMPT function are kept the same and the values that
changed between S2MPU versions are used as variables instead of
macros

Bug: 255731794
Change-Id: I2a1b8bab630032d8c923c23e96e1182ce5f734ff
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:18 +00:00
Mostafa Saleh
61ca64906f ANDROID: KVM: arm64: s2mpu: Add MMIO and defines for V9 S2MPU
No functional change
This commit defines the new registers, fields and constants of V9 S2MPU

Bug: 255731794
Change-Id: Ic2e0f276d83604efd7534f74de862df05d4d5696
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:18 +00:00
Mostafa Saleh
6b6a752217 ANDROID: KVM: arm64: s2mpu: rename versions to match major arch
No functional change

Rename versions to match MAJOR_ARCH_VER[31:28] in user manual
0x11000000: is v1
0x20000000: is v2

Bug: 255731794
Change-Id: I30762293a3fab8194b616b4520b047baf3a94790
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:18 +00:00
Mostafa Saleh
7868d051b0 ANDROID: KVM: arm64: s2mpu: Abstract register initialization with version_ops
As initialization sequence differ between versions, add a struct
version_ops to abstract init operations

This is just a clean before pushing new S2MPU code.
No functional changes.

Bug: 255731794
Change-Id: I67cc2fb351981280e23860d6f83bdc5632f3abc1
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:18 +00:00
Mostafa Saleh
fd3eeecd11 ANDROID: KVM: arm64: s2mpu: Pass driver version during init
Add new a function to explicitly init S2MPU driver from EL1
pkvm_iommu_s2mpu_init. Instead of being called implicitly from
register function, it now should be called from EL1 driver register
function, and it should pass the expected version of S2MPUs.

EL2 driver will work only on a set of compatible S2MPUs at run time
matching the version at init.

Bug: 255731794
Change-Id: I01a39aa357e368b636fe3c9347651e92f3c62fc2
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:17 +00:00
Mostafa Saleh
781efc8f25 ANDROID: KVM: arm64: s2mpu: Add SMPT and MPT functions to pgtable abstraction
No functional change
Move SMPT and MPT functions to io-pgtable-s2mpu.c as they will need to
change later when new version of S2MPU is added

Bug: 255731794
Change-Id: Ie890bd4e085c1e23a0d033147f955ba8789b8a28
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:17 +00:00
Mostafa Saleh
ace1a8e103 ANDROID: KVM: arm64: s2mpu: Abstract page table ops
Create a seprate file where S2MPU page table operations are allocated
based on configuration
This will be helpful when supporting S2MPU versions that have
different SMPT and FMPT formats

Bug: 255731794
Change-Id: I0b80a329d4357511baf6847baf84dc4644bb835f
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:17 +00:00
Mostafa Saleh
5747f9215c ANDROID: KVM: arm64: iommu: Support dynamic driver registration in IOMMU layer
This will allow IOMMU drivers to be loaded as EL2 modules
To achieve that, EL2 module init should register and init
the driver using pkvm_iommu_driver_init.

To avoid having to hardcode S2MPU drivers in IOMMU EL2 code,
pkvm_iommu_driver_init will have a ptr to driver struct instead of
id.

Id is removed from pkvm_iommu_driver and the address of the struct
is treated as the id of the driver, the kernel is expected to pass
linear mapping address of the driver and the hypervisor is expected
to use hyp address.

Bug: 249481474
Change-Id: I49db5b5d88a691d66fa2d5302198785da94d70d2
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:17 +00:00
Will Deacon
fe3157f328 ANDROID: KVM: arm64: Use 32-bit function ID for PSCI MEM_PROTECT call
The PSCI specification defines only a 32-bit function ID for the
MEM_PROTECT call used to protect against cold reboot attacks.

Fix the pKVM hypervisor invocation of MEM_PROTECT to use the 32-bit
function ID instead of the unallocated 64-bit flavour.

[ qperret: dropped the change to include/uapi/linux/psci.h as the 32 bit
  variant of the call have been introduced upstream by 3137f2e600
  ("firmware/psci: Add debugfs support to ease debugging") ]

Bug: 260316363
Signed-off-by: Will Deacon <willdeacon@google.com>
Change-Id: I675a57419064f7f006960ca5370e9dc2d5279a90
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:17 +00:00
Keir Fraser
8260bd357c Revert "ANDROID: virtio_balloon: New module parameter "pkvm""
This reverts commit 87bcd3edf3ee7b9d5e76a8090b3f05ea1535026d.

Reason for revert: Memory reclaim capability will be checked by the
host before configuring the virtio_balloon device.

Bug: 240239989
Change-Id: I03e7c39ec6d671babeace4040138b416c7e201cf
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:17 +00:00
David Brazdil
5584b944ab ANDROID: KVM: arm64: s2mpu: Fix SYNC latency regression
SysMMU_SYNCs provide an invalidation-complete signal to the S2MPU
driver but the latency can be quite high. Improve this by waiting for
all the SYNCs in parallel - separate the initiation of invalidation
barrier from waiting for completion. This way we initiate invalidation
on all SYNCs first, then wait for all of them to complete.

The previously introduced exponential-backoff only kicks in if the
SYNC_COMP_COMPLETE bit is not set after the parallel invalidation.

Bug: 249161451
Signed-off-by: David Brazdil <dbrazdil@google.com>
Change-Id: I9d544bc65f8633d376c7ccd65ea23195ca432964
(cherry picked from commit 1da102d4e3d54285f1d2071a7bee79fbb4c9ce71)
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:17 +00:00
David Brazdil
f21a6c3cb9 ANDROID: KVM: arm64: iommu: Add host_stage2_idmap_complete
Add a new callback to pkvm_iommu_ops called after
host_stage2_idmap_apply on all IOMMU devices. This allows the drivers to
complete operations like invalidation in two stages.

Bug: 249161451
Signed-off-by: David Brazdil <dbrazdil@google.com>
Change-Id: I9c077fd2b18ce54ad67eb34ef16bc94428797419
(cherry picked from commit ce39549d9283e32cee6a476e4603e7b586957ecf)
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:16 +00:00
Quentin Perret
4dcd45d0cb ANDROID: KVM: arm64: Don't update IOMMUs unnecessarily
When handling host stage-2 faults the hypervisor currently updates the
CPU _and_ IOMMUs page-tables. However, since we currently proactively
map accessible PA ranges into IOMMUs, updating them during stage-2
faults is unnecessary -- it only needs to be done during ownership
transitions. Optimize this by skipping the IOMMU updates from the host
memory abort path, which also reduces contention on the host stage-2
lock during boot and saves up to 1.1 sec of boot time on Pixel 6.

Bug: 232879742
Change-Id: Icb6f1b2d10de8cc2f976bbc94b9b79ad7aaa6135
Signed-off-by: Quentin Perret <qperret@google.com>
(cherry picked from commit 20c6e1ba55403fad0e7f039cc5d7e6f8c0fffa50)
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:16 +00:00