Commit Graph

1143001 Commits

Author SHA1 Message Date
Will Deacon
cb335ce6d0 FROMLIST: KVM: arm64: Handle FFA_MEM_LEND calls from the host
Handle FFA_MEM_LEND calls from the host by treating them identically to
FFA_MEM_SHARE calls for the purposes of the host stage-2 page-table, but
forwarding on the original request to EL3.

Bug: 254811097
Change-Id: I8f53bca6f0865fabd9938eefd8427fa0e78016ed
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-12-qperret@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:07 +00:00
Will Deacon
2e47f0ecdc FROMLIST: KVM: arm64: Handle FFA_MEM_RECLAIM calls from the host
Intecept FFA_MEM_RECLAIM calls from the host and transition the host
stage-2 page-table entries from the SHARED_OWNED state back to the OWNED
state once EL3 has confirmed that the secure mapping has been reclaimed.

Bug: 254811097
Change-Id: I58365e1b3fafa47f290a292fe57f6d2ed7f9091b
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-11-qperret@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:07 +00:00
Will Deacon
8453f39a4a FROMLIST: KVM: arm64: Handle FFA_MEM_SHARE calls from the host
Intercept FFA_MEM_SHARE/FFA_FN64_MEM_SHARE calls from the host and
transition the host stage-2 page-table entries from the OWNED state to
the SHARED_OWNED state prior to forwarding the call onto EL3.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: Ic75a943c67e6cb96794c250dccf2a59362bf857e
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-10-qperret@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:06 +00:00
Will Deacon
3399bf007b BACKPORT: FROMLIST: KVM: arm64: Add FF-A helpers to share/unshare memory with secure world
Extend pKVM's memory protection code so that we can update the host's
stage-2 page-table to track pages shared with secure world by the host
using FF-A and prevent those pages from being mapped into a guest.

[ qperret: BACKPORT due to context conflicts in mem_protect.c caused by
  the presense of guest-related memory transition in the android kernel
  (host_donate_guest and friends) ]

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: Ib4d404cd1d4fa11d7bf8c1d0b8ec00838a8038a0
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-9-qperret@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:06 +00:00
Will Deacon
e7e5e9120f FROMLIST: KVM: arm64: Handle FFA_RXTX_MAP and FFA_RXTX_UNMAP calls from the host
Handle FFA_RXTX_MAP and FFA_RXTX_UNMAP calls from the host by sharing
the host's mailbox memory with the hypervisor and establishing a
separate pair of mailboxes between the hypervisor and the SPMD at EL3.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: Ib5fa89e9b01aa20f7c1b5b41df79d66e98d07f55
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-8-qperret@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:06 +00:00
Will Deacon
508713769a FROMLIST: KVM: arm64: Allocate pages for hypervisor FF-A mailboxes
The FF-A proxy code needs to allocate its own buffer pair for
communication with EL3 and for forwarding calls from the host at EL1.

Reserve a couple of pages for this purpose and use them to initialise
the hypervisor's FF-A buffer structure.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: Id72cd7f59be20eb6d1faa6f1c5e64ecc8debf929
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-7-qperret@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:06 +00:00
Fuad Tabba
a805114bc5 FROMLIST: KVM: arm64: Handle FFA_FEATURES call from the host
Filter out advertising unsupported features, and only advertise
features and properties that are supported by the hypervisor proxy.

Bug: 254811097
Change-Id: I071766d6d241f4bdd00b8f80e6b237c184a1e59a
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-6-qperret@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:06 +00:00
Will Deacon
376236b858 BACKPORT: FROMLIST: KVM: arm64: Probe FF-A version and host/hyp partition ID during init
Probe FF-A during pKVM initialisation so that we can detect any
inconsistencies in the version or partition ID early on.

[ qperret: BACKPORT due to trivial conflict with header includes in
  setup.c ]

Bug: 254811097
Change-Id: I7def4c2c497017ba86621bc98298bc65ffdeefae
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-5-qperret@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:06 +00:00
Will Deacon
1e8c7d7216 FROMLIST: KVM: arm64: Block unsafe FF-A calls from the host
When KVM is initialised in protected mode, we must take care to filter
certain FFA calls from the host kernel so that the integrity of guest
and hypervisor memory is maintained and is not made available to the
secure world.

