summaryrefslogtreecommitdiff
path: root/core/host/task.c
blob: fa853dce603e4536ad58540412e2064999c0fdc9 (plain)
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* Copyright 2013 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Task scheduling / events module for Chrome EC operating system */

#include "atomic.h"
#include "common.h"
#include "console.h"
#include "host_task.h"
#include "task.h"
#include "task_id.h"
#include "test_util.h"
#include "timer.h"

#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <malloc.h>
#include <pthread.h>
#include <semaphore.h>

#define SIGNAL_INTERRUPT SIGUSR1

struct emu_task_t {
	pthread_t thread;
	pthread_cond_t resume;
	atomic_t event;
	timestamp_t wake_time;
	uint8_t started;
};

struct task_args {
	void (*routine)(void *);
	void *d;
};

static struct emu_task_t tasks[TASK_ID_COUNT];
static pthread_cond_t scheduler_cond;
static pthread_mutex_t run_lock;
static task_id_t running_task_id;
static int task_started;

static sem_t interrupt_sem;
static pthread_mutex_t interrupt_lock;
static pthread_t interrupt_thread;
static bool in_interrupt;
static int interrupt_disabled;
static void (*pending_isr)(void);
static int generator_sleeping;
static timestamp_t generator_sleep_deadline;
static int has_interrupt_generator = 1;

/* thread local task id */
static __thread task_id_t my_task_id = TASK_ID_INVALID;

static void task_enable_all_tasks_callback(void);

#define TASK(n, r, d, s) void r(void *);
CONFIG_TASK_LIST
CONFIG_TEST_TASK_LIST
CONFIG_CTS_TASK_LIST
#undef TASK

/* usleep that uses OS functions, instead of emulated timer. */
/* LCOV_EXCL_START */
void _usleep(int usec)
{
	struct timespec req;

	req.tv_sec = usec / 1000000;
	req.tv_nsec = (usec % 1000000) * 1000;

	nanosleep(&req, NULL);
}
/* LCOV_EXCL_STOP */

/* msleep that uses OS functions, instead of emulated timer. */
void _msleep(int msec)
{
	_usleep(1000 * msec);
}

/* Idle task */
void __idle(void *d)
{
	while (1)
		task_wait_event(-1);
}

void _run_test(void *d)
{
	run_test(0, NULL);
}

#define TASK(n, r, d, s) { r, d },
const struct task_args task_info[TASK_ID_COUNT] = {
	{ __idle, NULL },
	CONFIG_TASK_LIST CONFIG_TEST_TASK_LIST CONFIG_CTS_TASK_LIST{ _run_test,
								     NULL },
};
#undef TASK

#define TASK(n, r, d, s) #n,
static const char *const task_names[] = {
	"<< idle >>",
	CONFIG_TASK_LIST CONFIG_TEST_TASK_LIST CONFIG_CTS_TASK_LIST
	"<< test runner >>",
};
#undef TASK

void task_pre_init(void)
{
	/* Nothing */
}

bool in_interrupt_context(void)
{
	return in_interrupt;
}

test_mockable void interrupt_disable(void)
{
	pthread_mutex_lock(&interrupt_lock);
	interrupt_disabled = 1;
	pthread_mutex_unlock(&interrupt_lock);
}

test_mockable void interrupt_enable(void)
{
	pthread_mutex_lock(&interrupt_lock);
	interrupt_disabled = 0;
	pthread_mutex_unlock(&interrupt_lock);
}

inline bool is_interrupt_enabled(void)
{
	return !interrupt_disabled;
}

static void _task_execute_isr(int sig)
{
	in_interrupt = true;
	pending_isr();
	sem_post(&interrupt_sem);
	in_interrupt = false;
}

void task_register_interrupt(void)
{
	sem_init(&interrupt_sem, 0, 0);
	signal(SIGNAL_INTERRUPT, _task_execute_isr);
}

void task_trigger_test_interrupt(void (*isr)(void))
{
	pid_t main_pid;
	pthread_mutex_lock(&interrupt_lock);
	if (interrupt_disabled) {
		pthread_mutex_unlock(&interrupt_lock);
		return;
	}

	/* Suspend current task and execute ISR */
	pending_isr = isr;
	if (task_started) {
		pthread_kill(tasks[running_task_id].thread, SIGNAL_INTERRUPT);
	} else {
		main_pid = getpid();
		kill(main_pid, SIGNAL_INTERRUPT);
	}

	/* Wait for ISR to complete */
	sem_wait(&interrupt_sem);
	/* LCOV_EXCL_START */
	while (in_interrupt)
		_usleep(10);
	/* LCOV_EXCL_STOP */
	pending_isr = NULL;

	pthread_mutex_unlock(&interrupt_lock);
}

