Subversion Repositories HelenOS

Rev

Rev 3022 | Rev 4420 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3022 Rev 4055
Line 30... Line 30...
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief   Kernel initialization thread.
35
 * @brief Kernel initialization thread.
36
 *
36
 *
37
 * This file contains kinit kernel thread which carries out
37
 * This file contains kinit kernel thread which carries out
38
 * high level system initialization.
38
 * high level system initialization.
39
 *
39
 *
40
 * This file is responsible for finishing SMP configuration
40
 * This file is responsible for finishing SMP configuration
Line 45... Line 45...
45
#include <config.h>
45
#include <config.h>
46
#include <arch.h>
46
#include <arch.h>
47
#include <proc/scheduler.h>
47
#include <proc/scheduler.h>
48
#include <proc/task.h>
48
#include <proc/task.h>
49
#include <proc/thread.h>
49
#include <proc/thread.h>
-
 
50
#include <proc/program.h>
50
#include <panic.h>
51
#include <panic.h>
51
#include <func.h>
52
#include <func.h>
52
#include <cpu.h>
53
#include <cpu.h>
53
#include <arch/asm.h>
54
#include <arch/asm.h>
54
#include <mm/page.h>
55
#include <mm/page.h>
Line 61... Line 62...
61
#include <interrupt.h>
62
#include <interrupt.h>
62
#include <console/kconsole.h>
63
#include <console/kconsole.h>
63
#include <security/cap.h>
64
#include <security/cap.h>
64
#include <lib/rd.h>
65
#include <lib/rd.h>
65
#include <ipc/ipc.h>
66
#include <ipc/ipc.h>
-
 
67
#include <debug.h>
-
 
68
#include <string.h>
66
 
69
 
67
#ifdef CONFIG_SMP
70
#ifdef CONFIG_SMP
68
#include <smp/smp.h>
71
#include <smp/smp.h>
69
#endif /* CONFIG_SMP */
72
#endif /* CONFIG_SMP */
70
 
73
 
71
#include <synch/waitq.h>
74
#include <synch/waitq.h>
72
#include <synch/spinlock.h>
75
#include <synch/spinlock.h>
73
 
76
 
-
 
77
#define ALIVE_CHARS  4
-
 
78
 
-
 
79
#ifdef CONFIG_KCONSOLE
-
 
80
static char alive[ALIVE_CHARS] = "-\\|/";
-
 
81
#endif
-
 
82
 
-
 
83
#define INIT_PREFIX      "init:"
-
 
84
#define INIT_PREFIX_LEN  5
-
 
85
 
74
/** Kernel initialization thread.
86
/** Kernel initialization thread.
75
 *
87
 *
76
 * kinit takes care of higher level kernel
88
 * kinit takes care of higher level kernel
77
 * initialization (i.e. thread creation,
89
 * initialization (i.e. thread creation,
78
 * userspace initialization etc.).
90
 * userspace initialization etc.).
79
 *
91
 *
80
 * @param arg Not used.
92
 * @param arg Not used.
81
 */
93
 */
