Kernel.cpp Code:
Code: Select all
#include "types.h"
#include "gdt.h"
#include "interrupts.h"
void printf(char* str)
{
static uint16_t* VideoMemory = (uint16_t*)0xb8000;
static uint8_t x =0 , y=0;
for(int i = 0; str[i] != '\0'; i++)
{
switch(str[i])
{
case '\n':
y++;
x = 0;
break;
default:
VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | str[i];
x++;
break;
}
if(x >= 80)
{
y++;
x = 0;
}
if(y >= 25)
{
for(y = 0; y < 25; y++)
for(x = 0; x < 80; x++)
VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | ' ';
x = 0;
y=0;
}
}
}
extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot_magic*/)
{
printf("Kapilar Onyukleyicisine Hosgeldiniz...");
GlobalDescriptorTable gdt;
InterruptManager interrupts(&gdt);
interrupts.Activate();
while(1);
}
Code: Select all
#include "interrupts.h"
void printf(char* str);
static void SetInterruptDescriptorTableEntry(
uint8_t interruptNumber,
uint16_t codeSegmentSelectorOffset,
void (*handler) (),
uint8_t DescriptorPrivlegeLevel,
uint8_t DescriptorType)
{
uint8_t const IDT_DESC_PRESENT = 0x80;
interruptDescriptorTable[interruptNumber].handlerAdressLowBits = ((uint32_t)handler) & 0xFFFF;
interruptDescriptorTable[interruptNumber].handlerAdressHighBits = (((uint32_t)handler) >> 16) & 0xFFFF;
interruptDescriptorTable[interruptNumber].gdt_codeSegmentSelector = codeSegmentSelectorOffset;
interruptDescriptorTable[interruptNumber].access = IDT_DESC_PRESENT | DescriptorType | ((DescriptorPrivlegeLevel&3) << 5);
interruptDescriptorTable[interruptNumber].reserved = 0;
}
InterruptManager::InterruptManager(GlobalDescriptorTable* gdt)
: picMasterCommand(0x20),
picMasterData(0x21),
picSlaveCommand(0xA0),
picSlaveData(0xA1)
{
uint16_t CodeSegment = gdt->CodeSegmentSelector();
const uint8_t IDT_INTERRUPT_GATE = 0xE;
for(uint16_t i = 0; i < 256; i++)
SetInterruptDescriptorTableEntry(i, CodeSegment, &IgnoreInterruptRequest, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x20, CodeSegment, &HandleInterruptRequest0x00, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x21, CodeSegment, &HandleInterruptRequest0x01, 0, IDT_INTERRUPT_GATE);
picMasterCommand.Write(0x11);
picSlaveCommand.Write(0x11);
picMasterData.Write(0x20);
picSlaveData.Write(0x28);
picMasterData.Write(0x04);
picSlaveData.Write(0x02);
picMasterData.Write(0x01);
picSlaveData.Write(0x01);
picMasterData.Write(0x00);
picSlaveData.Write(0x00);
InterruptDescriptorTablePointer idt;
idt.size = 256 * sizeof(GateDescriptor) - 1;
idt.base = (uint32_t)interruptDescriptorTable;
asm volatile("lidt %0" : : "m" (lidt));
}
InterruptManager::~InterruptManager()
{
}
void InterruptManager::Activate()
{
asm("sti");
}
uint32_t InterruptManager::handleInterrupt(uint8_t interruptNumber, uint32_t esp)
{
printf(" INTERRUPT");
return esp;
}
interrupts.h
Code: Select all
#ifndef __INTERRUPTS_H
#define __INTERRUPTS_H
#include "types.h"
#include "port.h"
#include "gdt.h"
class InterruptManager
{
protected:
struct GateDescriptor
{
uint16_t handlerAdressLowBits;
uint16_t gdt_codeSegmentSelector;
uint16_t reserved;
uint16_t access;
uint16_t handlerAdressHighBits;
} __attribute__((packed));
static GateDescriptor interruptDescriptorTable[256];
struct InterruptDescriptorTablePointer
{
uint16_t size;
uint32_t base;
} __attribute__((packed));
static void SetInterruptDescriptorTableEntry(
uint8_t interruptNumber,
uint16_t codeSegmentSelectorOffset,
void (*handler) (),
uint8_t DescriptorPrivlegeLevel,
uint8_t DescriptorType
);
Port8BitSlow picMasterCommand;
Port8BitSlow picMasterData;
Port8BitSlow picSlaveCommand;
Port8BitSlow picSlaveData;
public:
InterruptManager(GlobalDescriptorTable* gdt);
~InterruptManager();
void Activate();
static uint32_t handleInterrupt(uint8_t interruptNumber, uint32_t esp);
static void IgnoreInterruptRequest();
static void HandleInterruptRequest0x00();
static void HandleInterruptRequest0x01();
};
#endif
Code: Select all
.set IRQ_BASE, 0X20
.section .text
.extern _ZN16InterruptManager15handleInterruptEhj
.global _ZN16InterruptManager22IgnoreInterruptRequestEv
.macro HandleException num
.global _ZN16InterruptManager16HandleException\num\()Ev
_ZN16InterruptManager16HandleException\num\()Ev:
movb $\num, (interruptnumber)
jmp int_bottom
.endm
.macro HandleInterruptRequest num
.global _ZN16InterruptManager26HandleInterruptRequest\num\()Ev
_ZN16InterruptManager26HandleInterruptRequest\num\()Ev:
movb $\num + IRQ_BASE, (interruptnumber)
jmp int_bottom
.endm
HandleInterruptRequest 0x00
HandleInterruptRequest 0x01
int_bottom:
pusha
pushl %ds
pushl %es
pushl %fs
pushl %gs
pushl% esp
push (interruptnumber)
call _ZN16InterruptManager15handleInterruptEhj
# addl $5, %esp
movl %eax, %esp
popl %gs
popl %fs
popl %es
popl %ds
popa
_ZN16InterruptManager22IgnoreInterruptRequestEv:
iret
.data
interruptnumber: .byte 0