All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tim <mdspan.github@gmail.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, richard.henderson@linaro.org,
	zhao1.liu@intel.com, Tim <mdspan.github@gmail.com>
Subject: [PATCH v3 3/5] target/i386: add MMU indexes for LAM-masked data accesses
Date: Tue, 24 Nov 2026 10:16:52 +0000	[thread overview]
Message-ID: <20261124101655.2269531-4-mdspan.github@gmail.com> (raw)
In-Reply-To: <20261124101655.2269531-1-mdspan.github@gmail.com>

Add LAM forms of the three 64-bit data indexes, and a hflags bit,
HF_LAM, that is set when CPUID reports LAM and any of CR3.LAM_U57,
CR3.LAM_U48, and CR4.LAM_SUP is set.  cpu_x86_update_cr3() and
cpu_x86_update_cr4() keep the bit up to date.  The bit is part of the
TB flags, since translate.c fixes the data mmu_idx per TB.

While HF_LAM is set, x86_mmu_index_pl() and x86_mmu_index_kernel_pl()
return the LAM forms.  Instruction fetches always use the normal
indexes, since LAM applies only to data accesses.  If LAM is not
enabled, only the normal indexes are used.

This lets the fetch and data translations of a linear address differ,
which LAM needs.

Suggested-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Tim <mdspan.github@gmail.com>
---
v3: new patch, in place of the v2 fetch MMU indexes, as suggested in
review of v2.

 target/i386/cpu.h            | 46 ++++++++++++++++++++++++++++++++----
 target/i386/helper.c         | 13 ++++++++++
 target/i386/tcg/seg_helper.c |  2 +-
 target/i386/tcg/tcg-cpu.c    | 12 ++++++++--
 4 files changed, 65 insertions(+), 8 deletions(-)

diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index 3945e903..0923ab4f 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -192,6 +192,7 @@ typedef enum X86Seg {
 #define HF_UMIP_SHIFT       27 /* CR4.UMIP */
 #define HF_AVX_EN_SHIFT     28 /* AVX Enabled (CR4+XCR0) */
 #define HF_APX_EN_SHIFT     29 /* APX Enabled (CR4+XCR0) */
+#define HF_LAM_SHIFT        30 /* LAM enabled (CR3+CR4) */
 
 #define HF_CPL_MASK          (3 << HF_CPL_SHIFT)
 #define HF_INHIBIT_IRQ_MASK  (1 << HF_INHIBIT_IRQ_SHIFT)
@@ -220,6 +221,7 @@ typedef enum X86Seg {
 #define HF_UMIP_MASK         (1 << HF_UMIP_SHIFT)
 #define HF_AVX_EN_MASK       (1 << HF_AVX_EN_SHIFT)
 #define HF_APX_EN_MASK       (1 << HF_APX_EN_SHIFT)
+#define HF_LAM_MASK          (1 << HF_LAM_SHIFT)
 
 /* hflags2 */
 
@@ -260,6 +262,9 @@ typedef enum X86Seg {
 #define CR0_CD_MASK  (1U << 30)
 #define CR0_PG_MASK  (1U << 31)
 
+#define CR3_LAM_U57_MASK (1ULL << 61)
+#define CR3_LAM_U48_MASK (1ULL << 62)
+
 #define CR4_VME_MASK  (1U << 0)
 #define CR4_PVI_MASK  (1U << 1)
 #define CR4_TSD_MASK  (1U << 2)
@@ -2781,6 +2786,7 @@ static inline bool x86_has_cpuid_0x1f(X86CPU *cpu)
 void x86_cpu_set_a20(X86CPU *cpu, int a20_state);
 void cpu_sync_avx_hflag(CPUX86State *env);
 void cpu_sync_apx_hflag(CPUX86State *env);
+void cpu_sync_lam_hflag(CPUX86State *env);
 
 typedef enum X86ASIdx {
     X86ASIdx_MEM = 0,
@@ -2838,8 +2844,11 @@ uint64_t cpu_get_tsc(CPUX86State *env);
 #define MMU_USER32_IDX     3
 #define MMU_KNOSMAP64_IDX  4
 #define MMU_KNOSMAP32_IDX  5
-#define MMU_PHYS_IDX       6
-#define MMU_NESTED_IDX     7
+#define MMU_KSMAP64_LAM_IDX    6
+#define MMU_USER64_LAM_IDX     7
+#define MMU_KNOSMAP64_LAM_IDX  8
+#define MMU_PHYS_IDX       9
+#define MMU_NESTED_IDX     10
 
 #ifdef CONFIG_USER_ONLY
 #ifdef TARGET_X86_64
@@ -2851,18 +2860,45 @@ uint64_t cpu_get_tsc(CPUX86State *env);
 
 static inline bool is_mmu_index_smap(int mmu_index)
 {
-    return (mmu_index & ~1) == MMU_KSMAP64_IDX;
+    return (mmu_index & ~1) == MMU_KSMAP64_IDX ||
+           mmu_index == MMU_KSMAP64_LAM_IDX;
 }
 
 static inline bool is_mmu_index_user(int mmu_index)
 {
-    return (mmu_index & ~1) == MMU_USER64_IDX;
+    return (mmu_index & ~1) == MMU_USER64_IDX ||
+           mmu_index == MMU_USER64_LAM_IDX;
+}
+
+/* LAM masks the linear addresses of accesses with these indexes */
+static inline bool is_mmu_index_lam(int mmu_index)
+{
+    return mmu_index >= MMU_KSMAP64_LAM_IDX &&
+           mmu_index <= MMU_KNOSMAP64_LAM_IDX;
 }
 
 static inline bool is_mmu_index_32(int mmu_index)
 {
     assert(mmu_index < MMU_PHYS_IDX);
-    return mmu_index & 1;
+    return !is_mmu_index_lam(mmu_index) && (mmu_index & 1);
+}
+
+/* The LAM form of a 64-bit data index, if LAM is enabled */
+static inline int x86_mmu_index_lam(CPUX86State *env, int mmu_index)
+{
+    if (!(env->hflags & HF_LAM_MASK)) {
+        return mmu_index;
+    }
+    switch (mmu_index) {
+    case MMU_KSMAP64_IDX:
+        return MMU_KSMAP64_LAM_IDX;
+    case MMU_USER64_IDX:
+        return MMU_USER64_LAM_IDX;
+    case MMU_KNOSMAP64_IDX:
+        return MMU_KNOSMAP64_LAM_IDX;
+    default:
+        return mmu_index;
+    }
 }
 
 #define CC_DST  (env->cc_dst)
diff --git a/target/i386/helper.c b/target/i386/helper.c
index 723e87fe..e116ae27 100644
--- a/target/i386/helper.c
+++ b/target/i386/helper.c
@@ -46,6 +46,17 @@ void cpu_sync_apx_hflag(CPUX86State *env)
     }
 }
 
+void cpu_sync_lam_hflag(CPUX86State *env)
+{
+    if ((env->features[FEAT_7_1_EAX] & CPUID_7_1_EAX_LAM) &&
+        ((env->cr[3] & (CR3_LAM_U57_MASK | CR3_LAM_U48_MASK)) ||
+         (env->cr[4] & CR4_LAM_SUP_MASK))) {
+        env->hflags |= HF_LAM_MASK;
+    } else {
+        env->hflags &= ~HF_LAM_MASK;
+    }
+}
+
 void cpu_sync_avx_hflag(CPUX86State *env)
 {
     if ((env->cr[4] & CR4_OSXSAVE_MASK)
@@ -188,6 +199,7 @@ void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0)
 void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3)
 {
     env->cr[3] = new_cr3;
+    cpu_sync_lam_hflag(env);
     if (env->cr[0] & CR0_PG_MASK) {
         qemu_log_mask(CPU_LOG_MMU,
                         "CR3 update: CR3=" TARGET_FMT_lx "\n", new_cr3);
@@ -261,6 +273,7 @@ void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
     cpu_sync_bndcs_hflags(env);
     cpu_sync_avx_hflag(env);
     cpu_sync_apx_hflag(env);
+    cpu_sync_lam_hflag(env);
 }
 
 #if !defined(CONFIG_USER_ONLY)
diff --git a/target/i386/tcg/seg_helper.c b/target/i386/tcg/seg_helper.c
index f11b55d1..e26ebf3a 100644
--- a/target/i386/tcg/seg_helper.c
+++ b/target/i386/tcg/seg_helper.c
@@ -138,7 +138,7 @@ static int x86_mmu_index_kernel_pl(CPUX86State *env, unsigned pl)
         (pl < 3 && (env->eflags & AC_MASK)
          ? MMU_KNOSMAP64_IDX : MMU_KSMAP64_IDX);
 
-    return mmu_index_base + mmu_index_32;
+    return x86_mmu_index_lam(env, mmu_index_base + mmu_index_32);
 }
 
 int cpu_mmu_index_kernel(CPUX86State *env)
diff --git a/target/i386/tcg/tcg-cpu.c b/target/i386/tcg/tcg-cpu.c
index 167ff769..f1e9b19b 100644
--- a/target/i386/tcg/tcg-cpu.c
+++ b/target/i386/tcg/tcg-cpu.c
@@ -116,7 +116,7 @@ static void x86_restore_state_to_opc(CPUState *cs,
     }
 }
 
-int x86_mmu_index_pl(CPUX86State *env, unsigned pl)
+static int x86_mmu_index_pl_nolam(CPUX86State *env, unsigned pl)
 {
     int mmu_index_32 = (env->hflags & HF_CS64_MASK) ? 0 : 1;
     int mmu_index_base =
@@ -127,10 +127,18 @@ int x86_mmu_index_pl(CPUX86State *env, unsigned pl)
     return mmu_index_base + mmu_index_32;
 }
 
+int x86_mmu_index_pl(CPUX86State *env, unsigned pl)
+{
+    return x86_mmu_index_lam(env, x86_mmu_index_pl_nolam(env, pl));
+}
+
 static int x86_cpu_mmu_index(CPUState *cs, bool ifetch)
 {
     CPUX86State *env = cpu_env(cs);
-    return x86_mmu_index_pl(env, env->hflags & HF_CPL_MASK);
+    unsigned pl = env->hflags & HF_CPL_MASK;
+
+    /* LAM does not apply to instruction fetches */
+    return ifetch ? x86_mmu_index_pl_nolam(env, pl) : x86_mmu_index_pl(env, pl);
 }
 
 #ifndef CONFIG_USER_ONLY
-- 
2.47.3



  parent reply	other threads:[~2026-11-24 10:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 10:16 [PATCH v3 0/5] target/i386: emulate LAM in TCG Tim
2026-11-24 10:16 ` [PATCH v3 1/5] target/i386: mask the PML5 table address taken from CR3 Tim
2026-11-24 10:16 ` [PATCH v3 2/5] accel/tcg: compare large pages under the flush mask Tim
2026-11-24 10:16 ` Tim [this message]
2026-11-24 10:16 ` [PATCH v3 4/5] target/i386: emulate LAM in TCG Tim
2026-11-24 10:16 ` [PATCH v3 5/5] tests/tcg/x86_64: add a LAM system test Tim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124101655.2269531-4-mdspan.github@gmail.com \
    --to=mdspan.github@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=zhao1.liu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.