summaryrefslogtreecommitdiff
path: root/kernel/kernel.c
blob: 5e52a6874a365f4ce1d2b654fe0935a09c62fb75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#define FOOLOS_MODULE_NAME "kernel"

#ifdef __linux__
#error "Watchout! this is not Linux but FoolOS. Use a cross-compiler"
#endif

#include <stdint.h>

#include "config.h"

#include "lib/logger/log.h"

#include "timer.h"
#include "mem.h"
#include "vmem.h"
#include "interrupts.h"

#include "syscalls.h" // for syscall_execve


#include "console.h"


// for built-in shell
#include "lib/buffer/ringbuffer.h"
#include "task.h"


// CODE FOR Stack Smashing Protector, TODO: MOVE / and do not duplicate
// with sys.c
// http://wiki.osdev.org/Stack_Smashing_Protector
 
#if UINT32_MAX == UINTPTR_MAX
#define STACK_CHK_GUARD 0xe2dee396
#else
#define STACK_CHK_GUARD 0x595e9fbd94fda766
#endif
 
uintptr_t __stack_chk_guard = STACK_CHK_GUARD;
 
__attribute__((noreturn))
void __stack_chk_fail(void)
{
	panic(FOOLOS_MODULE_NAME,"Stack smashing detected");
}
//

// mp informs us if this if this is the main processor
void kernel_main(uint32_t initial_stack, int mp)
{
    //
    // Configuring the PIT timer.
    //
    timer_init();

    //
    //	Memory Init
    //
    //  after this is set up we will be able to allocate and deallocate 
    //  blocks of physical memory :)
    //
    //	we know that here, the bootloader placed the memory map and
    //	the number of entries.
    //
    mem_init(0xa001,*((uint16_t *)(0xa000)));

    //
    // Activate Virtual Memory (paging)
    // 0x8048000 is where user programms start!
    vmem_init();

    //
    // init output to screen
    // 
    console_init();


    // log buffered messages to console
    log_log();

    //
    // Setup Interrupts (code segment: 0x08)
    //
    int_init(0x08);

    //
    // Gather Info about other processors. (APs)
    // ACPI or MP
    //
    //

    /*
    smp_processors procdata;    

    if(!acpi_find(&procdata))
	if(!mp_find(&procdata))
	    panic(FOOLOS_MODULE_NAME,"ACPI and MP search failed! I do not want to continue!");
    */

    task_init();
    while(1);
   
    // load and run foolshell
    // we will come back into the kernel only on interrupts...
/*
    
    static char *argv[]={"/bin/foolshell",NULL};
    static char *env[]={"PATH=/bin","PWD=/home/miguel","PS1=$ ",NULL};
    syscall_execve("/bin/foolshell",argv,env); 
  */  


    // 
    // Start the other Processors (also before paging for some reason!)
    //
    //smp_log_procdata(&procdata);
    //smp_start_aps(&procdata,0x80000);	// starts at 0x80000 
					// but it will be copied over mbr
					

    //
    // Scan the PCI Bus
    // 
    // We are interested in the E1000 Network Adapter in particular
    // Its driver will be hopefully implemented one day ;) TODO
    //
    //pci_init();

    //
    // Initialize Multitasking
    //
    // For now this starts three "tasks" which are scheduled 
    // round robin style.
    //

    //task_init();

    // Just hang around here, if its reached. 
    // we do our tasks anyway. on the next clock tick.
    // while(1);
   
 
}