summaryrefslogtreecommitdiff
path: root/kern/thread.c
blob: 24498b01a1f60a8849ce1bc1420a3f3aedf3570e (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
/*
 * Copyright (c) 2012 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <kern/error.h>
#include <kern/init.h>
#include <kern/kmem.h>
#include <kern/list.h>
#include <kern/macros.h>
#include <kern/param.h>
#include <kern/stddef.h>
#include <kern/string.h>
#include <kern/thread.h>
#include <machine/cpu.h>
#include <machine/tcb.h>

/*
 * Make sure thread stacks are properly aligned.
 */
#define THREAD_STACK_ALIGN 8

/*
 * Per processor run queue.
 */
struct thread_runq {
    struct list threads;
} __aligned(CPU_L1_SIZE);

/*
 * Per-processor run queues.
 */
static struct thread_runq thread_runqs[MAX_CPUS];

/*
 * Caches for allocated threads and their stacks.
 */
static struct kmem_cache thread_cache;
static struct kmem_cache thread_stack_cache;

static void __init
thread_runq_init(struct thread_runq *runq)
{
    list_init(&runq->threads);
}

void __init
thread_setup(void)
{
    size_t i;

    tcb_setup();

    kmem_cache_init(&thread_cache, "thread", sizeof(struct thread),
                    0, NULL, NULL, NULL, 0);
    kmem_cache_init(&thread_stack_cache, "thread_stack", STACK_SIZE,
                    THREAD_STACK_ALIGN, NULL, NULL, NULL, 0);

    for (i = 0; i < ARRAY_SIZE(thread_runqs); i++)
        thread_runq_init(&thread_runqs[i]);
}

int
thread_create(struct thread **threadp, const char *name, struct task *task,
              thread_run_fn_t run_fn, void *arg)
{
    struct thread *thread;
    struct tcb *tcb;
    void *stack;
    int error;

    thread = kmem_cache_alloc(&thread_cache);

    if (thread == NULL) {
        error = ERROR_NOMEM;
        goto error_thread;
    }

    stack = kmem_cache_alloc(&thread_stack_cache);

    if (stack == NULL) {
        error = ERROR_NOMEM;
        goto error_stack;
    }

    error = tcb_create(&tcb, stack, thread);

    if (error)
        goto error_tcb;

    if (name == NULL)
        name = task->name;

    /* XXX Assign all threads to the main processor for now */
    thread->tcb = tcb;
    list_insert_tail(&thread_runqs[0].threads, &thread->runq_node);
    task_add_thread(task, thread);
    thread->task = task;
    thread->stack = stack;
    strlcpy(thread->name, name, sizeof(thread->name));
    thread->run_fn = run_fn;
    thread->arg = arg;
    *threadp = thread;
    return 0;

error_tcb:
    kmem_cache_free(&thread_stack_cache, stack);
error_stack:
    kmem_cache_free(&thread_cache, thread);
error_thread:
    return error;
}

void __init
thread_load(struct thread *thread)
{
    tcb_load(thread->tcb);
}

void
thread_main(struct thread *thread)
{
    thread->run_fn(thread->arg);

    for (;;)
        cpu_idle();
}