Subversion Repositories HelenOS-historic

Rev

Rev 640 | Rev 673 | 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
 
29
#include <arch/asm.h>
97 jermar 30
#include <context.h>
1 jermar 31
#include <print.h>
68 decky 32
#include <panic.h>
561 decky 33
#include <debug.h>
1 jermar 34
#include <config.h>
35
#include <time/clock.h>
36
#include <proc/scheduler.h>
37
#include <proc/thread.h>
38
#include <proc/task.h>
39
#include <main/kinit.h>
518 jermar 40
#include <console/kconsole.h>
1 jermar 41
#include <cpu.h>
402 jermar 42
#include <align.h>
578 palkovsky 43
#include <interrupt.h>
1 jermar 44
 
458 decky 45
#ifdef CONFIG_SMP
11 jermar 46
#include <arch/smp/apic.h>
34 jermar 47
#include <arch/smp/mps.h>
458 decky 48
#endif /* CONFIG_SMP */
1 jermar 49
 
34 jermar 50
#include <smp/smp.h>
51
 
146 cejka 52
#include <arch/mm/memory_init.h>
373 jermar 53
#include <mm/heap.h>
1 jermar 54
#include <mm/frame.h>
55
#include <mm/page.h>
5 jermar 56
#include <mm/tlb.h>
373 jermar 57
#include <mm/vm.h>
58
 
1 jermar 59
#include <synch/waitq.h>
60
 
210 decky 61
#include <arch/arch.h>
1 jermar 62
#include <arch.h>
76 jermar 63
#include <arch/faddr.h>
1 jermar 64
 
106 jermar 65
#include <typedefs.h>
66
 
462 decky 67
char *project = "SPARTAN kernel";
561 decky 68
char *copyright = "Copyright (C) 2001-2005 HelenOS project";
69
char *release = RELEASE;
70
char *name = NAME;
621 palkovsky 71
char *arch = ARCH;
561 decky 72
 
73
#ifdef REVISION
74
    char *revision = ", revision " REVISION;
462 decky 75
#else
463 decky 76
    char *revision = "";
462 decky 77
#endif
1 jermar 78
 
561 decky 79
#ifdef TIMESTAMP
80
    char *timestamp = " on " TIMESTAMP;
81
#else
82
    char *timestamp = "";
83
#endif
84
 
1 jermar 85
config_t config;
86
context_t ctx;
87
 
523 jermar 88
/**
89
 * These 'hardcoded' variables will be intialized by
105 jermar 90
 * the linker or the low level assembler code with
91
 * appropriate sizes and addresses.
1 jermar 92
 */
37 jermar 93
__address hardcoded_load_address = 0;
106 jermar 94
size_t hardcoded_ktext_size = 0;
95
size_t hardcoded_kdata_size = 0;
1 jermar 96
 
506 decky 97
__address init_addr = 0;
98
size_t init_size = 0;
99
 
1 jermar 100
void main_bsp(void);
101
void main_ap(void);
102
 
103
/*
104
 * These two functions prevent stack from underflowing during the
105
 * kernel boot phase when SP is set to the very top of the reserved
106
 * space. The stack could get corrupted by a fooled compiler-generated
107
 * pop sequence otherwise.
108
 */
109
static void main_bsp_separated_stack(void);
625 palkovsky 110
#ifdef CONFIG_SMP
1 jermar 111
static void main_ap_separated_stack(void);
625 palkovsky 112
#endif
1 jermar 113
 
108 decky 114
/** Bootstrap CPU main kernel routine
115
 *
116
 * Initializes the kernel by bootstrap CPU.
117
 *
413 jermar 118
 * Assuming interrupts_disable().
108 decky 119
 *
1 jermar 120
 */
121
void main_bsp(void)
122
{
123
    config.cpu_count = 1;
124
    config.cpu_active = 1;
651 decky 125
 
369 jermar 126
    config.base = hardcoded_load_address;
127
    config.memory_size = get_memory_size();
628 decky 128
    config.init_addr = init_addr;
129
    config.init_size = init_size;
210 decky 130
 
651 decky 131
    if (init_size > 0)
132
        config.heap_addr = init_addr + init_size;
133
    else
134
        config.heap_addr = hardcoded_load_address + hardcoded_ktext_size + hardcoded_kdata_size;
210 decky 135
 
651 decky 136
    config.heap_size = CONFIG_HEAP_SIZE + (config.memory_size / FRAME_SIZE) * sizeof(frame_t);
137
 
138
    config.kernel_size = ALIGN_UP(config.heap_addr - hardcoded_load_address + config.heap_size, PAGE_SIZE);
139
    config.heap_delta = config.kernel_size - (config.heap_addr - hardcoded_load_address + config.heap_size);
140
    config.kernel_size = config.kernel_size + CONFIG_STACK_SIZE;
141
 
47 jermar 142
    context_save(&ctx);
651 decky 143
    context_set(&ctx, FADDR(main_bsp_separated_stack), config.base + config.kernel_size, CONFIG_STACK_SIZE);
47 jermar 144
    context_restore(&ctx);
1 jermar 145
    /* not reached */
146
}
147
 
108 decky 148
 
