target-arm/arm-semi.c: Factor out repeated 'return env->regs[0]'

Factor out a repeated pattern in the semihosting code:

    gdb_do_syscall(arm_semi_cb, "system,%s", arg0, (int)arg1+1);
    /* arm_semi_cb sets env->regs[0] to the syscall return value */
    return env->regs[0];

For A64 the return value will go in a different register; pull
the sequence out into its own function that passes the return
value in a static variable rather than overloading regs[0]
for the purpose, so the code will work on both A32/T32 and A64.

Note that the lack-of-synchronization bug noted in the FIXME
comment is not introduced by this commit, but was already present.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Christopher Covington <christopher.covington@linaro.org>
Tested-by: Christopher Covington <cov@codeaurora.org>
Message-id: 1439483745-28752-5-git-send-email-peter.maydell@linaro.org
This commit is contained in:
Peter Maydell 2015-09-07 10:39:27 +01:00
parent 19239b39e7
commit bb19cbc95a
1 changed files with 47 additions and 32 deletions

View File

@ -134,6 +134,7 @@ static void arm_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
#ifdef CONFIG_USER_ONLY #ifdef CONFIG_USER_ONLY
TaskState *ts = cs->opaque; TaskState *ts = cs->opaque;
#endif #endif
target_ulong reg0 = env->regs[0];
if (ret == (target_ulong)-1) { if (ret == (target_ulong)-1) {
#ifdef CONFIG_USER_ONLY #ifdef CONFIG_USER_ONLY
@ -141,22 +142,23 @@ static void arm_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
#else #else
syscall_err = err; syscall_err = err;
#endif #endif
env->regs[0] = ret; reg0 = ret;
} else { } else {
/* Fixup syscalls that use nonstardard return conventions. */ /* Fixup syscalls that use nonstardard return conventions. */
switch (env->regs[0]) { switch (reg0) {
case TARGET_SYS_WRITE: case TARGET_SYS_WRITE:
case TARGET_SYS_READ: case TARGET_SYS_READ:
env->regs[0] = arm_semi_syscall_len - ret; reg0 = arm_semi_syscall_len - ret;
break; break;
case TARGET_SYS_SEEK: case TARGET_SYS_SEEK:
env->regs[0] = 0; reg0 = 0;
break; break;
default: default:
env->regs[0] = ret; reg0 = ret;
break; break;
} }
} }
env->regs[0] = reg0;
} }
static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err) static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err)
@ -175,6 +177,25 @@ static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err)
#endif #endif
} }
static target_ulong arm_gdb_syscall(ARMCPU *cpu, gdb_syscall_complete_cb cb,
const char *fmt, ...)
{
va_list va;
CPUARMState *env = &cpu->env;
va_start(va, fmt);
gdb_do_syscallv(cb, fmt, va);
va_end(va);
/* FIXME: we are implicitly relying on the syscall completing
* before this point, which is not guaranteed. We should
* put in an explicit synchronization between this and
* the callback function.
*/
return env->regs[0];
}
/* Read the input value from the argument block; fail the semihosting /* Read the input value from the argument block; fail the semihosting
* call if the memory read fails. * call if the memory read fails.
*/ */
@ -223,9 +244,8 @@ uint32_t do_arm_semihosting(CPUARMState *env)
return result_fileno; return result_fileno;
} }
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", arg0, ret = arm_gdb_syscall(cpu, arm_semi_cb, "open,%s,%x,1a4", arg0,
(int)arg2+1, gdb_open_modeflags[arg1]); (int)arg2+1, gdb_open_modeflags[arg1]);
ret = env->regs[0];
} else { } else {
ret = set_swi_errno(ts, open(s, open_modeflags[arg1], 0644)); ret = set_swi_errno(ts, open(s, open_modeflags[arg1], 0644));
} }
@ -234,8 +254,7 @@ uint32_t do_arm_semihosting(CPUARMState *env)
case TARGET_SYS_CLOSE: case TARGET_SYS_CLOSE:
GET_ARG(0); GET_ARG(0);
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
gdb_do_syscall(arm_semi_cb, "close,%x", arg0); return arm_gdb_syscall(cpu, arm_semi_cb, "close,%x", arg0);
return env->regs[0];
} else { } else {
return set_swi_errno(ts, close(arg0)); return set_swi_errno(ts, close(arg0));
} }
@ -248,8 +267,7 @@ uint32_t do_arm_semihosting(CPUARMState *env)
return (uint32_t)-1; return (uint32_t)-1;
/* Write to debug console. stderr is near enough. */ /* Write to debug console. stderr is near enough. */
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args); return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,1", args);
return env->regs[0];
} else { } else {
return write(STDERR_FILENO, &c, 1); return write(STDERR_FILENO, &c, 1);
} }
@ -260,8 +278,8 @@ uint32_t do_arm_semihosting(CPUARMState *env)
return (uint32_t)-1; return (uint32_t)-1;
len = strlen(s); len = strlen(s);
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
gdb_do_syscall(arm_semi_cb, "write,2,%x,%x", args, len); return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,%x",
ret = env->regs[0]; args, len);
} else { } else {
ret = write(STDERR_FILENO, s, len); ret = write(STDERR_FILENO, s, len);
} }
@ -274,8 +292,8 @@ uint32_t do_arm_semihosting(CPUARMState *env)
len = arg2; len = arg2;
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
arm_semi_syscall_len = len; arm_semi_syscall_len = len;
gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", arg0, arg1, len); return arm_gdb_syscall(cpu, arm_semi_cb, "write,%x,%x,%x",
return env->regs[0]; arg0, arg1, len);
} else { } else {
s = lock_user(VERIFY_READ, arg1, len, 1); s = lock_user(VERIFY_READ, arg1, len, 1);
if (!s) { if (!s) {
@ -295,8 +313,8 @@ uint32_t do_arm_semihosting(CPUARMState *env)
len = arg2; len = arg2;
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
arm_semi_syscall_len = len; arm_semi_syscall_len = len;
gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", arg0, arg1, len); return arm_gdb_syscall(cpu, arm_semi_cb, "read,%x,%x,%x",
return env->regs[0]; arg0, arg1, len);
} else { } else {
s = lock_user(VERIFY_WRITE, arg1, len, 0); s = lock_user(VERIFY_WRITE, arg1, len, 0);
if (!s) { if (!s) {
@ -317,8 +335,7 @@ uint32_t do_arm_semihosting(CPUARMState *env)
case TARGET_SYS_ISTTY: case TARGET_SYS_ISTTY:
GET_ARG(0); GET_ARG(0);
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
gdb_do_syscall(arm_semi_cb, "isatty,%x", arg0); return arm_gdb_syscall(cpu, arm_semi_cb, "isatty,%x", arg0);
return env->regs[0];
} else { } else {
return isatty(arg0); return isatty(arg0);
} }
@ -326,8 +343,8 @@ uint32_t do_arm_semihosting(CPUARMState *env)
GET_ARG(0); GET_ARG(0);
GET_ARG(1); GET_ARG(1);
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
gdb_do_syscall(arm_semi_cb, "lseek,%x,%x,0", arg0, arg1); return arm_gdb_syscall(cpu, arm_semi_cb, "lseek,%x,%x,0",
return env->regs[0]; arg0, arg1);
} else { } else {
ret = set_swi_errno(ts, lseek(arg0, arg1, SEEK_SET)); ret = set_swi_errno(ts, lseek(arg0, arg1, SEEK_SET));
if (ret == (uint32_t)-1) if (ret == (uint32_t)-1)
@ -337,9 +354,8 @@ uint32_t do_arm_semihosting(CPUARMState *env)
case TARGET_SYS_FLEN: case TARGET_SYS_FLEN:
GET_ARG(0); GET_ARG(0);
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x", return arm_gdb_syscall(cpu, arm_semi_flen_cb, "fstat,%x,%x",
arg0, env->regs[13]-64); arg0, env->regs[13]-64);
return env->regs[0];
} else { } else {
struct stat buf; struct stat buf;
ret = set_swi_errno(ts, fstat(arg0, &buf)); ret = set_swi_errno(ts, fstat(arg0, &buf));
@ -354,8 +370,8 @@ uint32_t do_arm_semihosting(CPUARMState *env)
GET_ARG(0); GET_ARG(0);
GET_ARG(1); GET_ARG(1);
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
gdb_do_syscall(arm_semi_cb, "unlink,%s", arg0, (int)arg1+1); ret = arm_gdb_syscall(cpu, arm_semi_cb, "unlink,%s",
ret = env->regs[0]; arg0, (int)arg1+1);
} else { } else {
s = lock_user_string(arg0); s = lock_user_string(arg0);
if (!s) { if (!s) {
@ -372,9 +388,8 @@ uint32_t do_arm_semihosting(CPUARMState *env)
GET_ARG(2); GET_ARG(2);
GET_ARG(3); GET_ARG(3);
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
gdb_do_syscall(arm_semi_cb, "rename,%s,%s", return arm_gdb_syscall(cpu, arm_semi_cb, "rename,%s,%s",
arg0, (int)arg1+1, arg2, (int)arg3+1); arg0, (int)arg1+1, arg2, (int)arg3+1);
return env->regs[0];
} else { } else {
char *s2; char *s2;
s = lock_user_string(arg0); s = lock_user_string(arg0);
@ -398,8 +413,8 @@ uint32_t do_arm_semihosting(CPUARMState *env)
GET_ARG(0); GET_ARG(0);
GET_ARG(1); GET_ARG(1);
if (use_gdb_syscalls()) { if (use_gdb_syscalls()) {
gdb_do_syscall(arm_semi_cb, "system,%s", arg0, (int)arg1+1); return arm_gdb_syscall(cpu, arm_semi_cb, "system,%s",
return env->regs[0]; arg0, (int)arg1+1);
} else { } else {
s = lock_user_string(arg0); s = lock_user_string(arg0);
if (!s) { if (!s) {