As a first step, intercept and block all memory-related FF-A SMC calls
from the host to EL3. This puts the framework in place for handling them
properly.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: I5279bce56956c590862a68e8c4803dd2205e3f81
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-4-qperret@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:06 +00:00
Will Deacon
dba12d9059 FROMLIST: firmware: arm_ffa: Move comment before the field it is documenting
This is consistent with the other comments in the struct.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: I10e9014a0d505fe5e132fb1cd6105b95a3f5f2bf
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-3-qperret@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:05 +00:00
Will Deacon
40e897b246 FROMLIST: firmware: arm_ffa: Move constants to header file
FF-A function IDs and error codes will be needed in the hypervisor too,
so move to them to the header file where they can be shared. Rename the
version constants with an "FFA_" prefix so that they are less likely
to clash with other code in the tree.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: I00ed487279fdfb61ea34ae99140c6fac8ee89187
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-2-qperret@google.com
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:05 +00:00
Quentin Perret
3c8afbbfa4 ANDROID: KVM: arm64: Issue CMOs when tearing down shadow pages
On the guest teardown path, pKVM will zero the pages used to back the
guest shadow data structures before returning them to the host as they
may contain secrets (e.g. in the vCPU registers). However, the zeroing
is done using a cacheable alias, and CMOs are missing, hence giving the
host a potential opportunity to read the original content of the shadow
structs from memory.

Fix this by issuing CMOs after zeroing the pages.

[ qperret@: moved the CMOs to __unmap_donated_memory() to cover all
  callers, including the __pkvm_init_vm() error path ]

Bug: 259551298
Change-Id: Id696d47d16e4c3fd870cb70b792eeb7f2282fc78
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:05 +00:00
Will Deacon
ffe9d28274 ANDROID: KVM: arm64: Use PSCI MEM_PROTECT to zap guest pages on reset
If a malicious/compromised host issues a PSCI SYSTEM_RESET call in the
presence of guest-owned pages then the contents of those pages may be
susceptible to cold-reboot attacks.

Use the PSCI MEM_PROTECT call to ensure that volatile memory is wiped by
the firmware if a SYSTEM_RESET occurs while unpoisoned guest pages exist
in the system. Since this call does not offer protection for a "warm"
reset initiated by SYSTEM_RESET2, detect this case in the PSCI relay and
repaint the call to a standard SYSTEM_RESET instead.

Signed-off-by: Will Deacon <will@kernel.org>
Bug: 254821051
Change-Id: I5c3dd93bc83ebcd0b6cea2ec734f6e3a77f0064e
Signed-off-by: Will Deacon <willdeacon@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:05 +00:00
Will Deacon
22c8a338c7 ANDROID: KVM: arm64: Check IPA range for pvmfw during guest donation
When donating pages to the guest, we only check the first IPA in the
range against the pvmfw loading range. Although this is fine for the
page-at-a-time faulting path, it doesn't fit with the rest of the mem
protection logic, which deals with the possibility of an arbitrarily
sized contiguous address range.

Rework the logic so that we check the whole IPA range during guest
donation and trigger the pvmfw loading path if any of the pages
intersect with the pvmfw region.

Signed-off-by: Will Deacon <will@kernel.org>
Bug: 254819795
Change-Id: I6fef9f1898e65a95cab7f6a0ffa8aa422a8d5a91
Signed-off-by: Will Deacon <willdeacon@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:05 +00:00
Will Deacon
677980a696 ANDROID: KVM: arm64: Use fixmap when poisoning pvmfw pages
When poisoning the pvmfw pages during system reset at EL2, ensure that we
use a writable fixmap mapping rather than the persistent read-only mapping
of the region.

Signed-off-by: Will Deacon <will@kernel.org>
Bug: 254819795
Change-Id: I4c8be092d3c822695afd7d03d0d64163664a9f64
Signed-off-by: Will Deacon <willdeacon@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:05 +00:00
Will Deacon
a542a1202a ANDROID: KVM: arm64: Rename pkvm_clear_pvmfw_pages()
pkvm_clear_pvmfw_pages() is used to poison the pvmfw pages during reset,
so rename it to pkvm_poison_pvmfw_pages() instead.