149
/** Bootstrap CPU main kernel routine stack wrapper
150
 *
151
 * Second part of main_bsp().
152
 *
153
 */
174 jermar 154
void main_bsp_separated_stack(void)
155
{
1 jermar 156
    vm_t *m;
157
    task_t *k;
158
    thread_t *t;
207 decky 159
 
184 jermar 160
    the_initialize(THE);
235 decky 161
 
517 jermar 162
    /*
163
     * kconsole data structures must be initialized very early
164
     * because other subsystems will register their respective
165
     * commands.
166
     */
167
    kconsole_init();
623 jermar 168
 
578 palkovsky 169
    /* Exception handler initialization, before architecture
170
     * starts adding it's own handlers
171
     */
172
    exc_init();
517 jermar 173
 
26 jermar 174
    arch_pre_mm_init();
651 decky 175
    early_heap_init(config.heap_addr, config.heap_size + config.heap_delta);
23 jermar 176
    frame_init();
177
    page_init();
178
    tlb_init();
179
    arch_post_mm_init();
651 decky 180
 
562 decky 181
    printf("%s, release %s (%s)%s\nBuilt%s for %s\n%s\n", project, release, name, revision, timestamp, arch, copyright);
77 jermar 182
    printf("%P: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n",
1 jermar 183
        config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024);
184
 
503 jermar 185
    arch_pre_smp_init();
34 jermar 186
    smp_init();
149 jermar 187
    printf("config.memory_size=%dM\n", config.memory_size/(1024*1024));
27 jermar 188
    printf("config.cpu_count=%d\n", config.cpu_count);
1 jermar 189
 
190
    cpu_init();
220 vana 191
 
1 jermar 192
    calibrate_delay_loop();
640 jermar 193
 
1 jermar 194
    timeout_init();
195
    scheduler_init();
196
    task_init();
197
    thread_init();
628 decky 198
 
199
    if (config.init_size > 0)
200
        printf("config.init_addr=%X, config.init_size=%d\n", config.init_addr, config.init_size);
1 jermar 201
 
202
    /*
203
     * Create kernel vm mapping.
204
     */
167 jermar 205
    m = vm_create(GET_PTL0_ADDRESS());
210 decky 206
    if (!m)
207
        panic("can't create kernel vm address space\n");
1 jermar 208
 
209
    /*
210
     * Create kernel task.
211
     */
212
    k = task_create(m);
210 decky 213
    if (!k)
214
        panic("can't create kernel task\n");
215
 
1 jermar 216
    /*
217
     * Create the first thread.
218
     */
219
    t = thread_create(kinit, NULL, k, 0);
210 decky 220
    if (!t)
221
        panic("can't create kinit thread\n");
1 jermar 222
    thread_ready(t);
223
    /*
224
     * This call to scheduler() will return to kinit,
225
     * starting the thread of kernel threads.
226
     */
227
    scheduler();
228
    /* not reached */
229
}
230
 
108 decky 231
 
458 decky 232
#ifdef CONFIG_SMP
108 decky 233
/** Application CPUs main kernel routine
234
 *
235
 * Executed by application processors, temporary stack
236
 * is at ctx.sp which was set during BP boot.
237
 *
413 jermar 238
 * Assuming interrupts_disable()'d.
108 decky 239
 *
1 jermar 240
 */
241
void main_ap(void)
242
{
243
    /*
244
     * Incrementing the active CPU counter will guarantee that the
245
     * pm_init() will not attempt to build GDT and IDT tables again.
246
     * Neither frame_init() will do the complete thing. Neither cpu_init()
247
     * will do.
248
     */
249
    config.cpu_active++;
250
 
192 jermar 251
    /*
252
     * The THE structure is well defined because ctx.sp is used as stack.
253
     */
254
    the_initialize(THE);
298 decky 255
 
26 jermar 256
    arch_pre_mm_init();
1 jermar 257
    frame_init();
258
    page_init();
389 jermar 259
    tlb_init();
26 jermar 260
    arch_post_mm_init();
298 decky 261
 
1 jermar 262
    cpu_init();
298 decky 263
 
1 jermar 264
    calibrate_delay_loop();
265
 
266
    l_apic_init();
267
    l_apic_debug();
268
 
192 jermar 269
    the_copy(THE, (the_t *) CPU->stack);
1 jermar 270
 
271
    /*
272
     * If we woke kmp up before we left the kernel stack, we could
273
     * collide with another CPU coming up. To prevent this, we
274
     * switch to this cpu's private stack prior to waking kmp up.
275
     */
414 jermar 276
    context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
47 jermar 277
    context_restore(&CPU->saved_context);
1 jermar 278
    /* not reached */
279
}
280
 
108 decky 281
 
282
/** Application CPUs main kernel routine stack wrapper
283
 *
284
 * Second part of main_ap().
285
 *
286
 */
1 jermar 287
void main_ap_separated_stack(void)
288
{
289
    /*
290
     * Configure timeouts for this cpu.
291
     */
292
    timeout_init();
293
 
294
    waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
295
    scheduler();
296
    /* not reached */
297
}
458 decky 298
#endif /* CONFIG_SMP */