diff mbox series

[v4,44/53] semihosting: Cleanup chardev init

Message ID 20220607204557.658541-45-richard.henderson@linaro.org
State Accepted
Commit fb08790b35174a98301ecbac4d5234d0cbfebea0
Headers show
Series semihosting cleanup | expand

Commit Message

Richard Henderson June 7, 2022, 8:45 p.m. UTC
Rename qemu_semihosting_connect_chardevs to
qemu_semihosting_chardev_init; pass the result
directly to qemu_semihosting_console_init.

Store the chardev in SemihostingConsole instead
of SemihostingConfig, which lets us drop
semihosting_get_chardev.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 include/semihosting/semihost.h | 13 ++-----------
 semihosting/config.c           | 17 +++++++----------
 semihosting/console.c          | 31 +++++++++++++++----------------
 softmmu/vl.c                   |  3 +--
 stubs/semihost.c               |  6 +-----
 5 files changed, 26 insertions(+), 44 deletions(-)

Comments

Luc Michel June 24, 2022, 9:07 a.m. UTC | #1
On 13:45 Tue 07 Jun     , Richard Henderson wrote:
> Rename qemu_semihosting_connect_chardevs to
> qemu_semihosting_chardev_init; pass the result
> directly to qemu_semihosting_console_init.
> 
> Store the chardev in SemihostingConsole instead
> of SemihostingConfig, which lets us drop
> semihosting_get_chardev.
> 
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

Reviewed-by: Luc Michel <lmichel@kalray.eu>

