Subversion Repositories HelenOS-historic

Rev

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