Skip to content

Commit 29d24e3

Browse files
committed
Merge tag 'kvm-ppc-cve-4.15-2' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
Add PPC KVM ioctl to report vulnerability and workaround status to userspace.
2 parents 37b9595 + 3214d01 commit 29d24e3

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed

Documentation/virtual/kvm/api.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3403,6 +3403,52 @@ invalid, if invalid pages are written to (e.g. after the end of memory)
34033403
or if no page table is present for the addresses (e.g. when using
34043404
hugepages).
34053405

3406+
4.108 KVM_PPC_GET_CPU_CHAR
3407+
3408+
Capability: KVM_CAP_PPC_GET_CPU_CHAR
3409+
Architectures: powerpc
3410+
Type: vm ioctl
3411+
Parameters: struct kvm_ppc_cpu_char (out)
3412+
Returns: 0 on successful completion
3413+
-EFAULT if struct kvm_ppc_cpu_char cannot be written
3414+
3415+
This ioctl gives userspace information about certain characteristics
3416+
of the CPU relating to speculative execution of instructions and
3417+
possible information leakage resulting from speculative execution (see
3418+
CVE-2017-5715, CVE-2017-5753 and CVE-2017-5754). The information is
3419+
returned in struct kvm_ppc_cpu_char, which looks like this:
3420+
3421+
struct kvm_ppc_cpu_char {
3422+
__u64 character; /* characteristics of the CPU */
3423+
__u64 behaviour; /* recommended software behaviour */
3424+
__u64 character_mask; /* valid bits in character */
3425+
__u64 behaviour_mask; /* valid bits in behaviour */
3426+
};
3427+
3428+
For extensibility, the character_mask and behaviour_mask fields
3429+
indicate which bits of character and behaviour have been filled in by
3430+
the kernel. If the set of defined bits is extended in future then
3431+
userspace will be able to tell whether it is running on a kernel that
3432+
knows about the new bits.
3433+
3434+
The character field describes attributes of the CPU which can help
3435+
with preventing inadvertent information disclosure - specifically,
3436+
whether there is an instruction to flash-invalidate the L1 data cache
3437+
(ori 30,30,0 or mtspr SPRN_TRIG2,rN), whether the L1 data cache is set
3438+
to a mode where entries can only be used by the thread that created
3439+
them, whether the bcctr[l] instruction prevents speculation, and
3440+
whether a speculation barrier instruction (ori 31,31,0) is provided.
3441+
3442+
The behaviour field describes actions that software should take to
3443+
prevent inadvertent information disclosure, and thus describes which
3444+
vulnerabilities the hardware is subject to; specifically whether the
3445+
L1 data cache should be flushed when returning to user mode from the
3446+
kernel, and whether a speculation barrier should be placed between an
3447+
array bounds check and the array access.
3448+
3449+
These fields use the same bit definitions as the new
3450+
H_GET_CPU_CHARACTERISTICS hypercall.
3451+
34063452
5. The kvm_run structure
34073453
------------------------
34083454

arch/powerpc/include/uapi/asm/kvm.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,31 @@ struct kvm_ppc_rmmu_info {
443443
__u32 ap_encodings[8];
444444
};
445445

446+
/* For KVM_PPC_GET_CPU_CHAR */
447+
struct kvm_ppc_cpu_char {
448+
__u64 character; /* characteristics of the CPU */
449+
__u64 behaviour; /* recommended software behaviour */
450+
__u64 character_mask; /* valid bits in character */
451+
__u64 behaviour_mask; /* valid bits in behaviour */
452+
};
453+
454+
/*
455+
* Values for character and character_mask.
456+
* These are identical to the values used by H_GET_CPU_CHARACTERISTICS.
457+
*/
458+
#define KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 (1ULL << 63)
459+
#define KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED (1ULL << 62)
460+
#define KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 (1ULL << 61)
461+
#define KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 (1ULL << 60)
462+
#define KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV (1ULL << 59)
463+
#define KVM_PPC_CPU_CHAR_BR_HINT_HONOURED (1ULL << 58)
464+
#define KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF (1ULL << 57)
465+
#define KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS (1ULL << 56)
466+
467+
#define KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY (1ULL << 63)
468+
#define KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR (1ULL << 62)
469+
#define KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR (1ULL << 61)
470+
446471
/* Per-vcpu XICS interrupt controller state */
447472
#define KVM_REG_PPC_ICP_STATE (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x8c)
448473

arch/powerpc/kvm/powerpc.c

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
#include <asm/iommu.h>
4040
#include <asm/switch_to.h>
4141
#include <asm/xive.h>
42+
#ifdef CONFIG_PPC_PSERIES
43+
#include <asm/hvcall.h>
44+
#include <asm/plpar_wrappers.h>
45+
#endif
4246

4347
#include "timing.h"
4448
#include "irq.h"
@@ -548,6 +552,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
548552
#ifdef CONFIG_KVM_XICS
549553
case KVM_CAP_IRQ_XICS:
550554
#endif
555+
case KVM_CAP_PPC_GET_CPU_CHAR:
551556
r = 1;
552557
break;
553558

@@ -1759,6 +1764,124 @@ static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
17591764
return r;
17601765
}
17611766

