From nobody Tue Nov 26 10:21:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1708694097; cv=none; d=zohomail.com; s=zohoarc; b=WpD1dkg5zG1pmxxZPMNploSdQsFAOdGh8m7XLBsD3yM0Gt9ik3x0TvJlNJfWoe8+KA8vKWGe0gYdZMe80OyyukDcTrjuEFdZSrRJVC8QSRa9e/NhWzT4h4J0gY5fwGNk3XeVcG4OuS7awh3167Yz6oN5ujIeTF6cupOa00+9pgg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708694097; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=IAP6k7a7KD7cA1kLn+K+ZlS1jEOLhqfbzH2yPUv60PA=; b=RerXINsgweMvh38STFMMs4MD7P7SV6b4tysdUW9Ki8VNtmzkUlmBxUJWWyhBgoGzIzZog1tl24mhtjm7oto5Lggp3jn+mvi7HPp9pvT9z7bvKZTpYDz5d7RRHcjb2rZTuxX2oGw4fjP4qWUGtGtSUO8pwvnJW7eOyvku2/hxYk0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1708694097846245.98897702096724; Fri, 23 Feb 2024 05:14:57 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rdVNd-0005V5-Su; Fri, 23 Feb 2024 08:14:21 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rdVJZ-0002YM-2E for qemu-devel@nongnu.org; Fri, 23 Feb 2024 08:10:09 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rdVJX-0000S0-5u for qemu-devel@nongnu.org; Fri, 23 Feb 2024 08:10:08 -0500 Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-453-Hhiso-0WPu6VCWxM0Ux6sQ-1; Fri, 23 Feb 2024 08:10:05 -0500 Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-512aa50323cso946749e87.0 for ; Fri, 23 Feb 2024 05:10:04 -0800 (PST) Received: from [192.168.10.118] ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id bi3-20020a0565120e8300b00512e5a40534sm260869lfb.214.2024.02.23.05.10.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 05:10:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708693806; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IAP6k7a7KD7cA1kLn+K+ZlS1jEOLhqfbzH2yPUv60PA=; b=isFUVvGqwnO235MDy4bke0noH3kwk+zkSDtS1Z+jnKzTcgHpEjQ22rvBiwTxfPaVDvy392 HpevyfDm6/h8uzAGIBJNs71mIn85Q6BfrGBO9ak/48URXv3XjlhSI4tDtSsmi3Fe5rgLjU dMKHJAZXYhX3pki7OSoOe21xlE3R4k8= X-MC-Unique: Hhiso-0WPu6VCWxM0Ux6sQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708693803; x=1709298603; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IAP6k7a7KD7cA1kLn+K+ZlS1jEOLhqfbzH2yPUv60PA=; b=Bkg1jHvhL/KxPd2asiM4BDLT/LsdaNRQ2/Z2WKgZ3Rm1GqMD8sfoloFYPlPjwoE6qh +8IzNtKye98DxDA3uAl3iKfnCViirW+6GvEj8Jg/v5ZS1w1LgcDtqWWBQ7Xw+tmmW7fd FCwTVV91ndXF2zUqfSyrFRnS+7dqDZtJFEHfx93aChStAI6sWkktH76yEyI8rRGfkOFo l0G3jpwnvGbBNoomvYhH5GnH3b4sN1sHPVs0qKdy0iTsvVdaZkK91rn/5ofLONDI9QP4 e3uh8q3fs2w1a5+EXM67DhuD0/uog7IGAtiB+tZK4PvzTJXQcC55V3v8Y5EKPHEt89Nb SHUQ== X-Gm-Message-State: AOJu0YxyNuVYdL/WbjBy1V1OCtG2B7TK5id9j4aboOakG1ETi8AeFJfT t406L2Jn/xR8A4h/pdGgGZtBmfng+7ZlcutWNumiN59TAhOAO2LjA0Ku7Rw4ozOjboEHY1BubFX oLKGJCsP0h2Hqq1XdZloxd081bndBhoAWC3t6kgnnfnBWOFt4VTDS6+S3wopp6tzgCSWmVDzUHu Uk+4/6QGs0HHtfpZWMhj3EirpWXLIzwR1u7XV4 X-Received: by 2002:ac2:484f:0:b0:512:d91f:95ce with SMTP id 15-20020ac2484f000000b00512d91f95cemr1255461lfy.65.1708693802974; Fri, 23 Feb 2024 05:10:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IGRgR/uJZHjMvnfc8TeK/IA48ymX6CSy2Ilr69bnEh/lJB5w8jWwgMOWQr2ePbdyg3hETSMyg== X-Received: by 2002:ac2:484f:0:b0:512:d91f:95ce with SMTP id 15-20020ac2484f000000b00512d91f95cemr1255447lfy.65.1708693802650; Fri, 23 Feb 2024 05:10:02 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: richard.henderson@linaro.org, mcb30@ipxe.org Subject: [PATCH v2 4/7] target/i386: use separate MMU indexes for 32-bit accesses Date: Fri, 23 Feb 2024 14:09:45 +0100 Message-ID: <20240223130948.237186-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240223130948.237186-1-pbonzini@redhat.com> References: <20240223130948.237186-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.066, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1708694099980100003 Content-Type: text/plain; charset="utf-8" Accesses from a 32-bit environment (32-bit code segment for instruction accesses, EFER.LMA=3D=3D0 for processor accesses) have to mask away the upper 32 bits of the address. While a bit wasteful, the easiest way to do so is to use separate MMU indexes. These days, QEMU anyway is compiled with a fixed value for NB_MMU_MODES. Split MMU_USER_IDX, MMU_KSMAP_IDX and MMU_KNOSMAP_IDX in two. Signed-off-by: Paolo Bonzini Reviewed-by: Zhao Liu --- target/i386/cpu.h | 34 ++++++++++++++++++++-------- target/i386/cpu.c | 11 +++++---- target/i386/tcg/sysemu/excp_helper.c | 3 ++- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 8c271ca62e5..ee4ad372021 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -2299,27 +2299,41 @@ uint64_t cpu_get_tsc(CPUX86State *env); #define cpu_list x86_cpu_list =20 /* MMU modes definitions */ -#define MMU_KSMAP_IDX 0 -#define MMU_USER_IDX 1 -#define MMU_KNOSMAP_IDX 2 -#define MMU_NESTED_IDX 3 -#define MMU_PHYS_IDX 4 +#define MMU_KSMAP64_IDX 0 +#define MMU_KSMAP32_IDX 1 +#define MMU_USER64_IDX 2 +#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 + +#ifdef CONFIG_USER_ONLY +#ifdef TARGET_X86_64 +#define MMU_USER_IDX MMU_USER64_IDX +#else +#define MMU_USER_IDX MMU_USER32_IDX +#endif +#endif =20 static inline bool is_mmu_index_smap(int mmu_index) { - return mmu_index =3D=3D MMU_KSMAP_IDX; + return (mmu_index & ~1) =3D=3D MMU_KSMAP64_IDX; } =20 static inline bool is_mmu_index_user(int mmu_index) { - return mmu_index =3D=3D MMU_USER_IDX; + return (mmu_index & ~1) =3D=3D MMU_USER64_IDX; } =20 static inline int cpu_mmu_index_kernel(CPUX86State *env) { - return !(env->hflags & HF_SMAP_MASK) ? MMU_KNOSMAP_IDX : - ((env->hflags & HF_CPL_MASK) < 3 && (env->eflags & AC_MASK)) - ? MMU_KNOSMAP_IDX : MMU_KSMAP_IDX; + int mmu_index_32 =3D (env->hflags & HF_LMA_MASK) ? 1 : 0; + int mmu_index_base =3D + !(env->hflags & HF_SMAP_MASK) ? MMU_KNOSMAP64_IDX : + ((env->hflags & HF_CPL_MASK) < 3 && (env->eflags & AC_MASK)) ? MMU= _KNOSMAP64_IDX : MMU_KSMAP64_IDX; + + return mmu_index_base + mmu_index_32; } =20 #define CC_DST (env->cc_dst) diff --git a/target/i386/cpu.c b/target/i386/cpu.c index 7f908236767..647371198c7 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -7732,13 +7732,16 @@ static bool x86_cpu_has_work(CPUState *cs) return x86_cpu_pending_interrupt(cs, cs->interrupt_request) !=3D 0; } =20 -static int x86_cpu_mmu_index(CPUState *cs, bool ifetch) +static int x86_cpu_mmu_index(CPUState *env, bool ifetch) { CPUX86State *env =3D cpu_env(cs); + int mmu_index_32 =3D (env->hflags & HF_CS64_MASK) ? 1 : 0; + int mmu_index_base =3D + (env->hflags & HF_CPL_MASK) =3D=3D 3 ? MMU_USER64_IDX : + !(env->hflags & HF_SMAP_MASK) ? MMU_KNOSMAP64_IDX : + (env->eflags & AC_MASK) ? MMU_KNOSMAP64_IDX : MMU_KSMAP64_IDX; =20 - return (env->hflags & HF_CPL_MASK) =3D=3D 3 ? MMU_USER_IDX : - (!(env->hflags & HF_SMAP_MASK) || (env->eflags & AC_MASK)) - ? MMU_KNOSMAP_IDX : MMU_KSMAP_IDX; + return mmu_index_base + mmu_index_32; } =20 static void x86_disas_set_info(CPUState *cs, disassemble_info *info) diff --git a/target/i386/tcg/sysemu/excp_helper.c b/target/i386/tcg/sysemu/= excp_helper.c index a0d5ce39300..b2c525e1a92 100644 --- a/target/i386/tcg/sysemu/excp_helper.c +++ b/target/i386/tcg/sysemu/excp_helper.c @@ -545,7 +545,8 @@ static bool get_physical_address(CPUX86State *env, vadd= r addr, if (likely(use_stage2)) { in.cr3 =3D env->nested_cr3; in.pg_mode =3D env->nested_pg_mode; - in.mmu_idx =3D MMU_USER_IDX; + in.mmu_idx =3D + env->nested_pg_mode & PG_MODE_LMA ? MMU_USER64_IDX : MMU_U= SER32_IDX; in.ptw_idx =3D MMU_PHYS_IDX; =20 if (!mmu_translate(env, &in, out, err)) { --=20 2.43.0