测试IPI

This commit is contained in:
fslongjin
2022-04-07 20:21:28 +08:00
parent ac2508224e
commit 279de4c7f9
14 changed files with 265 additions and 81 deletions

18
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"x86_64"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

View File

@ -85,7 +85,8 @@
"thread": "c",
"cinttypes": "c",
"cstdbool": "c",
"typeinfo": "c"
"typeinfo": "c",
"x86_64_ipi.h": "c"
},
"C_Cpp.errorSquiggles": "Enabled",
"esbonio.sphinx.confDir": ""

View File

@ -21,7 +21,7 @@ ata3: enabled=0
pci: enabled=1, chipset=i440fx
vga: extension=vbe, update_freq=5
cpu: count=1:2:2, ips=4000000, quantum=16, model=corei7_haswell_4770, reset_on_triple_fault=1, cpuid_limit_winnt=0, ignore_bad_msrs=1, mwait_is_nop=0, msrs="msrs.def"
cpu: count=1:2:1, ips=4000000, quantum=16, model=corei7_haswell_4770, reset_on_triple_fault=1, cpuid_limit_winnt=0, ignore_bad_msrs=1, mwait_is_nop=0, msrs="msrs.def"
cpuid: x86_64=1,level=6, mmx=1, sep=1, simd=avx512, aes=1, movbe=1, xsave=1,apic=x2apic,sha=1,movbe=1,adx=1,xsaveopt=1,avx_f16c=1,avx_fma=1,bmi=bmi2,1g_pages=1,pcid=1,fsgsbase=1,smep=1,smap=1,mwait=1,vmx=1
cpuid: family=6, model=0x1a, stepping=5, vendor_string="GenuineIntel", brand_string="Intel(R) Core(TM) i7-4770 CPU (Haswell)"

View File

@ -7,23 +7,18 @@ DIR_LIB=lib
lib_patterns := *.a
LIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns)))
CFLAGS := -mcmodel=large -fno-builtin -m64 -g -O0
CFLAGS := -mcmodel=large -fno-builtin -m64 -g -O0 -I .
ARCH=x86_64
# 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_
PIC := _INTR_APIC_
CFLAGS += -D $(PIC)
CFLAGS += -D $(PIC) -D $(ARCH)
ASFLAGS := --64
all: kernel
objcopy -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../bin/kernel/kernel.elf
#
LD_LIST := head.o
OBJ_LIST := head.o
kernel: head.o entry.o main.o printk.o trap.o mm.o slab.o irq.o pic.o process.o syscall.o multiboot2.o cpu.o acpi.o ps2_keyboard.o ps2_mouse.o ata.o pci.o ahci.o smp.o apu_boot.o
ld -b elf64-x86-64 -z muldefs -o kernel head.o exception/entry.o main.o common/printk.o exception/trap.o exception/irq.o mm/mm.o mm/slab.o process/process.o syscall/syscall.o driver/multiboot2/multiboot2.o \
common/cpu.o smp/smp.o smp/apu_boot.o \
driver/acpi/acpi.o driver/interrupt/pic.o driver/keyboard/ps2_keyboard.o driver/mouse/ps2_mouse.o driver/disk/ata.o driver/pci/pci.o driver/disk/ahci/ahci.o \
-T link.lds
head.o: head.S
gcc -E head.S > head.s # 预处理
@ -74,6 +69,15 @@ apu_boot.o: smp/apu_boot.S
cpu.o: common/cpu.c
gcc $(CFLAGS) -c common/cpu.c -o common/cpu.o
# IPI的代码
ifeq ($(ARCH), x86_64)
OBJ_LIST += ipi.o
LD_LIST += arch/x86_64/x86_64_ipi.o
ipi.o: arch/x86_64/x86_64_ipi.c
gcc $(CFLAGS) -c arch/x86_64/x86_64_ipi.c -o arch/x86_64/x86_64_ipi.o
endif
# 驱动程序
# 中断处理芯片的驱动程序
ifeq ($(PIC), _INTR_8259A_)
@ -106,5 +110,18 @@ ahci.o: driver/disk/ahci/ahci.c
gcc $(CFLAGS) -c driver/disk/ahci/ahci.c -o driver/disk/ahci/ahci.o
all: kernel
objcopy -I elf64-x86-64 -O elf64-x86-64 -R ".comment" -R ".eh_frame" kernel ../bin/kernel/kernel.elf
#
kernel: head.o entry.o main.o printk.o trap.o mm.o slab.o irq.o pic.o process.o syscall.o multiboot2.o cpu.o acpi.o ps2_keyboard.o ps2_mouse.o ata.o pci.o ahci.o smp.o apu_boot.o $(OBJ_LIST)
ld -b elf64-x86-64 -z muldefs -o kernel head.o exception/entry.o main.o common/printk.o exception/trap.o exception/irq.o mm/mm.o mm/slab.o process/process.o syscall/syscall.o driver/multiboot2/multiboot2.o \
common/cpu.o smp/smp.o smp/apu_boot.o \
driver/acpi/acpi.o driver/interrupt/pic.o driver/keyboard/ps2_keyboard.o driver/mouse/ps2_mouse.o driver/disk/ata.o driver/pci/pci.o driver/disk/ahci/ahci.o \
$(LD_LIST) \
-T link.lds
clean:
rm -rf $(GARBAGE)

