Subversion Repositories HelenOS

Rev

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

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