The RMPREAD instruction returns an architecture defined format of an
RMP entry. This is the preferred method for examining RMP entries.
In preparation for using the RMPREAD instruction, convert the existing
code that directly accesses the RMP to map the raw RMP information into
the architecture defined format.
RMPREAD output returns a status bit for the 2MB region status. If the
input page address is 2MB aligned and any other pages within the 2MB
region are assigned, then 2MB region status will be set to 1. Otherwise,
the 2MB region status will be set to 0. For systems that do not support
RMPREAD, calculating this value would require looping over all of the RMP
table entries within that range until one is found with the assigned bit
set. Since this bit is not defined in the current format, and so not used
today, do not incur the overhead associated with calculating it.
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
---
arch/x86/virt/svm/sev.c | 141 ++++++++++++++++++++++++++++------------
1 file changed, 98 insertions(+), 43 deletions(-)
diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c
index 0ce17766c0e5..103a2dd6e81d 100644
--- a/arch/x86/virt/svm/sev.c
+++ b/arch/x86/virt/svm/sev.c
@@ -30,11 +30,27 @@
#include <asm/cmdline.h>
#include <asm/iommu.h>
+/*
+ * The RMP entry format as returned by the RMPREAD instruction.
+ */
+struct rmpentry {
+ u64 gpa;
+ u8 assigned :1,
+ rsvd1 :7;
+ u8 pagesize :1,
+ hpage_region_status :1,
+ rsvd2 :6;
+ u8 immutable :1,
+ rsvd3 :7;
+ u8 rsvd4;
+ u32 asid;
+} __packed;
+
/*
* The RMP entry format is not architectural. The format is defined in PPR
* Family 19h Model 01h, Rev B1 processor.
*/
-struct rmpentry {
+struct rmpentry_raw {
union {
struct {
u64 assigned : 1,
@@ -62,7 +78,7 @@ struct rmpentry {
#define PFN_PMD_MASK GENMASK_ULL(63, PMD_SHIFT - PAGE_SHIFT)
static u64 probed_rmp_base, probed_rmp_size;
-static struct rmpentry *rmptable __ro_after_init;
+static struct rmpentry_raw *rmptable __ro_after_init;
static u64 rmptable_max_pfn __ro_after_init;
static LIST_HEAD(snp_leaked_pages_list);
@@ -247,8 +263,8 @@ static int __init snp_rmptable_init(void)
rmptable_start += RMPTABLE_CPU_BOOKKEEPING_SZ;
rmptable_size = probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ;
- rmptable = (struct rmpentry *)rmptable_start;
- rmptable_max_pfn = rmptable_size / sizeof(struct rmpentry) - 1;
+ rmptable = (struct rmpentry_raw *)rmptable_start;
+ rmptable_max_pfn = rmptable_size / sizeof(struct rmpentry_raw) - 1;
cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/rmptable_init:online", __snp_enable, NULL);
@@ -270,48 +286,77 @@ static int __init snp_rmptable_init(void)
*/
device_initcall(snp_rmptable_init);
-static struct rmpentry *get_rmpentry(u64 pfn)
+static struct rmpentry_raw *__get_rmpentry(unsigned long pfn)
{
- if (WARN_ON_ONCE(pfn > rmptable_max_pfn))
- return ERR_PTR(-EFAULT);
-
- return &rmptable[pfn];
-}
-
-static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level)
-{
- struct rmpentry *large_entry, *entry;
-
- if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))
+ if (!rmptable)
return ERR_PTR(-ENODEV);
- entry = get_rmpentry(pfn);
- if (IS_ERR(entry))
- return entry;
+ if (unlikely(pfn > rmptable_max_pfn))
+ return ERR_PTR(-EFAULT);
+
+ return rmptable + pfn;
+}
+
+static int get_rmpentry(u64 pfn, struct rmpentry *entry)
+{
+ struct rmpentry_raw *e;
+
+ e = __get_rmpentry(pfn);
+ if (IS_ERR(e))
+ return PTR_ERR(e);
+
+ /*
+ * Map the RMP table entry onto the RMPREAD output format.
+ * The 2MB region status indicator (hpage_region_status field) is not
+ * calculated, since the overhead could be significant and the field
+ * is not used.
+ */
+ memset(entry, 0, sizeof(*entry));
+ entry->gpa = e->gpa << PAGE_SHIFT;
+ entry->asid = e->asid;
+ entry->assigned = e->assigned;
+ entry->pagesize = e->pagesize;
+ entry->immutable = e->immutable;
+
+ return 0;
+}
+
+static int __snp_lookup_rmpentry(u64 pfn, struct rmpentry *entry, int *level)
+{
+ struct rmpentry large_entry;
+ int ret;
+
+ if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))
+ return -ENODEV;
+
+ ret = get_rmpentry(pfn, entry);
+ if (ret)
+ return ret;
/*
* Find the authoritative RMP entry for a PFN. This can be either a 4K
* RMP entry or a special large RMP entry that is authoritative for a
* whole 2M area.
*/
- large_entry = get_rmpentry(pfn & PFN_PMD_MASK);
- if (IS_ERR(large_entry))
- return large_entry;
+ ret = get_rmpentry(pfn & PFN_PMD_MASK, &large_entry);
+ if (ret)
+ return ret;
- *level = RMP_TO_PG_LEVEL(large_entry->pagesize);
+ *level = RMP_TO_PG_LEVEL(large_entry.pagesize);
- return entry;
+ return 0;
}
int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level)
{
- struct rmpentry *e;
+ struct rmpentry e;
+ int ret;
- e = __snp_lookup_rmpentry(pfn, level);
- if (IS_ERR(e))
- return PTR_ERR(e);
+ ret = __snp_lookup_rmpentry(pfn, &e, level);
+ if (ret)
+ return ret;
- *assigned = !!e->assigned;
+ *assigned = !!e.assigned;
return 0;
}
EXPORT_SYMBOL_GPL(snp_lookup_rmpentry);
@@ -324,20 +369,28 @@ EXPORT_SYMBOL_GPL(snp_lookup_rmpentry);
*/
static void dump_rmpentry(u64 pfn)
{
+ struct rmpentry_raw *e_raw;
u64 pfn_i, pfn_end;
- struct rmpentry *e;
- int level;
+ struct rmpentry e;
+ int level, ret;
- e = __snp_lookup_rmpentry(pfn, &level);
- if (IS_ERR(e)) {
- pr_err("Failed to read RMP entry for PFN 0x%llx, error %ld\n",
- pfn, PTR_ERR(e));
+ ret = __snp_lookup_rmpentry(pfn, &e, &level);
+ if (ret) {
+ pr_err("Failed to read RMP entry for PFN 0x%llx, error %d\n",
+ pfn, ret);
return;
}
- if (e->assigned) {
+ if (e.assigned) {
+ e_raw = __get_rmpentry(pfn);
+ if (IS_ERR(e_raw)) {
+ pr_err("Failed to read RMP contents for PFN 0x%llx, error %ld\n",
+ pfn, PTR_ERR(e_raw));
+ return;
+ }
+
pr_info("PFN 0x%llx, RMP entry: [0x%016llx - 0x%016llx]\n",
- pfn, e->lo, e->hi);
+ pfn, e_raw->lo, e_raw->hi);
return;
}
@@ -356,16 +409,18 @@ static void dump_rmpentry(u64 pfn)
pfn, pfn_i, pfn_end);
while (pfn_i < pfn_end) {
- e = __snp_lookup_rmpentry(pfn_i, &level);
- if (IS_ERR(e)) {
- pr_err("Error %ld reading RMP entry for PFN 0x%llx\n",
- PTR_ERR(e), pfn_i);
+ e_raw = __get_rmpentry(pfn_i);
+ if (IS_ERR(e_raw)) {
+ pr_err("Error %ld reading RMP contents for PFN 0x%llx\n",
+ PTR_ERR(e_raw), pfn_i);
pfn_i++;
continue;
}
- if (e->lo || e->hi)
- pr_info("PFN: 0x%llx, [0x%016llx - 0x%016llx]\n", pfn_i, e->lo, e->hi);
+ if (e_raw->lo || e_raw->hi)
+ pr_info("PFN: 0x%llx, [0x%016llx - 0x%016llx]\n",
+ pfn_i, e_raw->lo, e_raw->hi);
+
pfn_i++;
}
}
--
2.43.2
On Tue, Jul 30, 2024 at 02:40:01PM -0500, Tom Lendacky wrote:
> The RMPREAD instruction returns an architecture defined format of an
> RMP entry. This is the preferred method for examining RMP entries.
>
> In preparation for using the RMPREAD instruction, convert the existing
> code that directly accesses the RMP to map the raw RMP information into
> the architecture defined format.
>
> RMPREAD output returns a status bit for the 2MB region status. If the
> input page address is 2MB aligned and any other pages within the 2MB
> region are assigned, then 2MB region status will be set to 1. Otherwise,
> the 2MB region status will be set to 0. For systems that do not support
> RMPREAD, calculating this value would require looping over all of the RMP
> table entries within that range until one is found with the assigned bit
> set. Since this bit is not defined in the current format, and so not used
> today, do not incur the overhead associated with calculating it.
>
> Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
> ---
> arch/x86/virt/svm/sev.c | 141 ++++++++++++++++++++++++++++------------
> 1 file changed, 98 insertions(+), 43 deletions(-)
>
> diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c
> index 0ce17766c0e5..103a2dd6e81d 100644
> --- a/arch/x86/virt/svm/sev.c
> +++ b/arch/x86/virt/svm/sev.c
> @@ -30,11 +30,27 @@
> #include <asm/cmdline.h>
> #include <asm/iommu.h>
>
> +/*
> + * The RMP entry format as returned by the RMPREAD instruction.
I'm guessing this is the architectural variant...
> + */
> +struct rmpentry {
> + u64 gpa;
> + u8 assigned :1,
> + rsvd1 :7;
> + u8 pagesize :1,
> + hpage_region_status :1,
> + rsvd2 :6;
> + u8 immutable :1,
> + rsvd3 :7;
> + u8 rsvd4;
> + u32 asid;
> +} __packed;
> +
> /*
> * The RMP entry format is not architectural. The format is defined in PPR
> * Family 19h Model 01h, Rev B1 processor.
... considering this thing?
> */
> -struct rmpentry {
> +struct rmpentry_raw {
"raw"? Hm.
So what is the goal here?
Use rmpentry_raw for the kernel's representation and convert the format that
gets returned by RMPREAD into rmpentry_raw?
Because if so, the _raw thing is what comes from RMPREAD.
Because as it is now, it is begging to confuse people.
Because if you call the *new* entry differently, it won't cause any of the
churn you have to do below...
Hmmm?
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette
> On Wed, 4 Sep 2024 12:47:28 +0200, Borislav Petkov wrote:
Sorry, this email seemed to get lost in our email quarantine. Trying to
reply using copy/paste and git send-email...
> > On Tue, Jul 30, 2024 at 02:40:01PM -0500, Tom Lendacky wrote:
> > The RMPREAD instruction returns an architecture defined format of an
> > RMP entry. This is the preferred method for examining RMP entries.
> >
> > In preparation for using the RMPREAD instruction, convert the existing
> > code that directly accesses the RMP to map the raw RMP information into
> > the architecture defined format.
> >
> > RMPREAD output returns a status bit for the 2MB region status. If the
> > input page address is 2MB aligned and any other pages within the 2MB
> > region are assigned, then 2MB region status will be set to 1. Otherwise,
> > the 2MB region status will be set to 0. For systems that do not support
> > RMPREAD, calculating this value would require looping over all of the RMP
> > table entries within that range until one is found with the assigned bit
> > set. Since this bit is not defined in the current format, and so not used
> > today, do not incur the overhead associated with calculating it.
> >
> > Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
> > ---
> > arch/x86/virt/svm/sev.c | 141 ++++++++++++++++++++++++++++------------
> > 1 file changed, 98 insertions(+), 43 deletions(-)
> >
> > diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c
> > index 0ce17766c0e5..103a2dd6e81d 100644
> > --- a/arch/x86/virt/svm/sev.c
> > +++ b/arch/x86/virt/svm/sev.c
> > @@ -30,11 +30,27 @@
> > #include <asm/cmdline.h>
> > #include <asm/iommu.h>
> >
> > +/*
> > + * The RMP entry format as returned by the RMPREAD instruction.
>
> I'm guessing this is the architectural variant...
Yes, this is the format returned by RMPREAD.
>
> > + */
> > +struct rmpentry {
> > + u64 gpa;
> > + u8 assigned :1,
> > + rsvd1 :7;
> > + u8 pagesize :1,
> > + hpage_region_status :1,
> > + rsvd2 :6;
> > + u8 immutable :1,
> > + rsvd3 :7;
> > + u8 rsvd4;
> > + u32 asid;
> > +} __packed;
> > +
> > /*
> > * The RMP entry format is not architectural. The format is defined in PPR
> > * Family 19h Model 01h, Rev B1 processor.
>
> ... considering this thing?
>
> > */
> > -struct rmpentry {
> > +struct rmpentry_raw {
>
> "raw"? Hm.
>
> So what is the goal here?
>
> Use rmpentry_raw for the kernel's representation and convert the format that
> gets returned by RMPREAD into rmpentry_raw?
No, just the opposite. Take the current systems that don't have RMPREAD support
and transform the "raw" RMP entry data obtained directly from the RMP table
into the architectural variant. This way, only the architectural variant is
used going forward.
>
> Because if so, the _raw thing is what comes from RMPREAD.
>
> Because as it is now, it is begging to confuse people.
>
> Because if you call the *new* entry differently, it won't cause any of the
> churn you have to do below...
I can look at naming the new struct "rmpread" and see how that looks if you
prefer.
Thanks,
Tom
>
> Hmmm?
>
> --
> Regards/Gruss,
> Boris.
>
On Thu, Oct 17, 2024 at 01:16:36PM -0500, Tom Lendacky wrote:
> I can look at naming the new struct "rmpread" and see how that looks if you
> prefer.
I think I understand what you mean now and I guess something like the below
would make it more clear. Diff ontop:
diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c
index 73d4f422829a..73d9295dd013 100644
--- a/arch/x86/virt/svm/sev.c
+++ b/arch/x86/virt/svm/sev.c
@@ -48,7 +48,8 @@ struct rmpentry {
/*
* The RMP entry format is not architectural. The format is defined in PPR
- * Family 19h Model 01h, Rev B1 processor.
+ * Family 19h Model 01h, Rev B1 processor. This is the raw, verbatim
+ * representation as it is found in the RMP table.
*/
struct rmpentry_raw {
union {
@@ -286,7 +287,7 @@ static int __init snp_rmptable_init(void)
*/
device_initcall(snp_rmptable_init);
-static struct rmpentry_raw *__get_rmpentry(unsigned long pfn)
+static struct rmpentry_raw *get_raw_rmpentry(unsigned long pfn)
{
if (!rmptable)
return ERR_PTR(-ENODEV);
@@ -312,7 +313,7 @@ static int get_rmpentry(u64 pfn, struct rmpentry *entry)
return ret;
}
- e = __get_rmpentry(pfn);
+ e = get_raw_rmpentry(pfn);
if (IS_ERR(e))
return PTR_ERR(e);
@@ -393,7 +394,7 @@ static void dump_rmpentry(u64 pfn)
}
if (e.assigned) {
- e_raw = __get_rmpentry(pfn);
+ e_raw = get_raw_rmpentry(pfn);
if (IS_ERR(e_raw)) {
pr_err("Failed to read RMP contents for PFN 0x%llx, error %ld\n",
pfn, PTR_ERR(e_raw));
@@ -420,7 +421,7 @@ static void dump_rmpentry(u64 pfn)
pfn, pfn_i, pfn_end);
while (pfn_i < pfn_end) {
- e_raw = __get_rmpentry(pfn_i);
+ e_raw = get_raw_rmpentry(pfn_i);
if (IS_ERR(e_raw)) {
pr_err("Error %ld reading RMP contents for PFN 0x%llx\n",
PTR_ERR(e_raw), pfn_i);
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette
© 2016 - 2026 Red Hat, Inc.