Skip to content

Commit 6f324b0

Browse files
lai: initial intigration
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent ca08655 commit 6f324b0

File tree

10 files changed

+84
-223
lines changed

10 files changed

+84
-223
lines changed

src/Cargo.lock

Lines changed: 14 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aero_kernel/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ hashbrown = "0.11.2"
3333
rustc-demangle = "0.1.20"
3434
stivale-boot = "0.2.6"
3535
intrusive-collections = "0.9.2"
36-
aml = "0.15.0"
3736
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
37+
lai = { path = "../../../lai" }
3838

3939
[dependencies.vte]
4040
git = "https://github.com/Andy-Python-Programmer/vte"

src/aero_kernel/src/acpi/fadt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use super::sdt::Sdt;
2626
pub const SIGNATURE: &str = "FACP";
2727

2828
#[repr(C, packed)]
29-
pub(super) struct Fadt {
29+
pub struct Fadt {
3030
pub header: Sdt,
3131
pub firmware_ctrl: u32,
3232
pub dsdt: u32,

src/aero_kernel/src/acpi/mod.rs

Lines changed: 22 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
//!
2323
//! **Notes**: <https://wiki.osdev.org/ACPI>
2424
25-
use aml::AmlContext;
2625
use spin::Once;
2726

28-
use crate::mem::paging::{PhysAddr, VirtAddr};
29-
30-
use crate::utils::sync::{Mutex, MutexGuard};
27+
use crate::{
28+
mem::paging::{PhysAddr, VirtAddr},
29+
utils::sync::{Mutex, MutexGuard},
30+
};
3131

3232
use self::{hpet::Hpet, madt::Madt, mcfg::Mcfg, sdt::Sdt};
3333

@@ -38,8 +38,6 @@ pub mod mcfg;
3838
pub mod rsdp;
3939
pub mod sdt;
4040

41-
static AML_CONTEXT: Once<Mutex<AmlContext>> = Once::new();
42-
4341
enum AcpiHeader {
4442
Rsdt(&'static rsdp::Rsdt<u32>),
4543
Xsdt(&'static rsdp::Rsdt<u64>),
@@ -73,12 +71,19 @@ impl AcpiTable {
7371
}
7472

7573
/// Lookup ACPI table entry with the provided signature.
76-
fn lookup_entry(&self, signature: &str) -> Option<&'static Sdt> {
74+
pub fn lookup_entry(&self, signature: &str) -> Option<&'static Sdt> {
7775
match self.header {
7876
AcpiHeader::Rsdt(rsdt) => rsdt.lookup_entry(signature),
7977
AcpiHeader::Xsdt(xsdt) => xsdt.lookup_entry(signature),
8078
}
8179
}
80+
81+
pub fn revision(&self) -> u8 {
82+
match self.header {
83+
AcpiHeader::Rsdt(rsdt) => rsdt.header.revision,
84+
AcpiHeader::Xsdt(xsdt) => xsdt.header.revision,
85+
}
86+
}
8287
}
8388

8489
#[repr(packed)]
@@ -91,140 +96,21 @@ pub struct GenericAddressStructure {
9196
pub address: u64,
9297
}
9398

94-
struct AmlHandler;
95-
96-
impl aml::Handler for AmlHandler {
97-
fn read_u8(&self, address: usize) -> u8 {
98-
log::trace!("AML: Reading byte from {:#x}", address);
99-
100-
unsafe {
101-
PhysAddr::new(address as u64)
102-
.as_hhdm_virt()
103-
.as_ptr::<u8>()
104-
.read_volatile()
105-
}
106-
}
107-
108-
fn read_u16(&self, _address: usize) -> u16 {
109-
todo!()
110-
}
111-
112-
fn read_u32(&self, _address: usize) -> u32 {
113-
todo!()
114-
}
115-
116-
fn read_u64(&self, _address: usize) -> u64 {
117-
todo!()
118-
}
119-
120-
fn write_u8(&mut self, _address: usize, _value: u8) {
121-
todo!()
122-
}
123-
124-
fn write_u16(&mut self, _address: usize, _value: u16) {
125-
todo!()
126-
}
127-
128-
fn write_u32(&mut self, _address: usize, _value: u32) {
129-
todo!()
130-
}
131-
132-
fn write_u64(&mut self, _address: usize, _value: u64) {
133-
todo!()
134-
}
135-
136-
fn read_io_u8(&self, _port: u16) -> u8 {
137-
todo!()
138-
}
139-
140-
fn read_io_u16(&self, _port: u16) -> u16 {
141-
todo!()
142-
}
143-
144-
fn read_io_u32(&self, _port: u16) -> u32 {
145-
todo!()
146-
}
99+
static ACPI_TABLE: Once<Mutex<AcpiTable>> = Once::new();
147100

148-
fn write_io_u8(&self, _port: u16, _value: u8) {
149-
todo!()
150-
}
151-
152-
fn write_io_u16(&self, _port: u16, _value: u16) {
153-
todo!()
154-
}
155-
156-
fn write_io_u32(&self, _port: u16, _value: u32) {
157-
todo!()
158-
}
159-
160-
fn read_pci_u8(&self, _segment: u16, _bus: u8, _device: u8, _unction: u8, _offset: u16) -> u8 {
161-
todo!()
162-
}
163-
164-
fn read_pci_u16(
165-
&self,
166-
_segment: u16,
167-
_bus: u8,
168-
_device: u8,
169-
_function: u8,
170-
_offset: u16,
171-
) -> u16 {
172-
todo!()
173-
}
174-
175-
fn read_pci_u32(
176-
&self,
177-
_segment: u16,
178-
_bus: u8,
179-
_device: u8,
180-
_function: u8,
181-
_offset: u16,
182-
) -> u32 {
183-
todo!()
184-
}
185-
186-
fn write_pci_u8(
187-
&self,
188-
_segment: u16,
189-
_bus: u8,
190-
_device: u8,
191-
_function: u8,
192-
_offset: u16,
193-
_value: u8,
194-
) {
195-
todo!()
196-
}
197-
198-
fn write_pci_u16(
199-
&self,
200-
_segment: u16,
201-
_bus: u8,
202-
_device: u8,
203-
_function: u8,
204-
_offset: u16,
205-
_value: u16,
206-
) {
207-
todo!()
208-
}
209-
210-
fn write_pci_u32(
211-
&self,
212-
_segment: u16,
213-
_bus: u8,
214-
_device: u8,
215-
_function: u8,
216-
_offset: u16,
217-
_value: u32,
218-
) {
219-
todo!()
220-
}
101+
pub fn get_acpi_table() -> MutexGuard<'static, AcpiTable> {
102+
ACPI_TABLE.get().unwrap().lock()
221103
}
222104

223105
/// Initialize the ACPI tables.
224-
pub fn init(rsdp_address: PhysAddr) -> Result<(), aml::AmlError> {
106+
pub fn init(rsdp_address: PhysAddr) {
225107
let rsdp_address = rsdp_address.as_hhdm_virt();
226108
let acpi_table = AcpiTable::new(rsdp_address);
227109

110+
ACPI_TABLE.call_once(|| Mutex::new(acpi_table));
111+
112+
let acpi_table = get_acpi_table();
113+
228114
macro init_table($sig:path => $ty:ty) {
229115
if let Some(table) = acpi_table.lookup_entry($sig) {
230116
<$ty>::new(table);
@@ -233,7 +119,7 @@ pub fn init(rsdp_address: PhysAddr) -> Result<(), aml::AmlError> {
233119

234120
if let Some(header) = acpi_table.lookup_entry(mcfg::SIGNATURE) {
235121
unsafe {
236-
let mcfg: &'static Mcfg = header.as_ptr();
122+
let mcfg: &'static Mcfg = header.as_ref();
237123
mcfg.init();
238124
}
239125
}
@@ -247,44 +133,11 @@ pub fn init(rsdp_address: PhysAddr) -> Result<(), aml::AmlError> {
247133
header.data_len()
248134
);
249135
} else {
250-
let madt: &'static Madt = header.as_ptr();
136+
let madt: &'static Madt = header.as_ref();
251137
madt.init();
252138
}
253139
}
254140
}
255141

256142
init_table!(hpet::SIGNATURE => Hpet);
257-
258-
let aml_context = AmlContext::new(box AmlHandler, aml::DebugVerbosity::None);
259-
260-
if let Some(fadt) = acpi_table.lookup_entry(fadt::SIGNATURE) {
261-
let fadt: &'static fadt::Fadt = unsafe { fadt.as_ptr() };
262-
263-
// The DSDT table is put inside the FADT table, instead of listing it in another ACPI table. So
264-
// we need to extract the DSDT table from the FADT table.
265-
let _dsdt_stream = unsafe {
266-
let addr = PhysAddr::new(fadt.dsdt as u64).as_hhdm_virt();
267-
let sdt = Sdt::from_address(addr);
268-
269-
core::slice::from_raw_parts(sdt.data_address() as *mut u8, sdt.data_len())
270-
};
271-
272-
// aml_context.parse_table(dsdt_stream)?;
273-
}
274-
275-
// let pci_router =
276-
// PciRoutingTable::from_prt_path(&AmlName::from_str("\\_SB.PCI0._PRT")?, &mut aml_context)?;
277-
278-
// drivers::pci::init_pci_router(pci_router);
279-
280-
AML_CONTEXT.call_once(move || Mutex::new(aml_context));
281-
Ok(())
282-
}
283-
284-
/// Returns an immutable reference to the AML context.
285-
pub fn get_aml_context() -> MutexGuard<'static, AmlContext> {
286-
AML_CONTEXT
287-
.get()
288-
.expect("attempted to get AML context before the ACPI initialization")
289-
.lock()
290143
}

src/aero_kernel/src/acpi/sdt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use core::mem;
2222
use crate::mem::paging::VirtAddr;
2323

2424
#[repr(C, packed)]
25-
pub(super) struct Sdt {
25+
pub struct Sdt {
2626
pub(super) signature: [u8; 4],
2727
pub(super) length: u32,
2828
pub(super) revision: u8,
@@ -73,7 +73,7 @@ impl Sdt {
7373
}
7474

7575
#[inline]
76-
pub(super) unsafe fn as_ptr<T>(&self) -> &'static T {
76+
pub unsafe fn as_ref<T>(&self) -> &'static T {
7777
&*(self as *const _ as *const T)
7878
}
7979
}

src/aero_kernel/src/arch/x86_64/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ extern "C" fn x86_64_aero_main(boot_info: &'static StivaleStruct) -> ! {
188188
apic::init();
189189
log::info!("loaded APIC");
190190

191-
acpi::init(rsdp_address).unwrap();
191+
acpi::init(rsdp_address);
192192
log::info!("loaded ACPI");
193193

194194
tls::init(0);

0 commit comments

Comments
 (0)