Skip to contentSkip to conversation

target-mips: add UHI semihosting support

PATCH In review Leon Alrae 3 participants 13 messages Jun 19, 2015 – Jun 24, 2015

lore
Share & tools
lore

Leon Alrae

author
#0 lore raw

v3 cover target-mips: add UHI semihosting support

This series introduces the Unified Hosting Interface support to QEMU. Version 3 of this patchset contains just minor cleanup and corrections in mips-semi.c comparing to previous version.

It has been on the mailing list for a while and given that command line option responsible for passing semihosting arguments is clarified I'm planning to apply this patch series to mips-next soon.

Patches apply on top of:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05013.html

Simple example for those who would like to give it a try:

Bare Metal Toolchain containing UHI support can be obtained from:
http://codescape-mips-sdk.imgtec.com/components/toolchain/2015.01-7/

* Source file:
#include <stdio.h>

  int main(int argc, char *argv[])
  {
      int i;
      for (i = 0; i < argc; i++) {
          printf("argv[%d] = %s\n", i, argv[i]);
      }
      return 0;
  }

* Build:

  $ mips-img-elf-gcc -mips64r6 -mabi=64 -EL -Tuhi64_64.ld main.c

* Run

  $ qemu-system-mips64el -cpu mips64r6-generic -M malta -nographic -kernel a.out -semihosting-config arg=a.out,arg=foo,arg="bar baz"
  argv[0] = a.out
  argv[1] = foo
  argv[2] = bar baz

Regards,
Leon

v3:
* remove useless #ifndef CONFIG_USER_ONLY
* correct errno in copy_stat_to_target(), write_to_file() and read_from_file()

v2:
* use new -semihosting arg option
* if semihosting args are not present, then fall back to old-style -kernel/-append
* clean up SDBBP and remove identical code in different branch
* fix overlooked microMIPS SDBBP
* translate errno values which don't map 1:1
* update dates in the licence header
* return -1 and fake errno when lock_user() fails in write_to_file() and read_from_file()

Leon Alrae (3):
target-mips: remove identical code in different branch
target-mips: add Unified Hosting Interface (UHI) support
target-mips: convert host to MIPS errno values when required

Maciej W. Rozycki (1):
include/softmmu-semi.h: Make semihosting support 64-bit clean

Matthew Fortune (1):
hw/mips: Do not clear BEV for MIPS malta kernel load

 hw/mips/mips_malta.c        |  11 +-
 include/exec/softmmu-semi.h |  13 +-
 qemu-options.hx             |  10 +-
 target-mips/Makefile.objs   |   2 +-
 target-mips/helper.h        |   2 +
 target-mips/mips-semi.c     | 358 ++++++++++++++++++++++++++++++++++++++++++++
 target-mips/translate.c     |  68 +++++----
 7 files changed, 426 insertions(+), 38 deletions(-)
 create mode 100644 target-mips/mips-semi.c
author
#1 lore raw

PATCH v3 1/5 include/softmmu-semi.h: Make semihosting support 64-bit clean

From: "Maciej W. Rozycki" <[email protected]>

Correct addresses passed around in semihosting to use a data type suitable for both 32-bit and 64-bit targets.

---
 include/exec/softmmu-semi.h | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
include/exec/softmmu-semi.h +7 -6
index 8401f7d..1819cc2 100644
@@ -9,14 +9,14 @@
 #ifndef SOFTMMU_SEMI_H
 #define SOFTMMU_SEMI_H 1
 
-static inline uint32_t softmmu_tget32(CPUArchState *env, uint32_t addr)
+static inline uint32_t softmmu_tget32(CPUArchState *env, target_ulong addr)
 {
     uint32_t val;
 
     cpu_memory_rw_debug(ENV_GET_CPU(env), addr, (uint8_t *)&val, 4, 0);
     return tswap32(val);
 }
