Subversion Repositories HelenOS-historic

Rev

Rev 105 | Rev 108 | 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>
1 jermar 33
#include <config.h>
34
#include <time/clock.h>
35
#include <proc/scheduler.h>
36
#include <proc/thread.h>
37
#include <proc/task.h>
38
#include <mm/vm.h>
39
#include <main/kinit.h>
40
#include <cpu.h>
41
#include <mm/heap.h>
42
 
43
#ifdef __SMP__
11 jermar 44
#include <arch/smp/apic.h>
34 jermar 45
#include <arch/smp/mps.h>
1 jermar 46
#endif /* __SMP__ */
47
 
34 jermar 48
#include <smp/smp.h>
49
 
1 jermar 50
#include <mm/frame.h>
51
#include <mm/page.h>
5 jermar 52
#include <mm/tlb.h>
1 jermar 53
#include <synch/waitq.h>
54
 
55
#include <arch.h>
76 jermar 56
#include <arch/faddr.h>
1 jermar 57
 
106 jermar 58
#include <typedefs.h>
59
 
1 jermar 60
char *project = "SPARTAN kernel";
78 jermar 61
char *copyright = "Copyright (C) 2001-2005 Jakub Jermar\nCopyright (C) 2005 HelenOS project";
1 jermar 62
 
63
config_t config;
64
context_t ctx;
65
 
66
/*
67
 * These 'hardcoded' variables will be intialised by
105 jermar 68
 * the linker or the low level assembler code with
69
 * appropriate sizes and addresses.
1 jermar 70
 */
37 jermar 71
__address hardcoded_load_address = 0;
106 jermar 72
size_t hardcoded_ktext_size = 0;
73
size_t hardcoded_kdata_size = 0;
1 jermar 74
 
75
void main_bsp(void);
76
void main_ap(void);
77
 
78
/*
79
 * These two functions prevent stack from underflowing during the
80
 * kernel boot phase when SP is set to the very top of the reserved
81
 * space. The stack could get corrupted by a fooled compiler-generated
82
 * pop sequence otherwise.
83
 */
84
static void main_bsp_separated_stack(void);
85
static void main_ap_separated_stack(void);
86
 
87
/*
88
 * Executed by the bootstrap processor. cpu_priority_high()'d
89
 */
90
void main_bsp(void)
91
{
92
    config.cpu_count = 1;
93
    config.cpu_active = 1;
94
 
95
    config.base = hardcoded_load_address;
96
    config.memory_size = CONFIG_MEMORY_SIZE;
97
    config.kernel_size = hardcoded_ktext_size + hardcoded_kdata_size + CONFIG_HEAP_SIZE + CONFIG_STACK_SIZE;
98
 
47 jermar 99
    context_save(&ctx);
97 jermar 100
    context_set(&ctx, FADDR(main_bsp_separated_stack), config.base + hardcoded_ktext_size + hardcoded_kdata_size + CONFIG_HEAP_SIZE, CONFIG_STACK_SIZE);
47 jermar 101
    context_restore(&ctx);
1 jermar 102
    /* not reached */
103
}
104
 
105
void main_bsp_separated_stack(void) {
106
    vm_t *m;
107
    task_t *k;
108
    thread_t *t;
109
 
26 jermar 110
    arch_pre_mm_init();
1 jermar 111
 
23 jermar 112
    heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_HEAP_SIZE);
113
    frame_init();
114
    page_init();
115
    tlb_init();
16 jermar 116
 
23 jermar 117
    arch_post_mm_init();
118
 
71 jermar 119
    printf("%s\n%s\n", project, copyright);
77 jermar 120
    printf("%P: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n",
1 jermar 121
        config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024);
122
 
32 jermar 123
    arch_late_init();
27 jermar 124
 
34 jermar 125
    smp_init();
27 jermar 126
    printf("config.cpu_count=%d\n", config.cpu_count);
1 jermar 127
 
128
    cpu_init();
129
    calibrate_delay_loop();
130
 
131
    timeout_init();
132
    scheduler_init();
133
    task_init();
134
    thread_init();
135
 
136
    /*
137
     * Create kernel vm mapping.
138
     */
139
    m = vm_create();
140
    if (!m) panic("can't create kernel vm address space\n");
141
 
142
    /*
143
     * Create kernel task.
144
     */
145
    k = task_create(m);
146
    if (!k) panic("can't create kernel task\n");
147
 
148
    /*
149
     * Create the first thread.
150
     */
151
    t = thread_create(kinit, NULL, k, 0);
152
    if (!t) panic("can't create kinit thread\n");
153
 
154
    thread_ready(t);
155
 
156
    /*
157
     * This call to scheduler() will return to kinit,
158
     * starting the thread of kernel threads.
159
     */
160
    scheduler();
161
    /* not reached */
162
}
163
 
164
#ifdef __SMP__
165
/*
166
 * Executed by application processors. cpu_priority_high()'d
167
 * Temporary stack is at ctx.sp which was set during BP boot.
168
 */
169
void main_ap(void)
170
{
171
    /*
172
     * Incrementing the active CPU counter will guarantee that the
173
     * pm_init() will not attempt to build GDT and IDT tables again.
174
     * Neither frame_init() will do the complete thing. Neither cpu_init()
175
     * will do.
176
     */
177
    config.cpu_active++;
178
 
26 jermar 179
    arch_pre_mm_init();
1 jermar 180
    frame_init();
181
    page_init();
26 jermar 182
    arch_post_mm_init();
1 jermar 183
 
184
    cpu_init();
185
    calibrate_delay_loop();
186
 
187
    l_apic_init();
188
    l_apic_debug();
189
 
190
 
191
    /*
192
     * If we woke kmp up before we left the kernel stack, we could
193
     * collide with another CPU coming up. To prevent this, we
194
     * switch to this cpu's private stack prior to waking kmp up.
195
     */
97 jermar 196
    context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), CPU->stack, CPU_STACK_SIZE);
47 jermar 197
    context_restore(&CPU->saved_context);
1 jermar 198
    /* not reached */
199
}
200
 
201
void main_ap_separated_stack(void)
202
{
203
    /*
204
     * Configure timeouts for this cpu.
205
     */
206
    timeout_init();
207
 
208
    waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
209
    scheduler();
210
    /* not reached */
211
}
212
#endif /* __SMP__*/