aboutsummaryrefslogtreecommitdiff
path: root/src/uapi/ipc.c
blob: 613d4a994466e8fdf31923378e885470740fe488 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2021 - 2022, Kim Kuparinen < kimi.h.kuparinen@gmail.com > */

/**
 * @file ipc.c
 * Interprocess communication syscall implementations.
 */

#include <kmi/orphanage.h>
#include <kmi/debug.h>
#include <kmi/uapi.h>
#include <kmi/bkl.h>
#include <kmi/tcb.h>
#include <kmi/ipi.h>
#include <kmi/irq.h>
#include <kmi/conf.h>

/** Structure for maintaining the required context data for an rpc call. */
struct call_ctx {
	/** Execution continuation point. */
	vm_t exec;

	/** Register save area. */
	vm_t regs;

	/** Position in rpc stack. */
	vm_t rpc_stack;

	/** Effective process ID. */
	id_t eid;

	/** Current process ID. */
	id_t pid;
};

/**
 * Represents difference between where rpc stack was before rpc call and during.
 * Used to figure out which areas should be marked inaccessible.
 */
struct stack_diff {
	/** Current stack start. Keep in mind that stacks grow down. */
	vm_t start;
	/** Difference end, i.e. previous stack start. */
	vm_t end;
};

/** Enumerator for IPC kind. Used by do_ipc(). */
enum ipc_kind {
	IPC_REQ, IPC_FWD, IPC_KICK
};

/**
 * Now that we know we're doing an rpc, clone virtual memories and do
 * the slow stuff.
 *
 * @param t Thread to migrate.
 * @param r Process to migrate to.
 * @param s RPC stack regions to mark inaccessible.
 */
static void finalize_rpc(struct tcb *t, struct tcb *r, vm_t s)
{
	clone_uvmem(r->proc.vmem, t->rpc.vmem);
	set_return(t, r->callback);
	reference_proc(r);
	t->pid = r->rid;

	/* make sure updates are visible when swapping to the new virtual memory */
	mark_rpc_invalid(t, s);
	use_vmem(t->rpc.vmem);
}

/**
 * Optimistically assume we're going to take the rpc and do some preparations
 * for it. Most notably, write the arguments as early as possible to
 * free up registers for the compiler to play with.
 *
 * @param t Thread to migrate.
 * @param a RPC arguments.
 * @param kind Kind of IPC we're doing. Essentially toggles kick boolean.
 * @return RPC stack difference that should be passed to finalize_rpc().
 */
static vm_t enter_rpc(struct tcb *t, struct sys_ret a,
                      enum ipc_kind kind)
{
	/* reuse current rpc stack location if we're being kicked */
	vm_t rpc_stack = (kind == IPC_KICK &&
	                  is_rpc(t)) ? t->rpc_stack : rpc_position(t);

	struct call_ctx *ctx = (struct call_ctx *)(rpc_stack) - 1;
	ctx->regs = t->regs;
	t->regs = (vm_t)ctx;

	/* try to get rid of args as fast as possible to free up registers for
	 * later use */
	set_args(t, 6, a);

	ctx->exec = t->exec;
	ctx->pid = t->pid;
	ctx->eid = t->eid;
	ctx->rpc_stack = rpc_stack;

	/** @todo if we run out of rpc_stack space we should just stop, likely
	 * return a status? except it shouldn't happen after we've run
	 * enough_rpc_stack(). */
	vm_t new_stack = rpc_stack - BASE_PAGE_SIZE;

	/** @todo what if each stack is only some number of pages, and if a proc
	 * goes over the limit is is seen as programming error? Possibly user
	 * configurable number as well, might actually use the config subsystem
	 * :D
	 * In such a case it would probably be smarter to mark all pages
	 * inaccessible at first, and then mark the first page accessible. If
	 * the process needs more stack space it'll cause a paging exception,
	 * we'll handle it separately and if the process isn't going over the
	 * limit just give it more.
	 * */
	t->rpc_stack = new_stack;
	set_stack(t, new_stack);
	return new_stack;
}

/**
 * Check that there's enough stack left for an rpc invocation.
 *
 * @param t Thread whose migration to check.
 * @return \c true if there's enough stack left to safely do migration,
 * \c false otherwise.
 */
static bool __enough_rpc_stack(struct tcb *t)
{
	/* get top of call stack */
	vm_t top = rpc_position(t);

	/* if we can still fit an rpc stack into the call stack, we can safely
	 * do the migration. */
	return top - BASE_PAGE_SIZE - __rpc_stack_size >= RPC_STACK_BASE;
}

/**
 * Actually run notification handler, no ifs or buts.
 *
 * @param t Current thread.
 * @param r Process where notification handler is.
 *
 * \p t and \p r may be the same thread.
 */
