Subversion Repositories HelenOS-historic

Rev

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

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