source: rtems-libbsd/rtemsbsd/rtems/rtems-bsd-thread.c @ 549488b

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 549488b was 549488b, checked in by Sebastian Huber <sebastian.huber@…>, on 10/10/13 at 12:45:09

Disable alternative routing tables

  • Property mode set to 100644
File size: 7.6 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_rtems
5 *
6 * @brief TODO.
7 */
8
9/*
10 * Copyright (c) 2009-2013 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40#include <machine/rtems-bsd-config.h>
41#include <machine/rtems-bsd-thread.h>
42
43#include <rtems/bsd/sys/param.h>
44#include <rtems/bsd/sys/types.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/proc.h>
48#include <sys/kthread.h>
49#include <sys/malloc.h>
50#include <sys/selinfo.h>
51#include <sys/filedesc.h>
52#include <sys/jail.h>
53#include <sys/resourcevar.h>
54
55#include <rtems/score/threadimpl.h>
56#include <rtems/score/objectimpl.h>
57
58RTEMS_CHAIN_DEFINE_EMPTY(rtems_bsd_thread_chain);
59
60/* FIXME: What to do with the credentials? */
61static struct ucred FIXME_ucred = {
62  .cr_ref = 1                          /* reference count */
63};
64static struct proc  FIXME_proc = {
65  .p_ucred = NULL /* (c) Process owner's identity. */
66};
67static struct prison FIXME_prison = {
68  .pr_parent = NULL
69};
70
71static size_t rtems_bsd_extension_index;
72
73struct thread *
74rtems_bsd_get_thread(const Thread_Control *thread)
75{
76        return thread->extensions[rtems_bsd_extension_index];
77}
78
79static struct thread *
80rtems_bsd_get_thread_by_id(rtems_id task_id)
81{
82        struct thread *td = NULL;
83        Thread_Control *thread;
84        Objects_Locations location;
85
86        thread = _Thread_Get(task_id, &location);
87        switch (location) {
88                case OBJECTS_LOCAL:
89                        td = rtems_bsd_get_thread(thread);
90                        _Objects_Put(&thread->Object);
91                        break;
92#if defined(RTEMS_MULTIPROCESSING)
93                case OBJECTS_REMOTE:
94                        _Thread_Dispatch();
95                        break;
96#endif
97                default:
98                        break;
99        }
100
101        return td;
102}
103
104struct thread *
105rtems_bsd_thread_create(Thread_Control *thread, int wait)
106{
107        struct thread *td = malloc(sizeof(*td), M_TEMP, M_ZERO | wait);
108
109        if (td != NULL) {
110                td->td_thread = thread;
111                td->td_proc = &FIXME_proc;
112        }
113
114        thread->extensions[rtems_bsd_extension_index] = td;
115
116        return td;
117}
118
119static struct thread *
120rtems_bsd_get_curthread(int wait)
121{
122        Thread_Control *executing = _Thread_Get_executing();
123        struct thread *td = rtems_bsd_get_thread(executing);
124
125        if (td == NULL) {
126                td = rtems_bsd_thread_create(executing, wait);
127        }
128
129        return td;
130}
131
132struct thread *
133rtems_bsd_get_curthread_or_wait_forever(void)
134{
135        return rtems_bsd_get_curthread(M_WAITOK);
136}
137
138struct thread *
139rtems_bsd_get_curthread_or_null(void)
140{
141        return rtems_bsd_get_curthread(0);
142}
143
144static bool
145rtems_bsd_is_bsd_thread(Thread_Control *thread)
146{
147        return thread->Object.name.name_u32 == BSD_TASK_NAME;
148}
149
150static bool
151rtems_bsd_extension_thread_create(
152        Thread_Control *executing,
153        Thread_Control *created
154)
155{
156        bool ok = true;
157
158        if (rtems_bsd_is_bsd_thread(created)) {
159                struct thread *td = rtems_bsd_thread_create(created, 0);
160
161                ok = td != NULL;
162                if (ok) {
163                        rtems_chain_append(&rtems_bsd_thread_chain, &td->td_node);
164                }
165        }
166
167        return ok;
168}
169
170static void
171rtems_bsd_extension_thread_delete(
172        Thread_Control *executing,
173        Thread_Control *deleted
174)
175{
176        struct thread *td = rtems_bsd_get_thread(deleted);
177
178        if (td != NULL) {
179                seltdfini(td);
180
181                if (rtems_bsd_is_bsd_thread(deleted)) {
182                        rtems_chain_explicit_extract(&rtems_bsd_thread_chain, &td->td_node);
183                }
184
185                free(td, M_TEMP);
186        }
187}
188
189static const rtems_extensions_table rtems_bsd_extensions = {
190        .thread_create = rtems_bsd_extension_thread_create,
191        .thread_delete = rtems_bsd_extension_thread_delete
192};
193
194static void
195rtems_bsd_threads_init(void *arg __unused)
196{
197        rtems_id ext_id;
198        rtems_status_code sc;
199
200        sc = rtems_extension_create(
201                BSD_TASK_NAME,
202                &rtems_bsd_extensions,
203                &ext_id
204        );
205        if (sc != RTEMS_SUCCESSFUL) {
206                BSD_PANIC("cannot create extension");
207        }
208
209        rtems_bsd_extension_index = rtems_object_id_get_index(ext_id);
210
211        mtx_init(&FIXME_prison.pr_mtx, "prison lock", NULL, MTX_DEF | MTX_DUPOK);
212
213        FIXME_ucred.cr_prison   = &FIXME_prison;    /* jail(2) */
214        FIXME_ucred.cr_uidinfo  = uifind(0);
215        FIXME_ucred.cr_ruidinfo = uifind(0);
216        FIXME_ucred.cr_ngroups = 1;     /* group 0 */
217
218        FIXME_proc.p_ucred = crhold(&FIXME_ucred);
219        FIXME_proc.p_pid = getpid();
220}
221
222SYSINIT(rtems_bsd_threads, SI_SUB_INTRINSIC, SI_ORDER_ANY, rtems_bsd_threads_init, NULL);
223
224static int
225rtems_bsd_thread_start(struct thread **td_ptr, void (*func)(void *), void *arg, int flags, int pages, const char *fmt, va_list ap)
226{
227        int eno = 0;
228        rtems_status_code sc;
229        rtems_id task_id;
230
231        BSD_ASSERT(pages >= 0);
232
233        sc = rtems_task_create(
234                BSD_TASK_NAME,
235                BSD_TASK_PRIORITY_NORMAL,
236                BSD_MINIMUM_TASK_STACK_SIZE + (size_t) pages * PAGE_SIZE,
237                RTEMS_DEFAULT_ATTRIBUTES,
238                RTEMS_DEFAULT_ATTRIBUTES,
239                &task_id
240        );
241        if (sc == RTEMS_SUCCESSFUL) {
242                struct thread *td = rtems_bsd_get_thread_by_id(task_id);
243
244                BSD_ASSERT(td != NULL);
245
246                vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
247
248                sc = rtems_task_start(task_id, (rtems_task_entry) func, (rtems_task_argument) arg);
249                BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
250
251                if (td_ptr != NULL) {
252                        *td_ptr = td;
253                }
254        } else {
255                eno = ENOMEM;
256        }
257
258        return eno;
259}
260
261static __dead2 void
262rtems_bsd_thread_delete(void)
263{
264        rtems_task_delete(RTEMS_SELF);
265        BSD_PANIC("delete self failed");
266}
267
268void
269kproc_start(const void *udata)
270{
271        const struct kproc_desc *pd = udata;
272        int eno = kproc_create((void (*)(void *))pd->func, NULL, pd->global_procpp, 0, 0, "%s", pd->arg0);
273
274        BSD_ASSERT(eno == 0);
275}
276
277int
278kproc_create(void (*func)(void *), void *arg, struct proc **newpp, int flags, int pages, const char *fmt, ...)
279{
280        int eno = 0;
281        va_list ap;
282
283        va_start(ap, fmt);
284        eno = rtems_bsd_thread_start(newpp, func, arg, flags, pages, fmt, ap);
285        va_end(ap);
286
287        return eno;
288}
289
290void
291kproc_exit(int ecode)
292{
293        rtems_bsd_thread_delete();
294}
295
296void
297kthread_start(const void *udata)
298{
299        const struct kthread_desc *td = udata;
300        int eno = kthread_add((void (*)(void *)) td->func, NULL, NULL, td->global_threadpp, 0, 0, "%s", td->arg0);
301
302        BSD_ASSERT(eno == 0);
303}
304
305int
306kthread_add(void (*func)(void *), void *arg, struct proc *p, struct thread **newtdp, int flags, int pages, const char *fmt, ...)
307{
308        int eno = 0;
309        va_list ap;
310
311        va_start(ap, fmt);
312        eno = rtems_bsd_thread_start(newtdp, func, arg, flags, pages, fmt, ap);
313        va_end(ap);
314
315        return eno;
316}
317
318void
319kthread_exit(void)
320{
321        rtems_bsd_thread_delete();
322}
323
324int
325kproc_kthread_add(void (*func)(void *), void *arg, struct proc **procptr, struct thread **tdptr, int flags, int pages, const char * procname, const char *fmt, ...)
326{
327        int eno = 0;
328        va_list ap;
329
330        va_start(ap, fmt);
331        eno = rtems_bsd_thread_start(tdptr, func, arg, flags, pages, fmt, ap);
332        va_end(ap);
333
334        return eno;
335}
Note: See TracBrowser for help on using the repository browser.