Skip to content

Commit

Permalink
semihosting: Refactored out the SEMIHOSTING_SYS_EXIT* logic into a ne…
Browse files Browse the repository at this point in the history
…w function
  • Loading branch information
dragonmux committed Jan 14, 2024
1 parent 67e1d19 commit a4bd7e5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/target/semihosting.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,16 @@ int32_t semihosting_readc(target_s *const target)
#endif
}

int32_t semihosting_exit(target_s *const target, const semihosting_exit_reason_e reason, const uint32_t status_code)
{
if (reason == EXIT_REASON_APPLICATION_EXIT)
tc_printf(target, "exit(%" PRIu32 ")\n", status_code);
else
tc_printf(target, "Exception trapped: %" PRIx32 " (%" PRIu32 ")\n", reason, status_code);
target_halt_resume(target, true);
return 0;
}

int32_t semihosting_get_command_line(target_s *const target, const semihosting_s *const request)
{
/* Extract the location of the result buffer and its length */
Expand Down Expand Up @@ -745,15 +755,11 @@ int32_t semihosting_request(target_s *const target, const uint32_t syscall, cons
return target->tc->gdb_errno;
#endif

case SEMIHOSTING_SYS_EXIT: /* _exit() */
tc_printf(target, "_exit(0x%x)\n", request.r1);
target_halt_resume(target, 1);
break;
case SEMIHOSTING_SYS_EXIT: /* exit */
return semihosting_exit(target, request.r1, 0);

case SEMIHOSTING_SYS_EXIT_EXTENDED: /* _exit() */
tc_printf(target, "_exit(0x%x%08x)\n", request.params[1], request.params[0]); /* exit() with 64bit exit value */
target_halt_resume(target, 1);
break;
case SEMIHOSTING_SYS_EXIT_EXTENDED: /* exit extended */
return semihosting_exit(target, request.params[0], request.params[1]);

case SEMIHOSTING_SYS_GET_CMDLINE: /* get_cmdline */
return semihosting_get_command_line(target, &request);
Expand Down
24 changes: 24 additions & 0 deletions src/target/semihosting_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,28 @@ typedef struct semihosting_time {
uint32_t seconds;
} semihosting_time_s;

typedef enum semihosting_exit_reason {
/* Hardware exceptions */
EXIT_REASON_BRANCH_THROUGH_ZERO = 0x20000U,
EXIT_REASON_UNDEFINED_INSN = 0x20001U,
EXIT_REASON_SOFTWARE_INTERRUPT = 0x20002U,
EXIT_REASON_PREFETCH_ABORT = 0x20003U,
EXIT_REASON_DATA_ABORT = 0x20004U,
EXIT_REASON_ADDRESS_EXCEPTION = 0x20005U,
EXIT_REASON_IRQ = 0x20006U,
EXIT_REASON_FIQ = 0x20007U,

/* Software reasons */
EXIT_REASON_BREAKPOINT = 0x20020U,
EXIT_REASON_WATCHPOINT = 0x20021U,
EXIT_REASON_STEP_COMPLETE = 0x20022U,
EXIT_REASON_RUNTIME_ERROR_UNKNOWN = 0x20023U,
EXIT_REASON_INTERNAL_ERROR = 0x20024U,
EXIT_REASON_USER_INTERRUPTION = 0x20025U,
EXIT_REASON_APPLICATION_EXIT = 0x20026U,
EXIT_REASON_STACK_OVERFLOW = 0x20027U,
EXIT_REASON_DIVIDE_BY_ZERO = 0x20028U,
EXIT_REASON_OS_SPECIFIC = 0x20029U,
} semihosting_exit_reason_e;

#endif /* TARGET_SEMIHOSTING_INTERNAL_H */

0 comments on commit a4bd7e5

Please sign in to comment.