View File

@ -0,0 +1,37 @@
#include "x86_64_ipi.h"
void ipi_send_IPI(uint32_t dest_mode, uint32_t deliver_status, uint32_t level, uint32_t trigger,
uint32_t vector, uint32_t deliver_mode, uint32_t dest_shorthand, bool apic_type, uint32_t destination)
{
struct INT_CMD_REG icr_entry;
icr_entry.dest_mode = dest_mode;
icr_entry.deliver_status = deliver_status;
icr_entry.res_1 = 0;
icr_entry.level = level;
icr_entry.trigger = trigger;
icr_entry.res_2 = 0;
icr_entry.res_3 = 0;
icr_entry.vector = vector;
icr_entry.deliver_mode = deliver_mode;
icr_entry.dest_shorthand = dest_shorthand;
// x2APIC下ICR寄存器地址为0x830
// xAPIC下则为0xfee00300(31-0) 0xfee00310 (63-32)
if (apic_type) // x2APIC
{
icr_entry.destination.x2apic_destination = destination;
wrmsr(0x830, *(unsigned long *)&icr_entry); // 发送ipi
}
else // xAPIC
{
icr_entry.destination.apic_destination.dest_field = destination & 0xff;
icr_entry.destination.apic_destination.res_4 = 0;
// 先向高32bit写数据然后再向低32bit写数据不能调转
*(uint32_t*)(APIC_LOCAL_APIC_VIRT_BASE_ADDR+0x310) = (uint32_t)(((*(ul*)&icr_entry)>>32)&0xffff);
*(uint32_t*)(APIC_LOCAL_APIC_VIRT_BASE_ADDR+0x300) = (uint32_t)((*(ul*)&icr_entry)&0xffff);
}
}

View File

@ -0,0 +1,31 @@
/**
* @file ipi.h
* @author fslongjin(longjin@RinGoTek.cn)
* @brief 多核通信驱动
* @version 0.1
* @date 2022-04-07
*
* @copyright Copyright (c) 2022
*
*/
#pragma once
#include "../../common/kprint.h"
#include "../../driver/interrupt/apic/apic.h"
/**
* @brief 发送ipi消息
*
* @param dest_mode 目标模式
* @param deliver_status 投递模式
* @param level 信号驱动电平
* @param trigger 触发模式
* @param vector 中断向量
* @param deliver_mode 投递模式
* @param dest_shorthand 投递目标速记值
* @param apic_type apic的类型 0:xapic 1: x2apic
* @param destination 投递目标
*/
void ipi_send_IPI(uint32_t dest_mode, uint32_t deliver_status, uint32_t level, uint32_t trigger,
uint32_t vector, uint32_t deliver_mode, uint32_t dest_shorthand, bool apic_type,uint32_t destination);

View File

