semihosting: clean up and add --semihosting-config arg
v5 cover semihosting: clean up and add --semihosting-config arg
Just a rebase of v4 as there were changes in qemu_opt_foreach API.
Regards,
Leon
v5:
* rebase
v4:
* add semihosting_get_cmdline() and update arm-semi.c to support new option
* for backward compatibility use -kernel/-append to initialize semihosting.argv
* update qemu doc to describe the interaction between arg and -kernel/-append
v3:
* improved documentation (rephrased and used @table so that generated doc looks nicer)
v2:
* squash clean-up related patches so renaming is not required (these modifications are relatively simple anyway).
Leon Alrae (2):
semihosting: create SemihostingConfig structure and semihost.h
semihosting: add --semihosting-config arg sub-argument
gdbstub.c | 8 ++-- include/exec/gdbstub.h | 6 --- include/exec/semihost.h | 62 +++++++++++++++++++++++++++ include/sysemu/sysemu.h | 1 - qemu-options.hx | 21 +++++++--- target-arm/arm-semi.c | 10 ++--- target-arm/helper.c | 7 ++-- target-lm32/helper.c | 3 +- target-m68k/op_helper.c | 5 +-- target-xtensa/translate.c | 3 +- vl.c | 104 ++++++++++++++++++++++++++++++++++++++++++---- 11 files changed, 190 insertions(+), 40 deletions(-) create mode 100644 include/exec/semihost.h
PATCH v5 2/2 semihosting: add --semihosting-config arg sub-argument
Add new "arg" sub-argument to the --semihosting-config allowing the user to pass multiple input arguments separately. It is required for example by UHI semihosting to construct argc and argv.
Also, update ARM semihosting to support new option (at the moment it is the only target which cares about arguments).
If the semihosting is enabled and no semihosting args have been specified, then fall back to -kernel/-append. The -append string is split on whitespace before initializing semihosting.argv[1..n]; this is different from what QEMU MIPS machines' pseudo-bootloaders do (i.e. argv[1] contains the whole -append), but is more intuitive from UHI user's point of view and Linux kernel just does not care as it concatenates argv[1..n] into single cmdline string anyway.
--- include/exec/semihost.h | 18 ++++++++++++++ qemu-options.hx | 21 ++++++++++++---- target-arm/arm-semi.c | 10 +++----- vl.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 12 deletions(-)
include/exec/semihost.h +18 -0
@@ -36,9 +36,27 @@ static inline SemihostingTarget semihosting_get_target(void) {return SEMIHOSTING_TARGET_AUTO ; } + +static inline const char *semihosting_get_arg (int i) +{ +return NULL ; +} + +static inline int semihosting_get_argc (void ) +{ +return 0 ; +} + +static inline const char *semihosting_get_cmdline (void ) +{ +return NULL ; +}#else bool semihosting_enabled (void );SemihostingTarget semihosting_get_target (void ); +const char *semihosting_get_arg (int i); +int semihosting_get_argc (void ); +const char *semihosting_get_cmdline (void );#endif #endif
qemu-options.hx +16 -5
@@ -3351,14 +3351,25 @@ STEXI Enable semihosting mode (ARM, M68K, Xtensa only). ETEXI DEF("semihosting-config", HAS_ARG, QEMU_OPTION_semihosting_config, - "-semihosting-config [enable=on|off,]target=native|gdb|auto semihosting configuration\n", + "-semihosting-config [enable=on|off][,target=native|gdb|auto][,arg=str[,...]]\n" \ + " semihosting configuration\n", QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | QEMU_ARCH_LM32) STEXI -@item -semihosting-config [enable=on|off,]target=native|gdb|auto +@item -semihosting-config [enable=on|off][,target=native|gdb|auto][,arg=str[,...]] @findex -semihosting-config -Enable semihosting and define where the semihosting calls will be addressed, -to QEMU (@code{native}) or to GDB (@code{gdb}). The default is @code{auto}, which means -@code{gdb} during debug sessions and @code{native} otherwise (ARM, M68K, Xtensa only). +Enable and configure semihosting (ARM, M68K, Xtensa only). +@table @option +@item target=@code{native|gdb|auto} +Defines where the semihosting calls will be addressed, to QEMU (@code{native}) +or to GDB (@code{gdb}). The default is @code{auto}, which means @code{gdb} +during debug sessions and @code{native} otherwise. +@item arg=@var{str1},arg=@var{str2},... +Allows the user to pass input arguments, and can be used multiple times to build +up a list. The old-style @code{-kernel}/@code{-append} method of passing a +command line is still supported for backward compatibility. If both the +@code{--semihosting-config arg} and the @code{-kernel}/@code{-append} are +specified, the former is passed to semihosting as it always takes precedence. +@end table ETEXI DEF("old-param", 0, QEMU_OPTION_old_param, "-old-param old param mode\n", QEMU_ARCH_ARM)
target-arm/arm-semi.c +3 -7
@@ -27,6 +27,7 @@#include <time.h> #include "cpu.h" +#include "exec/semihost.h" #ifdef CONFIG_USER_ONLY #include "qemu.h"
@@ -440,10 +441,7 @@ uint32_t do_arm_semihosting(CPUARMState *env) input_size = arg1;/* Compute the size of the output string. */ #if !defined(CONFIG_USER_ONLY ) - output_size =strlen (ts->boot_info->kernel_filename) - +1 /* Separating space. */ - +strlen (ts->boot_info->kernel_cmdline) - +1 ;/* Terminating null byte. */ + output_size =strlen (semihosting_get_cmdline ()) +1 ;#else unsigned int i;
@@ -474,9 +472,7 @@ uint32_t do_arm_semihosting(CPUARMState *env)/* Copy the command-line arguments. */ #if !defined(CONFIG_USER_ONLY ) -pstrcpy (output_buffer, output_size, ts->boot_info->kernel_filename); -pstrcat (output_buffer, output_size," " ); -pstrcat (output_buffer, output_size, ts->boot_info->kernel_cmdline); +pstrcpy (output_buffer, output_size,semihosting_get_cmdline ());#else if (output_size ==1 ) {/* Empty command-line. */
vl.c +66 -0
96 lines not on this page · raw message
PATCH v5 1/2 semihosting: create SemihostingConfig structure and semihost.h
In reply to Leon AlraeRemove semihosting_enabled and semihosting_target and replace them with SemihostingConfig structure containing equivalent fields. The structure is defined in vl.c where it is actually set.
Also introduce separate header file include/exec/semihost.h allowing to access semihosting config related stuff from target specific semihosting code.
--- gdbstub.c | 8 ++++---- include/exec/gdbstub.h | 6 ------ include/exec/semihost.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ include/sysemu/sysemu.h | 1 - target-arm/helper.c | 7 ++++--- target-lm32/helper.c | 3 ++- target-m68k/op_helper.c | 5 ++--- target-xtensa/translate.c | 3 ++- vl.c | 38 +++++++++++++++++++++++++++++--------- 9 files changed, 87 insertions(+), 28 deletions(-) create mode 100644 include/exec/semihost.h
gdbstub.c +4 -4
@@ -40,6 +40,7 @@#include "cpu.h" #include "qemu/sockets.h" #include "sysemu/kvm.h" +#include "exec/semihost.h" #ifdef CONFIG_USER_ONLY #define GDB_ATTACHED "0"
@@ -323,8 +324,6 @@ static GDBState *gdbserver_state;bool gdb_has_xml; -int semihosting_target =SEMIHOSTING_TARGET_AUTO ; -#ifdef CONFIG_USER_ONLY /* XXX: This is not thread safe. Do we care? */ static int gdbserver_fd =-1 ;
@@ -362,10 +361,11 @@ static enum {/* Decide if either remote gdb syscalls or native file IO should be used. */ int use_gdb_syscalls (void ) { -if (semihosting_target ==SEMIHOSTING_TARGET_NATIVE ) { +SemihostingTarget target =semihosting_get_target (); +if (target ==SEMIHOSTING_TARGET_NATIVE ) {/* -semihosting-config target=native */ return false; - }else if (semihosting_target ==SEMIHOSTING_TARGET_GDB ) { + }else if (target ==SEMIHOSTING_TARGET_GDB ) {/* -semihosting-config target=gdb */ return true; }
include/exec/gdbstub.h +0 -6
@@ -95,10 +95,4 @@ extern bool gdb_has_xml;/* in gdbstub-xml.c, generated by scripts/feature_to_c.sh */ extern const char *const xml_builtin[][2 ]; -/* Command line option defining whether semihosting should go via gdb or not */ -extern int semihosting_target; -#define SEMIHOSTING_TARGET_AUTO 0 -#define SEMIHOSTING_TARGET_NATIVE 1 -#define SEMIHOSTING_TARGET_GDB 2 -#endif
include/exec/semihost.h new file +44 -0
44 lines not on this page · raw message
include/sysemu/sysemu.h +0 -1
@@ -126,7 +126,6 @@ extern int cursor_hide;extern int graphic_rotate;extern int no_quit;extern int no_shutdown; -extern int semihosting_enabled;extern int old_param;extern int boot_menu;extern bool boot_strict;
target-arm/helper.c +4 -3
@@ -10,6 +10,7 @@#include "exec/cpu_ldst.h" #include "arm_ldst.h" #include <zlib.h> /* For crc32 */ +#include "exec/semihost.h" #ifndef CONFIG_USER_ONLY static inline bool get_phys_addr(CPUARMState *env, target_ulong address,
@@ -4478,7 +4479,7 @@ void arm_v7m_cpu_do_interrupt(CPUState *cs)armv7m_nvic_set_pending (env->nvic,ARMV7M_EXCP_MEM );return ;case EXCP_BKPT : -if (semihosting_enabled) { +if (semihosting_enabled ()) {int nr; nr =arm_lduw_code (env, env->regs[15 ], env->bswap_code) &0xff ;if (nr ==0xab ) {
@@ -4790,7 +4791,7 @@ void arm_cpu_do_interrupt(CPUState *cs) offset =4 ;break ;case EXCP_SWI : -if (semihosting_enabled) { +if (semihosting_enabled ()) {/* Check for semihosting interrupt. */ if (env->thumb) { mask =arm_lduw_code (env, env->regs[15 ] -2 , env->bswap_code)
@@ -4817,7 +4818,7 @@ void arm_cpu_do_interrupt(CPUState *cs)break ;case EXCP_BKPT :/* See if this is a semihosting syscall. */ -if (env->thumb && semihosting_enabled) { +if (env->thumb &&semihosting_enabled ()) { mask =arm_lduw_code (env, env->regs[15 ], env->bswap_code) &0xff ;if (mask ==0xab && (env->uncached_cpsr &CPSR_M ) !=ARM_CPU_MODE_USR ) {
target-lm32/helper.c +2 -1
@@ -20,6 +20,7 @@#include "cpu.h" #include "qemu/host-utils.h" #include "sysemu/sysemu.h" +#include "exec/semihost.h" int lm32_cpu_handle_mmu_fault (CPUState *cs,vaddr address,int rw,int mmu_idx)
@@ -162,7 +163,7 @@ void lm32_cpu_do_interrupt(CPUState *cs)switch (cs->exception_index) {case EXCP_SYSTEMCALL : -if (unlikely (semihosting_enabled)) { +if (unlikely (semihosting_enabled ())) {/* do_semicall() returns true if call was handled. Otherwise * do the normal exception handling. */ if (lm32_cpu_do_semihosting (cs)) {
target-m68k/op_helper.c +2 -3
@@ -19,6 +19,7 @@#include "cpu.h" #include "exec/helper-proto.h" #include "exec/cpu_ldst.h" +#include "exec/semihost.h" #if defined(CONFIG_USER_ONLY )
@@ -33,8 +34,6 @@ static inline void do_interrupt_m68k_hardirq(CPUM68KState *env)#else -extern int semihosting_enabled; -/* Try to fill the TLB and return an exception if error. If retaddr is NULL, it means that the function was called in C code (i.e. not from generated code or from helper.c) */
@@ -85,7 +84,7 @@ static void do_interrupt_all(CPUM68KState *env, int is_hw)do_rte (env);return ;case EXCP_HALT_INSN : -if (semihosting_enabled +if (semihosting_enabled () && (env->sr &SR_S ) !=0 && (env->pc &3 ) ==0 && cpu_lduw_code(env, env->pc - 4) == 0x4e71
target-xtensa/translate.c +2 -1
@@ -37,6 +37,7 @@#include "qemu/log.h" #include "sysemu/sysemu.h" #include "exec/cpu_ldst.h" +#include "exec/semihost.h" #include "exec/helper-proto.h" #include "exec/helper-gen.h"
@@ -1216,7 +1217,7 @@ static void disas_xtensa_insn(CPUXtensaState *env, DisasContext *dc)break ;case 1 :/*SIMCALL*/ -if (semihosting_enabled) { +if (semihosting_enabled ()) {if (gen_check_privilege (dc)) {gen_helper_simcall (cpu_env); }
vl.c +29 -9
@@ -119,6 +119,7 @@ int main(int argc, char **argv)#include "qapi/opts-visitor.h" #include "qom/object_interfaces.h" #include "qapi-event.h" +#include "exec/semihost.h" #define MAX_VIRTIO_CONSOLES 1#define MAX_SCLP_CONSOLES 1
@@ -169,7 +170,6 @@ int graphic_rotate = 0;const char *watchdog;QEMUOptionRom option_rom[MAX_OPTION_ROMS ];int nb_option_roms; -int semihosting_enabled =0 ;int old_param =0 ;const char *qemu_name;int alt_grab =0 ;
@@ -1246,6 +1246,26 @@ static void configure_msg(QemuOpts *opts) }/***********************************************************/ +/* Semihosting */ + +typedef struct SemihostingConfig { +bool enabled; +SemihostingTarget target; +}SemihostingConfig ; + +static SemihostingConfig semihosting; + +bool semihosting_enabled (void ) +{ +return semihosting.enabled; +} + +SemihostingTarget semihosting_get_target (void ) +{ +return semihosting.target; +} + +/***********************************************************/ /* USB devices */ static int usb_device_add (const char *devname)
@@ -3622,24 +3642,24 @@ int main(int argc, char **argv, char **envp) nb_option_roms++;break ;case QEMU_OPTION_semihosting: - semihosting_enabled =1 ; - semihosting_target =SEMIHOSTING_TARGET_AUTO ; + semihosting.enabled = true; + semihosting.target =SEMIHOSTING_TARGET_AUTO ;break ;case QEMU_OPTION_semihosting_config: - semihosting_enabled =1 ; + semihosting.enabled = true; opts =qemu_opts_parse (qemu_find_opts ("semihosting-config" ), optarg,0 );if (opts !=NULL ) { - semihosting_enabled =qemu_opt_get_bool (opts,"enable" , + semihosting.enabled =qemu_opt_get_bool (opts,"enable" , true);const char *target =qemu_opt_get (opts,"target" );if (target !=NULL ) {if (strcmp ("native" , target) ==0 ) { - semihosting_target =SEMIHOSTING_TARGET_NATIVE ; + semihosting.target =SEMIHOSTING_TARGET_NATIVE ; }else if (strcmp ("gdb" , target) ==0 ) { - semihosting_target =SEMIHOSTING_TARGET_GDB ; + semihosting.target =SEMIHOSTING_TARGET_GDB ; }else if (strcmp ("auto" , target) ==0 ) { - semihosting_target =SEMIHOSTING_TARGET_AUTO ; + semihosting.target =SEMIHOSTING_TARGET_AUTO ; }else { fprintf(stderr, "Unsupported semihosting-config" " %s\n",
@@ -3647,7 +3667,7 @@ int main(int argc, char **argv, char **envp)exit (1 ); } }else { - semihosting_target =SEMIHOSTING_TARGET_AUTO ; + semihosting.target =SEMIHOSTING_TARGET_AUTO ; } }else { fprintf(stderr, "Unsupported semihosting-config %s\n",
Quoted 4 lines · Leon Alrae
Just a rebase of v4 as there were changes in
qemu_opt_foreachAPI.Regards,
Leon
Thanks; applied to target-arm.next.
-- PMM
Quoted 10 lines · Peter Maydell
Quoted 4 lines · Leon Alrae
Just a rebase of v4 as there were changes in
qemu_opt_foreachAPI.Regards,
LeonThanks; applied to target-arm.next.
I merged target-arm.next to my branch and I'm not sure the current approach is very correct.
if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
/* fall back to the -kernel/-append */
semihosting_arg_fallback(kernel_filename, kernel_cmdline);
}this code in vl.c automatically falls back to -kernel/-append, which, for Cortex-M might be a problem.
without defining semihosting arguments, an example run for my case would look like:
GNU ARM Eclipse 64-bits QEMU v2.3.50 (qemu-system-gnuarmeclipse).
Board: 'STM32-H103' (Olimex Header Board for STM32F103RBT6).
Device: 'STM32F103RB' (Cortex-M3, MPU), Flash: 128 KB, RAM: 20 KB.
Image: '/Users/ilg/My Files/MacBookPro Projects/GNU ARM Eclipse/Eclipse Workspaces/qemu-images-44-osx/f1-h103-blink-sh-shd/Debug/f1-h103-blink-sh-shd.elf'. Command line: '/Users/ilg/My Files/MacBookPro Projects/GNU ARM Eclipse/Eclipse Workspaces/qemu-images-44-osx/f1-h103-blink-sh-shd/Debug/f1-h103-blink-sh-shd.elf' (145 bytes). Load 10016 bytes at 0x08000000-0x0800271F.
Load 132 bytes at 0x08002720-0x080027A3.
Load 704 bytes at 0x20000084-0x20000343.
as you can see, it tries to use the kernel name as command line, which exceeds the application small buffer size.
unless you have a better idea, since for my use case the -kernel command line options is not only inappropriate and confusing, but also a cause of troubles, in my branch I have to enable again the -image option, which will also avoid this problem.
regards,
Liviu
Quoted 31 lines · Liviu Ionescu
Quoted 10 lines · Peter Maydell
Quoted 4 lines · Leon Alrae
Just a rebase of v4 as there were changes in
qemu_opt_foreachAPI.Regards,
LeonThanks; applied to target-arm.next.
I merged target-arm.next to my branch and I'm not sure the current approach is very correct.
if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) { /* fall back to the -kernel/-append */ semihosting_arg_fallback(kernel_filename, kernel_cmdline); }this code in
vl.cautomatically falls back to -kernel/-append, which, for Cortex-M might be a problem.without defining semihosting arguments, an example run for my case would look like:
GNU ARM Eclipse 64-bits QEMU v2.3.50 (qemu-system-gnuarmeclipse).
Board: 'STM32-H103' (Olimex Header Board for STM32F103RBT6).
Device: 'STM32F103RB' (Cortex-M3, MPU), Flash: 128 KB, RAM: 20 KB.
Image: '/Users/ilg/My Files/MacBookPro Projects/GNU ARM Eclipse/Eclipse Workspaces/qemu-images-44-osx/f1-h103-blink-sh-shd/Debug/f1-h103-blink-sh-shd.elf'. Command line: '/Users/ilg/My Files/MacBookPro Projects/GNU ARM Eclipse/Eclipse Workspaces/qemu-images-44-osx/f1-h103-blink-sh-shd/Debug/f1-h103-blink-sh-shd.elf' (145 bytes). Load 10016 bytes at 0x08000000-0x0800271F.
Load 132 bytes at 0x08002720-0x080027A3.
Load 704 bytes at 0x20000084-0x20000343.
Quoted 1 line
as you can see, it tries to use the kernel name as command line, which exceeds the application small buffer size.
It only does that if you didn't specify the arguments the new way (at least that is the intention). We need to maintain the old behaviour for back compatibility.
-- PMM
Quoted 7 lines · Peter Maydell
Quoted 2 lines
as you can see, it tries to use the kernel name as command line, which exceeds the application small buffer size.
It only does that if you didn't specify the arguments the new way (at least that is the intention).
what if I do not need to specify any arguments at all, and I want the application to know this?
Quoted 2 lines
We need to maintain the
old behaviour for back compatibility.
...
Quoted 6 lines · Peter Maydell
Quoted 2 lines
+
output_size= strlen(semihosting_get_cmdline()) + 1;It looks like
semihosting_get_cmdline()can return NULL, in which case this will blow up, I think.
if back compatibility for preserving the bugs was the goal, then it was reached, the new version also crashes in cases where there are no args and no kernel (for example when the image is loaded via GDB).
Quoted 5 lines · Leon Alrae
As far as I can tell existing
implementation may also blow up ifkernel_filenameis NULL, so we retain the same behaviour.
whatever your views regarding compatibility are, here the correct approach is to check the semihosting_get_cmdline() result for NULL, and make TARGET_SYS_GET_CMDLINE return -1 or an empty string.
regards,
Liviu
Quoted 10 lines · Liviu Ionescu
Quoted 7 lines · Peter Maydell
Quoted 2 lines
as you can see, it tries to use the kernel name as command line, which exceeds the application small buffer size.
It only does that if you didn't specify the arguments the new way (at least that is the intention).
what if I do not need to specify any arguments at all, and I want the application to know this?
For ARM, that's an empty command string. Does arg='' work in the new syntax?
Quoted 7 lines
Quoted 2 lines
We need to maintain the
old behaviour for back compatibility....
Quoted 2 lines · Peter Maydell
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
+
output_size= strlen(semihosting_get_cmdline()) + 1;It looks like
semihosting_get_cmdline()can return NULL, in which case this will blow up, I think.if back compatibility for preserving the bugs was the goal, then it was reached, the new version also crashes in cases where there are no args and no kernel (for example when the image is loaded via GDB).
Right, we discussed that in code review and concluded that it wasn't a new bug. I'm happy to accept a patch to fix it, but I figured it was better to get these patches in rather than go through another round to incorporate a fix to a problem that already existed in the current code.
thanks
-- PMM
Quoted 4 lines · Peter Maydell
For ARM, that's an empty command string.
this is too complicated, and currently not documented.
the natural method to specify that there are no args is... to add no args to the command line.
Quoted 2 lines
Does arg='' work in the
new syntax?
I don't know, in my environment I'm not using this syntax, I use the "--semihosting-cmdline arg0 arg1 ... " at the end of the line, which fits better my needs.
Quoted 1 line
... I'm happy to accept a patch to fix it,
based on the top of target-arm.next, right?
regards,
Liviu
Quoted 6 lines · Liviu Ionescu
Quoted 4 lines · Peter Maydell
For ARM, that's an empty command string.
this is too complicated, and currently not documented.
I mean, in the ARM semihosting API, this is indicated by returning an empty command string when the app asks for the command line.
Quoted 9 lines
the natural method to specify that there are no args is... to add no args to the command line.
Quoted 2 lines
Does arg='' work in the
new syntax?I don't know, in my environment I'm not using this syntax, I use the "--semihosting-cmdline arg0 arg1 ... " at the end of the line, which fits better my needs.
If you're using your own syntax you can deal with this however you like.
Quoted 3 lines
Quoted 1 line
... I'm happy to accept a patch to fix it,
based on the top of target-arm.next, right?
Against master is fine, all the outstanding arm patches have been flushed to master.
-- PMM