From patchwork Mon Jun 17 06:28:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 804834 Delivered-To: patch@linaro.org Received: by 2002:adf:fb90:0:b0:360:93e7:1765 with SMTP id a16csp423745wrr; Sun, 16 Jun 2024 23:30:23 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVzrahOcZZ3XvWeyRSYcP0czBZcep929qKdu/fq9Q0h+45CiXupmMwEKh/k10/pVjhW0S/IF10ciE+w81ty/ZTp X-Google-Smtp-Source: AGHT+IFH6+LV46DNU69m9q/1xFoduKR+afIq5PThZ23Kye/uKugxu4/ySo0YgSaFr5tejSaIQI5A X-Received: by 2002:a05:6214:21e9:b0:6b1:e371:99d9 with SMTP id 6a1803df08f44-6b2afd66b39mr118348646d6.8.1718605823664; Sun, 16 Jun 2024 23:30:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718605823; cv=none; d=google.com; s=arc-20160816; b=qj4ADXbvRgv7mb+JeeGOtQwggRi3lEtSg5kpX2BV9zP9TUJBJ1joi/i9BuT5pYUWS+ uy8T0ahcVgoQRNJVVmRLm2RzzdaBcrcowgXkGCr35YSgd+OpIToexbqpkg+KogOo9c3+ xdOhePU35U90BANZ04ojxeKzitiDE1IisAz5d/nlnI/BOfeZCoz5+TTOOnkZfHgYyDmD Su1XQWRW/Zkjhg1nTzcizwUAow+Go/NaI6XAgWaaUxle1b3yv2+wPX0PuvhaoFLeoIP/ IaLpQ64GXaLe14+3ItAFzOqacFG4WrjNvF6YnMBY16bUjxKUwBcVBxXnaTQBMaR/LTQy YayA== 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=LjO3sCIUBtHps7O0z2KWESgFmJaLDV1pcN1gD8v3XwE=; fh=ZLVo7h1WCYX+mCQET83n+8bPmyNFw4pMQxFlbH8xyms=; b=LxInrSeO7hjX5CCKseAkuI1cya0wlYSpF5z6Fppb9DzSArT9oQ6mEuD8kkICHgkoRh QfzZFJAPKqj4lfG7RhvQ+0AdLTEeSqG21Eh0cBWDpwOXu0JsNp/sVlz/bvfopH+HwSjP GNlerKswdB1KRyQBhgX/fxXsc2IAuNKgFL226qv+62Aj0lsOf7dl+YlwbIzDy/NwHBVI XVLuHiS2l02SSu9NSG+cUFktDT1pz3sU+SDCnZKIcILMwkf4+Ard2Vp/JSgMr3/RL6+s hMn3K+nR5WHoNl69emI6jpFyT5fUuX5jxdNZhAxaNl+o7votZ6+arULHpHCkAcLzlk7i k7ww==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KsrFPOy6; 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 6a1803df08f44-6b2a5afe3d8si96217236d6.203.2024.06.16.23.30.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 16 Jun 2024 23:30:23 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KsrFPOy6; 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 1sJ5rh-0003lJ-LP; Mon, 17 Jun 2024 02:29:17 -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 1sJ5rg-0003l6-37 for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:16 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sJ5re-0003S6-F5 for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:15 -0400 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-2bfffa3c748so3283068a91.3 for ; Sun, 16 Jun 2024 23:29:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718605752; x=1719210552; 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=LjO3sCIUBtHps7O0z2KWESgFmJaLDV1pcN1gD8v3XwE=; b=KsrFPOy6gWSUIE6kjTBOwsAgGcvZQ4tgtMvjUCKM4eXmYcR4YTk44+xtEQMXobU5Hn 571U8p6gYV777wMDqGmXa9Qta75qfV/3arfzX3pmdu8lvGf98oiwHauvZZsS+hAmK1DO ItL3NB5tTv2ulcQHEj8CubxURbrj+tVsg8yESAd2yeTPwWG9W9yzriVll+YJjX2VQQnM i+3F5VNzIpQaQGTTNkt/KPAlFdQBA5XjXTTu/4by8wwh4iMof3ogMDKpWUPRl9/l43A6 +X9SKEjJVBEIKacm9PGk7M01FDfM4A+fUkewp2U6cymMmU1cnZyvWO4amgU3WijmLq8T /+/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718605752; x=1719210552; 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=LjO3sCIUBtHps7O0z2KWESgFmJaLDV1pcN1gD8v3XwE=; b=jcoEP+HsqmOLJp7s3fQ9dOIomdDsGlTkvFRjS9RPL/b/wy4B+gBOgK/wlj7d76DqJ1 NPcClinaHsjTKGWOm/1crPdg5WrBJ6ZIpHdNbEx9Zodh2nGEknI2K8zGXyQK4V6BvcZn EZSaKKx8BtUa6/eyhYFrYY7Dna/Jmg7P+67Xy1feSgHcbyJ8nQl+sfy13pY1iw7A/NsU NowSCn00CUsE/4Sdx/a70cpR3BclEP18PxQI+3hCmx3OTrdJNOUunTPmT9EEumbOytMm +EcJT/8GRX6c/I8dEdw/4eFKwjhzoTG8LYdQBsB4121xfnYsW36vc+CJV1hrGAIf4m1E f6YA== X-Gm-Message-State: AOJu0YwFtKJ08JO+wxwyaGzfm1/f/hyuJJ8QE64C7HUbnGwDXvW+Bq2w n/5KF40yf+h8p6yXEdXFMr+GsE+5XmRk5nDOIOjZEoJPQInZHbT+dD60na8qn5n0gq21Yoo0SfZ L X-Received: by 2002:a17:90b:2288:b0:2c4:ac12:d269 with SMTP id 98e67ed59e1d1-2c4da9d0b32mr8825719a91.0.1718605752289; Sun, 16 Jun 2024 23:29:12 -0700 (PDT) Received: from amd.. ([2804:7f0:b400:8dcb:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ed69csm10641863a91.14.2024.06.16.23.29.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 23:29:11 -0700 (PDT) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org, peter.maydell@linaro.org, alex.bennee@linaro.org, richard.henderson@linaro.org Cc: gustavo.romero@linaro.org Subject: [PATCH v3 1/9] gdbstub: Clean up process_string_cmd Date: Mon, 17 Jun 2024 06:28:41 +0000 Message-Id: <20240617062849.3531745-2-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240617062849.3531745-1-gustavo.romero@linaro.org> References: <20240617062849.3531745-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=gustavo.romero@linaro.org; helo=mail-pj1-x102e.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, T_SCC_BODY_TEXT_LINE=-0.01 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 Change 'process_string_cmd' to return true on success and false on failure, instead of 0 and -1. Signed-off-by: Gustavo Romero Reviewed-by: Alex Bennée Reviewed-by: Richard Henderson --- gdbstub/gdbstub.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c index b3574997ea..37314b92e5 100644 --- a/gdbstub/gdbstub.c +++ b/gdbstub/gdbstub.c @@ -962,14 +962,14 @@ static inline int startswith(const char *string, const char *pattern) return !strncmp(string, pattern, strlen(pattern)); } -static int process_string_cmd(const char *data, - const GdbCmdParseEntry *cmds, int num_cmds) +static bool process_string_cmd(const char *data, + const GdbCmdParseEntry *cmds, int num_cmds) { int i; g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant)); if (!cmds) { - return -1; + return false; } for (i = 0; i < num_cmds; i++) { @@ -984,16 +984,16 @@ static int process_string_cmd(const char *data, if (cmd->schema) { if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema, params)) { - return -1; + return false; } } gdbserver_state.allow_stop_reply = cmd->allow_stop_reply; cmd->handler(params, NULL); - return 0; + return true; } - return -1; + return false; } static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd) @@ -1007,7 +1007,7 @@ static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd) /* In case there was an error during the command parsing we must * send a NULL packet to indicate the command is not supported */ - if (process_string_cmd(data, cmd, 1)) { + if (!process_string_cmd(data, cmd, 1)) { gdb_put_packet(""); } } @@ -1523,9 +1523,9 @@ static void handle_v_commands(GArray *params, void *user_ctx) return; } - if (process_string_cmd(get_param(params, 0)->data, - gdb_v_commands_table, - ARRAY_SIZE(gdb_v_commands_table))) { + if (!process_string_cmd(get_param(params, 0)->data, + gdb_v_commands_table, + ARRAY_SIZE(gdb_v_commands_table))) { gdb_put_packet(""); } } @@ -1889,15 +1889,15 @@ static void handle_gen_query(GArray *params, void *user_ctx) return; } - if (!process_string_cmd(get_param(params, 0)->data, - gdb_gen_query_set_common_table, - ARRAY_SIZE(gdb_gen_query_set_common_table))) { + if (process_string_cmd(get_param(params, 0)->data, + gdb_gen_query_set_common_table, + ARRAY_SIZE(gdb_gen_query_set_common_table))) { return; } - if (process_string_cmd(get_param(params, 0)->data, - gdb_gen_query_table, - ARRAY_SIZE(gdb_gen_query_table))) { + if (!process_string_cmd(get_param(params, 0)->data, + gdb_gen_query_table, + ARRAY_SIZE(gdb_gen_query_table))) { gdb_put_packet(""); } } @@ -1908,13 +1908,13 @@ static void handle_gen_set(GArray *params, void *user_ctx) return; } - if (!process_string_cmd(get_param(params, 0)->data, - gdb_gen_query_set_common_table, - ARRAY_SIZE(gdb_gen_query_set_common_table))) { + if (process_string_cmd(get_param(params, 0)->data, + gdb_gen_query_set_common_table, + ARRAY_SIZE(gdb_gen_query_set_common_table))) { return; } - if (process_string_cmd(get_param(params, 0)->data, + if (!process_string_cmd(get_param(params, 0)->data, gdb_gen_set_table, ARRAY_SIZE(gdb_gen_set_table))) { gdb_put_packet(""); From patchwork Mon Jun 17 06:28:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 804832 Delivered-To: patch@linaro.org Received: by 2002:adf:fb90:0:b0:360:93e7:1765 with SMTP id a16csp423566wrr; Sun, 16 Jun 2024 23:29:51 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVcikruCGMbYAEgLorJvUXyFJfnx3Ef+d1clN1ptsCplERSrpHIhf/Abu9INPy3cDBwYTITnFn3aI8C4M6VnEqV X-Google-Smtp-Source: AGHT+IFJLASl6y5TAE2spORQJcg/uh53Sy/89g9KpQXm0Vodfpm2SIiBHcQlvWHU6VVBDkU2WRfF X-Received: by 2002:a05:620a:179f:b0:797:a485:28e5 with SMTP id af79cd13be357-798d23e8429mr1211864285a.7.1718605791143; Sun, 16 Jun 2024 23:29:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718605791; cv=none; d=google.com; s=arc-20160816; b=1ATRli563/ptSLXwzqLtgd5aVJ4JZcCgZTByoKsMUYNaoXEhn4EX5mi5B12vE34QpS cYTaCIoZF6Yg4NM8mxlnAfVnFPrcCKYupiMJwgYkHMrTnS6qDQbbp0jCgA5riGv8QIP0 l2OVItRyScuX/8qjvDu1VLcoUN5Hx7a+6QUnD82T3PJI12eQMdindAr5X86tfMP60I9b CUAWXcLoL8kbLbCxUZ3xgDg7oTrXpnX8C86J5LLsTCszgeiyx2jm0TR2yJy0IJ1xAsqf TygtI5bCRLUhZSNxQnY4i6YTtdnnl+8fdtWvsL+XrF62HkXRf606En2enuv/HGMdaUTv +w5A== 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=6Sf8/CZYkfq0TP6X1c0yibA0dD8nesLemm2CrbqDsi4=; fh=ZLVo7h1WCYX+mCQET83n+8bPmyNFw4pMQxFlbH8xyms=; b=VuGyubzZTsej5/ra7sRjroT6mh7/d6UJ4jAiv6UBwwfUa3uxk6F2CbyAz2lhakpU46 ibrqn+8lLczJ/bsgPSK9v7YBLYGOcA9Loeu3ZT7KtxX48vrTWEc5cqqTfiPXkXP0ZrqF Y23tQfukJzbKMZiddHrPzZJiqylcnrgBBrJPlgFukUsgPHbqgB8CVuBNeaMj68evkKLt 5roKy9LAWpQBLmH5llv3sUmu6WYlPTRF08VzLIHlDveW4xYZA8PEs110cwuvu37VnHmF 9v+vRzrQLiOPCVCKcTbOnD9VgBnCtGyTPw6GzfGo+Y9FzjvlpLN7+Np9pCSQ64FAyOSn 6DIA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LkTejs1B; 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 af79cd13be357-798abd167desi896147585a.400.2024.06.16.23.29.50 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 16 Jun 2024 23:29:51 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LkTejs1B; 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 1sJ5rm-0003mF-RL; Mon, 17 Jun 2024 02:29:22 -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 1sJ5rk-0003lj-Kj for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:20 -0400 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sJ5rh-0003ST-CJ for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:20 -0400 Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-656d8b346d2so2846895a12.2 for ; Sun, 16 Jun 2024 23:29:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718605755; x=1719210555; 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=6Sf8/CZYkfq0TP6X1c0yibA0dD8nesLemm2CrbqDsi4=; b=LkTejs1B9jGOSpUsXrF4AUr7H0K41KWK9cHNrkk7KmXRs8/QJ6lRyAORs8IWbB+gbB Mzzb8jHLd8/Su7Ex1qIlhm/5EhORhL67Z8GANeQvvHO/dgEFlIbvsRL5uT+IbPKLZsFj fTBvlwYLqvxACT/OxH7De1xQ/uEsynY/z7d4VGXONp1+kdlWObjSHtx7Veb0Mcfmf8jn 1bhMddKJE+2UgSVZ4SlPzYlrGjo0J4gmqMbI1wtEkmzsiFADf1lbTRpb4+AoysmW0FGO jYnsxXvluv3D54OaQejIz0GIM3izgVGGEusexWA6SyuWMwqgoNyFlBhXfVRPRx6rDopj OoYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718605755; x=1719210555; 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=6Sf8/CZYkfq0TP6X1c0yibA0dD8nesLemm2CrbqDsi4=; b=EjT9WSb6tHcugSSktPTxpZqmYx+elTdo5SeRwXdaiZG6dFiuQ71kDvMtkLBGqpNN7k TtNzxRtyumPlcHE1TDwGCg4aJmdTnAO0AJJoNW4cIJ2uJObTGl7kzHzhvvYcD4abAZtw 0fgpwSJtcG+Rg5oY7/O4BE5y1X2SgH+llSN4QWDrMtlvhf3iA3CepT4NFGQ2eZn33ZjS c5ZU2FnLFk7qgwe4mgctLR/zJDtczWbvzlzEt+gY/sMB6LOeVDyfxwh0QWl7a7HrYPPO qQe7OvyDjqz83VPtzb+FxNfReitfyCamPZ5QdWQgM9SUXrNoN9lzK9cFViHW2Z9pTIM8 ME5g== X-Gm-Message-State: AOJu0YyM/6IYVk2M4KeGJqB3wiot4Q5lMlpoOVo4yIoKaLxzRWEJyMw8 WC7GFt6sFH1EIR4uwUVAjSs9EPK8u/jt+FfuK/eP0RhOstuYleVO/Ze5fY1doK0aEnyOFY/7+mn Q X-Received: by 2002:a17:90b:4a8d:b0:2c4:e333:35e9 with SMTP id 98e67ed59e1d1-2c4e3333826mr5896600a91.30.1718605755092; Sun, 16 Jun 2024 23:29:15 -0700 (PDT) Received: from amd.. ([2804:7f0:b400:8dcb:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ed69csm10641863a91.14.2024.06.16.23.29.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 23:29:14 -0700 (PDT) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org, peter.maydell@linaro.org, alex.bennee@linaro.org, richard.henderson@linaro.org Cc: gustavo.romero@linaro.org Subject: [PATCH v3 2/9] gdbstub: Move GdbCmdParseEntry into a new header file Date: Mon, 17 Jun 2024 06:28:42 +0000 Message-Id: <20240617062849.3531745-3-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240617062849.3531745-1-gustavo.romero@linaro.org> References: <20240617062849.3531745-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52a; envelope-from=gustavo.romero@linaro.org; helo=mail-pg1-x52a.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, T_SCC_BODY_TEXT_LINE=-0.01 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 Move GdbCmdParseEntry and its associated types into a separate header file to allow the use of GdbCmdParseEntry and other gdbstub command functions outside of gdbstub.c. Since GdbCmdParseEntry and get_param are now public, kdoc GdbCmdParseEntry and rename get_param to gdb_get_cmd_param. This commit also makes gdb_put_packet public since is used in gdbstub command handling. Signed-off-by: Gustavo Romero Reviewed-by: Alex Bennée --- gdbstub/gdbstub.c | 134 ++++++++++++++----------------------- gdbstub/internals.h | 22 ------ gdbstub/syscalls.c | 7 +- gdbstub/system.c | 7 +- gdbstub/user-target.c | 25 +++---- gdbstub/user.c | 7 +- include/gdbstub/commands.h | 74 ++++++++++++++++++++ 7 files changed, 148 insertions(+), 128 deletions(-) create mode 100644 include/gdbstub/commands.h diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c index 37314b92e5..9ff2f4177d 100644 --- a/gdbstub/gdbstub.c +++ b/gdbstub/gdbstub.c @@ -30,6 +30,7 @@ #include "qemu/error-report.h" #include "trace.h" #include "exec/gdbstub.h" +#include "gdbstub/commands.h" #include "gdbstub/syscalls.h" #ifdef CONFIG_USER_ONLY #include "accel/tcg/vcpu-state.h" @@ -920,43 +921,6 @@ static int cmd_parse_params(const char *data, const char *schema, return 0; } -typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx); - -/* - * cmd_startswith -> cmd is compared using startswith - * - * allow_stop_reply -> true iff the gdbstub can respond to this command with a - * "stop reply" packet. The list of commands that accept such response is - * defined at the GDB Remote Serial Protocol documentation. see: - * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets. - * - * schema definitions: - * Each schema parameter entry consists of 2 chars, - * the first char represents the parameter type handling - * the second char represents the delimiter for the next parameter - * - * Currently supported schema types: - * 'l' -> unsigned long (stored in .val_ul) - * 'L' -> unsigned long long (stored in .val_ull) - * 's' -> string (stored in .data) - * 'o' -> single char (stored in .opcode) - * 't' -> thread id (stored in .thread_id) - * '?' -> skip according to delimiter - * - * Currently supported delimiters: - * '?' -> Stop at any delimiter (",;:=\0") - * '0' -> Stop at "\0" - * '.' -> Skip 1 char unless reached "\0" - * Any other value is treated as the delimiter value itself - */ -typedef struct GdbCmdParseEntry { - GdbCmdHandler handler; - const char *cmd; - bool cmd_startswith; - const char *schema; - bool allow_stop_reply; -} GdbCmdParseEntry; - static inline int startswith(const char *string, const char *pattern) { return !strncmp(string, pattern, strlen(pattern)); @@ -1023,7 +987,7 @@ static void handle_detach(GArray *params, void *user_ctx) return; } - pid = get_param(params, 0)->val_ul; + pid = gdb_get_cmd_param(params, 0)->val_ul; } #ifdef CONFIG_USER_ONLY @@ -1061,13 +1025,13 @@ static void handle_thread_alive(GArray *params, void *user_ctx) return; } - if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { + if (gdb_get_cmd_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { gdb_put_packet("E22"); return; } - cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, - get_param(params, 0)->thread_id.tid); + cpu = gdb_get_cpu(gdb_get_cmd_param(params, 0)->thread_id.pid, + gdb_get_cmd_param(params, 0)->thread_id.tid); if (!cpu) { gdb_put_packet("E22"); return; @@ -1079,7 +1043,7 @@ static void handle_thread_alive(GArray *params, void *user_ctx) static void handle_continue(GArray *params, void *user_ctx) { if (params->len) { - gdb_set_cpu_pc(get_param(params, 0)->val_ull); + gdb_set_cpu_pc(gdb_get_cmd_param(params, 0)->val_ull); } gdbserver_state.signal = 0; @@ -1095,7 +1059,7 @@ static void handle_cont_with_sig(GArray *params, void *user_ctx) * omit the addr parameter */ if (params->len) { - signal = get_param(params, 0)->val_ul; + signal = gdb_get_cmd_param(params, 0)->val_ul; } gdbserver_state.signal = gdb_signal_to_target(signal); @@ -1115,18 +1079,18 @@ static void handle_set_thread(GArray *params, void *user_ctx) return; } - if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) { + if (gdb_get_cmd_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) { gdb_put_packet("E22"); return; } - if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) { + if (gdb_get_cmd_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) { gdb_put_packet("OK"); return; } - pid = get_param(params, 1)->thread_id.pid; - tid = get_param(params, 1)->thread_id.tid; + pid = gdb_get_cmd_param(params, 1)->thread_id.pid; + tid = gdb_get_cmd_param(params, 1)->thread_id.tid; #ifdef CONFIG_USER_ONLY if (gdb_handle_set_thread_user(pid, tid)) { return; @@ -1142,7 +1106,7 @@ static void handle_set_thread(GArray *params, void *user_ctx) * Note: This command is deprecated and modern gdb's will be using the * vCont command instead. */ - switch (get_param(params, 0)->opcode) { + switch (gdb_get_cmd_param(params, 0)->opcode) { case 'c': gdbserver_state.c_cpu = cpu; gdb_put_packet("OK"); @@ -1167,9 +1131,9 @@ static void handle_insert_bp(GArray *params, void *user_ctx) } res = gdb_breakpoint_insert(gdbserver_state.c_cpu, - get_param(params, 0)->val_ul, - get_param(params, 1)->val_ull, - get_param(params, 2)->val_ull); + gdb_get_cmd_param(params, 0)->val_ul, + gdb_get_cmd_param(params, 1)->val_ull, + gdb_get_cmd_param(params, 2)->val_ull); if (res >= 0) { gdb_put_packet("OK"); return; @@ -1191,9 +1155,9 @@ static void handle_remove_bp(GArray *params, void *user_ctx) } res = gdb_breakpoint_remove(gdbserver_state.c_cpu, - get_param(params, 0)->val_ul, - get_param(params, 1)->val_ull, - get_param(params, 2)->val_ull); + gdb_get_cmd_param(params, 0)->val_ul, + gdb_get_cmd_param(params, 1)->val_ull, + gdb_get_cmd_param(params, 2)->val_ull); if (res >= 0) { gdb_put_packet("OK"); return; @@ -1225,10 +1189,10 @@ static void handle_set_reg(GArray *params, void *user_ctx) return; } - reg_size = strlen(get_param(params, 1)->data) / 2; - gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size); + reg_size = strlen(gdb_get_cmd_param(params, 1)->data) / 2; + gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 1)->data, reg_size); gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data, - get_param(params, 0)->val_ull); + gdb_get_cmd_param(params, 0)->val_ull); gdb_put_packet("OK"); } @@ -1243,7 +1207,7 @@ static void handle_get_reg(GArray *params, void *user_ctx) reg_size = gdb_read_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf, - get_param(params, 0)->val_ull); + gdb_get_cmd_param(params, 0)->val_ull); if (!reg_size) { gdb_put_packet("E14"); return; @@ -1264,16 +1228,16 @@ static void handle_write_mem(GArray *params, void *user_ctx) } /* gdb_hextomem() reads 2*len bytes */ - if (get_param(params, 1)->val_ull > - strlen(get_param(params, 2)->data) / 2) { + if (gdb_get_cmd_param(params, 1)->val_ull > + strlen(gdb_get_cmd_param(params, 2)->data) / 2) { gdb_put_packet("E22"); return; } - gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data, - get_param(params, 1)->val_ull); + gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 2)->data, + gdb_get_cmd_param(params, 1)->val_ull); if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu, - get_param(params, 0)->val_ull, + gdb_get_cmd_param(params, 0)->val_ull, gdbserver_state.mem_buf->data, gdbserver_state.mem_buf->len, true)) { gdb_put_packet("E14"); @@ -1291,16 +1255,16 @@ static void handle_read_mem(GArray *params, void *user_ctx) } /* gdb_memtohex() doubles the required space */ - if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) { + if (gdb_get_cmd_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) { gdb_put_packet("E22"); return; } g_byte_array_set_size(gdbserver_state.mem_buf, - get_param(params, 1)->val_ull); + gdb_get_cmd_param(params, 1)->val_ull); if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu, - get_param(params, 0)->val_ull, + gdb_get_cmd_param(params, 0)->val_ull, gdbserver_state.mem_buf->data, gdbserver_state.mem_buf->len, false)) { gdb_put_packet("E14"); @@ -1324,8 +1288,8 @@ static void handle_write_all_regs(GArray *params, void *user_ctx) } cpu_synchronize_state(gdbserver_state.g_cpu); - len = strlen(get_param(params, 0)->data) / 2; - gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len); + len = strlen(gdb_get_cmd_param(params, 0)->data) / 2; + gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 0)->data, len); registers = gdbserver_state.mem_buf->data; for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0; @@ -1360,7 +1324,7 @@ static void handle_read_all_regs(GArray *params, void *user_ctx) static void handle_step(GArray *params, void *user_ctx) { if (params->len) { - gdb_set_cpu_pc(get_param(params, 0)->val_ull); + gdb_set_cpu_pc(gdb_get_cmd_param(params, 0)->val_ull); } cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags); @@ -1373,7 +1337,7 @@ static void handle_backward(GArray *params, void *user_ctx) gdb_put_packet("E22"); } if (params->len == 1) { - switch (get_param(params, 0)->opcode) { + switch (gdb_get_cmd_param(params, 0)->opcode) { case 's': if (replay_reverse_step()) { gdb_continue(); @@ -1408,7 +1372,7 @@ static void handle_v_cont(GArray *params, void *user_ctx) return; } - res = gdb_handle_vcont(get_param(params, 0)->data); + res = gdb_handle_vcont(gdb_get_cmd_param(params, 0)->data); if ((res == -EINVAL) || (res == -ERANGE)) { gdb_put_packet("E22"); } else if (res) { @@ -1426,7 +1390,7 @@ static void handle_v_attach(GArray *params, void *user_ctx) goto cleanup; } - process = gdb_get_process(get_param(params, 0)->val_ul); + process = gdb_get_process(gdb_get_cmd_param(params, 0)->val_ul); if (!process) { goto cleanup; } @@ -1523,7 +1487,7 @@ static void handle_v_commands(GArray *params, void *user_ctx) return; } - if (!process_string_cmd(get_param(params, 0)->data, + if (!process_string_cmd(gdb_get_cmd_param(params, 0)->data, gdb_v_commands_table, ARRAY_SIZE(gdb_v_commands_table))) { gdb_put_packet(""); @@ -1555,7 +1519,7 @@ static void handle_set_qemu_sstep(GArray *params, void *user_ctx) return; } - new_sstep_flags = get_param(params, 0)->val_ul; + new_sstep_flags = gdb_get_cmd_param(params, 0)->val_ul; if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) { gdb_put_packet("E22"); @@ -1615,13 +1579,13 @@ static void handle_query_thread_extra(GArray *params, void *user_ctx) CPUState *cpu; if (!params->len || - get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { + gdb_get_cmd_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { gdb_put_packet("E22"); return; } - cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, - get_param(params, 0)->thread_id.tid); + cpu = gdb_get_cpu(gdb_get_cmd_param(params, 0)->thread_id.pid, + gdb_get_cmd_param(params, 0)->thread_id.tid); if (!cpu) { return; } @@ -1673,7 +1637,7 @@ static void handle_query_supported(GArray *params, void *user_ctx) #endif if (params->len) { - const char *gdb_supported = get_param(params, 0)->data; + const char *gdb_supported = gdb_get_cmd_param(params, 0)->data; if (strstr(gdb_supported, "multiprocess+")) { gdbserver_state.multiprocess = true; @@ -1707,15 +1671,15 @@ static void handle_query_xfer_features(GArray *params, void *user_ctx) return; } - p = get_param(params, 0)->data; + p = gdb_get_cmd_param(params, 0)->data; xml = get_feature_xml(p, &p, process); if (!xml) { gdb_put_packet("E00"); return; } - addr = get_param(params, 1)->val_ul; - len = get_param(params, 2)->val_ul; + addr = gdb_get_cmd_param(params, 1)->val_ul; + len = gdb_get_cmd_param(params, 2)->val_ul; total_len = strlen(xml); if (addr > total_len) { gdb_put_packet("E00"); @@ -1889,13 +1853,13 @@ static void handle_gen_query(GArray *params, void *user_ctx) return; } - if (process_string_cmd(get_param(params, 0)->data, + if (process_string_cmd(gdb_get_cmd_param(params, 0)->data, gdb_gen_query_set_common_table, ARRAY_SIZE(gdb_gen_query_set_common_table))) { return; } - if (!process_string_cmd(get_param(params, 0)->data, + if (!process_string_cmd(gdb_get_cmd_param(params, 0)->data, gdb_gen_query_table, ARRAY_SIZE(gdb_gen_query_table))) { gdb_put_packet(""); @@ -1908,13 +1872,13 @@ static void handle_gen_set(GArray *params, void *user_ctx) return; } - if (process_string_cmd(get_param(params, 0)->data, + if (process_string_cmd(gdb_get_cmd_param(params, 0)->data, gdb_gen_query_set_common_table, ARRAY_SIZE(gdb_gen_query_set_common_table))) { return; } - if (!process_string_cmd(get_param(params, 0)->data, + if (!process_string_cmd(gdb_get_cmd_param(params, 0)->data, gdb_gen_set_table, ARRAY_SIZE(gdb_gen_set_table))) { gdb_put_packet(""); diff --git a/gdbstub/internals.h b/gdbstub/internals.h index 32f9f63297..34121dc61a 100644 --- a/gdbstub/internals.h +++ b/gdbstub/internals.h @@ -106,7 +106,6 @@ static inline int tohex(int v) */ void gdb_put_strbuf(void); -int gdb_put_packet(const char *buf); int gdb_put_packet_binary(const char *buf, int len, bool dump); void gdb_hextomem(GByteArray *mem, const char *buf, int len); void gdb_memtohex(GString *buf, const uint8_t *mem, int len); @@ -166,27 +165,6 @@ void gdb_put_buffer(const uint8_t *buf, int len); */ void gdb_init_gdbserver_state(void); -typedef enum GDBThreadIdKind { - GDB_ONE_THREAD = 0, - GDB_ALL_THREADS, /* One process, all threads */ - GDB_ALL_PROCESSES, - GDB_READ_THREAD_ERR -} GDBThreadIdKind; - -typedef union GdbCmdVariant { - const char *data; - uint8_t opcode; - unsigned long val_ul; - unsigned long long val_ull; - struct { - GDBThreadIdKind kind; - uint32_t pid; - uint32_t tid; - } thread_id; -} GdbCmdVariant; - -#define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i)) - void gdb_handle_query_rcmd(GArray *params, void *ctx); /* system */ void gdb_handle_query_offsets(GArray *params, void *user_ctx); /* user */ void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */ diff --git a/gdbstub/syscalls.c b/gdbstub/syscalls.c index 02e3a8f74c..4e1295b782 100644 --- a/gdbstub/syscalls.c +++ b/gdbstub/syscalls.c @@ -16,6 +16,7 @@ #include "sysemu/runstate.h" #include "gdbstub/user.h" #include "gdbstub/syscalls.h" +#include "gdbstub/commands.h" #include "trace.h" #include "internals.h" @@ -154,9 +155,9 @@ void gdb_handle_file_io(GArray *params, void *user_ctx) uint64_t ret; int err; - ret = get_param(params, 0)->val_ull; + ret = gdb_get_cmd_param(params, 0)->val_ull; if (params->len >= 2) { - err = get_param(params, 1)->val_ull; + err = gdb_get_cmd_param(params, 1)->val_ull; } else { err = 0; } @@ -196,7 +197,7 @@ void gdb_handle_file_io(GArray *params, void *user_ctx) gdbserver_syscall_state.current_syscall_cb = NULL; } - if (params->len >= 3 && get_param(params, 2)->opcode == (uint8_t)'C') { + if (params->len >= 3 && gdb_get_cmd_param(params, 2)->opcode == (uint8_t)'C') { gdb_put_packet("T02"); return; } diff --git a/gdbstub/system.c b/gdbstub/system.c index d235403855..1ad87fe7fd 100644 --- a/gdbstub/system.c +++ b/gdbstub/system.c @@ -16,6 +16,7 @@ #include "qemu/cutils.h" #include "exec/gdbstub.h" #include "gdbstub/syscalls.h" +#include "gdbstub/commands.h" #include "exec/hwaddr.h" #include "exec/tb-flush.h" #include "sysemu/cpus.h" @@ -501,7 +502,7 @@ void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx) return; } - if (!get_param(params, 0)->val_ul) { + if (!gdb_get_cmd_param(params, 0)->val_ul) { phy_memory_mode = 0; } else { phy_memory_mode = 1; @@ -519,7 +520,7 @@ void gdb_handle_query_rcmd(GArray *params, void *ctx) return; } - len = strlen(get_param(params, 0)->data); + len = strlen(gdb_get_cmd_param(params, 0)->data); if (len % 2) { gdb_put_packet("E01"); return; @@ -527,7 +528,7 @@ void gdb_handle_query_rcmd(GArray *params, void *ctx) g_assert(gdbserver_state.mem_buf->len == 0); len = len / 2; - gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len); + gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 0)->data, len); g_byte_array_append(gdbserver_state.mem_buf, &zero, 1); qemu_chr_be_write(gdbserver_system_state.mon_chr, gdbserver_state.mem_buf->data, diff --git a/gdbstub/user-target.c b/gdbstub/user-target.c index a9c6c64512..b5e01fd8b0 100644 --- a/gdbstub/user-target.c +++ b/gdbstub/user-target.c @@ -9,6 +9,7 @@ #include "qemu/osdep.h" #include "exec/gdbstub.h" +#include "gdbstub/commands.h" #include "qemu.h" #include "internals.h" #ifdef CONFIG_LINUX @@ -250,8 +251,8 @@ void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx) return; } - offset = get_param(params, 0)->val_ul; - len = get_param(params, 1)->val_ul; + offset = gdb_get_cmd_param(params, 0)->val_ul; + len = gdb_get_cmd_param(params, 1)->val_ul; ts = get_task_state(gdbserver_state.c_cpu); saved_auxv = ts->info->saved_auxv; auxv_len = ts->info->auxv_len; @@ -288,7 +289,7 @@ void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx) static const char *get_filename_param(GArray *params, int i) { - const char *hex_filename = get_param(params, i)->data; + const char *hex_filename = gdb_get_cmd_param(params, i)->data; gdb_hextomem(gdbserver_state.mem_buf, hex_filename, strlen(hex_filename) / 2); g_byte_array_append(gdbserver_state.mem_buf, (const guint8 *)"", 1); @@ -306,8 +307,8 @@ static void hostio_reply_with_data(const void *buf, size_t n) void gdb_handle_v_file_open(GArray *params, void *user_ctx) { const char *filename = get_filename_param(params, 0); - uint64_t flags = get_param(params, 1)->val_ull; - uint64_t mode = get_param(params, 2)->val_ull; + uint64_t flags = gdb_get_cmd_param(params, 1)->val_ull; + uint64_t mode = gdb_get_cmd_param(params, 2)->val_ull; #ifdef CONFIG_LINUX int fd = do_guest_openat(cpu_env(gdbserver_state.g_cpu), 0, filename, @@ -325,7 +326,7 @@ void gdb_handle_v_file_open(GArray *params, void *user_ctx) void gdb_handle_v_file_close(GArray *params, void *user_ctx) { - int fd = get_param(params, 0)->val_ul; + int fd = gdb_get_cmd_param(params, 0)->val_ul; if (close(fd) == -1) { g_string_printf(gdbserver_state.str_buf, "F-1,%d", errno); @@ -338,9 +339,9 @@ void gdb_handle_v_file_close(GArray *params, void *user_ctx) void gdb_handle_v_file_pread(GArray *params, void *user_ctx) { - int fd = get_param(params, 0)->val_ul; - size_t count = get_param(params, 1)->val_ull; - off_t offset = get_param(params, 2)->val_ull; + int fd = gdb_get_cmd_param(params, 0)->val_ul; + size_t count = gdb_get_cmd_param(params, 1)->val_ull; + off_t offset = gdb_get_cmd_param(params, 2)->val_ull; size_t bufsiz = MIN(count, BUFSIZ); g_autofree char *buf = g_try_malloc(bufsiz); @@ -383,9 +384,9 @@ void gdb_handle_v_file_readlink(GArray *params, void *user_ctx) void gdb_handle_query_xfer_exec_file(GArray *params, void *user_ctx) { - uint32_t pid = get_param(params, 0)->val_ul; - uint32_t offset = get_param(params, 1)->val_ul; - uint32_t length = get_param(params, 2)->val_ul; + uint32_t pid = gdb_get_cmd_param(params, 0)->val_ul; + uint32_t offset = gdb_get_cmd_param(params, 1)->val_ul; + uint32_t length = gdb_get_cmd_param(params, 2)->val_ul; GDBProcess *process = gdb_get_process(pid); if (!process) { diff --git a/gdbstub/user.c b/gdbstub/user.c index e34b58b407..b36033bc7a 100644 --- a/gdbstub/user.c +++ b/gdbstub/user.c @@ -16,6 +16,7 @@ #include "exec/hwaddr.h" #include "exec/tb-flush.h" #include "exec/gdbstub.h" +#include "gdbstub/commands.h" #include "gdbstub/syscalls.h" #include "gdbstub/user.h" #include "gdbstub/enums.h" @@ -793,7 +794,7 @@ void gdb_syscall_return(CPUState *cs, int num) void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx) { - const char *param = get_param(params, 0)->data; + const char *param = gdb_get_cmd_param(params, 0)->data; GDBSyscallsMask catch_syscalls_mask; bool catch_all_syscalls; unsigned int num; @@ -858,8 +859,8 @@ void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx) unsigned long offset, len; uint8_t *siginfo_offset; - offset = get_param(params, 0)->val_ul; - len = get_param(params, 1)->val_ul; + offset = gdb_get_cmd_param(params, 0)->val_ul; + len = gdb_get_cmd_param(params, 1)->val_ul; if (offset + len > gdbserver_user_state.siginfo_len) { /* Invalid offset and/or requested length. */ diff --git a/include/gdbstub/commands.h b/include/gdbstub/commands.h new file mode 100644 index 0000000000..dd45c38472 --- /dev/null +++ b/include/gdbstub/commands.h @@ -0,0 +1,74 @@ +#ifndef GDBSTUB_COMMANDS_H +#define GDBSTUB + +typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx); + +typedef enum GDBThreadIdKind { + GDB_ONE_THREAD = 0, + GDB_ALL_THREADS, /* One process, all threads */ + GDB_ALL_PROCESSES, + GDB_READ_THREAD_ERR +} GDBThreadIdKind; + +typedef union GdbCmdVariant { + const char *data; + uint8_t opcode; + unsigned long val_ul; + unsigned long long val_ull; + struct { + GDBThreadIdKind kind; + uint32_t pid; + uint32_t tid; + } thread_id; +} GdbCmdVariant; + +#define gdb_get_cmd_param(p, i) (&g_array_index(p, GdbCmdVariant, i)) + +/** + * typedef GdbCmdParseEntry - gdb command parser + * + * This structure keeps the information necessary to match a gdb command, + * parse it (extract its parameters), and select the correct handler for it. + * + * @cmd: The command to be matched + * @cmd_startswith: If true, @cmd is compared using startswith + * @schema: Each schema for the command parameter entry consists of 2 chars, + * the first char represents the parameter type handling the second char + * represents the delimiter for the next parameter. + * + * Currently supported schema types: + * 'l' -> unsigned long (stored in .val_ul) + * 'L' -> unsigned long long (stored in .val_ull) + * 's' -> string (stored in .data) + * 'o' -> single char (stored in .opcode) + * 't' -> thread id (stored in .thread_id) + * '?' -> skip according to delimiter + * + * Currently supported delimiters: + * '?' -> Stop at any delimiter (",;:=\0") + * '0' -> Stop at "\0" + * '.' -> Skip 1 char unless reached "\0" + * Any other value is treated as the delimiter value itself + * + * @allow_stop_reply: True iff the gdbstub can respond to this command with a + * "stop reply" packet. The list of commands that accept such response is + * defined at the GDB Remote Serial Protocol documentation. See: + * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets. + */ +typedef struct GdbCmdParseEntry { + GdbCmdHandler handler; + const char *cmd; + bool cmd_startswith; + const char *schema; + bool allow_stop_reply; +} GdbCmdParseEntry; + +#define get_cmd_parsers(p) (&g_array_index(p, GdbCmdParseEntry, 0)) + +/** + * gdb_put_packet() - put string into gdb server's buffer so it is sent + * to the client + */ +int gdb_put_packet(const char *buf); + +#endif /* GDBSTUB_COMMANDS_H */ From patchwork Mon Jun 17 06:28:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 804833 Delivered-To: patch@linaro.org Received: by 2002:adf:fb90:0:b0:360:93e7:1765 with SMTP id a16csp423682wrr; Sun, 16 Jun 2024 23:30:11 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUsALWRtbP9Kl3d4DAxqB0DGKe0/xwLxZYsBhRPmFEiQ26fbP7iiZWb3ASbs2J3ZKIDuVtiaXq5f6LOEizXfp8f X-Google-Smtp-Source: AGHT+IGqGz2Zg4s9RENPK84IwEmmcLVsRJdW0GhiInF5vyb91BfJAFBcEM3VwstgzEmeFgg6htmz X-Received: by 2002:a05:622a:106:b0:440:565a:9203 with SMTP id d75a77b69052e-44216857286mr108094501cf.2.1718605811468; Sun, 16 Jun 2024 23:30:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718605811; cv=none; d=google.com; s=arc-20160816; b=PsnZ3RbCszkCy42kx6+ZttkRF7ucneWghG+z7eEqzBpmWZXNtacUr3bmIXslQ4SzOl JtH1Jtosaw3/4hLb2XeQZT5iQQCP89Ud0m4q33yPT+LtPN7k3ApsTTN18kkGvDDfLjZs qNp4LdeCILumZ8hQxuFpOfIDWyGnFqHJxPbHZS4frugpWqAk5KFVNidpzOXeXclaL+4f az+RdFisyaGzOibtvgNUqmdNYD+umOdWGiv6Q+LtjBQ6fHqSO55mO+TunUp8mheAtD1U ayfhVei/IDpaHucFnej195jHHJvfTrWU3s9m8ghgBnYDjaotMeS9Mdpsge6c8MBb+GrX 1HGg== 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=I4X1Q5LR9CyzMxkopn2wmWu6BnVGz6FW4Yqv/puuawE=; fh=ZLVo7h1WCYX+mCQET83n+8bPmyNFw4pMQxFlbH8xyms=; b=djioly+qx+TMEyO7uvnUWpr1Fv36h/H/9iflwC2Hxa9LO/VPy2eeXrEh3t8nw0Jmfh japJRQwfrS5lxvKp6/Wsbo7PiKzgiC2/mA6377g4QmVUh7GmkUWBCkZBz7sQeJd3yNyW mEg5mEteBmkUfAHhJlPUKId30XppMbrjoVOygfCaHftjNyNGmNvO4+gWjun+8yQ+CTgB nQHvK1JPTLIN6Mh+zda0C3u7721KcIH4eAXzEcnflSSZ4DrOSSMCSCokMAukJA28nRLo nlu47Y4Y6yXjXrMK2/5bMIs2F6mZUBh3jc9AX6w5dnHlVq/ph5ipV5mAoAWKSHFzMvNb Dkgw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=H6hFwRJT; 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 d75a77b69052e-441f3120e45si96563271cf.786.2024.06.16.23.30.11 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 16 Jun 2024 23:30:11 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=H6hFwRJT; 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 1sJ5rn-0003mH-OM; Mon, 17 Jun 2024 02:29:23 -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 1sJ5rl-0003ls-Iz for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:21 -0400 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sJ5rj-0003Se-Mf for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:21 -0400 Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-2c4c7eb425fso3218054a91.1 for ; Sun, 16 Jun 2024 23:29:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718605758; x=1719210558; 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=I4X1Q5LR9CyzMxkopn2wmWu6BnVGz6FW4Yqv/puuawE=; b=H6hFwRJTCX9S1/MSxumw3Rp3MEKdi5+omxQCA2MAKlevHp4lfSbcoB++UdW8A8Ggcz RoS2kN41GHNNT4hakN0Vv83MRE/bTTF3NsxNmUZvzaoVq841FsiWfS2fxof5xtfdjc2v rBpcyBdZAkdzPNUBsAihV3XRlh1UJKwIIQFbcmDi0DbdGLlpL2l++kcO0psrEaCD4xsR mLlLeiT4BDtojCdCX3YgBqCkXYsDXhN3ZbAIlpwfM/Jp4GofXKISu9pvvweQdN/1o8Ir Ecl+YqUaBb03cHiNnnLeCtXloCb23NGQhjJTaA3WULr2NJpdE3lukAZkUp+5yR1tYf68 3NMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718605758; x=1719210558; 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=I4X1Q5LR9CyzMxkopn2wmWu6BnVGz6FW4Yqv/puuawE=; b=RKnpVqf9ZnzrPF2zVssLI6Nb0DEfNbntTEPr9F1rLGNKjpzQ9zO6SuhQ2K8KEwYTZi Vp9Dwx22cs+Aigyf+VWj0GJx0XioLZeL8Ik5N+9SwbcKx4gyl8kI+zddVvquRXiyw5ug Mp1lkKYxQ6OeBUbPWtUhiy8Zg2YXV5WQtCqaFZ1tpAid9LjCwJrm1+zkrwMZsiInUcH1 UyEnhbyNpw73WYNkCsTwyyTVvw1ai5L173cjN2G9Ng+mPOwfF6genrwbwiVFxjvpvr4N 92k4c7+CsN+5oikUbGL85nhGpAc/UYhADSXKpAsvZmk7waLmYBmeRhdG4fQHboOcn9jC UBjQ== X-Gm-Message-State: AOJu0YwjAsWeUkcPOSCl7LAEQs/KiRpEWtLDZYlUisIsZ7gxO+93W0QE GD+/J4mctZ/NKXM55ukJsNRv823nFwU2qr/KvjkLP2PQFbd4G0HO/uITKAApHPXOx9LGZosItlP 6 X-Received: by 2002:a17:90a:b00f:b0:2c2:d813:bffa with SMTP id 98e67ed59e1d1-2c4dbd431b5mr7658930a91.43.1718605757794; Sun, 16 Jun 2024 23:29:17 -0700 (PDT) Received: from amd.. ([2804:7f0:b400:8dcb:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ed69csm10641863a91.14.2024.06.16.23.29.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 23:29:17 -0700 (PDT) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org, peter.maydell@linaro.org, alex.bennee@linaro.org, richard.henderson@linaro.org Cc: gustavo.romero@linaro.org Subject: [PATCH v3 3/9] gdbstub: Add support for target-specific stubs Date: Mon, 17 Jun 2024 06:28:43 +0000 Message-Id: <20240617062849.3531745-4-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240617062849.3531745-1-gustavo.romero@linaro.org> References: <20240617062849.3531745-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102a; envelope-from=gustavo.romero@linaro.org; helo=mail-pj1-x102a.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, T_SCC_BODY_TEXT_LINE=-0.01 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 Currently, it's not possible to have stubs specific to a given target, even though there are GDB features which are target-specific, like, for instance, memory tagging. This commit introduces gdb_extend_qsupported_features, gdb_extend_query_table, and gdb_extend_set_table functions as interfaces to extend the qSupported string, the query handler table, and the set handler table, allowing target-specific stub implementations. Signed-off-by: Gustavo Romero Reviewed-by: Alex Bennée --- gdbstub/gdbstub.c | 59 ++++++++++++++++++++++++++++++++++---- include/gdbstub/commands.h | 22 ++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c index 9ff2f4177d..e69cc5131e 100644 --- a/gdbstub/gdbstub.c +++ b/gdbstub/gdbstub.c @@ -1609,6 +1609,12 @@ static void handle_query_thread_extra(GArray *params, void *user_ctx) gdb_put_strbuf(); } +static char *extended_qsupported_features; +void gdb_extend_qsupported_features(char *qsupported_features) +{ + extended_qsupported_features = qsupported_features; +} + static void handle_query_supported(GArray *params, void *user_ctx) { CPUClass *cc; @@ -1648,6 +1654,11 @@ static void handle_query_supported(GArray *params, void *user_ctx) } g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+"); + + if (extended_qsupported_features) { + g_string_append(gdbserver_state.str_buf, extended_qsupported_features); + } + gdb_put_strbuf(); } @@ -1729,6 +1740,14 @@ static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = { }, }; +static GdbCmdParseEntry *extended_query_table; +static int extended_query_table_size; +void gdb_extend_query_table(GdbCmdParseEntry *table, int size) +{ + extended_query_table = table; + extended_query_table_size = size; +} + static const GdbCmdParseEntry gdb_gen_query_table[] = { { .handler = handle_query_curr_tid, @@ -1821,6 +1840,14 @@ static const GdbCmdParseEntry gdb_gen_query_table[] = { #endif }; +static GdbCmdParseEntry *extended_set_table; +static int extended_set_table_size; +void gdb_extend_set_table(GdbCmdParseEntry *table, int size) +{ + extended_set_table = table; + extended_set_table_size = size; +} + static const GdbCmdParseEntry gdb_gen_set_table[] = { /* Order is important if has same prefix */ { @@ -1859,11 +1886,21 @@ static void handle_gen_query(GArray *params, void *user_ctx) return; } - if (!process_string_cmd(gdb_get_cmd_param(params, 0)->data, - gdb_gen_query_table, - ARRAY_SIZE(gdb_gen_query_table))) { - gdb_put_packet(""); + if (process_string_cmd(gdb_get_cmd_param(params, 0)->data, + gdb_gen_query_table, + ARRAY_SIZE(gdb_gen_query_table))) { + return; + } + + if (extended_query_table && + process_string_cmd(gdb_get_cmd_param(params, 0)->data, + extended_query_table, + extended_query_table_size)) { + return; } + + /* Can't handle query, return Empty response. */ + gdb_put_packet(""); } static void handle_gen_set(GArray *params, void *user_ctx) @@ -1878,11 +1915,21 @@ static void handle_gen_set(GArray *params, void *user_ctx) return; } - if (!process_string_cmd(gdb_get_cmd_param(params, 0)->data, + if (process_string_cmd(gdb_get_cmd_param(params, 0)->data, gdb_gen_set_table, ARRAY_SIZE(gdb_gen_set_table))) { - gdb_put_packet(""); + return; } + + if (extended_set_table && + process_string_cmd(gdb_get_cmd_param(params, 0)->data, + extended_set_table, + extended_set_table_size)) { + return; + } + + /* Can't handle set, return Empty response. */ + gdb_put_packet(""); } static void handle_target_halt(GArray *params, void *user_ctx) diff --git a/include/gdbstub/commands.h b/include/gdbstub/commands.h index dd45c38472..2204c3ddbe 100644 --- a/include/gdbstub/commands.h +++ b/include/gdbstub/commands.h @@ -71,4 +71,26 @@ typedef struct GdbCmdParseEntry { */ int gdb_put_packet(const char *buf); +/** + * gdb_extend_query_table() - Extend query table. + * @table: The table with the additional query packet handlers. + * @size: The number of handlers to be added. + */ +void gdb_extend_query_table(GdbCmdParseEntry *table, int size); + +/** + * gdb_extend_set_table() - Extend set table. + * @table: The table with the additional set packet handlers. + * @size: The number of handlers to be added. + */ +void gdb_extend_set_table(GdbCmdParseEntry *table, int size); + +/** + * gdb_extend_qsupported_features() - Extend the qSupported features string. + * @qsupported_features: The additional qSupported feature(s) string. The string + * should start with a semicolon and, if there are more than one feature, the + * features should be separate by a semiocolon. + */ +void gdb_extend_qsupported_features(char *qsupported_features); + #endif /* GDBSTUB_COMMANDS_H */ From patchwork Mon Jun 17 06:28:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 804836 Delivered-To: patch@linaro.org Received: by 2002:adf:fb90:0:b0:360:93e7:1765 with SMTP id a16csp423827wrr; Sun, 16 Jun 2024 23:30:42 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXQOg8EXj3rKix02xd6Tg+WZAgYoYypLy8nDaIjw/oEpOBAS+hyFf5srU15y2eyLxWuYUZt2Zy+GYQq6mBoUsv+ X-Google-Smtp-Source: AGHT+IG4/H+Y0cDYMT6jya2qY5nj/b/0cq4WSLzuEJ+Y/44BFlsO1JLii8exubCTcUC6V69DaNJF X-Received: by 2002:a05:622a:1aa5:b0:441:59c6:770d with SMTP id d75a77b69052e-44216b4531emr143467261cf.60.1718605842427; Sun, 16 Jun 2024 23:30:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718605842; cv=none; d=google.com; s=arc-20160816; b=VEdV5Fs6TBBQGhXFlwjgQ2ZVbxpErKm4tAi/fEPQh6VvtT8OZ5EyfH7XVf4rD4WdP1 snVJkv85O/ZOmYFOvFPNMpM6ihRz6SVsz9/3vO8w4fsZt1f4lfzIvEj1nTOQXu0mEyOq M8EBfObLU4kcRKVjtDlwjtouMlvDAP05d6wXtwzp/QvR7d+IL6pTxfyLmnOSpJhMeCDt dN9D7eY2mIFT1lglhHOCBp2wmjBkJop5D/KtND5V3eCi1pj2jN7hSi+fuS1LG5JyRVfj OFhZW7DIPzYoOncqoXcvezqpw7dhk8Az5G/NPyx4rMdJgbc347oYMWccO0GcZsdlYpmF zD7A== 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=THKPrI8tkqIcVbNLqgSdh8XD5pC35UvKcKsvFX/vTJs=; fh=ZLVo7h1WCYX+mCQET83n+8bPmyNFw4pMQxFlbH8xyms=; b=hACDVeHyrYGtYyo/nVKsvjuf9JsU/w/7JLLkAgJsev2tTNn0LqEizHGpQ64sCJ3u4O xg/nxKQeYQjVi6N2uuOdwEr3tSQMZPCKipfKwQ0no3j7MyrGcKOje1i/4CpWnjZegnwc Yv1deOBMtL7rkI56+eI+Y7uxwSpem0XAawdnTVonpMxj6JWnBtCr2wdbcNJDnnIbYscJ U0FNeraWwR1678XcSeVmHyw2ly20O6vYeh29tYofqapSMrIV8qZVVAOq0VaDuQdt2iyu OvAkkOou7JkKJYsT3X4EcAzNBzxhSK5pjtu8kaDzm52ejcabXnz7G9Fbw5CceNfAuDY7 P9lA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sg015Bif; 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 d75a77b69052e-441f2fc7ee7si92355101cf.503.2024.06.16.23.30.41 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 16 Jun 2024 23:30:42 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sg015Bif; 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 1sJ5rp-0003mn-Fw; Mon, 17 Jun 2024 02:29:25 -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 1sJ5ro-0003me-8N for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:24 -0400 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sJ5rm-0003T2-Hd for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:24 -0400 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2c508ea0cc5so880884a91.1 for ; Sun, 16 Jun 2024 23:29:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718605760; x=1719210560; 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=THKPrI8tkqIcVbNLqgSdh8XD5pC35UvKcKsvFX/vTJs=; b=sg015BifnO6gDhoP+juRO+UzUVbYkMwnVES/ZPAlKWtDUkiuP+IS6z9SJTwwkBgAwS NrgFhj43wyGsxFtjzhmlvEpplzCbSNIWuQJ4ENVzJl6LmJINCXlA7B9vb762ZI+ihF/3 ZXeSqcLRK9JiVqpZJujq7oR677vKaMdd2ocdFXlE9kGbx05AckKorbc8uikheN421Mna /3IupMSjbzBBu+fWSzZtY1mdispriQpVzCGt4MVKVSiircMoiyfhOTINeSkuq756vE/d qj9SKnThs3ip9Yeu9c/MXhE7Jh16bun5lDIv9Oq+Z4slzGW10lzpGUq1ErcHkLAXUjsS Tw+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718605760; x=1719210560; 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=THKPrI8tkqIcVbNLqgSdh8XD5pC35UvKcKsvFX/vTJs=; b=ZwIENE1o/H29hIH1H/MLgE0Lcprhrwb7kplVs5TxZyKzbmZPCymZRJzsx9FqrWjYMo CoR/HYQJmnNFo1Mh8hiEFesct8D3yc6daSYDVevDO2Esb+V245MJKweTotjHb0xQEoDB Kpu8xxxt4T9upHvqJqmOybSQoRS95S3fKgOqQU5f4eyXSlStAMpfqTcuX+KtiRDMrHPP GOMJDC+ElqmcE7h4cj6qWKnT4dzTvKvkM8Jx5NAsisKeEB1Z8/xBUuoCwKB+MX245QKj aaviWnfLe4N4wQKdcqCo+Z09cLKVUMxIpIzMznkFtAPoB47OaQz6o/pC0p2r0gSivQp1 7GJg== X-Gm-Message-State: AOJu0Yw6OmAbFdXy/jDvHIzfayPntRIKvBep0tMirPmau6YPTiOb9nFf cwtZ9ERS0jD0PqbOj1lNaJpRqHJzUHkh6tZWJICa4hIHbaVmH7FWPVFxXwY9Mpn7B9pWXLkQQH/ b X-Received: by 2002:a17:90a:5d13:b0:2c3:274b:dfb0 with SMTP id 98e67ed59e1d1-2c4db24b497mr7594237a91.17.1718605760456; Sun, 16 Jun 2024 23:29:20 -0700 (PDT) Received: from amd.. ([2804:7f0:b400:8dcb:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ed69csm10641863a91.14.2024.06.16.23.29.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 23:29:20 -0700 (PDT) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org, peter.maydell@linaro.org, alex.bennee@linaro.org, richard.henderson@linaro.org Cc: gustavo.romero@linaro.org Subject: [PATCH v3 4/9] target/arm: Fix exception case in allocation_tag_mem_probe Date: Mon, 17 Jun 2024 06:28:44 +0000 Message-Id: <20240617062849.3531745-5-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240617062849.3531745-1-gustavo.romero@linaro.org> References: <20240617062849.3531745-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=gustavo.romero@linaro.org; helo=mail-pj1-x1036.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, T_SCC_BODY_TEXT_LINE=-0.01 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 If page in 'ptr_access' is inaccessible and probe is 'true' allocation_tag_mem_probe should not throw an exception, but currently it does, so fix it. Signed-off-by: Gustavo Romero Reviewed-by: Alex Bennée Reviewed-by: Richard Henderson --- target/arm/tcg/mte_helper.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/target/arm/tcg/mte_helper.c b/target/arm/tcg/mte_helper.c index 037ac6dd60..a50d576294 100644 --- a/target/arm/tcg/mte_helper.c +++ b/target/arm/tcg/mte_helper.c @@ -96,6 +96,9 @@ static uint8_t *allocation_tag_mem_probe(CPUARMState *env, int ptr_mmu_idx, assert(!(probe && ra)); if (!(flags & (ptr_access == MMU_DATA_STORE ? PAGE_WRITE_ORG : PAGE_READ))) { + if (probe) { + return NULL; + } cpu_loop_exit_sigsegv(env_cpu(env), ptr, ptr_access, !(flags & PAGE_VALID), ra); } From patchwork Mon Jun 17 06:28:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 804838 Delivered-To: patch@linaro.org Received: by 2002:adf:fb90:0:b0:360:93e7:1765 with SMTP id a16csp423860wrr; Sun, 16 Jun 2024 23:30:50 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXcn7lvDbnDidKpX6kDWnPy8OWOfhuPD9wEQY1X+DxJAaD5eD90a0H1CK/2yJB8ZWQcZVW8N+UzbHt3VMyeo7Un X-Google-Smtp-Source: AGHT+IFRXN5asYVKkdXKWkkGUHB0RBI0bTeI1K3n5uFANn/F9++mI90YAjWRiGrJr0atqTDxCHh2 X-Received: by 2002:a05:6808:16a7:b0:3d2:21d7:4b41 with SMTP id 5614622812f47-3d24e8e08c9mr10310179b6e.18.1718605849688; Sun, 16 Jun 2024 23:30:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718605849; cv=none; d=google.com; s=arc-20160816; b=WlUYtovJ049g9HNdr/NFxUiH4xcphgra/fytjT8ZgPlDjzd58pgVZDz2HGCfKORriL q8M572y4bZfK7DeejSfIgrakGr7TrcNpQwG+k6JWKWvy3b+0do6G4aCXPEpKcynXK8CX Lq7smU3rhk+aavoK4vAUI1ycI0oljkWkow/lgJDznLVobzT8uhAqTjEDwDp9CbuOK2qQ H+JZMoH+/NAtgiqg0itSfWi80EELicwSbxv2lrqlzvQOU2jhhbJjmjfjk16oDRvXVgMo iw/7WDrZZCgx5hNtZh4exB6buwBALDwD+cOVTlcFdpJ1a/OC3HhcJnC4OtBrxvsCPlRX //vQ== 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=ZHrQ3G77kgCnmRSnx6cW7o5SiefrhlcrhIVHrhW0O+A=; fh=ZLVo7h1WCYX+mCQET83n+8bPmyNFw4pMQxFlbH8xyms=; b=hG0ccJeQ/PQaPZfmbGirNhbvgvFzWmgJMk4pfGbI2Mt84+QcuTXB4vR/UkwDFZC9/R x3KOvetycENjngvl/0mu1f5dpS/99Ag6Vii7hTf2flmW8gbq1Sv3sG3mOcfJHCIp5/1x JUjpS2C3xvbuawEkE+cMYTvH/Z4dF3OzoPgMbxNcwn3uSx5wsR10a+yUHvbAQTyag3f0 fFu8ugPwCWDHt76EMl5siEwX5t2Fta2Nz48y8R9B5jh6XgivcwwXu+cZ/ceMQFdBVgeg wZ+6a5Xp5U1mGUPGnLKn48ZCKYwo6eTBiMaXOTfy7Azy6SeZ2Ah/WQjxDy0Accag6Y02 PLnQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VK1txVQS; 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 af79cd13be357-798ab903556si907716285a.371.2024.06.16.23.30.49 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 16 Jun 2024 23:30:49 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VK1txVQS; 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 1sJ5rt-0003nX-8i; Mon, 17 Jun 2024 02:29:29 -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 1sJ5rq-0003nA-Vm for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:27 -0400 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sJ5rp-0003TN-1x for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:26 -0400 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-681ad081695so2948419a12.3 for ; Sun, 16 Jun 2024 23:29:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718605763; x=1719210563; 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=ZHrQ3G77kgCnmRSnx6cW7o5SiefrhlcrhIVHrhW0O+A=; b=VK1txVQS3Z5CJZzZhL4ptrvr1qjEl+mw0bBFfmhyDN01tVWZN8ePHmUnbaM0+O97ZG lNs+xr8u9lWqI1px7ByGNhVL736h/d24lr1A2jjDWLsoRhYKilpqEL8HUeErYOMIDx5A vtuE2DaMjxQ5m/BL83inSB7r777ul5xDA2vCDwIg+44Q2sYmqPKLTu/p7ugqxsu0qJDM WK+xjU5chSPBmyJjM9C+MIIn95Ry5ToceVmiLUEL/jmM7oey8OjdC5Foy1RJq+QlvOCR myvvJFeSRgSyuU0D6F1VvuLGG8LJNioGjgWiFpSumHUqCfnNiqDHaKDwFzAk8fVnc6R5 mLog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718605763; x=1719210563; 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=ZHrQ3G77kgCnmRSnx6cW7o5SiefrhlcrhIVHrhW0O+A=; b=RY1qStKh1o2zHMjRUTTW2E5KOT4WukNCWbhiUapckicm3kwRpCgifAT6BXO2vHDdi6 bF8Oikd39zSvvNA5WkA+/pE1wVGbc50x2JykiO0pVnXNMuoLhVixXPtmpLRI3taz9ZGu VZxKpOmfgvi//BmPzwAwRo9dML72R1ZZWS0HKeDkb6VPLIxDDwOuW1bSO/izX8eSupX9 InB1PVaP+LdfD3TmiDLz6RaYRnunEDm3Klqg44ZmQsBaALRewN6pY+wafxvoFuU3MkuQ hvduGJ5WYrjuQmNTbn57apwDSzOHzbTa4cXq4RxDX1Mo6rJSRW9EOOUR6MRsVIRaPN1Q rSwg== X-Gm-Message-State: AOJu0YylKwPCTbVkSKUNmYMh6ZPKlBgvBLJAh+i4vrlOLtd2+ln1BIvW EfN0mr2u7rZNNjCPjhwafcjEUp8hslj3N9jGsw/JR+Nia68AZDy5r48RiXg1Dz7f6aKOEQsLNDQ o X-Received: by 2002:a05:6a20:9190:b0:1b8:3f12:c8ed with SMTP id adf61e73a8af0-1bae7ec30c9mr9677094637.24.1718605763218; Sun, 16 Jun 2024 23:29:23 -0700 (PDT) Received: from amd.. ([2804:7f0:b400:8dcb:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ed69csm10641863a91.14.2024.06.16.23.29.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 23:29:22 -0700 (PDT) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org, peter.maydell@linaro.org, alex.bennee@linaro.org, richard.henderson@linaro.org Cc: gustavo.romero@linaro.org Subject: [PATCH v3 5/9] target/arm: Make some MTE helpers widely available Date: Mon, 17 Jun 2024 06:28:45 +0000 Message-Id: <20240617062849.3531745-6-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240617062849.3531745-1-gustavo.romero@linaro.org> References: <20240617062849.3531745-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52b; envelope-from=gustavo.romero@linaro.org; helo=mail-pg1-x52b.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, T_SCC_BODY_TEXT_LINE=-0.01 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 Make the MTE helpers allocation_tag_mem_probe, load_tag1, and store_tag1 available to other subsystems. Signed-off-by: Gustavo Romero Reviewed-by: Richard Henderson --- target/arm/tcg/mte_helper.c | 45 +++++--------------------- target/arm/tcg/mte_helper.h | 63 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 38 deletions(-) create mode 100644 target/arm/tcg/mte_helper.h diff --git a/target/arm/tcg/mte_helper.c b/target/arm/tcg/mte_helper.c index a50d576294..e3d505e96a 100644 --- a/target/arm/tcg/mte_helper.c +++ b/target/arm/tcg/mte_helper.c @@ -29,6 +29,7 @@ #include "hw/core/tcg-cpu-ops.h" #include "qapi/error.h" #include "qemu/guest-random.h" +#include "mte_helper.h" static int choose_nonexcluded_tag(int tag, int offset, uint16_t exclude) @@ -50,42 +51,10 @@ static int choose_nonexcluded_tag(int tag, int offset, uint16_t exclude) return tag; } -/** - * allocation_tag_mem_probe: - * @env: the cpu environment - * @ptr_mmu_idx: the addressing regime to use for the virtual address - * @ptr: the virtual address for which to look up tag memory - * @ptr_access: the access to use for the virtual address - * @ptr_size: the number of bytes in the normal memory access - * @tag_access: the access to use for the tag memory - * @probe: true to merely probe, never taking an exception - * @ra: the return address for exception handling - * - * Our tag memory is formatted as a sequence of little-endian nibbles. - * That is, the byte at (addr >> (LOG2_TAG_GRANULE + 1)) contains two - * tags, with the tag at [3:0] for the lower addr and the tag at [7:4] - * for the higher addr. - * - * Here, resolve the physical address from the virtual address, and return - * a pointer to the corresponding tag byte. - * - * If there is no tag storage corresponding to @ptr, return NULL. - * - * If the page is inaccessible for @ptr_access, or has a watchpoint, there are - * three options: - * (1) probe = true, ra = 0 : pure probe -- we return NULL if the page is not - * accessible, and do not take watchpoint traps. The calling code must - * handle those cases in the right priority compared to MTE traps. - * (2) probe = false, ra = 0 : probe, no fault expected -- the caller guarantees - * that the page is going to be accessible. We will take watchpoint traps. - * (3) probe = false, ra != 0 : non-probe -- we will take both memory access - * traps and watchpoint traps. - * (probe = true, ra != 0 is invalid and will assert.) - */ -static uint8_t *allocation_tag_mem_probe(CPUARMState *env, int ptr_mmu_idx, - uint64_t ptr, MMUAccessType ptr_access, - int ptr_size, MMUAccessType tag_access, - bool probe, uintptr_t ra) +uint8_t *allocation_tag_mem_probe(CPUARMState *env, int ptr_mmu_idx, + uint64_t ptr, MMUAccessType ptr_access, + int ptr_size, MMUAccessType tag_access, + bool probe, uintptr_t ra) { #ifdef CONFIG_USER_ONLY uint64_t clean_ptr = useronly_clean_ptr(ptr); @@ -287,7 +256,7 @@ uint64_t HELPER(addsubg)(CPUARMState *env, uint64_t ptr, return address_with_allocation_tag(ptr + offset, rtag); } -static int load_tag1(uint64_t ptr, uint8_t *mem) +inline int load_tag1(uint64_t ptr, uint8_t *mem) { int ofs = extract32(ptr, LOG2_TAG_GRANULE, 1) * 4; return extract32(*mem, ofs, 4); @@ -321,7 +290,7 @@ static void check_tag_aligned(CPUARMState *env, uint64_t ptr, uintptr_t ra) } /* For use in a non-parallel context, store to the given nibble. */ -static void store_tag1(uint64_t ptr, uint8_t *mem, int tag) +inline void store_tag1(uint64_t ptr, uint8_t *mem, int tag) { int ofs = extract32(ptr, LOG2_TAG_GRANULE, 1) * 4; *mem = deposit32(*mem, ofs, 4, tag); diff --git a/target/arm/tcg/mte_helper.h b/target/arm/tcg/mte_helper.h new file mode 100644 index 0000000000..69ad8457f8 --- /dev/null +++ b/target/arm/tcg/mte_helper.h @@ -0,0 +1,63 @@ +/* + * ARM MemTag operation helpers. + * + * This code is licensed under the GNU GPL v2 or later. + * + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#ifndef TARGET_ARM_MTE_H +#define TARGET_ARM_MTE_H + +/** + * allocation_tag_mem_probe: + * @env: the cpu environment + * @ptr_mmu_idx: the addressing regime to use for the virtual address + * @ptr: the virtual address for which to look up tag memory + * @ptr_access: the access to use for the virtual address + * @ptr_size: the number of bytes in the normal memory access + * @tag_access: the access to use for the tag memory + * @probe: true to merely probe, never taking an exception + * @ra: the return address for exception handling + * + * Our tag memory is formatted as a sequence of little-endian nibbles. + * That is, the byte at (addr >> (LOG2_TAG_GRANULE + 1)) contains two + * tags, with the tag at [3:0] for the lower addr and the tag at [7:4] + * for the higher addr. + * + * Here, resolve the physical address from the virtual address, and return + * a pointer to the corresponding tag byte. + * + * If there is no tag storage corresponding to @ptr, return NULL. + * + * If the page is inaccessible for @ptr_access, or has a watchpoint, there are + * three options: + * (1) probe = true, ra = 0 : pure probe -- we return NULL if the page is not + * accessible, and do not take watchpoint traps. The calling code must + * handle those cases in the right priority compared to MTE traps. + * (2) probe = false, ra = 0 : probe, no fault expected -- the caller guarantees + * that the page is going to be accessible. We will take watchpoint traps. + * (3) probe = false, ra != 0 : non-probe -- we will take both memory access + * traps and watchpoint traps. + * (probe = true, ra != 0 is invalid and will assert.) + */ +uint8_t *allocation_tag_mem_probe(CPUARMState *env, int ptr_mmu_idx, + uint64_t ptr, MMUAccessType ptr_access, + int ptr_size, MMUAccessType tag_access, + bool probe, uintptr_t ra); +/** + * load_tag1 - Load 1 tag (nibble) from byte + * @ptr: The tagged address + * @mem: The tag address (packed, 2 tags in byte) + */ +int load_tag1(uint64_t ptr, uint8_t *mem); + +/** + * store_tag1 - Store 1 tag (nibble) into byte + * @ptr: The tagged address + * @mem: The tag address (packed, 2 tags in byte) + * @tag: The tag to be stored in the nibble + */ +void store_tag1(uint64_t ptr, uint8_t *mem, int tag); + +#endif /* TARGET_ARM_MTE_H */ From patchwork Mon Jun 17 06:28:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 804835 Delivered-To: patch@linaro.org Received: by 2002:adf:fb90:0:b0:360:93e7:1765 with SMTP id a16csp423825wrr; Sun, 16 Jun 2024 23:30:42 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWEkUbRKgZAS836gLdbkdqrNcrL22Nan+nfuCN3Vos2J2JpCkPFWieyw8w8fDJQLGVzN0Iv2u4w3JpJbccs7t9N X-Google-Smtp-Source: AGHT+IHACfweH+T/Vsyw6SGayFKuj1lS0bwmjARRWvSh8onuZ3STOmEgtY3G2NYsY1dApJupjZ9t X-Received: by 2002:a05:620a:2585:b0:794:f60e:c050 with SMTP id af79cd13be357-798d26a468bmr951937485a.71.1718605842050; Sun, 16 Jun 2024 23:30:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718605842; cv=none; d=google.com; s=arc-20160816; b=Yw1gKAEYYOWA1ZJJEcIP9F/1bsLcligmzoBYBgTW7aaLBD4sRUVm1C2kNO1UGZR9aN WxQm2wmuPSwkv4LM4M0t0g/B0YndKo4MfnSIGV2f3wf2KrGcqxK1599GYEUi3k6bjMDA oEDVJY0ED9ddYMmiVcP5YXB4firIN7+ZW0rCFi8TQtZue4a3gZzRTy53xKsDIR3HE9hS MNFtY9m5G005fgxi7pBe//GMUe8EN7z6+fgdzmRStH9wfIgMUs09cMWfQeTEnyCoD/m6 +C++K9V1w5oAwDJaK/311+MjlC79n2MsHkwLMrXGZooIaXxGFFfvdGvfGmK1Hx0iSepA eA3Q== 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=nd0arIutfBDSvvt+flGv2i8x1ts6dfIDGNUcyWMohdI=; fh=ZLVo7h1WCYX+mCQET83n+8bPmyNFw4pMQxFlbH8xyms=; b=EVbVTADLncnpGdz2LwLH2+sWpqeL+XjytEp5W+Zgv/gdT2Lrg3ayYtHShMN4syyMo4 FXCNobbxC8IXPFa3Y6zUZYMYDc/DH1MzNWSE28UVvtiLQOlZOGtdiC9LUvMAZ+SeeHvx Gc2c1qe8Mju3olk7wUu0UODD3+wB1aDzB47SZa3/JKAP9bD98aET/k7IihdDg0xGRXnL 5qzfItCabNL8d2L/AnrLT9TzukVjoimFbbv2n6Fs725rkooAipazl2Ff/7/PqdwG7gUi 5hEfRZD6lP/1dn/3PE86TkF1+uExbUWaQzRt2zEiuQxsr5a/Fo53HF3T2CjrGlGmgECv L1Ww==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="VxGMxv/O"; 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 d75a77b69052e-4421ad8efb3si68853831cf.55.2024.06.16.23.30.41 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 16 Jun 2024 23:30:42 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="VxGMxv/O"; 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 1sJ5rv-0003oE-2T; Mon, 17 Jun 2024 02:29:31 -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 1sJ5rt-0003nY-E9 for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:29 -0400 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sJ5rr-0003TY-Nr for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:29 -0400 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-70599522368so2779728b3a.2 for ; Sun, 16 Jun 2024 23:29:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718605766; x=1719210566; 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=nd0arIutfBDSvvt+flGv2i8x1ts6dfIDGNUcyWMohdI=; b=VxGMxv/OYpdnzES8/scfNCxztBPMY6UP+TpvFDEVskSJ1Yqg3j4CEb/XH8bshZLKqm iuK3jiVl+FOmNc949PNPyfpS+IgutBBVZoO0/a/yr58b+xMqFhWxjxv1pvGTjgTaRIiv u536P27OYChLagKFPXIvt/zqjIL3xI7HwqeSk0R8lS3gDuqvWkaVUpQF+9P0tYVl/K1T YGnvvIkoWHE0Dqn3dHVgZ2awg6kJBmXOIFhhy3KOX7KeqkGmr31F+sGaQmAsL6EYtKAd 7CeEZ2PCiDzfm5UQnBfv4KExh9wE7tvq2fB+zuj9iV4YTySiDpb6on1jmWNeQK/x23qd Q7ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718605766; x=1719210566; 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=nd0arIutfBDSvvt+flGv2i8x1ts6dfIDGNUcyWMohdI=; b=KXZUhbaGemDN1fqVpMUXpQqB8X4p9yYU0EpjhWZ0PghztzVLhF1ybTwlfH5FWsSI/i RrqPZkmxgT/LpPsvElA3V9mS57Y+7fmLP606XYWSMeRkrtGIryXnind6PvJlrP0BnOuQ 847A5/BWRJWC8LkdZu7Ik8/aU2TY5kALiMcazjfwANsC0Z5RJnqpb4XNeAzz7cxNxvPL QTnqSQiYdo1uIIfeQxOk5dhmoMvAYlc1l6nazWZlUTMbciT8HKWxScdTL/kqSFimyOO0 sBGs3leD8aX0m9x4WqKXT2EUsji7QGqpawtsdXhbrxUVhGPIz3gv3Io3T1rOdd0liTtn 5uxA== X-Gm-Message-State: AOJu0YwN9KTEompBbVYQOmKExFP2zIBGPLYq0CIY5gU8RjFsDF8em1AF JUsEZrYf794D6XRCZM4PC8Kw+J7BgWOsWuOrIBWn1mdQqPxMEEgtOukNQB97q5a+DOKzbl1xxDd E X-Received: by 2002:a05:6a20:6f8e:b0:1b5:d36a:5d6e with SMTP id adf61e73a8af0-1bae8214a4amr8881310637.43.1718605765907; Sun, 16 Jun 2024 23:29:25 -0700 (PDT) Received: from amd.. ([2804:7f0:b400:8dcb:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ed69csm10641863a91.14.2024.06.16.23.29.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 23:29:25 -0700 (PDT) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org, peter.maydell@linaro.org, alex.bennee@linaro.org, richard.henderson@linaro.org Cc: gustavo.romero@linaro.org Subject: [PATCH v3 6/9] target/arm: Factor out code for setting MTE TCF0 field Date: Mon, 17 Jun 2024 06:28:46 +0000 Message-Id: <20240617062849.3531745-7-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240617062849.3531745-1-gustavo.romero@linaro.org> References: <20240617062849.3531745-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=gustavo.romero@linaro.org; helo=mail-pf1-x42c.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, T_SCC_BODY_TEXT_LINE=-0.01 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 Factor out the code used for setting the MTE TCF0 field from the prctl code into a convenient function. Other subsystems, like gdbstub, need to set this field as well, so keep it as a separate function to avoid duplication and ensure consistency in how this field is set across the board. Signed-off-by: Gustavo Romero --- linux-user/aarch64/target_prctl.h | 22 ++--------------- target/arm/tcg/mte_user_helper.h | 40 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 target/arm/tcg/mte_user_helper.h diff --git a/linux-user/aarch64/target_prctl.h b/linux-user/aarch64/target_prctl.h index aa8e203c15..cfc8567eac 100644 --- a/linux-user/aarch64/target_prctl.h +++ b/linux-user/aarch64/target_prctl.h @@ -7,6 +7,7 @@ #define AARCH64_TARGET_PRCTL_H #include "target/arm/cpu-features.h" +#include "target/arm/tcg/mte_user_helper.h" static abi_long do_prctl_sve_get_vl(CPUArchState *env) { @@ -173,26 +174,7 @@ static abi_long do_prctl_set_tagged_addr_ctrl(CPUArchState *env, abi_long arg2) env->tagged_addr_enable = arg2 & PR_TAGGED_ADDR_ENABLE; if (cpu_isar_feature(aa64_mte, cpu)) { - /* - * Write PR_MTE_TCF to SCTLR_EL1[TCF0]. - * - * The kernel has a per-cpu configuration for the sysadmin, - * /sys/devices/system/cpu/cpu/mte_tcf_preferred, - * which qemu does not implement. - * - * Because there is no performance difference between the modes, and - * because SYNC is most useful for debugging MTE errors, choose SYNC - * as the preferred mode. With this preference, and the way the API - * uses only two bits, there is no way for the program to select - * ASYMM mode. - */ - unsigned tcf = 0; - if (arg2 & PR_MTE_TCF_SYNC) { - tcf = 1; - } else if (arg2 & PR_MTE_TCF_ASYNC) { - tcf = 2; - } - env->cp15.sctlr_el[1] = deposit64(env->cp15.sctlr_el[1], 38, 2, tcf); + arm_set_mte_tcf0(env, arg2); /* * Write PR_MTE_TAG to GCR_EL1[Exclude]. diff --git a/target/arm/tcg/mte_user_helper.h b/target/arm/tcg/mte_user_helper.h new file mode 100644 index 0000000000..dee74d0923 --- /dev/null +++ b/target/arm/tcg/mte_user_helper.h @@ -0,0 +1,40 @@ +/* + * ARM MemTag convenience functions. + * + * This code is licensed under the GNU GPL v2 or later. + * + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#ifndef MTE_H +#define MTE_H + +#ifdef CONFIG_USER_ONLY +#include "sys/prctl.h" + +static inline void arm_set_mte_tcf0(CPUArchState *env, abi_long value) +{ + /* + * Write PR_MTE_TCF to SCTLR_EL1[TCF0]. + * + * The kernel has a per-cpu configuration for the sysadmin, + * /sys/devices/system/cpu/cpu/mte_tcf_preferred, + * which qemu does not implement. + * + * Because there is no performance difference between the modes, and + * because SYNC is most useful for debugging MTE errors, choose SYNC + * as the preferred mode. With this preference, and the way the API + * uses only two bits, there is no way for the program to select + * ASYMM mode. + */ + unsigned tcf = 0; + if (value & PR_MTE_TCF_SYNC) { + tcf = 1; + } else if (value & PR_MTE_TCF_ASYNC) { + tcf = 2; + } + env->cp15.sctlr_el[1] = deposit64(env->cp15.sctlr_el[1], 38, 2, tcf); +} +#endif /* CONFIG_USER_ONLY */ + +#endif /* MTE_H */ From patchwork Mon Jun 17 06:28:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 804831 Delivered-To: patch@linaro.org Received: by 2002:adf:fb90:0:b0:360:93e7:1765 with SMTP id a16csp423567wrr; Sun, 16 Jun 2024 23:29:51 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCU/XGPuA9l1MnNs1iExXCHWPJjZGOI8Yasc5phCQEDI+NeiMk/tSAScNztjuoMRiTVPdzDxqwsiQUC/qCBijBZl X-Google-Smtp-Source: AGHT+IG7vzcio/dgzLuDsHLpiDdUiyDFF1lH0yXw99FBwKTFk1Rma4TmfseVDfv8LTVSpFHCR4kR X-Received: by 2002:a05:620a:2990:b0:795:5d1e:2f05 with SMTP id af79cd13be357-798d24214f3mr1108495885a.19.1718605791189; Sun, 16 Jun 2024 23:29:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718605791; cv=none; d=google.com; s=arc-20160816; b=NRAcpDt8blvhEHA0DLYW0y7oyfu+uVsJ9v4gsS9bGG+qfwoWlRT5fUO+S+NRDhzUCv 0eNCcJLOt6ll2sU7WdvmU0RMcAvL7DLgfnD4/JY92vGH7ueUnlntpfnNcC4wI4URTWFp KFMKsLsNf3xt8eO8WKo61fjlnXSHc/TcnGwpU2XMBLVmnpC1oDyvKwzUAZNhhcsOmB+x CrnHfLU+4f7xAi/9ZDZHGdZilkny3Wl0/BQRnUz9eBqnV2jhqQjiJzXWIeSNJr1FNZof q3J7fq3qrbi4J80eEB/RP8RAbw6mWJZcWJyqYTzQbATUrfPtb5k5QdBJzK+AhnoWz5SD 0dCg== 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=YGDcQ/GV1X1agXVxeAbW8TiRh8SRfIqjde3rxwV7xb0=; fh=ZLVo7h1WCYX+mCQET83n+8bPmyNFw4pMQxFlbH8xyms=; b=PzqXIsk5tpp8S4KdcoPIEMDXNjB21o9jHf4EagL77ylt7snbkBEgobViWj+6PYBvd+ Q+nNxkgp0t1CMPJ0vFFXeTeUoMKlVSq2BKLOZFNYiGxg0Cjdd0FYNEMQC2ZF8ZYlzbPb uzZ6j9B3Oy4vfpmgJCOKamE/icVCaAnAZNiGDT3oi5DAqLgALvF1ox2eW6wrgq9H01XY a/Xp7J0Uy5rk9jTdsG0l6MgKAhD1Ccy3B4T3LsS67EO2lym6fcE7Q4/2WQ3/CWBsyPuc yvs+FMP6f/nqIVAaO6iLU2jBbhpNO3IMZYINa8L+3Wtbncrmk/z9Dd+qY1DdhOxvXuJs yFBA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=H7UwL1Af; 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 af79cd13be357-798abe6e402si914312785a.673.2024.06.16.23.29.51 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 16 Jun 2024 23:29:51 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=H7UwL1Af; 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 1sJ5rz-0003oh-3P; Mon, 17 Jun 2024 02:29:35 -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 1sJ5rv-0003oN-Qa for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:32 -0400 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sJ5ru-0003Tj-9L for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:31 -0400 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-709423bc2e5so620495a12.0 for ; Sun, 16 Jun 2024 23:29:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718605769; x=1719210569; 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=YGDcQ/GV1X1agXVxeAbW8TiRh8SRfIqjde3rxwV7xb0=; b=H7UwL1AfHcBZWnc2sIxfTJaZmE3ZqV3pD0ayAcbjEa3gtUmf6Ucms294OBNT9FcEjp bBr7ZMxbo2rCc/jTP5awQTz5jLbLXcTHCF7dOmmg3OPbJPyp5QitkWqdvdtZZoYiY/b6 JdEB1421VucxMIGLcPFol7nKXeY0ebuK/VLVmP/OvF3J+rSx6uiUNIdp9JTn6KPvzokI cM/3U+wr3/jk5FZf6GhJY/aeaG1IVHZmcy2nGRTWhx07i411YuImmowpZ1pJB2tH4vsq Ik096fmVhxIcmwLYpCSMqi/YZjKddO1ayRmqtfL4AsN0hyPn9AownMAECQEc9zU2tVil PkvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718605769; x=1719210569; 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=YGDcQ/GV1X1agXVxeAbW8TiRh8SRfIqjde3rxwV7xb0=; b=cbUtlhJVLrSHhBugUtSwiQZyjsjYMywDd1kHiMCUz4XuKBe+S0I4LlwknR0RkzoqVL QZPfcP8N1ycwlHWIYQABD5AeIK+ymGShfA4D1P+IzVU89L9srNC75ehdB6yY4pasqq3D 5fre+j0nQbvUlmi7N7uqLR4I1tBaQonhz9om4Hjc4O8PvGAvkheo900eSbqh8wf7PXCS jQ8d+pWk7+NXoUO/nh6Cf/UWB+PB4fy0FFXSreg4FKq9CseU86qqiq9tzIZdMUPy31aM EPKiizQ0c/qF9/Mgxg0Ib4eDyjF+kOSbPplVbB9t96w6gNzx+Gj5kWIFCHB4Zvj7jcB6 vZNg== X-Gm-Message-State: AOJu0YzGGSQbvVBfawu2ZMO9nog7Pqc4RqyfB6WlJy6iwY42QOZrI5Oc cI7y1CyhHoSvqTpzl9bKc/qYZUsf8nIUkxLpWT9SlmeedUkwff/iz8drkeeWCkpyrTG5luS6Abr m X-Received: by 2002:a17:90a:a389:b0:2c5:2acb:1acd with SMTP id 98e67ed59e1d1-2c52acb1bf8mr1693418a91.10.1718605768724; Sun, 16 Jun 2024 23:29:28 -0700 (PDT) Received: from amd.. ([2804:7f0:b400:8dcb:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ed69csm10641863a91.14.2024.06.16.23.29.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 23:29:28 -0700 (PDT) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org, peter.maydell@linaro.org, alex.bennee@linaro.org, richard.henderson@linaro.org Cc: gustavo.romero@linaro.org Subject: [PATCH v3 7/9] gdbstub: Make get cpu and hex conversion functions non-internal Date: Mon, 17 Jun 2024 06:28:47 +0000 Message-Id: <20240617062849.3531745-8-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240617062849.3531745-1-gustavo.romero@linaro.org> References: <20240617062849.3531745-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52b; envelope-from=gustavo.romero@linaro.org; helo=mail-pg1-x52b.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, T_SCC_BODY_TEXT_LINE=-0.01 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 Make the gdb_first_attached_cpu and gdb_hextomem non-internal so they are not confined to use only in gdbstub.c. Signed-off-by: Gustavo Romero Reviewed-by: Richard Henderson --- gdbstub/internals.h | 2 -- include/exec/gdbstub.h | 5 +++++ include/gdbstub/commands.h | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/gdbstub/internals.h b/gdbstub/internals.h index 34121dc61a..81875abf5f 100644 --- a/gdbstub/internals.h +++ b/gdbstub/internals.h @@ -107,7 +107,6 @@ static inline int tohex(int v) void gdb_put_strbuf(void); int gdb_put_packet_binary(const char *buf, int len, bool dump); -void gdb_hextomem(GByteArray *mem, const char *buf, int len); void gdb_memtohex(GString *buf, const uint8_t *mem, int len); void gdb_memtox(GString *buf, const char *mem, int len); void gdb_read_byte(uint8_t ch); @@ -130,7 +129,6 @@ bool gdb_got_immediate_ack(void); /* utility helpers */ GDBProcess *gdb_get_process(uint32_t pid); CPUState *gdb_get_first_cpu_in_process(GDBProcess *process); -CPUState *gdb_first_attached_cpu(void); void gdb_append_thread_id(CPUState *cpu, GString *buf); int gdb_get_cpu_index(CPUState *cpu); unsigned int gdb_get_max_cpus(void); /* both */ diff --git a/include/exec/gdbstub.h b/include/exec/gdbstub.h index 1bd2c4ec2a..77e5ec9a5b 100644 --- a/include/exec/gdbstub.h +++ b/include/exec/gdbstub.h @@ -135,4 +135,9 @@ void gdb_set_stop_cpu(CPUState *cpu); /* in gdbstub-xml.c, generated by scripts/feature_to_c.py */ extern const GDBFeature gdb_static_features[]; +/** + * Return the first attached CPU + */ +CPUState *gdb_first_attached_cpu(void); + #endif /* GDBSTUB_H */ diff --git a/include/gdbstub/commands.h b/include/gdbstub/commands.h index 2204c3ddbe..914b6d7313 100644 --- a/include/gdbstub/commands.h +++ b/include/gdbstub/commands.h @@ -93,4 +93,10 @@ void gdb_extend_set_table(GdbCmdParseEntry *table, int size); */ void gdb_extend_qsupported_features(char *qsupported_features); +/** + * Convert a hex string to bytes. Conversion is done per byte, so 2 hex digits + * are converted to 1 byte. Invalid hex digits are treated as 0 digits. + */ +void gdb_hextomem(GByteArray *mem, const char *buf, int len); + #endif /* GDBSTUB_COMMANDS_H */ From patchwork Mon Jun 17 06:28:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 804839 Delivered-To: patch@linaro.org Received: by 2002:adf:fb90:0:b0:360:93e7:1765 with SMTP id a16csp423878wrr; Sun, 16 Jun 2024 23:30:55 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWXXd+vOgQo4TaYIRTzI5RG+EIcHRp2H0lL4+VaA4raa9IUDXaWSrlammWywK6LwAIJ8DyeSdj9WknYeS6zFpDf X-Google-Smtp-Source: AGHT+IHiw2OuwEjDAIoQZw1DqT4kOeIuKyHrKV/v2VcCdtXEOUrkOxaEsj1voIz1Ezf+9gZQGds9 X-Received: by 2002:a05:6102:512a:b0:48d:73d4:8b7e with SMTP id ada2fe7eead31-48dae322a1emr10297547137.20.1718605854868; Sun, 16 Jun 2024 23:30:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718605854; cv=none; d=google.com; s=arc-20160816; b=LCknrNPkgjwfnJqcSHJs/DcDucz59gxpDXvsdqklAX6YOJyX5o3rEokjwMgWIjEsZ2 ZdJbUKzBxIcFZJt8/0GNtYvjewz7dvr8IP93hP/XzOv7v8tHppUoq5Y/GJ9ijRXIqsNY 0qYtrZHF+6OZCQNkSbwqYnNI5P9SKE8aC9hNi74hYVHTPB1VLio1VWDK9f4JdQOeS4FP BWRsmYmhWs5lGYhD6eB5GkJtBtSbqnkd8QXWKZahQ8eCABQfn5/bXChIF8TF20/30jM+ WanJqcwhCswMvXCzmxfwQ+3hgdLrrThc5rQmiGieXoBHGWyB4CAlHDl6EF7x4RxNXN50 rkOQ== 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=MS31PBh9kytr9SgobPJ6M5BYzekU30VYtxk+7KFtYPA=; fh=ZLVo7h1WCYX+mCQET83n+8bPmyNFw4pMQxFlbH8xyms=; b=ZgbxSqgfFhy8kbmt9d9YqBWlNpjIBJZV/nsbPhDyrol5ChJIz5ZfOhQ43XUzWnWDLH Zs6hKkq2LbseFF8SKAF5aTZSI8Jh7KDziRHxcR88RdFoEo1Q6VVRzfzyMt0Cqd91aRFR 2evPXr67C8NkXULPnawo+PSZP29felEyf7N/H6zLi15db18Lyevct+MdlHZAx1NyCBWs 8CBI7FiJzcOfHAlEluRjEKoklcAUX5xJxsSQAXIlTCmTDxweUaeunkMqv0mtMIMMK0wC xzIYCN5k0c0PDffR7uhF7vDW6AjB9z8DGktGrIjNmeI3TF6SAbwsih1YCMcbOy5JyFTy mY2w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=aTCJv789; 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 d75a77b69052e-441ef3b9e95si65431171cf.131.2024.06.16.23.30.54 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 16 Jun 2024 23:30:54 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=aTCJv789; 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 1sJ5s2-0003pD-4d; Mon, 17 Jun 2024 02:29:38 -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 1sJ5s0-0003p1-Nk for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:36 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sJ5ry-0003Tx-DT for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:36 -0400 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-70109d34a16so3542680b3a.2 for ; Sun, 16 Jun 2024 23:29:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718605771; x=1719210571; 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=MS31PBh9kytr9SgobPJ6M5BYzekU30VYtxk+7KFtYPA=; b=aTCJv789FHe0LFI7RUc8t23nk1fi1C56AbHgbWkCcI5rOJi/MSxITZCYrry8pNf0sW zyjNsCZEVfJxPIq9rG/zpgb2FeJlRa3NC6aPKtTwU0lyi78pomHQW8jOa5JZWH4hWQcw 4x3EZ+swpfRCybDMhNeF9adwAwpSLo2sB0OYDfbC5s3188MsGPIDftb17LYom9TRYU4T td7ooVdX7qzOvqIdyATgXdxTPn5fB9uP0kK7s9WxCEUP1yfBX/uZD9VoGf7hMrGsU43Z LfkK7sj9dZFgACmyc9ltI0ezY3OqWMh/f7wvERBfCoT7VD3x+gMCYGBBhnvVSOA1CcaN eSig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718605771; x=1719210571; 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=MS31PBh9kytr9SgobPJ6M5BYzekU30VYtxk+7KFtYPA=; b=M5WD/JAQ+zFpdJ7m2SiiLEk0SypK1v7SaDReN+JBsI8W9NbV0OxguIVsQbnd//4J2r KlsFnu+CgCL6Vm3npCwEVbuZU9wJbyS9n++VBz2zWmkmXzSF1snnvLZjs//qC4rFaJGZ hofS+00qMN8Ug0FzccCtyI7Lx/Mnv9UREt29JqMZyEkuaLqQEXAuRgKnNt9g8j4NhF0B reXB/rhPNrOlQTR2whpRDOUZoX0XEnvOboaHAlDU9lqJYyTQfMDfvNPJijn5bshjMlv3 IobSmeGJjV/f4PReax/11t3U9lrkQkXMnxE6TpFq8kytAAhRPrrMNAR+rIDNcD/L0Sqb FRUQ== X-Gm-Message-State: AOJu0YxEieKAakUBWnsRlQS/74mPvE/lmlYCfeN/Ma3oxQAiH768Gp6k ErHsGB8pjIUXhg9hrf1KPP6AReLz1ZKXpk3ynzcNaTYZPx0f7kijnEJiTO4/6brWoL+6lJp+i+n f X-Received: by 2002:a05:6a20:f3a8:b0:1b6:1a81:5dad with SMTP id adf61e73a8af0-1bae7e9c52bmr8449871637.14.1718605771395; Sun, 16 Jun 2024 23:29:31 -0700 (PDT) Received: from amd.. ([2804:7f0:b400:8dcb:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ed69csm10641863a91.14.2024.06.16.23.29.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 23:29:31 -0700 (PDT) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org, peter.maydell@linaro.org, alex.bennee@linaro.org, richard.henderson@linaro.org Cc: gustavo.romero@linaro.org Subject: [PATCH v3 8/9] gdbstub: Add support for MTE in user mode Date: Mon, 17 Jun 2024 06:28:48 +0000 Message-Id: <20240617062849.3531745-9-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240617062849.3531745-1-gustavo.romero@linaro.org> References: <20240617062849.3531745-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::430; envelope-from=gustavo.romero@linaro.org; helo=mail-pf1-x430.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, T_SCC_BODY_TEXT_LINE=-0.01 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 This commit implements the stubs to handle the qIsAddressTagged, qMemTag, and QMemTag GDB packets, allowing all GDB 'memory-tag' subcommands to work with QEMU gdbstub on aarch64 user mode. It also implements the get/set functions for the special GDB MTE register 'tag_ctl', used to control the MTE fault type at runtime. Signed-off-by: Gustavo Romero --- configs/targets/aarch64-linux-user.mak | 2 +- gdb-xml/aarch64-mte.xml | 11 ++ target/arm/cpu.c | 1 + target/arm/gdbstub.c | 46 ++++++ target/arm/gdbstub64.c | 217 +++++++++++++++++++++++++ target/arm/internals.h | 6 + 6 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 gdb-xml/aarch64-mte.xml diff --git a/configs/targets/aarch64-linux-user.mak b/configs/targets/aarch64-linux-user.mak index ba8bc5fe3f..8f0ed21d76 100644 --- a/configs/targets/aarch64-linux-user.mak +++ b/configs/targets/aarch64-linux-user.mak @@ -1,6 +1,6 @@ TARGET_ARCH=aarch64 TARGET_BASE_ARCH=arm -TARGET_XML_FILES= gdb-xml/aarch64-core.xml gdb-xml/aarch64-fpu.xml gdb-xml/aarch64-pauth.xml +TARGET_XML_FILES= gdb-xml/aarch64-core.xml gdb-xml/aarch64-fpu.xml gdb-xml/aarch64-pauth.xml gdb-xml/aarch64-mte.xml TARGET_HAS_BFLT=y CONFIG_SEMIHOSTING=y CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y diff --git a/gdb-xml/aarch64-mte.xml b/gdb-xml/aarch64-mte.xml new file mode 100644 index 0000000000..4b70b4f17a --- /dev/null +++ b/gdb-xml/aarch64-mte.xml @@ -0,0 +1,11 @@ + + + + + + + diff --git a/target/arm/cpu.c b/target/arm/cpu.c index 35fa281f1b..14d4eca127 100644 --- a/target/arm/cpu.c +++ b/target/arm/cpu.c @@ -2518,6 +2518,7 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp) register_cp_regs_for_features(cpu); arm_cpu_register_gdb_regs_for_features(cpu); + arm_cpu_register_gdb_commands(cpu); init_cpreg_list(cpu); diff --git a/target/arm/gdbstub.c b/target/arm/gdbstub.c index a3bb73cfa7..c3a9b5eb1e 100644 --- a/target/arm/gdbstub.c +++ b/target/arm/gdbstub.c @@ -21,6 +21,7 @@ #include "cpu.h" #include "exec/gdbstub.h" #include "gdbstub/helpers.h" +#include "gdbstub/commands.h" #include "sysemu/tcg.h" #include "internals.h" #include "cpu-features.h" @@ -474,6 +475,41 @@ static GDBFeature *arm_gen_dynamic_m_secextreg_feature(CPUState *cs, #endif #endif /* CONFIG_TCG */ +void arm_cpu_register_gdb_commands(ARMCPU *cpu) +{ + GArray *query_table = + g_array_new(FALSE, FALSE, sizeof(GdbCmdParseEntry)); + GArray *set_table = + g_array_new(FALSE, FALSE, sizeof(GdbCmdParseEntry)); + GString *qsupported_features = g_string_new(NULL); + + if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { + #ifdef TARGET_AARCH64 + aarch64_cpu_register_gdb_commands(cpu, qsupported_features, query_table, + set_table); + #endif + } + + /* Set arch-specific handlers for 'q' commands. */ + if (query_table->len) { + gdb_extend_query_table(&g_array_index(query_table, + GdbCmdParseEntry, 0), + query_table->len); + } + + /* Set arch-specific handlers for 'Q' commands. */ + if (set_table->len) { + gdb_extend_set_table(&g_array_index(set_table, + GdbCmdParseEntry, 0), + set_table->len); + } + + /* Set arch-specific qSupported feature. */ + if (qsupported_features->len) { + gdb_extend_qsupported_features(qsupported_features->str); + } +} + void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu) { CPUState *cs = CPU(cpu); @@ -507,6 +543,16 @@ void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu) gdb_find_static_feature("aarch64-pauth.xml"), 0); } + +#ifdef CONFIG_USER_ONLY + /* Memory Tagging Extension (MTE) 'tag_ctl' pseudo-register. */ + if (cpu_isar_feature(aa64_mte, cpu)) { + gdb_register_coprocessor(cs, aarch64_gdb_get_tag_ctl_reg, + aarch64_gdb_set_tag_ctl_reg, + gdb_find_static_feature("aarch64-mte.xml"), + 0); + } +#endif #endif } else { if (arm_feature(env, ARM_FEATURE_NEON)) { diff --git a/target/arm/gdbstub64.c b/target/arm/gdbstub64.c index caa31ff3fa..f94a416836 100644 --- a/target/arm/gdbstub64.c +++ b/target/arm/gdbstub64.c @@ -21,6 +21,9 @@ #include "cpu.h" #include "internals.h" #include "gdbstub/helpers.h" +#include "gdbstub/commands.h" +#include "tcg/mte_helper.h" +#include "tcg/mte_user_helper.h" int aarch64_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n) { @@ -381,3 +384,217 @@ GDBFeature *arm_gen_dynamic_svereg_feature(CPUState *cs, int base_reg) return &cpu->dyn_svereg_feature.desc; } + +#ifdef CONFIG_USER_ONLY +int aarch64_gdb_get_tag_ctl_reg(CPUState *cs, GByteArray *buf, int reg) +{ + ARMCPU *cpu = ARM_CPU(cs); + CPUARMState *env = &cpu->env; + uint64_t tcf0; + + assert(reg == 0); + + tcf0 = extract64(env->cp15.sctlr_el[1], 38, 2); + + return gdb_get_reg64(buf, tcf0); +} + +int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int reg) +{ + ARMCPU *cpu = ARM_CPU(cs); + CPUARMState *env = &cpu->env; + + uint8_t tcf; + + assert(reg == 0); + + tcf = *buf << PR_MTE_TCF_SHIFT; + + if (!tcf) { + return 0; + } + + /* + * 'tag_ctl' register is actually a "pseudo-register" provided by GDB to + * expose options regarding the type of MTE fault that can be controlled at + * runtime. + */ + arm_set_mte_tcf0(env, tcf); + + return 1; +} + +static void handle_q_memtag(GArray *params, G_GNUC_UNUSED void *user_ctx) +{ + ARMCPU *cpu = ARM_CPU(gdb_first_attached_cpu()); + CPUARMState *env = &cpu->env; + + uint64_t addr = gdb_get_cmd_param(params, 0)->val_ull; + uint64_t len = gdb_get_cmd_param(params, 1)->val_ul; + int type = gdb_get_cmd_param(params, 2)->val_ul; + + uint8_t *tags; + uint8_t addr_tag; + + g_autoptr(GString) str_buf = g_string_new(NULL); + + /* + * GDB does not query multiple tags for a memory range on remote targets, so + * that's not supported either by gdbstub. + */ + if (len != 1) { + gdb_put_packet("E02"); + } + + /* GDB never queries a tag different from an allocation tag (type 1). */ + if (type != 1) { + gdb_put_packet("E03"); + } + + /* Note that tags are packed here (2 tags packed in one byte). */ + tags = allocation_tag_mem_probe(env, 0, addr, MMU_DATA_LOAD, 8 /* 64-bit */, + MMU_DATA_LOAD, true, 0); + if (!tags) { + /* Address is not in a tagged region. */ + gdb_put_packet("E04"); + return; + } + + /* Unpack tag from byte. */ + addr_tag = load_tag1(addr, tags); + g_string_printf(str_buf, "m%.2x", addr_tag); + + gdb_put_packet(str_buf->str); +} + +static void handle_q_isaddresstagged(GArray *params, G_GNUC_UNUSED void *user_ctx) +{ + ARMCPU *cpu = ARM_CPU(gdb_first_attached_cpu()); + CPUARMState *env = &cpu->env; + + uint64_t addr = gdb_get_cmd_param(params, 0)->val_ull; + + uint8_t *tags; + const char *reply; + + tags = allocation_tag_mem_probe(env, 0, addr, MMU_DATA_LOAD, 8 /* 64-bit */, + MMU_DATA_LOAD, true, 0); + reply = tags ? "01" : "00"; + + gdb_put_packet(reply); +} + +static void handle_Q_memtag(GArray *params, G_GNUC_UNUSED void *user_ctx) +{ + ARMCPU *cpu = ARM_CPU(gdb_first_attached_cpu()); + CPUARMState *env = &cpu->env; + + uint64_t start_addr = gdb_get_cmd_param(params, 0)->val_ull; + uint64_t len = gdb_get_cmd_param(params, 1)->val_ul; + int type = gdb_get_cmd_param(params, 2)->val_ul; + char const *new_tags_str = gdb_get_cmd_param(params, 3)->data; + + uint64_t end_addr; + + int num_new_tags; + uint8_t *tags; + + g_autoptr(GByteArray) new_tags = g_byte_array_new(); + + /* + * Only the allocation tag (i.e. type 1) can be set at the stub side. + */ + if (type != 1) { + gdb_put_packet("E02"); + return; + } + + end_addr = start_addr + (len - 1); /* 'len' is always >= 1 */ + /* Check if request's memory range does not cross page boundaries. */ + if ((start_addr ^ end_addr) & TARGET_PAGE_MASK) { + gdb_put_packet("E03"); + return; + } + + /* + * Get all tags in the page starting from the tag of the start address. + * Note that there are two tags packed into a single byte here. + */ + tags = allocation_tag_mem_probe(env, 0, start_addr, MMU_DATA_STORE, + 8 /* 64-bit */, MMU_DATA_STORE, true, 0); + if (!tags) { + /* Address is not in a tagged region. */ + gdb_put_packet("E04"); + return; + } + + /* Convert tags provided by GDB, 2 hex digits per tag. */ + num_new_tags = strlen(new_tags_str) / 2; + gdb_hextomem(new_tags, new_tags_str, num_new_tags); + + uint64_t address = start_addr; + int new_tag_index = 0; + while (address <= end_addr) { + uint8_t new_tag; + int packed_index; + + /* + * Find packed tag index from unpacked tag index. There are two tags + * in one packed index (one tag per nibble). + */ + packed_index = new_tag_index / 2; + + new_tag = new_tags->data[new_tag_index % num_new_tags]; + store_tag1(address, tags + packed_index, new_tag); + + address += TAG_GRANULE; + new_tag_index++; + } + + gdb_put_packet("OK"); +} + +enum Command { + qMemTags, + qIsAddressTagged, + QMemTags, + NUM_CMDS +}; + +static GdbCmdParseEntry cmd_handler_table[NUM_CMDS] = { + [qMemTags] = { + .handler = handle_q_memtag, + .cmd_startswith = 1, + .cmd = "MemTags:", + .schema = "L,l:l0" + }, + [qIsAddressTagged] = { + .handler = handle_q_isaddresstagged, + .cmd_startswith = 1, + .cmd = "IsAddressTagged:", + .schema = "L0" + }, + [QMemTags] = { + .handler = handle_Q_memtag, + .cmd_startswith = 1, + .cmd = "MemTags:", + .schema = "L,l:l:s0" + }, +}; +#endif /* CONFIG_USER_ONLY */ + +void aarch64_cpu_register_gdb_commands(ARMCPU *cpu, GString *qsupported, + GArray *qtable, GArray *stable) +{ +#ifdef CONFIG_USER_ONLY + /* MTE */ + if (cpu_isar_feature(aa64_mte, cpu)) { + g_string_append(qsupported, ";memory-tagging+"); + + g_array_append_val(qtable, cmd_handler_table[qMemTags]); + g_array_append_val(qtable, cmd_handler_table[qIsAddressTagged]); + + g_array_append_val(stable, cmd_handler_table[QMemTags]); + } +#endif +} diff --git a/target/arm/internals.h b/target/arm/internals.h index 11b5da2562..e1aa1a63b9 100644 --- a/target/arm/internals.h +++ b/target/arm/internals.h @@ -358,6 +358,10 @@ void init_cpreg_list(ARMCPU *cpu); void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu); void arm_translate_init(void); +void arm_cpu_register_gdb_commands(ARMCPU *cpu); +void aarch64_cpu_register_gdb_commands(ARMCPU *cpu, GString *, GArray *, + GArray *); + void arm_restore_state_to_opc(CPUState *cs, const TranslationBlock *tb, const uint64_t *data); @@ -1640,6 +1644,8 @@ int aarch64_gdb_get_fpu_reg(CPUState *cs, GByteArray *buf, int reg); int aarch64_gdb_set_fpu_reg(CPUState *cs, uint8_t *buf, int reg); int aarch64_gdb_get_pauth_reg(CPUState *cs, GByteArray *buf, int reg); int aarch64_gdb_set_pauth_reg(CPUState *cs, uint8_t *buf, int reg); +int aarch64_gdb_get_tag_ctl_reg(CPUState *cs, GByteArray *buf, int reg); +int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int reg); void arm_cpu_sve_finalize(ARMCPU *cpu, Error **errp); void arm_cpu_sme_finalize(ARMCPU *cpu, Error **errp); void arm_cpu_pauth_finalize(ARMCPU *cpu, Error **errp); From patchwork Mon Jun 17 06:28:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 804837 Delivered-To: patch@linaro.org Received: by 2002:adf:fb90:0:b0:360:93e7:1765 with SMTP id a16csp423845wrr; Sun, 16 Jun 2024 23:30:48 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWiN78iRLH7J1OD8G+z2PHSdtG40zUFkGa3YqWgI4caE/ZavZ0w3tmKMavubL99hIHDshBvCTwu7B9SqnOiOocw X-Google-Smtp-Source: AGHT+IErCZDhtwMMcdSt3P1naiPs9Ik4uq5G7wdHtToMTRv6DyDFUISuqfU6KMtntoUZtO5dxTw0 X-Received: by 2002:a05:6808:2118:b0:3d2:2768:c8e2 with SMTP id 5614622812f47-3d24e9303b0mr10437276b6e.30.1718605847975; Sun, 16 Jun 2024 23:30:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718605847; cv=none; d=google.com; s=arc-20160816; b=RSDa1HECf6CTvpnmocladVISr6nDlKIrKIy0raXR4a5ESTMXICuEYlEF5bQfm/VPe7 QuUt+m8jNBGQjaoPJiMjkenP3PP/bBSgrF/HjfBTERYrRHwOOgk5/KH6YnRZ8WECpT/K YpgpGGQCcmsOrVmnBFw571Q4rSKPy6g0WK33dpTpLWzpAwDFOPBsVvgSDrdAo6lnYZZf gPsxiEW68I3CNFqZ3ABhd98mr32WkmeV09OMFU1AwBRRr0hBSX67fQidD+8zFlmGq9Dl 5tLj6X1mP3tNsgj4kqEaCD/MLlmqgNqJ4ANUZloLVRPmZ2EFN5XCbudQu8YwhehBQYfp NgEg== 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=GKMLRqEaGMi/2Mej7Bv8/NGqGmoUYZ4V+5iy4rtT/G4=; fh=ZLVo7h1WCYX+mCQET83n+8bPmyNFw4pMQxFlbH8xyms=; b=tw52tsUE7atqH1DsYS0VKidNCnE5G68Hpmj5heWzZApBeGpuM9V8w4QV9l3KNrV1pT +k6/Sswd/G5c9XF9AuGVAzejTyhPllXWMd9YiFfKUke6D+ZeQEgy/SgoxVRzp5YhU1VY CimOXVs3MFIUXymZ+8cwy4wrH7UtL9MJtJAnUxIYjz7/nTMLzu8vJ02yC7Qi4423k6wz 6YxeOlxHtNWPnq8yjXrN0bf14uuKtmJ6AqCZJ4/BEoyJ57qo1gHPexJUXfs16f9IPvnT QYKEkFxgKuHvc9TvwYjA4iqcwN15UEpNL4ubsDP87L8IR5erEN46WiDMLZrWACqzK8X2 9V2A==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IrMRwCPo; 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 af79cd13be357-798ac087abasi914256385a.712.2024.06.16.23.30.47 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 16 Jun 2024 23:30:47 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IrMRwCPo; 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 1sJ5s5-0003wv-AN; Mon, 17 Jun 2024 02:29:41 -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 1sJ5s2-0003pC-3R for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:38 -0400 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sJ5rz-0003U7-UB for qemu-devel@nongnu.org; Mon, 17 Jun 2024 02:29:37 -0400 Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-2c2e31d319eso3276516a91.1 for ; Sun, 16 Jun 2024 23:29:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718605774; x=1719210574; 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=GKMLRqEaGMi/2Mej7Bv8/NGqGmoUYZ4V+5iy4rtT/G4=; b=IrMRwCPoq5aIbLUuHg6WhPaaiB8vZr6mGEp/J0ZSicbGVC8RbfZ6Cx80MtTph1x/uu 7gwIiJBbX5MCnqeE0tIHtLmM64B4yrtrmRuPX7EOFs4N0UL/U7z38E3ohGF4q5/1ugqn YdrkmCGQPjycJCQwi+wkDIZO9ztb7dkE52Pp3hNRFHubjtjRwNbr++QwLyKHCbOfhE5n pYFoCWGVMMPTk4Ta0LSfd60knDUBvcGGkiQXHutjRS81kND7uWTceRtbVLb7DQJg8MMp Bwerj4Kqn58jd4hkQNbeTVk9D35MjW3JGk36IUe8ElXPQ1H8klmb42AYVt9S5fKC9uPZ rLpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718605774; x=1719210574; 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=GKMLRqEaGMi/2Mej7Bv8/NGqGmoUYZ4V+5iy4rtT/G4=; b=SVSYc3TA/2FTINZagFYVC2QpXSnIFJRWSO2knvSVG40SRQM7Y7gCg8sW8jYx56cRUx 3O/viIfcyyjvL1cYDEhcPeLwWwJqbJDqaI7vxIrUYg/vnKyCRJneSRUXboGVSqbbcIgM I6QBTmvGwC2EjViSg5nEfvn8kjmIOuuvKcR1985JpGMLR2MFcwOAdjVjPycwtGdeTZz+ 4RqWmLmpAz7IGFp9sJCpqPOXUeuJXQVRx2Ar1buQdURL7oIJ2NDIN0ND/l0dXMlihjPW FkDvd93KGnLIX/Sz2iXFYpUzpdsx6dqzNVIwjZhh7h/j2irYXI/isU60y9vyNz4+sR7o MKmA== X-Gm-Message-State: AOJu0YyPO3wTCyQfP2gbSU1IrGMvbRDbSZj1bA67rZvBI7Zmdi0m62af +kMYBAzDsS5ZmrYo341N9WX0D2bAiOLTiHXsuFm950AdcK7VeQBwiT+EL5namV6Yh0K+tR0m2I0 Z X-Received: by 2002:a17:90a:6b07:b0:2c2:fd13:b4ff with SMTP id 98e67ed59e1d1-2c4dbb43dfemr7415966a91.39.1718605774265; Sun, 16 Jun 2024 23:29:34 -0700 (PDT) Received: from amd.. ([2804:7f0:b400:8dcb:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c4a75ed69csm10641863a91.14.2024.06.16.23.29.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 23:29:33 -0700 (PDT) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org, peter.maydell@linaro.org, alex.bennee@linaro.org, richard.henderson@linaro.org Cc: gustavo.romero@linaro.org Subject: [PATCH v3 9/9] tests/tcg/aarch64: Add MTE gdbstub tests Date: Mon, 17 Jun 2024 06:28:49 +0000 Message-Id: <20240617062849.3531745-10-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240617062849.3531745-1-gustavo.romero@linaro.org> References: <20240617062849.3531745-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102d; envelope-from=gustavo.romero@linaro.org; helo=mail-pj1-x102d.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, T_SCC_BODY_TEXT_LINE=-0.01 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 Add tests to exercise the MTE stubs. Signed-off-by: Gustavo Romero --- tests/tcg/aarch64/Makefile.target | 11 ++- tests/tcg/aarch64/gdbstub/test-mte.py | 86 +++++++++++++++++++++++ tests/tcg/aarch64/mte-8.c | 98 +++++++++++++++++++++++++++ 3 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/aarch64/gdbstub/test-mte.py create mode 100644 tests/tcg/aarch64/mte-8.c diff --git a/tests/tcg/aarch64/Makefile.target b/tests/tcg/aarch64/Makefile.target index 70d728ae9a..2b3fd636be 100644 --- a/tests/tcg/aarch64/Makefile.target +++ b/tests/tcg/aarch64/Makefile.target @@ -62,7 +62,7 @@ AARCH64_TESTS += bti-2 # MTE Tests ifneq ($(CROSS_CC_HAS_ARMV8_MTE),) -AARCH64_TESTS += mte-1 mte-2 mte-3 mte-4 mte-5 mte-6 mte-7 +AARCH64_TESTS += mte-1 mte-2 mte-3 mte-4 mte-5 mte-6 mte-7 mte-8 mte-%: CFLAGS += -march=armv8.5-a+memtag endif @@ -127,7 +127,14 @@ run-gdbstub-sve-ioctls: sve-ioctls --bin $< --test $(AARCH64_SRC)/gdbstub/test-sve-ioctl.py, \ basic gdbstub SVE ZLEN support) -EXTRA_RUNS += run-gdbstub-sysregs run-gdbstub-sve-ioctls +run-gdbstub-mte: mte-8 + $(call run-test, $@, $(GDB_SCRIPT) \ + --gdb $(GDB) \ + --qemu $(QEMU) --qargs "$(QEMU_OPTS)" \ + --bin $< --test $(AARCH64_SRC)/gdbstub/test-mte.py, \ + gdbstub MTE support) + +EXTRA_RUNS += run-gdbstub-sysregs run-gdbstub-sve-ioctls run-gdbstub-mte endif endif diff --git a/tests/tcg/aarch64/gdbstub/test-mte.py b/tests/tcg/aarch64/gdbstub/test-mte.py new file mode 100644 index 0000000000..2db0663c1a --- /dev/null +++ b/tests/tcg/aarch64/gdbstub/test-mte.py @@ -0,0 +1,86 @@ +from __future__ import print_function +# +# Test GDB memory-tag commands that exercise the stubs for the qIsAddressTagged, +# qMemTag, and QMemTag packets. Logical tag-only commands rely on local +# operations, hence don't exercise any stub. +# +# The test consists in breaking just after a atag() call (which sets the +# allocation tag -- see mte-8.c for details) and setting/getting tags in +# different memory locations and ranges starting at the address of the array +# 'a'. +# +# This is launched via tests/guest-debug/run-test.py +# + + +import gdb +import re +from test_gdbstub import main, report + + +PATTERN_0 = "Memory tags for address 0x[0-9a-f]+ match \(0x[0-9a-f]+\)." +PATTERN_1 = ".*(0x[0-9a-f]+)" + + +def run_test(): + gdb.execute("break 95", False, True) + gdb.execute("continue", False, True) + try: + # Test if we can check correctly that the allocation tag for + # array 'a' matches the logical tag after atag() is called. + co = gdb.execute("memory-tag check a", False, True) + tags_match = re.findall(PATTERN_0, co, re.MULTILINE) + if tags_match: + report(True, f"{tags_match[0]}") + else: + report(False, "Logical and allocation tags don't match!") + + # Test allocation tag 'set and print' commands. Commands on logical + # tags rely on local operation and so don't exercise any stub. + + # Set the allocation tag for the first granule (16 bytes) of + # address starting at 'a' address to a known value, i.e. 0x04. + gdb.execute("memory-tag set-allocation-tag a 1 04", False, True) + + # Then set the allocation tag for the second granule to a known + # value, i.e. 0x06. This tests that contiguous tag granules are + # set correct and don't run over each other. + gdb.execute("memory-tag set-allocation-tag a+16 1 06", False, True) + + # Read the known values back and check if they remain the same. + + co = gdb.execute("memory-tag print-allocation-tag a", False, True) + first_tag = re.match(PATTERN_1, co)[1] + + co = gdb.execute("memory-tag print-allocation-tag a+16", False, True) + second_tag = re.match(PATTERN_1, co)[1] + + if first_tag == "0x4" and second_tag == "0x6": + report(True, "Allocation tags are correctly set/printed.") + else: + report(False, "Can't set/print allocation tags!") + + # Now test fill pattern by setting a whole page with a pattern. + gdb.execute("memory-tag set-allocation-tag a 4096 0a0b", False, True) + + # And read back the tags of the last two granules in page so + # we also test if the pattern is set correctly up to the end of + # the page. + co = gdb.execute("memory-tag print-allocation-tag a+4096-32", False, True) + tag = re.match(PATTERN_1, co)[1] + + co = gdb.execute("memory-tag print-allocation-tag a+4096-16", False, True) + last_tag = re.match(PATTERN_1, co)[1] + + if tag == "0xa" and last_tag == "0xb": + report(True, "Fill pattern is ok.") + else: + report(False, "Fill pattern failed!") + + except gdb.error: + # This usually happens because a GDB version that does not + # support memory tagging was used to run the test. + report(False, "'memory-tag' command failed!") + + +main(run_test, expected_arch="aarch64") diff --git a/tests/tcg/aarch64/mte-8.c b/tests/tcg/aarch64/mte-8.c new file mode 100644 index 0000000000..9fffd7b737 --- /dev/null +++ b/tests/tcg/aarch64/mte-8.c @@ -0,0 +1,98 @@ +/* + * To be compiled with -march=armv8.5-a+memtag + * + * This test is adapted from a Linux test. Please see: + * + * https://www.kernel.org/doc/html/next/arch/arm64/memory-tagging-extension.html#example-of-correct-usage + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +/* + * From arch/arm64/include/uapi/asm/hwcap.h + */ +#define HWCAP2_MTE (1 << 18) + +/* + * From arch/arm64/include/uapi/asm/mman.h + */ +#define PROT_MTE 0x20 + +/* + * Insert a random logical tag into the given pointer. + */ +#define insert_random_tag(ptr) ({ \ + uint64_t __val; \ + asm("irg %0, %1" : "=r" (__val) : "r" (ptr)); \ + __val; \ +}) + +/* + * Set the allocation tag on the destination address. + */ +#define set_tag(tagged_addr) do { \ + asm volatile("stg %0, [%0]" : : "r" (tagged_addr) : "memory"); \ +} while (0) + + +int main(int argc, char *argv[]) +{ + unsigned char *a; + unsigned long page_sz = sysconf(_SC_PAGESIZE); + unsigned long hwcap2 = getauxval(AT_HWCAP2); + + /* check if MTE is present */ + if (!(hwcap2 & HWCAP2_MTE)) + return EXIT_FAILURE; + + /* + * Enable the tagged address ABI, synchronous or asynchronous MTE + * tag check faults (based on per-CPU preference) and allow all + * non-zero tags in the randomly generated set. + */ + if (prctl(PR_SET_TAGGED_ADDR_CTRL, + PR_TAGGED_ADDR_ENABLE | PR_MTE_TCF_SYNC | PR_MTE_TCF_ASYNC | + (0xfffe << PR_MTE_TAG_SHIFT), + 0, 0, 0)) { + perror("prctl() failed"); + return EXIT_FAILURE; + } + + a = mmap(0, page_sz, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (a == MAP_FAILED) { + perror("mmap() failed"); + return EXIT_FAILURE; + } + + printf("a[] address is %p\n", a); + + /* + * Enable MTE on the above anonymous mmap. The flag could be passed + * directly to mmap() and skip this step. + */ + if (mprotect(a, page_sz, PROT_READ | PROT_WRITE | PROT_MTE)) { + perror("mprotect() failed"); + return EXIT_FAILURE; + } + + /* access with the default tag (0) */ + a[0] = 1; + a[1] = 2; + + printf("a[0] = %hhu a[1] = %hhu\n", a[0], a[1]); + + /* set the logical and allocation tags */ + a = (unsigned char *)insert_random_tag(a); + set_tag(a); + + printf("%p\n", a); + + return 0; +}