void interrupt_generator_udelay(unsigned us)
{
	generator_sleep_deadline.val = get_time().val + us;
	generator_sleeping = 1;
	while (get_time().val < generator_sleep_deadline.val)
		;
	generator_sleeping = 0;
}

const char *task_get_name(task_id_t tskid)
{
	return task_names[tskid];
}

pthread_t task_get_thread(task_id_t tskid)
{
	return tasks[tskid].thread;
}

void task_set_event(task_id_t tskid, uint32_t event)
{
	atomic_or(&tasks[tskid].event, event);
}

atomic_t *task_get_event_bitmap(task_id_t tskid)
{
	return &tasks[tskid].event;
}

uint32_t task_wait_event(int timeout_us)
{
	int tid = task_get_current();
	int ret;
	pthread_mutex_lock(&interrupt_lock);
	if (timeout_us > 0)
		tasks[tid].wake_time.val = get_time().val + timeout_us;

	/* Transfer control to scheduler */
	pthread_cond_signal(&scheduler_cond);
	pthread_cond_wait(&tasks[tid].resume, &run_lock);

	/* Resume */
	ret = atomic_clear(&tasks[tid].event);
	pthread_mutex_unlock(&interrupt_lock);
	return ret;
}

uint32_t task_wait_event_mask(uint32_t event_mask, int timeout_us)
{
	uint64_t deadline = get_time().val + timeout_us;
	uint32_t events = 0;
	int time_remaining_us = timeout_us;

	/* Add the timer event to the mask so we can indicate a timeout */
	event_mask |= TASK_EVENT_TIMER;

	while (!(events & event_mask)) {
		/* Collect events to re-post later */
		events |= task_wait_event(time_remaining_us);

		time_remaining_us = deadline - get_time().val;
		if (timeout_us > 0 && time_remaining_us <= 0) {
			/* Ensure we return a TIMER event if we timeout */
			events |= TASK_EVENT_TIMER;
			break;
		}
	}

	/* Re-post any other events collected */
	if (events & ~event_mask)
		atomic_or(&tasks[task_get_current()].event,
			  events & ~event_mask);

	return events & event_mask;
}

void mutex_lock(struct mutex *mtx)
{
	int value = 0;
	int id = 1 << task_get_current();

	mtx->waiters |= id;

	do {
		if (mtx->lock == 0) {
			mtx->lock = 1;
			value = 1;
		}

		if (!value)
			task_wait_event_mask(TASK_EVENT_MUTEX, 0);
	} while (!value);

	mtx->waiters &= ~id;
}

void mutex_unlock(struct mutex *mtx)
{
	int v;
	mtx->lock = 0;

	for (v = 31; v >= 0; --v)
		if ((1ul << v) & mtx->waiters) {
			mtx->waiters &= ~(1ul << v);
			task_set_event(v, TASK_EVENT_MUTEX);
			break;
		}
}

task_id_t task_get_current(void)
{
	return my_task_id;
}

task_id_t task_get_running(void)
{
	return running_task_id;
}

void task_print_list(void)
{
	int i;

	ccputs("Name         Events\n");

	for (i = 0; i < TASK_ID_COUNT; i++) {
		ccprintf("%4d %-16s %08x\n", i, task_names[i], tasks[i].event);
		cflush();
	}
}

static int command_task_info(int argc, const char **argv)
{
	task_print_list();

	return EC_SUCCESS;
}
DECLARE_SAFE_CONSOLE_COMMAND(taskinfo, command_task_info, NULL,
			     "Print task info");

static void _wait_for_task_started(int can_sleep)
{
	int i, ok;

	while (1) {
		ok = 1;
		for (i = 0; i < TASK_ID_COUNT - 1; ++i) {
			if (!tasks[i].started) {
				if (can_sleep)
					msleep(10);
				else
					_msleep(10);
				ok = 0;
				break;
			}
		}
		if (ok)
			return;
	}
}

void wait_for_task_started(void)
{
	_wait_for_task_started(1);
}

void wait_for_task_started_nosleep(void)
{
	_wait_for_task_started(0);
}

static task_id_t task_get_next_wake(void)
{
	int i;
	timestamp_t min_time;
	int which_task = TASK_ID_INVALID;

	min_time.val = ~0ull;

	for (i = TASK_ID_COUNT - 1; i >= 0; --i)
		if (min_time.val >= tasks[i].wake_time.val) {
			min_time.val = tasks[i].wake_time.val;
			which_task = i;
		}

	return which_task;
}