@ -389,7 +389,9 @@ void apic_local_apic_init()
*/
void apic_init()
{
// 初始化中断门, 中断使用第二个ist
for (int i = 32; i <= 55; ++i)
set_intr_gate(i, 2, interrupt_table[i - 32]);
// 初始化主芯片
io_out8(0x20, 0x11); // 初始化主芯片的icw1
io_out8(0x21, 0x20); // 设置主芯片的中断向量号为0x20(0x20-0x27)
@ -401,17 +403,15 @@ void apic_init()
io_out8(0xa1, 0x28); // 设置从芯片的中断向量号为0x28(0x28-0x2f)
io_out8(0xa1, 0x02); // 设置从芯片连接到主芯片的int2
io_out8(0xa1, 0x01);
// 屏蔽类8259A芯片
io_mfence();
io_out8(0xa1, 0xff);
io_mfence();
io_out8(0x21, 0xff);
io_mfence();
io_out8(0xa1, 0xff);
io_mfence();
kdebug("8259A Masked.");
// 初始化中断门, 中断使用第二个ist
for (int i = 32; i <= 55; ++i)
set_intr_gate(i, 2, interrupt_table[i - 32]);
// enable IMCR
io_out8(0x22, 0x70);
@ -420,10 +420,8 @@ void apic_init()
apic_local_apic_init();
apic_io_apic_init();
kdebug("vvvvv");
sti();
kdebug("qqqqq");
}
/**
* @brief 中断服务程序
@ -433,8 +431,11 @@ void apic_init()
*/
void do_IRQ(struct pt_regs *rsp, ul number)
{
unsigned char x = io_in8(0x60);
switch (number & 0x80) // 以0x80为界限低于0x80的是外部中断控制器高于0x80的是Local APIC
{
case 0x00: // 外部中断控制器
/* code */
{
irq_desc_t *irq = &interrupt_desc[number - 32];
// 执行中断上半部处理程序
@ -456,6 +457,15 @@ void do_IRQ(struct pt_regs *rsp, ul number)
"wrmsr \n\t" ::
: "memory");
}
}
break;
case 0x80:
printk_color(RED, BLACK, "SMP IPI [ %d ]\n", number);
apic_local_apic_edge_ack(number);
default:
kwarn("do IRQ receive: %d", number);
break;
}
}
/**
@ -571,6 +581,21 @@ void apic_ioapic_edge_ack(ul irq_num) // 边沿触发
: "memory");
}
/**
* @brief local apic 边沿触发应答
*
* @param irq_num
*/
void apic_local_apic_edge_ack(ul irq_num)
{
// 向EOI寄存器写入0x00表示结束中断
__asm__ __volatile__("movq $0x00, %%rdx \n\t"
"movq $0x00, %%rax \n\t"
"movq $0x80b, %%rcx \n\t"
"wrmsr \n\t" ::
: "memory");
}
/**
* @brief 读取指定类型的 Interrupt Control Structure
*

View File

@ -297,5 +297,8 @@ 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); // 电平触发
void apic_ioapic_edge_ack(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边沿触发 应答

View File

@ -112,6 +112,38 @@ void (*interrupt_table[24])(void) =
IRQ0x37interrupt,
};
/**
* @brief 声明10个IPI消息处理程序向量号从200(0xc8)开始
*
*/
Build_IRQ(0xc8)
Build_IRQ(0xc9)
Build_IRQ(0xca)
Build_IRQ(0xcb)
Build_IRQ(0xcc)
Build_IRQ(0xcd)
Build_IRQ(0xce)
Build_IRQ(0xcf)
Build_IRQ(0xd0)
Build_IRQ(0xd1)
// 初始化IPI中断服务程序数组
void (*SMP_interrupt_table[SMP_IRQ_NUM])(void) =
{
IRQ0xc8interrupt,
IRQ0xc9interrupt,
IRQ0xcainterrupt,
IRQ0xcbinterrupt,
IRQ0xccinterrupt,
IRQ0xcdinterrupt,
IRQ0xceinterrupt,
IRQ0xcfinterrupt,
IRQ0xd0interrupt,
IRQ0xd1interrupt,
};
/**
* @brief 中断注册函数
*

View File

@ -15,9 +15,13 @@
#include "../process/ptrace.h"
#define SMP_IRQ_NUM 10
extern void (*interrupt_table[24])(void);
extern void do_IRQ(struct pt_regs *regs, ul number);
extern void (*SMP_interrupt_table[SMP_IRQ_NUM])(void);
/* ========= 中断向量分配表 ==========
0~255 IDT
@ -121,6 +125,9 @@ typedef struct
#define IRQ_NUM 24
irq_desc_t interrupt_desc[IRQ_NUM] = {0};
irq_desc_t SMP_IPI_desc[SMP_IRQ_NUM];
/**
* @brief 中断注册函数
*

View File

@ -13,6 +13,7 @@
#include "process/process.h"
#include "syscall/syscall.h"
#include "smp/smp.h"
#include <smp/ipi.h>
#include "driver/multiboot2/multiboot2.h"
#include "driver/acpi/acpi.h"
@ -224,6 +225,9 @@ void Start_Kernel(void)
analyze_mousecode();
}
*/
ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0xc8, ICR_APIC_FIXED, ICR_No_Shorthand, true, 1); // 测试ipi
while (1)
hlt();
}

