Tensilica iss provides support for applications running in freestanding environment through SIMCALL command. It is used by Tensilica libc to access argc/argv, for file I/O, etc.
Signed-off-by: Max Filippov <jcmvb...@gmail.com> --- RFC -> PATCH changes: - use -semihosting to implement SIMCALL; - fix user memory manipulation code; - fill in errno for failure cases; - rename SYS_* to TARGET_SYS_*; --- Makefile.target | 1 + qemu-options.hx | 4 +- target-xtensa/cpu.h | 1 + target-xtensa/helpers.h | 1 + target-xtensa/op_helper.c | 5 ++ target-xtensa/translate.c | 8 ++- xtensa-semi.c | 176 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 xtensa-semi.c diff --git a/Makefile.target b/Makefile.target index ca0db72..1ce0521 100644 --- a/Makefile.target +++ b/Makefile.target @@ -380,6 +380,7 @@ obj-alpha-y = alpha_palcode.o obj-xtensa-y += xtensa_pic.o obj-xtensa-y += xtensa_sample.o +obj-xtensa-y += xtensa-semi.o main.o: QEMU_CFLAGS+=$(GPROF_CFLAGS) diff --git a/qemu-options.hx b/qemu-options.hx index 82e085a..e2acbf4 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -2328,11 +2328,11 @@ STEXI 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) + "-semihosting semihosting mode\n", QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA) STEXI @item -semihosting @findex -semihosting -Semihosting mode (ARM, M68K only). +Semihosting mode (ARM, M68K, Xtensa only). ETEXI DEF("old-param", 0, QEMU_OPTION_old_param, "-old-param old param mode\n", QEMU_ARCH_ARM) diff --git a/target-xtensa/cpu.h b/target-xtensa/cpu.h index 793a6b5..55d81e9 100644 --- a/target-xtensa/cpu.h +++ b/target-xtensa/cpu.h @@ -226,6 +226,7 @@ int cpu_xtensa_signal_handler(int host_signum, void *pinfo, void *puc); void xtensa_cpu_list(FILE *f, fprintf_function cpu_fprintf); void xtensa_sync_window_from_phys(CPUState *env); void xtensa_sync_phys_from_window(CPUState *env); +void simcall(CPUState *env); static inline int xtensa_option_enabled(const XtensaConfig *config, int opt) { diff --git a/target-xtensa/helpers.h b/target-xtensa/helpers.h index 7babf73..87c7cc5 100644 --- a/target-xtensa/helpers.h +++ b/target-xtensa/helpers.h @@ -13,6 +13,7 @@ DEF_HELPER_2(window_check, void, i32, i32) DEF_HELPER_0(restore_owb, void) DEF_HELPER_1(movsp, void, i32) DEF_HELPER_1(wsr_lend, void, i32) +DEF_HELPER_0(simcall, void) DEF_HELPER_0(dump_state, void) #include "def-helper.h" diff --git a/target-xtensa/op_helper.c b/target-xtensa/op_helper.c index 3a0fa01..b170dbe 100644 --- a/target-xtensa/op_helper.c +++ b/target-xtensa/op_helper.c @@ -291,6 +291,11 @@ void HELPER(wsr_lend)(uint32_t v) } } +void HELPER(simcall)(void) +{ + simcall(env); +} + void HELPER(dump_state)(void) { cpu_dump_state(env, stderr, fprintf, 0); diff --git a/target-xtensa/translate.c b/target-xtensa/translate.c index 6e66f3f..b40218d 100644 --- a/target-xtensa/translate.c +++ b/target-xtensa/translate.c @@ -35,6 +35,7 @@ #include "disas.h" #include "tcg-op.h" #include "qemu-log.h" +#include "sysemu.h" #include "helpers.h" #define GEN_HELPER 1 @@ -675,7 +676,12 @@ static void disas_xtensa_insn(DisasContext *dc) break; case 1: /*SIMCALL*/ - TBD(); + if (semihosting_enabled) { + gen_helper_simcall(); + } else { + qemu_log("SIMCALL but semihosting is disabled\n"); + gen_exception_cause(dc, ILLEGAL_INSTRUCTION_CAUSE); + } break; default: diff --git a/xtensa-semi.c b/xtensa-semi.c new file mode 100644 index 0000000..2ce6cc1 --- /dev/null +++ b/xtensa-semi.c @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Open Source and Linux Lab nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <errno.h> +#include <unistd.h> +#include <string.h> +#include <stddef.h> +#include "exec.h" +#include "qemu-log.h" + +enum { + TARGET_SYS_exit = 1, + TARGET_SYS_read = 3, + TARGET_SYS_write = 4, + TARGET_SYS_open = 5, + TARGET_SYS_close = 6, + + TARGET_SYS_argc = 1000, + TARGET_SYS_argv_sz = 1001, + TARGET_SYS_argv = 1002, + TARGET_SYS_memset = 1004, +}; + +void simcall(CPUState *env) +{ + uint32_t *regs = env->regs; + + switch (regs[2]) { + case TARGET_SYS_exit: + qemu_log("exit(%d) simcall\n", regs[3]); + exit(regs[3]); + break; + + case TARGET_SYS_read: + { + target_phys_addr_t len = regs[5]; + void *buf = cpu_physical_memory_map(regs[4], &len, 1); + + if (buf) { + regs[2] = read(regs[3], buf, len); + regs[3] = errno; + cpu_physical_memory_unmap(buf, len, 1, len); + } else { + regs[2] = -1; + regs[3] = EINVAL; + } + } + break; + + case TARGET_SYS_write: + { + target_phys_addr_t len = regs[5]; + void *buf = cpu_physical_memory_map(regs[4], &len, 0); + + if (buf) { + regs[2] = write(regs[3], buf, len); + regs[3] = errno; + cpu_physical_memory_unmap(buf, len, 0, len); + } else { + regs[2] = -1; + regs[3] = EINVAL; + } + } + break; + + case TARGET_SYS_open: + { + char name[1024]; + int rc; + int i; + + for (i = 0; i < ARRAY_SIZE(name); ++i) { + rc = cpu_memory_rw_debug( + env, regs[3] + i, (uint8_t *)name + i, 1, 0); + if (rc != 0 || name[i] == 0) { + break; + } + } + + if (rc == 0 && i < ARRAY_SIZE(name)) { + regs[2] = open(name, regs[4], regs[5]); + regs[3] = errno; + } else { + regs[2] = -1; + regs[3] = EINVAL; + } + } + break; + + case TARGET_SYS_close: + if (regs[3] < 3) { + regs[2] = regs[3] = 0; + } else { + regs[2] = close(regs[3]); + regs[3] = errno; + } + break; + + case TARGET_SYS_argc: + regs[2] = 1; + regs[3] = 0; + break; + + case TARGET_SYS_argv_sz: + regs[2] = 128; + regs[3] = 0; + break; + + case TARGET_SYS_argv: + { + struct Argv { + uint32_t argptr[2]; + char text[120]; + } argv = { + {0, 0}, + "test" + }; + + argv.argptr[0] = tswap32(regs[3] + offsetof(struct Argv, text)); + cpu_memory_rw_debug( + env, regs[3], (uint8_t *)&argv, sizeof(argv), 1); + } + break; + + case TARGET_SYS_memset: + { + uint32_t base = regs[3]; + uint32_t sz = regs[5]; + + while (sz) { + target_phys_addr_t len = sz; + void *buf = cpu_physical_memory_map(base, &len, 1); + + if (buf && len) { + memset(buf, regs[4], len); + cpu_physical_memory_unmap(buf, len, 1, len); + } else { + len = 1; + } + base += len; + sz -= len; + } + regs[2] = regs[3]; + regs[3] = 0; + } + break; + + default: + qemu_log("%s(%d): not implemented\n", __func__, regs[2]); + break; + } +} -- 1.7.3.4