Subversion Repositories HelenOS-historic

Rev

Rev 1248 | Rev 1389 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1248 Rev 1315
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/**
29
/**
30
 * @file    main.c
30
 * @file    main.c
31
 * @brief   Main initialization kernel function for all processors.
31
 * @brief   Main initialization kernel function for all processors.
32
 *
32
 *
33
 * During kernel boot, all processors, after architecture dependent
33
 * During kernel boot, all processors, after architecture dependent
34
 * initialization, start executing code found in this file. After
34
 * initialization, start executing code found in this file. After
35
 * bringing up all subsystems, control is passed to scheduler().
35
 * bringing up all subsystems, control is passed to scheduler().
36
 *
36
 *
37
 * The bootstrap processor starts executing main_bsp() while
37
 * The bootstrap processor starts executing main_bsp() while
38
 * the application processors start executing main_ap().
38
 * the application processors start executing main_ap().
39
 *
39
 *
40
 * @see scheduler()
40
 * @see scheduler()
41
 * @see main_bsp()
41
 * @see main_bsp()
42
 * @see main_ap()
42
 * @see main_ap()
43
 */
43
 */
44
 
44
 
45
#include <arch/asm.h>
45
#include <arch/asm.h>
46
#include <context.h>
46
#include <context.h>
47
#include <print.h>
47
#include <print.h>
48
#include <panic.h>
48
#include <panic.h>
49
#include <debug.h>
49
#include <debug.h>
50
#include <config.h>
50
#include <config.h>
51
#include <time/clock.h>
51
#include <time/clock.h>
52
#include <proc/scheduler.h>
52
#include <proc/scheduler.h>
53
#include <proc/thread.h>
53
#include <proc/thread.h>
54
#include <proc/task.h>
54
#include <proc/task.h>
55
#include <main/kinit.h>
55
#include <main/kinit.h>
56
#include <main/version.h>
56
#include <main/version.h>
57
#include <console/kconsole.h>
57
#include <console/kconsole.h>
58
#include <cpu.h>
58
#include <cpu.h>
59
#include <align.h>
59
#include <align.h>
60
#include <interrupt.h>
60
#include <interrupt.h>
61
#include <arch/mm/memory_init.h>
61
#include <arch/mm/memory_init.h>
62
#include <mm/frame.h>
62
#include <mm/frame.h>
63
#include <mm/page.h>
63
#include <mm/page.h>
64
#include <genarch/mm/page_pt.h>
64
#include <genarch/mm/page_pt.h>
65
#include <mm/tlb.h>
65
#include <mm/tlb.h>
66
#include <mm/as.h>
66
#include <mm/as.h>
67
#include <mm/slab.h>
67
#include <mm/slab.h>
68
#include <synch/waitq.h>
68
#include <synch/waitq.h>
69
#include <synch/futex.h>
69
#include <synch/futex.h>
70
#include <arch/arch.h>
70
#include <arch/arch.h>
71
#include <arch.h>
71
#include <arch.h>
72
#include <arch/faddr.h>
72
#include <arch/faddr.h>
73
#include <typedefs.h>
73
#include <typedefs.h>
74
#include <ipc/ipc.h>
74
#include <ipc/ipc.h>
75
#include <macros.h>
75
#include <macros.h>
76
#include <adt/btree.h>
76
#include <adt/btree.h>
77
 
77
 
78
#ifdef CONFIG_SMP
78
#ifdef CONFIG_SMP
79
#include <arch/smp/apic.h>
79
#include <arch/smp/apic.h>
80
#include <arch/smp/mps.h>
80
#include <arch/smp/mps.h>
81
#endif /* CONFIG_SMP */
81
#endif /* CONFIG_SMP */
82
#include <smp/smp.h>
82
#include <smp/smp.h>
83
 
83
 
84
config_t config;    /**< Global configuration structure. */
84
/** Global configuration structure. */
-
 
85
config_t config = {
-
 
86
    .mm_initialized = false
-
 
87
};
-
 
88
 
85
init_t init = {0};      /**< Initial user-space tasks */
89
/** Initial user-space tasks */
-
 
90
init_t init = {
-
 
91
    0
-
 
92
};
86
 
93
 
87
context_t ctx;
94
context_t ctx;
88
 
95
 
89
/**
96
/**
90
 * These 'hardcoded' variables will be intialized by
97
 * These 'hardcoded' variables will be intialized by
91
 * the linker or the low level assembler code with
98
 * the linker or the low level assembler code with
92
 * appropriate sizes and addresses.
99
 * appropriate sizes and addresses.
93
 */
100
 */
