Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user task exit by exception #226

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions arch/risc-v/src/common/riscv_exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <nuttx/irq.h>
#include <nuttx/arch.h>

#include "sched/sched.h"
#include "riscv_internal.h"
#include "chip.h"

Expand Down Expand Up @@ -72,17 +73,43 @@ static const char *g_reasons_str[RISCV_MAX_EXCEPTION + 1] =

int riscv_exception(int mcause, void *regs, void *args)
{
#ifdef CONFIG_ARCH_KERNEL_STACK
pussuw marked this conversation as resolved.
Show resolved Hide resolved
FAR struct tcb_s *tcb = this_task();
#endif
uintptr_t cause = mcause & RISCV_IRQ_MASK;

_alert("EXCEPTION: %s. MCAUSE: %" PRIxREG ", EPC: %" PRIxREG
", MTVAL: %" PRIxREG "\n",
mcause > RISCV_MAX_EXCEPTION ? "Unknown" : g_reasons_str[cause],
cause, READ_CSR(CSR_EPC), READ_CSR(CSR_TVAL));

_alert("PANIC!!! Exception = %" PRIxREG "\n", cause);
up_irq_save();
CURRENT_REGS = regs;
PANIC_WITH_REGS("panic", regs);
#ifdef CONFIG_ARCH_KERNEL_STACK
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL)
pussuw marked this conversation as resolved.
Show resolved Hide resolved
{
pussuw marked this conversation as resolved.
Show resolved Hide resolved
_alert("Segmentation fault in PID %d: %s\n", tcb->pid, tcb->name);

tcb->flags |= TCB_FLAG_FORCED_CANCEL;

/* Return to _exit function in privileged mode with argument SIGSEGV */

CURRENT_REGS[REG_EPC] = (uintptr_t)_exit;
CURRENT_REGS[REG_A0] = SIGSEGV;
CURRENT_REGS[REG_INT_CTX] |= STATUS_PPP;

/* Continue with kernel stack in use. The frame(s) in kernel stack
* are no longer needed, so just set it to top
*/

CURRENT_REGS[REG_SP] = (uintptr_t)tcb->xcp.ktopstk;
}
else
#endif
{
_alert("PANIC!!! Exception = %" PRIxREG "\n", cause);
up_irq_save();
CURRENT_REGS = regs;
PANIC_WITH_REGS("panic", regs);
}

return 0;
}
Expand Down
7 changes: 7 additions & 0 deletions arch/risc-v/src/common/supervisor/riscv_perform_syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <nuttx/addrenv.h>

#include "sched/sched.h"
#include "riscv_internal.h"

/****************************************************************************
Expand Down Expand Up @@ -59,6 +60,12 @@ void *riscv_perform_syscall(uintptr_t *regs)

if (regs != CURRENT_REGS)
{
/* Record the new "running" task. g_running_tasks[] is only used by
* assertion logic for reporting crashes.
*/

g_running_tasks[this_cpu()] = this_task();

/* Restore the cpu lock */

restore_critical_section();
Expand Down
34 changes: 19 additions & 15 deletions sched/group/group_killchildren.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,29 +181,33 @@ int group_kill_children(FAR struct tcb_s *tcb)

#if defined(CONFIG_GROUP_KILL_CHILDREN_TIMEOUT_MS) && \
CONFIG_GROUP_KILL_CHILDREN_TIMEOUT_MS != 0
/* Send SIGTERM for each first */

group_foreachchild(tcb->group, group_kill_children_handler,
(FAR void *)((uintptr_t)tcb->pid));
if ((tcb->flags & TCB_FLAG_FORCED_CANCEL) == 0)
{
/* Send SIGTERM for each first */

/* Wait a bit for child exit */
group_foreachchild(tcb->group, group_kill_children_handler,
(FAR void *)((uintptr_t)tcb->pid));

ret = CONFIG_GROUP_KILL_CHILDREN_TIMEOUT_MS;
while (1)
{
if (tcb->group->tg_nmembers <= 1)
/* Wait a bit for child exit */

ret = CONFIG_GROUP_KILL_CHILDREN_TIMEOUT_MS;
while (1)
{
break;
}
if (tcb->group->tg_nmembers <= 1)
{
break;
}

nxsig_usleep(USEC_PER_MSEC);
nxsig_usleep(USEC_PER_MSEC);

# if CONFIG_GROUP_KILL_CHILDREN_TIMEOUT_MS > 0
if (--ret < 0)
{
break;
}
if (--ret < 0)
{
break;
}
# endif
}
}
#endif

Expand Down
Loading