Skip to content

Commit

Permalink
api: call arceos_api in axstd
Browse files Browse the repository at this point in the history
TODO: sync, fs
  • Loading branch information
equation314 committed Aug 4, 2023
1 parent 849d173 commit 29c68a5
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 115 deletions.
9 changes: 1 addition & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions api/arceos_api/src/imp/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub use axdisplay::DisplayInfo as AxDisplayInfo;

/// Gets the framebuffer information.
pub fn ax_framebuffer_info() -> AxDisplayInfo {
axdisplay::framebuffer_info()
}

/// Flushes the framebuffer, i.e. show on the screen.
pub fn ax_framebuffer_flush() {
axdisplay::framebuffer_flush()
}
5 changes: 5 additions & 0 deletions api/arceos_api/src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ cfg_net! {
pub use net::*;
}

cfg_display! {
mod display;
pub use display::*;
}

mod stdio {
use core::fmt;

Expand Down
15 changes: 15 additions & 0 deletions api/arceos_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#![feature(strict_provenance)]
#![feature(doc_auto_cfg)]
#![feature(doc_cfg)]
#![allow(unused_imports)]

#[cfg(any(
feature = "alloc",
Expand Down Expand Up @@ -193,6 +194,20 @@ pub mod net {
}
}

/// Graphics manipulation operations.
pub mod display {
define_api_type! {
@cfg "display";
pub type AxDisplayInfo;
}

define_api! {
@cfg "display";
pub fn ax_framebuffer_info() -> AxDisplayInfo;
pub fn ax_framebuffer_flush();
}
}

