🆕 新增了使用8259A芯片的中断响应程序

This commit is contained in:
fslongjin
2022-01-29 12:52:25 +08:00
parent 49eaf6ce95
commit eff673edcb
8 changed files with 302 additions and 7 deletions

35
kernel/exception/8259A.c Normal file
View File

@ -0,0 +1,35 @@
#include "8259A.h"
#include "irq.c"
#include "../common/printk.h"
#include "../common/kprint.h"
void init_8259A()
{
// 初始化中断门, 中断使用第二个ist
for(int i=32;i<=55;++i)
set_intr_gate(i, 2, interrupt[i-32]);
kinfo("Initializing 8259A...");
// 初始化主芯片
io_out8(0x20, 0x11); // 初始化主芯片的icw1
io_out8(0x21, 0x20); // 设置主芯片的中断向量号为0x20(0x20-0x27)
io_out8(0x21, 0x04); // 设置int2端口级联从芯片
io_out8(0x21, 0x01); // 设置为AEOI模式、FNM、无缓冲
// 初始化从芯片
io_out8(0xa0, 0x11);
io_out8(0xa1, 0x28); // 设置从芯片的中断向量号为0x28(0x28-0x2f)
io_out8(0xa1, 0x02); // 设置从芯片连接到主芯片的int2
io_out8(0xa1, 0x01);
// 设置ocw1, 允许所有中断请求
io_out8(0x21, 0x00);
io_out8(0xa1, 0x00);
sti();
kinfo("IRQ circuit 8259A initialized.");
}

25
kernel/exception/8259A.h Normal file
View File

@ -0,0 +1,25 @@
/**
* @file 8259A.h
* @author longjin
* @brief 8259A中断芯片
* @version 0.1
* @date 2022-01-29
*
* @copyright Copyright (c) 2022
*
*/
#pragma once
#include "../common/glib.h"
#define PIC_EOI 0x20
#define PIC_master 0x20 /* IO base address for master PIC */
#define PIC2_slave 0xA0 /* IO base address for slave PIC */
// 初始化8259A芯片的中断服务
void init_8259A();

134
kernel/exception/irq.c Normal file
View File

@ -0,0 +1,134 @@
#include "irq.h"
#include "8259A.h"
#include "../common/asm.h"
#include"../common/printk.h"
#include "gate.h"
// 保存函数调用现场的寄存器
#define SAVE_ALL_REGS \
"cld; \n\t" \
"pushq %rax; \n\t" \
"pushq %rax; \n\t" \
"movq %es, %rax; \n\t" \
"pushq %rax; \n\t" \
"movq %ds, %rax; \n\t" \
"pushq %rax; \n\t" \
"xorq %rax, %rax;\n\t" \
"pushq %rbp; \n\t" \
"pushq %rdi; \n\t" \
"pushq %rsi; \n\t" \
"pushq %rdx; \n\t" \
"pushq %rcx; \n\t" \
"pushq %rbx; \n\t" \
"pushq %r8 ; \n\t" \
"pushq %r9 ; \n\t" \
"pushq %r10; \n\t" \
"pushq %r11; \n\t" \
"pushq %r12; \n\t" \
"pushq %r13; \n\t" \
"pushq %r14; \n\t" \
"pushq %r15; \n\t" \
"movq $0x10, %rdx;\n\t" \
"movq %rdx, %ds; \n\t" \
"movq %rdx, %es; \n\t"
// 定义IRQ处理函数的名字格式IRQ+中断号+interrupt
#define IRQ_NAME2(name1) name1##interrupt(void)
#define IRQ_NAME(number) IRQ_NAME2(IRQ##number)
// 构造中断entry
// 为了复用返回函数的代码需要压入一个错误码0
#define Build_IRQ(number) \
void IRQ_NAME(number); \
__asm__ (SYMBOL_NAME_STR(IRQ)#number"interrupt: \n\t" \
"pushq $0x00 \n\t" \
SAVE_ALL_REGS \
"movq %rsp, %rdi\n\t" \
"leaq ret_from_intr(%rip), %rax\n\t" \
"pushq %rax \n\t" \
"movq $"#number", %rsi \n\t" \
"jmp do_IRQ\n\t");
// 构造中断入口
Build_IRQ(0x20)
Build_IRQ(0x21)
Build_IRQ(0x22)
Build_IRQ(0x23)
Build_IRQ(0x24)
Build_IRQ(0x25)
Build_IRQ(0x26)
Build_IRQ(0x27)
Build_IRQ(0x28)
Build_IRQ(0x29)
Build_IRQ(0x2a)
Build_IRQ(0x2b)
Build_IRQ(0x2c)
Build_IRQ(0x2d)
Build_IRQ(0x2e)
Build_IRQ(0x2f)
Build_IRQ(0x30)
Build_IRQ(0x31)
Build_IRQ(0x32)
Build_IRQ(0x33)
Build_IRQ(0x34)
Build_IRQ(0x35)
Build_IRQ(0x36)
Build_IRQ(0x37)
// 初始化中断数组
void (*interrupt[24])(void)=
{
IRQ0x20interrupt,
IRQ0x21interrupt,
IRQ0x22interrupt,
IRQ0x23interrupt,
IRQ0x24interrupt,
IRQ0x25interrupt,
IRQ0x26interrupt,
IRQ0x27interrupt,
IRQ0x28interrupt,
IRQ0x29interrupt,
IRQ0x2ainterrupt,
IRQ0x2binterrupt,
IRQ0x2cinterrupt,
IRQ0x2dinterrupt,
IRQ0x2einterrupt,
IRQ0x2finterrupt,
IRQ0x30interrupt,
IRQ0x31interrupt,
IRQ0x32interrupt,
IRQ0x33interrupt,
IRQ0x34interrupt,
IRQ0x35interrupt,
IRQ0x36interrupt,
IRQ0x37interrupt,
};
/**
* @brief 初始化中断模块
*/
void init_irq()
{
init_8259A();
}
/**
* @brief 中断服务程序
*
* @param rsp 中断栈指针
* @param number 中断号
*/
void do_IRQ(ul rsp, ul number)
{
if(number!=0x20)
printk_color(ORANGE, BLACK, "Received irq:%#018x\n", number);
// 向主芯片发送中断结束信号
io_out8(PIC_master, PIC_EOI);
}

29
kernel/exception/irq.h Normal file
View File

@ -0,0 +1,29 @@
/**
* @file irq.h
* @author longjin
* @brief 中断处理程序
* @version 0.1
* @date 2022-01-28
*
* @copyright Copyright (c) 2022
*
*/
#pragma once
#include "../common/glib.h"
/**
* @brief 初始化中断模块
*/
void init_irq();
/**
* @brief 中断服务程序
*
* @param rsp 中断栈指针
* @param number 中断号
*/
void do_IRQ(ul rsp, ul number);