新增rust ffi (#77)

* 引入cargo

* 取消对Cargo.lock的跟踪

* 解决vscode报错问题

* new: rust的代码能够调用c语言的printk_color

* 1、将原本run.sh的工作拆解,变为几个不同的make命令
2、在docker镜像中编译rust

* 更改workflow

* update workflow

* new: 解决workflow无法通过编译的问题
This commit is contained in:
login
2022-11-11 15:35:37 +08:00
committed by GitHub
parent 5e023cf791
commit 2813126e31
271 changed files with 609 additions and 307 deletions

View File

@ -0,0 +1,8 @@
all: acpi.o
CFLAGS += -I .
acpi.o: acpi.c
$(CC) $(CFLAGS) -c acpi.c -o acpi.o

View File

@ -0,0 +1,241 @@
#include "acpi.h"
#include <common/printk.h>
#include <common/kprint.h>
#include <driver/multiboot2/multiboot2.h>
#include <mm/mm.h>
#include <mm/mmio.h>
#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的虚拟地址
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);
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 初始化acpi模块
*
*/
// todo: 修复bug当物理机上提供了rsdpv2之后rsdpv1是不提供的物理地址为0因此需要手动判断rsdp的版本信息然后做对应的解析。
void acpi_init()
{
kinfo("Initializing ACPI...");
// 获取物理地址
int reserved;
multiboot2_iter(multiboot2_get_acpi_old_RSDP, &old_acpi, &reserved);
rsdpv1 = &(old_acpi.rsdp);
multiboot2_iter(multiboot2_get_acpi_new_RSDP, &new_acpi, &reserved);
rsdpv2 = &(new_acpi.rsdp);
uint64_t paddr = 0;
// An ACPI-compatible OS must use the XSDT if present
if (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;
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);
// 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空间
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);
}
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;
mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &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);
// 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空间
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);
// kinfo("entry mapped!");
}
else
{
// should not reach here!
kBUG("At acpi_init(): Cannot get right SDT!");
while (1)
;
}
kinfo("ACPI module initialized!");
return;
}

View File

@ -0,0 +1,197 @@
/**
* 解析acpi信息的模块
**/
#pragma once
#include <common/glib.h>
#include <mm/mm.h>
#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];
unsigned char Checksum;
unsigned char OEMID[6];
unsigned char Revision;
// 32bit physical address of the RSDT
uint RsdtAddress;
} __attribute__((packed));
struct acpi_RSDP_2_t
{
struct acpi_RSDP_t rsdp1;
// fields below are only valid when the revision value is 2 or above
// 表的长度单位字节从offset=0开始算
uint Length;
// 64bit的XSDT的物理地址
ul XsdtAddress;
unsigned char ExtendedChecksum; // 整个表的checksum包括了之前的checksum区域
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);
// 初始化acpi模块
void acpi_init();