From patchwork Sat Jun 19 03:43:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 463855 Delivered-To: patch@linaro.org Received: by 2002:a05:6638:102:0:0:0:0 with SMTP id x2csp534408jao; Fri, 18 Jun 2021 20:48:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwoPpCQvzo21ASsOUitaCVRRvCglE2XM+lBLT7i0zWBiraMxJscy4VLV/jG68/0qQFsPKbM X-Received: by 2002:a5d:83ce:: with SMTP id u14mr10609219ior.45.1624074487044; Fri, 18 Jun 2021 20:48:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624074487; cv=none; d=google.com; s=arc-20160816; b=etut9Q5nMdct23WjUtK0mPw24IDdAiHQ/xqKR74sEOtMLf7IyD0LdoUv2vItXzXQzR PdIH7JTJ8Fx2wRU7P0IpS0Uu8dWfRtmenptGy95Bti5GrRIPRI+jdUVv+t0X4iTkqwhL RUPGVfRALpwqcFaNaGeT+RMe/kNseqKgAsbm85ZRlbtHa61M9BzAzm/j05bWRY7xTLJA XvthKjAJWPhJnGZho/xDpgPsQBN+TGpqMx1hm5190n4jTLeK/K6RWa0WxyAvvYHUA06l G950Ogc/SxNaivoLBc0biLen1esx9ndH1qmx21Jo83Z8OHTs8vL2ZwyAert45loKK6Pb HDAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=+OXX5KjIYz/ZS9gLgWEfQeUt72TgJXhxPXE04khIgeU=; b=LcZyFwDZhS4/flgXZ9L0SjKkW7mmN9SlxZBChALVjWdzy6m1/xejttA6S+7aOSJm+m 0jXYUzq6FIOd+SZdEqs+AD9nXkX1wqSm99o0xcgGcHELBLAykGjNYI5LllFklehuGkhD HvoSW4o5jbd90SeJRWRHPml6CYnf3XyaRMSX0UiADkQzeyiPxL9oYUeLTMxgivQqfSNV 8rK4BVH+ZpbiFE1pmfl5gLKrxlzwWg0qoyrUkKTJ9MXLO2VXrkyN/FCP+92+TNy3O8g3 b0w+oTJraHSbkhmAPlet9+uYkegxvxsqRvA01AjipU5kEavxjQTp5QiVO8QbFnoIRhW5 sCCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="S/GO4BjO"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id x20si12921691iov.0.2021.06.18.20.48.06 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 18 Jun 2021 20:48:07 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="S/GO4BjO"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:37436 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luRxm-0001kj-E0 for patch@linaro.org; Fri, 18 Jun 2021 23:48:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33074) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtX-00008y-4T for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:44 -0400 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]:35551) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtN-00081H-FJ for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:42 -0400 Received: by mail-pg1-x536.google.com with SMTP id v7so9460941pgl.2 for ; Fri, 18 Jun 2021 20:43:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+OXX5KjIYz/ZS9gLgWEfQeUt72TgJXhxPXE04khIgeU=; b=S/GO4BjOsoDKWuxkTN3vkKjQvtSHxdJU+cp65gGSYdH8lpEeAV5c7cnIL1WjBP4wdx 5nsJCXf8LFwN+xhblY2eeqDKUp0awxxIUOL3Nl8QFq0E5kUC7CUbWdbOmx3+r6zTjnmq xE7WG1Meb/uLClwRX5aQElP/TXMLeq3L/KtcyF+aMNcIZmRcGjmkFah5ybjrS5bzKgSn pLzUvvrbF7XszymWpihKOsGvH3tPeTqgrffuvJc8xP+qi5/QqYqicUk+CxRj8QVX8IbD dEG1dng/6O9bp/MSh5ML4bL8mFOoQsuZyojezQdT2PfHdQK2QUVTVTgzQgci0Q3PBfuC k2Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+OXX5KjIYz/ZS9gLgWEfQeUt72TgJXhxPXE04khIgeU=; b=rHEJYOJDSM4QN514MkYLlvI49VA5XK6KDVvb7ig8PcQSGN1txmfKM02HigRkbohuUJ FNmPnyh5xqIRZ5JWjNOpvjbI1mMZUWl+INIavBuoxVePfztLr7gpJMFiVYSfVdapTxy3 hMUjc22fg2ZTnmxL/hDydNoUmxCYrNTesesuWN9irGwjiiCAfuEwwRqRwG3cs9FNO3dM FGiipcZph4HfbSNfMsuzHLHVIaJJMjQnob4CacX8iP+jfnsV45NFgjtLbbe3HIjFeO4r iHl+1VbvTTy+fnFb9MbUpzNNFWVtBkCp7d7HiufiMCxOvh6a5sbWz+liHL84wTph+h/Q NR3g== X-Gm-Message-State: AOAM531IJoxFI50f8jJma79HQN0+Rx7XYz3wqTBQZhAXaQVPO+6NEMI/ Tu26PuYSPHQ5L/Wy8m2ee3LrHqXWSuF+6A== X-Received: by 2002:aa7:962f:0:b029:2ed:cf:1f90 with SMTP id r15-20020aa7962f0000b02902ed00cf1f90mr8006496pfg.76.1624074211934; Fri, 18 Jun 2021 20:43:31 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:31 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 02/12] linux-user: Introduce imgsrc_read, imgsrc_read_alloc Date: Fri, 18 Jun 2021 20:43:19 -0700 Message-Id: <20210619034329.532318-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::536; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x536.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 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Introduced and initialized, but not yet really used. These will tidy the current tests vs BPRM_BUF_SIZE. Signed-off-by: Richard Henderson --- linux-user/qemu.h | 50 ++++++++++++++++++++++++++++++++++-------- linux-user/linuxload.c | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 9 deletions(-) -- 2.25.1 diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 9e5e2aa499..f4cdfb16b3 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -168,6 +168,37 @@ extern unsigned long mmap_min_addr; /* ??? See if we can avoid exposing so much of the loader internals. */ +typedef struct { + const void *cache; + unsigned int cache_size; + int fd; +} ImageSource; + +/** + * imgsrc_read: Read from ImageSource + * @dst: destination for read + * @offset: offset within file for read + * @len: size of the read + * @img: ImageSource to read from + * @errp: Error details. + * + * Read into @dst, using the cache when possible. + */ +bool imgsrc_read(void *dst, off_t offset, size_t len, + const ImageSource *img, Error **errp); + +/** + * imgsrc_read_alloc: Read from ImageSource + * @offset: offset within file for read + * @size: size of the read + * @img: ImageSource to read from + * @errp: Error details. + * + * Read into newly allocated memory, using the cache when possible. + */ +void *imgsrc_read_alloc(off_t offset, size_t len, + const ImageSource *img, Error **errp); + /* Read a good amount of data initially, to hopefully get all the program headers loaded. */ #define BPRM_BUF_SIZE 1024 @@ -177,15 +208,16 @@ extern unsigned long mmap_min_addr; * used when loading binaries. */ struct linux_binprm { - char buf[BPRM_BUF_SIZE] __attribute__((aligned)); - abi_ulong p; - int fd; - int e_uid, e_gid; - int argc, envc; - char **argv; - char **envp; - char * filename; /* Name of binary */ - int (*core_dump)(int, const CPUArchState *); /* coredump routine */ + char buf[BPRM_BUF_SIZE] __attribute__((aligned)); + ImageSource src; + abi_ulong p; + int fd; + int e_uid, e_gid; + int argc, envc; + char **argv; + char **envp; + char *filename; /* Name of binary */ + int (*core_dump)(int, const CPUArchState *); /* coredump routine */ }; typedef struct IOCTLEntry IOCTLEntry; diff --git a/linux-user/linuxload.c b/linux-user/linuxload.c index 9d4eb5e94b..3b0bafc490 100644 --- a/linux-user/linuxload.c +++ b/linux-user/linuxload.c @@ -2,6 +2,7 @@ #include "qemu/osdep.h" #include "qemu.h" +#include "qapi/error.h" #define NGROUPS 32 @@ -74,6 +75,10 @@ static int prepare_binprm(struct linux_binprm *bprm) /* Make sure the rest of the loader won't read garbage. */ memset(bprm->buf + retval, 0, BPRM_BUF_SIZE - retval); } + + bprm->src.cache = bprm->buf; + bprm->src.cache_size = retval; + return retval; } @@ -129,6 +134,7 @@ int loader_exec(int fdexec, const char *filename, char **argv, char **envp, int retval; bprm->fd = fdexec; + bprm->src.fd = fdexec; bprm->filename = (char *)filename; bprm->argc = count(argv); bprm->argv = argv; @@ -163,3 +169,43 @@ int loader_exec(int fdexec, const char *filename, char **argv, char **envp, return retval; } + +bool imgsrc_read(void *dst, off_t offset, size_t len, + const ImageSource *img, Error **errp) +{ + ssize_t ret; + + if (offset + len <= img->cache_size) { + memcpy(dst, img->cache + offset, len); + return true; + } + + if (img->fd < 0) { + error_setg(errp, "read past end of buffer"); + return false; + } + + ret = pread(img->fd, dst, len, offset); + if (ret == len) { + return true; + } + if (ret < 0) { + error_setg_errno(errp, errno, "Error reading file header"); + } else { + error_setg(errp, "Incomplete read of file header"); + } + return false; +} + +void *imgsrc_read_alloc(off_t offset, size_t len, + const ImageSource *img, Error **errp) +{ + void *alloc = g_malloc(len); + bool ok = imgsrc_read(alloc, offset, len, img, errp); + + if (!ok) { + g_free(alloc); + alloc = NULL; + } + return alloc; +}