24
kernel/smp/ipi.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#ifdef x86_64
#include <arch/x86_64/x86_64_ipi.h>
#else
#error "error type of arch!"
#endif
/**
* @brief 发送ipi消息
*
* @param dest_mode 目标模式
* @param deliver_status 投递模式
* @param level 信号驱动电平
* @param trigger 触发模式
* @param vector 中断向量
* @param deliver_mode 投递模式
* @param dest_shorthand 投递目标速记值
* @param apic_type apic的类型 0:xapic 1: x2apic
* @param destination 投递目标
*/
extern void ipi_send_IPI(uint32_t dest_mode, uint32_t deliver_status, uint32_t level, uint32_t trigger,
uint32_t vector, uint32_t deliver_mode, uint32_t dest_shorthand, bool apic_type, uint32_t destination);

View File

@ -7,17 +7,17 @@
#include "../process/process.h"
#include "../process/spinlock.h"
#include "ipi.h"
static spinlock_t multi_core_starting_lock; // 多核启动锁
static struct acpi_Processor_Local_APIC_Structure_t *proc_local_apic_structs[MAX_SUPPORTED_PROCESSOR_NUM];
static uint32_t total_processor_num = 0;
int current_starting_cpu = 0;
void smp_init()
{
spin_init(&multi_core_starting_lock);
spin_init(&multi_core_starting_lock); // 初始化多核启动锁
ul tmp_vaddr[MAX_SUPPORTED_PROCESSOR_NUM] = {0};
@ -31,22 +31,12 @@ void smp_init()
// 将引导程序复制到物理地址0x20000处
memcpy((unsigned char *)0x20000, _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start);
// wrmsr(0x830, 0xc4500); // init IPI
struct INT_CMD_REG icr_entry;
icr_entry.dest_mode = DEST_PHYSICAL;
icr_entry.deliver_status = IDLE;
icr_entry.res_1 = 0;
icr_entry.level = ICR_LEVEL_DE_ASSERT;
icr_entry.trigger = EDGE_TRIGGER;
icr_entry.res_2 = 0;
icr_entry.res_3 = 0;
// 设置多核IPI中断门
for (int i = 200; i < 210; ++i)
set_intr_gate(i, 2, SMP_interrupt_table[i - 200]);
memset(SMP_IPI_desc, 0, sizeof(irq_desc_t) * SMP_IRQ_NUM);
icr_entry.vector = 0x00;
icr_entry.deliver_mode = ICR_INIT;
icr_entry.dest_shorthand = ICR_ALL_EXCLUDE_Self;
icr_entry.destination.x2apic_destination = 0x00;
wrmsr(0x830, *(unsigned long *)&icr_entry); // INIT IPI
ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x00, ICR_INIT, ICR_ALL_EXCLUDE_Self, true, 0x00);
for (int i = 1; i < total_processor_num; ++i) // i从1开始不初始化bsp
{
@ -67,18 +57,9 @@ void smp_init()
kdebug("GDT Table %#018lx, \t %#018lx", GDT_Table[10 + i * 2], GDT_Table[10 + i * 2 + 1]);
kdebug("(cpu_core_info[i].tss_vaddr)=%#018lx", (cpu_core_info[i].tss_vaddr));
kdebug("(cpu_core_info[i].stack_start)=%#018lx", (cpu_core_info[i].stack_start));
icr_entry.vector = 0x20;
icr_entry.deliver_mode = ICR_Start_up;
icr_entry.dest_shorthand = ICR_No_Shorthand;
icr_entry.destination.x2apic_destination = proc_local_apic_structs[i]->ACPI_ID;
// 先init ipi 然后连续发送两次start-up IPI
// x2APIC下ICR寄存器地址为0x830
// xAPIC下则为0xfee00300(31-0) 0xfee00310 (63-32)
wrmsr(0x830, *(ul *)&icr_entry); // start-up IPI
wrmsr(0x830, *(ul *)&icr_entry); // start-up IPI
//for(int k=0;i<1e5;++k);
// 连续发送两次start-up IPI
ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->ACPI_ID);
ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->ACPI_ID);
}
}
@ -89,7 +70,7 @@ void smp_init()
void smp_ap_start()
{
// 切换栈基地址
//uint64_t stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
// uint64_t stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
__asm__ __volatile__("movq %0, %%rbp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
: "memory");
__asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
@ -107,8 +88,11 @@ void smp_ap_start()
spin_unlock(&multi_core_starting_lock);
sti();
kdebug("IDT_addr = %#018lx", &IDT_Table);
//sti();
// sti();
// int a = 1 / 0; // 在这儿会出现异常cs fs gs ss寄存器会被改变
kdebug("IDT_addr = %#018lx", &IDT_Table);
while (1) // 这里要循环hlt原因是当收到中断后核心会被唤醒处理完中断之后不会自动hlt
hlt();
}

View File

@ -1,3 +1,4 @@
#pragma once
#include "../common/glib.h"
#include "../common/asm.h"