> ---
>  include/semihosting/semihost.h | 13 ++-----------
>  semihosting/config.c           | 17 +++++++----------
>  semihosting/console.c          | 31 +++++++++++++++----------------
>  softmmu/vl.c                   |  3 +--
>  stubs/semihost.c               |  6 +-----
>  5 files changed, 26 insertions(+), 44 deletions(-)
> 
> diff --git a/include/semihosting/semihost.h b/include/semihosting/semihost.h
> index 0c55ade3ac..5b36a76f08 100644
> --- a/include/semihosting/semihost.h
> +++ b/include/semihosting/semihost.h
> @@ -51,14 +51,6 @@ static inline const char *semihosting_get_cmdline(void)
>  {
>      return NULL;
>  }
> -
> -static inline Chardev *semihosting_get_chardev(void)
> -{
> -    return NULL;
> -}
> -static inline void qemu_semihosting_console_init(void)
> -{
> -}
>  #else /* !CONFIG_USER_ONLY */
>  bool semihosting_enabled(void);
>  SemihostingTarget semihosting_get_target(void);
> @@ -66,12 +58,11 @@ const char *semihosting_get_arg(int i);
>  int semihosting_get_argc(void);
>  const char *semihosting_get_cmdline(void);
>  void semihosting_arg_fallback(const char *file, const char *cmd);
> -Chardev *semihosting_get_chardev(void);
>  /* for vl.c hooks */
>  void qemu_semihosting_enable(void);
>  int qemu_semihosting_config_options(const char *opt);
> -void qemu_semihosting_connect_chardevs(void);
> -void qemu_semihosting_console_init(void);
> +void qemu_semihosting_chardev_init(void);
> +void qemu_semihosting_console_init(Chardev *);
>  #endif /* CONFIG_USER_ONLY */
>  
>  #endif /* SEMIHOST_H */
> diff --git a/semihosting/config.c b/semihosting/config.c
> index 50d82108e6..4bca769fad 100644
> --- a/semihosting/config.c
> +++ b/semihosting/config.c
> @@ -50,7 +50,6 @@ QemuOptsList qemu_semihosting_config_opts = {
>  typedef struct SemihostingConfig {
>      bool enabled;
>      SemihostingTarget target;
> -    Chardev *chardev;
>      char **argv;
>      int argc;
>      const char *cmdline; /* concatenated argv */
> @@ -121,11 +120,6 @@ void semihosting_arg_fallback(const char *file, const char *cmd)
>      }
>  }
>  
> -Chardev *semihosting_get_chardev(void)
> -{
> -    return semihosting.chardev;
> -}
> -
>  void qemu_semihosting_enable(void)
>  {
>      semihosting.enabled = true;
> @@ -171,16 +165,19 @@ int qemu_semihosting_config_options(const char *optarg)
>      return 0;
>  }
>  
> -void qemu_semihosting_connect_chardevs(void)
> +/* We had to defer this until chardevs were created */
> +void qemu_semihosting_chardev_init(void)
>  {
> -    /* We had to defer this until chardevs were created */
> +    Chardev *chr = NULL;
> +
>      if (semihost_chardev) {
> -        Chardev *chr = qemu_chr_find(semihost_chardev);
> +        chr = qemu_chr_find(semihost_chardev);
>          if (chr == NULL) {
>              error_report("semihosting chardev '%s' not found",
>                           semihost_chardev);
>              exit(1);
>          }
> -        semihosting.chardev = chr;
>      }
> +
> +    qemu_semihosting_console_init(chr);
>  }
> diff --git a/semihosting/console.c b/semihosting/console.c
> index df618a28a4..4088192842 100644
> --- a/semihosting/console.c
> +++ b/semihosting/console.c
> @@ -27,11 +27,21 @@
>  #include "qapi/error.h"
>  #include "qemu/fifo8.h"
>  
> +/* Access to this structure is protected by the BQL */
> +typedef struct SemihostingConsole {
> +    CharBackend         backend;
> +    Chardev             *chr;
> +    GSList              *sleeping_cpus;
> +    bool                got;
> +    Fifo8               fifo;
> +} SemihostingConsole;
> +
> +static SemihostingConsole console;
> +
>  int qemu_semihosting_log_out(const char *s, int len)
>  {
> -    Chardev *chardev = semihosting_get_chardev();
> -    if (chardev) {
> -        return qemu_chr_write_all(chardev, (uint8_t *) s, len);
> +    if (console.chr) {
> +        return qemu_chr_write_all(console.chr, (uint8_t *) s, len);
>      } else {
>          return write(STDERR_FILENO, s, len);
>      }
> @@ -106,16 +116,6 @@ void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr)
>  
>  #define FIFO_SIZE   1024
>  
> -/* Access to this structure is protected by the BQL */
> -typedef struct SemihostingConsole {
> -    CharBackend         backend;
> -    GSList              *sleeping_cpus;
> -    bool                got;
> -    Fifo8               fifo;
> -} SemihostingConsole;
> -
> -static SemihostingConsole console;
> -
>  static int console_can_read(void *opaque)
>  {
>      SemihostingConsole *c = opaque;
> @@ -169,10 +169,9 @@ int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
>      return ret;
>  }
>  
> -void qemu_semihosting_console_init(void)
> +void qemu_semihosting_console_init(Chardev *chr)
>  {
> -    Chardev *chr = semihosting_get_chardev();
> -
> +    console.chr = chr;
>      if  (chr) {
>          fifo8_create(&console.fifo, FIFO_SIZE);
>          qemu_chr_fe_init(&console.backend, chr, &error_abort);
> diff --git a/softmmu/vl.c b/softmmu/vl.c
> index 4c1e94b00e..83e2af4eab 100644
> --- a/softmmu/vl.c
> +++ b/softmmu/vl.c
> @@ -1944,8 +1944,7 @@ static void qemu_create_late_backends(void)
>          exit(1);
>  
>      /* now chardevs have been created we may have semihosting to connect */
> -    qemu_semihosting_connect_chardevs();
> -    qemu_semihosting_console_init();
> +    qemu_semihosting_chardev_init();
>  }
>  
>  static void cxl_set_opts(void)
> diff --git a/stubs/semihost.c b/stubs/semihost.c
> index 4bf2cf71b9..f486651afb 100644
> --- a/stubs/semihost.c
> +++ b/stubs/semihost.c
> @@ -65,10 +65,6 @@ void semihosting_arg_fallback(const char *file, const char *cmd)
>  {
>  }
>  
> -void qemu_semihosting_connect_chardevs(void)
> -{
> -}
> -
> -void qemu_semihosting_console_init(void)
> +void qemu_semihosting_chardev_init(void)
>  {
>  }
> -- 
> 2.34.1
> 
> 
> 
> 
> To declare a filtering error, please use the following link : https://www.security-mail.net/reporter.php?mid=f3b8.629fd419.cecd0.0&r=lmichel%40kalrayinc.com&s=qemu-devel-bounces%2Blmichel%3Dkalrayinc.com%40nongnu.org&o=%5BPATCH+v4+44%2F53%5D+semihosting%3A+Cleanup+chardev+init&verdict=C&c=10d71a2e8353924e95ea4f98e698ba376e5bb76b
> 

--
diff mbox series

Patch

diff --git a/include/semihosting/semihost.h b/include/semihosting/semihost.h
index 0c55ade3ac..5b36a76f08 100644
--- a/include/semihosting/semihost.h
+++ b/include/semihosting/semihost.h
@@ -51,14 +51,6 @@  static inline const char *semihosting_get_cmdline(void)
 {
     return NULL;
 }
-
-static inline Chardev *semihosting_get_chardev(void)
-{
-    return NULL;
-}
-static inline void qemu_semihosting_console_init(void)
-{
-}
 #else /* !CONFIG_USER_ONLY */
 bool semihosting_enabled(void);
 SemihostingTarget semihosting_get_target(void);
@@ -66,12 +58,11 @@  const char *semihosting_get_arg(int i);
 int semihosting_get_argc(void);
 const char *semihosting_get_cmdline(void);
 void semihosting_arg_fallback(const char *file, const char *cmd);
-Chardev *semihosting_get_chardev(void);
 /* for vl.c hooks */
 void qemu_semihosting_enable(void);
 int qemu_semihosting_config_options(const char *opt);
-void qemu_semihosting_connect_chardevs(void);
-void qemu_semihosting_console_init(void);
+void qemu_semihosting_chardev_init(void);
+void qemu_semihosting_console_init(Chardev *);
 #endif /* CONFIG_USER_ONLY */
 
 #endif /* SEMIHOST_H */
diff --git a/semihosting/config.c b/semihosting/config.c
index 50d82108e6..4bca769fad 100644
--- a/semihosting/config.c
+++ b/semihosting/config.c
@@ -50,7 +50,6 @@  QemuOptsList qemu_semihosting_config_opts = {
 typedef struct SemihostingConfig {
     bool enabled;
     SemihostingTarget target;
-    Chardev *chardev;
     char **argv;
     int argc;
     const char *cmdline; /* concatenated argv */
@@ -121,11 +120,6 @@  void semihosting_arg_fallback(const char *file, const char *cmd)
     }
 }
 
-Chardev *semihosting_get_chardev(void)
-{
-    return semihosting.chardev;
-}
-
 void qemu_semihosting_enable(void)
 {
     semihosting.enabled = true;
@@ -171,16 +165,19 @@  int qemu_semihosting_config_options(const char *optarg)
     return 0;
 }
 
-void qemu_semihosting_connect_chardevs(void)
+/* We had to defer this until chardevs were created */
+void qemu_semihosting_chardev_init(void)
 {
-    /* We had to defer this until chardevs were created */
+    Chardev *chr = NULL;
+
     if (semihost_chardev) {
-        Chardev *chr = qemu_chr_find(semihost_chardev);
+        chr = qemu_chr_find(semihost_chardev);
         if (chr == NULL) {
             error_report("semihosting chardev '%s' not found",
                          semihost_chardev);
             exit(1);
         }
-        semihosting.chardev = chr;
     }
+
+    qemu_semihosting_console_init(chr);
 }
diff --git a/semihosting/console.c b/semihosting/console.c
index df618a28a4..4088192842 100644
--- a/semihosting/console.c
+++ b/semihosting/console.c
@@ -27,11 +27,21 @@ 
 #include "qapi/error.h"
 #include "qemu/fifo8.h"
 
+/* Access to this structure is protected by the BQL */
+typedef struct SemihostingConsole {
+    CharBackend         backend;
+    Chardev             *chr;
+    GSList              *sleeping_cpus;
+    bool                got;
+    Fifo8               fifo;
+} SemihostingConsole;
+
+static SemihostingConsole console;
+
 int qemu_semihosting_log_out(const char *s, int len)
 {
-    Chardev *chardev = semihosting_get_chardev();
-    if (chardev) {
-        return qemu_chr_write_all(chardev, (uint8_t *) s, len);
+    if (console.chr) {
+        return qemu_chr_write_all(console.chr, (uint8_t *) s, len);
     } else {
         return write(STDERR_FILENO, s, len);
     }
@@ -106,16 +116,6 @@  void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr)
 
 #define FIFO_SIZE   1024
 
-/* Access to this structure is protected by the BQL */
-typedef struct SemihostingConsole {
-    CharBackend         backend;
-    GSList              *sleeping_cpus;
-    bool                got;
-    Fifo8               fifo;
-} SemihostingConsole;
-
-static SemihostingConsole console;
-
 static int console_can_read(void *opaque)
 {
     SemihostingConsole *c = opaque;
@@ -169,10 +169,9 @@  int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
     return ret;
 }
 
-void qemu_semihosting_console_init(void)
+void qemu_semihosting_console_init(Chardev *chr)
 {
-    Chardev *chr = semihosting_get_chardev();
-
+    console.chr = chr;
     if  (chr) {
         fifo8_create(&console.fifo, FIFO_SIZE);
         qemu_chr_fe_init(&console.backend, chr, &error_abort);
diff --git a/softmmu/vl.c b/softmmu/vl.c
index 4c1e94b00e..83e2af4eab 100644
--- a/softmmu/vl.c
+++ b/softmmu/vl.c
@@ -1944,8 +1944,7 @@  static void qemu_create_late_backends(void)
         exit(1);
 
     /* now chardevs have been created we may have semihosting to connect */
-    qemu_semihosting_connect_chardevs();
-    qemu_semihosting_console_init();
+    qemu_semihosting_chardev_init();
 }
 
 static void cxl_set_opts(void)
diff --git a/stubs/semihost.c b/stubs/semihost.c
index 4bf2cf71b9..f486651afb 100644
--- a/stubs/semihost.c
+++ b/stubs/semihost.c
@@ -65,10 +65,6 @@  void semihosting_arg_fallback(const char *file, const char *cmd)
 {
 }
 
-void qemu_semihosting_connect_chardevs(void)
-{
-}
-
-void qemu_semihosting_console_init(void)
+void qemu_semihosting_chardev_init(void)
 {
 }