pub mod io {
define_api_type! {
pub type AxPollState;
Expand Down
4 changes: 4 additions & 0 deletions api/arceos_api/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ macro_rules! cfg_net {
($($item:item)*) => { _cfg_common!{ "net" $($item)* } }
}

macro_rules! cfg_display {
($($item:item)*) => { _cfg_common!{ "display" $($item)* } }
}

macro_rules! cfg_task {
($($item:item)*) => { _cfg_common!{ "multitask" $($item)* } }
}
8 changes: 6 additions & 2 deletions apps/display/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use embedded_graphics::pixelcolor::Rgb888;
use embedded_graphics::prelude::{RgbColor, Size};
use embedded_graphics::{draw_target::DrawTarget, prelude::OriginDimensions};

pub use axstd::os::arceos::axdisplay::{framebuffer_flush, framebuffer_info, DisplayInfo};
use axstd::os::arceos::api::display as api;

pub struct Display {
size: Size,
Expand All @@ -11,12 +11,16 @@ pub struct Display {

impl Display {
pub fn new() -> Self {
let info = framebuffer_info();
let info = api::ax_framebuffer_info();
let fb =
unsafe { core::slice::from_raw_parts_mut(info.fb_base_vaddr as *mut u8, info.fb_size) };
let size = Size::new(info.width, info.height);
Self { size, fb }
}

pub fn flush(&self) {
api::ax_framebuffer_flush();
}
}

impl OriginDimensions for Display {
Expand Down
7 changes: 3 additions & 4 deletions apps/display/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
extern crate axstd as std;

mod display;
use display::*;

use self::display::Display;
use embedded_graphics::{
mono_font::{ascii::FONT_10X20, MonoTextStyle},
pixelcolor::Rgb888,
Expand Down Expand Up @@ -67,15 +67,14 @@ impl DrawingBoard {
}
}

fn test_gpu() -> i32 {
fn test_gpu() {
let mut board = DrawingBoard::new();
board.disp.clear(Rgb888::BLACK).unwrap();
for _ in 0..5 {
board.latest_pos.x += RECT_SIZE as i32 + 20;
board.paint();
framebuffer_flush();
board.disp.flush();
}
0
}

#[cfg_attr(feature = "axstd", no_mangle)]
Expand Down
18 changes: 13 additions & 5 deletions apps/task/parallel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ use std::thread;
use std::{sync::Arc, vec::Vec};

#[cfg(feature = "axstd")]
use std::os::arceos::axtask::WaitQueue;
use std::os::arceos::api::task::{self as api, AxWaitQueueHandle};

const NUM_DATA: usize = 2_000_000;
const NUM_TASKS: usize = 16;

#[cfg(feature = "axstd")]
fn barrier() {
use std::sync::atomic::{AtomicUsize, Ordering};
static BARRIER_WQ: WaitQueue = WaitQueue::new();
static BARRIER_WQ: AxWaitQueueHandle = AxWaitQueueHandle::new();
static BARRIER_COUNT: AtomicUsize = AtomicUsize::new(0);

BARRIER_COUNT.fetch_add(1, Ordering::Relaxed);
BARRIER_WQ.wait_until(|| BARRIER_COUNT.load(Ordering::Relaxed) == NUM_TASKS);
BARRIER_WQ.notify_all(true); // wakeup all
api::ax_wait_queue_wait(
&BARRIER_WQ,
|| BARRIER_COUNT.load(Ordering::Relaxed) == NUM_TASKS,
None,
);
api::ax_wait_queue_wake(&BARRIER_WQ, u32::MAX); // wakeup all
}

#[cfg(not(feature = "axstd"))]
Expand Down Expand Up @@ -56,7 +60,11 @@ fn main() {
#[cfg(feature = "axstd")]
{
// equals to sleep(500ms)
let timeout = WaitQueue::new().wait_timeout(std::time::Duration::from_millis(500));
let timeout = api::ax_wait_queue_wait(
&AxWaitQueueHandle::new(),
|| false,
Some(std::time::Duration::from_millis(500)),
);
assert!(timeout);
}

Expand Down
6 changes: 3 additions & 3 deletions apps/task/priority/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{thread, time};
use std::{vec, vec::Vec};

#[cfg(any(feature = "axstd", target_os = "arceos"))]
use std::os::arceos::axtask;
use std::os::arceos::api::task::ax_set_current_priority;

struct TaskParam {
data_len: usize,
Expand Down Expand Up @@ -62,7 +62,7 @@ fn load(n: &u64) -> u64 {
#[cfg_attr(feature = "axstd", no_mangle)]
fn main() {
#[cfg(feature = "axstd")]
axtask::set_priority(-20);
ax_set_current_priority(-20).ok();

let data = (0..PAYLOAD_KIND)
.map(|i| Arc::new(vec![TASK_PARAMS[i].value; TASK_PARAMS[i].data_len]))
Expand All @@ -80,7 +80,7 @@ fn main() {
let nice = TASK_PARAMS[i].nice;
tasks.push(thread::spawn(move || {
#[cfg(feature = "axstd")]
axtask::set_priority(nice);
ax_set_current_priority(nice).ok();

let left = 0;
let right = data_len;
Expand Down
26 changes: 8 additions & 18 deletions ulib/axstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,31 @@ smp = ["axfeat/smp"]
fp_simd = ["axfeat/fp_simd"]

# Interrupts
irq = ["axfeat/irq"]
irq = ["arceos_api/irq", "axfeat/irq"]

# Memory
alloc = ["dep:axalloc", "axfeat/alloc", "axio/alloc"]
alloc = ["arceos_api/alloc", "axfeat/alloc", "axio/alloc"]
alloc-tlsf = ["axfeat/alloc-tlsf"]
alloc-slab = ["axfeat/alloc-slab"]
alloc-buddy = ["axfeat/alloc-buddy"]
paging = ["axfeat/paging"]

# Multi-threading and scheduler
multitask = ["axtask/multitask", "axsync/multitask", "axfeat/multitask"]
multitask = ["arceos_api/multitask", "axsync/multitask", "axfeat/multitask"]
sched_fifo = ["axfeat/sched_fifo"]
sched_rr = ["axfeat/sched_rr"]
sched_cfs = ["axfeat/sched_cfs"]

# File system
fs = ["dep:axfs", "axfeat/fs"]
fs = ["dep:axfs", "arceos_api/fs", "axfeat/fs"]
myfs = ["axfeat/myfs"]

# Networking
net = ["dep:axnet", "axfeat/net"]
net = ["arceos_api/net", "axfeat/net"]
dns = []

# Display
display = ["dep:axdisplay", "axfeat/display"]
display = ["arceos_api/display", "axfeat/display"]

# Device drivers
bus-mmio = ["axfeat/bus-mmio"]
Expand All @@ -68,20 +68,10 @@ log-level-debug = ["axfeat/log-level-debug"]
log-level-trace = ["axfeat/log-level-trace"]

[dependencies]
# ArceOS modules
axfeat = { path = "../../api/axfeat" }
axruntime = { path = "../../modules/axruntime" }
axhal = { path = "../../modules/axhal" }
axlog = { path = "../../modules/axlog" }
axconfig = { path = "../../modules/axconfig" }
axalloc = { path = "../../modules/axalloc", optional = true }
axfs = { path = "../../modules/axfs", optional = true }
axnet = { path = "../../modules/axnet", optional = true }
axdisplay = { path = "../../modules/axdisplay", optional = true }
axsync = { path = "../../modules/axsync", optional = true }
axtask = { path = "../../modules/axtask", optional = true }

# Other crates
axfeat = { path = "../../api/axfeat" }
arceos_api = { path = "../../api/arceos_api" }
axio = { path = "../../crates/axio" }
axerrno = { path = "../../crates/axerrno" }
spinlock = { path = "../../crates/spinlock" }
4 changes: 2 additions & 2 deletions ulib/axstd/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use {crate::io, alloc::string::String};
/// Returns the current working directory as a [`String`].
#[cfg(feature = "fs")]
pub fn current_dir() -> io::Result<String> {
axfs::api::current_dir()
arceos_api::fs::ax_current_dir()
}

/// Changes the current working directory to the specified path.
#[cfg(feature = "fs")]
pub fn set_current_dir(path: &str) -> io::Result<()> {
axfs::api::set_current_dir(path)
arceos_api::fs::ax_set_current_dir(path)
}
7 changes: 3 additions & 4 deletions ulib/axstd/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Read for StdinRaw {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let mut read_len = 0;
while read_len < buf.len() {
if let Some(c) = axhal::console::getchar() {
if let Some(c) = arceos_api::stdio::ax_console_read_byte() {
buf[read_len] = c;
read_len += 1;
} else {
Expand All @@ -25,8 +25,7 @@ impl Read for StdinRaw {

impl Write for StdoutRaw {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
axhal::console::write_bytes(buf);
Ok(buf.len())
arceos_api::stdio::ax_console_write_bytes(buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
Expand Down Expand Up @@ -167,7 +166,7 @@ pub fn __print_impl(args: core::fmt::Arguments) {
if cfg!(feature = "smp") {
// synchronize using the lock in axlog, to avoid interleaving
// with kernel logs
axlog::print_fmt(args).unwrap();
arceos_api::stdio::ax_console_write_fmt(args).unwrap();
} else {
stdout().lock().write_fmt(args).unwrap();
}
Expand Down
3 changes: 0 additions & 3 deletions ulib/axstd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@
#![feature(doc_auto_cfg)]
#![feature(ip_in_core)]

#[cfg(not(test))]
extern crate axruntime;

#[cfg(feature = "alloc")]
extern crate alloc;

Expand Down
5 changes: 2 additions & 3 deletions ulib/axstd/src/net/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ mod dns {
return Ok(vec![SocketAddr::V4(addr)].into_iter());
}

Ok(axnet::dns_query(host)?
Ok(arceos_api::net::ax_dns_query(host)?
.into_iter()
.map(|ip| SocketAddr::new(ip, port))
.collect::<Vec<_>>()
Expand All @@ -165,8 +165,7 @@ mod dns {
let port: u16 = port_str
.parse()
.map_err(|_| axerrno::ax_err_type!(InvalidInput, "invalid port value"))?;

Ok(axnet::dns_query(host)?
Ok(arceos_api::net::ax_dns_query(host)?
.into_iter()
.map(|ip| SocketAddr::new(ip, port))
.collect::<Vec<_>>()
Expand Down
Loading

0 comments on commit 29c68a5

Please sign in to comment.