summaryrefslogtreecommitdiff
path: root/src/kernel.c
blob: 9f4fae505b315bfe7677f32108d111f4535a7ce4 (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
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>

#include "vga.h"
#include "serial.h"
#include "console.h"
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#include "interrupts.h"
#include "keyboard.h"
#include "mouse.h"
#include "pci.h"
#include "setjmp.h"

#include "kernel.h"

static jmp_buf panic_jmp_buf;

static void handle_keyboard_event( keyboard_event_t *event, void *context );
static void handle_mouse_event( mouse_event_t *event, void *context );

static bool terminate = false;

// must be first entry in kernel.bin (0x8400) as stage 2 of
// the boot loader expects the entry point to be here!
void kernel_main( void )
{	
	serial_t serial;
	serial_init( &serial );

	vga_t vga;
	vga_init( &vga );
	vga_set_color( &vga, VGA_COLOR_LIGHT_GREY );
	vga_set_background_color( &vga, VGA_COLOR_BLACK );

	console_t console;
	console_init( &console );
	console_add_vga_output( &console, &vga );
	console_add_serial_output( &console, &serial );
	
	// initialize the early console of the kernel
	stdio_set_console( &console );
	puts( "Started early kernel console" );
	printf( "Kernel code and data is at 0x%X, kernel stack at 0x%X\n", 0x8400, 0x90000 );

	puts( "Initializing interrupts" );
	interrupt_t interrupt;
	interrupts_init( &interrupt );
	
	puts( "Initializing PS/2 keyboard" );
	keyboard_t keyboard;
	keyboard_init( &keyboard, &handle_keyboard_event, (void *)&vga );
	interrupt_handler_t keyboard_interrupt_handler;
	interrupt_handler_init( &keyboard_interrupt_handler, IRQ_BASE + 0x01, &interrupt, keyboard_handle_interrupt, &keyboard );
	interrupts_register_interrupt_handler( keyboard_interrupt_handler );

	puts( "Initializing PS/2 mouse" );
	mouse_t mouse;
	mouse_init( &mouse, &handle_mouse_event, (void *)&vga );
	interrupt_handler_t mouse_interrupt_handler;
	interrupt_handler_init( &mouse_interrupt_handler, IRQ_BASE + 0x0C, &interrupt, mouse_handle_interrupt, &mouse );
	interrupts_register_interrupt_handler( mouse_interrupt_handler );

	// exit point in case of kernel panic, do this as soon as
	// possible
	if( setjmp( panic_jmp_buf ) > 0 ) {
		goto TERMINATE;
	}
	
	puts( "Enabling interrupt handing now.." );
	interrupts_enable( );

	puts( "Detecting devices via PCI.." );
	pci_controller_t pci_controller;
	pci_controller_init( &pci_controller );
	pci_controller_scan( &pci_controller );

	puts( "Running.." );
	
	// endless loop doing nothing, later we have to get events
	// here from queues and for instance print characters received
	// from the keyboard to stdout
	while( !terminate ) {
		// for now we are only interested in interrupts,
		// so we let the main thread sleep instead of
		// burning CPU..
		kernel_halt( );
	}			
	
TERMINATE:
	puts( "Deinitializing PS/2 mouse.." );
	mouse_deinit( &mouse );

	puts( "Deinitializing PS/2 keyboard.." );
	keyboard_deinit( &keyboard );
	
	puts( "Terminating" );
}

void kernel_panic( const char *format, ... )
{
	(void)printf( "\n*** KERNEL PANIC ***\n" );
	
	va_list args;
        va_start( args, format );
        (void)vprintf( format, args );
        va_end( args );

	longjmp( panic_jmp_buf, 47 );
}

static void handle_keyboard_event( keyboard_event_t *event, void *context )
{
	static int pos = 0;
	static char buf[80];
	vga_t *vga = (vga_t *)context;

	vga_hide_mouse_cursor( vga );

	if( event->type == KEYBOARD_EVENT_TYPE_KEY_PRESSED ) {
		if( event->key == KEYBOARD_KEY_ASCII ) {
			if( event->ascii_key == '\n' ) {
				buf[pos] = '\0';
				pos = 0;
				puts( "" );
				if( strcmp( buf, "help" ) == 0 ) {
					puts( "quit   - terminate os" );
					puts( "mem    - show memory usage" );
					puts( "driver - show status of drivers" );
					puts( "proc   - show process status" );  
				} else if( strcmp( buf, "quit" ) == 0 ) {
					terminate = true;
				} else if( strcmp( buf, "mem" ) == 0 ) {
					// TODO: print memory usage
				} else if( strcmp( buf, "proc" ) == 0 ) {
					// TODO: print scheduler status
				} else if( strcmp( buf, "driver" ) == 0 ) {
					// TODO: print driver information
				} else {
					printf( "ERR: Unknown pre-boot command '%s'\n", buf );
				}
			} else {
				printf( "%c", event->ascii_key );
				buf[pos++] = event->ascii_key;
			}
		}
	}
}

static void handle_mouse_event( mouse_event_t *event, void *context )
{
	vga_t *vga = (vga_t *)context;

	switch( event->type ) {
		case MOUSE_EVENT_TYPE_BUTTON_UP:
			vga_hide_mouse_cursor( vga );
			printf( "MOUSE UP %d AT X:%d, Y:%d\n", event->button, event->cursor_x,
				event->cursor_y );
			vga_show_mouse_cursor( vga );
			break;

		case MOUSE_EVENT_TYPE_BUTTON_DOWN:
			vga_hide_mouse_cursor( vga );
			printf( "MOUSE DOWN %d AT X:%d, Y:%d\n", event->button, event->cursor_x,
				event->cursor_y );
			vga_show_mouse_cursor( vga );
			break;

		case MOUSE_EVENT_TYPE_MOVE:
			vga_move_mouse_cursor( vga, event->cursor_x, event->cursor_y );
			vga_show_mouse_cursor( vga );
			break;
	}
}