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 phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7A05CC77B75 for ; Fri, 12 May 2023 22:57:35 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E9949861F6; Sat, 13 May 2023 00:57:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="fstrGdjx"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 28650861F9; Sat, 13 May 2023 00:57:31 +0200 (CEST) Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7946985D10 for ; Sat, 13 May 2023 00:57:28 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ganboing@gmail.com Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-6438d95f447so7421545b3a.3 for ; Fri, 12 May 2023 15:57:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683932247; x=1686524247; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:from:references:cc:to:subject:from:to:cc :subject:date:message-id:reply-to; bh=0nLp1y6EqwItqAd12BC2834kBUoeEP9ndDoil9fPzKg=; b=fstrGdjxmEB06UTqtRQ035Btd/mLzFKwQItMuvx6IUBVkojQpDFKZm6rlYgcZlIUSN s7bl1PvP3TN7DqPdpWXX4jWRraoTkyhh3C/RncYBrLJoXrMvp0P7NKHZPDJRKxSwROIK uHjgHQNmmwIvRwiWZhQ/Tb7nsbOeP2/coCAhqh2vDVnCZWWap6arz2lgR2eC1+S+LUqS EbXJpAzGrnW5qsPZ0A+NVl7xniTSs9IcRVDJ70xaX0FLXO1H5bTGDpNU6KiF9Ci0Z3sh b1pYF1l6ypcLKigOAu1O0Yq4Okv/wxPdm5Xt6gF1gkXnmYQBZ6epSXZT7TZ/5mn1LhwC RQiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683932247; x=1686524247; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:from:references:cc:to:subject :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0nLp1y6EqwItqAd12BC2834kBUoeEP9ndDoil9fPzKg=; b=N0aM8wsgGJDQ8KlrWABGwgLAye2vKkuBkPbpxtk0WYwgXr8ueBFC0DDLLajej1A9Xe SeK4jqIc6GSaMPId5K+ExDseyB4w/e38nu8bgEYMOlw2OEvyWczIVzu1RnFWuxI5PGHz 8ATCWSLEv78jx0oenBsh/4P5h4TMk1BkduwpczNI+3Yo5PTdFkZjXH0KEp9ZvXFEtaiv p+vxI+fXd6hM5dE+eapjkTvh3testxD9s7V0pYPGOWN/P38gI+wa0nPLZnKIzEnbMzU1 2YzhvyNYaOWffg8i7Y464sKNj4Au0VsxJK5RGp3eB6Zi8GLaorIa0Pwwx6nLjI+69EJL +LmA== X-Gm-Message-State: AC+VfDxrPo9ODxwhUhnJUldHh/AfrJzmaybrNkvjByL5cwypR+aG0lpm wkWScaJ7nQQ7z4+DB5ZbB6ypQNGBPg1hHg== X-Google-Smtp-Source: ACHHUZ6bA5UMU31irIxFMklwSyPB0JYxZ2UKRG7WxiyPA2oR3eTwVtKDORLhuWk/Bqd3E0Jxp/sJmA== X-Received: by 2002:a05:6a00:198a:b0:643:791b:892d with SMTP id d10-20020a056a00198a00b00643791b892dmr32564788pfl.18.1683932246775; Fri, 12 May 2023 15:57:26 -0700 (PDT) Received: from [192.168.0.13] ([172.92.174.136]) by smtp.gmail.com with ESMTPSA id k3-20020aa790c3000000b0063d46ec5777sm7529950pfk.158.2023.05.12.15.57.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 12 May 2023 15:57:26 -0700 (PDT) Subject: Re: [PATCH v3 03/11] eeprom: starfive: Enable ID EEPROM configuration To: Yanhong Wang , u-boot@lists.denx.de, Rick Chen , Leo , Joe Hershberger , Ramon Fried Cc: Torsten Duwe , "samin . guo" , Leyfoon Tan , Walker Chen References: <20230428022515.29393-1-yanhong.wang@starfivetech.com> <20230428022515.29393-4-yanhong.wang@starfivetech.com> From: Bo Gan Message-ID: Date: Fri, 12 May 2023 15:57:25 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20230428022515.29393-4-yanhong.wang@starfivetech.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean On 4/27/23 7:25 PM, Yanhong Wang wrote: > +struct starfive_eeprom { > + struct eeprom_header header; > + struct starfive_eeprom_atom1 atom1; > + struct starfive_eeprom_atom4 atom4; > +}; > + > +static uchar eeprom_buf[STARFIVE_EEPROM_HATS_SIZE_MAX]; > + > +struct starfive_eeprom *pbuf = (struct starfive_eeprom *)eeprom_buf; > + > +/* Set to 1 if we've read EEPROM into memory */ > +static int has_been_read; > + `pbuf` is only used as a const pointer variable, so why not just define pbuf as the buffer? Something like: static union { struct starfive_eeprom { struct eeprom_header header; struct starfive_eeprom_atom1 atom1; struct starfive_eeprom_atom4 atom4; }; uchar eeprom_buf[STARFIVE_EEPROM_HATS_SIZE_MAX]; } pbuf; Also all global variables in this file should have __section(".data") attribute. This is because these uninitialized variables will by default get linked in .bss section. When calling this function early, before .bss gets initialized, we could get random data, or corrupt data that's not yet relocated from .bss region. This problem is more obvious when we have CONFIG_OF_SEPARATE=y. When set, device-tree is expected to get loaded by previous stage, and it's expected to be loaded after _end, which is before __bss_start. In general, we should not touch anything in .bss (either read/write) before `board_init_f` completes. I'm pretty confident on this because I've got a VF2 4GB board, and I hacked opensbi/u-boot to detect this kind of issue using PMP. I was using https://github.com/starfive-tech/u-boot/tree/JH7110_VisionFive2_devel. The eeprom implementation there were roughly the same as this patch. I was able to observe PMP access violation fault when I only allow R/X permission to u-boot image region and R/W to u-boot stack. The fault was originating from https://github.com/starfive-tech/u-boot/blob/JH7110_VisionFive2_devel/arch/riscv/cpu/jh7110/dram.c#L29 Although this patch is different than the github version, I still think we must avoid the same kind of problem. Many other board/drivers are already doing __section(".data").