Subversion Repositories HelenOS-historic

Rev

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

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