mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-19 00:46:31 +00:00
使用rust重写了apic的驱动 (#425)
* 使用rust重写了apic的驱动。 * 修正signal和调度器的部分加锁逻辑,增加回退策略。 * 把pcb的flags字段替换为无锁的 * 使用cargo管理apic的编译 * 删除makefile中指定PIC的变量 --------- Co-authored-by: Gou Ngai <ymd7823@outlook.com> Co-authored-by: 櫻井桃華 <89176634+TihayaKousaka@users.noreply.github.com>
This commit is contained in:
@ -1,14 +1,14 @@
|
||||
|
||||
CFLAGS += -I .
|
||||
|
||||
kernel_driver_subdirs:=interrupt pci acpi disk keyboard mouse multiboot2 timers hid
|
||||
kernel_driver_subdirs:=pci acpi disk keyboard mouse multiboot2 timers hid
|
||||
|
||||
ECHO:
|
||||
@echo "$@"
|
||||
|
||||
$(kernel_driver_subdirs): ECHO
|
||||
|
||||
$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)"
|
||||
$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)"
|
||||
|
||||
all: $(kernel_driver_subdirs)
|
||||
|
||||
|
@ -9,7 +9,7 @@ ECHO:
|
||||
@echo "$@"
|
||||
|
||||
$(kernel_driver_hid_subdirs): ECHO
|
||||
$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)"
|
||||
$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)"
|
||||
|
||||
$(kernel_driver_hid_objs): ECHO
|
||||
$(CC) $(CFLAGS) -c $@ -o $@.o
|
||||
|
@ -1,66 +0,0 @@
|
||||
#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()
|
||||
{
|
||||
// 初始化中断门, 中断使用第0个ist
|
||||
for(int i=32;i<=55;++i)
|
||||
set_intr_gate(i, 0, interrupt_table[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.");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
/**
|
||||
* @file 8259A.h
|
||||
* @author longjin
|
||||
* @brief 8259A中断芯片
|
||||
* @version 0.1
|
||||
* @date 2022-01-29
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <common/glib.h>
|
||||
#include <exception/irq.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();
|
||||
|
||||
/**
|
||||
* @brief 中断服务程序
|
||||
*
|
||||
* @param rsp 中断栈指针
|
||||
* @param number 中断号
|
||||
*/
|
||||
void do_IRQ(struct pt_regs* rsp, ul number);
|
||||
|
||||
|
||||
|
@ -1,14 +0,0 @@
|
||||
|
||||
all: pic.o
|
||||
|
||||
# 中断处理芯片的驱动程序
|
||||
ifeq ($(PIC), _INTR_8259A_)
|
||||
pic.o: 8259A/8259A.c
|
||||
$(CC) $(CFLAGS) -c 8259A/8259A.c -o pic.o
|
||||
else
|
||||
pic.o: apic/apic.c apic_timer.o
|
||||
$(CC) $(CFLAGS) -c apic/apic.c -o pic.o
|
||||
|
||||
apic_timer.o: apic/apic_timer.c
|
||||
$(CC) $(CFLAGS) -c apic/apic_timer.c -o apic/apic_timer.o
|
||||
endif
|
@ -1,741 +0,0 @@
|
||||
#include "apic.h"
|
||||
#include "apic_timer.h"
|
||||
#include <common/cpu.h>
|
||||
#include <common/glib.h>
|
||||
#include <common/kprint.h>
|
||||
#include <common/printk.h>
|
||||
#include <driver/acpi/acpi.h>
|
||||
#include <exception/gate.h>
|
||||
#include <exception/softirq.h>
|
||||
#include <process/process.h>
|
||||
#include <sched/sched.h>
|
||||
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize("O0")
|
||||
// 导出定义在irq.c中的中段门表
|
||||
extern void (*interrupt_table[26])(void);
|
||||
extern uint32_t rs_current_pcb_preempt_count();
|
||||
extern uint32_t rs_current_pcb_pid();
|
||||
extern uint32_t rs_current_pcb_flags();
|
||||
|
||||
static bool flag_support_apic = false;
|
||||
static bool flag_support_x2apic = false;
|
||||
uint8_t __apic_enable_state = APIC_XAPIC_ENABLED;
|
||||
static uint local_apic_version;
|
||||
static uint local_apic_max_LVT_entries;
|
||||
|
||||
static struct acpi_Multiple_APIC_Description_Table_t *madt;
|
||||
static struct acpi_IO_APIC_Structure_t *io_apic_ICS;
|
||||
|
||||
static void __local_apic_xapic_init();
|
||||
static void __local_apic_x2apic_init();
|
||||
|
||||
static __always_inline void __send_eoi()
|
||||
{
|
||||
if (CURRENT_APIC_STATE == APIC_X2APIC_ENABLED)
|
||||
{
|
||||
__asm__ __volatile__("movq $0x00, %%rdx \n\t"
|
||||
"movq $0x00, %%rax \n\t"
|
||||
"movq $0x80b, %%rcx \n\t"
|
||||
"wrmsr \n\t" ::
|
||||
: "memory");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
io_mfence();
|
||||
__write4b(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_EOI, 0);
|
||||
io_mfence();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化io_apic
|
||||
*
|
||||
*/
|
||||
void apic_io_apic_init()
|
||||
{
|
||||
|
||||
ul madt_addr;
|
||||
acpi_iter_SDT(acpi_get_MADT, &madt_addr);
|
||||
madt = (struct acpi_Multiple_APIC_Description_Table_t *)madt_addr;
|
||||
|
||||
// kdebug("MADT->local intr controller addr=%#018lx", madt->Local_Interrupt_Controller_Address);
|
||||
// kdebug("MADT->length= %d bytes", madt->header.Length);
|
||||
// 寻找io apic的ICS
|
||||
void *ent = (void *)(madt_addr) + sizeof(struct acpi_Multiple_APIC_Description_Table_t);
|
||||
struct apic_Interrupt_Controller_Structure_header_t *header =
|
||||
(struct apic_Interrupt_Controller_Structure_header_t *)ent;
|
||||
while (header->length > 2)
|
||||
{
|
||||
header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
|
||||
if (header->type == 1)
|
||||
{
|
||||
struct acpi_IO_APIC_Structure_t *t = (struct acpi_IO_APIC_Structure_t *)ent;
|
||||
// kdebug("IO apic addr = %#018lx", t->IO_APIC_Address);
|
||||
io_apic_ICS = t;
|
||||
break;
|
||||
}
|
||||
|
||||
ent += header->length;
|
||||
}
|
||||
// kdebug("Global_System_Interrupt_Base=%d", io_apic_ICS->Global_System_Interrupt_Base);
|
||||
|
||||
apic_ioapic_map.addr_phys = io_apic_ICS->IO_APIC_Address;
|
||||
apic_ioapic_map.virtual_index_addr = (unsigned char *)APIC_IO_APIC_VIRT_BASE_ADDR;
|
||||
apic_ioapic_map.virtual_data_addr = (uint *)(APIC_IO_APIC_VIRT_BASE_ADDR + 0x10);
|
||||
apic_ioapic_map.virtual_EOI_addr = (uint *)(APIC_IO_APIC_VIRT_BASE_ADDR + 0x40);
|
||||
|
||||
// kdebug("(ul)apic_ioapic_map.virtual_index_addr=%#018lx", (ul)apic_ioapic_map.virtual_index_addr);
|
||||
// 填写页表,完成地址映射
|
||||
rs_map_phys((ul)apic_ioapic_map.virtual_index_addr, apic_ioapic_map.addr_phys, PAGE_2M_SIZE,
|
||||
PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD);
|
||||
|
||||
// 设置IO APIC ID 为0x0f000000
|
||||
*apic_ioapic_map.virtual_index_addr = 0x00;
|
||||
io_mfence();
|
||||
*apic_ioapic_map.virtual_data_addr = 0x0f000000;
|
||||
io_mfence();
|
||||
|
||||
// kdebug("I/O APIC ID:%#010x", ((*apic_ioapic_map.virtual_data_addr) >> 24) & 0xff);
|
||||
io_mfence();
|
||||
|
||||
// 获取IO APIC Version
|
||||
*apic_ioapic_map.virtual_index_addr = 0x01;
|
||||
io_mfence();
|
||||
kdebug("IO APIC Version=%d, Max Redirection Entries=%d", *apic_ioapic_map.virtual_data_addr & 0xff,
|
||||
(((*apic_ioapic_map.virtual_data_addr) >> 16) & 0xff) + 1);
|
||||
|
||||
// 初始化RTE表项,将所有RTE表项屏蔽
|
||||
for (int i = 0x10; i < 0x40; i += 2)
|
||||
{
|
||||
// 以0x20为起始中断向量号,初始化RTE
|
||||
apic_ioapic_write_rte(i, 0x10020 + ((i - 0x10) >> 1));
|
||||
}
|
||||
|
||||
// 不需要手动启动IO APIC,只要初始化了RTE寄存器之后,io apic就会自动启用了。
|
||||
// 而且不是每台电脑都有RCBA寄存器,因此不需要手动启用IO APIC
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化AP处理器的Local apic
|
||||
*
|
||||
*/
|
||||
void apic_init_ap_core_local_apic()
|
||||
{
|
||||
kinfo("Initializing AP-core's local apic...");
|
||||
uint eax, edx;
|
||||
// 启用xAPIC 和x2APIC
|
||||
uint64_t ia32_apic_base = rdmsr(0x1b);
|
||||
ia32_apic_base |= (1 << 11);
|
||||
if (flag_support_x2apic) // 如果支持x2apic,则启用
|
||||
{
|
||||
ia32_apic_base |= (1 << 10);
|
||||
wrmsr(0x1b, ia32_apic_base);
|
||||
}
|
||||
ia32_apic_base = rdmsr(0x1b);
|
||||
eax = ia32_apic_base & 0xffffffff;
|
||||
|
||||
// 检测是否成功启用xAPIC和x2APIC
|
||||
if ((eax & 0xc00) == 0xc00)
|
||||
kinfo("xAPIC & x2APIC enabled!");
|
||||
else if ((eax & 0x800) == 0x800)
|
||||
kinfo("Only xAPIC enabled!");
|
||||
else
|
||||
kerror("Both xAPIC and x2APIC are not enabled.");
|
||||
|
||||
// 设置SVR寄存器,开启local APIC、禁止EOI广播
|
||||
if (flag_support_x2apic) // 当前为x2APIC
|
||||
__local_apic_x2apic_init();
|
||||
else // 当前为xapic
|
||||
__local_apic_xapic_init();
|
||||
barrier();
|
||||
kdebug("AP-core's local apic initialized.");
|
||||
barrier();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 当前使用xapic来初始化local apic
|
||||
*
|
||||
*/
|
||||
static void __local_apic_xapic_init()
|
||||
{
|
||||
__apic_enable_state = APIC_XAPIC_ENABLED;
|
||||
// 设置svr的 apic软件使能位
|
||||
uint64_t qword = *(uint64_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_SVR);
|
||||
|
||||
qword |= (1 << 8);
|
||||
*(volatile uint64_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_SVR) = qword;
|
||||
qword = *(volatile uint64_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_SVR);
|
||||
if (qword & 0x100)
|
||||
kinfo("APIC Software Enabled.");
|
||||
if (qword & 0x1000)
|
||||
kinfo("EOI-Broadcast Suppression Enabled.");
|
||||
barrier();
|
||||
// 从 Local APIC Version register 获取Local APIC Version
|
||||
qword = *(volatile uint64_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_Version);
|
||||
qword &= 0xffffffff;
|
||||
|
||||
local_apic_max_LVT_entries = ((qword >> 16) & 0xff) + 1;
|
||||
local_apic_version = qword & 0xff;
|
||||
|
||||
kdebug("local APIC Version:%#010x,Max LVT Entry:%#010x,SVR(Suppress EOI Broadcast):%#04x\t", local_apic_version,
|
||||
local_apic_max_LVT_entries, (qword >> 24) & 0x1);
|
||||
|
||||
if ((qword & 0xff) < 0x10)
|
||||
{
|
||||
kdebug("82489DX discrete APIC");
|
||||
}
|
||||
else if (((qword & 0xff) >= 0x10) && ((qword & 0xff) <= 0x15))
|
||||
kdebug("Integrated APIC.");
|
||||
|
||||
io_mfence();
|
||||
// 如果写入这里的话,在有的机器上面会报错
|
||||
// *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_CMCI) = APIC_LVT_INT_MASKED;
|
||||
io_mfence();
|
||||
*(volatile uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_TIMER) = APIC_LVT_INT_MASKED;
|
||||
io_mfence();
|
||||
|
||||
*(volatile uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_THERMAL) = APIC_LVT_INT_MASKED;
|
||||
io_mfence();
|
||||
*(volatile uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_PERFORMANCE_MONITOR) =
|
||||
APIC_LVT_INT_MASKED;
|
||||
io_mfence();
|
||||
*(volatile uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT0) = APIC_LVT_INT_MASKED;
|
||||
io_mfence();
|
||||
*(volatile uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT1) = APIC_LVT_INT_MASKED;
|
||||
io_mfence();
|
||||
*(volatile uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_ERROR) = APIC_LVT_INT_MASKED;
|
||||
io_mfence();
|
||||
|
||||
kdebug("All LVT Masked");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 当前使用x2apic来初始化local apic
|
||||
*
|
||||
*/
|
||||
static void __local_apic_x2apic_init()
|
||||
{
|
||||
__apic_enable_state = APIC_X2APIC_ENABLED;
|
||||
uint32_t eax, edx;
|
||||
__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");
|
||||
if (eax & 0x100)
|
||||
kinfo("APIC Software Enabled.");
|
||||
if (eax & 0x1000)
|
||||
kinfo("EOI-Broadcast Suppression Enabled.");
|
||||
|
||||
// 获取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");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化local apic
|
||||
*
|
||||
*/
|
||||
void apic_local_apic_init()
|
||||
{
|
||||
uint64_t ia32_apic_base = rdmsr(0x1b);
|
||||
// kdebug("apic base=%#018lx", (ia32_apic_base & 0x1FFFFFFFFFF000));
|
||||
// 映射Local APIC 寄存器地址
|
||||
// todo:
|
||||
rs_map_phys(APIC_LOCAL_APIC_VIRT_BASE_ADDR, (ia32_apic_base & 0x1FFFFFFFFFF000), PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD);
|
||||
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
|
||||
{
|
||||
flag_support_x2apic = false;
|
||||
kwarn("This computer does not support x2APIC");
|
||||
}
|
||||
|
||||
uint eax, edx;
|
||||
// 启用xAPIC 和x2APIC
|
||||
ia32_apic_base = rdmsr(0x1b);
|
||||
ia32_apic_base |= (1 << 11);
|
||||
if (flag_support_x2apic) // 如果支持x2apic,则启用
|
||||
{
|
||||
ia32_apic_base |= (1 << 10);
|
||||
wrmsr(0x1b, ia32_apic_base);
|
||||
}
|
||||
ia32_apic_base = rdmsr(0x1b);
|
||||
eax = ia32_apic_base & 0xffffffff;
|
||||
|
||||
// 检测是否成功启用xAPIC和x2APIC
|
||||
if ((eax & 0xc00) == 0xc00)
|
||||
kinfo("xAPIC & x2APIC enabled!\n");
|
||||
else if ((eax & 0x800) == 0x800)
|
||||
kinfo("Only xAPIC enabled!");
|
||||
else
|
||||
kerror("Both xAPIC and x2APIC are not enabled.");
|
||||
|
||||
// 设置SVR寄存器,开启local APIC、禁止EOI广播
|
||||
if (flag_support_x2apic) // 当前为x2APIC
|
||||
__local_apic_x2apic_init();
|
||||
else // 当前为xapic
|
||||
__local_apic_xapic_init();
|
||||
|
||||
// 获取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);
|
||||
// kdebug("local_apic_id=%#018lx", *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_ID));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化apic控制器
|
||||
*
|
||||
*/
|
||||
int apic_init()
|
||||
{
|
||||
cli();
|
||||
kinfo("Initializing APIC...");
|
||||
// 初始化中断门, 中断使用rsp0防止在软中断时发生嵌套,然后处理器重新加载导致数据被抹掉
|
||||
for (int i = 32; i <= 57; ++i)
|
||||
set_intr_gate(i, 0, interrupt_table[i - 32]);
|
||||
|
||||
// 设置local apic中断门
|
||||
for (int i = 150; i < 160; ++i)
|
||||
set_intr_gate(i, 0, local_apic_interrupt_table[i - 150]);
|
||||
|
||||
// 屏蔽类8259A芯片
|
||||
io_out8(0x21, 0xff);
|
||||
|
||||
io_out8(0xa1, 0xff);
|
||||
|
||||
// 写入8259A pic的EOI位
|
||||
io_out8(0x20, 0x20);
|
||||
io_out8(0xa0, 0x20);
|
||||
|
||||
kdebug("8259A Masked.");
|
||||
|
||||
// enable IMCR
|
||||
io_out8(0x22, 0x70);
|
||||
io_out8(0x23, 0x01);
|
||||
|
||||
apic_local_apic_init();
|
||||
|
||||
apic_io_apic_init();
|
||||
|
||||
// get RCBA address
|
||||
io_out32(0xcf8, 0x8000f8f0);
|
||||
uint32_t RCBA_phys = io_in32(0xcfc);
|
||||
|
||||
// 获取RCBA寄存器的地址
|
||||
if (RCBA_phys > 0xfec00000 && RCBA_phys < 0xfee00000)
|
||||
RCBA_vaddr = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + RCBA_phys;
|
||||
else
|
||||
{
|
||||
RCBA_vaddr = 0;
|
||||
kwarn("Cannot get RCBA address. RCBA_phys=%#010lx", RCBA_phys);
|
||||
}
|
||||
kinfo("APIC initialized.");
|
||||
// sti();
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* @brief 中断服务程序
|
||||
*
|
||||
* @param rsp 中断栈指针
|
||||
* @param number 中断向量号
|
||||
*/
|
||||
void do_IRQ(struct pt_regs *rsp, ul number)
|
||||
{
|
||||
if((rsp->cs & 0x3) == 3)
|
||||
{
|
||||
asm volatile("swapgs":::"memory");
|
||||
}
|
||||
if (number < 0x80 && number >= 32) // 以0x80为界限,低于0x80的是外部中断控制器,高于0x80的是Local APIC
|
||||
{
|
||||
// ==========外部中断控制器========
|
||||
irq_desc_t *irq = &interrupt_desc[number - 32];
|
||||
|
||||
// 执行中断上半部处理程序
|
||||
if (irq != NULL && irq->handler != NULL)
|
||||
irq->handler(number, irq->parameter, rsp);
|
||||
else
|
||||
kwarn("Intr vector [%d] does not have a handler!");
|
||||
// 向中断控制器发送应答消息
|
||||
if (irq->controller != NULL && irq->controller->ack != NULL)
|
||||
irq->controller->ack(number);
|
||||
else
|
||||
__send_eoi();
|
||||
}
|
||||
else if (number >= 200)
|
||||
{
|
||||
apic_local_apic_edge_ack(number);
|
||||
|
||||
{
|
||||
irq_desc_t *irq = &SMP_IPI_desc[number - 200];
|
||||
if (irq->handler != NULL)
|
||||
irq->handler(number, irq->parameter, rsp);
|
||||
}
|
||||
}
|
||||
else if (number >= 150 && number < 200)
|
||||
{
|
||||
irq_desc_t *irq = &local_apic_interrupt_desc[number - 150];
|
||||
|
||||
// 执行中断上半部处理程序
|
||||
if (irq != NULL && irq->handler != NULL)
|
||||
irq->handler(number, irq->parameter, rsp);
|
||||
else
|
||||
kwarn("Intr vector [%d] does not have a handler!");
|
||||
// 向中断控制器发送应答消息
|
||||
if (irq->controller != NULL && irq->controller->ack != NULL)
|
||||
irq->controller->ack(number);
|
||||
else
|
||||
__send_eoi(); // 向EOI寄存器写入0x00表示结束中断
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
kwarn("do IRQ receive: %d", number);
|
||||
// 忽略未知中断
|
||||
return;
|
||||
}
|
||||
|
||||
// kdebug("before softirq");
|
||||
// 进入软中断处理程序
|
||||
rs_do_softirq();
|
||||
|
||||
// kdebug("after softirq");
|
||||
// 检测当前进程是否持有自旋锁,若持有自旋锁,则不进行抢占式的进程调度
|
||||
if (rs_current_pcb_preempt_count() > 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (rs_current_pcb_preempt_count() < 0)
|
||||
kBUG("current_pcb->preempt_count<0! pid=%d", rs_current_pcb_pid()); // should not be here
|
||||
|
||||
// 检测当前进程是否可被调度
|
||||
if ((rs_current_pcb_flags() & PF_NEED_SCHED) && number == APIC_TIMER_IRQ_NUM)
|
||||
{
|
||||
io_mfence();
|
||||
sched();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 读取RTE寄存器
|
||||
* 由于RTE位宽为64位而IO window寄存器只有32位,因此需要两次读取
|
||||
* @param index 索引值
|
||||
* @return ul
|
||||
*/
|
||||
ul apic_ioapic_read_rte(unsigned char index)
|
||||
{
|
||||
// 由于处理器的乱序执行的问题,需要加入内存屏障以保证结果的正确性。
|
||||
ul ret;
|
||||
// 先读取高32bit
|
||||
*apic_ioapic_map.virtual_index_addr = index + 1;
|
||||
io_mfence();
|
||||
|
||||
ret = *apic_ioapic_map.virtual_data_addr;
|
||||
ret <<= 32;
|
||||
io_mfence();
|
||||
|
||||
// 读取低32bit
|
||||
*apic_ioapic_map.virtual_index_addr = index;
|
||||
io_mfence();
|
||||
ret |= *apic_ioapic_map.virtual_data_addr;
|
||||
io_mfence();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 写入RTE寄存器
|
||||
*
|
||||
* @param index 索引值
|
||||
* @param value 要写入的值
|
||||
*/
|
||||
void apic_ioapic_write_rte(unsigned char index, ul value)
|
||||
{
|
||||
// 先写入低32bit
|
||||
*apic_ioapic_map.virtual_index_addr = index;
|
||||
io_mfence();
|
||||
|
||||
*apic_ioapic_map.virtual_data_addr = value & 0xffffffff;
|
||||
io_mfence();
|
||||
// 再写入高32bit
|
||||
value >>= 32;
|
||||
io_mfence();
|
||||
*apic_ioapic_map.virtual_index_addr = index + 1;
|
||||
io_mfence();
|
||||
*apic_ioapic_map.virtual_data_addr = value & 0xffffffff;
|
||||
io_mfence();
|
||||
}
|
||||
|
||||
// =========== 中断控制操作接口 ============
|
||||
void apic_ioapic_enable(ul irq_num)
|
||||
{
|
||||
ul index = 0x10 + ((irq_num - 32) << 1);
|
||||
ul value = apic_ioapic_read_rte(index);
|
||||
value &= (~0x10000UL);
|
||||
apic_ioapic_write_rte(index, value);
|
||||
}
|
||||
|
||||
void apic_ioapic_disable(ul irq_num)
|
||||
{
|
||||
ul index = 0x10 + ((irq_num - 32) << 1);
|
||||
ul value = apic_ioapic_read_rte(index);
|
||||
value |= (0x10000UL);
|
||||
apic_ioapic_write_rte(index, value);
|
||||
}
|
||||
|
||||
ul apic_ioapic_install(ul irq_num, void *arg)
|
||||
{
|
||||
struct apic_IO_APIC_RTE_entry *entry = (struct apic_IO_APIC_RTE_entry *)arg;
|
||||
// RTE表项值写入对应的RTE寄存器
|
||||
apic_ioapic_write_rte(0x10 + ((irq_num - 32) << 1), *(ul *)entry);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void apic_ioapic_uninstall(ul irq_num)
|
||||
{
|
||||
// 将对应的RTE表项设置为屏蔽状态
|
||||
apic_ioapic_write_rte(0x10 + ((irq_num - 32) << 1), 0x10000UL);
|
||||
}
|
||||
|
||||
void apic_ioapic_level_ack(ul irq_num) // 电平触发
|
||||
{
|
||||
__send_eoi();
|
||||
*apic_ioapic_map.virtual_EOI_addr = irq_num;
|
||||
}
|
||||
|
||||
void apic_ioapic_edge_ack(ul irq_num) // 边沿触发
|
||||
{
|
||||
|
||||
// 向EOI寄存器写入0x00表示结束中断
|
||||
/*
|
||||
uint *eoi = (uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_EOI);
|
||||
*eoi = 0x00;
|
||||
|
||||
*/
|
||||
__send_eoi();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief local apic 边沿触发应答
|
||||
*
|
||||
* @param irq_num
|
||||
*/
|
||||
|
||||
void apic_local_apic_edge_ack(ul irq_num)
|
||||
{
|
||||
// 向EOI寄存器写入0x00表示结束中断
|
||||
__send_eoi();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 读取指定类型的 Interrupt Control Structure
|
||||
*
|
||||
* @param type ics的类型
|
||||
* @param ret_vaddr 对应的ICS的虚拟地址数组
|
||||
* @param total 返回数组的元素总个数
|
||||
* @return uint
|
||||
*/
|
||||
uint apic_get_ics(const uint type, ul ret_vaddr[], uint *total)
|
||||
{
|
||||
void *ent = (void *)(madt) + sizeof(struct acpi_Multiple_APIC_Description_Table_t);
|
||||
struct apic_Interrupt_Controller_Structure_header_t *header =
|
||||
(struct apic_Interrupt_Controller_Structure_header_t *)ent;
|
||||
bool flag = false;
|
||||
|
||||
uint cnt = 0;
|
||||
|
||||
while (header->length > 2)
|
||||
{
|
||||
header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
|
||||
if (header->type == type)
|
||||
{
|
||||
ret_vaddr[cnt++] = (ul)ent;
|
||||
flag = true;
|
||||
}
|
||||
ent += header->length;
|
||||
}
|
||||
|
||||
*total = cnt;
|
||||
if (!flag)
|
||||
return APIC_E_NOTFOUND;
|
||||
else
|
||||
return APIC_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 构造RTE Entry结构体
|
||||
*
|
||||
* @param entry 返回的结构体
|
||||
* @param vector 中断向量
|
||||
* @param deliver_mode 投递模式
|
||||
* @param dest_mode 目标模式
|
||||
* @param deliver_status 投递状态
|
||||
* @param polarity 电平触发极性
|
||||
* @param irr 远程IRR标志位(只读)
|
||||
* @param trigger 触发模式
|
||||
* @param mask 屏蔽标志位,(0为未屏蔽, 1为已屏蔽)
|
||||
* @param dest_apicID 目标apicID
|
||||
*/
|
||||
void apic_make_rte_entry(struct apic_IO_APIC_RTE_entry *entry, uint8_t vector, uint8_t deliver_mode, uint8_t dest_mode,
|
||||
uint8_t deliver_status, uint8_t polarity, uint8_t irr, uint8_t trigger, uint8_t mask,
|
||||
uint8_t dest_apicID)
|
||||
{
|
||||
|
||||
entry->vector = vector;
|
||||
entry->deliver_mode = deliver_mode;
|
||||
entry->dest_mode = dest_mode;
|
||||
entry->deliver_status = deliver_status;
|
||||
entry->polarity = polarity;
|
||||
entry->remote_IRR = irr;
|
||||
entry->trigger_mode = trigger;
|
||||
entry->mask = mask;
|
||||
|
||||
entry->reserved = 0;
|
||||
|
||||
if (dest_mode == DEST_PHYSICAL)
|
||||
{
|
||||
entry->destination.physical.phy_dest = dest_apicID;
|
||||
entry->destination.physical.reserved1 = 0;
|
||||
entry->destination.physical.reserved2 = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
entry->destination.logical.logical_dest = dest_apicID;
|
||||
entry->destination.logical.reserved1 = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取当前处理器的local apic id
|
||||
*
|
||||
* @return uint32_t
|
||||
*/
|
||||
uint32_t apic_get_local_apic_id()
|
||||
{
|
||||
// 获取Local APIC的基础信息 (参见英特尔开发手册Vol3A 10-39)
|
||||
// Table 10-6. Local APIC Register Address Map Supported by x2APIC
|
||||
|
||||
if (flag_support_x2apic)
|
||||
{
|
||||
// 获取 Local APIC ID
|
||||
// 0x802处是x2APIC ID 位宽32bits 的 Local APIC ID register
|
||||
uint32_t x = 0;
|
||||
__asm__ __volatile__("movq $0x802, %%rcx \n\t"
|
||||
"rdmsr \n\t"
|
||||
: "=a"(x)::"memory");
|
||||
return x;
|
||||
}
|
||||
else
|
||||
{
|
||||
// kdebug("get Local APIC ID: edx=%#010x, eax=%#010x", edx, eax);
|
||||
// kdebug("local_apic_id=%#018lx", );
|
||||
|
||||
uint32_t x = *(uint32_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_ID);
|
||||
x = ((x >> 24) & 0xff);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入icr寄存器
|
||||
*
|
||||
* @param value 写入的值
|
||||
*/
|
||||
void apic_write_icr(uint64_t value)
|
||||
{
|
||||
if (flag_support_x2apic)
|
||||
{
|
||||
wrmsr(0x830, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// kdebug("to write icr: %#018lx", value);
|
||||
const uint64_t PENDING = 1UL << 12;
|
||||
while (*(volatile uint32_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_ICR_31_0) & PENDING)
|
||||
;
|
||||
// kdebug("write icr: %#018lx", value);
|
||||
*(volatile uint32_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_ICR_63_32) = (value >> 32) & 0xffffffff;
|
||||
*(volatile uint32_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_ICR_31_0) = value & 0xffffffff;
|
||||
|
||||
while (*(volatile uint32_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_ICR_31_0) & PENDING)
|
||||
;
|
||||
// kdebug("write icr done");
|
||||
}
|
||||
}
|
||||
|
||||
// 查询是否启用了x2APIC
|
||||
bool apic_x2apic_enabled()
|
||||
{
|
||||
return flag_support_x2apic;
|
||||
}
|
||||
#pragma GCC pop_options
|
@ -1,335 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <common/asm.h>
|
||||
#include <process/ptrace.h>
|
||||
#include <exception/irq.h>
|
||||
#include <mm/mm.h>
|
||||
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize("O0")
|
||||
|
||||
#define APIC_SUCCESS 0
|
||||
#define APIC_E_NOTFOUND 1
|
||||
|
||||
#define APIC_IO_APIC_VIRT_BASE_ADDR SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + IO_APIC_MAPPING_OFFSET
|
||||
#define APIC_LOCAL_APIC_VIRT_BASE_ADDR SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + LOCAL_APIC_MAPPING_OFFSET
|
||||
|
||||
// 当前apic启用状态标志
|
||||
extern uint8_t __apic_enable_state;
|
||||
#define APIC_XAPIC_ENABLED 0
|
||||
#define APIC_X2APIC_ENABLED 1
|
||||
#define CURRENT_APIC_STATE (__apic_enable_state)
|
||||
|
||||
// ======== local apic 寄存器虚拟地址偏移量表 =======
|
||||
// 0x00~0x10 Reserved.
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ID 0x20
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_Version 0x30
|
||||
// 0x40~0x70 Reserved.
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_TPR 0x80
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_APR 0x90
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_PPR 0xa0
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_EOI 0xb0
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_RRD 0xc0
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_LDR 0xd0
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_DFR 0xe0
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_SVR 0xf0
|
||||
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ISR_31_0 0x100
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ISR_63_32 0x110
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ISR_95_64 0x120
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ISR_127_96 0x130
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ISR_159_128 0x140
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ISR_191_160 0x150
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ISR_223_192 0x160
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ISR_255_224 0x170
|
||||
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_TMR_31_0 0x180
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_TMR_63_32 0x190
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_TMR_95_64 0x1a0
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_TMR_127_96 0x1b0
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_TMR_159_128 0x1c0
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_TMR_191_160 0x1d0
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_TMR_223_192 0x1e0
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_TMR_255_224 0x1f0
|
||||
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_IRR_31_0 0x200
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_IRR_63_32 0x210
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_IRR_95_64 0x220
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_IRR_127_96 0x230
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_IRR_159_128 0x240
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_IRR_191_160 0x250
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_IRR_223_192 0x260
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_IRR_255_224 0x270
|
||||
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ESR 0x280
|
||||
|
||||
// 0x290~0x2e0 Reserved.
|
||||
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_LVT_CMCI 0x2f0
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ICR_31_0 0x300
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_ICR_63_32 0x310
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_LVT_TIMER 0x320
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_LVT_THERMAL 0x330
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_LVT_PERFORMANCE_MONITOR 0x340
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT0 0x350
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT1 0x360
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_LVT_ERROR 0x370
|
||||
// 初始计数寄存器(定时器专用)
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_INITIAL_COUNT_REG 0x380
|
||||
// 当前计数寄存器(定时器专用)
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_CURRENT_COUNT_REG 0x390
|
||||
// 0x3A0~0x3D0 Reserved.
|
||||
// 分频配置寄存器(定时器专用)
|
||||
#define LOCAL_APIC_OFFSET_Local_APIC_CLKDIV 0x3e0
|
||||
|
||||
uint32_t RCBA_vaddr = 0; // RCBA寄存器的虚拟地址
|
||||
|
||||
/*
|
||||
|
||||
1: LVT CMCI
|
||||
2: LVT Timer
|
||||
3: LVT Thermal Monitor
|
||||
4: LVT Performace Counter
|
||||
5: LVT LINT0
|
||||
6: LVT LINT1
|
||||
7: LVT Error
|
||||
|
||||
*/
|
||||
/**
|
||||
* LVT表项
|
||||
* */
|
||||
struct apic_LVT
|
||||
{
|
||||
uint vector : 8, // 0-7位全部置为1
|
||||
delivery_mode : 3, // 第[10:8]位置为100, 表示NMI
|
||||
reserved_1 : 1, // 第11位保留
|
||||
delivery_status : 1, // 第12位,投递状态 -> 发送挂起
|
||||
polarity : 1, // 第13位,电平触发极性 存在于LINT0,LINT1
|
||||
remote_IRR : 1, // 第14位,远程IRR标志位(只读) 存在于LINT0,LINT1
|
||||
trigger_mode : 1, // 第15位,触发模式(0位边沿触发,1为电平触发) 存在于LINT0,LINT1
|
||||
mask : 1, // 第16位,屏蔽标志位,(0为未屏蔽, 1为已屏蔽)
|
||||
timer_mode : 2, // 第[18:17]位,定时模式。(00:一次性定时, 01:周期性定时, 10:指定TSC值计数), 存在于定时器寄存器
|
||||
reserved_2 : 13; // [31:19]位保留
|
||||
|
||||
} __attribute((packed)); // 取消结构体的align
|
||||
|
||||
/*
|
||||
ICR
|
||||
*/
|
||||
|
||||
struct INT_CMD_REG
|
||||
{
|
||||
unsigned int vector : 8, // 0~7
|
||||
deliver_mode : 3, // 8~10
|
||||
dest_mode : 1, // 11
|
||||
deliver_status : 1, // 12
|
||||
res_1 : 1, // 13
|
||||
level : 1, // 14
|
||||
trigger : 1, // 15
|
||||
res_2 : 2, // 16~17
|
||||
dest_shorthand : 2, // 18~19
|
||||
res_3 : 12; // 20~31
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
unsigned int res_4 : 24, // 32~55
|
||||
dest_field : 8; // 56~63
|
||||
} apic_destination;
|
||||
|
||||
unsigned int x2apic_destination; // 32~63
|
||||
} destination;
|
||||
|
||||
} __attribute__((packed));
|
||||
|
||||
/**
|
||||
* @brief I/O APIC 的中断定向寄存器的结构体
|
||||
*
|
||||
*/
|
||||
struct apic_IO_APIC_RTE_entry
|
||||
{
|
||||
unsigned int vector : 8, // 0~7
|
||||
deliver_mode : 3, // [10:8] 投递模式默认为NMI
|
||||
dest_mode : 1, // 11 目标模式(0位物理模式,1为逻辑模式)
|
||||
deliver_status : 1, // 12 投递状态
|
||||
polarity : 1, // 13 电平触发极性
|
||||
remote_IRR : 1, // 14 远程IRR标志位(只读)
|
||||
trigger_mode : 1, // 15 触发模式(0位边沿触发,1为电平触发)
|
||||
mask : 1, // 16 屏蔽标志位,(0为未屏蔽, 1为已屏蔽)
|
||||
reserved : 15; // [31:17]位保留
|
||||
|
||||
union
|
||||
{
|
||||
// 物理模式
|
||||
struct
|
||||
{
|
||||
unsigned int reserved1 : 24, // [55:32] 保留
|
||||
phy_dest : 4, // [59:56] APIC ID
|
||||
reserved2 : 4; // [63:60] 保留
|
||||
} physical;
|
||||
|
||||
// 逻辑模式
|
||||
struct
|
||||
{
|
||||
unsigned int reserved1 : 24, // [55:32] 保留
|
||||
logical_dest : 8; // [63:56] 自定义APIC ID
|
||||
} logical;
|
||||
} destination;
|
||||
} __attribute__((packed));
|
||||
|
||||
// ========== APIC的寄存器的参数定义 ==============
|
||||
// 投递模式
|
||||
#define LOCAL_APIC_FIXED 0
|
||||
#define IO_APIC_FIXED 0
|
||||
#define ICR_APIC_FIXED 0
|
||||
|
||||
#define IO_APIC_Lowest_Priority 1
|
||||
#define ICR_Lowest_Priority 1
|
||||
|
||||
#define LOCAL_APIC_SMI 2
|
||||
#define APIC_SMI 2
|
||||
#define ICR_SMI 2
|
||||
|
||||
#define LOCAL_APIC_NMI 4
|
||||
#define APIC_NMI 4
|
||||
#define ICR_NMI 4
|
||||
|
||||
#define LOCAL_APIC_INIT 5
|
||||
#define APIC_INIT 5
|
||||
#define ICR_INIT 5
|
||||
|
||||
#define ICR_Start_up 6
|
||||
|
||||
#define IO_APIC_ExtINT 7
|
||||
|
||||
// 时钟模式
|
||||
#define APIC_LVT_Timer_One_Shot 0
|
||||
#define APIC_LVT_Timer_Periodic 1
|
||||
#define APIC_LVT_Timer_TSC_Deadline 2
|
||||
|
||||
// 屏蔽
|
||||
#define UNMASKED 0
|
||||
#define MASKED 1
|
||||
#define APIC_LVT_INT_MASKED 0x10000UL
|
||||
|
||||
// 触发模式
|
||||
#define EDGE_TRIGGER 0 // 边沿触发
|
||||
#define Level_TRIGGER 1 // 电平触发
|
||||
|
||||
// 投递模式
|
||||
#define IDLE 0 // 挂起
|
||||
#define SEND_PENDING 1 // 发送等待
|
||||
|
||||
// destination shorthand
|
||||
#define ICR_No_Shorthand 0
|
||||
#define ICR_Self 1
|
||||
#define ICR_ALL_INCLUDE_Self 2
|
||||
#define ICR_ALL_EXCLUDE_Self 3
|
||||
|
||||
// 投递目标模式
|
||||
#define DEST_PHYSICAL 0 // 物理模式
|
||||
#define DEST_LOGIC 1 // 逻辑模式
|
||||
|
||||
// level
|
||||
#define ICR_LEVEL_DE_ASSERT 0
|
||||
#define ICR_LEVEL_ASSERT 1
|
||||
|
||||
// 远程IRR标志位, 在处理Local APIC标志位时置位,在收到处理器发来的EOI命令时复位
|
||||
#define IRR_RESET 0
|
||||
#define IRR_ACCEPT 1
|
||||
|
||||
// 电平触发极性
|
||||
#define POLARITY_HIGH 0
|
||||
#define POLARITY_LOW 1
|
||||
|
||||
struct apic_IO_APIC_map
|
||||
{
|
||||
// 间接访问寄存器的物理基地址
|
||||
uint addr_phys;
|
||||
// 索引寄存器虚拟地址
|
||||
unsigned char *virtual_index_addr;
|
||||
// 数据寄存器虚拟地址
|
||||
uint *virtual_data_addr;
|
||||
// EOI寄存器虚拟地址
|
||||
uint *virtual_EOI_addr;
|
||||
} apic_ioapic_map;
|
||||
|
||||
/**
|
||||
* @brief 中断服务程序
|
||||
*
|
||||
* @param rsp 中断栈指针
|
||||
* @param number 中断向量号
|
||||
*/
|
||||
void do_IRQ(struct pt_regs *rsp, ul number);
|
||||
|
||||
/**
|
||||
* @brief 读取RTE寄存器
|
||||
*
|
||||
* @param index 索引值
|
||||
* @return ul
|
||||
*/
|
||||
ul apic_ioapic_read_rte(unsigned char index);
|
||||
|
||||
/**
|
||||
* @brief 写入RTE寄存器
|
||||
*
|
||||
* @param index 索引值
|
||||
* @param value 要写入的值
|
||||
*/
|
||||
void apic_ioapic_write_rte(unsigned char index, ul value);
|
||||
|
||||
/**
|
||||
* @brief 初始化AP处理器的Local apic
|
||||
*
|
||||
*/
|
||||
void apic_init_ap_core_local_apic();
|
||||
|
||||
/**
|
||||
* @brief 初始化apic控制器
|
||||
*
|
||||
*/
|
||||
int apic_init();
|
||||
|
||||
/**
|
||||
* @brief 读取指定类型的 Interrupt Control Structure
|
||||
*
|
||||
* @param type ics的类型
|
||||
* @param ret_vaddr 对应的ICS的虚拟地址数组
|
||||
* @param total 返回数组的元素总个数
|
||||
* @return uint
|
||||
*/
|
||||
uint apic_get_ics(const uint type, ul ret_vaddr[], uint *total);
|
||||
|
||||
// =========== 中断控制操作接口 ============
|
||||
void apic_ioapic_enable(ul irq_num);
|
||||
void apic_ioapic_disable(ul irq_num);
|
||||
ul apic_ioapic_install(ul irq_num, void *arg);
|
||||
void apic_ioapic_uninstall(ul irq_num);
|
||||
void apic_ioapic_level_ack(ul irq_num); // ioapic电平触发 应答
|
||||
void apic_ioapic_edge_ack(ul irq_num); // ioapic边沿触发 应答
|
||||
|
||||
// void apic_local_apic_level_ack(ul irq_num);// local apic电平触发 应答
|
||||
void apic_local_apic_edge_ack(ul irq_num); // local apic边沿触发 应答
|
||||
|
||||
/**
|
||||
* @brief 构造RTE Entry结构体
|
||||
*
|
||||
* @param entry 返回的结构体
|
||||
* @param vector 中断向量
|
||||
* @param deliver_mode 投递模式
|
||||
* @param dest_mode 目标模式
|
||||
* @param deliver_status 投递状态
|
||||
* @param polarity 电平触发极性
|
||||
* @param irr 远程IRR标志位(只读)
|
||||
* @param trigger 触发模式
|
||||
* @param mask 屏蔽标志位,(0为未屏蔽, 1为已屏蔽)
|
||||
* @param dest_apicID 目标apicID
|
||||
*/
|
||||
void apic_make_rte_entry(struct apic_IO_APIC_RTE_entry *entry, uint8_t vector, uint8_t deliver_mode, uint8_t dest_mode,
|
||||
uint8_t deliver_status, uint8_t polarity, uint8_t irr, uint8_t trigger, uint8_t mask, uint8_t dest_apicID);
|
||||
|
||||
uint32_t apic_get_local_apic_id();
|
||||
void apic_write_icr(uint64_t value);
|
||||
bool apic_x2apic_enabled();
|
||||
#pragma GCC pop_options
|
@ -1,2 +0,0 @@
|
||||
#include <common/stddef.h>
|
||||
extern uint64_t ioapic_get_base_paddr();
|
@ -1,128 +0,0 @@
|
||||
#include "apic_timer.h"
|
||||
#include <common/kprint.h>
|
||||
#include <exception/irq.h>
|
||||
#include <process/process.h>
|
||||
#include <sched/sched.h>
|
||||
|
||||
// #pragma GCC push_options
|
||||
// #pragma GCC optimize("O0")
|
||||
uint64_t apic_timer_ticks_result = 0;
|
||||
static spinlock_t apic_timer_init_lock = {1};
|
||||
// bsp 是否已经完成apic时钟初始化
|
||||
static bool bsp_initialized = false;
|
||||
|
||||
extern uint64_t rs_get_cycles();
|
||||
extern uint64_t rs_tsc_get_cpu_khz();
|
||||
|
||||
/**
|
||||
* @brief 初始化AP核的apic时钟
|
||||
*
|
||||
*/
|
||||
void apic_timer_ap_core_init()
|
||||
{
|
||||
while (!bsp_initialized)
|
||||
{
|
||||
pause();
|
||||
}
|
||||
|
||||
apic_timer_init();
|
||||
}
|
||||
|
||||
void apic_timer_enable(uint64_t irq_num)
|
||||
{
|
||||
// 启动apic定时器
|
||||
io_mfence();
|
||||
uint64_t val = apic_timer_get_LVT();
|
||||
io_mfence();
|
||||
val &= (~APIC_LVT_INT_MASKED);
|
||||
io_mfence();
|
||||
apic_timer_write_LVT(val);
|
||||
io_mfence();
|
||||
}
|
||||
|
||||
void apic_timer_disable(uint64_t irq_num)
|
||||
{
|
||||
apic_timer_stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 安装local apic定时器中断
|
||||
*
|
||||
* @param irq_num 中断向量号
|
||||
* @param arg 初始计数值
|
||||
* @return uint64_t
|
||||
*/
|
||||
uint64_t apic_timer_install(ul irq_num, void *arg)
|
||||
{
|
||||
// 设置div16
|
||||
io_mfence();
|
||||
apic_timer_stop();
|
||||
io_mfence();
|
||||
apic_timer_set_div(APIC_TIMER_DIVISOR);
|
||||
io_mfence();
|
||||
|
||||
// 设置初始计数
|
||||
|
||||
uint64_t cpu_khz = rs_tsc_get_cpu_khz();
|
||||
// 疑惑:这里使用khz吗?
|
||||
// 我觉得应该是hz,但是由于旧的代码是测量出initcnt的,而不是计算的
|
||||
// 然后我发现使用hz会导致计算出来的initcnt太大,导致系统卡顿,而khz的却能跑
|
||||
// TODO: 这里需要进一步研究
|
||||
uint64_t init_cnt = cpu_khz * APIC_TIMER_INTERVAL / (1000 * APIC_TIMER_DIVISOR);
|
||||
kdebug("cpu_khz: %ld, init_cnt: %ld", cpu_khz, init_cnt);
|
||||
apic_timer_set_init_cnt(init_cnt);
|
||||
io_mfence();
|
||||
// 填写LVT
|
||||
apic_timer_set_LVT(APIC_TIMER_IRQ_NUM, 1, APIC_LVT_Timer_Periodic);
|
||||
io_mfence();
|
||||
}
|
||||
|
||||
void apic_timer_uninstall(ul irq_num)
|
||||
{
|
||||
apic_timer_write_LVT(APIC_LVT_INT_MASKED);
|
||||
io_mfence();
|
||||
}
|
||||
|
||||
hardware_intr_controller apic_timer_intr_controller = {
|
||||
.enable = apic_timer_enable,
|
||||
.disable = apic_timer_disable,
|
||||
.install = apic_timer_install,
|
||||
.uninstall = apic_timer_uninstall,
|
||||
.ack = apic_local_apic_edge_ack,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief local apic定时器的中断处理函数
|
||||
*
|
||||
* @param number 中断向量号
|
||||
* @param param 参数
|
||||
* @param regs 寄存器值
|
||||
*/
|
||||
void apic_timer_handler(uint64_t number, uint64_t param, struct pt_regs *regs)
|
||||
{
|
||||
io_mfence();
|
||||
sched_update_jiffies();
|
||||
io_mfence();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化local APIC定时器
|
||||
*
|
||||
*/
|
||||
void apic_timer_init()
|
||||
{
|
||||
|
||||
uint64_t flags = 0;
|
||||
spin_lock_irqsave(&apic_timer_init_lock, flags);
|
||||
kinfo("Initializing apic timer for cpu %d", rs_current_pcb_cpuid());
|
||||
io_mfence();
|
||||
irq_register(APIC_TIMER_IRQ_NUM, NULL, &apic_timer_handler, 0, &apic_timer_intr_controller,
|
||||
"apic timer");
|
||||
io_mfence();
|
||||
if (rs_current_pcb_cpuid() == 0)
|
||||
{
|
||||
bsp_initialized = true;
|
||||
}
|
||||
kdebug("apic timer init done for cpu %d", rs_current_pcb_cpuid());
|
||||
spin_unlock_irqrestore(&apic_timer_init_lock, flags);
|
||||
}
|
@ -1,110 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <common/unistd.h>
|
||||
#include "apic.h"
|
||||
|
||||
extern uint64_t apic_timer_ticks_result;
|
||||
// 5ms产生一次中断
|
||||
#define APIC_TIMER_INTERVAL 5
|
||||
#define APIC_TIMER_DIVISOR 3
|
||||
|
||||
#define APIC_TIMER_IRQ_NUM 151
|
||||
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize("O0")
|
||||
|
||||
/**
|
||||
* @brief 设置apic定时器的分频计数
|
||||
*
|
||||
* @param divider 分频除数
|
||||
*/
|
||||
static __always_inline void apic_timer_set_div(uint64_t divider)
|
||||
{
|
||||
if (CURRENT_APIC_STATE == APIC_X2APIC_ENABLED)
|
||||
wrmsr(0x83e, divider);
|
||||
else
|
||||
__write4b(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_CLKDIV, divider);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 设置apic定时器的初始计数值
|
||||
*
|
||||
* @param init_cnt 初始计数值
|
||||
*/
|
||||
static __always_inline void apic_timer_set_init_cnt(uint32_t init_cnt)
|
||||
{
|
||||
if (CURRENT_APIC_STATE == APIC_X2APIC_ENABLED)
|
||||
wrmsr(0x838, init_cnt);
|
||||
else
|
||||
__write4b(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_INITIAL_COUNT_REG, init_cnt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 设置apic定时器的lvt,并启动定时器
|
||||
*
|
||||
* @param vector 中断向量号
|
||||
* @param mask 是否屏蔽(1:屏蔽, 0:不屏蔽)
|
||||
* @param mode 计时模式
|
||||
*/
|
||||
static __always_inline void apic_timer_set_LVT(uint32_t vector, uint32_t mask, uint32_t mode)
|
||||
{
|
||||
register uint32_t val = (mode << 17) | vector | (mask ? (APIC_LVT_INT_MASKED) : 0);
|
||||
if (CURRENT_APIC_STATE == APIC_X2APIC_ENABLED)
|
||||
wrmsr(0x832, val);
|
||||
else
|
||||
__write4b(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_TIMER, val);
|
||||
}
|
||||
|
||||
static __always_inline void apic_timer_write_LVT(uint32_t value)
|
||||
{
|
||||
if (CURRENT_APIC_STATE == APIC_X2APIC_ENABLED)
|
||||
wrmsr(0x832, value);
|
||||
else
|
||||
__write4b(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_TIMER, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取apic定时器的LVT的值
|
||||
*
|
||||
*/
|
||||
static __always_inline uint32_t apic_timer_get_LVT()
|
||||
{
|
||||
if (CURRENT_APIC_STATE == APIC_X2APIC_ENABLED)
|
||||
return rdmsr(0x832);
|
||||
else
|
||||
return __read4b(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_TIMER);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取apic定时器当前计数值
|
||||
*
|
||||
*/
|
||||
static __always_inline uint32_t apic_timer_get_current()
|
||||
{
|
||||
if (CURRENT_APIC_STATE == APIC_X2APIC_ENABLED)
|
||||
return (uint32_t)rdmsr(0x839);
|
||||
else
|
||||
return __read4b(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_CURRENT_COUNT_REG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 停止apic定时器
|
||||
*
|
||||
*/
|
||||
#define apic_timer_stop() \
|
||||
do \
|
||||
{ \
|
||||
uint32_t val = apic_timer_get_LVT(); \
|
||||
val |= APIC_LVT_INT_MASKED; \
|
||||
apic_timer_write_LVT(val); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* @brief 初始化local APIC定时器
|
||||
*
|
||||
*/
|
||||
void apic_timer_init();
|
||||
|
||||
void apic_timer_ap_core_init();
|
||||
|
||||
#pragma GCC pop_options
|
@ -1,5 +1,5 @@
|
||||
#include "ps2_keyboard.h"
|
||||
#include <driver/interrupt/apic/apic.h>
|
||||
#include <arch/x86_64/driver/apic/apic.h>
|
||||
#include <mm/mm.h>
|
||||
#include <mm/slab.h>
|
||||
#include <common/printk.h>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "ps2_mouse.h"
|
||||
#include <driver/interrupt/apic/apic.h>
|
||||
#include <arch/x86_64/driver/apic/apic.h>
|
||||
#include <mm/mm.h>
|
||||
#include <mm/slab.h>
|
||||
#include <common/printk.h>
|
||||
@ -209,19 +209,19 @@ void ps2_mouse_init()
|
||||
|
||||
// ======== 初始化中断RTE entry ==========
|
||||
|
||||
ps2_mouse_entry.vector = PS2_MOUSE_INTR_VECTOR; // 设置中断向量号
|
||||
ps2_mouse_entry.deliver_mode = IO_APIC_FIXED; // 投递模式:混合
|
||||
ps2_mouse_entry.dest_mode = DEST_PHYSICAL; // 物理模式投递中断
|
||||
ps2_mouse_entry.deliver_status = IDLE;
|
||||
ps2_mouse_entry.trigger_mode = EDGE_TRIGGER; // 设置边沿触发
|
||||
ps2_mouse_entry.polarity = POLARITY_HIGH; // 高电平触发
|
||||
ps2_mouse_entry.remote_IRR = IRR_RESET;
|
||||
ps2_mouse_entry.mask = MASKED;
|
||||
ps2_mouse_entry.reserved = 0;
|
||||
// ps2_mouse_entry.vector = PS2_MOUSE_INTR_VECTOR; // 设置中断向量号
|
||||
// ps2_mouse_entry.deliver_mode = IO_APIC_FIXED; // 投递模式:混合
|
||||
// ps2_mouse_entry.dest_mode = DEST_PHYSICAL; // 物理模式投递中断
|
||||
// ps2_mouse_entry.deliver_status = IDLE;
|
||||
// ps2_mouse_entry.trigger_mode = EDGE_TRIGGER; // 设置边沿触发
|
||||
// ps2_mouse_entry.polarity = POLARITY_HIGH; // 高电平触发
|
||||
// ps2_mouse_entry.remote_IRR = IRR_RESET;
|
||||
// ps2_mouse_entry.mask = MASKED;
|
||||
// ps2_mouse_entry.reserved = 0;
|
||||
|
||||
ps2_mouse_entry.destination.physical.reserved1 = 0;
|
||||
ps2_mouse_entry.destination.physical.reserved2 = 0;
|
||||
ps2_mouse_entry.destination.physical.phy_dest = 0; // 设置投递到BSP处理器
|
||||
// ps2_mouse_entry.destination.physical.reserved1 = 0;
|
||||
// ps2_mouse_entry.destination.physical.reserved2 = 0;
|
||||
// ps2_mouse_entry.destination.physical.phy_dest = 0; // 设置投递到BSP处理器
|
||||
|
||||
// 注册中断处理程序
|
||||
irq_register(PS2_MOUSE_INTR_VECTOR, &ps2_mouse_entry, &ps2_mouse_handler, (ul)ps2_mouse_buf_ptr, &ps2_mouse_intr_controller, "ps/2 mouse");
|
||||
|
Reference in New Issue
Block a user