summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/rtems/rtems-kernel-thread.c
blob: 8e3344ef65cdefea29c2deafdfc9b1abc4d4c18d (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
/**
 * @file
 *
 * @ingroup rtems_bsd_rtems
 *
 * @brief TODO.
 */

/*
 * Copyright (c) 2009-2015 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <machine/rtems-bsd-kernel-space.h>
#include <machine/rtems-bsd-thread.h>
#include <machine/rtems-bsd-support.h>

#include <sys/param.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <sys/malloc.h>
#include <sys/selinfo.h>
#include <sys/sleepqueue.h>

#include <rtems/bsd/bsd.h>

#include <rtems/score/objectimpl.h>
#include <rtems/score/statesimpl.h>
#include <rtems/score/threaddispatch.h>
#include <rtems/score/thread.h>
#include <rtems/score/threadimpl.h>
#include <rtems/score/threadqimpl.h>

static size_t rtems_bsd_extension_index;

static CHAIN_DEFINE_EMPTY(rtems_bsd_thread_delay_start_chain);

static bool rtems_bsd_thread_ready_to_start;

struct thread *
rtems_bsd_get_thread(const Thread_Control *thread)
{
	return thread->extensions[rtems_bsd_extension_index];
}

static Thread_Control *
rtems_bsd_get_thread_by_id(rtems_id task_id)
{
	Thread_Control *thread;
	ISR_lock_Context lock_context;

	thread = _Thread_Get(task_id, &lock_context);
	if (thread != NULL) {
		_ISR_lock_ISR_enable(&lock_context);
	}

	return (thread);
}

struct thread *
rtems_bsd_thread_create(Thread_Control *thread, int wait)
{
	struct thread *td = malloc(sizeof(*td), M_TEMP, M_ZERO | wait);
	struct sleepqueue *sq = sleepq_alloc();

	if (td != NULL && sq != NULL) {
		td->td_thread = thread;
		td->td_sleepqueue = sq;
	} else {
		free(td, M_TEMP);
		sleepq_free(sq);
		td = NULL;
	}

	thread->extensions[rtems_bsd_extension_index] = td;

	return td;
}

static struct thread *
rtems_bsd_get_curthread(int wait)
{
	Thread_Control *executing = _Thread_Get_executing();
	struct thread *td = rtems_bsd_get_thread(executing);

	if (td == NULL) {
		td = rtems_bsd_thread_create(executing, wait);
	}

	return td;
}

struct thread *
rtems_bsd_get_curthread_or_wait_forever(void)
{
	return rtems_bsd_get_curthread(M_WAITOK);
}

struct thread *
rtems_bsd_get_curthread_or_null(void)
{
	return rtems_bsd_get_curthread(0);
}

static bool
rtems_bsd_is_bsd_thread(Thread_Control *thread)
{
	return thread->Object.name.name_u32 == BSD_TASK_NAME;
}

static bool
rtems_bsd_extension_thread_create(
	Thread_Control *executing,
	Thread_Control *created
)
{
	bool ok;

	if (rtems_bsd_is_bsd_thread(created)) {
		struct thread *td = rtems_bsd_thread_create(created, 0);

		ok = td != NULL;
	} else {
		ok = true;
	}

	return ok;
}

static void
rtems_bsd_extension_thread_delete(
	Thread_Control *executing,
	Thread_Control *deleted
)
{
	struct thread *td = rtems_bsd_get_thread(deleted);

	if (td != NULL) {
		seltdfini(td);
		sleepq_free(td->td_sleepqueue);
		free(td, M_TEMP);
	}
}

static const rtems_extensions_table rtems_bsd_extensions = {
	.thread_create = rtems_bsd_extension_thread_create,
	.thread_delete = rtems_bsd_extension_thread_delete
};

static void
rtems_bsd_threads_init_early(void *arg)
{
	rtems_id ext_id;
	rtems_status_code sc;

	(void) arg;

	sc = rtems_extension_create(
		BSD_TASK_NAME,
		&rtems_bsd_extensions,
		&ext_id
	);
	if (sc != RTEMS_SUCCESSFUL) {
		BSD_PANIC("cannot create extension");
	}

	rtems_bsd_extension_index = rtems_object_id_get_index(ext_id);
}

static void
rtems_bsd_threads_init_late(void *arg)
{
	Chain_Control *chain = &rtems_bsd_thread_delay_start_chain;
	Chain_Node *node;

	(void) arg;

	while ((node = _Chain_Get_unprotected(chain)) != NULL) {
		Thread_Control *thread = (Thread_Control *) node;
		rtems_status_code sc;

		sc = rtems_task_start(thread->Object.id,
		    thread->Start.Entry.Kinds.Numeric.entry,
		    thread->Start.Entry.Kinds.Numeric.argument);
		BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
	}

	rtems_bsd_thread_ready_to_start = true;
}

SYSINIT(rtems_bsd_threads_early, SI_SUB_TUNABLES, SI_ORDER_ANY,
    rtems_bsd_threads_init_early, NULL);

SYSINIT(rtems_bsd_threads_late, SI_SUB_LAST, SI_ORDER_ANY,
    rtems_bsd_threads_init_late, NULL);

static int
rtems_bsd_thread_start(struct thread **td_ptr, void (*func)(void *), void *arg,
    int flags, int pages, const char *fmt, va_list ap)
{
	int eno = 0;
	rtems_status_code sc;
	rtems_id task_id;
	struct thread *td;
	char name[32];

	BSD_ASSERT(pages >= 0);

	vsnprintf(name, sizeof(name), fmt, ap);

	sc = rtems_task_create(
		BSD_TASK_NAME,
		rtems_bsd_get_task_priority(name),
		rtems_bsd_get_task_stack_size(name)
			+ (size_t) pages * PAGE_SIZE,
		RTEMS_DEFAULT_MODES,
		RTEMS_DEFAULT_ATTRIBUTES,
		&task_id
	);
	if (sc == RTEMS_SUCCESSFUL) {
		Thread_Control *thread = rtems_bsd_get_thread_by_id(task_id);

		BSD_ASSERT(thread != NULL);

		td = rtems_bsd_get_thread(thread);
		BSD_ASSERT(td != NULL);

		_Thread_Set_name(thread, name);

		if (rtems_bsd_thread_ready_to_start) {
			sc = rtems_task_start(task_id, (rtems_task_entry) func,
			    (rtems_task_argument) arg);
			BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
		} else {
			thread->Start.Entry.Kinds.Numeric.entry =
			    (void (*)(Thread_Entry_numeric_type))func;
			thread->Start.Entry.Kinds.Numeric.argument =
			    (Thread_Entry_numeric_type)arg;
			_Chain_Append_unprotected(
			    &rtems_bsd_thread_delay_start_chain,
			    &thread->Object.Node);
		}

		if (td_ptr != NULL) {
			*td_ptr = td;
		}
	} else {
		eno = ENOMEM;
	}

	return eno;
}

static __dead2 void
rtems_bsd_thread_delete(void)
{
	rtems_task_delete(RTEMS_SELF);
	BSD_PANIC("delete self failed");
}

void
kproc_start(const void *udata)
{
	const struct kproc_desc	*pd = udata;
	int eno = kproc_create((void (*)(void *))pd->func, NULL, pd->global_procpp, 0, 0, "%s", pd->arg0);

	BSD_ASSERT(eno == 0);
}

int
kproc_create(void (*func)(void *), void *arg, struct proc **newpp, int flags, int pages, const char *fmt, ...)
{
	int eno = 0;
	va_list ap;

	va_start(ap, fmt);
	eno = rtems_bsd_thread_start(newpp, func, arg, flags, pages, fmt, ap);
	va_end(ap);

	return eno;
}

void
kproc_exit(int ecode)
{
	rtems_bsd_thread_delete();
}

void
kthread_start(const void *udata)
{
	const struct kthread_desc *td = udata;
	int eno = kthread_add((void (*)(void *)) td->func, NULL, NULL, td->global_threadpp, 0, 0, "%s", td->arg0);

	BSD_ASSERT(eno == 0);
}

int
kthread_add(void (*func)(void *), void *arg, struct proc *p, struct thread **newtdp, int flags, int pages, const char *fmt, ...)
{
	int eno = 0;
	va_list ap;

	va_start(ap, fmt);
	eno = rtems_bsd_thread_start(newtdp, func, arg, flags, pages, fmt, ap);
	va_end(ap);

	return eno;
}

void
kthread_exit(void)
{
	rtems_bsd_thread_delete();
}

int
kproc_kthread_add(void (*func)(void *), void *arg, struct proc **procptr, struct thread **tdptr, int flags, int pages, const char * procname, const char *fmt, ...)
{
	int eno = 0;
	va_list ap;

	va_start(ap, fmt);
	eno = rtems_bsd_thread_start(tdptr, func, arg, flags, pages, fmt, ap);
	va_end(ap);

	return eno;
}