Subversion Repositories HelenOS-historic

Rev

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

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