From nobody Wed Nov 27 02:34:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1701983159467653.398715418034; Thu, 7 Dec 2023 13:05:59 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rBLY2-0002kt-UQ; Thu, 07 Dec 2023 16:04:43 -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 1rBLXs-0002go-TO for qemu-devel@nongnu.org; Thu, 07 Dec 2023 16:04:33 -0500 Received: from smtp1.lauterbach.com ([62.154.241.196]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rBLXq-0005o2-IQ for qemu-devel@nongnu.org; Thu, 07 Dec 2023 16:04:32 -0500 Received: (qmail 10351 invoked by uid 484); 7 Dec 2023 21:04:11 -0000 Received: from nedpc1.intern.lauterbach.com (Authenticated_SSL:neder@[10.2.11.92]) (envelope-sender ) by smtp1.lauterbach.com (qmail-ldap-1.03) with TLS_AES_256_GCM_SHA384 encrypted SMTP for ; 7 Dec 2023 21:04:09 -0000 X-Qmail-Scanner-Diagnostics: from nedpc1.intern.lauterbach.com by smtp1.lauterbach.com (envelope-from , uid 484) with qmail-scanner-2.11 (mhr: 1.0. clamdscan: 0.99/21437. spamassassin: 3.4.0. Clear:RC:1(10.2.11.92):. Processed in 3e-06 secs); 07 Dec 2023 21:04:11 -0000 From: Nicolas Eder To: qemu-devel@nongnu.org Cc: "Nicolas Eder" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , "Christian Boenig" , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v4 07/17] mcdstub: mcdserver initialization functions added Date: Thu, 7 Dec 2023 22:03:48 +0100 Message-Id: <20231207210358.7409-8-nicolas.eder@lauterbach.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231207210358.7409-1-nicolas.eder@lauterbach.com> References: <20231207210358.7409-1-nicolas.eder@lauterbach.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Qmail-Scanner-2.11: added fake Content-Type header Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=62.154.241.196; envelope-from=nicolas.eder@lauterbach.com; helo=smtp1.lauterbach.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1701983161139100003 Content-Type: text/plain; charset="utf-8" --- debug/mcdstub/mcdstub.c | 154 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/debug/mcdstub/mcdstub.c b/debug/mcdstub/mcdstub.c index 32f4d1980f..176d5d2311 100644 --- a/debug/mcdstub/mcdstub.c +++ b/debug/mcdstub/mcdstub.c @@ -22,9 +22,12 @@ #include "qemu/cutils.h" #include "qemu/module.h" #include "qemu/error-report.h" +#include "qemu/debug.h" #include "qapi/error.h" #include "chardev/char.h" #include "chardev/char-fe.h" +#include "hw/cpu/cluster.h" +#include "hw/boards.h" #include "sysemu/cpus.h" #include "sysemu/hw_accel.h" #include "sysemu/runstate.h" @@ -109,6 +112,39 @@ static void mcd_chr_event(void *opaque, QEMUChrEvent e= vent) { } =20 +/** + * init_query_cmds_table() - Initializes all query functions. + * + * This function adds all query functions to the mcd_query_cmds_table. This + * includes their command string, handler function and parameter schema. + * @mcd_query_cmds_table: Lookup table with all query commands. + */ +static void init_query_cmds_table(MCDCmdParseEntry *mcd_query_cmds_table) +{} + +/** + * mcd_set_stop_cpu() - Sets c_cpu to the just stopped CPU. + * + * @cpu: The CPU state. + */ +static void mcd_set_stop_cpu(CPUState *cpu) +{ + mcdserver_state.c_cpu =3D cpu; +} + +/** + * mcd_init_debug_class() - initialize mcd-specific DebugClass + */ +static void mcd_init_debug_class(void){ + Object *obj; + obj =3D object_new(TYPE_DEBUG); + DebugState *ds =3D DEBUG(obj); + DebugClass *dc =3D DEBUG_GET_CLASS(ds); + dc->set_stop_cpu =3D mcd_set_stop_cpu; + MachineState *ms =3D MACHINE(qdev_get_machine()); + ms->debug_state =3D ds; +} + /** * mcd_init_mcdserver_state() - Initializes the mcdserver_state struct. * @@ -119,6 +155,35 @@ static void mcd_chr_event(void *opaque, QEMUChrEvent e= vent) */ static void mcd_init_mcdserver_state(void) { + g_assert(!mcdserver_state.init); + memset(&mcdserver_state, 0, sizeof(MCDState)); + mcdserver_state.init =3D true; + mcdserver_state.str_buf =3D g_string_new(NULL); + mcdserver_state.mem_buf =3D g_byte_array_sized_new(MAX_PACKET_LENGTH); + mcdserver_state.last_packet =3D g_byte_array_sized_new(MAX_PACKET_LENG= TH + 4); + + /* + * What single-step modes are supported is accelerator dependent. + * By default try to use no IRQs and no timers while single + * stepping so as to make single stepping like a typical ICE HW step. + */ + mcdserver_state.supported_sstep_flags =3D + accel_supported_gdbstub_sstep_flags(); + mcdserver_state.sstep_flags =3D SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOT= IMER; + mcdserver_state.sstep_flags &=3D mcdserver_state.supported_sstep_flags; + + /* init query table */ + init_query_cmds_table(mcdserver_state.mcd_query_cmds_table); + + /* at this time the cpu hans't been started! -> set cpu_state */ + mcd_cpu_state_st cpu_state =3D { + .state =3D CORE_STATE_HALTED, + .info_str =3D STATE_STR_INIT_HALTED, + }; + mcdserver_state.cpu_state =3D cpu_state; + + /* create new debug object */ + mcd_init_debug_class(); } =20 /** @@ -128,6 +193,84 @@ static void mcd_init_mcdserver_state(void) */ static void reset_mcdserver_state(void) { + g_free(mcdserver_state.processes); + mcdserver_state.processes =3D NULL; + mcdserver_state.process_num =3D 0; +} + +/** + * mcd_create_default_process() - Creates a default process for debugging. + * + * This function creates a new, not yet attached, process with an ID one a= bove + * the previous maximum ID. + * @s: A MCDState object. + */ +static void mcd_create_default_process(MCDState *s) +{ + MCDProcess *process; + int max_pid =3D 0; + + if (mcdserver_state.process_num) { + max_pid =3D s->processes[s->process_num - 1].pid; + } + + s->processes =3D g_renew(MCDProcess, s->processes, ++s->process_num); + process =3D &s->processes[s->process_num - 1]; + + /* We need an available PID slot for this process */ + assert(max_pid < UINT32_MAX); + + process->pid =3D max_pid + 1; + process->attached =3D false; +} + +/** + * find_cpu_clusters() - Returns the CPU cluster of the child object. + * + * @param[in] child Object with unknown CPU cluster. + * @param[in] opaque Pointer to an MCDState object. + */ +static int find_cpu_clusters(Object *child, void *opaque) +{ + if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) { + MCDState *s =3D (MCDState *) opaque; + CPUClusterState *cluster =3D CPU_CLUSTER(child); + MCDProcess *process; + + s->processes =3D g_renew(MCDProcess, s->processes, ++s->process_nu= m); + + process =3D &s->processes[s->process_num - 1]; + assert(cluster->cluster_id !=3D UINT32_MAX); + process->pid =3D cluster->cluster_id + 1; + process->attached =3D false; + + return 0; + } + + return object_child_foreach(child, find_cpu_clusters, opaque); +} + +/** + * pid_order() - Compares process IDs. + * + * This function returns -1 if process "a" has a ower process ID than "b". + * If "b" has a lower ID than "a" 1 is returned and if they are qual 0 is + * returned. + * @a: Process a. + * @b: Process b. + */ +static int pid_order(const void *a, const void *b) +{ + MCDProcess *pa =3D (MCDProcess *) a; + MCDProcess *pb =3D (MCDProcess *) b; + + if (pa->pid < pb->pid) { + return -1; + } else if (pa->pid > pb->pid) { + return 1; + } else { + return 0; + } } =20 /** @@ -141,6 +284,17 @@ static void reset_mcdserver_state(void) */ static void create_processes(MCDState *s) { + object_child_foreach(object_get_root(), find_cpu_clusters, s); + + if (mcdserver_state.processes) { + /* Sort by PID */ + qsort(mcdserver_state.processes, + mcdserver_state.process_num, + sizeof(mcdserver_state.processes[0]), + pid_order); + } + + mcd_create_default_process(s); } =20 int mcdserver_start(const char *device) --=20 2.34.1