aboutsummaryrefslogtreecommitdiff
path: root/src/orphanage.c
blob: 7415e8b967fc63d0893ba4540b66af03682fe53d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2024, Kim Kuparinen < kimi.h.kuparinen@gmail.com > */

#include <kmi/assert.h>
#include <kmi/orphanage.h>

#include <arch/proc.h>

/**
 * @file orphanage.c
 *
 * Stuff related to orphaned threads implementation.
 */

bool orphan(struct tcb *t)
{
	struct tcb *r = get_rproc(t);
	return !r || r->dead;
}

void orphanize(struct tcb *t)
{
	catastrophic_assert(!is_rpc(t));

	struct tcb *r = get_tcb(t->rid);
	if (r)
		unreference_proc(r);

	/* attach to init process */
	struct tcb *init = get_tcb(1);
	reference_proc(init);

	t->rid = 1;
	t->pid = 1;
	t->eid = 1;

	t->proc = init->proc;
	use_vmem(t->proc.vmem);

	catastrophic_assert(init->callback);
	set_args3(t, 0, SYS_USER_ORPHANED, t->tid);
	set_return(t, init->callback);
	t->callback = init->callback;

	/** @todo release irqs, here or later? */

	ret_userspace_fast();
	unreachable();
}