[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v4 16/17] mcdstub: register access added
From: |
Nicolas Eder |
Subject: |
[PATCH v4 16/17] mcdstub: register access added |
Date: |
Thu, 7 Dec 2023 22:03:57 +0100 |
---
debug/mcdstub/arm_mcdstub.c | 28 +++
debug/mcdstub/mcdstub.c | 369 ++++++++++++++++++++++++++++++++++
include/mcdstub/arm_mcdstub.h | 15 ++
3 files changed, 412 insertions(+)
diff --git a/debug/mcdstub/arm_mcdstub.c b/debug/mcdstub/arm_mcdstub.c
index f14e677480..6d46e1bd7a 100644
--- a/debug/mcdstub/arm_mcdstub.c
+++ b/debug/mcdstub/arm_mcdstub.c
@@ -18,6 +18,7 @@
*/
#include "qemu/osdep.h"
+#include "exec/memory.h"
#include "mcdstub/arm_mcdstub.h"
int arm_mcd_store_mem_spaces(CPUState *cpu, GArray *memspaces)
@@ -259,3 +260,30 @@ uint16_t arm_mcd_get_opcode(CPUState *cs, uint32_t n)
/* TODO: not working with current build structure */
return 0;
}
+
+AddressSpace *arm_mcd_get_address_space(uint32_t cpu_id,
+ mcd_mem_space_st mem_space)
+{
+ /* get correct address space name */
+ char as_name[ARGUMENT_STRING_LENGTH] = {0};
+ if (mem_space.is_secure) {
+ sprintf(as_name, "cpu-secure-memory-%u", cpu_id);
+ } else {
+ sprintf(as_name, "cpu-memory-%u", cpu_id);
+ }
+ /* return correct address space */
+ AddressSpace *as = mcd_find_address_space(as_name);
+ return as;
+}
+
+MemTxAttrs arm_mcd_get_memtxattrs(mcd_mem_space_st mem_space)
+{
+ MemTxAttrs attributes = {0};
+ if (mem_space.is_secure) {
+ attributes.secure = 1;
+ attributes.space = 2;
+ } else {
+ attributes.space = 1;
+ }
+ return attributes;
+}
diff --git a/debug/mcdstub/mcdstub.c b/debug/mcdstub/mcdstub.c
index cc1587396d..e5fd575834 100644
--- a/debug/mcdstub/mcdstub.c
+++ b/debug/mcdstub/mcdstub.c
@@ -857,6 +857,326 @@ static void handle_reset(GArray *params, void *user_ctx)
*/
}
+/**
+ * mcd_memtohex() - Converts a byte array into a hex string.
+ *
+ * @mem: Pointer to byte array.
+ * @buf: Pointer to hex string.
+ * @len: Number of bytes.
+ */
+static void mcd_memtohex(GString *buf, const uint8_t *mem, int len)
+{
+ int i, c;
+ for (i = 0; i < len; i++) {
+ c = mem[i];
+ g_string_append_c(buf, nibble_to_hexchar(c >> 4));
+ g_string_append_c(buf, nibble_to_hexchar(c & 0xf));
+ }
+ g_string_append_c(buf, '\0');
+}
+
+/**
+ * mcd_hextomem() - Converts a hex string into a byte array.
+ *
+ * @mem: Pointer to byte array.
+ * @buf: Pointer to hex string.
+ * @len: Number of bytes.
+ */
+static void mcd_hextomem(GByteArray *mem, const char *buf, int len)
+{
+ int i;
+
+ for (i = 0; i < len; i++) {
+ guint8 byte = hexchar_to_nibble(buf[0]) << 4 |
+ hexchar_to_nibble(buf[1]);
+ g_byte_array_append(mem, &byte, 1);
+ buf += 2;
+ }
+}
+
+/**
+ * mcd_read_register() - Reads a registers data and stores it into the buf.
+ *
+ * This function collects the register type and internal ID
+ * (depending on the XML file). Then it calls the architecture specific
+ * read function. For ARM this is :c:func:`arm_mcd_read_register`.
+ * @cpu: CPU to which the register belongs.
+ * @buf: Byte array with register data.
+ * @reg: General ID of the register.
+ */
+static int mcd_read_register(CPUState *cpu, GByteArray *buf, int reg)
+{
+ CPUClass *cc = CPU_GET_CLASS(cpu);
+ CPUArchState *env = cpu_env(cpu);
+ GDBRegisterState *r;
+
+ if (reg < cc->gdb_num_core_regs) {
+ return cc->gdb_read_register(cpu, buf, 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->num_regs) {
+ return r->get_reg(env, buf, reg - r->base_reg);
+ }
+ }
+ return 0;
+}
+
+/**
+ * mcd_write_register() - Writes data from the buf to a register.
+ *
+ * This function collects the register type and internal ID
+ * (depending on the XML file). Then it calls the architecture specific
+ * write function. For ARM this is :c:func:`arm_mcd_write_register`.
+ * @cpu: CPU to which the register belongs.
+ * @mem_buf: Byte array with register data.
+ * @reg: General ID of the register.
+ */
+static int mcd_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
+{
+ CPUClass *cc = CPU_GET_CLASS(cpu);
+ CPUArchState *env = cpu_env(cpu);
+ GDBRegisterState *r;
+
+ if (reg < cc->gdb_num_core_regs) {
+ return cc->gdb_write_register(cpu, mem_buf, 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->num_regs) {
+ return r->set_reg(env, mem_buf, reg - r->base_reg);
+ }
+ }
+ return 0;
+}
+
+/**
+ * handle_read_register() - Handler for reading a register.
+ *
+ * This function calls :c:func:`mcd_read_register` to read a register. The
+ * register data gets stored in the mem_buf byte array. The data then gets
+ * converted into a hex string with :c:func:`mcd_memtohex` and then send.
+ * @params: GArray with all TCP packet parameters.
+ */
+static void handle_read_register(GArray *params, void *user_ctx)
+{
+ uint32_t cpu_id = get_param(params, 0)->cpu_id;
+ uint64_t reg_num = get_param(params, 1)->data_uint64_t;
+ int reg_size;
+
+ CPUState *cpu = mcd_get_cpu(cpu_id);
+ reg_size = mcd_read_register(cpu, mcdserver_state.mem_buf, reg_num);
+ mcd_memtohex(mcdserver_state.str_buf,
+ mcdserver_state.mem_buf->data, reg_size);
+ mcd_put_strbuf();
+}
+
+/**
+ * handle_write_register() - Handler for writing a register.
+ *
+ * This function converts the incoming hex string data into a byte array with
+ * :c:func:`mcd_hextomem`. Then it calls :c:func:`mcd_write_register` to write
+ * to the register.
+ * @params: GArray with all TCP packet parameters.
+ */
+static void handle_write_register(GArray *params, void *user_ctx)
+{
+ uint32_t cpu_id = get_param(params, 0)->cpu_id;
+ uint64_t reg_num = get_param(params, 1)->data_uint64_t;
+ uint32_t reg_size = get_param(params, 2)->data_uint32_t;
+
+ CPUState *cpu = mcd_get_cpu(cpu_id);
+ mcd_hextomem(mcdserver_state.mem_buf,
+ mcdserver_state.str_buf->str, reg_size);
+ if (mcd_write_register(cpu, mcdserver_state.mem_buf->data, reg_num) == 0) {
+ mcd_put_packet(TCP_EXECUTION_ERROR);
+ } else {
+ mcd_put_packet(TCP_EXECUTION_SUCCESS);
+ }
+}
+
+/**
+ * mcd_read_write_physical_memory() - Reades or writes from/to a logical
+ * memory address.
+ * @address_space: Desired QEMU address space (e.g. secure/non-secure)
+ * @attributes: Access attributes
+ * @addr: (physical) memory address
+ * @buf: Buffer for memory data
+ * @len: Length of the memory access
+ * @is_write: True for writing and false for reading
+ */
+static int mcd_read_write_physical_memory(AddressSpace *address_space,
+ MemTxAttrs attributes, hwaddr addr, uint8_t *buf, int len, bool is_write)
+{
+ if (is_write) {
+ return address_space_write_rom(address_space, addr, attributes, buf,
+ len);
+ } else {
+ return address_space_read_full(address_space, addr, attributes, buf,
+ len);
+ }
+}
+
+/**
+ * mcd_read_write_memory() - Reades or writes from/to a logical memory address.
+ * @cpu: CPUState
+ * @address_space: Desired QEMU address space (e.g. secure/non-secure)
+ * @attributes: Access attributes
+ * @addr: (logical) memory address
+ * @buf: Buffer for memory data
+ * @len: Length of the memory access
+ * @is_write: True for writing and false for reading
+ */
+static int mcd_read_write_memory(CPUState *cpu, AddressSpace *address_space,
+ MemTxAttrs attributes, vaddr addr, uint8_t *buf, uint64_t len,
+ bool is_write)
+{
+ /* get physical address */
+ if (cpu_memory_get_physical_address(cpu, &addr, &len) != 0) {
+ return -1;
+ }
+ /* read memory */
+ return mcd_read_write_physical_memory(address_space, attributes, addr, buf,
+ len, is_write);
+}
+
+/**
+ * mcd_get_address_space() - Returnes the correct QEMU address space name
+ * @cpu: CPUState
+ * @cpu_id: Correct CPU ID
+ * @mem_space: Desired mcd specific memory space.
+ */
+static AddressSpace *mcd_get_address_space(CPUState *cpu, uint32_t cpu_id,
+ mcd_mem_space_st mem_space)
+{
+ CPUClass *cc = CPU_GET_CLASS(cpu);
+ const gchar *arch = cc->gdb_arch_name(cpu);
+ if (strcmp(arch, MCDSTUB_ARCH_ARM) == 0) {
+ return arm_mcd_get_address_space(cpu_id, mem_space);
+ } else {
+ g_assert_not_reached();
+ }
+}
+
+/**
+ * mcd_get_memtxattrs() - Returnes the correct QEMU address space access
+ * attributes
+ * @cpu: CPUState
+ * @mem_space: Desired mcd specific memory space.
+ */
+static MemTxAttrs mcd_get_memtxattrs(CPUState *cpu, mcd_mem_space_st mem_space)
+{
+ CPUClass *cc = CPU_GET_CLASS(cpu);
+ const gchar *arch = cc->gdb_arch_name(cpu);
+ if (strcmp(arch, MCDSTUB_ARCH_ARM) == 0) {
+ return arm_mcd_get_memtxattrs(mem_space);
+ } else {
+ g_assert_not_reached();
+ }
+}
+
+/**
+ * handle_read_memory() - Handler for reading memory.
+ *
+ * First, this function checks whether reading a secure memory space is
+ * requested and changes the access mode with :c:func:`arm_mcd_set_scr`.
+ * Then it calls :c:func:`mcd_read_memory` to read memory. The collected
+ * data gets stored in the mem_buf byte array. The data then gets converted
+ * into a hex string with :c:func:`mcd_memtohex` and then send.
+ * @params: GArray with all TCP packet parameters.
+ */
+static void handle_read_memory(GArray *params, void *user_ctx)
+{
+ /* read input parameters */
+ uint32_t cpu_id = get_param(params, 0)->cpu_id;
+ uint32_t mem_space_id = get_param(params, 1)->data_uint32_t;
+ uint64_t mem_address = get_param(params, 2)->data_uint64_t;
+ uint32_t len = get_param(params, 3)->data_uint32_t;
+ /* check which memory space was requested */
+ CPUState *cpu = mcd_get_cpu(cpu_id);
+ GArray *memspaces =
+ g_list_nth_data(mcdserver_state.all_memspaces, cpu_id);
+ mcd_mem_space_st space = g_array_index(memspaces, mcd_mem_space_st,
+ mem_space_id - 1);
+ /* get data in the QEMU address space and access attributes */
+ AddressSpace *address_space = mcd_get_address_space(cpu, cpu_id, space);
+ MemTxAttrs attributes = mcd_get_memtxattrs(cpu, space);
+ /* read memory data */
+ g_byte_array_set_size(mcdserver_state.mem_buf, len);
+ if (space.is_physical) {
+ /* physical memory */
+ if (mcd_read_write_physical_memory(address_space, attributes,
+ mem_address, mcdserver_state.mem_buf->data,
+ mcdserver_state.mem_buf->len, false) != 0) {
+ mcd_put_packet(TCP_EXECUTION_ERROR);
+ return;
+ }
+ } else {
+ /* user space memory */
+ if (mcd_read_write_memory(cpu, address_space, attributes, mem_address,
+ mcdserver_state.mem_buf->data, mcdserver_state.mem_buf->len,
+ false) != 0) {
+ mcd_put_packet(TCP_EXECUTION_ERROR);
+ return;
+ }
+ }
+ /* send data */
+ mcd_memtohex(mcdserver_state.str_buf, mcdserver_state.mem_buf->data,
+ mcdserver_state.mem_buf->len);
+ mcd_put_strbuf();
+}
+
+/**
+ * handle_write_memory() - Handler for writing memory.
+ *
+ * First, this function checks whether reading a secure memory space is
+ * requested and changes the access mode with :c:func:`arm_mcd_set_scr`.
+ * Then it converts the incoming hex string data into a byte array with
+ * :c:func:`mcd_hextomem`. Then it calls :c:func:`mcd_write_memory` to write to
+ * the register.
+ * @params: GArray with all TCP packet parameters.
+ */
+static void handle_write_memory(GArray *params, void *user_ctx)
+{
+ /* read input parameters */
+ uint32_t cpu_id = get_param(params, 0)->cpu_id;
+ uint32_t mem_space_id = get_param(params, 1)->data_uint32_t;
+ uint64_t mem_address = get_param(params, 2)->data_uint64_t;
+ uint32_t len = get_param(params, 3)->data_uint32_t;
+ /* check which memory space was requested */
+ CPUState *cpu = mcd_get_cpu(cpu_id);
+ GArray *memspaces =
+ g_list_nth_data(mcdserver_state.all_memspaces, cpu_id);
+ mcd_mem_space_st space = g_array_index(memspaces, mcd_mem_space_st,
+ mem_space_id - 1);
+ /* get data in the QEMU address space and access attributes */
+ AddressSpace *address_space = mcd_get_address_space(cpu, cpu_id, space);
+ MemTxAttrs attributes = mcd_get_memtxattrs(cpu, space);
+ /* write memory data */
+ mcd_hextomem(mcdserver_state.mem_buf, mcdserver_state.str_buf->str, len);
+ if (space.is_physical) {
+ /* physical memory */
+ if (mcd_read_write_physical_memory(address_space, attributes,
+ mem_address, mcdserver_state.mem_buf->data,
+ mcdserver_state.mem_buf->len, true) != 0) {
+ mcd_put_packet(TCP_EXECUTION_ERROR);
+ return;
+ }
+ } else {
+ /* user space memory */
+ if (mcd_read_write_memory(cpu, address_space, attributes, mem_address,
+ mcdserver_state.mem_buf->data, mcdserver_state.mem_buf->len,
+ true) != 0) {
+ mcd_put_packet(TCP_EXECUTION_ERROR);
+ return;
+ }
+ }
+ /* send acknowledge */
+ mcd_put_packet(TCP_EXECUTION_SUCCESS);
+}
+
/**
* mcd_handle_packet() - Evaluates the type of received packet and chooses the
* correct handler.
@@ -973,6 +1293,55 @@ static int mcd_handle_packet(const char *line_buf)
cmd_parser = &reset_cmd_desc;
}
break;
+ case TCP_CHAR_READ_REGISTER:
+ {
+ static MCDCmdParseEntry read_reg_cmd_desc = {
+ .handler = handle_read_register,
+ };
+ read_reg_cmd_desc.cmd = (char[2]) { TCP_CHAR_READ_REGISTER, '\0' };
+ strcpy(read_reg_cmd_desc.schema,
+ (char[3]) { ARG_SCHEMA_CORENUM, ARG_SCHEMA_UINT64_T, '\0' });
+ cmd_parser = &read_reg_cmd_desc;
+ }
+ break;
+ case TCP_CHAR_WRITE_REGISTER:
+ {
+ static MCDCmdParseEntry write_reg_cmd_desc = {
+ .handler = handle_write_register,
+ };
+ write_reg_cmd_desc.cmd =
+ (char[2]) { TCP_CHAR_WRITE_REGISTER, '\0' };
+ strcpy(write_reg_cmd_desc.schema,
+ (char[5]) { ARG_SCHEMA_CORENUM, ARG_SCHEMA_UINT64_T,
+ ARG_SCHEMA_INT, ARG_SCHEMA_HEXDATA, '\0' });
+ cmd_parser = &write_reg_cmd_desc;
+ }
+ break;
+ case TCP_CHAR_READ_MEMORY:
+ {
+ static MCDCmdParseEntry read_mem_cmd_desc = {
+ .handler = handle_read_memory,
+ };
+ read_mem_cmd_desc.cmd = (char[2]) { TCP_CHAR_READ_MEMORY, '\0' };
+ strcpy(read_mem_cmd_desc.schema,
+ (char[5]) { ARG_SCHEMA_CORENUM, ARG_SCHEMA_INT,
+ ARG_SCHEMA_UINT64_T, ARG_SCHEMA_INT, '\0' });
+ cmd_parser = &read_mem_cmd_desc;
+ }
+ break;
+ case TCP_CHAR_WRITE_MEMORY:
+ {
+ static MCDCmdParseEntry write_mem_cmd_desc = {
+ .handler = handle_write_memory,
+ };
+ write_mem_cmd_desc.cmd = (char[2]) { TCP_CHAR_WRITE_MEMORY, '\0' };
+ strcpy(write_mem_cmd_desc.schema,
+ (char[6]) { ARG_SCHEMA_CORENUM, ARG_SCHEMA_INT,
+ ARG_SCHEMA_UINT64_T, ARG_SCHEMA_INT,
+ ARG_SCHEMA_HEXDATA, '\0' });
+ cmd_parser = &write_mem_cmd_desc;
+ }
+ break;
default:
/* command not supported */
mcd_put_packet("");
diff --git a/include/mcdstub/arm_mcdstub.h b/include/mcdstub/arm_mcdstub.h
index 9961145f07..69f6e2d487 100644
--- a/include/mcdstub/arm_mcdstub.h
+++ b/include/mcdstub/arm_mcdstub.h
@@ -100,4 +100,19 @@ int arm_mcd_get_additional_register_info(GArray
*reggroups, GArray *registers,
*/
uint16_t arm_mcd_get_opcode(CPUState *cs, uint32_t n);
+/**
+ * arm_mcd_get_address_space() - Returnes the correct QEMU address space name
+ * @cpu_id: Correct CPU ID
+ * @mem_space: Desired mcd specific memory space.
+ */
+AddressSpace *arm_mcd_get_address_space(uint32_t cpu_id,
+ mcd_mem_space_st mem_space);
+
+/**
+ * arm_mcd_get_memtxattrs() - Returnes the correct QEMU address space access
+ * attributes
+ * @mem_space: Desired mcd specific memory space.
+ */
+MemTxAttrs arm_mcd_get_memtxattrs(mcd_mem_space_st mem_space);
+
#endif /* ARM_MCDSTUB_H */
--
2.34.1
- [PATCH v4 00/17] first version of mcdstub, Nicolas Eder, 2023/12/07
- [PATCH v4 02/17] gdbstub: hex conversion functions moved to cutils.h, Nicolas Eder, 2023/12/07
- [PATCH v4 01/17] gdbstub, mcdstub: file and build structure adapted to accomodate for the mcdstub, Nicolas Eder, 2023/12/07
- [PATCH v4 03/17] gdbstub: GDBRegisterState moved to gdbstub.h so it can be used outside of the gdbstub, Nicolas Eder, 2023/12/07
- [PATCH v4 04/17] gdbstub: DebugClass added to system mode., Nicolas Eder, 2023/12/07
- [PATCH v4 05/17] mcdstub: memory helper functions added, Nicolas Eder, 2023/12/07
- [PATCH v4 12/17] mcdstub: all core specific queries added, Nicolas Eder, 2023/12/07
- [PATCH v4 06/17] mcdstub: -mcd start option added, mcd specific defines added, Nicolas Eder, 2023/12/07
- [PATCH v4 07/17] mcdstub: mcdserver initialization functions added, Nicolas Eder, 2023/12/07
- [PATCH v4 16/17] mcdstub: register access added,
Nicolas Eder <=
- [PATCH v4 08/17] cutils: qemu_strtou32 function added, Nicolas Eder, 2023/12/07
- [PATCH v4 13/17] mcdstub: go, step and break added, Nicolas Eder, 2023/12/07
- [PATCH v4 14/17] mcdstub: state query added, Nicolas Eder, 2023/12/07
- [PATCH v4 15/17] mcdstub: skeleton for reset handling added, Nicolas Eder, 2023/12/07
- [PATCH v4 11/17] mcdstub: system and core queries added, Nicolas Eder, 2023/12/07
- [PATCH v4 09/17] mcdstub: TCP packet plumbing added, Nicolas Eder, 2023/12/07
- [PATCH v4 10/17] mcdstub: open and close server funcitons added, Nicolas Eder, 2023/12/07
- [PATCH v4 17/17] mcdstub: break/watchpoints added, Nicolas Eder, 2023/12/07