From 046bce39a90a2195875b33cee0a2d80569dcb1b0 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Mon, 21 Mar 2022 17:13:15 +0800 Subject: [PATCH] ata_disk_handler --- kernel/Makefile | 7 +++-- kernel/common/glib.h | 14 +++++++++ kernel/driver/disk/ata.c | 46 +++++++++++++++++++++++------ kernel/driver/disk/ata.h | 4 +-- kernel/driver/interrupt/apic/apic.c | 1 - kernel/main.c | 9 ++++-- run.sh | 5 +++- 7 files changed, 68 insertions(+), 18 deletions(-) diff --git a/kernel/Makefile b/kernel/Makefile index 85f37a38..1a381c48 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -19,10 +19,10 @@ all: kernel objcopy -I elf64-x86-64 -S -R ".comment" -R ".eh_frame" -O elf64-x86-64 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 +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 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/keyboard/ps2_keyboard.o driver/mouse/ps2_mouse.o \ + driver/acpi/acpi.o driver/interrupt/pic.o driver/keyboard/ps2_keyboard.o driver/mouse/ps2_mouse.o driver/disk/ata.o \ -T link.lds head.o: head.S @@ -91,6 +91,9 @@ ps2_keyboard.o: driver/keyboard/ps2_keyboard.c ps2_mouse.o: driver/mouse/ps2_mouse.c gcc $(CFLAGS) -c driver/mouse/ps2_mouse.c -o driver/mouse/ps2_mouse.o +ata.o: driver/disk/ata.c + gcc $(CFLAGS) -c driver/disk/ata.c -o driver/disk/ata.o + clean: rm -rf $(GARBAGE) \ No newline at end of file diff --git a/kernel/common/glib.h b/kernel/common/glib.h index cdb80319..df09daa9 100644 --- a/kernel/common/glib.h +++ b/kernel/common/glib.h @@ -279,6 +279,20 @@ void io_out32(unsigned short port, unsigned int value) : "memory"); } +/** + * @brief 从端口读入n个word到buffer + * + */ +#define io_insw(port,buffer,nr) \ +__asm__ __volatile__("cld;rep;insw;mfence;"::"d"(port),"D"(buffer),"c"(nr):"memory") + +/** + * @brief 从输出buffer中的n个word到端口 + * + */ +#define io_outsw(port,buffer,nr) \ +__asm__ __volatile__("cld;rep;outsw;mfence;"::"d"(port),"S"(buffer),"c"(nr):"memory") + /** * @brief 读取rsp寄存器的值(存储了页目录的基地址) * diff --git a/kernel/driver/disk/ata.c b/kernel/driver/disk/ata.c index 26a89a04..cc522fda 100644 --- a/kernel/driver/disk/ata.c +++ b/kernel/driver/disk/ata.c @@ -13,7 +13,35 @@ struct apic_IO_APIC_RTE_entry entry; */ void ata_disk_handler(ul irq_num, ul param, struct pt_regs *regs) { + struct ata_identify_device_data info; + kdebug("irq_num=%ld", irq_num); + // 从端口读入磁盘配置信息 + io_insw(PORT_DISK0_DATA, &info, 256); + kdebug("General_Config=%#018lx", info.General_Config); + printk("Serial number:"); + unsigned char buf[64]; + int js=0; + //printk("%d", info.Serial_Number); + printk("sizeof short=%d\n", sizeof(ushort)); + + for(int i = 0;i<10;i++) + { + printk("[%d] %d \n",js, ((info.Serial_Number[i]))); + printk("[%d] %d shift 8\n",js, ((info.Serial_Number[i]) >> 8)); + printk("[%d] %d and 0xff\n",js, ((info.Serial_Number[i]) >> 8)&0xff); + buf[js++]=((info.Serial_Number[i]) >> 8) & 0xff; + buf[js++]=(info.Serial_Number[i] & 0xff); + } + buf[js] = '\0'; + printk("xxx"); + printk("buf[0]=%d", buf[0]); + printk("buf[0]=%c", buf[0]); + printk("%s", buf); + printk("\n"); + + + } hardware_intr_controller ata_disk_intr_controller = @@ -31,7 +59,7 @@ hardware_intr_controller ata_disk_intr_controller = */ void ata_init() { - entry.vector = 0x2f; + entry.vector = 0x2e; entry.deliver_mode = IO_APIC_FIXED; entry.dest_mode = DEST_PHYSICAL; entry.deliver_status = IDLE; @@ -47,14 +75,14 @@ void ata_init() irq_register(entry.vector, &entry, &ata_disk_handler, 0, &ata_disk_intr_controller, "ATA Disk 1"); - io_out8(PORT_DISK1_STATUS_CTRL_REG, 0); // 使能中断请求 + io_out8(PORT_DISK0_STATUS_CTRL_REG, 0); // 使能中断请求 - io_out8(PORT_DISK1_ERR_STATUS, 0); - io_out8(PORT_DISK1_SECTOR_CNT, 0); - io_out8(PORT_DISK1_LBA_7_0, 0); - io_out8(PORT_DISK1_LBA_15_8, 0); - io_out8(PORT_DISK1_LBA_23_16, 0); - io_out8(PORT_DISK1_DEVICE_CONFIGURE_REG, 0); + io_out8(PORT_DISK0_ERR_STATUS, 0); + io_out8(PORT_DISK0_SECTOR_CNT, 0); + io_out8(PORT_DISK0_LBA_7_0, 0); + io_out8(PORT_DISK0_LBA_15_8, 0); + io_out8(PORT_DISK0_LBA_23_16, 0); + io_out8(PORT_DISK0_DEVICE_CONFIGURE_REG, 0); - io_out8(PORT_DISK1_CONTROLLER_STATUS_CMD, 0xec); // 获取硬件设备识别信息 + io_out8(PORT_DISK0_CONTROLLER_STATUS_CMD, 0xec); // 获取硬件设备识别信息 } \ No newline at end of file diff --git a/kernel/driver/disk/ata.h b/kernel/driver/disk/ata.h index c8e570c3..b28380b9 100644 --- a/kernel/driver/disk/ata.h +++ b/kernel/driver/disk/ata.h @@ -9,7 +9,7 @@ #define PORT_DISK0_LBA_7_0 0x1f3 // 扇区号 / LBA[7:0] #define PORT_DISK0_LBA_15_8 0x1f4 // 柱面号[7:0] / LBA[15:8] #define PORT_DISK0_LBA_23_16 0x1f5 // 柱面号[15:8] / LBA[23:16] -#define PORT_DISK0_LBA_DEVICE_CONFIGURE_REG 0x1f6 // 设备配置寄存器 +#define PORT_DISK0_DEVICE_CONFIGURE_REG 0x1f6 // 设备配置寄存器 #define PORT_DISK0_CONTROLLER_STATUS_CMD 0x1f7 // 控制器状态端口 / 控制器命令端口 #define PORT_DISK0_STATUS_CTRL_REG 0x3f6 // 状态寄存器 / 控制寄存器 @@ -32,7 +32,7 @@ /** * @brief 执行0xec指令返回的512bytes的硬件设备识别信息 - * 位于ATA8-ACS中 + * 位于ATA8-ACS中 Table-22 */ struct ata_identify_device_data { diff --git a/kernel/driver/interrupt/apic/apic.c b/kernel/driver/interrupt/apic/apic.c index 9dc3f1e3..59b9a94e 100644 --- a/kernel/driver/interrupt/apic/apic.c +++ b/kernel/driver/interrupt/apic/apic.c @@ -78,7 +78,6 @@ void apic_io_apic_init() apic_ioapic_write_rte(i, 0x10020 + ((i - 0x10) >> 1)); } - // 不需要手动启动IO APIC,只要初始化了RTE寄存器之后,io apic就会自动启用了。 // 而且不是每台电脑都有RCBA寄存器,因此不需要手动启用IO APIC /* diff --git a/kernel/main.c b/kernel/main.c index be8b1f35..1ef01aaf 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -17,6 +17,7 @@ #include "driver/acpi/acpi.h" #include "driver/keyboard/ps2_keyboard.h" #include "driver/mouse/ps2_mouse.h" +#include "driver/disk/ata.h" unsigned int *FR_address = (unsigned int *)0xb8000; //帧缓存区的地址 @@ -162,8 +163,9 @@ void system_initialize() syscall_init(); cpu_init(); - ps2_keyboard_init(); - ps2_mouse_init(); + //ps2_keyboard_init(); + //ps2_mouse_init(); + ata_init(); // test_slab(); // test_mm(); @@ -180,6 +182,7 @@ void Start_Kernel(void) // show_welcome(); // test_mm(); +/* while (1) { ps2_keyboard_analyze_keycode(); @@ -193,7 +196,7 @@ void Start_Kernel(void) // printk_color(GREEN, BLACK, " (Mouse: byte0:%d, x:%3d, y:%3d, byte3:%3d)\n", packet.byte0, packet.movement_x, packet.movement_y, (unsigned char)packet.byte3); } } - +*/ /* while (1) { diff --git a/run.sh b/run.sh index 1be7640f..322f0acc 100644 --- a/run.sh +++ b/run.sh @@ -93,7 +93,10 @@ if [ $flag_can_run -eq 1 ]; then bochs -q -f ${bochsrc} -rc ./tools/bochsinit else qemu-system-x86_64 -cdrom ${iso} -m 512M \ - -monitor telnet::2333,server,nowait -serial stdio -s -cpu IvyBridge --enable-kvm + -monitor telnet::2333,server,nowait -serial stdio -s -cpu IvyBridge --enable-kvm \ + #-drive id=disk,file=bin/disk.img,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 + -hda bin/disk.img + fi else echo "不满足运行条件"