Re: [Openocd-development] [PATCH] basic ARM semihosting support
On Thursday 03 December 2009, Nicolas Pitre wrote: Interest is pretty evident when you want to display debugging information from code running on a target without a serial port for example, or during early board bringup when there is simply no code yet to drive the serial port. This is also pretty handy when writing, say, SPI flash programming utilities which are best ran on the target directly but with the ability to provide interactive menus and read (or write) data files directly from the host. Etc. Etc... Those are three good examples; thanks. Related to that SPI example: more efficient writing of multi-stage NAND boot loaders too! Doing page-at-a-time I/O, and ECC, over JTAG is not speedy, and when you need to craft special headers for ROM bootloaders ... ugh! I'll let your email linger in my mailbox for a while, and hope that somebody else turns it into a HOWTO. :) My thought on this is that since this is a new feature, and one that relies on a library that not all compilers provide, we're better off working with a separate HOWTO for a while. - Dave ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
On Thu, 2009-12-03 at 08:23 +0100, Øyvind Harboe wrote: I would like to see this merged. Any objections? Go for it! --Z ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
On Thu, 2009-12-03 at 08:23 +0100, Øyvind Harboe wrote: I would like to see this merged. Any objections? Actually, I did just find two things: 1) The header is missing from the Makefile.am 2) The header is missing a copyright header and #ifdef wrapper --Z ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
I added a missing #include sys/stat.h, but it is not quite ready to commit yet I think. Does it build under Windows? Perhaps it does, I didn't try. I don't think we should try to make this work under anything but POSIX API for now, but the build should not fail. Autotools magic? Some docs to openocd.texi would be great. -- Øyvind Harboe US toll free 1-866-980-3434 / International +47 51 63 25 00 http://www.zylin.com/zy1000.html ARM7 ARM9 ARM11 XScale Cortex JTAG debugger and flash programmer From 5b1b44ad56646fae8c6d41123e9aa709259663c7 Mon Sep 17 00:00:00 2001 From: Nicolas Pitre n...@fluxnic.net Date: Thu, 3 Dec 2009 01:58:05 -0500 Subject: [PATCH] basic ARM semihosting support MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Semihosting enables code running on an ARM target to use the I/O facilities on the host computer. The target application must be linked against a library that forwards operation requests by using the SVC instruction that is trapped at the SWI vector by the debugger. The hosted library version provided with CodeSourcery's Sourcery G++ Lite for ARM EABI is one example. This is currently available for ARM9 processors, but any ARM variant should be able to support this with little additional work. Tested using binaries compiled with Sourcery G++ Lite 2009q1-161 and ARM RVCT 3.0. Signed-off-by: Nicolas Pitre n...@marvell.com Signed-off-by: Øyvind Harboe oyvind.har...@zylin.com --- src/target/Makefile.am |1 + src/target/arm7_9_common.c | 74 src/target/arm_semihosting.c | 381 ++ src/target/arm_semihosting.h |2 + 4 files changed, 458 insertions(+), 0 deletions(-) create mode 100644 src/target/arm_semihosting.c create mode 100644 src/target/arm_semihosting.h diff --git a/src/target/Makefile.am b/src/target/Makefile.am index 6d2b76d..df42413 100644 --- a/src/target/Makefile.am +++ b/src/target/Makefile.am @@ -80,6 +80,7 @@ ARM_DEBUG_SRC = \ arm_jtag.c \ arm_disassembler.c \ arm_simulator.c \ + arm_semihosting.c \ arm_adi_v5.c \ embeddedice.c \ trace.c \ diff --git a/src/target/arm7_9_common.c b/src/target/arm7_9_common.c index b5553cd..e4ae4f9 100644 --- a/src/target/arm7_9_common.c +++ b/src/target/arm7_9_common.c @@ -36,6 +36,7 @@ #include etm.h #include time_support.h #include arm_simulator.h +#include arm_semihosting.h #include algorithm.h #include register.h @@ -867,6 +868,7 @@ int arm7_9_poll(struct target *target) { int retval; struct arm7_9_common *arm7_9 = target_to_arm7_9(target); + struct arm *armv4_5 = arm7_9-armv4_5_common; struct reg *dbg_stat = arm7_9-eice_cache-reg_list[EICE_DBG_STAT]; /* read debug status register */ @@ -915,6 +917,38 @@ int arm7_9_poll(struct target *target) } } + if (semihosting_active + armv4_5-core_mode == ARMV4_5_MODE_SVC + buf_get_u32(armv4_5-core_cache-reg_list[15].value, 0, 32) == 0x08) + { +uint32_t lr = buf_get_u32(ARMV4_5_CORE_REG_MODE(armv4_5-core_cache, ARMV4_5_MODE_SVC, 14).value, 0, 32); +uint32_t spsr = buf_get_u32(armv4_5-spsr-value, 0, 32); +int semihosting_ok; + +/* check instruction that triggered this trap */ +if (spsr (1 5)) { + /* was in Thumb mode */ + uint8_t insn_buf[2]; + uint16_t insn; + retval = target_read_memory(target, lr-2, 2, 1, insn_buf); + if (retval != ERROR_OK) + return retval; + insn = target_buffer_get_u16(target, insn_buf); + semihosting_ok = (insn == 0xdfab); +} else { + /* was in ARM mode */ + uint8_t insn_buf[4]; + uint32_t insn; + retval = target_read_memory(target, lr-4, 4, 1, insn_buf); + if (retval != ERROR_OK) + return retval; + insn = target_buffer_get_u32(target, insn_buf); + semihosting_ok = (insn == 0xef123456); +} +if (semihosting_ok) + return do_arm_semihosting(target); + } + if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK) { return retval; @@ -2814,6 +2848,39 @@ COMMAND_HANDLER(handle_arm7_9_dcc_downloads_command) return ERROR_OK; } +COMMAND_HANDLER(handle_arm7_9_semihosting_command) +{ + struct target *target = get_current_target(CMD_CTX); + struct arm7_9_common *arm7_9 = target_to_arm7_9(target); + + if (!is_arm7_9(arm7_9)) + { + command_print(CMD_CTX, current target isn't an ARM7/ARM9 target); + return ERROR_TARGET_INVALID; + } + + if (CMD_ARGC 0) + { + COMMAND_PARSE_ENABLE(CMD_ARGV[0], semihosting_active); + + /* TODO: support other methods if vector catch is unavailable */ + if (arm7_9-has_vector_catch) { + struct reg *vector_catch = arm7_9-eice_cache-reg_list[EICE_VEC_CATCH]; + if (!vector_catch-valid) +embeddedice_read_reg(vector_catch); + buf_set_u32(vector_catch-value, 2, 1, semihosting_active); + embeddedice_store_reg(vector_catch); + } else if (semihosting_active) { + command_print(CMD_CTX, vector catch unavailable); +
Re: [Openocd-development] [PATCH] basic ARM semihosting support
On Thu, 2009-12-03 at 10:39 +0100, Øyvind Harboe wrote: I added a missing #include sys/stat.h, but it is not quite ready to commit yet I think. Does it build under Windows? Perhaps it does, I didn't try. I don't think we should try to make this work under anything but POSIX API for now, but the build should not fail. Autotools magic? Some docs to openocd.texi would be great. Okay, one more nit to pick from me too: use a static function to encapsulate the hunk of new code that's being added 4-tabs to the wind. Please, I beg you :) I guess I got excited to consider it, then thought I might actually want to look at the patch after that initial enthusiasm started to pass. --Z ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
Hello Nicolas, Thursday, December 3, 2009, 7:58:05 AM, you wrote: NP Semihosting enables code running on an ARM target to use the I/O NP facilities on the host computer. The target application must be linked NP against a library that forwards operation requests by using the SVC NP instruction that is trapped at the SWI vector by the debugger. The hosted NP library version provided with CodeSourcery's Sourcery G++ Lite for ARM EABI NP is one example. NP This is currently available for ARM9 processors, but any ARM variant should NP be able to support this with little additional work. GDB protocol has support for target syscalls: http://sourceware.org/gdb/download/onlinedocs/gdb_37.html#SEC686 Might be worth adding an option to translate semihosting calls into File-I/O requests. I think this should e.g. allow IDEs to print the target output into the Console window. -- WBR, Igormailto:skochin...@mail.ru ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
On Thu, 3 Dec 2009, Zach Welch wrote: On Thu, 2009-12-03 at 10:39 +0100, Øyvind Harboe wrote: I added a missing #include sys/stat.h, but it is not quite ready to commit yet I think. Does it build under Windows? Perhaps it does, I didn't try. I don't think we should try to make this work under anything but POSIX API for now, but the build should not fail. Autotools magic? Some docs to openocd.texi would be great. Okay, one more nit to pick from me too: use a static function to encapsulate the hunk of new code that's being added 4-tabs to the wind. Please, I beg you :) I know... My problem initially is that I had 3 cases: return error right away, return success right away, or fall through the code that follows. This was akward to express with a single return value so I just left the code inline and didn't revisit it. Nicolas ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
On Thu, 3 Dec 2009, Øyvind Harboe wrote: I added a missing #include sys/stat.h, but it is not quite ready to commit yet I think. Does it build under Windows? Perhaps it does, I didn't try. It probably does. We maintain both a Linux and Windows build internally and I didn't get any complaint yet. I don't think we should try to make this work under anything but POSIX API for now, but the build should not fail. Autotools magic? It might even just work, at least under Cygwin. The way to make it more portable is to stick to stdio calls. Problem with that is that fopen() and friends are using FILE* pointers which can't be passed directly (think of 64-bit hosts). So a mapping table would have to be maintained, etc. That might be the way to go anyway for added security, but I was not that motivated, at least for a first shot at it. And this code as is fulfills my need already. Some docs to openocd.texi would be great. It sure would. ;-) Nicolas ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
On Thu, 3 Dec 2009, Igor Skochinsky wrote: Hello Nicolas, Thursday, December 3, 2009, 7:58:05 AM, you wrote: NP Semihosting enables code running on an ARM target to use the I/O NP facilities on the host computer. The target application must be linked NP against a library that forwards operation requests by using the SVC NP instruction that is trapped at the SWI vector by the debugger. The hosted NP library version provided with CodeSourcery's Sourcery G++ Lite for ARM EABI NP is one example. NP This is currently available for ARM9 processors, but any ARM variant should NP be able to support this with little additional work. GDB protocol has support for target syscalls: http://sourceware.org/gdb/download/onlinedocs/gdb_37.html#SEC686 Might be worth adding an option to translate semihosting calls into File-I/O requests. I think this should e.g. allow IDEs to print the target output into the Console window. That would be great indeed. I don't anticipate doing that work myself in a near future though, so please be my guest. ;-) Nicolas ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
I think it makes sense to wrap up this patch and get it pushed. Zach had some suggestions to the C code and a tiny bit of docs and I think we're good to go. -- Øyvind Harboe US toll free 1-866-980-3434 / International +47 51 63 25 00 http://www.zylin.com/zy1000.html ARM7 ARM9 ARM11 XScale Cortex JTAG debugger and flash programmer ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
On Thu, 2009-12-03 at 12:27 -0500, Nicolas Pitre wrote: On Thu, 3 Dec 2009, Zach Welch wrote: On Thu, 2009-12-03 at 10:39 +0100, Øyvind Harboe wrote: I added a missing #include sys/stat.h, but it is not quite ready to commit yet I think. Does it build under Windows? Perhaps it does, I didn't try. I don't think we should try to make this work under anything but POSIX API for now, but the build should not fail. Autotools magic? Some docs to openocd.texi would be great. Okay, one more nit to pick from me too: use a static function to encapsulate the hunk of new code that's being added 4-tabs to the wind. Please, I beg you :) I know... My problem initially is that I had 3 cases: return error right away, return success right away, or fall through the code that follows. This was akward to express with a single return value so I just left the code inline and didn't revisit it. Feel free to submit additional patches to clean up the surrounding code before your existing patch, if that will make the current patch cleaner. The fact that there are already 3 levels of indent in that function indicates such work would only improve the surrounding code. ;) --Z ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
On Thu, 3 Dec 2009, Zach Welch wrote: On Thu, 2009-12-03 at 12:27 -0500, Nicolas Pitre wrote: On Thu, 3 Dec 2009, Zach Welch wrote: On Thu, 2009-12-03 at 10:39 +0100, Øyvind Harboe wrote: I added a missing #include sys/stat.h, but it is not quite ready to commit yet I think. Does it build under Windows? Perhaps it does, I didn't try. I don't think we should try to make this work under anything but POSIX API for now, but the build should not fail. Autotools magic? Some docs to openocd.texi would be great. Okay, one more nit to pick from me too: use a static function to encapsulate the hunk of new code that's being added 4-tabs to the wind. Please, I beg you :) I know... My problem initially is that I had 3 cases: return error right away, return success right away, or fall through the code that follows. This was akward to express with a single return value so I just left the code inline and didn't revisit it. Feel free to submit additional patches to clean up the surrounding code before your existing patch, if that will make the current patch cleaner. The fact that there are already 3 levels of indent in that function indicates such work would only improve the surrounding code. ;) Please don't commit anything yet. I'm coming up with another version Nicolas ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
Cool! On Thursday 03 December 2009, Nicolas Pitre wrote: Some docs to openocd.texi would be great. It sure would. ;-) More like essential. ;) Also, NEWS; and surely a few TODO list updates (like that mapping table, etc). What's the story on how one would *use* this? Must target code be linked with pay-money-for-software from ARM? Or do other options exist? It seems to just use SVC, so that means no particular constraints (as would be the case if it required DCC and precluded using libdcc). Please start us with a brief how to... email, and recap who would be interested (and why). Done well that'd be the seeed of the User's Guide coverage. Code-wise ... I'd rather see an explicit flag stored in the struct arm, rather than depend on nobody setting their own values for the vector catch flag. Instead of that global. (Resolve before merge please.) It'd also make sense to have the semhosting active status appear in armv4_5_arch_state(); but nothing if it's inactive. Maybe just an optional string at the end of the line with CPSR and PC. - Dave ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
On Thu, 3 Dec 2009, David Brownell wrote: Cool! On Thursday 03 December 2009, Nicolas Pitre wrote: Some docs to openocd.texi would be great. It sure would. ;-) More like essential. ;) I provided the minimum in version 2 of the patch. For improved prose I'll defer to those amongst us with better English writing skills. Also, NEWS; and surely a few TODO list updates (like that mapping table, etc). I'll let someone else modify the NEWS. As to the TODO items: they're already commented in the code. So please just commit v2 as is now and let's put incremental improvements on top. I want other people to get involved too! ;-) What's the story on how one would *use* this? Must target code be linked with pay-money-for-software from ARM? Or do other options exist? Did you read my patch commit log? OK let's make it easy (for those who are eager to contribute extra documentation... hint hint). Semihosting crash HOWTO --- 1) Get Sourcery G++ Lite 2009q1-161 for ARM EABI from http://www.codesourcery.com/sgpp/lite/arm/portal/release830 Maybe there is a newer release now which should work just as well. I'm still using the one above so... 2) Install it (how to install is out of the scope for this HOWTO) Documentation on proper installation is provided through the above site. 3) Create test code $ cat foobar.c EOF #include stdio.h #include stdlib.h int main() { printf(\n\nHello World!\n\n\n); exit(0); } EOF 4) Compile it $ arm-none-eabi-gcc -T generic-hosted.ld -o foobar.elf foobar.c For more control on things like heap and stack placement you'll have to read the accompanying documentation (or dig in the provided linker script). 5) Using OpenOCD, load onto target | soft_reset_halt | load_image foobar.elf | arm7_9 semihosting enable | resume 0 Interaction with the remote application then happens on the console where OpenOCD is running. It seems to just use SVC, so that means no particular constraints (as would be the case if it required DCC and precluded using libdcc). Right. And documentation on the semihosting interface is publicly available (see reference in the code). Please start us with a brief how to... email, and recap who would be interested (and why). Done well that'd be the seeed of the User's Guide coverage. Interest is pretty evident when you want to display debugging information from code running on a target without a serial port for example, or during early board bringup when there is simply no code yet to drive the serial port. This is also pretty handy when writing, say, SPI flash programming utilities which are best ran on the target directly but with the ability to provide interactive menus and read (or write) data files directly from the host. Etc. Etc... Code-wise ... I'd rather see an explicit flag stored in the struct arm, rather than depend on nobody setting their own values for the vector catch flag. Instead of that global. (Resolve before merge please.) Not sure that I follow you here. But like I said above: please merge it now and let's improve it later. This is already plenty functional as is and further cleanups should be trivial. It'd also make sense to have the semhosting active status appear in armv4_5_arch_state(); but nothing if it's inactive. Maybe just an optional string at the end of the line with CPSR and PC. Indeed. Same argument apply. Nicolas ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
Re: [Openocd-development] [PATCH] basic ARM semihosting support
On Thursday 03 December 2009, Nicolas Pitre wrote: I'll let someone else modify the NEWS. As to the TODO items: they're already commented in the code. So please just commit v2 as is now and let's put incremental improvements on top. I want other people to get involved too! ;-) OK, I merged v2 with a NEWS update. Let's see what happens... Thanks for this nice feature. This should be the beginning of some interesting contributions. ;) - Dave ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
[Openocd-development] [PATCH] basic ARM semihosting support
Semihosting enables code running on an ARM target to use the I/O facilities on the host computer. The target application must be linked against a library that forwards operation requests by using the SVC instruction that is trapped at the SWI vector by the debugger. The hosted library version provided with CodeSourcery's Sourcery G++ Lite for ARM EABI is one example. This is currently available for ARM9 processors, but any ARM variant should be able to support this with little additional work. Tested using binaries compiled with Sourcery G++ Lite 2009q1-161 and ARM RVCT 3.0. Signed-off-by: Nicolas Pitre n...@marvell.com diff --git a/src/target/Makefile.am b/src/target/Makefile.am index cb473a3..b083a55 100644 --- a/src/target/Makefile.am +++ b/src/target/Makefile.am @@ -80,6 +80,7 @@ ARM_DEBUG_SRC = \ arm_jtag.c \ arm_disassembler.c \ arm_simulator.c \ + arm_semihosting.c \ arm_adi_v5.c \ embeddedice.c \ trace.c \ diff --git a/src/target/arm7_9_common.c b/src/target/arm7_9_common.c index b5553cd..e4ae4f9 100644 --- a/src/target/arm7_9_common.c +++ b/src/target/arm7_9_common.c @@ -36,6 +36,7 @@ #include etm.h #include time_support.h #include arm_simulator.h +#include arm_semihosting.h #include algorithm.h #include register.h @@ -867,6 +868,7 @@ int arm7_9_poll(struct target *target) { int retval; struct arm7_9_common *arm7_9 = target_to_arm7_9(target); + struct arm *armv4_5 = arm7_9-armv4_5_common; struct reg *dbg_stat = arm7_9-eice_cache-reg_list[EICE_DBG_STAT]; /* read debug status register */ @@ -915,6 +917,38 @@ int arm7_9_poll(struct target *target) } } + if (semihosting_active + armv4_5-core_mode == ARMV4_5_MODE_SVC + buf_get_u32(armv4_5-core_cache-reg_list[15].value, 0, 32) == 0x08) + { + uint32_t lr = buf_get_u32(ARMV4_5_CORE_REG_MODE(armv4_5-core_cache, ARMV4_5_MODE_SVC, 14).value, 0, 32); + uint32_t spsr = buf_get_u32(armv4_5-spsr-value, 0, 32); + int semihosting_ok; + + /* check instruction that triggered this trap */ + if (spsr (1 5)) { + /* was in Thumb mode */ + uint8_t insn_buf[2]; + uint16_t insn; + retval = target_read_memory(target, lr-2, 2, 1, insn_buf); + if (retval != ERROR_OK) + return retval; + insn = target_buffer_get_u16(target, insn_buf); + semihosting_ok = (insn == 0xdfab); + } else { + /* was in ARM mode */ + uint8_t insn_buf[4]; + uint32_t insn; + retval = target_read_memory(target, lr-4, 4, 1, insn_buf); + if (retval != ERROR_OK) + return retval; + insn = target_buffer_get_u32(target, insn_buf); + semihosting_ok = (insn == 0xef123456); + } + if (semihosting_ok) + return do_arm_semihosting(target); + } + if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK) { return retval; @@ -2814,6 +2848,39 @@ COMMAND_HANDLER(handle_arm7_9_dcc_downloads_command) return ERROR_OK; } +COMMAND_HANDLER(handle_arm7_9_semihosting_command) +{ + struct target *target = get_current_target(CMD_CTX); + struct arm7_9_common *arm7_9 = target_to_arm7_9(target); + + if (!is_arm7_9(arm7_9)) + { + command_print(CMD_CTX, current target isn't an ARM7/ARM9 target); + return ERROR_TARGET_INVALID; + } + + if (CMD_ARGC 0) + { + COMMAND_PARSE_ENABLE(CMD_ARGV[0], semihosting_active); + + /* TODO: support other methods if vector catch is unavailable */ + if (arm7_9-has_vector_catch) { + struct reg *vector_catch = arm7_9-eice_cache-reg_list[EICE_VEC_CATCH]; + if (!vector_catch-valid) + embeddedice_read_reg(vector_catch); + buf_set_u32(vector_catch-value, 2, 1, semihosting_active); +
Re: [Openocd-development] [PATCH] basic ARM semihosting support
I would like to see this merged. Any objections? -- Øyvind Harboe US toll free 1-866-980-3434 / International +47 51 63 25 00 http://www.zylin.com/zy1000.html ARM7 ARM9 ARM11 XScale Cortex JTAG debugger and flash programmer ___ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development