From eff673edcb1f52deb48037c23e1f776a1ec7c200 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Sat, 29 Jan 2022 12:52:25 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20=E6=96=B0=E5=A2=9E=E4=BA=86=E4=BD=BF?= =?UTF-8?q?=E7=94=A88259A=E8=8A=AF=E7=89=87=E7=9A=84=E4=B8=AD=E6=96=AD?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/Makefile | 12 +++- kernel/common/asm.h | 15 ++++- kernel/common/glib.h | 50 ++++++++++++++- kernel/exception/8259A.c | 35 ++++++++++ kernel/exception/8259A.h | 25 ++++++++ kernel/exception/irq.c | 134 +++++++++++++++++++++++++++++++++++++++ kernel/exception/irq.h | 29 +++++++++ kernel/main.c | 9 ++- 8 files changed, 302 insertions(+), 7 deletions(-) create mode 100644 kernel/exception/8259A.c create mode 100644 kernel/exception/8259A.h create mode 100644 kernel/exception/irq.c create mode 100644 kernel/exception/irq.h diff --git a/kernel/Makefile b/kernel/Makefile index cd720750..1f376bf5 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -10,8 +10,8 @@ all: kernel objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary kernel ../bin/kernel/kernel.bin -kernel: head.o entry.o main.o printk.o trap.o mm.o - ld -b elf64-x86-64 -z muldefs -o kernel head.o exception/entry.o main.o common/printk.o exception/trap.o mm/mm.o -T link.lds +kernel: head.o entry.o main.o printk.o trap.o mm.o irq.o 8259A.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 exception/8259A.o mm/mm.o -T link.lds head.o: head.S gcc -E head.S > head.s # 预处理 @@ -21,6 +21,8 @@ entry.o: exception/entry.S gcc -E exception/entry.S > exception/entry.s as --64 -o exception/entry.o exception/entry.s + + main.o: main.c # -fno-builtin: 不使用C语言内建函数 # The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture. @@ -33,6 +35,12 @@ printk.o: common/printk.c trap.o: exception/trap.c gcc -mcmodel=large -fno-builtin -m64 -c exception/trap.c -o exception/trap.o +irq.o: exception/irq.c + gcc -mcmodel=large -fno-builtin -m64 -c exception/irq.c -o exception/irq.o + +8259A.o: exception/8259A.c + gcc -mcmodel=large -fno-builtin -m64 -c exception/8259A.c -o exception/8259A.o + mm.o: mm/mm.c gcc -mcmodel=large -fno-builtin -m64 -c mm/mm.c -o mm/mm.o diff --git a/kernel/common/asm.h b/kernel/common/asm.h index 3d7ecaf6..a1a07b3c 100644 --- a/kernel/common/asm.h +++ b/kernel/common/asm.h @@ -9,4 +9,17 @@ name: -#endif \ No newline at end of file +#endif + +// 符号名 +#define SYMBOL_NAME(X) X +// 符号名字符串 +#define SYMBOL_NAME_STR(X) #X +// 符号名label +#define SYMBOL_NAME_LABEL(X) X##: + +#define L1_CACHE_BYTES 32 + +#define asmlinkage __attribute__((regparm(0))) + +#define ____cacheline_aligned __attribute__((__aligned__(L1_CACHE_BYTES))) \ No newline at end of file diff --git a/kernel/common/glib.h b/kernel/common/glib.h index 66d35f58..dcfb5bcb 100644 --- a/kernel/common/glib.h +++ b/kernel/common/glib.h @@ -18,11 +18,11 @@ //内存屏障 #define io_mfence() __asm__ __volatile__("mfence\n\t" :: \ - : "memory") // 在mfence指令前的读写操作当必须在mfence指令后的读写操作前完成。 + : "memory") // 在mfence指令前的读写操作必须在mfence指令后的读写操作前完成。 #define io_sfence() __asm__ __volatile__("sfence\n\t" :: \ - : "memory") // 在sfence指令前的写操作当必须在sfence指令后的写操作前完成 + : "memory") // 在sfence指令前的写操作必须在sfence指令后的写操作前完成 #define io_lfence() __asm__ __volatile__("lfence\n\t" :: \ - : "memory") // 在lfence指令前的读操作当必须在lfence指令后的读操作前完成。 + : "memory") // 在lfence指令前的读操作必须在lfence指令后的读操作前完成。 // 定义类型的缩写 typedef unsigned long ul; @@ -140,4 +140,48 @@ void *memset_c(void *dst, unsigned char c, ul n) for (int i = 0; i < n; ++i) s[i] = c; return dst; +} + +// 从io口读入8个bit +unsigned char io_in8(unsigned short port) +{ + unsigned char ret = 0; + __asm__ __volatile__( "inb %%dx, %0 \n\t" + "mfence \n\t" + :"=a"(ret) + :"d"(port) + :"memory"); + return ret; +} + +// 从io口读入32个bit +unsigned int io_in32(unsigned short port) +{ + unsigned int ret = 0; + __asm__ __volatile__( "inl %%dx, %0 \n\t" + "mfence \n\t" + :"=a"(ret) + :"d"(port) + :"memory"); + return ret; +} + +// 输出8个bit到输出端口 +void io_out8(unsigned short port,unsigned char value) +{ + __asm__ __volatile__( "outb %0, %%dx \n\t" + "mfence \n\t" + : + :"a"(value),"d"(port) + :"memory"); +} + +// 输出32个bit到输出端口 +void io_out32(unsigned short port,unsigned int value) +{ + __asm__ __volatile__( "outl %0, %%dx \n\t" + "mfence \n\t" + : + :"a"(value),"d"(port) + :"memory"); } \ No newline at end of file diff --git a/kernel/exception/8259A.c b/kernel/exception/8259A.c new file mode 100644 index 00000000..eb613dbd --- /dev/null +++ b/kernel/exception/8259A.c @@ -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."); + +} + diff --git a/kernel/exception/8259A.h b/kernel/exception/8259A.h new file mode 100644 index 00000000..47e7d9e2 --- /dev/null +++ b/kernel/exception/8259A.h @@ -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(); + + + + diff --git a/kernel/exception/irq.c b/kernel/exception/irq.c new file mode 100644 index 00000000..1ad7c7ef --- /dev/null +++ b/kernel/exception/irq.c @@ -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); +} \ No newline at end of file diff --git a/kernel/exception/irq.h b/kernel/exception/irq.h new file mode 100644 index 00000000..ae05ac83 --- /dev/null +++ b/kernel/exception/irq.h @@ -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); diff --git a/kernel/main.c b/kernel/main.c index 473c1718..a79475c7 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -6,6 +6,7 @@ #include "common/printk.h" #include "exception/gate.h" #include "exception/trap.h" +#include "exception/irq.h" #include "mm/mm.h" #include "common/kprint.h" @@ -69,6 +70,7 @@ void test_mm() //printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *memory_management_struct.bmp, *(memory_management_struct.bmp + 1)); kinfo("Try to allocate 64 memory pages."); struct Page *page = alloc_pages(ZONE_NORMAL, 64, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL); + /* for (int i = 0; i <= 65; ++i) { printk("page%d\tattr:%#018lx\tphys_addr:%#018lx\t", i, page->attr, page->addr_phys); @@ -76,6 +78,7 @@ void test_mm() if (((i + 1) % 2) == 0) printk("\n"); } + */ //printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *(memory_management_struct.bmp), *(memory_management_struct.bmp + 1)); } @@ -97,8 +100,12 @@ void init() //asm volatile(" fxsave %0 " ::"m"(fxsave_region)); // 初始化内存管理单元 - mm_init(); + + // 初始化中断模块 + init_irq(); + + } //操作系统内核从这里开始执行 void Start_Kernel(void)