Reputation: 212
Trying to make interrupts working in 64-bit higher half kernel. github repo: https://github.com/JustVic/kernel_interrupts
all interupts code in this three files: trap.h, trap.c, trap.S
trap.h:
#ifndef _TRAP_H_
#define _TRAP_H_
#include "stdint.h"
struct IdtEntry{
uint16_t low;
uint16_t selector;
uint8_t res0;
uint8_t attr;
uint16_t mid;
uint32_t high;
uint32_t res1;
} __attribute__((packed));
struct IdtPtr {
uint16_t limit;
uint64_t addr;
} __attribute__((packed));
struct TrapFrame {
int64_t r15;
int64_t r14;
int64_t r13;
int64_t r12;
int64_t r11;
int64_t r10;
int64_t r9;
int64_t r8;
int64_t rbp;
int64_t rdi;
int64_t rsi;
int64_t rdx;
int64_t rcx;
int64_t rbx;
int64_t rax;
int64_t trapno;
int64_t errorcode;
int64_t rip;
int64_t cs;
int64_t rflags;
int64_t rsp;
int64_t ss;
};
void vector0(void);
void vector1(void);
void vector2(void);
void vector3(void);
void vector4(void);
void vector5(void);
void vector6(void);
void vector7(void);
void vector8(void);
void vector10(void);
void vector11(void);
void vector12(void);
void vector13(void);
void vector14(void);
void vector16(void);
void vector17(void);
void vector18(void);
void vector19(void);
void vector32(void);
void vector39(void);
void init_idt(void);
void eoi(void);
void load_idt(struct IdtPtr *ptr);
unsigned char read_isr(void);
#endif
trap.c:
#include "trap.h"
struct IdtPtr idt_pointer;
struct IdtEntry vectors[256];
void init_idt_entry(struct IdtEntry *entry, uint64_t addr, uint8_t attribute)
{
entry->low = (uint16_t)addr;
entry->selector = 0x08;
entry->attr = attribute;
entry->mid = (uint16_t)(addr>>16);
entry->high = (uint32_t)(addr>>32);
}
void init_idt(void)
{
init_idt_entry(&vectors[0],(uint64_t)vector0,0x8e);
init_idt_entry(&vectors[1],(uint64_t)vector1,0x8e);
init_idt_entry(&vectors[2],(uint64_t)vector2,0x8e);
init_idt_entry(&vectors[3],(uint64_t)vector3,0x8e);
init_idt_entry(&vectors[4],(uint64_t)vector4,0x8e);
init_idt_entry(&vectors[5],(uint64_t)vector5,0x8e);
init_idt_entry(&vectors[6],(uint64_t)vector6,0x8e);
init_idt_entry(&vectors[7],(uint64_t)vector7,0x8e);
init_idt_entry(&vectors[8],(uint64_t)vector8,0x8e);
init_idt_entry(&vectors[10],(uint64_t)vector10,0x8e);
init_idt_entry(&vectors[11],(uint64_t)vector11,0x8e);
init_idt_entry(&vectors[12],(uint64_t)vector12,0x8e);
init_idt_entry(&vectors[13],(uint64_t)vector13,0x8e);
init_idt_entry(&vectors[14],(uint64_t)vector14,0x8e);
init_idt_entry(&vectors[16],(uint64_t)vector16,0x8e);
init_idt_entry(&vectors[17],(uint64_t)vector17,0x8e);
init_idt_entry(&vectors[18],(uint64_t)vector18,0x8e);
init_idt_entry(&vectors[19],(uint64_t)vector19,0x8e);
init_idt_entry(&vectors[32],(uint64_t)vector32,0x8e);
init_idt_entry(&vectors[39],(uint64_t)vector39,0x8e);
idt_pointer.limit = sizeof(vectors)-1;
idt_pointer.addr = (uint64_t)vectors;
load_idt(&idt_pointer);
}
void handler(struct TrapFrame *tf)
{
unsigned char isr_value;
printk("received interupt:");
printk(" %d\n", tf->trapno);
switch (tf->trapno) {
case 32:
eoi();
break;
case 39:
isr_value = read_isr();
if ((isr_value&(1<<7)) != 0) {
eoi();
}
break;
default:
while (1) { }
}
}
trap.S:
.text
.extern handler
.global vector0
.global vector1
.global vector2
.global vector3
.global vector4
.global vector5
.global vector6
.global vector7
.global vector8
.global vector10
.global vector11
.global vector12
.global vector13
.global vector14
.global vector16
.global vector17
.global vector18
.global vector19
.global vector32
.global vector39
.global eoi
.global read_isr
.global load_idt
Trap:
push %rax
push %rbx
push %rcx
push %rdx
push %rsi
push %rdi
push %rbp
push %r8
push %r9
push %r10
push %r11
push %r12
push %r13
push %r14
push %r15
mov %rsp,%rdi
call handler
TrapReturn:
pop %r15
pop %r14
pop %r13
pop %r12
pop %r11
pop %r10
pop %r9
pop %r8
pop %rbp
pop %rdi
pop %rsi
pop %rdx
pop %rcx
pop %rbx
pop %rax
add $16,%rsp
iretq
vector0:
push $0
push $0
jmp Trap
vector1:
push $0
push $1
jmp Trap
vector2:
push $0
push $2
jmp Trap
vector3:
push $0
push $3
jmp Trap
vector4:
push $0
push $4
jmp Trap
vector5:
push $0
push $5
jmp Trap
vector6:
push $0
push $6
jmp Trap
vector7:
push $0
push $7
jmp Trap
vector8:
push $8
jmp Trap
vector10:
push $10
jmp Trap
vector11:
push $11
jmp Trap
vector12:
push $12
jmp Trap
vector13:
push $13
jmp Trap
vector14:
push $14
jmp Trap
vector16:
push $0
push $16
jmp Trap
vector17:
push $17
jmp Trap
vector18:
push $0
push $18
jmp Trap
vector19:
push $0
push $19
jmp Trap
vector32:
push $0
push $32
jmp Trap
vector39:
push $0
push $39
jmp Trap
eoi:
mov $0x20,%al
out %al, $0x20
ret
read_isr:
mov $11,%al
out %al, $0x20
in $0x20,%al
ret
load_idt:
lidt (%rdi)
ret
GDT setup in boot32.S in the entry function _start:
...
lgdt (init_gdt64_ptr)
...
and in data section:
.section .data
.align 16
gdt64:
.quad 0x0000000000000000 // 0x00 NULL
.quad 0x0020980000000000 // 0x08 KCODE64
gdt64_end:
.align 16
init_gdt64_ptr:
.word gdt64_end - gdt64 - 1
.long gdt64
If I run this Qemu reboots. I suspect it's triple fault. Help me to understang why it's not working?
Upvotes: 1
Views: 153
Reputation: 47613
From what I can see in your code, once you enter long mode you remove the identity mapping when you do this:
movq $0x0, p4_table
invlpg 0
The problem is that from this point on all the data structures and variables you had in the 32-bit code are no longer accessible, which includes one very important thing - the GDT. Before you remove the identity mapping you should fix up the GDT pointer and reload it with LGDT
based on the high memory address mapping. You will have to add KERNEL_VMA
to the base address within the GDT pointer and you will have to load the GDT relative to KERNEL_VMA
with LGDT
.
In boot32.S
I'd put a label for the base address within the init_gdt64_ptr
and make it globally visible so we can access it in boot64.S
.global init_gdt64_ptr_baseaddr
init_gdt64_ptr:
.word gdt64_end - gdt64 - 1
init_gdt64_ptr_baseaddr:
.quad gdt64 # Change to QUAD from LONG
I have also extended the size of the base address in init_gdt64_ptr
from .long
to .quad
so that this GDT pointer can be loaded with LGDT
in either 32-bit protected mode or long mode (64-bit).
In boot64.s
we can add KERNEL_VMA
to the base address and also load init_gdt64_ptr
relative to high memory as well like this:
mov $KERNEL_VMA, %rax
add %rax, init_gdt64_ptr_baseaddr # Adjust the base pointer to high memory address
lgdt init_gdt64_ptr(%rax) # Reload the GDT relative to its high memory address
add %rax, %rsp # You were already adjusting the stack, I just moved it
// Setup segment selectors # Load the segment registers
movw $0, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss
movq $0x0, p4_table
invlpg 0
Now we have a GDT that resides in high memory. I believe that the problem you are encountering is related to the fact that when you do int $0x3
in your kernel that an attempt is made to reload the CS register by the CPU but it fails because your GDT in lower memory is no longer mapped and thus a fault occurs, eventually leading to a triple fault.
Upvotes: 3