From patchwork Tue Jul 6 23:48:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 470596 Delivered-To: patch@linaro.org Received: by 2002:a17:906:971a:0:0:0:0 with SMTP id k26csp5548438ejx; Tue, 6 Jul 2021 16:57:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxetwR0h2ozZOqF+ZsXq5aBagllcODYZCCw92JxVQMrA3qPzcn1vhXALVewXYam041pX2xF X-Received: by 2002:ac8:4258:: with SMTP id r24mr1072471qtm.11.1625615857384; Tue, 06 Jul 2021 16:57:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625615857; cv=none; d=google.com; s=arc-20160816; b=0HiYqK1KC7zW7aWDWA2iQMTdi4TDKkHEmmIAED9TD4NdWH42tWtkwv6jRGtqhaDBht 86lnp22zI9HvGCE89ZokecfSCTmQZ5qdrb3GDK2gjJQ1JcfA2WfwzGzBnPcFR+CTwWO8 YolwPaW8LidC2bZcg+MtAZ8QCc6xF2KIKDKt+uckWbzWm5Mn37efpVz/IrQmAfZfCrCI 9K12Ee9IH/N5Ar2isTDnCwI5WAm0ZKoKZfw+d9KX6BIOwA6kXQmoXPOmlrh0vqQYoZoF LdwqZCbnUeiSYKQFFwEMm82FutDhNWuVlOgP32UsB+Ju2FtIlrX+Ej5+46H5fnjmerJZ qEvw== 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=sI1gb2RZ4LF5vZHmG5bQUBKdhL05N+vy/QQy5NL+Fw9Q+F40GXZB+0lHBwlaGn1GWf 6Pgsx8LkLmBTPKgCUaPuclsyH/oNdx/vnY951Hs+vLll2f5cOFXVXOIy3i88I5ZDLhym SoLG5+u52daBqeoXIh70FLWEMlmT+opTvwn2zWEyEl1CPvJaFyN/pRKdkWoGeYRxux1N oGQB1HjLzjsfCi9d3e7IK2Sh2VavczVE0n550Fvn4Em33RvznstGvsJH2sjdSSgQC5K+ tknAcxrBVl0dD967FLABrcbeaXLeR54nViZBhmmxTQTGlXiI3hw5djUtgiGW5Kk/gkES bfhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=YEhACm19; 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 o15si10242304qtv.261.2021.07.06.16.57.37 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 06 Jul 2021 16:57:37 -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=YEhACm19; 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]:33168 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m0uwa-0000fN-L1 for patch@linaro.org; Tue, 06 Jul 2021 19:57:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52898) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m0uou-0000lW-Pc for qemu-devel@nongnu.org; Tue, 06 Jul 2021 19:49:40 -0400 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]:37715) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m0uoq-0006b5-IG for qemu-devel@nongnu.org; Tue, 06 Jul 2021 19:49:40 -0400 Received: by mail-pg1-x52f.google.com with SMTP id t9so411063pgn.4 for ; Tue, 06 Jul 2021 16:49:36 -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=YEhACm19xCJeHS8mYi60ZnutuviMKuddF9tGq7NpARJetL0j+iIbFDKxcxRL49NfnW YGEVe0ObyBlvpeAKRjNGQ19wm02CFo8sfFJ0BDCi2KvXZZ0u8qp5g81xA18V8F3WKvOO I7Z+j6y3rxdiBihUu4q84Oye3wYm4UEgwRpRdnnwfmqIt+8q+Tfub+xRAIQDnTSV8HW1 1fF45x0Km+bEeFG3nULFe8nR15PgWqiX81IXGVliq4Wf82fZ93KK/bpIaKqgyG+ogb3X I5vD0EI6J90BvY7ZxB76WjNWvTAtr10kgQDn8HsD7qmsetMGXGalHckAYybQZMc7gckc KU1w== 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=XmXISCASqOCxLR4A8C2X2hNnoNM0hL1YoBSkKJyEyITIJZYtzewGPYP4XtPCL0/NDc kCPClFpNUTVzKBnzyIEHEbc+8eB+m3wqZVh1mriLWQp7Ffu4G1cdLNuPulbu8MnLk6F9 Wi9kCi4sGeZw8bSx4i1huRoNqNCfO0OKFBfMZu5tIstpNDp75/IfxB2oiffbZEWABcBE M+oPJF9plyMtwTWtKO59pBKj+RHeBSgF3ekqw7IGAJVehF/9GOFmeaoDV0kiMpSTz+WE xSifhs5a1SY04TMEQl4y1AjFqmSEatYEaRgYUgGwz1BO4ENARhbSl9LXGINOfS3klYGX rRkw== X-Gm-Message-State: AOAM532nlHHf5YITF6Z7UrAYmTaZUOeBRj8t5DX5mnoKcQ6Uoeqj/OAc 2tIX3h2TjLFMKa+fS+BJoIoS+Rp5BugPZw== X-Received: by 2002:a63:1c20:: with SMTP id c32mr23124348pgc.41.1625615375305; Tue, 06 Jul 2021 16:49:35 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id b3sm18058114pfi.179.2021.07.06.16.49.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jul 2021 16:49:35 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 03/36] linux-user: Introduce imgsrc_read, imgsrc_read_alloc Date: Tue, 6 Jul 2021 16:48:59 -0700 Message-Id: <20210706234932.356913-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210706234932.356913-1-richard.henderson@linaro.org> References: <20210706234932.356913-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52f; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x52f.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; +}