94
__address hardcoded_load_address = 0;
101
__address hardcoded_load_address = 0;
95
size_t hardcoded_ktext_size = 0;
102
size_t hardcoded_ktext_size = 0;
96
size_t hardcoded_kdata_size = 0;
103
size_t hardcoded_kdata_size = 0;
97
 
104
 
98
void main_bsp(void);
105
void main_bsp(void);
99
void main_ap(void);
106
void main_ap(void);
100
 
107
 
101
/*
108
/*
102
 * These two functions prevent stack from underflowing during the
109
 * These two functions prevent stack from underflowing during the
103
 * kernel boot phase when SP is set to the very top of the reserved
110
 * kernel boot phase when SP is set to the very top of the reserved
104
 * space. The stack could get corrupted by a fooled compiler-generated
111
 * space. The stack could get corrupted by a fooled compiler-generated
105
 * pop sequence otherwise.
112
 * pop sequence otherwise.
106
 */
113
 */
107
static void main_bsp_separated_stack(void);
114
static void main_bsp_separated_stack(void);
108
#ifdef CONFIG_SMP
115
#ifdef CONFIG_SMP
109
static void main_ap_separated_stack(void);
116
static void main_ap_separated_stack(void);
110
#endif
117
#endif
111
 
118
 
112
#define CONFIG_STACK_SIZE   ((1<<STACK_FRAMES)*STACK_SIZE)
119
#define CONFIG_STACK_SIZE   ((1<<STACK_FRAMES)*STACK_SIZE)
113
 
120
 
114
/** Main kernel routine for bootstrap CPU.
121
/** Main kernel routine for bootstrap CPU.
115
 *
122
 *
116
 * Initializes the kernel by bootstrap CPU.
123
 * Initializes the kernel by bootstrap CPU.
117
 * This function passes control directly to
124
 * This function passes control directly to
118
 * main_bsp_separated_stack().
125
 * main_bsp_separated_stack().
119
 *
126
 *
120
 * Assuming interrupts_disable().
127
 * Assuming interrupts_disable().
121
 *
128
 *
122
 */
129
 */
123
void main_bsp(void)
130
void main_bsp(void)
124
{
131
{
125
    __address stackaddr;
132
    __address stackaddr;
126
 
133
 
127
    config.cpu_count = 1;
134
    config.cpu_count = 1;
128
    config.cpu_active = 1;
135
    config.cpu_active = 1;
129
   
136
   
130
    config.base = hardcoded_load_address;
137
    config.base = hardcoded_load_address;
131
    config.memory_size = get_memory_size();
138
    config.memory_size = get_memory_size();
132
   
139
   
133
    config.kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size, PAGE_SIZE);
140
    config.kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size, PAGE_SIZE);
134
    stackaddr = config.base + config.kernel_size;
141
    stackaddr = config.base + config.kernel_size;
135
   
142
   
136
    /* Avoid placing kernel on top of init */
143
    /* Avoid placing kernel on top of init */
137
    count_t i;
144
    count_t i;
138
    bool overlap = false;
145
    bool overlap = false;
139
    for (i = 0; i < init.cnt; i++)
146
    for (i = 0; i < init.cnt; i++)
140
        if (PA_overlaps(stackaddr, CONFIG_STACK_SIZE, init.tasks[i].addr, init.tasks[i].size)) {
147
        if (PA_overlaps(stackaddr, CONFIG_STACK_SIZE, init.tasks[i].addr, init.tasks[i].size)) {
141
            stackaddr = ALIGN_UP(init.tasks[i].addr + init.tasks[i].size, CONFIG_STACK_SIZE);
148
            stackaddr = ALIGN_UP(init.tasks[i].addr + init.tasks[i].size, CONFIG_STACK_SIZE);
142
            init.tasks[i].size = ALIGN_UP(init.tasks[i].size, CONFIG_STACK_SIZE) + CONFIG_STACK_SIZE;
149
            init.tasks[i].size = ALIGN_UP(init.tasks[i].size, CONFIG_STACK_SIZE) + CONFIG_STACK_SIZE;
143
            overlap = true;
150
            overlap = true;
144
        }
151
        }
145
   
152
   
146
    if (!overlap)
153
    if (!overlap)
147
        config.kernel_size += CONFIG_STACK_SIZE;
154
        config.kernel_size += CONFIG_STACK_SIZE;
148
   
155
   
149
    context_save(&ctx);
156
    context_save(&ctx);
150
    context_set(&ctx, FADDR(main_bsp_separated_stack), stackaddr, THREAD_STACK_SIZE);
157
    context_set(&ctx, FADDR(main_bsp_separated_stack), stackaddr, THREAD_STACK_SIZE);
151
    context_restore(&ctx);
158
    context_restore(&ctx);
152
    /* not reached */
159
    /* not reached */
153
}
160
}
154
 
161
 
155
 
