diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 3ca3d29..1d8a597 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -17,5 +17,6 @@ } __attribute__((packed)) IDTR; extern void initInterrupts(); +extern void setInterrupt(uint8_t vector, void* callback); #endif \ No newline at end of file diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 3ca3d29..1d8a597 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -17,5 +17,6 @@ } __attribute__((packed)) IDTR; extern void initInterrupts(); +extern void setInterrupt(uint8_t vector, void* callback); #endif \ No newline at end of file diff --git a/src/headers/ports.h b/src/headers/ports.h index 716fd0a..a47812e 100644 --- a/src/headers/ports.h +++ b/src/headers/ports.h @@ -6,4 +6,6 @@ extern void outb(uint16_t port, uint8_t val); extern uint8_t inb(uint16_t port); +extern void ioWait(); + #endif \ No newline at end of file diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 3ca3d29..1d8a597 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -17,5 +17,6 @@ } __attribute__((packed)) IDTR; extern void initInterrupts(); +extern void setInterrupt(uint8_t vector, void* callback); #endif \ No newline at end of file diff --git a/src/headers/ports.h b/src/headers/ports.h index 716fd0a..a47812e 100644 --- a/src/headers/ports.h +++ b/src/headers/ports.h @@ -6,4 +6,6 @@ extern void outb(uint16_t port, uint8_t val); extern uint8_t inb(uint16_t port); +extern void ioWait(); + #endif \ No newline at end of file diff --git a/src/headers/timer.h b/src/headers/timer.h new file mode 100644 index 0000000..e977777 --- /dev/null +++ b/src/headers/timer.h @@ -0,0 +1,6 @@ +#ifndef TIMER_H +#define TIMER_H + +extern void setupTimer(); + +#endif \ No newline at end of file diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 3ca3d29..1d8a597 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -17,5 +17,6 @@ } __attribute__((packed)) IDTR; extern void initInterrupts(); +extern void setInterrupt(uint8_t vector, void* callback); #endif \ No newline at end of file diff --git a/src/headers/ports.h b/src/headers/ports.h index 716fd0a..a47812e 100644 --- a/src/headers/ports.h +++ b/src/headers/ports.h @@ -6,4 +6,6 @@ extern void outb(uint16_t port, uint8_t val); extern uint8_t inb(uint16_t port); +extern void ioWait(); + #endif \ No newline at end of file diff --git a/src/headers/timer.h b/src/headers/timer.h new file mode 100644 index 0000000..e977777 --- /dev/null +++ b/src/headers/timer.h @@ -0,0 +1,6 @@ +#ifndef TIMER_H +#define TIMER_H + +extern void setupTimer(); + +#endif \ No newline at end of file diff --git a/src/interrupts/exceptions.c b/src/interrupts/exceptions.c new file mode 100644 index 0000000..19d8819 --- /dev/null +++ b/src/interrupts/exceptions.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include <_stdio.h> + +#define exception(exceptionNR, name) void exception##exceptionNR##Handler () {printf("exception "#name" happend!"); yields();} +#define setExceptionHandler(exceptionNR) setInterrupt(exceptionNR, &exception##exceptionNR##Handler); + +exception(0, DIVIDE_BY_ZERO); +exception(1, DEBUG); +exception(2, NON_MASKABLE_INTERRUPT); +exception(3, BREAKPOINT); +exception(4, OVERFLOW); +exception(5, BOUND_RANGE_EXCEEDED); +exception(6, INVALID_OPCODE); +exception(7, DEVIVE_NOT_AVAILABLE); +exception(8, DOUBLE_FAULT); +exception(10, INVALID_TSS); +exception(11, SEGMENT_NOT_PRESENT); +exception(12, STACK_SEGMENT_FAULT); +exception(13, GENERAL_PROTECTION_FAULT); +exception(14, PAGE_FAULT); +exception(16, X87_FLOATING_POINT_EXCEPTION); +exception(17, ALIGNMENT_CHECK); +exception(18, MACHINE_CHECK); +exception(19, SMID_FLOATING_POINT_EXCEPTION); +exception(20, VIRTUALIZATION_EXCEPTION); +exception(30, SECURITy_EXCEPTION); + + +void setupExceptions() { + printf("setting exceptions\n"); + setExceptionHandler(0); + setExceptionHandler(1); + setExceptionHandler(2); + setExceptionHandler(3); + setExceptionHandler(4); + setExceptionHandler(5); + setExceptionHandler(6); + setExceptionHandler(7); + setExceptionHandler(8); + setExceptionHandler(10); + setExceptionHandler(11); + setExceptionHandler(12); + setExceptionHandler(13); + setExceptionHandler(14); + setExceptionHandler(16); + setExceptionHandler(17); + setExceptionHandler(18); + setExceptionHandler(19); + setExceptionHandler(20); + setExceptionHandler(30); +} \ No newline at end of file diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 3ca3d29..1d8a597 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -17,5 +17,6 @@ } __attribute__((packed)) IDTR; extern void initInterrupts(); +extern void setInterrupt(uint8_t vector, void* callback); #endif \ No newline at end of file diff --git a/src/headers/ports.h b/src/headers/ports.h index 716fd0a..a47812e 100644 --- a/src/headers/ports.h +++ b/src/headers/ports.h @@ -6,4 +6,6 @@ extern void outb(uint16_t port, uint8_t val); extern uint8_t inb(uint16_t port); +extern void ioWait(); + #endif \ No newline at end of file diff --git a/src/headers/timer.h b/src/headers/timer.h new file mode 100644 index 0000000..e977777 --- /dev/null +++ b/src/headers/timer.h @@ -0,0 +1,6 @@ +#ifndef TIMER_H +#define TIMER_H + +extern void setupTimer(); + +#endif \ No newline at end of file diff --git a/src/interrupts/exceptions.c b/src/interrupts/exceptions.c new file mode 100644 index 0000000..19d8819 --- /dev/null +++ b/src/interrupts/exceptions.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include <_stdio.h> + +#define exception(exceptionNR, name) void exception##exceptionNR##Handler () {printf("exception "#name" happend!"); yields();} +#define setExceptionHandler(exceptionNR) setInterrupt(exceptionNR, &exception##exceptionNR##Handler); + +exception(0, DIVIDE_BY_ZERO); +exception(1, DEBUG); +exception(2, NON_MASKABLE_INTERRUPT); +exception(3, BREAKPOINT); +exception(4, OVERFLOW); +exception(5, BOUND_RANGE_EXCEEDED); +exception(6, INVALID_OPCODE); +exception(7, DEVIVE_NOT_AVAILABLE); +exception(8, DOUBLE_FAULT); +exception(10, INVALID_TSS); +exception(11, SEGMENT_NOT_PRESENT); +exception(12, STACK_SEGMENT_FAULT); +exception(13, GENERAL_PROTECTION_FAULT); +exception(14, PAGE_FAULT); +exception(16, X87_FLOATING_POINT_EXCEPTION); +exception(17, ALIGNMENT_CHECK); +exception(18, MACHINE_CHECK); +exception(19, SMID_FLOATING_POINT_EXCEPTION); +exception(20, VIRTUALIZATION_EXCEPTION); +exception(30, SECURITy_EXCEPTION); + + +void setupExceptions() { + printf("setting exceptions\n"); + setExceptionHandler(0); + setExceptionHandler(1); + setExceptionHandler(2); + setExceptionHandler(3); + setExceptionHandler(4); + setExceptionHandler(5); + setExceptionHandler(6); + setExceptionHandler(7); + setExceptionHandler(8); + setExceptionHandler(10); + setExceptionHandler(11); + setExceptionHandler(12); + setExceptionHandler(13); + setExceptionHandler(14); + setExceptionHandler(16); + setExceptionHandler(17); + setExceptionHandler(18); + setExceptionHandler(19); + setExceptionHandler(20); + setExceptionHandler(30); +} \ No newline at end of file diff --git a/src/interrupts/interrupts.c b/src/interrupts/interrupts.c index 74d2931..0c9e2b1 100644 --- a/src/interrupts/interrupts.c +++ b/src/interrupts/interrupts.c @@ -1,33 +1,63 @@ #include #include <_stdio.h> #include +#include +#include +#include +#include IDTR idtr; IDTEntry* entries; void* buffer; -void setInterrupt(uint8_t vector, void* callback, uint8_t flags) { +void setInterrupt(uint8_t vector, void* callback) { IDTEntry* descriptor = &entries[vector]; - descriptor->isr_low = (uint32_t)callback & 0xFFFF; - descriptor->codeSegment = 0x08; - descriptor->attributes = flags; + descriptor->codeSegment = 0x08; + descriptor->attributes = 0x8E; descriptor->isr_high = (uint32_t)callback >> 16; descriptor->reserved = 0; + __asm__ volatile ("lidt %0" : : "memory"(idtr)); } +uint32_t count = 0x01; + void testInterrupt() { - printf("interrupt!\n"); + printf("interrupt 0x%x!\n", count++); + yields(); +} + +#define PIC1 0x20 +#define PIC2 0xA0 +#define PIC1_COMMAND PIC1 +#define PIC1_DATA (PIC1+1) +#define PIC2_COMMAND PIC2 +#define PIC2_DATA (PIC2+1) + +void remapPIC(int offset1, int offset2) { + printf("remapping pic\n"); + outb(0x20, 0x11); + outb(0xA0, 0x11); + outb(0x21, 0x20); + outb(0xA1, offset1); + outb(0x21, offset2); + outb(0xA1, 0x02); + outb(0x21, 0x01); + outb(0xA1, 0x01); + outb(0x21, 0x0); + outb(0xA1, 0x0); } void initInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); + remapPIC(32, 40); + buffer = malloc(sizeof(IDTEntry) * 256); entries = (IDTEntry*) (((int) buffer + sizeof(IDTEntry)) & -1 ^ 0x0F); idtr.limit = sizeof(IDTEntry) * 256 - 1; - idtr.base = entries; + idtr.base = (uint32_t) entries; for (int i = 0; i < 256; i++) { - setInterrupt(i, testInterrupt, 0x8E); + setInterrupt(i, &testInterrupt); } - __asm__ volatile ("lidt %0" : : "memory"(idtr)); + setupExceptions(); + // setupTimer(); __asm__ volatile ("sti"); } \ No newline at end of file diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 3ca3d29..1d8a597 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -17,5 +17,6 @@ } __attribute__((packed)) IDTR; extern void initInterrupts(); +extern void setInterrupt(uint8_t vector, void* callback); #endif \ No newline at end of file diff --git a/src/headers/ports.h b/src/headers/ports.h index 716fd0a..a47812e 100644 --- a/src/headers/ports.h +++ b/src/headers/ports.h @@ -6,4 +6,6 @@ extern void outb(uint16_t port, uint8_t val); extern uint8_t inb(uint16_t port); +extern void ioWait(); + #endif \ No newline at end of file diff --git a/src/headers/timer.h b/src/headers/timer.h new file mode 100644 index 0000000..e977777 --- /dev/null +++ b/src/headers/timer.h @@ -0,0 +1,6 @@ +#ifndef TIMER_H +#define TIMER_H + +extern void setupTimer(); + +#endif \ No newline at end of file diff --git a/src/interrupts/exceptions.c b/src/interrupts/exceptions.c new file mode 100644 index 0000000..19d8819 --- /dev/null +++ b/src/interrupts/exceptions.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include <_stdio.h> + +#define exception(exceptionNR, name) void exception##exceptionNR##Handler () {printf("exception "#name" happend!"); yields();} +#define setExceptionHandler(exceptionNR) setInterrupt(exceptionNR, &exception##exceptionNR##Handler); + +exception(0, DIVIDE_BY_ZERO); +exception(1, DEBUG); +exception(2, NON_MASKABLE_INTERRUPT); +exception(3, BREAKPOINT); +exception(4, OVERFLOW); +exception(5, BOUND_RANGE_EXCEEDED); +exception(6, INVALID_OPCODE); +exception(7, DEVIVE_NOT_AVAILABLE); +exception(8, DOUBLE_FAULT); +exception(10, INVALID_TSS); +exception(11, SEGMENT_NOT_PRESENT); +exception(12, STACK_SEGMENT_FAULT); +exception(13, GENERAL_PROTECTION_FAULT); +exception(14, PAGE_FAULT); +exception(16, X87_FLOATING_POINT_EXCEPTION); +exception(17, ALIGNMENT_CHECK); +exception(18, MACHINE_CHECK); +exception(19, SMID_FLOATING_POINT_EXCEPTION); +exception(20, VIRTUALIZATION_EXCEPTION); +exception(30, SECURITy_EXCEPTION); + + +void setupExceptions() { + printf("setting exceptions\n"); + setExceptionHandler(0); + setExceptionHandler(1); + setExceptionHandler(2); + setExceptionHandler(3); + setExceptionHandler(4); + setExceptionHandler(5); + setExceptionHandler(6); + setExceptionHandler(7); + setExceptionHandler(8); + setExceptionHandler(10); + setExceptionHandler(11); + setExceptionHandler(12); + setExceptionHandler(13); + setExceptionHandler(14); + setExceptionHandler(16); + setExceptionHandler(17); + setExceptionHandler(18); + setExceptionHandler(19); + setExceptionHandler(20); + setExceptionHandler(30); +} \ No newline at end of file diff --git a/src/interrupts/interrupts.c b/src/interrupts/interrupts.c index 74d2931..0c9e2b1 100644 --- a/src/interrupts/interrupts.c +++ b/src/interrupts/interrupts.c @@ -1,33 +1,63 @@ #include #include <_stdio.h> #include +#include +#include +#include +#include IDTR idtr; IDTEntry* entries; void* buffer; -void setInterrupt(uint8_t vector, void* callback, uint8_t flags) { +void setInterrupt(uint8_t vector, void* callback) { IDTEntry* descriptor = &entries[vector]; - descriptor->isr_low = (uint32_t)callback & 0xFFFF; - descriptor->codeSegment = 0x08; - descriptor->attributes = flags; + descriptor->codeSegment = 0x08; + descriptor->attributes = 0x8E; descriptor->isr_high = (uint32_t)callback >> 16; descriptor->reserved = 0; + __asm__ volatile ("lidt %0" : : "memory"(idtr)); } +uint32_t count = 0x01; + void testInterrupt() { - printf("interrupt!\n"); + printf("interrupt 0x%x!\n", count++); + yields(); +} + +#define PIC1 0x20 +#define PIC2 0xA0 +#define PIC1_COMMAND PIC1 +#define PIC1_DATA (PIC1+1) +#define PIC2_COMMAND PIC2 +#define PIC2_DATA (PIC2+1) + +void remapPIC(int offset1, int offset2) { + printf("remapping pic\n"); + outb(0x20, 0x11); + outb(0xA0, 0x11); + outb(0x21, 0x20); + outb(0xA1, offset1); + outb(0x21, offset2); + outb(0xA1, 0x02); + outb(0x21, 0x01); + outb(0xA1, 0x01); + outb(0x21, 0x0); + outb(0xA1, 0x0); } void initInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); + remapPIC(32, 40); + buffer = malloc(sizeof(IDTEntry) * 256); entries = (IDTEntry*) (((int) buffer + sizeof(IDTEntry)) & -1 ^ 0x0F); idtr.limit = sizeof(IDTEntry) * 256 - 1; - idtr.base = entries; + idtr.base = (uint32_t) entries; for (int i = 0; i < 256; i++) { - setInterrupt(i, testInterrupt, 0x8E); + setInterrupt(i, &testInterrupt); } - __asm__ volatile ("lidt %0" : : "memory"(idtr)); + setupExceptions(); + // setupTimer(); __asm__ volatile ("sti"); } \ No newline at end of file diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index 34eea3f..5e71b2d 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -83,7 +83,6 @@ } } -char HEX_PREFIX[] = "0x"; char HEX_CHARS[] = "0123456789ABCDEF"; void putHex(char** write, uint32_t x) { @@ -94,10 +93,10 @@ alreadyWriting = true; } if (alreadyWriting) { - **write = HEX_CHARS[byte & 0x0F]; - (*write)++; **write = HEX_CHARS[byte >> 8]; (*write)++; + **write = HEX_CHARS[byte & 0x0F]; + (*write)++; } } } diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 3ca3d29..1d8a597 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -17,5 +17,6 @@ } __attribute__((packed)) IDTR; extern void initInterrupts(); +extern void setInterrupt(uint8_t vector, void* callback); #endif \ No newline at end of file diff --git a/src/headers/ports.h b/src/headers/ports.h index 716fd0a..a47812e 100644 --- a/src/headers/ports.h +++ b/src/headers/ports.h @@ -6,4 +6,6 @@ extern void outb(uint16_t port, uint8_t val); extern uint8_t inb(uint16_t port); +extern void ioWait(); + #endif \ No newline at end of file diff --git a/src/headers/timer.h b/src/headers/timer.h new file mode 100644 index 0000000..e977777 --- /dev/null +++ b/src/headers/timer.h @@ -0,0 +1,6 @@ +#ifndef TIMER_H +#define TIMER_H + +extern void setupTimer(); + +#endif \ No newline at end of file diff --git a/src/interrupts/exceptions.c b/src/interrupts/exceptions.c new file mode 100644 index 0000000..19d8819 --- /dev/null +++ b/src/interrupts/exceptions.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include <_stdio.h> + +#define exception(exceptionNR, name) void exception##exceptionNR##Handler () {printf("exception "#name" happend!"); yields();} +#define setExceptionHandler(exceptionNR) setInterrupt(exceptionNR, &exception##exceptionNR##Handler); + +exception(0, DIVIDE_BY_ZERO); +exception(1, DEBUG); +exception(2, NON_MASKABLE_INTERRUPT); +exception(3, BREAKPOINT); +exception(4, OVERFLOW); +exception(5, BOUND_RANGE_EXCEEDED); +exception(6, INVALID_OPCODE); +exception(7, DEVIVE_NOT_AVAILABLE); +exception(8, DOUBLE_FAULT); +exception(10, INVALID_TSS); +exception(11, SEGMENT_NOT_PRESENT); +exception(12, STACK_SEGMENT_FAULT); +exception(13, GENERAL_PROTECTION_FAULT); +exception(14, PAGE_FAULT); +exception(16, X87_FLOATING_POINT_EXCEPTION); +exception(17, ALIGNMENT_CHECK); +exception(18, MACHINE_CHECK); +exception(19, SMID_FLOATING_POINT_EXCEPTION); +exception(20, VIRTUALIZATION_EXCEPTION); +exception(30, SECURITy_EXCEPTION); + + +void setupExceptions() { + printf("setting exceptions\n"); + setExceptionHandler(0); + setExceptionHandler(1); + setExceptionHandler(2); + setExceptionHandler(3); + setExceptionHandler(4); + setExceptionHandler(5); + setExceptionHandler(6); + setExceptionHandler(7); + setExceptionHandler(8); + setExceptionHandler(10); + setExceptionHandler(11); + setExceptionHandler(12); + setExceptionHandler(13); + setExceptionHandler(14); + setExceptionHandler(16); + setExceptionHandler(17); + setExceptionHandler(18); + setExceptionHandler(19); + setExceptionHandler(20); + setExceptionHandler(30); +} \ No newline at end of file diff --git a/src/interrupts/interrupts.c b/src/interrupts/interrupts.c index 74d2931..0c9e2b1 100644 --- a/src/interrupts/interrupts.c +++ b/src/interrupts/interrupts.c @@ -1,33 +1,63 @@ #include #include <_stdio.h> #include +#include +#include +#include +#include IDTR idtr; IDTEntry* entries; void* buffer; -void setInterrupt(uint8_t vector, void* callback, uint8_t flags) { +void setInterrupt(uint8_t vector, void* callback) { IDTEntry* descriptor = &entries[vector]; - descriptor->isr_low = (uint32_t)callback & 0xFFFF; - descriptor->codeSegment = 0x08; - descriptor->attributes = flags; + descriptor->codeSegment = 0x08; + descriptor->attributes = 0x8E; descriptor->isr_high = (uint32_t)callback >> 16; descriptor->reserved = 0; + __asm__ volatile ("lidt %0" : : "memory"(idtr)); } +uint32_t count = 0x01; + void testInterrupt() { - printf("interrupt!\n"); + printf("interrupt 0x%x!\n", count++); + yields(); +} + +#define PIC1 0x20 +#define PIC2 0xA0 +#define PIC1_COMMAND PIC1 +#define PIC1_DATA (PIC1+1) +#define PIC2_COMMAND PIC2 +#define PIC2_DATA (PIC2+1) + +void remapPIC(int offset1, int offset2) { + printf("remapping pic\n"); + outb(0x20, 0x11); + outb(0xA0, 0x11); + outb(0x21, 0x20); + outb(0xA1, offset1); + outb(0x21, offset2); + outb(0xA1, 0x02); + outb(0x21, 0x01); + outb(0xA1, 0x01); + outb(0x21, 0x0); + outb(0xA1, 0x0); } void initInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); + remapPIC(32, 40); + buffer = malloc(sizeof(IDTEntry) * 256); entries = (IDTEntry*) (((int) buffer + sizeof(IDTEntry)) & -1 ^ 0x0F); idtr.limit = sizeof(IDTEntry) * 256 - 1; - idtr.base = entries; + idtr.base = (uint32_t) entries; for (int i = 0; i < 256; i++) { - setInterrupt(i, testInterrupt, 0x8E); + setInterrupt(i, &testInterrupt); } - __asm__ volatile ("lidt %0" : : "memory"(idtr)); + setupExceptions(); + // setupTimer(); __asm__ volatile ("sti"); } \ No newline at end of file diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index 34eea3f..5e71b2d 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -83,7 +83,6 @@ } } -char HEX_PREFIX[] = "0x"; char HEX_CHARS[] = "0123456789ABCDEF"; void putHex(char** write, uint32_t x) { @@ -94,10 +93,10 @@ alreadyWriting = true; } if (alreadyWriting) { - **write = HEX_CHARS[byte & 0x0F]; - (*write)++; **write = HEX_CHARS[byte >> 8]; (*write)++; + **write = HEX_CHARS[byte & 0x0F]; + (*write)++; } } } diff --git a/src/kernel/drivers/timer/timer.c b/src/kernel/drivers/timer/timer.c new file mode 100644 index 0000000..c386a7d --- /dev/null +++ b/src/kernel/drivers/timer/timer.c @@ -0,0 +1,51 @@ +#include +#include <_stdio.h> +#include +#include +#include + + +#define PIT_A 0x40 +#define PIT_CONTROL 0x43 + +#define PIT_MASK 0xFF +#define PIT_SCALE 1193180 +#define PIT_SET 0x36 + +#define CMD_BINARY 0x00 + +#define CMD_MODE0 0x00 +#define CMD_MODE1 0x02 +#define CMD_MODE2 0x04 +#define CMD_MODE3 0x06 +#define CMD_MODE4 0x08 +#define CMD_MODE5 0x0a + +#define CMD_RW_BOTH 0x30 + +#define CMD_COUNTER0 0x00 +#define CMD_COUNTER2 0x80 + +void setTimerFreq(uint32_t hz) { + int divisor = PIT_SCALE / hz; + outb(PIT_CONTROL, CMD_BINARY | CMD_MODE3 | CMD_RW_BOTH | CMD_COUNTER0); + outb(PIT_A, (uint8_t) divisor); + outb(PIT_A, (uint8_t) (divisor >> 8)); +} + +uint64_t timer_ticks = 0; +uint8_t timer_subticks = 0; + +void timerHandler() { + printf("timer\n"); + yields(); + outb(0x20, 0x20); + __asm__ volatile ("iret"); +} + +void setupTimer() { + printf("setting timer frequency to 100Hz\n"); + setTimerFreq(100); + setInterrupt(32, &timerHandler); +} + diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 3ca3d29..1d8a597 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -17,5 +17,6 @@ } __attribute__((packed)) IDTR; extern void initInterrupts(); +extern void setInterrupt(uint8_t vector, void* callback); #endif \ No newline at end of file diff --git a/src/headers/ports.h b/src/headers/ports.h index 716fd0a..a47812e 100644 --- a/src/headers/ports.h +++ b/src/headers/ports.h @@ -6,4 +6,6 @@ extern void outb(uint16_t port, uint8_t val); extern uint8_t inb(uint16_t port); +extern void ioWait(); + #endif \ No newline at end of file diff --git a/src/headers/timer.h b/src/headers/timer.h new file mode 100644 index 0000000..e977777 --- /dev/null +++ b/src/headers/timer.h @@ -0,0 +1,6 @@ +#ifndef TIMER_H +#define TIMER_H + +extern void setupTimer(); + +#endif \ No newline at end of file diff --git a/src/interrupts/exceptions.c b/src/interrupts/exceptions.c new file mode 100644 index 0000000..19d8819 --- /dev/null +++ b/src/interrupts/exceptions.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include <_stdio.h> + +#define exception(exceptionNR, name) void exception##exceptionNR##Handler () {printf("exception "#name" happend!"); yields();} +#define setExceptionHandler(exceptionNR) setInterrupt(exceptionNR, &exception##exceptionNR##Handler); + +exception(0, DIVIDE_BY_ZERO); +exception(1, DEBUG); +exception(2, NON_MASKABLE_INTERRUPT); +exception(3, BREAKPOINT); +exception(4, OVERFLOW); +exception(5, BOUND_RANGE_EXCEEDED); +exception(6, INVALID_OPCODE); +exception(7, DEVIVE_NOT_AVAILABLE); +exception(8, DOUBLE_FAULT); +exception(10, INVALID_TSS); +exception(11, SEGMENT_NOT_PRESENT); +exception(12, STACK_SEGMENT_FAULT); +exception(13, GENERAL_PROTECTION_FAULT); +exception(14, PAGE_FAULT); +exception(16, X87_FLOATING_POINT_EXCEPTION); +exception(17, ALIGNMENT_CHECK); +exception(18, MACHINE_CHECK); +exception(19, SMID_FLOATING_POINT_EXCEPTION); +exception(20, VIRTUALIZATION_EXCEPTION); +exception(30, SECURITy_EXCEPTION); + + +void setupExceptions() { + printf("setting exceptions\n"); + setExceptionHandler(0); + setExceptionHandler(1); + setExceptionHandler(2); + setExceptionHandler(3); + setExceptionHandler(4); + setExceptionHandler(5); + setExceptionHandler(6); + setExceptionHandler(7); + setExceptionHandler(8); + setExceptionHandler(10); + setExceptionHandler(11); + setExceptionHandler(12); + setExceptionHandler(13); + setExceptionHandler(14); + setExceptionHandler(16); + setExceptionHandler(17); + setExceptionHandler(18); + setExceptionHandler(19); + setExceptionHandler(20); + setExceptionHandler(30); +} \ No newline at end of file diff --git a/src/interrupts/interrupts.c b/src/interrupts/interrupts.c index 74d2931..0c9e2b1 100644 --- a/src/interrupts/interrupts.c +++ b/src/interrupts/interrupts.c @@ -1,33 +1,63 @@ #include #include <_stdio.h> #include +#include +#include +#include +#include IDTR idtr; IDTEntry* entries; void* buffer; -void setInterrupt(uint8_t vector, void* callback, uint8_t flags) { +void setInterrupt(uint8_t vector, void* callback) { IDTEntry* descriptor = &entries[vector]; - descriptor->isr_low = (uint32_t)callback & 0xFFFF; - descriptor->codeSegment = 0x08; - descriptor->attributes = flags; + descriptor->codeSegment = 0x08; + descriptor->attributes = 0x8E; descriptor->isr_high = (uint32_t)callback >> 16; descriptor->reserved = 0; + __asm__ volatile ("lidt %0" : : "memory"(idtr)); } +uint32_t count = 0x01; + void testInterrupt() { - printf("interrupt!\n"); + printf("interrupt 0x%x!\n", count++); + yields(); +} + +#define PIC1 0x20 +#define PIC2 0xA0 +#define PIC1_COMMAND PIC1 +#define PIC1_DATA (PIC1+1) +#define PIC2_COMMAND PIC2 +#define PIC2_DATA (PIC2+1) + +void remapPIC(int offset1, int offset2) { + printf("remapping pic\n"); + outb(0x20, 0x11); + outb(0xA0, 0x11); + outb(0x21, 0x20); + outb(0xA1, offset1); + outb(0x21, offset2); + outb(0xA1, 0x02); + outb(0x21, 0x01); + outb(0xA1, 0x01); + outb(0x21, 0x0); + outb(0xA1, 0x0); } void initInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); + remapPIC(32, 40); + buffer = malloc(sizeof(IDTEntry) * 256); entries = (IDTEntry*) (((int) buffer + sizeof(IDTEntry)) & -1 ^ 0x0F); idtr.limit = sizeof(IDTEntry) * 256 - 1; - idtr.base = entries; + idtr.base = (uint32_t) entries; for (int i = 0; i < 256; i++) { - setInterrupt(i, testInterrupt, 0x8E); + setInterrupt(i, &testInterrupt); } - __asm__ volatile ("lidt %0" : : "memory"(idtr)); + setupExceptions(); + // setupTimer(); __asm__ volatile ("sti"); } \ No newline at end of file diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index 34eea3f..5e71b2d 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -83,7 +83,6 @@ } } -char HEX_PREFIX[] = "0x"; char HEX_CHARS[] = "0123456789ABCDEF"; void putHex(char** write, uint32_t x) { @@ -94,10 +93,10 @@ alreadyWriting = true; } if (alreadyWriting) { - **write = HEX_CHARS[byte & 0x0F]; - (*write)++; **write = HEX_CHARS[byte >> 8]; (*write)++; + **write = HEX_CHARS[byte & 0x0F]; + (*write)++; } } } diff --git a/src/kernel/drivers/timer/timer.c b/src/kernel/drivers/timer/timer.c new file mode 100644 index 0000000..c386a7d --- /dev/null +++ b/src/kernel/drivers/timer/timer.c @@ -0,0 +1,51 @@ +#include +#include <_stdio.h> +#include +#include +#include + + +#define PIT_A 0x40 +#define PIT_CONTROL 0x43 + +#define PIT_MASK 0xFF +#define PIT_SCALE 1193180 +#define PIT_SET 0x36 + +#define CMD_BINARY 0x00 + +#define CMD_MODE0 0x00 +#define CMD_MODE1 0x02 +#define CMD_MODE2 0x04 +#define CMD_MODE3 0x06 +#define CMD_MODE4 0x08 +#define CMD_MODE5 0x0a + +#define CMD_RW_BOTH 0x30 + +#define CMD_COUNTER0 0x00 +#define CMD_COUNTER2 0x80 + +void setTimerFreq(uint32_t hz) { + int divisor = PIT_SCALE / hz; + outb(PIT_CONTROL, CMD_BINARY | CMD_MODE3 | CMD_RW_BOTH | CMD_COUNTER0); + outb(PIT_A, (uint8_t) divisor); + outb(PIT_A, (uint8_t) (divisor >> 8)); +} + +uint64_t timer_ticks = 0; +uint8_t timer_subticks = 0; + +void timerHandler() { + printf("timer\n"); + yields(); + outb(0x20, 0x20); + __asm__ volatile ("iret"); +} + +void setupTimer() { + printf("setting timer frequency to 100Hz\n"); + setTimerFreq(100); + setInterrupt(32, &timerHandler); +} + diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 48ea59f..e52b7f9 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -13,14 +13,9 @@ initMemoryAllocation(0x1000000); // initializing stacks after the kernel seems not to work :( otherwise, _kernel_end should be passed here initOSTasks(); drawLogo(); - printf("giving up control...\n"); - yields(); - printf("Returned to mainTask!\n\n"); printf("initializing interrupts\n"); initInterrupts(); + 1/0; yields(); - printf("trying interrupt\n"); - asm ("int $0x80"); - printf("the interrupt worked!\n"); - yield(); + while(1); } \ No newline at end of file diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 3ca3d29..1d8a597 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -17,5 +17,6 @@ } __attribute__((packed)) IDTR; extern void initInterrupts(); +extern void setInterrupt(uint8_t vector, void* callback); #endif \ No newline at end of file diff --git a/src/headers/ports.h b/src/headers/ports.h index 716fd0a..a47812e 100644 --- a/src/headers/ports.h +++ b/src/headers/ports.h @@ -6,4 +6,6 @@ extern void outb(uint16_t port, uint8_t val); extern uint8_t inb(uint16_t port); +extern void ioWait(); + #endif \ No newline at end of file diff --git a/src/headers/timer.h b/src/headers/timer.h new file mode 100644 index 0000000..e977777 --- /dev/null +++ b/src/headers/timer.h @@ -0,0 +1,6 @@ +#ifndef TIMER_H +#define TIMER_H + +extern void setupTimer(); + +#endif \ No newline at end of file diff --git a/src/interrupts/exceptions.c b/src/interrupts/exceptions.c new file mode 100644 index 0000000..19d8819 --- /dev/null +++ b/src/interrupts/exceptions.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include <_stdio.h> + +#define exception(exceptionNR, name) void exception##exceptionNR##Handler () {printf("exception "#name" happend!"); yields();} +#define setExceptionHandler(exceptionNR) setInterrupt(exceptionNR, &exception##exceptionNR##Handler); + +exception(0, DIVIDE_BY_ZERO); +exception(1, DEBUG); +exception(2, NON_MASKABLE_INTERRUPT); +exception(3, BREAKPOINT); +exception(4, OVERFLOW); +exception(5, BOUND_RANGE_EXCEEDED); +exception(6, INVALID_OPCODE); +exception(7, DEVIVE_NOT_AVAILABLE); +exception(8, DOUBLE_FAULT); +exception(10, INVALID_TSS); +exception(11, SEGMENT_NOT_PRESENT); +exception(12, STACK_SEGMENT_FAULT); +exception(13, GENERAL_PROTECTION_FAULT); +exception(14, PAGE_FAULT); +exception(16, X87_FLOATING_POINT_EXCEPTION); +exception(17, ALIGNMENT_CHECK); +exception(18, MACHINE_CHECK); +exception(19, SMID_FLOATING_POINT_EXCEPTION); +exception(20, VIRTUALIZATION_EXCEPTION); +exception(30, SECURITy_EXCEPTION); + + +void setupExceptions() { + printf("setting exceptions\n"); + setExceptionHandler(0); + setExceptionHandler(1); + setExceptionHandler(2); + setExceptionHandler(3); + setExceptionHandler(4); + setExceptionHandler(5); + setExceptionHandler(6); + setExceptionHandler(7); + setExceptionHandler(8); + setExceptionHandler(10); + setExceptionHandler(11); + setExceptionHandler(12); + setExceptionHandler(13); + setExceptionHandler(14); + setExceptionHandler(16); + setExceptionHandler(17); + setExceptionHandler(18); + setExceptionHandler(19); + setExceptionHandler(20); + setExceptionHandler(30); +} \ No newline at end of file diff --git a/src/interrupts/interrupts.c b/src/interrupts/interrupts.c index 74d2931..0c9e2b1 100644 --- a/src/interrupts/interrupts.c +++ b/src/interrupts/interrupts.c @@ -1,33 +1,63 @@ #include #include <_stdio.h> #include +#include +#include +#include +#include IDTR idtr; IDTEntry* entries; void* buffer; -void setInterrupt(uint8_t vector, void* callback, uint8_t flags) { +void setInterrupt(uint8_t vector, void* callback) { IDTEntry* descriptor = &entries[vector]; - descriptor->isr_low = (uint32_t)callback & 0xFFFF; - descriptor->codeSegment = 0x08; - descriptor->attributes = flags; + descriptor->codeSegment = 0x08; + descriptor->attributes = 0x8E; descriptor->isr_high = (uint32_t)callback >> 16; descriptor->reserved = 0; + __asm__ volatile ("lidt %0" : : "memory"(idtr)); } +uint32_t count = 0x01; + void testInterrupt() { - printf("interrupt!\n"); + printf("interrupt 0x%x!\n", count++); + yields(); +} + +#define PIC1 0x20 +#define PIC2 0xA0 +#define PIC1_COMMAND PIC1 +#define PIC1_DATA (PIC1+1) +#define PIC2_COMMAND PIC2 +#define PIC2_DATA (PIC2+1) + +void remapPIC(int offset1, int offset2) { + printf("remapping pic\n"); + outb(0x20, 0x11); + outb(0xA0, 0x11); + outb(0x21, 0x20); + outb(0xA1, offset1); + outb(0x21, offset2); + outb(0xA1, 0x02); + outb(0x21, 0x01); + outb(0xA1, 0x01); + outb(0x21, 0x0); + outb(0xA1, 0x0); } void initInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); + remapPIC(32, 40); + buffer = malloc(sizeof(IDTEntry) * 256); entries = (IDTEntry*) (((int) buffer + sizeof(IDTEntry)) & -1 ^ 0x0F); idtr.limit = sizeof(IDTEntry) * 256 - 1; - idtr.base = entries; + idtr.base = (uint32_t) entries; for (int i = 0; i < 256; i++) { - setInterrupt(i, testInterrupt, 0x8E); + setInterrupt(i, &testInterrupt); } - __asm__ volatile ("lidt %0" : : "memory"(idtr)); + setupExceptions(); + // setupTimer(); __asm__ volatile ("sti"); } \ No newline at end of file diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index 34eea3f..5e71b2d 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -83,7 +83,6 @@ } } -char HEX_PREFIX[] = "0x"; char HEX_CHARS[] = "0123456789ABCDEF"; void putHex(char** write, uint32_t x) { @@ -94,10 +93,10 @@ alreadyWriting = true; } if (alreadyWriting) { - **write = HEX_CHARS[byte & 0x0F]; - (*write)++; **write = HEX_CHARS[byte >> 8]; (*write)++; + **write = HEX_CHARS[byte & 0x0F]; + (*write)++; } } } diff --git a/src/kernel/drivers/timer/timer.c b/src/kernel/drivers/timer/timer.c new file mode 100644 index 0000000..c386a7d --- /dev/null +++ b/src/kernel/drivers/timer/timer.c @@ -0,0 +1,51 @@ +#include +#include <_stdio.h> +#include +#include +#include + + +#define PIT_A 0x40 +#define PIT_CONTROL 0x43 + +#define PIT_MASK 0xFF +#define PIT_SCALE 1193180 +#define PIT_SET 0x36 + +#define CMD_BINARY 0x00 + +#define CMD_MODE0 0x00 +#define CMD_MODE1 0x02 +#define CMD_MODE2 0x04 +#define CMD_MODE3 0x06 +#define CMD_MODE4 0x08 +#define CMD_MODE5 0x0a + +#define CMD_RW_BOTH 0x30 + +#define CMD_COUNTER0 0x00 +#define CMD_COUNTER2 0x80 + +void setTimerFreq(uint32_t hz) { + int divisor = PIT_SCALE / hz; + outb(PIT_CONTROL, CMD_BINARY | CMD_MODE3 | CMD_RW_BOTH | CMD_COUNTER0); + outb(PIT_A, (uint8_t) divisor); + outb(PIT_A, (uint8_t) (divisor >> 8)); +} + +uint64_t timer_ticks = 0; +uint8_t timer_subticks = 0; + +void timerHandler() { + printf("timer\n"); + yields(); + outb(0x20, 0x20); + __asm__ volatile ("iret"); +} + +void setupTimer() { + printf("setting timer frequency to 100Hz\n"); + setTimerFreq(100); + setInterrupt(32, &timerHandler); +} + diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 48ea59f..e52b7f9 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -13,14 +13,9 @@ initMemoryAllocation(0x1000000); // initializing stacks after the kernel seems not to work :( otherwise, _kernel_end should be passed here initOSTasks(); drawLogo(); - printf("giving up control...\n"); - yields(); - printf("Returned to mainTask!\n\n"); printf("initializing interrupts\n"); initInterrupts(); + 1/0; yields(); - printf("trying interrupt\n"); - asm ("int $0x80"); - printf("the interrupt worked!\n"); - yield(); + while(1); } \ No newline at end of file diff --git a/src/kernel/textMode/_stdio.c b/src/kernel/textMode/_stdio.c deleted file mode 100644 index fcecf6f..0000000 --- a/src/kernel/textMode/_stdio.c +++ /dev/null @@ -1,199 +0,0 @@ -#include -#include -#include <_stdio.h> -#include -#include -#include -#include -#include -#include - -uint16_t* framebuffer = (uint16_t*) FRAMEBUFFER_LOCATION; -char format = 0x0F; - -void putCharWithFormatAtOffset(uint16_t c, uint16_t position) { - *(framebuffer + position) = c; -} - -void putCharWithFormatAtPosition(uint8_t x, uint8_t y, uint32_t c) { - putCharWithFormatAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -void putCharAtOffset(char c, uint16_t offset) { - putCharWithFormatAtOffset(c | format << 8, offset); -} - -void putCharAt(char c, uint8_t x, uint8_t y) { - putCharAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -uint16_t getCharWithFormatAt(uint8_t x, uint8_t y) { - return *(framebuffer + x + y * VIDEO_WIDTH); -} - -void putCharsAt(char* string, uint8_t x, uint8_t y) { - uint16_t position = 0; - char c = *string; - while (c != 0x00) { - putCharAt(c, x + position, y); - position++; - c = *(string + position); - } -} - -void shiftUp() { - for (int y = 1; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, y-1, getCharWithFormatAt(x, y)); - } - } - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, VIDEO_HEIGHT-1, 0x00); - } -} - -void newLine() { - uint16_t offset = getCursorOffset(); - uint8_t x = offset % VIDEO_WIDTH; - uint8_t y = offset / VIDEO_WIDTH; - x = 0; - y++; - if (y >= VIDEO_HEIGHT) { - shiftUp(); - y--; - } - setCursorPosition(x, y); -} - -void putChar(char c) { - if (c == '\n') { - newLine(); - return; - } - uint16_t offset = getCursorOffset(); - putCharAtOffset(c, offset); - setCursorOffset(offset + 1); -} - -void puts(char* string) { - while (*string != 0x00) { - putChar(*string); - string++; - } -} - -char HEX_PREFIX[] = "0x"; -char HEX_CHARS[] = "0123456789ABCDEF"; - -void putHex(char** write, uint32_t x) { - uint8_t alreadyWriting = 0; - for (uint8_t position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = 0x01; - } - if (alreadyWriting) { - **write = HEX_CHARS[byte % 256]; - *write++; - **write = HEX_CHARS[byte / 256]; - *write++; - } - } -} - -uint32_t getInsertLength(char insertType, uint32_t x) { - switch (insertType) - { - case 's': - return strlen((char*) x); - case 'x': - for (uint8_t position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - return 2*position; - } - } - return 0; - } - return 0; -} - -void handleInsert(char** write, char insertType, uint32_t x) { - if (insertType == 's') { - char* string = (char*) x; - uint32_t length = strlen(string); - for (uint32_t position = 0; position < length; position++) { - **write = string[position]; - *write++; - } - return; - } - if (insertType == 'x') { - putHex(write, x); - } -} - -void printf(const char* format, ...) { - uint32_t size = 0; - va_list valist; - va_start(valist, format); - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - size += getInsertLength(format[++i], va_arg(valist, uint32_t)); - continue; - } - size++; - } - va_start(valist, format); - - char* data = malloc(size); - char* write = data; - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - handleInsert(&write, format[++i], va_arg(valist, uint32_t)); - continue; - } - *write = format[i]; - write++; - } - va_end(valist); - - Message* message = malloc(sizeof(Message)); - - message->data = data; - message->size = size; - message->next = 0x00; - sendMessage(getPrinterTask(), message); - schedule(getPrinterTask()); -} - -void clearScreen() { - setCursorOffset(0); - for (int y = 0; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharAt(' ', x, y); - } - } -} - -void setTextStyle(uint8_t style) { - format = style; -} - -void putChars(char* string, uint32_t length) { - for (uint32_t i = 0; i < length; i++) { - if (string[i] == '\n') { - newLine(); - continue; - } - putChar(string[i]); - } -} - -uint32_t strlen(char* string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} \ No newline at end of file diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 3ca3d29..1d8a597 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -17,5 +17,6 @@ } __attribute__((packed)) IDTR; extern void initInterrupts(); +extern void setInterrupt(uint8_t vector, void* callback); #endif \ No newline at end of file diff --git a/src/headers/ports.h b/src/headers/ports.h index 716fd0a..a47812e 100644 --- a/src/headers/ports.h +++ b/src/headers/ports.h @@ -6,4 +6,6 @@ extern void outb(uint16_t port, uint8_t val); extern uint8_t inb(uint16_t port); +extern void ioWait(); + #endif \ No newline at end of file diff --git a/src/headers/timer.h b/src/headers/timer.h new file mode 100644 index 0000000..e977777 --- /dev/null +++ b/src/headers/timer.h @@ -0,0 +1,6 @@ +#ifndef TIMER_H +#define TIMER_H + +extern void setupTimer(); + +#endif \ No newline at end of file diff --git a/src/interrupts/exceptions.c b/src/interrupts/exceptions.c new file mode 100644 index 0000000..19d8819 --- /dev/null +++ b/src/interrupts/exceptions.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include <_stdio.h> + +#define exception(exceptionNR, name) void exception##exceptionNR##Handler () {printf("exception "#name" happend!"); yields();} +#define setExceptionHandler(exceptionNR) setInterrupt(exceptionNR, &exception##exceptionNR##Handler); + +exception(0, DIVIDE_BY_ZERO); +exception(1, DEBUG); +exception(2, NON_MASKABLE_INTERRUPT); +exception(3, BREAKPOINT); +exception(4, OVERFLOW); +exception(5, BOUND_RANGE_EXCEEDED); +exception(6, INVALID_OPCODE); +exception(7, DEVIVE_NOT_AVAILABLE); +exception(8, DOUBLE_FAULT); +exception(10, INVALID_TSS); +exception(11, SEGMENT_NOT_PRESENT); +exception(12, STACK_SEGMENT_FAULT); +exception(13, GENERAL_PROTECTION_FAULT); +exception(14, PAGE_FAULT); +exception(16, X87_FLOATING_POINT_EXCEPTION); +exception(17, ALIGNMENT_CHECK); +exception(18, MACHINE_CHECK); +exception(19, SMID_FLOATING_POINT_EXCEPTION); +exception(20, VIRTUALIZATION_EXCEPTION); +exception(30, SECURITy_EXCEPTION); + + +void setupExceptions() { + printf("setting exceptions\n"); + setExceptionHandler(0); + setExceptionHandler(1); + setExceptionHandler(2); + setExceptionHandler(3); + setExceptionHandler(4); + setExceptionHandler(5); + setExceptionHandler(6); + setExceptionHandler(7); + setExceptionHandler(8); + setExceptionHandler(10); + setExceptionHandler(11); + setExceptionHandler(12); + setExceptionHandler(13); + setExceptionHandler(14); + setExceptionHandler(16); + setExceptionHandler(17); + setExceptionHandler(18); + setExceptionHandler(19); + setExceptionHandler(20); + setExceptionHandler(30); +} \ No newline at end of file diff --git a/src/interrupts/interrupts.c b/src/interrupts/interrupts.c index 74d2931..0c9e2b1 100644 --- a/src/interrupts/interrupts.c +++ b/src/interrupts/interrupts.c @@ -1,33 +1,63 @@ #include #include <_stdio.h> #include +#include +#include +#include +#include IDTR idtr; IDTEntry* entries; void* buffer; -void setInterrupt(uint8_t vector, void* callback, uint8_t flags) { +void setInterrupt(uint8_t vector, void* callback) { IDTEntry* descriptor = &entries[vector]; - descriptor->isr_low = (uint32_t)callback & 0xFFFF; - descriptor->codeSegment = 0x08; - descriptor->attributes = flags; + descriptor->codeSegment = 0x08; + descriptor->attributes = 0x8E; descriptor->isr_high = (uint32_t)callback >> 16; descriptor->reserved = 0; + __asm__ volatile ("lidt %0" : : "memory"(idtr)); } +uint32_t count = 0x01; + void testInterrupt() { - printf("interrupt!\n"); + printf("interrupt 0x%x!\n", count++); + yields(); +} + +#define PIC1 0x20 +#define PIC2 0xA0 +#define PIC1_COMMAND PIC1 +#define PIC1_DATA (PIC1+1) +#define PIC2_COMMAND PIC2 +#define PIC2_DATA (PIC2+1) + +void remapPIC(int offset1, int offset2) { + printf("remapping pic\n"); + outb(0x20, 0x11); + outb(0xA0, 0x11); + outb(0x21, 0x20); + outb(0xA1, offset1); + outb(0x21, offset2); + outb(0xA1, 0x02); + outb(0x21, 0x01); + outb(0xA1, 0x01); + outb(0x21, 0x0); + outb(0xA1, 0x0); } void initInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); + remapPIC(32, 40); + buffer = malloc(sizeof(IDTEntry) * 256); entries = (IDTEntry*) (((int) buffer + sizeof(IDTEntry)) & -1 ^ 0x0F); idtr.limit = sizeof(IDTEntry) * 256 - 1; - idtr.base = entries; + idtr.base = (uint32_t) entries; for (int i = 0; i < 256; i++) { - setInterrupt(i, testInterrupt, 0x8E); + setInterrupt(i, &testInterrupt); } - __asm__ volatile ("lidt %0" : : "memory"(idtr)); + setupExceptions(); + // setupTimer(); __asm__ volatile ("sti"); } \ No newline at end of file diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index 34eea3f..5e71b2d 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -83,7 +83,6 @@ } } -char HEX_PREFIX[] = "0x"; char HEX_CHARS[] = "0123456789ABCDEF"; void putHex(char** write, uint32_t x) { @@ -94,10 +93,10 @@ alreadyWriting = true; } if (alreadyWriting) { - **write = HEX_CHARS[byte & 0x0F]; - (*write)++; **write = HEX_CHARS[byte >> 8]; (*write)++; + **write = HEX_CHARS[byte & 0x0F]; + (*write)++; } } } diff --git a/src/kernel/drivers/timer/timer.c b/src/kernel/drivers/timer/timer.c new file mode 100644 index 0000000..c386a7d --- /dev/null +++ b/src/kernel/drivers/timer/timer.c @@ -0,0 +1,51 @@ +#include +#include <_stdio.h> +#include +#include +#include + + +#define PIT_A 0x40 +#define PIT_CONTROL 0x43 + +#define PIT_MASK 0xFF +#define PIT_SCALE 1193180 +#define PIT_SET 0x36 + +#define CMD_BINARY 0x00 + +#define CMD_MODE0 0x00 +#define CMD_MODE1 0x02 +#define CMD_MODE2 0x04 +#define CMD_MODE3 0x06 +#define CMD_MODE4 0x08 +#define CMD_MODE5 0x0a + +#define CMD_RW_BOTH 0x30 + +#define CMD_COUNTER0 0x00 +#define CMD_COUNTER2 0x80 + +void setTimerFreq(uint32_t hz) { + int divisor = PIT_SCALE / hz; + outb(PIT_CONTROL, CMD_BINARY | CMD_MODE3 | CMD_RW_BOTH | CMD_COUNTER0); + outb(PIT_A, (uint8_t) divisor); + outb(PIT_A, (uint8_t) (divisor >> 8)); +} + +uint64_t timer_ticks = 0; +uint8_t timer_subticks = 0; + +void timerHandler() { + printf("timer\n"); + yields(); + outb(0x20, 0x20); + __asm__ volatile ("iret"); +} + +void setupTimer() { + printf("setting timer frequency to 100Hz\n"); + setTimerFreq(100); + setInterrupt(32, &timerHandler); +} + diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 48ea59f..e52b7f9 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -13,14 +13,9 @@ initMemoryAllocation(0x1000000); // initializing stacks after the kernel seems not to work :( otherwise, _kernel_end should be passed here initOSTasks(); drawLogo(); - printf("giving up control...\n"); - yields(); - printf("Returned to mainTask!\n\n"); printf("initializing interrupts\n"); initInterrupts(); + 1/0; yields(); - printf("trying interrupt\n"); - asm ("int $0x80"); - printf("the interrupt worked!\n"); - yield(); + while(1); } \ No newline at end of file diff --git a/src/kernel/textMode/_stdio.c b/src/kernel/textMode/_stdio.c deleted file mode 100644 index fcecf6f..0000000 --- a/src/kernel/textMode/_stdio.c +++ /dev/null @@ -1,199 +0,0 @@ -#include -#include -#include <_stdio.h> -#include -#include -#include -#include -#include -#include - -uint16_t* framebuffer = (uint16_t*) FRAMEBUFFER_LOCATION; -char format = 0x0F; - -void putCharWithFormatAtOffset(uint16_t c, uint16_t position) { - *(framebuffer + position) = c; -} - -void putCharWithFormatAtPosition(uint8_t x, uint8_t y, uint32_t c) { - putCharWithFormatAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -void putCharAtOffset(char c, uint16_t offset) { - putCharWithFormatAtOffset(c | format << 8, offset); -} - -void putCharAt(char c, uint8_t x, uint8_t y) { - putCharAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -uint16_t getCharWithFormatAt(uint8_t x, uint8_t y) { - return *(framebuffer + x + y * VIDEO_WIDTH); -} - -void putCharsAt(char* string, uint8_t x, uint8_t y) { - uint16_t position = 0; - char c = *string; - while (c != 0x00) { - putCharAt(c, x + position, y); - position++; - c = *(string + position); - } -} - -void shiftUp() { - for (int y = 1; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, y-1, getCharWithFormatAt(x, y)); - } - } - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, VIDEO_HEIGHT-1, 0x00); - } -} - -void newLine() { - uint16_t offset = getCursorOffset(); - uint8_t x = offset % VIDEO_WIDTH; - uint8_t y = offset / VIDEO_WIDTH; - x = 0; - y++; - if (y >= VIDEO_HEIGHT) { - shiftUp(); - y--; - } - setCursorPosition(x, y); -} - -void putChar(char c) { - if (c == '\n') { - newLine(); - return; - } - uint16_t offset = getCursorOffset(); - putCharAtOffset(c, offset); - setCursorOffset(offset + 1); -} - -void puts(char* string) { - while (*string != 0x00) { - putChar(*string); - string++; - } -} - -char HEX_PREFIX[] = "0x"; -char HEX_CHARS[] = "0123456789ABCDEF"; - -void putHex(char** write, uint32_t x) { - uint8_t alreadyWriting = 0; - for (uint8_t position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = 0x01; - } - if (alreadyWriting) { - **write = HEX_CHARS[byte % 256]; - *write++; - **write = HEX_CHARS[byte / 256]; - *write++; - } - } -} - -uint32_t getInsertLength(char insertType, uint32_t x) { - switch (insertType) - { - case 's': - return strlen((char*) x); - case 'x': - for (uint8_t position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - return 2*position; - } - } - return 0; - } - return 0; -} - -void handleInsert(char** write, char insertType, uint32_t x) { - if (insertType == 's') { - char* string = (char*) x; - uint32_t length = strlen(string); - for (uint32_t position = 0; position < length; position++) { - **write = string[position]; - *write++; - } - return; - } - if (insertType == 'x') { - putHex(write, x); - } -} - -void printf(const char* format, ...) { - uint32_t size = 0; - va_list valist; - va_start(valist, format); - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - size += getInsertLength(format[++i], va_arg(valist, uint32_t)); - continue; - } - size++; - } - va_start(valist, format); - - char* data = malloc(size); - char* write = data; - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - handleInsert(&write, format[++i], va_arg(valist, uint32_t)); - continue; - } - *write = format[i]; - write++; - } - va_end(valist); - - Message* message = malloc(sizeof(Message)); - - message->data = data; - message->size = size; - message->next = 0x00; - sendMessage(getPrinterTask(), message); - schedule(getPrinterTask()); -} - -void clearScreen() { - setCursorOffset(0); - for (int y = 0; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharAt(' ', x, y); - } - } -} - -void setTextStyle(uint8_t style) { - format = style; -} - -void putChars(char* string, uint32_t length) { - for (uint32_t i = 0; i < length; i++) { - if (string[i] == '\n') { - newLine(); - continue; - } - putChar(string[i]); - } -} - -uint32_t strlen(char* string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} \ No newline at end of file diff --git a/src/kernel/textMode/cursor.c b/src/kernel/textMode/cursor.c deleted file mode 100644 index 8247c5e..0000000 --- a/src/kernel/textMode/cursor.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include -#include <_stdio.h> - -void setCursorOffset(uint16_t offset) { - outb(0x3D4, 0x0F); - outb(0x3D5, (uint8_t) (offset & 0xFF)); - outb(0x3D4, 0x0E); - outb(0x3D5, (uint8_t) ((offset >> 8) & 0xFF)); -} - -void setCursorPosition(uint8_t x, uint8_t y) { - setCursorOffset(x + y * VIDEO_WIDTH); -} - -uint16_t getCursorOffset() { - uint16_t result = 0; - outb(0x3D4, 0x0F); - result |= inb(0x3D5); - outb(0x3D4, 0x0E); - result |= ((uint16_t)inb(0x3D5)) << 8; - return result; -} \ No newline at end of file diff --git a/src/headers/exceptions.h b/src/headers/exceptions.h new file mode 100644 index 0000000..0e0715e --- /dev/null +++ b/src/headers/exceptions.h @@ -0,0 +1,6 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +extern void setupExceptions(); + +#endif \ No newline at end of file diff --git a/src/headers/interrupts.h b/src/headers/interrupts.h index 3ca3d29..1d8a597 100644 --- a/src/headers/interrupts.h +++ b/src/headers/interrupts.h @@ -17,5 +17,6 @@ } __attribute__((packed)) IDTR; extern void initInterrupts(); +extern void setInterrupt(uint8_t vector, void* callback); #endif \ No newline at end of file diff --git a/src/headers/ports.h b/src/headers/ports.h index 716fd0a..a47812e 100644 --- a/src/headers/ports.h +++ b/src/headers/ports.h @@ -6,4 +6,6 @@ extern void outb(uint16_t port, uint8_t val); extern uint8_t inb(uint16_t port); +extern void ioWait(); + #endif \ No newline at end of file diff --git a/src/headers/timer.h b/src/headers/timer.h new file mode 100644 index 0000000..e977777 --- /dev/null +++ b/src/headers/timer.h @@ -0,0 +1,6 @@ +#ifndef TIMER_H +#define TIMER_H + +extern void setupTimer(); + +#endif \ No newline at end of file diff --git a/src/interrupts/exceptions.c b/src/interrupts/exceptions.c new file mode 100644 index 0000000..19d8819 --- /dev/null +++ b/src/interrupts/exceptions.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include <_stdio.h> + +#define exception(exceptionNR, name) void exception##exceptionNR##Handler () {printf("exception "#name" happend!"); yields();} +#define setExceptionHandler(exceptionNR) setInterrupt(exceptionNR, &exception##exceptionNR##Handler); + +exception(0, DIVIDE_BY_ZERO); +exception(1, DEBUG); +exception(2, NON_MASKABLE_INTERRUPT); +exception(3, BREAKPOINT); +exception(4, OVERFLOW); +exception(5, BOUND_RANGE_EXCEEDED); +exception(6, INVALID_OPCODE); +exception(7, DEVIVE_NOT_AVAILABLE); +exception(8, DOUBLE_FAULT); +exception(10, INVALID_TSS); +exception(11, SEGMENT_NOT_PRESENT); +exception(12, STACK_SEGMENT_FAULT); +exception(13, GENERAL_PROTECTION_FAULT); +exception(14, PAGE_FAULT); +exception(16, X87_FLOATING_POINT_EXCEPTION); +exception(17, ALIGNMENT_CHECK); +exception(18, MACHINE_CHECK); +exception(19, SMID_FLOATING_POINT_EXCEPTION); +exception(20, VIRTUALIZATION_EXCEPTION); +exception(30, SECURITy_EXCEPTION); + + +void setupExceptions() { + printf("setting exceptions\n"); + setExceptionHandler(0); + setExceptionHandler(1); + setExceptionHandler(2); + setExceptionHandler(3); + setExceptionHandler(4); + setExceptionHandler(5); + setExceptionHandler(6); + setExceptionHandler(7); + setExceptionHandler(8); + setExceptionHandler(10); + setExceptionHandler(11); + setExceptionHandler(12); + setExceptionHandler(13); + setExceptionHandler(14); + setExceptionHandler(16); + setExceptionHandler(17); + setExceptionHandler(18); + setExceptionHandler(19); + setExceptionHandler(20); + setExceptionHandler(30); +} \ No newline at end of file diff --git a/src/interrupts/interrupts.c b/src/interrupts/interrupts.c index 74d2931..0c9e2b1 100644 --- a/src/interrupts/interrupts.c +++ b/src/interrupts/interrupts.c @@ -1,33 +1,63 @@ #include #include <_stdio.h> #include +#include +#include +#include +#include IDTR idtr; IDTEntry* entries; void* buffer; -void setInterrupt(uint8_t vector, void* callback, uint8_t flags) { +void setInterrupt(uint8_t vector, void* callback) { IDTEntry* descriptor = &entries[vector]; - descriptor->isr_low = (uint32_t)callback & 0xFFFF; - descriptor->codeSegment = 0x08; - descriptor->attributes = flags; + descriptor->codeSegment = 0x08; + descriptor->attributes = 0x8E; descriptor->isr_high = (uint32_t)callback >> 16; descriptor->reserved = 0; + __asm__ volatile ("lidt %0" : : "memory"(idtr)); } +uint32_t count = 0x01; + void testInterrupt() { - printf("interrupt!\n"); + printf("interrupt 0x%x!\n", count++); + yields(); +} + +#define PIC1 0x20 +#define PIC2 0xA0 +#define PIC1_COMMAND PIC1 +#define PIC1_DATA (PIC1+1) +#define PIC2_COMMAND PIC2 +#define PIC2_DATA (PIC2+1) + +void remapPIC(int offset1, int offset2) { + printf("remapping pic\n"); + outb(0x20, 0x11); + outb(0xA0, 0x11); + outb(0x21, 0x20); + outb(0xA1, offset1); + outb(0x21, offset2); + outb(0xA1, 0x02); + outb(0x21, 0x01); + outb(0xA1, 0x01); + outb(0x21, 0x0); + outb(0xA1, 0x0); } void initInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); + remapPIC(32, 40); + buffer = malloc(sizeof(IDTEntry) * 256); entries = (IDTEntry*) (((int) buffer + sizeof(IDTEntry)) & -1 ^ 0x0F); idtr.limit = sizeof(IDTEntry) * 256 - 1; - idtr.base = entries; + idtr.base = (uint32_t) entries; for (int i = 0; i < 256; i++) { - setInterrupt(i, testInterrupt, 0x8E); + setInterrupt(i, &testInterrupt); } - __asm__ volatile ("lidt %0" : : "memory"(idtr)); + setupExceptions(); + // setupTimer(); __asm__ volatile ("sti"); } \ No newline at end of file diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index 34eea3f..5e71b2d 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -83,7 +83,6 @@ } } -char HEX_PREFIX[] = "0x"; char HEX_CHARS[] = "0123456789ABCDEF"; void putHex(char** write, uint32_t x) { @@ -94,10 +93,10 @@ alreadyWriting = true; } if (alreadyWriting) { - **write = HEX_CHARS[byte & 0x0F]; - (*write)++; **write = HEX_CHARS[byte >> 8]; (*write)++; + **write = HEX_CHARS[byte & 0x0F]; + (*write)++; } } } diff --git a/src/kernel/drivers/timer/timer.c b/src/kernel/drivers/timer/timer.c new file mode 100644 index 0000000..c386a7d --- /dev/null +++ b/src/kernel/drivers/timer/timer.c @@ -0,0 +1,51 @@ +#include +#include <_stdio.h> +#include +#include +#include + + +#define PIT_A 0x40 +#define PIT_CONTROL 0x43 + +#define PIT_MASK 0xFF +#define PIT_SCALE 1193180 +#define PIT_SET 0x36 + +#define CMD_BINARY 0x00 + +#define CMD_MODE0 0x00 +#define CMD_MODE1 0x02 +#define CMD_MODE2 0x04 +#define CMD_MODE3 0x06 +#define CMD_MODE4 0x08 +#define CMD_MODE5 0x0a + +#define CMD_RW_BOTH 0x30 + +#define CMD_COUNTER0 0x00 +#define CMD_COUNTER2 0x80 + +void setTimerFreq(uint32_t hz) { + int divisor = PIT_SCALE / hz; + outb(PIT_CONTROL, CMD_BINARY | CMD_MODE3 | CMD_RW_BOTH | CMD_COUNTER0); + outb(PIT_A, (uint8_t) divisor); + outb(PIT_A, (uint8_t) (divisor >> 8)); +} + +uint64_t timer_ticks = 0; +uint8_t timer_subticks = 0; + +void timerHandler() { + printf("timer\n"); + yields(); + outb(0x20, 0x20); + __asm__ volatile ("iret"); +} + +void setupTimer() { + printf("setting timer frequency to 100Hz\n"); + setTimerFreq(100); + setInterrupt(32, &timerHandler); +} + diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 48ea59f..e52b7f9 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -13,14 +13,9 @@ initMemoryAllocation(0x1000000); // initializing stacks after the kernel seems not to work :( otherwise, _kernel_end should be passed here initOSTasks(); drawLogo(); - printf("giving up control...\n"); - yields(); - printf("Returned to mainTask!\n\n"); printf("initializing interrupts\n"); initInterrupts(); + 1/0; yields(); - printf("trying interrupt\n"); - asm ("int $0x80"); - printf("the interrupt worked!\n"); - yield(); + while(1); } \ No newline at end of file diff --git a/src/kernel/textMode/_stdio.c b/src/kernel/textMode/_stdio.c deleted file mode 100644 index fcecf6f..0000000 --- a/src/kernel/textMode/_stdio.c +++ /dev/null @@ -1,199 +0,0 @@ -#include -#include -#include <_stdio.h> -#include -#include -#include -#include -#include -#include - -uint16_t* framebuffer = (uint16_t*) FRAMEBUFFER_LOCATION; -char format = 0x0F; - -void putCharWithFormatAtOffset(uint16_t c, uint16_t position) { - *(framebuffer + position) = c; -} - -void putCharWithFormatAtPosition(uint8_t x, uint8_t y, uint32_t c) { - putCharWithFormatAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -void putCharAtOffset(char c, uint16_t offset) { - putCharWithFormatAtOffset(c | format << 8, offset); -} - -void putCharAt(char c, uint8_t x, uint8_t y) { - putCharAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -uint16_t getCharWithFormatAt(uint8_t x, uint8_t y) { - return *(framebuffer + x + y * VIDEO_WIDTH); -} - -void putCharsAt(char* string, uint8_t x, uint8_t y) { - uint16_t position = 0; - char c = *string; - while (c != 0x00) { - putCharAt(c, x + position, y); - position++; - c = *(string + position); - } -} - -void shiftUp() { - for (int y = 1; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, y-1, getCharWithFormatAt(x, y)); - } - } - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, VIDEO_HEIGHT-1, 0x00); - } -} - -void newLine() { - uint16_t offset = getCursorOffset(); - uint8_t x = offset % VIDEO_WIDTH; - uint8_t y = offset / VIDEO_WIDTH; - x = 0; - y++; - if (y >= VIDEO_HEIGHT) { - shiftUp(); - y--; - } - setCursorPosition(x, y); -} - -void putChar(char c) { - if (c == '\n') { - newLine(); - return; - } - uint16_t offset = getCursorOffset(); - putCharAtOffset(c, offset); - setCursorOffset(offset + 1); -} - -void puts(char* string) { - while (*string != 0x00) { - putChar(*string); - string++; - } -} - -char HEX_PREFIX[] = "0x"; -char HEX_CHARS[] = "0123456789ABCDEF"; - -void putHex(char** write, uint32_t x) { - uint8_t alreadyWriting = 0; - for (uint8_t position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = 0x01; - } - if (alreadyWriting) { - **write = HEX_CHARS[byte % 256]; - *write++; - **write = HEX_CHARS[byte / 256]; - *write++; - } - } -} - -uint32_t getInsertLength(char insertType, uint32_t x) { - switch (insertType) - { - case 's': - return strlen((char*) x); - case 'x': - for (uint8_t position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - return 2*position; - } - } - return 0; - } - return 0; -} - -void handleInsert(char** write, char insertType, uint32_t x) { - if (insertType == 's') { - char* string = (char*) x; - uint32_t length = strlen(string); - for (uint32_t position = 0; position < length; position++) { - **write = string[position]; - *write++; - } - return; - } - if (insertType == 'x') { - putHex(write, x); - } -} - -void printf(const char* format, ...) { - uint32_t size = 0; - va_list valist; - va_start(valist, format); - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - size += getInsertLength(format[++i], va_arg(valist, uint32_t)); - continue; - } - size++; - } - va_start(valist, format); - - char* data = malloc(size); - char* write = data; - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - handleInsert(&write, format[++i], va_arg(valist, uint32_t)); - continue; - } - *write = format[i]; - write++; - } - va_end(valist); - - Message* message = malloc(sizeof(Message)); - - message->data = data; - message->size = size; - message->next = 0x00; - sendMessage(getPrinterTask(), message); - schedule(getPrinterTask()); -} - -void clearScreen() { - setCursorOffset(0); - for (int y = 0; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharAt(' ', x, y); - } - } -} - -void setTextStyle(uint8_t style) { - format = style; -} - -void putChars(char* string, uint32_t length) { - for (uint32_t i = 0; i < length; i++) { - if (string[i] == '\n') { - newLine(); - continue; - } - putChar(string[i]); - } -} - -uint32_t strlen(char* string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} \ No newline at end of file diff --git a/src/kernel/textMode/cursor.c b/src/kernel/textMode/cursor.c deleted file mode 100644 index 8247c5e..0000000 --- a/src/kernel/textMode/cursor.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include -#include <_stdio.h> - -void setCursorOffset(uint16_t offset) { - outb(0x3D4, 0x0F); - outb(0x3D5, (uint8_t) (offset & 0xFF)); - outb(0x3D4, 0x0E); - outb(0x3D5, (uint8_t) ((offset >> 8) & 0xFF)); -} - -void setCursorPosition(uint8_t x, uint8_t y) { - setCursorOffset(x + y * VIDEO_WIDTH); -} - -uint16_t getCursorOffset() { - uint16_t result = 0; - outb(0x3D4, 0x0F); - result |= inb(0x3D5); - outb(0x3D4, 0x0E); - result |= ((uint16_t)inb(0x3D5)) << 8; - return result; -} \ No newline at end of file diff --git a/src/kernel/util/ports.c b/src/kernel/util/ports.c index 6cfbb2a..ab0066e 100644 --- a/src/kernel/util/ports.c +++ b/src/kernel/util/ports.c @@ -9,4 +9,8 @@ uint8_t result; __asm__("in %%dx, %%al" : "=a" (result) : "d" (port)); return result; +} + +void ioWait() { + asm volatile ( "outb %%al, $0x80" : : "a"(0) ); } \ No newline at end of file