Signed-off-by: Will Deacon <will@kernel.org>
Bug: 254819795
Change-Id: Ie5b9c90f0707fa81d9099425cff35383bfb0d009
Signed-off-by: Will Deacon <willdeacon@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:05 +00:00
Will Deacon
8b744c24f0 ANDROID: KVM: arm64: Rename hyp_zero_page() and make available as helper
hyp_zero_page() is used for poisoning memory, so rename it to
hyp_poison_page() to avoid confusing with the concept of a "zero page"
and make it available outside of mem_protect.c as it will be used to
poison the pvmfw memory in a subsequent patch.

Signed-off-by: Will Deacon <will@kernel.org>
Bug: 254819795
Change-Id: Ia4aec46437db3ffe466ae09bd180392fa06c0b46
Signed-off-by: Will Deacon <willdeacon@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:04 +00:00
Will Deacon
8f9c7f7acb ANDROID: KVM: arm64: Don't check for hyp_fixmap_map() returning NULL
hyp_fixmap_map() never returns NULL, so remove the redundant checks for
it and simplify the error handling in the callers.

Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Will Deacon <willdeacon@google.com>
Bug: 254819795
Change-Id: Ie73a97cc3d9bded3750abe6e243003827393ee5e
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:04 +00:00
Keir Fraser
6fe4c366af ANDROID: virtio_balloon: Do not clear VIRTIO_F_ACCESS_PLATFORM
This essentially reverts commit e41b135550
"virtio_balloon: disable VIOMMU support".

Although the virtio_balloon driver does not translate through a
VIOMMU (or bounce buffer) the pages that it sends to the device,
it *does* need to perform these translations on the virtio rings
themselves.

This fixes virtio_balloon initialisation inside a PKVM/ARM64
protected virtual machine.

Bug: 240239989
Change-Id: I2a84eec870fd638223b231e5c4d1c27216dc40a2
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:04 +00:00
Keir Fraser
a12648bd1e ANDROID: virtio_balloon: New module parameter "pkvm"
This specifies that the driver is running on a PKVM hypervisor
and must use the memrelinquish service to cooperatively release
memory. If this service is unavailable, virtio_balloon cannot be
used.

Bug: 240239989
Change-Id: I8800c4435d8fae9df6f1ab108cc61c8f93020773
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:04 +00:00
Keir Fraser
e210ad7cd9 ANDROID: KVM: arm64: Introduce kvm_has_memrelinquish_services
Used to determine whether memrelinquish services have been
initialised.

Bug: 240239989
Change-Id: I81dd23d8122ea54924d52b3fdc1fc4a8cdb28ea5
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:04 +00:00
Vincent Donnefort
bf76ea0a21 ANDROID: KVM: arm64: Flush nVHE hyp_vcpu memcache
When using nVHE in protected mode, the host donates pages through an arch
specific memcache the hyp can then pours in its local vcpu copy. The latter
should be flushed on VM teardown.

Bug: 237506543
Change-Id: Ic37d794ac33e9f844fa6ae1b4943febcdad5b033
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:04 +00:00
Keir Fraser
4a477ac0a9 ANDROID: KVM: arm64: Avoid unnecessary unmap walk in MEM_RELINQUISH hypercall
If the mapping is determined to be not present in an earlier walk,
attempting the unmap is pointless.

Bug: 259217067
Change-Id: I6fd939556b80d7a9a0731cab36166a652f7a7c6d
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:04 +00:00
Keir Fraser
15a4df5ad4 ANDROID: KVM: arm64: Strictly check page type in MEM_RELINQUISH hypercall
The VM should only relinquish "normal" pages. For a protected VM, this
means PAGE_OWNED; For a normal VM, this means PAGE_SHARED_BORROWED. All
other page types are rejected and failure is reported to the caller.

Bug: 259217067
Change-Id: Icff3474dc2c975a6c5befe546c5521a05b3bd575
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:03 +00:00
Keir Fraser
cafd6d7893 ANDROID: KVM: Include prototype for page_relinquish before definition
Fixes build failure on -Werror=missing-prototypes.

At the same time, make the header file more resilient to ordering by
declaring 'struct page'.

Bug: 240239989
Change-Id: I84d069bde5ff03d1afa55d25c01448b0d43042da
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:03 +00:00
Fuad Tabba
eb0f5f7486 ANDROID: arm64: ioremap/iounmap use stage-2 granule size
When MMIO guard is queried, it advertises the guard granule size
it uses. Use that value.

Fixes: arm64: Implement ioremap/iounmap hooks calling into KVM's MMIO guard

