Subversion Repositories HelenOS-historic

Rev

Rev 26 | Rev 32 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
#include <arch/asm.h>
30
#include <arch/context.h>
31
#include <print.h>
32
#include <config.h>
33
#include <time/clock.h>
34
#include <proc/scheduler.h>
35
#include <proc/thread.h>
36
#include <proc/task.h>
37
#include <mm/vm.h>
38
#include <main/kinit.h>
39
#include <cpu.h>
40
#include <mm/heap.h>
41
 
42
#ifdef __SMP__
11 jermar 43
#include <arch/smp/apic.h>
44
#include <arch/smp/mp.h>
1 jermar 45
#endif /* __SMP__ */
46
 
47
#include <mm/frame.h>
48
#include <mm/page.h>
5 jermar 49
#include <mm/tlb.h>
1 jermar 50
#include <synch/waitq.h>
51
 
52
#include <arch.h>
53
 
54
char *project = "SPARTAN kernel";
55
char *copyright = "Copyright (C) 2001-2005 Jakub Jermar";
56
 
57
config_t config;
58
context_t ctx;
59
 
60
/*
61
 * These 'hardcoded' variables will be intialised by
62
 * the linker with appropriate sizes and addresses.
63
 */
64
__u32 hardcoded_load_address = 0;
65
__u32 hardcoded_ktext_size = 0;
66
__u32 hardcoded_kdata_size = 0;
67
 
68
void main_bsp(void);
69
void main_ap(void);
70
 
71
/*
72
 * These two functions prevent stack from underflowing during the
73
 * kernel boot phase when SP is set to the very top of the reserved
74
 * space. The stack could get corrupted by a fooled compiler-generated
75
 * pop sequence otherwise.
76
 */
77
static void main_bsp_separated_stack(void);
78
static void main_ap_separated_stack(void);
79
 
80
/*
81
 * Executed by the bootstrap processor. cpu_priority_high()'d
82
 */
83
void main_bsp(void)
84
{
85
    config.cpu_count = 1;
86
    config.cpu_active = 1;
87
 
88
    config.base = hardcoded_load_address;
89
    config.memory_size = CONFIG_MEMORY_SIZE;
90
    config.kernel_size = hardcoded_ktext_size + hardcoded_kdata_size + CONFIG_HEAP_SIZE + CONFIG_STACK_SIZE;
91
 
92
    context_save(&ctx);
93
    ctx.sp = config.base + config.kernel_size - 8;
94
    ctx.pc = (__address) main_bsp_separated_stack;
95
    context_restore(&ctx);
96
    /* not reached */
97
}
98
 
99
void main_bsp_separated_stack(void) {
100
    vm_t *m;
101
    task_t *k;
102
    thread_t *t;
103
 
26 jermar 104
    arch_pre_mm_init();
1 jermar 105
 
23 jermar 106
    heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_HEAP_SIZE);
107
    frame_init();
108
    page_init();
109
    tlb_init();
16 jermar 110
 
23 jermar 111
    arch_post_mm_init();
112
 
1 jermar 113
    printf("%s, %s\n", project, copyright);
114
 
115
    printf("%L: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n",
116
        config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024);
117
 
118
    #ifdef __SMP__
119
    mp_init();  /* Multiprocessor */
120
    #endif /* __SMP__ */
27 jermar 121
 
122
    printf("config.cpu_count=%d\n", config.cpu_count);
1 jermar 123
 
124
    cpu_init();
125
    calibrate_delay_loop();
126
 
127
    timeout_init();
128
    scheduler_init();
129
    task_init();
130
    thread_init();
131
 
132
    /*
133
     * Create kernel vm mapping.
134
     */
135
    m = vm_create();
136
    if (!m) panic("can't create kernel vm address space\n");
137
 
138
    /*
139
     * Create kernel task.
140
     */
141
    k = task_create(m);
142
    if (!k) panic("can't create kernel task\n");
143
 
144
    /*
145
     * Create the first thread.
146
     */
147
    t = thread_create(kinit, NULL, k, 0);
148
    if (!t) panic("can't create kinit thread\n");
149
 
150
    thread_ready(t);
151
 
152
    /*
153
     * This call to scheduler() will return to kinit,
154
     * starting the thread of kernel threads.
155
     */
156
    scheduler();
157
    /* not reached */
158
}
159
 
160
#ifdef __SMP__
161
/*
162
 * Executed by application processors. cpu_priority_high()'d
163
 * Temporary stack is at ctx.sp which was set during BP boot.
164
 */
165
void main_ap(void)
166
{
167
    /*
168
     * Incrementing the active CPU counter will guarantee that the
169
     * pm_init() will not attempt to build GDT and IDT tables again.
170
     * Neither frame_init() will do the complete thing. Neither cpu_init()
171
     * will do.
172
     */
173
    config.cpu_active++;
174
 
26 jermar 175
    arch_pre_mm_init();
1 jermar 176
    frame_init();
177
    page_init();
26 jermar 178
    arch_post_mm_init();
1 jermar 179
 
180
    cpu_init();
181
    calibrate_delay_loop();
182
 
183
    l_apic_init();
184
    l_apic_debug();
185
 
186
 
187
    /*
188
     * If we woke kmp up before we left the kernel stack, we could
189
     * collide with another CPU coming up. To prevent this, we
190
     * switch to this cpu's private stack prior to waking kmp up.
191
     */
15 jermar 192
    CPU->saved_context.sp = (__address) &CPU->stack[CPU_STACK_SIZE-8];
193
    CPU->saved_context.pc = (__address) main_ap_separated_stack;
194
    context_restore(&CPU->saved_context);
1 jermar 195
    /* not reached */
196
}
197
 
198
void main_ap_separated_stack(void)
199
{
200
    /*
201
     * Configure timeouts for this cpu.
202
     */
203
    timeout_init();
204
 
205
    waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
206
    scheduler();
207
    /* not reached */
208
}
209
#endif /* __SMP__*/