diff --git a/kernel/src/arch/x86_64/pci/pci.rs b/kernel/src/arch/x86_64/pci/pci.rs index e19227d17..40e09342c 100644 --- a/kernel/src/arch/x86_64/pci/pci.rs +++ b/kernel/src/arch/x86_64/pci/pci.rs @@ -4,11 +4,10 @@ use crate::driver::pci::pci::{ BusDeviceFunction, PciAddr, PciError, PciRoot, SegmentGroupNumber, PORT_PCI_CONFIG_ADDRESS, PORT_PCI_CONFIG_DATA, }; -use crate::include::bindings::bindings::{acpi_get_MCFG, acpi_iter_SDT, io_in32, io_out32}; +use crate::include::bindings::bindings::{io_in32, io_out32}; use crate::mm::PhysAddr; use acpi::mcfg::Mcfg; -use core::ffi::c_void; pub struct X86_64PciArch; impl TraitPciArch for X86_64PciArch { @@ -45,33 +44,20 @@ impl TraitPciArch for X86_64PciArch { } fn ecam_root(segement: SegmentGroupNumber) -> Result { - let mut data: usize = 0; - let data_point = &mut data; - unsafe { - acpi_iter_SDT(Some(acpi_get_MCFG), data_point as *mut usize as *mut c_void); - }; - // 防止无PCIE的机器找不到MCFG Table导致的错误 - if data == 0 { - return Err(PciError::McfgTableNotFound); - } - //kdebug!("{}",data); - //loop{} - - let binding = acpi_manager() + let mcfg = acpi_manager() .tables() .expect("get acpi_manager table error") - .find_table::(); - if let Ok(mcfg) = binding { - for mcfg_entry in mcfg.entries() { - if mcfg_entry.pci_segment_group == segement { - return Ok(PciRoot { - physical_address_base: PhysAddr::new(mcfg_entry.base_address as usize), - mmio_guard: None, - segement_group_number: segement, - bus_begin: mcfg_entry.bus_number_start, - bus_end: mcfg_entry.bus_number_end, - }); - } + .find_table::() + .map_err(|_| PciError::McfgTableNotFound)?; + for mcfg_entry in mcfg.entries() { + if mcfg_entry.pci_segment_group == segement { + return Ok(PciRoot { + physical_address_base: PhysAddr::new(mcfg_entry.base_address as usize), + mmio_guard: None, + segement_group_number: segement, + bus_begin: mcfg_entry.bus_number_start, + bus_end: mcfg_entry.bus_number_end, + }); } } return Err(PciError::SegmentNotFound); diff --git a/kernel/src/driver/acpi/acpi.c b/kernel/src/driver/acpi/acpi.c index ff3cf11fa..a73903be9 100644 --- a/kernel/src/driver/acpi/acpi.c +++ b/kernel/src/driver/acpi/acpi.c @@ -1,132 +1,19 @@ #include "acpi.h" -#include #include #include -#include -#include -extern void rs_acpi_init(uint64_t rsdp_paddr); - -#define acpi_get_RSDT_entry_vaddr(phys_addr) (acpi_description_header_base + (phys_addr)-acpi_RSDT_entry_phys_base) // 获取RSDT entry的虚拟地址 -// #define acpi_get_XSDT_entry_vaddr(phys_addr) (ACPI_DESCRIPTION_HEDERS_BASE + (phys_addr)-acpi_XSDT_entry_phys_base) // 获取XSDT entry的虚拟地址 +extern void rs_acpi_init(uint64_t rsdp_paddr1, uint64_t rsdp_paddr2); static struct acpi_RSDP_t *rsdpv1; static struct acpi_RSDP_2_t *rsdpv2; -static struct acpi_RSDT_Structure_t *rsdt; -static struct acpi_XSDT_Structure_t *xsdt; static struct multiboot_tag_old_acpi_t old_acpi; static struct multiboot_tag_new_acpi_t new_acpi; -static ul acpi_RSDT_offset = 0; -static ul acpi_XSDT_offset = 0; -static uint acpi_RSDT_Entry_num = 0; -static uint acpi_XSDT_Entry_num = 0; - -static ul acpi_RSDT_entry_phys_base = 0; // RSDT中的第一个entry所在物理页的基地址 - -static uint64_t acpi_madt_vaddr = 0; // MADT的虚拟地址 -static uint64_t acpi_rsdt_virt_addr_base = 0; // RSDT的虚拟地址 -static uint64_t acpi_description_header_base = 0; // RSDT中的第一个entry所在虚拟地址 - -// static ul acpi_XSDT_entry_phys_base = 0; // XSDT中的第一个entry所在物理页的基地址 - -/** - * @brief 迭代器,用于迭代描述符头(位于ACPI标准文件的Table 5-29) - * @param _fun 迭代操作调用的函数 - * @param _data 数据 - */ -void acpi_iter_SDT(bool (*_fun)(const struct acpi_system_description_table_header_t *, void *), - void *_data) -{ - - struct acpi_system_description_table_header_t *sdt_header; - if (acpi_use_xsdt) - { - ul *ent = &(xsdt->Entry); - for (int i = 0; i < acpi_XSDT_Entry_num; ++i) - { - // mm_map_phys_addr(acpi_description_header_base + PAGE_2M_SIZE * i, (*(ent + i)) & PAGE_2M_MASK, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false); - rs_map_phys(acpi_description_header_base + PAGE_2M_SIZE * i, (*(ent + i)) & PAGE_2M_MASK, PAGE_2M_SIZE, PAGE_KERNEL_PAGE); - sdt_header = (struct acpi_system_description_table_header_t *)((ul)(acpi_description_header_base + PAGE_2M_SIZE * i)); - - if (_fun(sdt_header, _data) == true) - return; - } - } - else - { - uint *ent = &(rsdt->Entry); - for (int i = 0; i < acpi_RSDT_Entry_num; ++i) - { - - sdt_header = (struct acpi_system_description_table_header_t *)(acpi_get_RSDT_entry_vaddr((ul)(*(ent + i)))); - - if (_fun(sdt_header, _data) == true) - return; - } - } - - return; -} - -/** - * @brief 获取MADT信息 Multiple APIC Description Table - * - * @param _iter_data 要被迭代的信息的结构体 - * @param _data 返回的MADT的虚拟地址 - * @param count 返回数组的长度 - * @return true - * @return false - */ -bool acpi_get_MADT(const struct acpi_system_description_table_header_t *_iter_data, void *_data) -{ - if (!(_iter_data->Signature[0] == 'A' && _iter_data->Signature[1] == 'P' && _iter_data->Signature[2] == 'I' && _iter_data->Signature[3] == 'C')) - return false; - //*(struct acpi_Multiple_APIC_Description_Table_t *)_data = *(struct acpi_Multiple_APIC_Description_Table_t *)_iter_data; - // 返回MADT的虚拟地址 - *(ul *)_data = (ul)_iter_data; - acpi_madt_vaddr = (ul)_iter_data; - return true; -} - -/** - * @brief 获取HPET HPET_description_table - * - * @param _iter_data 要被迭代的信息的结构体 - * @param _data 返回的HPET表的虚拟地址 - * @return true - * @return false - */ -bool acpi_get_HPET(const struct acpi_system_description_table_header_t *_iter_data, void *_data) -{ - if (!(_iter_data->Signature[0] == 'H' && _iter_data->Signature[1] == 'P' && _iter_data->Signature[2] == 'E' && _iter_data->Signature[3] == 'T')) - return false; - *(ul *)_data = (ul)_iter_data; - return true; -} - -/** - * @brief 获取MCFG MCFG_description_table - * - * @param _iter_data 要被迭代的信息的结构体 - * @param _data 返回的MCFG表的虚拟地址 - * @return true - * @return false - */ -bool acpi_get_MCFG(const struct acpi_system_description_table_header_t *_iter_data, void *_data) -{ - if (!(_iter_data->Signature[0] == 'M' && _iter_data->Signature[1] == 'C' && _iter_data->Signature[2] == 'F' && _iter_data->Signature[3] == 'G')) - return false; - *(ul *)_data = (ul)_iter_data; - return true; -} - /** * @brief 初始化acpi模块 * */ -// todo: 修复bug:当物理机上提供了rsdpv2之后,rsdpv1是不提供的(物理地址为0),因此需要手动判断rsdp的版本信息,然后做对应的解析。 void acpi_init() { kinfo("Initializing ACPI..."); @@ -137,131 +24,11 @@ void acpi_init() multiboot2_iter(multiboot2_get_acpi_old_RSDP, &old_acpi, &reserved); rsdpv1 = &(old_acpi.rsdp); - // 这里有bug:当multiboot2不存在rsdpv2的时候,会导致错误 - // multiboot2_iter(multiboot2_get_acpi_new_RSDP, &new_acpi, &reserved); - // rsdpv2 = &(new_acpi.rsdp); - rsdpv2 = NULL; - rs_acpi_init((uint64_t)rsdpv1); - - uint64_t paddr = 0; - // An ACPI-compatible OS must use the XSDT if present - if (rsdpv2 && rsdpv2->XsdtAddress != 0x00UL) - { - // 不要删除这段注释(因为还不确定是代码的bug,还是真机的bug) - /* - acpi_use_xsdt = true; - ul xsdt_phys_base = rsdpv2->XsdtAddress & PAGE_2M_MASK; - acpi_XSDT_offset = rsdpv2->XsdtAddress - xsdt_phys_base; - mm_map_phys_addr(ACPI_XSDT_VIRT_ADDR_BASE, xsdt_phys_base, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false); - kdebug("XSDT mapped!"); - - xsdt = (struct acpi_XSDT_Structure_t *)(ACPI_XSDT_VIRT_ADDR_BASE + acpi_XSDT_offset); - // 计算RSDT Entry的数量 - kdebug("offset=%d", sizeof(xsdt->header)); - kdebug("xsdt sign=%s", xsdt->header.Signature); - acpi_XSDT_Entry_num = (xsdt->header.Length - sizeof(xsdt->header)) / 8; - - printk_color(ORANGE, BLACK, "XSDT Length=%dbytes.\n", xsdt->header.Length); - printk_color(ORANGE, BLACK, "XSDT Entry num=%d\n", acpi_XSDT_Entry_num); - - mm_map_phys_addr(ACPI_XSDT_VIRT_ADDR_BASE, xsdt_phys_base, xsdt->header.Length + PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false); - // 映射所有的Entry的物理地址 - ul *ent = &(xsdt->Entry); - for (int j = 0; j < acpi_XSDT_Entry_num; ++j) - { - kdebug("entry=%#018lx, virt=%#018lx", (*(ent + j)) & PAGE_2M_MASK, ACPI_XSDT_DESCRIPTION_HEDERS_BASE + PAGE_2M_SIZE * j); - // 映射RSDT ENTRY的物理地址 - mm_map_phys_addr(ACPI_XSDT_DESCRIPTION_HEDERS_BASE + PAGE_2M_SIZE * j, (*(ent + j)) & PAGE_2M_MASK, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false); - } - */ - - // 由于解析XSDT出现问题。暂时只使用Rsdpv2的rsdt,但是这是不符合ACPI规范的!!! - ul rsdt_phys_base = rsdpv2->rsdp1.RsdtAddress & PAGE_2M_MASK; - acpi_RSDT_offset = rsdpv2->rsdp1.RsdtAddress - rsdt_phys_base; - - // 申请mmio空间 - uint64_t size = 0; - rs_mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_rsdt_virt_addr_base, &size); - - // 映射rsdt表 - paddr = (uint64_t)rsdt_phys_base; - // mm_map(&initial_mm, acpi_rsdt_virt_addr_base, PAGE_2M_SIZE, paddr); - rs_map_phys(acpi_rsdt_virt_addr_base, paddr, PAGE_2M_SIZE, PAGE_KERNEL_PAGE); - - // rsdt表虚拟地址 - rsdt = (struct acpi_RSDT_Structure_t *)(acpi_rsdt_virt_addr_base + acpi_RSDT_offset); - kdebug("RSDT mapped!(v2)"); - - // 计算RSDT Entry的数量 - kdebug("offset=%d", sizeof(rsdt->header)); - acpi_RSDT_Entry_num = (rsdt->header.Length - 36) / 4; - - printk_color(ORANGE, BLACK, "RSDT Length=%dbytes.\n", rsdt->header.Length); - printk_color(ORANGE, BLACK, "RSDT Entry num=%d\n", acpi_RSDT_Entry_num); - - // 申请mmio空间 - rs_mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_description_header_base, &size); - - // 映射所有的Entry的物理地址 - acpi_RSDT_entry_phys_base = ((ul)(rsdt->Entry)) & PAGE_2M_MASK; - // 由于地址只是32bit的,并且存在脏数据,这里需要手动清除高32bit,否则会触发#GP - acpi_RSDT_entry_phys_base = MASK_HIGH_32bit(acpi_RSDT_entry_phys_base); - - paddr = (uint64_t)acpi_RSDT_entry_phys_base; - // mm_map(&initial_mm, acpi_description_header_base, PAGE_2M_SIZE, paddr); - rs_map_phys(acpi_description_header_base, paddr, PAGE_2M_SIZE, PAGE_KERNEL_PAGE); - } - else if (rsdpv1->RsdtAddress != (uint)0x00UL) - { - // rsdt表物理地址 - ul rsdt_phys_base = rsdpv1->RsdtAddress & PAGE_2M_MASK; - acpi_RSDT_offset = rsdpv1->RsdtAddress - rsdt_phys_base; - - kdebug("rsdpv1->RsdtAddress=%#018lx", rsdpv1->RsdtAddress); - // 申请mmio空间 - uint64_t size = 0; - rs_mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_rsdt_virt_addr_base, &size); - // acpi_rsdt_virt_addr_base = 0xffffb00000000000UL; - kdebug("ACPI: mmio created. acpi_rsdt_virt_addr_base = %#018lx,size= %#010lx", acpi_rsdt_virt_addr_base, size); - - // kdebug("acpi_rsdt_virt_addr_base = %#018lx,size= %#010lx", acpi_rsdt_virt_addr_base, size); - // 映射rsdt表 - paddr = (uint64_t)rsdt_phys_base; - // mm_map(&initial_mm, acpi_rsdt_virt_addr_base, PAGE_2M_SIZE, paddr); - rs_map_phys(acpi_rsdt_virt_addr_base, paddr, PAGE_2M_SIZE, PAGE_KERNEL_PAGE); - // rsdt表虚拟地址 - rsdt = (struct acpi_RSDT_Structure_t *)(acpi_rsdt_virt_addr_base + acpi_RSDT_offset); - kdebug("RSDT mapped!"); - - // kdebug("length = %d",rsdt->header.Length); - // 计算RSDT Entry的数量 - // kdebug("offset=%d", sizeof(rsdt->header)); - - acpi_RSDT_Entry_num = (rsdt->header.Length - 36) / 4; - - printk_color(ORANGE, BLACK, "RSDT Length=%dbytes.\n", rsdt->header.Length); - printk_color(ORANGE, BLACK, "RSDT Entry num=%d\n", acpi_RSDT_Entry_num); - - // 申请mmio空间 - rs_mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_description_header_base, &size); - - // 映射所有的Entry的物理地址 - acpi_RSDT_entry_phys_base = ((ul)(rsdt->Entry)) & PAGE_2M_MASK; - // 由于地址只是32bit的,并且存在脏数据,这里需要手动清除高32bit,否则会触发#GP - acpi_RSDT_entry_phys_base = MASK_HIGH_32bit(acpi_RSDT_entry_phys_base); + multiboot2_iter(multiboot2_get_acpi_new_RSDP, &new_acpi, &reserved); + rsdpv2 = &(new_acpi.rsdp); - paddr = (uint64_t)acpi_RSDT_entry_phys_base; - // mm_map(&initial_mm, acpi_description_header_base, PAGE_2M_SIZE, paddr); - rs_map_phys(acpi_description_header_base, paddr, PAGE_2M_SIZE, PAGE_KERNEL_PAGE); - kinfo("entry mapped!"); - } - else - { - // should not reach here! - kBUG("At acpi_init(): Cannot get right SDT!"); - while (1) - ; - } + // rsdpv1、rsdpv2,二者有一个能成功即可 + rs_acpi_init((uint64_t)rsdpv1, (uint64_t)rsdpv2); kinfo("ACPI module initialized!"); return; diff --git a/kernel/src/driver/acpi/acpi.h b/kernel/src/driver/acpi/acpi.h index e0d999468..1e6bcac8a 100644 --- a/kernel/src/driver/acpi/acpi.h +++ b/kernel/src/driver/acpi/acpi.h @@ -7,28 +7,6 @@ #include #include -#define ACPI_ICS_TYPE_PROCESSOR_LOCAL_APIC 0 -#define ACPI_ICS_TYPE_IO_APIC 1 -#define ACPI_ICS_TYPE_INTERRUPT_SOURCE_OVERRIDE 2 -#define ACPI_ICS_TYPE_NMI_SOURCE 3 -#define ACPI_ICS_TYPE_LOCAL_APIC_NMI 4 -#define ACPI_ICS_TYPE_LOCAL_APIC_ADDRESS_OVERRIDE 5 -#define ACPI_ICS_TYPE_IO_SAPIC 6 -#define ACPI_ICS_TYPE_LOCAL_SAPIC 7 -#define ACPI_ICS_TYPE_PLATFORM_INTERRUPT_SOURCES 8 -#define ACPI_ICS_TYPE_PROCESSOR_LOCAL_x2APIC 9 -#define ACPI_ICS_TYPE_PROCESSOR_LOCAL_x2APIC_NMI 0xA -#define ACPI_ICS_TYPE_PROCESSOR_GICC 0xB -#define ACPI_ICS_TYPE_PROCESSOR_GICD 0xC -#define ACPI_ICS_TYPE_PROCESSOR_GIC_MSI_Frame 0xD -#define ACPI_ICS_TYPE_PROCESSOR_GICR 0xE -#define ACPI_ICS_TYPE_PROCESSOR_GIC_ITS 0xF -// 0x10-0x7f Reserved. OSPM skips structures of the reserved type. -// 0x80-0xff Reserved for OEM use -// extern const uint64_t acpi_rsdt_virt_addr_base ; // RSDT的虚拟地址 -// extern const uint64_t acpi_description_header_base ; // RSDT中的第一个entry所在虚拟地址 - -bool acpi_use_xsdt = false; struct acpi_RSDP_t { unsigned char Signature[8]; @@ -55,152 +33,5 @@ struct acpi_RSDP_2_t unsigned char Reserved[3]; } __attribute__((packed)); -struct acpi_system_description_table_header_t -{ - // The ascii string representation of the table header. - unsigned char Signature[4]; - // 整个表的长度(单位:字节),包括了header,从偏移量0处开始 - uint Length; - // The revision of the structure corresponding to the signature field for this table. - unsigned char Revision; - // The entire table, including the checksum field, must add to zero to be considered valid. - char Checksum; - - unsigned char OEMID[6]; - unsigned char OEM_Table_ID[8]; - uint OEMRevision; - uint CreatorID; - uint CreatorRevision; -} __attribute__((packed)); - -// HPET描述符结构体,sign为HPET -struct acpi_HPET_description_table_t -{ - struct acpi_system_description_table_header_t header; - - uint8_t hardware_rev_id; - uint8_t comparator_count : 5; // Number of Comparators in 1st Timer Block - uint8_t counter_size : 1; // COUNT_SIZE_CAP counter size - uint8_t reserved0 : 1; - uint8_t legacy_replacement : 1; // LegacyReplacement IRQ Routing Capable - uint16_t pci_vendor_id; // PCI Vendor ID of 1st Timer Block - - uint8_t address_space_id; // 0 - system memory, 1 - system I/O - uint8_t register_bit_width; - uint8_t register_bit_offset; - uint8_t reserved1; - uint64_t address; - - uint8_t hpet_number; - uint16_t minimum_tick; // The minimum clock ticks can be set without lost interrupts while the counter is programmed to operate in periodic mode - - uint8_t page_protection; - -} __attribute__((packed)); - -// =========== MADT结构,其中Signature为APIC ============ -struct acpi_Multiple_APIC_Description_Table_t -{ - struct acpi_system_description_table_header_t header; - - // 32bit的,每个处理器可访问的local中断控制器的物理地址 - uint Local_Interrupt_Controller_Address; - - // Multiple APIC flags, 详见 ACPI Specification Version 6.3, Table 5-44 - uint flags; - - // 接下来的(length-44)字节是Interrupt Controller Structure -}; - -struct apic_Interrupt_Controller_Structure_header_t -{ - unsigned char type; - unsigned char length; -}; - -struct acpi_Processor_Local_APIC_Structure_t -{ - // type=0 - struct apic_Interrupt_Controller_Structure_header_t header; - unsigned char ACPI_Processor_UID; - // 处理器的local apic id - unsigned char local_apic_id; - //详见 ACPI Specification Version 6.3, Table 5-47 - uint flags; -}; - -struct acpi_IO_APIC_Structure_t -{ - // type=1 - struct apic_Interrupt_Controller_Structure_header_t header; - unsigned char IO_APIC_ID; - unsigned char Reserved; - // 32bit的IO APIC物理地址 (每个IO APIC都有一个独立的物理地址) - uint IO_APIC_Address; - // 当前IO APIC的全局系统中断向量号起始值 - // The number of intr inputs is determined by the IO APIC's Max Redir Entry register. - uint Global_System_Interrupt_Base; -}; - -// =========== RSDT 结构 ============= -struct acpi_RSDT_Structure_t -{ - // 通过RSDT的header->Length可以计算出entry的数量n - // n = (length - 32)/4 - struct acpi_system_description_table_header_t header; - - // 一个包含了n个32bit物理地址的数组,指向了其他的description headers - uint Entry; -}; - -// =========== XSDT 结构 ============= -struct acpi_XSDT_Structure_t -{ - // 通过RSDT的header->Length可以计算出entry的数量n - // n = (length - 36)/8 - struct acpi_system_description_table_header_t header; - - // 一个包含了n个64bit物理地址的数组,指向了其他的description headers - ul Entry; -}; - -/** - * @brief 迭代器,用于迭代描述符头(位于ACPI标准文件的Table 5-29) - * @param _fun 迭代操作调用的函数 - * @param _data 数据 - */ -void acpi_iter_SDT(bool (*_fun)(const struct acpi_system_description_table_header_t *, void *), - void *_data); - -/** - * @brief 获取MADT信息 Multiple APIC Description Table - * - * @param _iter_data 要被迭代的信息的结构体 - * @param _data 返回的MADT的虚拟地址 - * @param count 返回数组的长度 - * @return true - * @return false - */ -bool acpi_get_MADT(const struct acpi_system_description_table_header_t *_iter_data, void *_data); - -/** - * @brief 获取HPET HPET_description_table - * - * @param _iter_data 要被迭代的信息的结构体 - * @param _data 返回的HPET表的虚拟地址 - * @return true - * @return false - */ -bool acpi_get_HPET(const struct acpi_system_description_table_header_t *_iter_data, void *_data); - -/** - * @brief 获取MCFG MCFG_description_table - * - * @param _iter_data 要被迭代的信息的结构体 - * @param _data 返回的MCFG表的虚拟地址 - * @return true - * @return false - */ -bool acpi_get_MCFG(const struct acpi_system_description_table_header_t *_iter_data, void *_data); // 初始化acpi模块 void acpi_init(); \ No newline at end of file diff --git a/kernel/src/driver/acpi/c_adapter.rs b/kernel/src/driver/acpi/c_adapter.rs index dfa4d5381..e4636c203 100644 --- a/kernel/src/driver/acpi/c_adapter.rs +++ b/kernel/src/driver/acpi/c_adapter.rs @@ -1,26 +1,8 @@ -use crate::{ - arch::MMArch, - libs::align::AlignedBox, - mm::{MemoryManagementArch, VirtAddr}, -}; - use super::acpi_manager; -static mut RSDP_TMP_BOX: Option> = None; - #[no_mangle] -unsafe extern "C" fn rs_acpi_init(rsdp_vaddr: u64) { - RSDP_TMP_BOX = Some(AlignedBox::new_zeroed().expect("rs_acpi_init(): failed to alloc")); - let size = core::mem::size_of::(); - let tmp_data = core::slice::from_raw_parts(rsdp_vaddr as usize as *const u8, size); - RSDP_TMP_BOX.as_mut().unwrap()[0..size].copy_from_slice(tmp_data); - - let rsdp_paddr = MMArch::virt_2_phys(VirtAddr::new( - RSDP_TMP_BOX.as_ref().unwrap().as_ptr() as usize - )) - .unwrap(); - +unsafe extern "C" fn rs_acpi_init(rsdp_vaddr1: u64, rsdp_vaddr2: u64) { acpi_manager() - .init(rsdp_paddr) + .init(rsdp_vaddr1, rsdp_vaddr2) .expect("rs_acpi_init(): failed to init acpi"); } diff --git a/kernel/src/driver/acpi/mod.rs b/kernel/src/driver/acpi/mod.rs index 9ec99cab5..ec7f558ec 100644 --- a/kernel/src/driver/acpi/mod.rs +++ b/kernel/src/driver/acpi/mod.rs @@ -1,15 +1,16 @@ -use core::{fmt::Debug, ptr::NonNull}; +use core::{fmt::Debug, hint::spin_loop, ptr::NonNull}; -use acpi::{AcpiHandler, PlatformInfo}; +use acpi::{AcpiHandler, AcpiTables, PlatformInfo}; use alloc::{string::ToString, sync::Arc}; use crate::{ + arch::MMArch, driver::base::firmware::sys_firmware_kset, kinfo, - libs::align::{page_align_down, page_align_up}, + libs::align::{page_align_down, page_align_up, AlignedBox}, mm::{ mmio_buddy::{mmio_pool, MMIOSpaceGuard}, - PhysAddr, VirtAddr, + MemoryManagementArch, PhysAddr, VirtAddr, }, syscall::SystemError, }; @@ -28,6 +29,8 @@ static mut __ACPI_TABLE: Option> = None; /// `/sys/firmware/acpi`的kset static mut ACPI_KSET_INSTANCE: Option> = None; +static mut RSDP_TMP_BOX: Option> = None; + #[inline(always)] pub fn acpi_manager() -> &'static AcpiManager { &AcpiManager @@ -46,13 +49,14 @@ impl AcpiManager { /// /// ## 参数 /// - /// - `rsdp_paddr`: RSDP的物理地址 + /// - `rsdp_vaddr1`: RSDP(v1)的虚拟地址 + /// - `rsdp_vaddr2`: RSDP(v2)的虚拟地址 /// /// /// ## 参考资料 /// /// https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1390 - pub fn init(&self, rsdp_paddr: PhysAddr) -> Result<(), SystemError> { + pub fn init(&self, rsdp_vaddr1: u64, rsdp_vaddr2: u64) -> Result<(), SystemError> { kinfo!("Initializing Acpi Manager..."); // 初始化`/sys/firmware/acpi`的kset @@ -61,26 +65,84 @@ impl AcpiManager { unsafe { ACPI_KSET_INSTANCE = Some(kset.clone()); } - self.map_tables(rsdp_paddr)?; + self.map_tables(rsdp_vaddr1, rsdp_vaddr2)?; self.bus_init()?; kinfo!("Acpi Manager initialized."); return Ok(()); } - fn map_tables(&self, rsdp_paddr: PhysAddr) -> Result<(), SystemError> { - let acpi_table: acpi::AcpiTables = - unsafe { acpi::AcpiTables::from_rsdp(AcpiHandlerImpl, rsdp_paddr.data()) }.map_err( - |e| { - kerror!("acpi_init(): failed to parse acpi tables, error: {:?}", e); - SystemError::ENOMEM - }, - )?; + fn map_tables(&self, rsdp_vaddr1: u64, rsdp_vaddr2: u64) -> Result<(), SystemError> { + let rsdp_paddr1 = Self::rsdp_paddr(rsdp_vaddr1); + let res1 = unsafe { acpi::AcpiTables::from_rsdp(AcpiHandlerImpl, rsdp_paddr1.data()) }; + let e1; + match res1 { + // 如果rsdpv1能够获取到acpi_table,则就用该表,不用rsdpv2了 + Ok(acpi_table) => { + Self::set_acpi_table(acpi_table); + return Ok(()); + } + Err(e) => { + e1 = e; + Self::drop_rsdp_tmp_box(); + } + } + + let rsdp_paddr2 = Self::rsdp_paddr(rsdp_vaddr2); + let res2 = unsafe { acpi::AcpiTables::from_rsdp(AcpiHandlerImpl, rsdp_paddr2.data()) }; + match res2 { + Ok(acpi_table) => { + Self::set_acpi_table(acpi_table); + } + // 如果rsdpv1和rsdpv2都无法获取到acpi_table,说明有问题,打印报错信息后进入死循环 + Err(e2) => { + kerror!("acpi_init(): failed to parse acpi tables, error: (rsdpv1: {:?}) or (rsdpv2: {:?})", e1, e2); + Self::drop_rsdp_tmp_box(); + loop { + spin_loop(); + } + } + } + + return Ok(()); + } + + /// 通过RSDP虚拟地址获取RSDP物理地址 + /// + /// ## 参数 + /// + /// - `rsdp_vaddr`: RSDP的虚拟地址 + /// + /// ## 返回值 + /// + /// RSDP物理地址 + fn rsdp_paddr(rsdp_vaddr: u64) -> PhysAddr { + unsafe { + RSDP_TMP_BOX = Some(AlignedBox::new_zeroed().expect("rs_acpi_init(): failed to alloc")) + }; + let size = core::mem::size_of::(); + let tmp_data = + unsafe { core::slice::from_raw_parts(rsdp_vaddr as usize as *const u8, size) }; + unsafe { RSDP_TMP_BOX.as_mut().unwrap()[0..size].copy_from_slice(tmp_data) }; + let rsdp_paddr = unsafe { + MMArch::virt_2_phys(VirtAddr::new( + RSDP_TMP_BOX.as_ref().unwrap().as_ptr() as usize + )) + .unwrap() + }; + + return rsdp_paddr; + } + fn set_acpi_table(acpi_table: AcpiTables) { unsafe { __ACPI_TABLE = Some(acpi_table); } + } - return Ok(()); + fn drop_rsdp_tmp_box() { + unsafe { + RSDP_TMP_BOX = None; + } } #[allow(dead_code)] diff --git a/kernel/src/driver/multiboot2/multiboot2.c b/kernel/src/driver/multiboot2/multiboot2.c index f0e2bbb9f..234ee599a 100644 --- a/kernel/src/driver/multiboot2/multiboot2.c +++ b/kernel/src/driver/multiboot2/multiboot2.c @@ -11,7 +11,7 @@ unsigned int multiboot2_boot_info_size; static uint8_t mbi_raw[MBI_RAW_MAX_SIZE] = {0}; bool multiboot2_init(uint64_t mb2_info_paddr, uint32_t mb2_magic) { - uint64_t vaddr = phys_2_virt(mb2_info_paddr); + uint64_t vaddr = (uint64_t)phys_2_virt(mb2_info_paddr); if (mb2_magic != MULTIBOOT2_BOOTLOADER_MAGIC) return false; // vaddr+0 处保存了大小 diff --git a/kernel/src/exception/irq.c b/kernel/src/exception/irq.c index bed67d9c9..78f1cf339 100644 --- a/kernel/src/exception/irq.c +++ b/kernel/src/exception/irq.c @@ -240,7 +240,7 @@ int irq_unregister(ul irq_num) if (p->irq_name) kfree(p->irq_name); p->irq_name = NULL; - p->parameter = NULL; + p->parameter = (ul)NULL; p->flags = 0; p->handler = NULL; diff --git a/kernel/src/smp/smp.c b/kernel/src/smp/smp.c index cffdcde86..f674e66e9 100644 --- a/kernel/src/smp/smp.c +++ b/kernel/src/smp/smp.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -13,10 +14,10 @@ #include "ipi.h" /* x86-64 specific MSRs */ -#define MSR_EFER 0xc0000080 /* extended feature register */ -#define MSR_STAR 0xc0000081 /* legacy mode SYSCALL target */ -#define MSR_LSTAR 0xc0000082 /* long mode SYSCALL target */ -#define MSR_SYSCALL_MASK 0xc0000084 /* EFLAGS mask for syscall */ +#define MSR_EFER 0xc0000080 /* extended feature register */ +#define MSR_STAR 0xc0000081 /* legacy mode SYSCALL target */ +#define MSR_LSTAR 0xc0000082 /* long mode SYSCALL target */ +#define MSR_SYSCALL_MASK 0xc0000084 /* EFLAGS mask for syscall */ static void __smp_kick_cpu_handler(uint64_t irq_num, uint64_t param, struct pt_regs *regs); static void __smp__flush_tlb_ipi_handler(uint64_t irq_num, uint64_t param, struct pt_regs *regs); @@ -77,7 +78,7 @@ void smp_init() kdebug("total_processor_num=%d", total_processor_num); // 注册接收kick_cpu功能的处理函数。(向量号200) - ipi_regiserIPI(KICK_CPU_IRQ_NUM, NULL, &__smp_kick_cpu_handler, NULL, NULL, "IPI kick cpu"); + ipi_regiserIPI(KICK_CPU_IRQ_NUM, NULL, &__smp_kick_cpu_handler, (uint64_t)NULL, NULL, "IPI kick cpu"); ipi_regiserIPI(FLUSH_TLB_IRQ_NUM, NULL, &__smp__flush_tlb_ipi_handler, NULL, NULL, "IPI flush tlb"); int core_to_start = 0; @@ -116,7 +117,8 @@ void smp_init() // 连续发送两次start-up IPI int r = rs_ipi_send_smp_startup(__cpu_info[i].apic_id); - if(r){ + if (r) + { kerror("Failed to send startup ipi to cpu: %d", __cpu_info[i].apic_id); } io_mfence(); @@ -158,9 +160,8 @@ void smp_ap_start_stage2() spin_unlock_no_preempt(&multi_core_starting_lock); rs_init_syscall_64(); - - apic_timer_ap_core_init(); + apic_timer_ap_core_init(); sti(); sched(); diff --git a/user/dadk/config/tar_1_35.dadk b/user/dadk/config/tar_1_35.dadk index ab992d7cf..787c39b8c 100644 --- a/user/dadk/config/tar_1_35.dadk +++ b/user/dadk/config/tar_1_35.dadk @@ -6,7 +6,7 @@ "task_type": { "InstallFromPrebuilt": { "Archive": { - "url": "https://mirrors.dragonos.org/pub/third_party/gnu/tar/tar-1.35-x86_64-linux-gnu.tar.xz" + "url": "https://mirrors.dragonos.org.cn/pub/third_party/gnu/tar/tar-1.35-x86_64-linux-gnu.tar.xz" } } },