Bug: 251432016
Change-Id: Iff4dcb6229bf89aef832a29a98fecc041a1aec1b
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:03 +00:00
Fuad Tabba
9720e4d374 ANDROID: arm64: Check if pfn is valid for all ioremap loop iterations
The size could cover pfns that are valid.

Fixes:
arm64: Implement ioremap/iounmap hooks calling into KVM's MMIO guard

Bug: 251432016
Change-Id: Ie3b678d40d629ed610b0ee484b5007cb64437435
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:03 +00:00
Sebastian Ene
437fc98ccb ANDROID: arm64: Auto-enroll MMIO guard on protected vms
Set the MMIO guard flag for protected vms prior to entering the guest
for the first time.

Bug: 216798684
Change-Id: I1448102ae85176d495ae7f8d6d20de4092049f0d
Signed-off-by: Sebastian Ene <sebastianene@google.com>
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:03 +00:00
Marc Zyngier
0dbb5f89f5 ANDROID: KVM: arm64: Add some documentation for the MMIO guard feature
Document the hypercalls user for the MMIO guard infrastructure.

Bug: 209580772
Change-Id: I927bcd6c5e3ef932265d817288ff2b46b0e0db66
Signed-off-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:03 +00:00
Marc Zyngier
b0af5b4925 ANDROID: KVM: arm64: Plumb MMIO checking into the fault handling
Plumb the MMIO checking code into the MMIO fault handling code.
Any fault hitting outside of an MMIO region will now report
an invalid syndrome, and won't leak any data from the guest.

Bug: 209580772
Change-Id: I68bef2d0211a804aa1e598aeaa0c85dc4098f61e
Signed-off-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:03 +00:00
Marc Zyngier
1ac4d8a25d ANDROID: KVM: arm64: pkvm: Wire MMIO guard hypercalls
Plumb in the hypercall interface to allow a guest to discover,
enroll, map and unmap MMIO regions.

Bug: 209580772
Change-Id: I0390456ffde8ceca351d3d8e82fd1dddeb747fac
Signed-off-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
[tabba@:
- use the new pkvm_hyp_* infrastructure
- move pkvm_refill_memcache() up in file to expose it to
handle_pvm_entry_hvc64()
- include asm/stage2_pgtable.h in hypercalls.c for
topup_hyp_memcache()
- fix pkvm_install_ioguard_page() retval to u64, reported in
b/253586500 and fixed in a separate patch before
- fix smccc to return success, reported in b/251426790 and fixed
in a separate patch before
]
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:02 +00:00
Marc Zyngier
0e40cc466b ANDROID: KVM: arm64: pkvm: Add MMIO guard infrastructure
Introduce the infrastructure required to identify an IPA region
that is expected to be used as an MMIO window.

This include mapping, unmapping and checking the regions. Nothing
calls into it yet, so no expected functional change.

Bug: 209580772
Change-Id: I227eaa28b98e067e3daae4f9e1071eb37a6761cc
Signed-off-by: Marc Zyngier <maz@kernel.org>
[tabba@: use the new pkvm_hyp_* infrastructure, and remove
redundant reassignment in __pkvm_remove_ioguard_page()]
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:02 +00:00
Marc Zyngier
e439ca0810 ANDROID: KVM: arm64: Introduce KVM_ARCH_FLAG_MMIO_GUARD flag
Add a per-VM flag indicating that the guest has bought into the
MMIO guard enforcement framework.

Bug: 209580772
Change-Id: If60b2b38a419a9f44ebe9029f55dd016fd2444b5
Signed-off-by: Marc Zyngier <maz@kernel.org>
[tabba@: had to assign it a new number since there are existing
flags now]
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:02 +00:00
Marc Zyngier
d8bf88fa79 ANDROID: KVM: arm64: Expose topup_hyp_memcache() to the rest of KVM
In order to simplify the implementation of an EL2-only version of
MMIO guard, expose topup_hyp_memcache() and simplify its usage
by only requiring a vcpu.