1767+
#ifdef CONFIG_PPC_BOOK3S_64
1768+
/*
1769+
* These functions check whether the underlying hardware is safe
1770+
* against attacks based on observing the effects of speculatively
1771+
* executed instructions, and whether it supplies instructions for
1772+
* use in workarounds. The information comes from firmware, either
1773+
* via the device tree on powernv platforms or from an hcall on
1774+
* pseries platforms.
1775+
*/
1776+
#ifdef CONFIG_PPC_PSERIES
1777+
static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
1778+
{
1779+
struct h_cpu_char_result c;
1780+
unsigned long rc;
1781+
1782+
if (!machine_is(pseries))
1783+
return -ENOTTY;
1784+
1785+
rc = plpar_get_cpu_characteristics(&c);
1786+
if (rc == H_SUCCESS) {
1787+
cp->character = c.character;
1788+
cp->behaviour = c.behaviour;
1789+
cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
1790+
KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
1791+
KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
1792+
KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
1793+
KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
1794+
KVM_PPC_CPU_CHAR_BR_HINT_HONOURED |
1795+
KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF |
1796+
KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
1797+
cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
1798+
KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
1799+
KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
1800+
}
1801+
return 0;
1802+
}
1803+
#else
1804+
static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
1805+
{
1806+
return -ENOTTY;
1807+
}
1808+
#endif
1809+
1810+
static inline bool have_fw_feat(struct device_node *fw_features,
1811+
const char *state, const char *name)
1812+
{
1813+
struct device_node *np;
1814+
bool r = false;
1815+
1816+
np = of_get_child_by_name(fw_features, name);
1817+
if (np) {
1818+
r = of_property_read_bool(np, state);
1819+
of_node_put(np);
1820+
}
1821+
return r;
1822+
}
1823+
1824+
static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char *cp)
1825+
{
1826+
struct device_node *np, *fw_features;
1827+
int r;
1828+
1829+
memset(cp, 0, sizeof(*cp));
1830+
r = pseries_get_cpu_char(cp);
1831+
if (r != -ENOTTY)
1832+
return r;
1833+
1834+
np = of_find_node_by_name(NULL, "ibm,opal");
1835+
if (np) {
1836+
fw_features = of_get_child_by_name(np, "fw-features");
1837+
of_node_put(np);
1838+
if (!fw_features)
1839+
return 0;
1840+
if (have_fw_feat(fw_features, "enabled",
1841+
"inst-spec-barrier-ori31,31,0"))
1842+
cp->character |= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31;
1843+
if (have_fw_feat(fw_features, "enabled",
1844+
"fw-bcctrl-serialized"))
1845+
cp->character |= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED;
1846+
if (have_fw_feat(fw_features, "enabled",
1847+
"inst-l1d-flush-ori30,30,0"))
1848+
cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30;
1849+
if (have_fw_feat(fw_features, "enabled",
1850+
"inst-l1d-flush-trig2"))
1851+
cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2;
1852+
if (have_fw_feat(fw_features, "enabled",
1853+
"fw-l1d-thread-split"))
1854+
cp->character |= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV;
1855+
if (have_fw_feat(fw_features, "enabled",
1856+
"fw-count-cache-disabled"))
1857+
cp->character |= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
1858+
cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
1859+
KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
1860+
KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
1861+
KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
1862+
KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
1863+
KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
1864+
1865+
if (have_fw_feat(fw_features, "enabled",
1866+
"speculation-policy-favor-security"))
1867+
cp->behaviour |= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY;
1868+
if (!have_fw_feat(fw_features, "disabled",
1869+
"needs-l1d-flush-msr-pr-0-to-1"))
1870+
cp->behaviour |= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR;
1871+
if (!have_fw_feat(fw_features, "disabled",
1872+
"needs-spec-barrier-for-bound-checks"))
1873+
cp->behaviour |= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
1874+
cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
1875+
KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
1876+
KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
1877+
1878+
of_node_put(fw_features);
1879+
}
1880+
1881+
return 0;
1882+
}
1883+
#endif
1884+
17621885
long kvm_arch_vm_ioctl(struct file *filp,
17631886
unsigned int ioctl, unsigned long arg)
17641887
{
@@ -1861,6 +1984,14 @@ long kvm_arch_vm_ioctl(struct file *filp,
18611984
r = -EFAULT;
18621985
break;
18631986
}
1987+
case KVM_PPC_GET_CPU_CHAR: {
1988+
struct kvm_ppc_cpu_char cpuchar;
1989+
1990+
r = kvmppc_get_cpu_char(&cpuchar);
1991+
if (r >= 0 && copy_to_user(argp, &cpuchar, sizeof(cpuchar)))
1992+
r = -EFAULT;
1993+
break;
1994+
}
18641995
default: {
18651996
struct kvm *kvm = filp->private_data;
18661997
r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);

include/uapi/linux/kvm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,7 @@ struct kvm_ppc_resize_hpt {
932932
#define KVM_CAP_HYPERV_SYNIC2 148
933933
#define KVM_CAP_HYPERV_VP_INDEX 149
934934
#define KVM_CAP_S390_AIS_MIGRATION 150
935+
#define KVM_CAP_PPC_GET_CPU_CHAR 151
935936

936937
#ifdef KVM_CAP_IRQ_ROUTING
937938

@@ -1261,6 +1262,8 @@ struct kvm_s390_ucas_mapping {
12611262
#define KVM_PPC_CONFIGURE_V3_MMU _IOW(KVMIO, 0xaf, struct kvm_ppc_mmuv3_cfg)
12621263
/* Available with KVM_CAP_PPC_RADIX_MMU */
12631264
#define KVM_PPC_GET_RMMU_INFO _IOW(KVMIO, 0xb0, struct kvm_ppc_rmmu_info)
1265+
/* Available with KVM_CAP_PPC_GET_CPU_CHAR */
1266+
#define KVM_PPC_GET_CPU_CHAR _IOR(KVMIO, 0xb1, struct kvm_ppc_cpu_char)
12641267

12651268
/* ioctl for vm fd */
12661269
#define KVM_CREATE_DEVICE _IOWR(KVMIO, 0xe0, struct kvm_create_device)

0 commit comments

Comments
 (0)