82
void kinit(void *arg)
94
void kinit(void *arg)
83
{
95
{
84
    thread_t *t;
-
 
85
 
96
 
-
 
97
#if defined(CONFIG_SMP) || defined(CONFIG_KCONSOLE)
-
 
98
    thread_t *thread;
-
 
99
#endif
-
 
100
   
86
    /*
101
    /*
87
     * Detach kinit as nobody will call thread_join_timeout() on it.
102
     * Detach kinit as nobody will call thread_join_timeout() on it.
88
     */
103
     */
89
    thread_detach(THREAD);
104
    thread_detach(THREAD);
90
 
105
   
91
    interrupts_disable();
106
    interrupts_disable();
92
 
107
   
93
#ifdef CONFIG_SMP           
108
#ifdef CONFIG_SMP
94
    if (config.cpu_count > 1) {
109
    if (config.cpu_count > 1) {
95
        waitq_initialize(&ap_completion_wq);
110
        waitq_initialize(&ap_completion_wq);
96
        /*
111
        /*
97
         * Create the kmp thread and wait for its completion.
112
         * Create the kmp thread and wait for its completion.
98
         * cpu1 through cpuN-1 will come up consecutively and
113
         * cpu1 through cpuN-1 will come up consecutively and
99
         * not mess together with kcpulb threads.
114
         * not mess together with kcpulb threads.
100
         * Just a beautification.
115
         * Just a beautification.
101
         */
116
         */
102
        if ((t = thread_create(kmp, NULL, TASK, THREAD_FLAG_WIRED,
117
        thread = thread_create(kmp, NULL, TASK, THREAD_FLAG_WIRED, "kmp", true);
103
            "kmp", true))) {
118
        if (thread != NULL) {
104
            spinlock_lock(&t->lock);
119
            spinlock_lock(&thread->lock);
105
            t->cpu = &cpus[0];
120
            thread->cpu = &cpus[0];
106
            spinlock_unlock(&t->lock);
121
            spinlock_unlock(&thread->lock);
107
            thread_ready(t);
122
            thread_ready(thread);
108
        } else
123
        } else
109
            panic("thread_create/kmp\n");
124
            panic("Unable to create kmp thread.");
110
        thread_join(t);
125
        thread_join(thread);
111
        thread_detach(t);
126
        thread_detach(thread);
112
    }
127
    }
113
#endif /* CONFIG_SMP */
-
 
114
    /*
-
 
115
     * Now that all CPUs are up, we can report what we've found.
-
 
116
     */
-
 
117
    cpu_list();
-
 
118
 
128
   
119
#ifdef CONFIG_SMP
-
 
120
    if (config.cpu_count > 1) {
129
    if (config.cpu_count > 1) {
121
        count_t i;
130
        count_t i;
122
       
131
       
123
        /*
132
        /*
124
         * For each CPU, create its load balancing thread.
133
         * For each CPU, create its load balancing thread.
125
         */
134
         */
126
        for (i = 0; i < config.cpu_count; i++) {
135
        for (i = 0; i < config.cpu_count; i++) {
127
 
-
 
128
            if ((t = thread_create(kcpulb, NULL, TASK,
136
            thread = thread_create(kcpulb, NULL, TASK, THREAD_FLAG_WIRED, "kcpulb", true);
129
                THREAD_FLAG_WIRED, "kcpulb", true))) {
137
            if (thread != NULL) {
130
                spinlock_lock(&t->lock);           
138
                spinlock_lock(&thread->lock);
131
                t->cpu = &cpus[i];
139
                thread->cpu = &cpus[i];
132
                spinlock_unlock(&t->lock);
140
                spinlock_unlock(&thread->lock);
133
                thread_ready(t);
141
                thread_ready(thread);
134
            } else
142
            } else
135
                panic("thread_create/kcpulb\n");
143
                printf("Unable to create kcpulb thread for cpu" PRIc "\n", i);
136
 
-
 
137
        }
144
        }
138
    }
145
    }
139
#endif /* CONFIG_SMP */
146
#endif /* CONFIG_SMP */
140
 
147
   
141
    /*
148
    /*
142
     * At this point SMP, if present, is configured.
149
     * At this point SMP, if present, is configured.
143
     */
150
     */
144
    arch_post_smp_init();
151
    arch_post_smp_init();
145
 
152
   
-
 
153
#ifdef CONFIG_KCONSOLE
-
 
154
    if (stdin) {
-
 
155
        /*
-
 
156
         * Create kernel console.
-
 
157
         */
-
 
158
        thread = thread_create(kconsole_thread, NULL, TASK, 0, "kconsole", false);
-
 
159
        if (thread != NULL)
-
 
160
            thread_ready(thread);
-
 
161
        else
-
 
162
            printf("Unable to create kconsole thread\n");
-
 
163
    }
-
 
164
#endif /* CONFIG_KCONSOLE */
-
 
165
   
-
 
166
    interrupts_enable();
-
 
167
   
146
    /*
168
    /*
147
     * Create kernel console.
169
     * Create user tasks, load RAM disk images.
148
     */
170
     */
149
    t = thread_create(kconsole, (void *) "kconsole", TASK, 0, "kconsole", false);
-
 
150
    if (t)
-
 
151
        thread_ready(t);
-
 
152
    else
-
 
153
        panic("thread_create/kconsole\n");
-
 
154
 
-
 
155
    interrupts_enable();
-
 
156
 
-
 
157
    count_t i;
171
    count_t i;
-
 
172
    program_t programs[CONFIG_INIT_TASKS];
-
 
173
   
158
    for (i = 0; i < init.cnt; i++) {
174
    for (i = 0; i < init.cnt; i++) {
159
        /*
-
 
160
         * Run user tasks, load RAM disk images.
-
 
161
         */
-
 
162
       
-
 
163
        if (init.tasks[i].addr % FRAME_SIZE) {
175
        if (init.tasks[i].addr % FRAME_SIZE) {
164
            printf("init[%d].addr is not frame aligned", i);
176
            printf("init[%" PRIc "].addr is not frame aligned\n", i);
165
            continue;
177
            continue;
166
        }
178
        }
167
 
-
 
168
        task_t *utask = task_run_program((void *) init.tasks[i].addr,
-
 
169
            "uspace");
-
 
170
       
179
       
-
 
180
        /*
-
 
181
         * Construct task name from the 'init:' prefix and the
-
 
182
         * name stored in the init structure (if any).
-
 
183
         */
-
 
184
       
-
 
185
        char namebuf[TASK_NAME_BUFLEN];
-
 
186
        char *name;
-
 
187
       
-
 
188
        name = init.tasks[i].name;
171
        if (utask) {
189
        if (name[0] == '\0')
-
 
190
            name = "<unknown>";
-
 
191
       
-
 
192
        ASSERT(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN);
-
 
193
        strncpy(namebuf, INIT_PREFIX, TASK_NAME_BUFLEN);
-
 
194
        strncpy(namebuf + INIT_PREFIX_LEN, name,
-
 
195
            TASK_NAME_BUFLEN - INIT_PREFIX_LEN);
-
 
196
       
-
 
197
        int rc = program_create_from_image((void *) init.tasks[i].addr,
-
 
198
            namebuf, &programs[i]);
-
 
199
       
-
 
200
        if ((rc == 0) && (programs[i].task != NULL)) {
172
            /*
201
            /*
173
             * Set capabilities to init userspace tasks.
202
             * Set capabilities to init userspace tasks.
174
             */
203
             */
175
            cap_set(utask, CAP_CAP | CAP_MEM_MANAGER |
204
            cap_set(programs[i].task, CAP_CAP | CAP_MEM_MANAGER |
176
                CAP_IO_MANAGER | CAP_PREEMPT_CONTROL | CAP_IRQ_REG);
205
                CAP_IO_MANAGER | CAP_PREEMPT_CONTROL | CAP_IRQ_REG);
177
           
206
           
178
            if (!ipc_phone_0)
207
            if (!ipc_phone_0)
179
                ipc_phone_0 = &utask->answerbox;
208
                ipc_phone_0 = &programs[i].task->answerbox;
-
 
209
        } else if (rc == 0) {
-
 
210
            /* It was the program loader and was registered */
180
        } else {
211
        } else {
-
 
212
            /* RAM disk image */
181
            int rd = init_rd((rd_header *) init.tasks[i].addr,
213
            int rd = init_rd((rd_header_t *) init.tasks[i].addr, init.tasks[i].size);
182
                init.tasks[i].size);
-
 
183
           
214
           
184
            if (rd != RE_OK)
215
            if (rd != RE_OK)
185
                printf("Init binary %zd not used, error code %d.\n", i, rd);
216
                printf("Init binary %" PRIc " not used (error %d)\n", i, rd);
186
        }
217
        }
187
    }
218
    }
188
 
219
   
-
 
220
    /*
-
 
221
     * Run user tasks with small delays
-
 
222
     * to avoid intermixed klog output.
-
 
223
     *
-
 
224
     * TODO: This certainly does not guarantee
-
 
225
     *       anything, it just works in most of the
-
 
226
     *       cases. Some better way how to achieve
-
 
227
     *       nice klog output should be found.
-
 
228
     */
-
 
229
    for (i = 0; i < init.cnt; i++) {
-
 
230
        if (programs[i].task != NULL) {
-
 
231
            program_ready(&programs[i]);
-
 
232
            thread_usleep(10000);
-
 
233
        }
-
 
234
    }
189
 
235
   
-
 
236
#ifdef CONFIG_KCONSOLE
190
    if (!stdin) {
237
    if (!stdin) {
-
 
238
        thread_sleep(10);
-
 
239
        printf("kinit: No stdin\nKernel alive: .");
-
 
240
       
-
 
241
        unsigned int i = 0;
191
        while (1) {
242
        while (true) {
-
 
243
            printf("\b%c", alive[i % ALIVE_CHARS]);
192
            thread_sleep(1);
244
            thread_sleep(1);
193
            printf("kinit... ");
245
            i++;
194
        }
246
        }
195
    }
247
    }
-
 
248
#endif /* CONFIG_KCONSOLE */
196
}
249
}
197
 
250
 
198
/** @}
251
/** @}
199
 */
252
 */