Bug: 209580772
Change-Id: I4f54c57a9693cf7a3450f99fedc15ae32af09a31
Signed-off-by: Marc Zyngier <maz@kernel.org>
[tabba@: original patch did the same for free_hyp_memcache(), but
it's already exposed]
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:02 +00:00
Marc Zyngier
2b8c3bb99b ANDROID: KVM: arm64: Define MMIO guard hypercalls
Define the handful of hypercalls that MMIO guard will require.

Bug: 209580772
Change-Id: Iac312b2327c31a1532fdb38e8fa8066291d9f611
Signed-off-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:02 +00:00
Fuad Tabba
7c4000b1ea ANDROID: KVM: arm64: FAR_EL2 mask as a define
Create a macro definition for the FAR_EL2 mask and use it instead
of a hard-coded value, and put it in a share header to be used by
hyp.

No functional change intended.

Bug: 209580772
Change-Id: Ib83932d670cba6bf8f1ed45d2c0e1ed34331d98d
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:02 +00:00
Marc Zyngier
2e5989b503 ANDROID: KVM: arm64: Turn kvm_pgtable_stage2_set_owner into kvm_pgtable_stage2_annotate
kvm_pgtable_stage2_set_owner() could be generalised into a way
to store up to 63 bits in the page tables, as long as we don't
set bit 0.

Let's just do that.

Bug: 209580772
Change-Id: I4e42d149b457870c35a5ae0f77e14c95dee16b4d
Signed-off-by: Marc Zyngier <maz@kernel.org>
[tabba@: Fix conflict in host_stage2_set_owner_locked()]
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:02 +00:00
Keir Fraser
1f6276e0c5 ANDROID: memory relinquish: Fix build dependencies
Memory relinquish interface is used by both memory ballooning and
by page reporting. It must be built if either is specified.

Bug: 258944680
Change-Id: I3b949dadbfc4a2b17dba1809a46f0a7386e70ebf
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:01 +00:00
Fuad Tabba
0a1f3a1f7b ANDROID: KVM: arm64: Monitor Debug support for non-protected guests
Add monitor debug support for non-protected guests in protected
mode.

Save and restore the monitor debug state when running a
non-protected guest, and propagate the monitor debug
configuration of non-protected vcpus from the host.

This patch assumes that the hyp vcpu debug iflags are kept in
sync with the host.

Bug: 228011917
Signed-off-by: Fuad Tabba <tabba@google.com>
Change-Id: Ie525693a6a6f236e388b16a1af297403e729057f
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:01 +00:00
Fuad Tabba
b34bc46188 ANDROID: KVM: arm64: Factor out code for saving/restoring guest debug regs
This code will be reused when supporting debug for non-protected
VMs in protected mode.

No functional change intended

Bug: 228011917
Signed-off-by: Fuad Tabba <tabba@google.com>
Change-Id: If05dc8fdb3fff8e811f06cf5050d3eaf0ce67116
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:01 +00:00
Fuad Tabba
f4b25ab106 ANDROID: KVM: arm64: Flush the vcpu iflags for non-protected VMs
The iflags are meant as input flags to the hypervisor, and will
be used in future patches by calls to functions that sync debug
and pmu state. Ensure that the hyp_vcpu copy is up-to-date with
the host's on entry.

Bug: 228011917
Signed-off-by: Fuad Tabba <tabba@google.com>
Change-Id: Id04d65ee084c3745ddc283ff5e30348511a4a1d2
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:01 +00:00
Keir Fraser
aa2a4936ec ANDROID: virtio_balloon: Do not translate reported pages through DMA API
The free-page reporting and hinting queues do not pass arrays of page
addresses (like the basic inflate queue) but instead pass the free page
ranges as buffers. This does not work well with DMA API: The host wants
to know the GPA, not an IOVA.

For these two virtqueues, disable DMA API and pass through buffers untranslated.

Bug: 240239989
Change-Id: I2d13a8b7e8f6775819de7fe96f4579afa08b1300
Signed-off-by: Keir Fraser <keirf@google.com>
[ qperret@: Fixed minor context conflict in virtio.h ]
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:01 +00:00
Keir Fraser
e95813e9da ANDROID: KVM: arm64: balloon: Notify hyp before reporting free pages to host
When running as a protected VM, the hypervisor isolates the VM's
memory pages from the host. Returning ownership of a VM page therefore
requires hypervisor involvement, and acknowledgement from the
protected VM that it is voluntarily cooperating.

To this end, notify pages via the new relinquish hypercall when they
are being reported to the host as free and available for temporary
reclaim.

Bug: 240239989
Change-Id: I8718e468be63c3aacb2f79ff141fbcedd6d19b56
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:01 +00:00
Keir Fraser
23cfd30bb3 ANDROID: KVM: arm64: memory balloon: Notify hyp when ballooning
When running as a protected VM, the hypervisor isolates the VM's
memory pages from the host. Returning ownership of a VM page
therefore requires hypervisor involvement, and acknowledgement from
the protected VM that it is voluntarily cooperating.

To this end, notify pages via the new relinquish hypercall when they
are entered into the memory balloon.

Bug: 240239989
Change-Id: Ic89b45312a7478ddff081a934d99e693eded92dc
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:00 +00:00
Keir Fraser
102dcecb67 ANDROID: Define mem_relinquish interface for releasing memory to a hypervisor.
On PKVM/ARM64 this uses the ARM SMCCC relinquish hypercall when available.

Bug: 240239989
Change-Id: Ifa85b641a48f348a2364cf8c6b06b6417f1eeedb
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:00 +00:00
Keir Fraser
4fc49028bb ANDROID: KVM: arm64: Implement MEM_RELINQUISH SMCCC hypercall
This allows a VM running on PKVM to notify the hypervisor (and host)
that it is returning pages to host ownership.

Bug: 240239989
Change-Id: I4644736db04afacd7da4c6f465130c73c2e44b93
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:00 +00:00
Quentin Perret
f3f6e13358 ANDROID: KVM: arm64: Turn llist of pinned pages into an rb-tree
Indexed by IPA, so we can efficiently lookup.

Bug: 240239989
Change-Id: I10ad6ad5a7a6aa34a0814ed334b20f4ae42ca830
Signed-off-by: Quentin Perret <qperret@google.com>
Signed-off-by: Keir Fraser <keirf@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:00 +00:00
Marc Zyngier
71fb5851c5 FROMLIST: KVM: arm64: pkvm: Fixup boot mode to reflect that the kernel resumes from EL1
The kernel has an awfully complicated boot sequence in order to cope
with the various EL2 configurations, including those that "enhanced"
the architecture. We go from EL2 to EL1, then back to EL2, staying
at EL2 if VHE capable and otherwise go back to EL1.

Here's a paracetamol tablet for you.

The cpu_resume path follows the same logic, because coming up with
two versions of a square wheel is hard.

However, things aren't this straightforward with pKVM, as the host
resume path is always proxied by the hypervisor, which means that
the kernel is always entered at EL1. Which contradicts what the
__boot_cpu_mode[] array contains (it obviously says EL2).

This thus triggers a HVC call from EL1 to EL2 in a vain attempt
to upgrade from EL1 to EL2 VHE, which we are, funnily enough,
reluctant to grant to the host kernel. This is also completely
unexpected, and puzzles your average EL2 hacker.

Address it by fixing up the boot mode at the point the host gets
deprivileged. is_hyp_mode_available() and co already have a static
branch to deal with this, making it pretty safe.

Cc: <stable@vger.kernel.org> # 5.15+
Reported-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Tested-by: Vincent Donnefort <vdonnefort@google.com>
Bug: 258157858
Link: https://lore.kernel.org/all/20221108100138.3887862-1-vdonnefort@google.com/
Change-Id: I4a2269402ececa0ec47cab88343c3c623b4b2e3d
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:00 +00:00
David Brazdil
223a3844b7 ANDROID: KVM: arm64: Add .hyp.data section
The hypervisor has not needed its own .data section because all globals
were either .rodata or .bss. Linked lists are initialized with the head
pointing to itself. To avoid having to work around this by initializing
at runtime, add a .hyp.data section.

Bug: 190463801
Signed-off-by: David Brazdil <dbrazdil@google.com>
Change-Id: I7a56dc4c93e05bbef53c66837164d17c6103b6b8
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:00 +00:00
Will Deacon
dd0b2eb0d5 ANDROID: KVM: arm64: relay entropy requests from protected guests directly to secure
As pKVM does not trust the host, it should not be involved in the
handling of, or be able to observe the response to entropy requests
issues by protected guests.

When an SMC-based implementation of the ARM SMCCC TRNG interface is
present, pass any HVC-based requests directly on to the secure firmware.

Co-developed-by: Ard Biesheuvel <ardb@google.com>
Signed-off-by: Ard Biesheuvel <ardb@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Bug: 209580772
Change-Id: Ica492ce49fd059a62ecc31bb7ac13c9adb773a08
Signed-off-by: Will Deacon <willdeacon@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
2022-12-15 16:13:00 +00:00