diff --git a/kernel/Makefile b/kernel/Makefile index b5ff5be5..cea48eb2 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -20,10 +20,10 @@ all: kernel # cp 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 +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 keyboard.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 \ - driver/acpi/acpi.o driver/interrupt/pic.o \ + driver/acpi/acpi.o driver/interrupt/pic.o driver/keyboard/keyboard.o \ -T link.lds head.o: head.S @@ -86,5 +86,8 @@ multiboot2.o: driver/multiboot2/multiboot2.c acpi.o: driver/acpi/acpi.c gcc $(CFLAGS) -c driver/acpi/acpi.c -o driver/acpi/acpi.o +keyboard.o: driver/keyboard/keyboard.c + gcc $(CFLAGS) -c driver/keyboard/keyboard.c -o driver/keyboard/keyboard.o + clean: rm -rf $(GARBAGE) \ No newline at end of file diff --git a/kernel/driver/interrupt/apic/apic.c b/kernel/driver/interrupt/apic/apic.c index 9ff50e12..7958fa80 100644 --- a/kernel/driver/interrupt/apic/apic.c +++ b/kernel/driver/interrupt/apic/apic.c @@ -291,13 +291,14 @@ void do_IRQ(struct pt_regs *rsp, ul number) { unsigned char x = io_in8(0x60); - printk_color(BLUE, WHITE, "(IRQ:%#04x)\tkey code:%#04x\n", number, x); irq_desc_t *irq = &interrupt_desc[number - 32]; // 执行中断上半部处理程序 if (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) @@ -316,7 +317,7 @@ void do_IRQ(struct pt_regs *rsp, ul number) * @param index 索引值 * @return ul */ -ul apic_read_ioapic_rte(unsigned char index) +ul apic_ioapic_read_rte(unsigned char index) { // 由于处理器的乱序执行的问题,需要加入内存屏障以保证结果的正确性。 ul ret; diff --git a/kernel/driver/interrupt/apic/apic.h b/kernel/driver/interrupt/apic/apic.h index 01d7bda8..b05368b5 100644 --- a/kernel/driver/interrupt/apic/apic.h +++ b/kernel/driver/interrupt/apic/apic.h @@ -212,7 +212,7 @@ struct apic_IO_APIC_RTE_entry #define ICR_ALL_INCLUDE_Self 2 #define ICR_ALL_EXCLUDE_Self 3 -// 目标模式 +// 投递目标模式 #define DEST_PHYSICAL 0 // 物理模式 #define DEST_LOGIC 1 // 逻辑模式 @@ -220,7 +220,7 @@ struct apic_IO_APIC_RTE_entry #define ICR_LEVEL_DE_ASSERT 0 #define ICR_LEVEL_ASSERT 1 -// 远程IRR +// 远程IRR标志位, 在处理Local APIC标志位时置位,在收到处理器发来的EOI命令时复位 #define IRR_RESET 0 #define IRR_ACCEPT 1 diff --git a/kernel/driver/keyboard/keyboard.c b/kernel/driver/keyboard/keyboard.c new file mode 100644 index 00000000..8c9d5828 --- /dev/null +++ b/kernel/driver/keyboard/keyboard.c @@ -0,0 +1,111 @@ +#include "keyboard.h" +#include "../interrupt/apic/apic.h" +#include "../../mm/mm.h" +#include "../../mm/slab.h" +#include "../../common/printk.h" + +static struct keyboard_input_buffer *kb_buf_ptr = NULL; +static int shift_l, shift_r, ctrl_l, ctrl_r, alt_l, alt_r; +struct apic_IO_APIC_RTE_entry entry; + +hardware_intr_controller keyboard_intr_controller = + { + .enable = apic_ioapic_enable, + .disable = apic_ioapic_disable, + .install = apic_ioapic_install, + .uninstall = apic_ioapic_uninstall, + .ack = apic_ioapic_edge_ack, + +}; + +/** + * @brief 键盘中断处理函数(中断上半部) + * 将数据存入缓冲区 + * @param irq_num 中断向量号 + * @param param 参数 + * @param regs 寄存器信息 + */ +void keyboard_handler(ul irq_num, ul param, struct pt_regs *regs) +{ + // 读取键盘输入的信息 + unsigned x = io_in8(0x60); + printk_color(ORANGE, BLACK, "key_pressed:%02x\n", x); + + // 当头指针越过界时,恢复指向数组头部 + if (kb_buf_ptr->ptr_head == kb_buf_ptr + keyboard_buffer_size) + kb_buf_ptr->ptr_head = kb_buf_ptr->buffer; + + if (kb_buf_ptr->count >= keyboard_buffer_size) + { + kwarn("Keyboard input buffer is full."); + return; + } + + *kb_buf_ptr->ptr_head = x; + ++(kb_buf_ptr->count); + ++(kb_buf_ptr->ptr_head); +} +/** + * @brief 初始化键盘驱动程序的函数 + * + */ +void keyboard_init() +{ + // ======= 初始化键盘循环队列缓冲区 =========== + + // 申请键盘循环队列缓冲区的内存 + kb_buf_ptr = (struct keyboard_input_buffer *)kmalloc(sizeof(struct keyboard_input_buffer), 0); + + kb_buf_ptr->ptr_head = kb_buf_ptr; + kb_buf_ptr->ptr_tail = kb_buf_ptr; + kb_buf_ptr->count = 0; + + memset(kb_buf_ptr->buffer, 0, keyboard_buffer_size); + + // ======== 初始化中断RTE entry ========== + + entry.vector = 0x21; // 设置中断向量号 + entry.deliver_mode = IO_APIC_FIXED; // 投递模式:混合 + entry.dest_mode = DEST_PHYSICAL; // 物理模式投递中断 + entry.deliver_status = IDLE; + entry.trigger_mode = EDGE_TRIGGER; // 设置边沿触发 + entry.polarity = POLARITY_HIGH; // 高电平触发 + entry.remote_IRR = IRR_RESET; + entry.mask = MASKED; + entry.reserved = 0; + + entry.destination.physical.reserved1 = 0; + entry.destination.physical.reserved2 = 0; + entry.destination.physical.phy_dest = 0; // 设置投递到BSP处理器 + + // ======== 初始化键盘控制器,写入配置值 ========= + wait_keyboard_write(); + io_out8(PORT_KEYBOARD_CONTROL, KEYBOARD_COMMAND_WRITE); + wait_keyboard_write(); + io_out8(PORT_KEYBOARD_DATA, KEYBOARD_PARAM_INIT); + wait_keyboard_write(); + + // 执行一百万次nop,等待键盘控制器把命令执行完毕 + for (int i = 0; i < 1000; ++i) + for (int j = 0; j < 1000; ++j) + nop(); + shift_l = 0; + shift_r = 0; + ctrl_l = 0; + ctrl_r = 0; + alt_l = 0; + alt_r = 0; + + // 注册中断处理程序 + irq_register(0x21, &entry, &keyboard_handler, (ul)kb_buf_ptr, &keyboard_intr_controller, "ps/2 keyboard"); +} + +/** + * @brief 键盘驱动卸载函数 + * + */ +void keyboard_exit() +{ + irq_unregister(0x21); + kfree((ul *)kb_buf_ptr); +} diff --git a/kernel/driver/keyboard/keyboard.h b/kernel/driver/keyboard/keyboard.h new file mode 100644 index 00000000..5f0deaca --- /dev/null +++ b/kernel/driver/keyboard/keyboard.h @@ -0,0 +1,47 @@ +#pragma once + +#include "../../common/glib.h" + +// 定义键盘循环队列缓冲区大小为100bytes +#define keyboard_buffer_size 100 + +/** + * @brief 键盘循环队列缓冲区结构体 + * + */ +struct keyboard_input_buffer +{ + unsigned char *ptr_head; + unsigned char *ptr_tail; + int count; + unsigned char buffer[keyboard_buffer_size]; +}; + +#define PORT_KEYBOARD_DATA 0x60 +#define PORT_KEYBOARD_STATUS 0x64 +#define PORT_KEYBOARD_CONTROL 0x64 + +#define KEYBOARD_COMMAND_WRITE 0x60 // 向键盘发送配置命令 +#define KEYBOARD_COMMAND_READ 0x20 // 读取键盘的配置值 +#define KEYBOARD_PARAM_INIT 0x47 // 初始化键盘控制器的配置值 + +// ========= 检测键盘输入/输出缓冲区是否已满 +#define KEYBOARD_FLAG_OUTBUF_FULL 0x01 // 键盘的输出缓冲区已满标志位 +#define KEYBOARD_FLAG_INBUF_FULL 0x02 // 键盘的输入缓冲区已满标志位 + +// 等待向键盘控制器写入信息完成 +#define wait_keyboard_write() while (io_in8(PORT_KEYBOARD_STATUS) & KEYBOARD_FLAG_INBUF_FULL) +// 等待从键盘控制器读取信息完成 +#define wait_keyboard_read() while (io_in8(PORT_KEYBOARD_STATUS) & KEYBOARD_FLAG_OUTBUF_FULL) + +/** + * @brief 初始化键盘驱动程序的函数 + * + */ +void keyboard_init(); + +/** + * @brief 键盘驱动卸载函数 + * + */ +void keyboard_exit(); \ No newline at end of file diff --git a/kernel/exception/irq.c b/kernel/exception/irq.c index af5dead9..003c981a 100644 --- a/kernel/exception/irq.c +++ b/kernel/exception/irq.c @@ -122,7 +122,7 @@ void (*interrupt_table[24])(void) = * @param irq_name 中断名 * @return int */ -int irq_register(ul irq_num, void *arg, void (*handler)(ul irq_num, ul parameter, struct pt_regs *regs), ul paramater, hardware_int_controller *controller, char *irq_name) +int irq_register(ul irq_num, void *arg, void (*handler)(ul irq_num, ul parameter, struct pt_regs *regs), ul paramater, hardware_intr_controller *controller, char *irq_name) { // 由于为I/O APIC分配的中断向量号是从32开始的,因此要减去32才是对应的interrupt_desc的元素 irq_desc_t *p = &interrupt_desc[irq_num - 32]; diff --git a/kernel/exception/irq.h b/kernel/exception/irq.h index 7da5c6c2..b1898a29 100644 --- a/kernel/exception/irq.h +++ b/kernel/exception/irq.h @@ -88,7 +88,7 @@ extern void do_IRQ(struct pt_regs *regs, ul number); */ -typedef struct hardware_int_type +typedef struct hardware_intr_type { // 使能中断操作接口 void (*enable)(ul irq_num); @@ -101,12 +101,12 @@ typedef struct hardware_int_type void (*uninstall)(ul irq_num); // 应答中断操作接口 void (*ack)(ul irq_num); -} hardware_int_controller; +} hardware_intr_controller; // 中断描述结构体 typedef struct { - hardware_int_controller *controller; + hardware_intr_controller *controller; // 中断名 char *irq_name; // 中断处理函数的参数 @@ -132,7 +132,7 @@ irq_desc_t interrupt_desc[IRQ_NUM] = {0}; * @param irq_name 中断名 * @return int */ -int irq_register(ul irq_num, void *arg, void (*handler)(ul irq_num, ul parameter, struct pt_regs *regs), ul paramater, hardware_int_controller *controller, char *irq_name); +int irq_register(ul irq_num, void *arg, void (*handler)(ul irq_num, ul parameter, struct pt_regs *regs), ul paramater, hardware_intr_controller *controller, char *irq_name); /** * @brief 中断注销函数 diff --git a/kernel/main.c b/kernel/main.c index f4348a7a..0c41dad0 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -15,6 +15,7 @@ #include "driver/multiboot2/multiboot2.h" #include "driver/acpi/acpi.h" +#include "driver/keyboard/keyboard.h" unsigned int *FR_address = (unsigned int *)0xb8000; //帧缓存区的地址 @@ -159,8 +160,8 @@ void system_initialize() // 先初始化系统调用模块 syscall_init(); - //cpu_init(); - + cpu_init(); + keyboard_init(); // test_slab(); // test_mm();