summaryrefslogtreecommitdiff
path: root/kern/task.c
blob: 5df72251fc5f421f42fece98e2e12749689bb885 (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
/*
 * Copyright (c) 2012-2014 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 <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

#include <kern/init.h>
#include <kern/kmem.h>
#include <kern/list.h>
#include <kern/macros.h>
#include <kern/shell.h>
#include <kern/spinlock.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <vm/vm_map.h>

#ifdef __LP64__
#define TASK_INFO_ADDR_FMT "%016lx"
#else /* __LP64__ */
#define TASK_INFO_ADDR_FMT "%08lx"
#endif /* __LP64__ */

struct task task_kernel_task;

/*
 * Cache for allocated tasks.
 */
static struct kmem_cache task_cache;

/*
 * Global list of tasks.
 */
static struct list task_list;
static struct spinlock task_list_lock;

static void
task_init(struct task *task, const char *name, struct vm_map *map)
{
    task->nr_refs = 1;
    spinlock_init(&task->lock);
    list_init(&task->threads);
    task->map = map;
    strlcpy(task->name, name, sizeof(task->name));
}

#ifdef CONFIG_SHELL

static void
task_shell_info(int argc, char *argv[])
{
    struct task *task;
    int error;

    if (argc == 1) {
        task_info(NULL);
        return;
    } else {
        task = task_lookup(argv[1]);

        if (task == NULL) {
            error = EINVAL;
            goto error;
        }

        task_info(task);
        task_unref(task);
    }

    return;

error:
    printf("task: info: %s\n", strerror(error));
}

static struct shell_cmd task_shell_cmds[] = {
    SHELL_CMD_INITIALIZER("task_info", task_shell_info,
                          "task_info [<task_name>]",
                          "display tasks and threads"),
};

static int __init
task_setup_shell(void)
{
    SHELL_REGISTER_CMDS(task_shell_cmds);
    return 0;
}

INIT_OP_DEFINE(task_setup_shell,
               INIT_OP_DEP(printf_setup, true),
               INIT_OP_DEP(shell_setup, true),
               INIT_OP_DEP(task_setup, true),
               INIT_OP_DEP(thread_setup, true));

#endif /* CONFIG_SHELL */

static int __init
task_setup(void)
{
    struct task *kernel_task;

    kernel_task = task_get_kernel_task();
    kmem_cache_init(&task_cache, "task", sizeof(struct task), 0, NULL, 0);
    list_init(&task_list);
    spinlock_init(&task_list_lock);
    task_init(kernel_task, "x15", vm_map_get_kernel_map());
    list_insert_head(&task_list, &kernel_task->node);
    return 0;
}

INIT_OP_DEFINE(task_setup,
               INIT_OP_DEP(kmem_setup, true),
               INIT_OP_DEP(spinlock_setup, true),
               INIT_OP_DEP(vm_map_setup, true));

int
task_create(struct task **taskp, const char *name)
{
    struct vm_map *map;
    struct task *task;
    int error;

    task = kmem_cache_alloc(&task_cache);

    if (task == NULL) {
        error = ENOMEM;
        goto error_task;
    }

    error = vm_map_create(&map);

    if (error) {
        goto error_map;
    }

    task_init(task, name, map);

    spinlock_lock(&task_list_lock);
    list_insert_tail(&task_list, &task->node);
    spinlock_unlock(&task_list_lock);

    *taskp = task;
    return 0;

error_map:
    kmem_cache_free(&task_cache, task);
error_task:
    return error;
}

struct task *
task_lookup(const char *name)
{
    struct task *task;

    spinlock_lock(&task_list_lock);

    list_for_each_entry(&task_list, task, node) {
        spinlock_lock(&task->lock);

        if (strcmp(task->name, name) == 0) {
            task_ref(task);
            spinlock_unlock(&task->lock);
            spinlock_unlock(&task_list_lock);
            return task;
        }

        spinlock_unlock(&task->lock);
    }

    spinlock_unlock(&task_list_lock);

    return NULL;
}

void
task_add_thread(struct task *task, struct thread *thread)
{
    spinlock_lock(&task->lock);
    list_insert_tail(&task->threads, &thread->task_node);
    spinlock_unlock(&task->lock);
}

void
task_remove_thread(struct task *task, struct thread *thread)
{
    spinlock_lock(&task->lock);
    list_remove(&thread->task_node);
    spinlock_unlock(&task->lock);
}

struct thread *
task_lookup_thread(struct task *task, const char *name)
{
    struct thread *thread;

    spinlock_lock(&task->lock);

    list_for_each_entry(&task->threads, thread, task_node) {
        if (strcmp(thread->name, name) == 0) {
            thread_ref(thread);
            spinlock_unlock(&task->lock);
            return thread;
        }
    }

    spinlock_unlock(&task->lock);

    return NULL;
}

void
task_info(struct task *task)
{
    struct thread *thread;

    if (task == NULL) {
        spinlock_lock(&task_list_lock);

        list_for_each_entry(&task_list, task, node) {
            task_info(task);
        }

        spinlock_unlock(&task_list_lock);

        return;
    }

    spinlock_lock(&task->lock);

    printf("task: name: %s, threads:\n", task->name);

    /*
     * Don't grab any lock when accessing threads, so that the function
     * can be used to debug in the middle of most critical sections.
     * Threads are only destroyed after being removed from their task
     * so holding the task lock is enough to guarantee existence.
     *
     * TODO Handle tasks and threads names modifications.
     */
    list_for_each_entry(&task->threads, thread, task_node) {
        printf(TASK_INFO_ADDR_FMT " %c %8s:" TASK_INFO_ADDR_FMT
               " %.2s:%02hu %02u %s\n",
               (unsigned long)thread,
               thread_state_to_chr(thread),
               thread_wchan_desc(thread),
               (unsigned long)thread_wchan_addr(thread),
               thread_sched_class_to_str(thread_user_sched_class(thread)),
               thread_user_priority(thread),
               thread_real_global_priority(thread),
               thread->name);
    }

    spinlock_unlock(&task->lock);
}