aboutsummaryrefslogtreecommitdiff
path: root/common/uapi/proc.c
blob: ea42a3a5c8d7ecf16afce0f59c5342efb8c5482a (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright 2021 - 2022, Kim Kuparinen < kimi.h.kuparinen@gmail.com > */

/**
 * @file proc.c
 * Process/thread handling syscall implementations.
 */

#include <apos/elf.h>
#include <apos/uapi.h>
#include <apos/proc.h>
#include <apos/bits.h>
#include <apos/mem_regions.h>

#include <arch/proc.h>

/**
 * Create syscall handler.
 *
 * \todo Implement.
 *
 * @param func Function to jump to at thread creation.
 * @param arg Argument to pass to the function.
 *
 * @return \ref OK and 0.
 */
SYSCALL_DEFINE5(create)(sys_arg_t func,
		sys_arg_t d0, sys_arg_t d1, sys_arg_t d2, sys_arg_t d3){
	return SYS_RET1(OK);
}

/**
 * Fork syscall handler.
 *
 * \todo Not entirely sure how I should handle forks/execs etc, mostly whether I
 * should allow forks/execs to be called directly or only though the process
 * manager. Probably though the process manager, although that will add in a
 * slight bit of delay.
 *
 * I suppose I could add in a runtime parameter
 * that would allow them to be called directly, and then the process manager
 * would have to periodically ask the kernel about all threads it is aware of
 * via sys_sync. Dunno.
 *
 * @return \ref OK and 0.
 */
SYSCALL_DEFINE0(fork)(){
	struct tcb *t = create_proc(cur_proc());
	if (!t)
		return SYS_RET1(ERR_OOMEM);

	/* prepare args for when we eventually swap to the new proc */
	set_args(t, SYS_RET2(OK, t->pid));

	return SYS_RET2(OK, 0);
}

/**
 * Exec syscall handler.
 *
 * @param bin Binary to execute.
 * @param interp Optional interpreter binary.
 * @return \see prepare_proc().
 */
SYSCALL_DEFINE2(exec)(sys_arg_t bin, sys_arg_t interp){
	/** \todo execute new process, probably with more sensible argc passing */
	struct tcb *r = cur_tcb();

	/* mark binary to be kept */
	struct mem_region *b = find_used_region(&r->sp_r, bin);
	if (!b)
		return SYS_RET1(ERR_INVAL);

	set_bit(b->flags, MR_KEEP);

	struct mem_region *i = 0;
	if (interp) {
		/* mark interpreter to be kept */
		i = find_used_region(&r->sp_r, interp);
		if (!i)
			return SYS_RET1(ERR_INVAL);

		set_bit(i->flags, MR_KEEP);
	}

	/* free everything except regions to be kept */
	clear_uvmem(r);

	/* restore to normal */
	clear_bit(b->flags, MR_KEEP);
	if (interp)
		clear_bit(b->flags, MR_KEEP);

	return SYS_RET1(prepare_proc(r, bin, interp));
}

/**
 * Spawn syscall handler.
 *
 * @param bin Binary to execute.
 * @param interp Optional interpreter binary.
 * @return \see prepare_proc() and process id of the new process.
 */
SYSCALL_DEFINE2(spawn)(sys_arg_t bin, sys_arg_t interp)
{
	struct tcb *t = create_proc(NULL);
	if (!t)
		return SYS_RET1(ERR_OOMEM);

	return SYS_RET2(prepare_proc(t, bin, interp), t->pid);
}

/**
 * Kill syscall handler.
 *
 * @param tid Thread to kill.
 * \todo Implement.
 *
 * @return ERR_PERM if not capable to kill, otherwise OK.
 */
SYSCALL_DEFINE1(kill)(sys_arg_t tid)
{
	return SYS_RET1(OK);
}

/**
 * Swap syscall handler.
 *
 * \todo Implement.
 * \todo Should swap return the registers of the new thread that would be used
 * for message passing?
 *
 * @param tid Thread ID to swap to.
 * @return \ref OK.
 */
SYSCALL_DEFINE1(swap)(sys_arg_t tid){
	struct tcb *t = get_tcb(tid);
	if (!t)
		return SYS_RET1(ERR_INVAL);

	/* switch over to new thread */
	use_tcb(t);

	return get_args(t);
}