🆕 引入APIC驱动,完成local apic的初始化

This commit is contained in:
fslongjin
2022-03-04 13:40:22 +08:00
parent eb11be9270
commit 574f323284
10 changed files with 350 additions and 109 deletions

View File

@ -1,7 +1,9 @@
#include "8259A.h"
#include "../../../common/printk.h"
#include "../../../common/kprint.h"
#include "../../../exception/gate.h"
// 导出定义在irq.c中的中段门表
extern void (*interrupt_table[24])(void);
void init_8259A()
@ -34,3 +36,31 @@ void init_8259A()
}
/**
* @brief 中断服务程序
*
* @param rsp 中断栈指针
* @param number 中断号
*/
void do_IRQ(struct pt_regs *regs, ul number)
{
unsigned char x;
switch (number)
{
case 0x20: // 时钟中断信号
break;
case 0x21: // 键盘中断
x = io_in8(0x60);
printk_color(ORANGE, BLACK, "Received key irq, key code:%#018lx\n", x);
break;
default:
break;
}
if(number!=0x20)
printk_color(ORANGE, BLACK, "Received irq:%#018x\n", number);
// 向主芯片发送中断结束信号
io_out8(PIC_master, PIC_EOI);
}

View File

@ -12,6 +12,7 @@
#pragma once
#include "../../../common/glib.h"
#include "../../../exception/irq.h"
#define PIC_EOI 0x20
#define PIC_master 0x20 /* IO base address for master PIC */
@ -20,6 +21,13 @@
// 初始化8259A芯片的中断服务
void init_8259A();
/**
* @brief 中断服务程序
*
* @param rsp 中断栈指针
* @param number 中断号
*/
void do_IRQ(struct pt_regs* rsp, ul number);

View File

