Hexagon: lldb read/write predicate registers p0/p1/p2/p3
PATCH 1/1 Hexagon: lldb read/write predicate registers p0/p1/p2/p3
hexagon-core.xml only exposes register p3_0 which is an alias that aggregates the predicate registers. It is more convenient for users to interact directly with the predicate registers.
Tested with lldb downloaded from this location
https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.4/clang+llvm-18.1.4-x86_64-linux-gnu-ubuntu-18.04.tar.xz
BEFORE:
(lldb) reg read p3_0
p3_0 = 0x00000000
(lldb) reg read p0
error: Invalid register name 'p0'.
(lldb) reg write p1 0xf
error: Register not found for 'p1'.
AFTER:
(lldb) reg read p3_0
p3_0 = 0x00000000
(lldb) reg read p0
p0 = 0x00
(lldb) reg read -s 1
Predicate Registers:
p0 = 0x00
p1 = 0x00
p2 = 0x00
p3 = 0x00(lldb) reg write p1 0xf
(lldb) reg read p3_0
p3_0 = 0x00000f00
(lldb) reg write p3_0 0xff00ff00
(lldb) reg read -s 1
Predicate Registers:
p0 = 0x00
p1 = 0xff
p2 = 0x00
p3 = 0xff--- target/hexagon/gdbstub.c | 19 ++++++++++++++++++- gdb-xml/hexagon-core.xml | 6 +++++- 2 files changed, 23 insertions(+), 2 deletions(-)
target/hexagon/gdbstub.c +18 -1
@@ -1,5 +1,5 @@/* -* Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved. +* Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -36,6 +36,14 @@ int hexagon_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)return gdb_get_regl (mem_buf, env->gpr[n]); } + n -=TOTAL_PER_THREAD_REGS ; + +if (n <NUM_PREGS ) { +return gdb_get_reg8 (mem_buf, env->pred[n]); + } + + n -=NUM_PREGS ; +g_assert_not_reached (); }
@@ -56,6 +64,15 @@ int hexagon_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)return sizeof (target_ulong); } + n -=TOTAL_PER_THREAD_REGS ; + +if (n <NUM_PREGS ) { + env->pred[n] =ldtul_p (mem_buf); +return sizeof (uint8_t ); + } + + n -=NUM_PREGS ; +g_assert_not_reached (); }
gdb-xml/hexagon-core.xml +5 -1
@@ -1,6 +1,6 @@ <?xml version="1.0"?> <!-- - Copyright(c) 2023 Qualcomm Innovation Center, Inc. All Rights Reserved. + Copyright(c) 2023-2024 Qualcomm Innovation Center, Inc. All Rights Reserved. This work is licensed under the terms of the GNU GPL, version 2 or (at your option) any later version. See the COPYING file in the
@@ -80,5 +80,9 @@ <reg name="c29" bitsize="32" offset="244" encoding="uint" format="hex" group="Thread Registers" dwarf_regnum="61"/> <reg name="utimerlo" bitsize="32" offset="248" encoding="uint" format="hex" group="Thread Registers" dwarf_regnum="62"/> <reg name="utimerhi" bitsize="32" offset="252" encoding="uint" format="hex" group="Thread Registers" dwarf_regnum="63"/> + <reg name="p0" bitsize="8" offset="256" encoding="uint" format="hex" group="Predicate Registers" dwarf_regnum="64"/> + <reg name="p1" bitsize="8" offset="257" encoding="uint" format="hex" group="Predicate Registers" dwarf_regnum="65"/> + <reg name="p2" bitsize="8" offset="258" encoding="uint" format="hex" group="Predicate Registers" dwarf_regnum="66"/> + <reg name="p3" bitsize="8" offset="259" encoding="uint" format="hex" group="Predicate Registers" dwarf_regnum="67"/> </feature>
Signature · 1 line
2.34.1
Quoted 6 lines · On Wed, 12 Jun 2024 10:42:39 -0600 Taylor Simpson <[email protected]>…
target/hexagon/gdbstub.c +0
-0
target/hexagon/gdbstub.c int hexagon_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 71
Quoted 8 lines
target/hexagon/gdbstub.c +5
-0@@ -56,6 +64,15 @@ int hexagon_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)return sizeof (target_ulong); } + n -=TOTAL_PER_THREAD_REGS ; + +if (n <NUM_PREGS ) { + env->pred[n] =ldtul_p (mem_buf); +return sizeof (uint8_t );
I wonder, shouldn't this be sizeof(target_ulong) since we wrote a target_ulong?
Quoted 6 lines
target/hexagon/gdbstub.c +4
-0+ } + + n -=NUM_PREGS ; +g_assert_not_reached (); }
Quoted 25 lines
-----Original Message-----
From: Matheus Tavares Bernardino <[email protected]>
Sent: Wednesday, June 12, 2024 12:30 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] Hexagon: lldb read/write predicate registers p0/p1/p2/p3target/hexagon/gdbstub.c int hexagon_gdb_write_register(CPUState *cs,
Quoted 6 lines · On Wed, 12 Jun 2024 10:42:39 -0600 Taylor Simpson <[email protected]>…
target/hexagon/gdbstub.c index renamed from target/hexagon/gdbstub.c +0
-0
502c6987f0..e67e627fc9100644target/hexagon/gdbstub.c +0
-0@@ -56,6 +64,15 @@ int hexagon_gdb_write_register(CPUState *cs,
uint8_t*mem_buf, int n)Quoted 3 lines
target/hexagon/gdbstub.c +0
-0return sizeof (target_ulong); }
Quoted 8 lines
target/hexagon/gdbstub.c int hexagon_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 71
Quoted 5 lines
target/hexagon/gdbstub.c +5
-0+ n -=TOTAL_PER_THREAD_REGS ; + +if (n <NUM_PREGS ) { + env->pred[n] =ldtul_p (mem_buf); +return sizeof (uint8_t );I wonder, shouldn't this be
sizeof(target_ulong)since we wrote atarget_ulong?
Good question.
From the architecture point of view, predicates are 8 bits (Section 2.2.5 of the v73 Hexagon PRM). However, we model them in QEMU as target_ulong because TCG variables must be either 32 bits or 64 bits. There isn't an option for 8 bits. Whenever we write to a predicate, do "and" with 0xff first to ensure there are only 8 bits written (see gen_log_pred_write in target/hexagon/genptr.c).
I did some more digging and here is what I found:
- Since we have bitsize="8" in hexagon-core.xml, lldb will reject any attempt to write something larger.
(lldb) reg write p1 0x1ff
error: Failed to write register 'p1' with value '0x1ff': value 0x1ff is
too large to fit in a 1 byte unsigned integer value
- For the lldb "reg write" command, the return value from hexagon_gdb_write_register isn't used.
- The only place the return value is used is in handle_write_all_regs. This function is called in response to a "G" packet from the debugger. I don't know if/when lldb uses this packet, but it seems like it would count on it being 8 bits since that's what is in hexagon-core.xml.
Ted <[email protected]>, when would lldb generate a "G" packet, and what assumptions will it make about the size of predicate registers?
Thanks,
Taylor
Quoted 69 lines
-----Original Message-----
From: [email protected] <[email protected]>
Sent: Wednesday, June 12, 2024 9:49 PM
To: Matheus Bernardino (QUIC) <[email protected]>
Cc: [email protected]; Brian Cain <[email protected]>; Ted Woodward <[email protected]>; [email protected]; Sid Manning <[email protected]>; Marco Liebel (QUIC) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH] Hexagon: lldb read/write predicate registers p0/p1/p2/p3Quoted 25 lines
-----Original Message-----
From: Matheus Tavares Bernardino <[email protected]>
Sent: Wednesday, June 12, 2024 12:30 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] Hexagon: lldb read/write predicate registers p0/p1/p2/p3target/hexagon/gdbstub.c int hexagon_gdb_write_register(CPUState *cs,
Quoted 7 lines · On Wed, 12 Jun 2024 10:42:39 -0600 Taylor Simpson <[email protected]>…
target/hexagon/gdbstub.c +0
-0index
502c6987f0..e67e627fc9100644target/hexagon/gdbstub.c +0
-0@@ -56,6 +64,15 @@ int hexagon_gdb_write_register(CPUState *cs,
uint8_t*mem_buf, int n)Quoted 3 lines
target/hexagon/gdbstub.c +0
-0return sizeof (target_ulong); }Quoted 8 lines
target/hexagon/gdbstub.c int hexagon_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 71
Quoted 5 lines
target/hexagon/gdbstub.c +5
-0+ n -=TOTAL_PER_THREAD_REGS ; + +if (n <NUM_PREGS ) { + env->pred[n] =ldtul_p (mem_buf); +return sizeof (uint8_t );I wonder, shouldn't this be
sizeof(target_ulong)since we wrote atarget_ulong?Good question.
From the architecture point of view, predicates are 8 bits (Section 2.2.5 of the v73 Hexagon PRM). However, we model them in QEMU as
target_ulongbecause TCG variables must be either 32 bits or 64 bits. There isn't an option for 8 bits. Whenever we write to a predicate, do "and" with0xfffirst to ensure there are only 8 bits written (seegen_log_pred_writeintarget/hexagon/genptr.c).I did some more digging and here is what I found:
- Since we have bitsize="8" in hexagon-core.xml, lldb will reject any attempt to write something larger.
(lldb) reg write p10x1ff
error: Failed to write register 'p1' with value '0x1ff': value0x1ffis too large to
fit in a 1 byte unsigned integer value
- For the lldb "reg write" command, the return value fromhexagon_gdb_write_registerisn't used.
- The only place the return value is used is inhandle_write_all_regs. This function is called in response to a "G" packet from the debugger. I don't know if/when lldb uses this packet, but it seems like it would count on it being 8 bits since that's what is in hexagon-core.xml.
Quoted 2 lines
Ted <[email protected]>, when would lldb generate a "G" packet, and what assumptions will it make about the size of predicate registers?
When you use the expression parser to call a function, lldb will save the current state, set up the function call, set a breakpoint on a return (by changing the lr register and setting a breakpoint on the new address), set the PC to the function address, and resume. After the breakpoint is hit, lldb will restore the saved state.
Since QEMU doesn't support the lldb RSP extension QSaveRegisterState/QRestoreRegisterState, lldb will use G/g packets to save and restore the register state.
lldb doesn't interpret the values from the G/g packets. It just saves and restores them, so I don't think the new predicate definitions will matter for that. You can test this out by changing the predicate registers, then calling a function with the expression parser. Not a varargs function, since the IR interpreter doesn't handle those.
Ted
Quoted 4 lines
Thanks,
Taylor
Quoted 88 lines
-----Original Message-----
From: Ted Woodward <[email protected]>
Sent: Thursday, June 13, 2024 9:03 AM
To: [email protected]; Matheus Bernardino (QUIC) <[email protected]>
Cc: [email protected]; Brian Cain <[email protected]>; [email protected]; Sid Manning <[email protected]>; Marco Liebel (QUIC) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH] Hexagon: lldb read/write predicate registers p0/p1/p2/p3Quoted 6 lines
-----Original Message-----
From: [email protected] <[email protected]>
Sent: Wednesday, June 12, 2024 9:49 PM
To: Matheus Bernardino (QUIC) <[email protected]>
Cc: [email protected]; Brian Cain <[email protected]>; Ted Woodward <[email protected]>; [email protected]; SidManning
Quoted 1 line
<[email protected]>; Marco Liebel (QUIC)
Quoted 10 lines
[email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH] Hexagon: lldb read/write predicate registers p0/p1/p2/p3Quoted 5 lines
-----Original Message-----
From: Matheus Tavares Bernardino <[email protected]>
Sent: Wednesday, June 12, 2024 12:30 PM
To: [email protected]
Cc: [email protected]; [email protected];Quoted 52 lines
Quoted 20 lines
[email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] Hexagon: lldb read/write predicate registers p0/p1/p2/p3target/hexagon/gdbstub.c int hexagon_gdb_write_register(CPUState *cs,
Quoted 7 lines · On Wed, 12 Jun 2024 10:42:39 -0600 Taylor Simpson <[email protected]>…
target/hexagon/gdbstub.c +0
-0index
502c6987f0..e67e627fc9100644target/hexagon/gdbstub.c +0
-0@@ -56,6 +64,15 @@ int hexagon_gdb_write_register(CPUState *cs,
uint8_t*mem_buf, int n)Quoted 3 lines
target/hexagon/gdbstub.c +0
-0return sizeof (target_ulong); }Quoted 8 lines
target/hexagon/gdbstub.c int hexagon_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 71
Quoted 5 lines
target/hexagon/gdbstub.c +5
-0+ n -=TOTAL_PER_THREAD_REGS ; + +if (n <NUM_PREGS ) { + env->pred[n] =ldtul_p (mem_buf); +return sizeof (uint8_t );I wonder, shouldn't this be
sizeof(target_ulong)since we wrote atarget_ulong?Good question.
From the architecture point of view, predicates are 8 bits (Section 2.2.5 of the
v73 Hexagon PRM). However, we model them in QEMU astarget_ulongbecause TCG variables must be either 32 bits or 64 bits. There isn't an option for 8 bits. Whenever we write to a predicate, do "and" with0xfffirst to ensure there are only 8 bits written (seegen_log_pred_writeintarget/hexagon/genptr.c).I did some more digging and here is what I found:
- Since we have bitsize="8" in hexagon-core.xml, lldb will reject any attempt to write something larger.
(lldb) reg write p10x1ff
error: Failed to write register 'p1' with value '0x1ff': value0x1ff
is too large to fit in a 1 byte unsigned integer value
- For the lldb "reg write" command, the return value fromhexagon_gdb_write_registerisn't used.
- The only place the return value is used is inhandle_write_all_regs. This function is called in response to a "G" packet from the debugger. I don't know if/when lldb uses this packet, but it seems like it would count on it being 8 bits since that's what is in hexagon-core.xml.
Quoted 6 lines
Quoted 2 lines
Ted <[email protected]>, when would lldb generate a "G" packet, and what assumptions will it make about the size of predicate registers?
When you use the expression parser to call a function, lldb will save the current state, set up the function call, set a breakpoint on a return (by changing the lr register and setting a breakpoint on the new address), set
the
Quoted 1 line
PC to the function address, and resume. After the breakpoint is hit, lldb
will
Quoted 8 lines
restore the saved state.
Since QEMU doesn't support the lldb RSP extension QSaveRegisterState/QRestoreRegisterState,
lldb will use G/g packets to save and restore the register state.lldb doesn't interpret the values from the G/g packets. It just saves and restores them, so I don't think the new predicate definitions will matter
for
Quoted 1 line
that. You can test this out by changing the predicate registers, then
calling a
Quoted 4 lines
function with the expression parser. Not a varargs function, since the IR interpreter doesn't handle those.
Ted
Thanks Ted! We do indeed execute handle_write_all_regs when we print the result of a function call in lldb.
So, the answer to Metheus' question is "no". We should return sizeof uint8_t. However, we should also mask off the high bits from the value returned from ldtul_p before assigning to the predicate register. This avoids putting bits from subsequent items in the buffer into the register.
env->pred[n] = ldtul_p(mem_buf) & 0xff;
I'll send v2 of the patch with this change shortly.
Taylor
Brian Cain
AppliedQuoted 38 lines · Taylor Simpson
hexagon-core.xml only exposes register
p3_0which is an alias that aggregates the predicate registers. It is more convenient for users to interact directly with the predicate registers.Tested with lldb downloaded from this location
https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.4/clang+llvm-18.1.4-x86_64-linux-gnu-ubuntu-18.04.tar.xzBEFORE:
(lldb) reg readp3_0p3_0 = 0x00000000(lldb) reg read p0
error: Invalid register name 'p0'.
(lldb) reg write p10xf
error: Register not found for 'p1'.AFTER:
(lldb) reg readp3_0p3_0 = 0x00000000 (lldb) reg read p0 p0 = 0x00 (lldb) reg read -s 1 Predicate Registers: p0 = 0x00 p1 = 0x00 p2 = 0x00 p3 = 0x00(lldb) reg write p1
0xf
(lldb) reg readp3_0p3_0 = 0x00000f00 (lldb) reg write p3_0 0xff00ff00 (lldb) reg read -s 1 Predicate Registers: p0 = 0x00 p1 = 0xff p2 = 0x00 p3 = 0xff
Quoted 2 lines
Signed-off-by Taylor Simpson <[email protected]>---
Queued. Thanks!
Quoted 68 lines
target/hexagon/gdbstub.c | 19 ++++++++++++++++++- gdb-xml/hexagon-core.xml | 6 +++++- 2 files changed, 23 insertions(+), 2 deletions(-)target/hexagon/gdbstub.c +18
-1@@ -1,5 +1,5 @@/* -* Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved. +* Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,6 +36,14 @@ int hexagon_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)return gdb_get_regl (mem_buf, env->gpr[n]); } + n -=TOTAL_PER_THREAD_REGS ; + +if (n <NUM_PREGS ) { +return gdb_get_reg8 (mem_buf, env->pred[n]); + } + + n -=NUM_PREGS ; +g_assert_not_reached (); }@@ -56,6 +64,15 @@ int hexagon_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)return sizeof (target_ulong); } + n -=TOTAL_PER_THREAD_REGS ; + +if (n <NUM_PREGS ) { + env->pred[n] =ldtul_p (mem_buf); +return sizeof (uint8_t ); + } + + n -=NUM_PREGS ; +g_assert_not_reached (); }gdb-xml/hexagon-core.xml +5
-1@@ -1,6 +1,6 @@ <?xml version="1.0"?> <!-- - Copyright(c) 2023 Qualcomm Innovation Center, Inc. All Rights Reserved. + Copyright(c) 2023-2024 Qualcomm Innovation Center, Inc. All Rights Reserved. This work is licensed under the terms of the GNU GPL, version 2 or (at your option) any later version. See the COPYING file in the@@ -80,5 +80,9 @@ <reg name="c29" bitsize="32" offset="244" encoding="uint" format="hex" group="Thread Registers" dwarf_regnum="61"/> <reg name="utimerlo" bitsize="32" offset="248" encoding="uint" format="hex" group="Thread Registers" dwarf_regnum="62"/> <reg name="utimerhi" bitsize="32" offset="252" encoding="uint" format="hex" group="Thread Registers" dwarf_regnum="63"/> + <reg name="p0" bitsize="8" offset="256" encoding="uint" format="hex" group="Predicate Registers" dwarf_regnum="64"/> + <reg name="p1" bitsize="8" offset="257" encoding="uint" format="hex" group="Predicate Registers" dwarf_regnum="65"/> + <reg name="p2" bitsize="8" offset="258" encoding="uint" format="hex" group="Predicate Registers" dwarf_regnum="66"/> + <reg name="p3" bitsize="8" offset="259" encoding="uint" format="hex" group="Predicate Registers" dwarf_regnum="67"/> </feature>