162
 
156
/** Main kernel routine for bootstrap CPU using new stack.
163
/** Main kernel routine for bootstrap CPU using new stack.
157
 *
164
 *
158
 * Second part of main_bsp().
165
 * Second part of main_bsp().
159
 *
166
 *
160
 */
167
 */
161
void main_bsp_separated_stack(void)
168
void main_bsp_separated_stack(void)
162
{
169
{
163
    task_t *k;
170
    task_t *k;
164
    thread_t *t;
171
    thread_t *t;
165
    count_t i;
172
    count_t i;
166
   
173
   
167
    the_initialize(THE);
174
    the_initialize(THE);
168
 
175
 
169
    /*
176
    /*
170
     * kconsole data structures must be initialized very early
177
     * kconsole data structures must be initialized very early
171
     * because other subsystems will register their respective
178
     * because other subsystems will register their respective
172
     * commands.
179
     * commands.
173
     */
180
     */
174
    kconsole_init();
181
    kconsole_init();
175
   
182
   
176
    /*
183
    /*
177
     * Exception handler initialization, before architecture
184
     * Exception handler initialization, before architecture
178
     * starts adding its own handlers
185
     * starts adding its own handlers
179
     */
186
     */
180
    exc_init();
187
    exc_init();
181
 
188
 
182
    /*
189
    /*
183
     * Memory management subsystems initialization.
190
     * Memory management subsystems initialization.
184
     */
191
     */
185
    arch_pre_mm_init();
192
    arch_pre_mm_init();
186
    frame_init();       /* Initialize at least 1 memory segment big enough for slab to work */
193
    frame_init();       /* Initialize at least 1 memory segment big enough for slab to work */
187
    slab_cache_init();
194
    slab_cache_init();
188
    btree_init();
195
    btree_init();
189
    as_init();
196
    as_init();
190
    page_init();
197
    page_init();
191
    tlb_init();
198
    tlb_init();
-
 
199
    config.mm_initialized = true;
192
    arch_post_mm_init();   
200
    arch_post_mm_init();   
193
 
201
 
194
    version_print();
202
    version_print();
195
    printf("%.*p: hardcoded_ktext_size=%zdK, hardcoded_kdata_size=%zdK\n", sizeof(__address) * 2, config.base, hardcoded_ktext_size / 1024, hardcoded_kdata_size / 1024);
203
    printf("%.*p: hardcoded_ktext_size=%zdK, hardcoded_kdata_size=%zdK\n", sizeof(__address) * 2, config.base, hardcoded_ktext_size / 1024, hardcoded_kdata_size / 1024);
196
 
204
 
197
    arch_pre_smp_init();
205
    arch_pre_smp_init();
198
    smp_init();
206
    smp_init();
199
   
207
   
200
    slab_enable_cpucache(); /* Slab must be initialized AFTER we know the number of processors */
208
    slab_enable_cpucache(); /* Slab must be initialized AFTER we know the number of processors */
201
 
209
 
202
    printf("config.memory_size=%zdM\n", config.memory_size/(1024*1024));
210
    printf("config.memory_size=%zdM\n", config.memory_size/(1024*1024));
203
    printf("config.cpu_count=%zd\n", config.cpu_count);
211
    printf("config.cpu_count=%zd\n", config.cpu_count);
204
    cpu_init();
212
    cpu_init();
205
   
213
   
206
    calibrate_delay_loop();
214
    calibrate_delay_loop();
207
    timeout_init();
215
    timeout_init();
208
    scheduler_init();
216
    scheduler_init();
209
    task_init();
217
    task_init();
210
    thread_init();
218
    thread_init();
211
    futex_init();
219
    futex_init();
212
   
220
   
213
    for (i = 0; i < init.cnt; i++)
221
    for (i = 0; i < init.cnt; i++)
214
        printf("init[%zd].addr=%.*p, init[%zd].size=%zd\n", i, sizeof(__address)*2, init.tasks[i].addr, i, init.tasks[i].size);
222
        printf("init[%zd].addr=%.*p, init[%zd].size=%zd\n", i, sizeof(__address)*2, init.tasks[i].addr, i, init.tasks[i].size);
215
   
223
   
216
    ipc_init();
224
    ipc_init();
217
 
225
 
218
    /*
226
    /*
219
     * Create kernel task.
227
     * Create kernel task.
220
     */
228
     */
221
    k = task_create(AS_KERNEL, "KERNEL");
229
    k = task_create(AS_KERNEL, "KERNEL");
222
    if (!k)
230
    if (!k)
223
        panic("can't create kernel task\n");
231
        panic("can't create kernel task\n");
224
   
232
   
225
    /*
233
    /*
226
     * Create the first thread.
234
     * Create the first thread.
227
     */
235
     */
228
    t = thread_create(kinit, NULL, k, 0, "kinit");
236
    t = thread_create(kinit, NULL, k, 0, "kinit");
229
    if (!t)
237
    if (!t)
230
        panic("can't create kinit thread\n");
238
        panic("can't create kinit thread\n");
231
    thread_ready(t);
239
    thread_ready(t);
232
   
240
   
233
    /*
241
    /*
234
     * This call to scheduler() will return to kinit,
242
     * This call to scheduler() will return to kinit,
235
     * starting the thread of kernel threads.
243
     * starting the thread of kernel threads.
236
     */
244
     */
237
    scheduler();
245
    scheduler();
238
    /* not reached */
246
    /* not reached */
239
}
247
}
240
 
