OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [baremetal/] [test_suite/] [src/] [thread.c] - Blame information for rev 6

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Thread creation userspace helpers
3
 *
4
 * Copyright (C) 2009 B Labs Ltd.
5
 */
6
#include <thread.h>
7
#include <l4/api/errno.h>
8
 
9
char stack[THREADS_TOTAL][STACK_SIZE] ALIGN(8);
10
char *__stack_ptr = &stack[1][0];
11
 
12
char utcb[THREADS_TOTAL][UTCB_SIZE] ALIGN(8);
13
char *__utcb_ptr = &utcb[1][0];
14
 
15
extern void local_setup_new_thread(void);
16
 
17
int thread_create(int (*func)(void *), void *args, unsigned int flags,
18
                  struct task_ids *new_ids)
19
{
20
        struct task_ids ids;
21
        struct exregs_data exregs;
22
        int err;
23
 
24
        l4_getid(&ids);
25
 
26
        /* Shared space only */
27
        if (!(TC_SHARE_SPACE & flags)) {
28
                printf("%s: This function allows only "
29
                       "shared space thread creation.\n",
30
                       __FUNCTION__);
31
                return -EINVAL;
32
        }
33
 
34
        /* Create thread */
35
        if ((err = l4_thread_control(THREAD_CREATE | flags, &ids)) < 0)
36
                return err;
37
 
38
        /* Check if more stack/utcb available */
39
        if ((unsigned long)__utcb_ptr ==
40
            (unsigned long)&utcb[THREADS_TOTAL][0])
41
                return -ENOMEM;
42
        if ((unsigned long)__stack_ptr ==
43
            (unsigned long)&stack[THREADS_TOTAL][0])
44
                return -ENOMEM;
45
 
46
        /* First word of new stack is arg */
47
        *(((unsigned int *)__stack_ptr) -1) = (unsigned int)args;
48
 
49
        /* Second word of new stack is function address */
50
        *(((unsigned int *)__stack_ptr) -2) = (unsigned int)func;
51
 
52
        /* Setup new thread pc, sp, utcb */
53
        memset(&exregs, 0, sizeof(exregs));
54
        exregs_set_stack(&exregs, (unsigned long)__stack_ptr);
55
        exregs_set_utcb(&exregs, (unsigned long)__utcb_ptr);
56
        exregs_set_pc(&exregs, (unsigned long)local_setup_new_thread);
57
 
58
        if ((err = l4_exchange_registers(&exregs, ids.tid)) < 0)
59
                return err;
60
 
61
        /* Update utcb, stack pointers */
62
        __stack_ptr += STACK_SIZE;
63
        __utcb_ptr += UTCB_SIZE;
64
 
65
        /* Start the new thread */
66
        if ((err = l4_thread_control(THREAD_RUN, &ids)) < 0)
67
                return err;
68
 
69
        memcpy(new_ids, &ids, sizeof(ids));
70
 
71
        return 0;
72
}
73
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.