Subversion Repositories HelenOS

Rev

Rev 1229 | Rev 1315 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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