Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp160292pxb; Wed, 22 Sep 2021 18:52:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxnA67FWntGVIRA/zIXxpW69+46jUoJ/vUxJBokZEFQedWly7G/YIkbVBYECNUMFaYyNQ55 X-Received: by 2002:a50:9dcf:: with SMTP id l15mr2794345edk.128.1632361946363; Wed, 22 Sep 2021 18:52:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632361946; cv=none; d=google.com; s=arc-20160816; b=sNVXTPn14c+0rghyYKXYllohnh9O/Bmk9120QMjbZtlZHaDpWNW3GIkkql518Ikmy6 GL+gf0Axh2tB6L9yNpb4zRGMw9iSoIc4Up2i+OKctnzy5snHu304H8p/OzZgdGpsDfTR 9/Z/OIjXTitd9u9OcKgo1g7aLLJwQz7b3RBuDfQumWfZjCzBbXwB6ypY8qcDDI5x4I6a 8oh2APpIeyeVBZWJwLYirttovQa1xItspvuQhrPKFnLhBlXZyStqMl0/tOkXOO4te4ZP zIAZNQBzG7WTpGaekfnLnsJE1tZbVhHcBUS5uKn6mbtlDOM8vNkvtd9SdaBAgI8hSE7L HgvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=vHchXl8mrU0hcMLr2GF0n33FKRG61T38fqwkdg1AY/Q=; b=xLQ9U5sJ6YnyNY8r9efyyCDBcwTPFgfzM8eLea8zUEPAwHGokjJY3BKjMdhYs/8yt4 76UVjdH42/jxp8wXK/IHtXcla0H0kzU/vwcH3bRzpz63xobFpqZ1dCuEu1xe+PUr6ynY FiNLqr6Kabor5OxZe44ArIsO2cPlmjn7NzTuf6zbK7QL8jVaikM1SbnHObk4noctQZjf RpkvTWBnDsebtbMuRWtztHlXC3PagowH5QprteMWci9OZzzFbqc4voJxQ38EU6iySr4b EQ6/ofLlio/xJBaHH2a5RIy+W+8SRHxZj91Z+otBH09lJqS+yL3AIKsrU/6dyCOjFUNI eaEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=iObpCcVf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q16si4503455ejy.322.2021.09.22.18.52.02; Wed, 22 Sep 2021 18:52:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=iObpCcVf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238841AbhIWBtn (ORCPT + 99 others); Wed, 22 Sep 2021 21:49:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238177AbhIWBtm (ORCPT ); Wed, 22 Sep 2021 21:49:42 -0400 Received: from mail-qv1-xf2d.google.com (mail-qv1-xf2d.google.com [IPv6:2607:f8b0:4864:20::f2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE017C061574; Wed, 22 Sep 2021 18:48:11 -0700 (PDT) Received: by mail-qv1-xf2d.google.com with SMTP id f2so3295850qvx.2; Wed, 22 Sep 2021 18:48:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vHchXl8mrU0hcMLr2GF0n33FKRG61T38fqwkdg1AY/Q=; b=iObpCcVfEXOR1qFpSmNg/dL3bBP7IDkI30qoSfwYqrmjrAOA/S7zkogrnLZMUN2kgG KEspLpaVXS8WFLxOjMwPwM3VF+S1fKUMgO7FsnihdnY8yr4VkevWYFFjdR04eICgk7jB PZLTv42hrFGGc2irzQ/LRbtwtbdCyoGAMufnl6nUdmlbe/MAvl5OzKUwCQTt1kWMOA6F /5g1HWDpM7IaNC196Sg6TvEys43fDTdEbMTWjVSosB/anzCMhZYsL7bD2uDdpkFOQwin xZQqTPI4u82z8pfdGDgeyjd8FJ25JNijINchot8wru0ULxm5RlbSDY8zMp6EsshlEMEI 3PDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vHchXl8mrU0hcMLr2GF0n33FKRG61T38fqwkdg1AY/Q=; b=aRVsqvgBYqrtp9G4OOcMWTJFYjbP1FM+EwgqNpBpXO0bPZWPrgi6oDpnueTX0eRBjx itdkYjub1nIV8yrLiippeSqzjZcNr4Z7QewWqFZim3O78v53gJcgigPvuAY0RuqJHoUU Gn3v47EmWoftaIZaE7/oGGbJguz49XofwVhVd9gH4vscm1JK2f2z/RYLfsMajeaGZKCm WitA6WbGDuJ1Eg1WW0qjGney27PPykzCX+nVW42CWqhrty+xSMhlfq41aL3bcEbAI29g WB/CXlV9VQzjOvYK0s5sVsyXF087Dwb4OanP65yp98JFAE7+1mH55X4+OFzCgmlTiWSF 7nsA== X-Gm-Message-State: AOAM530qMlM8MSmpSNvh3nmYrQI27tx6rvycdx3tXEAt0gTqQt/AuWdb teyL01f1Wqm89OZSADOmBhzQS19LrYGLOhTSO8g= X-Received: by 2002:a05:6214:584:: with SMTP id bx4mr2261885qvb.40.1632361691038; Wed, 22 Sep 2021 18:48:11 -0700 (PDT) MIME-Version: 1.0 References: <1631092255-25150-1-git-send-email-shengjiu.wang@nxp.com> <1631092255-25150-4-git-send-email-shengjiu.wang@nxp.com> <20210915161624.GA1770838@p14s> <20210916165957.GA1825273@p14s> <20210917152236.GA1878943@p14s> <20210922175521.GA2157824@p14s> In-Reply-To: <20210922175521.GA2157824@p14s> From: Shengjiu Wang Date: Thu, 23 Sep 2021 09:48:00 +0800 Message-ID: Subject: Re: [PATCH v4 3/4] remoteproc: imx_dsp_rproc: Add remoteproc driver for DSP on i.MX To: Mathieu Poirier Cc: Shengjiu Wang , Ohad Ben Cohen , Bjorn Andersson , Rob Herring , Shawn Guo , Sascha Hauer , Sascha Hauer , Fabio Estevam , Daniel Baluta , NXP Linux Team , "open list:REMOTE PROCESSOR (REMOTEPROC) SUBSYSTEM" , "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS" , "moderated list:ARM/FREESCALE IMX / MXC ARM ARCHITECTURE" , linux-kernel Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 23, 2021 at 1:55 AM Mathieu Poirier wrote: > > On Wed, Sep 22, 2021 at 09:35:54AM +0800, Shengjiu Wang wrote: > > Hi Mathieu > > > > On Fri, Sep 17, 2021 at 11:22 PM Mathieu Poirier > > wrote: > > > > > > On Fri, Sep 17, 2021 at 05:44:44PM +0800, Shengjiu Wang wrote: > > > > On Fri, Sep 17, 2021 at 1:20 PM Shengjiu Wang wrote: > > > > > > > > > > On Fri, Sep 17, 2021 at 1:00 AM Mathieu Poirier > > > > > wrote: > > > > > > > > > > > > [...] > > > > > > > > > > > > > > > + > > > > > > > > > +/** > > > > > > > > > + * imx_dsp_rproc_elf_load_segments() - load firmware segments to memory > > > > > > > > > + * @rproc: remote processor which will be booted using these fw segments > > > > > > > > > + * @fw: the ELF firmware image > > > > > > > > > + * > > > > > > > > > + * This function specially checks if memsz is zero or not, otherwise it > > > > > > > > > + * is mostly same as rproc_elf_load_segments(). > > > > > > > > > + */ > > > > > > > > > +static int imx_dsp_rproc_elf_load_segments(struct rproc *rproc, > > > > > > > > > + const struct firmware *fw) > > > > > > > > > +{ > > > > > > > > > + struct device *dev = &rproc->dev; > > > > > > > > > + u8 class = fw_elf_get_class(fw); > > > > > > > > > + u32 elf_phdr_get_size = elf_size_of_phdr(class); > > > > > > > > > + const u8 *elf_data = fw->data; > > > > > > > > > + const void *ehdr, *phdr; > > > > > > > > > + int i, ret = 0; > > > > > > > > > + u16 phnum; > > > > > > > > > + > > > > > > > > > + ehdr = elf_data; > > > > > > > > > + phnum = elf_hdr_get_e_phnum(class, ehdr); > > > > > > > > > + phdr = elf_data + elf_hdr_get_e_phoff(class, ehdr); > > > > > > > > > + > > > > > > > > > + /* go through the available ELF segments */ > > > > > > > > > + for (i = 0; i < phnum; i++, phdr += elf_phdr_get_size) { > > > > > > > > > + u64 da = elf_phdr_get_p_paddr(class, phdr); > > > > > > > > > + u64 memsz = elf_phdr_get_p_memsz(class, phdr); > > > > > > > > > + u64 filesz = elf_phdr_get_p_filesz(class, phdr); > > > > > > > > > + u64 offset = elf_phdr_get_p_offset(class, phdr); > > > > > > > > > + u32 type = elf_phdr_get_p_type(class, phdr); > > > > > > > > > + void *ptr; > > > > > > > > > + bool is_iomem; > > > > > > > > > + > > > > > > > > > + if (type != PT_LOAD || !memsz) > > > > > > > > > > > > > > > > You did a really good job with adding comments but this part is undocumented... > > > > > > > > If I read this correctly you need to check for !memsz because some part of > > > > > > > > the program segment may have a header but its memsz is zero, in which case it can > > > > > > > > be safely skipped. So why is that segment in the image to start with, and why > > > > > > > > is it marked PT_LOAD if it is not needed? This is very puzzling... > > > > > > > > > > > > > > Actually I have added comments in the header of this function. > > > > > > > > > > > > Indeed there is a mention of memsz in the function's header but it doesn't > > > > > > mention _why_ this is needed, and that is what I'm looking for. > > > > > > > > > > > > > > > > > > > > memsz= 0 with PT_LOAD issue, I have asked the toolchain's vendor, > > > > > > > they said that this case is allowed by elf spec... > > > > > > > > > > > > > > And in the "pru_rproc.c" and "mtk_scp.c", seems they met same problem > > > > > > > they also check the filesz in their internal xxx_elf_load_segments() function. > > > > > > > > > > > > In both cases they are skipping PT_LOAD sections where "filesz" is '0', which > > > > > > makes sense because we don't know how many bytes to copy. But here you are > > > > > > skipping over a PT_LOAD section with a potentially valid filesz, and that is the > > > > > > part I don't understand. > > > > > > > > > > Ok, I can use filesz instead. For my case, filesz = memsz = 0, > > > > > it is the same result I want. > > > > > > If that is the case then rproc_elf_load_segments() should work, i.e it won't > > > copy anything. If rproc_elf_load_segments() doesn't work for you then there are > > > corner cases you haven't told me about. > > > > > > > > > > > > > The reason why I use "memsz '' is because there is "if (filesz > memsz) " > > > > > check after this, if memsz is zero, then "filesz" should be zero too, other > > > > > values are not allowed. > > > > > > > > But I still think checking "!memsz" is better than filesz, because > > > > memsz > filesz is allowed (filesz = 0), the code below can be executed. > > > > filesz > memsz is not allowed. > > The question remains the same - have you seen instances where memsz > filesz? > Also, can you point me to the reference where it is said that memsz is allowed? > And if it is allowed than how do we know that this program section has valid > data, because after all, filesz is 0? https://refspecs.linuxbase.org/elf/elf.pdf This is the specification. page 40, p_filesz and p_memsz can be zero. p_filesz This member gives the number of bytes in the file image of the segment; it may be zero. p_memsz This member gives the number of bytes in the memory image of the segment; it may be zero. And page 41, p_memsz can > p_filesz. PT_LOAD The array element specifies a loadable segment, described by p_filesz and p_memsz. The bytes from the file are mapped to the beginning of the memory segment. If the segment's memory size (p_memsz) is larger than the file size (p_filesz), the "extra'' bytes are defined to hold the value 0 and to follow the segment's initialized area. The file size may not be larger than the memory size. Loadable segment entries in the program header table appear in ascending order, sorted on the p_vaddr member best regards wang shengjiu > > > > > > > > > What do you think? > > > > > > I don't see a need to add a custom implementation for things that _may_ happen. > > > If using the default rproc_elf_load_segments() works than go with that. We can deal > > > with problems if/when there is a need for it. > > > > > > > The default rproc_elf_load_segments() with filesz = memsz = 0, then the > > rproc_da_to_va() return ptr=NULL, then rproc_elf_load_segments() will return > > with error. So this is the reason to add a custom implementation. > > Ok, I see about rproc_da_to_va() returning NULL and failing everything from > there one. > > > > > best regards > > wang shengjiu