static __noreturn void __run_notify(struct tcb *t, struct tcb *r)
{
	use_tcb(r);

	enum sys_user code = SYS_USER_NOTIFY;
	enum notify_flag flags = 0;

	/* if we're in the root process, we can safely handle signals and
	 * becoming orphaned */
	if (!is_rpc(t))
		set_bits(flags,
		         t->notify_flags & (NOTIFY_SIGNAL | NOTIFY_ORPHANED));

	/* handle critical notifications with special care */
	if (is_set(t->notify_flags, NOTIFY_IRQ | NOTIFY_TIMER)) {
		set_bits(flags, t->notify_flags & (NOTIFY_IRQ | NOTIFY_TIMER));
		disable_irqs();
	}

	/* signal to whoever is receiving us that we're from the kernel
	 * ("pid 0"), and we are notifying the current thread */
	vm_t s = enter_rpc(t,
	                   SYS_RET5(0, code, flags, t->eid, t->tid),
	                   IPC_REQ);

	finalize_rpc(t, r, s);

	clear_bits(t->notify_flags, flags);

	bkl_unlock();
	ret_userspace_fast();
	unreachable();
}

void notify(struct tcb *t, enum notify_flag flag)
{
	set_bits(t->notify_flags, flag);

	if (!t->notify_flags)
		return;

	struct tcb *r = get_tcb(t->notify_id);
	if (!r || r->state || !r->callback) {
		error("notify callback unavailable\n");
		t->notify_flags = 0;
		return;
	}

	/* signals are only run when thread is in root process, whereas
	 * interrupts are always run (if the resources allow it, that its */
	if (!is_set(t->notify_flags, NOTIFY_IRQ | NOTIFY_TIMER) && is_rpc(t))
		return;

	/* we're either in the root process or we have a critical notification,
	 * check that there's enough rpc stack, otherwise wait and try again
	 * later */
	if (unlikely(!__enough_rpc_stack(r)))
		return;

	/* if someone else is running the thread we would like to send a
	 * notification to, do an ipi. Otherwise, either we're currently running
	 * it or the thread is idle, either is fine for __run_notify(). */
	if (running(r) && r != cur_tcb()) {
		send_ipi(r);
		return;
	}

	__run_notify(t, r);
}

/**
 * Jump back to process where rpc came from, assuming such a thing exists.
 * If we're queued for a notification, jump to it instead.
 *
 * @param t Thread to do return migration on.
 * @param a Arguments to pass along.
 */
static void leave_rpc(struct tcb *t, struct sys_ret a)
{
	vm_t rpc_stack = t->rpc_stack + BASE_PAGE_SIZE;
	struct call_ctx *ctx = (struct call_ctx *)(rpc_stack) - 1;

	t->regs = ctx->regs;
	/* again, get rid of args as fast as possible */
	set_args(t, 6, a);

	struct tcb *r = get_tcb(ctx->pid);
	while (!r || !is_proc(r) || zombie(r)) {
		/* we unwound back to our root process which is apparently dead,
		 * we're orphaned :( */
		if (rpc_stack_empty(ctx->rpc_stack)) {
			orphanize(t);
			break;
		}

		rpc_stack = ctx->rpc_stack + BASE_PAGE_SIZE;
		ctx = (struct call_ctx *)(rpc_stack) - 1;

		r = get_tcb(ctx->pid);
		set_args1(t, ERR_NF);
	}

	if (orphan(t) && !is_rpc(t))
		unorphanize(t);

	set_return(t, ctx->exec);
	/* if we're returning from a failed rpc, this should essentially be a
	 * no-op */
	mark_rpc_valid(t, ctx->rpc_stack);
	t->rpc_stack = ctx->rpc_stack;
	t->pid = ctx->pid;
	t->eid = ctx->eid;

	/* notification queued, try to run it */
	if (t->notify_flags)
		notify(t, 0);

	if (is_rpc(t)) {
		use_vmem(t->rpc.vmem);
		return;
	}

	/* notification didn't take, return back to process normally */
	use_vmem(t->proc.vmem);
}

/**
 * IPC server notification syscall handler.
 *
 * @param t Current tcb.
 * @param callback Address of server callback.
 * @return \ref OK and \c 0.
 */
SYSCALL_DEFINE1(ipc_server)(struct tcb *t, sys_arg_t callback)
{
	get_cproc(t)->callback = callback;
	return_args1(t, OK);
}

/**
 * Actual IPC syscall handler.
 *
 * @param t Current tcb.
 * @param pid Process to request RPC to.
 * @param d0 IPC argument 0.
 * @param d1 IPC argument 1.
 * @param d2 IPC argument 2.
 * @param d3 IPC argument 3.
 * @param kind Which kind of IPC to perform.
 *
 * Returns \ref ERR_OOMEM if there isn't enough IPC stack left, \ref ERR_INVAL
 * if the the target process doesn't exist, \ref ERR_NOINIT if the target
 * process hasn't defined a callback. Otherwise \ref OK and whatever the target
 * process sends back.
 *
 * @todo should all static functions have double underscores? I seem to be
 * inconsistent.
 */
static void do_ipc(struct tcb *t,
                   sys_arg_t pid,
                   sys_arg_t d0,
                   sys_arg_t d1,
                   sys_arg_t d2,
                   sys_arg_t d3,
                   enum ipc_kind kind)
{
	if (unlikely(!__enough_rpc_stack(t)))
		return_args1(t, ERR_OOMEM);