static int fast_forward(void)
{
	/*
	 * No task has event pending, and thus the next time we have an
	 * event to process must be either of:
	 *   1. Interrupt generator triggers an interrupt
	 *   2. The next wake alarm is reached
	 * So we should check whether an interrupt may happen, and fast
	 * forward to the nearest among:
	 *   1. When interrupt generator wakes up
	 *   2. When the next task wakes up
	 */
	int task_id = task_get_next_wake();

	if (!has_interrupt_generator) {
		if (task_id == TASK_ID_INVALID) {
			return TASK_ID_IDLE;
		} else {
			force_time(tasks[task_id].wake_time);
			return task_id;
		}
	}

	if (!generator_sleeping)
		return TASK_ID_IDLE;

	if (task_id != TASK_ID_INVALID &&
	    tasks[task_id].thread != (pthread_t)NULL &&
	    tasks[task_id].wake_time.val < generator_sleep_deadline.val) {
		force_time(tasks[task_id].wake_time);
		return task_id;
	} else {
		force_time(generator_sleep_deadline);
		return TASK_ID_IDLE;
	}
}

int task_start_called(void)
{
	return task_started;
}

void task_scheduler(void)
{
	int i;
	timestamp_t now;

	task_started = 1;

	while (1) {
		now = get_time();
		i = TASK_ID_COUNT - 1;
		while (i >= 0) {
			/*
			 * Only tasks with spawned threads are valid to be
			 * resumed.
			 */
			if (tasks[i].thread) {
				if (tasks[i].event ||
				    now.val >= tasks[i].wake_time.val)
					break;
			}
			--i;
		}
		if (i < 0)
			i = fast_forward();

		now = get_time();
		if (now.val >= tasks[i].wake_time.val)
			tasks[i].event |= TASK_EVENT_TIMER;
		tasks[i].wake_time.val = ~0ull;
		running_task_id = i;
		tasks[i].started = 1;
		pthread_cond_signal(&tasks[i].resume);
		pthread_cond_wait(&scheduler_cond, &run_lock);
	}
}

void *_task_start_impl(void *a)
{
	long tid = (long)a;
	const struct task_args *arg = task_info + tid;
	my_task_id = tid;
	pthread_mutex_lock(&run_lock);

	/* Wait for scheduler */
	task_wait_event(1);
	tasks[tid].event = 0;

	/* Start the task routine */
	(arg->routine)(arg->d);

	/* Catch exited routine */
	while (1)
		task_wait_event(-1);
}

test_mockable void interrupt_generator(void)
{
	has_interrupt_generator = 0;
}

void *_task_int_generator_start(void *d)
{
	my_task_id = TASK_ID_INT_GEN;
	interrupt_generator();
	return NULL;
}

int task_start(void)
{
	int i = TASK_ID_HOOKS;

	pthread_mutex_init(&run_lock, NULL);
	pthread_mutex_init(&interrupt_lock, NULL);
	pthread_cond_init(&scheduler_cond, NULL);

	pthread_mutex_lock(&run_lock);

	/*
	 * Initialize the hooks task first.  After its init, it will callback to
	 * enable the remaining tasks.
	 */
	tasks[i].event = TASK_EVENT_WAKE;
	tasks[i].wake_time.val = ~0ull;
	tasks[i].started = 0;
	pthread_cond_init(&tasks[i].resume, NULL);
	pthread_create(&tasks[i].thread, NULL, _task_start_impl,
		       (void *)(uintptr_t)i);
	pthread_cond_wait(&scheduler_cond, &run_lock);
	/*
	 * Interrupt lock is grabbed by the task which just started.
	 * Let's unlock it so the next task can be started.
	 */
	pthread_mutex_unlock(&interrupt_lock);

	/*
	 * The hooks task is  waiting in task_wait_event(). Lock interrupt_lock
	 * here so the first task chosen sees it locked.
	 */
	pthread_mutex_lock(&interrupt_lock);

	pthread_create(&interrupt_thread, NULL, _task_int_generator_start,
		       NULL);

	/*
	 * Tell the hooks task to continue so that it can call back to enable
	 * the other tasks.
	 */
	pthread_cond_signal(&tasks[i].resume);
	pthread_cond_wait(&scheduler_cond, &run_lock);
	task_enable_all_tasks_callback();

	task_scheduler();

	return 0;
}

static void task_enable_all_tasks_callback(void)
{
	int i;

	/* Initialize the remaning tasks. */
	for (i = 0; i < TASK_ID_COUNT; ++i) {
		if (tasks[i].thread != (pthread_t)NULL)
			continue;

		tasks[i].event = TASK_EVENT_WAKE;
		tasks[i].wake_time.val = ~0ull;
		tasks[i].started = 0;
		pthread_cond_init(&tasks[i].resume, NULL);
		pthread_create(&tasks[i].thread, NULL, _task_start_impl,
			       (void *)(uintptr_t)i);
		/*
		 * Interrupt lock is grabbed by the task which just started.
		 * Let's unlock it so the next task can be started.
		 */
		pthread_mutex_unlock(&interrupt_lock);
		pthread_cond_wait(&scheduler_cond, &run_lock);
	}
}

void task_enable_all_tasks(void)
{
	/* Signal to the scheduler to enable the remaining tasks. */
	pthread_cond_signal(&scheduler_cond);
}