Skip to content

Commit 641a11d

Browse files
committed
Ability to kill whole process branch
Useful to abort the whole process context in case of an fatal error. Child processes (non-thread) which use a different context are not killed (those get taken up by the root process) but all thread processes are killed.
1 parent 93741d8 commit 641a11d

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

include/process.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ typedef struct KProcessInfo {
7070
void kprocess_init();
7171
INT kprocess_create (void* processStartAddress, SIZE binLengthBytes, KProcessFlags flags);
7272
bool kprocess_yield (ProcessRegisterState* currentState);
73-
bool kprocess_exit (U8 exitCode);
73+
bool kprocess_exit (U8 exitCode, bool killBranch);
7474
VMemoryManager* kprocess_getCurrentContext();
7575
UINT kprocess_getCurrentPID();
7676
bool kprocess_popEvent (UINT pid, KProcessEvent* ev);

src/kernel/x86/process.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -688,13 +688,22 @@ bool kprocess_yield (ProcessRegisterState* currentState)
688688
return s_switchProcess (pinfo, currentState);
689689
}
690690

691-
bool kprocess_exit (U8 exitCode)
691+
bool kprocess_exit (U8 exitCode, bool killBranch)
692692
{
693-
FUNC_ENTRY ("Exit Code: %x", exitCode);
693+
FUNC_ENTRY ("Exit Code: %x, killBranch: %x", exitCode, killBranch);
694+
695+
k_assert(currentProcess != NULL, "Nothing to exit");
694696

695697
UINT ret;
696-
UINT flags = (currentProcess != NULL) ? currentProcess->flags : 0;
698+
UINT flags = currentProcess->flags;
697699
UINT l_exitCode = (UINT)exitCode;
700+
KProcessInfo* process = currentProcess;
701+
702+
// If parent of the current process is NULL, then the current process is the root process
703+
// and branch head. Otherwise its the parent of the current process.
704+
if (killBranch && currentProcess->parent != NULL) {
705+
process = currentProcess->parent;
706+
}
698707

699708
// clang-format off
700709
// In a Kernel process, no stack switch happens in a system call and the same process stack is
@@ -721,10 +730,9 @@ bool kprocess_exit (U8 exitCode)
721730
//////////////////////////////////////////////////////////////////////////////
722731
".cont:;"
723732
//////////////////////////////////////////////////////////////////////////////
724-
// Call to kprocess_kill_process to kill 'currentProcess'.
733+
// Call to kprocess_kill_process to kill 'current process'.
725734
" push %2;" // exit code
726-
" lea eax, [currentProcess];"
727-
" push eax;" // &currentProcess
735+
" push %3;"
728736
" call kprocess_kill_process;"
729737
" add esp, 16;"
730738
//////////////////////////////////////////////////////////////////////////////
@@ -734,6 +742,7 @@ bool kprocess_exit (U8 exitCode)
734742
//////////////////////////////////////////////////////////////////////////////
735743
// Call to kprocess_yield to go to the next process now that the current one is
736744
// destoryed.
745+
" mov DWORD PTR [currentProcess], 0;"
737746
" xor eax, eax;" // NULL is passed in the first argument of yeld.
738747
" push eax;"
739748
" call kprocess_yield;"
@@ -747,8 +756,8 @@ bool kprocess_exit (U8 exitCode)
747756
" pop ebp;"
748757
//////////////////////////////////////////////////////////////////////////////
749758
: "=r"(ret)
750-
: "r"(flags), "r"(l_exitCode)
751-
: // No clobber
759+
: "r"(flags), "r"(l_exitCode), "r"(&process)
760+
: "memory"
752761
);
753762
// clang-format on
754763

src/kernel/x86/syscalls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void ksys_killProcess (SystemcallFrame frame, UINT exitCode)
274274
{
275275
FUNC_ENTRY ("Frame return address: %x:%x, exit code: %x", frame.cs, frame.eip, exitCode);
276276
(void)frame;
277-
kprocess_exit ((U8)exitCode);
277+
kprocess_exit ((U8)exitCode, false);
278278
}
279279

280280
U32 ksys_process_getPID (SystemcallFrame frame)

0 commit comments

Comments
 (0)