summaryrefslogtreecommitdiff
path: root/kernel/kernel.c
blob: bf4daac08102e560ccfd3f8ceeadd4b2fc86185b (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#define FOOLOS_MODULE_NAME "kernel"

#include "lib/logger/log.h"	// logger facilities
#include "lib/int/stdint.h"
#include "lib/bool/bool.h"
#include "smp.h"

// TODO: WHHYY can i compile it without the includes!???

///////
// interrupt handler prototypes
// todo: move somewhere else!?
void int_clock_handler();
void int_kb_handler();
//void int_floppy_handler();

uint32_t read_eip();

void kernel_ap()
{
	uint32_t cpu_counter=0;
	uint32_t ebp;
	asm volatile("mov %%ebp,%0":"=r"(ebp));
//	log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"initial ebp: 0x%08X",ebp);
	int proc=ebp-0xffffff;
	proc/=-0x400;
	while(1)PutString("%d", proc*100,580,0b1111100000000000, (cpu_counter++));
}
//
// KERNEL MAIN 

// this is the very heart of our operating system!
//
volatile uint8_t proc;

void kernel_main(uint32_t initial_stack, int mp)
{

    // catch the APs (Application Processors)
    if(mp==1)
    {
	uint32_t ebp=0xffffff-proc*0x400;
	proc++;

	asm volatile("mov %0, %%ebp"::"r"(ebp));
        asm volatile("mov %ebp, %esp");
        asm volatile("jmp kernel_ap");
	
    }

    proc=0;

    // 
    // We want to get output to the screen as fast as possible!
    //
    // Our Fool-Boot-Loader did set up VESA already for us.
    // The desired VESA mode is hardcoded in [boot/mbr.asm].
    //
    // The [vesa_init(...)] function requires:
    //
    //	* the addresses of the vbeinfo struct
    //	* the address of the vbemodeinfo struct (for selected mode).
    //	* the address of our Fool-Font binary data.
    //
    //	The first two paramters are hardcoded in [boot/mbr.asm],
    //	while the last one is set in the Makefile. The font binary
    //	is integrated in the kernel image.
    //
    //  this function returns the physical base address of 
    //  our video memory 
    //

    uint32_t vesa_physbase=vesa_init(0x8300,0x8400,0x7600);


    //
    // Print initial address of the esp stack pointer
    //

    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"initial esp: 0x%08X",initial_stack);

    //
    // Initialize other processors (run this before entering paged mode)
    //
    //	This currently uses the MP Floating Pointer Struct.
    //	Should support APCI in future too.
    //

    smp_processors procdata1;    
    smp_processors procdata2;    
    smp_processors procdata;    

    
    // try to find acpi tables
    bool succ_acpi=acpi_find(&procdata1);

    // fallback to mp tables
    bool succ_mp=mp_find(&procdata2);

    if(!succ_acpi&&!succ_mp)
	panic(FOOLOS_MODULE_NAME,"ACPI and MP search failed! I do not want to continue!");

    if(succ_acpi)procdata=procdata1;
    else procdata=procdata2;

    // multiprocessing!

    smp_log_procdata(&procdata1);
    smp_log_procdata(&procdata2);
    smp_start_aps(&procdata);
	


    //
    //  Setup PIC
    //
    //  Do we nee this when using APIC?
    //

    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"setting up PIC.");
    pic_setup();

    //
    // Configuring the PIT timer.
    //
    
    timer_init();
    //
    //	Memory Init
    //

    // we know that here, the bootloader placed the mamory map!
    mem_init(0x7c00+0x400,*((uint16_t *)(0x7c00+0x600)));

    // paging  (pass the vesa physbase address for identity mapping)
    vmem_init(vesa_physbase);

    //
    // Interrupts
    //

    // init and interrupt decriptor table 
    int_init(0x08);

    // set default interrupts
    int_install();

    // setup some custom interrupts
    // remember that we shifted all interrupts with the pic by 32

    // install PIT interrupt handler (irq 0 => 32)
    int_install_ir(32, 0b10001110, 0x08,&int_clock_handler);

    // install keyboard interrupt handler (irq 1 => 33)
    int_install_ir(33, 0b10001110, 0x08,&int_kb_handler);

    // install floppy interrupt handler (irq 6 => 38)
    //int_install_ir(38, 0b10001110, 0x08,&int_floppy_handler);

    // now we can enable interrupts back again
    int_enable();


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

    pci_init();

    //
    // Initialize Floppy Disk
    //
    
    // floppy_init();


    // 
    // "Shell"
    //
    // Will process input from the keyboard but will be completely
    // redesigned soon.
    //
    
    shell_init();

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

//    smp_log_procdata(&procdata1);
    task_init();

    
    //
    // Just hang here. 
    //
   
    while(1) ;

 
}