Skip to content

Latest commit

 

History

History
105 lines (82 loc) · 2.36 KB

File metadata and controls

105 lines (82 loc) · 2.36 KB

Code: ISR.asm and InterruptHandler.c that defines behavior for interrupt

;; part of ISR code because most code is similar

%macro KSAVECONTEXT 0
    ;; push general registers

    push rbp
    mov rbp, rsp
    push rax
    push rbx
    push rcx
    push rdx
    push rdi
    push rsi
    push r8
    push r9
    push r10
    push r11
    push r12
    push r13
    push r14
    push r15

    mov ax, ds
    push rax
    mov ax, es
    push rax
    push fs
    push gs


    ;; switch segment selector

    mov ax, 0x10    ; 0x10: kernel data segment descriptor offset
    mov ds, ax
    mov es, ax
    mov gs, ax
    mov fs, ax
%endmacro

kISRDoubleFault:
    KSAVECONTEXT
    mov rdi, 8
    mov rsi, qword [rbp + 8] 
    call kCommonExceptionHandler
    KLOADCONTEXT
    add rsp, 8  ; remove error code from stack
    iretq
/* part of InterruptHandler.h 
 * rest code is for handling interrupt that does not have certain behavior
 * to do
 */


// PIC keyboard Interrupt Handler. This function prints how many this
// interrupt is received from booting to the top right corner
// params:
//   iVectorNumber: IDT gate descriptor index number
void kKeyboardHandler(int iVectorNumber) {
    char vcBuffer[] = "[INT:  , ]";
    // count how many interrupt occurs
    static int g_iCommonInterruptCount = 0;

    // interrupt number as ASCII two-digit number
    vcBuffer[5] = '0' + iVectorNumber / 10;
    vcBuffer[6] = '0' + iVectorNumber % 10;

    // interrupt count
    vcBuffer[8] = '0' + g_iCommonInterruptCount;
    g_iCommonInterruptCount = (g_iCommonInterruptCount + 1) % 10;
    kPrintString(70, 0, vcBuffer);

    // send EOI to PIC controller
    // reason for iVectorNumber - PIC_IRQSTARTVECTOR is that
    // iVectorNumber is number defined in ISR.
    // iVectorNumber is not IRQ number
    kSendEOIToPIC(iVectorNumber - PIC_IRQSTARTVECTOR);
}

Explanation

What does the code do?

  1. When interrupt or exception occurs, the first call is a function in ISR.asm

  2. The function in assembly save registers to stack referenced by IST for context switch and call function defined in C

  3. After the C function, ISR restores the context and then return to previous code

  • functions in ISR.asm is intermediate functions

Assembly

  1. %macro and %endmacro is #define in C

  2. iretq restores context that was stored into stack because of interrupt and return to caller