-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.c
246 lines (194 loc) · 7.47 KB
/
system.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*****************************************************************************/
/* File: system.c */
/* */
/* Description: Source file for x86 system information. */
/* Contains E820 map and VGA output code. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: Mar 28, 2020 */
/* */
/*****************************************************************************/
//
// Reserved memory aside from E820
//
//
#include "system.h"
#include "util.h"
#include "lock.h"
#include "debug.h"
#include "keyboard.h"
#define PIT_FREQUENCY 1000
short *vga_buf_ptr;
spinlock spinlock_vga;
int ebda;
#define IOPORT_PS2_DATA 0x60
#define IOPORT_PS2_STATUS 0x64
#define IOPORT_PS2_COMMAND 0x64
void vga_init() {
INIT_SPIN_LOCK(&spinlock_vga);
vga_buf_ptr = (short*)VIDEO_VIRT_BUFFER;
}
void devices_init() {
keyboard_init();
}
void device_enable_interrupts() {
keyboard_enable_interrupt();
}
//
// Outputs a NULL terminated string in VGA buffer
//
void print_vga(char *c) {
spinlock_lock(&spinlock_vga);
while(*c) {
if (*c == '\n') {
vga_buf_ptr += (160-(((int)vga_buf_ptr-VIDEO_VIRT_BUFFER)%160))/sizeof(short);
c++;
continue;
}
if (vga_buf_ptr >= (short*)(VIDEO_VIRT_BUFFER+(160*25)))
break;
*vga_buf_ptr = ((0xF << 8) | *c);
c++;
vga_buf_ptr++;
}
spinlock_unlock(&spinlock_vga);
}
void print_vga_fixed(char *c, int col, int row) {
unsigned short *p = (unsigned short *)((char*)VIDEO_VIRT_BUFFER+col+(row*160));
while(*c) {
if (row > 25) break;
*p = ((0xF << 8) | *c);
c++;
col += 2;
p++;
if (col == 160) {
row++;
col = 0;
}
}
}
//
// Dumps E820 map
//
void dump_e820() {
u32 i;
u32 e820_count= *((int *) E820_MAP_VIRT_COUNT);
struct e820_map *e820 = (struct e820_map *) E820_MAP_VIRT_ADDRESS;
printf(KERNEL_DEBUG, "E820 map at %p\n", e820);
printf(KERNEL_DEBUG, "===========================================================\n");
printf(KERNEL_DEBUG, "Base address | Length | Type\n");
for(i=0;i<e820_count;i++, e820++) {
// CONVERT_64
printf(KERNEL_DEBUG, "0x%llx | 0x%llx | %d %s\n", (long long)e820->base, (long long)e820->length, e820->type, (e820->type == 1) ? " Free memory" : " Reserved memory");
}
}
void mask_pic_8259() {
__asm__ __volatile__("movb $0xff, %%al;" // mask master and slave PICs
"outb %%al, $0x21;"
"outb %%al, $0xa1;"
:
:
: "%eax");
}
void init_pic_8259() {
__asm__ __volatile__("movb $0xff, %%al;" // mask master and slave PICs
"outb %%al, $0x21;"
"outb %%al, $0xa1;"
"movb $0x11, %%al;" // init PIC1
"outb %%al, $0x20;"
"movb $0x20, %%al;" // map vector 0x20-0x27 for IRQ 0-7
"outb %%al, $0x21;"
"movb $0x4, %%al;" // cascade slave at IRQ 2
"outb %%al, $0x21;"
"movb $0x1, %%al;" // Regular EOI for PIC1
"outb %%al, $0x21;"
"movb $0x11, %%al;" // init PIC2
"outb %%al, $0xa0;"
"movb $0x28, %%al;" // map vector 0x28-0x2f for IRQ 8-15
"outb %%al, $0xa1;"
"movb $0x2, %%al;" // inform PIC2 that IRQ2 is the connected at PIC1
"outb %%al, $0xa1;"
"movb $0x1, %%al;" // manual EOI for PIC2
"outb %%al, $0xa1;"
"xorl %%ecx, %%ecx;"
"movl $0xf000000, %%ecx;" // small delay
"1:;"
"loop 1b;"
"xorb %%al, %%al;" // unmask PIC1 and PIC2
"outb %%al, $0x21;"
"outb %%al, $0xa1;"
:
:
: "%eax", "%ecx"
);
}
void init_pit_frequency() {
__asm__ __volatile__("movb $0x34, %%al;" // PIT channel 0 and rate generator
"outb %%al, $0x43;" // writing to command register
"movw %0, %%ax;"
"outb %%al, $0x40;" // writing low byte of frequency divisor
"rolw $8, %%ax;"
"outb %%al, $0x40;" // then high byte
:
: "i" (PIT_FREQUENCY)
: "%eax"
);
}
extern int sched_tick;
void pit_wait(int cycles) {
int status_reg = 0;
int counter = 0;
// uses PIT channel 2 and mode 0 to wait for cycles.
__asm__ __volatile__("cli;"
"movb $0xb0, %%al;" // PIT channel 2 and mode 0
"outb %%al, $0x43;" // writing to command register
"movw %w0, %%ax;"
"outb %%al, $0x42;" // low byte to channel 2
"rolw $8, %%ax;"
"outb %%al, $0x42;" // then high byte
"1:"
"cli;"
"movb $0xe8, %%al;" // read back for status
"outb %%al, $0x43;" // writing to command register
"inb $0x42, %%al;" // read status from channel 2
"movb %%al, %b1;"
"andb $0x80, %%al;" // check if OUT is high
"sti;"
"jnz 1f;"
"nop;nop;nop;nop;" // give chance to PIC to handle interrupts
"incl %2;" // loop count - for debugging purpose
"jmp 1b;"
"1:"
"cli;"
: "=r"(counter), "=r"(status_reg)
: "0"(cycles)
: "%eax", "cc", "memory"
);
#ifdef DEBUG
printf(KERNEL_DEBUG, "status_reg: %x", status_reg);
printf(KERNEL_DEBUG, "counter: %x\n", counter);
#endif
}
void pit_wait_ms(int ms) {
for(int i = 0; i < ms; i++) {
pit_wait(PIT_HZ);
}
}
void bda_read_table() {
short *p = (short*)(0x40e + KERNEL_VIRT_ADDR);
ebda = ((int)(unsigned short)*p) << 4;
printf(KERNEL_INFO, "EBDA: %x ", ebda);
}
void read_msr(int msr, int *eax, int *edx) {
__asm__ __volatile__("rdmsr;"
: "=a" (*eax), "=d" (*edx)
: "c" (msr)
: "memory");
}
void write_msr(int msr, int eax, int edx) {
__asm__ __volatile__("wrmsr;"
:
: "c" (msr), "a"(eax), "d"(edx)
:);
}