From nobody Wed Oct 30 19:55:50 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1694797355; cv=none; d=zohomail.com; s=zohoarc; b=d/ZBbWPURl492rfDUNqdRDDaDAlaoaUTC7B1meGfy6CTcU0dWhVkfSsUQtzzYyZvF+MNqkraUGLa5zonJ9uLfPC9kAGpwAzZbFtjfcdOrKuTR2Qw1c5M1yYQarjBzunr0oKgpLxhKagY9vw7DihRF0GYTrobzmluVjJ07wLF5xc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694797355; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=8ftC+g7kcyTDI9qRSERzH52xT8C77XKKARe67AM6V0M=; b=CANCocc+eIMH+27Bb2iwi8p1XrgzRZbz0XFINYW4JqdA5oH35HUpfeU6rZleJAqfTwJMcsZSzkBkvSJUK+2EpUbB9dtDNzKWtpAoKPm+d/j/gF8acdlUDhZYz+oU2WEQzuPDtx7BxPsCLcg8JLjbhdMiZUGxdRU4JOLaSejMZS0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1694797355153757.9597870449958; Fri, 15 Sep 2023 10:02:35 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qhCCp-0003Ne-CW; Fri, 15 Sep 2023 13:02:11 -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 1qhCCn-0003MO-Ot for qemu-devel@nongnu.org; Fri, 15 Sep 2023 13:02:09 -0400 Received: from mail-lf1-x133.google.com ([2a00:1450:4864:20::133]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qhCCl-0000L3-6m for qemu-devel@nongnu.org; Fri, 15 Sep 2023 13:02:09 -0400 Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-5007abb15e9so4055813e87.0 for ; Fri, 15 Sep 2023 10:02:06 -0700 (PDT) Received: from localhost.localdomain (109-252-90-8.nat.spd-mgts.ru. [109.252.90.8]) by smtp.gmail.com with ESMTPSA id u7-20020a056512040700b005009ab1992esm690431lfk.154.2023.09.15.10.02.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 10:02:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1694797324; x=1695402124; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8ftC+g7kcyTDI9qRSERzH52xT8C77XKKARe67AM6V0M=; b=uPcL4daw0NtmYsNCmseAgVmDHHINPIcswR/ET+nD+qfTha0MaTRH1PUHkgutbGnt6q KfKSzMz42MupfYAKbG3pbuYwmoNBSCM3LDighHczwy/tWC9K4Yf3Gqvh/sRXoo/OTZck i4UVlB086t8MDbehx3/1ii0qkHJCfvol1szOVnQ6fWmQ3RjZnwhyaVb7BOph7QAqENSd LR4dIVBGtPUpdg0QfG55EWycQ+DXGcR9MhQv4C9B5FRQurfR1dD7wJFbecXzSCiBsS0e +QdC3bdC4K6Dml9y6bupmN0+1LM52zy/sH+Z/Z0klXqdnw0L7OwWMgxmSrP6rtTUg5pu GzrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694797324; x=1695402124; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8ftC+g7kcyTDI9qRSERzH52xT8C77XKKARe67AM6V0M=; b=OiyXuGa3GFECfrJUvao4AhdmRd4C06qq/4K+NWv3H8XBU0AZw3Ngbiv+Ynduik9bV9 CTPshXwkM3DMcrISXHAL+MUGi2zf1SuAYttvd0S10F7g1sP+X9CSmEgq3psFu34rkwKa Rq5BS6tFmkDa5oQRzyNe80WuIFrQk7tqSmSbYJMbk+1b6En/B82e2GpsaVwSF9Z5OZr+ 1vEIE36lPsguq4XrPfVHVikSdWjLFNNBjVO3DcUyAvrOohba/6L+AiPAgrd5vVpq6vri GZwV8EjC6+rOblWwpLrxIhWgDhFtSNI+mMe9u2JBjQ/1GkBerhqTOJlCgPDGyQXmRfU6 awcg== X-Gm-Message-State: AOJu0YzZssXQQ5c8Q9rWOam3+gokoDSLdci++r8GtYNQfeyAwcsaoNX7 Zty3OQPDSxWGHjpZYnS3G140tA== X-Google-Smtp-Source: AGHT+IHHUH/R1AiMDQc71t7Sn0+4SjrOwqcNDWeUlgsS+TTyVMrgZxV4RFJjUfrPifRKb2U/2YTERQ== X-Received: by 2002:a05:6512:234f:b0:501:b97a:9f50 with SMTP id p15-20020a056512234f00b00501b97a9f50mr2842570lfu.65.1694797324119; Fri, 15 Sep 2023 10:02:04 -0700 (PDT) From: Viktor Prutyanov To: annie.li@oracle.com, akihiko.odaki@daynix.com, kkostiuk@redhat.com Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, yan@daynix.com, viktor@daynix.com, viktor.prutyanov@phystech.edu Subject: [PATCH v2 1/5] elf2dmp: replace PE export name check with PDB name check Date: Fri, 15 Sep 2023 20:01:49 +0300 Message-Id: <20230915170153.10959-2-viktor@daynix.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230915170153.10959-1-viktor@daynix.com> References: <20230915170153.10959-1-viktor@daynix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2a00:1450:4864:20::133; envelope-from=viktor@daynix.com; helo=mail-lf1-x133.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1694797356838100003 Content-Type: text/plain; charset="utf-8" PE export name check introduced in d399d6b179 isn't reliable enough, because a page with the export directory may be not present for some reason. On the other hand, elf2dmp retrieves the PDB name in any case. It can be also used to check that a PE image is the kernel image. So, check PDB name when searching for Windows kernel image. Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=3D2165917 Signed-off-by: Viktor Prutyanov Reviewed-by: Akihiko Odaki --- contrib/elf2dmp/main.c | 93 +++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 60 deletions(-) diff --git a/contrib/elf2dmp/main.c b/contrib/elf2dmp/main.c index 6d4d18501a..bb6744c0cd 100644 --- a/contrib/elf2dmp/main.c +++ b/contrib/elf2dmp/main.c @@ -411,89 +411,64 @@ static int write_dump(struct pa_space *ps, return fclose(dmp_file); } =20 -static bool pe_check_export_name(uint64_t base, void *start_addr, - struct va_space *vs) -{ - IMAGE_EXPORT_DIRECTORY export_dir; - const char *pe_name; - - if (pe_get_data_dir_entry(base, start_addr, IMAGE_FILE_EXPORT_DIRECTOR= Y, - &export_dir, sizeof(export_dir), vs)) { - return false; - } - - pe_name =3D va_space_resolve(vs, base + export_dir.Name); - if (!pe_name) { - return false; - } - - return !strcmp(pe_name, PE_NAME); -} - -static int pe_get_pdb_symstore_hash(uint64_t base, void *start_addr, - char *hash, struct va_space *vs) +static bool pe_check_pdb_name(uint64_t base, void *start_addr, + struct va_space *vs, OMFSignatureRSDS *rsds) { const char sign_rsds[4] =3D "RSDS"; IMAGE_DEBUG_DIRECTORY debug_dir; - OMFSignatureRSDS rsds; - char *pdb_name; - size_t pdb_name_sz; - size_t i; + char pdb_name[sizeof(PDB_NAME)]; =20 if (pe_get_data_dir_entry(base, start_addr, IMAGE_FILE_DEBUG_DIRECTORY, &debug_dir, sizeof(debug_dir), vs)) { eprintf("Failed to get Debug Directory\n"); - return 1; + return false; } =20 if (debug_dir.Type !=3D IMAGE_DEBUG_TYPE_CODEVIEW) { - return 1; + eprintf("Debug Directory type is not CodeView\n"); + return false; } =20 if (va_space_rw(vs, base + debug_dir.AddressOfRawData, - &rsds, sizeof(rsds), 0)) { - return 1; + rsds, sizeof(*rsds), 0)) { + eprintf("Failed to resolve OMFSignatureRSDS\n"); + return false; } =20 - printf("CodeView signature is \'%.4s\'\n", rsds.Signature); - - if (memcmp(&rsds.Signature, sign_rsds, sizeof(sign_rsds))) { - return 1; + if (memcmp(&rsds->Signature, sign_rsds, sizeof(sign_rsds))) { + eprintf("CodeView signature is \'%.4s\', \'%s\' expected\n", + rsds->Signature, sign_rsds); + return false; } =20 - pdb_name_sz =3D debug_dir.SizeOfData - sizeof(rsds); - pdb_name =3D malloc(pdb_name_sz); - if (!pdb_name) { - return 1; + if (debug_dir.SizeOfData - sizeof(*rsds) !=3D sizeof(PDB_NAME)) { + eprintf("PDB name size doesn't match\n"); + return false; } =20 if (va_space_rw(vs, base + debug_dir.AddressOfRawData + - offsetof(OMFSignatureRSDS, name), pdb_name, pdb_name_sz, 0= )) { - free(pdb_name); - return 1; + offsetof(OMFSignatureRSDS, name), pdb_name, sizeof(PDB_NAM= E), + 0)) { + eprintf("Failed to resolve PDB name\n"); + return false; } =20 printf("PDB name is \'%s\', \'%s\' expected\n", pdb_name, PDB_NAME); =20 - if (strcmp(pdb_name, PDB_NAME)) { - eprintf("Unexpected PDB name, it seems the kernel isn't found\n"); - free(pdb_name); - return 1; - } - - free(pdb_name); + return !strcmp(pdb_name, PDB_NAME); +} =20 - sprintf(hash, "%.08x%.04x%.04x%.02x%.02x", rsds.guid.a, rsds.guid.b, - rsds.guid.c, rsds.guid.d[0], rsds.guid.d[1]); +static void pe_get_pdb_symstore_hash(OMFSignatureRSDS *rsds, char *hash) +{ + sprintf(hash, "%.08x%.04x%.04x%.02x%.02x", rsds->guid.a, rsds->guid.b, + rsds->guid.c, rsds->guid.d[0], rsds->guid.d[1]); hash +=3D 20; - for (i =3D 0; i < 6; i++, hash +=3D 2) { - sprintf(hash, "%.02x", rsds.guid.e[i]); + for (unsigned int i =3D 0; i < 6; i++, hash +=3D 2) { + sprintf(hash, "%.02x", rsds->guid.e[i]); } =20 - sprintf(hash, "%.01x", rsds.age); - - return 0; + sprintf(hash, "%.01x", rsds->age); } =20 int main(int argc, char *argv[]) @@ -515,6 +490,7 @@ int main(int argc, char *argv[]) KDDEBUGGER_DATA64 *kdbg; uint64_t KdVersionBlock; bool kernel_found =3D false; + OMFSignatureRSDS rsds; =20 if (argc !=3D 3) { eprintf("usage:\n\t%s elf_file dmp_file\n", argv[0]); @@ -562,7 +538,8 @@ int main(int argc, char *argv[]) } =20 if (*(uint16_t *)nt_start_addr =3D=3D 0x5a4d) { /* MZ */ - if (pe_check_export_name(KernBase, nt_start_addr, &vs)) { + printf("Checking candidate KernBase =3D 0x%016"PRIx64"\n", Ker= nBase); + if (pe_check_pdb_name(KernBase, nt_start_addr, &vs, &rsds)) { kernel_found =3D true; break; } @@ -578,11 +555,7 @@ int main(int argc, char *argv[]) printf("KernBase =3D 0x%016"PRIx64", signature is \'%.2s\'\n", KernBas= e, (char *)nt_start_addr); =20 - if (pe_get_pdb_symstore_hash(KernBase, nt_start_addr, pdb_hash, &vs)) { - eprintf("Failed to get PDB symbol store hash\n"); - err =3D 1; - goto out_ps; - } + pe_get_pdb_symstore_hash(&rsds, pdb_hash); =20 sprintf(pdb_url, "%s%s/%s/%s", SYM_URL_BASE, PDB_NAME, pdb_hash, PDB_N= AME); printf("PDB URL is %s\n", pdb_url); --=20 2.21.0 From nobody Wed Oct 30 19:55:50 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1694797358; cv=none; d=zohomail.com; s=zohoarc; b=JIyQ9lx3Euq9yuFW6torJ/+iwx5zbW0JRXc1pzOMjareOytZ98hbrDhsspxWhG/zfBhIJKBDfLdkM4Qmd4EovcqsncfNXjk7ZN7ub1YaHMufWOdHEthuBrEGsJ22bBr+H5YZ1UNkhSaKyLpUTScFJL2hqsqkwp/wKO1TQ83+4CI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694797358; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=sRDq0Frl7ofiI6ZyF6ZdAHnr6ekU2KaX/hYSpVHMAzM=; b=Ewc51KuPqoZRoZUUfTkGF6GH+nVsGFiruJfHofcI1TCBl82Gi2kztpF8nKEHfkk/erOrGgDrY301wODq2RNVLsH5nEOY9U0LreS/l7gcdmHu0l6JIjYh3p5ttI/i/BpYJiZFmrsB8zYwfZcQCDq0+Va8JnSE+D7pXVREkMuGaGI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1694797358152312.2356094988397; Fri, 15 Sep 2023 10:02:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qhCCn-0003Mb-VE; Fri, 15 Sep 2023 13:02:09 -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 1qhCCm-0003K0-9I for qemu-devel@nongnu.org; Fri, 15 Sep 2023 13:02:08 -0400 Received: from mail-lf1-x133.google.com ([2a00:1450:4864:20::133]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qhCCk-0000L7-Kg for qemu-devel@nongnu.org; Fri, 15 Sep 2023 13:02:08 -0400 Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-501be2d45e0so3997134e87.3 for ; Fri, 15 Sep 2023 10:02:06 -0700 (PDT) Received: from localhost.localdomain (109-252-90-8.nat.spd-mgts.ru. [109.252.90.8]) by smtp.gmail.com with ESMTPSA id u7-20020a056512040700b005009ab1992esm690431lfk.154.2023.09.15.10.02.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 10:02:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1694797325; x=1695402125; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sRDq0Frl7ofiI6ZyF6ZdAHnr6ekU2KaX/hYSpVHMAzM=; b=Z04TlJ5kRjuDkgbzhnMZ78YBzuuGpjhcWz67uZXup3hEHKKI7M8Ntnt45Hipf0xjiO rC5tapE6y5wbJlOyXXD45qYe19ICe68fs1JxNJD7NpaDpPt74uaqEGo0TG/c9H/Xv+EG kbPeoivsF9G7n4DgrCN4nRihc1AgSwgU0NZDdOCafXhQl6CwUlPimg3YTVf3zSild3WV VLcMUDKqNCQQSKtnA7fEhXrekpfxdFOsPvKtCer7knVLIXqkb4Du5vTcW/XSEBT1ebaI d3FrSi03vIp37FCNKLUmr6PWnDC9P/WWsMA6TvdK2K9dcgLppyVcrc7Rd+rJhhNPNqdZ kguw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694797325; x=1695402125; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sRDq0Frl7ofiI6ZyF6ZdAHnr6ekU2KaX/hYSpVHMAzM=; b=SUxNL2tEQ0QW0uKDiHbz5g+/GmOUNfAtFCrdodRPS2B/RjVyw6CR6ripBEUgT1s8uv pNdsj9sRFcMClRs97BW+aXSFspNlvgvjtR13vmLPPLzB3ygh4H0qczhWW5pThQbTTCWG RJiNo9QyhX+75wRBz73m0mztHIW4wBcUgALjqa/s/ceyryF6ues3wSnBCTa1eQsZPdll fxkpHEa5kHysKc0cElU7Xo6Sn1PlXmsxhvhgjfJsJ/neM80DFhA2P+6yM12fYlMWQhWo P/ccT/IrWViuedXoRgcVTQo3eNgtuLLmZo0bGNm1WXmpkQ9xaj42Rx3EJE4jYP2YI2VF 6m9Q== X-Gm-Message-State: AOJu0YynhU1NmCnI9kKSlJVk56gS/L0HJk8t8mnJFijretGsqowvq46w jBgO3S1y4lbmrosjKrbsPbYDHQ== X-Google-Smtp-Source: AGHT+IFKUOufH7SUKZ6gGvJ6klAtzbt//pQHo0fmJdXwRV3M8cHJuhWNEYOIv5x8D20fdDmcuzzaJQ== X-Received: by 2002:a19:7113:0:b0:4f8:6abe:5249 with SMTP id m19-20020a197113000000b004f86abe5249mr1700668lfc.3.1694797325233; Fri, 15 Sep 2023 10:02:05 -0700 (PDT) From: Viktor Prutyanov To: annie.li@oracle.com, akihiko.odaki@daynix.com, kkostiuk@redhat.com Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, yan@daynix.com, viktor@daynix.com, viktor.prutyanov@phystech.edu Subject: [PATCH v2 2/5] elf2dmp: introduce physical block alignment Date: Fri, 15 Sep 2023 20:01:50 +0300 Message-Id: <20230915170153.10959-3-viktor@daynix.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230915170153.10959-1-viktor@daynix.com> References: <20230915170153.10959-1-viktor@daynix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2a00:1450:4864:20::133; envelope-from=viktor@daynix.com; helo=mail-lf1-x133.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1694797358900100001 Content-Type: text/plain; charset="utf-8" Physical memory ranges may not be aligned to page size in QEMU ELF, but DMP can only contain page-aligned runs. So, align them. Signed-off-by: Viktor Prutyanov Reviewed-by: Akihiko Odaki --- contrib/elf2dmp/addrspace.c | 31 +++++++++++++++++++++++++++++-- contrib/elf2dmp/addrspace.h | 1 + contrib/elf2dmp/main.c | 5 +++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/contrib/elf2dmp/addrspace.c b/contrib/elf2dmp/addrspace.c index 0b04cba00e..64b5d680ad 100644 --- a/contrib/elf2dmp/addrspace.c +++ b/contrib/elf2dmp/addrspace.c @@ -14,7 +14,7 @@ static struct pa_block *pa_space_find_block(struct pa_spa= ce *ps, uint64_t pa) =20 for (i =3D 0; i < ps->block_nr; i++) { if (ps->block[i].paddr <=3D pa && - pa <=3D ps->block[i].paddr + ps->block[i].size) { + pa < ps->block[i].paddr + ps->block[i].size) { return ps->block + i; } } @@ -33,6 +33,30 @@ static uint8_t *pa_space_resolve(struct pa_space *ps, ui= nt64_t pa) return block->addr + (pa - block->paddr); } =20 +static void pa_block_align(struct pa_block *b) +{ + uint64_t low_align =3D ((b->paddr - 1) | ELF2DMP_PAGE_MASK) + 1 - b->p= addr; + uint64_t high_align =3D (b->paddr + b->size) & ELF2DMP_PAGE_MASK; + + if (low_align =3D=3D 0 && high_align =3D=3D 0) { + return; + } + + if (low_align + high_align < b->size) { + printf("Block 0x%"PRIx64"+:0x%"PRIx64" will be aligned to " + "0x%"PRIx64"+:0x%"PRIx64"\n", b->paddr, b->size, + b->paddr + low_align, b->size - low_align - high_align); + b->size -=3D low_align + high_align; + } else { + printf("Block 0x%"PRIx64"+:0x%"PRIx64" is too small to align\n", + b->paddr, b->size); + b->size =3D 0; + } + + b->addr +=3D low_align; + b->paddr +=3D low_align; +} + int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf) { Elf64_Half phdr_nr =3D elf_getphdrnum(qemu_elf->map); @@ -60,10 +84,13 @@ int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu= _elf) .paddr =3D phdr[i].p_paddr, .size =3D phdr[i].p_filesz, }; - block_i++; + pa_block_align(&ps->block[block_i]); + block_i =3D ps->block[block_i].size ? (block_i + 1) : block_i; } } =20 + ps->block_nr =3D block_i; + return 0; } =20 diff --git a/contrib/elf2dmp/addrspace.h b/contrib/elf2dmp/addrspace.h index 00b44c1218..039c70c5b0 100644 --- a/contrib/elf2dmp/addrspace.h +++ b/contrib/elf2dmp/addrspace.h @@ -12,6 +12,7 @@ =20 #define ELF2DMP_PAGE_BITS 12 #define ELF2DMP_PAGE_SIZE (1ULL << ELF2DMP_PAGE_BITS) +#define ELF2DMP_PAGE_MASK (ELF2DMP_PAGE_SIZE - 1) #define ELF2DMP_PFN_MASK (~(ELF2DMP_PAGE_SIZE - 1)) =20 #define INVALID_PA UINT64_MAX diff --git a/contrib/elf2dmp/main.c b/contrib/elf2dmp/main.c index bb6744c0cd..b7e3930164 100644 --- a/contrib/elf2dmp/main.c +++ b/contrib/elf2dmp/main.c @@ -400,9 +400,10 @@ static int write_dump(struct pa_space *ps, for (i =3D 0; i < ps->block_nr; i++) { struct pa_block *b =3D &ps->block[i]; =20 - printf("Writing block #%zu/%zu to file...\n", i, ps->block_nr); + printf("Writing block #%zu/%zu of %"PRIu64" bytes to file...\n", i, + ps->block_nr, b->size); if (fwrite(b->addr, b->size, 1, dmp_file) !=3D 1) { - eprintf("Failed to write dump header\n"); + eprintf("Failed to write block\n"); fclose(dmp_file); return 1; } --=20 2.21.0 From nobody Wed Oct 30 19:55:50 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1694797364; cv=none; d=zohomail.com; s=zohoarc; b=dVs/8buD7g6N3YarQhESQf5ybVUKG9EDl4t3Bsb8KLBKnuw00Msa99JXoDGi95wn23jMrWGNumEhAwoTW36kg7BV0gimuwIFw5lkBF9FgoizuUS47KNabQ5Xmr8PHPseNXoqinoQEiIzDbohpN50MLdAoj8TDTon0nvpLaPY8u4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694797364; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=271dWGSnrPrZmSkc1ZOMJavGXnqksEpNUC/AjjrXlbo=; b=maqb9voVrgbyGa8xl7U4jQ1d/YuTJVFDL7lGz38gKFlRi4HODLl3Zcn66v4m5oC3bDV3Q8+bWkWnrGpOqhB34/Dg+A12B3Vq2/84auC7pIlDIae1V3WyWU4foIEGU8wUw4zRZTP/0PjAJ/WNor1YXzCZGxylaep9bffhtbd9Fww= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1694797364836930.0494791009936; Fri, 15 Sep 2023 10:02:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qhCCp-0003Ny-Qk; Fri, 15 Sep 2023 13:02:11 -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 1qhCCn-0003MY-Th for qemu-devel@nongnu.org; Fri, 15 Sep 2023 13:02:09 -0400 Received: from mail-lf1-x130.google.com ([2a00:1450:4864:20::130]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qhCCm-0000LE-62 for qemu-devel@nongnu.org; Fri, 15 Sep 2023 13:02:09 -0400 Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-502984f5018so3945695e87.3 for ; Fri, 15 Sep 2023 10:02:07 -0700 (PDT) Received: from localhost.localdomain (109-252-90-8.nat.spd-mgts.ru. [109.252.90.8]) by smtp.gmail.com with ESMTPSA id u7-20020a056512040700b005009ab1992esm690431lfk.154.2023.09.15.10.02.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 10:02:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1694797326; x=1695402126; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=271dWGSnrPrZmSkc1ZOMJavGXnqksEpNUC/AjjrXlbo=; b=YKiHnal5kPU03Mliq1FirXF6j1EqmN1wP+9YlJQAgUq8E65SEUGoDKiK7kA02kQa4i p9hpxFi30PqOb+MEclfAbwYIM6tvdM2FQ6zVZjvri87dMbisFoeVQT5VbOefuv80KRNt IV0PInSu4UgVn2rm53gWrOVB4iWDrGcfTxH6T9Jth5xlgUsvvCBcvyeAHYETKaeQlK3X 1HpQwcZjhKWFmbfVqnhxay0ju2ZVi3ZSM2qmiXbCk7gqTDBcYE/NAS9eokDC2JifQP6o MyNulzt4O8N2DK/Gcn/AC1g1pVrb2dysXALiQjBONC5ogVPdfyK5a0EkfP/pKerqCpJ3 cdxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694797326; x=1695402126; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=271dWGSnrPrZmSkc1ZOMJavGXnqksEpNUC/AjjrXlbo=; b=Hn2YTuP0R9f7tPcCuc5J/IMuhmWkAge2xg5SDcUPP522p7UQmkfVvkifN5e/SHjgUH u5vfKpm89q+PTrUm6SjDMD6KrnfuCAZuC05pdcsBJi4dM7KIHCeWP+whV88fMWQyfkUP dEeSw7NXQveVg4UmI7VIkamEphfUlOBbH37dvnDDhuSrtXXl0hReIErXdebDoWn8h0tn kzyxHQKiaeD3hbv1qkJgx+MN3mzYA1mR/l5Vnb5h+3AYuLImSKCoOBdoqVW1ahYTshDM kB7YGggF8AkGw73Ealf+ZJdyDRILPIf2p6+KAvpga6h51EX20wCv74zZkeL7MSG1rKP6 LdDA== X-Gm-Message-State: AOJu0YwtMxFzUVwt74cTf3AqUMimkPYr68QkcAtB1UrpWttzMb5tYjll zzpeXQjsbHHtimsx5aZ0/LgXVg== X-Google-Smtp-Source: AGHT+IFiuWEr+A1uDCQQNju3g8Ij93JcB/aeQ341BKvV0sT3TF4UW1iZhtzJlbEdEo/9cCVXH6KlXg== X-Received: by 2002:a19:4f1a:0:b0:4ff:95c:e158 with SMTP id d26-20020a194f1a000000b004ff095ce158mr2103744lfb.64.1694797326437; Fri, 15 Sep 2023 10:02:06 -0700 (PDT) From: Viktor Prutyanov To: annie.li@oracle.com, akihiko.odaki@daynix.com, kkostiuk@redhat.com Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, yan@daynix.com, viktor@daynix.com, viktor.prutyanov@phystech.edu Subject: [PATCH v2 3/5] elf2dmp: introduce merging of physical memory runs Date: Fri, 15 Sep 2023 20:01:51 +0300 Message-Id: <20230915170153.10959-4-viktor@daynix.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230915170153.10959-1-viktor@daynix.com> References: <20230915170153.10959-1-viktor@daynix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2a00:1450:4864:20::130; envelope-from=viktor@daynix.com; helo=mail-lf1-x130.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1694797367029100003 Content-Type: text/plain; charset="utf-8" DMP supports 42 physical memory runs at most. So, merge adjacent physical memory ranges from QEMU ELF when possible to minimize total number of runs. Signed-off-by: Viktor Prutyanov Reviewed-by: Akihiko Odaki --- contrib/elf2dmp/main.c | 56 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/contrib/elf2dmp/main.c b/contrib/elf2dmp/main.c index b7e3930164..b4683575fd 100644 --- a/contrib/elf2dmp/main.c +++ b/contrib/elf2dmp/main.c @@ -20,6 +20,7 @@ #define PE_NAME "ntoskrnl.exe" =20 #define INITIAL_MXCSR 0x1f80 +#define MAX_NUMBER_OF_RUNS 42 =20 typedef struct idt_desc { uint16_t offset1; /* offset bits 0..15 */ @@ -234,6 +235,42 @@ static int fix_dtb(struct va_space *vs, QEMU_Elf *qe) return 1; } =20 +static void try_merge_runs(struct pa_space *ps, + WinDumpPhyMemDesc64 *PhysicalMemoryBlock) +{ + unsigned int merge_cnt =3D 0, run_idx =3D 0; + + PhysicalMemoryBlock->NumberOfRuns =3D 0; + + for (size_t idx =3D 0; idx < ps->block_nr; idx++) { + struct pa_block *blk =3D ps->block + idx; + struct pa_block *next =3D blk + 1; + + PhysicalMemoryBlock->NumberOfPages +=3D blk->size / ELF2DMP_PAGE_S= IZE; + + if (idx + 1 !=3D ps->block_nr && blk->paddr + blk->size =3D=3D nex= t->paddr) { + printf("Block #%lu 0x%"PRIx64"+:0x%"PRIx64" and %u previous wi= ll be" + " merged\n", idx, blk->paddr, blk->size, merge_cnt); + merge_cnt++; + } else { + struct pa_block *first_merged =3D blk - merge_cnt; + + printf("Block #%lu 0x%"PRIx64"+:0x%"PRIx64" and %u previous wi= ll be" + " merged to 0x%"PRIx64"+:0x%"PRIx64" (run #%u)\n", + idx, blk->paddr, blk->size, merge_cnt, first_merged->p= addr, + blk->paddr + blk->size - first_merged->paddr, run_idx); + PhysicalMemoryBlock->Run[run_idx] =3D (WinDumpPhyMemRun64) { + .BasePage =3D first_merged->paddr / ELF2DMP_PAGE_SIZE, + .PageCount =3D (blk->paddr + blk->size - first_merged->pad= dr) / + ELF2DMP_PAGE_SIZE, + }; + PhysicalMemoryBlock->NumberOfRuns++; + run_idx++; + merge_cnt =3D 0; + } + } +} + static int fill_header(WinDumpHeader64 *hdr, struct pa_space *ps, struct va_space *vs, uint64_t KdDebuggerDataBlock, KDDEBUGGER_DATA64 *kdbg, uint64_t KdVersionBlock, int nr_cpus) @@ -244,7 +281,6 @@ static int fill_header(WinDumpHeader64 *hdr, struct pa_= space *ps, KUSD_OFFSET_PRODUCT_TYPE); DBGKD_GET_VERSION64 kvb; WinDumpHeader64 h; - size_t i; =20 QEMU_BUILD_BUG_ON(KUSD_OFFSET_SUITE_MASK >=3D ELF2DMP_PAGE_SIZE); QEMU_BUILD_BUG_ON(KUSD_OFFSET_PRODUCT_TYPE >=3D ELF2DMP_PAGE_SIZE); @@ -282,13 +318,17 @@ static int fill_header(WinDumpHeader64 *hdr, struct p= a_space *ps, .RequiredDumpSpace =3D sizeof(h), }; =20 - for (i =3D 0; i < ps->block_nr; i++) { - h.PhysicalMemoryBlock.NumberOfPages +=3D - ps->block[i].size / ELF2DMP_PAGE_SIZE; - h.PhysicalMemoryBlock.Run[i] =3D (WinDumpPhyMemRun64) { - .BasePage =3D ps->block[i].paddr / ELF2DMP_PAGE_SIZE, - .PageCount =3D ps->block[i].size / ELF2DMP_PAGE_SIZE, - }; + if (h.PhysicalMemoryBlock.NumberOfRuns <=3D MAX_NUMBER_OF_RUNS) { + for (size_t idx =3D 0; idx < ps->block_nr; idx++) { + h.PhysicalMemoryBlock.NumberOfPages +=3D + ps->block[idx].size / ELF2DMP_PAGE_SIZE; + h.PhysicalMemoryBlock.Run[idx] =3D (WinDumpPhyMemRun64) { + .BasePage =3D ps->block[idx].paddr / ELF2DMP_PAGE_SIZE, + .PageCount =3D ps->block[idx].size / ELF2DMP_PAGE_SIZE, + }; + } + } else { + try_merge_runs(ps, &h.PhysicalMemoryBlock); } =20 h.RequiredDumpSpace +=3D --=20 2.21.0 From nobody Wed Oct 30 19:55:50 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1694797386; cv=none; d=zohomail.com; s=zohoarc; b=Hg/dSmyiZARjw76vnY5bnnh/37kBauUlRS6FvwiiP21rB4zYudhgcSwJLEbx/wr/WtNYCj0VjIX2VJvif75nCwIt3/7PzyednckU7SkPwoOr+m0Rl2qcwIInXUl1YBWsmpCDFwx0/eT+IqAlIFPWfXXjPYL5PzwGW8qTQwovtUk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694797386; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ZX87+UWr8lrrIsKDRoUiR5UzsqRSt0csBfbQi+d1bi8=; b=SEu2o8wP5h/Aw8eGjv7vILu8xS8JY9CCg79Z2EOqeCNuXRO+g+M3/Xj92kaICqVI1SwqV0FoSk+NYGHqXexHYGS8SNpL6sbJfpraX9laTXJagbT9KCty244tAqa+6XpJb1YmhqG37RTbhOKpxYwcmop0+HJ9GoofCUx6mJbIcW8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16947973868941023.1448027564807; Fri, 15 Sep 2023 10:03:06 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qhCCs-0003SC-LZ; Fri, 15 Sep 2023 13:02:14 -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 1qhCCp-0003Nf-B5 for qemu-devel@nongnu.org; Fri, 15 Sep 2023 13:02:11 -0400 Received: from mail-lf1-x12b.google.com ([2a00:1450:4864:20::12b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qhCCn-0000LY-LD for qemu-devel@nongnu.org; Fri, 15 Sep 2023 13:02:11 -0400 Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-5007abb15e9so4055959e87.0 for ; Fri, 15 Sep 2023 10:02:09 -0700 (PDT) Received: from localhost.localdomain (109-252-90-8.nat.spd-mgts.ru. [109.252.90.8]) by smtp.gmail.com with ESMTPSA id u7-20020a056512040700b005009ab1992esm690431lfk.154.2023.09.15.10.02.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 10:02:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1694797328; x=1695402128; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZX87+UWr8lrrIsKDRoUiR5UzsqRSt0csBfbQi+d1bi8=; b=07b1JL+XD6ma5HT/9tPTuROsdTEGqjc5CGx1dGFzdWWNZPwBdyHsCoTFLy/ftT6pVT w7FpdOdbgWDov9drTwS16xoKQ8bYVTyy0D0e/ZGArHyVileOfG0Q0FXmkKJzo+6acRGG Pvv9OBTawkBzwQNdxykY9Jraylv9YXnS7pgGdKW0nYBC8VwBC1+OHiPyehDw4mUDWHTk VzVdNBTah7EnKU/KGzPNqhrZk7ON5B/6IlN9jai24EjN70KRW42RzuJ9BtnmgiwTn5Bh 8xZTYR9euul3dvAhKMmqJudVXVc3V6Y+dua8KHWMef4aBSz7BkhnXwedFC/twpTx8Xyh uuTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694797328; x=1695402128; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZX87+UWr8lrrIsKDRoUiR5UzsqRSt0csBfbQi+d1bi8=; b=dDeTCTczunEr9QKCh7wwNT9j/EQnB5Jp0UG4vR4d/giz+VuOZFx1V/RbVIhic6pIlc lOoV1pSj9GnXJIjmwlLVzewVBwcL5OlzLhf4C+CKbTutac/MDosqoSskkYReUgtGhzEg K0LOu+QgtiKDXaRJVhmjWOCzyLZrkcYnM34mtot2709w0HMD94VZ24DCoShsQxsIc8fP Slkcs7+hwSpueKAUyphgki9OmiWwOP8Z6ZoO5ZcZ5Rta0UEbIotomQzzcsDDJnnt4PNG p4E7yYTJjw5Sk3oQjWDgwSTTwA3e1jNEu7rll3VtA4hHa6OLD3ARRw214ijJXAYeVsP3 kQcw== X-Gm-Message-State: AOJu0Yx1DI6pL5pz/LXUOI4MdNBYlU7WjchSEMCZ2SYKF/V5tkwATHL+ PNOIajZqG3QOEdhKuhwanFO+Kg== X-Google-Smtp-Source: AGHT+IFGetnVODBDR3ayqC8t+QXWUBxmoURbyCJi6/DcpXjJNtGGwbP8IJyqsDRPhYNfS161bL6mZA== X-Received: by 2002:a05:6512:3b9d:b0:500:807a:f1a4 with SMTP id g29-20020a0565123b9d00b00500807af1a4mr2711891lfv.18.1694797327845; Fri, 15 Sep 2023 10:02:07 -0700 (PDT) From: Viktor Prutyanov To: annie.li@oracle.com, akihiko.odaki@daynix.com, kkostiuk@redhat.com Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, yan@daynix.com, viktor@daynix.com, viktor.prutyanov@phystech.edu Subject: [PATCH v2 4/5] elf2dmp: use Linux mmap with MAP_NORESERVE when possible Date: Fri, 15 Sep 2023 20:01:52 +0300 Message-Id: <20230915170153.10959-5-viktor@daynix.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230915170153.10959-1-viktor@daynix.com> References: <20230915170153.10959-1-viktor@daynix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2a00:1450:4864:20::12b; envelope-from=viktor@daynix.com; helo=mail-lf1-x12b.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1694797387528100002 Content-Type: text/plain; charset="utf-8" Glib's g_mapped_file_new maps file with PROT_READ|PROT_WRITE and MAP_PRIVATE. This leads to premature physical memory allocation of dump file size on Linux hosts and may fail. On Linux, mapping the file with MAP_NORESERVE limits the allocation by available memory. Signed-off-by: Viktor Prutyanov Reviewed-by: Akihiko Odaki --- contrib/elf2dmp/qemu_elf.c | 68 +++++++++++++++++++++++++++++++------- contrib/elf2dmp/qemu_elf.h | 2 ++ 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/contrib/elf2dmp/qemu_elf.c b/contrib/elf2dmp/qemu_elf.c index ebda60dcb8..de6ad744c6 100644 --- a/contrib/elf2dmp/qemu_elf.c +++ b/contrib/elf2dmp/qemu_elf.c @@ -165,10 +165,40 @@ static bool check_ehdr(QEMU_Elf *qe) return true; } =20 -int QEMU_Elf_init(QEMU_Elf *qe, const char *filename) +static int QEMU_Elf_map(QEMU_Elf *qe, const char *filename) { +#ifdef CONFIG_LINUX + struct stat st; + int fd; + + printf("Using Linux mmap\n"); + + fd =3D open(filename, O_RDONLY, 0); + if (fd =3D=3D -1) { + eprintf("Failed to open ELF dump file \'%s\'\n", filename); + return 1; + } + + if (fstat(fd, &st)) { + eprintf("Failed to get size of ELF dump file\n"); + close(fd); + return 1; + } + qe->size =3D st.st_size; + + qe->map =3D mmap(NULL, qe->size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_NORESERVE, fd, 0); + if (qe->map =3D=3D MAP_FAILED) { + eprintf("Failed to map ELF file\n"); + close(fd); + return 1; + } + + close(fd); +#else GError *gerr =3D NULL; - int err =3D 0; + + printf("Using GLib mmap\n"); =20 qe->gmf =3D g_mapped_file_new(filename, TRUE, &gerr); if (gerr) { @@ -179,29 +209,43 @@ int QEMU_Elf_init(QEMU_Elf *qe, const char *filename) =20 qe->map =3D g_mapped_file_get_contents(qe->gmf); qe->size =3D g_mapped_file_get_length(qe->gmf); +#endif + + return 0; +} + +static void QEMU_Elf_unmap(QEMU_Elf *qe) +{ +#ifdef CONFIG_LINUX + munmap(qe->map, qe->size); +#else + g_mapped_file_unref(qe->gmf); +#endif +} + +int QEMU_Elf_init(QEMU_Elf *qe, const char *filename) +{ + if (QEMU_Elf_map(qe, filename)) { + return 1; + } =20 if (!check_ehdr(qe)) { eprintf("Input file has the wrong format\n"); - err =3D 1; - goto out_unmap; + QEMU_Elf_unmap(qe); + return 1; } =20 if (init_states(qe)) { eprintf("Failed to extract QEMU CPU states\n"); - err =3D 1; - goto out_unmap; + QEMU_Elf_unmap(qe); + return 1; } =20 return 0; - -out_unmap: - g_mapped_file_unref(qe->gmf); - - return err; } =20 void QEMU_Elf_exit(QEMU_Elf *qe) { exit_states(qe); - g_mapped_file_unref(qe->gmf); + QEMU_Elf_unmap(qe); } diff --git a/contrib/elf2dmp/qemu_elf.h b/contrib/elf2dmp/qemu_elf.h index b2f0d9cbc9..afa75f10b2 100644 --- a/contrib/elf2dmp/qemu_elf.h +++ b/contrib/elf2dmp/qemu_elf.h @@ -32,7 +32,9 @@ typedef struct QEMUCPUState { int is_system(QEMUCPUState *s); =20 typedef struct QEMU_Elf { +#ifndef CONFIG_LINUX GMappedFile *gmf; +#endif size_t size; void *map; QEMUCPUState **state; --=20 2.21.0 From nobody Wed Oct 30 19:55:50 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1694797358; cv=none; d=zohomail.com; s=zohoarc; b=E4VnnpTJms6RtnO5Dk489l8cY38efBQUCLJC/GGKXsFJReb4FFUJdmHwb0i/N4V5zOaE/1YC6i2F4YFax+sLayrrYxFnf8ncV/qwuvRAfQPVpdQLCFiyJrrFu5y4e0zgoTrlnsJtHIKLy9cMTaODdQET7la4cw7ruIFBxm3Ac8w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694797358; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FyhglYJ5pbQBkUGv5WkINwdsbdNBkbHZUQKyK9LQHl4=; b=dw161aNSLF3z1d1ao3GwqoY825jQ8hvq1md19W1Pzyio/zprjcHjAiqlqyhA1KxUaORqaEwU6GLBOTd9aJBcEjPdARXvC3qMRXlFgl44mcaEJFGUJEavrSvwAtbSv2/PQ4Q5qb3nNXoNg9dfrAUiUx558qC9TkEEgu/vRz23X24= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1694797358184988.6196606073831; Fri, 15 Sep 2023 10:02:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qhCCr-0003Rj-Vs; Fri, 15 Sep 2023 13:02:14 -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 1qhCCq-0003Oj-Iv for qemu-devel@nongnu.org; Fri, 15 Sep 2023 13:02:12 -0400 Received: from mail-lf1-x131.google.com ([2a00:1450:4864:20::131]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qhCCo-0000Lf-Ej for qemu-devel@nongnu.org; Fri, 15 Sep 2023 13:02:12 -0400 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-502a25ab777so3778784e87.2 for ; Fri, 15 Sep 2023 10:02:10 -0700 (PDT) Received: from localhost.localdomain (109-252-90-8.nat.spd-mgts.ru. [109.252.90.8]) by smtp.gmail.com with ESMTPSA id u7-20020a056512040700b005009ab1992esm690431lfk.154.2023.09.15.10.02.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 10:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1694797329; x=1695402129; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FyhglYJ5pbQBkUGv5WkINwdsbdNBkbHZUQKyK9LQHl4=; b=P0IhuH8Jr002M8B4onCr+XpRgQr2u1Ijpv6VKYMA5JSwbiiSbFVZQIABT6LJisnTXD J90zqXGhSGoMWUtYNfTJKtq1wKCzBqrexKj4OKfQ4XC4XsUsp3S/WN1q8gfzFl0Kc+Z3 RzvzSJpiASzjXlObjgITdD2//a+75o3PqPlUCaW2FaZttFic/ES8WW2DQnzefUaTxkze jfaxdgb5iEdDQdhSR9vtahLcF4T55kzR35rOuVjgTU1Lti2KrTv+aBmRlD+wk5hdHdmR RA6kX/nSczC+tePC8J2UHgLine8c4+Q8pYrHMBIRnkOYZ3YwzfVyBU/i3H1GBWUlL1TJ Hvng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694797329; x=1695402129; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FyhglYJ5pbQBkUGv5WkINwdsbdNBkbHZUQKyK9LQHl4=; b=H4iUQH/TEDgQT0AcYn1tyyApK0aEA1BrwSeZPl/XDtfwzq3aANQB83+556/gqyP5vG gnbvp0Y9mBpy85uidVDcKNKhNo+u5zD7sL/56SVFywBswFCjST06Zcio3VitPddz8i7N usQw0rlNhvK3ew2He3ite9DsxEXUppMNsTDxeFaGcQnSnMzs7/+fRXRQ2+hojef4q8Hz YClYotSPxm13Pf8Op5/CqM6SZWwtXVq/92HUdjWf8ZENM1tr6yw2X6bifUWwz1IyApSv TLrtSEDASE55DdWPyRLKpafH0zYVFWlnOInO4vo56/crx7NC6qxfY1Nse4DEkSpwEjNj Vblw== X-Gm-Message-State: AOJu0YymlKZJR3QIf+4GD0mE7hLsVbVrH7dE5T58QMAqSDppy5N7dMRx 0Ubo2DTlsM+H3JeOxGG7S/YaqA== X-Google-Smtp-Source: AGHT+IF9ZwuI0AuFZD+KpwXCREmHVC9qBatbDrVLhofe/2MBYV7qELZ59PbuwNOwdo8ZxlrcLrqkqQ== X-Received: by 2002:a05:6512:3d8c:b0:500:c4f1:6bb6 with SMTP id k12-20020a0565123d8c00b00500c4f16bb6mr2531377lfv.61.1694797328886; Fri, 15 Sep 2023 10:02:08 -0700 (PDT) From: Viktor Prutyanov To: annie.li@oracle.com, akihiko.odaki@daynix.com, kkostiuk@redhat.com Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, yan@daynix.com, viktor@daynix.com, viktor.prutyanov@phystech.edu Subject: [PATCH v2 5/5] elf2dmp: rework PDB_STREAM_INDEXES::segments obtaining Date: Fri, 15 Sep 2023 20:01:53 +0300 Message-Id: <20230915170153.10959-6-viktor@daynix.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230915170153.10959-1-viktor@daynix.com> References: <20230915170153.10959-1-viktor@daynix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2a00:1450:4864:20::131; envelope-from=viktor@daynix.com; helo=mail-lf1-x131.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1694797359354100003 Content-Type: text/plain; charset="utf-8" PDB for Windows 11 kernel has slightly different structure compared to previous versions. Since elf2dmp don't use the other fields, copy only 'segments' field from PDB_STREAM_INDEXES. Signed-off-by: Viktor Prutyanov Reviewed-by: Akihiko Odaki --- contrib/elf2dmp/pdb.c | 15 ++++----------- contrib/elf2dmp/pdb.h | 2 +- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/contrib/elf2dmp/pdb.c b/contrib/elf2dmp/pdb.c index adcfa7e154..6ca5086f02 100644 --- a/contrib/elf2dmp/pdb.c +++ b/contrib/elf2dmp/pdb.c @@ -160,7 +160,7 @@ static void *pdb_ds_read_file(struct pdb_reader* r, uin= t32_t file_number) static int pdb_init_segments(struct pdb_reader *r) { char *segs; - unsigned stream_idx =3D r->sidx.segments; + unsigned stream_idx =3D r->segments; =20 segs =3D pdb_ds_read_file(r, stream_idx); if (!segs) { @@ -177,9 +177,6 @@ static int pdb_init_symbols(struct pdb_reader *r) { int err =3D 0; PDB_SYMBOLS *symbols; - PDB_STREAM_INDEXES *sidx =3D &r->sidx; - - memset(sidx, -1, sizeof(*sidx)); =20 symbols =3D pdb_ds_read_file(r, 3); if (!symbols) { @@ -188,15 +185,11 @@ static int pdb_init_symbols(struct pdb_reader *r) =20 r->symbols =3D symbols; =20 - if (symbols->stream_index_size !=3D sizeof(PDB_STREAM_INDEXES)) { - err =3D 1; - goto out_symbols; - } - - memcpy(sidx, (const char *)symbols + sizeof(PDB_SYMBOLS) + + r->segments =3D *(uint16_t *)((const char *)symbols + sizeof(PDB_SYMBO= LS) + symbols->module_size + symbols->offset_size + symbols->hash_size + symbols->srcmodule_size + - symbols->pdbimport_size + symbols->unknown2_size, sizeof(*sidx= )); + symbols->pdbimport_size + symbols->unknown2_size + + offsetof(PDB_STREAM_INDEXES, segments)); =20 /* Read global symbol table */ r->modimage =3D pdb_ds_read_file(r, symbols->gsym_file); diff --git a/contrib/elf2dmp/pdb.h b/contrib/elf2dmp/pdb.h index 4ea8925ee8..2a50da56ac 100644 --- a/contrib/elf2dmp/pdb.h +++ b/contrib/elf2dmp/pdb.h @@ -227,7 +227,7 @@ struct pdb_reader { } ds; uint32_t file_used[1024]; PDB_SYMBOLS *symbols; - PDB_STREAM_INDEXES sidx; + uint16_t segments; uint8_t *modimage; char *segs; size_t segs_size; --=20 2.21.0