From patchwork Fri Feb 23 16:21:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 775142 Delivered-To: patch@linaro.org Received: by 2002:adf:a356:0:b0:33b:4db1:f5b3 with SMTP id d22csp435619wrb; Fri, 23 Feb 2024 08:38:48 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCWOkjkewi95GcO1iqX13xaYC5fX7kvPUUKb9CjoLSzOPOe7CgPA9+zcDBQyMUb4QN/n/jAoDw19YMJ6K1Vg4HHk X-Google-Smtp-Source: AGHT+IEWBWFUDYUhWMKHY2+ecHSQ8fL3Zc4FMMgbevvHIupbUHR6LgzTOrv7RKsCFeToxwVYhMeH X-Received: by 2002:a0c:e38e:0:b0:68f:501c:ea3e with SMTP id a14-20020a0ce38e000000b0068f501cea3emr340388qvl.28.1708706327859; Fri, 23 Feb 2024 08:38:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708706327; cv=none; d=google.com; s=arc-20160816; b=ql0pWDSJfXoNFYcrXcV/mQQYjd07E8YrOfs9zQc6a0HRW/8HiSU1TTPQMhg2vBZ+QF 8kcS0TWWRH+dtY+XqVhdmW/3YTIUv/F4hPjv18GzsKBrsEkow7LcHCLfGpmQ3wWuvHtu 2Q/UFtC5FKqFmoXjABub434XYg7VXr/1KI/jXFOjzy6g4vymSw+IJL+6vrRElfr2pYJ/ joEHF47ogHwoJre9Go6yxkJZEaBN/ybLPFt81yKgI6Rrl6uAySPKvIvTDJgZwgE0IU+c OQTmw6Qd1Nd5XrTUeNVha5uVRdajLKzWbIXb13OoSstAhmNy8VZSzMcUO1T/QP7L3cqw IKYw== 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=hxDVqDXi3LGJSJYkq05RZi8ehgH06PAsi/NzuX8UMrA=; fh=2IXDtwkhE3u9iDHwRQ543d03t9X1toYDqf9u6DZTCZM=; b=hsZaYe+foKu5kerUHbMQs+V23XMqjD27TUzHPIsRcjQlCi2vXogAujPGaH+rXGNVvA uRdMN/s+CenByNvyM2LgA2moKegPDtvigSD0/HYDJi9Pzyv55sKwPYN5DPsM30MsGjfg IOhEzxMooXDTpaXS3VyP3HuGqWDuYJ73nDHOD43iKTurQa578t6TgTn5mA2gIA69yPc4 RxoT36pKoA+62RHzx83FmC5Kce2NJwL9F7hp2DpEgiJnjCAN3DBLczjaYf1DDWS05Vih j7DUFgizDtket1rWKeOJPZVxi2pJBMdZXZ/pStyvlBvLv6yCCSIfp/4HAxeZ6pHgGfsC voiw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=reN9Cpk1; 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 gm11-20020a056214268b00b0068f34e76d4dsi15267315qvb.587.2024.02.23.08.38.47 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 23 Feb 2024 08:38:47 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=reN9Cpk1; 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 1rdYZB-0005BE-It; Fri, 23 Feb 2024 11:38:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rdYJo-0004A3-R8 for qemu-devel@nongnu.org; Fri, 23 Feb 2024 11:22:36 -0500 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rdYJR-00066l-5A for qemu-devel@nongnu.org; Fri, 23 Feb 2024 11:22:36 -0500 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-41297377abeso2934605e9.2 for ; Fri, 23 Feb 2024 08:22:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708705331; x=1709310131; 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=hxDVqDXi3LGJSJYkq05RZi8ehgH06PAsi/NzuX8UMrA=; b=reN9Cpk1BIEZuw72t0dvk9DUSCaeBOJ+BBVDm0zhCeL2gIp1ETRcoRp/AKqFMUNEpn XuwKgEwIAGRe0/iHoANY/ZQRcdIUVZWSGl06MIyqqZ5iSWhcJMuFtWRBx4oWqm89srLC m+Oxfp3lijhJa/4xZZETlWNBbLlnnXJ0naN0gV612uUZCHEzcdgX7XjH4sRAo3U/Pk1w PhDTUAFgtqBd1qekqzG9Vwr0xxrgD9jB6LBYJlfCbC83+1+nL9W9TxvuZFjHhBdcU23t /OnFpGt5jiTGh0ehMTRAncnG1+Ipf5iM/as2Uixk5P/BdG8sd3Pn4JaKHAmbIaW8g/56 Q2rQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708705331; x=1709310131; 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=hxDVqDXi3LGJSJYkq05RZi8ehgH06PAsi/NzuX8UMrA=; b=tl5Um/M4gWh1SJ1rXV89cofUgO8o68D6H361WLKjHNolgxyBpV1/iYwqd8nGJTxa2l hD6MIGjEej56zhm4OLQy4W7XW35HuPcc0oXUfMabPckGIclcomNuNdqG7uMMveO+Rhvz 4jayaTSbSZGOictGULXKOK1sjVhoJlfg1ITWps30Rh+vBIhId0kXmxAnEu5LbBV89tAH O8B0Kcc4l5M9+Xi+UMs8P0lNXv2vCtWJdOaFciAKY4xA5Irks7fc0/iA4SXGR+kVfz/n KDRNYogfqbn6tmMiclcyVBJiYUhdp8YeILQ+NECxNWz87TDCXg6vwOTqVeI0qru6qK2e W8+Q== X-Gm-Message-State: AOJu0YzYn6Qfw71LQ6mbfeVtV7723LOPoV2WiUK8X6F7dXPoZWqyzy5C 7yBKiNYt5y1FILHv+nMq3nXXQ6Xubbt6WWebd1lIoZZ7ExMYpTtX2rRekOTako4= X-Received: by 2002:a05:600c:a3a4:b0:412:78bd:c087 with SMTP id hn36-20020a05600ca3a400b0041278bdc087mr255071wmb.9.1708705331497; Fri, 23 Feb 2024 08:22:11 -0800 (PST) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id s15-20020a05600c45cf00b0041279707ffbsm2869838wmo.15.2024.02.23.08.22.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 08:22:06 -0800 (PST) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id 603335F92C; Fri, 23 Feb 2024 16:22:03 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Song Gao , qemu-s390x@nongnu.org, Liu Zhiwei , Pierrick Bouvier , Thomas Huth , Daniel Henrique Barboza , Bin Meng , Yanan Wang , Laurent Vivier , qemu-ppc@nongnu.org, David Hildenbrand , =?utf-8?q?C=C3=A9dric_Le_Goater?= , Ilya Leoshkevich , Cleber Rosa , Nicholas Piggin , Marcel Apfelbaum , Eduardo Habkost , Richard Henderson , =?utf-8?q?Philippe_Mathie?= =?utf-8?q?u-Daud=C3=A9?= , Alexandre Iooss , Peter Maydell , =?utf-8?q?Alex_Benn=C3=A9e?= , Daniel Henrique Barboza , qemu-arm@nongnu.org, qemu-riscv@nongnu.org, Palmer Dabbelt , Mahmoud Mandour , John Snow , Weiwei Li , Alistair Francis , Yoshinori Sato , Brian Cain , Paolo Bonzini , "Edgar E. Iglesias" , Michael Rolnik , Akihiko Odaki Subject: [PATCH v2 09/27] gdbstub: Simplify XML lookup Date: Fri, 23 Feb 2024 16:21:44 +0000 Message-Id: <20240223162202.1936541-10-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240223162202.1936541-1-alex.bennee@linaro.org> References: <20240223162202.1936541-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::330; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x330.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=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Akihiko Odaki Now we know all instances of GDBFeature that is used in CPU so we can traverse them to find XML. This removes the need for a CPU-specific lookup function for dynamic XMLs. Signed-off-by: Akihiko Odaki Reviewed-by: Alex Bennée Message-Id: <20240103173349.398526-33-alex.bennee@linaro.org> Message-Id: <20231213-gdb-v17-7-777047380591@daynix.com> Signed-off-by: Alex Bennée --- include/exec/gdbstub.h | 6 +++ gdbstub/gdbstub.c | 118 +++++++++++++++++++++-------------------- hw/core/cpu-common.c | 5 +- 3 files changed, 69 insertions(+), 60 deletions(-) diff --git a/include/exec/gdbstub.h b/include/exec/gdbstub.h index bcaab1bc750..82a8afa237f 100644 --- a/include/exec/gdbstub.h +++ b/include/exec/gdbstub.h @@ -27,6 +27,12 @@ typedef struct GDBFeatureBuilder { typedef int (*gdb_get_reg_cb)(CPUState *cpu, GByteArray *buf, int reg); typedef int (*gdb_set_reg_cb)(CPUState *cpu, uint8_t *buf, int reg); +/** + * gdb_init_cpu(): Initialize the CPU for gdbstub. + * @cpu: The CPU to be initialized. + */ +void gdb_init_cpu(CPUState *cpu); + /** * gdb_register_coprocessor() - register a supplemental set of registers * @cpu - the CPU associated with registers diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c index 486ceb52d2e..d573f808d2e 100644 --- a/gdbstub/gdbstub.c +++ b/gdbstub/gdbstub.c @@ -352,6 +352,7 @@ static const char *get_feature_xml(const char *p, const char **newp, { CPUState *cpu = gdb_get_first_cpu_in_process(process); CPUClass *cc = CPU_GET_CLASS(cpu); + GDBRegisterState *r; size_t len; /* @@ -365,7 +366,6 @@ static const char *get_feature_xml(const char *p, const char **newp, /* Is it the main target xml? */ if (strncmp(p, "target.xml", len) == 0) { if (!process->target_xml) { - GDBRegisterState *r; g_autoptr(GPtrArray) xml = g_ptr_array_new_with_free_func(g_free); g_ptr_array_add( @@ -380,18 +380,12 @@ static const char *get_feature_xml(const char *p, const char **newp, g_markup_printf_escaped("%s", cc->gdb_arch_name(cpu))); } - g_ptr_array_add( - xml, - g_markup_printf_escaped("", - cc->gdb_core_xml_file)); - if (cpu->gdb_regs) { - for (guint i = 0; i < cpu->gdb_regs->len; i++) { - r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); - g_ptr_array_add( - xml, - g_markup_printf_escaped("", - r->feature->xmlname)); - } + for (guint i = 0; i < cpu->gdb_regs->len; i++) { + r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); + g_ptr_array_add( + xml, + g_markup_printf_escaped("", + r->feature->xmlname)); } g_ptr_array_add(xml, g_strdup("")); g_ptr_array_add(xml, NULL); @@ -400,20 +394,11 @@ static const char *get_feature_xml(const char *p, const char **newp, } return process->target_xml; } - /* Is it dynamically generated by the target? */ - if (cc->gdb_get_dynamic_xml) { - g_autofree char *xmlname = g_strndup(p, len); - const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname); - if (xml) { - return xml; - } - } - /* Is it one of the encoded gdb-xml/ files? */ - for (int i = 0; gdb_static_features[i].xmlname; i++) { - const char *name = gdb_static_features[i].xmlname; - if ((strncmp(name, p, len) == 0) && - strlen(name) == len) { - return gdb_static_features[i].xml; + /* Is it one of the features? */ + for (guint i = 0; i < cpu->gdb_regs->len; i++) { + r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); + if (strncmp(p, r->feature->xmlname, len) == 0) { + return r->feature->xml; } } @@ -508,12 +493,10 @@ static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) return cc->gdb_read_register(cpu, buf, reg); } - if (cpu->gdb_regs) { - for (guint i = 0; i < cpu->gdb_regs->len; i++) { - r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); - if (r->base_reg <= reg && reg < r->base_reg + r->feature->num_regs) { - return r->get_reg(cpu, buf, reg - r->base_reg); - } + for (guint i = 0; i < cpu->gdb_regs->len; i++) { + r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); + if (r->base_reg <= reg && reg < r->base_reg + r->feature->num_regs) { + return r->get_reg(cpu, buf, reg - r->base_reg); } } return 0; @@ -528,51 +511,70 @@ static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) return cc->gdb_write_register(cpu, mem_buf, reg); } - if (cpu->gdb_regs) { - for (guint i = 0; i < cpu->gdb_regs->len; i++) { - r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); - if (r->base_reg <= reg && reg < r->base_reg + r->feature->num_regs) { - return r->set_reg(cpu, mem_buf, reg - r->base_reg); - } + for (guint i = 0; i < cpu->gdb_regs->len; i++) { + r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); + if (r->base_reg <= reg && reg < r->base_reg + r->feature->num_regs) { + return r->set_reg(cpu, mem_buf, reg - r->base_reg); } } return 0; } +static void gdb_register_feature(CPUState *cpu, int base_reg, + gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg, + const GDBFeature *feature) +{ + GDBRegisterState s = { + .base_reg = base_reg, + .get_reg = get_reg, + .set_reg = set_reg, + .feature = feature + }; + + g_array_append_val(cpu->gdb_regs, s); +} + +void gdb_init_cpu(CPUState *cpu) +{ + CPUClass *cc = CPU_GET_CLASS(cpu); + const GDBFeature *feature; + + cpu->gdb_regs = g_array_new(false, false, sizeof(GDBRegisterState)); + + if (cc->gdb_core_xml_file) { + feature = gdb_find_static_feature(cc->gdb_core_xml_file); + gdb_register_feature(cpu, 0, + cc->gdb_read_register, cc->gdb_write_register, + feature); + } + + cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs; +} + void gdb_register_coprocessor(CPUState *cpu, gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg, const GDBFeature *feature, int g_pos) { GDBRegisterState *s; guint i; + int base_reg = cpu->gdb_num_regs; - if (cpu->gdb_regs) { - for (i = 0; i < cpu->gdb_regs->len; i++) { - /* Check for duplicates. */ - s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); - if (s->feature == feature) { - return; - } + for (i = 0; i < cpu->gdb_regs->len; i++) { + /* Check for duplicates. */ + s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); + if (s->feature == feature) { + return; } - } else { - cpu->gdb_regs = g_array_new(false, false, sizeof(GDBRegisterState)); - i = 0; } - g_array_set_size(cpu->gdb_regs, i + 1); - s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); - s->base_reg = cpu->gdb_num_regs; - s->get_reg = get_reg; - s->set_reg = set_reg; - s->feature = feature; + gdb_register_feature(cpu, base_reg, get_reg, set_reg, feature); /* Add to end of list. */ cpu->gdb_num_regs += feature->num_regs; if (g_pos) { - if (g_pos != s->base_reg) { + if (g_pos != base_reg) { error_report("Error: Bad gdb register numbering for '%s', " - "expected %d got %d", feature->xml, - g_pos, s->base_reg); + "expected %d got %d", feature->xml, g_pos, base_reg); } else { cpu->gdb_num_g_regs = cpu->gdb_num_regs; } diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c index 67db07741d7..fe16d0d9df8 100644 --- a/hw/core/cpu-common.c +++ b/hw/core/cpu-common.c @@ -27,6 +27,7 @@ #include "qemu/main-loop.h" #include "exec/log.h" #include "exec/cpu-common.h" +#include "exec/gdbstub.h" #include "qemu/error-report.h" #include "qemu/qemu-print.h" #include "sysemu/tcg.h" @@ -240,11 +241,10 @@ static void cpu_common_unrealizefn(DeviceState *dev) static void cpu_common_initfn(Object *obj) { CPUState *cpu = CPU(obj); - CPUClass *cc = CPU_GET_CLASS(obj); + gdb_init_cpu(cpu); cpu->cpu_index = UNASSIGNED_CPU_INDEX; cpu->cluster_index = UNASSIGNED_CLUSTER_INDEX; - cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs; /* user-mode doesn't have configurable SMP topology */ /* the default value is changed by qemu_init_vcpu() for system-mode */ cpu->nr_cores = 1; @@ -264,6 +264,7 @@ static void cpu_common_finalize(Object *obj) { CPUState *cpu = CPU(obj); + g_array_free(cpu->gdb_regs, TRUE); qemu_lockcnt_destroy(&cpu->in_ioctl_lock); qemu_mutex_destroy(&cpu->work_mutex); }