@ -0,0 +1,200 @@
#include "apic.h"
#include "../../../common/kprint.h"
#include "../../../common/printk.h"
#include "../../../common/cpu.h"
#include "../../../common/glib.h"
#include "../../../exception/gate.h"
// 导出定义在irq.c中的中段门表
extern void (*interrupt_table[24])(void);
bool flag_support_apic = false;
bool flag_support_x2apic = false;
uint local_apic_version;
uint local_apic_max_LVT_entries;
/**
* @brief 初始化io_apic
*
*/
void apic_io_apic_init()
{
// 初始化中断门, 中断使用第二个ist
for (int i = 32; i <= 55; ++i)
set_intr_gate(i, 2, interrupt_table[i - 32]);
// 屏蔽类8259A芯片
io_out8(0x21, 0xff);
io_out8(0xa1, 0xff);
kdebug("8259A Masked.");
apic_local_apic_init();
sti();
}
/**
* @brief 初始化local apic
*
*/
void apic_local_apic_init()
{
uint a, b, c, d;
cpu_cpuid(1, 0, &a, &b, &c, &d);
kdebug("CPUID 0x01, eax:%#010lx, ebx:%#010lx, ecx:%#010lx, edx:%#010lx", a, b, c, d);
// 判断是否支持APIC和xAPIC
if ((1 << 9) & d)
{
flag_support_apic = true;
kdebug("This computer support APIC&xAPIC");
}
else
{
flag_support_apic = false;
kerror("This computer does not support APIC&xAPIC");
while (1)
;
}
// 判断是否支持x2APIC
if ((1 << 21) & c)
{
flag_support_x2apic = true;
kdebug("This computer support x2APIC");
}
else
{
kerror("This computer does not support x2APIC");
}
uint eax, edx;
// 启用xAPIC 和x2APIC
__asm__ __volatile__("movq $0x1b, %%rcx \n\t" // 读取IA32_APIC_BASE寄存器
"rdmsr \n\t"
"bts $10, %%rax \n\t"
"bts $11, %%rax \n\t"
"wrmsr \n\t"
"movq $0x1b, %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)::"memory");
kdebug("After enable xAPIC and x2APIC: edx=%#010x, eax=%#010x", edx, eax);
// 检测是否成功启用xAPIC和x2APIC
if (eax & 0xc00)
kinfo("xAPIC & x2APIC enabled!");
// 设置SVR寄存器开启local APIC、禁止EOI广播
__asm__ __volatile__("movq 0x80f, %%rcx \n\t"
"rdmsr \n\t"
"bts $8, %%rax \n\t"
"bts $12, %%rax \n\t"
"movq 0x80f, %%rcx \n\t"
"wrmsr \n\t"
"movq $0x80f , %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)::"memory", "rcx");
/*
//enable SVR[8]
__asm__ __volatile__( "movq $0x80f, %%rcx \n\t"
"rdmsr \n\t"
"bts $8, %%rax \n\t"
"bts $12,%%rax\n\t"
"wrmsr \n\t"
"movq $0x80f, %%rcx \n\t"
"rdmsr \n\t"
:"=a"(eax),"=d"(edx)
:
:"memory");
*/
kdebug("After setting SVR: edx=%#010x, eax=%#010x", edx, eax);
if (eax & 0x100)
kinfo("APIC Software Enabled.");
if (eax & 0x1000)
kinfo("EOI-Broadcast Suppression Enabled.");
// 获取Local APIC的基础信息 参见英特尔开发手册Vol3A 10-39
// Table 10-6. Local APIC Register Address Map Supported by x2APIC
// 获取 Local APIC ID
// 0x802处是x2APIC ID 位宽32bits 的 Local APIC ID register
__asm__ __volatile__("movq $0x802, %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)::"memory");
kdebug("get Local APIC ID: edx=%#010x, eax=%#010x", edx, eax);
// 获取Local APIC Version
// 0x803处是 Local APIC Version register
__asm__ __volatile__("movq $0x803, %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)::"memory");
local_apic_max_LVT_entries = ((eax >> 16) & 0xff) + 1;
local_apic_version = eax & 0xff;
kdebug("local APIC Version:%#010x,Max LVT Entry:%#010x,SVR(Suppress EOI Broadcast):%#04x\t", local_apic_version, local_apic_max_LVT_entries, (eax >> 24) & 0x1);
if ((eax & 0xff) < 0x10)
{
kdebug("82489DX discrete APIC");
}
else if (((eax & 0xff) >= 0x10) && ((eax & 0xff) <= 0x15))
kdebug("Integrated APIC.");
// 由于尚未配置LVT对应的处理程序因此先屏蔽所有的LVT
__asm__ __volatile__(
"movq $0x82f, %%rcx \n\t" // CMCI
"wrmsr \n\t"
"movq $0x832, %%rcx \n\t" // Timer
"wrmsr \n\t"
"movq $0x833, %%rcx \n\t" // Thermal Monitor
"wrmsr \n\t"
"movq $0x834, %%rcx \n\t" // Performance Counter
"wrmsr \n\t"
"movq $0x835, %%rcx \n\t" // LINT0
"wrmsr \n\t"
"movq $0x836, %%rcx \n\t" // LINT1
"wrmsr \n\t"
"movq $0x837, %%rcx \n\t" // Error
"wrmsr \n\t"
:
: "a"(0x10000), "d"(0x00)
: "memory");
kdebug("All LVT Masked");
// 获取TPR寄存器的值
__asm__ __volatile__("movq $0x808, %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)::"memory");
kdebug("LVT_TPR=%#010x", eax);
// 获取PPR寄存器的值
__asm__ __volatile__("movq $0x80a, %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)::"memory");
kdebug("LVT_PPR=%#010x", eax);
}
/**
* @brief 初始化apic控制器
*
*/
void apic_init()
{
apic_io_apic_init();
}
/**
* @brief 中断服务程序
*
* @param rsp 中断栈指针
* @param number 中断号
*/
void do_IRQ(struct pt_regs *rsp, ul number)
{
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "../../../common/asm.h"
#include"../../../process/ptrace.h"
#include"../../../exception/irq.h"
/**
* @brief 中断服务程序
*
* @param rsp 中断栈指针
* @param number 中断号
*/
void do_IRQ(struct pt_regs* rsp, ul number);
/**
* @brief 初始化apic控制器
*
*/
void apic_init();