248
 
241
 
249
 
242
#ifdef CONFIG_SMP
250
#ifdef CONFIG_SMP
243
/** Main kernel routine for application CPUs.
251
/** Main kernel routine for application CPUs.
244
 *
252
 *
245
 * Executed by application processors, temporary stack
253
 * Executed by application processors, temporary stack
246
 * is at ctx.sp which was set during BP boot.
254
 * is at ctx.sp which was set during BP boot.
247
 * This function passes control directly to
255
 * This function passes control directly to
248
 * main_ap_separated_stack().
256
 * main_ap_separated_stack().
249
 *
257
 *
250
 * Assuming interrupts_disable()'d.
258
 * Assuming interrupts_disable()'d.
251
 *
259
 *
252
 */
260
 */
253
void main_ap(void)
261
void main_ap(void)
254
{
262
{
255
    /*
263
    /*
256
     * Incrementing the active CPU counter will guarantee that the
264
     * Incrementing the active CPU counter will guarantee that the
257
     * pm_init() will not attempt to build GDT and IDT tables again.
265
     * pm_init() will not attempt to build GDT and IDT tables again.
258
     * Neither frame_init() will do the complete thing. Neither cpu_init()
266
     * Neither frame_init() will do the complete thing. Neither cpu_init()
259
     * will do.
267
     * will do.
260
     */
268
     */
261
    config.cpu_active++;
269
    config.cpu_active++;
262
 
270
 
263
    /*
271
    /*
264
     * The THE structure is well defined because ctx.sp is used as stack.
272
     * The THE structure is well defined because ctx.sp is used as stack.
265
     */
273
     */
266
    the_initialize(THE);
274
    the_initialize(THE);
267
   
275
   
268
    arch_pre_mm_init();
276
    arch_pre_mm_init();
269
    frame_init();
277
    frame_init();
270
    page_init();
278
    page_init();
271
    tlb_init();
279
    tlb_init();
272
    arch_post_mm_init();
280
    arch_post_mm_init();
273
   
281
   
274
    cpu_init();
282
    cpu_init();
275
   
283
   
276
    calibrate_delay_loop();
284
    calibrate_delay_loop();
277
 
285
 
278
    l_apic_init();
286
    l_apic_init();
279
    l_apic_debug();
287
    l_apic_debug();
280
 
288
 
281
    the_copy(THE, (the_t *) CPU->stack);
289
    the_copy(THE, (the_t *) CPU->stack);
282
 
290
 
283
    /*
291
    /*
284
     * If we woke kmp up before we left the kernel stack, we could
292
     * If we woke kmp up before we left the kernel stack, we could
285
     * collide with another CPU coming up. To prevent this, we
293
     * collide with another CPU coming up. To prevent this, we
286
     * switch to this cpu's private stack prior to waking kmp up.
294
     * switch to this cpu's private stack prior to waking kmp up.
287
     */
295
     */
288
    context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
296
    context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
289
    context_restore(&CPU->saved_context);
297
    context_restore(&CPU->saved_context);
290
    /* not reached */
298
    /* not reached */
291
}
299
}
292
 
300
 
293
 
301
 
294
/** Main kernel routine for application CPUs using new stack.
302
/** Main kernel routine for application CPUs using new stack.
295
 *
303
 *
296
 * Second part of main_ap().
304
 * Second part of main_ap().
297
 *
305
 *
298
 */
306
 */
299
void main_ap_separated_stack(void)
307
void main_ap_separated_stack(void)
300
{
308
{
301
    /*
309
    /*
302
     * Configure timeouts for this cpu.
310
     * Configure timeouts for this cpu.
303
     */
311
     */
304
    timeout_init();
312
    timeout_init();
305
 
313
 
306
    waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
314
    waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
307
    scheduler();
315
    scheduler();
308
    /* not reached */
316
    /* not reached */
309
}
317
}
310
#endif /* CONFIG_SMP */
318
#endif /* CONFIG_SMP */
311
 
319