-static inline uint32_t softmmu_tget8(CPUArchState *env, uint32_t addr)
+static inline uint32_t softmmu_tget8(CPUArchState *env, target_ulong addr)
 {
     uint8_t val;
 
@@ -28,7 +28,8 @@ static inline uint32_t softmmu_tget8(CPUArchState *env, uint32_t addr)
 #define get_user_u8(arg, p) ({ arg = softmmu_tget8(env, p) ; 0; })
 #define get_user_ual(arg, p) get_user_u32(arg, p)
 
-static inline void softmmu_tput32(CPUArchState *env, uint32_t addr, uint32_t val)
+static inline void softmmu_tput32(CPUArchState *env,
+                                  target_ulong addr, uint32_t val)
 {
     val = tswap32(val);
     cpu_memory_rw_debug(ENV_GET_CPU(env), addr, (uint8_t *)&val, 4, 1);
@@ -36,8 +37,8 @@ static inline void softmmu_tput32(CPUArchState *env, uint32_t addr, uint32_t val
 #define put_user_u32(arg, p) ({ softmmu_tput32(env, p, arg) ; 0; })
 #define put_user_ual(arg, p) put_user_u32(arg, p)
 
-static void *softmmu_lock_user(CPUArchState *env, uint32_t addr, uint32_t len,
-                               int copy)
+static void *softmmu_lock_user(CPUArchState *env,
+                               target_ulong addr, target_ulong len, int copy)
 {
     uint8_t *p;
     /* TODO: Make this something that isn't fixed size.  */
@@ -48,7 +49,7 @@ static void *softmmu_lock_user(CPUArchState *env, uint32_t addr, uint32_t len,
     return p;
 }
 #define lock_user(type, p, len, copy) softmmu_lock_user(env, p, len, copy)
-static char *softmmu_lock_user_string(CPUArchState *env, uint32_t addr)
+static char *softmmu_lock_user_string(CPUArchState *env, target_ulong addr)
 {
     char *p;
     char *s;
Reviewed-by on v3 1/5
#2 lore raw

Re: [Qemu-devel] [PATCH v3 1/5] include/softmmu-semi.h: Make semihosting support 64-bit clean

Quoted 11 lines · Leon Alrae

From: "Maciej W. Rozycki" <[email protected]>

Correct addresses passed around in semihosting to use a data type suitable for both 32-bit and 64-bit targets.

---
 include/exec/softmmu-semi.h | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
author
#3 lore raw

PATCH v3 2/5 hw/mips: Do not clear BEV for MIPS malta kernel load

In reply to Leon Alrae

From: Matthew Fortune <[email protected]>

The BEV flag controls whether the boot exception vector is still in place when starting a kernel. When cleared the exception vector at EBASE (or hard coded address of 0x80000000) is used instead.

The early stages of the linux kernel would benefit from BEV still being set to ensure any faults get handled by the boot rom exception handlers. This is a moot point for system qemu as there aren't really any BEV handlers, but there are other good reasons to change this...

The UHI (semi-hosting interface) defines special behaviours depending on whether an application starts in an environment with BEV set or cleared. When BEV is set then UHI assumes that a bootloader is relatively dumb and has no advanced exception handling logic. However, when BEV is cleared then UHI assumes that the bootloader has the ability to handle UHI exceptions with its exception handlers and will unwind and forward UHI SYSCALL exceptions to the exception vector that was installed prior to running the application.

---
 hw/mips/mips_malta.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/mips/mips_malta.c +1 -1
index 786a8f0..a5d64d5 100644
@@ -887,7 +887,7 @@ static void main_cpu_reset(void *opaque)
        read only location. The kernel location and the arguments table
        location does not change. */
     if (loaderparams.kernel_filename) {
-        env->CP0_Status &= ~((1 << CP0St_BEV) | (1 << CP0St_ERL));
+        env->CP0_Status &= ~(1 << CP0St_ERL);
     }
 
     malta_mips_config(cpu);
#4 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] hw/mips: Do not clear BEV for MIPS malta kernel load

Quoted 10 lines · Leon Alrae

From: Matthew Fortune <[email protected]>

The BEV flag controls whether the boot exception vector is still in place when starting a kernel. When cleared the exception vector at EBASE (or hard coded address of 0x80000000) is used instead.

The early stages of the linux kernel would benefit from BEV still being set to ensure any faults get handled by the boot rom exception handlers. This is a moot point for system qemu as there aren't really any BEV handlers, but there are other good reasons to change this...

The fake boot loader doesn't provide any exception handler at address 0xbcf00200. On the other hand the kernel might provide an exception handler at address 0x80000000 through the ELF file (this doesn't seem to be the case currently).

Is there a clear interface which defines the value of this bit when booting a kernel. If not, it would be a good idea to look what YAMON or U-Boot do and mimic that.

Quoted 8 lines

The UHI (semi-hosting interface) defines special behaviours depending on whether an application starts in an environment with BEV set or cleared. When BEV is set then UHI assumes that a bootloader is relatively dumb and has no advanced exception handling logic. However, when BEV is cleared then UHI assumes that the bootloader has the ability to handle UHI exceptions with its exception handlers and will unwind and forward UHI SYSCALL exceptions to the exception vector that was installed prior to running the application.

In UHI mode that indeed makes sense.

Quoted 20 lines
---
 hw/mips/mips_malta.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/mips/mips_malta.c +1 -1
index 786a8f0..a5d64d5 100644
@@ -887,7 +887,7 @@ static void main_cpu_reset(void *opaque)
        read only location. The kernel location and the arguments table
        location does not change. */
     if (loaderparams.kernel_filename) {
-        env->CP0_Status &= ~((1 << CP0St_BEV) | (1 << CP0St_ERL));
+        env->CP0_Status &= ~(1 << CP0St_ERL);
     }
 
     malta_mips_config(cpu);
Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#5 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] hw/mips: Do not clear BEV for MIPS malta kernel load

Quoted 12 lines · Aurelien Jarno
Quoted 10 lines · Leon Alrae

From: Matthew Fortune <[email protected]>

The BEV flag controls whether the boot exception vector is still in place when starting a kernel. When cleared the exception vector at EBASE (or hard coded address of 0x80000000) is used instead.

The early stages of the linux kernel would benefit from BEV still being set to ensure any faults get handled by the boot rom exception handlers. This is a moot point for system qemu as there aren't really any BEV handlers, but there are other good reasons to change this...

Quoted 4 lines

The fake boot loader doesn't provide any exception handler at address 0xbcf00200. On the other hand the kernel might provide an exception handler at address 0x80000000 through the ELF file (this doesn't seem to be the case currently).

I don't think this would be a reasonable thing to support. If something clears BEV then IMO it is responsible for putting an exception handler in 0x80000000 (/EBASE). The fact that the fake bootloader has nothing at 0xbfc00200 doesn't really make anything any worse, it doesn't have one at 0x80000000 either. An application that is aware of the need for providing an exception vector is also going to have to account for BEV and EBASE to operate safely so I'd call it an app bug if it did anything other.

Quoted 3 lines

Is there a clear interface which defines the value of this bit when booting a kernel. If not, it would be a good idea to look what YAMON or U-Boot do and mimic that.

My investigation at the time (as a non-kernel developer) showed that the status register is taken over entirely with the only exception being that the ERL bit is mostly expected to be clear by the time the kernel is entered.

u-boot and yamon clear BEV but, importantly, provide an exception vector at EBASE.

Thanks,
Matthew

Quoted 36 lines
Quoted 8 lines

The UHI (semi-hosting interface) defines special behaviours depending on whether an application starts in an environment with BEV set or cleared. When BEV is set then UHI assumes that a bootloader is relatively dumb and has no advanced exception handling logic. However, when BEV is cleared then UHI assumes that the bootloader has the ability to handle UHI exceptions with its exception handlers and will unwind and forward UHI SYSCALL exceptions to the exception vector that was installed prior to running the application.

In UHI mode that indeed makes sense.

hw/mips/mips_malta.c static void main_cpu_reset(void *opaque) · line 887

Quoted 12 lines
---
 hw/mips/mips_malta.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/mips/mips_malta.c index renamed from hw/mips/mips_malta.c +0 -0

786a8f0..a5d64d5 100644

hw/mips/mips_malta.c +0 -0
@@ -887,7 +887,7 @@ static void main_cpu_reset(void *opaque)
        read only location. The kernel location and the arguments

table

hw/mips/mips_malta.c static void main_cpu_reset(void *opaque) · line 893

Quoted 8 lines
hw/mips/mips_malta.c +1 -1
        location does not change. */
     if (loaderparams.kernel_filename) {
-        env->CP0_Status &= ~((1 << CP0St_BEV) | (1 << CP0St_ERL));
+        env->CP0_Status &= ~(1 << CP0St_ERL);
     }
 
     malta_mips_config(cpu);

--
Aurelien Jarno GPG: 4096R/1DDD8C9B
[email protected] http://www.aurel32.net

author
#6 lore raw

PATCH v3 3/5 target-mips: remove identical code in different branch

In reply to Leon Alrae
---
 target-mips/translate.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
target-mips/translate.c +4 -21
index 1d128ee..6fd6dd9 100644
@@ -11852,11 +11852,7 @@ static int decode_mips16_opc (CPUMIPSState *env, DisasContext *ctx)
              *      when in debug mode...
              */
             check_insn(ctx, ISA_MIPS32);
-            if (!(ctx->hflags & MIPS_HFLAG_DM)) {
-                generate_exception(ctx, EXCP_DBp);
-            } else {
-                generate_exception(ctx, EXCP_DBp);
-            }
+            generate_exception(ctx, EXCP_DBp);
             break;
         case RR_SLT:
             gen_slt(ctx, OPC_SLT, 24, rx, ry);
@@ -12707,11 +12703,7 @@ static void gen_pool16c_insn(DisasContext *ctx)
          *      when in debug mode...
          */
         check_insn(ctx, ISA_MIPS32);
-        if (!(ctx->hflags & MIPS_HFLAG_DM)) {
-            generate_exception(ctx, EXCP_DBp);
-        } else {
-            generate_exception(ctx, EXCP_DBp);
-        }
+        generate_exception(ctx, EXCP_DBp);
         break;
     case JRADDIUSP + 0:
     case JRADDIUSP + 1:
@@ -13076,11 +13068,7 @@ static void gen_pool32axf (CPUMIPSState *env, DisasContext *ctx, int rt, int rs)
             break;
         case SDBBP:
             check_insn(ctx, ISA_MIPS32);
-            if (!(ctx->hflags & MIPS_HFLAG_DM)) {
-                generate_exception(ctx, EXCP_DBp);
-            } else {
-                generate_exception(ctx, EXCP_DBp);
-            }
+            generate_exception(ctx, EXCP_DBp);
             break;
         default:
             goto pool32axf_invalid;
@@ -16849,12 +16837,7 @@ static void decode_opc_special2_legacy(CPUMIPSState *env, DisasContext *ctx)
          *      when in debug mode...
          */
         check_insn(ctx, ISA_MIPS32);
-        if (!(ctx->hflags & MIPS_HFLAG_DM)) {
-            generate_exception(ctx, EXCP_DBp);
-        } else {
-            generate_exception(ctx, EXCP_DBp);
-        }
-        /* Treat as NOP. */
+        generate_exception(ctx, EXCP_DBp);
         break;
 #if defined(TARGET_MIPS64)
     case OPC_DCLO:
Reviewed-by on v3 3/5
#7 lore raw

Re: [Qemu-devel] [PATCH v3 3/5] target-mips: remove identical code in different branch

Quoted 10 lines · Leon Alrae
---
 target-mips/translate.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
target-mips/translate.c +0 -0
index 1d128ee..6fd6dd9 100644

target-mips/translate.c static int decode_mips16_opc (CPUMIPSState *env, DisasContext *ctx) · line 11855

Quoted 9 lines
target-mips/translate.c +1 -5
@@ -11852,11 +11852,7 @@ static int decode_mips16_opc (CPUMIPSState *env, DisasContext *ctx)
              *      when in debug mode...
              */
             check_insn(ctx, ISA_MIPS32);
-            if (!(ctx->hflags & MIPS_HFLAG_DM)) {
-                generate_exception(ctx, EXCP_DBp);
-            } else {
-                generate_exception(ctx, EXCP_DBp);
-            }
+            generate_exception(ctx, EXCP_DBp);

The reason for this duplicated code, is from the comment above, that is we are not sure which exception should be generated in debug mode. If someone knows the answer (or my experiment that on real hardware) that might be a good opportu to fix that the correct way.

Quoted 34 lines
target-mips/translate.c +2 -10
             break;
         case RR_SLT:
             gen_slt(ctx, OPC_SLT, 24, rx, ry);
@@ -12707,11 +12703,7 @@ static void gen_pool16c_insn(DisasContext *ctx)
          *      when in debug mode...
          */
         check_insn(ctx, ISA_MIPS32);
-        if (!(ctx->hflags & MIPS_HFLAG_DM)) {
-            generate_exception(ctx, EXCP_DBp);
-        } else {
-            generate_exception(ctx, EXCP_DBp);
-        }
+        generate_exception(ctx, EXCP_DBp);
         break;
     case JRADDIUSP + 0:
     case JRADDIUSP + 1:
@@ -13076,11 +13068,7 @@ static void gen_pool32axf (CPUMIPSState *env, DisasContext *ctx, int rt, int rs)
             break;
         case SDBBP:
             check_insn(ctx, ISA_MIPS32);
-            if (!(ctx->hflags & MIPS_HFLAG_DM)) {
-                generate_exception(ctx, EXCP_DBp);
-            } else {
-                generate_exception(ctx, EXCP_DBp);
-            }
+            generate_exception(ctx, EXCP_DBp);
             break;
         default:
             goto pool32axf_invalid;
@@ -16849,12 +16837,7 @@ static void decode_opc_special2_legacy(CPUMIPSState *env, DisasContext *ctx)
          *      when in debug mode...
          */
         check_insn(ctx, ISA_MIPS32);

target-mips/translate.c static void decode_opc_special2_legacy(CPUMIPSState *env, DisasContext *ctx) · line 16843

Quoted 10 lines
target-mips/translate.c +1 -6
-        if (!(ctx->hflags & MIPS_HFLAG_DM)) {
-            generate_exception(ctx, EXCP_DBp);
-        } else {
-            generate_exception(ctx, EXCP_DBp);
-        }
-        /* Treat as NOP. */
+        generate_exception(ctx, EXCP_DBp);
         break;
 #if defined(TARGET_MIPS64)
     case OPC_DCLO:

Besides the nitpick above:

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
author
#8 lore raw

Re: [Qemu-devel] [PATCH v3 3/5] target-mips: remove identical code in different branch

Quoted 17 lines · Aurelien Jarno
Quoted 16 lines · Leon Alrae
---
 target-mips/translate.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
target-mips/translate.c +0 -3
index 1d128ee..6fd6dd9 100644
@@ -11852,11 +11852,7 @@ static int decode_mips16_opc (CPUMIPSState *env, DisasContext *ctx)
              *      when in debug mode...
              */
             check_insn(ctx, ISA_MIPS32);
-            if (!(ctx->hflags & MIPS_HFLAG_DM)) {
-                generate_exception(ctx, EXCP_DBp);
-            } else {
Quoted 8 lines

target-mips/translate.c static int decode_mips16_opc (CPUMIPSState *env, DisasContext *ctx) · line 11855

Quoted 3 lines
target-mips/translate.c +1 -2
-                generate_exception(ctx, EXCP_DBp);
-            }
+            generate_exception(ctx, EXCP_DBp);

The reason for this duplicated code, is from the comment above, that is we are not sure which exception should be generated in debug mode. If someone knows the answer (or my experiment that on real hardware) that might be a good opportu to fix that the correct way.

Yes, that's why I left the comment so we are still aware of this problem and we know where to fix it once we know the answer. But at the time I was touching this code I felt it should be removed (it generates 4 medium issues in Coverity).

Leon

author
#9 lore raw

PATCH v3 4/5 target-mips: add Unified Hosting Interface (UHI) support

In reply to Leon Alrae

Add UHI semihosting support for MIPS. QEMU run with "-semihosting" option will alter the behaviour of SDBBP 1 instruction -- UHI operation will be called instead of generating a debug exception.

Also tweak Malta's pseudo-bootloader. On CPU reset the $4 register is set to -1 if semihosting arguments are passed to indicate that the UHI operations should be used to obtain input arguments.

---
 hw/mips/mips_malta.c      |   9 +-
 qemu-options.hx           |  10 +-
 target-mips/Makefile.objs |   2 +-
 target-mips/helper.h      |   2 +
 target-mips/mips-semi.c   | 336 ++++++++++++++++++++++++++++++++++++++++++++++
 target-mips/translate.c   |  75 ++++++++---
 6 files changed, 408 insertions(+), 26 deletions(-)
 create mode 100644 target-mips/mips-semi.c
hw/mips/mips_malta.c +8 -1
index a5d64d5..3082e75 100644
@@ -53,6 +53,7 @@
 #include "qemu/error-report.h"
 #include "hw/empty_slot.h"
 #include "sysemu/kvm.h"
+#include "exec/semihost.h"
 
 //#define DEBUG_BOARD_INIT
 
@@ -634,7 +635,13 @@ static void write_bootloader (CPUMIPSState *env, uint8_t *base,
 
     /* Second part of the bootloader */
     p = (uint32_t *) (base + 0x580);
-    stl_p(p++, 0x24040002);                                      /* addiu a0, zero, 2 */
+
+    if (semihosting_get_argc()) {
+        /* Preserve a0 content as arguments have been passed */
+        stl_p(p++, 0x00000000);                         /* nop */
+    } else {
+        stl_p(p++, 0x24040002);                         /* addiu a0, zero, 2 */
+    }
     stl_p(p++, 0x3c1d0000 | (((ENVP_ADDR - 64) >> 16) & 0xffff)); /* lui sp, high(ENVP_ADDR) */
     stl_p(p++, 0x37bd0000 | ((ENVP_ADDR - 64) & 0xffff));        /* ori sp, sp, low(ENVP_ADDR) */
     stl_p(p++, 0x3c050000 | ((ENVP_ADDR >> 16) & 0xffff));       /* lui a1, high(ENVP_ADDR) */
qemu-options.hx +6 -4
index 7959dd0..5243aee 100644
@@ -3344,20 +3344,22 @@ Set OpenBIOS nvram @var{variable} to given @var{value} (PPC, SPARC only).
 ETEXI
 DEF("semihosting", 0, QEMU_OPTION_semihosting,
     "-semihosting    semihosting mode\n",
-    QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | QEMU_ARCH_LM32)
+    QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | QEMU_ARCH_LM32 |
+    QEMU_ARCH_MIPS)
 STEXI
 @item -semihosting
 @findex -semihosting
-Enable semihosting mode (ARM, M68K, Xtensa only).
+Enable semihosting mode (ARM, M68K, Xtensa, MIPS only).
 ETEXI
 DEF("semihosting-config", HAS_ARG, QEMU_OPTION_semihosting_config,
     "-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)
+QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | QEMU_ARCH_LM32 |
+QEMU_ARCH_MIPS)
 STEXI
 @item -semihosting-config [enable=on|off][,target=native|gdb|auto][,arg=str[,...]]
 @findex -semihosting-config
-Enable and configure semihosting (ARM, M68K, Xtensa only).
+Enable and configure semihosting (ARM, M68K, Xtensa, MIPS only).
 @table @option
 @item target=@code{native|gdb|auto}
 Defines where the semihosting calls will be addressed, to QEMU (@code{native})
target-mips/Makefile.objs +1 -1
index 108fd9b..bc5ed85 100644
@@ -1,4 +1,4 @@
 obj-y += translate.o dsp_helper.o op_helper.o lmi_helper.o helper.o cpu.o
-obj-y += gdbstub.o msa_helper.o
+obj-y += gdbstub.o msa_helper.o mips-semi.o
 obj-$(CONFIG_SOFTMMU) += machine.o
 obj-$(CONFIG_KVM) += kvm.o
target-mips/helper.h +2 -0
index 8df98c7..2b28e87 100644
@@ -1,6 +1,8 @@
 DEF_HELPER_3(raise_exception_err, noreturn, env, i32, int)
 DEF_HELPER_2(raise_exception, noreturn, env, i32)
 
+DEF_HELPER_1(do_semihosting, void, env)
+
 #ifdef TARGET_MIPS64
 DEF_HELPER_4(sdl, void, env, tl, tl, int)
 DEF_HELPER_4(sdr, void, env, tl, tl, int)
target-mips/mips-semi.c new file +336 -0

336 lines not on this page · raw message

target-mips/translate.c +55 -20

124 lines not on this page · raw message

Reviewed-by on v3 4/5
#10 lore raw

Re: [Qemu-devel] [PATCH v3 4/5] target-mips: add Unified Hosting Interface (UHI) support

Quoted 9 lines · Leon Alrae

Add UHI semihosting support for MIPS. QEMU run with "-semihosting" option will alter the behaviour of SDBBP 1 instruction -- UHI operation will be called instead of generating a debug exception.

Also tweak Malta's pseudo-bootloader. On CPU reset the $4 register is set to -1 if semihosting arguments are passed to indicate that the UHI operations should be used to obtain input arguments.

Quoted 9 lines
---
 hw/mips/mips_malta.c      |   9 +-
 qemu-options.hx           |  10 +-
 target-mips/Makefile.objs |   2 +-
 target-mips/helper.h      |   2 +
 target-mips/mips-semi.c   | 336 ++++++++++++++++++++++++++++++++++++++++++++++
 target-mips/translate.c   |  75 ++++++++---
 6 files changed, 408 insertions(+), 26 deletions(-)
 create mode 100644 target-mips/mips-semi.c
Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
author
#11 lore raw

PATCH v3 5/5 target-mips: convert host to MIPS errno values when required

In reply to Leon Alrae

Convert only errno values which can be returned by system calls in mips-semi.c and are not generic to all archs.

---
 target-mips/mips-semi.c | 44 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 33 insertions(+), 11 deletions(-)
target-mips/mips-semi.c +33 -11
index 6944e98..1162c76 100644
@@ -72,6 +72,28 @@ enum UHIOpenFlags {
     UHIOpen_EXCL   = 0x800
 };
 
+/* Errno values taken from asm-mips/errno.h */
+static uint16_t host_to_mips_errno[] = {
+    [ENAMETOOLONG] = 78,
+#ifdef EOVERFLOW
+    [EOVERFLOW]    = 79,
+#endif
+#ifdef ELOOP
+    [ELOOP]        = 90,
+#endif
+};
+
+static int errno_mips(int err)
+{
+    if (err < 0 || err >= ARRAY_SIZE(host_to_mips_errno)) {
+        return EINVAL;
+    } else if (host_to_mips_errno[err]) {
+        return host_to_mips_errno[err];
+    } else {
+        return err;
+    }
+}
+
 static int copy_stat_to_target(CPUMIPSState *env, const struct stat *src,
                                target_ulong vaddr)
 {
@@ -223,7 +245,7 @@ void helper_do_semihosting(CPUMIPSState *env)
             gpr[2] = 2;
         } else {
             gpr[2] = open(p, get_open_flags(gpr[5]), gpr[6]);
-            gpr[3] = errno;
+            gpr[3] = errno_mips(errno);
         }
         FREE_TARGET_STRING(p, gpr[4]);
         break;
@@ -234,24 +256,24 @@ void helper_do_semihosting(CPUMIPSState *env)
             goto uhi_done;
         }
         gpr[2] = close(gpr[4]);
-        gpr[3] = errno;
+        gpr[3] = errno_mips(errno);
         break;
     case UHI_read:
         gpr[2] = read_from_file(env, gpr[4], gpr[5], gpr[6], 0);
-        gpr[3] = errno;
+        gpr[3] = errno_mips(errno);
         break;
     case UHI_write:
         gpr[2] = write_to_file(env, gpr[4], gpr[5], gpr[6], 0);
-        gpr[3] = errno;
+        gpr[3] = errno_mips(errno);
         break;
     case UHI_lseek:
         gpr[2] = lseek(gpr[4], gpr[5], gpr[6]);
-        gpr[3] = errno;
+        gpr[3] = errno_mips(errno);
         break;
     case UHI_unlink:
         GET_TARGET_STRING(p, gpr[4]);
         gpr[2] = remove(p);
-        gpr[3] = errno;
+        gpr[3] = errno_mips(errno);
         FREE_TARGET_STRING(p, gpr[4]);
         break;
     case UHI_fstat:
@@ -259,12 +281,12 @@ void helper_do_semihosting(CPUMIPSState *env)
             struct stat sbuf;
             memset(&sbuf, 0, sizeof(sbuf));
             gpr[2] = fstat(gpr[4], &sbuf);
-            gpr[3] = errno;
+            gpr[3] = errno_mips(errno);
             if (gpr[2]) {
                 goto uhi_done;
             }
             gpr[2] = copy_stat_to_target(env, &sbuf, gpr[5]);
-            gpr[3] = errno;
+            gpr[3] = errno_mips(errno);
         }
         break;
     case UHI_argc:
@@ -311,18 +333,18 @@ void helper_do_semihosting(CPUMIPSState *env)
         break;
     case UHI_pread:
         gpr[2] = read_from_file(env, gpr[4], gpr[5], gpr[6], gpr[7]);
-        gpr[3] = errno;
+        gpr[3] = errno_mips(errno);
         break;
     case UHI_pwrite:
         gpr[2] = write_to_file(env, gpr[4], gpr[5], gpr[6], gpr[7]);
-        gpr[3] = errno;
+        gpr[3] = errno_mips(errno);
         break;
 #ifndef _WIN32
     case UHI_link:
         GET_TARGET_STRING(p, gpr[4]);
         GET_TARGET_STRING(p2, gpr[5]);
         gpr[2] = link(p, p2);
-        gpr[3] = errno;
+        gpr[3] = errno_mips(errno);
         FREE_TARGET_STRING(p2, gpr[5]);
         FREE_TARGET_STRING(p, gpr[4]);
         break;
Reviewed-by on v3 5/5
#12 lore raw

Re: [Qemu-devel] [PATCH v3 5/5] target-mips: convert host to MIPS errno values when required

Quoted 7 lines · Leon Alrae

Convert only errno values which can be returned by system calls in mips-semi.c and are not generic to all archs.

---
 target-mips/mips-semi.c | 44 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 33 insertions(+), 11 deletions(-)
Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net