Subversion Repositories HelenOS-historic

Rev

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

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