Skip to content

Commit 86ef8ad

Browse files
committed
feat: add axklib
1 parent 73ce902 commit 86ef8ad

File tree

7 files changed

+203
-0
lines changed

7 files changed

+203
-0
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ members = [
1717
"modules/axsync",
1818
"modules/axtask",
1919
"modules/axipi",
20+
"modules/axklib-impl",
2021

2122
"api/axfeat",
2223
"api/arceos_api",
2324
"api/arceos_posix_api",
25+
"api/axklib",
2426

2527
"ulib/axstd",
2628
"ulib/axlibc",

api/axklib/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "axklib"
3+
version.workspace = true
4+
edition.workspace = true
5+
authors = ["周睿 <zrufo747@outlook.com>"]
6+
license.workspace = true
7+
homepage.workspace = true
8+
documentation.workspace = true
9+
repository.workspace = true
10+
keywords.workspace = true
11+
categories.workspace = true
12+
13+
[dependencies]
14+
trait-ffi = "0.2"
15+
memory_addr = "0.4"
16+
axerrno = "0.1"

api/axklib/src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![no_std]
2+
3+
use core::time::Duration;
4+
5+
pub use axerrno::AxResult;
6+
pub type IrqHandler = fn();
7+
pub use memory_addr::{PhysAddr, VirtAddr};
8+
9+
use trait_ffi::*;
10+
11+
#[def_extern_trait]
12+
pub trait Klib {
13+
/// Maps a physical memory region to a virtual address space and returns the virtual address.
14+
///
15+
/// The returned virtual address is guaranteed to be page-aligned.
16+
fn mem_iomap(addr: PhysAddr, size: usize) -> AxResult<VirtAddr>;
17+
18+
fn time_busy_wait(dur: Duration);
19+
20+
fn irq_set_enable(irq: usize, enabled: bool);
21+
22+
fn irq_register(irq: usize, handler: IrqHandler) -> bool;
23+
}
24+
25+
pub mod mem {
26+
pub use super::klib::mem_iomap as iomap;
27+
}
28+
29+
pub mod time {
30+
pub use super::klib::time_busy_wait as busy_wait;
31+
}
32+
33+
pub mod irq {
34+
pub use super::klib::irq_register as register;
35+
pub use super::klib::irq_set_enable as set_enable;
36+
}

modules/axklib-impl/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
authors = ["周睿 <zrufo747@outlook.com>"]
3+
categories.workspace = true
4+
documentation.workspace = true
5+
edition.workspace = true
6+
homepage.workspace = true
7+
keywords.workspace = true
8+
license.workspace = true
9+
name = "axklib-impl"
10+
repository.workspace = true
11+
version.workspace = true
12+
13+
[features]
14+
irq = ["axhal/irq"]
15+
16+
[dependencies]
17+
axhal = {workspace = true}
18+
axklib = {path = "../../api/axklib"}
19+
axmm = {workspace = true}
20+
trait-ffi = "0.2"

modules/axklib-impl/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![no_std]
2+
3+
use core::time::Duration;
4+
5+
use axklib::*;
6+
7+
struct KlibImpl;
8+
9+
impl_trait! {
10+
impl Klib for KlibImpl {
11+
fn mem_iomap(addr: PhysAddr, size: usize) -> AxResult<VirtAddr> {
12+
mem::iomap(addr, size)
13+
}
14+
15+
fn time_busy_wait(dur: Duration) {
16+
time::busy_wait(dur);
17+
}
18+
19+
fn irq_set_enable(_irq: usize, _enabled: bool) {
20+
#[cfg(feature = "irq")]
21+
irq::set_enable(_irq, _enabled);
22+
#[cfg(not(feature = "irq"))]
23+
unimplemented!();
24+
}
25+
26+
fn irq_register(_irq: usize, _handler: IrqHandler) -> bool {
27+
#[cfg(feature = "irq")]
28+
{
29+
irq::register(_irq, _handler)
30+
}
31+
#[cfg(not(feature = "irq"))]
32+
{
33+
unimplemented!()
34+
}
35+
}
36+
}
37+
}

modules/axmm/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,19 @@ pub fn init_memory_management() {
112112
pub fn init_memory_management_secondary() {
113113
unsafe { axhal::asm::write_kernel_page_table(kernel_page_table_root()) };
114114
}
115+
116+
/// Maps a physical memory region to virtual address space for device access.
117+
pub fn iomap(addr: PhysAddr, size: usize) -> AxResult<VirtAddr> {
118+
let virt = phys_to_virt(addr);
119+
120+
let virt_aligned = virt.align_down_4k();
121+
let addr_aligned = addr.align_down_4k();
122+
let size_aligned = (addr + size).align_up_4k() - addr_aligned;
123+
124+
let flags = MappingFlags::DEVICE | MappingFlags::READ | MappingFlags::WRITE;
125+
let mut tb = kernel_aspace().lock();
126+
tb.map_linear(virt_aligned, addr_aligned, size_aligned, flags)?;
127+
// flush TLB
128+
tb.protect(virt_aligned, size_aligned, flags)?;
129+
Ok(virt)
130+
}

0 commit comments

Comments
 (0)