source: mainline/generic/src/main/main.c@ d3e7ff4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d3e7ff4 was 6d9c49a, checked in by Ondrej Palkovsky <ondrap@…>, 20 years ago

Added kernel IPC functionality.

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[f761f1eb]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>
[d5d2a3f]30#include <context.h>
[f761f1eb]31#include <print.h>
[02a99d2]32#include <panic.h>
[940cac0]33#include <debug.h>
[f761f1eb]34#include <config.h>
35#include <time/clock.h>
36#include <proc/scheduler.h>
37#include <proc/thread.h>
38#include <proc/task.h>
39#include <main/kinit.h>
[5fe5f1e]40#include <main/version.h>
[f4338d2]41#include <console/kconsole.h>
[f761f1eb]42#include <cpu.h>
[e0cdb7b6]43#include <align.h>
[aace6624]44#include <interrupt.h>
[a55f97f]45#include <arch/mm/memory_init.h>
[f761f1eb]46#include <mm/frame.h>
47#include <mm/page.h>
[6d7ffa65]48#include <genarch/mm/page_pt.h>
[169587a]49#include <mm/tlb.h>
[20d50a1]50#include <mm/as.h>
[4e147a6]51#include <mm/slab.h>
[f761f1eb]52#include <synch/waitq.h>
[393f631]53#include <arch/arch.h>
[f761f1eb]54#include <arch.h>
[f2ffad4]55#include <arch/faddr.h>
[ac5d02b]56#include <typedefs.h>
[6d9c49a]57#include <ipc/ipc.h>
[ac5d02b]58
[5fe5f1e]59#ifdef CONFIG_SMP
60#include <arch/smp/apic.h>
61#include <arch/smp/mps.h>
62#endif /* CONFIG_SMP */
63#include <smp/smp.h>
64
65config_t config; /**< Global configuration structure. */
66
[f761f1eb]67context_t ctx;
68
[880de6e]69/**
70 * These 'hardcoded' variables will be intialized by
[dcbc8be]71 * the linker or the low level assembler code with
72 * appropriate sizes and addresses.
[f761f1eb]73 */
[2217ac3]74__address hardcoded_load_address = 0;
[ac5d02b]75size_t hardcoded_ktext_size = 0;
76size_t hardcoded_kdata_size = 0;
[f761f1eb]77
[aa72859]78__address init_addr = 0;
79size_t init_size = 0;
80
[f761f1eb]81void main_bsp(void);
82void main_ap(void);
83
84/*
85 * These two functions prevent stack from underflowing during the
86 * kernel boot phase when SP is set to the very top of the reserved
87 * space. The stack could get corrupted by a fooled compiler-generated
88 * pop sequence otherwise.
89 */
90static void main_bsp_separated_stack(void);
[80d2bdb]91#ifdef CONFIG_SMP
[f761f1eb]92static void main_ap_separated_stack(void);
[80d2bdb]93#endif
[f761f1eb]94
[673104e]95/** Bootstrap CPU main kernel routine
96 *
97 * Initializes the kernel by bootstrap CPU.
[5fe5f1e]98 * This function passes control directly to
99 * main_bsp_separated_stack().
[673104e]100 *
[22f7769]101 * Assuming interrupts_disable().
[673104e]102 *
[f761f1eb]103 */
104void main_bsp(void)
105{
[085d973]106 __address stackaddr;
107
[f761f1eb]108 config.cpu_count = 1;
109 config.cpu_active = 1;
[d6e8529]110
[aed4eca]111 config.base = hardcoded_load_address;
112 config.memory_size = get_memory_size();
[6c68b97]113 config.init_addr = init_addr;
114 config.init_size = init_size;
[393f631]115
[085d973]116 config.kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size, PAGE_SIZE);
117 stackaddr = config.base + config.kernel_size;
118 /* Avoid placing kernel on top of init */
[bb68433]119 if (overlaps(stackaddr,CONFIG_STACK_SIZE,
120 config.init_addr, config.init_size)) {
[085d973]121
122 stackaddr = ALIGN_UP(config.init_addr+config.init_size,
123 CONFIG_STACK_SIZE);
124 config.init_size = ALIGN_UP(config.init_size,CONFIG_STACK_SIZE) + CONFIG_STACK_SIZE;
125 } else {
126 config.kernel_size += CONFIG_STACK_SIZE;
127 }
[393f631]128
[be50915]129 context_save(&ctx);
[085d973]130 context_set(&ctx, FADDR(main_bsp_separated_stack),
131 stackaddr, CONFIG_STACK_SIZE);
[be50915]132 context_restore(&ctx);
[f761f1eb]133 /* not reached */
134}
135
[673104e]136
137/** Bootstrap CPU main kernel routine stack wrapper
138 *
139 * Second part of main_bsp().
140 *
141 */
[fde6429]142void main_bsp_separated_stack(void)
143{
[f761f1eb]144 task_t *k;
145 thread_t *t;
[8f91729]146
[bcdd9aa]147 the_initialize(THE);
[ff3b3197]148 /*
149 * kconsole data structures must be initialized very early
150 * because other subsystems will register their respective
151 * commands.
152 */
153 kconsole_init();
[dc747e3]154
[ef67bab]155 /*
156 * Exception handler initialization, before architecture
[0132630]157 * starts adding its own handlers
[aace6624]158 */
159 exc_init();
[fc1e4f6]160
161 /*
162 * Memory management subsystems initialization.
163 */
[1fbbcd6]164 arch_pre_mm_init();
[085d973]165 /* Initialize at least 1 memory segment big enough for slab to work */
[7eade45]166 frame_init();
[8e1ea655]167 slab_cache_init();
[ef67bab]168 as_init();
[7eade45]169 page_init();
170 tlb_init();
[085d973]171 arch_post_mm_init();
[0132630]172 version_print();
173
[da79d0fd]174 printf("%P: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n",
[f761f1eb]175 config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024);
176
[7453929]177 arch_pre_smp_init();
[ed0dd65]178 smp_init();
[c5613b72]179 /* Slab must be initialized AFTER we know the number of processors */
[8e1ea655]180 slab_enable_cpucache();
[c5613b72]181
[11485dec]182 printf("config.memory_size=%dM\n", config.memory_size/(1024*1024));
[c9b8c5c]183 printf("config.cpu_count=%d\n", config.cpu_count);
[f761f1eb]184 cpu_init();
[6f8a426]185
[f761f1eb]186 calibrate_delay_loop();
187 timeout_init();
188 scheduler_init();
189 task_init();
190 thread_init();
[6c68b97]191
192 if (config.init_size > 0)
[49a39c2]193 printf("config.init_addr=%P, config.init_size=%d\n", config.init_addr, config.init_size);
[6d9c49a]194
195 ipc_init();
[f761f1eb]196 /*
197 * Create kernel task.
198 */
[ef67bab]199 k = task_create(AS_KERNEL);
[393f631]200 if (!k)
201 panic("can't create kernel task\n");
[6f8a426]202
[f761f1eb]203 /*
204 * Create the first thread.
205 */
206 t = thread_create(kinit, NULL, k, 0);
[393f631]207 if (!t)
208 panic("can't create kinit thread\n");
[f761f1eb]209 thread_ready(t);
[6f8a426]210
[f761f1eb]211 /*
212 * This call to scheduler() will return to kinit,
213 * starting the thread of kernel threads.
214 */
215 scheduler();
216 /* not reached */
217}
218
[673104e]219
[5f85c91]220#ifdef CONFIG_SMP
[673104e]221/** Application CPUs main kernel routine
222 *
223 * Executed by application processors, temporary stack
224 * is at ctx.sp which was set during BP boot.
[5fe5f1e]225 * This function passes control directly to
226 * main_ap_separated_stack().
[673104e]227 *
[22f7769]228 * Assuming interrupts_disable()'d.
[673104e]229 *
[f761f1eb]230 */
231void main_ap(void)
232{
233 /*
234 * Incrementing the active CPU counter will guarantee that the
235 * pm_init() will not attempt to build GDT and IDT tables again.
236 * Neither frame_init() will do the complete thing. Neither cpu_init()
237 * will do.
238 */
239 config.cpu_active++;
240
[7ce9284]241 /*
242 * The THE structure is well defined because ctx.sp is used as stack.
243 */
244 the_initialize(THE);
[c0b45fa]245
[f07bba5]246 arch_pre_mm_init();
[f761f1eb]247 frame_init();
248 page_init();
[ce031f0]249 tlb_init();
[f07bba5]250 arch_post_mm_init();
[c0b45fa]251
[f761f1eb]252 cpu_init();
[c0b45fa]253
[f761f1eb]254 calibrate_delay_loop();
255
256 l_apic_init();
257 l_apic_debug();
258
[7ce9284]259 the_copy(THE, (the_t *) CPU->stack);
[f761f1eb]260
261 /*
262 * If we woke kmp up before we left the kernel stack, we could
263 * collide with another CPU coming up. To prevent this, we
264 * switch to this cpu's private stack prior to waking kmp up.
265 */
[4b2c872d]266 context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
[be50915]267 context_restore(&CPU->saved_context);
[f761f1eb]268 /* not reached */
269}
270
[673104e]271
272/** Application CPUs main kernel routine stack wrapper
273 *
274 * Second part of main_ap().
275 *
276 */
[f761f1eb]277void main_ap_separated_stack(void)
278{
279 /*
280 * Configure timeouts for this cpu.
281 */
282 timeout_init();
283
284 waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
285 scheduler();
286 /* not reached */
287}
[5f85c91]288#endif /* CONFIG_SMP */
Note: See TracBrowser for help on using the repository browser.