	vm_t s = enter_rpc(t, SYS_RET6(t->eid, t->tid, d0, d1, d2, d3), kind);

	struct tcb *r = get_tcb(pid);
	if (unlikely(!r || !is_proc(r))) {
		leave_rpc(t, SYS_RET1(ERR_INVAL));
		return;
	}

	if (unlikely(zombie(r))) {
		leave_rpc(t, SYS_RET1(ERR_INVAL));
		return;
	}

	if (unlikely(!r->callback)) {
		leave_rpc(t, SYS_RET1(ERR_NOINIT));
		return;
	}

	if (kind != IPC_REQ)
		t->eid = t->pid;

	finalize_rpc(t, r, s);
	/* I tested out passing the return values as arguments to
	 * ret_userspace_fast, but apparently that causes enough stack shuffling
	 * to be slower overall. */
	bkl_unlock();
	ret_userspace_fast();
}
/**
 * IPC request syscall handler.
 *
 * @param t Current tcb.
 * @param pid Process to request RPC to.
 * @param d0 IPC argument 0.
 * @param d1 IPC argument 1.
 * @param d2 IPC argument 2.
 * @param d3 IPC argument 3.
 * @return When succesful: OK, thread id of the caller and the arguments as-is.
 */
SYSCALL_DEFINE5(ipc_req)(struct tcb *t, sys_arg_t pid,
                         sys_arg_t d0, sys_arg_t d1, sys_arg_t d2, sys_arg_t d3)
{
	enable_irqs();
	do_ipc(t, pid, d0, d1, d2, d3, IPC_REQ);
}

/**
 * IPC forwarding syscall handler.
 *
 * @param t Current tcb.
 * @param pid Process to request RPC to.
 * @param d0 IPC argument 0.
 * @param d1 IPC argument 1.
 * @param d2 IPC argument 2.
 * @param d3 IPC argument 3.
 * @return When succesful: OK, thread id of the caller and the arguments as-is.
 */
SYSCALL_DEFINE5(ipc_fwd)(struct tcb *t, sys_arg_t pid,
                         sys_arg_t d0, sys_arg_t d1, sys_arg_t d2, sys_arg_t d3)
{
	enable_irqs();
	do_ipc(t, pid, d0, d1, d2, d3, IPC_FWD);
}

/**
 * IPC kicking syscall handler.
 *
 * @param t Current tcb.
 * @param pid Process to request RPC to.
 * @param d0 IPC argument 0.
 * @param d1 IPC argument 1.
 * @param d2 IPC argument 2.
 * @param d3 IPC argument 3.
 * @return When succesful: OK, thread id of the caller and the arguments as-is.
 */
SYSCALL_DEFINE5(ipc_kick)(struct tcb *t, sys_arg_t pid,
                          sys_arg_t d0, sys_arg_t d1, sys_arg_t d2,
                          sys_arg_t d3)
{
	enable_irqs();
	do_ipc(t, pid, d0, d1, d2, d3, IPC_KICK);
}

/**
 * IPC response syscall handler.
 *
 * @param t Current tcb.
 * @param d0 IPC return value 0.
 * @param d1 IPC return value 1.
 * @param d2 IPC return value 2.
 * @param d3 IPC return value 3.
 * @return \c d0 and \c d1.
 */
SYSCALL_DEFINE4(ipc_resp)(struct tcb *t, sys_arg_t d0, sys_arg_t d1,
                          sys_arg_t d2,
                          sys_arg_t d3)
{
	/* if we're not in an rpc, the user messed something up. */
	/** @todo choose or come up with more fitting error value. */
	if (unlikely(!is_rpc(t)))
		return_args1(t, ERR_MISC);

	/* inform requester who answered (pid) in the case of the request being
	 * kicked forward */
	enable_irqs();
	leave_rpc(t, SYS_RET6(OK, t->pid, d0, d1, d2, d3));
}

/**
 * Ghost return, resetting register state.
 *
 * @param t Current tcb.
 * @return The previous registers of thread.
 */
SYSCALL_DEFINE0(ipc_ghost)(struct tcb *t)
{
	if (unlikely(!is_rpc(t)))
		return_args1(t, ERR_MISC);

	enable_irqs();
	leave_rpc(t, get_args(t));
}

/**
 * Notify syscall handler.
 *
 * \todo Implement.
 *
 * @param t Current tcb.
 * @param tid Thread ID to notify.
 * @return \ref OK and 0.
 */
SYSCALL_DEFINE1(notify)(struct tcb *t, sys_arg_t tid){
	if (t->tid != tid && !has_cap(t->caps, CAP_NOTIFY))
		return_args1(t, ERR_PERM);

	struct tcb *r = get_tcb(tid);
	if (!r)
		return_args1(t, ERR_INVAL);

	/* set args, if notify swaps us out we pick them up the next time this
	 * thread is scheduled */
	set_args1(t, OK);
	enable_irqs();
	notify(r, NOTIFY_SIGNAL);
}