From patchwork Mon Jan 16 22:36:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 643110 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp842297pvb; Mon, 16 Jan 2023 14:38:08 -0800 (PST) X-Google-Smtp-Source: AMrXdXvMnnyQsr64rhY/jXO6Nn0CZugTF6x+lPqbbWi2o5M165a68ceGNoMUmNl7+FxhOdmcJV/G X-Received: by 2002:ac8:7a8b:0:b0:3b6:2b50:15eb with SMTP id x11-20020ac87a8b000000b003b62b5015ebmr11271240qtr.57.1673908688645; Mon, 16 Jan 2023 14:38:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673908688; cv=none; d=google.com; s=arc-20160816; b=wKNs0aY1fP8DjhaDUAFif6RJsIWbhjZfweE+O9Ck4+x7Qmr5+LC4pIlC3pBnJtIGD3 6LleVFoDog/zXQ+CTRh8P+8krrsmPcIxSm4nfM7wtnNilysC+Jh3Chi7ySUv9V/6Xj9D JURVzDkGSRSpDlHuNVEVs7w0NSY0y71e+q600T9m4mzEJZ9fgK13yqRJnydB2SxyT8E6 58mcLOFEuxOCTD3C+HnSGtw9kV0uRqOB3dH8t6xWT7NIv+etPOhQ5+4HVkbgX6g8At8P qcUTbvSlHaltRP8CVpb3p4k24dcd/p2Zx++kw3y+/fx3WVF+gmLvAZNSIvALIdGsMmz5 cbmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to :from:dkim-signature; bh=sVoQ+dZBxCCrOAQkIYFBP0Uz5/oWgbT60mps7v3rv5w=; b=pKqEMcEUAEovSlDUe6BespvWuZf3ERiiHGH9odJFJHO5U29vluGVmmfd0l8NTP979i HM2Om/X38fVvxcqzZWvLhhHCUqB/yRDvYAqBQlccqujQxt8G9yqvj0m2n64RKb1xzzF3 7IMuETr0XzYF0/GUPqnFbiAUPW+osM5jPDjGEkqd14dQJo1EBOzwDbRT9iTeE52zaldz helAQFnK52M7/kQYKQVu7vXEKKE2vFycJDdTEC0E6tSe4oeyyAcSyeubxBPTYCwx9eQW 3X4rAWOVr4aYlebkFmM5dILEutcQz8GThFfG7ZZjPoqZsRoBPAoVRBAFngJ5pAjDMHwP //og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kjQPhh7y; 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=pass (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 bl5-20020a05622a244500b003a68ea8af2csi2626080qtb.83.2023.01.16.14.38.08 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 16 Jan 2023 14:38:08 -0800 (PST) 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=pass header.i=@linaro.org header.s=google header.b=kjQPhh7y; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pHY5v-0000Wb-Oe; Mon, 16 Jan 2023 17:36:47 -0500 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 1pHY5t-0000PQ-87 for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:45 -0500 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pHY5r-0001MF-L1 for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:45 -0500 Received: by mail-pl1-x629.google.com with SMTP id k12so9586595plk.0 for ; Mon, 16 Jan 2023 14:36:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=sVoQ+dZBxCCrOAQkIYFBP0Uz5/oWgbT60mps7v3rv5w=; b=kjQPhh7y6UmxHQT083q4jKpXzCOGnRVkRlBOiQfevp+dtKPpMRseWca1yXPQ9ZMx64 SuYh4/KLFO1mnSrjdjaxsKZSNDXRKrQk5qdfyqIz9kJf8jf3czzLieFL0pOmYAMtZzHq mE3pdJLo9z7dOpAoNGs9k3+GsE1Ts0ZLh1hxNGL/BLn9MzqrZZuoNPV44dxOLxdNR6di E4YE7Dt013/1JyU04xjOSmTvHxFn4rX04waA1K2cQ2KkzaMmg6e0HKcwCyKyTX5NDyGk lyH13LW0g22dcZk332w8tD/SPRFXqEZj7u9SVOMmkGevc3f1F+MS6XbJmdaITnBTHvr5 N94A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=sVoQ+dZBxCCrOAQkIYFBP0Uz5/oWgbT60mps7v3rv5w=; b=o574qI2VOXx3Nmi1e+HExI308A5VRMqAoc/3qwWs9HN0M7gtv/M075anxePo7jfCHN ob65yXePOKXGFk0js7Fs7UMjIMHV9jEbrfEO1JZYu81w7kjbfnoFkmoFMu7aiqzzvtGE ulglzcoi6wr+AdbbbRKxwqzHHXrGFji+5twdBf1q8pxKDtNcwxdzUmRo+XllZcnQ4nA3 /0sA/IRKpwhB4tmCJzXzSSJpapl/sHD5WvzW/8Cn+S6SjYGUvsMF0r7l0SAG+8++2yIb fpaUKnvuop+YvfK8b3XMGYJWgDP3QOgmLIkvg6Uy7LMJ2Alr6qsGSz16BFoONWgeO0ZV OkSQ== X-Gm-Message-State: AFqh2krHjIaY+vqyC+bCbUhdBAj+HOv5mV1hB6FPV4rcDptZMk332byb FdWc/3j5qOjanahxDwJHQ+BYyqkXNtQO/jox X-Received: by 2002:a17:902:d50e:b0:191:4378:ec06 with SMTP id b14-20020a170902d50e00b001914378ec06mr30669211plg.61.1673908602048; Mon, 16 Jan 2023 14:36:42 -0800 (PST) Received: from stoup.. (rrcs-173-198-77-218.west.biz.rr.com. [173.198.77.218]) by smtp.gmail.com with ESMTPSA id n3-20020a170903110300b00189f2fdbdd0sm19742492plh.234.2023.01.16.14.36.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Jan 2023 14:36:41 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, Ilya Leoshkevich , =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 1/5] linux-user: Clean up when exiting due to a signal Date: Mon, 16 Jan 2023 12:36:33 -1000 Message-Id: <20230116223637.3512814-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230116223637.3512814-1-richard.henderson@linaro.org> References: <20230116223637.3512814-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::629; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x629.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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Ilya Leoshkevich When exiting due to an exit() syscall, qemu-user calls preexit_cleanup(), but this is currently not the case when exiting due to a signal. This leads to various buffers not being flushed (e.g., for gprof, for gcov, and for the upcoming perf support). Add the missing call. Signed-off-by: Ilya Leoshkevich Reviewed-by: Alex Bennée Reviewed-by: Richard Henderson Message-Id: <20230112152013.125680-2-iii@linux.ibm.com> Signed-off-by: Richard Henderson --- linux-user/signal.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/linux-user/signal.c b/linux-user/signal.c index 61c6fa3fcf..098f3a787d 100644 --- a/linux-user/signal.c +++ b/linux-user/signal.c @@ -695,7 +695,7 @@ void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr, /* abort execution with signal */ static G_NORETURN -void dump_core_and_abort(int target_sig) +void dump_core_and_abort(CPUArchState *cpu_env, int target_sig) { CPUState *cpu = thread_cpu; CPUArchState *env = cpu->env_ptr; @@ -724,6 +724,8 @@ void dump_core_and_abort(int target_sig) target_sig, strsignal(host_sig), "core dumped" ); } + preexit_cleanup(cpu_env, 128 + target_sig); + /* The proper exit code for dying from an uncaught signal is * -. The kernel doesn't allow exit() or _exit() to pass * a negative value. To get the proper exit code we need to @@ -1058,12 +1060,12 @@ static void handle_pending_signal(CPUArchState *cpu_env, int sig, sig != TARGET_SIGURG && sig != TARGET_SIGWINCH && sig != TARGET_SIGCONT) { - dump_core_and_abort(sig); + dump_core_and_abort(cpu_env, sig); } } else if (handler == TARGET_SIG_IGN) { /* ignore sig */ } else if (handler == TARGET_SIG_ERR) { - dump_core_and_abort(sig); + dump_core_and_abort(cpu_env, sig); } else { /* compute the blocked signals during the handler execution */ sigset_t *blocked_set; From patchwork Mon Jan 16 22:36:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 643109 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp842220pvb; Mon, 16 Jan 2023 14:37:58 -0800 (PST) X-Google-Smtp-Source: AMrXdXuFQB3qn8QCH2E64ZGHYKwfXO4l2UHnDHPl+cVngeB7WlXvv0NTzGO5H+Zt5aKiLr4wRMBE X-Received: by 2002:ac8:6b90:0:b0:3a7:f3c4:dcd1 with SMTP id z16-20020ac86b90000000b003a7f3c4dcd1mr913400qts.23.1673908678437; Mon, 16 Jan 2023 14:37:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673908678; cv=none; d=google.com; s=arc-20160816; b=GwezrHXM8QCri6NdjZuuXI3O4Seiw114LuQy3fM+6r7znKGyWtHUlctNtkJ0a8XXWk F4URYb86thAD5H/RkANw65vTGCfidbc6GRACfnSSotoAFbyAEKPQAWLxjEbCM18jLlbd 5sRyp+LTAQmqV/96Lnl7Iqi9/QJLRR4OytohVFLnfk9xZPqmYdZaKyLQ2erzjjIkPlR9 lBwWbTKHax1EqE0f5CtLxbbqpUeeB4fkhmWaVUyEhV9RxKPhQbLjlbcKdtF1GE4RX/WL ufy01PoIQ4Pm12/tsmMe3roIJvd3R7OFBGFph9UxwAeDwSKc4HgLNa8rqS+tEbAAr+Xs 5qBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to :from:dkim-signature; bh=vigJ4H8I9yTvudPoDwjrtTe/cdhVD5twvfghMdTYAx8=; b=xiRQVjeZFvmXysh8vI1KbvxTEnOcw0ucsCVpGJUm53qIZ5mzDpytXylrXrfyAyWuGa B/rWgiH5OgLYARLtjD5uJCK1r5jC2ZzFScgB23jByr1ruHEbspKXG6MMj8OmQOic5dTa Ucad760OGVPu5RfGb2Vb6licIssSPhQopxUfHejIUF8P9imaSj7d9+2OIbzhvoNZfu2F LTGm/Fyql3yGGRaXUsgGSs5x7ObEpLgSFs7PxGd7q2db+ww7ZSJ9iqpCUe6SIzdaHYJS PNzbr4viKbqFscjtlhTntC5bK/K0cEhsNB2w/lER4opnESv74HTouKjR7t89kwIOc8jW jX5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nv1dLGr5; 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=pass (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 j10-20020a05620a288a00b006fc3edca90asi18291180qkp.73.2023.01.16.14.37.58 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 16 Jan 2023 14:37:58 -0800 (PST) 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=pass header.i=@linaro.org header.s=google header.b=nv1dLGr5; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pHY5x-0000Zb-Eb; Mon, 16 Jan 2023 17:36:49 -0500 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 1pHY5v-0000WK-CA for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:47 -0500 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pHY5t-0001MR-C6 for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:47 -0500 Received: by mail-pl1-x62e.google.com with SMTP id c6so31692634pls.4 for ; Mon, 16 Jan 2023 14:36:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=vigJ4H8I9yTvudPoDwjrtTe/cdhVD5twvfghMdTYAx8=; b=nv1dLGr5ZC5kAN1km/4zUaevO438QQemce4MN4yDsb5yc2vQVBd8KyiFiWGkJB1WDj ljvESg09j/KMeV6Fw75InjMtVGgmV5VdsYSvZ5Wu1ZUnhk8Xmm12oV1jrtjS1uVt9n9b BDIZtU6gQDRMt7bVzKqTdOw41wh+iJmMF2zsNmjtSNTtRYfywim4att5RCZCRpxy8Hj3 h6/Tw7aDJPRPya4OzIH55JIuhoNbRmhOToe50DyvjtBFUPe/Y34vw6/LqEjP524Zwcah GJt+FdsZ4+m0rmbI04YO80S/gWs+y0xv4hwks9meHiKP5DaV0OMzqQ4LkBMzcTjFeGt6 w47g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=vigJ4H8I9yTvudPoDwjrtTe/cdhVD5twvfghMdTYAx8=; b=U5/E/iar8wJkrq14FZxGoGCRciPdfydtwFXqY3d+kiFz5wnweUhZmpOTlKMPcK9CiH 964dI0+GPG+aDYTPV0ftDkRF/7YMnW7q+1XAXHnSp3mxBCcdy5JdaIpFwLI+hPweaqTR to7l6fsv3D1IBozuCjg17nmoIHE7JegTaB7situjQOWDTiCtX8I95NAd73d4PE1vwZGY UWlDRds+Jz+eYEhGsDNLJrDyDtxNJbM7fvtFfLclLHz5e0oZF7+jnQrQZfOty++Fc78Q GiLAVfp+I00gzooMzGbqBD3kzYcCNEPKRMolM4LDjKoDCALD+xIBIoybDFEkrFajC4I+ 3X0Q== X-Gm-Message-State: AFqh2kpsNvbVZp1LeXpqJgK/AXvH+3dt+EP3Dm0KVAdigVaFTSmlrdOA gu9IBz5FhQmYbpaNENlGJmv960hPTniKZxUt X-Received: by 2002:a17:902:8d83:b0:193:39bd:df97 with SMTP id v3-20020a1709028d8300b0019339bddf97mr1185900plo.14.1673908603789; Mon, 16 Jan 2023 14:36:43 -0800 (PST) Received: from stoup.. (rrcs-173-198-77-218.west.biz.rr.com. [173.198.77.218]) by smtp.gmail.com with ESMTPSA id n3-20020a170903110300b00189f2fdbdd0sm19742492plh.234.2023.01.16.14.36.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Jan 2023 14:36:43 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, Ilya Leoshkevich Subject: [PULL 2/5] accel/tcg: Add debuginfo support Date: Mon, 16 Jan 2023 12:36:34 -1000 Message-Id: <20230116223637.3512814-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230116223637.3512814-1-richard.henderson@linaro.org> References: <20230116223637.3512814-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62e; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62e.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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Ilya Leoshkevich Add libdw-based functions for loading and querying debuginfo. Load debuginfo from the system and the linux-user loaders. This is useful for the upcoming perf support, which can then put human-readable guest symbols instead of raw guest PCs into perfmap and jitdump files. Signed-off-by: Ilya Leoshkevich Message-Id: <20230112152013.125680-3-iii@linux.ibm.com> Signed-off-by: Richard Henderson --- meson.build | 8 ++++ accel/tcg/debuginfo.h | 77 +++++++++++++++++++++++++++++++++ accel/tcg/debuginfo.c | 96 ++++++++++++++++++++++++++++++++++++++++++ hw/core/loader.c | 5 +++ linux-user/elfload.c | 3 ++ accel/tcg/meson.build | 1 + linux-user/meson.build | 1 + 7 files changed, 191 insertions(+) create mode 100644 accel/tcg/debuginfo.h create mode 100644 accel/tcg/debuginfo.c diff --git a/meson.build b/meson.build index 5d68a8fd23..6d212f6c8e 100644 --- a/meson.build +++ b/meson.build @@ -1648,6 +1648,12 @@ if libbpf.found() and not cc.links(''' endif endif +# libdw +libdw = dependency('libdw', + method: 'pkg-config', + kwargs: static_kwargs, + required: false) + ################# # config-host.h # ################# @@ -1923,6 +1929,7 @@ config_host_data.set('CONFIG_DBUS_DISPLAY', dbus_display) config_host_data.set('CONFIG_CFI', get_option('cfi')) config_host_data.set('CONFIG_SELINUX', selinux.found()) config_host_data.set('CONFIG_XEN_BACKEND', xen.found()) +config_host_data.set('CONFIG_LIBDW', libdw.found()) if xen.found() # protect from xen.version() having less than three components xen_version = xen.version().split('.') + ['0', '0'] @@ -3976,6 +3983,7 @@ summary_info += {'libudev': libudev} # Dummy dependency, keep .found() summary_info += {'FUSE lseek': fuse_lseek.found()} summary_info += {'selinux': selinux} +summary_info += {'libdw': libdw} summary(summary_info, bool_yn: true, section: 'Dependencies') if not supported_cpus.contains(cpu) diff --git a/accel/tcg/debuginfo.h b/accel/tcg/debuginfo.h new file mode 100644 index 0000000000..7542cfe6e0 --- /dev/null +++ b/accel/tcg/debuginfo.h @@ -0,0 +1,77 @@ +/* + * Debug information support. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef ACCEL_TCG_DEBUGINFO_H +#define ACCEL_TCG_DEBUGINFO_H + +/* + * Debuginfo describing a certain address. + */ +struct debuginfo_query { + uint64_t address; /* Input: address. */ + int flags; /* Input: debuginfo subset. */ + const char *symbol; /* Symbol that the address is part of. */ + uint64_t offset; /* Offset from the symbol. */ + const char *file; /* Source file associated with the address. */ + int line; /* Line number in the source file. */ +}; + +/* + * Debuginfo subsets. + */ +#define DEBUGINFO_SYMBOL BIT(1) +#define DEBUGINFO_LINE BIT(2) + +#if defined(CONFIG_TCG) && defined(CONFIG_LIBDW) +/* + * Load debuginfo for the specified guest ELF image. + * Return true on success, false on failure. + */ +void debuginfo_report_elf(const char *name, int fd, uint64_t bias); + +/* + * Take the debuginfo lock. + */ +void debuginfo_lock(void); + +/* + * Fill each on N Qs with the debuginfo about Q->ADDRESS as specified by + * Q->FLAGS: + * + * - DEBUGINFO_SYMBOL: update Q->SYMBOL and Q->OFFSET. If symbol debuginfo is + * missing, then leave them as is. + * - DEBUINFO_LINE: update Q->FILE and Q->LINE. If line debuginfo is missing, + * then leave them as is. + * + * This function must be called under the debuginfo lock. The results can be + * accessed only until the debuginfo lock is released. + */ +void debuginfo_query(struct debuginfo_query *q, size_t n); + +/* + * Release the debuginfo lock. + */ +void debuginfo_unlock(void); +#else +static inline void debuginfo_report_elf(const char *image_name, int image_fd, + uint64_t load_bias) +{ +} + +static inline void debuginfo_lock(void) +{ +} + +static inline void debuginfo_query(struct debuginfo_query *q, size_t n) +{ +} + +static inline void debuginfo_unlock(void) +{ +} +#endif + +#endif diff --git a/accel/tcg/debuginfo.c b/accel/tcg/debuginfo.c new file mode 100644 index 0000000000..71c66d04d1 --- /dev/null +++ b/accel/tcg/debuginfo.c @@ -0,0 +1,96 @@ +/* + * Debug information support. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/lockable.h" + +#include + +#include "debuginfo.h" + +static QemuMutex lock; +static Dwfl *dwfl; +static const Dwfl_Callbacks dwfl_callbacks = { + .find_elf = NULL, + .find_debuginfo = dwfl_standard_find_debuginfo, + .section_address = NULL, + .debuginfo_path = NULL, +}; + +__attribute__((constructor)) +static void debuginfo_init(void) +{ + qemu_mutex_init(&lock); +} + +void debuginfo_report_elf(const char *name, int fd, uint64_t bias) +{ + QEMU_LOCK_GUARD(&lock); + + if (dwfl) { + dwfl_report_begin_add(dwfl); + } else { + dwfl = dwfl_begin(&dwfl_callbacks); + } + + if (dwfl) { + dwfl_report_elf(dwfl, name, name, fd, bias, true); + dwfl_report_end(dwfl, NULL, NULL); + } +} + +void debuginfo_lock(void) +{ + qemu_mutex_lock(&lock); +} + +void debuginfo_query(struct debuginfo_query *q, size_t n) +{ + const char *symbol, *file; + Dwfl_Module *dwfl_module; + Dwfl_Line *dwfl_line; + GElf_Off dwfl_offset; + GElf_Sym dwfl_sym; + size_t i; + int line; + + if (!dwfl) { + return; + } + + for (i = 0; i < n; i++) { + dwfl_module = dwfl_addrmodule(dwfl, q[i].address); + if (!dwfl_module) { + continue; + } + + if (q[i].flags & DEBUGINFO_SYMBOL) { + symbol = dwfl_module_addrinfo(dwfl_module, q[i].address, + &dwfl_offset, &dwfl_sym, + NULL, NULL, NULL); + if (symbol) { + q[i].symbol = symbol; + q[i].offset = dwfl_offset; + } + } + + if (q[i].flags & DEBUGINFO_LINE) { + dwfl_line = dwfl_module_getsrc(dwfl_module, q[i].address); + if (dwfl_line) { + file = dwfl_lineinfo(dwfl_line, NULL, &line, 0, NULL, NULL); + if (file) { + q[i].file = file; + q[i].line = line; + } + } + } + } +} + +void debuginfo_unlock(void) +{ + qemu_mutex_unlock(&lock); +} diff --git a/hw/core/loader.c b/hw/core/loader.c index 0548830733..55dbe2e199 100644 --- a/hw/core/loader.c +++ b/hw/core/loader.c @@ -61,6 +61,7 @@ #include "hw/boards.h" #include "qemu/cutils.h" #include "sysemu/runstate.h" +#include "accel/tcg/debuginfo.h" #include @@ -503,6 +504,10 @@ ssize_t load_elf_ram_sym(const char *filename, clear_lsb, data_swab, as, load_rom, sym_cb); } + if (ret != ELF_LOAD_FAILED) { + debuginfo_report_elf(filename, fd, 0); + } + fail: close(fd); return ret; diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 20894b633f..5928c14dfc 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -19,6 +19,7 @@ #include "qemu/selfmap.h" #include "qapi/error.h" #include "target_signal.h" +#include "accel/tcg/debuginfo.h" #ifdef _ARCH_PPC64 #undef ARCH_DLINFO @@ -3261,6 +3262,8 @@ static void load_elf_image(const char *image_name, int image_fd, load_symbols(ehdr, image_fd, load_bias); } + debuginfo_report_elf(image_name, image_fd, load_bias); + mmap_unlock(); close(image_fd); diff --git a/accel/tcg/meson.build b/accel/tcg/meson.build index 75e1dffb4d..55b3b4dd7e 100644 --- a/accel/tcg/meson.build +++ b/accel/tcg/meson.build @@ -12,6 +12,7 @@ tcg_ss.add(files( tcg_ss.add(when: 'CONFIG_USER_ONLY', if_true: files('user-exec.c')) tcg_ss.add(when: 'CONFIG_SOFTMMU', if_false: files('user-exec-stub.c')) tcg_ss.add(when: 'CONFIG_PLUGIN', if_true: [files('plugin-gen.c')]) +tcg_ss.add(when: libdw, if_true: files('debuginfo.c')) specific_ss.add_all(when: 'CONFIG_TCG', if_true: tcg_ss) specific_ss.add(when: ['CONFIG_SOFTMMU', 'CONFIG_TCG'], if_true: files( diff --git a/linux-user/meson.build b/linux-user/meson.build index de4320af05..7171dc60be 100644 --- a/linux-user/meson.build +++ b/linux-user/meson.build @@ -22,6 +22,7 @@ linux_user_ss.add(files( 'uname.c', )) linux_user_ss.add(rt) +linux_user_ss.add(libdw) linux_user_ss.add(when: 'TARGET_HAS_BFLT', if_true: files('flatload.c')) linux_user_ss.add(when: 'TARGET_I386', if_true: files('vm86.c')) From patchwork Mon Jan 16 22:36:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 643108 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp842014pvb; Mon, 16 Jan 2023 14:37:24 -0800 (PST) X-Google-Smtp-Source: AMrXdXukSjycEPnnqQBiKmyH28/xjhdvgc1kfg3dJrSvtI8OO0NCQnVHFB4UZf7gcfVZGZd2MJR1 X-Received: by 2002:ac8:5b90:0:b0:3a8:30c9:ba8f with SMTP id a16-20020ac85b90000000b003a830c9ba8fmr1389911qta.28.1673908643902; Mon, 16 Jan 2023 14:37:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673908643; cv=none; d=google.com; s=arc-20160816; b=uS76SBXoX0nAlu4M9adjpRorFmoi4IHqwbu7uv8db7YGwI54W/lkJo6f8bq5iENB9z OlXRN/EZ23oiqC9kaBx95K1AhUWvA3YFqqv47yBSR2QsTECHx6xM9hZ8ZDF7Epy/Rdh0 o8s0xBWMmVDicc0epbcMr05Opk3TNeBKOpNGfKH7IKDVyHNPfgyVXCdZb3m9JoYTnuQR b8jP+6ZKdULc5AX/gvmDD/kXBvuIGIV6S1b/BVhof1aExgVdodkJsPX5mJUOd2iuoQB1 0NJudoA4uF+/7Wvm7+WbU0GSjCklzNDwk0cl7sPEazsyMLzXtjK75BgbQ3UTzPVzJ+QJ 0CJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to :from:dkim-signature; bh=/wYfKQTYOcZ0ofMN4FjmG8yGu5vhzwJPZoaR3YT8hBI=; b=NqCTBPGFKofoueTk2d+G1TSJw1xJLrn5c0lWsLww29ucE08ZHNXj1S3hESq/isyK7z s0dxvkx2BgAQO3Xk/26vXAJkijAkE2cWB536GYv/l6hLo7x4inSNe1f++OjbTKfD+IzJ 9VAhdiDa5GdD4He82w5rXevgA3c8O6HwqbzrQJr47IxtAIh49UOtVdeMG5g7JbuIkUy0 dzBjPLPPQIbGgaabc0BUjy1Y+NEtmGK9kuP36OrSk5qK+gepUCMleBcxoKrdjUj7KWNs lcTOdBsR4kOY3OkJhpohhIvn7V0FGbj6AjILvF61cJyCMKMhvjSdQIXXSL3kq+V6UXyd OIDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SKMbDKMp; 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=pass (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 cj19-20020a05622a259300b0035d43b31782si1060700qtb.389.2023.01.16.14.37.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 16 Jan 2023 14:37:23 -0800 (PST) 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=pass header.i=@linaro.org header.s=google header.b=SKMbDKMp; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pHY69-0000f7-Cy; Mon, 16 Jan 2023 17:37:01 -0500 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 1pHY5y-0000Zg-7n for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:58 -0500 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pHY5v-0001Md-AF for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:49 -0500 Received: by mail-pl1-x62b.google.com with SMTP id p24so31649436plw.11 for ; Mon, 16 Jan 2023 14:36:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=/wYfKQTYOcZ0ofMN4FjmG8yGu5vhzwJPZoaR3YT8hBI=; b=SKMbDKMpvk7HUmhdniSIlzME5WBw6Rb3Hi+enDRh4T+XSsKj6gF/uSpZBMzhvAUCui 7I7hqGfrJvBM2VBRgwdrwDuBhM5PwyN3BlYDoAm5DfUl9v4dfVze6MS/mfCdjYZt2RVs wumyxMkYEVFbVHKAJsgFeBgfYfbV18iSeglTUjCHgeI4qKoD5fRR8NBTsiP6mF5YSd9B DOfm0xYCayHBmAMna4VmXHqlUhv2YOLQw2gq4C6yPmiCs9KPDnF4JcAawPdQFGOPITeJ sHUbStgCWnzbJXdY/ldNrCll6ehT7XhV9yY1LGYZyB6kImAUkJMAZxtBq4E/ANzhl5IX QWiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=/wYfKQTYOcZ0ofMN4FjmG8yGu5vhzwJPZoaR3YT8hBI=; b=ajR5ku9AyC+sX1ba0eiJdxxYTjJbni4VJfi6NzEgTb5R+8VtBqoewmJEyuDdf3gDn3 D6gEuVLbuUlWkMK6UVWfVhXc/lfw3udYPfAqn7vr/ZPmyJ7pV31CeMgxQcGrIiZkd92q nmu1c5AgYxNFwdO3oPzB8owIJybnu3cJTr3w2vECVE56w19FOiiUl59+CaNS2PiG21L7 Ae+mfrN1W11AyjdnxJQvxZJ9ptRbx4KTMXUrUA3HMj4KT9Qw7Cqlds8DVI7isBFieXG4 04I4TEmvOZPtOd0djv6DTNxF8IFXnHfrtjzvWo1FKtMJwIx1lLGqpcFxAQZMcPU/1tQ4 SN6A== X-Gm-Message-State: AFqh2kpaeAncKglYCzz2rv4pUdIfJeNhse7QQ19v2xOkFz7ev6+XUZKu RbpR6WchU2Tqm1h+1YjNNre6aXx3RRUPm9Zu X-Received: by 2002:a17:903:50b:b0:192:d0a7:a0f4 with SMTP id jn11-20020a170903050b00b00192d0a7a0f4mr1091821plb.51.1673908605615; Mon, 16 Jan 2023 14:36:45 -0800 (PST) Received: from stoup.. (rrcs-173-198-77-218.west.biz.rr.com. [173.198.77.218]) by smtp.gmail.com with ESMTPSA id n3-20020a170903110300b00189f2fdbdd0sm19742492plh.234.2023.01.16.14.36.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Jan 2023 14:36:45 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, Ilya Leoshkevich , "Vanderson M . do Rosario" , =?utf-8?q?Alex_Benn?= =?utf-8?q?=C3=A9e?= Subject: [PULL 3/5] tcg: add perfmap and jitdump Date: Mon, 16 Jan 2023 12:36:35 -1000 Message-Id: <20230116223637.3512814-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230116223637.3512814-1-richard.henderson@linaro.org> References: <20230116223637.3512814-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62b.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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Ilya Leoshkevich Add ability to dump /tmp/perf-.map and jit-.dump. The first one allows the perf tool to map samples to each individual translation block. The second one adds the ability to resolve symbol names, line numbers and inspect JITed code. Example of use: perf record qemu-x86_64 -perfmap ./a.out perf report or perf record -k 1 qemu-x86_64 -jitdump ./a.out DEBUGINFOD_URLS= perf inject -j -i perf.data -o perf.data.jitted perf report -i perf.data.jitted Co-developed-by: Vanderson M. do Rosario Co-developed-by: Alex Bennée Signed-off-by: Ilya Leoshkevich Message-Id: <20230112152013.125680-4-iii@linux.ibm.com> Signed-off-by: Richard Henderson --- docs/devel/tcg.rst | 23 +++ accel/tcg/perf.h | 49 +++++ accel/tcg/perf.c | 375 ++++++++++++++++++++++++++++++++++++++ accel/tcg/translate-all.c | 7 + linux-user/exit.c | 2 + linux-user/main.c | 15 ++ softmmu/vl.c | 11 ++ tcg/tcg.c | 2 + accel/tcg/meson.build | 1 + qemu-options.hx | 20 ++ 10 files changed, 505 insertions(+) create mode 100644 accel/tcg/perf.h create mode 100644 accel/tcg/perf.c diff --git a/docs/devel/tcg.rst b/docs/devel/tcg.rst index 136a7a0d96..b4096a17df 100644 --- a/docs/devel/tcg.rst +++ b/docs/devel/tcg.rst @@ -188,3 +188,26 @@ memory areas instead calls out to C code for device emulation. Finally, the MMU helps tracking dirty pages and pages pointed to by translation blocks. +Profiling JITted code +--------------------- + +The Linux ``perf`` tool will treat all JITted code as a single block as +unlike the main code it can't use debug information to link individual +program counter samples with larger functions. To overcome this +limitation you can use the ``-perfmap`` or the ``-jitdump`` option to generate +map files. ``-perfmap`` is lightweight and produces only guest-host mappings. +``-jitdump`` additionally saves JITed code and guest debug information (if +available); its output needs to be integrated with the ``perf.data`` file +before the final report can be viewed. + +.. code:: + + perf record $QEMU -perfmap $REMAINING_ARGS + perf report + + perf record -k 1 $QEMU -jitdump $REMAINING_ARGS + DEBUGINFOD_URLS= perf inject -j -i perf.data -o perf.data.jitted + perf report -i perf.data.jitted + +Note that qemu-system generates mappings only for ``-kernel`` files in ELF +format. diff --git a/accel/tcg/perf.h b/accel/tcg/perf.h new file mode 100644 index 0000000000..f92dd52c69 --- /dev/null +++ b/accel/tcg/perf.h @@ -0,0 +1,49 @@ +/* + * Linux perf perf-.map and jit-.dump integration. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef ACCEL_TCG_PERF_H +#define ACCEL_TCG_PERF_H + +#if defined(CONFIG_TCG) && defined(CONFIG_LINUX) +/* Start writing perf-.map. */ +void perf_enable_perfmap(void); + +/* Start writing jit-.dump. */ +void perf_enable_jitdump(void); + +/* Add information about TCG prologue to profiler maps. */ +void perf_report_prologue(const void *start, size_t size); + +/* Add information about JITted guest code to profiler maps. */ +void perf_report_code(uint64_t guest_pc, TranslationBlock *tb, + const void *start); + +/* Stop writing perf-.map and/or jit-.dump. */ +void perf_exit(void); +#else +static inline void perf_enable_perfmap(void) +{ +} + +static inline void perf_enable_jitdump(void) +{ +} + +static inline void perf_report_prologue(const void *start, size_t size) +{ +} + +static inline void perf_report_code(uint64_t guest_pc, TranslationBlock *tb, + const void *start) +{ +} + +static inline void perf_exit(void) +{ +} +#endif + +#endif diff --git a/accel/tcg/perf.c b/accel/tcg/perf.c new file mode 100644 index 0000000000..ae19f6e28f --- /dev/null +++ b/accel/tcg/perf.c @@ -0,0 +1,375 @@ +/* + * Linux perf perf-.map and jit-.dump integration. + * + * The jitdump spec can be found at [1]. + * + * [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/tools/perf/Documentation/jitdump-specification.txt + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "elf.h" +#include "exec/exec-all.h" +#include "qemu/timer.h" +#include "tcg/tcg.h" + +#include "debuginfo.h" +#include "perf.h" + +static FILE *safe_fopen_w(const char *path) +{ + int saved_errno; + FILE *f; + int fd; + + /* Delete the old file, if any. */ + unlink(path); + + /* Avoid symlink attacks by using O_CREAT | O_EXCL. */ + fd = open(path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); + if (fd == -1) { + return NULL; + } + + /* Convert fd to FILE*. */ + f = fdopen(fd, "w"); + if (f == NULL) { + saved_errno = errno; + close(fd); + errno = saved_errno; + return NULL; + } + + return f; +} + +static FILE *perfmap; + +void perf_enable_perfmap(void) +{ + char map_file[32]; + + snprintf(map_file, sizeof(map_file), "/tmp/perf-%d.map", getpid()); + perfmap = safe_fopen_w(map_file); + if (perfmap == NULL) { + warn_report("Could not open %s: %s, proceeding without perfmap", + map_file, strerror(errno)); + } +} + +/* Get PC and size of code JITed for guest instruction #INSN. */ +static void get_host_pc_size(uintptr_t *host_pc, uint16_t *host_size, + const void *start, size_t insn) +{ + uint16_t start_off = insn ? tcg_ctx->gen_insn_end_off[insn - 1] : 0; + + if (host_pc) { + *host_pc = (uintptr_t)start + start_off; + } + if (host_size) { + *host_size = tcg_ctx->gen_insn_end_off[insn] - start_off; + } +} + +static const char *pretty_symbol(const struct debuginfo_query *q, size_t *len) +{ + static __thread char buf[64]; + int tmp; + + if (!q->symbol) { + tmp = snprintf(buf, sizeof(buf), "guest-0x%"PRIx64, q->address); + if (len) { + *len = MIN(tmp + 1, sizeof(buf)); + } + return buf; + } + + if (!q->offset) { + if (len) { + *len = strlen(q->symbol) + 1; + } + return q->symbol; + } + + tmp = snprintf(buf, sizeof(buf), "%s+0x%"PRIx64, q->symbol, q->offset); + if (len) { + *len = MIN(tmp + 1, sizeof(buf)); + } + return buf; +} + +static void write_perfmap_entry(const void *start, size_t insn, + const struct debuginfo_query *q) +{ + uint16_t host_size; + uintptr_t host_pc; + + get_host_pc_size(&host_pc, &host_size, start, insn); + fprintf(perfmap, "%"PRIxPTR" %"PRIx16" %s\n", + host_pc, host_size, pretty_symbol(q, NULL)); +} + +static FILE *jitdump; + +#define JITHEADER_MAGIC 0x4A695444 +#define JITHEADER_VERSION 1 + +struct jitheader { + uint32_t magic; + uint32_t version; + uint32_t total_size; + uint32_t elf_mach; + uint32_t pad1; + uint32_t pid; + uint64_t timestamp; + uint64_t flags; +}; + +enum jit_record_type { + JIT_CODE_LOAD = 0, + JIT_CODE_DEBUG_INFO = 2, +}; + +struct jr_prefix { + uint32_t id; + uint32_t total_size; + uint64_t timestamp; +}; + +struct jr_code_load { + struct jr_prefix p; + + uint32_t pid; + uint32_t tid; + uint64_t vma; + uint64_t code_addr; + uint64_t code_size; + uint64_t code_index; +}; + +struct debug_entry { + uint64_t addr; + int lineno; + int discrim; + const char name[]; +}; + +struct jr_code_debug_info { + struct jr_prefix p; + + uint64_t code_addr; + uint64_t nr_entry; + struct debug_entry entries[]; +}; + +static uint32_t get_e_machine(void) +{ + Elf64_Ehdr elf_header; + FILE *exe; + size_t n; + + QEMU_BUILD_BUG_ON(offsetof(Elf32_Ehdr, e_machine) != + offsetof(Elf64_Ehdr, e_machine)); + + exe = fopen("/proc/self/exe", "r"); + if (exe == NULL) { + return EM_NONE; + } + + n = fread(&elf_header, sizeof(elf_header), 1, exe); + fclose(exe); + if (n != 1) { + return EM_NONE; + } + + return elf_header.e_machine; +} + +void perf_enable_jitdump(void) +{ + struct jitheader header; + char jitdump_file[32]; + void *perf_marker; + + if (!use_rt_clock) { + warn_report("CLOCK_MONOTONIC is not available, proceeding without jitdump"); + return; + } + + snprintf(jitdump_file, sizeof(jitdump_file), "jit-%d.dump", getpid()); + jitdump = safe_fopen_w(jitdump_file); + if (jitdump == NULL) { + warn_report("Could not open %s: %s, proceeding without jitdump", + jitdump_file, strerror(errno)); + return; + } + + /* + * `perf inject` will see that the mapped file name in the corresponding + * PERF_RECORD_MMAP or PERF_RECORD_MMAP2 event is of the form jit-%d.dump + * and will process it as a jitdump file. + */ + perf_marker = mmap(NULL, qemu_real_host_page_size(), PROT_READ | PROT_EXEC, + MAP_PRIVATE, fileno(jitdump), 0); + if (perf_marker == MAP_FAILED) { + warn_report("Could not map %s: %s, proceeding without jitdump", + jitdump_file, strerror(errno)); + fclose(jitdump); + jitdump = NULL; + return; + } + + header.magic = JITHEADER_MAGIC; + header.version = JITHEADER_VERSION; + header.total_size = sizeof(header); + header.elf_mach = get_e_machine(); + header.pad1 = 0; + header.pid = getpid(); + header.timestamp = get_clock(); + header.flags = 0; + fwrite(&header, sizeof(header), 1, jitdump); +} + +void perf_report_prologue(const void *start, size_t size) +{ + if (perfmap) { + fprintf(perfmap, "%"PRIxPTR" %zx tcg-prologue-buffer\n", + (uintptr_t)start, size); + } +} + +/* Write a JIT_CODE_DEBUG_INFO jitdump entry. */ +static void write_jr_code_debug_info(const void *start, + const struct debuginfo_query *q, + size_t icount) +{ + struct jr_code_debug_info rec; + struct debug_entry ent; + uintptr_t host_pc; + int insn; + + /* Write the header. */ + rec.p.id = JIT_CODE_DEBUG_INFO; + rec.p.total_size = sizeof(rec) + sizeof(ent) + 1; + rec.p.timestamp = get_clock(); + rec.code_addr = (uintptr_t)start; + rec.nr_entry = 1; + for (insn = 0; insn < icount; insn++) { + if (q[insn].file) { + rec.p.total_size += sizeof(ent) + strlen(q[insn].file) + 1; + rec.nr_entry++; + } + } + fwrite(&rec, sizeof(rec), 1, jitdump); + + /* Write the main debug entries. */ + for (insn = 0; insn < icount; insn++) { + if (q[insn].file) { + get_host_pc_size(&host_pc, NULL, start, insn); + ent.addr = host_pc; + ent.lineno = q[insn].line; + ent.discrim = 0; + fwrite(&ent, sizeof(ent), 1, jitdump); + fwrite(q[insn].file, strlen(q[insn].file) + 1, 1, jitdump); + } + } + + /* Write the trailing debug_entry. */ + ent.addr = (uintptr_t)start + tcg_ctx->gen_insn_end_off[icount - 1]; + ent.lineno = 0; + ent.discrim = 0; + fwrite(&ent, sizeof(ent), 1, jitdump); + fwrite("", 1, 1, jitdump); +} + +/* Write a JIT_CODE_LOAD jitdump entry. */ +static void write_jr_code_load(const void *start, uint16_t host_size, + const struct debuginfo_query *q) +{ + static uint64_t code_index; + struct jr_code_load rec; + const char *symbol; + size_t symbol_size; + + symbol = pretty_symbol(q, &symbol_size); + rec.p.id = JIT_CODE_LOAD; + rec.p.total_size = sizeof(rec) + symbol_size + host_size; + rec.p.timestamp = get_clock(); + rec.pid = getpid(); + rec.tid = qemu_get_thread_id(); + rec.vma = (uintptr_t)start; + rec.code_addr = (uintptr_t)start; + rec.code_size = host_size; + rec.code_index = code_index++; + fwrite(&rec, sizeof(rec), 1, jitdump); + fwrite(symbol, symbol_size, 1, jitdump); + fwrite(start, host_size, 1, jitdump); +} + +void perf_report_code(uint64_t guest_pc, TranslationBlock *tb, + const void *start) +{ + struct debuginfo_query *q; + size_t insn; + + if (!perfmap && !jitdump) { + return; + } + + q = g_try_malloc0_n(tb->icount, sizeof(*q)); + if (!q) { + return; + } + + debuginfo_lock(); + + /* Query debuginfo for each guest instruction. */ + for (insn = 0; insn < tb->icount; insn++) { + /* FIXME: This replicates the restore_state_to_opc() logic. */ + q[insn].address = tcg_ctx->gen_insn_data[insn][0]; + if (TARGET_TB_PCREL) { + q[insn].address |= (guest_pc & TARGET_PAGE_MASK); + } else { +#if defined(TARGET_I386) + q[insn].address -= tb->cs_base; +#endif + } + q[insn].flags = DEBUGINFO_SYMBOL | (jitdump ? DEBUGINFO_LINE : 0); + } + debuginfo_query(q, tb->icount); + + /* Emit perfmap entries if needed. */ + if (perfmap) { + flockfile(perfmap); + for (insn = 0; insn < tb->icount; insn++) { + write_perfmap_entry(start, insn, &q[insn]); + } + funlockfile(perfmap); + } + + /* Emit jitdump entries if needed. */ + if (jitdump) { + flockfile(jitdump); + write_jr_code_debug_info(start, q, tb->icount); + write_jr_code_load(start, tcg_ctx->gen_insn_end_off[tb->icount - 1], + q); + funlockfile(jitdump); + } + + debuginfo_unlock(); + g_free(q); +} + +void perf_exit(void) +{ + if (perfmap) { + fclose(perfmap); + perfmap = NULL; + } + + if (jitdump) { + fclose(jitdump); + jitdump = NULL; + } +} diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 51ac1f6c84..979f8e1107 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -62,6 +62,7 @@ #include "tb-hash.h" #include "tb-context.h" #include "internal.h" +#include "perf.h" /* Make sure all possible CPU event bits fit in tb->trace_vcpu_dstate */ QEMU_BUILD_BUG_ON(CPU_TRACE_DSTATE_MAX_EVENTS > @@ -406,6 +407,12 @@ TranslationBlock *tb_gen_code(CPUState *cpu, } tb->tc.size = gen_code_size; + /* + * For TARGET_TB_PCREL, attribute all executions of the generated + * code to its first mapping. + */ + perf_report_code(pc, tb, tcg_splitwx_to_rx(gen_code_buf)); + #ifdef CONFIG_PROFILER qatomic_set(&prof->code_time, prof->code_time + profile_getclock() - ti); qatomic_set(&prof->code_in_len, prof->code_in_len + tb->size); diff --git a/linux-user/exit.c b/linux-user/exit.c index fa6ef0b9b4..607b6da9fc 100644 --- a/linux-user/exit.c +++ b/linux-user/exit.c @@ -17,6 +17,7 @@ * along with this program; if not, see . */ #include "qemu/osdep.h" +#include "accel/tcg/perf.h" #include "exec/gdbstub.h" #include "qemu.h" #include "user-internals.h" @@ -38,4 +39,5 @@ void preexit_cleanup(CPUArchState *env, int code) #endif gdb_exit(code); qemu_plugin_user_exit(); + perf_exit(); } diff --git a/linux-user/main.c b/linux-user/main.c index a17fed045b..4290651c3c 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -53,6 +53,7 @@ #include "signal-common.h" #include "loader.h" #include "user-mmap.h" +#include "accel/tcg/perf.h" #ifdef CONFIG_SEMIHOSTING #include "semihosting/semihost.h" @@ -423,6 +424,16 @@ static void handle_arg_abi_call0(const char *arg) } #endif +static void handle_arg_perfmap(const char *arg) +{ + perf_enable_perfmap(); +} + +static void handle_arg_jitdump(const char *arg) +{ + perf_enable_jitdump(); +} + static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins); #ifdef CONFIG_PLUGIN @@ -493,6 +504,10 @@ static const struct qemu_argument arg_table[] = { {"xtensa-abi-call0", "QEMU_XTENSA_ABI_CALL0", false, handle_arg_abi_call0, "", "assume CALL0 Xtensa ABI"}, #endif + {"perfmap", "QEMU_PERFMAP", false, handle_arg_perfmap, + "", "Generate a /tmp/perf-${pid}.map file for perf"}, + {"jitdump", "QEMU_JITDUMP", false, handle_arg_jitdump, + "", "Generate a jit-${pid}.dump file for perf"}, {NULL, NULL, false, NULL, NULL, NULL} }; diff --git a/softmmu/vl.c b/softmmu/vl.c index 9bd0e52d01..9177d95d4e 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -96,6 +96,9 @@ #include "fsdev/qemu-fsdev.h" #endif #include "sysemu/qtest.h" +#ifdef CONFIG_TCG +#include "accel/tcg/perf.h" +#endif #include "disas/disas.h" @@ -2926,6 +2929,14 @@ void qemu_init(int argc, char **argv) case QEMU_OPTION_DFILTER: qemu_set_dfilter_ranges(optarg, &error_fatal); break; +#if defined(CONFIG_TCG) && defined(CONFIG_LINUX) + case QEMU_OPTION_perfmap: + perf_enable_perfmap(); + break; + case QEMU_OPTION_jitdump: + perf_enable_jitdump(); + break; +#endif case QEMU_OPTION_seed: qemu_guest_random_seed_main(optarg, &error_fatal); break; diff --git a/tcg/tcg.c b/tcg/tcg.c index da91779890..9b7df71e7a 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -61,6 +61,7 @@ #include "exec/log.h" #include "tcg/tcg-ldst.h" #include "tcg-internal.h" +#include "accel/tcg/perf.h" /* Forward declarations for functions declared in tcg-target.c.inc and used here. */ @@ -913,6 +914,7 @@ void tcg_prologue_init(TCGContext *s) #endif prologue_size = tcg_current_code_size(s); + perf_report_prologue(s->code_gen_ptr, prologue_size); #ifndef CONFIG_TCG_INTERPRETER flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf), diff --git a/accel/tcg/meson.build b/accel/tcg/meson.build index 55b3b4dd7e..77740b1a0d 100644 --- a/accel/tcg/meson.build +++ b/accel/tcg/meson.build @@ -13,6 +13,7 @@ tcg_ss.add(when: 'CONFIG_USER_ONLY', if_true: files('user-exec.c')) tcg_ss.add(when: 'CONFIG_SOFTMMU', if_false: files('user-exec-stub.c')) tcg_ss.add(when: 'CONFIG_PLUGIN', if_true: [files('plugin-gen.c')]) tcg_ss.add(when: libdw, if_true: files('debuginfo.c')) +tcg_ss.add(when: 'CONFIG_LINUX', if_true: files('perf.c')) specific_ss.add_all(when: 'CONFIG_TCG', if_true: tcg_ss) specific_ss.add(when: ['CONFIG_SOFTMMU', 'CONFIG_TCG'], if_true: files( diff --git a/qemu-options.hx b/qemu-options.hx index 3aa3a2f5a3..d59d19704b 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -4838,6 +4838,26 @@ SRST Enable synchronization profiling. ERST +#if defined(CONFIG_TCG) && defined(CONFIG_LINUX) +DEF("perfmap", 0, QEMU_OPTION_perfmap, + "-perfmap generate a /tmp/perf-${pid}.map file for perf\n", + QEMU_ARCH_ALL) +SRST +``-perfmap`` + Generate a map file for Linux perf tools that will allow basic profiling + information to be broken down into basic blocks. +ERST + +DEF("jitdump", 0, QEMU_OPTION_jitdump, + "-jitdump generate a jit-${pid}.dump file for perf\n", + QEMU_ARCH_ALL) +SRST +``-jitdump`` + Generate a dump file for Linux perf tools that maps basic blocks to symbol + names, line numbers and JITted code. +ERST +#endif + DEFHEADING() DEFHEADING(Generic object creation:) From patchwork Mon Jan 16 22:36:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 643113 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp842350pvb; Mon, 16 Jan 2023 14:38:20 -0800 (PST) X-Google-Smtp-Source: AMrXdXtWtS9L4Zk6bXI5MGbhSqeR+6SrL9oR0/VN1B3fzzAJVgV8BGl7QokBqWOpvtnZsTE89uGk X-Received: by 2002:ac8:1090:0:b0:3b1:89e:ccc2 with SMTP id a16-20020ac81090000000b003b1089eccc2mr814850qtj.48.1673908700582; Mon, 16 Jan 2023 14:38:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673908700; cv=none; d=google.com; s=arc-20160816; b=PD1rXXi1k1ygU8oYh35VyjbaLZ8DxP1OV5GcUoAkFv8Px/dbuyv8kouvtXg/7h4yq2 rcg5Fo/TbXm3gisJxBAM66d0pAqI+rLkms1k0vvFXGGEl1b9hmRnEB+t3O8XEYJ3RpW5 fJ+IuuSU6AySmdX+ADXGHNUDvTJuw0q9gpXV7dDxmEsuyiWlOGBjX6mjFxPbJrmLoihT 9/Ig27Uv6gukyI1WfveOE5l6mVlXTp3fCtf5Sdffp8nslXTGNOYwt/P9rlhZYqKA/TgP qYo6NbYEAnmHeGJtnvuvB6iw+K97CtZ2BKRwgeZXNLcRKQ++uApiL9GX1Odvyi6ABxQq 3AiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to :from:dkim-signature; bh=W+D/HiblqeSFw2GirhfaFCRyev36g0CtLDjZtrpyEfc=; b=qAXxKV4Q0eCrIpgyablg7Jq90XppYjnmfkLTVArBHFGOtJ/3IASTOWTNg4airt4Ou+ 2HzZ4n0JgktcQi77/FDmvfcXx/6mLSK6Hmtuc07dgEjTsGo+GmflWPpdFg2l53Cn4MGS HzgcyOVyqCUhvrwkLT8NkgR4AsogzW4IfpM0UJf2TMsm24d6ZRNLXhKrMaYdzeToAfDP H5aUDEdXXO+cITOa8eR/A1O7rrLSuaFefITIuaef+vVP4nz4RQJ9aNxAhkc8WJePy7id jnb2e9C2g9ov1H4t5rqVX4XjibvdLGZfIUtm8Q7PnfL1Vf7B5JdFMtQTP0YCL+VY10AD u6pA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=DxdrDPS+; 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=pass (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 i17-20020a05620a405100b007055fa9305fsi19258083qko.505.2023.01.16.14.38.20 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 16 Jan 2023 14:38:20 -0800 (PST) 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=pass header.i=@linaro.org header.s=google header.b=DxdrDPS+; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pHY6D-0000gf-HU; Mon, 16 Jan 2023 17:37:05 -0500 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 1pHY62-0000a3-91 for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:59 -0500 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pHY5w-0001Mn-Kp for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:50 -0500 Received: by mail-pl1-x632.google.com with SMTP id jn22so31642958plb.13 for ; Mon, 16 Jan 2023 14:36:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=W+D/HiblqeSFw2GirhfaFCRyev36g0CtLDjZtrpyEfc=; b=DxdrDPS+LR6MmGfCoxvr2gDPhFcyeycCwe5lJ+nlGXBKMu2tqsU+Sg2+qTTyVdGQbp FZxaMC6+UozFE1WMeSMZd4XOS+E0M0X1UmVfmABMiODSTGjs29z7cwQLoAOK7+faRvIM BmSJ9HCCwu06jjQ9ySR/Q75frpey/QBcIZtmd4RsKhoCyorIK6z2hRyuLkjV/xYmmU+F fUBO6We7/7ThqgArfV22EpE+SGCuavAtFtgUwB8wzPMNywZLIYzJ1dx3uNOS/pmt+10Q jPdUOgumcHwcvRfY6FBnSGc+ETkb5+GLuq9cU+YmbZCGiPrh368KLOR8e0tnx8YdNqr1 1Vvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=W+D/HiblqeSFw2GirhfaFCRyev36g0CtLDjZtrpyEfc=; b=tfvbIEi67gb041eEVla8v6SRvoYeMUsFnl+ZsHIyA3V6DsMApK2x82+ZYiqMCcLFjv MWdXxUnHaOaik0IIeKRw/K9pXbMva7eUxqJ1HxvohjmyttYs+L+K8YZ/g8WgGfASxGHp sq82I6uqTxMS4uQUl1NaGa0G9iDbCPlbT/AEFnwn9PjOUxjbXVX4ZFeYTFfIT4tO+KEc rQgGoDvOykfEegCwWyw3yzehCb60n/owlxPSqqxec/IETredPiuUnh4REnrJJfgQUuHS R6V/ovMVjDvFlnzJ+U2cijP6TbRd1tE/Fc+hTbDf19ineOl8GPsMiRfBH0YGmm1k7fXQ 737g== X-Gm-Message-State: AFqh2kr6wD3JhZMEpfl+1diFCELwQHYCQrI5lpz56thpcB+wF+70TFJy sgSA9FGsjM0+GAWFq8kpZ1273qdNHqrbicAy X-Received: by 2002:a17:902:720a:b0:193:25b6:71bc with SMTP id ba10-20020a170902720a00b0019325b671bcmr1416721plb.25.1673908607199; Mon, 16 Jan 2023 14:36:47 -0800 (PST) Received: from stoup.. (rrcs-173-198-77-218.west.biz.rr.com. [173.198.77.218]) by smtp.gmail.com with ESMTPSA id n3-20020a170903110300b00189f2fdbdd0sm19742492plh.234.2023.01.16.14.36.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Jan 2023 14:36:46 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= Subject: [PULL 4/5] util/bufferiszero: Use __attribute__((target)) for avx2/avx512 Date: Mon, 16 Jan 2023 12:36:36 -1000 Message-Id: <20230116223637.3512814-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230116223637.3512814-1-richard.henderson@linaro.org> References: <20230116223637.3512814-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x632.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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Use the attribute, which is supported by clang, instead of the #pragma, which is not supported and, for some reason, also not detected by the meson probe, so we fail by -Werror. Include only as that is the outermost "official" header for these intrinsics -- emmintrin.h and smmintrin -- are older SSE2 and SSE4 specific headers, while the immintrin.h includes all of the Intel intrinsics. Reviewed-by: Daniel P. Berrangé Signed-off-by: Richard Henderson --- meson.build | 8 ++------ util/bufferiszero.c | 41 ++++++----------------------------------- 2 files changed, 8 insertions(+), 41 deletions(-) diff --git a/meson.build b/meson.build index 6d212f6c8e..58d8cd68a6 100644 --- a/meson.build +++ b/meson.build @@ -2338,11 +2338,9 @@ config_host_data.set('CONFIG_CPUID_H', have_cpuid_h) config_host_data.set('CONFIG_AVX2_OPT', get_option('avx2') \ .require(have_cpuid_h, error_message: 'cpuid.h not available, cannot enable AVX2') \ .require(cc.links(''' - #pragma GCC push_options - #pragma GCC target("avx2") #include #include - static int bar(void *a) { + static int __attribute__((target("avx2"))) bar(void *a) { __m256i x = *(__m256i *)a; return _mm256_testz_si256(x, x); } @@ -2352,11 +2350,9 @@ config_host_data.set('CONFIG_AVX2_OPT', get_option('avx2') \ config_host_data.set('CONFIG_AVX512F_OPT', get_option('avx512f') \ .require(have_cpuid_h, error_message: 'cpuid.h not available, cannot enable AVX512F') \ .require(cc.links(''' - #pragma GCC push_options - #pragma GCC target("avx512f") #include #include - static int bar(void *a) { + static int __attribute__((target("avx512f"))) bar(void *a) { __m512i x = *(__m512i *)a; return _mm512_test_epi64_mask(x, x); } diff --git a/util/bufferiszero.c b/util/bufferiszero.c index ec3cd4ca15..1790ded7d4 100644 --- a/util/bufferiszero.c +++ b/util/bufferiszero.c @@ -64,18 +64,11 @@ buffer_zero_int(const void *buf, size_t len) } #if defined(CONFIG_AVX512F_OPT) || defined(CONFIG_AVX2_OPT) || defined(__SSE2__) -/* Do not use push_options pragmas unnecessarily, because clang - * does not support them. - */ -#if defined(CONFIG_AVX512F_OPT) || defined(CONFIG_AVX2_OPT) -#pragma GCC push_options -#pragma GCC target("sse2") -#endif -#include +#include /* Note that each of these vectorized functions require len >= 64. */ -static bool +static bool __attribute__((target("sse2"))) buffer_zero_sse2(const void *buf, size_t len) { __m128i t = _mm_loadu_si128(buf); @@ -104,20 +97,9 @@ buffer_zero_sse2(const void *buf, size_t len) return _mm_movemask_epi8(_mm_cmpeq_epi8(t, zero)) == 0xFFFF; } -#if defined(CONFIG_AVX512F_OPT) || defined(CONFIG_AVX2_OPT) -#pragma GCC pop_options -#endif #ifdef CONFIG_AVX2_OPT -/* Note that due to restrictions/bugs wrt __builtin functions in gcc <= 4.8, - * the includes have to be within the corresponding push_options region, and - * therefore the regions themselves have to be ordered with increasing ISA. - */ -#pragma GCC push_options -#pragma GCC target("sse4") -#include - -static bool +static bool __attribute__((target("sse4"))) buffer_zero_sse4(const void *buf, size_t len) { __m128i t = _mm_loadu_si128(buf); @@ -145,12 +127,7 @@ buffer_zero_sse4(const void *buf, size_t len) return _mm_testz_si128(t, t); } -#pragma GCC pop_options -#pragma GCC push_options -#pragma GCC target("avx2") -#include - -static bool +static bool __attribute__((target("avx2"))) buffer_zero_avx2(const void *buf, size_t len) { /* Begin with an unaligned head of 32 bytes. */ @@ -176,15 +153,10 @@ buffer_zero_avx2(const void *buf, size_t len) return _mm256_testz_si256(t, t); } -#pragma GCC pop_options #endif /* CONFIG_AVX2_OPT */ #ifdef CONFIG_AVX512F_OPT -#pragma GCC push_options -#pragma GCC target("avx512f") -#include - -static bool +static bool __attribute__((target("avx512f"))) buffer_zero_avx512(const void *buf, size_t len) { /* Begin with an unaligned head of 64 bytes. */ @@ -210,8 +182,7 @@ buffer_zero_avx512(const void *buf, size_t len) return !_mm512_test_epi64_mask(t, t); } -#pragma GCC pop_options -#endif +#endif /* CONFIG_AVX512F_OPT */ /* Note that for test_buffer_is_zero_next_accel, the most preferred From patchwork Mon Jan 16 22:36:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 643111 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp842322pvb; Mon, 16 Jan 2023 14:38:12 -0800 (PST) X-Google-Smtp-Source: AMrXdXvzaMBwPS8oznb7UQtgFhXlY5i0Bi+GMv/z9tupt4hrou1zxj5sSEOQ8Qaw0XsOGI50dOBZ X-Received: by 2002:a05:6214:5d82:b0:4c7:7237:76ff with SMTP id mf2-20020a0562145d8200b004c7723776ffmr1878843qvb.42.1673908692161; Mon, 16 Jan 2023 14:38:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673908692; cv=none; d=google.com; s=arc-20160816; b=e3q2RzhxVhLGB+DwdK6gKdujTiiJkOC4BRfRecr8J/uDhuXNGz0iKFXrc5kYHVUEfW XGUWnOn6MM8eo5NKIeF7q0WBo46Tx/PXXa48UymCFgbWnAuKDON/9qHe84J8mZJAGPSY 0fDM6CupohCHm6P58Y7oln4N/uZ8eMqaOT93j5J+1o9gy6SmtKl3Qi7hNw7RlSDuRs+i /G0+8VOhpfn2ml5xXO3aWsTKIX74A7LJJVjq+3AzXzuou2Drhp99HHfpNlZSBElxSdH7 i+7uzkunKlnwXxDjW41HOslmaJVVlhAFXrYRziuPQaV+J9Bm7SSFb2QPW768lbs/ZDCg awDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to :from:dkim-signature; bh=0l2tmn7cqPSHygkIlv8PBR3kE2IV45gjKZ9i2g3E1kk=; b=xUEP88rj1slLlNghSrm5ncqZxJjpDLyskUD/MPz6FzJwWbkWWRIRy6sqJQZUWYh4+4 LWICkxqj+cIZuhzi1uEd/xcqyoImeJ/K2ulvFThT1kOr6Ty9oA4/j4HqMcyQJc784vjz KZItDrEkLosgKc2+4I4Ns5nW4lBdkb98NgZYVJsDMFOWZsqieU9I6atAvWDOZtRWhXN3 dknBMfVBNvja0cdK4J6tTGWIpKBebj1IEtJBr31g3KyrGcGtGJobQqlxDOwlMMKB0ifZ bAB6FN30Ls++JJ87EZfv9zCrnZYbIGzEYOZrcVnCejm0UihBJb+atFBUOsGXMUA+4Ij7 kgiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=G9yOxsLG; 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=pass (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 jx10-20020a0562142b0a00b00535256ea443si699724qvb.3.2023.01.16.14.38.12 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 16 Jan 2023 14:38:12 -0800 (PST) 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=pass header.i=@linaro.org header.s=google header.b=G9yOxsLG; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pHY6E-0000gy-3r; Mon, 16 Jan 2023 17:37:06 -0500 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 1pHY64-0000a8-Cb for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:59 -0500 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pHY62-0001Mr-0y for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:55 -0500 Received: by mail-pj1-x1034.google.com with SMTP id o13so26963573pjg.2 for ; Mon, 16 Jan 2023 14:36:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=0l2tmn7cqPSHygkIlv8PBR3kE2IV45gjKZ9i2g3E1kk=; b=G9yOxsLGKG4hd4ZroJ9tV/vKLiM1AxaTdxUobDNumRGFf3ujeidAfb0McXLJNxdw6F ajcLejxsnayTe7/CmUyALsJ/Bg5cHH159P/CMrnvb0ILvw99+RG0vBQ/mudGXFhNEGAZ V9ATeSDclmU1ZS2ADCwjaVAQGDVxPCj/N61KtvMxh9ZtOt1ct1/U8jIwxVETRUskt/+6 gpj+fyEK4WpOrN6f4uh90wUtrNUYaShBW49aDOgQlWUOf0U9h4zNY0DKMi2aO7OICCQl IL+K5CpeaUqKIAg0fPyzDLWjzsLktt40Ew4JAk982Vsd/KgSsAgciDTrm6lyH3X1c2vJ 7yHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=0l2tmn7cqPSHygkIlv8PBR3kE2IV45gjKZ9i2g3E1kk=; b=ULyQoD9/5UYCollGGtYXyGFrvvPuh1u1tolONXSiLO/1J6TYZimN/z1Nyx3kYUu7sA /VlYPT0MBaCfFtCpJj7q/SB+2t9B+0xCTgjEPe7tvirGHrskHUGSZ9KWH8rNtZKTmJyG 56zbn4hvzBhU45j6mtowgGqlSuk1bybWtpWW0m3zT+l951ONddUQHTFHj/NKAQiaXTlz Rfiu/rXt6Qfo5lKifeyjzSLh7+663HsDkVzJt0ZCNmnCbisN+oi0m6gbZ/TYETBh9N07 aZufA6AlYbgzLLnKag0P5l1Fl5plJu0R/3HPjWPaNgGySRTLCBQLFlaGdjVlJRavQGEi 6YQQ== X-Gm-Message-State: AFqh2kpP5U7ruFTxxUtlh9u1C787FK7aN+Di4Vs5dYWKRjGn6MRZ/+lz gAvqP2IdJl+g2VYWEoX2ix6zCQNJjPtmJgVu X-Received: by 2002:a17:902:968f:b0:194:79b1:ea27 with SMTP id n15-20020a170902968f00b0019479b1ea27mr1113546plp.50.1673908608682; Mon, 16 Jan 2023 14:36:48 -0800 (PST) Received: from stoup.. (rrcs-173-198-77-218.west.biz.rr.com. [173.198.77.218]) by smtp.gmail.com with ESMTPSA id n3-20020a170903110300b00189f2fdbdd0sm19742492plh.234.2023.01.16.14.36.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Jan 2023 14:36:48 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PULL 5/5] accel/tcg: Split out cpu_exec_{setjmp,loop} Date: Mon, 16 Jan 2023 12:36:37 -1000 Message-Id: <20230116223637.3512814-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230116223637.3512814-1-richard.henderson@linaro.org> References: <20230116223637.3512814-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1034; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1034.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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Recently the g_assert(cpu == current_cpu) test has been intermittently failing with gcc. Reorg the code around the setjmp to minimize the lifetime of the cpu variable affected by the setjmp. This appears to fix the existing issue with clang as well. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1147 Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson --- accel/tcg/cpu-exec.c | 111 +++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 57 deletions(-) diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c index 356fe348de..8927092537 100644 --- a/accel/tcg/cpu-exec.c +++ b/accel/tcg/cpu-exec.c @@ -909,64 +909,10 @@ static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb, /* main execution loop */ -int cpu_exec(CPUState *cpu) +static int __attribute__((noinline)) +cpu_exec_loop(CPUState *cpu, SyncClocks *sc) { int ret; - SyncClocks sc = { 0 }; - - /* replay_interrupt may need current_cpu */ - current_cpu = cpu; - - if (cpu_handle_halt(cpu)) { - return EXCP_HALTED; - } - - rcu_read_lock(); - - cpu_exec_enter(cpu); - - /* Calculate difference between guest clock and host clock. - * This delay includes the delay of the last cycle, so - * what we have to do is sleep until it is 0. As for the - * advance/delay we gain here, we try to fix it next time. - */ - init_delay_params(&sc, cpu); - - /* prepare setjmp context for exception handling */ - if (sigsetjmp(cpu->jmp_env, 0) != 0) { -#if defined(__clang__) - /* - * Some compilers wrongly smash all local variables after - * siglongjmp (the spec requires that only non-volatile locals - * which are changed between the sigsetjmp and siglongjmp are - * permitted to be trashed). There were bug reports for gcc - * 4.5.0 and clang. The bug is fixed in all versions of gcc - * that we support, but is still unfixed in clang: - * https://bugs.llvm.org/show_bug.cgi?id=21183 - * - * Reload an essential local variable here for those compilers. - * Newer versions of gcc would complain about this code (-Wclobbered), - * so we only perform the workaround for clang. - */ - cpu = current_cpu; -#else - /* Non-buggy compilers preserve this; assert the correct value. */ - g_assert(cpu == current_cpu); -#endif - -#ifndef CONFIG_SOFTMMU - clear_helper_retaddr(); - if (have_mmap_lock()) { - mmap_unlock(); - } -#endif - if (qemu_mutex_iothread_locked()) { - qemu_mutex_unlock_iothread(); - } - qemu_plugin_disable_mem_helpers(cpu); - - assert_no_pages_locked(); - } /* if an exception is pending, we execute it here */ while (!cpu_handle_exception(cpu, &ret)) { @@ -1033,9 +979,60 @@ int cpu_exec(CPUState *cpu) /* Try to align the host and virtual clocks if the guest is in advance */ - align_clocks(&sc, cpu); + align_clocks(sc, cpu); } } + return ret; +} + +static int cpu_exec_setjmp(CPUState *cpu, SyncClocks *sc) +{ + /* Prepare setjmp context for exception handling. */ + if (unlikely(sigsetjmp(cpu->jmp_env, 0) != 0)) { + /* Non-buggy compilers preserve this; assert the correct value. */ + g_assert(cpu == current_cpu); + +#ifndef CONFIG_SOFTMMU + clear_helper_retaddr(); + if (have_mmap_lock()) { + mmap_unlock(); + } +#endif + if (qemu_mutex_iothread_locked()) { + qemu_mutex_unlock_iothread(); + } + qemu_plugin_disable_mem_helpers(cpu); + + assert_no_pages_locked(); + } + + return cpu_exec_loop(cpu, sc); +} + +int cpu_exec(CPUState *cpu) +{ + int ret; + SyncClocks sc = { 0 }; + + /* replay_interrupt may need current_cpu */ + current_cpu = cpu; + + if (cpu_handle_halt(cpu)) { + return EXCP_HALTED; + } + + rcu_read_lock(); + cpu_exec_enter(cpu); + + /* + * Calculate difference between guest clock and host clock. + * This delay includes the delay of the last cycle, so + * what we have to do is sleep until it is 0. As for the + * advance/delay we gain here, we try to fix it next time. + */ + init_delay_params(&sc, cpu); + + ret = cpu_exec_setjmp(cpu, &sc); cpu_exec_exit(cpu); rcu_read_unlock();