Subversion Repositories HelenOS-historic

Rev

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

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