Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AEA29C3DA4A for ; Sun, 11 Aug 2024 22:24:17 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sdGyj-00015y-IF; Sun, 11 Aug 2024 18:23:57 -0400 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 1sdGyf-00012D-V9 for qemu-riscv@nongnu.org; Sun, 11 Aug 2024 18:23:53 -0400 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sdGyc-00017i-2t for qemu-riscv@nongnu.org; Sun, 11 Aug 2024 18:23:52 -0400 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1ff1cd07f56so31519305ad.2 for ; Sun, 11 Aug 2024 15:23:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1723415027; x=1724019827; darn=nongnu.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=200woV0Waq5KPyQXnG1armcVi5NgaKFdPO/3NpLgxp8=; b=qKxXX/7RztlQllOwFGhkkaO4ds7kyXL+6Rg84P6I6CuMh1VNCOSPDupL8BSHB7CiT+ SgDy+HpPQbIneXa6w7E4PJNfrFF/KEw/G18kq5KySpQp6PqUlDh0zRECOZEk+RjzOfa0 iGef+w+gFCz7kOpSkqDhCUrC37Kw3G0DVP01uE6j2DpHOdldPi+TZqdtwvJxcs5ZP57u 7fc92cu3f6o0wsPH58f+Po3M7jl47L+kMhV7q4w3fzV1IyKzI3SX85WlU+0dGwPE752V HRhy1iCNkzu0y0Xql7/MiU8x+K0l0iBh+ZL+LzI3WCDtMfha7T0gehfNwFFL2ZCANYPC M16A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723415027; x=1724019827; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=200woV0Waq5KPyQXnG1armcVi5NgaKFdPO/3NpLgxp8=; b=lUSaPayn407egIPxoT/qDCNJPSDivAXVv8HV7Tg44pLmZOJKC57cAfWwDbElhDRZmh LcoK9hBOVidnPJBWw2Wq4UnoqbYm2EHuGaFbRJqQTsGE2GX+YgnR7qDfNmGXsTQlQkjf SKJZWxvv0y6TYy20Oz4LM0xTWMsyJeHLogNv9JOY6HAQxNmz6bFltXhZfc6oGYamrLln ya2iiEXbtf8QXFoxmzaziN1A3mcb9h+/Bd7tp0UrsRCrVDn/GDj+KO/1fKVMrXlh9ZyP K/Vt93hvzl3dnb32v3Sd/mbWCHHJ9dgAz/yP78bZbYkG6PBnivcwocNNiiWztW+JHDaS NW0Q== X-Forwarded-Encrypted: i=1; AJvYcCXTEHug+EY7xvFjrZSPl2J3kpEsA+gPGNINkFik3RTuQS1x9OUxAaDNfVD7B05aYI5bTDQ53k5wMgxR9ioZVBx/+CjhBsw= X-Gm-Message-State: AOJu0YwRaCzeCxkJc4kE60nH1YMzkKwaRIh1UfYax2C7s14C2ThGyPzY 4X3nqKaSr9zMa/LXYqXJlO9AqK/0DNyj7k5oUzR3DkkiLA9NRP210OC6/ON//sk= X-Google-Smtp-Source: AGHT+IGaymqK8zVtfDLYM7UtR+kKdB9K+VbAIc4ifMF1m2D0zX6NOpj2+7mIGXVP/Fb7R/SPz917Fg== X-Received: by 2002:a17:902:ccc7:b0:1fd:9e44:e5e9 with SMTP id d9443c01a7336-200ae5d3c99mr116261685ad.53.1723415027165; Sun, 11 Aug 2024 15:23:47 -0700 (PDT) Received: from [192.168.1.113] ([203.30.4.111]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d1c9dc8988sm6824183a91.48.2024.08.11.15.23.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 11 Aug 2024 15:23:46 -0700 (PDT) Message-ID: Date: Mon, 12 Aug 2024 08:23:38 +1000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 14/20] target/riscv: mmu changes for zicfiss shadow stack protection To: Deepak Gupta Cc: qemu-devel@nongnu.org, qemu-riscv@nongnu.org, pbonzini@redhat.com, palmer@dabbelt.com, Alistair.Francis@wdc.com, laurent@vivier.eu, bmeng.cn@gmail.com, liwei1518@gmail.com, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com References: <20240807000652.1417776-1-debug@rivosinc.com> <20240807000652.1417776-15-debug@rivosinc.com> Content-Language: en-US From: Richard Henderson In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::62f; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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-riscv@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-riscv-bounces+qemu-riscv=archiver.kernel.org@nongnu.org Sender: qemu-riscv-bounces+qemu-riscv=archiver.kernel.org@nongnu.org On 8/10/24 04:55, Deepak Gupta wrote: > On Wed, Aug 07, 2024 at 01:19:55PM +1000, Richard Henderson wrote: >> On 8/7/24 10:06, Deepak Gupta wrote: >>>      int prot = 0; >>> -    if (pte & PTE_R) { >>> +    /* >>> +     * If PTE has read bit in it or it's shadow stack page, >>> +     * then reads allowed >>> +     */ >>> +    if ((pte & PTE_R) || sstack_page) { >>>          prot |= PAGE_READ; >>>      } >> >> I feel like this logic could be simplified somehow. >> I'll think about it. > > Ok let me know. > >> >>> @@ -1409,6 +1461,11 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, >>>      qemu_log_mask(CPU_LOG_MMU, "%s ad %" VADDR_PRIx " rw %d mmu_idx %d\n", >>>                    __func__, address, access_type, mmu_idx); >>> +    /* If shadow stack instruction initiated this access, treat it as store */ >>> +    if (mmu_idx & MMU_IDX_SS_ACCESS) { >>> +        access_type = MMU_DATA_STORE; >>> +    } >> >> I know you're trying to massage the fault type, but I think this is the wrong place. > > Is it okay if I add `mmu_idx` argument to `raise_mmu_exception` ? > Inside `raise_mmu_exception`, then based on `mmu_idx == shadow stack index`, I can convert > a fault due to access_type=MMU_DATA_LOAD into store page fault. We have other places where we miss-categorize amo instructions and raise the wrong fault, I think particularly without smp, when we implement amo without host atomic operations. We should perhaps come up with a general purpose solution. For instance, set TARGET_INSN_START_EXTRA_WORDS to 2. In the second extra unwind word, set bit 0 to 1 if the instruction should raise STORE_AMO on a read fault. Handle this in raise_mmu_exception, which would then need to perform the restore and exit itself. r~