From 6b0d75315ffd24eba62cd24a6f985746bf332589 Mon Sep 17 00:00:00 2001 From: lhw2002426 <1466397747@qq.com> Date: Wed, 21 Aug 2024 09:41:03 +0800 Subject: [PATCH 01/15] add loopback --- apps/c/httpclient/expect_info.out | 7 +- crates/driver_net/Cargo.toml | 1 + crates/driver_net/src/lib.rs | 3 + crates/driver_net/src/loopback.rs | 131 +++++++++++++++++++++++++ crates/lwip_rust/build.rs | 5 +- crates/lwip_rust/custom/lwipopts.h | 4 +- modules/ruxdriver/Cargo.toml | 1 + modules/ruxdriver/build.rs | 2 +- modules/ruxdriver/src/drivers.rs | 17 ++++ modules/ruxdriver/src/macros.rs | 6 ++ modules/ruxfs/Cargo.toml | 2 +- modules/ruxfs/tests/test_fatfs.rs | 4 +- modules/ruxfs/tests/test_ramfs.rs | 4 +- modules/ruxnet/Cargo.toml | 3 +- modules/ruxnet/src/lib.rs | 9 +- modules/ruxnet/src/lwip_impl/driver.rs | 104 +++++++++++--------- modules/ruxnet/src/lwip_impl/mod.rs | 2 +- modules/ruxnet/src/smoltcp_impl/dns.rs | 9 +- modules/ruxnet/src/smoltcp_impl/mod.rs | 85 ++++++++++++---- modules/ruxnet/src/smoltcp_impl/tcp.rs | 13 ++- 20 files changed, 327 insertions(+), 85 deletions(-) create mode 100644 crates/driver_net/src/loopback.rs diff --git a/apps/c/httpclient/expect_info.out b/apps/c/httpclient/expect_info.out index 19688d533..35cf49e11 100644 --- a/apps/c/httpclient/expect_info.out +++ b/apps/c/httpclient/expect_info.out @@ -17,7 +17,12 @@ Initialize platform devices... Initialize device drivers... registered a new Net device at .\+: "virtio-net" Initialize network subsystem... - use NIC 0: "virtio-net" + net stack: smoltcp + use NIC: "loopback" +created net interface "loopback": + ether: 00-00-00-00-00-00 + ip: 127.0.0.1/24 + use NIC: "virtio-net" created net interface "eth0": ether: 52-54-00-12-34-56 ip: 10.0.2.15/24 diff --git a/crates/driver_net/Cargo.toml b/crates/driver_net/Cargo.toml index 5018c4a7c..dfd40e509 100644 --- a/crates/driver_net/Cargo.toml +++ b/crates/driver_net/Cargo.toml @@ -11,6 +11,7 @@ documentation = "https://rcore-os.github.io/arceos/driver_net/index.html" [features] default = [] +loopback = [] ixgbe = ["dep:ixgbe-driver"] [dependencies] diff --git a/crates/driver_net/src/lib.rs b/crates/driver_net/src/lib.rs index 6fd20c600..ad8536061 100644 --- a/crates/driver_net/src/lib.rs +++ b/crates/driver_net/src/lib.rs @@ -20,6 +20,9 @@ use alloc::sync::Arc; #[cfg(feature = "ixgbe")] /// ixgbe NIC device driver. pub mod ixgbe; +#[cfg(feature = "loopback")] +/// loopback device driver +pub mod loopback; mod net_buf; use core::ptr::NonNull; diff --git a/crates/driver_net/src/loopback.rs b/crates/driver_net/src/loopback.rs new file mode 100644 index 000000000..3574cee75 --- /dev/null +++ b/crates/driver_net/src/loopback.rs @@ -0,0 +1,131 @@ +/* Copyright (c) [2023] [Syswonder Community] +* [Ruxos] is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ +use crate::{EthernetAddress, NetBuf, NetBufBox, NetBufPool, NetBufPtr, NetDriverOps}; +use alloc::collections::VecDeque; +use alloc::sync::Arc; +use driver_common::{BaseDriverOps, DevError, DevResult, DeviceType}; + +extern crate alloc; + +const NET_BUF_LEN: usize = 1526; + +/// The VirtIO network device driver. +/// +/// `QS` is the VirtIO queue size. +pub struct LoopbackDevice { + mac_address: EthernetAddress, + pub(crate) queue: VecDeque, + buf_pool: Arc, +} + +unsafe impl Send for LoopbackDevice {} +unsafe impl Sync for LoopbackDevice {} + +impl LoopbackDevice { + /// Creates a new driver instance and initializes the device + pub fn new(mac_address: Option<[u8; 6]>) -> Self { + let buf_pool = match NetBufPool::new(1024, NET_BUF_LEN) { + Ok(pool) => pool, + Err(_) => { + panic!("fail to create netbufpool"); + } + }; + Self { + mac_address: match mac_address { + Some(address) => EthernetAddress(address), + None => EthernetAddress([0; 6]), + }, + queue: VecDeque::new(), + buf_pool: buf_pool, + } + } +} + +impl BaseDriverOps for LoopbackDevice { + fn device_name(&self) -> &str { + "loopback" + } + + fn device_type(&self) -> DeviceType { + DeviceType::Net + } +} + +use log::info; + +impl NetDriverOps for LoopbackDevice { + #[inline] + fn mac_address(&self) -> EthernetAddress { + EthernetAddress(self.mac_address.0) + } + + #[inline] + fn can_transmit(&self) -> bool { + true + } + + #[inline] + fn can_receive(&self) -> bool { + !self.queue.is_empty() + } + + #[inline] + fn rx_queue_size(&self) -> usize { + self.queue.len() + } + + #[inline] + fn tx_queue_size(&self) -> usize { + self.queue.len() + } + + fn fill_rx_buffers(&mut self, buf_pool: &Arc) -> DevResult { + Ok(()) + } + + fn recycle_rx_buffer(&mut self, rx_buf: NetBufPtr) -> DevResult { + Ok(()) + } + + fn recycle_tx_buffers(&mut self) -> DevResult { + Ok(()) + } + + fn prepare_tx_buffer(&self, tx_buf: &mut NetBuf, pkt_len: usize) -> DevResult { + Ok(()) + } + + fn transmit(&mut self, tx_buf: NetBufPtr) -> DevResult { + unsafe { self.queue.push_back(NetBuf::from_buf_ptr(tx_buf)) } + Ok(()) + } + + fn receive(&mut self) -> DevResult { + if let Some(token) = self.queue.pop_front() { + Ok(token.into_buf_ptr()) + } else { + Err(DevError::Again) + } + } + + fn alloc_tx_buffer(&mut self, size: usize) -> DevResult { + let mut net_buf = self.buf_pool.alloc_boxed().ok_or(DevError::NoMemory)?; + let pkt_len = size; + + // 1. Check if the buffer is large enough. + let hdr_len = net_buf.header_len(); + if hdr_len + pkt_len > net_buf.capacity() { + return Err(DevError::InvalidParam); + } + net_buf.set_packet_len(pkt_len); + + // 2. Return the buffer. + Ok(net_buf.into_buf_ptr()) + } +} diff --git a/crates/lwip_rust/build.rs b/crates/lwip_rust/build.rs index 1fcdf0c3e..fb85d3674 100644 --- a/crates/lwip_rust/build.rs +++ b/crates/lwip_rust/build.rs @@ -1,5 +1,3 @@ -use std::path::PathBuf; - fn main() { println!("cargo:rustc-link-lib=lwip"); println!("cargo:rerun-if-changed=custom"); @@ -28,9 +26,8 @@ fn generate_lwip_bindings() { .generate() .expect("Unable to generate bindings"); - let out_path = PathBuf::from("src"); bindings - .write_to_file(out_path.join("bindings.rs")) + .write_to_file("src/bindings.rs") .expect("Couldn't write bindings!"); } diff --git a/crates/lwip_rust/custom/lwipopts.h b/crates/lwip_rust/custom/lwipopts.h index fefe19fce..f8f519f02 100644 --- a/crates/lwip_rust/custom/lwipopts.h +++ b/crates/lwip_rust/custom/lwipopts.h @@ -25,8 +25,8 @@ #define LWIP_TCP 1 #define LWIP_CALLBACK_API 1 #define LWIP_NETIF_API 0 -#define LWIP_NETIF_LOOPBACK 0 -#define LWIP_HAVE_LOOPIF 1 +#define LWIP_NETIF_LOOPBACK 1 +#define LWIP_HAVE_LOOPIF 0 #define LWIP_HAVE_SLIPIF 0 #define LWIP_NETCONN 0 #define LWIP_SOCKET 0 diff --git a/modules/ruxdriver/Cargo.toml b/modules/ruxdriver/Cargo.toml index b90a247c6..c4f99e04d 100644 --- a/modules/ruxdriver/Cargo.toml +++ b/modules/ruxdriver/Cargo.toml @@ -32,6 +32,7 @@ virtio-9p = ["_9p","virtio", "driver_virtio/v9p"] ramdisk = ["block", "driver_block/ramdisk"] bcm2835-sdhci = ["block", "driver_block/bcm2835-sdhci"] ixgbe = ["net", "driver_net/ixgbe", "dep:axalloc", "dep:ruxhal"] +loopback = ["driver_net/loopback", "dyn"] # more devices example: e1000 = ["net", "driver_net/e1000"] default = ["bus-mmio"] diff --git a/modules/ruxdriver/build.rs b/modules/ruxdriver/build.rs index 6e1c859a8..b57e90b47 100644 --- a/modules/ruxdriver/build.rs +++ b/modules/ruxdriver/build.rs @@ -7,7 +7,7 @@ * See the Mulan PSL v2 for more details. */ -const NET_DEV_FEATURES: &[&str] = &["ixgbe", "virtio-net"]; +const NET_DEV_FEATURES: &[&str] = &["ixgbe", "virtio-net", "loopback"]; const BLOCK_DEV_FEATURES: &[&str] = &["ramdisk", "bcm2835-sdhci", "virtio-blk"]; const DISPLAY_DEV_FEATURES: &[&str] = &["virtio-gpu"]; const _9P_DEV_FEATURES: &[&str] = &["virtio-9p"]; diff --git a/modules/ruxdriver/src/drivers.rs b/modules/ruxdriver/src/drivers.rs index 7fe1115bc..9a214f390 100644 --- a/modules/ruxdriver/src/drivers.rs +++ b/modules/ruxdriver/src/drivers.rs @@ -42,6 +42,23 @@ pub trait DriverProbe { } } +cfg_if::cfg_if! { + if #[cfg(net_dev = "loopback")] + { + pub struct LoopbackDriver; + register_net_driver!(LoopbackDriver, driver_net::loopback::LoopbackDevice); + + impl DriverProbe for LoopbackDriver { + fn probe_global() -> Option { + debug!("mmc probe"); + Some(AxDeviceEnum::from_net( + driver_net::loopback::LoopbackDevice::new(None), + )) + } + } + } +} + #[cfg(net_dev = "virtio-net")] register_net_driver!( ::Driver, diff --git a/modules/ruxdriver/src/macros.rs b/modules/ruxdriver/src/macros.rs index e9aa44192..cfd406d9e 100644 --- a/modules/ruxdriver/src/macros.rs +++ b/modules/ruxdriver/src/macros.rs @@ -51,6 +51,12 @@ macro_rules! for_each_drivers { #[allow(unused_imports)] use crate::virtio::{self, VirtIoDevMeta}; + #[cfg(net_dev = "loopback")] + { + type $drv_type = crate::drivers::LoopbackDriver; + $code + } + #[cfg(net_dev = "virtio-net")] { type $drv_type = ::Driver; diff --git a/modules/ruxfs/Cargo.toml b/modules/ruxfs/Cargo.toml index 0ca624616..88e7415d9 100644 --- a/modules/ruxfs/Cargo.toml +++ b/modules/ruxfs/Cargo.toml @@ -55,7 +55,7 @@ features = [ # no std ] [dev-dependencies] -ruxdriver = { path = "../ruxdriver", features = ["block", "ramdisk"] } +ruxdriver = { path = "../ruxdriver", features = ["block", "ramdisk", "dyn"] } driver_block = { path = "../../crates/driver_block", features = ["ramdisk"] } axsync = { path = "../axsync", features = ["multitask"] } ruxtask = { path = "../ruxtask", features = ["test"] } diff --git a/modules/ruxfs/tests/test_fatfs.rs b/modules/ruxfs/tests/test_fatfs.rs index da542c00a..88d844882 100644 --- a/modules/ruxfs/tests/test_fatfs.rs +++ b/modules/ruxfs/tests/test_fatfs.rs @@ -33,7 +33,9 @@ fn test_fatfs() { // By default, mount_points[0] will be rootfs let mut mount_points: Vec = Vec::new(); // setup and initialize blkfs as one mountpoint for rootfs - mount_points.push(ruxfs::init_blkfs(AxDeviceContainer::from_one(disk))); + mount_points.push(ruxfs::init_blkfs(AxDeviceContainer::from_one(Box::new( + disk, + )))); ruxfs::prepare_commonfs(&mut mount_points); // setup and initialize rootfs diff --git a/modules/ruxfs/tests/test_ramfs.rs b/modules/ruxfs/tests/test_ramfs.rs index da93bff19..61c57a45d 100644 --- a/modules/ruxfs/tests/test_ramfs.rs +++ b/modules/ruxfs/tests/test_ramfs.rs @@ -58,9 +58,9 @@ fn test_ramfs() { // By default, mount_points[0] will be rootfs let mut mount_points: Vec = Vec::new(); // setup and initialize blkfs as one mountpoint for rootfs - mount_points.push(ruxfs::init_blkfs(AxDeviceContainer::from_one( + mount_points.push(ruxfs::init_blkfs(AxDeviceContainer::from_one(Box::new( RamDisk::default(), - ))); + )))); ruxfs::prepare_commonfs(&mut mount_points); // setup and initialize rootfs diff --git a/modules/ruxnet/Cargo.toml b/modules/ruxnet/Cargo.toml index 3c81a2510..bfc19c5f5 100644 --- a/modules/ruxnet/Cargo.toml +++ b/modules/ruxnet/Cargo.toml @@ -11,8 +11,9 @@ documentation = "https://rcore-os.github.io/arceos/ruxnet/index.html" [features] lwip = ["dep:lwip_rust"] +loopback = ["ruxdriver/loopback"] smoltcp = [] -default = ["smoltcp"] +default = ["smoltcp", "loopback"] [dependencies] log = "0.4" diff --git a/modules/ruxnet/src/lib.rs b/modules/ruxnet/src/lib.rs index a8414b105..f7304e199 100644 --- a/modules/ruxnet/src/lib.rs +++ b/modules/ruxnet/src/lib.rs @@ -63,8 +63,6 @@ use ruxdriver::{prelude::*, AxDeviceContainer}; pub fn init_network(mut net_devs: AxDeviceContainer) { info!("Initialize network subsystem..."); - let dev = net_devs.take_one().expect("No NIC device found!"); - info!(" use NIC 0: {:?}", dev.device_name()); cfg_if::cfg_if! { if #[cfg(feature = "lwip")] { info!(" net stack: lwip"); @@ -74,5 +72,10 @@ pub fn init_network(mut net_devs: AxDeviceContainer) { compile_error!("No network stack is selected"); } } - net_impl::init(dev); + net_impl::init(); + while !net_devs.is_empty() { + let dev = net_devs.take_one().expect("No NIC device found!"); + info!(" use NIC: {:?}", dev.device_name()); + net_impl::init_netdev(dev); + } } diff --git a/modules/ruxnet/src/lwip_impl/driver.rs b/modules/ruxnet/src/lwip_impl/driver.rs index 164ea7289..70c546bdd 100644 --- a/modules/ruxnet/src/lwip_impl/driver.rs +++ b/modules/ruxnet/src/lwip_impl/driver.rs @@ -3,7 +3,7 @@ use crate::{ net_impl::addr::{mask_to_prefix, MacAddr}, IpAddr, }; -use alloc::{boxed::Box, collections::VecDeque, sync::Arc}; +use alloc::{boxed::Box, collections::VecDeque, sync::Arc, vec}; #[cfg(feature = "irq")] use axdriver::register_interrupt_handler; use axsync::Mutex; @@ -12,8 +12,8 @@ use driver_net::{DevError, NetBuf, NetBufBox, NetBufPool, NetBufPtr}; use lazy_init::LazyInit; use lwip_rust::bindings::{ err_enum_t_ERR_MEM, err_enum_t_ERR_OK, err_t, etharp_output, ethernet_input, ip4_addr_t, - lwip_htonl, lwip_init, netif, netif_add, netif_set_default, netif_set_link_up, netif_set_up, - pbuf, pbuf_free, rx_custom_pbuf_alloc, rx_custom_pbuf_free, rx_custom_pbuf_init, + lwip_htonl, lwip_init, netif, netif_add, netif_poll, netif_set_default, netif_set_link_up, + netif_set_up, pbuf, pbuf_free, rx_custom_pbuf_alloc, rx_custom_pbuf_free, rx_custom_pbuf_init, rx_custom_pbuf_t, sys_check_timeouts, NETIF_FLAG_BROADCAST, NETIF_FLAG_ETHARP, NETIF_FLAG_ETHERNET, }; @@ -203,6 +203,9 @@ static ETH0: LazyInit = LazyInit::new(); /// packets to the NIC. pub fn poll_interfaces() { ETH0.poll(); + unsafe { + netif_poll(&mut ETH0.netif.lock().0); + } } fn ip4_addr_gen(a: u8, b: u8, c: u8, d: u8) -> ip4_addr_t { @@ -212,58 +215,67 @@ fn ip4_addr_gen(a: u8, b: u8, c: u8, d: u8) -> ip4_addr_t { }, } } +pub fn init() {} -pub fn init(mut net_dev: AxNetDevice) { - LWIP_MUTEX.init_by(Mutex::new(0)); - let _guard = LWIP_MUTEX.lock(); - - let ipaddr: ip4_addr_t = ip4_addr_gen(10, 0, 2, 15); // QEMU user networking default IP - let netmask: ip4_addr_t = ip4_addr_gen(255, 255, 255, 0); - let gw: ip4_addr_t = ip4_addr_gen(10, 0, 2, 2); // QEMU user networking gateway - - let dev = net_dev; - let mut netif: netif = unsafe { core::mem::zeroed() }; - netif.hwaddr_len = 6; - netif.hwaddr = dev.mac_address().0; - - ETH0.init_by(InterfaceWrapper { - name: "eth0", - dev: Arc::new(Mutex::new(DeviceWrapper::new(dev))), - netif: Mutex::new(NetifWrapper(netif)), - }); +pub fn init_netdev(mut net_dev: AxNetDevice) { + match net_dev.device_name() { + "loopback" => { + info!("use lwip netif loopback"); + } + _ => { + LWIP_MUTEX.init_by(Mutex::new(0)); + let _guard = LWIP_MUTEX.lock(); + + let ipaddr: ip4_addr_t = ip4_addr_gen(10, 0, 2, 15); // QEMU user networking default IP + let netmask: ip4_addr_t = ip4_addr_gen(255, 255, 255, 0); + let gw: ip4_addr_t = ip4_addr_gen(10, 0, 2, 2); // QEMU user networking gateway + + let dev = net_dev; + let mut netif: netif = unsafe { core::mem::zeroed() }; + netif.hwaddr_len = 6; + netif.hwaddr = dev.mac_address().0; + + ETH0.init_by(InterfaceWrapper { + name: "eth0", + dev: Arc::new(Mutex::new(DeviceWrapper::new(dev))), + netif: Mutex::new(NetifWrapper(netif)), + }); + + unsafe { + lwip_init(); + rx_custom_pbuf_init(); + netif_add( + &mut ETH0.netif.lock().0, + &ipaddr, + &netmask, + &gw, + Ð0 as *const _ as *mut c_void, + Some(ethif_init), + Some(ethernet_input), + ); + netif_set_link_up(&mut ETH0.netif.lock().0); + netif_set_up(&mut ETH0.netif.lock().0); + netif_set_default(&mut ETH0.netif.lock().0); + } - unsafe { - lwip_init(); - rx_custom_pbuf_init(); - netif_add( - &mut ETH0.netif.lock().0, - &ipaddr, - &netmask, - &gw, - Ð0 as *const _ as *mut c_void, - Some(ethif_init), - Some(ethernet_input), - ); - netif_set_link_up(&mut ETH0.netif.lock().0); - netif_set_up(&mut ETH0.netif.lock().0); - netif_set_default(&mut ETH0.netif.lock().0); + info!("created net interface {:?}:", ETH0.name()); + info!( + " ether: {}", + MacAddr::from_bytes(Ð0.netif.lock().0.hwaddr) + ); + let ip = IpAddr::from(ETH0.netif.lock().0.ip_addr); + let mask = mask_to_prefix(IpAddr::from(ETH0.netif.lock().0.netmask)).unwrap(); + info!(" ip: {}/{}", ip, mask); + info!(" gateway: {}", IpAddr::from(ETH0.netif.lock().0.gw)); + } } - - info!("created net interface {:?}:", ETH0.name()); - info!( - " ether: {}", - MacAddr::from_bytes(Ð0.netif.lock().0.hwaddr) - ); - let ip = IpAddr::from(ETH0.netif.lock().0.ip_addr); - let mask = mask_to_prefix(IpAddr::from(ETH0.netif.lock().0.netmask)).unwrap(); - info!(" ip: {}/{}", ip, mask); - info!(" gateway: {}", IpAddr::from(ETH0.netif.lock().0.gw)); } pub fn lwip_loop_once() { let guard = LWIP_MUTEX.lock(); unsafe { ETH0.poll(); + netif_poll(&mut ETH0.netif.lock().0); sys_check_timeouts(); } drop(guard); diff --git a/modules/ruxnet/src/lwip_impl/mod.rs b/modules/ruxnet/src/lwip_impl/mod.rs index ccc72eca6..87084c607 100644 --- a/modules/ruxnet/src/lwip_impl/mod.rs +++ b/modules/ruxnet/src/lwip_impl/mod.rs @@ -6,7 +6,7 @@ mod udp; pub use self::addr::{IpAddr, Ipv4Addr, SocketAddr}; pub use self::dns::{dns_query, resolve_socket_addr}; -pub use self::driver::{init, poll_interfaces}; +pub use self::driver::{init, init_netdev, poll_interfaces}; pub use self::tcp::TcpSocket; pub use self::udp::UdpSocket; use core::ffi::c_uint; diff --git a/modules/ruxnet/src/smoltcp_impl/dns.rs b/modules/ruxnet/src/smoltcp_impl/dns.rs index 00b7c0575..53e7db29f 100644 --- a/modules/ruxnet/src/smoltcp_impl/dns.rs +++ b/modules/ruxnet/src/smoltcp_impl/dns.rs @@ -16,7 +16,7 @@ use smoltcp::socket::dns::{self, GetQueryResultError, StartQueryError}; use smoltcp::wire::DnsQueryType; use super::addr::into_core_ipaddr; -use super::{SocketSetWrapper, ETH0, SOCKET_SET}; +use super::{SocketSetWrapper, IFACE_LIST, SOCKET_SET}; /// A DNS socket. struct DnsSocket { @@ -44,7 +44,12 @@ impl DnsSocket { pub fn query(&self, name: &str, query_type: DnsQueryType) -> AxResult> { // let local_addr = self.local_addr.unwrap_or_else(f); let handle = self.handle.ok_or_else(|| ax_err_type!(InvalidInput))?; - let iface = Ð0.iface; + let binding = IFACE_LIST.lock(); + let iface = &binding + .iter() + .find(|iface| iface.name() == "eth0") + .unwrap() + .iface; let query_handle = SOCKET_SET .with_socket_mut::(handle, |socket| { socket.start_query(iface.lock().context(), name, query_type) diff --git a/modules/ruxnet/src/smoltcp_impl/mod.rs b/modules/ruxnet/src/smoltcp_impl/mod.rs index da911f2f7..192fe2d93 100644 --- a/modules/ruxnet/src/smoltcp_impl/mod.rs +++ b/modules/ruxnet/src/smoltcp_impl/mod.rs @@ -14,6 +14,7 @@ mod listen_table; mod tcp; mod udp; +use alloc::string::{String, ToString}; use alloc::vec; use core::cell::RefCell; use core::ops::DerefMut; @@ -35,6 +36,8 @@ pub use self::dns::dns_query; pub use self::tcp::TcpSocket; pub use self::udp::UdpSocket; +pub use driver_net::loopback::LoopbackDevice; + macro_rules! env_or_default { ($key:literal) => { match option_env!($key) { @@ -61,7 +64,15 @@ const LISTEN_QUEUE_SIZE: usize = 512; static LISTEN_TABLE: LazyInit = LazyInit::new(); static SOCKET_SET: LazyInit = LazyInit::new(); -static ETH0: LazyInit = LazyInit::new(); +static IFACE_LIST: LazyInit>> = LazyInit::new(); + +fn route_dev(addr: [u8; 4]) -> String { + if addr[0] == 127 { + "loopback".to_string() + } else { + "eth0".to_string() + } +} struct SocketSetWrapper<'a>(Mutex>); @@ -129,7 +140,9 @@ impl<'a> SocketSetWrapper<'a> { } pub fn poll_interfaces(&self) { - ETH0.poll(&self.0); + for iface in IFACE_LIST.lock().iter() { + iface.poll(&self.0); + } } pub fn remove(&self, handle: SocketHandle) { @@ -311,29 +324,65 @@ pub fn poll_interfaces() { /// Benchmark raw socket transmit bandwidth. pub fn bench_transmit() { - ETH0.dev.lock().bench_transmit_bandwidth(); + IFACE_LIST + .lock() + .iter() + .find(|iface| iface.name() == "eth0") + .unwrap() + .dev + .lock() + .bench_transmit_bandwidth(); } /// Benchmark raw socket receive bandwidth. pub fn bench_receive() { - ETH0.dev.lock().bench_receive_bandwidth(); + IFACE_LIST + .lock() + .iter() + .find(|iface| iface.name() == "eth0") + .unwrap() + .dev + .lock() + .bench_receive_bandwidth(); } -pub(crate) fn init(net_dev: AxNetDevice) { - let ether_addr = EthernetAddress(net_dev.mac_address().0); - let eth0 = InterfaceWrapper::new("eth0", net_dev, ether_addr); - - let ip = IP.parse().expect("invalid IP address"); - let gateway = GATEWAY.parse().expect("invalid gateway IP address"); - eth0.setup_ip_addr(ip, IP_PREFIX); - eth0.setup_gateway(gateway); +pub(crate) fn init() { + let mut socketset = SocketSetWrapper::new(); - ETH0.init_by(eth0); - SOCKET_SET.init_by(SocketSetWrapper::new()); + IFACE_LIST.init_by(Mutex::new(vec::Vec::new())); + SOCKET_SET.init_by(socketset); LISTEN_TABLE.init_by(ListenTable::new()); +} + +pub(crate) fn init_netdev(net_dev: AxNetDevice) { + match net_dev.device_name() { + "loopback" => { + let ether_addr = EthernetAddress(net_dev.mac_address().0); + let lo = InterfaceWrapper::new("loopback", net_dev, ether_addr); - info!("created net interface {:?}:", ETH0.name()); - info!(" ether: {}", ETH0.ethernet_address()); - info!(" ip: {}/{}", ip, IP_PREFIX); - info!(" gateway: {}", gateway); + let ip = "127.0.0.1".parse().expect("invalid IP address"); + lo.setup_ip_addr(ip, IP_PREFIX); + + info!("created net interface {:?}:", lo.name()); + info!(" ether: {}", lo.ethernet_address()); + info!(" ip: {}/{}", "127.0.0.1", IP_PREFIX); + IFACE_LIST.lock().push(lo); + } + _ => { + let ether_addr = EthernetAddress(net_dev.mac_address().0); + let eth0 = InterfaceWrapper::new("eth0", net_dev, ether_addr); + + let ip = IP.parse().expect("invalid IP address"); + let gateway = GATEWAY.parse().expect("invalid gateway IP address"); + eth0.setup_ip_addr(ip, IP_PREFIX); + eth0.setup_gateway(gateway); + + info!("created net interface {:?}:", eth0.name()); + info!(" ether: {}", eth0.ethernet_address()); + info!(" ip: {}/{}", ip, IP_PREFIX); + info!(" gateway: {}", gateway); + + IFACE_LIST.lock().push(eth0); + } + } } diff --git a/modules/ruxnet/src/smoltcp_impl/tcp.rs b/modules/ruxnet/src/smoltcp_impl/tcp.rs index de9c14fd5..45978f71e 100644 --- a/modules/ruxnet/src/smoltcp_impl/tcp.rs +++ b/modules/ruxnet/src/smoltcp_impl/tcp.rs @@ -20,7 +20,7 @@ use smoltcp::socket::tcp::{self, ConnectError, State}; use smoltcp::wire::{IpEndpoint, IpListenEndpoint}; use super::addr::{from_core_sockaddr, into_core_sockaddr, is_unspecified, UNSPECIFIED_ENDPOINT}; -use super::{SocketSetWrapper, ETH0, LISTEN_TABLE, SOCKET_SET}; +use super::{route_dev, SocketSetWrapper, IFACE_LIST, LISTEN_TABLE, SOCKET_SET}; // State transitions: // CLOSED -(connect)-> BUSY -> CONNECTING -> CONNECTED -(shutdown)-> BUSY -> CLOSED @@ -139,7 +139,16 @@ impl TcpSocket { // TODO: check remote addr unreachable let remote_endpoint = from_core_sockaddr(remote_addr); let bound_endpoint = self.bound_endpoint()?; - let iface = Ð0.iface; + let binding = IFACE_LIST.lock(); + let iface_name = match remote_addr { + SocketAddr::V4(addr) => route_dev(addr.ip().octets()), + _ => panic!("IPv6 not supported"), + }; + let iface = &binding + .iter() + .find(|iface| iface.name() == iface_name) + .unwrap() + .iface; let (local_endpoint, remote_endpoint) = SOCKET_SET .with_socket_mut::(handle, |socket| { socket From 200226fb0e5e876a8102ed060ee29cd9b2d82ada Mon Sep 17 00:00:00 2001 From: WuZheng Date: Tue, 17 Sep 2024 15:40:15 +0800 Subject: [PATCH 02/15] temporily commit for test --- Cargo.lock | 37 +- Cargo.toml | 1 + Makefile | 8 +- api/ruxfeat/Cargo.toml | 3 +- api/ruxos_posix_api/Cargo.toml | 3 +- api/ruxos_posix_api/src/imp/execve/mod.rs | 12 + api/ruxos_posix_api/src/imp/execve/stack.rs | 31 +- api/ruxos_posix_api/src/imp/fd_ops.rs | 57 +-- api/ruxos_posix_api/src/imp/fs.rs | 276 +++--------- api/ruxos_posix_api/src/imp/io.rs | 6 +- api/ruxos_posix_api/src/imp/io_mpx/epoll.rs | 2 +- api/ruxos_posix_api/src/imp/io_mpx/poll.rs | 3 +- api/ruxos_posix_api/src/imp/io_mpx/select.rs | 3 +- api/ruxos_posix_api/src/imp/ioctl.rs | 3 +- api/ruxos_posix_api/src/imp/mmap/api.rs | 61 ++- api/ruxos_posix_api/src/imp/mmap/trap.rs | 228 ++++++---- api/ruxos_posix_api/src/imp/mmap/utils.rs | 216 ++++------ api/ruxos_posix_api/src/imp/net.rs | 4 +- api/ruxos_posix_api/src/imp/pipe.rs | 2 +- api/ruxos_posix_api/src/imp/pthread/mod.rs | 89 ++-- api/ruxos_posix_api/src/imp/resources.rs | 4 +- api/ruxos_posix_api/src/imp/task.rs | 2 +- modules/ruxdriver/Cargo.toml | 5 + modules/ruxdriver/src/lib.rs | 2 +- modules/ruxdriver/src/virtio.rs | 13 +- modules/ruxfdtable/src/lib.rs | 14 +- modules/ruxfs/Cargo.toml | 13 +- modules/ruxfs/src/fs/fatfs.rs | 16 +- modules/ruxfs/src/lib.rs | 2 +- modules/ruxfs/src/root.rs | 118 ++---- modules/ruxhal/src/arch/aarch64/context.rs | 122 +++++- modules/ruxhal/src/arch/aarch64/trap.S | 2 - modules/ruxhal/src/arch/aarch64/trap.rs | 8 +- modules/ruxhal/src/arch/x86_64/mod.rs | 2 +- modules/ruxhal/src/mem.rs | 16 - modules/ruxhal/src/paging.rs | 122 +----- .../src/platform/aarch64_common/pl011.rs | 2 +- modules/ruxhal/src/trap.rs | 14 +- modules/ruxmm/Cargo.toml | 33 ++ modules/ruxmm/src/lib.rs | 21 + modules/ruxmm/src/mem.rs | 42 ++ modules/ruxmm/src/paging.rs | 138 ++++++ modules/ruxnet/src/smoltcp_impl/tcp.rs | 2 +- modules/ruxruntime/Cargo.toml | 5 +- modules/ruxruntime/src/lib.rs | 14 +- modules/ruxruntime/src/mp.rs | 6 +- modules/ruxtask/Cargo.toml | 36 +- modules/ruxtask/src/api.rs | 25 ++ modules/ruxtask/src/fs.rs | 331 +++++++++++++++ modules/ruxtask/src/lib.rs | 6 +- modules/ruxtask/src/run_queue.rs | 91 ++-- modules/ruxtask/src/task.rs | 395 +++++++++++++++++- modules/ruxtask/src/vma.rs | 197 +++++++++ 53 files changed, 1967 insertions(+), 897 deletions(-) create mode 100644 modules/ruxmm/Cargo.toml create mode 100644 modules/ruxmm/src/lib.rs create mode 100644 modules/ruxmm/src/mem.rs create mode 100644 modules/ruxmm/src/paging.rs create mode 100644 modules/ruxtask/src/fs.rs create mode 100644 modules/ruxtask/src/vma.rs diff --git a/Cargo.lock b/Cargo.lock index 905076383..997c8ce9c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1591,6 +1591,7 @@ version = "0.1.0" dependencies = [ "axalloc", "cfg-if", + "crate_interface", "driver_9p", "driver_block", "driver_common", @@ -1627,6 +1628,7 @@ dependencies = [ "ruxdriver", "ruxfs", "ruxhal", + "ruxmm", "ruxnet", "ruxruntime", "ruxtask", @@ -1643,7 +1645,6 @@ dependencies = [ "axfs_ramfs", "axfs_vfs", "axio", - "axsync", "capability", "cfg-if", "crate_interface", @@ -1653,7 +1654,7 @@ dependencies = [ "log", "memory_addr", "ruxdriver", - "ruxtask", + "spin 0.9.8", ] [[package]] @@ -1718,6 +1719,26 @@ dependencies = [ "ruxos_posix_api", ] +[[package]] +name = "ruxmm" +version = "0.1.0" +dependencies = [ + "axalloc", + "bitflags 2.4.0", + "cfg-if", + "crate_interface", + "kernel_guard", + "log", + "memory_addr", + "page_table", + "page_table_entry", + "ruxdriver", + "ruxhal", + "ruxtask", + "spinlock", + "static_assertions", +] + [[package]] name = "ruxmusl" version = "0.1.0" @@ -1796,6 +1817,7 @@ dependencies = [ "ruxfs", "ruxfutex", "ruxhal", + "ruxmm", "ruxnet", "ruxruntime", "ruxtask", @@ -1836,6 +1858,7 @@ dependencies = [ "ruxfs", "ruxfutex", "ruxhal", + "ruxmm", "ruxnet", "ruxrand", "ruxtask", @@ -1846,21 +1869,29 @@ dependencies = [ name = "ruxtask" version = "0.1.0" dependencies = [ + "axalloc", "axerrno", + "axfs_vfs", + "axio", "cfg-if", "crate_interface", + "flatten_objects", "kernel_guard", "lazy_init", + "lazy_static", "log", "memory_addr", + "page_table", + "page_table_entry", "percpu", "rand", "ruxconfig", "ruxfdtable", + "ruxfs", "ruxhal", - "ruxrand", "ruxtask", "scheduler", + "spin 0.9.8", "spinlock", "timer_list", ] diff --git a/Cargo.toml b/Cargo.toml index 2f381d05b..e6822d916 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,7 @@ members = [ "modules/ruxnet", "modules/axsync", "modules/rux9p", + "modules/ruxmm", "modules/ruxconfig", "modules/ruxdisplay", "modules/ruxdriver", diff --git a/Makefile b/Makefile index 97b46532b..4c7b5f8c5 100644 --- a/Makefile +++ b/Makefile @@ -222,13 +222,7 @@ justrun: $(call run_qemu) debug: build - $(call run_qemu_debug) & - sleep 1 - $(GDB) $(OUT_ELF) \ - -ex 'target remote localhost:1234' \ - -ex 'b rust_entry' \ - -ex 'continue' \ - -ex 'disp /16i $$pc' + $(call run_qemu_debug) debug_no_attach: build $(call run_qemu_debug) diff --git a/api/ruxfeat/Cargo.toml b/api/ruxfeat/Cargo.toml index 7926cee2f..a57dac5b3 100644 --- a/api/ruxfeat/Cargo.toml +++ b/api/ruxfeat/Cargo.toml @@ -31,7 +31,7 @@ alloc = ["axalloc", "ruxruntime/alloc", "ruxfs/alloc", "ruxhal/alloc"] alloc-tlsf = ["axalloc/tlsf"] alloc-slab = ["axalloc/slab"] alloc-buddy = ["axalloc/buddy"] -paging = ["alloc", "ruxhal/paging", "ruxruntime/paging"] +paging = ["alloc", "ruxhal/paging", "ruxtask/paging", "ruxruntime/paging", "ruxmm/paging"] tls = ["alloc", "ruxhal/tls", "ruxruntime/tls", "ruxtask?/tls"] # Multi-threading and scheduler @@ -93,6 +93,7 @@ tty = ["ruxhal/tty", "ruxruntime/tty", "alloc", "irq"] [dependencies] ruxruntime = { path = "../../modules/ruxruntime" } ruxhal = { path = "../../modules/ruxhal" } +ruxmm = { path = "../../modules/ruxmm" } axlog = { path = "../../modules/axlog" } axalloc = { path = "../../modules/axalloc", optional = true } ruxdriver = { path = "../../modules/ruxdriver", optional = true } diff --git a/api/ruxos_posix_api/Cargo.toml b/api/ruxos_posix_api/Cargo.toml index 6ae5c1cea..7ba4c38cb 100644 --- a/api/ruxos_posix_api/Cargo.toml +++ b/api/ruxos_posix_api/Cargo.toml @@ -19,7 +19,7 @@ default = [] smp = ["ruxfeat/smp"] alloc = ["dep:axalloc", "ruxfeat/alloc"] -paging = ["alloc", "ruxfeat/paging"] +paging = ["alloc", "ruxfeat/paging", "ruxmm"] multitask = ["ruxfeat/multitask", "ruxtask/multitask", "dep:ruxfutex"] fd = ["alloc"] fs = ["dep:ruxfs", "ruxfeat/fs", "fd"] @@ -44,6 +44,7 @@ axlog = { path = "../../modules/axlog" } ruxhal = { path = "../../modules/ruxhal" } axsync = { path = "../../modules/axsync" } ruxfdtable = { path = "../../modules/ruxfdtable" } +ruxmm = { path = "../../modules/ruxmm", optional = true } ruxfutex = { path = "../../modules/ruxfutex", optional = true } axalloc = { path = "../../modules/axalloc", optional = true } ruxtask = { path = "../../modules/ruxtask", optional = true } diff --git a/api/ruxos_posix_api/src/imp/execve/mod.rs b/api/ruxos_posix_api/src/imp/execve/mod.rs index 4b1dc5c46..ad9a7cea0 100644 --- a/api/ruxos_posix_api/src/imp/execve/mod.rs +++ b/api/ruxos_posix_api/src/imp/execve/mod.rs @@ -4,6 +4,7 @@ mod stack; use alloc::vec; use core::ffi::c_char; +use ruxtask::current; use crate::{ config, @@ -14,6 +15,7 @@ use crate::{ /// int execve(const char *pathname, char *const argv[], char *const envp[] ); pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! { + error!("execve: pathname {:?}, argv {:?}, envp {:?}", pathname, argv, envp); use auxv::*; let path = char_ptr_to_str(pathname).unwrap(); @@ -33,6 +35,7 @@ pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! { }; // create stack + // memory broken, use stack alloc to store args and envs let mut stack = stack::Stack::new(); // non 8B info @@ -119,6 +122,15 @@ pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! { prog.entry ); + // TODO: may lead to memory leaky, release stack after the change of stack + current().set_stack_top(stack.stack_top() - stack.stack_size(), stack.stack_size()); + warn!( + "sys_execve: current_id_name {:?}, stack top 0x{:x}, size 0x{:x}", + current().id_name(), + current().stack_top(), + stack.stack_size() + ); + set_sp_and_jmp(sp, entry); } diff --git a/api/ruxos_posix_api/src/imp/execve/stack.rs b/api/ruxos_posix_api/src/imp/execve/stack.rs index 4561a09f3..1bd067c6c 100644 --- a/api/ruxos_posix_api/src/imp/execve/stack.rs +++ b/api/ruxos_posix_api/src/imp/execve/stack.rs @@ -1,9 +1,12 @@ -use alloc::{vec, vec::Vec}; +use alloc::vec::Vec; +use ruxtask::task::TaskStack; const STACK_SIZE: usize = ruxconfig::TASK_STACK_SIZE; #[derive(Debug)] pub struct Stack { + /// task stack + task_stack: TaskStack, /// stack data: Vec, /// index of top byte of stack @@ -13,9 +16,15 @@ pub struct Stack { impl Stack { /// alloc a stack pub fn new() -> Self { - Self { - data: vec![0u8; STACK_SIZE], - top: STACK_SIZE, + let task_stack = TaskStack::alloc(STACK_SIZE); + unsafe { + let start = task_stack.top().as_mut_ptr().sub(STACK_SIZE); + + Self { + task_stack, + data: Vec::from_raw_parts(start, STACK_SIZE, STACK_SIZE), + top: STACK_SIZE, + } } } @@ -24,6 +33,14 @@ impl Stack { self.data.as_ptr() as usize + self.top } + pub fn stack_size(&self) -> usize { + self.data.len() + } + + pub fn stack_top(&self) -> usize { + self.task_stack.top().into() + } + /// push data to stack and return the addr of sp pub fn push(&mut self, data: &[T], align: usize) -> usize { // move sp to right place @@ -41,3 +58,9 @@ impl Stack { sp as usize } } + +impl Drop for Stack { + fn drop(&mut self) { + error!("execve's stack dropped. {:#?}", self); + } +} diff --git a/api/ruxos_posix_api/src/imp/fd_ops.rs b/api/ruxos_posix_api/src/imp/fd_ops.rs index 6f271de17..b12bd1e4d 100644 --- a/api/ruxos_posix_api/src/imp/fd_ops.rs +++ b/api/ruxos_posix_api/src/imp/fd_ops.rs @@ -7,13 +7,13 @@ * See the Mulan PSL v2 for more details. */ -use alloc::sync::Arc; use core::ffi::c_int; use axerrno::{LinuxError, LinuxResult}; -use ruxfdtable::{FileLike, RuxStat, RuxTimeSpec, FD_TABLE, RUX_FILE_LIMIT}; +use ruxfdtable::{RuxStat, RuxTimeSpec}; +use ruxtask::current; +use ruxtask::fs::{add_file_like, close_file_like, get_file_like, RUX_FILE_LIMIT}; -use super::stdio::{stdin, stdout}; use crate::ctypes; impl From for RuxTimeSpec { @@ -124,39 +124,6 @@ impl From for ctypes::stat { } } -lazy_static::lazy_static! { - static ref MUST_EXEC: usize = { - FD_TABLE.write().add_at(0, Arc::new(stdin()) as _).unwrap(); // stdin - FD_TABLE.write().add_at(1, Arc::new(stdout()) as _).unwrap(); // stdout - FD_TABLE.write().add_at(2, Arc::new(stdout()) as _).unwrap(); // stderr - 0 - }; -} - -pub fn get_file_like(fd: c_int) -> LinuxResult> { - let _exec = *MUST_EXEC; - FD_TABLE - .read() - .get(fd as usize) - .cloned() - .ok_or(LinuxError::EBADF) -} - -pub fn add_file_like(f: Arc) -> LinuxResult { - let _exec = *MUST_EXEC; - Ok(FD_TABLE.write().add(f).ok_or(LinuxError::EMFILE)? as c_int) -} - -pub fn close_file_like(fd: c_int) -> LinuxResult { - let _exec = *MUST_EXEC; - let f = FD_TABLE - .write() - .remove(fd as usize) - .ok_or(LinuxError::EBADF)?; - drop(f); - Ok(()) -} - /// Close a file by `fd`. pub fn sys_close(fd: c_int) -> c_int { debug!("sys_close <= {}", fd); @@ -169,7 +136,7 @@ pub fn sys_close(fd: c_int) -> c_int { fn dup_fd(old_fd: c_int) -> LinuxResult { let f = get_file_like(old_fd)?; let new_fd = add_file_like(f)?; - Ok(new_fd) + Ok(new_fd as _) } /// Duplicate a file descriptor. @@ -195,11 +162,13 @@ pub fn sys_dup2(old_fd: c_int, new_fd: c_int) -> c_int { if new_fd as usize >= RUX_FILE_LIMIT { return Err(LinuxError::EBADF); } - close_file_like(new_fd)?; + close_file_like(new_fd as _)?; - let f = get_file_like(old_fd)?; - FD_TABLE - .write() + let f = get_file_like(old_fd as _)?; + let binding_task = current(); + let mut binding_fs = binding_task.fs.lock(); + let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; + fd_table .add_at(new_fd as usize, f) .ok_or(LinuxError::EMFILE)?; @@ -267,8 +236,10 @@ pub fn sys_fcntl(fd: c_int, cmd: c_int, arg: usize) -> c_int { if arg == 0 || arg == 1 || arg == 2 { return Ok(0); } - FD_TABLE - .write() + let binding_task = current(); + let mut binding_fs = binding_task.fs.lock(); + let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; + fd_table .add_at(arg, get_file_like(fd)?) .ok_or(LinuxError::EMFILE)?; let _ = close_file_like(fd); diff --git a/api/ruxos_posix_api/src/imp/fs.rs b/api/ruxos_posix_api/src/imp/fs.rs index 89019d865..745c8d5a4 100644 --- a/api/ruxos_posix_api/src/imp/fs.rs +++ b/api/ruxos_posix_api/src/imp/fs.rs @@ -7,167 +7,33 @@ * See the Mulan PSL v2 for more details. */ -use alloc::{borrow::Cow, string::String, sync::Arc}; -use core::ffi::{c_char, c_int, c_long, c_void, CStr}; +use alloc::sync::Arc; +use core::ffi::{c_char, c_int, c_long, c_void}; -use axerrno::{LinuxError, LinuxResult}; -use axio::{PollState, SeekFrom}; -use axsync::Mutex; -use ruxfdtable::{FileLike, RuxStat}; +use axerrno::LinuxError; +use axio::SeekFrom; +use ruxfdtable::FileLike; use ruxfs::{ api::set_current_dir, fops::{DirEntry, OpenOptions}, }; -use super::fd_ops::get_file_like; -use crate::ctypes; +use crate::{ctypes, utils::char_ptr_to_str}; use alloc::vec::Vec; +use ruxtask::fs::{get_file_like, Directory, File}; -pub struct File { - pub(crate) inner: Mutex, -} - -impl File { - pub(crate) fn new(inner: ruxfs::fops::File) -> Self { - Self { - inner: Mutex::new(inner), - } - } - - pub(crate) fn add_to_fd_table(self) -> LinuxResult { - super::fd_ops::add_file_like(Arc::new(self)) - } - - pub(crate) fn from_fd(fd: c_int) -> LinuxResult> { - let f = super::fd_ops::get_file_like(fd)?; - f.into_any() - .downcast::() - .map_err(|_| LinuxError::EINVAL) - } -} - -impl FileLike for File { - fn read(&self, buf: &mut [u8]) -> LinuxResult { - Ok(self.inner.lock().read(buf)?) - } - - fn write(&self, buf: &[u8]) -> LinuxResult { - Ok(self.inner.lock().write(buf)?) - } - - fn flush(&self) -> LinuxResult { - Ok(self.inner.lock().flush()?) - } - - fn stat(&self) -> LinuxResult { - let metadata = self.inner.lock().get_attr()?; - let ty = metadata.file_type() as u8; - let perm = metadata.perm().bits() as u32; - let st_mode = ((ty as u32) << 12) | perm; - - // Inode of files, for musl dynamic linker. - // WARN: there will be collision for files with the same size. - // TODO: implement real inode. - let st_ino = metadata.size() + st_mode as u64; - - let res = RuxStat::from(ctypes::stat { - st_ino, - st_nlink: 1, - st_mode, - st_uid: 1000, - st_gid: 1000, - st_size: metadata.size() as _, - st_blocks: metadata.blocks() as _, - st_blksize: 512, - ..Default::default() - }); - - Ok(res) - } +use super::stdio::{stdin, stdout}; - fn into_any(self: Arc) -> Arc { - self - } +struct InitFsImpl; - fn poll(&self) -> LinuxResult { - Ok(PollState { - readable: true, - writable: true, - }) - } - - fn set_nonblocking(&self, _nonblocking: bool) -> LinuxResult { - Ok(()) - } -} - -pub struct Directory { - inner: Mutex, -} - -impl Directory { - fn new(inner: ruxfs::fops::Directory) -> Self { - Self { - inner: Mutex::new(inner), - } - } - - fn add_to_fd_table(self) -> LinuxResult { - super::fd_ops::add_file_like(Arc::new(self)) - } - - fn from_fd(fd: c_int) -> LinuxResult> { - let f = super::fd_ops::get_file_like(fd)?; - f.into_any() - .downcast::() - .map_err(|_| LinuxError::EINVAL) - } -} - -impl FileLike for Directory { - fn read(&self, _buf: &mut [u8]) -> LinuxResult { - Err(LinuxError::EACCES) - } - - fn write(&self, _buf: &[u8]) -> LinuxResult { - Err(LinuxError::EACCES) - } - - fn flush(&self) -> LinuxResult { - Ok(()) - } - - fn stat(&self) -> LinuxResult { - let metadata = self.inner.lock().get_attr()?; - let ty = metadata.file_type() as u8; - let perm = metadata.perm().bits() as u32; - let st_mode = ((ty as u32) << 12) | perm; - Ok(RuxStat::from(ctypes::stat { - st_ino: 1, - st_nlink: 1, - st_mode, - st_uid: 1000, - st_gid: 1000, - st_size: metadata.size() as _, - st_blocks: metadata.blocks() as _, - st_blksize: 512, - ..Default::default() - })) - } - - fn into_any(self: Arc) -> Arc { - self - } - - fn poll(&self) -> LinuxResult { - Ok(PollState { - readable: true, - writable: true, - }) - } - - fn set_nonblocking(&self, _nonblocking: bool) -> LinuxResult { - Ok(()) +#[crate_interface::impl_interface] +impl ruxtask::fs::InitFs for InitFsImpl { + fn init(fs: &mut ruxtask::fs::FileSystem) { + debug!("init initial process's fd_table"); + let fd_table = &mut fs.fd_table; + fd_table.add_at(0, Arc::new(stdin()) as _).unwrap(); // stdin + fd_table.add_at(1, Arc::new(stdout()) as _).unwrap(); // stdout + fd_table.add_at(2, Arc::new(stdout()) as _).unwrap(); // stderr } } @@ -203,40 +69,40 @@ fn flags_to_options(flags: c_int, _mode: ctypes::mode_t) -> OpenOptions { /// Return its index in the file table (`fd`). Return `EMFILE` if it already /// has the maximum number of files open. pub fn sys_open(filename: *const c_char, flags: c_int, mode: ctypes::mode_t) -> c_int { - let filename = char_ptr_to_absolute_path(filename); + let filename = char_ptr_to_str(filename); debug!("sys_open <= {:?} {:#o} {:#o}", filename, flags, mode); syscall_body!(sys_open, { let options = flags_to_options(flags, mode); - let file = ruxfs::fops::File::open(&filename?, &options)?; + let file = ruxfs::fops::File::open(filename?, &options)?; File::new(file).add_to_fd_table() }) } /// Open a file under a specific dir pub fn sys_openat(fd: usize, path: *const c_char, flags: c_int, mode: ctypes::mode_t) -> c_int { - let path = char_ptr_to_absolute_path(path); + let path = char_ptr_to_str(path); let fd: c_int = fd as c_int; debug!("sys_openat <= {}, {:?}, {:#o} {:#o}", fd, path, flags, mode); syscall_body!(sys_openat, { let options = flags_to_options(flags, mode); if (flags as u32) & ctypes::O_DIRECTORY != 0 { let dir = if fd == ctypes::AT_FDCWD { - ruxfs::fops::Directory::open_dir(&path?, &options)? + ruxfs::fops::Directory::open_dir(path?, &options)? } else { Directory::from_fd(fd)? .inner - .lock() - .open_dir_at(&path?, &options)? + .write() + .open_dir_at(path?, &options)? }; Directory::new(dir).add_to_fd_table() } else { let file = if fd == ctypes::AT_FDCWD { - ruxfs::fops::File::open(&path?, &options)? + ruxfs::fops::File::open(path?, &options)? } else { Directory::from_fd(fd)? .inner - .lock() - .open_file_at(&path?, &options)? + .write() + .open_file_at(path?, &options)? }; File::new(file).add_to_fd_table() } @@ -258,7 +124,7 @@ pub fn sys_pread64( return Err(LinuxError::EFAULT); } let dst = unsafe { core::slice::from_raw_parts_mut(buf as *mut u8, count) }; - let size = File::from_fd(fd)?.inner.lock().read_at(pos as u64, dst)?; + let size = File::from_fd(fd)?.inner.write().read_at(pos as u64, dst)?; Ok(size as ctypes::ssize_t) }) } @@ -278,7 +144,7 @@ pub fn sys_pwrite64( return Err(LinuxError::EFAULT); } let src = unsafe { core::slice::from_raw_parts_mut(buf as *mut u8, count) }; - let size = File::from_fd(fd)?.inner.lock().write_at(pos as u64, src)?; + let size = File::from_fd(fd)?.inner.write().write_at(pos as u64, src)?; Ok(size as ctypes::ssize_t) }) } @@ -295,7 +161,7 @@ pub fn sys_lseek(fd: c_int, offset: ctypes::off_t, whence: c_int) -> ctypes::off 2 => SeekFrom::End(offset as _), _ => return Err(LinuxError::EINVAL), }; - let off = File::from_fd(fd)?.inner.lock().seek(pos)?; + let off = File::from_fd(fd)?.inner.write().seek(pos)?; Ok(off) }) } @@ -320,7 +186,7 @@ pub unsafe fn sys_fdatasync(fd: c_int) -> c_int { /// /// Return 0 if success. pub unsafe fn sys_stat(path: *const c_char, buf: *mut core::ffi::c_void) -> c_int { - let path = char_ptr_to_absolute_path(path); + let path = char_ptr_to_str(path); debug!("sys_stat <= {:?} {:#x}", path, buf as usize); syscall_body!(sys_stat, { if buf.is_null() { @@ -328,7 +194,7 @@ pub unsafe fn sys_stat(path: *const c_char, buf: *mut core::ffi::c_void) -> c_in } let mut options = OpenOptions::new(); options.read(true); - let file = ruxfs::fops::File::open(&path?, &options)?; + let file = ruxfs::fops::File::open(path?, &options)?; let st: ctypes::stat = File::new(file).stat()?.into(); #[cfg(not(feature = "musl"))] @@ -401,7 +267,7 @@ pub fn sys_fstat(fd: c_int, kst: *mut core::ffi::c_void) -> c_int { /// /// Return 0 if success. pub unsafe fn sys_lstat(path: *const c_char, buf: *mut ctypes::stat) -> ctypes::ssize_t { - let path = char_ptr_to_absolute_path(path); + let path = char_ptr_to_str(path); debug!("sys_lstat <= {:?} {:#x}", path, buf as usize); syscall_body!(sys_lstat, { if buf.is_null() { @@ -419,7 +285,7 @@ pub unsafe fn sys_newfstatat( kst: *mut ctypes::kstat, flag: c_int, ) -> c_int { - let path = char_ptr_to_absolute_path(path); + let path = char_ptr_to_str(path); debug!( "sys_newfstatat <= fd: {}, path: {:?}, flag: {:x}", _fd, path, flag @@ -430,7 +296,7 @@ pub unsafe fn sys_newfstatat( } let mut options = OpenOptions::new(); options.read(true); - let file = ruxfs::fops::File::open(&path?, &options)?; + let file = ruxfs::fops::File::open(path?, &options)?; let st = File::new(file).stat()?; unsafe { (*kst).st_dev = st.st_dev; @@ -473,10 +339,10 @@ pub fn sys_getcwd(buf: *mut c_char, size: usize) -> c_int { /// Return 0 if the operation succeeds, otherwise return -1. pub fn sys_rename(old: *const c_char, new: *const c_char) -> c_int { syscall_body!(sys_rename, { - let old_path = char_ptr_to_absolute_path(old)?; - let new_path = char_ptr_to_absolute_path(new)?; + let old_path = char_ptr_to_str(old)?; + let new_path = char_ptr_to_str(new)?; debug!("sys_rename <= old: {:?}, new: {:?}", old_path, new_path); - ruxfs::api::rename(&old_path, &new_path)?; + ruxfs::api::rename(old_path, new_path)?; Ok(0) }) } @@ -485,8 +351,8 @@ pub fn sys_rename(old: *const c_char, new: *const c_char) -> c_int { /// /// TODO: only support `oldfd`, `newfd` equals to AT_FDCWD pub fn sys_renameat(oldfd: c_int, old: *const c_char, newfd: c_int, new: *const c_char) -> c_int { - let old_path = char_ptr_to_absolute_path(old); - let new_path = char_ptr_to_absolute_path(new); + let old_path = char_ptr_to_str(old); + let new_path = char_ptr_to_str(new); debug!( "sys_renameat <= oldfd: {}, old: {:?}, newfd: {}, new: {:?}", oldfd, old_path, newfd, new_path @@ -494,7 +360,7 @@ pub fn sys_renameat(oldfd: c_int, old: *const c_char, newfd: c_int, new: *const assert_eq!(oldfd, ctypes::AT_FDCWD as c_int); assert_eq!(newfd, ctypes::AT_FDCWD as c_int); syscall_body!(sys_renameat, { - ruxfs::api::rename(&old_path?, &new_path?)?; + ruxfs::api::rename(old_path?, new_path?)?; Ok(0) }) } @@ -502,9 +368,9 @@ pub fn sys_renameat(oldfd: c_int, old: *const c_char, newfd: c_int, new: *const /// Remove a directory, which must be empty pub fn sys_rmdir(pathname: *const c_char) -> c_int { syscall_body!(sys_rmdir, { - let path = char_ptr_to_absolute_path(pathname)?; + let path = char_ptr_to_str(pathname)?; debug!("sys_rmdir <= path: {:?}", path); - ruxfs::api::remove_dir(&path)?; + ruxfs::api::remove_dir(path)?; Ok(0) }) } @@ -512,9 +378,9 @@ pub fn sys_rmdir(pathname: *const c_char) -> c_int { /// Removes a file from the filesystem. pub fn sys_unlink(pathname: *const c_char) -> c_int { syscall_body!(sys_unlink, { - let path = char_ptr_to_absolute_path(pathname)?; + let path = char_ptr_to_str(pathname)?; debug!("sys_unlink <= path: {:?}", path); - ruxfs::api::remove_file(&path)?; + ruxfs::api::remove_file(path)?; Ok(0) }) } @@ -524,7 +390,7 @@ pub fn sys_unlinkat(fd: c_int, pathname: *const c_char, flags: c_int) -> c_int { debug!( "sys_unlinkat <= fd: {}, pathname: {:?}, flags: {}", fd, - char_ptr_to_absolute_path(pathname), + char_ptr_to_str(pathname), flags ); if flags as u32 & ctypes::AT_REMOVEDIR != 0 { @@ -537,9 +403,9 @@ pub fn sys_unlinkat(fd: c_int, pathname: *const c_char, flags: c_int) -> c_int { pub fn sys_mkdir(pathname: *const c_char, mode: ctypes::mode_t) -> c_int { // TODO: implement mode syscall_body!(sys_mkdir, { - let path = char_ptr_to_absolute_path(pathname)?; + let path = char_ptr_to_str(pathname)?; debug!("sys_mkdir <= path: {:?}, mode: {:?}", path, mode); - ruxfs::api::create_dir(&path)?; + ruxfs::api::create_dir(path)?; Ok(0) }) } @@ -551,7 +417,7 @@ pub fn sys_mkdirat(fd: c_int, pathname: *const c_char, mode: ctypes::mode_t) -> debug!( "sys_mkdirat <= fd: {}, pathname: {:?}, mode: {:x?}", fd, - char_ptr_to_absolute_path(pathname), + char_ptr_to_str(pathname), mode ); sys_mkdir(pathname, mode) @@ -568,7 +434,7 @@ pub fn sys_fchownat( debug!( "sys_fchownat <= fd: {}, path: {:?}, uid: {}, gid: {}, flag: {}", fd, - char_ptr_to_absolute_path(path), + char_ptr_to_str(path), uid, gid, flag @@ -584,7 +450,7 @@ pub fn sys_readlinkat( buf: *mut c_char, bufsize: usize, ) -> usize { - let path = char_ptr_to_absolute_path(pathname); + let path = char_ptr_to_str(pathname); debug!( "sys_readlinkat <= path = {:?}, fd = {:}, buf = {:p}, bufsize = {:}", path, fd, buf, bufsize @@ -628,7 +494,7 @@ pub unsafe fn sys_getdents64( let mut my_dirent: Vec = (0..expect_entries).map(|_| DirEntry::default()).collect(); - let n = dir.inner.lock().read_dir(&mut my_dirent)?; + let n = dir.inner.write().read_dir(&mut my_dirent)?; for (i, entry) in my_dirent.iter().enumerate() { let linux_dirent = LinuxDirent64 { @@ -682,7 +548,7 @@ pub unsafe fn sys_preadv( /// The mode is either the value F_OK, for the existence of the file, /// or a mask consisting of the bitwise OR of one or more of R_OK, W_OK, and X_OK, for the read, write, execute permissions. pub fn sys_faccessat(dirfd: c_int, pathname: *const c_char, mode: c_int, flags: c_int) -> c_int { - let path = char_ptr_to_absolute_path(pathname).unwrap(); + let path = char_ptr_to_str(pathname).unwrap(); debug!( "sys_faccessat <= dirfd {} path {} mode {} flags {}", dirfd, path, mode, flags @@ -690,49 +556,17 @@ pub fn sys_faccessat(dirfd: c_int, pathname: *const c_char, mode: c_int, flags: syscall_body!(sys_faccessat, { let mut options = OpenOptions::new(); options.read(true); - let _file = ruxfs::fops::File::open(&path, &options)?; + let _file = ruxfs::fops::File::open(path, &options)?; Ok(0) }) } /// changes the current working directory to the directory specified in path. pub fn sys_chdir(path: *const c_char) -> c_int { - let p = char_ptr_to_absolute_path(path).unwrap(); + let p = char_ptr_to_str(path).unwrap(); debug!("sys_chdir <= path: {}", p); syscall_body!(sys_chdir, { - set_current_dir(&p)?; + set_current_dir(p)?; Ok(0) }) } - -/// from char_ptr get absolute_path_str -pub fn char_ptr_to_absolute_path<'a>(ptr: *const c_char) -> LinuxResult> { - if ptr.is_null() { - return Err(LinuxError::EFAULT); - } - - let path = unsafe { - let cstr = CStr::from_ptr(ptr); - cstr.to_str().map_err(|_| LinuxError::EINVAL)? - }; - - if path.starts_with("..") { - let stripped = path.strip_prefix("..").unwrap(); - let mut cwd = ruxfs::api::current_dir()?; - if let Some(index) = cwd.rfind('/') { - cwd.truncate(index); - if let Some(index) = cwd.rfind('/') { - cwd.truncate(index); - } - } - let absolute_path: String = cwd + stripped; - Ok(Cow::Owned(absolute_path)) - } else if path.starts_with('.') { - let stripped = path.strip_prefix('.').unwrap(); - let cwd = ruxfs::api::current_dir()?; - let absolute_path: String = cwd + stripped; - Ok(Cow::Owned(absolute_path)) - } else { - Ok(Cow::Borrowed(path)) - } -} diff --git a/api/ruxos_posix_api/src/imp/io.rs b/api/ruxos_posix_api/src/imp/io.rs index c509c567c..21ba68eeb 100644 --- a/api/ruxos_posix_api/src/imp/io.rs +++ b/api/ruxos_posix_api/src/imp/io.rs @@ -11,10 +11,10 @@ use crate::ctypes; use axerrno::LinuxError; use core::ffi::{c_int, c_void}; -#[cfg(feature = "fd")] -use crate::imp::fd_ops::get_file_like; #[cfg(not(feature = "fd"))] use axio::prelude::*; +#[cfg(feature = "fd")] +use ruxtask::fs::get_file_like; /// Read data from the file indicated by `fd`. /// @@ -28,7 +28,7 @@ pub fn sys_read(fd: c_int, buf: *mut c_void, count: usize) -> ctypes::ssize_t { let dst = unsafe { core::slice::from_raw_parts_mut(buf as *mut u8, count) }; #[cfg(feature = "fd")] { - Ok(get_file_like(fd)?.read(dst)? as ctypes::ssize_t) + Ok(get_file_like(fd as _)?.read(dst)? as ctypes::ssize_t) } #[cfg(not(feature = "fd"))] match fd { diff --git a/api/ruxos_posix_api/src/imp/io_mpx/epoll.rs b/api/ruxos_posix_api/src/imp/io_mpx/epoll.rs index 12bc3b16e..7802f4b6f 100644 --- a/api/ruxos_posix_api/src/imp/io_mpx/epoll.rs +++ b/api/ruxos_posix_api/src/imp/io_mpx/epoll.rs @@ -22,7 +22,7 @@ use ruxfdtable::{FileLike, RuxStat}; use ruxhal::time::current_time; use crate::ctypes; -use crate::imp::fd_ops::{add_file_like, get_file_like}; +use ruxtask::fs::{add_file_like, get_file_like}; pub struct EpollInstance { events: Mutex>, diff --git a/api/ruxos_posix_api/src/imp/io_mpx/poll.rs b/api/ruxos_posix_api/src/imp/io_mpx/poll.rs index d9aa4ff70..94468f7eb 100644 --- a/api/ruxos_posix_api/src/imp/io_mpx/poll.rs +++ b/api/ruxos_posix_api/src/imp/io_mpx/poll.rs @@ -7,10 +7,11 @@ * See the Mulan PSL v2 for more details. */ -use crate::{ctypes, imp::fd_ops::get_file_like}; use axerrno::{LinuxError, LinuxResult}; use ruxhal::time::current_time; +use ruxtask::fs::get_file_like; +use crate::ctypes; use core::{ffi::c_int, time::Duration}; fn poll_all(fds: &mut [ctypes::pollfd]) -> LinuxResult { diff --git a/api/ruxos_posix_api/src/imp/io_mpx/select.rs b/api/ruxos_posix_api/src/imp/io_mpx/select.rs index f8b9f383f..ca35ff3e5 100644 --- a/api/ruxos_posix_api/src/imp/io_mpx/select.rs +++ b/api/ruxos_posix_api/src/imp/io_mpx/select.rs @@ -12,7 +12,8 @@ use core::ffi::{c_int, c_void}; use axerrno::{LinuxError, LinuxResult}; use ruxhal::time::current_time; -use crate::{ctypes, imp::fd_ops::get_file_like}; +use crate::ctypes; +use ruxtask::fs::get_file_like; const FD_SETSIZE: usize = 1024; const BITS_PER_USIZE: usize = usize::BITS as usize; diff --git a/api/ruxos_posix_api/src/imp/ioctl.rs b/api/ruxos_posix_api/src/imp/ioctl.rs index 4838772c9..223a3c7b7 100644 --- a/api/ruxos_posix_api/src/imp/ioctl.rs +++ b/api/ruxos_posix_api/src/imp/ioctl.rs @@ -7,9 +7,10 @@ * See the Mulan PSL v2 for more details. */ -use crate::{imp::fd_ops::get_file_like, sys_getpgid}; +use crate::sys_getpgid; use axerrno::LinuxError; use core::ffi::c_int; +use ruxtask::fs::get_file_like; /// IOCTL oprations pub const TCGETS: usize = 0x5401; diff --git a/api/ruxos_posix_api/src/imp/mmap/api.rs b/api/ruxos_posix_api/src/imp/mmap/api.rs index 9a671d160..74217c7e2 100644 --- a/api/ruxos_posix_api/src/imp/mmap/api.rs +++ b/api/ruxos_posix_api/src/imp/mmap/api.rs @@ -15,12 +15,15 @@ use core::{ ops::Bound, }; use memory_addr::PAGE_SIZE_4K; -use ruxhal::{mem::VirtAddr, paging::pte_update_page}; +use ruxhal::mem::VirtAddr; +use ruxmm::paging::pte_update_page; use super::utils::{ find_free_region, get_mflags_from_usize, get_overlap, release_pages_mapped, shift_mapped_page, - snatch_fixed_region, Vma, MEM_MAP, VMA_END, VMA_MAP, + snatch_fixed_region, VMA_END, }; +use ruxtask::vma::Vma; +use ruxtask::{current, vma::FileInfo}; #[cfg(feature = "fs")] use { @@ -46,6 +49,14 @@ pub fn sys_mmap( syscall_body!(sys_mmap, { // transform C-type into rust-type let start = start as usize; + let len = VirtAddr::from(len).align_up_4k().as_usize(); + if !VirtAddr::from(start).is_aligned(PAGE_SIZE_4K) || len == 0 { + error!( + "mmap failed because start:0x{:x} is not aligned or len:0x{:x} == 0", + start, len + ); + return Err(LinuxError::EINVAL); + } let prot = prot as u32; let flags = flags as u32; let fid = fd; @@ -67,24 +78,9 @@ pub fn sys_mmap( fid }; - // align len to PAGE_SIZE_4K depending on `MAP_ANONYMOUS` or not. - let len = if fid < 0 { - VirtAddr::from(len).align_up_4k().as_usize() - } else { - VirtAddr::from(len).as_usize() - }; - - // check if `start` is aligned to `PAGE_SIZE_4K`or len is large than 0. - if !VirtAddr::from(start).is_aligned(PAGE_SIZE_4K) || len == 0 { - error!( - "mmap failed because start:0x{:x} is not aligned or len:0x{:x} == 0", - start, len - ); - return Err(LinuxError::EINVAL); - } - let mut new = Vma::new(fid, offset, prot, flags); - let mut vma_map = VMA_MAP.lock(); + let binding_task = current(); + let mut vma_map = binding_task.mm.vma_map.lock(); let addr_condition = if start == 0 { None } else { Some(start) }; let try_addr = if flags & ctypes::MAP_FIXED != 0 { @@ -111,7 +107,7 @@ pub fn sys_munmap(start: *mut c_void, len: ctypes::size_t) -> c_int { syscall_body!(sys_munmap, { // transform C-type into rust-type let start = start as usize; - let end = VirtAddr::from(start + len).as_usize(); + let end = VirtAddr::from(start + len).align_up_4k().as_usize(); if !VirtAddr::from(start).is_aligned(PAGE_SIZE_4K) || len == 0 { error!( @@ -121,7 +117,8 @@ pub fn sys_munmap(start: *mut c_void, len: ctypes::size_t) -> c_int { return Err(LinuxError::EINVAL); } - let mut vma_map = VMA_MAP.lock(); + let binding = current(); + let mut vma_map = binding.mm.vma_map.lock(); // In order to ensure that munmap can exit directly if it fails, it must // ensure that munmap semantics are correct before taking action. @@ -179,7 +176,7 @@ pub fn sys_munmap(start: *mut c_void, len: ctypes::size_t) -> c_int { } // delete the mapped and swapped page. - release_pages_mapped(start, end, true); + release_pages_mapped(start, end); #[cfg(feature = "fs")] release_pages_swaped(start, end); @@ -199,7 +196,7 @@ pub fn sys_mprotect(start: *mut c_void, len: ctypes::size_t, prot: c_int) -> c_i syscall_body!(sys_mprotect, { // transform C-type into rust-type let start = start as usize; - let end = VirtAddr::from(start + len).as_usize(); + let end = VirtAddr::from(start + len).align_up_4k().as_usize(); if !VirtAddr::from(start).is_aligned(PAGE_SIZE_4K) || len == 0 { return Err(LinuxError::EINVAL); } @@ -211,7 +208,8 @@ pub fn sys_mprotect(start: *mut c_void, len: ctypes::size_t, prot: c_int) -> c_i let mut post_shrink: Vec<(usize, usize)> = Vec::new(); let mut post_align_changed: Vec<(usize, usize)> = Vec::new(); - let mut vma_map = VMA_MAP.lock(); + let binding_task = current(); + let mut vma_map = binding_task.mm.vma_map.lock(); let mut node = vma_map.upper_bound_mut(Bound::Included(&start)); let mut counter = 0; // counter to check if all address in [start, start+len) is mapped. while let Some(vma) = node.value_mut() { @@ -256,7 +254,7 @@ pub fn sys_mprotect(start: *mut c_void, len: ctypes::size_t, prot: c_int) -> c_i } // upate PTEs if mprotect is successful. - for (&vaddr, _) in MEM_MAP.lock().range(start..end) { + for (&vaddr, _) in current().mm.mem_map.lock().range(start..end) { if pte_update_page( VirtAddr::from(vaddr), None, @@ -301,14 +299,14 @@ pub fn sys_msync(start: *mut c_void, len: ctypes::size_t, flags: c_int) -> c_int #[cfg(feature = "fs")] { let start = start as usize; - let end = VirtAddr::from(start + len).as_usize(); + let end = VirtAddr::from(start + len).align_up_4k().as_usize(); if !VirtAddr::from(start).is_aligned(PAGE_SIZE_4K) || len == 0 { return Err(LinuxError::EINVAL); } - for (&vaddr, page_info) in MEM_MAP.lock().range(start..end) { - if let Some((file, offset, size)) = page_info { + for (&vaddr, page_info) in current().mm.mem_map.lock().range(start..end) { + if let Some(FileInfo { file, offset, size }) = &page_info.mapping_file { let src = vaddr as *mut u8; - write_into(file, src, *offset as u64, *size); + write_into(&file, src, *offset as u64, *size); } } } @@ -351,7 +349,8 @@ pub fn sys_mremap( let mut consistent_vma: Option = None; // structure to verify the consistent in the range of [old_start, old_end) let mut post_remove: Vec = Vec::new(); // vma should be removed if success. - let mut vma_map = VMA_MAP.lock(); + let binding_task = current(); + let mut vma_map = binding_task.mm.vma_map.lock(); // collect and check vma alongside the range of [old_start, old_end). let mut node = vma_map.upper_bound_mut(Bound::Included(&old_start)); while let Some(vma) = node.value_mut() { @@ -452,7 +451,7 @@ pub fn sys_mremap( old_vma.end_addr = new_end; // delete the mapped and swapped page outside of new vma. - release_pages_mapped(new_end, old_end, false); + release_pages_mapped(new_end, old_end); #[cfg(feature = "fs")] release_pages_swaped(new_end, old_end); diff --git a/api/ruxos_posix_api/src/imp/mmap/trap.rs b/api/ruxos_posix_api/src/imp/mmap/trap.rs index 38cdee849..63b343d6f 100644 --- a/api/ruxos_posix_api/src/imp/mmap/trap.rs +++ b/api/ruxos_posix_api/src/imp/mmap/trap.rs @@ -10,30 +10,50 @@ #[cfg(feature = "fs")] use crate::{ ctypes, - imp::mmap::utils::{preload_page_with_swap, read_from, BITMAP_FREE, SWAPED_MAP, SWAP_FILE}, + imp::mmap::utils::{preload_page_with_swap, read_from}, }; #[cfg(not(feature = "fs"))] -use ruxhal::paging::alloc_page_preload; +use ruxmm::paging::alloc_page_preload; +#[cfg(feature = "fs")] +use ruxtask::vma::{BITMAP_FREE, SWAPED_MAP, SWAP_FILE}; -use crate::imp::mmap::utils::{get_mflags_from_usize, MEM_MAP, VMA_MAP}; -use core::{cmp::min, ops::Bound}; +use crate::imp::mmap::utils::get_mflags_from_usize; +use alloc::sync::Arc; +use core::{ + cmp::min, + ops::{Bound, DerefMut}, sync::atomic::{fence, Ordering}, +}; use memory_addr::PAGE_SIZE_4K; use page_table::MappingFlags; use ruxhal::{ - mem::VirtAddr, - paging::{do_pte_map, pte_query}, + mem::{direct_virt_to_phys, VirtAddr}, trap::PageFaultCause, }; +use ruxtask::{ + current, + vma::{FileInfo, PageInfo}, +}; + +use ruxmm::paging::{do_pte_map, pte_query, pte_update_page}; struct TrapHandlerImpl; #[crate_interface::impl_interface] impl ruxhal::trap::TrapHandler for TrapHandlerImpl { fn handle_page_fault(vaddr: usize, cause: PageFaultCause) -> bool { - let vma_map = VMA_MAP.lock(); + // warn!("----->handle_page_fault: vaddr=0x{:x?}, cause={:?}", vaddr, cause); + + // debug!("handle_page_fault: vaddr=0x{:x?}, cause={:?}", vaddr, cause); + let binding_task = current(); + let mut binding_mem_map = binding_task.mm.vma_map.lock(); + let vma_map = binding_mem_map.deref_mut(); if let Some(vma) = vma_map.upper_bound(Bound::Included(&vaddr)).value() { // Check if page existing in the vma, go to panic if not. if vma.end_addr <= vaddr { + error!( + "Page Fault not match: vaddr=0x{:x?}, cause={:?}", + vaddr, cause + ); return false; } @@ -49,31 +69,53 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl { ); // Check if the access meet the prot - match cause { - PageFaultCause::INSTRUCTION if !map_flag.contains(MappingFlags::EXECUTE) => { - return false - } - PageFaultCause::READ if !map_flag.contains(MappingFlags::READ) => return false, - PageFaultCause::WRITE if !map_flag.contains(MappingFlags::WRITE) => return false, - _ => {} - } - - // In a multi-threaded situation, it is possible that multiple threads - // simultaneously trigger a page miss interrupt on the same page, - // resulting in the page being actually mapped and causing an `AlreadyMap` - // error - if pte_query(VirtAddr::from(vaddr)).is_ok() { - return true; + if !map_flag.contains(cause.into()) { + error!( + "Page Fault: Access violation, vaddr:0x{:x?}, cause:{:?}", + vaddr, cause + ); + return false; } - let mut memory_map = MEM_MAP.lock(); + let binding_task = current(); + let mut binding_mem_map = binding_task.mm.mem_map.lock(); + let memory_map = binding_mem_map.deref_mut(); used_fs! { let mut swaped_map = SWAPED_MAP.lock(); let mut off_pool = BITMAP_FREE.lock(); } + // In a multi-threaded situation, it is possible that multiple threads + // simultaneously trigger a page miss interrupt on the same page, + // resulting in the page being actually mapped and causing an `AlreadyMap` + // error + let query_result = pte_query(VirtAddr::from(vaddr)); + let mem_item = memory_map.get(&vaddr); + let is_cow = if let Ok((_, mapping_flags, _)) = query_result { + assert!(mem_item.is_some()); + // Check if: + // 1. the page is mapped by another thread. + if mapping_flags.contains(cause.into()) { + return true; + } + // 2. the page is in Copy-on-Write mode so that it's set in read-only mode; + assert!(mapping_flags.contains(MappingFlags::READ)); + assert!(!mapping_flags.contains(MappingFlags::WRITE)); + let mem_arc = mem_item.unwrap(); + if Arc::strong_count(mem_arc).eq(&1) { + // the last owner of the page, we can safely map it. + pte_update_page(vaddr.into(), None, Some(map_flag)) + .expect("failed to update page table entry"); + return true; + } + true + } else { + // no page table entry found, it means the page is not mapped yet. + false + }; + // Due to the existence of only one page table in ruxos, in - // order to prevent data competition in multi-threaded environ- + // order to prevent data race in multi-threaded environ- // -ments caused by adding the current virtual address to the // page table, it is necessary to first map the physical address // that needs to be mapped to another virtual address, and then @@ -85,66 +127,110 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl { #[cfg(not(feature = "fs"))] let fake_vaddr = alloc_page_preload().expect("alloc memory for new page failed"); #[cfg(feature = "fs")] - let fake_vaddr = - preload_page_with_swap(&mut memory_map, &mut swaped_map, &mut off_pool); + let fake_vaddr = preload_page_with_swap(memory_map, &mut swaped_map, &mut off_pool); // Fill target data to assigned physical addresses, from file or zero according to mapping type let dst: *mut u8 = fake_vaddr.as_mut_ptr(); - #[cfg(feature = "fs")] - { - if let Some(off) = swaped_map.remove(&vaddr) { - off_pool.push(off); - read_from(&SWAP_FILE, dst, off as u64, size); - } else if let Some(file) = &vma.file { - let off = (vma.offset + (vaddr - vma.start_addr)) as u64; - read_from(file, dst, off, size); - } else { - // Set page to 0 for anonymous mapping - // - // Safe because the page memory is allocated here - // and the page fault exception has not exited. - unsafe { - dst.write_bytes(0, size); + + if !is_cow { + // get here if the page is belong to current process + #[cfg(feature = "fs")] + { + if let Some(swap_info) = swaped_map.remove(&vaddr) { + read_from(&SWAP_FILE, dst, swap_info.offset as u64, size); + } else if let Some(file) = &vma.file { + let off = (vma.offset + (vaddr - vma.start_addr)) as u64; + read_from(file, dst, off, size); + } else { + // Set page to 0 for anonymous mapping + // + // Safe because the page memory is allocated here + // and the page fault exception has not exited. + unsafe { + dst.write_bytes(0, size); + } } } - } - // Set page to 0 for anonymous mapping - // - // Safe because the page memory is allocated here - // and the page fault exception has not exited. - #[cfg(not(feature = "fs"))] - unsafe { - dst.write_bytes(0, size); - } + // Set page to 0 for anonymous mapping + // + // Safe because the page memory is allocated here + // and the page fault exception has not exited. + #[cfg(not(feature = "fs"))] + unsafe { + dst.write_bytes(0, size); + } - // Insert the record into `MEM_MAP` with write-back information(`None` if no need to write-back). - #[cfg(feature = "fs")] - if (vma.prot & ctypes::PROT_WRITE != 0) - && (vma.flags & ctypes::MAP_PRIVATE == 0) - && (vma.file.is_some()) - { - let map_length = min(PAGE_SIZE_4K, vma.end_addr - vaddr); - let offset = vma.offset + (vaddr - vma.start_addr); + // Insert the record into `MEM_MAP` with write-back information(`None` if no need to write-back). + #[cfg(feature = "fs")] + if (vma.prot & ctypes::PROT_WRITE != 0) + && (vma.flags & ctypes::MAP_PRIVATE == 0) + && (vma.file.is_some()) + { + let map_length = min(PAGE_SIZE_4K, vma.end_addr - vaddr); + let offset = vma.offset + (vaddr - vma.start_addr); + let file_info = FileInfo { + file: vma.file.as_ref().unwrap().clone(), + offset, + size: map_length, + }; + let page_info = PageInfo { + paddr: direct_virt_to_phys(fake_vaddr), + mapping_file: Some(file_info), + }; + memory_map.insert(vaddr, Arc::new(page_info)); + } else { + memory_map.insert( + vaddr, + Arc::new(PageInfo { + paddr: direct_virt_to_phys(fake_vaddr), + mapping_file: None, + }), + ); + } + #[cfg(not(feature = "fs"))] memory_map.insert( vaddr, - Some((vma.file.as_ref().unwrap().clone(), offset, map_length)), + Arc::new(PageInfo { + paddr: direct_virt_to_phys(VirtAddr::from(fake_vaddr)), + }), ); - } else { - memory_map.insert(vaddr, None); - } - #[cfg(not(feature = "fs"))] - memory_map.insert(vaddr, None); - // Do actual mmapping for target vaddr - // - // Note: other threads can access this page of memory after this code. - match do_pte_map(VirtAddr::from(vaddr), fake_vaddr, map_flag) { - Ok(()) => true, - Err(_) => false, + // Do actual mmapping for target vaddr + // + // Note: other threads can access this page of memory after this code. + match do_pte_map(VirtAddr::from(vaddr), fake_vaddr, map_flag) { + Ok(()) => true, + Err(_) => false, + } + } else { + // get here if the page is belong to current process and is in Copy-on-Write mode. + unsafe { + dst.copy_from(vaddr as *mut u8, size); + } + let paddr = direct_virt_to_phys(fake_vaddr); + let mapping_file = memory_map + .get(&vaddr.into()) + .unwrap() + .mapping_file + .clone(); + memory_map.remove(&vaddr.into()); + memory_map.insert( + vaddr.into(), + Arc::new(PageInfo { + paddr, + mapping_file, + }), + ); + fence(Ordering::SeqCst); + // Update the page table entry to map the physical address of the fake virtual address. + match pte_update_page(vaddr.into(), Some(paddr), Some(map_flag)) { + Ok(()) => true, + Err(_) => false, + } } } else { - warn!("vaddr=0x{:x?},cause=0x{:x?}", vaddr, cause); + warn!("vaddr={:#x?},cause={:#x?}", vaddr, cause); false } } diff --git a/api/ruxos_posix_api/src/imp/mmap/utils.rs b/api/ruxos_posix_api/src/imp/mmap/utils.rs index ff4729a67..51feca299 100644 --- a/api/ruxos_posix_api/src/imp/mmap/utils.rs +++ b/api/ruxos_posix_api/src/imp/mmap/utils.rs @@ -10,121 +10,41 @@ use crate::ctypes; #[cfg(feature = "fs")] -use {crate::imp::fs::File, alloc::sync::Arc, page_table::PagingError, ruxfs::fops::OpenOptions}; +use {alloc::sync::Arc, page_table::PagingError, ruxtask::fs::File}; use alloc::{collections::BTreeMap, vec::Vec}; -use axsync::Mutex; use core::{ cmp::{max, min}, - ops::Bound, + ops::{Bound, DerefMut}, }; use memory_addr::PAGE_SIZE_4K; use page_table::MappingFlags; -use ruxhal::{ - mem::VirtAddr, - paging::{alloc_page_preload, do_pte_map, pte_query, pte_swap_preload, pte_unmap_page}, -}; +use ruxhal::mem::VirtAddr; +use ruxmm::paging::{alloc_page_preload, do_pte_map, pte_query, pte_swap_preload, pte_unmap_page}; +use ruxtask::vma::{FileInfo, PageInfo, SwapInfo, BITMAP_FREE, SWAPED_MAP, SWAP_FILE}; +use ruxtask::{current, vma::Vma}; + +pub(crate) const VMA_START: usize = ruxconfig::MMAP_START_VADDR; +pub(crate) const VMA_END: usize = ruxconfig::MMAP_END_VADDR; // use `used_fs` instead of `#[cfg(feature = "fs")]{}` to cancel the scope of code. #[cfg(feature = "fs")] macro_rules! used_fs { - ($($code:tt)*) => {$($code)*}; - } + ($($code:tt)*) => {$($code)*}; + } #[cfg(not(feature = "fs"))] macro_rules! used_fs { ($($code:tt)*) => {}; } -pub(crate) const VMA_START: usize = ruxconfig::MMAP_START_VADDR; -pub(crate) const VMA_END: usize = ruxconfig::MMAP_END_VADDR; - -// TODO: move defination of `SWAP_MAX` and `SWAP_PATH` from const numbers to `ruxconfig`. -used_fs! { - pub(crate) const SWAP_MAX: usize = 1024 * 1024 * 1024; - pub(crate) const SWAP_PATH: &str = "swap.raw\0"; - pub(crate) static SWAPED_MAP: Mutex> = Mutex::new(BTreeMap::new()); // Vaddr => (page_size, offset_at_swaped) - lazy_static::lazy_static! { - pub(crate) static ref SWAP_FILE: Arc = open_swap_file(SWAP_PATH); - pub(crate) static ref BITMAP_FREE: Mutex> = Mutex::new((0..SWAP_MAX).step_by(PAGE_SIZE_4K).collect()); - } -} - -pub(crate) static VMA_MAP: Mutex> = Mutex::new(BTreeMap::new()); // start_addr -pub(crate) static MEM_MAP: Mutex> = Mutex::new(BTreeMap::new()); // Vaddr => (fid, offset, page_size) - -#[cfg(feature = "fs")] -type PageInfo = Option<(Arc, Offset, Len)>; // (fid, offset, page_size) -#[cfg(not(feature = "fs"))] -type PageInfo = Option; // (fid, offset, page_size) -#[cfg(feature = "fs")] -type Offset = usize; -type Len = usize; - -/// Data structure for mapping [start_addr, end_addr) with meta data. -pub(crate) struct Vma { - pub start_addr: usize, - pub end_addr: usize, - #[cfg(feature = "fs")] - pub file: Option>, - pub offset: usize, - pub prot: u32, - pub flags: u32, -} - -/// Impl for Vma. -impl Vma { - pub(crate) fn new(_fid: i32, offset: usize, prot: u32, flags: u32) -> Self { - #[cfg(feature = "fs")] - let file = if _fid < 0 { - None - } else { - Some(File::from_fd(_fid).expect("should be effective fid")) - }; - Vma { - start_addr: 0, - end_addr: 0, - #[cfg(feature = "fs")] - file, - offset, - flags, - prot, - } - } - - pub(crate) fn clone_from(vma: &Vma, start_addr: usize, end_addr: usize) -> Self { - Vma { - start_addr, - end_addr, - #[cfg(feature = "fs")] - file: vma.file.clone(), - offset: vma.offset, - prot: vma.prot, - flags: vma.prot, - } - } -} - -/// open target file -#[cfg(feature = "fs")] -fn open_swap_file(filename: &str) -> Arc { - let mut opt = OpenOptions::new(); - opt.read(true); - opt.write(true); - opt.append(true); - opt.create(true); - - let file = ruxfs::fops::File::open(filename, &opt).expect("create swap file failed"); - Arc::new(File::new(file)) -} - /// read from target file #[cfg(feature = "fs")] pub(crate) fn read_from(file: &Arc, buf: *mut u8, offset: u64, len: usize) { let src = unsafe { core::slice::from_raw_parts_mut(buf, len) }; let actual_len = file .inner - .lock() + .read() .read_at(offset, src) .expect("read_from failed"); if len != actual_len { @@ -138,7 +58,7 @@ pub(crate) fn write_into(file: &Arc, buf: *mut u8, offset: u64, len: usize let src = unsafe { core::slice::from_raw_parts_mut(buf, len) }; let actual_len = file .inner - .lock() + .write() .write_at(offset, src) .expect("write_into failed"); if len != actual_len { @@ -204,11 +124,9 @@ pub(crate) fn find_free_region( } // Search free region on the top of VMA_LISTS first. - const ALIGN_PAGE_MASK: usize = !(PAGE_SIZE_4K - 1); if let Some((_, last_vma)) = vma_map.last_key_value() { - let end_boundry = (last_vma.end_addr + PAGE_SIZE_4K) & ALIGN_PAGE_MASK; - if (VMA_END - end_boundry) & ALIGN_PAGE_MASK >= len { - return Some(end_boundry); + if VMA_END - last_vma.end_addr >= len { + return Some(last_vma.end_addr); } } else if VMA_END >= VMA_START + len { return Some(VMA_START); @@ -276,7 +194,7 @@ pub(crate) fn snatch_fixed_region( } // delete the mapped and swapped page. - release_pages_mapped(start, end, true); + release_pages_mapped(start, end); #[cfg(feature = "fs")] release_pages_swaped(start, end); @@ -285,16 +203,15 @@ pub(crate) fn snatch_fixed_region( /// release the range of [start, end) in mem_map /// take care of AA-deadlock, this function should not be used after `MEM_MAP` is used. -pub(crate) fn release_pages_mapped(start: usize, end: usize, writeback: bool) { - let mut memory_map = MEM_MAP.lock(); +pub(crate) fn release_pages_mapped(start: usize, end: usize) { + let binding = current(); + let mut memory_map = binding.mm.mem_map.lock(); let mut removing_vaddr = Vec::new(); - for (&vaddr, _page_info) in memory_map.range(start..end) { + for (&vaddr, page_info) in memory_map.range(start..end) { #[cfg(feature = "fs")] - if writeback { - if let Some((file, offset, size)) = _page_info { - let src = vaddr as *mut u8; - write_into(file, src, *offset as u64, *size); - } + if let Some(FileInfo { file, offset, size }) = &page_info.mapping_file { + let src = vaddr as *mut u8; + write_into(&file, src, *offset as u64, *size); } if pte_unmap_page(VirtAddr::from(vaddr)).is_err() { panic!("Release page failed when munmapping!"); @@ -311,12 +228,10 @@ pub(crate) fn release_pages_mapped(start: usize, end: usize, writeback: bool) { #[cfg(feature = "fs")] pub(crate) fn release_pages_swaped(start: usize, end: usize) { let mut swap_map = SWAPED_MAP.lock(); - let mut off_pool = BITMAP_FREE.lock(); let mut removing_vaddr = Vec::new(); - for (&vaddr, &off) in swap_map.range(start..end) { + for (&vaddr, _) in swap_map.range(start..end) { removing_vaddr.push(vaddr); - off_pool.push(off); } for vaddr in removing_vaddr { swap_map.remove(&vaddr); @@ -326,7 +241,9 @@ pub(crate) fn release_pages_swaped(start: usize, end: usize) { /// shift mapped the page in both MEM_MAP and SWAPED_MAP. /// No page fault here should be guaranteed pub(crate) fn shift_mapped_page(start: usize, end: usize, vma_offset: usize, copy: bool) { - let mut memory_map = MEM_MAP.lock(); + let binding_task = current(); + let mut binding_mem_map = binding_task.mm.mem_map.lock(); + let memory_map = binding_mem_map.deref_mut(); used_fs! { let mut swaped_map = SWAPED_MAP.lock(); let mut off_pool = BITMAP_FREE.lock(); @@ -350,8 +267,7 @@ pub(crate) fn shift_mapped_page(start: usize, end: usize, vma_offset: usize, cop #[cfg(not(feature = "fs"))] let fake_vaddr = alloc_page_preload().expect("alloc memory for new page failed"); #[cfg(feature = "fs")] - let fake_vaddr = - preload_page_with_swap(&mut memory_map, &mut swaped_map, &mut off_pool); + let fake_vaddr = preload_page_with_swap(memory_map, &mut swaped_map, &mut off_pool); let dst = unsafe { core::slice::from_raw_parts_mut(fake_vaddr.as_usize() as *mut u8, PAGE_SIZE_4K) @@ -361,12 +277,12 @@ pub(crate) fn shift_mapped_page(start: usize, end: usize, vma_offset: usize, cop let src = unsafe { core::slice::from_raw_parts_mut(start as *mut u8, PAGE_SIZE_4K) }; dst.clone_from_slice(src); - } else if page_info.is_none() + } else if page_info.mapping_file.is_none() /* has been swapped from memory */ { used_fs! { - let offset = swaped_map.get(&start).unwrap(); - read_from(&SWAP_FILE, start as *mut u8, *offset as u64, PAGE_SIZE_4K); + let swap_info = swaped_map.get(&start).unwrap(); + read_from(&SWAP_FILE, start as *mut u8, swap_info.offset as u64, PAGE_SIZE_4K); } } (fake_vaddr, flags) @@ -377,10 +293,10 @@ pub(crate) fn shift_mapped_page(start: usize, end: usize, vma_offset: usize, cop used_fs! { let mut opt_buffer = Vec::new(); - for (&start, &off_in_swap) in swaped_map.range(start..end) { - opt_buffer.push((start, off_in_swap)); + for (&start, &ref off_in_swap) in swaped_map.range(start..end) { + opt_buffer.push((start, off_in_swap.clone())); } - for (start, off_in_swap) in opt_buffer { + for (start, swap_info) in opt_buffer { // opt for the swapped file, should copy swaped page for the new page. if !copy { swaped_map.remove(&start); @@ -389,10 +305,10 @@ pub(crate) fn shift_mapped_page(start: usize, end: usize, vma_offset: usize, cop .pop() .expect("There are no free space in swap-file!"); let mut rw_buffer: [u8; PAGE_SIZE_4K] = [0_u8; PAGE_SIZE_4K]; - read_from(&SWAP_FILE, rw_buffer.as_mut_ptr(), off_in_swap as u64, PAGE_SIZE_4K); + read_from(&SWAP_FILE, rw_buffer.as_mut_ptr(), swap_info.offset as u64, PAGE_SIZE_4K); write_into(&SWAP_FILE, rw_buffer.as_mut_ptr(), off_ptr as u64, PAGE_SIZE_4K); } - swaped_map.insert(start + vma_offset, off_in_swap); + swaped_map.insert(start + vma_offset, swap_info.clone()); } } } @@ -402,8 +318,8 @@ pub(crate) fn shift_mapped_page(start: usize, end: usize, vma_offset: usize, cop /// of a virtual address that is also mapped to the allocated physical address. #[cfg(feature = "fs")] pub(crate) fn preload_page_with_swap( - memory_map: &mut BTreeMap, - swaped_map: &mut BTreeMap, + memory_map: &mut BTreeMap>, + swaped_map: &mut BTreeMap>, off_pool: &mut Vec, ) -> VirtAddr { match alloc_page_preload() { @@ -411,27 +327,47 @@ pub(crate) fn preload_page_with_swap( // Try to swap the mapped memory into Disk and use this segment of physical memory #[cfg(feature = "fs")] Err(PagingError::NoMemory) => match memory_map.pop_first() { - // For file mapping, the mapped content will be written directly to the original file. - Some((vaddr_swapped, Some((file, offset, size)))) => { - let offset = offset.try_into().unwrap(); - write_into(&file, vaddr_swapped as *mut u8, offset, size); - pte_swap_preload(VirtAddr::from(vaddr_swapped)).unwrap() + // Some((vaddr_swapped, PageInfo{paddr:_, mapping_file:Some(FileInfo{file, offset, size})})) => { + Some((vaddr_swapped, page_info)) => { + match &page_info.mapping_file { + // For file mapping, the mapped content will be written directly to the original file. + Some(FileInfo { file, offset, size }) => { + let offset = *offset as u64; + write_into(&file, vaddr_swapped as *mut u8, offset, *size); + pte_swap_preload(VirtAddr::from(vaddr_swapped)).unwrap() + } + // For anonymous mapping, you need to save the mapped memory to the prepared swap file, + // and record the memory address and its offset in the swap file. + None => { + let offset_get = off_pool.pop(); + let offset = offset_get.unwrap(); + swaped_map.insert(vaddr_swapped, Arc::new(offset.into())); + + write_into( + &SWAP_FILE, + vaddr_swapped as *mut u8, + offset as u64, + PAGE_SIZE_4K, + ); + pte_swap_preload(VirtAddr::from(vaddr_swapped)).unwrap() + } + } } // For anonymous mapping, you need to save the mapped memory to the prepared swap file, // and record the memory address and its offset in the swap file. - Some((vaddr_swapped, None)) => { - let offset_get = off_pool.pop(); - let offset = offset_get.unwrap(); - swaped_map.insert(vaddr_swapped, offset); - - write_into( - &SWAP_FILE, - vaddr_swapped as *mut u8, - offset as u64, - PAGE_SIZE_4K, - ); - pte_swap_preload(VirtAddr::from(vaddr_swapped)).unwrap() - } + // Some((vaddr_swapped, PageInfo{paddr:_, mapping_file:Some(FileInfo{file, offset, size})})) => { + // let offset_get = off_pool.pop(); + // let offset = offset_get.unwrap(); + // swaped_map.insert(vaddr_swapped, Arc::new(offset)); + + // write_into( + // &SWAP_FILE, + // vaddr_swapped as *mut u8, + // offset as u64, + // PAGE_SIZE_4K, + // ); + // pte_swap_preload(VirtAddr::from(vaddr_swapped)).unwrap() + // } _ => panic!("No memory for mmap, check if huge memory leaky exists"), }, diff --git a/api/ruxos_posix_api/src/imp/net.rs b/api/ruxos_posix_api/src/imp/net.rs index ef8465aba..c284ad06f 100644 --- a/api/ruxos_posix_api/src/imp/net.rs +++ b/api/ruxos_posix_api/src/imp/net.rs @@ -28,11 +28,11 @@ pub enum Socket { impl Socket { fn add_to_fd_table(self) -> LinuxResult { - super::fd_ops::add_file_like(Arc::new(self)) + ruxtask::fs::add_file_like(Arc::new(self)) } fn from_fd(fd: c_int) -> LinuxResult> { - let f = super::fd_ops::get_file_like(fd)?; + let f = ruxtask::fs::get_file_like(fd)?; f.into_any() .downcast::() .map_err(|_| LinuxError::EINVAL) diff --git a/api/ruxos_posix_api/src/imp/pipe.rs b/api/ruxos_posix_api/src/imp/pipe.rs index 7a67120ff..8b61a3f1a 100644 --- a/api/ruxos_posix_api/src/imp/pipe.rs +++ b/api/ruxos_posix_api/src/imp/pipe.rs @@ -15,8 +15,8 @@ use axio::PollState; use axsync::Mutex; use ruxfdtable::{FileLike, RuxStat}; -use super::fd_ops::{add_file_like, close_file_like}; use crate::{ctypes, sys_fcntl}; +use ruxtask::fs::{add_file_like, close_file_like}; #[derive(Copy, Clone, PartialEq)] enum RingBufferStatus { diff --git a/api/ruxos_posix_api/src/imp/pthread/mod.rs b/api/ruxos_posix_api/src/imp/pthread/mod.rs index 6d95dae34..b0bfd0218 100644 --- a/api/ruxos_posix_api/src/imp/pthread/mod.rs +++ b/api/ruxos_posix_api/src/imp/pthread/mod.rs @@ -92,7 +92,7 @@ impl Pthread { inner: task_inner, retval: my_packet, }; - let ptr = Box::into_raw(Box::new(thread)) as *mut c_void; + let ptr: *mut c_void = Box::into_raw(Box::new(thread)) as *mut c_void; TID_TO_PTHREAD.write().insert(tid, ForceSendSync(ptr)); Ok(ptr) } @@ -119,7 +119,6 @@ impl Pthread { }; let task_inner = ruxtask::pspawn(main, tls as usize, set_tid, tl); - let tid = task_inner.id().as_u64(); let thread = Pthread { inner: task_inner.clone(), @@ -132,6 +131,7 @@ impl Pthread { fn current_ptr() -> *mut Pthread { let tid = ruxtask::current().id().as_u64(); + error!("current_ptr, tid: {}", tid); match TID_TO_PTHREAD.read().get(&tid) { None => core::ptr::null_mut(), Some(ptr) => ptr.0 as *mut Pthread, @@ -263,41 +263,60 @@ pub unsafe fn sys_clone( ); syscall_body!(sys_clone, { - if (flags as u32 & ctypes::CLONE_THREAD) == 0 { - debug!("ONLY support thread"); - return Err(LinuxError::EINVAL); - } - - let func = unsafe { - core::mem::transmute::<*const (), extern "C" fn(arg: *mut c_void) -> *mut c_void>( - (*(stack as *mut usize)) as *const (), - ) - }; - let args = unsafe { *((stack as usize + 8) as *mut usize) } as *mut c_void; - - let set_tid = if (flags as u32 & ctypes::CLONE_CHILD_SETTID) != 0 { - core::sync::atomic::AtomicU64::new(ctid as _) + if (flags as u32 & ctypes::CLONE_THREAD) != 0 { + let func = unsafe { + core::mem::transmute::<*const (), extern "C" fn(arg: *mut c_void) -> *mut c_void>( + (*(stack as *mut usize)) as *const (), + ) + }; + let args = unsafe { *((stack as usize + 8) as *mut usize) } as *mut c_void; + + let set_tid = if (flags as u32 & ctypes::CLONE_CHILD_SETTID) != 0 { + core::sync::atomic::AtomicU64::new(ctid as _) + } else { + core::sync::atomic::AtomicU64::new(0) + }; + + let (tid, task_inner) = Pthread::pcreate( + core::ptr::null(), + func, + args, + tls, + set_tid, + core::sync::atomic::AtomicU64::from(ctid as u64), + )?; + + // write tid to ptid + if (flags as u32 & ctypes::CLONE_PARENT_SETTID) != 0 { + unsafe { *ptid = tid as c_int }; + } + ruxtask::put_task(task_inner); + + return Ok(tid); + } else if (flags as u32 & ctypes::SIGCHLD) != 0 { + TID_TO_PTHREAD.read(); + let pid = if let Some(task_ref) = ruxtask::fork_task() { + warn!("fork_task success, pid: {}", task_ref.id().as_u64()); + task_ref.id().as_u64() + } else { + let children_ref = ruxtask::current(); + let tid = children_ref.id().as_u64(); + let thread = Pthread { + inner: children_ref.clone(), + retval: Arc::new(Packet { + result: UnsafeCell::new(core::ptr::null_mut()), + }), + }; + let ptr = Box::into_raw(Box::new(thread)) as *mut c_void; + TID_TO_PTHREAD.write().insert(tid, ForceSendSync(ptr)); + 0 + }; + warn!("will sys_clone <= pid: {}", pid); + return Ok(pid); } else { - core::sync::atomic::AtomicU64::new(0) - }; - - let (tid, task_inner) = Pthread::pcreate( - core::ptr::null(), - func, - args, - tls, - set_tid, - core::sync::atomic::AtomicU64::from(ctid as u64), - )?; - - // write tid to ptid - if (flags as u32 & ctypes::CLONE_PARENT_SETTID) != 0 { - unsafe { *ptid = tid as c_int }; + debug!("ONLY support CLONE_THREAD and SIGCHLD"); + return Err(LinuxError::EINVAL); } - - ruxtask::put_task(task_inner); - - Ok(tid) }) } diff --git a/api/ruxos_posix_api/src/imp/resources.rs b/api/ruxos_posix_api/src/imp/resources.rs index f3e05da73..d7cab055e 100644 --- a/api/ruxos_posix_api/src/imp/resources.rs +++ b/api/ruxos_posix_api/src/imp/resources.rs @@ -56,8 +56,8 @@ pub unsafe fn sys_getrlimit(resource: c_int, rlimits: *mut ctypes::rlimit) -> c_ }, #[cfg(feature = "fd")] ctypes::RLIMIT_NOFILE => unsafe { - (*rlimits).rlim_cur = ruxfdtable::RUX_FILE_LIMIT as _; - (*rlimits).rlim_max = ruxfdtable::RUX_FILE_LIMIT as _; + (*rlimits).rlim_cur = ruxtask::fs::RUX_FILE_LIMIT as _; + (*rlimits).rlim_max = ruxtask::fs::RUX_FILE_LIMIT as _; }, ctypes::RLIMIT_MEMLOCK => {} ctypes::RLIMIT_AS => {} diff --git a/api/ruxos_posix_api/src/imp/task.rs b/api/ruxos_posix_api/src/imp/task.rs index 2ad8ff009..dc202bdb0 100644 --- a/api/ruxos_posix_api/src/imp/task.rs +++ b/api/ruxos_posix_api/src/imp/task.rs @@ -41,7 +41,7 @@ pub fn sys_gettid() -> c_int { /// Get current process ID. pub fn sys_getpid() -> c_int { - syscall_body!(sys_getpid, Ok(2)) + syscall_body!(sys_getpid, Ok(ruxtask::current().id().as_u64() as c_int)) } /// Get parent process's ID. diff --git a/modules/ruxdriver/Cargo.toml b/modules/ruxdriver/Cargo.toml index b90a247c6..68dc98e16 100644 --- a/modules/ruxdriver/Cargo.toml +++ b/modules/ruxdriver/Cargo.toml @@ -39,6 +39,7 @@ default = ["bus-mmio"] [dependencies] log = "0.4" cfg-if = "1.0" +crate_interface = "0.1.1" driver_common = { path = "../../crates/driver_common" } driver_block = { path = "../../crates/driver_block", optional = true } driver_net = { path = "../../crates/driver_net", optional = true } @@ -46,6 +47,10 @@ driver_display = { path = "../../crates/driver_display", optional = true } driver_9p = { path = "../../crates/driver_9p", optional = true } driver_pci = { path = "../../crates/driver_pci", optional = true } driver_virtio = { path = "../../crates/driver_virtio", optional = true } + axalloc = { path = "../axalloc", optional = true } ruxhal = { path = "../ruxhal", optional = true } ruxconfig = { path = "../ruxconfig", optional = true } + + + diff --git a/modules/ruxdriver/src/lib.rs b/modules/ruxdriver/src/lib.rs index ad3d32ce8..be11d76ae 100644 --- a/modules/ruxdriver/src/lib.rs +++ b/modules/ruxdriver/src/lib.rs @@ -82,7 +82,7 @@ mod dummy; mod structs; #[cfg(feature = "virtio")] -mod virtio; +pub mod virtio; #[cfg(feature = "ixgbe")] mod ixgbe; diff --git a/modules/ruxdriver/src/virtio.rs b/modules/ruxdriver/src/virtio.rs index ec97412b5..b13e98a75 100644 --- a/modules/ruxdriver/src/virtio.rs +++ b/modules/ruxdriver/src/virtio.rs @@ -14,7 +14,7 @@ use axalloc::global_allocator; use cfg_if::cfg_if; use driver_common::{BaseDriverOps, DevResult, DeviceType}; use driver_virtio::{BufferDirection, PhysAddr, VirtIoHal}; -use ruxhal::mem::{direct_virt_to_phys, phys_to_virt, virt_to_phys}; +use ruxhal::mem::{direct_virt_to_phys, phys_to_virt, VirtAddr}; use crate::{drivers::DriverProbe, AxDeviceEnum}; @@ -162,6 +162,13 @@ impl DriverProbe for VirtIoDriver { } } +#[crate_interface::def_interface] +pub trait AddressTranslate { + fn virt_to_phys(vaddr: VirtAddr) -> Option { + Some(direct_virt_to_phys(vaddr).into()) + } +} + pub struct VirtIoHalImpl; unsafe impl VirtIoHal for VirtIoHalImpl { @@ -189,7 +196,9 @@ unsafe impl VirtIoHal for VirtIoHalImpl { #[inline] unsafe fn share(buffer: NonNull<[u8]>, _direction: BufferDirection) -> PhysAddr { let vaddr = buffer.as_ptr() as *mut u8 as usize; - virt_to_phys(vaddr.into()).into() + let paddr = + crate_interface::call_interface!(AddressTranslate::virt_to_phys, VirtAddr::from(vaddr)); + paddr.unwrap() } #[inline] diff --git a/modules/ruxfdtable/src/lib.rs b/modules/ruxfdtable/src/lib.rs index 275ef96f2..0514c1bca 100644 --- a/modules/ruxfdtable/src/lib.rs +++ b/modules/ruxfdtable/src/lib.rs @@ -16,9 +16,8 @@ use core::marker::Sync; use axerrno::LinuxResult; use axio::PollState; -use flatten_objects::FlattenObjects; -use spin::RwLock; +#[derive(Default)] ///Rust version for struct timespec in ctypes. Represents a high-resolution time specification. pub struct RuxTimeSpec { /// Whole seconds part of the timespec. @@ -29,6 +28,7 @@ pub struct RuxTimeSpec { ///Rust version for struct stat in ctypes. Represents file status information. #[cfg(target_arch = "aarch64")] +#[derive(Default)] pub struct RuxStat { /// Device identifier. pub st_dev: u64, @@ -125,13 +125,3 @@ pub trait FileLike: Send + Sync { /// Sets or clears the non-blocking I/O mode for the file-like object. fn set_nonblocking(&self, nonblocking: bool) -> LinuxResult; } -/// Maximum number of files per process -pub const RUX_FILE_LIMIT: usize = 1024; - -lazy_static::lazy_static! { - /// Global file descriptor table protected by a read-write lock. - pub static ref FD_TABLE: RwLock, RUX_FILE_LIMIT>> = { - let fd_table = FlattenObjects::new(); - RwLock::new(fd_table) - }; -} diff --git a/modules/ruxfs/Cargo.toml b/modules/ruxfs/Cargo.toml index 0ca624616..e255bbb0f 100644 --- a/modules/ruxfs/Cargo.toml +++ b/modules/ruxfs/Cargo.toml @@ -18,7 +18,8 @@ procfs = ["dep:axfs_ramfs"] sysfs = ["dep:axfs_ramfs"] etcfs = ["dep:axfs_ramfs"] fatfs = ["dep:fatfs"] -myfs = ["dep:crate_interface"] +# myfs = ["dep:crate_interface"] +myfs = [] use-ramdisk = [] alloc = ["axalloc"] fp_simd = [] @@ -36,12 +37,12 @@ axerrno = { path = "../../crates/axerrno" } axfs_vfs = { path = "../../crates/axfs_vfs" } axfs_devfs = { path = "../../crates/axfs_devfs", optional = true } axfs_ramfs = { path = "../../crates/axfs_ramfs", optional = true } -ruxdriver = { path = "../ruxdriver", features = ["block"] } -axsync = { path = "../axsync" } -crate_interface = { version = "0.1.1", optional = true } -axalloc = { path = "../axalloc", optional = true } +crate_interface = { version = "0.1.1" } +spin = "0.9" memory_addr = "0.1.0" +ruxdriver = { path = "../ruxdriver", features = ["block"] } +axalloc = { path = "../axalloc", optional = true } [dependencies.fatfs] git = "https://github.com/syswonder/rust-fatfs.git" rev = "bf8ad02" @@ -57,5 +58,3 @@ features = [ # no std [dev-dependencies] ruxdriver = { path = "../ruxdriver", features = ["block", "ramdisk"] } driver_block = { path = "../../crates/driver_block", features = ["ramdisk"] } -axsync = { path = "../axsync", features = ["multitask"] } -ruxtask = { path = "../ruxtask", features = ["test"] } diff --git a/modules/ruxfs/src/fs/fatfs.rs b/modules/ruxfs/src/fs/fatfs.rs index 3b27b7f6f..93ebaf4d1 100644 --- a/modules/ruxfs/src/fs/fatfs.rs +++ b/modules/ruxfs/src/fs/fatfs.rs @@ -12,8 +12,8 @@ use core::cell::UnsafeCell; use axfs_vfs::{VfsDirEntry, VfsError, VfsNodePerm, VfsResult}; use axfs_vfs::{VfsNodeAttr, VfsNodeOps, VfsNodeRef, VfsNodeType, VfsOps}; -use axsync::Mutex; use fatfs::{Dir, File, LossyOemCpConverter, NullTimeProvider, Read, Seek, SeekFrom, Write}; +use spin::RwLock; use crate::dev::Disk; @@ -24,7 +24,7 @@ pub struct FatFileSystem { root_dir: UnsafeCell>, } -pub struct FileWrapper<'a>(Mutex>); +pub struct FileWrapper<'a>(RwLock>); pub struct DirWrapper<'a>(Dir<'a, Disk, NullTimeProvider, LossyOemCpConverter>); unsafe impl Sync for FatFileSystem {} @@ -63,7 +63,7 @@ impl FatFileSystem { } fn new_file(file: File<'_, Disk, NullTimeProvider, LossyOemCpConverter>) -> Arc { - Arc::new(FileWrapper(Mutex::new(file))) + Arc::new(FileWrapper(RwLock::new(file))) } fn new_dir(dir: Dir<'_, Disk, NullTimeProvider, LossyOemCpConverter>) -> Arc { @@ -75,11 +75,11 @@ impl VfsNodeOps for FileWrapper<'static> { axfs_vfs::impl_vfs_non_dir_default! {} fn fsync(&self) -> VfsResult { - self.0.lock().flush().map_err(as_vfs_err) + self.0.write().flush().map_err(as_vfs_err) } fn get_attr(&self) -> VfsResult { - let size = self.0.lock().seek(SeekFrom::End(0)).map_err(as_vfs_err)?; + let size = self.0.write().seek(SeekFrom::End(0)).map_err(as_vfs_err)?; let blocks = (size + BLOCK_SIZE as u64 - 1) / BLOCK_SIZE as u64; // FAT fs doesn't support permissions, we just set everything to 755 let perm = VfsNodePerm::from_bits_truncate(0o755); @@ -87,7 +87,7 @@ impl VfsNodeOps for FileWrapper<'static> { } fn read_at(&self, offset: u64, buf: &mut [u8]) -> VfsResult { - let mut file = self.0.lock(); + let mut file = self.0.write(); file.seek(SeekFrom::Start(offset)).map_err(as_vfs_err)?; let mut total_read = 0; @@ -103,7 +103,7 @@ impl VfsNodeOps for FileWrapper<'static> { } fn write_at(&self, offset: u64, buf: &[u8]) -> VfsResult { - let mut file = self.0.lock(); + let mut file = self.0.write(); file.seek(SeekFrom::Start(offset)).map_err(as_vfs_err)?; // TODO: more efficient let mut total_write = 0; @@ -119,7 +119,7 @@ impl VfsNodeOps for FileWrapper<'static> { } fn truncate(&self, size: u64) -> VfsResult { - let mut file = self.0.lock(); + let mut file = self.0.write(); file.seek(SeekFrom::Start(size)).map_err(as_vfs_err)?; // TODO: more efficient file.truncate().map_err(as_vfs_err) } diff --git a/modules/ruxfs/src/lib.rs b/modules/ruxfs/src/lib.rs index 6db931721..4c869ce11 100644 --- a/modules/ruxfs/src/lib.rs +++ b/modules/ruxfs/src/lib.rs @@ -38,7 +38,7 @@ extern crate alloc; mod dev; mod fs; mod mounts; -mod root; +pub mod root; #[cfg(feature = "alloc")] mod arch; diff --git a/modules/ruxfs/src/root.rs b/modules/ruxfs/src/root.rs index eb19f2c60..7ffb6a520 100644 --- a/modules/ruxfs/src/root.rs +++ b/modules/ruxfs/src/root.rs @@ -11,29 +11,24 @@ //! //! TODO: it doesn't work very well if the mount points have containment relationships. -use alloc::{format, string::String, sync::Arc, vec::Vec}; +use alloc::{string::String, sync::Arc, vec::Vec}; use axerrno::{ax_err, AxError, AxResult}; use axfs_vfs::{VfsError, VfsNodeAttr, VfsNodeOps, VfsNodeRef, VfsNodeType, VfsOps, VfsResult}; -use axsync::Mutex; -use lazy_init::LazyInit; use crate::api::FileType; -static CURRENT_DIR_PATH: Mutex = Mutex::new(String::new()); -static CURRENT_DIR: LazyInit> = LazyInit::new(); - /// mount point information pub struct MountPoint { - path: &'static str, - fs: Arc, + pub path: &'static str, + pub fs: Arc, } -struct RootDirectory { +pub struct RootDirectory { main_fs: Arc, mounts: Vec, } -static ROOT_DIR: LazyInit> = LazyInit::new(); +// static ROOT_DIR: LazyInit> = LazyInit::new(); impl MountPoint { /// create new MountPoint from data @@ -161,45 +156,7 @@ impl VfsNodeOps for RootDirectory { } } -pub(crate) fn init_rootfs(mount_points: Vec) { - let main_fs = mount_points - .first() - .expect("No filesystem found") - .fs - .clone(); - let mut root_dir = RootDirectory::new(main_fs); - - for mp in mount_points.iter().skip(1) { - let path = mp.path; - let vfsops = mp.fs.clone(); - let message = format!("failed to mount filesystem at {}", path); - info!("mounting {}", path); - root_dir.mount(path, vfsops).expect(&message); - } - - ROOT_DIR.init_by(Arc::new(root_dir)); - CURRENT_DIR.init_by(Mutex::new(ROOT_DIR.clone())); - *CURRENT_DIR_PATH.lock() = "/".into(); -} - -fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef { - if path.starts_with('/') { - ROOT_DIR.clone() - } else { - dir.cloned().unwrap_or_else(|| CURRENT_DIR.lock().clone()) - } -} - -pub(crate) fn absolute_path(path: &str) -> AxResult { - if path.starts_with('/') { - Ok(axfs_vfs::path::canonicalize(path)) - } else { - let path = CURRENT_DIR_PATH.lock().clone() + path; - Ok(axfs_vfs::path::canonicalize(&path)) - } -} - -pub(crate) fn lookup(dir: Option<&VfsNodeRef>, path: &str) -> AxResult { +pub fn lookup(dir: Option<&VfsNodeRef>, path: &str) -> AxResult { if path.is_empty() { return ax_err!(NotFound); } @@ -266,9 +223,9 @@ pub(crate) fn remove_dir(dir: Option<&VfsNodeRef>, path: &str) -> AxResult { { return ax_err!(InvalidInput); } - if ROOT_DIR.contains(&absolute_path(path)?) { - return ax_err!(PermissionDenied); - } + // if ROOT_DIR.contains(&absolute_path(path)?) { + // return ax_err!(PermissionDenied); + // } let node = lookup(dir, path)?; let attr = node.get_attr()?; @@ -281,34 +238,6 @@ pub(crate) fn remove_dir(dir: Option<&VfsNodeRef>, path: &str) -> AxResult { } } -pub(crate) fn current_dir() -> AxResult { - Ok(CURRENT_DIR_PATH.lock().clone()) -} - -pub(crate) fn set_current_dir(path: &str) -> AxResult { - let mut abs_path = absolute_path(path)?; - if !abs_path.ends_with('/') { - abs_path += "/"; - } - if abs_path == "/" { - *CURRENT_DIR.lock() = ROOT_DIR.clone(); - *CURRENT_DIR_PATH.lock() = "/".into(); - return Ok(()); - } - - let node = lookup(None, &abs_path)?; - let attr = node.get_attr()?; - if !attr.is_dir() { - ax_err!(NotADirectory) - } else if !attr.perm().owner_executable() { - ax_err!(PermissionDenied) - } else { - *CURRENT_DIR.lock() = node; - *CURRENT_DIR_PATH.lock() = abs_path; - Ok(()) - } -} - pub(crate) fn rename(old: &str, new: &str) -> AxResult { if parent_node_of(None, new).lookup(new).is_ok() { warn!("dst file already exist, now remove it"); @@ -316,3 +245,32 @@ pub(crate) fn rename(old: &str, new: &str) -> AxResult { } parent_node_of(None, old).rename(old, new) } + +#[crate_interface::def_interface] +pub trait CurrentWorkingDirectoryOps { + fn init_rootfs(mount_points: Vec); + fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef; + fn absolute_path(path: &str) -> AxResult; + fn current_dir() -> AxResult; + fn set_current_dir(path: &str) -> AxResult; +} + +pub(crate) fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef { + crate_interface::call_interface!(CurrentWorkingDirectoryOps::parent_node_of, dir, path) +} + +pub(crate) fn absolute_path(path: &str) -> AxResult { + crate_interface::call_interface!(CurrentWorkingDirectoryOps::absolute_path, path) +} + +pub(crate) fn current_dir() -> AxResult { + crate_interface::call_interface!(CurrentWorkingDirectoryOps::current_dir) +} + +pub(crate) fn set_current_dir(path: &str) -> AxResult { + crate_interface::call_interface!(CurrentWorkingDirectoryOps::set_current_dir, path) +} + +pub(crate) fn init_rootfs(mount_points: Vec) { + crate_interface::call_interface!(CurrentWorkingDirectoryOps::init_rootfs, mount_points) +} diff --git a/modules/ruxhal/src/arch/aarch64/context.rs b/modules/ruxhal/src/arch/aarch64/context.rs index d129d23f1..5fc15460c 100644 --- a/modules/ruxhal/src/arch/aarch64/context.rs +++ b/modules/ruxhal/src/arch/aarch64/context.rs @@ -11,7 +11,9 @@ use core::{ arch::asm, fmt::{Debug, LowerHex}, }; -use memory_addr::VirtAddr; +use memory_addr::{PhysAddr, VirtAddr}; + +use super::write_page_table_root; /// Saved registers when a trap (exception) occurs. #[repr(C)] @@ -105,7 +107,7 @@ pub struct TaskContext { impl TaskContext { /// Creates a new default context for a new task. pub const fn new() -> Self { - unsafe { core::mem::MaybeUninit::zeroed().assume_init() } + unsafe { core::mem::MaybeUninit::::zeroed().assume_init() } } /// Initializes the context for a new task, with the given entry point and @@ -116,6 +118,37 @@ impl TaskContext { self.tpidr_el0 = tls_area.as_usize() as u64; } + /// Saves the current task's context from CPU to memory. + pub fn save_current_content(&mut self, src: *const u8, dst: *mut u8, size: usize) { + unsafe { + warn!( + "save_current_content: src={:#x}, dst={:#x}, size={:#x}", + src as usize, dst as usize, size + ); + save_stack(src, dst, size); + #[cfg(feature = "fp_simd")] + save_fpstate_context(&mut self.fp_state); + // will ret from here + save_current_context(self); + } + } + + /// Switches to another task in another process. + /// + /// It first saves the current task's context from CPU to this place, and then + /// restores the next task's context from `next_ctx` to CPU. + pub fn switch_process_to(&mut self, next_ctx: &Self, page_table_addr: PhysAddr) { + #[cfg(feature = "fp_simd")] + self.fp_state.switch_to(&next_ctx.fp_state); + + // warn!("switch_to: {:#x?}", next_ctx); + unsafe { + // switch to the next process's page table, stack would be unavailable before context switch finished + write_page_table_root(page_table_addr); + context_switch(self, next_ctx) + } + } + /// Switches to another task. /// /// It first saves the current task's context from CPU to this place, and then @@ -123,10 +156,95 @@ impl TaskContext { pub fn switch_to(&mut self, next_ctx: &Self) { #[cfg(feature = "fp_simd")] self.fp_state.switch_to(&next_ctx.fp_state); + // warn!("switch_to: {:#x?}", next_ctx); unsafe { context_switch(self, next_ctx) } } } +#[naked] +#[allow(named_asm_labels)] +// TODO: consider using SIMD instructions to copy the stack in parallel. +unsafe extern "C" fn save_stack(src: *const u8, dst: *mut u8, size: usize) { + // x0: src, x1: dst, x2: size + asm!( + " + mov x9, 0x0 // clear x9 + + _copy_stack_start: + cmp x9, x2 + b.eq _copy_stack_end + ldr x12, [x0] + str x12, [x1] + add x0, x0, 8 + add x1, x1, 8 + add x9, x9, 8 + b _copy_stack_start + _copy_stack_end: + + dsb sy + isb + ret", + options(noreturn), + ) +} + +#[naked] +#[allow(named_asm_labels)] +unsafe extern "C" fn save_current_context( + _current_task: &mut TaskContext, + // temp_stack_top: &u64, + // current_stack_top: &u64, +) { + asm!( + " + stp x29, x30, [x0, 12 * 8] + stp x27, x28, [x0, 10 * 8] + stp x25, x26, [x0, 8 * 8] + stp x23, x24, [x0, 6 * 8] + stp x21, x22, [x0, 4 * 8] + stp x19, x20, [x0, 2 * 8] + mrs x20, tpidr_el0 + mov x19, sp + stp x19, x20, [x0, 0 * 8] // [x0] is parent's sp + ldp x19, x20, [x0, 2 * 8] + isb + ret", + options(noreturn), + ) +} + +#[naked] +#[cfg(feature = "fp_simd")] +unsafe extern "C" fn save_fpstate_context(_current_fpstate: &mut FpState) { + asm!( + " + // save fp/neon context + mrs x9, fpcr + mrs x10, fpsr + stp q0, q1, [x0, 0 * 16] + stp q2, q3, [x0, 2 * 16] + stp q4, q5, [x0, 4 * 16] + stp q6, q7, [x0, 6 * 16] + stp q8, q9, [x0, 8 * 16] + stp q10, q11, [x0, 10 * 16] + stp q12, q13, [x0, 12 * 16] + stp q14, q15, [x0, 14 * 16] + stp q16, q17, [x0, 16 * 16] + stp q18, q19, [x0, 18 * 16] + stp q20, q21, [x0, 20 * 16] + stp q22, q23, [x0, 22 * 16] + stp q24, q25, [x0, 24 * 16] + stp q26, q27, [x0, 26 * 16] + stp q28, q29, [x0, 28 * 16] + stp q30, q31, [x0, 30 * 16] + str x9, [x0, 64 * 8] + str x10, [x0, 65 * 8] + isb + ret", + options(noreturn), + ) +} + #[naked] unsafe extern "C" fn context_switch(_current_task: &mut TaskContext, _next_task: &TaskContext) { asm!( diff --git a/modules/ruxhal/src/arch/aarch64/trap.S b/modules/ruxhal/src/arch/aarch64/trap.S index f06468287..2213597ba 100644 --- a/modules/ruxhal/src/arch/aarch64/trap.S +++ b/modules/ruxhal/src/arch/aarch64/trap.S @@ -16,7 +16,6 @@ stp x26, x27, [sp, 26 * 8] stp x28, x29, [sp, 28 * 8] - mrs x9, sp_el0 mrs x10, elr_el1 mrs x11, spsr_el1 stp x30, x9, [sp, 30 * 8] @@ -26,7 +25,6 @@ .macro RESTORE_REGS ldp x10, x11, [sp, 32 * 8] ldp x30, x9, [sp, 30 * 8] - msr sp_el0, x9 msr elr_el1, x10 msr spsr_el1, x11 diff --git a/modules/ruxhal/src/arch/aarch64/trap.rs b/modules/ruxhal/src/arch/aarch64/trap.rs index e81cdff95..f917300eb 100644 --- a/modules/ruxhal/src/arch/aarch64/trap.rs +++ b/modules/ruxhal/src/arch/aarch64/trap.rs @@ -106,9 +106,15 @@ fn handle_sync_exception(tf: &mut TrapFrame) { PageFaultCause::INSTRUCTION // = instruction fetch } }; - if crate::trap::handle_page_fault(vaddr, cause) { + let is_mapped = crate::trap::handle_page_fault(vaddr, cause); + + if is_mapped { return; } + error!( + "Page fault @ {:#x}, cause={:?}, is_mapped={}", + tf.elr, cause, is_mapped + ); } panic!( "EL1 Page Fault @ {:#x}, FAR={:#x}, ISS={:#x}:\n{:#x?}", diff --git a/modules/ruxhal/src/arch/x86_64/mod.rs b/modules/ruxhal/src/arch/x86_64/mod.rs index 973ff5cf6..ea97eee5c 100644 --- a/modules/ruxhal/src/arch/x86_64/mod.rs +++ b/modules/ruxhal/src/arch/x86_64/mod.rs @@ -235,7 +235,7 @@ pub unsafe fn init_syscall_entry() { .has_syscall_sysret()); x86_64::registers::model_specific::LStar::write(x86_64::VirtAddr::new( - x86_syscall_entry as usize as u64, + x86_syscall_entry as usize, )); x86_64::registers::model_specific::Efer::update(|efer| { efer.insert( diff --git a/modules/ruxhal/src/mem.rs b/modules/ruxhal/src/mem.rs index b05409a6b..7e0132c8c 100644 --- a/modules/ruxhal/src/mem.rs +++ b/modules/ruxhal/src/mem.rs @@ -11,8 +11,6 @@ use core::fmt; -#[cfg(feature = "paging")] -use crate::paging::pte_query; #[doc(no_inline)] pub use memory_addr::{PhysAddr, VirtAddr, PAGE_SIZE_4K}; @@ -69,20 +67,6 @@ pub const fn direct_virt_to_phys(vaddr: VirtAddr) -> PhysAddr { PhysAddr::from(vaddr.as_usize() - ruxconfig::PHYS_VIRT_OFFSET) } -/// Converts a virtual address to a physical address. -/// -/// When paging is enabled, query physical address from the page table -#[inline] -pub fn virt_to_phys(vaddr: VirtAddr) -> PhysAddr { - #[cfg(feature = "paging")] - match pte_query(vaddr) { - Ok((paddr, _, _)) => paddr, - Err(_) => PhysAddr::from(0_usize), // for address unmapped - } - #[cfg(not(feature = "paging"))] - direct_virt_to_phys(vaddr) -} - /// Converts a physical address to a virtual address. /// /// It assumes that there is a linear mapping with the offset diff --git a/modules/ruxhal/src/paging.rs b/modules/ruxhal/src/paging.rs index eb2311dc7..96fdee92b 100644 --- a/modules/ruxhal/src/paging.rs +++ b/modules/ruxhal/src/paging.rs @@ -9,19 +9,14 @@ //! Page table manipulation. extern crate alloc; -use crate::arch::flush_tlb; -use spinlock::SpinNoIrq; use crate::mem::{ - direct_virt_to_phys, memory_regions, phys_to_virt, MemRegionFlags, PhysAddr, VirtAddr, - PAGE_SIZE_4K, + direct_virt_to_phys, phys_to_virt, MemRegionFlags, PhysAddr, VirtAddr, PAGE_SIZE_4K, }; use axalloc::global_allocator; -use lazy_init::LazyInit; #[doc(no_inline)] -use page_table::{MappingFlags, PageSize, PagingError, PagingIf, PagingResult}; - +use page_table::{MappingFlags, PagingIf}; impl From for MappingFlags { fn from(f: MemRegionFlags) -> Self { let mut ret = Self::empty(); @@ -78,116 +73,3 @@ cfg_if::cfg_if! { pub type PageTable = page_table::aarch64::A64PageTable; } } - -pub(crate) static KERNEL_PAGE_TABLE: LazyInit> = LazyInit::new(); - -/// Remap the regions for kernel memory -pub fn remap_kernel_memory() -> PagingResult { - if crate::cpu::this_cpu_is_bsp() { - let mut kernel_page_table = PageTable::try_new()?; - for r in memory_regions() { - kernel_page_table.map_region( - phys_to_virt(r.paddr), - r.paddr, - r.size, - r.flags.into(), - true, - )?; - } - - KERNEL_PAGE_TABLE.init_by(SpinNoIrq::new(kernel_page_table)); - } - unsafe { crate::arch::write_page_table_root(KERNEL_PAGE_TABLE.lock().root_paddr()) }; - Ok(()) -} - -/// Temporarily, `malloc` alloc memory in heap simply, and it can not be swapped -/// into swap file. Once the memory is not enough with all memory alloced, it -/// will be too late, as there will be no memory for `malloc` any more. In practice, -/// this is highly likely to cause errors of insufficient memory. To prevent this, -/// mmapping will not alloc from physical address to avoid this. -/// -/// After the page of `malloc` can be swapped, or it raises a propriately handler -/// to swap page when memory is not enough, it will be okay to delete this. -const PAGE_NUM_MIN: usize = 1024; - -/// Obtain fake VirtAddr addresses without performing virtual memory mapping -/// to prevent physical competition between multiple threads. -/// After call the function. the page is alloced in allocator but its virtual -/// address is still on linear mapping region. -/// use `do_pte_map` to do actually page mapping after call this function. -pub fn alloc_page_preload() -> Result { - if global_allocator().available_pages() < PAGE_NUM_MIN { - warn!( - "available page num is {:?}", - global_allocator().available_pages() - ); - return Err(PagingError::NoMemory); - }; - match global_allocator().alloc_pages(1, PAGE_SIZE_4K) { - Ok(fake_vaddr) => Ok(VirtAddr::from(fake_vaddr)), - Err(_) => Err(PagingError::NoMemory), - } -} - -/// Unmap memory for an mmap-induced PageFault and updating PTE entries. -/// After call the function. the page is alloced in allocator but its virtual -/// address is still on linear mapping region. -/// use `do_pte_map` to do actually page mapping after call this function. -pub fn pte_swap_preload(swaped_vaddr: VirtAddr) -> PagingResult { - trace!("swapping swaped_vaddr: 0x{:x?}", swaped_vaddr,); - let mut kernel_page_table = KERNEL_PAGE_TABLE.lock(); - let (paddr, _) = kernel_page_table.unmap(swaped_vaddr)?; - flush_tlb(Some(swaped_vaddr)); - Ok(phys_to_virt(paddr)) -} - -/// Map memory for an mmap-induced PageFault and updating PTE entries, -/// This function must be called after `alloc_page_preload` and -/// `pte_swap_preload` when the mapping operator is ready. -pub fn do_pte_map(vaddr: VirtAddr, fake_vaddr: VirtAddr, flags: MappingFlags) -> PagingResult { - KERNEL_PAGE_TABLE.lock().map( - vaddr, - direct_virt_to_phys(fake_vaddr), - PageSize::Size4K, - flags, - ) -} - -/// Query PTE entries of the virtual address. -/// -/// get the physical address information corresponding to the virtual address from the page table -pub fn pte_query(vaddr: VirtAddr) -> PagingResult<(PhysAddr, MappingFlags, PageSize)> { - let kernel_page_table = KERNEL_PAGE_TABLE.lock(); - kernel_page_table.query(vaddr) -} - -/// Update flags or physical address for an PTE entries. -/// -/// change the physical address or access permissions mapped by the virtual address -pub fn pte_update_page( - vaddr: VirtAddr, - paddr: Option, - flags: Option, -) -> PagingResult { - trace!( - "updating vaddr:0x{:x?} paddr:0x{:x?} flags:0x{:x?}", - vaddr, - paddr, - flags - ); - KERNEL_PAGE_TABLE.lock().update(vaddr, paddr, flags)?; - flush_tlb(Some(vaddr)); - Ok(()) -} - -/// Unmapping and decalloc memory for an page in page table. -/// -/// release the corresponding memory at the same time -pub fn pte_unmap_page(vaddr: VirtAddr) -> PagingResult { - trace!("unmapping vaddr: 0x{:x?}", vaddr); - let (paddr, _) = KERNEL_PAGE_TABLE.lock().unmap(vaddr)?; - global_allocator().dealloc_pages(phys_to_virt(paddr).as_usize(), 1); - flush_tlb(Some(vaddr)); - Ok(()) -} diff --git a/modules/ruxhal/src/platform/aarch64_common/pl011.rs b/modules/ruxhal/src/platform/aarch64_common/pl011.rs index a311f9834..d67aec6a2 100644 --- a/modules/ruxhal/src/platform/aarch64_common/pl011.rs +++ b/modules/ruxhal/src/platform/aarch64_common/pl011.rs @@ -37,7 +37,7 @@ impl RxRingBuffer { empty: true, } } - + #[cfg(not(feature = "tty"))] fn push(&mut self, n: u8) { if self.tail != self.head || self.empty { self.buffer[self.tail] = n; diff --git a/modules/ruxhal/src/trap.rs b/modules/ruxhal/src/trap.rs index 344ca06ac..787a3e9a8 100644 --- a/modules/ruxhal/src/trap.rs +++ b/modules/ruxhal/src/trap.rs @@ -9,9 +9,10 @@ //! Trap handling. use crate_interface::{call_interface, def_interface}; +use page_table::MappingFlags; /// Several reasons for page missing exceptions. -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub enum PageFaultCause { /// pageFault caused by memory WRITE. WRITE, @@ -21,6 +22,17 @@ pub enum PageFaultCause { INSTRUCTION, } +/// `PageFaultCause` corresponding to `MappingFlags`. +impl Into for PageFaultCause { + fn into(self) -> MappingFlags { + match self { + PageFaultCause::WRITE => MappingFlags::WRITE, + PageFaultCause::READ => MappingFlags::READ, + PageFaultCause::INSTRUCTION => MappingFlags::EXECUTE, + } + } +} + /// Trap handler interface. /// /// This trait is defined with the [`#[def_interface]`][1] attribute. Users diff --git a/modules/ruxmm/Cargo.toml b/modules/ruxmm/Cargo.toml new file mode 100644 index 000000000..327d3ff5b --- /dev/null +++ b/modules/ruxmm/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "ruxmm" +version = "0.1.0" +edition = "2021" +authors = [ + "Yuekai Jia ", + "yanjuguang ", +] +description = "Ruxos hardware abstraction layer, provides unified APIs for platform-specific operations" +license = "GPL-3.0-or-later OR Apache-2.0" +homepage = "https://github.com/syswonder/ruxos" +repository = "https://github.com/syswonder/ruxos/tree/main/modules/ruxmm" + +[features] +default = [] +paging = [] + +[dependencies] +log = "0.4" +cfg-if = "1.0" +bitflags = "2.2" +static_assertions = "1.1.0" +kernel_guard = "0.1.0" +spinlock = { path = "../../crates/spinlock" } +page_table = { path = "../../crates/page_table"} +page_table_entry = { path = "../../crates/page_table_entry" } +memory_addr = "0.1.0" +crate_interface = "0.1.1" + +ruxtask = { path = "../ruxtask" } +ruxdriver ={ path = "../ruxdriver" } +axalloc = { path = "../axalloc"} +ruxhal ={ path = "../ruxhal" } \ No newline at end of file diff --git a/modules/ruxmm/src/lib.rs b/modules/ruxmm/src/lib.rs new file mode 100644 index 000000000..76a09e7b2 --- /dev/null +++ b/modules/ruxmm/src/lib.rs @@ -0,0 +1,21 @@ +/* Copyright (c) [2023] [Syswonder Community] + * [Ruxos] is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +#![no_std] +#![feature(asm_const)] +#![feature(naked_functions)] +#![feature(const_option)] +#![feature(doc_auto_cfg)] + +#[allow(unused_imports)] +#[macro_use] + +pub mod mem; +#[cfg(feature = "paging")] +pub mod paging; diff --git a/modules/ruxmm/src/mem.rs b/modules/ruxmm/src/mem.rs new file mode 100644 index 000000000..388510fd4 --- /dev/null +++ b/modules/ruxmm/src/mem.rs @@ -0,0 +1,42 @@ +/* Copyright (c) [2023] [Syswonder Community] + * [Ruxos] is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +#[cfg(feature = "paging")] +use crate::paging::pte_query; + +use ruxdriver::virtio::AddressTranslate; +use ruxhal::mem::{direct_virt_to_phys, PhysAddr, VirtAddr}; + +#[cfg(feature = "paging")] +struct AddressTranslateImpl; + +/// Converts a virtual address to a physical address. +/// +/// When paging is enabled, query physical address from the page table +#[cfg(feature = "paging")] +#[crate_interface::impl_interface] +impl AddressTranslate for AddressTranslateImpl { + fn virt_to_phys(vaddr: VirtAddr) -> Option { + match pte_query(vaddr) { + Ok((paddr, _, _)) => Some(paddr.into()), + Err(_) => None, // for address unmapped + } + } +} + +/// Converts a virtual address to a physical address. +/// +/// When paging is enabled, query physical address from the page table +#[cfg(not(feature = "paging"))] +#[crate_interface::impl_interface] +impl AddressTranslate for AddressTranslateImpl { + fn virt_to_phys(vaddr: VirtAddr) -> Option { + Some(direct_virt_to_phys(vaddr)) + } +} diff --git a/modules/ruxmm/src/paging.rs b/modules/ruxmm/src/paging.rs new file mode 100644 index 000000000..e5522fcfa --- /dev/null +++ b/modules/ruxmm/src/paging.rs @@ -0,0 +1,138 @@ +/* Copyright (c) [2023] [Syswonder Community] + * [Ruxos] is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +//! Page table manipulation. +extern crate alloc; + +use ruxhal::arch::flush_tlb; +use ruxhal::mem::{ + direct_virt_to_phys, memory_regions, phys_to_virt, PhysAddr, VirtAddr, PAGE_SIZE_4K, +}; + +use axalloc::global_allocator; +#[doc(no_inline)] +use page_table::{MappingFlags, PageSize, PagingError, PagingResult}; + +use log::{trace, warn}; + +/// Remap the regions for kernel memory +pub fn remap_kernel_memory() -> PagingResult { + let current_task = ruxtask::current(); + let mut kernel_page_table = current_task.pagetable.lock(); + if ruxhal::cpu::this_cpu_is_bsp() { + for r in memory_regions() { + kernel_page_table.map_region( + phys_to_virt(r.paddr), + r.paddr, + r.size, + r.flags.into(), + true, + )?; + } + } + + unsafe { ruxhal::arch::write_page_table_root(kernel_page_table.root_paddr()) }; + Ok(()) +} + +/// Temporarily, `malloc` alloc memory in heap simply, and it can not be swapped +/// into swap file. Once the memory is not enough with all memory alloced, it +/// will be too late, as there will be no memory for `malloc` any more. In practice, +/// this is highly likely to cause errors of insufficient memory. To prevent this, +/// mmapping will not alloc from physical address to avoid this. +/// +/// After the page of `malloc` can be swapped, or it raises a propriately handler +/// to swap page when memory is not enough, it will be okay to delete this. +const PAGE_NUM_MIN: usize = 1024; + +/// Obtain fake VirtAddr addresses without performing virtual memory mapping +/// to prevent physical competition between multiple threads. +/// After call the function. the page is alloced in allocator but its virtual +/// address is still on linear mapping region. +/// use `do_pte_map` to do actually page mapping after call this function. +pub fn alloc_page_preload() -> Result { + if global_allocator().available_pages() < PAGE_NUM_MIN { + warn!( + "available page num is {:?}", + global_allocator().available_pages() + ); + return Err(PagingError::NoMemory); + }; + match global_allocator().alloc_pages(1, PAGE_SIZE_4K) { + Ok(fake_vaddr) => Ok(VirtAddr::from(fake_vaddr)), + Err(_) => Err(PagingError::NoMemory), + } +} + +/// Unmap memory for an mmap-induced PageFault and updating PTE entries. +/// After call the function. the page is alloced in allocator but its virtual +/// address is still on linear mapping region. +/// use `do_pte_map` to do actually page mapping after call this function. +pub fn pte_swap_preload(swaped_vaddr: VirtAddr) -> PagingResult { + trace!("swapping swaped_vaddr: 0x{:x?}", swaped_vaddr,); + let binding = ruxtask::current(); + let mut kernel_page_table = binding.pagetable.lock(); + let (paddr, _) = kernel_page_table.unmap(swaped_vaddr)?; + flush_tlb(Some(swaped_vaddr)); + Ok(phys_to_virt(paddr)) +} + +/// Map memory for an mmap-induced PageFault and updating PTE entries, +/// This function must be called after `alloc_page_preload` and +/// `pte_swap_preload` when the mapping operator is ready. +pub fn do_pte_map(vaddr: VirtAddr, fake_vaddr: VirtAddr, flags: MappingFlags) -> PagingResult { + let ret = ruxtask::current().pagetable.lock().map( + vaddr, + direct_virt_to_phys(fake_vaddr), + PageSize::Size4K, + flags, + ); + ret +} + +/// Query PTE entries of the virtual address. +/// +/// get the physical address information corresponding to the virtual address from the page table +pub fn pte_query(vaddr: VirtAddr) -> PagingResult<(PhysAddr, MappingFlags, PageSize)> { + let binding = ruxtask::current(); + let kernel_page_table = binding.pagetable.lock(); + kernel_page_table.query(vaddr) +} + +/// Update flags or physical address for an PTE entries. +/// +/// change the physical address or access permissions mapped by the virtual address +pub fn pte_update_page( + vaddr: VirtAddr, + paddr: Option, + flags: Option, +) -> PagingResult { + trace!( + "updating vaddr:0x{:x?} paddr:0x{:x?} flags:0x{:x?}", + vaddr, + paddr, + flags + ); + ruxtask::current() + .pagetable + .lock() + .update(vaddr, paddr, flags)?; + flush_tlb(Some(vaddr)); + Ok(()) +} + +/// Unmapping and decalloc memory for an page in page table. +/// +/// release the corresponding memory at the same time +pub fn pte_unmap_page(vaddr: VirtAddr) -> PagingResult { + trace!("unmapping vaddr: 0x{:x?}", vaddr); + ruxtask::current().pagetable.lock().unmap(vaddr)?; + flush_tlb(Some(vaddr)); + Ok(()) +} diff --git a/modules/ruxnet/src/smoltcp_impl/tcp.rs b/modules/ruxnet/src/smoltcp_impl/tcp.rs index de9c14fd5..368160e4b 100644 --- a/modules/ruxnet/src/smoltcp_impl/tcp.rs +++ b/modules/ruxnet/src/smoltcp_impl/tcp.rs @@ -152,7 +152,7 @@ impl TcpSocket { ax_err!(ConnectionRefused, "socket connect() failed") } })?; - Ok(( + Ok::<(IpEndpoint, IpEndpoint), AxError>(( socket.local_endpoint().unwrap(), socket.remote_endpoint().unwrap(), )) diff --git a/modules/ruxruntime/Cargo.toml b/modules/ruxruntime/Cargo.toml index dc2e36855..560280fc4 100644 --- a/modules/ruxruntime/Cargo.toml +++ b/modules/ruxruntime/Cargo.toml @@ -20,12 +20,12 @@ smp = ["ruxhal/smp"] irq = ["ruxhal/irq", "ruxtask?/irq", "percpu", "kernel_guard"] tls = ["ruxhal/tls", "ruxtask?/tls"] alloc = ["axalloc", "dtb"] -paging = ["ruxhal/paging", "lazy_init"] +paging = ["ruxhal/paging", "lazy_init", "ruxmm"] rtc = ["ruxhal/rtc"] multitask = ["ruxtask/multitask", "dep:ruxfutex", "rand"] rand = ["dep:ruxrand"] -fs = ["ruxdriver", "ruxfs"] +fs = ["ruxdriver", "ruxfs", "ruxtask/fs"] blkfs = ["fs"] virtio-9p = ["fs", "rux9p"] net-9p = ["fs", "rux9p"] @@ -48,6 +48,7 @@ rux9p = { path = "../rux9p", optional = true } ruxnet = { path = "../ruxnet", optional = true } ruxdisplay = { path = "../ruxdisplay", optional = true } ruxtask = { path = "../ruxtask", optional = true } +ruxmm = { path = "../ruxmm", optional = true } axsync = { path = "../axsync", optional = true } ruxfutex = { path = "../ruxfutex", optional = true } ruxrand = { path = "../ruxrand", optional = true } diff --git a/modules/ruxruntime/src/lib.rs b/modules/ruxruntime/src/lib.rs index 73b80371b..3edd81522 100644 --- a/modules/ruxruntime/src/lib.rs +++ b/modules/ruxruntime/src/lib.rs @@ -189,12 +189,6 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! { #[cfg(feature = "alloc")] init_allocator(); - #[cfg(feature = "paging")] - { - info!("Initialize kernel page table..."); - remap_kernel_memory().expect("remap kernel memoy failed"); - } - #[cfg(feature = "tty")] tty::init(); @@ -211,6 +205,12 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! { ruxfutex::init_futex(); } + #[cfg(feature = "paging")] + { + info!("Initialize kernel page table..."); + remap_kernel_memory().expect("remap kernel memoy failed"); + } + #[cfg(any(feature = "fs", feature = "net", feature = "display"))] { #[allow(unused_variables)] @@ -394,7 +394,7 @@ fn init_allocator() { } #[cfg(feature = "paging")] -use ruxhal::paging::remap_kernel_memory; +use ruxmm::paging::remap_kernel_memory; #[cfg(feature = "irq")] fn init_interrupt() { diff --git a/modules/ruxruntime/src/mp.rs b/modules/ruxruntime/src/mp.rs index c9bdc6444..5b9c12419 100644 --- a/modules/ruxruntime/src/mp.rs +++ b/modules/ruxruntime/src/mp.rs @@ -43,9 +43,6 @@ pub extern "C" fn rust_main_secondary(cpu_id: usize) -> ! { ENTERED_CPUS.fetch_add(1, Ordering::Relaxed); info!("Secondary CPU {:x} started.", cpu_id); - #[cfg(feature = "paging")] - super::remap_kernel_memory().unwrap(); - ruxhal::platform_init_secondary(); #[cfg(feature = "rand")] @@ -54,6 +51,9 @@ pub extern "C" fn rust_main_secondary(cpu_id: usize) -> ! { #[cfg(feature = "multitask")] ruxtask::init_scheduler_secondary(); + #[cfg(feature = "paging")] + super::remap_kernel_memory().unwrap(); + info!("Secondary CPU {:x} init OK.", cpu_id); super::INITED_CPUS.fetch_add(1, Ordering::Relaxed); diff --git a/modules/ruxtask/Cargo.toml b/modules/ruxtask/Cargo.toml index 0a7c1daa8..3b4b13c6e 100644 --- a/modules/ruxtask/Cargo.toml +++ b/modules/ruxtask/Cargo.toml @@ -2,7 +2,10 @@ name = "ruxtask" version = "0.1.0" edition = "2021" -authors = ["Yuekai Jia ", "AuYang261 "] +authors = [ + "Yuekai Jia ", + "AuYang261 ", +] description = "Ruxos task management module" license = "GPL-3.0-or-later OR Apache-2.0" homepage = "https://github.com/syswonder/ruxos" @@ -12,21 +15,15 @@ repository = "https://github.com/syswonder/ruxos/tree/main/modules/ruxtask" default = [] multitask = [ - "dep:ruxconfig", - "dep:ruxrand", - "dep:percpu", - "dep:spinlock", - "dep:lazy_init", - "dep:memory_addr", - "dep:scheduler", - "dep:timer_list", - "dep:crate_interface", - "dep:kernel_guard", + "dep:ruxconfig", "dep:percpu", "dep:spinlock", "dep:lazy_init", "dep:memory_addr", + "dep:scheduler", "dep:timer_list", "kernel_guard", "dep:crate_interface", ] irq = [] tls = ["ruxhal/tls"] musl = [] preempt = ["irq", "percpu?/preempt", "kernel_guard/preempt"] +paging = [] +fs = [] sched_fifo = ["multitask"] sched_rr = ["multitask", "preempt"] @@ -38,11 +35,12 @@ test = ["percpu?/sp-naive"] cfg-if = "1.0" log = "0.4" axerrno = { path = "../../crates/axerrno" } + ruxhal = { path = "../ruxhal" } -ruxfdtable = { path = "../ruxfdtable" } +ruxfs = { path = "../ruxfs" } ruxconfig = { path = "../ruxconfig", optional = true } -ruxrand = { path = "../ruxrand", optional = true } - +axalloc = { path = "../axalloc" } +ruxfdtable = { path = "../ruxfdtable" } percpu = { path = "../../crates/percpu", optional = true } spinlock = { path = "../../crates/spinlock", optional = true } lazy_init = { path = "../../crates/lazy_init", optional = true } @@ -51,8 +49,18 @@ scheduler = { path = "../../crates/scheduler", optional = true } timer_list = { path = "../../crates/timer_list", optional = true } kernel_guard = { version = "0.1.0", optional = true } crate_interface = { version = "0.1.1", optional = true } +flatten_objects = { path = "../../crates/flatten_objects" } +spin = "0.9" +axio ={ path = "../../crates/axio" } +lazy_static = { version = "1.4", features = ["spin_no_std"] } +page_table = { path = "../../crates/page_table" } +page_table_entry = { path = "../../crates/page_table_entry" } + +# for testing +axfs_vfs = { path = "../../crates/axfs_vfs"} [dev-dependencies] rand = "0.8" ruxhal = { path = "../ruxhal", features = ["fp_simd"] } ruxtask = { path = ".", features = ["test"] } + diff --git a/modules/ruxtask/src/api.rs b/modules/ruxtask/src/api.rs index 67476e271..fad33a827 100644 --- a/modules/ruxtask/src/api.rs +++ b/modules/ruxtask/src/api.rs @@ -68,6 +68,7 @@ pub fn current_may_uninit() -> Option { /// # Panics /// /// Panics if the current task is not initialized. +#[inline(never)] pub fn current() -> CurrentTask { CurrentTask::get() } @@ -128,6 +129,30 @@ where TaskInner::new_musl(f, name, stack_size, tls, set_tid, tl) } +pub fn fork_task() -> Option { + let current_process = current(); + let current_id = current_process.id().as_u64(); + let children_process = TaskInner::fork(); + + // Judge whether the parent process is blocked, if yes, add it to the blocking queue of the child process + if current().id().as_u64() == current_id { + RUN_QUEUE.lock().add_task(children_process.clone()); + + warn!( + "parent process[{}] is leaving, add it to the blocking queue of the child process[{}]", + current().id().as_u64(), + children_process.id().as_u64() + ); + return Some(children_process.clone()); + } + + error!( + "children process[{}] is forked, return None", + current().id().as_u64() + ); + return None; +} + /// Spawns a new task with the default parameters. /// /// The default task name is an empty string. The default task stack size is diff --git a/modules/ruxtask/src/fs.rs b/modules/ruxtask/src/fs.rs new file mode 100644 index 000000000..3821d7a05 --- /dev/null +++ b/modules/ruxtask/src/fs.rs @@ -0,0 +1,331 @@ +/* Copyright (c) [2023] [Syswonder Community] + * [Ruxos] is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +use crate::current; +use alloc::{format, string::String, sync::Arc, vec::Vec}; +use axerrno::{ax_err, AxResult}; +use axfs_vfs::VfsNodeRef; +use flatten_objects::FlattenObjects; +use ruxfdtable::FileLike; +use ruxfs::{ + root::{lookup, CurrentWorkingDirectoryOps, RootDirectory}, + MountPoint, +}; + +use axerrno::{LinuxError, LinuxResult}; +use axio::PollState; +use ruxfdtable::RuxStat; +use spin::RwLock; + +#[crate_interface::def_interface] +pub trait InitFs { + fn init(task_inner: &mut FileSystem); +} + +pub fn get_file_like(fd: i32) -> LinuxResult> { + // let _exec = *MUST_EXEC; + let binding_task = current(); + let mut binding_fs = binding_task.fs.lock(); + let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; + fd_table.get(fd as usize).cloned().ok_or(LinuxError::EBADF) +} + +pub fn add_file_like(f: Arc) -> LinuxResult { + // let _exec = *MUST_EXEC; + let binding_task = current(); + let mut binding_fs = binding_task.fs.lock(); + let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; + Ok(fd_table.add(f).ok_or(LinuxError::EMFILE)? as i32) +} + +pub fn close_file_like(fd: i32) -> LinuxResult { + // let _exec = *MUST_EXEC; + let binding_task = current(); + let mut binding_fs = binding_task.fs.lock(); + let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; + let f = fd_table.remove(fd as usize).ok_or(LinuxError::EBADF)?; + drop(f); + Ok(()) +} + +pub struct File { + pub inner: RwLock, +} + +impl File { + pub fn new(inner: ruxfs::fops::File) -> Self { + Self { + inner: RwLock::new(inner), + } + } + + pub fn add_to_fd_table(self) -> LinuxResult { + add_file_like(Arc::new(self)) + } + + pub fn from_fd(fd: i32) -> LinuxResult> { + let f = get_file_like(fd)?; + f.into_any() + .downcast::() + .map_err(|_| LinuxError::EINVAL) + } +} + +impl FileLike for File { + fn read(&self, buf: &mut [u8]) -> LinuxResult { + Ok(self.inner.write().read(buf)?) + } + + fn write(&self, buf: &[u8]) -> LinuxResult { + Ok(self.inner.write().write(buf)?) + } + + fn flush(&self) -> LinuxResult { + Ok(self.inner.write().flush()?) + } + + fn stat(&self) -> LinuxResult { + let metadata = self.inner.read().get_attr()?; + let ty = metadata.file_type() as u8; + let perm = metadata.perm().bits() as u32; + let st_mode = ((ty as u32) << 12) | perm; + + // Inode of files, for musl dynamic linker. + // WARN: there will be collision for files with the same size. + // TODO: implement real inode. + let st_ino = metadata.size() + st_mode as u64; + + let res = RuxStat { + st_ino, + st_nlink: 1, + st_mode, + st_uid: 1000, + st_gid: 1000, + st_size: metadata.size() as _, + st_blocks: metadata.blocks() as _, + st_blksize: 512, + ..Default::default() + }; + + Ok(res) + } + + fn into_any(self: Arc) -> Arc { + self + } + + fn poll(&self) -> LinuxResult { + Ok(PollState { + readable: true, + writable: true, + }) + } + + fn set_nonblocking(&self, _nonblocking: bool) -> LinuxResult { + Ok(()) + } +} + +pub struct Directory { + pub inner: RwLock, +} + +impl Directory { + pub fn new(inner: ruxfs::fops::Directory) -> Self { + Self { + inner: RwLock::new(inner), + } + } + + pub fn add_to_fd_table(self) -> LinuxResult { + add_file_like(Arc::new(self)) + } + + pub fn from_fd(fd: i32) -> LinuxResult> { + let f = get_file_like(fd)?; + f.into_any() + .downcast::() + .map_err(|_| LinuxError::EINVAL) + } +} + +impl FileLike for Directory { + fn read(&self, _buf: &mut [u8]) -> LinuxResult { + Err(LinuxError::EACCES) + } + + fn write(&self, _buf: &[u8]) -> LinuxResult { + Err(LinuxError::EACCES) + } + + fn flush(&self) -> LinuxResult { + Ok(()) + } + + fn stat(&self) -> LinuxResult { + let metadata = self.inner.read().get_attr()?; + let ty = metadata.file_type() as u8; + let perm = metadata.perm().bits() as u32; + let st_mode = ((ty as u32) << 12) | perm; + Ok(RuxStat { + st_ino: 1, + st_nlink: 1, + st_mode, + st_uid: 1000, + st_gid: 1000, + st_size: metadata.size() as _, + st_blocks: metadata.blocks() as _, + st_blksize: 512, + ..Default::default() + }) + } + + fn into_any(self: Arc) -> Arc { + self + } + + fn poll(&self) -> LinuxResult { + Ok(PollState { + readable: true, + writable: true, + }) + } + + fn set_nonblocking(&self, _nonblocking: bool) -> LinuxResult { + Ok(()) + } +} + +/// Maximum number of files per process +pub const RUX_FILE_LIMIT: usize = 1024; + +/// A struct representing a file system object. +pub struct FileSystem { + pub fd_table: FlattenObjects, RUX_FILE_LIMIT>, + pub current_path: String, + pub current_dir: VfsNodeRef, + pub root_dir: Arc, +} + +impl Clone for FileSystem { + fn clone(&self) -> Self { + let mut new_fd_table = FlattenObjects::new(); + // get all file descriptors from the original file system to copy them to the new one + // TODO: make this more efficient by only copying the used file descriptors + for fd in 0..self.fd_table.capacity() { + if let Some(f) = self.fd_table.get(fd) { + new_fd_table.add_at(fd, f.clone()).unwrap(); + } + } + + Self { + fd_table: new_fd_table, + current_path: self.current_path.clone(), + current_dir: self.current_dir.clone(), + root_dir: self.root_dir.clone(), + } + } +} + +pub fn init_rootfs(mount_points: Vec) { + let main_fs = mount_points + .first() + .expect("No filesystem found") + .fs + .clone(); + let mut root_dir = RootDirectory::new(main_fs); + + for mp in mount_points.iter().skip(1) { + let path = mp.path; + let vfsops = mp.fs.clone(); + let message = format!("failed to mount filesystem at {}", path); + info!("mounting {}", path); + root_dir.mount(path, vfsops).expect(&message); + } + + let root_dir_arc = Arc::new(root_dir); + + let mut fs = FileSystem { + fd_table: FlattenObjects::new(), + current_path: "/".into(), + current_dir: root_dir_arc.clone(), + root_dir: root_dir_arc.clone(), + }; + let fs_mutable = &mut fs; + crate_interface::call_interface!(InitFs::init, fs_mutable); + current().fs.lock().replace(fs); +} + +fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef { + if path.starts_with('/') { + current().fs.lock().as_mut().unwrap().root_dir.clone() + } else { + dir.cloned() + .unwrap_or_else(|| current().fs.lock().as_mut().unwrap().current_dir.clone()) + } +} + +pub fn absolute_path(path: &str) -> AxResult { + if path.starts_with('/') { + Ok(axfs_vfs::path::canonicalize(path)) + } else { + let path = current().fs.lock().as_mut().unwrap().current_path.clone() + path; + Ok(axfs_vfs::path::canonicalize(&path)) + } +} + +pub fn current_dir() -> AxResult { + Ok(current().fs.lock().as_mut().unwrap().current_path.clone()) +} + +pub fn set_current_dir(path: &str) -> AxResult { + let mut abs_path = absolute_path(path)?; + if !abs_path.ends_with('/') { + abs_path += "/"; + } + if abs_path == "/" { + current().fs.lock().as_mut().unwrap().current_dir = + current().fs.lock().as_mut().unwrap().root_dir.clone(); + current().fs.lock().as_mut().unwrap().current_path = "/".into(); + return Ok(()); + } + + let node = lookup(None, &abs_path)?; + let attr = node.get_attr()?; + if !attr.is_dir() { + ax_err!(NotADirectory) + } else if !attr.perm().owner_executable() { + ax_err!(PermissionDenied) + } else { + current().fs.lock().as_mut().unwrap().current_dir = node; + current().fs.lock().as_mut().unwrap().current_path = abs_path; + Ok(()) + } +} + +struct CurrentWorkingDirectoryImpl; + +#[crate_interface::impl_interface] +impl CurrentWorkingDirectoryOps for CurrentWorkingDirectoryImpl { + fn init_rootfs(mount_points: Vec) { + init_rootfs(mount_points) + } + fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef { + parent_node_of(dir, path) + } + fn absolute_path(path: &str) -> AxResult { + absolute_path(path) + } + fn current_dir() -> AxResult { + current_dir() + } + fn set_current_dir(path: &str) -> AxResult { + set_current_dir(path) + } +} diff --git a/modules/ruxtask/src/lib.rs b/modules/ruxtask/src/lib.rs index 8993df9f0..fbbbaf76d 100644 --- a/modules/ruxtask/src/lib.rs +++ b/modules/ruxtask/src/lib.rs @@ -45,9 +45,13 @@ cfg_if::cfg_if! { extern crate alloc; mod run_queue; - mod task; + pub mod task; mod api; mod wait_queue; + #[cfg(feature = "paging")] + pub mod vma; + // #[cfg(feature = "fs")] + pub mod fs; #[cfg(feature = "irq")] /// load average pub mod loadavg; diff --git a/modules/ruxtask/src/run_queue.rs b/modules/ruxtask/src/run_queue.rs index 869363710..6b9ac647e 100644 --- a/modules/ruxtask/src/run_queue.rs +++ b/modules/ruxtask/src/run_queue.rs @@ -7,25 +7,20 @@ * See the Mulan PSL v2 for more details. */ +use crate::{current, fs::RUX_FILE_LIMIT}; use alloc::collections::VecDeque; use alloc::sync::Arc; use axerrno::{LinuxError, LinuxResult}; -use kernel_guard::NoPreemptIrqSave; use lazy_init::LazyInit; -use ruxfdtable::{FD_TABLE, RUX_FILE_LIMIT}; -use ruxrand::ExpRand; use scheduler::BaseScheduler; -use spinlock::{BaseSpinLock, Combine, SpinNoIrq}; +use spinlock::SpinNoIrq; use crate::task::{CurrentTask, TaskState}; use crate::{AxTaskRef, Scheduler, TaskInner, WaitQueue}; -pub(crate) const BACKOFF_LIMIT: u32 = 8; -pub(crate) type DefaultStrategy = Combine, spinlock::NoOp>; -pub(crate) type RQLock = BaseSpinLock; - // TODO: per-CPU -pub(crate) static RUN_QUEUE: LazyInit> = LazyInit::new(); +pub(crate) static RUN_QUEUE: LazyInit> = LazyInit::new(); +// pub static BLOCKING_QUEUE: LazyInit> = LazyInit::new(); // TODO: per-CPU static EXITED_TASKS: SpinNoIrq> = SpinNoIrq::new(VecDeque::new()); @@ -40,11 +35,11 @@ pub(crate) struct AxRunQueue { } impl AxRunQueue { - pub fn new() -> RQLock { + pub fn new() -> SpinNoIrq { let gc_task = TaskInner::new(gc_entry, "gc".into(), ruxconfig::TASK_STACK_SIZE); let mut scheduler = Scheduler::new(); scheduler.add_task(gc_task); - RQLock::new(Self { scheduler }) + SpinNoIrq::new(Self { scheduler }) } pub fn add_task(&mut self, task: AxTaskRef) { @@ -79,6 +74,9 @@ impl AxRunQueue { #[cfg(feature = "preempt")] pub fn preempt_resched(&mut self) { let curr = crate::current(); + if !curr.is_running() { + error!("id_name={:#?}", curr.id_name()); + } assert!(curr.is_running()); // When we get the mutable reference of the run queue, we must @@ -102,10 +100,12 @@ impl AxRunQueue { pub fn exit_current(&mut self, exit_code: i32) -> ! { let curr = crate::current(); + error!("exit_current current id={}", curr.id_name()); debug!("task exit: {}, exit_code={}", curr.id_name(), exit_code); assert!(curr.is_running()); assert!(!curr.is_idle()); - if curr.is_init() { + + if crate::current().is_init() { EXITED_TASKS.lock().clear(); ruxhal::misc::terminate(); } else { @@ -179,7 +179,17 @@ impl AxRunQueue { // Safety: IRQs must be disabled at this time. IDLE_TASK.current_ref_raw().get_unchecked().clone() }); - self.switch_to(prev, next); + + if next.process_id().as_u64() == prev.process_id().as_u64() { + self.switch_to(prev, next); + } else { + error!( + "switch_to: prev_task={:?}, next_task={:?}", + prev.id_name(), + next.id_name() + ); + self.switch_process(prev, next); + } } fn switch_to(&mut self, prev_task: CurrentTask, next_task: AxTaskRef) { @@ -208,16 +218,49 @@ impl AxRunQueue { (*prev_ctx_ptr).switch_to(&*next_ctx_ptr); } } + + fn switch_process(&mut self, prev_task: CurrentTask, next_task: AxTaskRef) { + trace!("ret_from_fork : {}", next_task.id_name()); + #[cfg(feature = "preempt")] + next_task.set_preempt_pending(false); + next_task.set_state(TaskState::Running); + + let binding = prev_task.clone(); + let prev_inner = binding.inner(); + let binding = next_task.clone(); + let next_inner = binding.inner(); + unsafe { + let cur_ctx_ptr = prev_inner.ctx_mut_ptr(); + let next_ctx_ptr = next_inner.ctx_mut_ptr(); + + let next_page_table_addr = next_inner.pagetable.lock().root_paddr(); + + // The strong reference count of `prev_task` will be decremented by 1, + // but won't be dropped until `gc_entry()` is called. + assert!(Arc::strong_count(prev_task.as_task_ref()) > 1); + assert!(Arc::strong_count(&next_task) >= 1); + + // warn!( + // "process switch: prev_task={:?}, stack_top={:?}; next_task={:?}, stack_top={:?}", + // prev_task.id_name(), + // prev_inner.stack_top(), + // next_task.clone().id_name(), + // next_inner.stack_top() + // ); + CurrentTask::set_current(prev_task, next_task); + + // restore the registers content from next_task, and overwrite the content from children's stack. + (*cur_ctx_ptr).switch_process_to(&(*next_ctx_ptr), next_page_table_addr); + } + } } fn gc_flush_file(fd: usize) -> LinuxResult { trace!("gc task flush: {}", fd); - FD_TABLE - .read() - .get(fd) - .cloned() - .ok_or(LinuxError::EBADF)? - .flush() + let binding = current(); + let mut fs = binding.fs.lock(); + let fd_table = &fs.as_mut().unwrap().fd_table; + fd_table.get(fd).cloned().ok_or(LinuxError::EBADF)?.flush() } fn gc_entry() { @@ -249,19 +292,19 @@ fn gc_entry() { } pub(crate) fn init() { + let main_task = TaskInner::new_init("main".into()); + main_task.set_state(TaskState::Running); + unsafe { CurrentTask::init_current(main_task) }; + const IDLE_TASK_STACK_SIZE: usize = 4096; let idle_task = TaskInner::new(|| crate::run_idle(), "idle".into(), IDLE_TASK_STACK_SIZE); IDLE_TASK.with_current(|i| i.init_by(idle_task.clone())); - let main_task = TaskInner::new_init("main".into()); - main_task.set_state(TaskState::Running); - RUN_QUEUE.init_by(AxRunQueue::new()); - unsafe { CurrentTask::init_current(main_task) } } pub(crate) fn init_secondary() { - let idle_task = TaskInner::new_init("idle".into()); + let idle_task = TaskInner::new_idle("idle".into()); idle_task.set_state(TaskState::Running); IDLE_TASK.with_current(|i| i.init_by(idle_task.clone())); unsafe { CurrentTask::init_current(idle_task) } diff --git a/modules/ruxtask/src/task.rs b/modules/ruxtask/src/task.rs index 31950e583..0d8ab6d39 100644 --- a/modules/ruxtask/src/task.rs +++ b/modules/ruxtask/src/task.rs @@ -7,10 +7,22 @@ * See the Mulan PSL v2 for more details. */ -use alloc::{boxed::Box, string::String, sync::Arc}; +use crate::fs::FileSystem; +use alloc::collections::BTreeMap; +use alloc::{ + boxed::Box, + string::String, + sync::{Arc, Weak}, +}; use core::ops::Deref; use core::sync::atomic::{AtomicBool, AtomicI32, AtomicU64, AtomicU8, Ordering}; use core::{alloc::Layout, cell::UnsafeCell, fmt, ptr::NonNull}; +use page_table::PageSize; +use page_table_entry::MappingFlags; +use ruxhal::mem::direct_virt_to_phys; +#[cfg(feature = "paging")] +use ruxhal::{mem::phys_to_virt, paging::PageTable}; +use spinlock::SpinNoIrq; #[cfg(feature = "preempt")] use core::sync::atomic::AtomicUsize; @@ -18,11 +30,13 @@ use core::sync::atomic::AtomicUsize; #[cfg(feature = "tls")] use ruxhal::tls::TlsArea; -use memory_addr::{align_up_4k, VirtAddr}; -use ruxhal::arch::TaskContext; +use memory_addr::{align_up_4k, VirtAddr, PAGE_SIZE_4K}; +use ruxhal::arch::{flush_tlb, TaskContext}; #[cfg(not(feature = "musl"))] use crate::tsd::{DestrFunction, KEYS, TSD}; +use crate::vma::MmapStruct; +use crate::current; use crate::{AxRunQueue, AxTask, AxTaskRef, WaitQueue}; /// A unique identifier for a thread. @@ -45,11 +59,12 @@ pub enum TaskState { /// The inner task structure. pub struct TaskInner { + parent_process: Option>, + process_task: Weak, id: TaskId, name: String, is_idle: bool, is_init: bool, - entry: Option<*mut dyn FnOnce()>, state: AtomicU8, @@ -65,7 +80,7 @@ pub struct TaskInner { exit_code: AtomicI32, wait_for_exit: WaitQueue, - kstack: Option, + kstack: SpinNoIrq>>, ctx: UnsafeCell, #[cfg(feature = "tls")] @@ -80,6 +95,13 @@ pub struct TaskInner { // clear tid #[cfg(feature = "musl")] tl: AtomicU64, + #[cfg(feature = "paging")] + // The page table of the task. + pub pagetable: Arc>, + // file system + pub fs: Arc>>, + // memory management + pub mm: Arc, } impl TaskId { @@ -132,6 +154,32 @@ impl TaskInner { alloc::format!("Task({}, {:?})", self.id.as_u64(), self.name) } + /// Get pointer for parent process task + pub fn parent_process(&self) -> Option { + if let Some(parent_process) = self.parent_process.as_ref() { + return parent_process.upgrade(); + } + None + } + + /// Get process task + pub fn process_task(&self) -> Arc { + if let Some(process_task) = self.process_task.upgrade() { + process_task.clone() + } else { + current().as_task_ref().clone() + } + } + + /// Get pid of the process of the task. + pub fn process_id(&self) -> TaskId { + if let Some(process_task) = self.process_task.upgrade() { + process_task.id + } else { + self.id + } + } + /// Wait for the task to exit, and return the exit code. /// /// It will return immediately if the task has already exited (but not dropped). @@ -152,10 +200,21 @@ impl TaskInner { } } +static PROCESS_MAP: SpinNoIrq>> = SpinNoIrq::new(BTreeMap::new()); + +use log::error; // private methods impl TaskInner { + // clone a thread fn new_common(id: TaskId, name: String) -> Self { + error!( + "new_common: process_id={:#}, name={:?}", + current().id_name(), + id.0 + ); Self { + parent_process: Some(Arc::downgrade(current().as_task_ref())), + process_task: Arc::downgrade(¤t().process_task()), id, name, is_idle: false, @@ -171,7 +230,7 @@ impl TaskInner { preempt_disable_count: AtomicUsize::new(0), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), - kstack: None, + kstack: SpinNoIrq::new(Arc::new(None)), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] tls: TlsArea::alloc(), @@ -181,6 +240,10 @@ impl TaskInner { set_tid: AtomicU64::new(0), #[cfg(feature = "musl")] tl: AtomicU64::new(0), + #[cfg(feature = "paging")] + pagetable: current().pagetable.clone(), + fs: current().fs.clone(), + mm: current().mm.clone(), } } @@ -192,7 +255,10 @@ impl TaskInner { set_tid: AtomicU64, tl: AtomicU64, ) -> Self { + use crate::current; Self { + parent_process: Some(Arc::downgrade(current().as_task_ref())), + process_task: Arc::downgrade(¤t().process_task()), id, name, is_idle: false, @@ -208,16 +274,32 @@ impl TaskInner { preempt_disable_count: AtomicUsize::new(0), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), - kstack: None, + kstack: SpinNoIrq::new(Arc::new(None)), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] tls: TlsArea::new_with_addr(tls), set_tid, // clear child tid tl, + #[cfg(feature = "paging")] + pagetable: current().pagetable.clone(), + fs: current().fs.clone(), + mm: current().mm.clone(), } } + pub fn stack_top(&self) -> VirtAddr { + self.kstack.lock().as_ref().as_ref().unwrap().top() + } + + pub fn set_stack_top(&self, begin: usize, size: usize) { + error!("set_stack_top: begin={:#x}, size={:#x}", begin, size); + *self.kstack.lock() = Arc::new(Some(TaskStack { + ptr: NonNull::new(begin as *mut u8).unwrap(), + layout: Layout::from_size_align(size, PAGE_SIZE_4K).unwrap(), + })); + } + /// for set_tid_addr #[cfg(feature = "musl")] pub fn set_child_tid(&self, tid: usize) { @@ -239,9 +321,7 @@ impl TaskInner { F: FnOnce() + Send + 'static, { let mut t = Self::new_common_tls(TaskId::new(), name, tls, set_tid, tl); - debug!("new task: {}", t.id_name()); let kstack = TaskStack::alloc(align_up_4k(stack_size)); - #[cfg(feature = "tls")] let tls = VirtAddr::from(t.tls.tls_ptr() as usize); #[cfg(not(feature = "tls"))] @@ -249,7 +329,7 @@ impl TaskInner { t.entry = Some(Box::into_raw(Box::new(entry))); t.ctx.get_mut().init(task_entry as usize, kstack.top(), tls); - t.kstack = Some(kstack); + t.kstack = SpinNoIrq::new(Arc::new(Some(kstack))); if t.name == "idle" { t.is_idle = true; } @@ -272,13 +352,182 @@ impl TaskInner { t.entry = Some(Box::into_raw(Box::new(entry))); t.ctx.get_mut().init(task_entry as usize, kstack.top(), tls); - t.kstack = Some(kstack); + t.kstack = SpinNoIrq::new(Arc::new(Some(kstack))); if t.name == "idle" { t.is_idle = true; } Arc::new(AxTask::new(t)) } + pub fn fork() -> AxTaskRef { + use crate::alloc::string::ToString; + + let current_task = crate::current(); + let name = current_task.as_task_ref().name().to_string(); + let current_stack_bindings = current_task.as_task_ref().kstack.lock(); + let current_stack = current_stack_bindings.as_ref().as_ref().clone().unwrap(); + let current_stack_top = current_stack.top(); + let stack_size = current_stack.layout.size(); + debug!( + "fork: current_stack_top={:#x}, stack_size={:#x}", + current_stack_top, stack_size + ); + + #[cfg(feature = "paging")] + // TODO: clone parent page table, and mark all unshared pages to read-only + let mut cloned_page_table = PageTable::try_new().expect("failed to create page table"); + let cloned_mm = current().mm.as_ref().clone(); + + // clone the global shared pages (as system memory) + // TODO: exclude the stack page from the cloned page table + #[cfg(feature = "paging")] + for r in ruxhal::mem::memory_regions() { + cloned_page_table + .map_region( + phys_to_virt(r.paddr), + r.paddr, + r.size, + r.flags.into(), + false, + ) + .expect("failed to map region when forking"); + } + + // mapping the page for stack to the process's stack, stack must keep at the same position. + // TODO: merge these code with previous. + #[cfg(feature = "paging")] + let new_stack = TaskStack::alloc(align_up_4k(stack_size)); + let new_stack_vaddr = new_stack.end(); + let stack_paddr = direct_virt_to_phys(new_stack_vaddr); + + // Note: the stack region is mapped to the same position as the parent process's stack, be careful when update the stack region for the forked process. + let (_, prev_flag, _) = cloned_page_table + .query(current_stack.end()) + .expect("failed to query stack region when forking"); + cloned_page_table + .unmap_region(current_stack.end(), align_up_4k(stack_size)) + .expect("failed to unmap stack region when forking"); + cloned_page_table + .map_region( + current_stack.end(), + stack_paddr, + stack_size, + prev_flag, + true, + ) + .expect("failed to map stack region when forking"); + + // clone parent pages in memory, and mark all unshared pages to read-only + for (vaddr, page_info) in cloned_mm.mem_map.lock().iter() { + let paddr = page_info.paddr; + cloned_page_table + .map((*vaddr).into(), paddr, PageSize::Size4K, MappingFlags::READ) + .expect("failed to map when forking"); + } + + // mark the parent process's page table to read-only. + for (vaddr, _) in current_task.mm.mem_map.lock().iter() { + let mut page_table = current_task.pagetable.lock(); + let vaddr = VirtAddr::from(*vaddr); + let (_, mapping_flag, _) = page_table + .query(vaddr) + .expect("Inconsistent page table with mem_map"); + if mapping_flag.contains(MappingFlags::EXECUTE) { + page_table + .update( + vaddr, + None, + Some(MappingFlags::READ | MappingFlags::EXECUTE), + ) + .expect("failed to update mapping when forking"); + + cloned_page_table + .update( + vaddr, + None, + Some(MappingFlags::READ | MappingFlags::EXECUTE), + ) + .expect("failed to update mapping when forking"); + } else { + page_table + .update(vaddr, None, Some(MappingFlags::READ)) + .expect("failed to update mapping when forking"); + + } + flush_tlb(Some(vaddr)); + } + + let mut t = Self { + parent_process: Some(Arc::downgrade(current_task.as_task_ref())), + process_task: Weak::new(), + id: TaskId::new(), + name, + is_idle: false, + is_init: false, + entry: None, + state: AtomicU8::new(TaskState::Ready as u8), + in_wait_queue: AtomicBool::new(false), + #[cfg(feature = "irq")] + in_timer_list: AtomicBool::new(false), + #[cfg(feature = "preempt")] + need_resched: AtomicBool::new(false), + #[cfg(feature = "preempt")] + preempt_disable_count: AtomicUsize::new(0), + exit_code: AtomicI32::new(0), + wait_for_exit: WaitQueue::new(), + kstack: SpinNoIrq::new(Arc::new(Some(new_stack))), + ctx: UnsafeCell::new(TaskContext::new()), + #[cfg(feature = "tls")] + tls: TlsArea::alloc(), + #[cfg(not(feature = "musl"))] + tsd: spinlock::SpinNoIrq::new([core::ptr::null_mut(); ruxconfig::PTHREAD_KEY_MAX]), + #[cfg(feature = "musl")] + set_tid: AtomicU64::new(0), + #[cfg(feature = "musl")] + tl: AtomicU64::new(0), + #[cfg(feature = "paging")] + pagetable: Arc::new(SpinNoIrq::new(cloned_page_table)), + fs: Arc::new(SpinNoIrq::new(current_task.fs.lock().clone())), + mm: Arc::new(cloned_mm), + }; + + debug!("new task forked: {}", t.id_name()); + + #[cfg(feature = "tls")] + let tls = VirtAddr::from(t.tls.tls_ptr() as usize); + #[cfg(not(feature = "tls"))] + let tls = VirtAddr::from(0); + + t.entry = None; + t.ctx.get_mut().init( + task_entry as usize, + t.kstack.lock().as_ref().as_ref().unwrap().top(), + tls, + ); + let task_ref = Arc::new(AxTask::new(t)); + + warn!( + "start: copy stack content: current_stack_top={:#x} => new_stack_addr={:#x}", + current_stack.end(), + new_stack_vaddr + ); + unsafe { + // copy the stack content from current stack to new stack + (*task_ref.ctx_mut_ptr()).save_current_content( + current_stack.end().as_ptr(), + new_stack_vaddr.as_mut_ptr(), + stack_size, + ); + } + warn!( + "end: copy stack content: current_stack_top={:#x} => new_stack_addr={:#x}", + current_stack.end(), + new_stack_vaddr + ); + + task_ref + } + /// Creates an "init task" using the current CPU states, to use as the /// current task. /// @@ -288,11 +537,92 @@ impl TaskInner { /// And there is no need to set the `entry`, `kstack` or `tls` fields, as /// they will be filled automatically when the task is switches out. pub(crate) fn new_init(name: String) -> AxTaskRef { - let mut t = Self::new_common(TaskId::new(), name); - t.is_init = true; - if t.name == "idle" { - t.is_idle = true; - } + let mut t = Self { + parent_process: None, + process_task: Weak::new(), + id: TaskId::new(), + name, + is_idle: false, + is_init: true, + entry: None, + state: AtomicU8::new(TaskState::Ready as u8), + in_wait_queue: AtomicBool::new(false), + #[cfg(feature = "irq")] + in_timer_list: AtomicBool::new(false), + #[cfg(feature = "preempt")] + need_resched: AtomicBool::new(false), + #[cfg(feature = "preempt")] + preempt_disable_count: AtomicUsize::new(0), + exit_code: AtomicI32::new(0), + wait_for_exit: WaitQueue::new(), + kstack: SpinNoIrq::new(Arc::new(None)), + ctx: UnsafeCell::new(TaskContext::new()), + #[cfg(feature = "tls")] + tls: TlsArea::alloc(), + #[cfg(not(feature = "musl"))] + tsd: spinlock::SpinNoIrq::new([core::ptr::null_mut(); ruxconfig::PTHREAD_KEY_MAX]), + #[cfg(feature = "musl")] + set_tid: AtomicU64::new(0), + #[cfg(feature = "musl")] + tl: AtomicU64::new(0), + #[cfg(feature = "paging")] + pagetable: Arc::new(SpinNoIrq::new( + PageTable::try_new().expect("failed to create page table"), + )), + fs: Arc::new(SpinNoIrq::new(None)), + mm: Arc::new(MmapStruct::new()), + }; + error!("new init task: {}", t.id_name()); + t.set_stack_top(boot_stack as usize, ruxconfig::TASK_STACK_SIZE); + t.ctx.get_mut().init( + task_entry as usize, + VirtAddr::from(boot_stack as usize), + VirtAddr::from(t.tls.tls_ptr() as usize), + ); + let task_ref = Arc::new(AxTask::new(t)); + PROCESS_MAP + .lock() + .insert(task_ref.id().as_u64(), task_ref.clone()); + task_ref + } + + pub fn new_idle(name: String) -> AxTaskRef { + let bindings = PROCESS_MAP.lock(); + let (&_parent_id, &ref task_ref) = bindings.first_key_value().unwrap(); + let t = Self { + parent_process: Some(Arc::downgrade(task_ref)), + process_task: task_ref.process_task.clone(), + id: TaskId::new(), + name, + is_idle: true, + is_init: false, + entry: None, + state: AtomicU8::new(TaskState::Ready as u8), + in_wait_queue: AtomicBool::new(false), + #[cfg(feature = "irq")] + in_timer_list: AtomicBool::new(false), + #[cfg(feature = "preempt")] + need_resched: AtomicBool::new(false), + #[cfg(feature = "preempt")] + preempt_disable_count: AtomicUsize::new(0), + exit_code: AtomicI32::new(0), + wait_for_exit: WaitQueue::new(), + kstack: SpinNoIrq::new(Arc::new(None)), + ctx: UnsafeCell::new(TaskContext::new()), + #[cfg(feature = "tls")] + tls: TlsArea::alloc(), + #[cfg(not(feature = "musl"))] + tsd: spinlock::SpinNoIrq::new([core::ptr::null_mut(); ruxconfig::PTHREAD_KEY_MAX]), + #[cfg(feature = "musl")] + set_tid: AtomicU64::new(0), + #[cfg(feature = "musl")] + tl: AtomicU64::new(0), + #[cfg(feature = "paging")] + pagetable: task_ref.pagetable.clone(), + fs: task_ref.fs.clone(), + mm: task_ref.mm.clone(), + }; + Arc::new(AxTask::new(t)) } @@ -450,11 +780,12 @@ impl fmt::Debug for TaskInner { impl Drop for TaskInner { fn drop(&mut self) { - debug!("task drop: {}", self.id_name()); + error!("task drop: {}", self.id_name()); } } -struct TaskStack { +#[derive(Debug)] +pub struct TaskStack { ptr: NonNull, layout: Layout, } @@ -472,10 +803,23 @@ impl TaskStack { pub const fn top(&self) -> VirtAddr { unsafe { core::mem::transmute(self.ptr.as_ptr().add(self.layout.size())) } } + + pub const fn end(&self) -> VirtAddr { + unsafe { core::mem::transmute(self.ptr.as_ptr()) } + } + + pub fn size(&self) -> usize { + self.layout.size() + } } impl Drop for TaskStack { fn drop(&mut self) { + warn!( + "taskStack drop: ptr={:#x}, size={:#x}", + self.ptr.as_ptr() as usize, + self.layout.size() + ); unsafe { alloc::alloc::dealloc(self.ptr.as_ptr(), self.layout) } } } @@ -504,7 +848,7 @@ impl CurrentTask { &self.0 } - pub(crate) fn clone(&self) -> AxTaskRef { + pub fn clone(&self) -> AxTaskRef { self.0.deref().clone() } @@ -520,6 +864,10 @@ impl CurrentTask { } pub(crate) unsafe fn set_current(prev: Self, next: AxTaskRef) { + error!( + "-----------set_current-------------,next ptr={:#}", + next.id_name() + ); let Self(arc) = prev; ManuallyDrop::into_inner(arc); // `call Arc::drop()` to decrease prev task reference count. let ptr = Arc::into_raw(next); @@ -541,7 +889,14 @@ extern "C" fn task_entry() -> ! { ruxhal::arch::enable_irqs(); let task = crate::current(); if let Some(entry) = task.entry { - unsafe { Box::from_raw(entry)() }; + unsafe { + let in_entry = Box::from_raw(entry); + in_entry() + }; } crate::exit(0); } + +extern "C" { + fn boot_stack(); +} diff --git a/modules/ruxtask/src/vma.rs b/modules/ruxtask/src/vma.rs new file mode 100644 index 000000000..315ad19cb --- /dev/null +++ b/modules/ruxtask/src/vma.rs @@ -0,0 +1,197 @@ +/* Copyright (c) [2023] [Syswonder Community] + * [Ruxos] is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +//! Virtual Memory Area (VMA) data structure. +//! +//! This module provides data structures for virtual memory area (VMA) management. +//! TODO: use `Mutex` to replace `SpinNoIrq` to make it more efficient. + +use crate::current; +use crate::{fs::File, TaskId}; +use alloc::vec::Vec; +use alloc::{collections::BTreeMap, sync::Arc}; +use axalloc::global_allocator; +use memory_addr::{PhysAddr, PAGE_SIZE_4K}; +#[cfg(feature = "fs")] +use ruxfs::fops::OpenOptions; +use ruxhal::mem::phys_to_virt; +use spinlock::SpinNoIrq; + +// use `used_fs` instead of `#[cfg(feature = "fs")]{}` to cancel the scope of code. +#[cfg(feature = "fs")] +macro_rules! used_fs { + ($($code:tt)*) => {$($code)*}; + } + +#[cfg(not(feature = "fs"))] +macro_rules! used_fs { + ($($code:tt)*) => {}; +} + +// TODO: move defination of `SWAP_MAX` and `SWAP_PATH` from const numbers to `ruxconfig`. +used_fs! { + // pub(crate) const SWAP_MAX: usize = 1024 * 1024 * 1024; + pub(crate) const SWAP_MAX: usize = 0; + pub(crate) const SWAP_PATH: &str = "swap.raw\0"; + pub static SWAPED_MAP: SpinNoIrq>> = SpinNoIrq::new(BTreeMap::new()); // Vaddr => (page_size, offset_at_swaped) + lazy_static::lazy_static! { + pub static ref SWAP_FILE: Arc = open_swap_file(SWAP_PATH); + pub static ref BITMAP_FREE: SpinNoIrq> = SpinNoIrq::new((0..SWAP_MAX).step_by(PAGE_SIZE_4K).collect()); + } +} + +/// open target file +#[cfg(feature = "fs")] +fn open_swap_file(filename: &str) -> Arc { + let mut opt = OpenOptions::new(); + opt.read(true); + opt.write(true); + opt.append(true); + opt.create(true); + + let file = ruxfs::fops::File::open(filename, &opt).expect("create swap file failed"); + Arc::new(File::new(file)) +} + +/// Data structure for file mapping. +#[derive(Clone)] +pub struct FileInfo { + pub file: Arc, + pub offset: usize, + pub size: usize, +} + +/// Data structure for information of mapping. +pub struct PageInfo { + pub paddr: PhysAddr, + #[cfg(feature = "fs")] + pub mapping_file: Option, +} + +/// Data structure for swaping out a page in a file. +#[derive(Debug, Clone)] +pub struct SwapInfo { + pub offset: usize, +} + +impl From for SwapInfo { + fn from(value: usize) -> Self { + SwapInfo { offset: value } + } +} + +/// Data structure for mmap for a specific process. +pub struct MmapStruct { + /// virtual memory area list + pub vma_map: SpinNoIrq>, + /// page that already loaded into memory + pub mem_map: SpinNoIrq>>, + /// pages that swapped out into swap file or disk + pub swaped_map: SpinNoIrq>>, +} + +/// clone data structure for MmapStruct (when forking). +impl Clone for MmapStruct { + fn clone(&self) -> Self { + Self { + vma_map: SpinNoIrq::new(self.vma_map.lock().clone()), + mem_map: SpinNoIrq::new(self.mem_map.lock().clone()), + swaped_map: SpinNoIrq::new(self.swaped_map.lock().clone()), + } + } +} + +// release memory of a page in swaping file +impl Drop for SwapInfo { + fn drop(&mut self) { + BITMAP_FREE.lock().push(self.offset); + } +} + +// release memory of a page in memory +impl Drop for PageInfo { + fn drop(&mut self) { + // use `global_allocator()` to dealloc pages. + global_allocator().dealloc_pages(phys_to_virt(self.paddr).as_usize(), 1); + } +} + +/// Data structure for mapping [start_addr, end_addr) with meta data. +#[derive(Clone)] +pub struct Vma { + /// start address of the mapping + pub start_addr: usize, + /// end address of the mapping + pub end_addr: usize, + /// file that the mapping is backed by + pub file: Option>, + /// offset in the file + pub offset: usize, + /// size of the mapping + pub prot: u32, + /// flags of the mapping + pub flags: u32, + /// process that the mapping belongs to + pub from_process: TaskId, +} + +impl MmapStruct { + /// Create a new `MmapStruct` instance. + pub const fn new() -> Self { + Self { + vma_map: SpinNoIrq::new(BTreeMap::new()), + mem_map: SpinNoIrq::new(BTreeMap::new()), + swaped_map: SpinNoIrq::new(BTreeMap::new()), + } + } +} + +/// Impl for Vma. +impl Vma { + pub fn new(_fid: i32, offset: usize, prot: u32, flags: u32) -> Self { + // #[cfg(feature = "fs")] + let file = if _fid < 0 { + None + } else { + let binding = current(); + let fs = binding.fs.lock(); + let fd_table = &fs.as_ref().unwrap().fd_table; + let f = fd_table.get(_fid as usize).unwrap(); + Some( + f.clone() + .into_any() + .downcast::() + .expect("should be effective fid"), + ) + }; + Vma { + start_addr: 0, + end_addr: 0, + // #[cfg(feature = "fs")] + file, + offset, + flags, + prot, + from_process: current().id(), + } + } + + pub fn clone_from(vma: &Vma, start_addr: usize, end_addr: usize) -> Self { + Vma { + start_addr, + end_addr, + // #[cfg(feature = "fs")] + file: vma.file.clone(), + offset: vma.offset, + prot: vma.prot, + flags: vma.prot, + from_process: current().id(), + } + } +} From c08acb7e08228e6981042e5406b995dd1dbe6d32 Mon Sep 17 00:00:00 2001 From: WuZheng Date: Thu, 26 Sep 2024 11:18:23 +0800 Subject: [PATCH 03/15] fix problem for fs deadlock and context switch failds. --- api/ruxos_posix_api/src/imp/getrandom.rs | 1 + api/ruxos_posix_api/src/imp/ioctl.rs | 8 ++- api/ruxos_posix_api/src/imp/pthread/mod.rs | 1 - apps/c/dl/axbuild.mk | 2 +- apps/c/dl/features.txt | 1 + modules/ruxhal/src/arch/aarch64/context.rs | 47 +++++++-------- modules/ruxhal/src/trap.rs | 2 + modules/ruxmm/src/mem.rs | 4 +- modules/ruxruntime/src/lib.rs | 2 +- modules/ruxtask/src/api.rs | 17 ++---- modules/ruxtask/src/fs.rs | 5 +- modules/ruxtask/src/run_queue.rs | 67 +++++----------------- modules/ruxtask/src/task.rs | 23 ++------ modules/ruxtask/src/vma.rs | 6 +- ulib/ruxmusl/src/trap.rs | 1 + 15 files changed, 69 insertions(+), 118 deletions(-) diff --git a/api/ruxos_posix_api/src/imp/getrandom.rs b/api/ruxos_posix_api/src/imp/getrandom.rs index 61cafb103..15c2580c2 100644 --- a/api/ruxos_posix_api/src/imp/getrandom.rs +++ b/api/ruxos_posix_api/src/imp/getrandom.rs @@ -158,6 +158,7 @@ pub unsafe extern "C" fn sys_getrandom(buf: *mut c_void, buflen: size_t, flags: return Err(LinuxError::EFAULT); } + // BUG: flags are implemented wrongly, flags should be checks bit by bit match flags as _ { crate::ctypes::GRND_NONBLOCK => {} crate::ctypes::GRND_RANDOM => {} diff --git a/api/ruxos_posix_api/src/imp/ioctl.rs b/api/ruxos_posix_api/src/imp/ioctl.rs index 223a3c7b7..50fa8b89d 100644 --- a/api/ruxos_posix_api/src/imp/ioctl.rs +++ b/api/ruxos_posix_api/src/imp/ioctl.rs @@ -40,12 +40,18 @@ pub fn sys_ioctl(fd: c_int, request: usize, data: usize) -> c_int { } Ok(0) } + // TODO: a temporary solution for TIOCGWINSZ. TIOCGWINSZ => { let winsize = data as *mut ConsoleWinSize; unsafe { *winsize = ConsoleWinSize::default(); } - Ok(0) + if fd == 0 || fd == 1 || fd == 2{ + Ok(0) + } + else{ + Ok(-1) + } } TCGETS => { debug!("sys_ioctl: tty TCGETS"); diff --git a/api/ruxos_posix_api/src/imp/pthread/mod.rs b/api/ruxos_posix_api/src/imp/pthread/mod.rs index b0bfd0218..a9e7d1374 100644 --- a/api/ruxos_posix_api/src/imp/pthread/mod.rs +++ b/api/ruxos_posix_api/src/imp/pthread/mod.rs @@ -131,7 +131,6 @@ impl Pthread { fn current_ptr() -> *mut Pthread { let tid = ruxtask::current().id().as_u64(); - error!("current_ptr, tid: {}", tid); match TID_TO_PTHREAD.read().get(&tid) { None => core::ptr::null_mut(), Some(ptr) => ptr.0 as *mut Pthread, diff --git a/apps/c/dl/axbuild.mk b/apps/c/dl/axbuild.mk index 8c1a982aa..ac92d2d40 100644 --- a/apps/c/dl/axbuild.mk +++ b/apps/c/dl/axbuild.mk @@ -1,6 +1,6 @@ app-objs=main.o -ARGS = /bin/hello +ARGS = /bin/busybox,sh ENVS = V9P_PATH=${APP}/rootfs diff --git a/apps/c/dl/features.txt b/apps/c/dl/features.txt index ed120d4d3..3a5bcfa19 100644 --- a/apps/c/dl/features.txt +++ b/apps/c/dl/features.txt @@ -9,3 +9,4 @@ poll rtc signal virtio-9p +fp_simd diff --git a/modules/ruxhal/src/arch/aarch64/context.rs b/modules/ruxhal/src/arch/aarch64/context.rs index 5fc15460c..4b218b330 100644 --- a/modules/ruxhal/src/arch/aarch64/context.rs +++ b/modules/ruxhal/src/arch/aarch64/context.rs @@ -133,32 +133,20 @@ impl TaskContext { } } - /// Switches to another task in another process. + /// Switches to another task. /// /// It first saves the current task's context from CPU to this place, and then /// restores the next task's context from `next_ctx` to CPU. - pub fn switch_process_to(&mut self, next_ctx: &Self, page_table_addr: PhysAddr) { - #[cfg(feature = "fp_simd")] - self.fp_state.switch_to(&next_ctx.fp_state); - - // warn!("switch_to: {:#x?}", next_ctx); + #[inline(never)] + pub fn switch_to(&mut self, next_ctx: &Self, page_table_addr: PhysAddr) { unsafe { + #[cfg(feature = "fp_simd")] + fpstate_switch(&mut self.fp_state, &next_ctx.fp_state); // switch to the next process's page table, stack would be unavailable before context switch finished - write_page_table_root(page_table_addr); - context_switch(self, next_ctx) + // write_page_table_root(page_table_addr); + context_switch(self, next_ctx, page_table_addr.as_usize() as u64); } } - - /// Switches to another task. - /// - /// It first saves the current task's context from CPU to this place, and then - /// restores the next task's context from `next_ctx` to CPU. - pub fn switch_to(&mut self, next_ctx: &Self) { - #[cfg(feature = "fp_simd")] - self.fp_state.switch_to(&next_ctx.fp_state); - // warn!("switch_to: {:#x?}", next_ctx); - unsafe { context_switch(self, next_ctx) } - } } #[naked] @@ -191,9 +179,7 @@ unsafe extern "C" fn save_stack(src: *const u8, dst: *mut u8, size: usize) { #[naked] #[allow(named_asm_labels)] unsafe extern "C" fn save_current_context( - _current_task: &mut TaskContext, - // temp_stack_top: &u64, - // current_stack_top: &u64, + _current_task: &mut TaskContext ) { asm!( " @@ -246,7 +232,8 @@ unsafe extern "C" fn save_fpstate_context(_current_fpstate: &mut FpState) { } #[naked] -unsafe extern "C" fn context_switch(_current_task: &mut TaskContext, _next_task: &TaskContext) { +#[allow(named_asm_labels)] +unsafe extern "C" fn context_switch(_current_task: &mut TaskContext, _next_task: &TaskContext, _page_table_addr: u64) { asm!( " // save old context (callee-saved registers) @@ -260,6 +247,19 @@ unsafe extern "C" fn context_switch(_current_task: &mut TaskContext, _next_task: mrs x20, tpidr_el0 stp x19, x20, [x0] + // switch to next task's page table + mrs x19, TTBR1_EL1 + cmp x19, x2 + b.eq _switch_page_table_done + _switch_page_table: + mov x19, x2 + msr TTBR1_EL1, x19 + tlbi vmalle1 + dsb sy + isb + // no need to switch page table, just continue + _switch_page_table_done: + // restore new context ldp x19, x20, [x1] mov sp, x19 @@ -271,6 +271,7 @@ unsafe extern "C" fn context_switch(_current_task: &mut TaskContext, _next_task: ldp x27, x28, [x1, 10 * 8] ldp x29, x30, [x1, 12 * 8] + isb ret", options(noreturn), ) diff --git a/modules/ruxhal/src/trap.rs b/modules/ruxhal/src/trap.rs index 787a3e9a8..49f85e3fa 100644 --- a/modules/ruxhal/src/trap.rs +++ b/modules/ruxhal/src/trap.rs @@ -9,6 +9,7 @@ //! Trap handling. use crate_interface::{call_interface, def_interface}; +#[cfg(feature = "paging")] use page_table::MappingFlags; /// Several reasons for page missing exceptions. @@ -23,6 +24,7 @@ pub enum PageFaultCause { } /// `PageFaultCause` corresponding to `MappingFlags`. +#[cfg(feature = "paging")] impl Into for PageFaultCause { fn into(self) -> MappingFlags { match self { diff --git a/modules/ruxmm/src/mem.rs b/modules/ruxmm/src/mem.rs index 388510fd4..107e0d7cd 100644 --- a/modules/ruxmm/src/mem.rs +++ b/modules/ruxmm/src/mem.rs @@ -9,11 +9,9 @@ #[cfg(feature = "paging")] use crate::paging::pte_query; - use ruxdriver::virtio::AddressTranslate; use ruxhal::mem::{direct_virt_to_phys, PhysAddr, VirtAddr}; -#[cfg(feature = "paging")] struct AddressTranslateImpl; /// Converts a virtual address to a physical address. @@ -37,6 +35,6 @@ impl AddressTranslate for AddressTranslateImpl { #[crate_interface::impl_interface] impl AddressTranslate for AddressTranslateImpl { fn virt_to_phys(vaddr: VirtAddr) -> Option { - Some(direct_virt_to_phys(vaddr)) + Some(direct_virt_to_phys(vaddr).into()) } } diff --git a/modules/ruxruntime/src/lib.rs b/modules/ruxruntime/src/lib.rs index 3edd81522..b324b83a4 100644 --- a/modules/ruxruntime/src/lib.rs +++ b/modules/ruxruntime/src/lib.rs @@ -177,7 +177,7 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! { info!("Found physcial memory regions:"); for r in ruxhal::mem::memory_regions() { - info!( + error!( " [{:x?}, {:x?}) {} ({:?})", r.paddr, r.paddr + r.size, diff --git a/modules/ruxtask/src/api.rs b/modules/ruxtask/src/api.rs index fad33a827..edcd42f99 100644 --- a/modules/ruxtask/src/api.rs +++ b/modules/ruxtask/src/api.rs @@ -9,6 +9,8 @@ //! Task APIs for multi-task configuration. +use core::mem::ManuallyDrop; + use alloc::{string::String, sync::Arc}; pub(crate) use crate::run_queue::{AxRunQueue, RUN_QUEUE}; @@ -137,19 +139,12 @@ pub fn fork_task() -> Option { // Judge whether the parent process is blocked, if yes, add it to the blocking queue of the child process if current().id().as_u64() == current_id { RUN_QUEUE.lock().add_task(children_process.clone()); - - warn!( - "parent process[{}] is leaving, add it to the blocking queue of the child process[{}]", - current().id().as_u64(), - children_process.id().as_u64() - ); - return Some(children_process.clone()); + return Some(children_process); } - error!( - "children process[{}] is forked, return None", - current().id().as_u64() - ); + // should not drop the children_process here, because it will be taken in the parent process + let _ = ManuallyDrop::new(children_process); + return None; } diff --git a/modules/ruxtask/src/fs.rs b/modules/ruxtask/src/fs.rs index 3821d7a05..8853b18ac 100644 --- a/modules/ruxtask/src/fs.rs +++ b/modules/ruxtask/src/fs.rs @@ -33,7 +33,7 @@ pub fn get_file_like(fd: i32) -> LinuxResult> { let binding_task = current(); let mut binding_fs = binding_task.fs.lock(); let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; - fd_table.get(fd as usize).cloned().ok_or(LinuxError::EBADF) + fd_table.get(fd as usize).cloned().ok_or(LinuxError::EBADF).clone() } pub fn add_file_like(f: Arc) -> LinuxResult { @@ -49,8 +49,7 @@ pub fn close_file_like(fd: i32) -> LinuxResult { let binding_task = current(); let mut binding_fs = binding_task.fs.lock(); let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; - let f = fd_table.remove(fd as usize).ok_or(LinuxError::EBADF)?; - drop(f); + let _ = fd_table.remove(fd as usize).ok_or(LinuxError::EBADF)?; Ok(()) } diff --git a/modules/ruxtask/src/run_queue.rs b/modules/ruxtask/src/run_queue.rs index 6b9ac647e..30421569b 100644 --- a/modules/ruxtask/src/run_queue.rs +++ b/modules/ruxtask/src/run_queue.rs @@ -7,10 +7,13 @@ * See the Mulan PSL v2 for more details. */ -use crate::{current, fs::RUX_FILE_LIMIT}; +use log::error; + +use crate::fs::get_file_like; +use crate::{ fs::RUX_FILE_LIMIT}; use alloc::collections::VecDeque; use alloc::sync::Arc; -use axerrno::{LinuxError, LinuxResult}; +use axerrno::LinuxResult; use lazy_init::LazyInit; use scheduler::BaseScheduler; use spinlock::SpinNoIrq; @@ -20,7 +23,6 @@ use crate::{AxTaskRef, Scheduler, TaskInner, WaitQueue}; // TODO: per-CPU pub(crate) static RUN_QUEUE: LazyInit> = LazyInit::new(); -// pub static BLOCKING_QUEUE: LazyInit> = LazyInit::new(); // TODO: per-CPU static EXITED_TASKS: SpinNoIrq> = SpinNoIrq::new(VecDeque::new()); @@ -100,7 +102,6 @@ impl AxRunQueue { pub fn exit_current(&mut self, exit_code: i32) -> ! { let curr = crate::current(); - error!("exit_current current id={}", curr.id_name()); debug!("task exit: {}, exit_code={}", curr.id_name(), exit_code); assert!(curr.is_running()); assert!(!curr.is_idle()); @@ -180,16 +181,7 @@ impl AxRunQueue { IDLE_TASK.current_ref_raw().get_unchecked().clone() }); - if next.process_id().as_u64() == prev.process_id().as_u64() { - self.switch_to(prev, next); - } else { - error!( - "switch_to: prev_task={:?}, next_task={:?}", - prev.id_name(), - next.id_name() - ); - self.switch_process(prev, next); - } + self.switch_to(prev, next); } fn switch_to(&mut self, prev_task: CurrentTask, next_task: AxTaskRef) { @@ -204,7 +196,7 @@ impl AxRunQueue { if prev_task.ptr_eq(&next_task) { return; } - + // error!("next task: {}", next_task.id_name()); unsafe { let prev_ctx_ptr = prev_task.ctx_mut_ptr(); let next_ctx_ptr = next_task.ctx_mut_ptr(); @@ -214,53 +206,22 @@ impl AxRunQueue { assert!(Arc::strong_count(prev_task.as_task_ref()) > 1); assert!(Arc::strong_count(&next_task) >= 1); - CurrentTask::set_current(prev_task, next_task); - (*prev_ctx_ptr).switch_to(&*next_ctx_ptr); - } - } - - fn switch_process(&mut self, prev_task: CurrentTask, next_task: AxTaskRef) { - trace!("ret_from_fork : {}", next_task.id_name()); - #[cfg(feature = "preempt")] - next_task.set_preempt_pending(false); - next_task.set_state(TaskState::Running); - - let binding = prev_task.clone(); - let prev_inner = binding.inner(); - let binding = next_task.clone(); - let next_inner = binding.inner(); - unsafe { - let cur_ctx_ptr = prev_inner.ctx_mut_ptr(); - let next_ctx_ptr = next_inner.ctx_mut_ptr(); + let next_page_table = next_task.pagetable.lock(); + let root_paddr = next_page_table.root_paddr(); - let next_page_table_addr = next_inner.pagetable.lock().root_paddr(); - - // The strong reference count of `prev_task` will be decremented by 1, - // but won't be dropped until `gc_entry()` is called. - assert!(Arc::strong_count(prev_task.as_task_ref()) > 1); - assert!(Arc::strong_count(&next_task) >= 1); + // Drop the `next_page_table` here, so that it will not be dropped after context switch. + drop(next_page_table); - // warn!( - // "process switch: prev_task={:?}, stack_top={:?}; next_task={:?}, stack_top={:?}", - // prev_task.id_name(), - // prev_inner.stack_top(), - // next_task.clone().id_name(), - // next_inner.stack_top() - // ); CurrentTask::set_current(prev_task, next_task); - - // restore the registers content from next_task, and overwrite the content from children's stack. - (*cur_ctx_ptr).switch_process_to(&(*next_ctx_ptr), next_page_table_addr); + (*prev_ctx_ptr).switch_to(&*next_ctx_ptr, root_paddr); } } + } fn gc_flush_file(fd: usize) -> LinuxResult { trace!("gc task flush: {}", fd); - let binding = current(); - let mut fs = binding.fs.lock(); - let fd_table = &fs.as_mut().unwrap().fd_table; - fd_table.get(fd).cloned().ok_or(LinuxError::EBADF)?.flush() + get_file_like(fd as i32)?.flush() } fn gc_entry() { diff --git a/modules/ruxtask/src/task.rs b/modules/ruxtask/src/task.rs index 0d8ab6d39..7576a6ce8 100644 --- a/modules/ruxtask/src/task.rs +++ b/modules/ruxtask/src/task.rs @@ -35,6 +35,7 @@ use ruxhal::arch::{flush_tlb, TaskContext}; #[cfg(not(feature = "musl"))] use crate::tsd::{DestrFunction, KEYS, TSD}; +#[cfg(feature = "paging")] use crate::vma::MmapStruct; use crate::current; use crate::{AxRunQueue, AxTask, AxTaskRef, WaitQueue}; @@ -202,12 +203,11 @@ impl TaskInner { static PROCESS_MAP: SpinNoIrq>> = SpinNoIrq::new(BTreeMap::new()); -use log::error; // private methods impl TaskInner { // clone a thread fn new_common(id: TaskId, name: String) -> Self { - error!( + debug!( "new_common: process_id={:#}, name={:?}", current().id_name(), id.0 @@ -293,7 +293,7 @@ impl TaskInner { } pub fn set_stack_top(&self, begin: usize, size: usize) { - error!("set_stack_top: begin={:#x}, size={:#x}", begin, size); + debug!("set_stack_top: begin={:#x}, size={:#x}", begin, size); *self.kstack.lock() = Arc::new(Some(TaskStack { ptr: NonNull::new(begin as *mut u8).unwrap(), layout: Layout::from_size_align(size, PAGE_SIZE_4K).unwrap(), @@ -395,7 +395,6 @@ impl TaskInner { // mapping the page for stack to the process's stack, stack must keep at the same position. // TODO: merge these code with previous. - #[cfg(feature = "paging")] let new_stack = TaskStack::alloc(align_up_4k(stack_size)); let new_stack_vaddr = new_stack.end(); let stack_paddr = direct_virt_to_phys(new_stack_vaddr); @@ -506,11 +505,6 @@ impl TaskInner { ); let task_ref = Arc::new(AxTask::new(t)); - warn!( - "start: copy stack content: current_stack_top={:#x} => new_stack_addr={:#x}", - current_stack.end(), - new_stack_vaddr - ); unsafe { // copy the stack content from current stack to new stack (*task_ref.ctx_mut_ptr()).save_current_content( @@ -519,11 +513,6 @@ impl TaskInner { stack_size, ); } - warn!( - "end: copy stack content: current_stack_top={:#x} => new_stack_addr={:#x}", - current_stack.end(), - new_stack_vaddr - ); task_ref } @@ -572,7 +561,7 @@ impl TaskInner { fs: Arc::new(SpinNoIrq::new(None)), mm: Arc::new(MmapStruct::new()), }; - error!("new init task: {}", t.id_name()); + debug!("new init task: {}", t.id_name()); t.set_stack_top(boot_stack as usize, ruxconfig::TASK_STACK_SIZE); t.ctx.get_mut().init( task_entry as usize, @@ -704,7 +693,7 @@ impl TaskInner { self.preempt_disable_count.fetch_add(1, Ordering::Relaxed); } - #[inline] + #[cfg(feature = "preempt")] pub(crate) fn enable_preempt(&self, resched: bool) { if self.preempt_disable_count.fetch_sub(1, Ordering::Relaxed) == 1 && resched { @@ -864,7 +853,7 @@ impl CurrentTask { } pub(crate) unsafe fn set_current(prev: Self, next: AxTaskRef) { - error!( + debug!( "-----------set_current-------------,next ptr={:#}", next.id_name() ); diff --git a/modules/ruxtask/src/vma.rs b/modules/ruxtask/src/vma.rs index 315ad19cb..ef78ff921 100644 --- a/modules/ruxtask/src/vma.rs +++ b/modules/ruxtask/src/vma.rs @@ -13,6 +13,7 @@ //! TODO: use `Mutex` to replace `SpinNoIrq` to make it more efficient. use crate::current; +use crate::fs::get_file_like; use crate::{fs::File, TaskId}; use alloc::vec::Vec; use alloc::{collections::BTreeMap, sync::Arc}; @@ -159,10 +160,7 @@ impl Vma { let file = if _fid < 0 { None } else { - let binding = current(); - let fs = binding.fs.lock(); - let fd_table = &fs.as_ref().unwrap().fd_table; - let f = fd_table.get(_fid as usize).unwrap(); + let f = get_file_like(_fid).expect("invaild fd for vma"); Some( f.clone() .into_any() diff --git a/ulib/ruxmusl/src/trap.rs b/ulib/ruxmusl/src/trap.rs index 17fb9aecd..0c50e463f 100644 --- a/ulib/ruxmusl/src/trap.rs +++ b/ulib/ruxmusl/src/trap.rs @@ -12,6 +12,7 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl { fn handle_irq(_irq_num: usize) { #[cfg(feature = "irq")] { + // error!("irq {}", _irq_num); let guard = kernel_guard::NoPreempt::new(); ruxhal::irq::dispatch_irq(_irq_num); drop(guard); // rescheduling may occur when preemption is re-enabled. From e0cbc0fad0ab843f6e744d068c1c0767fc72b388 Mon Sep 17 00:00:00 2001 From: WuZheng Date: Fri, 27 Sep 2024 15:47:38 +0800 Subject: [PATCH 04/15] implement `sys_wait4` for multiprocess for aarch64. --- api/ruxos_posix_api/build.rs | 1 + api/ruxos_posix_api/src/imp/execve/mod.rs | 6 +- api/ruxos_posix_api/src/imp/fd_ops.rs | 5 ++ api/ruxos_posix_api/src/imp/getrandom.rs | 6 +- api/ruxos_posix_api/src/imp/ioctl.rs | 5 +- api/ruxos_posix_api/src/imp/mmap/trap.rs | 12 +--- api/ruxos_posix_api/src/imp/task.rs | 65 ++++++++++++++++++++++ api/ruxos_posix_api/src/imp/time.rs | 16 ++++++ api/ruxos_posix_api/src/lib.rs | 5 +- modules/ruxhal/src/arch/aarch64/context.rs | 20 ++----- modules/ruxruntime/src/lib.rs | 4 +- modules/ruxtask/src/fs.rs | 6 +- modules/ruxtask/src/run_queue.rs | 3 +- modules/ruxtask/src/task.rs | 24 ++++---- ulib/ruxmusl/src/aarch64/mod.rs | 12 ++++ ulib/ruxmusl/src/aarch64/syscall_id.rs | 2 + ulib/ruxmusl/src/trap.rs | 3 + 17 files changed, 147 insertions(+), 48 deletions(-) diff --git a/api/ruxos_posix_api/build.rs b/api/ruxos_posix_api/build.rs index a5b17661b..1c72378ed 100644 --- a/api/ruxos_posix_api/build.rs +++ b/api/ruxos_posix_api/build.rs @@ -105,6 +105,7 @@ typedef struct {{ "kstat", "stack_t", "ino_t", + "rusage", "dirent", ]; let allow_vars = [ diff --git a/api/ruxos_posix_api/src/imp/execve/mod.rs b/api/ruxos_posix_api/src/imp/execve/mod.rs index ad9a7cea0..127f58377 100644 --- a/api/ruxos_posix_api/src/imp/execve/mod.rs +++ b/api/ruxos_posix_api/src/imp/execve/mod.rs @@ -15,10 +15,14 @@ use crate::{ /// int execve(const char *pathname, char *const argv[], char *const envp[] ); pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! { - error!("execve: pathname {:?}, argv {:?}, envp {:?}", pathname, argv, envp); + debug!( + "execve: pathname {:?}, argv {:?}, envp {:?}", + pathname, argv, envp + ); use auxv::*; let path = char_ptr_to_str(pathname).unwrap(); + debug!("sys_execve: path is {}", path); let prog = load_elf::ElfProg::new(path); // get entry diff --git a/api/ruxos_posix_api/src/imp/fd_ops.rs b/api/ruxos_posix_api/src/imp/fd_ops.rs index b12bd1e4d..4fb7ba5a3 100644 --- a/api/ruxos_posix_api/src/imp/fd_ops.rs +++ b/api/ruxos_posix_api/src/imp/fd_ops.rs @@ -207,6 +207,11 @@ pub fn sys_fcntl(fd: c_int, cmd: c_int, arg: usize) -> c_int { syscall_body!(sys_fcntl, { match cmd as u32 { ctypes::F_DUPFD => dup_fd(fd), + ctypes::F_GETFD => { + // Return (as the function result) the file descriptor flags; the arg is ignored. + // temporary unsupport CLOEXEC flag + Ok(0) + } ctypes::F_DUPFD_CLOEXEC => { // TODO: Change fd flags dup_fd(fd) diff --git a/api/ruxos_posix_api/src/imp/getrandom.rs b/api/ruxos_posix_api/src/imp/getrandom.rs index 15c2580c2..e3241f729 100644 --- a/api/ruxos_posix_api/src/imp/getrandom.rs +++ b/api/ruxos_posix_api/src/imp/getrandom.rs @@ -159,10 +159,8 @@ pub unsafe extern "C" fn sys_getrandom(buf: *mut c_void, buflen: size_t, flags: } // BUG: flags are implemented wrongly, flags should be checks bit by bit - match flags as _ { - crate::ctypes::GRND_NONBLOCK => {} - crate::ctypes::GRND_RANDOM => {} - _ => return Err(LinuxError::EINVAL), + if flags != 0 { + warn!("flags are not implemented yet, flags: {}, ignored", flags); } // fill the buffer 8 bytes at a time first, then fill the remaining bytes let buflen_mod = buflen % (core::mem::size_of::() / core::mem::size_of::()); diff --git a/api/ruxos_posix_api/src/imp/ioctl.rs b/api/ruxos_posix_api/src/imp/ioctl.rs index 50fa8b89d..e0d8e5b4d 100644 --- a/api/ruxos_posix_api/src/imp/ioctl.rs +++ b/api/ruxos_posix_api/src/imp/ioctl.rs @@ -46,10 +46,9 @@ pub fn sys_ioctl(fd: c_int, request: usize, data: usize) -> c_int { unsafe { *winsize = ConsoleWinSize::default(); } - if fd == 0 || fd == 1 || fd == 2{ + if fd == 0 || fd == 1 || fd == 2 { Ok(0) - } - else{ + } else { Ok(-1) } } diff --git a/api/ruxos_posix_api/src/imp/mmap/trap.rs b/api/ruxos_posix_api/src/imp/mmap/trap.rs index 63b343d6f..692186621 100644 --- a/api/ruxos_posix_api/src/imp/mmap/trap.rs +++ b/api/ruxos_posix_api/src/imp/mmap/trap.rs @@ -21,7 +21,8 @@ use crate::imp::mmap::utils::get_mflags_from_usize; use alloc::sync::Arc; use core::{ cmp::min, - ops::{Bound, DerefMut}, sync::atomic::{fence, Ordering}, + ops::{Bound, DerefMut}, + sync::atomic::{fence, Ordering}, }; use memory_addr::PAGE_SIZE_4K; use page_table::MappingFlags; @@ -41,9 +42,6 @@ struct TrapHandlerImpl; #[crate_interface::impl_interface] impl ruxhal::trap::TrapHandler for TrapHandlerImpl { fn handle_page_fault(vaddr: usize, cause: PageFaultCause) -> bool { - // warn!("----->handle_page_fault: vaddr=0x{:x?}, cause={:?}", vaddr, cause); - - // debug!("handle_page_fault: vaddr=0x{:x?}, cause={:?}", vaddr, cause); let binding_task = current(); let mut binding_mem_map = binding_task.mm.vma_map.lock(); let vma_map = binding_mem_map.deref_mut(); @@ -209,11 +207,7 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl { dst.copy_from(vaddr as *mut u8, size); } let paddr = direct_virt_to_phys(fake_vaddr); - let mapping_file = memory_map - .get(&vaddr.into()) - .unwrap() - .mapping_file - .clone(); + let mapping_file = memory_map.get(&vaddr.into()).unwrap().mapping_file.clone(); memory_map.remove(&vaddr.into()); memory_map.insert( vaddr.into(), diff --git a/api/ruxos_posix_api/src/imp/task.rs b/api/ruxos_posix_api/src/imp/task.rs index dc202bdb0..19db94bc6 100644 --- a/api/ruxos_posix_api/src/imp/task.rs +++ b/api/ruxos_posix_api/src/imp/task.rs @@ -7,8 +7,11 @@ * See the Mulan PSL v2 for more details. */ +use crate::ctypes; use core::ffi::c_int; +use ruxtask::{task::PROCESS_MAP, yield_now}; + /// Relinquish the CPU, and switches to another task. /// /// For single-threaded configuration (`multitask` feature is disabled), we just @@ -49,6 +52,68 @@ pub fn sys_getppid() -> c_int { syscall_body!(sys_getppid, Ok(1)) } +/// Wait for a child process to exit and return its status. +/// +/// TOSO, wstatus, options, and rusage are not implemented yet. +pub fn sys_wait4( + pid: c_int, + wstatus: *mut c_int, + options: c_int, + rusage: *mut ctypes::rusage, +) -> c_int { + const WNOHANG: c_int = 0x00000001; + + error!( + "sys_wait4 <= pid: {}, wstatus: {:?}, options: {}, rusage: {:?}", + pid, wstatus, options, rusage + ); + + if pid > 0 { + loop { + let mut process_map = PROCESS_MAP.lock(); + if let Some(task) = process_map.get(&(pid as u64)) { + if task.state() == ruxtask::task::TaskState::Exited { + process_map.remove(&(pid as u64)); + return pid; + } else if options & WNOHANG != 0 { + return 0; // No child process + } + // for single-cpu system, we must yield to other tasks instead of dead-looping here. + yield_now(); + } else { + return -1; // No such process + } + } + } else if pid == -1 { + let mut to_remove: Option = None; + while to_remove.is_none() { + let process_map = PROCESS_MAP.lock(); + for (child_pid, task) in process_map + .iter() + .filter(|(_, task)| task.parent_process().is_some()) + { + let parent_pid = task.parent_process().unwrap().id().as_u64(); + if parent_pid == ruxtask::current().id().as_u64() { + if task.state() == ruxtask::task::TaskState::Exited { + // add to to_remove list + let _ = to_remove.insert(*child_pid); + break; + } + } + } + if options & WNOHANG != 0 { + return 0; // No child process + } + // for single-cpu system, we must yield to other tasks instead of dead-looping here. + yield_now(); + } + PROCESS_MAP.lock().remove(&to_remove.unwrap()); + return to_remove.unwrap() as c_int; + } else { + return -1; // Invalid argument + } +} + /// Exit current task pub fn sys_exit(exit_code: c_int) -> ! { debug!("sys_exit <= {}", exit_code); diff --git a/api/ruxos_posix_api/src/imp/time.rs b/api/ruxos_posix_api/src/imp/time.rs index 2fa61c80e..2e8242edf 100644 --- a/api/ruxos_posix_api/src/imp/time.rs +++ b/api/ruxos_posix_api/src/imp/time.rs @@ -110,6 +110,22 @@ pub unsafe fn sys_nanosleep(req: *const ctypes::timespec, rem: *mut ctypes::time }) } +/// Sleep some nanoseconds +/// +/// TODO: clockid is not used, should be woken by signals, and set errno +/// +pub unsafe fn sys_clock_nanosleep( + _clockid: *const ctypes::clockid_t, + _flags: c_int, + _t: *const ctypes::timespec, + _remain: *mut ctypes::timespec, +) -> c_int { + syscall_body!(sys_nanosleep, { + warn!("sys_clock_nanosleep is not implemented yet, but returns 0 for compatibility"); + Ok(0) + }) +} + /// Get time of the day, ignore second parameter pub unsafe fn sys_gettimeofday(ts: *mut ctypes::timespec, flags: c_int) -> c_int { debug!("sys_gettimeofday <= flags: {}", flags); diff --git a/api/ruxos_posix_api/src/lib.rs b/api/ruxos_posix_api/src/lib.rs index 263134c42..51c44653f 100644 --- a/api/ruxos_posix_api/src/lib.rs +++ b/api/ruxos_posix_api/src/lib.rs @@ -57,9 +57,10 @@ pub use imp::stat::{ }; pub use imp::sys::{sys_sysinfo, sys_uname}; pub use imp::sys_invalid; -pub use imp::task::{sys_exit, sys_getpid, sys_getppid, sys_gettid, sys_sched_yield}; +pub use imp::task::{sys_exit, sys_getpid, sys_getppid, sys_gettid, sys_sched_yield, sys_wait4}; pub use imp::time::{ - sys_clock_gettime, sys_clock_settime, sys_gettimeofday, sys_nanosleep, sys_times, + sys_clock_gettime, sys_clock_nanosleep, sys_clock_settime, sys_gettimeofday, sys_nanosleep, + sys_times, }; #[cfg(all(feature = "fd", feature = "musl"))] diff --git a/modules/ruxhal/src/arch/aarch64/context.rs b/modules/ruxhal/src/arch/aarch64/context.rs index 4b218b330..2afefb14f 100644 --- a/modules/ruxhal/src/arch/aarch64/context.rs +++ b/modules/ruxhal/src/arch/aarch64/context.rs @@ -13,8 +13,6 @@ use core::{ }; use memory_addr::{PhysAddr, VirtAddr}; -use super::write_page_table_root; - /// Saved registers when a trap (exception) occurs. #[repr(C)] #[derive(Default, Clone, Copy)] @@ -64,13 +62,6 @@ pub struct FpState { pub fpsr: u32, } -#[cfg(feature = "fp_simd")] -impl FpState { - fn switch_to(&mut self, next_fpstate: &FpState) { - unsafe { fpstate_switch(self, next_fpstate) } - } -} - /// Saved hardware states of a task. /// /// The context usually includes: @@ -143,7 +134,6 @@ impl TaskContext { #[cfg(feature = "fp_simd")] fpstate_switch(&mut self.fp_state, &next_ctx.fp_state); // switch to the next process's page table, stack would be unavailable before context switch finished - // write_page_table_root(page_table_addr); context_switch(self, next_ctx, page_table_addr.as_usize() as u64); } } @@ -178,9 +168,7 @@ unsafe extern "C" fn save_stack(src: *const u8, dst: *mut u8, size: usize) { #[naked] #[allow(named_asm_labels)] -unsafe extern "C" fn save_current_context( - _current_task: &mut TaskContext -) { +unsafe extern "C" fn save_current_context(_current_task: &mut TaskContext) { asm!( " stp x29, x30, [x0, 12 * 8] @@ -233,7 +221,11 @@ unsafe extern "C" fn save_fpstate_context(_current_fpstate: &mut FpState) { #[naked] #[allow(named_asm_labels)] -unsafe extern "C" fn context_switch(_current_task: &mut TaskContext, _next_task: &TaskContext, _page_table_addr: u64) { +unsafe extern "C" fn context_switch( + _current_task: &mut TaskContext, + _next_task: &TaskContext, + _page_table_addr: u64, +) { asm!( " // save old context (callee-saved registers) diff --git a/modules/ruxruntime/src/lib.rs b/modules/ruxruntime/src/lib.rs index b324b83a4..bcf11151a 100644 --- a/modules/ruxruntime/src/lib.rs +++ b/modules/ruxruntime/src/lib.rs @@ -177,7 +177,7 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! { info!("Found physcial memory regions:"); for r in ruxhal::mem::memory_regions() { - error!( + info!( " [{:x?}, {:x?}) {} ({:?})", r.paddr, r.paddr + r.size, @@ -422,7 +422,7 @@ fn init_interrupt() { fn do_signal() { let now_ns = ruxhal::time::current_time_nanos(); // timer signal num - let timers = [14, 26, 27]; + let timers = [14, 26, 27]; // what is the number? for (which, timer) in timers.iter().enumerate() { let mut ddl = Signal::timer_deadline(which, None).unwrap(); let interval = Signal::timer_interval(which, None).unwrap(); diff --git a/modules/ruxtask/src/fs.rs b/modules/ruxtask/src/fs.rs index 8853b18ac..75d71a332 100644 --- a/modules/ruxtask/src/fs.rs +++ b/modules/ruxtask/src/fs.rs @@ -33,7 +33,11 @@ pub fn get_file_like(fd: i32) -> LinuxResult> { let binding_task = current(); let mut binding_fs = binding_task.fs.lock(); let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; - fd_table.get(fd as usize).cloned().ok_or(LinuxError::EBADF).clone() + fd_table + .get(fd as usize) + .cloned() + .ok_or(LinuxError::EBADF) + .clone() } pub fn add_file_like(f: Arc) -> LinuxResult { diff --git a/modules/ruxtask/src/run_queue.rs b/modules/ruxtask/src/run_queue.rs index 30421569b..82fa5ffd1 100644 --- a/modules/ruxtask/src/run_queue.rs +++ b/modules/ruxtask/src/run_queue.rs @@ -10,7 +10,7 @@ use log::error; use crate::fs::get_file_like; -use crate::{ fs::RUX_FILE_LIMIT}; +use crate::fs::RUX_FILE_LIMIT; use alloc::collections::VecDeque; use alloc::sync::Arc; use axerrno::LinuxResult; @@ -216,7 +216,6 @@ impl AxRunQueue { (*prev_ctx_ptr).switch_to(&*next_ctx_ptr, root_paddr); } } - } fn gc_flush_file(fd: usize) -> LinuxResult { diff --git a/modules/ruxtask/src/task.rs b/modules/ruxtask/src/task.rs index 7576a6ce8..4099dbe61 100644 --- a/modules/ruxtask/src/task.rs +++ b/modules/ruxtask/src/task.rs @@ -33,11 +33,11 @@ use ruxhal::tls::TlsArea; use memory_addr::{align_up_4k, VirtAddr, PAGE_SIZE_4K}; use ruxhal::arch::{flush_tlb, TaskContext}; +use crate::current; #[cfg(not(feature = "musl"))] use crate::tsd::{DestrFunction, KEYS, TSD}; #[cfg(feature = "paging")] use crate::vma::MmapStruct; -use crate::current; use crate::{AxRunQueue, AxTask, AxTaskRef, WaitQueue}; /// A unique identifier for a thread. @@ -201,7 +201,7 @@ impl TaskInner { } } -static PROCESS_MAP: SpinNoIrq>> = SpinNoIrq::new(BTreeMap::new()); +pub static PROCESS_MAP: SpinNoIrq>> = SpinNoIrq::new(BTreeMap::new()); // private methods impl TaskInner { @@ -377,7 +377,7 @@ impl TaskInner { // TODO: clone parent page table, and mark all unshared pages to read-only let mut cloned_page_table = PageTable::try_new().expect("failed to create page table"); let cloned_mm = current().mm.as_ref().clone(); - + // clone the global shared pages (as system memory) // TODO: exclude the stack page from the cloned page table #[cfg(feature = "paging")] @@ -451,27 +451,29 @@ impl TaskInner { page_table .update(vaddr, None, Some(MappingFlags::READ)) .expect("failed to update mapping when forking"); - } flush_tlb(Some(vaddr)); } + let new_pid = TaskId::new(); let mut t = Self { parent_process: Some(Arc::downgrade(current_task.as_task_ref())), process_task: Weak::new(), - id: TaskId::new(), + id: new_pid, name, is_idle: false, is_init: false, entry: None, state: AtomicU8::new(TaskState::Ready as u8), - in_wait_queue: AtomicBool::new(false), + in_wait_queue: AtomicBool::new(current_task.in_wait_queue.load(Ordering::Relaxed)), #[cfg(feature = "irq")] - in_timer_list: AtomicBool::new(false), + in_timer_list: AtomicBool::new(current_task.in_timer_list.load(Ordering::Relaxed)), #[cfg(feature = "preempt")] - need_resched: AtomicBool::new(false), + need_resched: AtomicBool::new(current_task.need_resched.load(Ordering::Relaxed)), #[cfg(feature = "preempt")] - preempt_disable_count: AtomicUsize::new(0), + preempt_disable_count: AtomicUsize::new( + current_task.preempt_disable_count.load(Ordering::Relaxed), + ), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), kstack: SpinNoIrq::new(Arc::new(Some(new_stack))), @@ -504,6 +506,9 @@ impl TaskInner { tls, ); let task_ref = Arc::new(AxTask::new(t)); + PROCESS_MAP + .lock() + .insert(new_pid.as_u64(), task_ref.clone()); unsafe { // copy the stack content from current stack to new stack @@ -693,7 +698,6 @@ impl TaskInner { self.preempt_disable_count.fetch_add(1, Ordering::Relaxed); } - #[cfg(feature = "preempt")] pub(crate) fn enable_preempt(&self, resched: bool) { if self.preempt_disable_count.fetch_sub(1, Ordering::Relaxed) == 1 && resched { diff --git a/ulib/ruxmusl/src/aarch64/mod.rs b/ulib/ruxmusl/src/aarch64/mod.rs index a1dbe05f4..c7a15f889 100644 --- a/ulib/ruxmusl/src/aarch64/mod.rs +++ b/ulib/ruxmusl/src/aarch64/mod.rs @@ -224,6 +224,12 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize { args[0] as ctypes::clockid_t, args[1] as *mut ctypes::timespec, ) as _, + SyscallId::CLOCK_NANOSLEEP => ruxos_posix_api::sys_clock_nanosleep( + args[0] as *const ctypes::clockid_t, + args[1] as c_int, + args[2] as *const ctypes::timespec, + args[3] as *mut ctypes::timespec, + ) as _, SyscallId::SCHED_YIELD => ruxos_posix_api::sys_sched_yield() as _, #[cfg(feature = "signal")] SyscallId::KILL => ruxos_posix_api::sys_kill(args[0] as pid_t, args[1] as c_int) as _, @@ -398,6 +404,12 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize { args[1] as ctypes::size_t, args[2] as c_int, ) as _, + SyscallId::WAIT4 => ruxos_posix_api::sys_wait4( + args[0] as ctypes::pid_t, + args[1] as *mut c_int, + args[2] as c_int, + args[3] as *mut ctypes::rusage, + ) as _, SyscallId::PRLIMIT64 => ruxos_posix_api::sys_prlimit64( args[0] as ctypes::pid_t, args[1] as c_int, diff --git a/ulib/ruxmusl/src/aarch64/syscall_id.rs b/ulib/ruxmusl/src/aarch64/syscall_id.rs index c4b8e4307..c79257b66 100644 --- a/ulib/ruxmusl/src/aarch64/syscall_id.rs +++ b/ulib/ruxmusl/src/aarch64/syscall_id.rs @@ -80,6 +80,7 @@ pub enum SyscallId { NANO_SLEEP = 101, CLOCK_SETTIME = 112, CLOCK_GETTIME = 113, + CLOCK_NANOSLEEP = 115, SCHED_YIELD = 124, #[cfg(feature = "signal")] KILL = 129, @@ -147,6 +148,7 @@ pub enum SyscallId { MSYNC = 227, #[cfg(feature = "alloc")] MADVISE = 233, + WAIT4 = 260, PRLIMIT64 = 261, GETRANDOM = 278, } diff --git a/ulib/ruxmusl/src/trap.rs b/ulib/ruxmusl/src/trap.rs index 0c50e463f..01f3ab961 100644 --- a/ulib/ruxmusl/src/trap.rs +++ b/ulib/ruxmusl/src/trap.rs @@ -22,6 +22,9 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl { #[cfg(feature = "musl")] fn handle_syscall(syscall_id: usize, args: [usize; 6]) -> isize { let id = SyscallId::try_from(syscall_id).unwrap_or(SyscallId::INVALID); + if id == SyscallId::INVALID { + error!("Invalid syscall id {}", syscall_id); + } crate::syscall(id, args) } } From 2431ea727f740def1aca95c7d6855fbb56e95055 Mon Sep 17 00:00:00 2001 From: WuZheng Date: Fri, 27 Sep 2024 16:43:02 +0800 Subject: [PATCH 05/15] drop lock of `process_map` before yielding to other tasks --- api/ruxos_posix_api/src/imp/task.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/api/ruxos_posix_api/src/imp/task.rs b/api/ruxos_posix_api/src/imp/task.rs index 19db94bc6..eb93268f1 100644 --- a/api/ruxos_posix_api/src/imp/task.rs +++ b/api/ruxos_posix_api/src/imp/task.rs @@ -78,11 +78,13 @@ pub fn sys_wait4( } else if options & WNOHANG != 0 { return 0; // No child process } - // for single-cpu system, we must yield to other tasks instead of dead-looping here. - yield_now(); } else { return -1; // No such process } + // drop lock before yielding to other tasks + drop(process_map); + // for single-cpu system, we must yield to other tasks instead of dead-looping here. + yield_now(); } } else if pid == -1 { let mut to_remove: Option = None; @@ -104,6 +106,8 @@ pub fn sys_wait4( if options & WNOHANG != 0 { return 0; // No child process } + // drop lock before yielding to other tasks + drop(process_map); // for single-cpu system, we must yield to other tasks instead of dead-looping here. yield_now(); } From 7380ca72cf065e1950170b18f31fbc69f1458b9d Mon Sep 17 00:00:00 2001 From: WuZheng Date: Mon, 11 Nov 2024 16:50:19 +0800 Subject: [PATCH 06/15] fix wrong aligned_size in .percpu and deadlock problem in fork_task(). --- api/ruxos_posix_api/src/imp/pthread/mod.rs | 12 ++++++++ api/ruxos_posix_api/src/imp/task.rs | 19 +++++++++++- api/ruxos_posix_api/src/lib.rs | 2 +- crates/percpu/src/imp.rs | 5 ++-- modules/ruxtask/src/api.rs | 12 ++++++-- modules/ruxtask/src/task.rs | 35 +++++++++++++++------- ulib/ruxmusl/src/aarch64/mod.rs | 3 ++ ulib/ruxmusl/src/aarch64/syscall_id.rs | 1 + 8 files changed, 72 insertions(+), 17 deletions(-) diff --git a/api/ruxos_posix_api/src/imp/pthread/mod.rs b/api/ruxos_posix_api/src/imp/pthread/mod.rs index a9e7d1374..759512cf9 100644 --- a/api/ruxos_posix_api/src/imp/pthread/mod.rs +++ b/api/ruxos_posix_api/src/imp/pthread/mod.rs @@ -226,6 +226,18 @@ pub fn sys_pthread_exit(retval: *mut c_void) -> ! { Pthread::exit_current(retval); } +/// Exits the current thread. The value `retval` will be returned to the joiner. +pub fn sys_exit_group(status: c_int) -> ! { + error!("sys_exit_group <= {:#?}", status); + + // TODO: exit all threads, send signal to all threads + + #[cfg(feature = "multitask")] + ruxtask::exit(status); + #[cfg(not(feature = "multitask"))] + ruxhal::misc::terminate(); +} + /// Waits for the given thread to exit, and stores the return value in `retval`. pub unsafe fn sys_pthread_join(thread: ctypes::pthread_t, retval: *mut *mut c_void) -> c_int { debug!("sys_pthread_join <= {:#x}", retval as usize); diff --git a/api/ruxos_posix_api/src/imp/task.rs b/api/ruxos_posix_api/src/imp/task.rs index eb93268f1..b4227b8a1 100644 --- a/api/ruxos_posix_api/src/imp/task.rs +++ b/api/ruxos_posix_api/src/imp/task.rs @@ -63,7 +63,7 @@ pub fn sys_wait4( ) -> c_int { const WNOHANG: c_int = 0x00000001; - error!( + debug!( "sys_wait4 <= pid: {}, wstatus: {:?}, options: {}, rusage: {:?}", pid, wstatus, options, rusage ); @@ -73,6 +73,12 @@ pub fn sys_wait4( let mut process_map = PROCESS_MAP.lock(); if let Some(task) = process_map.get(&(pid as u64)) { if task.state() == ruxtask::task::TaskState::Exited { + unsafe { + // lower 8 bits of exit_code is the signal number, while upper 8 bits of exit_code is the exit status + // according to "bits/waitstatus.h" in glibc source code. + // TODO: add signal number to wstatus + wstatus.write(task.exit_code()<<8); + } process_map.remove(&(pid as u64)); return pid; } else if options & WNOHANG != 0 { @@ -98,6 +104,13 @@ pub fn sys_wait4( if parent_pid == ruxtask::current().id().as_u64() { if task.state() == ruxtask::task::TaskState::Exited { // add to to_remove list + let task_ref = process_map.get(child_pid).unwrap(); + unsafe { + // lower 8 bits of exit_code is the signal number, while upper 8 bits of exit_code is the exit status + // according to "bits/waitstatus.h" in glibc source code. + // TODO: add signal number to wstatus + wstatus.write(task.exit_code()<<8); + } let _ = to_remove.insert(*child_pid); break; } @@ -108,6 +121,10 @@ pub fn sys_wait4( } // drop lock before yielding to other tasks drop(process_map); + // check if the condition is meet + if !to_remove.is_none() { + break; + } // for single-cpu system, we must yield to other tasks instead of dead-looping here. yield_now(); } diff --git a/api/ruxos_posix_api/src/lib.rs b/api/ruxos_posix_api/src/lib.rs index 51c44653f..699eae360 100644 --- a/api/ruxos_posix_api/src/lib.rs +++ b/api/ruxos_posix_api/src/lib.rs @@ -117,7 +117,7 @@ pub use imp::pthread::sys_clone; #[cfg(all(feature = "multitask", feature = "musl"))] pub use imp::pthread::sys_set_tid_address; #[cfg(feature = "multitask")] -pub use imp::pthread::{sys_pthread_create, sys_pthread_exit, sys_pthread_join, sys_pthread_self}; +pub use imp::pthread::{sys_pthread_create, sys_pthread_exit, sys_exit_group, sys_pthread_join, sys_pthread_self}; #[cfg(feature = "fs")] pub use imp::execve::sys_execve; diff --git a/crates/percpu/src/imp.rs b/crates/percpu/src/imp.rs index aa7aeed99..f63c0b3dc 100644 --- a/crates/percpu/src/imp.rs +++ b/crates/percpu/src/imp.rs @@ -8,8 +8,9 @@ */ const fn align_up(val: usize) -> usize { - const PAGE_SIZE: usize = 0x1000; - (val + PAGE_SIZE - 1) & !(PAGE_SIZE - 1) + // should be the same as align_size in percpu. + const ALIGN_SIZE: usize = 64; + (val + ALIGN_SIZE - 1) & !(ALIGN_SIZE - 1) } #[cfg(not(target_os = "none"))] diff --git a/modules/ruxtask/src/api.rs b/modules/ruxtask/src/api.rs index edcd42f99..2ae4d1d4b 100644 --- a/modules/ruxtask/src/api.rs +++ b/modules/ruxtask/src/api.rs @@ -132,19 +132,25 @@ where } pub fn fork_task() -> Option { - let current_process = current(); - let current_id = current_process.id().as_u64(); + let current_id = current().id().as_u64(); let children_process = TaskInner::fork(); // Judge whether the parent process is blocked, if yes, add it to the blocking queue of the child process if current().id().as_u64() == current_id { RUN_QUEUE.lock().add_task(children_process.clone()); + return Some(children_process); } - + + unsafe {RUN_QUEUE.force_unlock(); } + // should not drop the children_process here, because it will be taken in the parent process + // and dropped in the parent process let _ = ManuallyDrop::new(children_process); + #[cfg(feature = "irq")] + ruxhal::arch::enable_irqs(); + return None; } diff --git a/modules/ruxtask/src/task.rs b/modules/ruxtask/src/task.rs index 4099dbe61..d84b72c89 100644 --- a/modules/ruxtask/src/task.rs +++ b/modules/ruxtask/src/task.rs @@ -163,6 +163,11 @@ impl TaskInner { None } + /// Get pointer for process task + pub fn exit_code(&self) -> i32 { + self.exit_code.load(Ordering::Acquire) + } + /// Get process task pub fn process_task(&self) -> Arc { if let Some(process_task) = self.process_task.upgrade() { @@ -581,16 +586,19 @@ impl TaskInner { } pub fn new_idle(name: String) -> AxTaskRef { + const IDLE_STACK_SIZE: usize = 4096; let bindings = PROCESS_MAP.lock(); let (&_parent_id, &ref task_ref) = bindings.first_key_value().unwrap(); - let t = Self { + let idle_kstack = TaskStack::alloc(align_up_4k(IDLE_STACK_SIZE)); + + let mut t = Self { parent_process: Some(Arc::downgrade(task_ref)), process_task: task_ref.process_task.clone(), id: TaskId::new(), name, is_idle: true, is_init: false, - entry: None, + entry: Some(Box::into_raw(Box::new(|| crate::run_idle()))), state: AtomicU8::new(TaskState::Ready as u8), in_wait_queue: AtomicBool::new(false), #[cfg(feature = "irq")] @@ -617,7 +625,21 @@ impl TaskInner { mm: task_ref.mm.clone(), }; - Arc::new(AxTask::new(t)) + #[cfg(feature = "tls")] + let tls = VirtAddr::from(t.tls.tls_ptr() as usize); + #[cfg(not(feature = "tls"))] + let tls = VirtAddr::from(0); + + debug!("new idle task: {}", t.id_name()); + t.ctx.get_mut().init( + task_entry as usize, + idle_kstack.top(), + tls, + ); + + let task_ref = Arc::new(AxTask::new(t)); + + task_ref } /// Get task state @@ -771,12 +793,6 @@ impl fmt::Debug for TaskInner { } } -impl Drop for TaskInner { - fn drop(&mut self) { - error!("task drop: {}", self.id_name()); - } -} - #[derive(Debug)] pub struct TaskStack { ptr: NonNull, @@ -786,7 +802,6 @@ pub struct TaskStack { impl TaskStack { pub fn alloc(size: usize) -> Self { let layout = Layout::from_size_align(size, 8).unwrap(); - debug!("taskStack::layout = {:?}", layout); Self { ptr: NonNull::new(unsafe { alloc::alloc::alloc(layout) }).unwrap(), layout, diff --git a/ulib/ruxmusl/src/aarch64/mod.rs b/ulib/ruxmusl/src/aarch64/mod.rs index c7a15f889..fb8f6f17b 100644 --- a/ulib/ruxmusl/src/aarch64/mod.rs +++ b/ulib/ruxmusl/src/aarch64/mod.rs @@ -201,6 +201,9 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize { SyscallId::EXIT => { ruxos_posix_api::sys_pthread_exit(args[0] as *mut core::ffi::c_void) as _ } + SyscallId::EXIT_GROUP => { + ruxos_posix_api::sys_exit_group(args[0] as c_int) + } #[cfg(feature = "multitask")] SyscallId::SET_TID_ADDRESS => ruxos_posix_api::sys_set_tid_address(args[0]) as _, #[cfg(feature = "multitask")] diff --git a/ulib/ruxmusl/src/aarch64/syscall_id.rs b/ulib/ruxmusl/src/aarch64/syscall_id.rs index c79257b66..c7faeb89d 100644 --- a/ulib/ruxmusl/src/aarch64/syscall_id.rs +++ b/ulib/ruxmusl/src/aarch64/syscall_id.rs @@ -73,6 +73,7 @@ pub enum SyscallId { FDATASYNC = 83, CAP_GET = 90, EXIT = 93, + EXIT_GROUP = 94, #[cfg(feature = "multitask")] SET_TID_ADDRESS = 96, #[cfg(feature = "multitask")] From ee23e4def16a5643a0dff83358eaf189801dac53 Mon Sep 17 00:00:00 2001 From: WuZheng Date: Thu, 14 Nov 2024 17:19:21 +0800 Subject: [PATCH 07/15] fix bugs for pipe's write end closure and exit group --- api/ruxos_posix_api/src/imp/pipe.rs | 12 +++++++++--- api/ruxos_posix_api/src/imp/pthread/mod.rs | 9 ++++++--- api/ruxos_posix_api/src/imp/task.rs | 3 +-- modules/ruxtask/src/fs.rs | 10 ++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/api/ruxos_posix_api/src/imp/pipe.rs b/api/ruxos_posix_api/src/imp/pipe.rs index 8b61a3f1a..6fb29a04c 100644 --- a/api/ruxos_posix_api/src/imp/pipe.rs +++ b/api/ruxos_posix_api/src/imp/pipe.rs @@ -7,7 +7,7 @@ * See the Mulan PSL v2 for more details. */ -use alloc::sync::Arc; +use alloc::sync::{Weak, Arc}; use core::ffi::c_int; use axerrno::{LinuxError, LinuxResult}; @@ -87,6 +87,8 @@ impl PipeRingBuffer { pub struct Pipe { readable: bool, buffer: Arc>, + // to find the write end when the read end is closed + _write_end_closed: Option>>, } impl Pipe { @@ -95,10 +97,12 @@ impl Pipe { let read_end = Pipe { readable: true, buffer: buffer.clone(), + _write_end_closed: None, }; let write_end = Pipe { readable: false, - buffer, + buffer: buffer.clone(), + _write_end_closed: Some(Arc::downgrade(&buffer)), }; (read_end, write_end) } @@ -112,7 +116,9 @@ impl Pipe { } pub fn write_end_close(&self) -> bool { - Arc::strong_count(&self.buffer) == 1 + let write_end_count = Arc::weak_count(&self.buffer); + // error!("Pipe::write_end_close <= buffer: {:#?} {:#?}", write_end_count, Arc::as_ptr(&self.buffer)); + write_end_count == 0 } } diff --git a/api/ruxos_posix_api/src/imp/pthread/mod.rs b/api/ruxos_posix_api/src/imp/pthread/mod.rs index 759512cf9..405d5b13a 100644 --- a/api/ruxos_posix_api/src/imp/pthread/mod.rs +++ b/api/ruxos_posix_api/src/imp/pthread/mod.rs @@ -12,7 +12,7 @@ use core::cell::UnsafeCell; use core::ffi::{c_int, c_void}; use axerrno::{LinuxError, LinuxResult}; -use ruxtask::AxTaskRef; +use ruxtask::{current, AxTaskRef}; use spin::RwLock; use crate::ctypes; @@ -228,9 +228,12 @@ pub fn sys_pthread_exit(retval: *mut c_void) -> ! { /// Exits the current thread. The value `retval` will be returned to the joiner. pub fn sys_exit_group(status: c_int) -> ! { - error!("sys_exit_group <= {:#?}", status); + debug!("sys_exit_group <= status: {:#?}", status); // TODO: exit all threads, send signal to all threads + + // drop all file opened by current task + current().fs.lock().as_mut().unwrap().close_all_files(); #[cfg(feature = "multitask")] ruxtask::exit(status); @@ -322,7 +325,7 @@ pub unsafe fn sys_clone( TID_TO_PTHREAD.write().insert(tid, ForceSendSync(ptr)); 0 }; - warn!("will sys_clone <= pid: {}", pid); + debug!("will sys_clone <= pid: {}", pid); return Ok(pid); } else { debug!("ONLY support CLONE_THREAD and SIGCHLD"); diff --git a/api/ruxos_posix_api/src/imp/task.rs b/api/ruxos_posix_api/src/imp/task.rs index b4227b8a1..17d365c83 100644 --- a/api/ruxos_posix_api/src/imp/task.rs +++ b/api/ruxos_posix_api/src/imp/task.rs @@ -54,7 +54,7 @@ pub fn sys_getppid() -> c_int { /// Wait for a child process to exit and return its status. /// -/// TOSO, wstatus, options, and rusage are not implemented yet. +/// TODO: part of options, and rusage are not implemented yet. pub fn sys_wait4( pid: c_int, wstatus: *mut c_int, @@ -104,7 +104,6 @@ pub fn sys_wait4( if parent_pid == ruxtask::current().id().as_u64() { if task.state() == ruxtask::task::TaskState::Exited { // add to to_remove list - let task_ref = process_map.get(child_pid).unwrap(); unsafe { // lower 8 bits of exit_code is the signal number, while upper 8 bits of exit_code is the exit status // according to "bits/waitstatus.h" in glibc source code. diff --git a/modules/ruxtask/src/fs.rs b/modules/ruxtask/src/fs.rs index 75d71a332..b603d237d 100644 --- a/modules/ruxtask/src/fs.rs +++ b/modules/ruxtask/src/fs.rs @@ -216,6 +216,16 @@ pub struct FileSystem { pub root_dir: Arc, } +impl FileSystem { + pub fn close_all_files(&mut self) { + for fd in 0..self.fd_table.capacity() { + if let Some(_) = self.fd_table.get(fd) { + self.fd_table.remove(fd).unwrap(); + } + } + } +} + impl Clone for FileSystem { fn clone(&self) -> Self { let mut new_fd_table = FlattenObjects::new(); From 2dba7dc609b910fbe1885421bf3d6d5617dc8d0a Mon Sep 17 00:00:00 2001 From: lhw Date: Tue, 26 Nov 2024 13:27:04 +0800 Subject: [PATCH 08/15] fix sys_rt_sigaction --- api/ruxos_posix_api/src/imp/pipe.rs | 2 +- api/ruxos_posix_api/src/imp/pthread/mod.rs | 2 +- api/ruxos_posix_api/src/imp/rt_sig.rs | 18 ++++++++++++------ api/ruxos_posix_api/src/imp/task.rs | 4 ++-- api/ruxos_posix_api/src/lib.rs | 4 +++- modules/ruxhal/src/arch/x86_64/mod.rs | 2 +- modules/ruxtask/src/api.rs | 10 ++++++---- modules/ruxtask/src/task.rs | 12 +++++------- ulib/ruxmusl/src/aarch64/mod.rs | 4 +--- 9 files changed, 32 insertions(+), 26 deletions(-) diff --git a/api/ruxos_posix_api/src/imp/pipe.rs b/api/ruxos_posix_api/src/imp/pipe.rs index 6fb29a04c..98110dbf7 100644 --- a/api/ruxos_posix_api/src/imp/pipe.rs +++ b/api/ruxos_posix_api/src/imp/pipe.rs @@ -7,7 +7,7 @@ * See the Mulan PSL v2 for more details. */ -use alloc::sync::{Weak, Arc}; +use alloc::sync::{Arc, Weak}; use core::ffi::c_int; use axerrno::{LinuxError, LinuxResult}; diff --git a/api/ruxos_posix_api/src/imp/pthread/mod.rs b/api/ruxos_posix_api/src/imp/pthread/mod.rs index 405d5b13a..da2694ff8 100644 --- a/api/ruxos_posix_api/src/imp/pthread/mod.rs +++ b/api/ruxos_posix_api/src/imp/pthread/mod.rs @@ -231,7 +231,7 @@ pub fn sys_exit_group(status: c_int) -> ! { debug!("sys_exit_group <= status: {:#?}", status); // TODO: exit all threads, send signal to all threads - + // drop all file opened by current task current().fs.lock().as_mut().unwrap().close_all_files(); diff --git a/api/ruxos_posix_api/src/imp/rt_sig.rs b/api/ruxos_posix_api/src/imp/rt_sig.rs index 9387a3a7b..8d89cf6aa 100644 --- a/api/ruxos_posix_api/src/imp/rt_sig.rs +++ b/api/ruxos_posix_api/src/imp/rt_sig.rs @@ -87,6 +87,8 @@ pub fn sys_rt_sigprocmask( } /// sigaction syscall for A64 musl +/// +/// TODO: if sa is 0, return now action pub unsafe fn sys_rt_sigaction( sig: c_int, sa: *const ctypes::sigaction, @@ -95,12 +97,16 @@ pub unsafe fn sys_rt_sigaction( ) -> c_int { debug!("sys_rt_sigaction <= sig: {}", sig); syscall_body!(sys_rt_sigaction, { - let sa = unsafe { *sa }; - let old = unsafe { *old }; - let sa = k_sigaction::from(sa); - let mut old_sa = k_sigaction::from(old); - sys_sigaction(sig as _, Some(&sa), Some(&mut old_sa)); - Ok(0) + if sa as u64 == 0 || old as u64 == 0 { + Err(LinuxError::EFAULT) + } else { + let sa = unsafe { *sa }; + let old = unsafe { *old }; + let sa = k_sigaction::from(sa); + let mut old_sa = k_sigaction::from(old); + sys_sigaction(sig as _, Some(&sa), Some(&mut old_sa)); + Ok(0) + } }) } diff --git a/api/ruxos_posix_api/src/imp/task.rs b/api/ruxos_posix_api/src/imp/task.rs index 17d365c83..ae07be44b 100644 --- a/api/ruxos_posix_api/src/imp/task.rs +++ b/api/ruxos_posix_api/src/imp/task.rs @@ -77,7 +77,7 @@ pub fn sys_wait4( // lower 8 bits of exit_code is the signal number, while upper 8 bits of exit_code is the exit status // according to "bits/waitstatus.h" in glibc source code. // TODO: add signal number to wstatus - wstatus.write(task.exit_code()<<8); + wstatus.write(task.exit_code() << 8); } process_map.remove(&(pid as u64)); return pid; @@ -108,7 +108,7 @@ pub fn sys_wait4( // lower 8 bits of exit_code is the signal number, while upper 8 bits of exit_code is the exit status // according to "bits/waitstatus.h" in glibc source code. // TODO: add signal number to wstatus - wstatus.write(task.exit_code()<<8); + wstatus.write(task.exit_code() << 8); } let _ = to_remove.insert(*child_pid); break; diff --git a/api/ruxos_posix_api/src/lib.rs b/api/ruxos_posix_api/src/lib.rs index c13cbaded..f7d50620e 100644 --- a/api/ruxos_posix_api/src/lib.rs +++ b/api/ruxos_posix_api/src/lib.rs @@ -120,7 +120,9 @@ pub use imp::pthread::sys_clone; #[cfg(all(feature = "multitask", feature = "musl"))] pub use imp::pthread::sys_set_tid_address; #[cfg(feature = "multitask")] -pub use imp::pthread::{sys_pthread_create, sys_pthread_exit, sys_exit_group, sys_pthread_join, sys_pthread_self}; +pub use imp::pthread::{ + sys_exit_group, sys_pthread_create, sys_pthread_exit, sys_pthread_join, sys_pthread_self, +}; #[cfg(feature = "fs")] pub use imp::execve::sys_execve; diff --git a/modules/ruxhal/src/arch/x86_64/mod.rs b/modules/ruxhal/src/arch/x86_64/mod.rs index ea97eee5c..0abb0fef4 100644 --- a/modules/ruxhal/src/arch/x86_64/mod.rs +++ b/modules/ruxhal/src/arch/x86_64/mod.rs @@ -235,7 +235,7 @@ pub unsafe fn init_syscall_entry() { .has_syscall_sysret()); x86_64::registers::model_specific::LStar::write(x86_64::VirtAddr::new( - x86_syscall_entry as usize, + x86_syscall_entry as u64, )); x86_64::registers::model_specific::Efer::update(|efer| { efer.insert( diff --git a/modules/ruxtask/src/api.rs b/modules/ruxtask/src/api.rs index 2ae4d1d4b..818ca3e68 100644 --- a/modules/ruxtask/src/api.rs +++ b/modules/ruxtask/src/api.rs @@ -138,12 +138,14 @@ pub fn fork_task() -> Option { // Judge whether the parent process is blocked, if yes, add it to the blocking queue of the child process if current().id().as_u64() == current_id { RUN_QUEUE.lock().add_task(children_process.clone()); - + return Some(children_process); } - - unsafe {RUN_QUEUE.force_unlock(); } - + + unsafe { + RUN_QUEUE.force_unlock(); + } + // should not drop the children_process here, because it will be taken in the parent process // and dropped in the parent process let _ = ManuallyDrop::new(children_process); diff --git a/modules/ruxtask/src/task.rs b/modules/ruxtask/src/task.rs index d84b72c89..cc1a45d20 100644 --- a/modules/ruxtask/src/task.rs +++ b/modules/ruxtask/src/task.rs @@ -629,14 +629,12 @@ impl TaskInner { let tls = VirtAddr::from(t.tls.tls_ptr() as usize); #[cfg(not(feature = "tls"))] let tls = VirtAddr::from(0); - + debug!("new idle task: {}", t.id_name()); - t.ctx.get_mut().init( - task_entry as usize, - idle_kstack.top(), - tls, - ); - + t.ctx + .get_mut() + .init(task_entry as usize, idle_kstack.top(), tls); + let task_ref = Arc::new(AxTask::new(t)); task_ref diff --git a/ulib/ruxmusl/src/aarch64/mod.rs b/ulib/ruxmusl/src/aarch64/mod.rs index 12f3ae009..d2e38e982 100644 --- a/ulib/ruxmusl/src/aarch64/mod.rs +++ b/ulib/ruxmusl/src/aarch64/mod.rs @@ -201,9 +201,7 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize { SyscallId::EXIT => { ruxos_posix_api::sys_pthread_exit(args[0] as *mut core::ffi::c_void) as _ } - SyscallId::EXIT_GROUP => { - ruxos_posix_api::sys_exit_group(args[0] as c_int) - } + SyscallId::EXIT_GROUP => ruxos_posix_api::sys_exit_group(args[0] as c_int), #[cfg(feature = "multitask")] SyscallId::SET_TID_ADDRESS => ruxos_posix_api::sys_set_tid_address(args[0]) as _, #[cfg(feature = "multitask")] From c0ebec144591aa4038f210bcf5df0f37dee987c4 Mon Sep 17 00:00:00 2001 From: lhw Date: Mon, 2 Dec 2024 22:26:46 +0800 Subject: [PATCH 09/15] add unixsocket without real inode --- Cargo.lock | 19 +- api/ruxos_posix_api/build.rs | 2 + api/ruxos_posix_api/ctypes.h | 1 + api/ruxos_posix_api/src/imp/net.rs | 237 ++++++++- modules/ruxfs/src/root.rs | 9 +- modules/ruxnet/Cargo.toml | 5 + modules/ruxnet/src/lib.rs | 4 + modules/ruxnet/src/lwip_impl/tcp.rs | 6 + modules/ruxnet/src/smoltcp_impl/tcp.rs | 11 +- modules/ruxnet/src/unix.rs | 702 +++++++++++++++++++++++++ 10 files changed, 966 insertions(+), 30 deletions(-) create mode 100644 modules/ruxnet/src/unix.rs diff --git a/Cargo.lock b/Cargo.lock index 997c8ce9c..599b3a91a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,6 +47,12 @@ dependencies = [ "slab_allocator", ] +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "android-tzdata" version = "0.1.1" @@ -888,9 +894,13 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.14.0" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash", + "allocator-api2", +] [[package]] name = "heapless" @@ -1758,17 +1768,22 @@ name = "ruxnet" version = "0.1.0" dependencies = [ "axerrno", + "axfs_vfs", "axio", "axlog", "axsync", "cfg-if", "cty", "driver_net", + "flatten_objects", + "hashbrown", "lazy_init", + "lazy_static", "log", "lwip_rust", "printf-compat", "ruxdriver", + "ruxfs", "ruxhal", "ruxtask", "smoltcp", diff --git a/api/ruxos_posix_api/build.rs b/api/ruxos_posix_api/build.rs index 1c72378ed..0529c64e6 100644 --- a/api/ruxos_posix_api/build.rs +++ b/api/ruxos_posix_api/build.rs @@ -111,7 +111,9 @@ typedef struct {{ let allow_vars = [ "O_.*", "AF_.*", + "SO_.*", "SOCK_.*", + "SOL_.*", "IPPROTO_.*", "FD_.*", "F_.*", diff --git a/api/ruxos_posix_api/ctypes.h b/api/ruxos_posix_api/ctypes.h index 6298ce6c3..1d43015d5 100644 --- a/api/ruxos_posix_api/ctypes.h +++ b/api/ruxos_posix_api/ctypes.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include diff --git a/api/ruxos_posix_api/src/imp/net.rs b/api/ruxos_posix_api/src/imp/net.rs index c284ad06f..7cafda5f4 100644 --- a/api/ruxos_posix_api/src/imp/net.rs +++ b/api/ruxos_posix_api/src/imp/net.rs @@ -11,19 +11,36 @@ use alloc::{sync::Arc, vec, vec::Vec}; use core::ffi::{c_char, c_int, c_void}; use core::mem::size_of; use core::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4}; +use core::sync::atomic::AtomicIsize; use axerrno::{LinuxError, LinuxResult}; use axio::PollState; use axsync::Mutex; use ruxfdtable::{FileLike, RuxStat}; -use ruxnet::{TcpSocket, UdpSocket}; +use ruxnet::{SocketAddrUnix, TcpSocket, UdpSocket, UnixSocket, UnixSocketType}; +use ruxtask::fs::RUX_FILE_LIMIT; use crate::ctypes; use crate::utils::char_ptr_to_str; +fn addrun_convert(addr: *const ctypes::sockaddr_un) -> SocketAddrUnix { + unsafe { + SocketAddrUnix { + sun_family: (*addr).sun_family, + sun_path: (*addr).sun_path, + } + } +} + +pub enum UnifiedSocketAddress { + Net(SocketAddr), + Unix(SocketAddrUnix), +} + pub enum Socket { Udp(Mutex), Tcp(Mutex), + Unix(Mutex), } impl Socket { @@ -42,6 +59,7 @@ impl Socket { match self { Socket::Udp(udpsocket) => Ok(udpsocket.lock().send(buf)?), Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().send(buf)?), + Socket::Unix(socket) => Ok(socket.lock().send(buf)?), } } @@ -49,6 +67,7 @@ impl Socket { match self { Socket::Udp(udpsocket) => Ok(udpsocket.lock().recv_from(buf).map(|e| e.0)?), Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().recv(buf, flags)?), + Socket::Unix(socket) => Ok(socket.lock().recv(buf, flags)?), } } @@ -56,6 +75,7 @@ impl Socket { match self { Socket::Udp(udpsocket) => Ok(udpsocket.lock().poll()?), Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().poll()?), + Socket::Unix(socket) => Ok(socket.lock().poll()?), } } @@ -63,27 +83,73 @@ impl Socket { match self { Socket::Udp(udpsocket) => Ok(udpsocket.lock().local_addr()?), Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().local_addr()?), + Socket::Unix(_) => Err(LinuxError::EOPNOTSUPP), } } - fn peer_addr(&self) -> LinuxResult { + fn peer_addr(&self) -> LinuxResult { match self { - Socket::Udp(udpsocket) => Ok(udpsocket.lock().peer_addr()?), - Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().peer_addr()?), + Socket::Udp(udpsocket) => Ok(UnifiedSocketAddress::Net(udpsocket.lock().peer_addr()?)), + Socket::Tcp(tcpsocket) => Ok(UnifiedSocketAddress::Net(tcpsocket.lock().peer_addr()?)), + Socket::Unix(unixsocket) => { + Ok(UnifiedSocketAddress::Unix(unixsocket.lock().peer_addr()?)) + } } } - fn bind(&self, addr: SocketAddr) -> LinuxResult { + fn bind( + &self, + socket_addr: *const ctypes::sockaddr, + addrlen: ctypes::socklen_t, + ) -> LinuxResult { match self { - Socket::Udp(udpsocket) => Ok(udpsocket.lock().bind(addr)?), - Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().bind(addr)?), + Socket::Udp(udpsocket) => { + let addr = from_sockaddr(socket_addr, addrlen)?; + Ok(udpsocket.lock().bind(addr)?) + } + Socket::Tcp(tcpsocket) => { + let addr = from_sockaddr(socket_addr, addrlen)?; + Ok(tcpsocket.lock().bind(addr)?) + } + Socket::Unix(socket) => { + if socket_addr.is_null() { + return Err(LinuxError::EFAULT); + } + if addrlen != size_of::() as _ { + return Err(LinuxError::EINVAL); + } + Ok(socket + .lock() + .bind(addrun_convert(socket_addr as *const ctypes::sockaddr_un))?) + } } } - fn connect(&self, addr: SocketAddr) -> LinuxResult { + fn connect( + &self, + socket_addr: *const ctypes::sockaddr, + addrlen: ctypes::socklen_t, + ) -> LinuxResult { match self { - Socket::Udp(udpsocket) => Ok(udpsocket.lock().connect(addr)?), - Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().connect(addr)?), + Socket::Udp(udpsocket) => { + let addr = from_sockaddr(socket_addr, addrlen)?; + Ok(udpsocket.lock().connect(addr)?) + } + Socket::Tcp(tcpsocket) => { + let addr = from_sockaddr(socket_addr, addrlen)?; + Ok(tcpsocket.lock().connect(addr)?) + } + Socket::Unix(socket) => { + if socket_addr.is_null() { + return Err(LinuxError::EFAULT); + } + if addrlen != size_of::() as _ { + return Err(LinuxError::EINVAL); + } + Ok(socket + .lock() + .connect(addrun_convert(socket_addr as *const ctypes::sockaddr_un))?) + } } } @@ -92,6 +158,7 @@ impl Socket { // diff: must bind before sendto Socket::Udp(udpsocket) => Ok(udpsocket.lock().send_to(buf, addr)?), Socket::Tcp(_) => Err(LinuxError::EISCONN), + Socket::Unix(_) => Err(LinuxError::EISCONN), } } @@ -103,6 +170,7 @@ impl Socket { .recv_from(buf) .map(|res| (res.0, Some(res.1)))?), Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().recv(buf, 0).map(|res| (res, None))?), + Socket::Unix(socket) => Ok(socket.lock().recv(buf, 0).map(|res| (res, None))?), } } @@ -110,13 +178,15 @@ impl Socket { match self { Socket::Udp(_) => Err(LinuxError::EOPNOTSUPP), Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().listen()?), + Socket::Unix(socket) => Ok(socket.lock().listen()?), } } - fn accept(&self) -> LinuxResult { + fn accept(&self) -> LinuxResult { match self { Socket::Udp(_) => Err(LinuxError::EOPNOTSUPP), - Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().accept()?), + Socket::Tcp(tcpsocket) => Ok(Socket::Tcp(Mutex::new(tcpsocket.lock().accept()?))), + Socket::Unix(unixsocket) => Ok(Socket::Unix(Mutex::new(unixsocket.lock().accept()?))), } } @@ -135,6 +205,12 @@ impl Socket { tcpsocket.shutdown()?; Ok(()) } + Socket::Unix(socket) => { + let socket = socket.lock(); + socket.peer_addr()?; + socket.shutdown()?; + Ok(()) + } } } } @@ -179,6 +255,7 @@ impl FileLike for Socket { match self { Socket::Udp(udpsocket) => udpsocket.lock().set_nonblocking(nonblock), Socket::Tcp(tcpsocket) => tcpsocket.lock().set_nonblocking(nonblock), + Socket::Unix(unixsocket) => unixsocket.lock().set_nonblocking(nonblock), } Ok(()) } @@ -199,6 +276,15 @@ impl From for ctypes::sockaddr_in { } } +impl From for ctypes::sockaddr_un { + fn from(addr: SocketAddrUnix) -> ctypes::sockaddr_un { + ctypes::sockaddr_un { + sun_family: addr.sun_family, + sun_path: addr.sun_path, + } + } +} + impl From for SocketAddrV4 { fn from(addr: ctypes::sockaddr_in) -> SocketAddrV4 { SocketAddrV4::new( @@ -208,8 +294,16 @@ impl From for SocketAddrV4 { } } +fn un_into_sockaddr(addr: SocketAddrUnix) -> (ctypes::sockaddr, ctypes::socklen_t) { + debug!("convert unixsocket address {:?} into ctypes sockaddr", addr); + ( + unsafe { *(&ctypes::sockaddr_un::from(addr) as *const _ as *const ctypes::sockaddr) }, + size_of::() as _, + ) +} + fn into_sockaddr(addr: SocketAddr) -> (ctypes::sockaddr, ctypes::socklen_t) { - debug!(" Sockaddr: {}", addr); + debug!("convert socket address {} into ctypes sockaddr", addr); match addr { SocketAddr::V4(addr) => ( unsafe { *(&ctypes::sockaddr_in::from(addr) as *const _ as *const ctypes::sockaddr) }, @@ -262,6 +356,10 @@ pub fn sys_socket(domain: c_int, socktype: c_int, protocol: c_int) -> c_int { tcp_socket.set_nonblocking(true); Socket::Tcp(Mutex::new(tcp_socket)).add_to_fd_table() } + (ctypes::AF_UNIX, ctypes::SOCK_STREAM, 0) => { + Socket::Unix(Mutex::new(UnixSocket::new(UnixSocketType::SockStream))) + .add_to_fd_table() + } _ => Err(LinuxError::EINVAL), } }) @@ -297,8 +395,7 @@ pub fn sys_bind( socket_fd, socket_addr as usize, addrlen ); syscall_body!(sys_bind, { - let addr = from_sockaddr(socket_addr, addrlen)?; - Socket::from_fd(socket_fd)?.bind(addr)?; + Socket::from_fd(socket_fd)?.bind(socket_addr, addrlen)?; Ok(0) }) } @@ -316,8 +413,7 @@ pub fn sys_connect( socket_fd, socket_addr as usize, addrlen ); syscall_body!(sys_connect, { - let addr = from_sockaddr(socket_addr, addrlen)?; - Socket::from_fd(socket_fd)?.connect(addr)?; + Socket::from_fd(socket_fd)?.connect(socket_addr, addrlen)?; Ok(0) }) } @@ -464,10 +560,16 @@ pub unsafe fn sys_accept( let socket = Socket::from_fd(socket_fd)?; let new_socket = socket.accept()?; let addr = new_socket.peer_addr()?; - let new_fd = Socket::add_to_fd_table(Socket::Tcp(Mutex::new(new_socket)))?; - unsafe { - (*socket_addr, *socket_len) = into_sockaddr(addr); + let new_fd = Socket::add_to_fd_table(new_socket)?; + match addr { + UnifiedSocketAddress::Net(addr) => unsafe { + (*socket_addr, *socket_len) = into_sockaddr(addr); + }, + UnifiedSocketAddress::Unix(addr) => unsafe { + (*socket_addr, *socket_len) = un_into_sockaddr(addr); + }, } + Ok(new_fd) }) } @@ -601,6 +703,87 @@ pub unsafe fn sys_getsockname( }) } +/// get socket option +/// +/// TODO: some options not impl, just return 0, like SO_RCVBUF SO_SNDBUF +pub fn sys_getsockopt( + socket_fd: c_int, + level: c_int, + optname: c_int, + optval: *mut c_void, + optlen: *mut ctypes::socklen_t, +) -> c_int { + unsafe { + info!( + "sys_getsockopt <= fd: {}, level: {}, optname: {}, optlen: {}, IGNORED", + socket_fd, + level, + optname, + core::ptr::read(optlen as *mut usize) + ); + } + syscall_body!(sys_getsockopt, { + return Ok(0); + if optval.is_null() { + return Err(LinuxError::EFAULT); + } + let socket = Socket::from_fd(socket_fd)?; + match level as u32 { + ctypes::SOL_SOCKET => { + let val = match optname as u32 { + ctypes::SO_ACCEPTCONN => match &*socket { + Socket::Udp(_) => 0, + Socket::Tcp(tcpsocket) => { + if tcpsocket.lock().is_listening() { + 1 + } else { + 0 + } + } + Socket::Unix(unixsocket) => { + if unixsocket.lock().is_listening() { + 1 + } else { + 0 + } + } + }, + ctypes::SO_TYPE => match &*socket { + Socket::Udp(_) => ctypes::SOCK_DGRAM, + Socket::Tcp(_) => ctypes::SOCK_STREAM, + Socket::Unix(unixsocket) => match unixsocket.lock().get_sockettype() { + UnixSocketType::SockStream => ctypes::SOCK_STREAM, + UnixSocketType::SockDgram | UnixSocketType::SockSeqpacket => { + ctypes::SOCK_DGRAM + } + }, + }, + ctypes::SO_RCVLOWAT | ctypes::SO_SNDLOWAT | ctypes::SO_BROADCAST => 1, + ctypes::SO_ERROR + | ctypes::SO_DONTROUTE + | ctypes::SO_KEEPALIVE + | ctypes::SO_LINGER + | ctypes::SO_OOBINLINE + | ctypes::SO_RCVBUF + | ctypes::SO_RCVTIMEO + | ctypes::SO_REUSEADDR + | ctypes::SO_SNDBUF + | ctypes::SO_SNDTIMEO => 0, + _ => return Err(LinuxError::ENOPROTOOPT), + }; + + unsafe { + core::ptr::write(optlen as *mut usize, core::mem::size_of::()); + core::ptr::write(optval as *mut i32, val as i32); + } + + Ok(0) + } + _ => Err(LinuxError::ENOSYS), + } + }) +} + /// Get peer address to which the socket sockfd is connected. pub unsafe fn sys_getpeername( sock_fd: c_int, @@ -618,8 +801,14 @@ pub unsafe fn sys_getpeername( if unsafe { *addrlen } < size_of::() as u32 { return Err(LinuxError::EINVAL); } - unsafe { - (*addr, *addrlen) = into_sockaddr(Socket::from_fd(sock_fd)?.peer_addr()?); + let sockaddr = Socket::from_fd(sock_fd)?.peer_addr()?; + match sockaddr { + UnifiedSocketAddress::Net(netaddr) => unsafe { + (*addr, *addrlen) = into_sockaddr(netaddr); + }, + UnifiedSocketAddress::Unix(unixaddr) => unsafe { + (*addr, *addrlen) = un_into_sockaddr(unixaddr); + }, } Ok(0) }) @@ -658,6 +847,10 @@ pub unsafe fn sys_sendmsg( from_sockaddr(msg.msg_name as *const ctypes::sockaddr, msg.msg_namelen)?, )?, Socket::Tcp(tcpsocket) => tcpsocket.lock().send(buf)?, + Socket::Unix(unixsocket) => unixsocket.lock().sendto( + buf, + addrun_convert(msg.msg_name as *const ctypes::sockaddr_un), + )?, }; } Ok(ret) diff --git a/modules/ruxfs/src/root.rs b/modules/ruxfs/src/root.rs index 7ffb6a520..117dca0b3 100644 --- a/modules/ruxfs/src/root.rs +++ b/modules/ruxfs/src/root.rs @@ -23,6 +23,7 @@ pub struct MountPoint { pub fs: Arc, } +/// fs root directory pub struct RootDirectory { main_fs: Arc, mounts: Vec, @@ -44,6 +45,7 @@ impl Drop for MountPoint { } impl RootDirectory { + /// Creates a new `RootDirectory` with the specified main filesystem. pub const fn new(main_fs: Arc) -> Self { Self { main_fs, @@ -51,6 +53,7 @@ impl RootDirectory { } } + /// Mounts a new filesystem at the specified path within the root directory. pub fn mount(&mut self, path: &'static str, fs: Arc) -> AxResult { if path == "/" { return ax_err!(InvalidInput, "cannot mount root filesystem"); @@ -75,10 +78,12 @@ impl RootDirectory { Ok(()) } + /// Unmounts a filesystem at the specified path, if it exists. pub fn _umount(&mut self, path: &str) { self.mounts.retain(|mp| mp.path != path); } + /// Checks if a given path is a mount point in the root directory. pub fn contains(&self, path: &str) -> bool { self.mounts.iter().any(|mp| mp.path == path) } @@ -156,6 +161,7 @@ impl VfsNodeOps for RootDirectory { } } +/// Looks up a node in the virtual file system by its path. pub fn lookup(dir: Option<&VfsNodeRef>, path: &str) -> AxResult { if path.is_empty() { return ax_err!(NotFound); @@ -168,7 +174,8 @@ pub fn lookup(dir: Option<&VfsNodeRef>, path: &str) -> AxResult { } } -pub(crate) fn create_file(dir: Option<&VfsNodeRef>, path: &str) -> AxResult { +/// Creates a file in the virtual file system at the specified path. +pub fn create_file(dir: Option<&VfsNodeRef>, path: &str) -> AxResult { if path.is_empty() { return ax_err!(NotFound); } else if path.ends_with('/') { diff --git a/modules/ruxnet/Cargo.toml b/modules/ruxnet/Cargo.toml index bfc19c5f5..0ec18fc25 100644 --- a/modules/ruxnet/Cargo.toml +++ b/modules/ruxnet/Cargo.toml @@ -16,18 +16,23 @@ smoltcp = [] default = ["smoltcp", "loopback"] [dependencies] +hashbrown = "0.14.5" log = "0.4" cfg-if = "1.0" spin = "0.9" driver_net = { path = "../../crates/driver_net" } +flatten_objects = { path = "../../crates/flatten_objects" } lazy_init = { path = "../../crates/lazy_init" } +lazy_static = { version = "1.4", features = ["spin_no_std"] } lwip_rust = { path = "../../crates/lwip_rust", optional = true } printf-compat = { version = "0.1", default-features = false, optional = true } axerrno = { path = "../../crates/axerrno" } +axfs_vfs = { path = "../../crates/axfs_vfs" } ruxhal = { path = "../ruxhal" } axsync = { path = "../axsync" } axlog = { path = "../axlog" } ruxtask = { path = "../ruxtask" } +ruxfs = { path = "../ruxfs" } ruxdriver = { path = "../ruxdriver", features = ["net"] } cty = { version = "0.2.2", optional = true } axio = { path = "../../crates/axio" } diff --git a/modules/ruxnet/src/lib.rs b/modules/ruxnet/src/lib.rs index f7304e199..c2b48311b 100644 --- a/modules/ruxnet/src/lib.rs +++ b/modules/ruxnet/src/lib.rs @@ -37,6 +37,9 @@ extern crate log; extern crate alloc; +mod unix; +pub use unix::{SocketAddrUnix, UnixSocket, UnixSocketType}; + cfg_if::cfg_if! { if #[cfg(feature = "lwip")] { mod lwip_impl; @@ -73,6 +76,7 @@ pub fn init_network(mut net_devs: AxDeviceContainer) { } } net_impl::init(); + unix::init_unix(); while !net_devs.is_empty() { let dev = net_devs.take_one().expect("No NIC device found!"); info!(" use NIC: {:?}", dev.device_name()); diff --git a/modules/ruxnet/src/lwip_impl/tcp.rs b/modules/ruxnet/src/lwip_impl/tcp.rs index 2c621f0eb..15a09961f 100644 --- a/modules/ruxnet/src/lwip_impl/tcp.rs +++ b/modules/ruxnet/src/lwip_impl/tcp.rs @@ -191,6 +191,12 @@ impl TcpSocket { } } + /// Returens if this socket is listening + #[inline] + pub fn is_listening(&self) -> bool { + unsafe { (*self.pcb.get()).state == tcp_state_LISTEN } + } + /// Returns whether this socket is in nonblocking mode. #[inline] pub fn is_nonblocking(&self) -> bool { diff --git a/modules/ruxnet/src/smoltcp_impl/tcp.rs b/modules/ruxnet/src/smoltcp_impl/tcp.rs index c20a2424c..2634b8e25 100644 --- a/modules/ruxnet/src/smoltcp_impl/tcp.rs +++ b/modules/ruxnet/src/smoltcp_impl/tcp.rs @@ -108,6 +108,12 @@ impl TcpSocket { } } + /// Returens if this socket is listening + #[inline] + pub fn is_listening(&self) -> bool { + self.get_state() == STATE_LISTENING + } + /// Returns whether this socket is in nonblocking mode. #[inline] pub fn is_nonblocking(&self) -> bool { @@ -432,11 +438,6 @@ impl TcpSocket { self.get_state() == STATE_CONNECTED } - #[inline] - fn is_listening(&self) -> bool { - self.get_state() == STATE_LISTENING - } - fn bound_endpoint(&self) -> AxResult { // SAFETY: no other threads can read or write `self.local_addr`. let local_addr = unsafe { self.local_addr.get().read() }; diff --git a/modules/ruxnet/src/unix.rs b/modules/ruxnet/src/unix.rs new file mode 100644 index 000000000..5436ab237 --- /dev/null +++ b/modules/ruxnet/src/unix.rs @@ -0,0 +1,702 @@ +/* Copyright (c) [2023] [Syswonder Community] +* [Ruxos] is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +use alloc::{sync::Arc, vec}; +use axerrno::{ax_err, AxError, AxResult, LinuxError, LinuxResult}; +use axio::PollState; +use axsync::Mutex; +use core::ffi::{c_char, c_int}; +use core::net::SocketAddr; +use core::sync::atomic::{AtomicBool, Ordering}; +use spin::RwLock; + +use lazy_init::LazyInit; + +use smoltcp::socket::tcp::SocketBuffer; + +use hashbrown::HashMap; + +use ruxfs::root::{create_file, lookup}; +use ruxtask::yield_now; + +const SOCK_ADDR_UN_PATH_LEN: usize = 108; +const UNIX_SOCKET_BUFFER_SIZE: usize = 4096; + +/// rust form for ctype sockaddr_un +#[derive(Clone, Copy, Debug)] +pub struct SocketAddrUnix { + /// AF_UNIX + pub sun_family: u16, + /// socket path + pub sun_path: [c_char; SOCK_ADDR_UN_PATH_LEN], /* Pathname */ +} + +impl SocketAddrUnix { + /// Sets the socket address to the specified new address. + pub fn set_addr(&mut self, new_addr: &SocketAddrUnix) { + self.sun_family = new_addr.sun_family; + self.sun_path = new_addr.sun_path; + } +} + +//To avoid owner question of FDTABLE outside and UnixTable in this crate we split the unixsocket +struct UnixSocketInner<'a> { + pub addr: Mutex, + pub buf: SocketBuffer<'a>, + pub peer_socket: Option, + pub status: UnixSocketStatus, +} + +impl<'a> UnixSocketInner<'a> { + pub fn new() -> Self { + Self { + addr: Mutex::new(SocketAddrUnix { + sun_family: 1, //AF_UNIX + sun_path: [0; SOCK_ADDR_UN_PATH_LEN], + }), + buf: SocketBuffer::new(vec![0; 64 * 1024]), + peer_socket: None, + status: UnixSocketStatus::Closed, + } + } + + pub fn get_addr(&self) -> SocketAddrUnix { + self.addr.lock().clone() + } + + pub fn get_peersocket(&self) -> Option { + self.peer_socket + } + + pub fn set_peersocket(&mut self, peer: usize) { + self.peer_socket = Some(peer) + } + + pub fn get_state(&self) -> UnixSocketStatus { + self.status + } + + pub fn set_state(&mut self, state: UnixSocketStatus) { + self.status = state + } + + pub fn can_accept(&mut self) -> bool { + match self.status { + UnixSocketStatus::Listening => !self.buf.is_empty(), + _ => false, + } + } + + pub fn may_recv(&mut self) -> bool { + match self.status { + UnixSocketStatus::Connected => true, + //State::FinWait1 | State::FinWait2 => true, + _ if !self.buf.is_empty() => true, + _ => false, + } + } + + pub fn can_recv(&mut self) -> bool { + if !self.may_recv() { + return false; + } + + !self.buf.is_empty() + } + + pub fn may_send(&mut self) -> bool { + match self.status { + UnixSocketStatus::Connected => true, + //State::CloseWait => true, + _ => false, + } + } + + pub fn can_send(&mut self) -> bool { + self.may_send() + } +} + +/// unix domain socket. +pub struct UnixSocket { + sockethandle: Option, + unixsocket_type: UnixSocketType, + nonblock: AtomicBool, +} + +// now there is no real inode, this func is to check whether file exists +// TODO: if inode impl, this should return inode +fn get_inode(addr: SocketAddrUnix) -> AxResult { + let slice = unsafe { core::slice::from_raw_parts(addr.sun_path.as_ptr(), addr.sun_path.len()) }; + + let socket_path = unsafe { + core::ffi::CStr::from_ptr(slice.as_ptr()) + .to_str() + .expect("Invalid UTF-8 string") + }; + let _vfsnode = match lookup(None, socket_path) { + Ok(node) => node, + Err(_) => { + return Err(AxError::NotFound); + } + }; + + Err(AxError::Unsupported) +} + +fn create_socket_file(addr: SocketAddrUnix) -> AxResult { + let slice = unsafe { core::slice::from_raw_parts(addr.sun_path.as_ptr(), addr.sun_path.len()) }; + + let socket_path = unsafe { + core::ffi::CStr::from_ptr(slice.as_ptr()) + .to_str() + .expect("Invalid UTF-8 string") + }; + let _vfsnode = create_file(None, socket_path)?; + Err(AxError::Unsupported) +} + +struct HashMapWarpper<'a> { + inner: HashMap>>>, + index_allcator: Mutex, +} +impl<'a> HashMapWarpper<'a> { + pub fn new() -> Self { + Self { + inner: HashMap::new(), + index_allcator: Mutex::new(0), + } + } + pub fn find(&self, predicate: F) -> Option<(&usize, &Arc>>)> + where + F: Fn(&Arc>>) -> bool, + { + self.inner.iter().find(|(_k, v)| predicate(v)) + } + + pub fn add(&mut self, value: Arc>>) -> Option { + let index_allcator = self.index_allcator.get_mut(); + while self.inner.contains_key(index_allcator) { + *index_allcator += 1; + } + self.inner.insert(*index_allcator, value); + Some(*index_allcator) + } + + pub fn replace_handle(&mut self, old: usize, new: usize) -> Option { + if let Some(value) = self.inner.remove(&old) { + self.inner.insert(new, value); + } + Some(new) + } + + pub fn get(&self, id: usize) -> Option<&Arc>>> { + self.inner.get(&id) + } + + pub fn get_mut(&mut self, id: usize) -> Option<&mut Arc>>> { + self.inner.get_mut(&id) + } +} +static UNIX_TABLE: LazyInit> = LazyInit::new(); + +/// unix socket type +#[derive(Debug, Clone, Copy)] +pub enum UnixSocketType { + /// A stream-oriented Unix domain socket. + SockStream, + /// A datagram-oriented Unix domain socket. + SockDgram, + /// A sequenced packet Unix domain socket. + SockSeqpacket, +} + +// State transitions: +// CLOSED -(connect)-> BUSY -> CONNECTING -> CONNECTED -(shutdown)-> BUSY -> CLOSED +// | +// |-(listen)-> BUSY -> LISTENING -(shutdown)-> BUSY -> CLOSED +// | +// -(bind)-> BUSY -> CLOSED +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum UnixSocketStatus { + Closed, + Busy, + Connecting, + Connected, + Listening, +} + +impl UnixSocket { + /// create a new socket + /// only support sock_stream + pub fn new(_type: UnixSocketType) -> Self { + match _type { + UnixSocketType::SockDgram | UnixSocketType::SockSeqpacket => unimplemented!(), + UnixSocketType::SockStream => { + let mut unixsocket = UnixSocket { + sockethandle: None, + unixsocket_type: _type, + nonblock: AtomicBool::new(false), + }; + let handle = UNIX_TABLE + .write() + .add(Arc::new(Mutex::new(UnixSocketInner::new()))) + .unwrap(); + unixsocket.set_sockethandle(handle); + unixsocket + } + } + } + + /// Sets the socket handle. + pub fn set_sockethandle(&mut self, fd: usize) { + self.sockethandle = Some(fd); + } + + /// Returns the socket handle. + pub fn get_sockethandle(&self) -> usize { + self.sockethandle.unwrap() + } + + /// Returns the peer socket handle, if available. + pub fn get_peerhandle(&self) -> Option { + UNIX_TABLE + .read() + .get(self.get_sockethandle()) + .unwrap() + .lock() + .get_peersocket() + } + + /// Returns the current state of the socket. + pub fn get_state(&self) -> UnixSocketStatus { + UNIX_TABLE + .read() + .get(self.get_sockethandle()) + .unwrap() + .lock() + .status + } + + /// Enqueues data into the socket buffer. + /// returns the number of bytes enqueued, or an error if the socket is closed. + pub fn enqueue_buf(&mut self, data: &[u8]) -> AxResult { + match self.get_state() { + UnixSocketStatus::Closed => Err(AxError::BadState), + _ => Ok(UNIX_TABLE + .write() + .get_mut(self.get_sockethandle()) + .unwrap() + .lock() + .buf + .enqueue_slice(data)), + } + } + + /// Dequeues data from the socket buffer. + /// return the number of bytes dequeued, or a BadState error if the socket is closed or a WouldBlock error if buffer is empty. + pub fn dequeue_buf(&mut self, data: &mut [u8]) -> AxResult { + match self.get_state() { + UnixSocketStatus::Closed => Err(AxError::BadState), + _ => { + if UNIX_TABLE + .write() + .get_mut(self.get_sockethandle()) + .unwrap() + .lock() + .buf + .is_empty() + { + return Err(AxError::WouldBlock); + } + Ok(UNIX_TABLE + .write() + .get_mut(self.get_sockethandle()) + .unwrap() + .lock() + .buf + .dequeue_slice(data)) + } + } + } + + /// Binds the socket to a specified address, get inode number of the address as handle + // TODO: bind to file system + pub fn bind(&mut self, addr: SocketAddrUnix) -> LinuxResult { + let now_state = self.get_state(); + match now_state { + UnixSocketStatus::Closed => { + { + match get_inode(addr) { + Ok(inode_addr) => { + UNIX_TABLE + .write() + .replace_handle(self.get_sockethandle(), inode_addr); + self.set_sockethandle(inode_addr); + } + Err(AxError::NotFound) => match create_socket_file(addr) { + Ok(inode_addr) => { + UNIX_TABLE + .write() + .replace_handle(self.get_sockethandle(), inode_addr); + self.set_sockethandle(inode_addr); + } + _ => { + warn!("unix socket can not get real inode"); + } + }, + _ => { + warn!("unix socket can not get real inode"); + } + } + } + let mut binding = UNIX_TABLE.write(); + let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock(); + socket_inner.addr.lock().set_addr(&addr); + socket_inner.set_state(UnixSocketStatus::Busy); + Ok(()) + } + _ => Err(LinuxError::EINVAL), + } + } + + /// Sends data through the socket to the connected peer, push data into buffer of peer socket + /// this will block if not connected by default + pub fn send(&self, buf: &[u8]) -> LinuxResult { + match self.unixsocket_type { + UnixSocketType::SockDgram | UnixSocketType::SockSeqpacket => Err(LinuxError::ENOTCONN), + UnixSocketType::SockStream => loop { + let now_state = self.get_state(); + match now_state { + UnixSocketStatus::Connecting => { + if self.is_nonblocking() { + return Err(LinuxError::EINPROGRESS); + } else { + yield_now(); + } + } + UnixSocketStatus::Connected => { + let peer_handle = UNIX_TABLE + .read() + .get(self.get_sockethandle()) + .unwrap() + .lock() + .get_peersocket() + .unwrap(); + return Ok(UNIX_TABLE + .write() + .get_mut(peer_handle) + .unwrap() + .lock() + .buf + .enqueue_slice(buf)); + } + _ => { + return Err(LinuxError::ENOTCONN); + } + } + }, + } + } + + /// Receives data from the socket, check if there any data in buffer + /// this will block if not connected or buffer is empty by default + pub fn recv(&self, buf: &mut [u8], _flags: i32) -> LinuxResult { + match self.unixsocket_type { + UnixSocketType::SockDgram | UnixSocketType::SockSeqpacket => Err(LinuxError::ENOTCONN), + UnixSocketType::SockStream => loop { + let now_state = self.get_state(); + match now_state { + UnixSocketStatus::Connecting => { + if self.is_nonblocking() { + return Err(LinuxError::EAGAIN); + } else { + yield_now(); + } + } + UnixSocketStatus::Connected => { + if UNIX_TABLE + .read() + .get(self.get_sockethandle()) + .unwrap() + .lock() + .buf + .is_empty() + { + if self.is_nonblocking() { + return Err(LinuxError::EAGAIN); + } else { + yield_now(); + } + } else { + return Ok(UNIX_TABLE + .read() + .get(self.get_sockethandle()) + .unwrap() + .lock() + .buf + .dequeue_slice(buf)); + } + } + _ => { + return Err(LinuxError::ENOTCONN); + } + } + }, + } + } + + /// Polls the socket's readiness for connection. + fn poll_connect(&self) -> LinuxResult { + let writable = { + let mut binding = UNIX_TABLE.write(); + let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock(); + if !socket_inner.get_peersocket().is_none() { + socket_inner.set_state(UnixSocketStatus::Connected); + true + } else { + false + } + }; + Ok(PollState { + readable: false, + writable, + }) + } + + /// Polls the socket's readiness for reading or writing. + pub fn poll(&self) -> LinuxResult { + let now_state = self.get_state(); + match now_state { + UnixSocketStatus::Connecting => self.poll_connect(), + UnixSocketStatus::Connected => { + let mut binding = UNIX_TABLE.write(); + let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock(); + Ok(PollState { + readable: !socket_inner.may_recv() || socket_inner.can_recv(), + writable: !socket_inner.may_send() || socket_inner.can_send(), + }) + } + UnixSocketStatus::Listening => { + let mut binding = UNIX_TABLE.write(); + let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock(); + Ok(PollState { + readable: socket_inner.can_accept(), + writable: false, + }) + } + _ => Ok(PollState { + readable: false, + writable: false, + }), + } + } + + /// Returns the local address of the socket. + pub fn local_addr(&self) -> LinuxResult { + unimplemented!() + } + + /// Returns the file descriptor for the socket. + fn fd(&self) -> c_int { + UNIX_TABLE + .write() + .get_mut(self.get_sockethandle()) + .unwrap() + .lock() + .addr + .lock() + .sun_path[0] as _ + } + + /// Returns the peer address of the socket. + pub fn peer_addr(&self) -> AxResult { + let now_state = self.get_state(); + match now_state { + UnixSocketStatus::Connected | UnixSocketStatus::Listening => { + let peer_sockethandle = self.get_peerhandle().unwrap(); + Ok(UNIX_TABLE + .read() + .get(peer_sockethandle) + .unwrap() + .lock() + .get_addr()) + } + _ => Err(AxError::NotConnected), + } + } + + /// Connects the socket to a specified address, push info into remote socket + pub fn connect(&mut self, addr: SocketAddrUnix) -> LinuxResult { + let now_state = self.get_state(); + if now_state != UnixSocketStatus::Connecting && now_state != UnixSocketStatus::Connected { + //a new block is needed to free rwlock + { + match get_inode(addr) { + Ok(inode_addr) => { + let binding = UNIX_TABLE.write(); + let remote_socket = binding.get(inode_addr).unwrap(); + if remote_socket.lock().get_state() != UnixSocketStatus::Listening { + error!("unix conncet error: remote socket not listening"); + return Err(LinuxError::EFAULT); + } + let data = &self.get_sockethandle().to_ne_bytes(); + let _res = remote_socket.lock().buf.enqueue_slice(data); + } + Err(AxError::NotFound) => return Err(LinuxError::ENOENT), + _ => { + warn!("unix socket can not get real inode"); + let binding = UNIX_TABLE.write(); + let (_remote_sockethandle, remote_socket) = binding + .find(|socket| socket.lock().addr.lock().sun_path == addr.sun_path) + .unwrap(); + let data = &self.get_sockethandle().to_ne_bytes(); + let _res = remote_socket.lock().buf.enqueue_slice(data); + } + } + } + { + let mut binding = UNIX_TABLE.write(); + let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock(); + socket_inner.set_state(UnixSocketStatus::Connecting); + } + } + + loop { + let PollState { writable, .. } = self.poll_connect()?; + if !writable { + // When set to non_blocking, directly return inporgress + if self.is_nonblocking() { + return Err(LinuxError::EINPROGRESS); + } else { + yield_now(); + } + } else if self.get_state() == UnixSocketStatus::Connected { + return Ok(()); + } else { + // When set to non_blocking, directly return inporgress + if self.is_nonblocking() { + return Err(LinuxError::EINPROGRESS); + } + warn!("socket connect() failed") + } + } + } + + /// Sends data to a specified address. + pub fn sendto(&self, buf: &[u8], addr: SocketAddrUnix) -> LinuxResult { + unimplemented!() + } + + /// Receives data from the socket and returns the sender's address. + pub fn recvfrom(&self, buf: &mut [u8]) -> LinuxResult<(usize, Option)> { + unimplemented!() + } + + /// Listens for incoming connections on the socket. + // TODO: check file system + pub fn listen(&mut self) -> LinuxResult { + let now_state = self.get_state(); + match now_state { + UnixSocketStatus::Busy => { + let mut binding = UNIX_TABLE.write(); + let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock(); + socket_inner.set_state(UnixSocketStatus::Listening); + Ok(()) + } + _ => { + Ok(()) //ignore simultaneous `listen`s. + } + } + } + + /// Accepts a new connection from a listening socket, get info from self buffer + pub fn accept(&mut self) -> AxResult { + let now_state = self.get_state(); + match now_state { + UnixSocketStatus::Listening => { + //buf dequeue as handle to get socket + loop { + let data: &mut [u8] = &mut [0u8; core::mem::size_of::()]; + let res = self.dequeue_buf(data); + match res { + Ok(_len) => { + let mut array = [0u8; core::mem::size_of::()]; + array.copy_from_slice(data); + let remote_handle = usize::from_ne_bytes(array); + let unix_socket = UnixSocket::new(UnixSocketType::SockStream); + { + let mut binding = UNIX_TABLE.write(); + let remote_socket = binding.get_mut(remote_handle).unwrap(); + remote_socket + .lock() + .set_peersocket(unix_socket.get_sockethandle()); + } + let mut binding = UNIX_TABLE.write(); + let mut socket_inner = binding + .get_mut(unix_socket.get_sockethandle()) + .unwrap() + .lock(); + socket_inner.set_peersocket(remote_handle); + socket_inner.set_state(UnixSocketStatus::Connected); + return Ok(unix_socket); + } + Err(AxError::WouldBlock) => { + if self.is_nonblocking() { + return Err(AxError::WouldBlock); + } else { + yield_now(); + } + } + Err(e) => { + return Err(e); + } + } + } + } + _ => ax_err!(InvalidInput, "socket accept() failed: not listen"), + } + } + + //TODO + /// Shuts down the socket. + pub fn shutdown(&self) -> LinuxResult { + unimplemented!() + } + + /// Returns whether this socket is in nonblocking mode. + #[inline] + pub fn is_nonblocking(&self) -> bool { + self.nonblock.load(Ordering::Acquire) + } + + /// Sets the nonblocking mode for the socket. + pub fn set_nonblocking(&self, nonblocking: bool) { + self.nonblock.store(nonblocking, Ordering::Release); + } + + /// Checks if the socket is in a listening state. + pub fn is_listening(&self) -> bool { + let now_state = self.get_state(); + match now_state { + UnixSocketStatus::Listening => true, + _ => false, + } + } + + /// Returns the socket type of the `UnixSocket`. + pub fn get_sockettype(&self) -> UnixSocketType { + self.unixsocket_type + } +} + +/// Initializes the global UNIX socket table, `UNIX_TABLE`, for managing Unix domain sockets. +pub(crate) fn init_unix() { + UNIX_TABLE.init_by(RwLock::new(HashMapWarpper::new())); +} From e149cde883352f2e6eccf9b41c1fe5cf55f4d4a0 Mon Sep 17 00:00:00 2001 From: WuZheng Date: Tue, 3 Dec 2024 00:44:53 +0800 Subject: [PATCH 10/15] fix bug for unexpected pagefault when nested fork. --- api/ruxos_posix_api/src/imp/pthread/mod.rs | 1 - crates/driver_net/src/loopback.rs | 8 +++----- modules/ruxnet/src/smoltcp_impl/mod.rs | 4 +--- modules/ruxtask/src/run_queue.rs | 1 + modules/ruxtask/src/task.rs | 21 +++++++++++++++------ 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/api/ruxos_posix_api/src/imp/pthread/mod.rs b/api/ruxos_posix_api/src/imp/pthread/mod.rs index da2694ff8..48a205c2e 100644 --- a/api/ruxos_posix_api/src/imp/pthread/mod.rs +++ b/api/ruxos_posix_api/src/imp/pthread/mod.rs @@ -310,7 +310,6 @@ pub unsafe fn sys_clone( } else if (flags as u32 & ctypes::SIGCHLD) != 0 { TID_TO_PTHREAD.read(); let pid = if let Some(task_ref) = ruxtask::fork_task() { - warn!("fork_task success, pid: {}", task_ref.id().as_u64()); task_ref.id().as_u64() } else { let children_ref = ruxtask::current(); diff --git a/crates/driver_net/src/loopback.rs b/crates/driver_net/src/loopback.rs index 3574cee75..072bd0d6f 100644 --- a/crates/driver_net/src/loopback.rs +++ b/crates/driver_net/src/loopback.rs @@ -57,8 +57,6 @@ impl BaseDriverOps for LoopbackDevice { } } -use log::info; - impl NetDriverOps for LoopbackDevice { #[inline] fn mac_address(&self) -> EthernetAddress { @@ -85,11 +83,11 @@ impl NetDriverOps for LoopbackDevice { self.queue.len() } - fn fill_rx_buffers(&mut self, buf_pool: &Arc) -> DevResult { + fn fill_rx_buffers(&mut self, _buf_pool: &Arc) -> DevResult { Ok(()) } - fn recycle_rx_buffer(&mut self, rx_buf: NetBufPtr) -> DevResult { + fn recycle_rx_buffer(&mut self, _rx_buf: NetBufPtr) -> DevResult { Ok(()) } @@ -97,7 +95,7 @@ impl NetDriverOps for LoopbackDevice { Ok(()) } - fn prepare_tx_buffer(&self, tx_buf: &mut NetBuf, pkt_len: usize) -> DevResult { + fn prepare_tx_buffer(&self, _tx_buf: &mut NetBuf, _pkt_len: usize) -> DevResult { Ok(()) } diff --git a/modules/ruxnet/src/smoltcp_impl/mod.rs b/modules/ruxnet/src/smoltcp_impl/mod.rs index 192fe2d93..50bfbfe9d 100644 --- a/modules/ruxnet/src/smoltcp_impl/mod.rs +++ b/modules/ruxnet/src/smoltcp_impl/mod.rs @@ -36,8 +36,6 @@ pub use self::dns::dns_query; pub use self::tcp::TcpSocket; pub use self::udp::UdpSocket; -pub use driver_net::loopback::LoopbackDevice; - macro_rules! env_or_default { ($key:literal) => { match option_env!($key) { @@ -347,7 +345,7 @@ pub fn bench_receive() { } pub(crate) fn init() { - let mut socketset = SocketSetWrapper::new(); + let socketset = SocketSetWrapper::new(); IFACE_LIST.init_by(Mutex::new(vec::Vec::new())); SOCKET_SET.init_by(socketset); diff --git a/modules/ruxtask/src/run_queue.rs b/modules/ruxtask/src/run_queue.rs index 82fa5ffd1..95de4926f 100644 --- a/modules/ruxtask/src/run_queue.rs +++ b/modules/ruxtask/src/run_queue.rs @@ -129,6 +129,7 @@ impl AxRunQueue { assert!(!curr.is_idle()); // we must not block current task with preemption disabled. + // only allow blocking current task with run_queue lock held. #[cfg(feature = "preempt")] assert!(curr.can_preempt(1)); diff --git a/modules/ruxtask/src/task.rs b/modules/ruxtask/src/task.rs index cc1a45d20..7311d53ad 100644 --- a/modules/ruxtask/src/task.rs +++ b/modules/ruxtask/src/task.rs @@ -81,6 +81,7 @@ pub struct TaskInner { exit_code: AtomicI32, wait_for_exit: WaitQueue, + stack_map_addr: SpinNoIrq, kstack: SpinNoIrq>>, ctx: UnsafeCell, @@ -235,6 +236,7 @@ impl TaskInner { preempt_disable_count: AtomicUsize::new(0), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), + stack_map_addr: SpinNoIrq::new(VirtAddr::from(0)), // should be set later kstack: SpinNoIrq::new(Arc::new(None)), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] @@ -279,6 +281,7 @@ impl TaskInner { preempt_disable_count: AtomicUsize::new(0), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), + stack_map_addr: SpinNoIrq::new(VirtAddr::from(0)), kstack: SpinNoIrq::new(Arc::new(None)), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] @@ -299,6 +302,7 @@ impl TaskInner { pub fn set_stack_top(&self, begin: usize, size: usize) { debug!("set_stack_top: begin={:#x}, size={:#x}", begin, size); + *self.stack_map_addr.lock() = VirtAddr::from(begin); *self.kstack.lock() = Arc::new(Some(TaskStack { ptr: NonNull::new(begin as *mut u8).unwrap(), layout: Layout::from_size_align(size, PAGE_SIZE_4K).unwrap(), @@ -406,14 +410,14 @@ impl TaskInner { // Note: the stack region is mapped to the same position as the parent process's stack, be careful when update the stack region for the forked process. let (_, prev_flag, _) = cloned_page_table - .query(current_stack.end()) + .query(*current().stack_map_addr.lock()) .expect("failed to query stack region when forking"); cloned_page_table - .unmap_region(current_stack.end(), align_up_4k(stack_size)) + .unmap_region(*current().stack_map_addr.lock(), align_up_4k(stack_size)) .expect("failed to unmap stack region when forking"); cloned_page_table .map_region( - current_stack.end(), + *current().stack_map_addr.lock(), stack_paddr, stack_size, prev_flag, @@ -477,10 +481,11 @@ impl TaskInner { need_resched: AtomicBool::new(current_task.need_resched.load(Ordering::Relaxed)), #[cfg(feature = "preempt")] preempt_disable_count: AtomicUsize::new( - current_task.preempt_disable_count.load(Ordering::Relaxed), + current_task.preempt_disable_count.load(Ordering::Acquire), ), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), + stack_map_addr: SpinNoIrq::new(*current().stack_map_addr.lock()), kstack: SpinNoIrq::new(Arc::new(Some(new_stack))), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] @@ -515,6 +520,7 @@ impl TaskInner { .lock() .insert(new_pid.as_u64(), task_ref.clone()); + warn!("forked task: save_current_content {}", task_ref.id_name()); unsafe { // copy the stack content from current stack to new stack (*task_ref.ctx_mut_ptr()).save_current_content( @@ -554,6 +560,7 @@ impl TaskInner { preempt_disable_count: AtomicUsize::new(0), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), + stack_map_addr: SpinNoIrq::new(VirtAddr::from(0)), // set in set_stack_top kstack: SpinNoIrq::new(Arc::new(None)), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] @@ -590,6 +597,7 @@ impl TaskInner { let bindings = PROCESS_MAP.lock(); let (&_parent_id, &ref task_ref) = bindings.first_key_value().unwrap(); let idle_kstack = TaskStack::alloc(align_up_4k(IDLE_STACK_SIZE)); + let idle_kstack_top = idle_kstack.top(); let mut t = Self { parent_process: Some(Arc::downgrade(task_ref)), @@ -609,7 +617,8 @@ impl TaskInner { preempt_disable_count: AtomicUsize::new(0), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), - kstack: SpinNoIrq::new(Arc::new(None)), + stack_map_addr: SpinNoIrq::new(idle_kstack.end()), + kstack: SpinNoIrq::new(Arc::new(Some(idle_kstack))), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] tls: TlsArea::alloc(), @@ -633,7 +642,7 @@ impl TaskInner { debug!("new idle task: {}", t.id_name()); t.ctx .get_mut() - .init(task_entry as usize, idle_kstack.top(), tls); + .init(task_entry as usize, idle_kstack_top, tls); let task_ref = Arc::new(AxTask::new(t)); From bd02cdd8227819e8a3d2e07e62b108cea32cba4d Mon Sep 17 00:00:00 2001 From: lhw Date: Wed, 11 Dec 2024 20:06:30 +0800 Subject: [PATCH 11/15] add unix socket drop --- modules/ruxnet/src/unix.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/modules/ruxnet/src/unix.rs b/modules/ruxnet/src/unix.rs index 5436ab237..9136090b8 100644 --- a/modules/ruxnet/src/unix.rs +++ b/modules/ruxnet/src/unix.rs @@ -203,6 +203,10 @@ impl<'a> HashMapWarpper<'a> { pub fn get_mut(&mut self, id: usize) -> Option<&mut Arc>>> { self.inner.get_mut(&id) } + + pub fn remove(&mut self, id: usize) -> Option>>> { + self.inner.remove(&id) + } } static UNIX_TABLE: LazyInit> = LazyInit::new(); @@ -664,10 +668,12 @@ impl UnixSocket { } } - //TODO /// Shuts down the socket. pub fn shutdown(&self) -> LinuxResult { - unimplemented!() + let mut binding = UNIX_TABLE.write(); + let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock(); + socket_inner.set_state(UnixSocketStatus::Closed); + Ok(()) } /// Returns whether this socket is in nonblocking mode. @@ -696,6 +702,13 @@ impl UnixSocket { } } +impl Drop for UnixSocket { + fn drop(&mut self) { + self.shutdown(); + UNIX_TABLE.write().remove(self.get_sockethandle()); + } +} + /// Initializes the global UNIX socket table, `UNIX_TABLE`, for managing Unix domain sockets. pub(crate) fn init_unix() { UNIX_TABLE.init_by(RwLock::new(HashMapWarpper::new())); From 6f4bf66548e38c23b4fc169eafc730806c7e35d8 Mon Sep 17 00:00:00 2001 From: lhw Date: Sun, 15 Dec 2024 15:31:03 +0800 Subject: [PATCH 12/15] move signal handle into ruxtask and fix sys_rt_rigaction --- api/ruxos_posix_api/Cargo.toml | 2 +- api/ruxos_posix_api/src/imp/rt_sig.rs | 20 ++++++-- api/ruxos_posix_api/src/imp/signal.rs | 2 +- modules/ruxruntime/src/lib.rs | 4 +- modules/ruxtask/Cargo.toml | 1 + modules/ruxtask/src/lib.rs | 8 ++++ modules/{ruxruntime => ruxtask}/src/signal.rs | 46 +++++++++++++------ modules/ruxtask/src/task.rs | 36 ++++++++++++--- ulib/ruxlibc/src/signal.rs | 27 +++++++---- 9 files changed, 108 insertions(+), 38 deletions(-) rename modules/{ruxruntime => ruxtask}/src/signal.rs (74%) diff --git a/api/ruxos_posix_api/Cargo.toml b/api/ruxos_posix_api/Cargo.toml index 927068c81..4fe38279f 100644 --- a/api/ruxos_posix_api/Cargo.toml +++ b/api/ruxos_posix_api/Cargo.toml @@ -24,7 +24,7 @@ multitask = ["ruxfeat/multitask", "ruxtask/multitask", "dep:ruxfutex"] fd = ["alloc"] fs = ["dep:ruxfs", "ruxfeat/fs", "fd"] net = ["dep:ruxnet", "ruxfeat/net", "fd"] -signal = ["ruxruntime/signal", "ruxhal/signal"] +signal = ["ruxruntime/signal", "ruxhal/signal", "ruxtask/signal"] pipe = ["fd"] select = ["fd"] epoll = ["fd"] diff --git a/api/ruxos_posix_api/src/imp/rt_sig.rs b/api/ruxos_posix_api/src/imp/rt_sig.rs index 8d89cf6aa..252b6249a 100644 --- a/api/ruxos_posix_api/src/imp/rt_sig.rs +++ b/api/ruxos_posix_api/src/imp/rt_sig.rs @@ -95,10 +95,24 @@ pub unsafe fn sys_rt_sigaction( old: *mut ctypes::sigaction, _sigsetsize: ctypes::size_t, ) -> c_int { - debug!("sys_rt_sigaction <= sig: {}", sig); + debug!( + "sys_rt_sigaction <= sig: {} sa {:x} old {:x}", + sig, sa as u64, old as u64 + ); syscall_body!(sys_rt_sigaction, { - if sa as u64 == 0 || old as u64 == 0 { - Err(LinuxError::EFAULT) + if sa as u64 == 0 && old as u64 == 0 { + sys_sigaction(sig as _, None, None); + Ok(0) + } else if sa as u64 != 0 && old as u64 == 0 { + let sa = unsafe { *sa }; + let sa = k_sigaction::from(sa); + sys_sigaction(sig as _, Some(&sa), None); + Ok(0) + } else if sa as u64 == 0 && old as u64 != 0 { + let old = unsafe { *old }; + let mut old_sa = k_sigaction::from(old); + sys_sigaction(sig as _, None, Some(&mut old_sa)); + Ok(0) } else { let sa = unsafe { *sa }; let old = unsafe { *old }; diff --git a/api/ruxos_posix_api/src/imp/signal.rs b/api/ruxos_posix_api/src/imp/signal.rs index 743441583..929d4b3b1 100644 --- a/api/ruxos_posix_api/src/imp/signal.rs +++ b/api/ruxos_posix_api/src/imp/signal.rs @@ -14,7 +14,7 @@ use crate::ctypes::k_sigaction; use crate::ctypes::{self, pid_t}; use axerrno::LinuxError; -use ruxruntime::{rx_sigaction, Signal}; +use ruxtask::{rx_sigaction, Signal}; /// Set signal handler pub fn sys_sigaction( diff --git a/modules/ruxruntime/src/lib.rs b/modules/ruxruntime/src/lib.rs index d407ca36e..90e4034c0 100644 --- a/modules/ruxruntime/src/lib.rs +++ b/modules/ruxruntime/src/lib.rs @@ -39,8 +39,6 @@ extern crate axlog; #[cfg(all(target_os = "none", not(test)))] mod lang_items; -#[cfg(feature = "signal")] -mod signal; #[cfg(not(feature = "musl"))] mod trap; @@ -52,7 +50,7 @@ mod mp; pub use self::mp::rust_main_secondary; #[cfg(feature = "signal")] -pub use self::signal::{rx_sigaction, Signal}; +use ruxtask::signal::{rx_sigaction, Signal}; #[cfg(feature = "alloc")] extern crate alloc; diff --git a/modules/ruxtask/Cargo.toml b/modules/ruxtask/Cargo.toml index 3b4b13c6e..386912fbb 100644 --- a/modules/ruxtask/Cargo.toml +++ b/modules/ruxtask/Cargo.toml @@ -24,6 +24,7 @@ musl = [] preempt = ["irq", "percpu?/preempt", "kernel_guard/preempt"] paging = [] fs = [] +signal = [] sched_fifo = ["multitask"] sched_rr = ["multitask", "preempt"] diff --git a/modules/ruxtask/src/lib.rs b/modules/ruxtask/src/lib.rs index fbbbaf76d..0bcfaff3f 100644 --- a/modules/ruxtask/src/lib.rs +++ b/modules/ruxtask/src/lib.rs @@ -48,6 +48,8 @@ cfg_if::cfg_if! { pub mod task; mod api; mod wait_queue; + #[cfg(feature = "signal")] + pub mod signal; #[cfg(feature = "paging")] pub mod vma; // #[cfg(feature = "fs")] @@ -79,8 +81,14 @@ cfg_if::cfg_if! { pub use self::api::*; pub use self::api::{sleep, sleep_until, yield_now}; pub use task::TaskState; + #[cfg(feature = "signal")] + pub use self::signal::{rx_sigaction, Signal}; } else { mod api_s; + #[cfg(feature = "signal")] + pub mod signal; pub use self::api_s::{sleep, sleep_until, yield_now}; + #[cfg(feature = "signal")] + pub use self::signal::{rx_sigaction, Signal}; } } diff --git a/modules/ruxruntime/src/signal.rs b/modules/ruxtask/src/signal.rs similarity index 74% rename from modules/ruxruntime/src/signal.rs rename to modules/ruxtask/src/signal.rs index 148afd29f..0f64c63b7 100644 --- a/modules/ruxruntime/src/signal.rs +++ b/modules/ruxtask/src/signal.rs @@ -7,6 +7,7 @@ * See the Mulan PSL v2 for more details. */ +use crate::current; #[cfg(feature = "irq")] use core::sync::atomic::AtomicI64; use core::{ @@ -85,6 +86,17 @@ impl TrapHandler for SignalHandler { } impl Signal { + ///crate new Signal struct + pub fn new() -> Self { + Self { + #[cfg(feature = "irq")] + signal: AtomicI64::new(0), + sigaction: [rx_sigaction::new(); 32], + // Default::default() is not const + timer_value: [Duration::from_nanos(0); 3], + timer_interval: [Duration::from_nanos(0); 3], + } + } /// Set signal /// signum: signal number, if signum < 0, just return current signal /// on: true: enable signal, false: disable signal @@ -94,7 +106,9 @@ impl Signal { if signum >= 32 { return None; } - let mut old = unsafe { SIGNAL_IF.signal.load(Ordering::Acquire) }; + let binding = current(); + let mut current_signal_if = binding.signal_if.lock(); + let mut old = unsafe { current_signal_if.signal.load(Ordering::Acquire) }; if signum >= 0 { loop { let new = if on { @@ -104,7 +118,7 @@ impl Signal { }; match unsafe { - SIGNAL_IF.signal.compare_exchange_weak( + current_signal_if.signal.compare_exchange_weak( old, new, Ordering::AcqRel, @@ -126,21 +140,21 @@ impl Signal { sigaction: Option<*const rx_sigaction>, oldact: Option<*mut rx_sigaction>, ) { - if signum >= unsafe { SIGNAL_IF.sigaction }.len() as u8 { + let binding = current(); + let mut current_signal_if = binding.signal_if.lock(); + if signum >= unsafe { current_signal_if.sigaction }.len() as u8 { return; } if let Some(oldact) = oldact { unsafe { - *oldact = SIGNAL_IF.sigaction[signum as usize]; + *oldact = current_signal_if.sigaction[signum as usize]; } } match sigaction { Some(s) => unsafe { - SIGNAL_IF.sigaction[signum as usize] = *s; - }, - None => unsafe { - SIGNAL_IF.sigaction[signum as usize].sa_handler.unwrap()(signum as c_int) + current_signal_if.sigaction[signum as usize] = *s; }, + None => {}, } } /// Set timer @@ -148,13 +162,15 @@ impl Signal { /// new_value: new timer value /// old_value: old timer value pub fn timer_deadline(which: usize, new_deadline: Option) -> Option { - if which >= unsafe { SIGNAL_IF.timer_value }.len() { + let binding = current(); + let mut current_signal_if = binding.signal_if.lock(); + if which >= unsafe { current_signal_if.timer_value }.len() { return None; } - let old = unsafe { SIGNAL_IF.timer_value }[which]; + let old = unsafe { current_signal_if.timer_value }[which]; if let Some(s) = new_deadline { unsafe { - SIGNAL_IF.timer_value[which] = Duration::from_nanos(s); + current_signal_if.timer_value[which] = Duration::from_nanos(s); } } Some(old.as_nanos() as u64) @@ -164,13 +180,15 @@ impl Signal { /// new_interval: new timer interval /// old_interval: old timer interval pub fn timer_interval(which: usize, new_interval: Option) -> Option { - if which >= unsafe { SIGNAL_IF.timer_interval }.len() { + let binding = current(); + let mut current_signal_if = binding.signal_if.lock(); + if which >= unsafe { current_signal_if.timer_interval }.len() { return None; } - let old = unsafe { SIGNAL_IF.timer_interval }[which]; + let old = unsafe { current_signal_if.timer_interval }[which]; if let Some(s) = new_interval { unsafe { - SIGNAL_IF.timer_interval[which] = Duration::from_nanos(s); + current_signal_if.timer_interval[which] = Duration::from_nanos(s); } } Some(old.as_nanos() as u64) diff --git a/modules/ruxtask/src/task.rs b/modules/ruxtask/src/task.rs index cc1a45d20..ce9e086bf 100644 --- a/modules/ruxtask/src/task.rs +++ b/modules/ruxtask/src/task.rs @@ -38,6 +38,8 @@ use crate::current; use crate::tsd::{DestrFunction, KEYS, TSD}; #[cfg(feature = "paging")] use crate::vma::MmapStruct; +#[cfg(feature = "signal")] +use crate::Signal; use crate::{AxRunQueue, AxTask, AxTaskRef, WaitQueue}; /// A unique identifier for a thread. @@ -81,6 +83,7 @@ pub struct TaskInner { exit_code: AtomicI32, wait_for_exit: WaitQueue, + stack_map_addr: SpinNoIrq, kstack: SpinNoIrq>>, ctx: UnsafeCell, @@ -90,6 +93,9 @@ pub struct TaskInner { #[cfg(not(feature = "musl"))] tsd: TSD, + #[cfg(feature = "signal")] + pub signal_if: Arc>, + // set tid #[cfg(feature = "musl")] set_tid: AtomicU64, @@ -235,6 +241,7 @@ impl TaskInner { preempt_disable_count: AtomicUsize::new(0), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), + stack_map_addr: SpinNoIrq::new(VirtAddr::from(0)), // should be set later kstack: SpinNoIrq::new(Arc::new(None)), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] @@ -243,6 +250,8 @@ impl TaskInner { tsd: spinlock::SpinNoIrq::new([core::ptr::null_mut(); ruxconfig::PTHREAD_KEY_MAX]), #[cfg(feature = "musl")] set_tid: AtomicU64::new(0), + #[cfg(feature = "signal")] + signal_if: current().signal_if.clone(), #[cfg(feature = "musl")] tl: AtomicU64::new(0), #[cfg(feature = "paging")] @@ -279,11 +288,14 @@ impl TaskInner { preempt_disable_count: AtomicUsize::new(0), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), + stack_map_addr: SpinNoIrq::new(VirtAddr::from(0)), kstack: SpinNoIrq::new(Arc::new(None)), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] tls: TlsArea::new_with_addr(tls), set_tid, + #[cfg(feature = "signal")] + signal_if: current().signal_if.clone(), // clear child tid tl, #[cfg(feature = "paging")] @@ -299,6 +311,7 @@ impl TaskInner { pub fn set_stack_top(&self, begin: usize, size: usize) { debug!("set_stack_top: begin={:#x}, size={:#x}", begin, size); + *self.stack_map_addr.lock() = VirtAddr::from(begin); *self.kstack.lock() = Arc::new(Some(TaskStack { ptr: NonNull::new(begin as *mut u8).unwrap(), layout: Layout::from_size_align(size, PAGE_SIZE_4K).unwrap(), @@ -406,14 +419,14 @@ impl TaskInner { // Note: the stack region is mapped to the same position as the parent process's stack, be careful when update the stack region for the forked process. let (_, prev_flag, _) = cloned_page_table - .query(current_stack.end()) + .query(*current().stack_map_addr.lock()) .expect("failed to query stack region when forking"); cloned_page_table - .unmap_region(current_stack.end(), align_up_4k(stack_size)) + .unmap_region(*current().stack_map_addr.lock(), align_up_4k(stack_size)) .expect("failed to unmap stack region when forking"); cloned_page_table .map_region( - current_stack.end(), + *current().stack_map_addr.lock(), stack_paddr, stack_size, prev_flag, @@ -477,10 +490,11 @@ impl TaskInner { need_resched: AtomicBool::new(current_task.need_resched.load(Ordering::Relaxed)), #[cfg(feature = "preempt")] preempt_disable_count: AtomicUsize::new( - current_task.preempt_disable_count.load(Ordering::Relaxed), + current_task.preempt_disable_count.load(Ordering::Acquire), ), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), + stack_map_addr: SpinNoIrq::new(*current().stack_map_addr.lock()), kstack: SpinNoIrq::new(Arc::new(Some(new_stack))), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] @@ -489,6 +503,8 @@ impl TaskInner { tsd: spinlock::SpinNoIrq::new([core::ptr::null_mut(); ruxconfig::PTHREAD_KEY_MAX]), #[cfg(feature = "musl")] set_tid: AtomicU64::new(0), + #[cfg(feature = "signal")] + signal_if: Arc::new(spinlock::SpinNoIrq::new(Signal::new())), #[cfg(feature = "musl")] tl: AtomicU64::new(0), #[cfg(feature = "paging")] @@ -515,6 +531,7 @@ impl TaskInner { .lock() .insert(new_pid.as_u64(), task_ref.clone()); + warn!("forked task: save_current_content {}", task_ref.id_name()); unsafe { // copy the stack content from current stack to new stack (*task_ref.ctx_mut_ptr()).save_current_content( @@ -554,6 +571,7 @@ impl TaskInner { preempt_disable_count: AtomicUsize::new(0), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), + stack_map_addr: SpinNoIrq::new(VirtAddr::from(0)), // set in set_stack_top kstack: SpinNoIrq::new(Arc::new(None)), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] @@ -562,6 +580,8 @@ impl TaskInner { tsd: spinlock::SpinNoIrq::new([core::ptr::null_mut(); ruxconfig::PTHREAD_KEY_MAX]), #[cfg(feature = "musl")] set_tid: AtomicU64::new(0), + #[cfg(feature = "signal")] + signal_if: Arc::new(spinlock::SpinNoIrq::new(Signal::new())), #[cfg(feature = "musl")] tl: AtomicU64::new(0), #[cfg(feature = "paging")] @@ -590,6 +610,7 @@ impl TaskInner { let bindings = PROCESS_MAP.lock(); let (&_parent_id, &ref task_ref) = bindings.first_key_value().unwrap(); let idle_kstack = TaskStack::alloc(align_up_4k(IDLE_STACK_SIZE)); + let idle_kstack_top = idle_kstack.top(); let mut t = Self { parent_process: Some(Arc::downgrade(task_ref)), @@ -609,7 +630,8 @@ impl TaskInner { preempt_disable_count: AtomicUsize::new(0), exit_code: AtomicI32::new(0), wait_for_exit: WaitQueue::new(), - kstack: SpinNoIrq::new(Arc::new(None)), + stack_map_addr: SpinNoIrq::new(idle_kstack.end()), + kstack: SpinNoIrq::new(Arc::new(Some(idle_kstack))), ctx: UnsafeCell::new(TaskContext::new()), #[cfg(feature = "tls")] tls: TlsArea::alloc(), @@ -617,6 +639,8 @@ impl TaskInner { tsd: spinlock::SpinNoIrq::new([core::ptr::null_mut(); ruxconfig::PTHREAD_KEY_MAX]), #[cfg(feature = "musl")] set_tid: AtomicU64::new(0), + #[cfg(feature = "signal")] + signal_if: task_ref.signal_if.clone(), #[cfg(feature = "musl")] tl: AtomicU64::new(0), #[cfg(feature = "paging")] @@ -633,7 +657,7 @@ impl TaskInner { debug!("new idle task: {}", t.id_name()); t.ctx .get_mut() - .init(task_entry as usize, idle_kstack.top(), tls); + .init(task_entry as usize, idle_kstack_top, tls); let task_ref = Arc::new(AxTask::new(t)); diff --git a/ulib/ruxlibc/src/signal.rs b/ulib/ruxlibc/src/signal.rs index 1fc19d958..080c59f3f 100644 --- a/ulib/ruxlibc/src/signal.rs +++ b/ulib/ruxlibc/src/signal.rs @@ -29,17 +29,24 @@ pub unsafe extern "C" fn sigaction_inner( } #[cfg(feature = "signal")] { - let mut sh = (*_act).__sa_handler.sa_handler; - if let Some(h) = sh { - if h as usize == crate::ctypes::SIGIGN as usize { - sh = Some(ignore_handler as unsafe extern "C" fn(c_int)); + + let k_act = { + if _act.is_null() { + None + } else { + let mut sh = (*_act).__sa_handler.sa_handler; + if let Some(h) = sh { + if h as usize == crate::ctypes::SIGIGN as usize { + sh = Some(ignore_handler as unsafe extern "C" fn(c_int)); + } + } + k_sigaction { + handler: sh, + flags: (*_act).sa_flags as _, + restorer: (*_act).sa_restorer, + mask: Default::default(), + } } - } - let k_act = k_sigaction { - handler: sh, - flags: (*_act).sa_flags as _, - restorer: (*_act).sa_restorer, - mask: Default::default(), }; let mut k_oldact = k_sigaction::default(); sys_sigaction( From 693655378b2c3f35ff6a9163fd41d80bc3fbd1d2 Mon Sep 17 00:00:00 2001 From: lhw Date: Sat, 21 Dec 2024 11:03:12 +0800 Subject: [PATCH 13/15] add poll state POLLHUP --- api/ruxos_posix_api/src/imp/io_mpx/epoll.rs | 6 ++++++ api/ruxos_posix_api/src/imp/io_mpx/poll.rs | 5 +++++ api/ruxos_posix_api/src/imp/io_mpx/select.rs | 4 ++++ api/ruxos_posix_api/src/imp/pipe.rs | 4 +++- api/ruxos_posix_api/src/imp/stdio.rs | 2 ++ crates/axio/src/lib.rs | 2 ++ modules/ruxconfig/defconfig.toml | 3 +++ modules/ruxnet/src/lwip_impl/tcp.rs | 4 +++- modules/ruxnet/src/lwip_impl/udp.rs | 1 + modules/ruxnet/src/smoltcp_impl/tcp.rs | 7 +++++++ modules/ruxnet/src/smoltcp_impl/udp.rs | 2 ++ modules/ruxnet/src/unix.rs | 17 +++++++++++++++++ modules/ruxtask/src/fs.rs | 2 ++ platforms/aarch64-qemu-virt.toml | 3 +++ platforms/aarch64-raspi4.toml | 3 +++ platforms/riscv64-qemu-virt.toml | 3 +++ platforms/x86_64-qemu-q35.toml | 3 +++ 17 files changed, 69 insertions(+), 2 deletions(-) diff --git a/api/ruxos_posix_api/src/imp/io_mpx/epoll.rs b/api/ruxos_posix_api/src/imp/io_mpx/epoll.rs index 7802f4b6f..09eb6766b 100644 --- a/api/ruxos_posix_api/src/imp/io_mpx/epoll.rs +++ b/api/ruxos_posix_api/src/imp/io_mpx/epoll.rs @@ -108,6 +108,12 @@ impl EpollInstance { events[events_num].data = ev.data; events_num += 1; } + + if state.pollhup { + events[events_num].events = ctypes::EPOLLHUP; + events[events_num].data = ev.data; + events_num += 1; + } } } } diff --git a/api/ruxos_posix_api/src/imp/io_mpx/poll.rs b/api/ruxos_posix_api/src/imp/io_mpx/poll.rs index 94468f7eb..103d85056 100644 --- a/api/ruxos_posix_api/src/imp/io_mpx/poll.rs +++ b/api/ruxos_posix_api/src/imp/io_mpx/poll.rs @@ -38,6 +38,11 @@ fn poll_all(fds: &mut [ctypes::pollfd]) -> LinuxResult { *revents |= ctypes::EPOLLOUT as i16; events_num += 1; } + + if state.pollhup { + *revents |= ctypes::EPOLLHUP as i16; + events_num += 1; + } } } } diff --git a/api/ruxos_posix_api/src/imp/io_mpx/select.rs b/api/ruxos_posix_api/src/imp/io_mpx/select.rs index ca35ff3e5..9fe94407c 100644 --- a/api/ruxos_posix_api/src/imp/io_mpx/select.rs +++ b/api/ruxos_posix_api/src/imp/io_mpx/select.rs @@ -100,6 +100,10 @@ impl FdSets { unsafe { set_fd_set(res_write_fds, fd) }; res_num += 1; } + if state.pollhup { + unsafe { set_fd_set(res_except_fds, fd) }; + res_num += 1; + } } Err(e) => { debug!(" except: {} {:?}", fd, e); diff --git a/api/ruxos_posix_api/src/imp/pipe.rs b/api/ruxos_posix_api/src/imp/pipe.rs index 98110dbf7..b81c2ee5f 100644 --- a/api/ruxos_posix_api/src/imp/pipe.rs +++ b/api/ruxos_posix_api/src/imp/pipe.rs @@ -25,7 +25,7 @@ enum RingBufferStatus { Normal, } -const RING_BUFFER_SIZE: usize = 256; +const RING_BUFFER_SIZE: usize = ruxconfig::PIPE_BUFFER_SIZE; pub struct PipeRingBuffer { arr: [u8; RING_BUFFER_SIZE], @@ -210,10 +210,12 @@ impl FileLike for Pipe { } fn poll(&self) -> LinuxResult { + let write_end_count = Arc::weak_count(&self.buffer); let buf = self.buffer.lock(); Ok(PollState { readable: self.readable() && buf.available_read() > 0, writable: self.writable() && buf.available_write() > 0, + pollhup: self.write_end_close(), }) } diff --git a/api/ruxos_posix_api/src/imp/stdio.rs b/api/ruxos_posix_api/src/imp/stdio.rs index 73af31e31..dffece5f2 100644 --- a/api/ruxos_posix_api/src/imp/stdio.rs +++ b/api/ruxos_posix_api/src/imp/stdio.rs @@ -163,6 +163,7 @@ impl ruxfdtable::FileLike for Stdin { Ok(PollState { readable: true, writable: true, + pollhup: false, }) } @@ -204,6 +205,7 @@ impl ruxfdtable::FileLike for Stdout { Ok(PollState { readable: true, writable: true, + pollhup: false, }) } diff --git a/crates/axio/src/lib.rs b/crates/axio/src/lib.rs index 6a34e59f3..b42ae626e 100644 --- a/crates/axio/src/lib.rs +++ b/crates/axio/src/lib.rs @@ -266,4 +266,6 @@ pub struct PollState { pub readable: bool, /// Object can be writen now. pub writable: bool, + /// Object is closed (by remote) now. + pub pollhup: bool, } diff --git a/modules/ruxconfig/defconfig.toml b/modules/ruxconfig/defconfig.toml index bda9cc8df..8d6b802e8 100644 --- a/modules/ruxconfig/defconfig.toml +++ b/modules/ruxconfig/defconfig.toml @@ -43,3 +43,6 @@ smp = "1" # Maximum number of keys per thread. pthread-key-max = "1024" + +# Pipe channel bufer size. +pipe-buffer-size = "0x10000" diff --git a/modules/ruxnet/src/lwip_impl/tcp.rs b/modules/ruxnet/src/lwip_impl/tcp.rs index 15a09961f..c3abfe3ee 100644 --- a/modules/ruxnet/src/lwip_impl/tcp.rs +++ b/modules/ruxnet/src/lwip_impl/tcp.rs @@ -13,7 +13,7 @@ use lwip_rust::bindings::{ err_enum_t_ERR_MEM, err_enum_t_ERR_OK, err_enum_t_ERR_USE, err_enum_t_ERR_VAL, err_t, ip_addr_t, pbuf, pbuf_free, tcp_accept, tcp_arg, tcp_bind, tcp_close, tcp_connect, tcp_listen_with_backlog, tcp_new, tcp_output, tcp_pcb, tcp_recv, tcp_recved, tcp_state_CLOSED, - tcp_state_LISTEN, tcp_write, TCP_DEFAULT_LISTEN_BACKLOG, TCP_MSS, + tcp_state_CLOSE_WAIT, tcp_state_LISTEN, tcp_write, TCP_DEFAULT_LISTEN_BACKLOG, TCP_MSS, }; use ruxtask::yield_now; @@ -475,6 +475,7 @@ impl TcpSocket { Ok(PollState { readable: self.inner.accept_queue.lock().len() != 0, writable: false, + pollhup: false, }) } else { let test = self.inner.recv_queue.lock().len(); @@ -482,6 +483,7 @@ impl TcpSocket { Ok(PollState { readable: self.inner.recv_queue.lock().len() != 0, writable: true, + pollhup: unsafe { (*self.pcb.get()).state } == tcp_state_CLOSE_WAIT, }) } } diff --git a/modules/ruxnet/src/lwip_impl/udp.rs b/modules/ruxnet/src/lwip_impl/udp.rs index b56437946..0c5046517 100644 --- a/modules/ruxnet/src/lwip_impl/udp.rs +++ b/modules/ruxnet/src/lwip_impl/udp.rs @@ -335,6 +335,7 @@ impl UdpSocket { Ok(PollState { readable: self.inner.recv_queue.lock().len() != 0, writable: true, + pollhup: false, }) } } diff --git a/modules/ruxnet/src/smoltcp_impl/tcp.rs b/modules/ruxnet/src/smoltcp_impl/tcp.rs index 2634b8e25..02613ead5 100644 --- a/modules/ruxnet/src/smoltcp_impl/tcp.rs +++ b/modules/ruxnet/src/smoltcp_impl/tcp.rs @@ -382,6 +382,7 @@ impl TcpSocket { _ => Ok(PollState { readable: false, writable: false, + pollhup: false, }), } } @@ -482,16 +483,21 @@ impl TcpSocket { Ok(PollState { readable: false, writable, + pollhup: false, }) } fn poll_stream(&self) -> AxResult { // SAFETY: `self.handle` should be initialized in a connected socket. let handle = unsafe { self.handle.get().read().unwrap() }; + let pollhup = SOCKET_SET.with_socket_mut::(handle, |socket| { + socket.state() == tcp::State::CloseWait + }); SOCKET_SET.with_socket::(handle, |socket| { Ok(PollState { readable: !socket.may_recv() || socket.can_recv(), writable: !socket.may_send() || socket.can_send(), + pollhup, }) }) } @@ -502,6 +508,7 @@ impl TcpSocket { Ok(PollState { readable: LISTEN_TABLE.can_accept(local_addr.port)?, writable: false, + pollhup: false, }) } diff --git a/modules/ruxnet/src/smoltcp_impl/udp.rs b/modules/ruxnet/src/smoltcp_impl/udp.rs index 5bf098166..db1692ca8 100644 --- a/modules/ruxnet/src/smoltcp_impl/udp.rs +++ b/modules/ruxnet/src/smoltcp_impl/udp.rs @@ -195,12 +195,14 @@ impl UdpSocket { return Ok(PollState { readable: false, writable: false, + pollhup: false, }); } SOCKET_SET.with_socket_mut::(self.handle, |socket| { Ok(PollState { readable: socket.can_recv(), writable: socket.can_send(), + pollhup: false, }) }) } diff --git a/modules/ruxnet/src/unix.rs b/modules/ruxnet/src/unix.rs index 9136090b8..e78dd0072 100644 --- a/modules/ruxnet/src/unix.rs +++ b/modules/ruxnet/src/unix.rs @@ -471,6 +471,7 @@ impl UnixSocket { Ok(PollState { readable: false, writable, + pollhup: false, }) } @@ -480,11 +481,25 @@ impl UnixSocket { match now_state { UnixSocketStatus::Connecting => self.poll_connect(), UnixSocketStatus::Connected => { + let remote_is_close = { + let remote_handle = self.get_peerhandle(); + match remote_handle { + Some(handle) => { + let mut binding = UNIX_TABLE.write(); + let mut remote_status = binding.get_mut(handle).unwrap().lock().get_state(); + remote_status == UnixSocketStatus::Closed + } + None => { + return Err(LinuxError::ENOTCONN); + } + } + }; let mut binding = UNIX_TABLE.write(); let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock(); Ok(PollState { readable: !socket_inner.may_recv() || socket_inner.can_recv(), writable: !socket_inner.may_send() || socket_inner.can_send(), + pollhup: remote_is_close, }) } UnixSocketStatus::Listening => { @@ -493,11 +508,13 @@ impl UnixSocket { Ok(PollState { readable: socket_inner.can_accept(), writable: false, + pollhup: false, }) } _ => Ok(PollState { readable: false, writable: false, + pollhup: false, }), } } diff --git a/modules/ruxtask/src/fs.rs b/modules/ruxtask/src/fs.rs index b603d237d..e6db7e567 100644 --- a/modules/ruxtask/src/fs.rs +++ b/modules/ruxtask/src/fs.rs @@ -127,6 +127,7 @@ impl FileLike for File { Ok(PollState { readable: true, writable: true, + pollhup: false, }) } @@ -197,6 +198,7 @@ impl FileLike for Directory { Ok(PollState { readable: true, writable: true, + pollhup: false, }) } diff --git a/platforms/aarch64-qemu-virt.toml b/platforms/aarch64-qemu-virt.toml index 03474a9f8..f98e3a878 100644 --- a/platforms/aarch64-qemu-virt.toml +++ b/platforms/aarch64-qemu-virt.toml @@ -90,3 +90,6 @@ gicd-paddr = "0x0800_0000" # PSCI psci-method = "hvc" + +# Pipe channel bufer size. +pipe-buffer-size = "0x10000" diff --git a/platforms/aarch64-raspi4.toml b/platforms/aarch64-raspi4.toml index ef2e20cc8..7b8a7fcf7 100644 --- a/platforms/aarch64-raspi4.toml +++ b/platforms/aarch64-raspi4.toml @@ -34,3 +34,6 @@ uart-irq = "0x79" # GIC Address gicc-paddr = "0xFF84_2000" gicd-paddr = "0xFF84_1000" + +# Pipe channel bufer size. +pipe-buffer-size = "0x10000" \ No newline at end of file diff --git a/platforms/riscv64-qemu-virt.toml b/platforms/riscv64-qemu-virt.toml index 3589913f2..791cf0899 100644 --- a/platforms/riscv64-qemu-virt.toml +++ b/platforms/riscv64-qemu-virt.toml @@ -59,3 +59,6 @@ pci-ranges = [ # Timer interrupt frequency in Hz. timer-frequency = "10_000_000" # 10MHz + +# Pipe channel bufer size. +pipe-buffer-size = "0x10000" diff --git a/platforms/x86_64-qemu-q35.toml b/platforms/x86_64-qemu-q35.toml index a3a119758..21e9b30d3 100644 --- a/platforms/x86_64-qemu-q35.toml +++ b/platforms/x86_64-qemu-q35.toml @@ -40,3 +40,6 @@ pci-ranges = [] # Timer interrupt frequencyin Hz. timer-frequency = "4_000_000_000" # 4.0GHz + +# Pipe channel bufer size. +pipe-buffer-size = "0x10000" From c75a5d9ed4ffe156a8893a65363c41115dd594af Mon Sep 17 00:00:00 2001 From: WuZheng Date: Thu, 26 Dec 2024 13:09:54 +0800 Subject: [PATCH 14/15] remove `ruxlibc` from ulibc. --- .github/workflows/build.yml | 6 +- .github/workflows/docs.yml | 2 +- .github/workflows/test.yml | 4 +- Cargo.lock | 18 +- Cargo.toml | 1 - Makefile | 10 +- README.md | 2 +- api/arceos_api/Cargo.toml | 2 + api/arceos_api/src/imp/mod.rs | 2 + .../arceos_api/src/imp/trap.rs | 15 +- api/ruxos_posix_api/Cargo.toml | 2 +- api/ruxos_posix_api/build.rs | 6 +- api/ruxos_posix_api/src/imp/fs.rs | 2 +- api/ruxos_posix_api/src/imp/mmap/api.rs | 2 +- api/ruxos_posix_api/src/imp/mmap/trap.rs | 6 +- api/ruxos_posix_api/src/imp/mmap/utils.rs | 21 +- api/ruxos_posix_api/src/imp/pipe.rs | 1 - api/ruxos_posix_api/src/imp/pthread/mod.rs | 57 +- api/ruxos_posix_api/src/imp/task.rs | 70 +- api/ruxos_posix_api/src/lib.rs | 4 +- apps/c/envtest/expect_info.out | 1 - apps/c/httpclient/features.txt | 2 + apps/c/memtest/expect_trace.out | 55 +- .../c/pthread/basic/expect_info_smp4_fifo.out | 1 - apps/c/pthread/basic/features.txt | 1 + .../parallel/expect_info_smp4_fifo.out | 26 - .../pthread/parallel/expect_info_smp4_rr.out | 27 - apps/c/pthread/parallel/features.txt | 1 + apps/c/pthread/parallel/test_cmd | 4 +- apps/c/pthread/pipe/expect_info_smp4_fifo.out | 1 - apps/c/pthread/pipe/features.txt | 1 + .../c/pthread/sleep/expect_info_smp4_fifo.out | 1 - apps/c/pthread/sleep/features.txt | 1 + apps/c/pthread/tsd/expect_info_smp4_fifo.out | 1 - apps/c/pthread/tsd/features.txt | 1 + modules/ruxdriver/src/virtio.rs | 19 +- modules/ruxfs/src/root.rs | 23 +- modules/ruxhal/Cargo.toml | 4 +- modules/ruxhal/src/arch/x86_64/mod.rs | 1 - modules/ruxhal/src/mem.rs | 2 + modules/ruxhal/src/platform/x86_pc/rtc.rs | 6 + modules/ruxmm/src/lib.rs | 3 + modules/ruxmm/src/mem.rs | 2 + modules/ruxnet/src/lwip_impl/addr.rs | 13 +- modules/ruxnet/src/lwip_impl/dns.rs | 2 +- modules/ruxnet/src/lwip_impl/driver.rs | 19 +- modules/ruxnet/src/lwip_impl/mod.rs | 4 +- modules/ruxnet/src/lwip_impl/tcp.rs | 6 +- modules/ruxnet/src/lwip_impl/udp.rs | 4 +- modules/ruxnet/src/unix.rs | 9 +- modules/ruxruntime/Cargo.toml | 20 +- .../resources/create_test_img.sh | 0 .../{ruxfs => ruxruntime}/resources/fat16.img | Bin .../tests/test_common/mod.rs | 0 .../{ruxfs => ruxruntime}/tests/test_fatfs.rs | 0 .../{ruxfs => ruxruntime}/tests/test_ramfs.rs | 1 + modules/ruxtask/Cargo.toml | 9 +- modules/ruxtask/src/api.rs | 2 +- modules/ruxtask/src/fs.rs | 73 +- modules/ruxtask/src/lib.rs | 2 +- modules/ruxtask/src/run_queue.rs | 16 +- modules/ruxtask/src/signal.rs | 7 +- modules/ruxtask/src/task.rs | 68 +- modules/ruxtask/src/vma.rs | 11 + scripts/make/build.mk | 6 +- scripts/make/build_c.mk | 4 +- scripts/make/cargo.mk | 2 +- scripts/make/features.mk | 15 +- scripts/make/test.mk | 6 +- scripts/test/app_test.sh | 2 +- ulib/axstd/Cargo.toml | 2 +- ulib/include/.gitignore | 2 + ulib/{ruxlibc => }/include/arpa/inet.h | 0 ulib/{ruxlibc => }/include/assert.h | 0 ulib/{ruxlibc => }/include/byteswap.h | 0 ulib/{ruxlibc => }/include/crypt.h | 0 ulib/{ruxlibc => }/include/ctype.h | 0 ulib/{ruxlibc => }/include/dirent.h | 0 ulib/{ruxlibc => }/include/dlfcn.h | 0 ulib/{ruxlibc => }/include/endian.h | 0 ulib/{ruxlibc => }/include/errno.h | 0 ulib/{ruxlibc => }/include/fcntl.h | 0 ulib/{ruxlibc => }/include/features.h | 0 ulib/{ruxlibc => }/include/float.h | 0 ulib/{ruxlibc => }/include/fnmatch.h | 0 ulib/{ruxlibc => }/include/glob.h | 0 ulib/{ruxlibc => }/include/grp.h | 0 ulib/{ruxlibc => }/include/inttypes.h | 0 ulib/{ruxlibc => }/include/ksigaction.h | 0 ulib/{ruxlibc => }/include/langinfo.h | 0 ulib/{ruxlibc => }/include/libgen.h | 0 ulib/{ruxlibc => }/include/limits.h | 0 ulib/{ruxlibc => }/include/locale.h | 0 ulib/{ruxlibc => }/include/malloc.h | 0 ulib/{ruxlibc => }/include/math.h | 0 ulib/{ruxlibc => }/include/memory.h | 0 ulib/{ruxlibc => }/include/netdb.h | 0 ulib/{ruxlibc => }/include/netinet/in.h | 0 ulib/{ruxlibc => }/include/netinet/tcp.h | 0 ulib/{ruxlibc => }/include/nscd.h | 0 ulib/{ruxlibc => }/include/poll.h | 0 ulib/{ruxlibc => }/include/pthread.h | 0 ulib/{ruxlibc => }/include/pwd.h | 0 ulib/{ruxlibc => }/include/regex.h | 0 ulib/{ruxlibc => }/include/sched.h | 0 ulib/{ruxlibc => }/include/semaphore.h | 0 ulib/{ruxlibc => }/include/setjmp.h | 0 ulib/{ruxlibc => }/include/signal.h | 0 ulib/{ruxlibc => }/include/stdarg.h | 0 ulib/{ruxlibc => }/include/stdbool.h | 0 ulib/{ruxlibc => }/include/stddef.h | 0 ulib/{ruxlibc => }/include/stdint.h | 0 ulib/{ruxlibc => }/include/stdio.h | 0 ulib/{ruxlibc => }/include/stdlib.h | 0 ulib/{ruxlibc => }/include/string.h | 0 ulib/{ruxlibc => }/include/strings.h | 0 ulib/{ruxlibc => }/include/sys/epoll.h | 0 ulib/{ruxlibc => }/include/sys/eventfd.h | 0 ulib/{ruxlibc => }/include/sys/file.h | 0 ulib/{ruxlibc => }/include/sys/ioctl.h | 0 ulib/{ruxlibc => }/include/sys/mman.h | 0 ulib/{ruxlibc => }/include/sys/param.h | 0 ulib/{ruxlibc => }/include/sys/prctl.h | 0 ulib/{ruxlibc => }/include/sys/random.h | 0 ulib/{ruxlibc => }/include/sys/resource.h | 0 ulib/{ruxlibc => }/include/sys/select.h | 0 ulib/{ruxlibc => }/include/sys/sendfile.h | 0 ulib/{ruxlibc => }/include/sys/socket.h | 0 ulib/{ruxlibc => }/include/sys/stat.h | 0 ulib/{ruxlibc => }/include/sys/statfs.h | 0 ulib/{ruxlibc => }/include/sys/statvfs.h | 0 ulib/{ruxlibc => }/include/sys/syscall.h | 0 ulib/{ruxlibc => }/include/sys/sysinfo.h | 0 ulib/{ruxlibc => }/include/sys/time.h | 0 ulib/{ruxlibc => }/include/sys/types.h | 0 ulib/{ruxlibc => }/include/sys/uio.h | 0 ulib/{ruxlibc => }/include/sys/un.h | 0 ulib/{ruxlibc => }/include/sys/utsname.h | 0 ulib/{ruxlibc => }/include/sys/vfs.h | 0 ulib/{ruxlibc => }/include/sys/wait.h | 0 ulib/{ruxlibc => }/include/syslog.h | 0 ulib/{ruxlibc => }/include/termios.h | 0 ulib/{ruxlibc => }/include/time.h | 0 ulib/{ruxlibc => }/include/unistd.h | 0 ulib/ruxlibc/.gitignore | 4 - ulib/ruxlibc/Cargo.toml | 67 - ulib/ruxlibc/build.rs | 33 - ulib/ruxlibc/c/assert.c | 17 - ulib/ruxlibc/c/ctype.c | 26 - ulib/ruxlibc/c/dirent.c | 107 -- ulib/ruxlibc/c/dlfcn.c | 48 - ulib/ruxlibc/c/fcntl.c | 71 - ulib/ruxlibc/c/flock.c | 18 - ulib/ruxlibc/c/fnmatch.c | 304 ---- ulib/ruxlibc/c/glob.c | 338 ---- ulib/ruxlibc/c/grp.c | 46 - ulib/ruxlibc/c/ioctl.c | 27 - ulib/ruxlibc/c/libgen.c | 42 - ulib/ruxlibc/c/libm.c | 26 - ulib/ruxlibc/c/libm.h | 242 --- ulib/ruxlibc/c/locale.c | 51 - ulib/ruxlibc/c/log.c | 404 ----- ulib/ruxlibc/c/math.c | 580 ------- ulib/ruxlibc/c/network.c | 243 --- ulib/ruxlibc/c/nscd_query.c | 103 -- ulib/ruxlibc/c/pow.c | 824 --------- ulib/ruxlibc/c/prctl.c | 19 - ulib/ruxlibc/c/printf.c | 1491 ----------------- ulib/ruxlibc/c/printf.h | 224 --- ulib/ruxlibc/c/printf_config.h | 23 - ulib/ruxlibc/c/pthread.c | 80 - ulib/ruxlibc/c/pwd.c | 52 - ulib/ruxlibc/c/resource.c | 27 - ulib/ruxlibc/c/sched.c | 24 - ulib/ruxlibc/c/select.c | 26 - ulib/ruxlibc/c/semaphore.c | 39 - ulib/ruxlibc/c/sendfile.c | 16 - ulib/ruxlibc/c/signal.c | 101 -- ulib/ruxlibc/c/socket.c | 94 -- ulib/ruxlibc/c/stat.c | 40 - ulib/ruxlibc/c/statfs.c | 18 - ulib/ruxlibc/c/stdio.c | 469 ------ ulib/ruxlibc/c/stdlib.c | 691 -------- ulib/ruxlibc/c/string.c | 473 ------ ulib/ruxlibc/c/syslog.c | 25 - ulib/ruxlibc/c/time.c | 216 --- ulib/ruxlibc/c/uio.c | 32 - ulib/ruxlibc/c/unistd.c | 236 --- ulib/ruxlibc/c/utsname.c | 18 - ulib/ruxlibc/c/wait.c | 26 - ulib/ruxlibc/ctypes.h | 11 - ulib/ruxlibc/src/env.rs | 140 -- ulib/ruxlibc/src/errno.rs | 48 - ulib/ruxlibc/src/fd_ops.rs | 63 - ulib/ruxlibc/src/fs.rs | 109 -- ulib/ruxlibc/src/io.rs | 54 - ulib/ruxlibc/src/io_mpx.rs | 76 - ulib/ruxlibc/src/lib.rs | 152 -- ulib/ruxlibc/src/malloc.rs | 65 - ulib/ruxlibc/src/mktime.rs | 67 - ulib/ruxlibc/src/mmap.rs | 64 - ulib/ruxlibc/src/net.rs | 198 --- ulib/ruxlibc/src/pipe.rs | 23 - ulib/ruxlibc/src/pthread.rs | 157 -- ulib/ruxlibc/src/rand.rs | 39 - ulib/ruxlibc/src/resource.rs | 29 - ulib/ruxlibc/src/setjmp.rs | 247 --- ulib/ruxlibc/src/signal.rs | 79 - ulib/ruxlibc/src/strftime.rs | 263 --- ulib/ruxlibc/src/string.rs | 29 - ulib/ruxlibc/src/strtod.rs | 140 -- ulib/ruxlibc/src/sys.rs | 49 - ulib/ruxlibc/src/time.rs | 87 - ulib/ruxlibc/src/unistd.rs | 87 - ulib/ruxmusl/src/aarch64/mod.rs | 4 + ulib/ruxmusl/src/aarch64/syscall_id.rs | 2 + ulib/ruxmusl/src/trap.rs | 3 + 217 files changed, 435 insertions(+), 10653 deletions(-) rename ulib/ruxlibc/src/utils.rs => api/arceos_api/src/imp/trap.rs (62%) rename modules/{ruxfs => ruxruntime}/resources/create_test_img.sh (100%) rename modules/{ruxfs => ruxruntime}/resources/fat16.img (100%) rename modules/{ruxfs => ruxruntime}/tests/test_common/mod.rs (100%) rename modules/{ruxfs => ruxruntime}/tests/test_fatfs.rs (100%) rename modules/{ruxfs => ruxruntime}/tests/test_ramfs.rs (99%) create mode 100644 ulib/include/.gitignore rename ulib/{ruxlibc => }/include/arpa/inet.h (100%) rename ulib/{ruxlibc => }/include/assert.h (100%) rename ulib/{ruxlibc => }/include/byteswap.h (100%) rename ulib/{ruxlibc => }/include/crypt.h (100%) rename ulib/{ruxlibc => }/include/ctype.h (100%) rename ulib/{ruxlibc => }/include/dirent.h (100%) rename ulib/{ruxlibc => }/include/dlfcn.h (100%) rename ulib/{ruxlibc => }/include/endian.h (100%) rename ulib/{ruxlibc => }/include/errno.h (100%) rename ulib/{ruxlibc => }/include/fcntl.h (100%) rename ulib/{ruxlibc => }/include/features.h (100%) rename ulib/{ruxlibc => }/include/float.h (100%) rename ulib/{ruxlibc => }/include/fnmatch.h (100%) rename ulib/{ruxlibc => }/include/glob.h (100%) rename ulib/{ruxlibc => }/include/grp.h (100%) rename ulib/{ruxlibc => }/include/inttypes.h (100%) rename ulib/{ruxlibc => }/include/ksigaction.h (100%) rename ulib/{ruxlibc => }/include/langinfo.h (100%) rename ulib/{ruxlibc => }/include/libgen.h (100%) rename ulib/{ruxlibc => }/include/limits.h (100%) rename ulib/{ruxlibc => }/include/locale.h (100%) rename ulib/{ruxlibc => }/include/malloc.h (100%) rename ulib/{ruxlibc => }/include/math.h (100%) rename ulib/{ruxlibc => }/include/memory.h (100%) rename ulib/{ruxlibc => }/include/netdb.h (100%) rename ulib/{ruxlibc => }/include/netinet/in.h (100%) rename ulib/{ruxlibc => }/include/netinet/tcp.h (100%) rename ulib/{ruxlibc => }/include/nscd.h (100%) rename ulib/{ruxlibc => }/include/poll.h (100%) rename ulib/{ruxlibc => }/include/pthread.h (100%) rename ulib/{ruxlibc => }/include/pwd.h (100%) rename ulib/{ruxlibc => }/include/regex.h (100%) rename ulib/{ruxlibc => }/include/sched.h (100%) rename ulib/{ruxlibc => }/include/semaphore.h (100%) rename ulib/{ruxlibc => }/include/setjmp.h (100%) rename ulib/{ruxlibc => }/include/signal.h (100%) rename ulib/{ruxlibc => }/include/stdarg.h (100%) rename ulib/{ruxlibc => }/include/stdbool.h (100%) rename ulib/{ruxlibc => }/include/stddef.h (100%) rename ulib/{ruxlibc => }/include/stdint.h (100%) rename ulib/{ruxlibc => }/include/stdio.h (100%) rename ulib/{ruxlibc => }/include/stdlib.h (100%) rename ulib/{ruxlibc => }/include/string.h (100%) rename ulib/{ruxlibc => }/include/strings.h (100%) rename ulib/{ruxlibc => }/include/sys/epoll.h (100%) rename ulib/{ruxlibc => }/include/sys/eventfd.h (100%) rename ulib/{ruxlibc => }/include/sys/file.h (100%) rename ulib/{ruxlibc => }/include/sys/ioctl.h (100%) rename ulib/{ruxlibc => }/include/sys/mman.h (100%) rename ulib/{ruxlibc => }/include/sys/param.h (100%) rename ulib/{ruxlibc => }/include/sys/prctl.h (100%) rename ulib/{ruxlibc => }/include/sys/random.h (100%) rename ulib/{ruxlibc => }/include/sys/resource.h (100%) rename ulib/{ruxlibc => }/include/sys/select.h (100%) rename ulib/{ruxlibc => }/include/sys/sendfile.h (100%) rename ulib/{ruxlibc => }/include/sys/socket.h (100%) rename ulib/{ruxlibc => }/include/sys/stat.h (100%) rename ulib/{ruxlibc => }/include/sys/statfs.h (100%) rename ulib/{ruxlibc => }/include/sys/statvfs.h (100%) rename ulib/{ruxlibc => }/include/sys/syscall.h (100%) rename ulib/{ruxlibc => }/include/sys/sysinfo.h (100%) rename ulib/{ruxlibc => }/include/sys/time.h (100%) rename ulib/{ruxlibc => }/include/sys/types.h (100%) rename ulib/{ruxlibc => }/include/sys/uio.h (100%) rename ulib/{ruxlibc => }/include/sys/un.h (100%) rename ulib/{ruxlibc => }/include/sys/utsname.h (100%) rename ulib/{ruxlibc => }/include/sys/vfs.h (100%) rename ulib/{ruxlibc => }/include/sys/wait.h (100%) rename ulib/{ruxlibc => }/include/syslog.h (100%) rename ulib/{ruxlibc => }/include/termios.h (100%) rename ulib/{ruxlibc => }/include/time.h (100%) rename ulib/{ruxlibc => }/include/unistd.h (100%) delete mode 100644 ulib/ruxlibc/.gitignore delete mode 100644 ulib/ruxlibc/Cargo.toml delete mode 100644 ulib/ruxlibc/build.rs delete mode 100644 ulib/ruxlibc/c/assert.c delete mode 100644 ulib/ruxlibc/c/ctype.c delete mode 100644 ulib/ruxlibc/c/dirent.c delete mode 100644 ulib/ruxlibc/c/dlfcn.c delete mode 100644 ulib/ruxlibc/c/fcntl.c delete mode 100644 ulib/ruxlibc/c/flock.c delete mode 100644 ulib/ruxlibc/c/fnmatch.c delete mode 100644 ulib/ruxlibc/c/glob.c delete mode 100644 ulib/ruxlibc/c/grp.c delete mode 100644 ulib/ruxlibc/c/ioctl.c delete mode 100644 ulib/ruxlibc/c/libgen.c delete mode 100644 ulib/ruxlibc/c/libm.c delete mode 100644 ulib/ruxlibc/c/libm.h delete mode 100644 ulib/ruxlibc/c/locale.c delete mode 100644 ulib/ruxlibc/c/log.c delete mode 100644 ulib/ruxlibc/c/math.c delete mode 100644 ulib/ruxlibc/c/network.c delete mode 100644 ulib/ruxlibc/c/nscd_query.c delete mode 100644 ulib/ruxlibc/c/pow.c delete mode 100644 ulib/ruxlibc/c/prctl.c delete mode 100644 ulib/ruxlibc/c/printf.c delete mode 100644 ulib/ruxlibc/c/printf.h delete mode 100644 ulib/ruxlibc/c/printf_config.h delete mode 100644 ulib/ruxlibc/c/pthread.c delete mode 100644 ulib/ruxlibc/c/pwd.c delete mode 100644 ulib/ruxlibc/c/resource.c delete mode 100644 ulib/ruxlibc/c/sched.c delete mode 100644 ulib/ruxlibc/c/select.c delete mode 100644 ulib/ruxlibc/c/semaphore.c delete mode 100644 ulib/ruxlibc/c/sendfile.c delete mode 100644 ulib/ruxlibc/c/signal.c delete mode 100644 ulib/ruxlibc/c/socket.c delete mode 100644 ulib/ruxlibc/c/stat.c delete mode 100644 ulib/ruxlibc/c/statfs.c delete mode 100644 ulib/ruxlibc/c/stdio.c delete mode 100644 ulib/ruxlibc/c/stdlib.c delete mode 100644 ulib/ruxlibc/c/string.c delete mode 100644 ulib/ruxlibc/c/syslog.c delete mode 100644 ulib/ruxlibc/c/time.c delete mode 100644 ulib/ruxlibc/c/uio.c delete mode 100644 ulib/ruxlibc/c/unistd.c delete mode 100644 ulib/ruxlibc/c/utsname.c delete mode 100644 ulib/ruxlibc/c/wait.c delete mode 100644 ulib/ruxlibc/ctypes.h delete mode 100644 ulib/ruxlibc/src/env.rs delete mode 100644 ulib/ruxlibc/src/errno.rs delete mode 100644 ulib/ruxlibc/src/fd_ops.rs delete mode 100644 ulib/ruxlibc/src/fs.rs delete mode 100644 ulib/ruxlibc/src/io.rs delete mode 100644 ulib/ruxlibc/src/io_mpx.rs delete mode 100644 ulib/ruxlibc/src/lib.rs delete mode 100644 ulib/ruxlibc/src/malloc.rs delete mode 100644 ulib/ruxlibc/src/mktime.rs delete mode 100644 ulib/ruxlibc/src/mmap.rs delete mode 100644 ulib/ruxlibc/src/net.rs delete mode 100644 ulib/ruxlibc/src/pipe.rs delete mode 100644 ulib/ruxlibc/src/pthread.rs delete mode 100644 ulib/ruxlibc/src/rand.rs delete mode 100644 ulib/ruxlibc/src/resource.rs delete mode 100644 ulib/ruxlibc/src/setjmp.rs delete mode 100644 ulib/ruxlibc/src/signal.rs delete mode 100644 ulib/ruxlibc/src/strftime.rs delete mode 100644 ulib/ruxlibc/src/string.rs delete mode 100644 ulib/ruxlibc/src/strtod.rs delete mode 100644 ulib/ruxlibc/src/sys.rs delete mode 100644 ulib/ruxlibc/src/time.rs delete mode 100644 ulib/ruxlibc/src/unistd.rs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c68842bf9..6a9c40a02 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,7 +7,7 @@ env: jobs: clippy: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 strategy: fail-fast: false steps: @@ -37,7 +37,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest] + os: [ubuntu-22.04] arch: [x86_64, riscv64, aarch64] steps: - uses: actions/checkout@v3 @@ -94,7 +94,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest] + os: [ubuntu-22.04] arch: [x86_64] steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 68e607319..3e0558e18 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -7,7 +7,7 @@ env: jobs: doc: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 strategy: fail-fast: false permissions: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b78448eb3..014375d5e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,7 +8,7 @@ env: jobs: unit-test: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v3 with: @@ -26,7 +26,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest] + os: [ubuntu-22.04] arch: [x86_64, riscv64, aarch64] steps: - uses: actions/checkout@v3 diff --git a/Cargo.lock b/Cargo.lock index 175ee3a34..06591d4bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,11 +98,13 @@ dependencies = [ "axerrno", "axio", "axlog", + "crate_interface", "ruxconfig", "ruxdisplay", "ruxfeat", "ruxfs", "ruxhal", + "ruxmm", "ruxnet", "ruxruntime", "ruxtask", @@ -1733,17 +1735,6 @@ dependencies = [ "x86_64", ] -[[package]] -name = "ruxlibc" -version = "0.1.0" -dependencies = [ - "axerrno", - "axio", - "bindgen 0.66.1", - "ruxfeat", - "ruxos_posix_api", -] - [[package]] name = "ruxmm" version = "0.1.0" @@ -1873,13 +1864,18 @@ name = "ruxruntime" version = "0.1.0" dependencies = [ "axalloc", + "axfs_ramfs", + "axfs_vfs", + "axio", "axlog", "axsync", "cfg-if", "crate_interface", + "driver_block", "dtb", "kernel_guard", "lazy_init", + "log", "percpu", "rux9p", "ruxconfig", diff --git a/Cargo.toml b/Cargo.toml index fda7d6d92..303a9acdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,7 +59,6 @@ members = [ "api/ruxos_posix_api", "ulib/axstd", - "ulib/ruxlibc", "ulib/ruxmusl", "apps/display/basic_painting", diff --git a/Makefile b/Makefile index e00e726e0..2d76c7945 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ V ?= # App options A ?= apps/c/helloworld APP ?= $(A) -FEATURES ?= multitask paging fs +FEATURES ?= APP_FEATURES ?= # QEMU options @@ -79,7 +79,7 @@ ARGS ?= ENVS ?= # Libc options -MUSL ?= n +MUSL ?= y # App type ifeq ($(wildcard $(APP)),) @@ -250,9 +250,6 @@ doc_check_missing: fmt: cargo fmt --all -fmt_c: - @clang-format --style=file -i $(shell find ulib/ruxlibc -iname '*.c' -o -iname '*.h') - test: $(call app_test) @@ -274,12 +271,11 @@ clean: clean_c clean_musl cargo clean clean_c:: - rm -rf ulib/ruxlibc/build_* rm -rf $(app-objs) clean_musl: rm -rf ulib/ruxmusl/build_* rm -rf ulib/ruxmusl/install -.PHONY: all build disasm run justrun debug clippy fmt fmt_c test test_no_fail_fast clean clean_c\ +.PHONY: all build disasm run justrun debug clippy fmt fmt_c test test_no_fail_fast clean \ clean_musl doc disk_image debug_no_attach prebuild _force diff --git a/README.md b/README.md index 006ca3a32..686c6ff21 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ The currently supported applications and programming languages, as well as their | Language | Description | |- | - | -| C | Run C apps by RuxOS ruxlibc or standard musl libc supported by ruxmusl. Evaluated by libc-bench. | +| C | Run C apps by standard musl libc supported by ruxmusl. Evaluated by libc-bench. | | C++ | Run C++ apps by c++ static library provided by musl libc. Passed c++ benchmark. Evaluated by c++ benchmark. | | [Perl](https://github.com/syswonder/rux-perl) | Run Perl standard library by musl libc. Evaluated by Perl benchmark. | | [Python](https://github.com/syswonder/rux-python3) | Run Python apps by dynamically loading Python modules. Evaluated by Python benchmark. | diff --git a/api/arceos_api/Cargo.toml b/api/arceos_api/Cargo.toml index b65cb69d9..616ce9ffd 100644 --- a/api/arceos_api/Cargo.toml +++ b/api/arceos_api/Cargo.toml @@ -28,9 +28,11 @@ myfs = ["ruxfeat/myfs"] dummy-if-not-enabled = [] [dependencies] +crate_interface = "0.1.1" ruxfeat = { path = "../ruxfeat" } ruxruntime = { path = "../../modules/ruxruntime" } ruxconfig = { path = "../../modules/ruxconfig" } +ruxmm = {path = "../../modules/ruxmm"} axlog = { path = "../../modules/axlog" } axio = { path = "../../crates/axio" } axerrno = { path = "../../crates/axerrno" } diff --git a/api/arceos_api/src/imp/mod.rs b/api/arceos_api/src/imp/mod.rs index c4d2e01ec..7a34fe345 100644 --- a/api/arceos_api/src/imp/mod.rs +++ b/api/arceos_api/src/imp/mod.rs @@ -9,6 +9,8 @@ mod mem; mod task; +#[cfg(feature = "paging")] +mod trap; cfg_fs! { mod fs; diff --git a/ulib/ruxlibc/src/utils.rs b/api/arceos_api/src/imp/trap.rs similarity index 62% rename from ulib/ruxlibc/src/utils.rs rename to api/arceos_api/src/imp/trap.rs index 14d05e5cf..bb90964ee 100644 --- a/ulib/ruxlibc/src/utils.rs +++ b/api/arceos_api/src/imp/trap.rs @@ -7,13 +7,14 @@ * See the Mulan PSL v2 for more details. */ -use core::ffi::c_int; +use ruxhal::trap::{PageFaultCause, TrapHandler}; -pub fn e(ret: c_int) -> c_int { - if ret < 0 { - crate::errno::set_errno(ret.abs()); - -1 - } else { - ret as _ +struct TrapHandlerImpl; + +#[crate_interface::impl_interface] +impl TrapHandler for TrapHandlerImpl { + fn handle_page_fault(vaddr: usize, cause: PageFaultCause) -> bool { + // TODO: handle page fault + panic!("Page fault at {:#x} with cause {:?}.", vaddr, cause); } } diff --git a/api/ruxos_posix_api/Cargo.toml b/api/ruxos_posix_api/Cargo.toml index 4fe38279f..3ff20b170 100644 --- a/api/ruxos_posix_api/Cargo.toml +++ b/api/ruxos_posix_api/Cargo.toml @@ -47,7 +47,7 @@ ruxfdtable = { path = "../../modules/ruxfdtable" } ruxmm = { path = "../../modules/ruxmm", optional = true } ruxfutex = { path = "../../modules/ruxfutex", optional = true } axalloc = { path = "../../modules/axalloc", optional = true } -ruxtask = { path = "../../modules/ruxtask", optional = true } +ruxtask = { path = "../../modules/ruxtask", features = ["notest"], optional = true } ruxfs = { path = "../../modules/ruxfs", optional = true } ruxnet = { path = "../../modules/ruxnet", optional = true } diff --git a/api/ruxos_posix_api/build.rs b/api/ruxos_posix_api/build.rs index 0529c64e6..76c151157 100644 --- a/api/ruxos_posix_api/build.rs +++ b/api/ruxos_posix_api/build.rs @@ -148,7 +148,7 @@ typedef struct {{ let mut builder = bindgen::Builder::default() .header(in_file) - .clang_arg("-I./../../ulib/ruxlibc/include") + .clang_arg("-I./../../ulib/include") .parse_callbacks(Box::new(MyCallbacks)) .derive_default(true) .size_t_is_usize(false) @@ -167,7 +167,7 @@ typedef struct {{ .expect("Couldn't write bindings!"); } - gen_pthread_mutex("../../ulib/ruxlibc/include/ax_pthread_mutex.h").unwrap(); - gen_pthread_cond("../../ulib/ruxlibc/include/ax_pthread_cond.h").unwrap(); + gen_pthread_mutex("../../ulib/include/ax_pthread_mutex.h").unwrap(); + gen_pthread_cond("../../ulib/include/ax_pthread_cond.h").unwrap(); gen_c_to_rust_bindings("ctypes.h", "src/ctypes_gen.rs"); } diff --git a/api/ruxos_posix_api/src/imp/fs.rs b/api/ruxos_posix_api/src/imp/fs.rs index 745c8d5a4..3b3e399aa 100644 --- a/api/ruxos_posix_api/src/imp/fs.rs +++ b/api/ruxos_posix_api/src/imp/fs.rs @@ -28,7 +28,7 @@ struct InitFsImpl; #[crate_interface::impl_interface] impl ruxtask::fs::InitFs for InitFsImpl { - fn init(fs: &mut ruxtask::fs::FileSystem) { + fn add_stdios_to_fd_table(fs: &mut ruxtask::fs::FileSystem) { debug!("init initial process's fd_table"); let fd_table = &mut fs.fd_table; fd_table.add_at(0, Arc::new(stdin()) as _).unwrap(); // stdin diff --git a/api/ruxos_posix_api/src/imp/mmap/api.rs b/api/ruxos_posix_api/src/imp/mmap/api.rs index e70acca02..447a63ad0 100644 --- a/api/ruxos_posix_api/src/imp/mmap/api.rs +++ b/api/ruxos_posix_api/src/imp/mmap/api.rs @@ -307,7 +307,7 @@ pub fn sys_msync(start: *mut c_void, len: ctypes::size_t, flags: c_int) -> c_int for (&vaddr, page_info) in current().mm.mem_map.lock().range(start..end) { if let Some(FileInfo { file, offset, size }) = &page_info.mapping_file { let src = vaddr as *mut u8; - write_into(&file, src, *offset as u64, *size); + write_into(file, src, *offset as u64, *size); } } } diff --git a/api/ruxos_posix_api/src/imp/mmap/trap.rs b/api/ruxos_posix_api/src/imp/mmap/trap.rs index c8fb1d693..cfa059757 100644 --- a/api/ruxos_posix_api/src/imp/mmap/trap.rs +++ b/api/ruxos_posix_api/src/imp/mmap/trap.rs @@ -205,10 +205,10 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl { dst.copy_from(vaddr as *mut u8, size); } let paddr = direct_virt_to_phys(fake_vaddr); - let mapping_file = memory_map.get(&vaddr.into()).unwrap().mapping_file.clone(); - memory_map.remove(&vaddr.into()); + let mapping_file = memory_map.get(&vaddr).unwrap().mapping_file.clone(); + memory_map.remove(&vaddr); memory_map.insert( - vaddr.into(), + vaddr, Arc::new(PageInfo { paddr, #[cfg(feature = "fs")] diff --git a/api/ruxos_posix_api/src/imp/mmap/utils.rs b/api/ruxos_posix_api/src/imp/mmap/utils.rs index 582a1dcdf..89ee4a9a2 100644 --- a/api/ruxos_posix_api/src/imp/mmap/utils.rs +++ b/api/ruxos_posix_api/src/imp/mmap/utils.rs @@ -212,7 +212,7 @@ pub(crate) fn release_pages_mapped(start: usize, end: usize) { #[cfg(feature = "fs")] if let Some(FileInfo { file, offset, size }) = &page_info.mapping_file { let src = vaddr as *mut u8; - write_into(&file, src, *offset as u64, *size); + write_into(file, src, *offset as u64, *size); } if pte_unmap_page(VirtAddr::from(vaddr)).is_err() { panic!("Release page failed when munmapping!"); @@ -294,7 +294,7 @@ pub(crate) fn shift_mapped_page(start: usize, end: usize, vma_offset: usize, cop used_fs! { let mut opt_buffer = Vec::new(); - for (&start, &ref off_in_swap) in swaped_map.range(start..end) { + for (&start, off_in_swap) in swaped_map.range(start..end) { opt_buffer.push((start, off_in_swap.clone())); } for (start, swap_info) in opt_buffer { @@ -334,7 +334,7 @@ pub(crate) fn preload_page_with_swap( // For file mapping, the mapped content will be written directly to the original file. Some(FileInfo { file, offset, size }) => { let offset = *offset as u64; - write_into(&file, vaddr_swapped as *mut u8, offset, *size); + write_into(file, vaddr_swapped as *mut u8, offset, *size); pte_swap_preload(VirtAddr::from(vaddr_swapped)).unwrap() } // For anonymous mapping, you need to save the mapped memory to the prepared swap file, @@ -354,21 +354,6 @@ pub(crate) fn preload_page_with_swap( } } } - // For anonymous mapping, you need to save the mapped memory to the prepared swap file, - // and record the memory address and its offset in the swap file. - // Some((vaddr_swapped, PageInfo{paddr:_, mapping_file:Some(FileInfo{file, offset, size})})) => { - // let offset_get = off_pool.pop(); - // let offset = offset_get.unwrap(); - // swaped_map.insert(vaddr_swapped, Arc::new(offset)); - - // write_into( - // &SWAP_FILE, - // vaddr_swapped as *mut u8, - // offset as u64, - // PAGE_SIZE_4K, - // ); - // pte_swap_preload(VirtAddr::from(vaddr_swapped)).unwrap() - // } _ => panic!("No memory for mmap, check if huge memory leaky exists"), }, diff --git a/api/ruxos_posix_api/src/imp/pipe.rs b/api/ruxos_posix_api/src/imp/pipe.rs index bb0ab0184..afc56c613 100644 --- a/api/ruxos_posix_api/src/imp/pipe.rs +++ b/api/ruxos_posix_api/src/imp/pipe.rs @@ -117,7 +117,6 @@ impl Pipe { pub fn write_end_close(&self) -> bool { let write_end_count = Arc::weak_count(&self.buffer); - // error!("Pipe::write_end_close <= buffer: {:#?} {:#?}", write_end_count, Arc::as_ptr(&self.buffer)); write_end_count == 0 } } diff --git a/api/ruxos_posix_api/src/imp/pthread/mod.rs b/api/ruxos_posix_api/src/imp/pthread/mod.rs index ce79a2be2..3ea951f5a 100644 --- a/api/ruxos_posix_api/src/imp/pthread/mod.rs +++ b/api/ruxos_posix_api/src/imp/pthread/mod.rs @@ -260,10 +260,7 @@ unsafe impl Send for ForceSendSync {} unsafe impl Sync for ForceSendSync {} /// Create new thread by `sys_clone`, return new thread ID -#[cfg(all( - feature = "musl", - any(target_arch = "aarch64", target_arch = "riscv64") -))] +#[cfg(all(feature = "musl", target_arch = "aarch64"))] pub unsafe fn sys_clone( flags: c_int, stack: *mut c_void, @@ -332,6 +329,58 @@ pub unsafe fn sys_clone( }) } +/// Create new thread by `sys_clone`, return new thread ID +#[cfg(all(feature = "musl", target_arch = "riscv64"))] +pub unsafe fn sys_clone( + flags: c_int, + stack: *mut c_void, + ptid: *mut ctypes::pid_t, + tls: *mut c_void, + ctid: *mut ctypes::pid_t, +) -> c_int { + debug!( + "sys_clone <= flags: {:x}, stack: {:p}, ctid: {:x}", + flags, stack, ctid as usize + ); + + syscall_body!(sys_clone, { + if (flags as u32 & ctypes::CLONE_THREAD) != 0 { + let func = unsafe { + core::mem::transmute::<*const (), extern "C" fn(arg: *mut c_void) -> *mut c_void>( + (*(stack as *mut usize)) as *const (), + ) + }; + let args = unsafe { *((stack as usize + 8) as *mut usize) } as *mut c_void; + + let set_tid = if (flags as u32 & ctypes::CLONE_CHILD_SETTID) != 0 { + core::sync::atomic::AtomicU64::new(ctid as _) + } else { + core::sync::atomic::AtomicU64::new(0) + }; + + let (tid, task_inner) = Pthread::pcreate( + core::ptr::null(), + func, + args, + tls, + set_tid, + core::sync::atomic::AtomicU64::from(ctid as u64), + )?; + + // write tid to ptid + if (flags as u32 & ctypes::CLONE_PARENT_SETTID) != 0 { + unsafe { *ptid = tid as c_int }; + } + ruxtask::put_task(task_inner); + + return Ok(tid); + } else { + debug!("ONLY support CLONE_THREAD and SIGCHLD"); + return Err(LinuxError::EINVAL); + } + }) +} + /// Create new thread by `sys_clone`, return new thread ID #[cfg(all(feature = "musl", target_arch = "x86_64"))] pub unsafe fn sys_clone( diff --git a/api/ruxos_posix_api/src/imp/task.rs b/api/ruxos_posix_api/src/imp/task.rs index ae07be44b..a81854bac 100644 --- a/api/ruxos_posix_api/src/imp/task.rs +++ b/api/ruxos_posix_api/src/imp/task.rs @@ -7,10 +7,13 @@ * See the Mulan PSL v2 for more details. */ -use crate::ctypes; use core::ffi::c_int; -use ruxtask::{task::PROCESS_MAP, yield_now}; +#[cfg(feature = "multitask")] +use { + crate::ctypes, + ruxtask::{task::PROCESS_MAP, yield_now}, +}; /// Relinquish the CPU, and switches to another task. /// @@ -31,31 +34,52 @@ pub fn sys_sched_yield() -> c_int { /// Get current thread ID. pub fn sys_gettid() -> c_int { syscall_body!(sys_gettid, - #[cfg(feature = "multitask")] - { - Ok(ruxtask::current().id().as_u64() as c_int) - } - #[cfg(not(feature = "multitask"))] - { + #[cfg(not(feature = "multitask"))]{ Ok(2) // `main` task ID } + #[cfg(feature = "multitask")]{ + Ok(ruxtask::current().id().as_u64() as c_int) + } ) } /// Get current process ID. pub fn sys_getpid() -> c_int { - syscall_body!(sys_getpid, Ok(ruxtask::current().id().as_u64() as c_int)) + #[cfg(not(feature = "multitask"))] + { + syscall_body!(sys_getpid, Ok(1)) + } + + #[cfg(feature = "multitask")] + { + syscall_body!(sys_getpid, Ok(ruxtask::current().id().as_u64() as c_int)) + } } /// Get parent process's ID. pub fn sys_getppid() -> c_int { - syscall_body!(sys_getppid, Ok(1)) + #[cfg(not(feature = "multitask"))] + { + syscall_body!(sys_getppid, Ok(1)) + } + + #[cfg(feature = "multitask")] + { + syscall_body!(sys_getppid, { + if let Some(parent_taskid) = ruxtask::current().parent_process() { + Ok(parent_taskid.id().as_u64() as c_int) + } else { + Ok(0) // `init` process ID + } + }) + } } /// Wait for a child process to exit and return its status. /// /// TODO: part of options, and rusage are not implemented yet. -pub fn sys_wait4( +#[cfg(feature = "multitask")] +pub unsafe fn sys_wait4( pid: c_int, wstatus: *mut c_int, options: c_int, @@ -101,18 +125,18 @@ pub fn sys_wait4( .filter(|(_, task)| task.parent_process().is_some()) { let parent_pid = task.parent_process().unwrap().id().as_u64(); - if parent_pid == ruxtask::current().id().as_u64() { - if task.state() == ruxtask::task::TaskState::Exited { - // add to to_remove list - unsafe { - // lower 8 bits of exit_code is the signal number, while upper 8 bits of exit_code is the exit status - // according to "bits/waitstatus.h" in glibc source code. - // TODO: add signal number to wstatus - wstatus.write(task.exit_code() << 8); - } - let _ = to_remove.insert(*child_pid); - break; + if parent_pid == ruxtask::current().id().as_u64() + && task.state() == ruxtask::task::TaskState::Exited + { + // add to to_remove list + unsafe { + // lower 8 bits of exit_code is the signal number, while upper 8 bits of exit_code is the exit status + // according to "bits/waitstatus.h" in glibc source code. + // TODO: add signal number to wstatus + wstatus.write(task.exit_code() << 8); } + let _ = to_remove.insert(*child_pid); + break; } } if options & WNOHANG != 0 { @@ -121,7 +145,7 @@ pub fn sys_wait4( // drop lock before yielding to other tasks drop(process_map); // check if the condition is meet - if !to_remove.is_none() { + if to_remove.is_some() { break; } // for single-cpu system, we must yield to other tasks instead of dead-looping here. diff --git a/api/ruxos_posix_api/src/lib.rs b/api/ruxos_posix_api/src/lib.rs index 9e0e69ce6..72e3f2c05 100644 --- a/api/ruxos_posix_api/src/lib.rs +++ b/api/ruxos_posix_api/src/lib.rs @@ -56,7 +56,9 @@ pub use imp::stat::{ }; pub use imp::sys::{sys_sysinfo, sys_uname}; pub use imp::sys_invalid; -pub use imp::task::{sys_exit, sys_getpid, sys_getppid, sys_gettid, sys_sched_yield, sys_wait4}; +#[cfg(feature = "multitask")] +pub use imp::task::sys_wait4; +pub use imp::task::{sys_exit, sys_getpid, sys_getppid, sys_gettid, sys_sched_yield}; pub use imp::time::{ sys_clock_gettime, sys_clock_nanosleep, sys_clock_settime, sys_gettimeofday, sys_nanosleep, sys_times, diff --git a/apps/c/envtest/expect_info.out b/apps/c/envtest/expect_info.out index cd1e5927d..5c11ea248 100644 --- a/apps/c/envtest/expect_info.out +++ b/apps/c/envtest/expect_info.out @@ -11,7 +11,6 @@ Found physcial memory regions: .bss (READ | WRITE | RESERVED) free memory (READ | WRITE | EXECUTE | FREE) Initialize global memory allocator... - use TLSF allocator. Initialize kernel page table... Initialize platform devices... Primary CPU 0 init OK. diff --git a/apps/c/httpclient/features.txt b/apps/c/httpclient/features.txt index 25ca64f38..af0b03c96 100644 --- a/apps/c/httpclient/features.txt +++ b/apps/c/httpclient/features.txt @@ -1,3 +1,5 @@ alloc paging net +poll +rtc diff --git a/apps/c/memtest/expect_trace.out b/apps/c/memtest/expect_trace.out index 3bdabf61c..52e341aa0 100644 --- a/apps/c/memtest/expect_trace.out +++ b/apps/c/memtest/expect_trace.out @@ -1,43 +1,14 @@ -smp = 1 -build_mode = release -log_level = trace - -Primary CPU 0 started, -Found physcial memory regions: - .text (READ | EXECUTE | RESERVED) - .rodata (READ | RESERVED) - .data .tdata .tbss .percpu (READ | WRITE | RESERVED) - .percpu (READ | WRITE | RESERVED) - boot stack (READ | WRITE | RESERVED) - .bss (READ | WRITE | RESERVED) - free memory (READ | WRITE | EXECUTE | FREE) -Initialize global memory allocator... - use TLSF allocator. -initialize global allocator at: \[0x[0-9a-f]\+, 0x[0-9a-f]\+) -Initialize kernel page table... -Initialize platform devices... -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | EXECUTE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE | DEVICE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE) -set page table root: PA:0x[0-9a-f]\+ => PA:0x[0-9a-f]\+ -Primary CPU 0 init OK. Running memory tests... -top of heap=0x[0-9a-f]\{16\} -72(+8)Byte allocated: p=0x[0-9a-f]\{16\} -allocate 8(+8)Byte for 9 times: -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -Memory tests run OK! -Shutting down... \ No newline at end of file +top of heap= +72(+8)Byte allocated: p= +allocate 8(+8)Byte for 9 +allocated addr= +allocated addr= +allocated addr= +allocated addr= +allocated addr= +allocated addr= +allocated addr= +allocated addr= +allocated addr= +Memory tests run OK! \ No newline at end of file diff --git a/apps/c/pthread/basic/expect_info_smp4_fifo.out b/apps/c/pthread/basic/expect_info_smp4_fifo.out index 3b75dedc8..115be8878 100644 --- a/apps/c/pthread/basic/expect_info_smp4_fifo.out +++ b/apps/c/pthread/basic/expect_info_smp4_fifo.out @@ -15,7 +15,6 @@ Initialize global memory allocator... Initialize kernel page table... Initialize platform devices... Initialize scheduling... - use FIFO scheduler. Pass NULL argument Recieve: Main thread pass message test_create_join: Child thread return message diff --git a/apps/c/pthread/basic/features.txt b/apps/c/pthread/basic/features.txt index 035f5582a..86e9e458a 100644 --- a/apps/c/pthread/basic/features.txt +++ b/apps/c/pthread/basic/features.txt @@ -2,3 +2,4 @@ alloc paging multitask irq +signal diff --git a/apps/c/pthread/parallel/expect_info_smp4_fifo.out b/apps/c/pthread/parallel/expect_info_smp4_fifo.out index b11eb07ca..c0e92224f 100644 --- a/apps/c/pthread/parallel/expect_info_smp4_fifo.out +++ b/apps/c/pthread/parallel/expect_info_smp4_fifo.out @@ -1,28 +1,3 @@ -smp = 4 -build_mode = release -log_level = info - -CPU 0 started -Found physcial memory regions: - .text (READ | EXECUTE | RESERVED) - .rodata (READ | RESERVED) - .data .tdata .tbss .percpu (READ | WRITE | RESERVED) - .percpu (READ | WRITE | RESERVED) - boot stack (READ | WRITE | RESERVED) - .bss (READ | WRITE | RESERVED) - free memory (READ | WRITE | EXECUTE | FREE) -Initialize global memory allocator... -Initialize kernel page table... -Initialize platform devices... -Initialize scheduling... - use FIFO scheduler -CPU 1 started -CPU 1 init OK -CPU 2 started -CPU 2 init OK -CPU 3 started -CPU 3 init OK -CPU 0 init OK part 0: \[0, 125000) part 1: \[125000, 250000) part 2: \[250000, 375000) @@ -56,4 +31,3 @@ part 13 finished part 14 finished part 15 finished (C)Pthread parallel run OK! -Shutting down... diff --git a/apps/c/pthread/parallel/expect_info_smp4_rr.out b/apps/c/pthread/parallel/expect_info_smp4_rr.out index 12efc08ad..c0e92224f 100644 --- a/apps/c/pthread/parallel/expect_info_smp4_rr.out +++ b/apps/c/pthread/parallel/expect_info_smp4_rr.out @@ -1,29 +1,3 @@ -smp = 4 -build_mode = release -log_level = info - -CPU 0 started -Found physcial memory regions: - .text (READ | EXECUTE | RESERVED) - .rodata (READ | RESERVED) - .data .tdata .tbss .percpu (READ | WRITE | RESERVED) - .percpu (READ | WRITE | RESERVED) - boot stack (READ | WRITE | RESERVED) - .bss (READ | WRITE | RESERVED) - free memory (READ | WRITE | EXECUTE | FREE) -Initialize global memory allocator... -Initialize kernel page table... -Initialize platform devices... -Initialize scheduling... - use Round-robin scheduler -CPU 1 started -CPU 1 init OK -CPU 2 started -CPU 2 init OK -CPU 3 started -CPU 3 init OK -Initialize interrupt handlers... -CPU 0 init OK part 0: \[0, 125000) part 1: \[125000, 250000) part 2: \[250000, 375000) @@ -57,4 +31,3 @@ part 13 finished part 14 finished part 15 finished (C)Pthread parallel run OK! -Shutting down... diff --git a/apps/c/pthread/parallel/features.txt b/apps/c/pthread/parallel/features.txt index 6ca9b0c5f..56be374d4 100644 --- a/apps/c/pthread/parallel/features.txt +++ b/apps/c/pthread/parallel/features.txt @@ -1,3 +1,4 @@ alloc paging multitask +signal diff --git a/apps/c/pthread/parallel/test_cmd b/apps/c/pthread/parallel/test_cmd index b0d44ae37..e6d65df42 100644 --- a/apps/c/pthread/parallel/test_cmd +++ b/apps/c/pthread/parallel/test_cmd @@ -1,3 +1,3 @@ -test_one "SMP=4 LOG=info" "expect_info_smp4_fifo.out" -test_one "SMP=4 LOG=info FEATURES=sched_rr" "expect_info_smp4_rr.out" +test_one "SMP=4 LOG=off" "expect_info_smp4_fifo.out" +test_one "SMP=4 LOG=off FEATURES=sched_rr" "expect_info_smp4_rr.out" rm -f $APP/*.o diff --git a/apps/c/pthread/pipe/expect_info_smp4_fifo.out b/apps/c/pthread/pipe/expect_info_smp4_fifo.out index fefc8cc0c..213c1d841 100644 --- a/apps/c/pthread/pipe/expect_info_smp4_fifo.out +++ b/apps/c/pthread/pipe/expect_info_smp4_fifo.out @@ -15,7 +15,6 @@ Initialize global memory allocator... Initialize kernel page table... Initialize platform devices... Initialize scheduling... - use FIFO scheduler. Child thread send message(1) Child thread send message(2) Main thread recieve (1): I am child(1)! diff --git a/apps/c/pthread/pipe/features.txt b/apps/c/pthread/pipe/features.txt index be558434d..1bcdfc264 100644 --- a/apps/c/pthread/pipe/features.txt +++ b/apps/c/pthread/pipe/features.txt @@ -2,3 +2,4 @@ paging alloc multitask pipe +signal diff --git a/apps/c/pthread/sleep/expect_info_smp4_fifo.out b/apps/c/pthread/sleep/expect_info_smp4_fifo.out index 6446cd725..b3b99198f 100644 --- a/apps/c/pthread/sleep/expect_info_smp4_fifo.out +++ b/apps/c/pthread/sleep/expect_info_smp4_fifo.out @@ -15,7 +15,6 @@ Initialize global memory allocator... Initialize kernel page table... Initialize platform devices... Initialize scheduling... - use FIFO scheduler. Initialize interrupt handlers... Hello, main task! main task sleep for 1\.[0-9]\+s diff --git a/apps/c/pthread/sleep/features.txt b/apps/c/pthread/sleep/features.txt index efe7876d3..a80f84ac6 100644 --- a/apps/c/pthread/sleep/features.txt +++ b/apps/c/pthread/sleep/features.txt @@ -2,3 +2,4 @@ paging alloc multitask irq +signal diff --git a/apps/c/pthread/tsd/expect_info_smp4_fifo.out b/apps/c/pthread/tsd/expect_info_smp4_fifo.out index 28018797a..480302205 100644 --- a/apps/c/pthread/tsd/expect_info_smp4_fifo.out +++ b/apps/c/pthread/tsd/expect_info_smp4_fifo.out @@ -15,7 +15,6 @@ Initialize global memory allocator... Initialize kernel page table... Initialize platform devices... Initialize scheduling... - use FIFO scheduler. max_keys = 1024, got No.0 destr_func, *arg = 0x1234 destr_func, *arg = 0x5678 diff --git a/apps/c/pthread/tsd/features.txt b/apps/c/pthread/tsd/features.txt index 035f5582a..86e9e458a 100644 --- a/apps/c/pthread/tsd/features.txt +++ b/apps/c/pthread/tsd/features.txt @@ -2,3 +2,4 @@ alloc paging multitask irq +signal diff --git a/modules/ruxdriver/src/virtio.rs b/modules/ruxdriver/src/virtio.rs index 2c36297a4..eed4d7da8 100644 --- a/modules/ruxdriver/src/virtio.rs +++ b/modules/ruxdriver/src/virtio.rs @@ -7,12 +7,21 @@ * See the Mulan PSL v2 for more details. */ +//! A driver for VirtIO devices. + use crate::{drivers::DriverProbe, AxDeviceEnum}; use cfg_if::cfg_if; use core::marker::PhantomData; use driver_common::{BaseDriverOps, DevResult, DeviceType}; #[cfg(bus = "mmio")] use ruxhal::mem::phys_to_virt; +#[cfg(any( + feature = "virtio-net", + feature = "virtio-blk", + feature = "virtio-gpu", + feature = "virtio-9p", + feature = "pci" +))] use ruxhal::virtio::virtio_hal::VirtIoHalImpl; cfg_if! { @@ -26,16 +35,21 @@ cfg_if! { /// A trait for VirtIO device meta information. pub trait VirtIoDevMeta { + /// The device type of the VirtIO device. const DEVICE_TYPE: DeviceType; + /// The device type of the VirtIO device. type Device: BaseDriverOps; + /// The driver for the VirtIO device. type Driver = VirtIoDriver; + /// Try to create a new instance of the VirtIO device. fn try_new(transport: VirtIoTransport) -> DevResult; } cfg_if! { if #[cfg(net_dev = "virtio-net")] { + /// A VirtIO network device. pub struct VirtIoNet; impl VirtIoDevMeta for VirtIoNet { @@ -51,6 +65,7 @@ cfg_if! { cfg_if! { if #[cfg(block_dev = "virtio-blk")] { + /// A VirtIO block device. pub struct VirtIoBlk; impl VirtIoDevMeta for VirtIoBlk { @@ -66,6 +81,7 @@ cfg_if! { cfg_if! { if #[cfg(display_dev = "virtio-gpu")] { + /// A VirtIO GPU device. pub struct VirtIoGpu; impl VirtIoDevMeta for VirtIoGpu { @@ -81,6 +97,7 @@ cfg_if! { cfg_if! { if #[cfg(_9p_dev = "virtio-9p")] { + /// A VirtIO 9P device. pub struct VirtIo9p; impl VirtIoDevMeta for VirtIo9p { @@ -94,7 +111,7 @@ cfg_if! { } } -/// A common driver for all VirtIO devices that implements [`DriverProbe`]. +/// A common driver for all VirtIO devices that implements DriverProbe. pub struct VirtIoDriver(PhantomData); impl DriverProbe for VirtIoDriver { diff --git a/modules/ruxfs/src/root.rs b/modules/ruxfs/src/root.rs index 117dca0b3..4e5593a3b 100644 --- a/modules/ruxfs/src/root.rs +++ b/modules/ruxfs/src/root.rs @@ -19,7 +19,9 @@ use crate::api::FileType; /// mount point information pub struct MountPoint { + /// mount point path pub path: &'static str, + /// mounted filesystem pub fs: Arc, } @@ -230,9 +232,16 @@ pub(crate) fn remove_dir(dir: Option<&VfsNodeRef>, path: &str) -> AxResult { { return ax_err!(InvalidInput); } - // if ROOT_DIR.contains(&absolute_path(path)?) { - // return ax_err!(PermissionDenied); - // } + + // TODO: judge if the path is a mount point, + // and return PermissionDenied if it is.(not allow to remove mount points) + // but it's not necessary to do this in this function. + // to meet multi-process requirement, some checks were commented out. + if crate_interface::call_interface!(CurrentWorkingDirectoryOps::root_dir,) + .contains(&absolute_path(path)?) + { + return ax_err!(PermissionDenied); + } let node = lookup(dir, path)?; let attr = node.get_attr()?; @@ -254,12 +263,20 @@ pub(crate) fn rename(old: &str, new: &str) -> AxResult { } #[crate_interface::def_interface] +/// Current working directory operations. pub trait CurrentWorkingDirectoryOps { + /// Initializes the root filesystem with the specified mount points. fn init_rootfs(mount_points: Vec); + /// Returns the parent node of the specified path. fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef; + /// Returns the absolute path of the specified path. fn absolute_path(path: &str) -> AxResult; + /// Returns the current working directory. fn current_dir() -> AxResult; + /// Sets the current working directory. fn set_current_dir(path: &str) -> AxResult; + /// get the root directory of the filesystem + fn root_dir() -> Arc; } pub(crate) fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef { diff --git a/modules/ruxhal/Cargo.toml b/modules/ruxhal/Cargo.toml index bc6e143f1..c12254a7a 100644 --- a/modules/ruxhal/Cargo.toml +++ b/modules/ruxhal/Cargo.toml @@ -23,7 +23,7 @@ tls = ["alloc"] default = [] musl = [] signal = [] -virtio_console = ["driver_console", "driver_virtio", "driver_virtio/console", "driver_common", "virtio-drivers", "axalloc", "lazy_static", "alloc", "virtio"] +virtio_console = ["driver_console", "driver_virtio", "driver_virtio/console", "driver_common", "virtio-drivers", "axalloc", "alloc", "virtio"] [dependencies] @@ -46,7 +46,7 @@ driver_console = { path = "../../crates/driver_console", optional = true } driver_virtio = { path = "../../crates/driver_virtio", optional = true } driver_common = { path = "../../crates/driver_common", optional = true } virtio-drivers = { git = "https://github.com/syswonder/virtio-drivers.git", rev = "62dbe5a", optional = true } -lazy_static = { version = "1.4", features = ["spin_no_std"], optional = true } +lazy_static = { version = "1.4", features = ["spin_no_std"] } memory_addr = "0.1.0" handler_table = "0.1.0" crate_interface = "0.1.1" diff --git a/modules/ruxhal/src/arch/x86_64/mod.rs b/modules/ruxhal/src/arch/x86_64/mod.rs index 973ff5cf6..a934c5b18 100644 --- a/modules/ruxhal/src/arch/x86_64/mod.rs +++ b/modules/ruxhal/src/arch/x86_64/mod.rs @@ -174,7 +174,6 @@ pub fn flush_tlb(vaddr: Option) { #[inline] #[cfg(all(feature = "irq", feature = "paging", feature = "smp"))] pub(crate) fn flush_tlb_ipi_handler() { - // error!("flush TLB entry in IPI handler"); let guard = kernel_guard::NoPreempt::new(); unsafe { let mut flushing_addresses = FLUSHING_ADDRESSES[this_cpu_id()].lock(); diff --git a/modules/ruxhal/src/mem.rs b/modules/ruxhal/src/mem.rs index 6c45d12eb..3475d9874 100644 --- a/modules/ruxhal/src/mem.rs +++ b/modules/ruxhal/src/mem.rs @@ -56,11 +56,13 @@ pub struct MemRegion { /// A trait for address translation. #[crate_interface::def_interface] pub trait AddressTranslate { + /// Translates a virtual address to a physical address. fn virt_to_phys(vaddr: VirtAddr) -> Option { Some(direct_virt_to_phys(vaddr).into()) } } +/// translates a virtual address to a physical address. pub fn address_translate(vaddr: VirtAddr) -> Option { crate_interface::call_interface!(AddressTranslate::virt_to_phys, vaddr) } diff --git a/modules/ruxhal/src/platform/x86_pc/rtc.rs b/modules/ruxhal/src/platform/x86_pc/rtc.rs index 6744bb7d1..cdc002d07 100644 --- a/modules/ruxhal/src/platform/x86_pc/rtc.rs +++ b/modules/ruxhal/src/platform/x86_pc/rtc.rs @@ -141,6 +141,12 @@ pub struct Rtc { nmi: bool, } +impl Default for Rtc { + fn default() -> Self { + Self::new() + } +} + impl Rtc { /// Create new empty RTC pub fn new() -> Self { diff --git a/modules/ruxmm/src/lib.rs b/modules/ruxmm/src/lib.rs index 76a09e7b2..31ce9993a 100644 --- a/modules/ruxmm/src/lib.rs +++ b/modules/ruxmm/src/lib.rs @@ -7,6 +7,8 @@ * See the Mulan PSL v2 for more details. */ +//! memory management module for RuxOS. + #![no_std] #![feature(asm_const)] #![feature(naked_functions)] @@ -18,4 +20,5 @@ pub mod mem; #[cfg(feature = "paging")] +/// A module for paging operations. pub mod paging; diff --git a/modules/ruxmm/src/mem.rs b/modules/ruxmm/src/mem.rs index fe9f599b8..6ada172a6 100644 --- a/modules/ruxmm/src/mem.rs +++ b/modules/ruxmm/src/mem.rs @@ -7,6 +7,8 @@ * See the Mulan PSL v2 for more details. */ +//! This module provides functions to translate virtual addresses to physical addresses. + #[cfg(feature = "paging")] use crate::paging::pte_query; use ruxhal::mem::{direct_virt_to_phys, AddressTranslate, PhysAddr, VirtAddr}; diff --git a/modules/ruxnet/src/lwip_impl/addr.rs b/modules/ruxnet/src/lwip_impl/addr.rs index 0cf41a737..2359d4323 100644 --- a/modules/ruxnet/src/lwip_impl/addr.rs +++ b/modules/ruxnet/src/lwip_impl/addr.rs @@ -3,14 +3,9 @@ use core::{ str::FromStr, }; -use lwip_rust::bindings::{ - ip4_addr_t, ip_addr_t, lwip_ip_addr_type_IPADDR_TYPE_V4, lwip_ip_addr_type_IPADDR_TYPE_V6, -}; +use lwip_rust::bindings::ip_addr_t; -use core::net::{ - Ipv4Addr as CoreIpv4Addr, Ipv6Addr as CoreIpv6Addr, SocketAddr as CoreSocketAddr, SocketAddrV4, - SocketAddrV6, -}; +use core::net::{Ipv4Addr as CoreIpv4Addr, SocketAddr as CoreSocketAddr, SocketAddrV4}; /// Mac Address #[derive(Clone, Copy, Debug, Default)] @@ -73,7 +68,7 @@ impl IpAddr { /// Get the IP Address as a byte array pub fn as_bytes(&self) -> &[u8] { match self { - IpAddr::Ipv4(Ipv4Addr(addr)) => unsafe { &addr[..] }, + IpAddr::Ipv4(Ipv4Addr(addr)) => &addr[..], _ => panic!("IPv6 not supported"), } } @@ -120,7 +115,7 @@ impl From for ip_addr_t { impl From for IpAddr { #[allow(non_upper_case_globals)] fn from(addr: ip_addr_t) -> IpAddr { - IpAddr::Ipv4(Ipv4Addr(unsafe { addr.addr.to_be_bytes() })) + IpAddr::Ipv4(Ipv4Addr(addr.addr.to_be_bytes())) } } diff --git a/modules/ruxnet/src/lwip_impl/dns.rs b/modules/ruxnet/src/lwip_impl/dns.rs index 829775745..626ec99cc 100644 --- a/modules/ruxnet/src/lwip_impl/dns.rs +++ b/modules/ruxnet/src/lwip_impl/dns.rs @@ -98,7 +98,7 @@ pub fn resolve_socket_addr(name: &str) -> AxResult> { } /// Public function for DNS query. -pub fn dns_query(name: &str) -> AxResult> { +pub fn dns_query(_name: &str) -> AxResult> { let empty_vec = alloc::vec::Vec::new(); Ok(empty_vec) } diff --git a/modules/ruxnet/src/lwip_impl/driver.rs b/modules/ruxnet/src/lwip_impl/driver.rs index 70c546bdd..72b521669 100644 --- a/modules/ruxnet/src/lwip_impl/driver.rs +++ b/modules/ruxnet/src/lwip_impl/driver.rs @@ -3,12 +3,12 @@ use crate::{ net_impl::addr::{mask_to_prefix, MacAddr}, IpAddr, }; -use alloc::{boxed::Box, collections::VecDeque, sync::Arc, vec}; +use alloc::{boxed::Box, collections::VecDeque, sync::Arc}; #[cfg(feature = "irq")] use axdriver::register_interrupt_handler; use axsync::Mutex; use core::{cell::RefCell, ffi::c_void}; -use driver_net::{DevError, NetBuf, NetBufBox, NetBufPool, NetBufPtr}; +use driver_net::{DevError, NetBuf, NetBufBox}; use lazy_init::LazyInit; use lwip_rust::bindings::{ err_enum_t_ERR_MEM, err_enum_t_ERR_OK, err_t, etharp_output, ethernet_input, ip4_addr_t, @@ -21,9 +21,6 @@ use ruxdriver::prelude::*; const RX_BUF_QUEUE_SIZE: usize = 64; -const NET_BUF_LEN: usize = 1526; -const NET_BUF_POOL_SIZE: usize = 128; - struct NetifWrapper(netif); unsafe impl Send for NetifWrapper {} @@ -164,7 +161,7 @@ extern "C" fn ethif_output(netif: *mut netif, p: *mut pbuf) -> err_t { if dev.can_transmit() { unsafe { - let tot_len = unsafe { (*p).tot_len }; + let tot_len = (*p).tot_len; let mut tx_buf = *NetBuf::from_buf_ptr(dev.alloc_tx_buffer(tot_len.into()).unwrap()); dev.prepare_tx_buffer(&mut tx_buf, tot_len.into()).unwrap(); @@ -172,12 +169,12 @@ extern "C" fn ethif_output(netif: *mut netif, p: *mut pbuf) -> err_t { let mut offset = 0; let mut q = p; while !q.is_null() { - let len = unsafe { (*q).len } as usize; - let payload = unsafe { (*q).payload }; - let payload = unsafe { core::slice::from_raw_parts(payload as *const u8, len) }; + let len = (*q).len as usize; + let payload = (*q).payload; + let payload = core::slice::from_raw_parts(payload as *const u8, len); tx_buf.packet_mut()[offset..offset + len].copy_from_slice(payload); offset += len; - q = unsafe { (*q).next }; + q = (*q).next; } trace!( @@ -217,7 +214,7 @@ fn ip4_addr_gen(a: u8, b: u8, c: u8, d: u8) -> ip4_addr_t { } pub fn init() {} -pub fn init_netdev(mut net_dev: AxNetDevice) { +pub fn init_netdev(net_dev: AxNetDevice) { match net_dev.device_name() { "loopback" => { info!("use lwip netif loopback"); diff --git a/modules/ruxnet/src/lwip_impl/mod.rs b/modules/ruxnet/src/lwip_impl/mod.rs index 87084c607..907701d00 100644 --- a/modules/ruxnet/src/lwip_impl/mod.rs +++ b/modules/ruxnet/src/lwip_impl/mod.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + mod addr; mod dns; mod driver; @@ -5,7 +7,7 @@ mod tcp; mod udp; pub use self::addr::{IpAddr, Ipv4Addr, SocketAddr}; -pub use self::dns::{dns_query, resolve_socket_addr}; +pub use self::dns::dns_query; pub use self::driver::{init, init_netdev, poll_interfaces}; pub use self::tcp::TcpSocket; pub use self::udp::UdpSocket; diff --git a/modules/ruxnet/src/lwip_impl/tcp.rs b/modules/ruxnet/src/lwip_impl/tcp.rs index c3abfe3ee..5f8691c21 100644 --- a/modules/ruxnet/src/lwip_impl/tcp.rs +++ b/modules/ruxnet/src/lwip_impl/tcp.rs @@ -7,7 +7,7 @@ use axerrno::{ax_err, AxError, AxResult}; use axio::PollState; use axsync::Mutex; use core::cell::UnsafeCell; -use core::sync::atomic::{AtomicBool, AtomicU8, Ordering}; +use core::sync::atomic::{AtomicBool, Ordering}; use core::{ffi::c_void, pin::Pin, ptr::null_mut}; use lwip_rust::bindings::{ err_enum_t_ERR_MEM, err_enum_t_ERR_OK, err_enum_t_ERR_USE, err_enum_t_ERR_VAL, err_t, @@ -375,7 +375,7 @@ impl TcpSocket { } /// Receives data from the socket, stores it in the given buffer. - pub fn recv(&self, buf: &mut [u8], flags: i32) -> AxResult { + pub fn recv(&self, buf: &mut [u8], _flags: i32) -> AxResult { loop { if self.inner.remote_closed { return Ok(0); @@ -470,7 +470,6 @@ impl TcpSocket { trace!("poll pcbstate: {:?}", unsafe { (*self.pcb.get()).state }); lwip_loop_once(); if unsafe { (*self.pcb.get()).state } == tcp_state_LISTEN { - let test = self.inner.accept_queue.lock().len(); // listener Ok(PollState { readable: self.inner.accept_queue.lock().len() != 0, @@ -478,7 +477,6 @@ impl TcpSocket { pollhup: false, }) } else { - let test = self.inner.recv_queue.lock().len(); // stream Ok(PollState { readable: self.inner.recv_queue.lock().len() != 0, diff --git a/modules/ruxnet/src/lwip_impl/udp.rs b/modules/ruxnet/src/lwip_impl/udp.rs index 0c5046517..d1c54f820 100644 --- a/modules/ruxnet/src/lwip_impl/udp.rs +++ b/modules/ruxnet/src/lwip_impl/udp.rs @@ -6,7 +6,7 @@ use alloc::{boxed::Box, collections::VecDeque}; use axerrno::{ax_err, AxError, AxResult}; use axio::PollState; use axsync::Mutex; -use core::sync::atomic::{AtomicBool, AtomicU8, Ordering}; +use core::sync::atomic::{AtomicBool, Ordering}; use core::{ffi::c_void, pin::Pin, ptr::null_mut}; use lwip_rust::bindings::{ err_enum_t_ERR_MEM, err_enum_t_ERR_OK, err_enum_t_ERR_RTE, err_enum_t_ERR_USE, @@ -253,7 +253,7 @@ impl UdpSocket { drop(guard); } - Ok((copy_len, addr.into())) + Ok((copy_len, addr)) }; drop(recv_queue); match res { diff --git a/modules/ruxnet/src/unix.rs b/modules/ruxnet/src/unix.rs index 5378241fb..53c6c7165 100644 --- a/modules/ruxnet/src/unix.rs +++ b/modules/ruxnet/src/unix.rs @@ -66,7 +66,7 @@ impl<'a> UnixSocketInner<'a> { } pub fn get_addr(&self) -> SocketAddrUnix { - self.addr.lock().clone() + *self.addr.lock() } pub fn get_peersocket(&self) -> Option { @@ -460,7 +460,7 @@ impl UnixSocket { let writable = { let mut binding = UNIX_TABLE.write(); let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock(); - if !socket_inner.get_peersocket().is_none() { + if socket_inner.get_peersocket().is_some() { socket_inner.set_state(UnixSocketStatus::Connected); true } else { @@ -694,10 +694,7 @@ impl UnixSocket { /// Checks if the socket is in a listening state. pub fn is_listening(&self) -> bool { let now_state = self.get_state(); - match now_state { - UnixSocketStatus::Listening => true, - _ => false, - } + matches!(now_state, UnixSocketStatus::Listening) } /// Returns the socket type of the `UnixSocket`. diff --git a/modules/ruxruntime/Cargo.toml b/modules/ruxruntime/Cargo.toml index d6823ee9e..3cbdb7615 100644 --- a/modules/ruxruntime/Cargo.toml +++ b/modules/ruxruntime/Cargo.toml @@ -20,7 +20,7 @@ smp = ["ruxhal/smp"] irq = ["ruxhal/irq", "ruxtask?/irq", "percpu", "kernel_guard"] tls = ["ruxhal/tls", "ruxtask?/tls"] alloc = ["axalloc", "dtb"] -paging = ["ruxhal/paging", "lazy_init", "ruxmm"] +paging = ["ruxhal/paging", "ruxmm/paging", "lazy_init", "ruxtask/paging"] rtc = ["ruxhal/rtc"] virtio_console = [] @@ -32,16 +32,18 @@ virtio-9p = ["fs", "rux9p"] net-9p = ["fs", "rux9p"] net = ["ruxdriver", "ruxnet"] display = ["ruxdriver", "ruxdisplay"] -signal = ["ruxhal/signal"] +signal = ["ruxhal/signal", "ruxtask/signal"] musl = ["dep:ruxfutex"] +# for testing +myfs = ["fs", "multitask", "alloc", "ruxfs/myfs", "ruxtask/test"] [dependencies] cfg-if = "1.0" ruxhal = { path = "../ruxhal" } axlog = { path = "../axlog" } -ruxconfig = { path = "../ruxconfig" } +ruxconfig = { path = "../ruxconfig"} axalloc = { path = "../axalloc", optional = true } ruxdriver = { path = "../ruxdriver", optional = true } ruxfs = { path = "../ruxfs", optional = true } @@ -49,7 +51,7 @@ rux9p = { path = "../rux9p", optional = true } ruxnet = { path = "../ruxnet", optional = true } ruxdisplay = { path = "../ruxdisplay", optional = true } ruxtask = { path = "../ruxtask", optional = true } -ruxmm = { path = "../ruxmm", optional = true } +ruxmm = { path = "../ruxmm" } axsync = { path = "../axsync", optional = true } ruxfutex = { path = "../ruxfutex", optional = true } ruxrand = { path = "../ruxrand", optional = true } @@ -61,3 +63,13 @@ lazy_init = { path = "../../crates/lazy_init", optional = true } dtb = { path = "../../crates/dtb", optional = true } tty = { path = "../../crates/tty", optional = true } + +[dev-dependencies] +log = "0.4" +ruxdriver = { path = "../ruxdriver", features = ["block", "ramdisk", "dyn"] } +driver_block = { path = "../../crates/driver_block", features = ["ramdisk"] } +axio = { path = "../../crates/axio", features = ["alloc"] } +ruxfs = { path = "../ruxfs" } +ruxtask = { path = "../ruxtask" } +axfs_vfs = { path = "../../crates/axfs_vfs" } +axfs_ramfs = { path = "../../crates/axfs_ramfs" } diff --git a/modules/ruxfs/resources/create_test_img.sh b/modules/ruxruntime/resources/create_test_img.sh similarity index 100% rename from modules/ruxfs/resources/create_test_img.sh rename to modules/ruxruntime/resources/create_test_img.sh diff --git a/modules/ruxfs/resources/fat16.img b/modules/ruxruntime/resources/fat16.img similarity index 100% rename from modules/ruxfs/resources/fat16.img rename to modules/ruxruntime/resources/fat16.img diff --git a/modules/ruxfs/tests/test_common/mod.rs b/modules/ruxruntime/tests/test_common/mod.rs similarity index 100% rename from modules/ruxfs/tests/test_common/mod.rs rename to modules/ruxruntime/tests/test_common/mod.rs diff --git a/modules/ruxfs/tests/test_fatfs.rs b/modules/ruxruntime/tests/test_fatfs.rs similarity index 100% rename from modules/ruxfs/tests/test_fatfs.rs rename to modules/ruxruntime/tests/test_fatfs.rs diff --git a/modules/ruxfs/tests/test_ramfs.rs b/modules/ruxruntime/tests/test_ramfs.rs similarity index 99% rename from modules/ruxfs/tests/test_ramfs.rs rename to modules/ruxruntime/tests/test_ramfs.rs index 61c57a45d..778de9938 100644 --- a/modules/ruxfs/tests/test_ramfs.rs +++ b/modules/ruxruntime/tests/test_ramfs.rs @@ -56,6 +56,7 @@ fn test_ramfs() { ruxtask::init_scheduler(); // call this to use `axsync::Mutex`. // By default, mount_points[0] will be rootfs + let mut mount_points: Vec = Vec::new(); // setup and initialize blkfs as one mountpoint for rootfs mount_points.push(ruxfs::init_blkfs(AxDeviceContainer::from_one(Box::new( diff --git a/modules/ruxtask/Cargo.toml b/modules/ruxtask/Cargo.toml index 386912fbb..cc1a9ba46 100644 --- a/modules/ruxtask/Cargo.toml +++ b/modules/ruxtask/Cargo.toml @@ -12,25 +12,26 @@ homepage = "https://github.com/syswonder/ruxos" repository = "https://github.com/syswonder/ruxos/tree/main/modules/ruxtask" [features] -default = [] +default = ["fs"] multitask = [ "dep:ruxconfig", "dep:percpu", "dep:spinlock", "dep:lazy_init", "dep:memory_addr", - "dep:scheduler", "dep:timer_list", "kernel_guard", "dep:crate_interface", + "dep:scheduler", "dep:timer_list", "kernel_guard", "dep:crate_interface" ] irq = [] tls = ["ruxhal/tls"] musl = [] preempt = ["irq", "percpu?/preempt", "kernel_guard/preempt"] -paging = [] +paging = ["ruxhal/paging"] fs = [] -signal = [] +signal = ["ruxhal/signal"] sched_fifo = ["multitask"] sched_rr = ["multitask", "preempt"] sched_cfs = ["multitask", "preempt"] test = ["percpu?/sp-naive"] +notest = [] [dependencies] cfg-if = "1.0" diff --git a/modules/ruxtask/src/api.rs b/modules/ruxtask/src/api.rs index 3981e3477..c4aa8f894 100644 --- a/modules/ruxtask/src/api.rs +++ b/modules/ruxtask/src/api.rs @@ -129,7 +129,7 @@ where } // temporarily only support aarch64 -#[cfg(target_arch = "aarch64")] +#[cfg(all(target_arch = "aarch64", feature = "paging", feature = "fs"))] pub fn fork_task() -> Option { use core::mem::ManuallyDrop; diff --git a/modules/ruxtask/src/fs.rs b/modules/ruxtask/src/fs.rs index be03efa29..b5eb30c7a 100644 --- a/modules/ruxtask/src/fs.rs +++ b/modules/ruxtask/src/fs.rs @@ -7,6 +7,10 @@ * See the Mulan PSL v2 for more details. */ +//! File system related functions. + +#![cfg(feature = "fs")] + use crate::current; use alloc::{format, string::String, sync::Arc, vec::Vec}; use axerrno::{ax_err, AxResult}; @@ -24,32 +28,48 @@ use ruxfdtable::RuxStat; use spin::RwLock; #[crate_interface::def_interface] +/// The interface for initializing the file system. pub trait InitFs { - fn init(task_inner: &mut FileSystem); + /// Initializes the file system. + fn add_stdios_to_fd_table(task_inner: &mut FileSystem); +} + +#[cfg(not(feature = "notest"))] +struct InitFsDefaultImpl; + +#[cfg(not(feature = "notest"))] +#[crate_interface::impl_interface] +impl InitFs for InitFsDefaultImpl { + fn add_stdios_to_fd_table(_task_inner: &mut FileSystem) { + // do nothing + } } +/// Initializes the file system. pub fn get_file_like(fd: i32) -> LinuxResult> { // let _exec = *MUST_EXEC; let binding_task = current(); let mut binding_fs = binding_task.fs.lock(); - let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; - fd_table - .get(fd as usize) - .cloned() - .ok_or(LinuxError::EBADF) - .clone() + if let Some(fs) = binding_fs.as_mut() { + fs.fd_table + .get(fd as usize) + .cloned() + .ok_or(LinuxError::EBADF) + } else { + Err(LinuxError::EBADF) + } } +/// Adds a file like object to the file descriptor table and returns the file descriptor. pub fn add_file_like(f: Arc) -> LinuxResult { - // let _exec = *MUST_EXEC; let binding_task = current(); let mut binding_fs = binding_task.fs.lock(); - let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; + let fd_table = &mut binding_fs.as_mut().expect("No fd table found").fd_table; Ok(fd_table.add(f).ok_or(LinuxError::EMFILE)? as i32) } +/// Removes a file like object from the file descriptor table. pub fn close_file_like(fd: i32) -> LinuxResult { - // let _exec = *MUST_EXEC; let binding_task = current(); let mut binding_fs = binding_task.fs.lock(); let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; @@ -57,21 +77,26 @@ pub fn close_file_like(fd: i32) -> LinuxResult { Ok(()) } +/// A struct representing a file object. pub struct File { + /// The inner file object. pub inner: RwLock, } impl File { + /// Creates a new file object with the given inner file object. pub fn new(inner: ruxfs::fops::File) -> Self { Self { inner: RwLock::new(inner), } } + /// Adds the file object to the file descriptor table and returns the file descriptor. pub fn add_to_fd_table(self) -> LinuxResult { add_file_like(Arc::new(self)) } + /// Creates a new file object from the given file descriptor. pub fn from_fd(fd: i32) -> LinuxResult> { let f = get_file_like(fd)?; f.into_any() @@ -136,21 +161,26 @@ impl FileLike for File { } } +/// A struct representing a directory object. pub struct Directory { + /// The inner directory object. pub inner: RwLock, } impl Directory { + /// Creates a new directory object with the given inner directory object. pub fn new(inner: ruxfs::fops::Directory) -> Self { Self { inner: RwLock::new(inner), } } + /// Adds the directory object to the file descriptor table and returns the file descriptor. pub fn add_to_fd_table(self) -> LinuxResult { add_file_like(Arc::new(self)) } + /// Creates a new directory object from the given file descriptor. pub fn from_fd(fd: i32) -> LinuxResult> { let f = get_file_like(fd)?; f.into_any() @@ -212,13 +242,18 @@ pub const RUX_FILE_LIMIT: usize = 1024; /// A struct representing a file system object. pub struct FileSystem { + /// The file descriptor table. pub fd_table: FlattenObjects, RUX_FILE_LIMIT>, + /// The current working directory. pub current_path: String, + /// The current directory. pub current_dir: VfsNodeRef, + /// The root directory. pub root_dir: Arc, } impl FileSystem { + /// Closes all file objects in the file descriptor table. pub fn close_all_files(&mut self) { for fd in 0..self.fd_table.capacity() { if self.fd_table.get(fd).is_some() { @@ -248,6 +283,7 @@ impl Clone for FileSystem { } } +/// Initializes the file system. pub fn init_rootfs(mount_points: Vec) { let main_fs = mount_points .first() @@ -272,8 +308,11 @@ pub fn init_rootfs(mount_points: Vec) { current_dir: root_dir_arc.clone(), root_dir: root_dir_arc.clone(), }; + + // TODO: make a more clear interface for adding stdios to fd table when not in unit tests let fs_mutable = &mut fs; - crate_interface::call_interface!(InitFs::init, fs_mutable); + crate_interface::call_interface!(InitFs::add_stdios_to_fd_table, fs_mutable); + current().fs.lock().replace(fs); } @@ -286,6 +325,7 @@ fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef { } } +/// Returns the absolute path of the given path. pub fn absolute_path(path: &str) -> AxResult { if path.starts_with('/') { Ok(axfs_vfs::path::canonicalize(path)) @@ -295,10 +335,12 @@ pub fn absolute_path(path: &str) -> AxResult { } } +/// Returns the current directory. pub fn current_dir() -> AxResult { Ok(current().fs.lock().as_mut().unwrap().current_path.clone()) } +/// Sets the current directory. pub fn set_current_dir(path: &str) -> AxResult { let mut abs_path = absolute_path(path)?; if !abs_path.ends_with('/') { @@ -343,4 +385,13 @@ impl CurrentWorkingDirectoryOps for CurrentWorkingDirectoryImpl { fn set_current_dir(path: &str) -> AxResult { set_current_dir(path) } + fn root_dir() -> Arc { + current() + .fs + .lock() + .as_mut() + .expect("No filesystem found") + .root_dir + .clone() + } } diff --git a/modules/ruxtask/src/lib.rs b/modules/ruxtask/src/lib.rs index 8b1d5060f..038dbfa3a 100644 --- a/modules/ruxtask/src/lib.rs +++ b/modules/ruxtask/src/lib.rs @@ -54,7 +54,7 @@ cfg_if::cfg_if! { pub mod signal; #[cfg(feature = "paging")] pub mod vma; - // #[cfg(feature = "fs")] + #[cfg(feature = "fs")] pub mod fs; #[cfg(feature = "irq")] /// load average diff --git a/modules/ruxtask/src/run_queue.rs b/modules/ruxtask/src/run_queue.rs index 796ac1bf5..7da23165f 100644 --- a/modules/ruxtask/src/run_queue.rs +++ b/modules/ruxtask/src/run_queue.rs @@ -7,8 +7,8 @@ * See the Mulan PSL v2 for more details. */ -use crate::fs::get_file_like; -use crate::fs::RUX_FILE_LIMIT; +#[cfg(feature = "fs")] +use crate::fs::{get_file_like, RUX_FILE_LIMIT}; use alloc::collections::VecDeque; use alloc::sync::Arc; use axerrno::LinuxResult; @@ -245,6 +245,7 @@ impl AxRunQueue { } } +#[cfg(feature = "fs")] fn gc_flush_file(fd: usize) -> LinuxResult { trace!("gc task flush: {}", fd); get_file_like(fd as i32)?.flush() @@ -253,10 +254,13 @@ fn gc_flush_file(fd: usize) -> LinuxResult { fn gc_entry() { let mut now_file_fd: usize = 3; loop { - let _ = gc_flush_file(now_file_fd); - now_file_fd += 1; - if now_file_fd >= RUX_FILE_LIMIT { - now_file_fd = 3; + #[cfg(feature = "fs")] + { + let _ = gc_flush_file(now_file_fd); + now_file_fd += 1; + if now_file_fd >= RUX_FILE_LIMIT { + now_file_fd = 3; + } } // Drop all exited tasks and recycle resources. let n = EXITED_TASKS.lock().len(); diff --git a/modules/ruxtask/src/signal.rs b/modules/ruxtask/src/signal.rs index 4864fe136..b1d16f742 100644 --- a/modules/ruxtask/src/signal.rs +++ b/modules/ruxtask/src/signal.rs @@ -7,6 +7,8 @@ * See the Mulan PSL v2 for more details. */ +//! Signal module for RuxOS. + use crate::current; #[cfg(feature = "irq")] use core::sync::atomic::AtomicI64; @@ -15,7 +17,6 @@ use core::{ time::Duration, }; -use crate_interface::impl_interface; use ruxhal::trap::TrapHandler; /// sigaction in kernel @@ -60,9 +61,9 @@ unsafe extern "C" fn default_handler(signum: c_int) { #[cfg(feature = "signal")] struct SignalHandler; -#[impl_interface] +#[cfg(feature = "signal")] +#[crate_interface::impl_interface] impl TrapHandler for SignalHandler { - #[cfg(feature = "signal")] fn handle_signal() { let signal = Signal::signal(-1, true).unwrap(); for signum in 0..32 { diff --git a/modules/ruxtask/src/task.rs b/modules/ruxtask/src/task.rs index 304379656..889334e33 100644 --- a/modules/ruxtask/src/task.rs +++ b/modules/ruxtask/src/task.rs @@ -7,6 +7,8 @@ * See the Mulan PSL v2 for more details. */ +//! implementation of task structure and related functions. +#[cfg(feature = "fs")] use crate::fs::FileSystem; use alloc::collections::BTreeMap; use alloc::{ @@ -92,6 +94,7 @@ pub struct TaskInner { tsd: TSD, #[cfg(feature = "signal")] + /// The signal to be sent to the task. pub signal_if: Arc>, // set tid @@ -101,11 +104,13 @@ pub struct TaskInner { #[cfg(feature = "musl")] tl: AtomicU64, #[cfg(feature = "paging")] - // The page table of the task. + /// The page table of the task. pub pagetable: Arc>, - // file system + /// file system + #[cfg(feature = "fs")] pub fs: Arc>>, - // memory management + #[cfg(feature = "paging")] + /// memory management pub mm: Arc, } @@ -210,6 +215,7 @@ impl TaskInner { } } +/// map task id into task. pub static PROCESS_MAP: SpinNoIrq>> = SpinNoIrq::new(BTreeMap::new()); // private methods @@ -254,7 +260,9 @@ impl TaskInner { tl: AtomicU64::new(0), #[cfg(feature = "paging")] pagetable: current().pagetable.clone(), + #[cfg(feature = "fs")] fs: current().fs.clone(), + #[cfg(feature = "paging")] mm: current().mm.clone(), } } @@ -298,15 +306,19 @@ impl TaskInner { tl, #[cfg(feature = "paging")] pagetable: current().pagetable.clone(), + #[cfg(feature = "fs")] fs: current().fs.clone(), + #[cfg(feature = "paging")] mm: current().mm.clone(), } } + /// Create a new idle task. pub fn stack_top(&self) -> VirtAddr { self.kstack.lock().as_ref().as_ref().unwrap().top() } + /// Set the stack top and size for the task. pub fn set_stack_top(&self, begin: usize, size: usize) { debug!("set_stack_top: begin={:#x}, size={:#x}", begin, size); *self.stack_map_addr.lock() = VirtAddr::from(begin); @@ -375,7 +387,7 @@ impl TaskInner { Arc::new(AxTask::new(t)) } - #[cfg(target_arch = "aarch64")] + #[cfg(all(target_arch = "aarch64", feature = "paging", feature = "fs"))] /// only support for aarch64 pub fn fork() -> AxTaskRef { use crate::alloc::string::ToString; @@ -515,7 +527,9 @@ impl TaskInner { tl: AtomicU64::new(0), #[cfg(feature = "paging")] pagetable: Arc::new(SpinNoIrq::new(cloned_page_table)), + #[cfg(feature = "fs")] fs: Arc::new(SpinNoIrq::new(current_task.fs.lock().clone())), + #[cfg(feature = "paging")] mm: Arc::new(cloned_mm), }; @@ -557,8 +571,9 @@ impl TaskInner { /// /// And there is no need to set the `entry`, `kstack` or `tls` fields, as /// they will be filled automatically when the task is switches out. + #[allow(unused_mut)] pub(crate) fn new_init(name: String) -> AxTaskRef { - let mut t = Self { + let mut t: TaskInner = Self { parent_process: None, process_task: Weak::new(), id: TaskId::new(), @@ -593,22 +608,28 @@ impl TaskInner { pagetable: Arc::new(SpinNoIrq::new( PageTable::try_new().expect("failed to create page table"), )), + #[cfg(feature = "fs")] fs: Arc::new(SpinNoIrq::new(None)), + #[cfg(feature = "paging")] mm: Arc::new(MmapStruct::new()), }; debug!("new init task: {}", t.id_name()); - #[cfg(feature = "tls")] - let tls = VirtAddr::from(t.tls.tls_ptr() as usize); - #[cfg(not(feature = "tls"))] - let tls = VirtAddr::from(0); + #[cfg(feature = "notest")] + { + #[cfg(feature = "tls")] + let tls = VirtAddr::from(t.tls.tls_ptr() as usize); + #[cfg(not(feature = "tls"))] + let tls = VirtAddr::from(0); + + t.set_stack_top(boot_stack as usize, ruxconfig::TASK_STACK_SIZE); + t.ctx.get_mut().init( + task_entry as usize, + VirtAddr::from(boot_stack as usize), + tls, + ); + } - t.set_stack_top(boot_stack as usize, ruxconfig::TASK_STACK_SIZE); - t.ctx.get_mut().init( - task_entry as usize, - VirtAddr::from(boot_stack as usize), - tls, - ); let task_ref = Arc::new(AxTask::new(t)); PROCESS_MAP .lock() @@ -616,6 +637,7 @@ impl TaskInner { task_ref } + /// Create a new idle task. pub fn new_idle(name: String) -> AxTaskRef { const IDLE_STACK_SIZE: usize = 4096; let bindings = PROCESS_MAP.lock(); @@ -656,7 +678,9 @@ impl TaskInner { tl: AtomicU64::new(0), #[cfg(feature = "paging")] pagetable: task_ref.pagetable.clone(), + #[cfg(feature = "fs")] fs: task_ref.fs.clone(), + #[cfg(feature = "paging")] mm: task_ref.mm.clone(), }; @@ -825,12 +849,15 @@ impl fmt::Debug for TaskInner { } #[derive(Debug)] +/// A wrapper of TaskStack to provide a safe interface for allocating and +/// deallocating task stacks. pub struct TaskStack { ptr: NonNull, layout: Layout, } impl TaskStack { + /// Allocate a new task stack with the given size. pub fn alloc(size: usize) -> Self { let layout = Layout::from_size_align(size, 8).unwrap(); Self { @@ -839,19 +866,28 @@ impl TaskStack { } } + /// Deallocate the task stack. pub const fn top(&self) -> VirtAddr { unsafe { core::mem::transmute(self.ptr.as_ptr().add(self.layout.size())) } } + /// Deallocate the task stack. pub const fn end(&self) -> VirtAddr { unsafe { core::mem::transmute(self.ptr.as_ptr()) } } + /// Deallocate the task stack. pub fn size(&self) -> usize { self.layout.size() } } +impl Drop for TaskStack { + fn drop(&mut self) { + unsafe { alloc::alloc::dealloc(self.ptr.as_ptr(), self.layout) } + } +} + /// A wrapper of [`AxTaskRef`] as the current task. pub struct CurrentTask(ManuallyDrop); @@ -874,6 +910,7 @@ impl CurrentTask { &self.0 } + /// clone [`CurrentTask`] as [`AxTaskRef`]. pub fn clone_as_taskref(&self) -> AxTaskRef { self.0.deref().clone() } @@ -923,6 +960,7 @@ extern "C" fn task_entry() -> ! { crate::exit(0); } +#[cfg(feature = "notest")] extern "C" { fn boot_stack(); } diff --git a/modules/ruxtask/src/vma.rs b/modules/ruxtask/src/vma.rs index d0fe4184b..48e302f97 100644 --- a/modules/ruxtask/src/vma.rs +++ b/modules/ruxtask/src/vma.rs @@ -46,9 +46,12 @@ used_fs! { // pub(crate) const SWAP_MAX: usize = 1024 * 1024 * 1024; pub(crate) const SWAP_MAX: usize = 0; pub(crate) const SWAP_PATH: &str = "swap.raw\0"; + /// record the mapping of swapped out pages. pub static SWAPED_MAP: SpinNoIrq>> = SpinNoIrq::new(BTreeMap::new()); // Vaddr => (page_size, offset_at_swaped) lazy_static::lazy_static! { + /// swap file for swapping out pages. pub static ref SWAP_FILE: Arc = open_swap_file(SWAP_PATH); + /// bitmap for free pages in swap file. pub static ref BITMAP_FREE: SpinNoIrq> = SpinNoIrq::new((0..SWAP_MAX).step_by(PAGE_SIZE_4K).collect()); } } @@ -69,21 +72,27 @@ fn open_swap_file(filename: &str) -> Arc { /// Data structure for file mapping. #[derive(Clone)] pub struct FileInfo { + /// file that the mapping is backed by pub file: Arc, + /// offset in the file pub offset: usize, + /// size of the mapping pub size: usize, } /// Data structure for information of mapping. pub struct PageInfo { + /// physical address of the page pub paddr: PhysAddr, #[cfg(feature = "fs")] + /// file that the mapping is backed by pub mapping_file: Option, } /// Data structure for swaping out a page in a file. #[derive(Debug, Clone)] pub struct SwapInfo { + /// offset in the swap file pub offset: usize, } @@ -162,6 +171,7 @@ impl MmapStruct { /// Impl for Vma. impl Vma { + /// Create a new `Vma` instance. pub fn new(_fid: i32, offset: usize, prot: u32, flags: u32) -> Self { // #[cfg(feature = "fs")] let file = if _fid < 0 { @@ -187,6 +197,7 @@ impl Vma { } } + /// Clone a new `Vma` instance. pub fn clone_from(vma: &Vma, start_addr: usize, end_addr: usize) -> Self { Vma { start_addr, diff --git a/scripts/make/build.mk b/scripts/make/build.mk index aee2098af..3672ff47c 100644 --- a/scripts/make/build.mk +++ b/scripts/make/build.mk @@ -41,11 +41,7 @@ ifeq ($(APP_TYPE), rust) $(call cargo_build,--manifest-path $(APP)/Cargo.toml,$(RUX_FEAT) $(LIB_FEAT) $(APP_FEAT)) @cp $(rust_elf) $(OUT_ELF) else ifeq ($(APP_TYPE), c) - ifeq ($(MUSL), y) - $(call cargo_build,-p ruxmusl,$(RUX_FEAT) $(LIB_FEAT)) - else - $(call cargo_build,-p ruxlibc,$(RUX_FEAT) $(LIB_FEAT)) - endif + $(call cargo_build,-p ruxmusl,$(RUX_FEAT) $(LIB_FEAT)) endif $(OUT_DIR): diff --git a/scripts/make/build_c.mk b/scripts/make/build_c.mk index d1522f98a..148de9c82 100644 --- a/scripts/make/build_c.mk +++ b/scripts/make/build_c.mk @@ -1,7 +1,7 @@ -rust_lib_name := ruxlibc +rust_lib_name := ruxmusl rust_lib := target/$(TARGET)/$(MODE)/lib$(rust_lib_name).a -ulib_dir := ulib/ruxlibc +ulib_dir := ulib/ruxmusl src_dir := $(ulib_dir)/c obj_dir := $(ulib_dir)/build_$(ARCH) inc_dir := $(ulib_dir)/include diff --git a/scripts/make/cargo.mk b/scripts/make/cargo.mk index 308896d27..d2d84c2fd 100644 --- a/scripts/make/cargo.mk +++ b/scripts/make/cargo.mk @@ -40,7 +40,7 @@ endef all_packages := \ $(shell ls $(CURDIR)/crates) \ $(shell ls $(CURDIR)/modules) \ - ruxfeat arceos_api axstd ruxlibc ruxmusl + ruxfeat arceos_api axstd ruxmusl define cargo_doc $(call run_cmd,cargo doc,--no-deps --all-features --workspace --exclude "arceos-*" --exclude "ruxos-*" --exclude "ruxos-*" $(verbose)) diff --git a/scripts/make/features.mk b/scripts/make/features.mk index 4a07c7e5c..a79d5ac8d 100644 --- a/scripts/make/features.mk +++ b/scripts/make/features.mk @@ -3,21 +3,17 @@ # Inputs: # - `FEATURES`: a list of features to be enabled split by spaces or commas. # The features can be selected from the crate `ruxfeat` or the user library -# (crate `axstd` or `ruxlibc`). +# (crate `axstd` or `ruxmusl`). # - `APP_FEATURES`: a list of features to be enabled for the Rust app. # # Outputs: # - `RUX_FEAT`: features to be enabled for Ruxos modules (crate `ruxfeat`). -# - `LIB_FEAT`: features to be enabled for the user library (crate `axstd`, `ruxlibc`, `ruxmusl`). +# - `LIB_FEAT`: features to be enabled for the user library (crate `axstd`, `ruxmusl`, `ruxmusl`). # - `APP_FEAT`: features to be enabled for the Rust app. ifeq ($(APP_TYPE),c) ax_feat_prefix := ruxfeat/ - ifeq ($(MUSL), y) - lib_feat_prefix := ruxmusl/ - else - lib_feat_prefix := ruxlibc/ - endif + lib_feat_prefix := ruxmusl/ lib_features := fp_simd alloc irq sched_rr paging multitask fs net fd pipe select poll epoll random-hw signal else # TODO: it's better to use `ruxfeat/` as `ax_feat_prefix`, but all apps need to have `ruxfeat` as a dependency @@ -32,6 +28,11 @@ ifeq ($(APP_TYPE),c) endif override FEATURES := $(shell echo $(FEATURES) | tr ',' ' ') +# TODO: remove below features when it is possible +override FEATURES += multitask +override FEATURES += fs +override FEATURES += paging +override FEATURES += alloc ifeq ($(APP_TYPE), c) ifneq ($(wildcard $(APP)/features.txt),) # check features.txt exists diff --git a/scripts/make/test.mk b/scripts/make/test.mk index b132654a6..8dcde7b4f 100644 --- a/scripts/make/test.mk +++ b/scripts/make/test.mk @@ -1,8 +1,10 @@ # Test scripts +# for unit tests, try to run the tests + define unit_test - $(call run_cmd,cargo test,-p percpu $(1) -- --nocapture) - $(call run_cmd,cargo test,-p ruxfs $(1) --features "myfs" -- --nocapture) + $(call run_cmd,cargo test,-p percpu $(1) -- --nocapture) + $(call run_cmd,cargo test,-p ruxruntime $(1) --features "myfs" -- --nocapture) $(call run_cmd,cargo test,--workspace --exclude lwip_rust --exclude "arceos-*" --exclude "ruxos-*" $(1) -- --nocapture) endef diff --git a/scripts/test/app_test.sh b/scripts/test/app_test.sh index b81ae0885..598ffcf53 100755 --- a/scripts/test/app_test.sh +++ b/scripts/test/app_test.sh @@ -108,8 +108,8 @@ if [ -z "$1" ]; then test_list=( "apps/c/helloworld" "apps/c/memtest" - "apps/c/sqlite3" "apps/c/httpclient" + "apps/c/sqlite3" "apps/c/pthread/basic" "apps/c/pthread/sleep" "apps/c/pthread/pipe" diff --git a/ulib/axstd/Cargo.toml b/ulib/axstd/Cargo.toml index 7c604d674..c583baf4a 100644 --- a/ulib/axstd/Cargo.toml +++ b/ulib/axstd/Cargo.toml @@ -36,7 +36,7 @@ alloc = ["arceos_api/alloc", "ruxfeat/alloc", "axio/alloc"] alloc-tlsf = ["ruxfeat/alloc-tlsf"] alloc-slab = ["ruxfeat/alloc-slab"] alloc-buddy = ["ruxfeat/alloc-buddy"] -paging = ["ruxfeat/paging"] +paging = ["ruxfeat/paging", "arceos_api/paging"] tls = ["ruxfeat/tls"] # Multi-threading and scheduler diff --git a/ulib/include/.gitignore b/ulib/include/.gitignore new file mode 100644 index 000000000..9a162c2b8 --- /dev/null +++ b/ulib/include/.gitignore @@ -0,0 +1,2 @@ +ax_pthread_cond.h +ax_pthread_mutex.h diff --git a/ulib/ruxlibc/include/arpa/inet.h b/ulib/include/arpa/inet.h similarity index 100% rename from ulib/ruxlibc/include/arpa/inet.h rename to ulib/include/arpa/inet.h diff --git a/ulib/ruxlibc/include/assert.h b/ulib/include/assert.h similarity index 100% rename from ulib/ruxlibc/include/assert.h rename to ulib/include/assert.h diff --git a/ulib/ruxlibc/include/byteswap.h b/ulib/include/byteswap.h similarity index 100% rename from ulib/ruxlibc/include/byteswap.h rename to ulib/include/byteswap.h diff --git a/ulib/ruxlibc/include/crypt.h b/ulib/include/crypt.h similarity index 100% rename from ulib/ruxlibc/include/crypt.h rename to ulib/include/crypt.h diff --git a/ulib/ruxlibc/include/ctype.h b/ulib/include/ctype.h similarity index 100% rename from ulib/ruxlibc/include/ctype.h rename to ulib/include/ctype.h diff --git a/ulib/ruxlibc/include/dirent.h b/ulib/include/dirent.h similarity index 100% rename from ulib/ruxlibc/include/dirent.h rename to ulib/include/dirent.h diff --git a/ulib/ruxlibc/include/dlfcn.h b/ulib/include/dlfcn.h similarity index 100% rename from ulib/ruxlibc/include/dlfcn.h rename to ulib/include/dlfcn.h diff --git a/ulib/ruxlibc/include/endian.h b/ulib/include/endian.h similarity index 100% rename from ulib/ruxlibc/include/endian.h rename to ulib/include/endian.h diff --git a/ulib/ruxlibc/include/errno.h b/ulib/include/errno.h similarity index 100% rename from ulib/ruxlibc/include/errno.h rename to ulib/include/errno.h diff --git a/ulib/ruxlibc/include/fcntl.h b/ulib/include/fcntl.h similarity index 100% rename from ulib/ruxlibc/include/fcntl.h rename to ulib/include/fcntl.h diff --git a/ulib/ruxlibc/include/features.h b/ulib/include/features.h similarity index 100% rename from ulib/ruxlibc/include/features.h rename to ulib/include/features.h diff --git a/ulib/ruxlibc/include/float.h b/ulib/include/float.h similarity index 100% rename from ulib/ruxlibc/include/float.h rename to ulib/include/float.h diff --git a/ulib/ruxlibc/include/fnmatch.h b/ulib/include/fnmatch.h similarity index 100% rename from ulib/ruxlibc/include/fnmatch.h rename to ulib/include/fnmatch.h diff --git a/ulib/ruxlibc/include/glob.h b/ulib/include/glob.h similarity index 100% rename from ulib/ruxlibc/include/glob.h rename to ulib/include/glob.h diff --git a/ulib/ruxlibc/include/grp.h b/ulib/include/grp.h similarity index 100% rename from ulib/ruxlibc/include/grp.h rename to ulib/include/grp.h diff --git a/ulib/ruxlibc/include/inttypes.h b/ulib/include/inttypes.h similarity index 100% rename from ulib/ruxlibc/include/inttypes.h rename to ulib/include/inttypes.h diff --git a/ulib/ruxlibc/include/ksigaction.h b/ulib/include/ksigaction.h similarity index 100% rename from ulib/ruxlibc/include/ksigaction.h rename to ulib/include/ksigaction.h diff --git a/ulib/ruxlibc/include/langinfo.h b/ulib/include/langinfo.h similarity index 100% rename from ulib/ruxlibc/include/langinfo.h rename to ulib/include/langinfo.h diff --git a/ulib/ruxlibc/include/libgen.h b/ulib/include/libgen.h similarity index 100% rename from ulib/ruxlibc/include/libgen.h rename to ulib/include/libgen.h diff --git a/ulib/ruxlibc/include/limits.h b/ulib/include/limits.h similarity index 100% rename from ulib/ruxlibc/include/limits.h rename to ulib/include/limits.h diff --git a/ulib/ruxlibc/include/locale.h b/ulib/include/locale.h similarity index 100% rename from ulib/ruxlibc/include/locale.h rename to ulib/include/locale.h diff --git a/ulib/ruxlibc/include/malloc.h b/ulib/include/malloc.h similarity index 100% rename from ulib/ruxlibc/include/malloc.h rename to ulib/include/malloc.h diff --git a/ulib/ruxlibc/include/math.h b/ulib/include/math.h similarity index 100% rename from ulib/ruxlibc/include/math.h rename to ulib/include/math.h diff --git a/ulib/ruxlibc/include/memory.h b/ulib/include/memory.h similarity index 100% rename from ulib/ruxlibc/include/memory.h rename to ulib/include/memory.h diff --git a/ulib/ruxlibc/include/netdb.h b/ulib/include/netdb.h similarity index 100% rename from ulib/ruxlibc/include/netdb.h rename to ulib/include/netdb.h diff --git a/ulib/ruxlibc/include/netinet/in.h b/ulib/include/netinet/in.h similarity index 100% rename from ulib/ruxlibc/include/netinet/in.h rename to ulib/include/netinet/in.h diff --git a/ulib/ruxlibc/include/netinet/tcp.h b/ulib/include/netinet/tcp.h similarity index 100% rename from ulib/ruxlibc/include/netinet/tcp.h rename to ulib/include/netinet/tcp.h diff --git a/ulib/ruxlibc/include/nscd.h b/ulib/include/nscd.h similarity index 100% rename from ulib/ruxlibc/include/nscd.h rename to ulib/include/nscd.h diff --git a/ulib/ruxlibc/include/poll.h b/ulib/include/poll.h similarity index 100% rename from ulib/ruxlibc/include/poll.h rename to ulib/include/poll.h diff --git a/ulib/ruxlibc/include/pthread.h b/ulib/include/pthread.h similarity index 100% rename from ulib/ruxlibc/include/pthread.h rename to ulib/include/pthread.h diff --git a/ulib/ruxlibc/include/pwd.h b/ulib/include/pwd.h similarity index 100% rename from ulib/ruxlibc/include/pwd.h rename to ulib/include/pwd.h diff --git a/ulib/ruxlibc/include/regex.h b/ulib/include/regex.h similarity index 100% rename from ulib/ruxlibc/include/regex.h rename to ulib/include/regex.h diff --git a/ulib/ruxlibc/include/sched.h b/ulib/include/sched.h similarity index 100% rename from ulib/ruxlibc/include/sched.h rename to ulib/include/sched.h diff --git a/ulib/ruxlibc/include/semaphore.h b/ulib/include/semaphore.h similarity index 100% rename from ulib/ruxlibc/include/semaphore.h rename to ulib/include/semaphore.h diff --git a/ulib/ruxlibc/include/setjmp.h b/ulib/include/setjmp.h similarity index 100% rename from ulib/ruxlibc/include/setjmp.h rename to ulib/include/setjmp.h diff --git a/ulib/ruxlibc/include/signal.h b/ulib/include/signal.h similarity index 100% rename from ulib/ruxlibc/include/signal.h rename to ulib/include/signal.h diff --git a/ulib/ruxlibc/include/stdarg.h b/ulib/include/stdarg.h similarity index 100% rename from ulib/ruxlibc/include/stdarg.h rename to ulib/include/stdarg.h diff --git a/ulib/ruxlibc/include/stdbool.h b/ulib/include/stdbool.h similarity index 100% rename from ulib/ruxlibc/include/stdbool.h rename to ulib/include/stdbool.h diff --git a/ulib/ruxlibc/include/stddef.h b/ulib/include/stddef.h similarity index 100% rename from ulib/ruxlibc/include/stddef.h rename to ulib/include/stddef.h diff --git a/ulib/ruxlibc/include/stdint.h b/ulib/include/stdint.h similarity index 100% rename from ulib/ruxlibc/include/stdint.h rename to ulib/include/stdint.h diff --git a/ulib/ruxlibc/include/stdio.h b/ulib/include/stdio.h similarity index 100% rename from ulib/ruxlibc/include/stdio.h rename to ulib/include/stdio.h diff --git a/ulib/ruxlibc/include/stdlib.h b/ulib/include/stdlib.h similarity index 100% rename from ulib/ruxlibc/include/stdlib.h rename to ulib/include/stdlib.h diff --git a/ulib/ruxlibc/include/string.h b/ulib/include/string.h similarity index 100% rename from ulib/ruxlibc/include/string.h rename to ulib/include/string.h diff --git a/ulib/ruxlibc/include/strings.h b/ulib/include/strings.h similarity index 100% rename from ulib/ruxlibc/include/strings.h rename to ulib/include/strings.h diff --git a/ulib/ruxlibc/include/sys/epoll.h b/ulib/include/sys/epoll.h similarity index 100% rename from ulib/ruxlibc/include/sys/epoll.h rename to ulib/include/sys/epoll.h diff --git a/ulib/ruxlibc/include/sys/eventfd.h b/ulib/include/sys/eventfd.h similarity index 100% rename from ulib/ruxlibc/include/sys/eventfd.h rename to ulib/include/sys/eventfd.h diff --git a/ulib/ruxlibc/include/sys/file.h b/ulib/include/sys/file.h similarity index 100% rename from ulib/ruxlibc/include/sys/file.h rename to ulib/include/sys/file.h diff --git a/ulib/ruxlibc/include/sys/ioctl.h b/ulib/include/sys/ioctl.h similarity index 100% rename from ulib/ruxlibc/include/sys/ioctl.h rename to ulib/include/sys/ioctl.h diff --git a/ulib/ruxlibc/include/sys/mman.h b/ulib/include/sys/mman.h similarity index 100% rename from ulib/ruxlibc/include/sys/mman.h rename to ulib/include/sys/mman.h diff --git a/ulib/ruxlibc/include/sys/param.h b/ulib/include/sys/param.h similarity index 100% rename from ulib/ruxlibc/include/sys/param.h rename to ulib/include/sys/param.h diff --git a/ulib/ruxlibc/include/sys/prctl.h b/ulib/include/sys/prctl.h similarity index 100% rename from ulib/ruxlibc/include/sys/prctl.h rename to ulib/include/sys/prctl.h diff --git a/ulib/ruxlibc/include/sys/random.h b/ulib/include/sys/random.h similarity index 100% rename from ulib/ruxlibc/include/sys/random.h rename to ulib/include/sys/random.h diff --git a/ulib/ruxlibc/include/sys/resource.h b/ulib/include/sys/resource.h similarity index 100% rename from ulib/ruxlibc/include/sys/resource.h rename to ulib/include/sys/resource.h diff --git a/ulib/ruxlibc/include/sys/select.h b/ulib/include/sys/select.h similarity index 100% rename from ulib/ruxlibc/include/sys/select.h rename to ulib/include/sys/select.h diff --git a/ulib/ruxlibc/include/sys/sendfile.h b/ulib/include/sys/sendfile.h similarity index 100% rename from ulib/ruxlibc/include/sys/sendfile.h rename to ulib/include/sys/sendfile.h diff --git a/ulib/ruxlibc/include/sys/socket.h b/ulib/include/sys/socket.h similarity index 100% rename from ulib/ruxlibc/include/sys/socket.h rename to ulib/include/sys/socket.h diff --git a/ulib/ruxlibc/include/sys/stat.h b/ulib/include/sys/stat.h similarity index 100% rename from ulib/ruxlibc/include/sys/stat.h rename to ulib/include/sys/stat.h diff --git a/ulib/ruxlibc/include/sys/statfs.h b/ulib/include/sys/statfs.h similarity index 100% rename from ulib/ruxlibc/include/sys/statfs.h rename to ulib/include/sys/statfs.h diff --git a/ulib/ruxlibc/include/sys/statvfs.h b/ulib/include/sys/statvfs.h similarity index 100% rename from ulib/ruxlibc/include/sys/statvfs.h rename to ulib/include/sys/statvfs.h diff --git a/ulib/ruxlibc/include/sys/syscall.h b/ulib/include/sys/syscall.h similarity index 100% rename from ulib/ruxlibc/include/sys/syscall.h rename to ulib/include/sys/syscall.h diff --git a/ulib/ruxlibc/include/sys/sysinfo.h b/ulib/include/sys/sysinfo.h similarity index 100% rename from ulib/ruxlibc/include/sys/sysinfo.h rename to ulib/include/sys/sysinfo.h diff --git a/ulib/ruxlibc/include/sys/time.h b/ulib/include/sys/time.h similarity index 100% rename from ulib/ruxlibc/include/sys/time.h rename to ulib/include/sys/time.h diff --git a/ulib/ruxlibc/include/sys/types.h b/ulib/include/sys/types.h similarity index 100% rename from ulib/ruxlibc/include/sys/types.h rename to ulib/include/sys/types.h diff --git a/ulib/ruxlibc/include/sys/uio.h b/ulib/include/sys/uio.h similarity index 100% rename from ulib/ruxlibc/include/sys/uio.h rename to ulib/include/sys/uio.h diff --git a/ulib/ruxlibc/include/sys/un.h b/ulib/include/sys/un.h similarity index 100% rename from ulib/ruxlibc/include/sys/un.h rename to ulib/include/sys/un.h diff --git a/ulib/ruxlibc/include/sys/utsname.h b/ulib/include/sys/utsname.h similarity index 100% rename from ulib/ruxlibc/include/sys/utsname.h rename to ulib/include/sys/utsname.h diff --git a/ulib/ruxlibc/include/sys/vfs.h b/ulib/include/sys/vfs.h similarity index 100% rename from ulib/ruxlibc/include/sys/vfs.h rename to ulib/include/sys/vfs.h diff --git a/ulib/ruxlibc/include/sys/wait.h b/ulib/include/sys/wait.h similarity index 100% rename from ulib/ruxlibc/include/sys/wait.h rename to ulib/include/sys/wait.h diff --git a/ulib/ruxlibc/include/syslog.h b/ulib/include/syslog.h similarity index 100% rename from ulib/ruxlibc/include/syslog.h rename to ulib/include/syslog.h diff --git a/ulib/ruxlibc/include/termios.h b/ulib/include/termios.h similarity index 100% rename from ulib/ruxlibc/include/termios.h rename to ulib/include/termios.h diff --git a/ulib/ruxlibc/include/time.h b/ulib/include/time.h similarity index 100% rename from ulib/ruxlibc/include/time.h rename to ulib/include/time.h diff --git a/ulib/ruxlibc/include/unistd.h b/ulib/include/unistd.h similarity index 100% rename from ulib/ruxlibc/include/unistd.h rename to ulib/include/unistd.h diff --git a/ulib/ruxlibc/.gitignore b/ulib/ruxlibc/.gitignore deleted file mode 100644 index 792f33c1e..000000000 --- a/ulib/ruxlibc/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -src/libctypes_gen.rs -include/ax_pthread_mutex.h -include/ax_pthread_cond.h -build_* diff --git a/ulib/ruxlibc/Cargo.toml b/ulib/ruxlibc/Cargo.toml deleted file mode 100644 index 8b00b811f..000000000 --- a/ulib/ruxlibc/Cargo.toml +++ /dev/null @@ -1,67 +0,0 @@ -[package] -name = "ruxlibc" -version = "0.1.0" -edition = "2021" -authors = [ - "Yuekai Jia ", - "yanjuguang ", - "wudashuai ", - "yfblock <321353225@qq.com>", - "scPointer ", - "Shiping Yuan ", -] -description = "Ruxos user program library for C apps" -license = "GPL-3.0-or-later OR Apache-2.0" -homepage = "https://github.com/syswonder/ruxos" -repository = "https://github.com/syswonder/ruxos/tree/main/ulib/ruxlibc" - -[lib] -crate-type = ["staticlib"] - -[features] -default = [] - -# Multicore -smp = ["ruxos_posix_api/smp"] - -# Floating point/SIMD -fp_simd = ["ruxfeat/fp_simd"] - -# Memory -alloc = ["ruxos_posix_api/alloc"] -paging = ["alloc", "ruxos_posix_api/paging"] -tls = ["alloc", "ruxfeat/tls"] - -# Multi-task -multitask = ["ruxos_posix_api/multitask"] - -# File system -fs = ["ruxos_posix_api/fs", "fd"] - -# Networking -net = ["ruxos_posix_api/net", "fd"] - -# Signal -signal = ["ruxos_posix_api/signal"] - -# Libc features -fd = [] -pipe = ["ruxos_posix_api/pipe"] -select = ["ruxos_posix_api/select"] -poll = ["ruxos_posix_api/poll"] -epoll = ["ruxos_posix_api/epoll"] -random-hw = ["ruxos_posix_api/random-hw"] - -# Interrupts -irq = ["ruxos_posix_api/irq", "ruxfeat/irq"] - -sched_rr = ["irq", "ruxfeat/sched_rr"] - -[dependencies] -ruxfeat = { path = "../../api/ruxfeat" } -ruxos_posix_api = { path = "../../api/ruxos_posix_api" } -axio = { path = "../../crates/axio" } -axerrno = { path = "../../crates/axerrno" } - -[build-dependencies] -bindgen ={ version = "0.66" } diff --git a/ulib/ruxlibc/build.rs b/ulib/ruxlibc/build.rs deleted file mode 100644 index 7a8501edd..000000000 --- a/ulib/ruxlibc/build.rs +++ /dev/null @@ -1,33 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -fn main() { - fn gen_c_to_rust_bindings(in_file: &str, out_file: &str) { - println!("cargo:rerun-if-changed={in_file}"); - - let allow_types = ["tm", "jmp_buf"]; - let mut builder = bindgen::Builder::default() - .header(in_file) - .clang_arg("-isystem./include") - .derive_default(true) - .size_t_is_usize(false) - .use_core(); - for ty in allow_types { - builder = builder.allowlist_type(ty); - } - - builder - .generate() - .expect("Unable to generate c->rust bindings") - .write_to_file(out_file) - .expect("Couldn't write bindings!"); - } - - gen_c_to_rust_bindings("ctypes.h", "src/libctypes_gen.rs"); -} diff --git a/ulib/ruxlibc/c/assert.c b/ulib/ruxlibc/c/assert.c deleted file mode 100644 index 4d9060c17..000000000 --- a/ulib/ruxlibc/c/assert.c +++ /dev/null @@ -1,17 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -_Noreturn void __assert_fail(const char *expr, const char *file, int line, const char *func) -{ - fprintf(stderr, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line); - abort(); -} diff --git a/ulib/ruxlibc/c/ctype.c b/ulib/ruxlibc/c/ctype.c deleted file mode 100644 index 3ec36db30..000000000 --- a/ulib/ruxlibc/c/ctype.c +++ /dev/null @@ -1,26 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -int tolower(int c) -{ - if (isupper(c)) - return c | 32; - return c; -} - -int toupper(int c) -{ - if (islower(c)) - return c & 0x5f; - return c; -} diff --git a/ulib/ruxlibc/c/dirent.c b/ulib/ruxlibc/c/dirent.c deleted file mode 100644 index da9fc05e8..000000000 --- a/ulib/ruxlibc/c/dirent.c +++ /dev/null @@ -1,107 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_FS - -#include -#include -#include -#include -#include -#include -#include -#include - -int closedir(DIR *dir) -{ - int ret = close(dir->fd); - free(dir); - return ret; -} - -DIR *fdopendir(int fd) -{ - DIR *dir; - struct stat st; - - if (fstat(fd, &st) < 0) { - return 0; - } - if (fcntl(fd, F_GETFL) & O_PATH) { - errno = EBADF; - return 0; - } - if (!S_ISDIR(st.st_mode)) { - errno = ENOTDIR; - return 0; - } - if (!(dir = calloc(1, sizeof(*dir)))) { - return 0; - } - - fcntl(fd, F_SETFD, FD_CLOEXEC); - dir->fd = fd; - return dir; -} - -int dirfd(DIR *d) -{ - return d->fd; -} - -// TODO -DIR *opendir(const char *__name) -{ - unimplemented(); - return NULL; -} - -// TODO -struct dirent *readdir(DIR *__dirp) -{ - unimplemented(); - return NULL; -} - -// TODO -int readdir_r(DIR *restrict dir, struct dirent *restrict buf, struct dirent **restrict result) -{ - struct dirent *de; - int errno_save = errno; - int ret; - - // LOCK(dir->lock); - errno = 0; - de = readdir(dir); - if ((ret = errno)) { - // UNLOCK(dir->lock); - return ret; - } - errno = errno_save; - if (de) - memcpy(buf, de, de->d_reclen); - else - buf = NULL; - - // UNLOCK(dir->lock); - *result = buf; - return 0; -} - -// TODO -void rewinddir(DIR *dir) -{ - // LOCK(dir->lock); - lseek(dir->fd, 0, SEEK_SET); - dir->buf_pos = dir->buf_end = 0; - dir->tell = 0; - // UNLOCK(dir->lock); -} - -#endif // RUX_CONFIG_FS diff --git a/ulib/ruxlibc/c/dlfcn.c b/ulib/ruxlibc/c/dlfcn.c deleted file mode 100644 index 5fc088d02..000000000 --- a/ulib/ruxlibc/c/dlfcn.c +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -// TODO -int dladdr(const void *__address, Dl_info *__info) -{ - unimplemented(); - return 0; -} - -// TODO -void *dlopen(const char *__file, int __mode) -{ - unimplemented(); - return NULL; -} - -// TODO -char *dlerror() -{ - unimplemented(); - return NULL; -} - -// TODO -void *dlsym(void *__restrict__ __handle, const char *__restrict__ __name) -{ - - unimplemented(); - return NULL; -} - -// TODO -int dlclose(void *p) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/fcntl.c b/ulib/ruxlibc/c/fcntl.c deleted file mode 100644 index 1c2aeedad..000000000 --- a/ulib/ruxlibc/c/fcntl.c +++ /dev/null @@ -1,71 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -#ifdef RUX_CONFIG_FD - -// TODO: remove this function in future work -int ax_fcntl(int fd, int cmd, size_t arg); - -int fcntl(int fd, int cmd, ... /* arg */) -{ - unsigned long arg; - va_list ap; - va_start(ap, cmd); - arg = va_arg(ap, unsigned long); - va_end(ap); - - return ax_fcntl(fd, cmd, arg); -} - -#endif // RUX_CONFIG_FD - -#ifdef RUX_CONFIG_FS - -// TODO: remove this function in future work -int ax_open(const char *filename, int flags, mode_t mode); - -int open(const char *filename, int flags, ...) -{ - mode_t mode = 0; - - if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) { - va_list ap; - va_start(ap, flags); - mode = va_arg(ap, mode_t); - va_end(ap); - } - - return ax_open(filename, flags, mode); -} - -// TODO -int posix_fadvise(int __fd, unsigned long __offset, unsigned long __len, int __advise) -{ - unimplemented(); - return 0; -} - -// TODO -int sync_file_range(int fd, off_t pos, off_t len, unsigned flags) -{ - unimplemented(); - return 0; -} - -int openat(int dirfd, const char *pathname, int flags, ...) -{ - unimplemented(); - return 0; -} - -#endif // RUX_CONFIG_FS diff --git a/ulib/ruxlibc/c/flock.c b/ulib/ruxlibc/c/flock.c deleted file mode 100644 index 36859d25c..000000000 --- a/ulib/ruxlibc/c/flock.c +++ /dev/null @@ -1,18 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int flock(int __fd, int __operation) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/fnmatch.c b/ulib/ruxlibc/c/fnmatch.c deleted file mode 100644 index 7e88e5970..000000000 --- a/ulib/ruxlibc/c/fnmatch.c +++ /dev/null @@ -1,304 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include - -#define END 0 -#define UNMATCHABLE -2 -#define BRACKET -3 -#define QUESTION -4 -#define STAR -5 - -// TODO: When wide characters implemented, remove annotations according to musl. - -static int str_next(const char *str, size_t n, size_t *step) -{ - if (!n) { - *step = 0; - return 0; - } - // TODO: When wide characters implemented, remove annotation - // if (str[0] >= 128U) { - // wchar_t wc; - // int k = mbtowc(&wc, str, n); - // if (k<0) { - // *step = 1; - // return -1; - // } - // *step = k; - // return wc; - // } - *step = 1; - return str[0]; -} - -static int pat_next(const char *pat, size_t m, size_t *step, int flags) -{ - if (!m || !*pat) { - *step = 0; - return END; - } - *step = 1; - if (pat[0]=='\\' && pat[1] && !(flags & FNM_NOESCAPE)) { - *step = 2; - pat++; - goto escaped; - } - if (pat[0]=='[') { - size_t k = 1; - if (k= 128U) { - // wchar_t wc; - // int k = mbtowc(&wc, pat, m); - // if (k<0) { - // *step = 0; - // return UNMATCHABLE; - // } - // *step = k + esc; - // return wc; - // } - return pat[0]; -} - -static int casefold(int k) -{ - int c = toupper(k); - return c == k ? tolower(k) : c; - -} - -static int match_bracket(const char *p, int k, int kfold) -{ - // TODO: When wide characters implemented, remove annotation and delete next line - // wchar_t wc; - char wc; - int inv = 0; - p++; - if (*p=='^' || *p=='!') { - inv = 1; - p++; - } - if (*p==']') { - if (k==']') return !inv; - p++; - } else if (*p=='-') { - if (k=='-') return !inv; - p++; - } - wc = p[-1]; - for (; *p != ']'; p++) { - if (p[0]=='-' && p[1]!=']') { - // TODO: When wide characters implemented, remove annotation - // wchar_t wc2; - // int l = mbtowc(&wc2, p+1, 4); - // if (l < 0) return 0; - char wc2 = p[1]; - if (wc <= wc2) - if ((unsigned)k-wc <= wc2-wc || - (unsigned)kfold-wc <= wc2-wc) - return !inv; - // TODO: When wide characters implemented, remove annotation and delete next line - // p += l-1; - p += 1; - continue; - } - if (*p < 128U) { - wc = (unsigned char)*p; - } - if (wc==k || wc==kfold) return !inv; - } - return inv; -} - -static int fnmatch_internal(const char *pat, size_t m, const char *str, size_t n, int flags) -{ - const char *p, *ptail, *endpat; - const char *s, *stail, *endstr; - size_t pinc, sinc, tailcnt=0; - int c, k, kfold; - - if (flags & FNM_PERIOD) { - if (*str == '.' && *pat != '.') - return FNM_NOMATCH; - } - for (;;) { - switch ((c = pat_next(pat, m, &pinc, flags))) { - case UNMATCHABLE: - return FNM_NOMATCH; - case STAR: - pat++; - m--; - break; - default: - k = str_next(str, n, &sinc); - if (k <= 0) - return (c==END) ? 0 : FNM_NOMATCH; - str += sinc; - n -= sinc; - kfold = flags & FNM_CASEFOLD ? casefold(k) : k; - if (c == BRACKET) { - if (!match_bracket(pat, k, kfold)) - return FNM_NOMATCH; - } else if (c != QUESTION && k != c && kfold != c) { - return FNM_NOMATCH; - } - pat+=pinc; - m-=pinc; - continue; - } - break; - } - - /* Compute real pat length if it was initially unknown/-1 */ - m = strnlen(pat, m); - endpat = pat + m; - - /* Find the last * in pat and count chars needed after it */ - for (p=ptail=pat; pstr && tailcnt; tailcnt--) { - // TODO: When wide characters implemented, remove annotation - // if (s[-1] < 128U || MB_CUR_MAX==1) s--; - // else while ((unsigned char)*--s-0x80U<0x40 && s>str); - s--; - } - if (tailcnt) return FNM_NOMATCH; - stail = s; - - /* Check that the pat and str tails match */ - p = ptail; - for (;;) { - c = pat_next(p, endpat-p, &pinc, flags); - p += pinc; - if ((k = str_next(s, endstr-s, &sinc)) <= 0) { - if (c != END) return FNM_NOMATCH; - break; - } - s += sinc; - kfold = flags & FNM_CASEFOLD ? casefold(k) : k; - if (c == BRACKET) { - if (!match_bracket(p-pinc, k, kfold)) - return FNM_NOMATCH; - } else if (c != QUESTION && k != c && kfold != c) { - return FNM_NOMATCH; - } - } - - /* We're all done with the tails now, so throw them out */ - endstr = stail; - endpat = ptail; - - /* Match pattern components until there are none left */ - while (pat 0) str += sinc; - else for (str++; str_next(str, endstr-str, &sinc)<0; str++); - } - - return 0; -} - -int fnmatch(const char *pat, const char *str, int flags) -{ - const char *s, *p; - size_t inc; - int c; - if (flags & FNM_PATHNAME) for (;;) { - for (s=str; *s && *s!='/'; s++); - for (p=pat; (c=pat_next(p, -1, &inc, flags))!=END && c!='/'; p+=inc); - if (c!=*s && (!*s || !(flags & FNM_LEADING_DIR))) - return FNM_NOMATCH; - if (fnmatch_internal(pat, p-pat, str, s-str, flags)) - return FNM_NOMATCH; - if (!c) return 0; - str = s+1; - pat = p+inc; - } else if (flags & FNM_LEADING_DIR) { - for (s=str; *s; s++) { - if (*s != '/') continue; - if (!fnmatch_internal(pat, -1, str, s-str, flags)) - return 0; - } - } - return fnmatch_internal(pat, -1, str, -1, flags); -} diff --git a/ulib/ruxlibc/c/glob.c b/ulib/ruxlibc/c/glob.c deleted file mode 100644 index 6b6137424..000000000 --- a/ulib/ruxlibc/c/glob.c +++ /dev/null @@ -1,338 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_FS - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -struct match { - struct match *next; - char name[]; -}; - -static int append(struct match **tail, const char *name, size_t len, int mark) -{ - struct match *new = malloc(sizeof(struct match) + len + 2); - if (!new) - return -1; - (*tail)->next = new; - new->next = NULL; - memcpy(new->name, name, len + 1); - if (mark && len && name[len - 1] != '/') { - new->name[len] = '/'; - new->name[len + 1] = 0; - } - *tail = new; - return 0; -} - -static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, - int (*errfunc)(const char *path, int err), struct match **tail) -{ - /* If GLOB_MARK is unused, we don't care about type. */ - if (!type && !(flags & GLOB_MARK)) - type = DT_REG; - - /* Special-case the remaining pattern being all slashes, in - * which case we can use caller-passed type if it's a dir. */ - if (*pat && type != DT_DIR) - type = 0; - while (pos + 1 < PATH_MAX && *pat == '/') buf[pos++] = *pat++; - - /* Consume maximal [escaped-]literal prefix of pattern, copying - * and un-escaping it to the running buffer as we go. */ - long i = 0, j = 0; - int in_bracket = 0, overflow = 0; - for (; pat[i] != '*' && pat[i] != '?' && (!in_bracket || pat[i] != ']'); i++) { - if (!pat[i]) { - if (overflow) - return 0; - pat += i; - pos += j; - i = j = 0; - break; - } else if (pat[i] == '[') { - in_bracket = 1; - } else if (pat[i] == '\\' && !(flags & GLOB_NOESCAPE)) { - /* Backslashes inside a bracket are (at least by - * our interpretation) non-special, so if next - * char is ']' we have a complete expression. */ - if (in_bracket && pat[i + 1] == ']') - break; - /* Unpaired final backslash never matches. */ - if (!pat[i + 1]) - return 0; - i++; - } - if (pat[i] == '/') { - if (overflow) - return 0; - in_bracket = 0; - pat += i + 1; - i = -1; - pos += j + 1; - j = -1; - } - /* Only store a character if it fits in the buffer, but if - * a potential bracket expression is open, the overflow - * must be remembered and handled later only if the bracket - * is unterminated (and thereby a literal), so as not to - * disallow long bracket expressions with short matches. */ - if (pos + (j + 1) < PATH_MAX) { - buf[pos + j++] = pat[i]; - } else if (in_bracket) { - overflow = 1; - } else { - return 0; - } - /* If we consume any new components, the caller-passed type - * or dummy type from above is no longer valid. */ - type = 0; - } - buf[pos] = 0; - if (!*pat) { - /* If we consumed any components above, or if GLOB_MARK is - * requested and we don't yet know if the match is a dir, - * we must confirm the file exists and/or determine its type. - * - * If marking dirs, symlink type is inconclusive; we need the - * type for the symlink target, and therefore must try stat - * first unless type is known not to be a symlink. Otherwise, - * or if that fails, use lstat for determining existence to - * avoid false negatives in the case of broken symlinks. */ - struct stat st; - if ((flags & GLOB_MARK) && (!type || type == DT_LNK) && !stat(buf, &st)) { - if (S_ISDIR(st.st_mode)) - type = DT_DIR; - else - type = DT_REG; - } - if (!type && lstat(buf, &st)) { - if (errno != ENOENT && (errfunc(buf, errno) || (flags & GLOB_ERR))) - return GLOB_ABORTED; - return 0; - } - if (append(tail, buf, pos, (flags & GLOB_MARK) && type == DT_DIR)) - return GLOB_NOSPACE; - return 0; - } - char *p2 = strchr(pat, '/'), saved_sep = '/'; - /* Check if the '/' was escaped and, if so, remove the escape char - * so that it will not be unpaired when passed to fnmatch. */ - if (p2 && !(flags & GLOB_NOESCAPE)) { - char *p; - for (p = p2; p > pat && p[-1] == '\\'; p--) - ; - if ((p2 - p) % 2) { - p2--; - saved_sep = '\\'; - } - } - DIR *dir = opendir(pos ? buf : "."); - if (!dir) { - if (errfunc(buf, errno) || (flags & GLOB_ERR)) - return GLOB_ABORTED; - return 0; - } - int old_errno = errno; - struct dirent *de; - while (errno = 0, de = readdir(dir)) { - /* Quickly skip non-directories when there's pattern left. */ - if (p2 && de->d_type && de->d_type != DT_DIR && de->d_type != DT_LNK) - continue; - - size_t l = strlen(de->d_name); - if (l >= PATH_MAX - pos) - continue; - - if (p2) - *p2 = 0; - - int fnm_flags = ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) | - ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0); - - if (fnmatch(pat, de->d_name, fnm_flags)) - continue; - - /* With GLOB_PERIOD, don't allow matching . or .. unless - * fnmatch would match them with FNM_PERIOD rules in effect. */ - if (p2 && (flags & GLOB_PERIOD) && de->d_name[0] == '.' && - (!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2])) && - fnmatch(pat, de->d_name, fnm_flags | FNM_PERIOD)) - continue; - - memcpy(buf + pos, de->d_name, l + 1); - if (p2) - *p2 = saved_sep; - int r = do_glob(buf, pos + l, de->d_type, p2 ? p2 : "", flags, errfunc, tail); - if (r) { - closedir(dir); - return r; - } - } - int readerr = errno; - if (p2) - *p2 = saved_sep; - closedir(dir); - if (readerr && (errfunc(buf, errno) || (flags & GLOB_ERR))) - return GLOB_ABORTED; - errno = old_errno; - return 0; -} - -static int ignore_err(const char *path, int err) -{ - return 0; -} - -static void freelist(struct match *head) -{ - struct match *match, *next; - for (match = head->next; match; match = next) { - next = match->next; - free(match); - } -} - -static int sort(const void *a, const void *b) -{ - return strcmp(*(const char **)a, *(const char **)b); -} - -static int expand_tilde(char **pat, char *buf, size_t *pos) -{ - char *p = *pat + 1; - size_t i = 0; - - char delim, *name_end = strchrnul(p, '/'); - if ((delim = *name_end)) - *name_end++ = 0; - *pat = name_end; - - char *home = *p ? NULL : getenv("HOME"); - if (!home) { - struct passwd pw, *res; - switch (*p ? getpwnam_r(p, &pw, buf, PATH_MAX, &res) - : getpwuid_r(getuid(), &pw, buf, PATH_MAX, &res)) { - case ENOMEM: - return GLOB_NOSPACE; - case 0: - if (!res) - default: - return GLOB_NOMATCH; - } - home = pw.pw_dir; - } - while (i < PATH_MAX - 2 && *home) buf[i++] = *home++; - if (*home) - return GLOB_NOMATCH; - if ((buf[i] = delim)) - buf[++i] = 0; - *pos = i; - return 0; -} - -int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), - glob_t *restrict g) -{ - struct match head = {.next = NULL}, *tail = &head; - size_t cnt, i; - size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0; - int error = 0; - char buf[PATH_MAX]; - - if (!errfunc) - errfunc = ignore_err; - - if (!(flags & GLOB_APPEND)) { - g->gl_offs = offs; - g->gl_pathc = 0; - g->gl_pathv = NULL; - } - - if (*pat) { - char *p = strdup(pat); - if (!p) - return GLOB_NOSPACE; - buf[0] = 0; - size_t pos = 0; - char *s = p; - if ((flags & (GLOB_TILDE | GLOB_TILDE_CHECK)) && *p == '~') - error = expand_tilde(&s, buf, &pos); - if (!error) - error = do_glob(buf, pos, 0, s, flags, errfunc, &tail); - free(p); - } - - if (error == GLOB_NOSPACE) { - freelist(&head); - return error; - } - - for (cnt = 0, tail = head.next; tail; tail = tail->next, cnt++) - ; - if (!cnt) { - if (flags & GLOB_NOCHECK) { - tail = &head; - if (append(&tail, pat, strlen(pat), 0)) - return GLOB_NOSPACE; - cnt++; - } else - return GLOB_NOMATCH; - } - - if (flags & GLOB_APPEND) { - char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *)); - if (!pathv) { - freelist(&head); - return GLOB_NOSPACE; - } - g->gl_pathv = pathv; - offs += g->gl_pathc; - } else { - g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *)); - if (!g->gl_pathv) { - freelist(&head); - return GLOB_NOSPACE; - } - for (i = 0; i < offs; i++) g->gl_pathv[i] = NULL; - } - for (i = 0, tail = head.next; i < cnt; tail = tail->next, i++) - g->gl_pathv[offs + i] = tail->name; - g->gl_pathv[offs + i] = NULL; - g->gl_pathc += cnt; - - if (!(flags & GLOB_NOSORT)) - qsort(g->gl_pathv + offs, cnt, sizeof(char *), sort); - - return error; -} - -void globfree(glob_t *g) -{ - size_t i; - for (i = 0; i < g->gl_pathc; i++) - free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name)); - free(g->gl_pathv); - g->gl_pathc = 0; - g->gl_pathv = NULL; -} - -#endif // RUX_CONFIG_FS diff --git a/ulib/ruxlibc/c/grp.c b/ulib/ruxlibc/c/grp.c deleted file mode 100644 index eb858607c..000000000 --- a/ulib/ruxlibc/c/grp.c +++ /dev/null @@ -1,46 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include - -/* Group members */ -static char *g_members__[] = { RUX_DEFAULT_USER, NULL }; - -/* Default group */ -static struct group g__ = { - .gr_name = RUX_DEFAULT_GROUP, - .gr_passwd = RUX_DEFAULT_PASS, - .gr_gid = RUX_DEFAULT_GID, - .gr_mem = g_members__, -}; - -// TODO -int initgroups(const char *user, gid_t group) -{ - unimplemented(); - return 0; -} - -struct group *getgrnam(const char *name) -{ - struct group *res; - - if (name && !strcmp(name, g__.gr_name)) - res = &g__; - else { - res = NULL; - errno = ENOENT; - } - - return res; -} diff --git a/ulib/ruxlibc/c/ioctl.c b/ulib/ruxlibc/c/ioctl.c deleted file mode 100644 index 578622a46..000000000 --- a/ulib/ruxlibc/c/ioctl.c +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -int rux_ioctl(int fd, int cmd, size_t arg); - -// TODO -int ioctl(int fd, int request, ...) -{ - unsigned long arg; - va_list ap; - va_start(ap, request); - arg = va_arg(ap, unsigned long); - va_end(ap); - - return rux_ioctl(fd, request, arg); -} diff --git a/ulib/ruxlibc/c/libgen.c b/ulib/ruxlibc/c/libgen.c deleted file mode 100644 index d0ffcdced..000000000 --- a/ulib/ruxlibc/c/libgen.c +++ /dev/null @@ -1,42 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -char *dirname(char *s) -{ - size_t i; - if (!s || !*s) - return "."; - i = strlen(s) - 1; - for (; s[i] == '/'; i--) - if (!i) - return "/"; - for (; s[i] != '/'; i--) - if (!i) - return "."; - for (; s[i] == '/'; i--) - if (!i) - return "/"; - s[i + 1] = 0; - return s; -} - -char *basename(char *s) -{ - size_t i; - if (!s || !*s) - return "."; - i = strlen(s) - 1; - for (; i && s[i] == '/'; i--) s[i] = 0; - for (; i && s[i - 1] != '/'; i--) - ; - return s + i; -} diff --git a/ulib/ruxlibc/c/libm.c b/ulib/ruxlibc/c/libm.c deleted file mode 100644 index 26269c3ed..000000000 --- a/ulib/ruxlibc/c/libm.c +++ /dev/null @@ -1,26 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_FP_SIMD - -#include - -#include "libm.h" - -double __math_divzero(uint32_t sign) -{ - return fp_barrier(sign ? -1.0 : 1.0) / 0.0; -} - -double __math_invalid(double x) -{ - return (x - x) / (x - x); -} - -#endif // RUX_CONFIG_FP_SIMD diff --git a/ulib/ruxlibc/c/libm.h b/ulib/ruxlibc/c/libm.h deleted file mode 100644 index 51046ba8c..000000000 --- a/ulib/ruxlibc/c/libm.h +++ /dev/null @@ -1,242 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifndef _LIBM_H -#define _LIBM_H - -#if RUX_CONFIG_FP_SIMD - -#include -#include -#include - -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN -union ldshape { - long double f; - struct { - uint64_t m; - uint16_t se; - } i; -}; -#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN -/* This is the m68k variant of 80-bit long double, and this definition only works - * on archs where the alignment requirement of uint64_t is <= 4. */ -union ldshape { - long double f; - struct { - uint16_t se; - uint16_t pad; - uint64_t m; - } i; -}; -#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN -union ldshape { - long double f; - struct { - uint64_t lo; - uint32_t mid; - uint16_t top; - uint16_t se; - } i; - struct { - uint64_t lo; - uint64_t hi; - } i2; -}; -#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN -union ldshape { - long double f; - struct { - uint16_t se; - uint16_t top; - uint32_t mid; - uint64_t lo; - } i; - struct { - uint64_t hi; - uint64_t lo; - } i2; -}; -#else -#error Unsupported long double representation -#endif - -/* Support non-nearest rounding mode. */ -#define WANT_ROUNDING 1 -/* Support signaling NaNs. */ -#define WANT_SNAN 0 - -#if WANT_SNAN -#error SNaN is unsupported -#else -#define issignalingf_inline(x) 0 -#define issignaling_inline(x) 0 -#endif - -#ifndef TOINT_INTRINSICS -#define TOINT_INTRINSICS 0 -#endif - -#if TOINT_INTRINSICS -/* Round x to nearest int in all rounding modes, ties have to be rounded - consistently with converttoint so the results match. If the result - would be outside of [-2^31, 2^31-1] then the semantics is unspecified. */ -static double_t roundtoint(double_t); - -/* Convert x to nearest int in all rounding modes, ties have to be rounded - consistently with roundtoint. If the result is not representible in an - int32_t then the semantics is unspecified. */ -static int32_t converttoint(double_t); -#endif - -/* Helps static branch prediction so hot path can be better optimized. */ -#ifdef __GNUC__ -#define predict_true(x) __builtin_expect(!!(x), 1) -#define predict_false(x) __builtin_expect(x, 0) -#else -#define predict_true(x) (x) -#define predict_false(x) (x) -#endif - -/* Evaluate an expression as the specified type. With standard excess - precision handling a type cast or assignment is enough (with - -ffloat-store an assignment is required, in old compilers argument - passing and return statement may not drop excess precision). */ - -static inline float eval_as_float(float x) -{ - float y = x; - return y; -} - -static inline double eval_as_double(double x) -{ - double y = x; - return y; -} - -/* fp_barrier returns its input, but limits code transformations - as if it had a side-effect (e.g. observable io) and returned - an arbitrary value. */ - -#ifndef fp_barrierf -#define fp_barrierf fp_barrierf -static inline float fp_barrierf(float x) -{ - volatile float y = x; - return y; -} -#endif - -#ifndef fp_barrier -#define fp_barrier fp_barrier -static inline double fp_barrier(double x) -{ - volatile double y = x; - return y; -} -#endif - -#ifndef fp_barrierl -#define fp_barrierl fp_barrierl -static inline long double fp_barrierl(long double x) -{ - volatile long double y = x; - return y; -} -#endif - -/* fp_force_eval ensures that the input value is computed when that's - otherwise unused. To prevent the constant folding of the input - expression, an additional fp_barrier may be needed or a compilation - mode that does so (e.g. -frounding-math in gcc). Then it can be - used to evaluate an expression for its fenv side-effects only. */ - -#ifndef fp_force_evalf -#define fp_force_evalf fp_force_evalf -static inline void fp_force_evalf(float x) -{ - volatile float y; - y = x; -} -#endif - -#ifndef fp_force_eval -#define fp_force_eval fp_force_eval -static inline void fp_force_eval(double x) -{ - volatile double y; - y = x; -} -#endif - -#ifndef fp_force_evall -#define fp_force_evall fp_force_evall -static inline void fp_force_evall(long double x) -{ - volatile long double y; - y = x; -} -#endif - -#define FORCE_EVAL(x) \ - do { \ - if (sizeof(x) == sizeof(float)) { \ - fp_force_evalf(x); \ - } else if (sizeof(x) == sizeof(double)) { \ - fp_force_eval(x); \ - } else { \ - fp_force_evall(x); \ - } \ - } while (0) - -#define asuint(f) \ - ((union { \ - float _f; \ - uint32_t _i; \ - }){f}) \ - ._i -#define asfloat(i) \ - ((union { \ - uint32_t _i; \ - float _f; \ - }){i}) \ - ._f -#define asuint64(f) \ - ((union { \ - double _f; \ - uint64_t _i; \ - }){f}) \ - ._i -#define asdouble(i) \ - ((union { \ - uint64_t _i; \ - double _f; \ - }){i}) \ - ._f - -/* error handling functions */ -float __math_xflowf(uint32_t, float); -float __math_uflowf(uint32_t); -float __math_oflowf(uint32_t); -float __math_divzerof(uint32_t); -float __math_invalidf(float); -double __math_xflow(uint32_t, double); -double __math_uflow(uint32_t); -double __math_oflow(uint32_t); -double __math_divzero(uint32_t); -double __math_invalid(double); -#if LDBL_MANT_DIG != DBL_MANT_DIG -long double __math_invalidl(long double); -#endif - -#endif // RUX_CONFIG_FP_SIMD - -#endif // _LIBM_H diff --git a/ulib/ruxlibc/c/locale.c b/ulib/ruxlibc/c/locale.c deleted file mode 100644 index 5cbc619ea..000000000 --- a/ulib/ruxlibc/c/locale.c +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -// TODO -char *setlocale(int __category, const char *__locale) -{ - unimplemented(); - return NULL; -} - -static const struct lconv posix_lconv = { - .decimal_point = ".", - .thousands_sep = "", - .grouping = "", - .int_curr_symbol = "", - .currency_symbol = "", - .mon_decimal_point = "", - .mon_thousands_sep = "", - .mon_grouping = "", - .positive_sign = "", - .negative_sign = "", - .int_frac_digits = CHAR_MAX, - .frac_digits = CHAR_MAX, - .p_cs_precedes = CHAR_MAX, - .p_sep_by_space = CHAR_MAX, - .n_cs_precedes = CHAR_MAX, - .n_sep_by_space = CHAR_MAX, - .p_sign_posn = CHAR_MAX, - .n_sign_posn = CHAR_MAX, - .int_p_cs_precedes = CHAR_MAX, - .int_p_sep_by_space = CHAR_MAX, - .int_n_cs_precedes = CHAR_MAX, - .int_n_sep_by_space = CHAR_MAX, - .int_p_sign_posn = CHAR_MAX, - .int_n_sign_posn = CHAR_MAX, -}; - -struct lconv *localeconv(void) -{ - return (void *)&posix_lconv; -} diff --git a/ulib/ruxlibc/c/log.c b/ulib/ruxlibc/c/log.c deleted file mode 100644 index 2dc35dad5..000000000 --- a/ulib/ruxlibc/c/log.c +++ /dev/null @@ -1,404 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_FP_SIMD - -#include -#include -#include -#include - -#include "libm.h" - -struct log_data { - double ln2hi; - double ln2lo; - double poly[LOG_POLY_ORDER - 1]; - double poly1[LOG_POLY1_ORDER - 1]; - struct { - double invc, logc; - } tab[1 << LOG_TABLE_BITS]; -#if !__FP_FAST_FMA - struct { - double chi, clo; - } tab2[1 << LOG_TABLE_BITS]; -#endif -}; - -const struct log_data __log_data = { - .ln2hi = 0x1.62e42fefa3800p-1, - .ln2lo = 0x1.ef35793c76730p-45, - .poly1 = - { - -0x1p-1, - 0x1.5555555555577p-2, - -0x1.ffffffffffdcbp-3, - 0x1.999999995dd0cp-3, - -0x1.55555556745a7p-3, - 0x1.24924a344de3p-3, - -0x1.fffffa4423d65p-4, - 0x1.c7184282ad6cap-4, - -0x1.999eb43b068ffp-4, - 0x1.78182f7afd085p-4, - -0x1.5521375d145cdp-4, - }, - .poly = - { - -0x1.0000000000001p-1, - 0x1.555555551305bp-2, - -0x1.fffffffeb459p-3, - 0x1.999b324f10111p-3, - -0x1.55575e506c89fp-3, - }, - .tab = - { - {0x1.734f0c3e0de9fp+0, -0x1.7cc7f79e69000p-2}, - {0x1.713786a2ce91fp+0, -0x1.76feec20d0000p-2}, - {0x1.6f26008fab5a0p+0, -0x1.713e31351e000p-2}, - {0x1.6d1a61f138c7dp+0, -0x1.6b85b38287800p-2}, - {0x1.6b1490bc5b4d1p+0, -0x1.65d5590807800p-2}, - {0x1.69147332f0cbap+0, -0x1.602d076180000p-2}, - {0x1.6719f18224223p+0, -0x1.5a8ca86909000p-2}, - {0x1.6524f99a51ed9p+0, -0x1.54f4356035000p-2}, - {0x1.63356aa8f24c4p+0, -0x1.4f637c36b4000p-2}, - {0x1.614b36b9ddc14p+0, -0x1.49da7fda85000p-2}, - {0x1.5f66452c65c4cp+0, -0x1.445923989a800p-2}, - {0x1.5d867b5912c4fp+0, -0x1.3edf439b0b800p-2}, - {0x1.5babccb5b90dep+0, -0x1.396ce448f7000p-2}, - {0x1.59d61f2d91a78p+0, -0x1.3401e17bda000p-2}, - {0x1.5805612465687p+0, -0x1.2e9e2ef468000p-2}, - {0x1.56397cee76bd3p+0, -0x1.2941b3830e000p-2}, - {0x1.54725e2a77f93p+0, -0x1.23ec58cda8800p-2}, - {0x1.52aff42064583p+0, -0x1.1e9e129279000p-2}, - {0x1.50f22dbb2bddfp+0, -0x1.1956d2b48f800p-2}, - {0x1.4f38f4734ded7p+0, -0x1.141679ab9f800p-2}, - {0x1.4d843cfde2840p+0, -0x1.0edd094ef9800p-2}, - {0x1.4bd3ec078a3c8p+0, -0x1.09aa518db1000p-2}, - {0x1.4a27fc3e0258ap+0, -0x1.047e65263b800p-2}, - {0x1.4880524d48434p+0, -0x1.feb224586f000p-3}, - {0x1.46dce1b192d0bp+0, -0x1.f474a7517b000p-3}, - {0x1.453d9d3391854p+0, -0x1.ea4443d103000p-3}, - {0x1.43a2744b4845ap+0, -0x1.e020d44e9b000p-3}, - {0x1.420b54115f8fbp+0, -0x1.d60a22977f000p-3}, - {0x1.40782da3ef4b1p+0, -0x1.cc00104959000p-3}, - {0x1.3ee8f5d57fe8fp+0, -0x1.c202956891000p-3}, - {0x1.3d5d9a00b4ce9p+0, -0x1.b81178d811000p-3}, - {0x1.3bd60c010c12bp+0, -0x1.ae2c9ccd3d000p-3}, - {0x1.3a5242b75dab8p+0, -0x1.a45402e129000p-3}, - {0x1.38d22cd9fd002p+0, -0x1.9a877681df000p-3}, - {0x1.3755bc5847a1cp+0, -0x1.90c6d69483000p-3}, - {0x1.35dce49ad36e2p+0, -0x1.87120a645c000p-3}, - {0x1.34679984dd440p+0, -0x1.7d68fb4143000p-3}, - {0x1.32f5cceffcb24p+0, -0x1.73cb83c627000p-3}, - {0x1.3187775a10d49p+0, -0x1.6a39a9b376000p-3}, - {0x1.301c8373e3990p+0, -0x1.60b3154b7a000p-3}, - {0x1.2eb4ebb95f841p+0, -0x1.5737d76243000p-3}, - {0x1.2d50a0219a9d1p+0, -0x1.4dc7b8fc23000p-3}, - {0x1.2bef9a8b7fd2ap+0, -0x1.4462c51d20000p-3}, - {0x1.2a91c7a0c1babp+0, -0x1.3b08abc830000p-3}, - {0x1.293726014b530p+0, -0x1.31b996b490000p-3}, - {0x1.27dfa5757a1f5p+0, -0x1.2875490a44000p-3}, - {0x1.268b39b1d3bbfp+0, -0x1.1f3b9f879a000p-3}, - {0x1.2539d838ff5bdp+0, -0x1.160c8252ca000p-3}, - {0x1.23eb7aac9083bp+0, -0x1.0ce7f57f72000p-3}, - {0x1.22a012ba940b6p+0, -0x1.03cdc49fea000p-3}, - {0x1.2157996cc4132p+0, -0x1.f57bdbc4b8000p-4}, - {0x1.201201dd2fc9bp+0, -0x1.e370896404000p-4}, - {0x1.1ecf4494d480bp+0, -0x1.d17983ef94000p-4}, - {0x1.1d8f5528f6569p+0, -0x1.bf9674ed8a000p-4}, - {0x1.1c52311577e7cp+0, -0x1.adc79202f6000p-4}, - {0x1.1b17c74cb26e9p+0, -0x1.9c0c3e7288000p-4}, - {0x1.19e010c2c1ab6p+0, -0x1.8a646b372c000p-4}, - {0x1.18ab07bb670bdp+0, -0x1.78d01b3ac0000p-4}, - {0x1.1778a25efbcb6p+0, -0x1.674f145380000p-4}, - {0x1.1648d354c31dap+0, -0x1.55e0e6d878000p-4}, - {0x1.151b990275fddp+0, -0x1.4485cdea1e000p-4}, - {0x1.13f0ea432d24cp+0, -0x1.333d94d6aa000p-4}, - {0x1.12c8b7210f9dap+0, -0x1.22079f8c56000p-4}, - {0x1.11a3028ecb531p+0, -0x1.10e4698622000p-4}, - {0x1.107fbda8434afp+0, -0x1.ffa6c6ad20000p-5}, - {0x1.0f5ee0f4e6bb3p+0, -0x1.dda8d4a774000p-5}, - {0x1.0e4065d2a9fcep+0, -0x1.bbcece4850000p-5}, - {0x1.0d244632ca521p+0, -0x1.9a1894012c000p-5}, - {0x1.0c0a77ce2981ap+0, -0x1.788583302c000p-5}, - {0x1.0af2f83c636d1p+0, -0x1.5715e67d68000p-5}, - {0x1.09ddb98a01339p+0, -0x1.35c8a49658000p-5}, - {0x1.08cabaf52e7dfp+0, -0x1.149e364154000p-5}, - {0x1.07b9f2f4e28fbp+0, -0x1.e72c082eb8000p-6}, - {0x1.06ab58c358f19p+0, -0x1.a55f152528000p-6}, - {0x1.059eea5ecf92cp+0, -0x1.63d62cf818000p-6}, - {0x1.04949cdd12c90p+0, -0x1.228fb8caa0000p-6}, - {0x1.038c6c6f0ada9p+0, -0x1.c317b20f90000p-7}, - {0x1.02865137932a9p+0, -0x1.419355daa0000p-7}, - {0x1.0182427ea7348p+0, -0x1.81203c2ec0000p-8}, - {0x1.008040614b195p+0, -0x1.0040979240000p-9}, - {0x1.fe01ff726fa1ap-1, 0x1.feff384900000p-9}, - {0x1.fa11cc261ea74p-1, 0x1.7dc41353d0000p-7}, - {0x1.f6310b081992ep-1, 0x1.3cea3c4c28000p-6}, - {0x1.f25f63ceeadcdp-1, 0x1.b9fc114890000p-6}, - {0x1.ee9c8039113e7p-1, 0x1.1b0d8ce110000p-5}, - {0x1.eae8078cbb1abp-1, 0x1.58a5bd001c000p-5}, - {0x1.e741aa29d0c9bp-1, 0x1.95c8340d88000p-5}, - {0x1.e3a91830a99b5p-1, 0x1.d276aef578000p-5}, - {0x1.e01e009609a56p-1, 0x1.07598e598c000p-4}, - {0x1.dca01e577bb98p-1, 0x1.253f5e30d2000p-4}, - {0x1.d92f20b7c9103p-1, 0x1.42edd8b380000p-4}, - {0x1.d5cac66fb5ccep-1, 0x1.606598757c000p-4}, - {0x1.d272caa5ede9dp-1, 0x1.7da76356a0000p-4}, - {0x1.cf26e3e6b2ccdp-1, 0x1.9ab434e1c6000p-4}, - {0x1.cbe6da2a77902p-1, 0x1.b78c7bb0d6000p-4}, - {0x1.c8b266d37086dp-1, 0x1.d431332e72000p-4}, - {0x1.c5894bd5d5804p-1, 0x1.f0a3171de6000p-4}, - {0x1.c26b533bb9f8cp-1, 0x1.067152b914000p-3}, - {0x1.bf583eeece73fp-1, 0x1.147858292b000p-3}, - {0x1.bc4fd75db96c1p-1, 0x1.2266ecdca3000p-3}, - {0x1.b951e0c864a28p-1, 0x1.303d7a6c55000p-3}, - {0x1.b65e2c5ef3e2cp-1, 0x1.3dfc33c331000p-3}, - {0x1.b374867c9888bp-1, 0x1.4ba366b7a8000p-3}, - {0x1.b094b211d304ap-1, 0x1.5933928d1f000p-3}, - {0x1.adbe885f2ef7ep-1, 0x1.66acd2418f000p-3}, - {0x1.aaf1d31603da2p-1, 0x1.740f8ec669000p-3}, - {0x1.a82e63fd358a7p-1, 0x1.815c0f51af000p-3}, - {0x1.a5740ef09738bp-1, 0x1.8e92954f68000p-3}, - {0x1.a2c2a90ab4b27p-1, 0x1.9bb3602f84000p-3}, - {0x1.a01a01393f2d1p-1, 0x1.a8bed1c2c0000p-3}, - {0x1.9d79f24db3c1bp-1, 0x1.b5b515c01d000p-3}, - {0x1.9ae2505c7b190p-1, 0x1.c2967ccbcc000p-3}, - {0x1.9852ef297ce2fp-1, 0x1.cf635d5486000p-3}, - {0x1.95cbaeea44b75p-1, 0x1.dc1bd3446c000p-3}, - {0x1.934c69de74838p-1, 0x1.e8c01b8cfe000p-3}, - {0x1.90d4f2f6752e6p-1, 0x1.f5509c0179000p-3}, - {0x1.8e6528effd79dp-1, 0x1.00e6c121fb800p-2}, - {0x1.8bfce9fcc007cp-1, 0x1.071b80e93d000p-2}, - {0x1.899c0dabec30ep-1, 0x1.0d46b9e867000p-2}, - {0x1.87427aa2317fbp-1, 0x1.13687334bd000p-2}, - {0x1.84f00acb39a08p-1, 0x1.1980d67234800p-2}, - {0x1.82a49e8653e55p-1, 0x1.1f8ffe0cc8000p-2}, - {0x1.8060195f40260p-1, 0x1.2595fd7636800p-2}, - {0x1.7e22563e0a329p-1, 0x1.2b9300914a800p-2}, - {0x1.7beb377dcb5adp-1, 0x1.3187210436000p-2}, - {0x1.79baa679725c2p-1, 0x1.377266dec1800p-2}, - {0x1.77907f2170657p-1, 0x1.3d54ffbaf3000p-2}, - {0x1.756cadbd6130cp-1, 0x1.432eee32fe000p-2}, - }, -#if !__FP_FAST_FMA - .tab2 = - { - {0x1.61000014fb66bp-1, 0x1.e026c91425b3cp-56}, - {0x1.63000034db495p-1, 0x1.dbfea48005d41p-55}, - {0x1.650000d94d478p-1, 0x1.e7fa786d6a5b7p-55}, - {0x1.67000074e6fadp-1, 0x1.1fcea6b54254cp-57}, - {0x1.68ffffedf0faep-1, -0x1.c7e274c590efdp-56}, - {0x1.6b0000763c5bcp-1, -0x1.ac16848dcda01p-55}, - {0x1.6d0001e5cc1f6p-1, 0x1.33f1c9d499311p-55}, - {0x1.6efffeb05f63ep-1, -0x1.e80041ae22d53p-56}, - {0x1.710000e86978p-1, 0x1.bff6671097952p-56}, - {0x1.72ffffc67e912p-1, 0x1.c00e226bd8724p-55}, - {0x1.74fffdf81116ap-1, -0x1.e02916ef101d2p-57}, - {0x1.770000f679c9p-1, -0x1.7fc71cd549c74p-57}, - {0x1.78ffffa7ec835p-1, 0x1.1bec19ef50483p-55}, - {0x1.7affffe20c2e6p-1, -0x1.07e1729cc6465p-56}, - {0x1.7cfffed3fc9p-1, -0x1.08072087b8b1cp-55}, - {0x1.7efffe9261a76p-1, 0x1.dc0286d9df9aep-55}, - {0x1.81000049ca3e8p-1, 0x1.97fd251e54c33p-55}, - {0x1.8300017932c8fp-1, -0x1.afee9b630f381p-55}, - {0x1.850000633739cp-1, 0x1.9bfbf6b6535bcp-55}, - {0x1.87000204289c6p-1, -0x1.bbf65f3117b75p-55}, - {0x1.88fffebf57904p-1, -0x1.9006ea23dcb57p-55}, - {0x1.8b00022bc04dfp-1, -0x1.d00df38e04b0ap-56}, - {0x1.8cfffe50c1b8ap-1, -0x1.8007146ff9f05p-55}, - {0x1.8effffc918e43p-1, 0x1.3817bd07a7038p-55}, - {0x1.910001efa5fc7p-1, 0x1.93e9176dfb403p-55}, - {0x1.9300013467bb9p-1, 0x1.f804e4b980276p-56}, - {0x1.94fffe6ee076fp-1, -0x1.f7ef0d9ff622ep-55}, - {0x1.96fffde3c12d1p-1, -0x1.082aa962638bap-56}, - {0x1.98ffff4458a0dp-1, -0x1.7801b9164a8efp-55}, - {0x1.9afffdd982e3ep-1, -0x1.740e08a5a9337p-55}, - {0x1.9cfffed49fb66p-1, 0x1.fce08c19bep-60}, - {0x1.9f00020f19c51p-1, -0x1.a3faa27885b0ap-55}, - {0x1.a10001145b006p-1, 0x1.4ff489958da56p-56}, - {0x1.a300007bbf6fap-1, 0x1.cbeab8a2b6d18p-55}, - {0x1.a500010971d79p-1, 0x1.8fecadd78793p-55}, - {0x1.a70001df52e48p-1, -0x1.f41763dd8abdbp-55}, - {0x1.a90001c593352p-1, -0x1.ebf0284c27612p-55}, - {0x1.ab0002a4f3e4bp-1, -0x1.9fd043cff3f5fp-57}, - {0x1.acfffd7ae1ed1p-1, -0x1.23ee7129070b4p-55}, - {0x1.aefffee510478p-1, 0x1.a063ee00edea3p-57}, - {0x1.b0fffdb650d5bp-1, 0x1.a06c8381f0ab9p-58}, - {0x1.b2ffffeaaca57p-1, -0x1.9011e74233c1dp-56}, - {0x1.b4fffd995badcp-1, -0x1.9ff1068862a9fp-56}, - {0x1.b7000249e659cp-1, 0x1.aff45d0864f3ep-55}, - {0x1.b8ffff987164p-1, 0x1.cfe7796c2c3f9p-56}, - {0x1.bafffd204cb4fp-1, -0x1.3ff27eef22bc4p-57}, - {0x1.bcfffd2415c45p-1, -0x1.cffb7ee3bea21p-57}, - {0x1.beffff86309dfp-1, -0x1.14103972e0b5cp-55}, - {0x1.c0fffe1b57653p-1, 0x1.bc16494b76a19p-55}, - {0x1.c2ffff1fa57e3p-1, -0x1.4feef8d30c6edp-57}, - {0x1.c4fffdcbfe424p-1, -0x1.43f68bcec4775p-55}, - {0x1.c6fffed54b9f7p-1, 0x1.47ea3f053e0ecp-55}, - {0x1.c8fffeb998fd5p-1, 0x1.383068df992f1p-56}, - {0x1.cb0002125219ap-1, -0x1.8fd8e64180e04p-57}, - {0x1.ccfffdd94469cp-1, 0x1.e7ebe1cc7ea72p-55}, - {0x1.cefffeafdc476p-1, 0x1.ebe39ad9f88fep-55}, - {0x1.d1000169af82bp-1, 0x1.57d91a8b95a71p-56}, - {0x1.d30000d0ff71dp-1, 0x1.9c1906970c7dap-55}, - {0x1.d4fffea790fc4p-1, -0x1.80e37c558fe0cp-58}, - {0x1.d70002edc87e5p-1, -0x1.f80d64dc10f44p-56}, - {0x1.d900021dc82aap-1, -0x1.47c8f94fd5c5cp-56}, - {0x1.dafffd86b0283p-1, 0x1.c7f1dc521617ep-55}, - {0x1.dd000296c4739p-1, 0x1.8019eb2ffb153p-55}, - {0x1.defffe54490f5p-1, 0x1.e00d2c652cc89p-57}, - {0x1.e0fffcdabf694p-1, -0x1.f8340202d69d2p-56}, - {0x1.e2fffdb52c8ddp-1, 0x1.b00c1ca1b0864p-56}, - {0x1.e4ffff24216efp-1, 0x1.2ffa8b094ab51p-56}, - {0x1.e6fffe88a5e11p-1, -0x1.7f673b1efbe59p-58}, - {0x1.e9000119eff0dp-1, -0x1.4808d5e0bc801p-55}, - {0x1.eafffdfa51744p-1, 0x1.80006d54320b5p-56}, - {0x1.ed0001a127fa1p-1, -0x1.002f860565c92p-58}, - {0x1.ef00007babcc4p-1, -0x1.540445d35e611p-55}, - {0x1.f0ffff57a8d02p-1, -0x1.ffb3139ef9105p-59}, - {0x1.f30001ee58ac7p-1, 0x1.a81acf2731155p-55}, - {0x1.f4ffff5823494p-1, 0x1.a3f41d4d7c743p-55}, - {0x1.f6ffffca94c6bp-1, -0x1.202f41c987875p-57}, - {0x1.f8fffe1f9c441p-1, 0x1.77dd1f477e74bp-56}, - {0x1.fafffd2e0e37ep-1, -0x1.f01199a7ca331p-57}, - {0x1.fd0001c77e49ep-1, 0x1.181ee4bceacb1p-56}, - {0x1.feffff7e0c331p-1, -0x1.e05370170875ap-57}, - {0x1.00ffff465606ep+0, -0x1.a7ead491c0adap-55}, - {0x1.02ffff3867a58p+0, -0x1.77f69c3fcb2ep-54}, - {0x1.04ffffdfc0d17p+0, 0x1.7bffe34cb945bp-54}, - {0x1.0700003cd4d82p+0, 0x1.20083c0e456cbp-55}, - {0x1.08ffff9f2cbe8p+0, -0x1.dffdfbe37751ap-57}, - {0x1.0b000010cda65p+0, -0x1.13f7faee626ebp-54}, - {0x1.0d00001a4d338p+0, 0x1.07dfa79489ff7p-55}, - {0x1.0effffadafdfdp+0, -0x1.7040570d66bcp-56}, - {0x1.110000bbafd96p+0, 0x1.e80d4846d0b62p-55}, - {0x1.12ffffae5f45dp+0, 0x1.dbffa64fd36efp-54}, - {0x1.150000dd59ad9p+0, 0x1.a0077701250aep-54}, - {0x1.170000f21559ap+0, 0x1.dfdf9e2e3deeep-55}, - {0x1.18ffffc275426p+0, 0x1.10030dc3b7273p-54}, - {0x1.1b000123d3c59p+0, 0x1.97f7980030188p-54}, - {0x1.1cffff8299eb7p+0, -0x1.5f932ab9f8c67p-57}, - {0x1.1effff48ad4p+0, 0x1.37fbf9da75bebp-54}, - {0x1.210000c8b86a4p+0, 0x1.f806b91fd5b22p-54}, - {0x1.2300003854303p+0, 0x1.3ffc2eb9fbf33p-54}, - {0x1.24fffffbcf684p+0, 0x1.601e77e2e2e72p-56}, - {0x1.26ffff52921d9p+0, 0x1.ffcbb767f0c61p-56}, - {0x1.2900014933a3cp+0, -0x1.202ca3c02412bp-56}, - {0x1.2b00014556313p+0, -0x1.2808233f21f02p-54}, - {0x1.2cfffebfe523bp+0, -0x1.8ff7e384fdcf2p-55}, - {0x1.2f0000bb8ad96p+0, -0x1.5ff51503041c5p-55}, - {0x1.30ffffb7ae2afp+0, -0x1.10071885e289dp-55}, - {0x1.32ffffeac5f7fp+0, -0x1.1ff5d3fb7b715p-54}, - {0x1.350000ca66756p+0, 0x1.57f82228b82bdp-54}, - {0x1.3700011fbf721p+0, 0x1.000bac40dd5ccp-55}, - {0x1.38ffff9592fb9p+0, -0x1.43f9d2db2a751p-54}, - {0x1.3b00004ddd242p+0, 0x1.57f6b707638e1p-55}, - {0x1.3cffff5b2c957p+0, 0x1.a023a10bf1231p-56}, - {0x1.3efffeab0b418p+0, 0x1.87f6d66b152bp-54}, - {0x1.410001532aff4p+0, 0x1.7f8375f198524p-57}, - {0x1.4300017478b29p+0, 0x1.301e672dc5143p-55}, - {0x1.44fffe795b463p+0, 0x1.9ff69b8b2895ap-55}, - {0x1.46fffe80475ep+0, -0x1.5c0b19bc2f254p-54}, - {0x1.48fffef6fc1e7p+0, 0x1.b4009f23a2a72p-54}, - {0x1.4afffe5bea704p+0, -0x1.4ffb7bf0d7d45p-54}, - {0x1.4d000171027dep+0, -0x1.9c06471dc6a3dp-54}, - {0x1.4f0000ff03ee2p+0, 0x1.77f890b85531cp-54}, - {0x1.5100012dc4bd1p+0, 0x1.004657166a436p-57}, - {0x1.530001605277ap+0, -0x1.6bfcece233209p-54}, - {0x1.54fffecdb704cp+0, -0x1.902720505a1d7p-55}, - {0x1.56fffef5f54a9p+0, 0x1.bbfe60ec96412p-54}, - {0x1.5900017e61012p+0, 0x1.87ec581afef9p-55}, - {0x1.5b00003c93e92p+0, -0x1.f41080abf0ccp-54}, - {0x1.5d0001d4919bcp+0, -0x1.8812afb254729p-54}, - {0x1.5efffe7b87a89p+0, -0x1.47eb780ed6904p-54}, - }, -#endif -}; - -#define T __log_data.tab -#define T2 __log_data.tab2 -#define B __log_data.poly1 -#define A __log_data.poly -#define Ln2hi __log_data.ln2hi -#define Ln2lo __log_data.ln2lo -#define N (1 << LOG_TABLE_BITS) -#define OFF 0x3fe6000000000000 - -static inline uint32_t top16(double x) -{ - return asuint64(x) >> 48; -} - -double log(double x) -{ - double_t w, z, r, r2, r3, y, invc, logc, kd, hi, lo; - uint64_t ix, iz, tmp; - uint32_t top; - int k, i; - - ix = asuint64(x); - top = top16(x); -#define LO asuint64(1.0 - 0x1p-4) -#define HI asuint64(1.0 + 0x1.09p-4) - if (predict_false(ix - LO < HI - LO)) { - if (WANT_ROUNDING && predict_false(ix == asuint64(1.0))) - return 0; - r = x - 1.0; - r2 = r * r; - r3 = r * r2; - y = r3 * - (B[1] + r * B[2] + r2 * B[3] + - r3 * (B[4] + r * B[5] + r2 * B[6] + r3 * (B[7] + r * B[8] + r2 * B[9] + r3 * B[10]))); - w = r * 0x1p27; - double_t rhi = r + w - w; - double_t rlo = r - rhi; - w = rhi * rhi * B[0]; - hi = r + w; - lo = r - hi + w; - lo += B[0] * rlo * (rhi + r); - y += lo; - y += hi; - return eval_as_double(y); - } - if (predict_false(top - 0x0010 >= 0x7ff0 - 0x0010)) { - if (ix * 2 == 0) - return __math_divzero(1); - if (ix == asuint64(INFINITY)) - return x; - if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0) - return __math_invalid(x); - ix = asuint64(x * 0x1p52); - ix -= 52ULL << 52; - } - - tmp = ix - OFF; - i = (tmp >> (52 - LOG_TABLE_BITS)) % N; - k = (int64_t)tmp >> 52; - iz = ix - (tmp & 0xfffULL << 52); - invc = T[i].invc; - logc = T[i].logc; - z = asdouble(iz); - -#if __FP_FAST_FMA - r = __builtin_fma(z, invc, -1.0); -#else - r = (z - T2[i].chi - T2[i].clo) * invc; -#endif - - kd = (double_t)k; - w = kd * Ln2hi + logc; - hi = w + r; - lo = w - hi + r + kd * Ln2lo; - r2 = r * r; - y = lo + r2 * A[0] + r * r2 * (A[1] + r * A[2] + r2 * (A[3] + r * A[4])) + hi; - return eval_as_double(y); -} - -#endif // RUX_CONFIG_FP_SIMD diff --git a/ulib/ruxlibc/c/math.c b/ulib/ruxlibc/c/math.c deleted file mode 100644 index 1f5266dfd..000000000 --- a/ulib/ruxlibc/c/math.c +++ /dev/null @@ -1,580 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_FP_SIMD - -#include -#include -#include -#include - -#include "libm.h" - -int __fpclassify(double x) -{ - union { - double f; - uint64_t i; - } u = {x}; - int e = u.i >> 52 & 0x7ff; - if (!e) - return u.i << 1 ? FP_SUBNORMAL : FP_ZERO; - if (e == 0x7ff) - return u.i << 12 ? FP_NAN : FP_INFINITE; - return FP_NORMAL; -} - -int __fpclassifyf(float x) -{ - union { - float f; - uint32_t i; - } u = {x}; - int e = u.i >> 23 & 0xff; - if (!e) - return u.i << 1 ? FP_SUBNORMAL : FP_ZERO; - if (e == 0xff) - return u.i << 9 ? FP_NAN : FP_INFINITE; - return FP_NORMAL; -} - -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -int __fpclassifyl(long double x) -{ - return __fpclassify(x); -} -#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 -int __fpclassifyl(long double x) -{ - union ldshape u = {x}; - int e = u.i.se & 0x7fff; - int msb = u.i.m >> 63; - if (!e && !msb) - return u.i.m ? FP_SUBNORMAL : FP_ZERO; - if (e == 0x7fff) { - /* The x86 variant of 80-bit extended precision only admits - * one representation of each infinity, with the mantissa msb - * necessarily set. The version with it clear is invalid/nan. - * The m68k variant, however, allows either, and tooling uses - * the version with it clear. */ - if (__BYTE_ORDER == __LITTLE_ENDIAN && !msb) - return FP_NAN; - return u.i.m << 1 ? FP_NAN : FP_INFINITE; - } - if (!msb) - return FP_NAN; - return FP_NORMAL; -} -#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 -int __fpclassifyl(long double x) -{ - union ldshape u = {x}; - int e = u.i.se & 0x7fff; - u.i.se = 0; - if (!e) - return u.i2.lo | u.i2.hi ? FP_SUBNORMAL : FP_ZERO; - if (e == 0x7fff) - return u.i2.lo | u.i2.hi ? FP_NAN : FP_INFINITE; - return FP_NORMAL; -} -#endif - -double fabs(double x) -{ - union { - double f; - uint64_t i; - } u = {x}; - u.i &= -1ULL / 2; - return u.f; -} - -static const double toint = 1 / DBL_EPSILON; - -double floor(double x) -{ - union { - double f; - uint64_t i; - } u = {x}; - int e = u.i >> 52 & 0x7ff; - double y; - - if (e >= 0x3ff + 52 || x == 0) - return x; - /* y = int(x) - x, where int(x) is an integer neighbor of x */ - if (u.i >> 63) - y = x - toint + toint - x; - else - y = x + toint - toint - x; - /* special case because of non-nearest rounding modes */ - if (e <= 0x3ff - 1) { - FORCE_EVAL(y); - return u.i >> 63 ? -1 : 0; - } - if (y > 0) - return x + y - 1; - return x + y; -} - -double rint(double x) -{ - unimplemented(); - return 0; -} - -long long llrint(double x) -{ - return rint(x); -} - -double sqrt(double x) -{ - unimplemented(); - return 0; -} - -double round(double x) -{ - unimplemented(); - return x; -} - -long double roundl(long double x) -{ - unimplemented(); - return x; -} - -long long llroundl(long double x) -{ - unimplemented(); - return x; -} - -double cos(double __x) -{ - unimplemented(); - return 0; -} - -double ceil(double x) -{ - union { - double f; - uint64_t i; - } u = {x}; - int e = u.i >> 52 & 0x7ff; - double_t y; - - if (e >= 0x3ff + 52 || x == 0) - return x; - if (u.i >> 63) - y = x - toint + toint - x; - else - y = x + toint - toint - x; - if (e <= 0x3ff - 1) { - FORCE_EVAL(y); - return u.i >> 63 ? -0.0 : 1; - } - if (y < 0) - return x + y + 1; - return x + y; -} - -// TODO -double sin(double __x) -{ - unimplemented(); - return 0; -} - -// TODO -double asin(double __x) -{ - unimplemented(); - return 0; -} - -long double ceill(long double x) -{ - unimplemented(); - return x; -} - -double acos(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double atan(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double atan2(double y, double x) -{ - unimplemented(); - return 0; -} - -double cosh(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double exp(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double frexp(double x, int *e) -{ - unimplemented(); - return 0; -} - -double ldexp(double x, int n) -{ - unimplemented(); - return 0; -} - -// TODO -double log10(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double modf(double x, double *iptr) -{ - unimplemented(); - return 0; -} - -double sinh(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double tan(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double tanh(double x) -{ - unimplemented(); - return 0; -} - -double copysign(double x, double y) -{ - union { - double f; - uint64_t i; - } ux = {x}, uy = {y}; - ux.i &= -1ULL / 2; - ux.i |= uy.i & 1ULL << 63; - return ux.f; -} - -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double copysignl(long double x, long double y) -{ - return copysign(x, y); -} -#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 -long double copysignl(long double x, long double y) -{ - union ldshape ux = {x}, uy = {y}; - ux.i.se &= 0x7fff; - ux.i.se |= uy.i.se & 0x8000; - return ux.f; -} -#endif - -double scalbn(double x, int n) -{ - union { - double f; - uint64_t i; - } u; - double_t y = x; - - if (n > 1023) { - y *= 0x1p1023; - n -= 1023; - if (n > 1023) { - y *= 0x1p1023; - n -= 1023; - if (n > 1023) - n = 1023; - } - } else if (n < -1022) { - /* make sure final n < -53 to avoid double - rounding in the subnormal range */ - y *= 0x1p-1022 * 0x1p53; - n += 1022 - 53; - if (n < -1022) { - y *= 0x1p-1022 * 0x1p53; - n += 1022 - 53; - if (n < -1022) - n = -1022; - } - } - u.i = (uint64_t)(0x3ff + n) << 52; - x = y * u.f; - return x; -} - -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double scalbnl(long double x, int n) -{ - return scalbn(x, n); -} -#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 -long double scalbnl(long double x, int n) -{ - union ldshape u; - - if (n > 16383) { - x *= 0x1p16383L; - n -= 16383; - if (n > 16383) { - x *= 0x1p16383L; - n -= 16383; - if (n > 16383) - n = 16383; - } - } else if (n < -16382) { - x *= 0x1p-16382L * 0x1p113L; - n += 16382 - 113; - if (n < -16382) { - x *= 0x1p-16382L * 0x1p113L; - n += 16382 - 113; - if (n < -16382) - n = -16382; - } - } - u.f = 1.0; - u.i.se = 0x3fff + n; - return x * u.f; -} -#endif - -double fmod(double x, double y) -{ - union { - double f; - uint64_t i; - } ux = {x}, uy = {y}; - int ex = ux.i >> 52 & 0x7ff; - int ey = uy.i >> 52 & 0x7ff; - int sx = ux.i >> 63; - uint64_t i; - - /* in the followings uxi should be ux.i, but then gcc wrongly adds */ - /* float load/store to inner loops ruining performance and code size */ - uint64_t uxi = ux.i; - - if (uy.i << 1 == 0 || isnan(y) || ex == 0x7ff) - return (x * y) / (x * y); - if (uxi << 1 <= uy.i << 1) { - if (uxi << 1 == uy.i << 1) - return 0 * x; - return x; - } - - /* normalize x and y */ - if (!ex) { - for (i = uxi << 12; i >> 63 == 0; ex--, i <<= 1) - ; - uxi <<= -ex + 1; - } else { - uxi &= -1ULL >> 12; - uxi |= 1ULL << 52; - } - if (!ey) { - for (i = uy.i << 12; i >> 63 == 0; ey--, i <<= 1) - ; - uy.i <<= -ey + 1; - } else { - uy.i &= -1ULL >> 12; - uy.i |= 1ULL << 52; - } - - /* x mod y */ - for (; ex > ey; ex--) { - i = uxi - uy.i; - if (i >> 63 == 0) { - if (i == 0) - return 0 * x; - uxi = i; - } - uxi <<= 1; - } - i = uxi - uy.i; - if (i >> 63 == 0) { - if (i == 0) - return 0 * x; - uxi = i; - } - for (; uxi >> 52 == 0; uxi <<= 1, ex--) - ; - - /* scale result */ - if (ex > 0) { - uxi -= 1ULL << 52; - uxi |= (uint64_t)ex << 52; - } else { - uxi >>= -ex + 1; - } - uxi |= (uint64_t)sx << 63; - ux.i = uxi; - return ux.f; -} - -// x86_64 has specific implementation -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double fmodl(long double x, long double y) -{ - return fmod(x, y); -} -#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 -long double fmodl(long double x, long double y) -{ - union ldshape ux = {x}, uy = {y}; - int ex = ux.i.se & 0x7fff; - int ey = uy.i.se & 0x7fff; - int sx = ux.i.se & 0x8000; - - if (y == 0 || isnan(y) || ex == 0x7fff) - return (x * y) / (x * y); - ux.i.se = ex; - uy.i.se = ey; - if (ux.f <= uy.f) { - if (ux.f == uy.f) - return 0 * x; - return x; - } - - /* normalize x and y */ - if (!ex) { - ux.f *= 0x1p120f; - ex = ux.i.se - 120; - } - if (!ey) { - uy.f *= 0x1p120f; - ey = uy.i.se - 120; - } - - /* x mod y */ -#if LDBL_MANT_DIG == 64 - uint64_t i, mx, my; - mx = ux.i.m; - my = uy.i.m; - for (; ex > ey; ex--) { - i = mx - my; - if (mx >= my) { - if (i == 0) - return 0 * x; - mx = 2 * i; - } else if (2 * mx < mx) { - mx = 2 * mx - my; - } else { - mx = 2 * mx; - } - } - i = mx - my; - if (mx >= my) { - if (i == 0) - return 0 * x; - mx = i; - } - for (; mx >> 63 == 0; mx *= 2, ex--) - ; - ux.i.m = mx; -#elif LDBL_MANT_DIG == 113 - uint64_t hi, lo, xhi, xlo, yhi, ylo; - xhi = (ux.i2.hi & -1ULL >> 16) | 1ULL << 48; - yhi = (uy.i2.hi & -1ULL >> 16) | 1ULL << 48; - xlo = ux.i2.lo; - ylo = uy.i2.lo; - for (; ex > ey; ex--) { - hi = xhi - yhi; - lo = xlo - ylo; - if (xlo < ylo) - hi -= 1; - if (hi >> 63 == 0) { - if ((hi | lo) == 0) - return 0 * x; - xhi = 2 * hi + (lo >> 63); - xlo = 2 * lo; - } else { - xhi = 2 * xhi + (xlo >> 63); - xlo = 2 * xlo; - } - } - hi = xhi - yhi; - lo = xlo - ylo; - if (xlo < ylo) - hi -= 1; - if (hi >> 63 == 0) { - if ((hi | lo) == 0) - return 0 * x; - xhi = hi; - xlo = lo; - } - for (; xhi >> 48 == 0; xhi = 2 * xhi + (xlo >> 63), xlo = 2 * xlo, ex--) - ; - ux.i2.hi = xhi; - ux.i2.lo = xlo; -#endif - - /* scale result */ - if (ex <= 0) { - ux.i.se = (ex + 120) | sx; - ux.f *= 0x1p-120f; - } else - ux.i.se = ex | sx; - return ux.f; -} -#endif - -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double fabsl(long double x) -{ - return fabs(x); -} -#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 -long double fabsl(long double x) -{ - union ldshape u = {x}; - - u.i.se &= 0x7fff; - return u.f; -} -#endif - -#endif // RUX_CONFIG_FP_SIMD diff --git a/ulib/ruxlibc/c/network.c b/ulib/ruxlibc/c/network.c deleted file mode 100644 index 0cd8c1bd2..000000000 --- a/ulib/ruxlibc/c/network.c +++ /dev/null @@ -1,243 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_NET - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -int h_errno; - -static const char gai_msgs[] = "Invalid flags\0" - "Name does not resolve\0" - "Try again\0" - "Non-recoverable error\0" - "Unknown error\0" - "Unrecognized address family or invalid length\0" - "Unrecognized socket type\0" - "Unrecognized service\0" - "Unknown error\0" - "Out of memory\0" - "System error\0" - "Overflow\0" - "\0Unknown error"; - -const char *gai_strerror(int ecode) -{ - const char *s; - for (s = gai_msgs, ecode++; ecode && *s; ecode++, s++) - for (; *s; s++) - ; - if (!*s) - s++; - return s; -} - -static const char msgs[] = "Host not found\0" - "Try again\0" - "Non-recoverable error\0" - "Address not available\0" - "\0Unknown error"; - -const char *hstrerror(int ecode) -{ - const char *s; - for (s = msgs, ecode--; ecode && *s; ecode--, s++) - for (; *s; s++) - ; - if (!*s) - s++; - return s; -} - -static __inline uint16_t __bswap_16(uint16_t __x) -{ - return __x << 8 | __x >> 8; -} - -static __inline uint32_t __bswap_32(uint32_t __x) -{ - return __x >> 24 | (__x >> 8 & 0xff00) | (__x << 8 & 0xff0000) | __x << 24; -} - -uint32_t htonl(uint32_t n) -{ - union { - int i; - char c; - } u = {1}; - return u.c ? __bswap_32(n) : n; -} - -uint16_t htons(uint16_t n) -{ - union { - int i; - char c; - } u = {1}; - return u.c ? __bswap_16(n) : n; -} - -uint32_t ntohl(uint32_t n) -{ - union { - int i; - char c; - } u = {1}; - return u.c ? __bswap_32(n) : n; -} - -uint16_t ntohs(uint16_t n) -{ - union { - int i; - char c; - } u = {1}; - return u.c ? __bswap_16(n) : n; -} - -static int hexval(unsigned c) -{ - if (c - '0' < 10) - return c - '0'; - c |= 32; - if (c - 'a' < 6) - return c - 'a' + 10; - return -1; -} - -int inet_pton(int af, const char *__restrict s, void *__restrict a0) -{ - uint16_t ip[8]; - unsigned char *a = a0; - int i, j, v, d, brk = -1, need_v4 = 0; - - if (af == AF_INET) { - for (i = 0; i < 4; i++) { - for (v = j = 0; j < 3 && isdigit(s[j]); j++) v = 10 * v + s[j] - '0'; - if (j == 0 || (j > 1 && s[0] == '0') || v > 255) - return 0; - a[i] = v; - if (s[j] == 0 && i == 3) - return 1; - if (s[j] != '.') - return 0; - s += j + 1; - } - return 0; - } else if (af != AF_INET6) { - errno = EAFNOSUPPORT; - return -1; - } - - if (*s == ':' && *++s != ':') - return 0; - - for (i = 0;; i++) { - if (s[0] == ':' && brk < 0) { - brk = i; - ip[i & 7] = 0; - if (!*++s) - break; - if (i == 7) - return 0; - continue; - } - for (v = j = 0; j < 4 && (d = hexval(s[j])) >= 0; j++) v = 16 * v + d; - if (j == 0) - return 0; - ip[i & 7] = v; - if (!s[j] && (brk >= 0 || i == 7)) - break; - if (i == 7) - return 0; - if (s[j] != ':') { - if (s[j] != '.' || (i < 6 && brk < 0)) - return 0; - need_v4 = 1; - i++; - break; - } - s += j + 1; - } - if (brk >= 0) { - for (j = 0; j < 7 - i; j++) ip[brk + j] = 0; - memmove(ip + brk + 7 - i, ip + brk, 2 * (i + 1 - brk)); - } - for (j = 0; j < 8; j++) { - *a++ = ip[j] >> 8; - *a++ = ip[j]; - } - if (need_v4 && inet_pton(AF_INET, (void *)s, a - 4) <= 0) - return 0; - return 1; -} - -const char *inet_ntop(int af, const void *__restrict a0, char *__restrict s, socklen_t l) -{ - const unsigned char *a = a0; - int i, j, max, best; - char buf[100]; - - switch (af) { - case AF_INET: - if (snprintf(s, l, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]) < l) - return s; - break; - case AF_INET6: - if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\377\377", 12)) - snprintf(buf, sizeof buf, "%x:%x:%x:%x:%x:%x:%x:%x", 256 * a[0] + a[1], - 256 * a[2] + a[3], 256 * a[4] + a[5], 256 * a[6] + a[7], 256 * a[8] + a[9], - 256 * a[10] + a[11], 256 * a[12] + a[13], 256 * a[14] + a[15]); - else - snprintf(buf, sizeof buf, "%x:%x:%x:%x:%x:%x:%d.%d.%d.%d", 256 * a[0] + a[1], - 256 * a[2] + a[3], 256 * a[4] + a[5], 256 * a[6] + a[7], 256 * a[8] + a[9], - 256 * a[10] + a[11], a[12], a[13], a[14], a[15]); - /* Replace longest /(^0|:)[:0]{2,}/ with "::" */ - for (i = best = 0, max = 2; buf[i]; i++) { - if (i && buf[i] != ':') - continue; - j = strspn(buf + i, ":0"); - if (j > max) - best = i, max = j; - } - if (max > 3) { - buf[best] = buf[best + 1] = ':'; - memmove(buf + best + 2, buf + best + max, i - best - max + 1); - } - if (strlen(buf) < l) { - strcpy(s, buf); - return s; - } - break; - default: - errno = EAFNOSUPPORT; - return 0; - } - errno = ENOSPC; - return 0; -} - -// TODO -struct hostent *gethostbyname(const char *name) -{ - unimplemented(); - return 0; -} - -#endif // RUX_CONFIG_NET diff --git a/ulib/ruxlibc/c/nscd_query.c b/ulib/ruxlibc/c/nscd_query.c deleted file mode 100644 index 8e7f85aaf..000000000 --- a/ulib/ruxlibc/c/nscd_query.c +++ /dev/null @@ -1,103 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include "nscd.h" -#include -#include -#include -#include -#include -#include -#include -#include - -static const struct { - short sun_family; - char sun_path[21]; -} addr = {AF_UNIX, "/var/run/nscd/socket"}; - -FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *swap) -{ - size_t i; - int fd; - FILE *f = 0; - int32_t req_buf[REQ_LEN] = {NSCDVERSION, req, strnlen(key, LOGIN_NAME_MAX) + 1}; - struct msghdr msg = { - .msg_iov = (struct iovec[]){{&req_buf, sizeof(req_buf)}, {(char *)key, strlen(key) + 1}}, - .msg_iovlen = 2}; - int errno_save = errno; - - *swap = 0; -retry: - memset(buf, 0, len); - buf[0] = NSCDVERSION; - - fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); - if (fd < 0) { - if (errno == EAFNOSUPPORT) { - f = fopen("/dev/null", "re"); - if (f) - errno = errno_save; - return f; - } - return 0; - } - - if (!(f = fdopen(fd, "r"))) { - close(fd); - return 0; - } - - if (req_buf[2] > LOGIN_NAME_MAX) - return f; - - if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) { - errno = errno_save; - return f; - } - goto error; - } - - if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0) - goto error; - - if (!fread(buf, len, 1, f)) { - if (ferror(f)) - goto error; - if (!*swap) { - fclose(f); - for (i = 0; i < sizeof(req_buf) / sizeof(req_buf[0]); i++) { - req_buf[i] = bswap_32(req_buf[i]); - } - *swap = 1; - goto retry; - } else { - errno = EIO; - goto error; - } - } - - if (*swap) { - for (i = 0; i < len / sizeof(buf[0]); i++) { - buf[i] = bswap_32(buf[i]); - } - } - - if (buf[0] != NSCDVERSION) { - errno = EIO; - goto error; - } - - return f; -error: - fclose(f); - return 0; -} diff --git a/ulib/ruxlibc/c/pow.c b/ulib/ruxlibc/c/pow.c deleted file mode 100644 index 7e5da7055..000000000 --- a/ulib/ruxlibc/c/pow.c +++ /dev/null @@ -1,824 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#if defined(RUX_CONFIG_FP_SIMD) - -#include -#include -#include -#include -#include - -#include "libm.h" - -#define OFF 0x3fe6955500000000 - -#define POW_LOG_TABLE_BITS 7 -#define POW_LOG_POLY_ORDER 8 -#define N (1 << POW_LOG_TABLE_BITS) -struct pow_log_data { - double ln2hi; - double ln2lo; - double poly[POW_LOG_POLY_ORDER - 1]; /* First coefficient is 1. */ - /* Note: the pad field is unused, but allows slightly faster indexing. */ - struct { - double invc, pad, logc, logctail; - } tab[1 << POW_LOG_TABLE_BITS]; -}; - -const struct - pow_log_data - __pow_log_data = - { - .ln2hi = 0x1.62e42fefa3800p-1, - .ln2lo = 0x1.ef35793c76730p-45, - .poly = - { - -0x1p-1, - 0x1.555555555556p-2 * -2, - -0x1.0000000000006p-2 * -2, - 0x1.999999959554ep-3 * 4, - -0x1.555555529a47ap-3 * 4, - 0x1.2495b9b4845e9p-3 * -8, - -0x1.0002b8b263fc3p-3 * -8, - }, - .tab = - { -#define A(a, b, c) {a, 0, b, c}, - A(0x1.6a00000000000p+0, -0x1.62c82f2b9c800p-2, 0x1.ab42428375680p-48) - A(0x1.6800000000000p+0, -0x1.5d1bdbf580800p-2, -0x1.ca508d8e0f720p-46) - A(0x1.6600000000000p+0, -0x1.5767717455800p-2, - -0x1.362a4d5b6506dp-45) - A(0x1.6400000000000p+0, -0x1.51aad872df800p-2, - -0x1.684e49eb067d5p-49) A(0x1.6200000000000p+0, - -0x1.4be5f95777800p-2, - -0x1.41b6993293ee0p-47) A(0x1.6000000000000p+0, -0x1.4618bc21c6000p-2, 0x1.3d82f484c84ccp-46) A(0x1.5e00000000000p+0, -0x1.404308686a800p-2, 0x1.c42f3ed820b3ap-50) A(0x1.5c00000000000p+0, -0x1.3a64c55694800p-2, 0x1.0b1c686519460p-45) A(0x1.5a00000000000p+0, -0x1.347dd9a988000p-2, 0x1.5594dd4c58092p-45) A(0x1.5800000000000p+0, -0x1.2e8e2bae12000p-2, 0x1.67b1e99b72bd8p-45) A(0x1.5600000000000p+0, - -0x1.2895a13de8800p-2, 0x1.5ca14b6cfb03fp-46) A(0x1.5600000000000p+0, - -0x1.2895a13de8800p-2, 0x1.5ca14b6cfb03fp-46) A(0x1.5400000000000p+0, - -0x1.22941fbcf7800p-2, - -0x1.65a242853da76p-46) A(0x1.5200000000000p+0, - -0x1.1c898c1699800p-2, - -0x1.fafbc68e75404p-46) A(0x1.5000000000000p+0, - -0x1.1675cababa800p-2, 0x1.f1fc63382a8f0p-46) A(0x1.4e00000000000p+0, - -0x1.1058bf9ae4800p-2, - -0x1.6a8c4fd055a66p-45) A(0x1.4c00000000000p+0, - -0x1.0a324e2739000p-2, -0x1.c6bee7ef4030ep-47) A(0x1.4a00000000000p+0, - -0x1.0402594b4d000p-2, -0x1.036b89ef42d7fp-48) A(0x1.4a00000000000p+0, - -0x1.0402594b4d000p-2, -0x1.036b89ef42d7fp-48) A(0x1.4800000000000p+0, - -0x1.fb9186d5e4000p-3, - 0x1.d572aab993c87p-47) A(0x1.4600000000000p+0, - -0x1.ef0adcbdc6000p-3, - 0x1.b26b79c86af24p-45) A(0x1.4400000000000p+0, - -0x1.e27076e2af000p-3, -0x1.72f4f543fff10p-46) A(0x1.4200000000000p+0, - -0x1.d5c216b4fc000p-3, 0x1.1ba91bbca681bp-45) A(0x1.4000000000000p+0, -0x1.c8ff7c79aa000p-3, 0x1.7794f689f8434p-45) A(0x1.4000000000000p+0, -0x1.c8ff7c79aa000p-3, 0x1.7794f689f8434p-45) A(0x1.3e00000000000p+0, - -0x1.bc286742d9000p-3, 0x1.94eb0318bb78fp-46) A(0x1.3c00000000000p+0, - -0x1.af3c94e80c000p-3, 0x1.a4e633fcd9066p-52) A(0x1.3a00000000000p+0, - -0x1.a23bc1fe2b000p-3, - -0x1.58c64dc46c1eap-45) A(0x1.3a00000000000p+0, - -0x1.a23bc1fe2b000p-3, -0x1.58c64dc46c1eap-45) A(0x1.3800000000000p+0, - -0x1.9525a9cf45000p-3, -0x1.ad1d904c1d4e3p-45) A(0x1.3600000000000p+0, - -0x1.87fa06520d000p-3, 0x1.bbdbf7fdbfa09p-45) A(0x1.3400000000000p+0, - -0x1.7ab890210e000p-3, 0x1.bdb9072534a58p-45) A(0x1.3400000000000p+0, - -0x1.7ab890210e000p-3, 0x1.bdb9072534a58p-45) A(0x1.3200000000000p+0, -0x1.6d60fe719d000p-3, -0x1.0e46aa3b2e266p-46) A(0x1.3000000000000p+0, - -0x1.5ff3070a79000p-3, -0x1.e9e439f105039p-46) A(0x1.3000000000000p+0, - -0x1.5ff3070a79000p-3, -0x1.e9e439f105039p-46) A(0x1.2e00000000000p+0, - -0x1.526e5e3a1b000p-3, -0x1.0de8b90075b8fp-45) A(0x1.2c00000000000p+0, - -0x1.44d2b6ccb8000p-3, 0x1.70cc16135783cp-46) A(0x1.2c00000000000p+0, -0x1.44d2b6ccb8000p-3, 0x1.70cc16135783cp-46) A(0x1.2a00000000000p+0, - -0x1.371fc201e9000p-3, 0x1.178864d27543ap-48) A(0x1.2800000000000p+0, - -0x1.29552f81ff000p-3, -0x1.48d301771c408p-45) A(0x1.2600000000000p+0, -0x1.1b72ad52f6000p-3, -0x1.e80a41811a396p-45) A(0x1.2600000000000p+0, -0x1.1b72ad52f6000p-3, -0x1.e80a41811a396p-45) A(0x1.2400000000000p+0, - -0x1.0d77e7cd09000p-3, - 0x1.a699688e85bf4p-47) A(0x1.2400000000000p+0, -0x1.0d77e7cd09000p-3, 0x1.a699688e85bf4p-47) A(0x1.2200000000000p+0, -0x1.fec9131dbe000p-4, -0x1.575545ca333f2p-45) A(0x1.2000000000000p+0, - -0x1.e27076e2b0000p-4, 0x1.a342c2af0003cp-45) A(0x1.2000000000000p+0, -0x1.e27076e2b0000p-4, 0x1.a342c2af0003cp-45) A(0x1.1e00000000000p+0, -0x1.c5e548f5bc000p-4, -0x1.d0c57585fbe06p-46) A(0x1.1c00000000000p+0, - -0x1.a926d3a4ae000p-4, 0x1.53935e85baac8p-45) A(0x1.1c00000000000p+0, - -0x1.a926d3a4ae000p-4, 0x1.53935e85baac8p-45) A(0x1.1a00000000000p+0, -0x1.8c345d631a000p-4, 0x1.37c294d2f5668p-46) A(0x1.1a00000000000p+0, - -0x1.8c345d631a000p-4, - 0x1.37c294d2f5668p-46) A(0x1.1800000000000p+0, -0x1.6f0d28ae56000p-4, - -0x1.69737c93373dap-45) A(0x1.1600000000000p+0, - -0x1.51b073f062000p-4, 0x1.f025b61c65e57p-46) A(0x1.1600000000000p+0, - -0x1.51b073f062000p-4, 0x1.f025b61c65e57p-46) A(0x1.1400000000000p+0, -0x1.341d7961be000p-4, 0x1.c5edaccf913dfp-45) A(0x1.1400000000000p+0, - -0x1.341d7961be000p-4, 0x1.c5edaccf913dfp-45) A(0x1.1200000000000p+0, -0x1.16536eea38000p-4, 0x1.47c5e768fa309p-46) A(0x1.1000000000000p+0, - -0x1.f0a30c0118000p-5, 0x1.d599e83368e91p-45) A(0x1.1000000000000p+0, -0x1.f0a30c0118000p-5, - 0x1.d599e83368e91p-45) A(0x1.0e00000000000p+0, - -0x1.b42dd71198000p-5, 0x1.c827ae5d6704cp-46) A(0x1.0e00000000000p+0, - -0x1.b42dd71198000p-5, 0x1.c827ae5d6704cp-46) A(0x1.0c00000000000p+0, - -0x1.77458f632c000p-5, -0x1.cfc4634f2a1eep-45) A(0x1.0c00000000000p+0, -0x1.77458f632c000p-5, -0x1.cfc4634f2a1eep-45) A(0x1.0a00000000000p+0, -0x1.39e87b9fec000p-5, 0x1.502b7f526feaap-48) A(0x1.0a00000000000p+0, - -0x1.39e87b9fec000p-5, 0x1.502b7f526feaap-48) A(0x1.0800000000000p+0, -0x1.f829b0e780000p-6, -0x1.980267c7e09e4p-45) A(0x1.0800000000000p+0, -0x1.f829b0e780000p-6, -0x1.980267c7e09e4p-45) A(0x1.0600000000000p+0, - -0x1.7b91b07d58000p-6, -0x1.88d5493faa639p-45) A(0x1.0400000000000p+0, - -0x1.fc0a8b0fc0000p-7, -0x1.f1e7cf6d3a69cp-50) A(0x1.0400000000000p+0, -0x1.fc0a8b0fc0000p-7, -0x1.f1e7cf6d3a69cp-50) A(0x1.0200000000000p+0, -0x1.fe02a6b100000p-8, -0x1.9e23f0dda40e4p-46) A(0x1.0200000000000p+0, - -0x1.fe02a6b100000p-8, - -0x1.9e23f0dda40e4p-46) A(0x1.0000000000000p+0, - 0x0.0000000000000p+0, 0x0.0000000000000p+0) A(0x1.0000000000000p+0, - 0x0.0000000000000p+0, - 0x0.0000000000000p+0) A(0x1.fc00000000000p-1, - 0x1.0101575890000p-7, -0x1.0c76b999d2be8p-46) A(0x1.f800000000000p-1, - 0x1.0205658938000p-6, -0x1.3dc5b06e2f7d2p-45) A(0x1.f400000000000p-1, - 0x1.8492528c90000p-6, - -0x1.aa0ba325a0c34p-45) A(0x1.f000000000000p-1, - 0x1.0415d89e74000p-5, - 0x1.111c05cf1d753p-47) A(0x1.ec00000000000p-1, 0x1.466aed42e0000p-5, -0x1.c167375bdfd28p-45) A(0x1.e800000000000p-1, - 0x1.894aa149fc000p-5, - -0x1.97995d05a267dp-46) A(0x1.e400000000000p-1, - 0x1.ccb73cdddc000p-5, -0x1.a68f247d82807p-46) A(0x1.e200000000000p-1, - 0x1.eea31c006c000p-5, - -0x1.e113e4fc93b7bp-47) A(0x1.de00000000000p-1, - 0x1.1973bd1466000p-4, - -0x1.5325d560d9e9bp-45) A(0x1.da00000000000p-1, 0x1.3bdf5a7d1e000p-4, 0x1.cc85ea5db4ed7p-45) A(0x1.d600000000000p-1, 0x1.5e95a4d97a000p-4, -0x1.c69063c5d1d1ep-45) A(0x1.d400000000000p-1, 0x1.700d30aeac000p-4, 0x1.c1e8da99ded32p-49) A(0x1.d000000000000p-1, - 0x1.9335e5d594000p-4, - 0x1.3115c3abd47dap-45) A(0x1.cc00000000000p-1, - 0x1.b6ac88dad6000p-4, - -0x1.390802bf768e5p-46) A(0x1.ca00000000000p-1, - 0x1.c885801bc4000p-4, - 0x1.646d1c65aacd3p-45) A(0x1.c600000000000p-1, - 0x1.ec739830a2000p-4, - -0x1.dc068afe645e0p-45) A(0x1.c400000000000p-1, - 0x1.fe89139dbe000p-4, - -0x1.534d64fa10afdp-45) A(0x1.c000000000000p-1, 0x1.1178e8227e000p-3, 0x1.1ef78ce2d07f2p-45) A(0x1.be00000000000p-1, 0x1.1aa2b7e23f000p-3, 0x1.ca78e44389934p-45) A(0x1.ba00000000000p-1, 0x1.2d1610c868000p-3, 0x1.39d6ccb81b4a1p-47) A(0x1.b800000000000p-1, 0x1.365fcb0159000p-3, 0x1.62fa8234b7289p-51) A(0x1.b400000000000p-1, 0x1.4913d8333b000p-3, 0x1.5837954fdb678p-45) A(0x1.b200000000000p-1, 0x1.527e5e4a1b000p-3, - 0x1.633e8e5697dc7p-45) A(0x1.ae00000000000p-1, - 0x1.6574ebe8c1000p-3, - 0x1.9cf8b2c3c2e78p-46) A(0x1.ac00000000000p-1, - 0x1.6f0128b757000p-3, -0x1.5118de59c21e1p-45) A(0x1.aa00000000000p-1, 0x1.7898d85445000p-3, -0x1.c661070914305p-46) A(0x1.a600000000000p-1, - 0x1.8beafeb390000p-3, -0x1.73d54aae92cd1p-47) A(0x1.a400000000000p-1, 0x1.95a5adcf70000p-3, 0x1.7f22858a0ff6fp-47) A(0x1.a000000000000p-1, 0x1.a93ed3c8ae000p-3, -0x1.8724350562169p-45) A(0x1.9e00000000000p-1, 0x1.b31d8575bd000p-3, -0x1.c358d4eace1aap-47) A(0x1.9c00000000000p-1, 0x1.bd087383be000p-3, -0x1.d4bc4595412b6p-45) A(0x1.9a00000000000p-1, 0x1.c6ffbc6f01000p-3, -0x1.1ec72c5962bd2p-48) A(0x1.9600000000000p-1, 0x1.db13db0d49000p-3, -0x1.aff2af715b035p-45) A(0x1.9400000000000p-1, - 0x1.e530effe71000p-3, - 0x1.212276041f430p-51) A(0x1.9200000000000p-1, 0x1.ef5ade4dd0000p-3, -0x1.a211565bb8e11p-51) A(0x1.9000000000000p-1, 0x1.f991c6cb3b000p-3, 0x1.bcbecca0cdf30p-46) A(0x1.8c00000000000p-1, 0x1.07138604d5800p-2, 0x1.89cdb16ed4e91p-48) A(0x1.8a00000000000p-1, - 0x1.0c42d67616000p-2, - 0x1.7188b163ceae9p-45) A(0x1.8800000000000p-1, 0x1.1178e8227e800p-2, -0x1.c210e63a5f01cp-45) A(0x1.8600000000000p-1, - 0x1.16b5ccbacf800p-2, - 0x1.b9acdf7a51681p-45) A(0x1.8400000000000p-1, - 0x1.1bf99635a6800p-2, - 0x1.ca6ed5147bdb7p-45) A(0x1.8200000000000p-1, - 0x1.214456d0eb800p-2, - 0x1.a87deba46baeap-47) A(0x1.7e00000000000p-1, - 0x1.2bef07cdc9000p-2, 0x1.a9cfa4a5004f4p-45) A(0x1.7c00000000000p-1, 0x1.314f1e1d36000p-2, -0x1.8e27ad3213cb8p-45) A(0x1.7a00000000000p-1, 0x1.36b6776be1000p-2, 0x1.16ecdb0f177c8p-46) A(0x1.7800000000000p-1, - 0x1.3c25277333000p-2, - 0x1.83b54b606bd5cp-46) A(0x1.7600000000000p-1, - 0x1.419b423d5e800p-2, - 0x1.8e436ec90e09dp-47) A(0x1.7400000000000p-1, 0x1.4718dc271c800p-2, -0x1.f27ce0967d675p-45) A(0x1.7200000000000p-1, 0x1.4c9e09e173000p-2, -0x1.e20891b0ad8a4p-45) A(0x1.7000000000000p-1, - 0x1.522ae0738a000p-2, - 0x1.ebe708164c759p-45) A(0x1.6e00000000000p-1, 0x1.57bf753c8d000p-2, 0x1.fadedee5d40efp-46) A(0x1.6c00000000000p-1, - 0x1.5d5bddf596000p-2, - -0x1.a0b2a08a465dcp-47)}, -}; - -#define T __pow_log_data.tab -#undef A -#define A __pow_log_data.poly -#define Ln2hi __pow_log_data.ln2hi -#define Ln2lo __pow_log_data.ln2lo - -/* Top 12 bits of a double (sign and exponent bits). */ -static inline uint32_t top12(double x) -{ - return asuint64(x) >> 52; -} - -/* Compute y+TAIL = log(x) where the rounded result is y and TAIL has about - additional 15 bits precision. IX is the bit representation of x, but - normalized in the subnormal range using the sign bit for the exponent. */ -static inline double_t log_inline(uint64_t ix, double_t *tail) -{ - /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ - double_t z, r, y, invc, logc, logctail, kd, hi, t1, t2, lo, lo1, lo2, p; - uint64_t iz, tmp; - int k, i; - - /* x = 2^k z; where z is in range [OFF,2*OFF) and exact. - The range is split into N subintervals. - The ith subinterval contains z and c is near its center. */ - tmp = ix - OFF; - i = (tmp >> (52 - POW_LOG_TABLE_BITS)) % N; - k = (int64_t)tmp >> 52; /* arithmetic shift */ - iz = ix - (tmp & 0xfffULL << 52); - z = asdouble(iz); - kd = (double_t)k; - - /* log(x) = k*Ln2 + log(c) + log1p(z/c-1). */ - invc = T[i].invc; - logc = T[i].logc; - logctail = T[i].logctail; - - /* Note: 1/c is j/N or j/N/2 where j is an integer in [N,2N) and - |z/c - 1| < 1/N, so r = z/c - 1 is exactly representible. */ -#if __FP_FAST_FMA - r = __builtin_fma(z, invc, -1.0); -#else - /* Split z such that rhi, rlo and rhi*rhi are exact and |rlo| <= |r|. */ - double_t zhi = asdouble((iz + (1ULL << 31)) & (-1ULL << 32)); - double_t zlo = z - zhi; - double_t rhi = zhi * invc - 1.0; - double_t rlo = zlo * invc; - r = rhi + rlo; -#endif - - /* k*Ln2 + log(c) + r. */ - t1 = kd * Ln2hi + logc; - t2 = t1 + r; - lo1 = kd * Ln2lo + logctail; - lo2 = t1 - t2 + r; - - /* Evaluation is optimized assuming superscalar pipelined execution. */ - double_t ar, ar2, ar3, lo3, lo4; - ar = A[0] * r; /* A[0] = -0.5. */ - ar2 = r * ar; - ar3 = r * ar2; - /* k*Ln2 + log(c) + r + A[0]*r*r. */ -#if __FP_FAST_FMA - hi = t2 + ar2; - lo3 = __builtin_fma(ar, r, -ar2); - lo4 = t2 - hi + ar2; -#else - double_t arhi = A[0] * rhi; - double_t arhi2 = rhi * arhi; - hi = t2 + arhi2; - lo3 = rlo * (ar + arhi); - lo4 = t2 - hi + arhi2; -#endif - /* p = log1p(r) - r - A[0]*r*r. */ - p = (ar3 * (A[1] + r * A[2] + ar2 * (A[3] + r * A[4] + ar2 * (A[5] + r * A[6])))); - lo = lo1 + lo2 + lo3 + lo4 + p; - y = hi + lo; - *tail = hi - y + lo; - return y; -} - -#undef N -#undef T -#define EXP_TABLE_BITS 7 -#define EXP_POLY_ORDER 5 -#define EXP_USE_TOINT_NARROW 0 -#define EXP2_POLY_ORDER 5 -struct exp_data { - double invln2N; - double shift; - double negln2hiN; - double negln2loN; - double poly[4]; /* Last four coefficients. */ - double exp2_shift; - double exp2_poly[EXP2_POLY_ORDER]; - uint64_t tab[2 * (1 << EXP_TABLE_BITS)]; -}; -#define N (1 << EXP_TABLE_BITS) - -const struct exp_data __exp_data = { - // N/ln2 - .invln2N = 0x1.71547652b82fep0 * N, - // -ln2/N - .negln2hiN = -0x1.62e42fefa0000p-8, - .negln2loN = -0x1.cf79abc9e3b3ap-47, -// Used for rounding when !TOINT_INTRINSICS -#if EXP_USE_TOINT_NARROW - .shift = 0x1800000000.8p0, -#else - .shift = 0x1.8p52, -#endif - // exp polynomial coefficients. - .poly = - { - // abs error: 1.555*2^-66 - // ulp error: 0.509 (0.511 without fma) - // if |x| < ln2/256+eps - // abs error if |x| < ln2/256+0x1p-15: 1.09*2^-65 - // abs error if |x| < ln2/128: 1.7145*2^-56 - 0x1.ffffffffffdbdp-2, - 0x1.555555555543cp-3, - 0x1.55555cf172b91p-5, - 0x1.1111167a4d017p-7, - }, - .exp2_shift = 0x1.8p52 / N, - // exp2 polynomial coefficients. - .exp2_poly = - { - // abs error: 1.2195*2^-65 - // ulp error: 0.507 (0.511 without fma) - // if |x| < 1/256 - // abs error if |x| < 1/128: 1.9941*2^-56 - 0x1.62e42fefa39efp-1, - 0x1.ebfbdff82c424p-3, - 0x1.c6b08d70cf4b5p-5, - 0x1.3b2abd24650ccp-7, - 0x1.5d7e09b4e3a84p-10, - }, - // 2^(k/N) ~= H[k]*(1 + T[k]) for int k in [0,N) - // tab[2*k] = asuint64(T[k]) - // tab[2*k+1] = asuint64(H[k]) - (k << 52)/N - .tab = - { - 0x0, - 0x3ff0000000000000, - 0x3c9b3b4f1a88bf6e, - 0x3feff63da9fb3335, - 0xbc7160139cd8dc5d, - 0x3fefec9a3e778061, - 0xbc905e7a108766d1, - 0x3fefe315e86e7f85, - 0x3c8cd2523567f613, - 0x3fefd9b0d3158574, - 0xbc8bce8023f98efa, - 0x3fefd06b29ddf6de, - 0x3c60f74e61e6c861, - 0x3fefc74518759bc8, - 0x3c90a3e45b33d399, - 0x3fefbe3ecac6f383, - 0x3c979aa65d837b6d, - 0x3fefb5586cf9890f, - 0x3c8eb51a92fdeffc, - 0x3fefac922b7247f7, - 0x3c3ebe3d702f9cd1, - 0x3fefa3ec32d3d1a2, - 0xbc6a033489906e0b, - 0x3fef9b66affed31b, - 0xbc9556522a2fbd0e, - 0x3fef9301d0125b51, - 0xbc5080ef8c4eea55, - 0x3fef8abdc06c31cc, - 0xbc91c923b9d5f416, - 0x3fef829aaea92de0, - 0x3c80d3e3e95c55af, - 0x3fef7a98c8a58e51, - 0xbc801b15eaa59348, - 0x3fef72b83c7d517b, - 0xbc8f1ff055de323d, - 0x3fef6af9388c8dea, - 0x3c8b898c3f1353bf, - 0x3fef635beb6fcb75, - 0xbc96d99c7611eb26, - 0x3fef5be084045cd4, - 0x3c9aecf73e3a2f60, - 0x3fef54873168b9aa, - 0xbc8fe782cb86389d, - 0x3fef4d5022fcd91d, - 0x3c8a6f4144a6c38d, - 0x3fef463b88628cd6, - 0x3c807a05b0e4047d, - 0x3fef3f49917ddc96, - 0x3c968efde3a8a894, - 0x3fef387a6e756238, - 0x3c875e18f274487d, - 0x3fef31ce4fb2a63f, - 0x3c80472b981fe7f2, - 0x3fef2b4565e27cdd, - 0xbc96b87b3f71085e, - 0x3fef24dfe1f56381, - 0x3c82f7e16d09ab31, - 0x3fef1e9df51fdee1, - 0xbc3d219b1a6fbffa, - 0x3fef187fd0dad990, - 0x3c8b3782720c0ab4, - 0x3fef1285a6e4030b, - 0x3c6e149289cecb8f, - 0x3fef0cafa93e2f56, - 0x3c834d754db0abb6, - 0x3fef06fe0a31b715, - 0x3c864201e2ac744c, - 0x3fef0170fc4cd831, - 0x3c8fdd395dd3f84a, - 0x3feefc08b26416ff, - 0xbc86a3803b8e5b04, - 0x3feef6c55f929ff1, - 0xbc924aedcc4b5068, - 0x3feef1a7373aa9cb, - 0xbc9907f81b512d8e, - 0x3feeecae6d05d866, - 0xbc71d1e83e9436d2, - 0x3feee7db34e59ff7, - 0xbc991919b3ce1b15, - 0x3feee32dc313a8e5, - 0x3c859f48a72a4c6d, - 0x3feedea64c123422, - 0xbc9312607a28698a, - 0x3feeda4504ac801c, - 0xbc58a78f4817895b, - 0x3feed60a21f72e2a, - 0xbc7c2c9b67499a1b, - 0x3feed1f5d950a897, - 0x3c4363ed60c2ac11, - 0x3feece086061892d, - 0x3c9666093b0664ef, - 0x3feeca41ed1d0057, - 0x3c6ecce1daa10379, - 0x3feec6a2b5c13cd0, - 0x3c93ff8e3f0f1230, - 0x3feec32af0d7d3de, - 0x3c7690cebb7aafb0, - 0x3feebfdad5362a27, - 0x3c931dbdeb54e077, - 0x3feebcb299fddd0d, - 0xbc8f94340071a38e, - 0x3feeb9b2769d2ca7, - 0xbc87deccdc93a349, - 0x3feeb6daa2cf6642, - 0xbc78dec6bd0f385f, - 0x3feeb42b569d4f82, - 0xbc861246ec7b5cf6, - 0x3feeb1a4ca5d920f, - 0x3c93350518fdd78e, - 0x3feeaf4736b527da, - 0x3c7b98b72f8a9b05, - 0x3feead12d497c7fd, - 0x3c9063e1e21c5409, - 0x3feeab07dd485429, - 0x3c34c7855019c6ea, - 0x3feea9268a5946b7, - 0x3c9432e62b64c035, - 0x3feea76f15ad2148, - 0xbc8ce44a6199769f, - 0x3feea5e1b976dc09, - 0xbc8c33c53bef4da8, - 0x3feea47eb03a5585, - 0xbc845378892be9ae, - 0x3feea34634ccc320, - 0xbc93cedd78565858, - 0x3feea23882552225, - 0x3c5710aa807e1964, - 0x3feea155d44ca973, - 0xbc93b3efbf5e2228, - 0x3feea09e667f3bcd, - 0xbc6a12ad8734b982, - 0x3feea012750bdabf, - 0xbc6367efb86da9ee, - 0x3fee9fb23c651a2f, - 0xbc80dc3d54e08851, - 0x3fee9f7df9519484, - 0xbc781f647e5a3ecf, - 0x3fee9f75e8ec5f74, - 0xbc86ee4ac08b7db0, - 0x3fee9f9a48a58174, - 0xbc8619321e55e68a, - 0x3fee9feb564267c9, - 0x3c909ccb5e09d4d3, - 0x3feea0694fde5d3f, - 0xbc7b32dcb94da51d, - 0x3feea11473eb0187, - 0x3c94ecfd5467c06b, - 0x3feea1ed0130c132, - 0x3c65ebe1abd66c55, - 0x3feea2f336cf4e62, - 0xbc88a1c52fb3cf42, - 0x3feea427543e1a12, - 0xbc9369b6f13b3734, - 0x3feea589994cce13, - 0xbc805e843a19ff1e, - 0x3feea71a4623c7ad, - 0xbc94d450d872576e, - 0x3feea8d99b4492ed, - 0x3c90ad675b0e8a00, - 0x3feeaac7d98a6699, - 0x3c8db72fc1f0eab4, - 0x3feeace5422aa0db, - 0xbc65b6609cc5e7ff, - 0x3feeaf3216b5448c, - 0x3c7bf68359f35f44, - 0x3feeb1ae99157736, - 0xbc93091fa71e3d83, - 0x3feeb45b0b91ffc6, - 0xbc5da9b88b6c1e29, - 0x3feeb737b0cdc5e5, - 0xbc6c23f97c90b959, - 0x3feeba44cbc8520f, - 0xbc92434322f4f9aa, - 0x3feebd829fde4e50, - 0xbc85ca6cd7668e4b, - 0x3feec0f170ca07ba, - 0x3c71affc2b91ce27, - 0x3feec49182a3f090, - 0x3c6dd235e10a73bb, - 0x3feec86319e32323, - 0xbc87c50422622263, - 0x3feecc667b5de565, - 0x3c8b1c86e3e231d5, - 0x3feed09bec4a2d33, - 0xbc91bbd1d3bcbb15, - 0x3feed503b23e255d, - 0x3c90cc319cee31d2, - 0x3feed99e1330b358, - 0x3c8469846e735ab3, - 0x3feede6b5579fdbf, - 0xbc82dfcd978e9db4, - 0x3feee36bbfd3f37a, - 0x3c8c1a7792cb3387, - 0x3feee89f995ad3ad, - 0xbc907b8f4ad1d9fa, - 0x3feeee07298db666, - 0xbc55c3d956dcaeba, - 0x3feef3a2b84f15fb, - 0xbc90a40e3da6f640, - 0x3feef9728de5593a, - 0xbc68d6f438ad9334, - 0x3feeff76f2fb5e47, - 0xbc91eee26b588a35, - 0x3fef05b030a1064a, - 0x3c74ffd70a5fddcd, - 0x3fef0c1e904bc1d2, - 0xbc91bdfbfa9298ac, - 0x3fef12c25bd71e09, - 0x3c736eae30af0cb3, - 0x3fef199bdd85529c, - 0x3c8ee3325c9ffd94, - 0x3fef20ab5fffd07a, - 0x3c84e08fd10959ac, - 0x3fef27f12e57d14b, - 0x3c63cdaf384e1a67, - 0x3fef2f6d9406e7b5, - 0x3c676b2c6c921968, - 0x3fef3720dcef9069, - 0xbc808a1883ccb5d2, - 0x3fef3f0b555dc3fa, - 0xbc8fad5d3ffffa6f, - 0x3fef472d4a07897c, - 0xbc900dae3875a949, - 0x3fef4f87080d89f2, - 0x3c74a385a63d07a7, - 0x3fef5818dcfba487, - 0xbc82919e2040220f, - 0x3fef60e316c98398, - 0x3c8e5a50d5c192ac, - 0x3fef69e603db3285, - 0x3c843a59ac016b4b, - 0x3fef7321f301b460, - 0xbc82d52107b43e1f, - 0x3fef7c97337b9b5f, - 0xbc892ab93b470dc9, - 0x3fef864614f5a129, - 0x3c74b604603a88d3, - 0x3fef902ee78b3ff6, - 0x3c83c5ec519d7271, - 0x3fef9a51fbc74c83, - 0xbc8ff7128fd391f0, - 0x3fefa4afa2a490da, - 0xbc8dae98e223747d, - 0x3fefaf482d8e67f1, - 0x3c8ec3bc41aa2008, - 0x3fefba1bee615a27, - 0x3c842b94c3a9eb32, - 0x3fefc52b376bba97, - 0x3c8a64a931d185ee, - 0x3fefd0765b6e4540, - 0xbc8e37bae43be3ed, - 0x3fefdbfdad9cbe14, - 0x3c77893b4d91cd9d, - 0x3fefe7c1819e90d8, - 0x3c5305c14160cc89, - 0x3feff3c22b8f71f1, - }, -}; - -#define InvLn2N __exp_data.invln2N -#define NegLn2hiN __exp_data.negln2hiN -#define NegLn2loN __exp_data.negln2loN -#define Shift __exp_data.shift -#define T __exp_data.tab -#define C2 __exp_data.poly[5 - EXP_POLY_ORDER] -#define C3 __exp_data.poly[6 - EXP_POLY_ORDER] -#define C4 __exp_data.poly[7 - EXP_POLY_ORDER] -#define C5 __exp_data.poly[8 - EXP_POLY_ORDER] -#define C6 __exp_data.poly[9 - EXP_POLY_ORDER] - -static inline double specialcase(double_t tmp, uint64_t sbits, uint64_t ki) -{ - double_t scale, y; - - if ((ki & 0x80000000) == 0) { - /* k > 0, the exponent of scale might have overflowed by <= 460. */ - sbits -= 1009ull << 52; - scale = asdouble(sbits); - y = 0x1p1009 * (scale + scale * tmp); - return eval_as_double(y); - } - /* k < 0, need special care in the subnormal range. */ - sbits += 1022ull << 52; - /* Note: sbits is signed scale. */ - scale = asdouble(sbits); - y = scale + scale * tmp; - if (fabs(y) < 1.0) { - /* Round y to the right precision before scaling it into the subnormal - range to avoid double rounding that can cause 0.5+E/2 ulp error where - E is the worst-case ulp error outside the subnormal range. So this - is only useful if the goal is better than 1 ulp worst-case error. */ - double_t hi, lo, one = 1.0; - if (y < 0.0) - one = -1.0; - lo = scale - y + scale * tmp; - hi = one + y; - lo = one - hi + y + lo; - y = eval_as_double(hi + lo) - one; - /* Fix the sign of 0. */ - if (y == 0.0) - y = asdouble(sbits & 0x8000000000000000); - /* The underflow exception needs to be signaled explicitly. */ - fp_force_eval(fp_barrier(0x1p-1022) * 0x1p-1022); - } - y = 0x1p-1022 * y; - return eval_as_double(y); -} - -#define SIGN_BIAS (0x800 << EXP_TABLE_BITS) - -double __math_xflow(uint32_t sign, double y) -{ - return eval_as_double(fp_barrier(sign ? -y : y) * y); -} - -double __math_uflow(uint32_t sign) -{ - return __math_xflow(sign, 0x1p-767); -} - -double __math_oflow(uint32_t sign) -{ - return __math_xflow(sign, 0x1p769); -} - -/* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|. - The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1. */ -static inline double exp_inline(double_t x, double_t xtail, uint32_t sign_bias) -{ - uint32_t abstop; - uint64_t ki, idx, top, sbits; - /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ - double_t kd, z, r, r2, scale, tail, tmp; - - abstop = top12(x) & 0x7ff; - if (predict_false(abstop - top12(0x1p-54) >= top12(512.0) - top12(0x1p-54))) { - if (abstop - top12(0x1p-54) >= 0x80000000) { - /* Avoid spurious underflow for tiny x. */ - /* Note: 0 is common input. */ - double_t one = WANT_ROUNDING ? 1.0 + x : 1.0; - return sign_bias ? -one : one; - } - if (abstop >= top12(1024.0)) { - /* Note: inf and nan are already handled. */ - if (asuint64(x) >> 63) - return __math_uflow(sign_bias); - else - return __math_oflow(sign_bias); - } - /* Large x is special cased below. */ - abstop = 0; - } - - /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */ - /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */ - z = InvLn2N * x; -#if TOINT_INTRINSICS - kd = roundtoint(z); - ki = converttoint(z); -#elif EXP_USE_TOINT_NARROW - /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */ - kd = eval_as_double(z + Shift); - ki = asuint64(kd) >> 16; - kd = (double_t)(int32_t)ki; -#else - /* z - kd is in [-1, 1] in non-nearest rounding modes. */ - kd = eval_as_double(z + Shift); - ki = asuint64(kd); - kd -= Shift; -#endif - r = x + kd * NegLn2hiN + kd * NegLn2loN; - /* The code assumes 2^-200 < |xtail| < 2^-8/N. */ - r += xtail; - /* 2^(k/N) ~= scale * (1 + tail). */ - idx = 2 * (ki % N); - top = (ki + sign_bias) << (52 - EXP_TABLE_BITS); - tail = asdouble(T[idx]); - /* This is only a valid scale when -1023*N < k < 1024*N. */ - sbits = T[idx + 1] + top; - /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */ - /* Evaluation is optimized assuming superscalar pipelined execution. */ - r2 = r * r; - /* Without fma the worst case error is 0.25/N ulp larger. */ - /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */ - tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5); - if (predict_false(abstop == 0)) - return specialcase(tmp, sbits, ki); - scale = asdouble(sbits); - /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there - is no spurious underflow here even without fma. */ - return eval_as_double(scale + scale * tmp); -} - -/* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is - the bit representation of a non-zero finite floating-point value. */ -static inline int checkint(uint64_t iy) -{ - int e = iy >> 52 & 0x7ff; - if (e < 0x3ff) - return 0; - if (e > 0x3ff + 52) - return 2; - if (iy & ((1ULL << (0x3ff + 52 - e)) - 1)) - return 0; - if (iy & (1ULL << (0x3ff + 52 - e))) - return 1; - return 2; -} - -#if 100 * __GNUC__ + __GNUC_MINOR__ >= 303 -#define NAN __builtin_nanf("") -#define INFINITY __builtin_inff() -#else -#define NAN (0.0f / 0.0f) -#define INFINITY 1e5000f -#endif - -static inline int zeroinfnan(uint64_t i) -{ - return 2 * i - 1 >= 2 * asuint64(INFINITY) - 1; -} - -#if WANT_SNAN -#error SNaN is unsupported -#else -#define issignalingf_inline(x) 0 -#define issignaling_inline(x) 0 -#endif - -double pow(double x, double y) -{ - uint32_t sign_bias = 0; - uint64_t ix, iy; - uint32_t topx, topy; - - ix = asuint64(x); - iy = asuint64(y); - topx = top12(x); - topy = top12(y); - if (predict_false(topx - 0x001 >= 0x7ff - 0x001 || (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be)) { - /* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0 - and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1. */ - /* Special cases: (x < 0x1p-126 or inf or nan) or - (|y| < 0x1p-65 or |y| >= 0x1p63 or nan). */ - if (predict_false(zeroinfnan(iy))) { - if (2 * iy == 0) - return issignaling_inline(x) ? x + y : 1.0; - if (ix == asuint64(1.0)) - return issignaling_inline(y) ? x + y : 1.0; - if (2 * ix > 2 * asuint64(INFINITY) || 2 * iy > 2 * asuint64(INFINITY)) - return x + y; - if (2 * ix == 2 * asuint64(1.0)) - return 1.0; - if ((2 * ix < 2 * asuint64(1.0)) == !(iy >> 63)) - return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf. */ - return y * y; - } - if (predict_false(zeroinfnan(ix))) { - double_t x2 = x * x; - if (ix >> 63 && checkint(iy) == 1) - x2 = -x2; - /* Without the barrier some versions of clang hoist the 1/x2 and - thus division by zero exception can be signaled spuriously. */ - return iy >> 63 ? fp_barrier(1 / x2) : x2; - } - /* Here x and y are non-zero finite. */ - if (ix >> 63) { - /* Finite x < 0. */ - int yint = checkint(iy); - if (yint == 0) - return __math_invalid(x); - if (yint == 1) - sign_bias = SIGN_BIAS; - ix &= 0x7fffffffffffffff; - topx &= 0x7ff; - } - if ((topy & 0x7ff) - 0x3be >= 0x43e - 0x3be) { - /* Note: sign_bias == 0 here because y is not odd. */ - if (ix == asuint64(1.0)) - return 1.0; - if ((topy & 0x7ff) < 0x3be) { - /* |y| < 2^-65, x^y ~= 1 + y*log(x). */ - if (WANT_ROUNDING) - return ix > asuint64(1.0) ? 1.0 + y : 1.0 - y; - else - return 1.0; - } - return (ix > asuint64(1.0)) == (topy < 0x800) ? __math_oflow(0) : __math_uflow(0); - } - if (topx == 0) { - /* Normalize subnormal x so exponent becomes negative. */ - ix = asuint64(x * 0x1p52); - ix &= 0x7fffffffffffffff; - ix -= 52ULL << 52; - } - } - - double_t lo; - double_t hi = log_inline(ix, &lo); - double_t ehi, elo; -#if __FP_FAST_FMA - ehi = y * hi; - elo = y * lo + __builtin_fma(y, hi, -ehi); -#else - double_t yhi = asdouble(iy & -1ULL << 27); - double_t ylo = y - yhi; - double_t lhi = asdouble(asuint64(hi) & -1ULL << 27); - double_t llo = hi - lhi + lo; - ehi = yhi * lhi; - elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25. */ -#endif - return exp_inline(ehi, elo, sign_bias); -} -#endif diff --git a/ulib/ruxlibc/c/prctl.c b/ulib/ruxlibc/c/prctl.c deleted file mode 100644 index 7f0f1ee2c..000000000 --- a/ulib/ruxlibc/c/prctl.c +++ /dev/null @@ -1,19 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int prctl(int option, ...) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/printf.c b/ulib/ruxlibc/c/printf.c deleted file mode 100644 index 128cfdb66..000000000 --- a/ulib/ruxlibc/c/printf.c +++ /dev/null @@ -1,1491 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -/** - * @author (c) Eyal Rozenberg - * 2021-2022, Haifa, Palestine/Israel - * @author (c) Marco Paland (info@paland.com) - * 2014-2019, PALANDesign Hannover, Germany - * - * @note Others have made smaller contributions to this file: see the - * contributors page at https://github.com/eyalroz/printf/graphs/contributors - * or ask one of the authors. The original code for exponential specifiers was - * contributed by Martijn Jasperse . - * - * @brief Small stand-alone implementation of the printf family of functions - * (`(v)printf`, `(v)s(n)printf` etc., geared towards use on embedded systems with - * a very limited resources. - * - * @note the implementations are thread-safe; re-entrant; use no functions from - * the standard library; and do not dynamically allocate any memory. - * - * @license The MIT License (MIT) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// Define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H=1 ...) to include the -// printf_config.h header file -#define PRINTF_INCLUDE_CONFIG_H 1 - -#if PRINTF_INCLUDE_CONFIG_H -#include "printf_config.h" -#endif - -#include "printf.h" - -#ifdef __cplusplus -#include -#include -#else -#include -#include -#include -#include -#endif // __cplusplus - -#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES -#define printf_ printf -#define sprintf_ sprintf -#define vsprintf_ vsprintf -#define snprintf_ snprintf -#define vsnprintf_ vsnprintf -#define vprintf_ vprintf -#endif - -// 'ntoa' conversion buffer size, this must be big enough to hold one converted -// numeric number including padded zeros (dynamically created on stack) -#ifndef PRINTF_INTEGER_BUFFER_SIZE -#define PRINTF_INTEGER_BUFFER_SIZE 32 -#endif - -// size of the fixed (on-stack) buffer for printing individual decimal numbers. -// this must be big enough to hold one converted floating-point value including -// padded zeros. -#ifndef PRINTF_DECIMAL_BUFFER_SIZE -#define PRINTF_DECIMAL_BUFFER_SIZE 32 -#endif - -// Support for the decimal notation floating point conversion specifiers (%f, %F) -#ifndef PRINTF_SUPPORT_DECIMAL_SPECIFIERS -#define PRINTF_SUPPORT_DECIMAL_SPECIFIERS 1 -#endif - -// Support for the exponential notation floating point conversion specifiers (%e, %g, %E, %G) -#ifndef PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS -#define PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS 1 -#endif - -// Support for the length write-back specifier (%n) -#ifndef PRINTF_SUPPORT_WRITEBACK_SPECIFIER -#define PRINTF_SUPPORT_WRITEBACK_SPECIFIER 1 -#endif - -// Default precision for the floating point conversion specifiers (the C standard sets this at 6) -#ifndef PRINTF_DEFAULT_FLOAT_PRECISION -#define PRINTF_DEFAULT_FLOAT_PRECISION 6 -#endif - -// According to the C languages standard, printf() and related functions must be able to print any -// integral number in floating-point notation, regardless of length, when using the %f specifier - -// possibly hundreds of characters, potentially overflowing your buffers. In this implementation, -// all values beyond this threshold are switched to exponential notation. -#ifndef PRINTF_MAX_INTEGRAL_DIGITS_FOR_DECIMAL -#define PRINTF_MAX_INTEGRAL_DIGITS_FOR_DECIMAL 9 -#endif - -// Support for the long long integral types (with the ll, z and t length modifiers for specifiers -// %d,%i,%o,%x,%X,%u, and with the %p specifier). Note: 'L' (long double) is not supported. -#ifndef PRINTF_SUPPORT_LONG_LONG -#define PRINTF_SUPPORT_LONG_LONG 1 -#endif - -// The number of terms in a Taylor series expansion of log_10(x) to -// use for approximation - including the power-zero term (i.e. the -// value at the point of expansion). -#ifndef PRINTF_LOG10_TAYLOR_TERMS -#define PRINTF_LOG10_TAYLOR_TERMS 4 -#endif - -#if PRINTF_LOG10_TAYLOR_TERMS <= 1 -#error "At least one non-constant Taylor expansion is necessary for the log10() calculation" -#endif - -// Be extra-safe, and don't assume format specifiers are completed correctly -// before the format string end. -#ifndef PRINTF_CHECK_FOR_NUL_IN_FORMAT_SPECIFIER -#define PRINTF_CHECK_FOR_NUL_IN_FORMAT_SPECIFIER 1 -#endif - -#define PRINTF_PREFER_DECIMAL false -#define PRINTF_PREFER_EXPONENTIAL true - -/////////////////////////////////////////////////////////////////////////////// - -// The following will convert the number-of-digits into an exponential-notation literal -#define PRINTF_CONCATENATE(s1, s2) s1##s2 -#define PRINTF_EXPAND_THEN_CONCATENATE(s1, s2) PRINTF_CONCATENATE(s1, s2) -#define PRINTF_FLOAT_NOTATION_THRESHOLD \ - PRINTF_EXPAND_THEN_CONCATENATE(1e, PRINTF_MAX_INTEGRAL_DIGITS_FOR_DECIMAL) - -// internal flag definitions -#define FLAGS_ZEROPAD (1U << 0U) -#define FLAGS_LEFT (1U << 1U) -#define FLAGS_PLUS (1U << 2U) -#define FLAGS_SPACE (1U << 3U) -#define FLAGS_HASH (1U << 4U) -#define FLAGS_UPPERCASE (1U << 5U) -#define FLAGS_CHAR (1U << 6U) -#define FLAGS_SHORT (1U << 7U) -#define FLAGS_INT (1U << 8U) -// Only used with PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS -#define FLAGS_LONG (1U << 9U) -#define FLAGS_LONG_LONG (1U << 10U) -#define FLAGS_PRECISION (1U << 11U) -#define FLAGS_ADAPT_EXP (1U << 12U) -#define FLAGS_POINTER (1U << 13U) -// Note: Similar, but not identical, effect as FLAGS_HASH -#define FLAGS_SIGNED (1U << 14U) -// Only used with PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS - -#ifdef PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS - -#define FLAGS_INT8 FLAGS_CHAR - -#if (SHRT_MAX == 32767LL) -#define FLAGS_INT16 FLAGS_SHORT -#elif (INT_MAX == 32767LL) -#define FLAGS_INT16 FLAGS_INT -#elif (LONG_MAX == 32767LL) -#define FLAGS_INT16 FLAGS_LONG -#elif (LLONG_MAX == 32767LL) -#define FLAGS_INT16 FLAGS_LONG_LONG -#else -#error "No basic integer type has a size of 16 bits exactly" -#endif - -#if (SHRT_MAX == 2147483647LL) -#define FLAGS_INT32 FLAGS_SHORT -#elif (INT_MAX == 2147483647LL) -#define FLAGS_INT32 FLAGS_INT -#elif (LONG_MAX == 2147483647LL) -#define FLAGS_INT32 FLAGS_LONG -#elif (LLONG_MAX == 2147483647LL) -#define FLAGS_INT32 FLAGS_LONG_LONG -#else -#error "No basic integer type has a size of 32 bits exactly" -#endif - -#if (SHRT_MAX == 9223372036854775807LL) -#define FLAGS_INT64 FLAGS_SHORT -#elif (INT_MAX == 9223372036854775807LL) -#define FLAGS_INT64 FLAGS_INT -#elif (LONG_MAX == 9223372036854775807LL) -#define FLAGS_INT64 FLAGS_LONG -#elif (LLONG_MAX == 9223372036854775807LL) -#define FLAGS_INT64 FLAGS_LONG_LONG -#else -#error "No basic integer type has a size of 64 bits exactly" -#endif - -#endif // PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS - -typedef unsigned int printf_flags_t; - -#define BASE_BINARY 2 -#define BASE_OCTAL 8 -#define BASE_DECIMAL 10 -#define BASE_HEX 16 - -typedef uint8_t numeric_base_t; - -#if PRINTF_SUPPORT_LONG_LONG -typedef unsigned long long printf_unsigned_value_t; -typedef long long printf_signed_value_t; -#else -typedef unsigned long printf_unsigned_value_t; -typedef long printf_signed_value_t; -#endif - -// The printf()-family functions return an `int`; it is therefore -// unnecessary/inappropriate to use size_t - often larger than int -// in practice - for non-negative related values, such as widths, -// precisions, offsets into buffers used for printing and the sizes -// of these buffers. instead, we use: -typedef unsigned int printf_size_t; -#define PRINTF_MAX_POSSIBLE_BUFFER_SIZE INT_MAX -// If we were to nitpick, this would actually be INT_MAX + 1, -// since INT_MAX is the maximum return value, which excludes the -// trailing '\0'. - -#if (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS) -#include -#if FLT_RADIX != 2 -#error "Non-binary-radix floating-point types are unsupported." -#endif - -#if DBL_MANT_DIG == 24 - -#define DOUBLE_SIZE_IN_BITS 32 -typedef uint32_t double_uint_t; -#define DOUBLE_EXPONENT_MASK 0xFFU -#define DOUBLE_BASE_EXPONENT 127 -#define DOUBLE_MAX_SUBNORMAL_EXPONENT_OF_10 -38 -#define DOUBLE_MAX_SUBNORMAL_POWER_OF_10 1e-38 - -#elif DBL_MANT_DIG == 53 - -#define DOUBLE_SIZE_IN_BITS 64 -typedef uint64_t double_uint_t; -#define DOUBLE_EXPONENT_MASK 0x7FFU -#define DOUBLE_BASE_EXPONENT 1023 -#define DOUBLE_MAX_SUBNORMAL_EXPONENT_OF_10 -308 -#define DOUBLE_MAX_SUBNORMAL_POWER_OF_10 1e-308 - -#else -#error "Unsupported double type configuration" -#endif -#define DOUBLE_STORED_MANTISSA_BITS (DBL_MANT_DIG - 1) - -typedef union { - double_uint_t U; - double F; -} double_with_bit_access; - -// This is unnecessary in C99, since compound initializers can be used, -// but: -// 1. Some compilers are finicky about this; -// 2. Some people may want to convert this to C89; -// 3. If you try to use it as C++, only C++20 supports compound literals -static inline double_with_bit_access get_bit_access(double x) -{ - double_with_bit_access dwba; - dwba.F = x; - return dwba; -} - -static inline int get_sign_bit(double x) -{ - // The sign is stored in the highest bit - return (int)(get_bit_access(x).U >> (DOUBLE_SIZE_IN_BITS - 1)); -} - -static inline int get_exp2(double_with_bit_access x) -{ - // The exponent in an IEEE-754 floating-point number occupies a contiguous - // sequence of bits (e.g. 52..62 for 64-bit doubles), but with a non-trivial representation: An - // unsigned offset from some negative value (with the extremal offset values reserved for - // special use). - return (int)((x.U >> DOUBLE_STORED_MANTISSA_BITS) & DOUBLE_EXPONENT_MASK) - - DOUBLE_BASE_EXPONENT; -} -#define PRINTF_ABS(_x) ((_x) > 0 ? (_x) : -(_x)) - -#endif // (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS) - -// Note in particular the behavior here on LONG_MIN or LLONG_MIN; it is valid -// and well-defined, but if you're not careful you can easily trigger undefined -// behavior with -LONG_MIN or -LLONG_MIN -#define ABS_FOR_PRINTING(_x) \ - ((printf_unsigned_value_t)((_x) > 0 ? (_x) : -((printf_signed_value_t)_x))) - -// wrapper (used as buffer) for output function type -// -// One of the following must hold: -// 1. max_chars is 0 -// 2. buffer is non-null -// 3. function is non-null -// -// ... otherwise bad things will happen. -typedef struct { - void (*function)(char c, void *extra_arg); - void *extra_function_arg; - char *buffer; - printf_size_t pos; - printf_size_t max_chars; -} output_gadget_t; - -// Note: This function currently assumes it is not passed a '\0' c, -// or alternatively, that '\0' can be passed to the function in the output -// gadget. The former assumption holds within the printf library. It also -// assumes that the output gadget has been properly initialized. -static inline void putchar_via_gadget(output_gadget_t *gadget, char c) -{ - printf_size_t write_pos = gadget->pos++; - // We're _always_ increasing pos, so as to count how may characters - // _would_ have been written if not for the max_chars limitation - if (write_pos >= gadget->max_chars) { - return; - } - if (gadget->function != NULL) { - // No check for c == '\0' . - gadget->function(c, gadget->extra_function_arg); - } else { - // it must be the case that gadget->buffer != NULL , due to the constraint - // on output_gadget_t ; and note we're relying on write_pos being non-negative. - gadget->buffer[write_pos] = c; - } -} - -// Possibly-write the string-terminating '\0' character -static inline void append_termination_with_gadget(output_gadget_t *gadget) -{ - if (gadget->function != NULL || gadget->max_chars == 0) { - return; - } - if (gadget->buffer == NULL) { - return; - } - printf_size_t null_char_pos = - gadget->pos < gadget->max_chars ? gadget->pos : gadget->max_chars - 1; - gadget->buffer[null_char_pos] = '\0'; -} - -// We can't use putchar_ as is, since our output gadget -// only takes pointers to functions with an extra argument -// static inline void putchar_wrapper(char c, void *unused) -// { -// (void)unused; -// putchar_(c); -// } - -static inline output_gadget_t discarding_gadget(void) -{ - output_gadget_t gadget; - gadget.function = NULL; - gadget.extra_function_arg = NULL; - gadget.buffer = NULL; - gadget.pos = 0; - gadget.max_chars = 0; - return gadget; -} - -static inline output_gadget_t buffer_gadget(char *buffer, size_t buffer_size) -{ - printf_size_t usable_buffer_size = (buffer_size > PRINTF_MAX_POSSIBLE_BUFFER_SIZE) - ? PRINTF_MAX_POSSIBLE_BUFFER_SIZE - : (printf_size_t)buffer_size; - output_gadget_t result = discarding_gadget(); - if (buffer != NULL) { - result.buffer = buffer; - result.max_chars = usable_buffer_size; - } - return result; -} - -static inline output_gadget_t function_gadget(void (*function)(char, void *), void *extra_arg) -{ - output_gadget_t result = discarding_gadget(); - result.function = function; - result.extra_function_arg = extra_arg; - result.max_chars = PRINTF_MAX_POSSIBLE_BUFFER_SIZE; - return result; -} - -// static inline output_gadget_t extern_putchar_gadget(void) -// { -// return function_gadget(putchar_wrapper, NULL); -// } - -// internal secure strlen -// @return The length of the string (excluding the terminating 0) limited by 'maxsize' -// @note strlen uses size_t, but wes only use this function with printf_size_t -// variables - hence the signature. -static inline printf_size_t strnlen_s_(const char *str, printf_size_t maxsize) -{ - const char *s; - for (s = str; *s && maxsize--; ++s) - ; - return (printf_size_t)(s - str); -} - -// internal test if char is a digit (0-9) -// @return true if char is a digit -static inline bool is_digit_(char ch) -{ - return (ch >= '0') && (ch <= '9'); -} - -// internal ASCII string to printf_size_t conversion -static printf_size_t atou_(const char **str) -{ - printf_size_t i = 0U; - while (is_digit_(**str)) { - i = i * 10U + (printf_size_t)(*((*str)++) - '0'); - } - return i; -} - -// output the specified string in reverse, taking care of any zero-padding -static void out_rev_(output_gadget_t *output, const char *buf, printf_size_t len, - printf_size_t width, printf_flags_t flags) -{ - const printf_size_t start_pos = output->pos; - - // pad spaces up to given width - if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { - for (printf_size_t i = len; i < width; i++) { - putchar_via_gadget(output, ' '); - } - } - - // reverse string - while (len) { - putchar_via_gadget(output, buf[--len]); - } - - // append pad spaces up to given width - if (flags & FLAGS_LEFT) { - while (output->pos - start_pos < width) { - putchar_via_gadget(output, ' '); - } - } -} - -// Invoked by print_integer after the actual number has been printed, performing necessary -// work on the number's prefix (as the number is initially printed in reverse order) -static void print_integer_finalization(output_gadget_t *output, char *buf, printf_size_t len, - bool negative, numeric_base_t base, printf_size_t precision, - printf_size_t width, printf_flags_t flags) -{ - printf_size_t unpadded_len = len; - - // pad with leading zeros - { - if (!(flags & FLAGS_LEFT)) { - if (width && (flags & FLAGS_ZEROPAD) && - (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { - width--; - } - while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_INTEGER_BUFFER_SIZE)) { - buf[len++] = '0'; - } - } - - while ((len < precision) && (len < PRINTF_INTEGER_BUFFER_SIZE)) { - buf[len++] = '0'; - } - - if (base == BASE_OCTAL && (len > unpadded_len)) { - // Since we've written some zeros, we've satisfied the alternative format leading space - // requirement - flags &= ~FLAGS_HASH; - } - } - - // handle hash - if (flags & (FLAGS_HASH | FLAGS_POINTER)) { - if (!(flags & FLAGS_PRECISION) && len && ((len == precision) || (len == width))) { - // Let's take back some padding digits to fit in what will eventually - // be the format-specific prefix - if (unpadded_len < len) { - len--; // This should suffice for BASE_OCTAL - } - if (len && (base == BASE_HEX || base == BASE_BINARY) && (unpadded_len < len)) { - len--; // ... and an extra one for 0x or 0b - } - } - if ((base == BASE_HEX) && !(flags & FLAGS_UPPERCASE) && - (len < PRINTF_INTEGER_BUFFER_SIZE)) { - buf[len++] = 'x'; - } else if ((base == BASE_HEX) && (flags & FLAGS_UPPERCASE) && - (len < PRINTF_INTEGER_BUFFER_SIZE)) { - buf[len++] = 'X'; - } else if ((base == BASE_BINARY) && (len < PRINTF_INTEGER_BUFFER_SIZE)) { - buf[len++] = 'b'; - } - if (len < PRINTF_INTEGER_BUFFER_SIZE) { - buf[len++] = '0'; - } - } - - if (len < PRINTF_INTEGER_BUFFER_SIZE) { - if (negative) { - buf[len++] = '-'; - } else if (flags & FLAGS_PLUS) { - buf[len++] = '+'; // ignore the space if the '+' exists - } else if (flags & FLAGS_SPACE) { - buf[len++] = ' '; - } - } - - out_rev_(output, buf, len, width, flags); -} - -// An internal itoa-like function -static void print_integer(output_gadget_t *output, printf_unsigned_value_t value, bool negative, - numeric_base_t base, printf_size_t precision, printf_size_t width, - printf_flags_t flags) -{ - char buf[PRINTF_INTEGER_BUFFER_SIZE]; - printf_size_t len = 0U; - - if (!value) { - if (!(flags & FLAGS_PRECISION)) { - buf[len++] = '0'; - flags &= ~FLAGS_HASH; - // We drop this flag this since either the alternative and regular modes of the - // specifier don't differ on 0 values, or (in the case of octal) we've already provided - // the special handling for this mode. - } else if (base == BASE_HEX) { - flags &= ~FLAGS_HASH; - // We drop this flag this since either the alternative and regular modes of the - // specifier don't differ on 0 values - } - } else { - do { - const char digit = (char)(value % base); - buf[len++] = (char)(digit < 10 ? '0' + digit - : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10); - value /= base; - } while (value && (len < PRINTF_INTEGER_BUFFER_SIZE)); - } - - print_integer_finalization(output, buf, len, negative, base, precision, width, flags); -} - -#if (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS) - -// Stores a fixed-precision representation of a double relative -// to a fixed precision (which cannot be determined by examining this structure) -struct double_components { - int_fast64_t integral; - int_fast64_t fractional; - // ... truncation of the actual fractional part of the double value, scaled - // by the precision value - bool is_negative; -}; - -#define NUM_DECIMAL_DIGITS_IN_INT64_T 18 -#define PRINTF_MAX_PRECOMPUTED_POWER_OF_10 NUM_DECIMAL_DIGITS_IN_INT64_T -static const double powers_of_10[NUM_DECIMAL_DIGITS_IN_INT64_T] = { - 1e00, 1e01, 1e02, 1e03, 1e04, 1e05, 1e06, 1e07, 1e08, - 1e09, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17}; - -#define PRINTF_MAX_SUPPORTED_PRECISION NUM_DECIMAL_DIGITS_IN_INT64_T - 1 - -// Break up a double number - which is known to be a finite non-negative number - -// into its base-10 parts: integral - before the decimal point, and fractional - after it. -// Taken the precision into account, but does not change it even internally. -static struct double_components get_components(double number, printf_size_t precision) -{ - struct double_components number_; - number_.is_negative = get_sign_bit(number); - double abs_number = (number_.is_negative) ? -number : number; - number_.integral = (int_fast64_t)abs_number; - double remainder = (abs_number - (double)number_.integral) * powers_of_10[precision]; - number_.fractional = (int_fast64_t)remainder; - - remainder -= (double)number_.fractional; - - if (remainder > 0.5) { - ++number_.fractional; - // handle rollover, e.g. case 0.99 with precision 1 is 1.0 - if ((double)number_.fractional >= powers_of_10[precision]) { - number_.fractional = 0; - ++number_.integral; - } - } else if ((remainder == 0.5) && ((number_.fractional == 0U) || (number_.fractional & 1U))) { - // if halfway, round up if odd OR if last digit is 0 - ++number_.fractional; - } - - if (precision == 0U) { - remainder = abs_number - (double)number_.integral; - if ((!(remainder < 0.5) || (remainder > 0.5)) && (number_.integral & 1)) { - // exactly 0.5 and ODD, then round up - // 1.5 -> 2, but 2.5 -> 2 - ++number_.integral; - } - } - return number_; -} - -#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS -struct scaling_factor { - double raw_factor; - bool multiply; // if true, need to multiply by raw_factor; otherwise need to divide by it -}; - -static double apply_scaling(double num, struct scaling_factor normalization) -{ - return normalization.multiply ? num * normalization.raw_factor : num / normalization.raw_factor; -} - -static double unapply_scaling(double normalized, struct scaling_factor normalization) -{ -#ifdef __GNUC__ -// accounting for a static analysis bug in GCC 6.x and earlier -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" -#endif - return normalization.multiply ? normalized / normalization.raw_factor - : normalized * normalization.raw_factor; -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif -} - -static struct scaling_factor update_normalization(struct scaling_factor sf, - double extra_multiplicative_factor) -{ - struct scaling_factor result; - if (sf.multiply) { - result.multiply = true; - result.raw_factor = sf.raw_factor * extra_multiplicative_factor; - } else { - int factor_exp2 = get_exp2(get_bit_access(sf.raw_factor)); - int extra_factor_exp2 = get_exp2(get_bit_access(extra_multiplicative_factor)); - - // Divide the larger-exponent raw raw_factor by the smaller - if (PRINTF_ABS(factor_exp2) > PRINTF_ABS(extra_factor_exp2)) { - result.multiply = false; - result.raw_factor = sf.raw_factor / extra_multiplicative_factor; - } else { - result.multiply = true; - result.raw_factor = extra_multiplicative_factor / sf.raw_factor; - } - } - return result; -} - -static struct double_components get_normalized_components(bool negative, printf_size_t precision, - double non_normalized, - struct scaling_factor normalization, - int floored_exp10) -{ - struct double_components components; - components.is_negative = negative; - double scaled = apply_scaling(non_normalized, normalization); - - bool close_to_representation_extremum = - ((-floored_exp10 + (int)precision) >= DBL_MAX_10_EXP - 1); - if (close_to_representation_extremum) { - // We can't have a normalization factor which also accounts for the precision, i.e. moves - // some decimal digits into the mantissa, since it's unrepresentable, or nearly - // unrepresentable. So, we'll give up early on getting extra precision... - return get_components(negative ? -scaled : scaled, precision); - } - components.integral = (int_fast64_t)scaled; - double remainder = non_normalized - unapply_scaling((double)components.integral, normalization); - double prec_power_of_10 = powers_of_10[precision]; - struct scaling_factor account_for_precision = - update_normalization(normalization, prec_power_of_10); - double scaled_remainder = apply_scaling(remainder, account_for_precision); - double rounding_threshold = 0.5; - - components.fractional = - (int_fast64_t)scaled_remainder; // when precision == 0, the assigned value should be 0 - scaled_remainder -= - (double)components.fractional; // when precision == 0, this will not change scaled_remainder - - components.fractional += (scaled_remainder >= rounding_threshold); - if (scaled_remainder == rounding_threshold) { - // banker's rounding: Round towards the even number (making the mean error 0) - components.fractional &= ~((int_fast64_t)0x1); - } - // handle rollover, e.g. the case of 0.99 with precision 1 becoming (0,100), - // and must then be corrected into (1, 0). - // Note: for precision = 0, this will "translate" the rounding effect from - // the fractional part to the integral part where it should actually be - // felt (as prec_power_of_10 is 1) - if ((double)components.fractional >= prec_power_of_10) { - components.fractional = 0; - ++components.integral; - } - return components; -} -#endif // PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - -static void print_broken_up_decimal(struct double_components number_, output_gadget_t *output, - printf_size_t precision, printf_size_t width, - printf_flags_t flags, char *buf, printf_size_t len) -{ - if (precision != 0U) { - // do fractional part, as an unsigned number - - printf_size_t count = precision; - - // %g/%G mandates we skip the trailing 0 digits... - if ((flags & FLAGS_ADAPT_EXP) && !(flags & FLAGS_HASH) && (number_.fractional > 0)) { - while (true) { - int_fast64_t digit = number_.fractional % 10U; - if (digit != 0) { - break; - } - --count; - number_.fractional /= 10U; - } - // ... and even the decimal point if there are no - // non-zero fractional part digits (see below) - } - - if (number_.fractional > 0 || !(flags & FLAGS_ADAPT_EXP) || (flags & FLAGS_HASH)) { - while (len < PRINTF_DECIMAL_BUFFER_SIZE) { - --count; - buf[len++] = (char)('0' + number_.fractional % 10U); - if (!(number_.fractional /= 10U)) { - break; - } - } - // add extra 0s - while ((len < PRINTF_DECIMAL_BUFFER_SIZE) && (count > 0U)) { - buf[len++] = '0'; - --count; - } - if (len < PRINTF_DECIMAL_BUFFER_SIZE) { - buf[len++] = '.'; - } - } - } else { - if ((flags & FLAGS_HASH) && (len < PRINTF_DECIMAL_BUFFER_SIZE)) { - buf[len++] = '.'; - } - } - - // Write the integer part of the number (it comes after the fractional - // since the character order is reversed) - while (len < PRINTF_DECIMAL_BUFFER_SIZE) { - buf[len++] = (char)('0' + (number_.integral % 10)); - if (!(number_.integral /= 10)) { - break; - } - } - - // pad leading zeros - if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) { - if (width && (number_.is_negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { - width--; - } - while ((len < width) && (len < PRINTF_DECIMAL_BUFFER_SIZE)) { - buf[len++] = '0'; - } - } - - if (len < PRINTF_DECIMAL_BUFFER_SIZE) { - if (number_.is_negative) { - buf[len++] = '-'; - } else if (flags & FLAGS_PLUS) { - buf[len++] = '+'; // ignore the space if the '+' exists - } else if (flags & FLAGS_SPACE) { - buf[len++] = ' '; - } - } - - out_rev_(output, buf, len, width, flags); -} - -// internal ftoa for fixed decimal floating point -static void print_decimal_number(output_gadget_t *output, double number, printf_size_t precision, - printf_size_t width, printf_flags_t flags, char *buf, - printf_size_t len) -{ - struct double_components value_ = get_components(number, precision); - print_broken_up_decimal(value_, output, precision, width, flags, buf, len); -} - -#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - -// A floor function - but one which only works for numbers whose -// floor value is representable by an int. -static int bastardized_floor(double x) -{ - if (x >= 0) { - return (int)x; - } - int n = (int)x; - return (((double)n) == x) ? n : n - 1; -} - -// Computes the base-10 logarithm of the input number - which must be an actual -// positive number (not infinity or NaN, nor a sub-normal) -static double log10_of_positive(double positive_number) -{ - // The implementation follows David Gay (https://www.ampl.com/netlib/fp/dtoa.c). - // - // Since log_10 ( M * 2^x ) = log_10(M) + x , we can separate the components of - // our input number, and need only solve log_10(M) for M between 1 and 2 (as - // the base-2 mantissa is always 1-point-something). In that limited range, a - // Taylor series expansion of log10(x) should serve us well enough; and we'll - // take the mid-point, 1.5, as the point of expansion. - - double_with_bit_access dwba = get_bit_access(positive_number); - // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c) - int exp2 = get_exp2(dwba); - // drop the exponent, so dwba.F comes into the range [1,2) - dwba.U = (dwba.U & (((double_uint_t)(1) << DOUBLE_STORED_MANTISSA_BITS) - 1U)) | - ((double_uint_t)DOUBLE_BASE_EXPONENT << DOUBLE_STORED_MANTISSA_BITS); - double z = (dwba.F - 1.5); - return ( - // Taylor expansion around 1.5: - 0.1760912590556812420 // Expansion term 0: ln(1.5) / ln(10) - + z * 0.2895296546021678851 // Expansion term 1: (M - 1.5) * 2/3 / ln(10) -#if PRINTF_LOG10_TAYLOR_TERMS > 2 - - z * z * 0.0965098848673892950 // Expansion term 2: (M - 1.5)^2 * 2/9 / ln(10) -#if PRINTF_LOG10_TAYLOR_TERMS > 3 - + z * z * z * 0.0428932821632841311 // Expansion term 2: (M - 1.5)^3 * 8/81 / ln(10) -#endif -#endif - // exact log_2 of the exponent x, with logarithm base change - + exp2 * 0.30102999566398119521 // = exp2 * log_10(2) = exp2 * ln(2)/ln(10) - ); -} - -static double pow10_of_int(int floored_exp10) -{ - // A crude hack for avoiding undesired behavior with barely-normal or slightly-subnormal values. - if (floored_exp10 == DOUBLE_MAX_SUBNORMAL_EXPONENT_OF_10) { - return DOUBLE_MAX_SUBNORMAL_POWER_OF_10; - } - // Compute 10^(floored_exp10) but (try to) make sure that doesn't overflow - double_with_bit_access dwba; - int exp2 = bastardized_floor(floored_exp10 * 3.321928094887362 + 0.5); - const double z = floored_exp10 * 2.302585092994046 - exp2 * 0.6931471805599453; - const double z2 = z * z; - dwba.U = ((double_uint_t)(exp2) + DOUBLE_BASE_EXPONENT) << DOUBLE_STORED_MANTISSA_BITS; - // compute exp(z) using continued fractions, - // see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex - dwba.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14))))); - return dwba.F; -} - -static void print_exponential_number(output_gadget_t *output, double number, - printf_size_t precision, printf_size_t width, - printf_flags_t flags, char *buf, printf_size_t len) -{ - const bool negative = get_sign_bit(number); - // This number will decrease gradually (by factors of 10) as we "extract" the exponent out of it - double abs_number = negative ? -number : number; - - int floored_exp10; - bool abs_exp10_covered_by_powers_table; - struct scaling_factor normalization; - - // Determine the decimal exponent - if (abs_number == 0.0) { - // TODO: This is a special-case for 0.0 (and -0.0); but proper handling is required for - // denormals more generally. - floored_exp10 = - 0; // ... and no need to set a normalization factor or check the powers table - } else { - double exp10 = log10_of_positive(abs_number); - floored_exp10 = bastardized_floor(exp10); - double p10 = pow10_of_int(floored_exp10); - // correct for rounding errors - if (abs_number < p10) { - floored_exp10--; - p10 /= 10; - } - abs_exp10_covered_by_powers_table = - PRINTF_ABS(floored_exp10) < PRINTF_MAX_PRECOMPUTED_POWER_OF_10; - normalization.raw_factor = - abs_exp10_covered_by_powers_table ? powers_of_10[PRINTF_ABS(floored_exp10)] : p10; - } - - // We now begin accounting for the widths of the two parts of our printed field: - // the decimal part after decimal exponent extraction, and the base-10 exponent part. - // For both of these, the value of 0 has a special meaning, but not the same one: - // a 0 exponent-part width means "don't print the exponent"; a 0 decimal-part width - // means "use as many characters as necessary". - - bool fall_back_to_decimal_only_mode = false; - if (flags & FLAGS_ADAPT_EXP) { - int required_significant_digits = (precision == 0) ? 1 : (int)precision; - // Should we want to fall-back to "%f" mode, and only print the decimal part? - fall_back_to_decimal_only_mode = - (floored_exp10 >= -4 && floored_exp10 < required_significant_digits); - // Now, let's adjust the precision - // This also decided how we adjust the precision value - as in "%g" mode, - // "precision" is the number of _significant digits_, and this is when we "translate" - // the precision value to an actual number of decimal digits. - int precision_ = - fall_back_to_decimal_only_mode - ? (int)precision - 1 - floored_exp10 - : (int)precision - 1; // the presence of the exponent ensures only one significant - // digit comes before the decimal point - precision = (precision_ > 0 ? (unsigned)precision_ : 0U); - flags |= FLAGS_PRECISION; // make sure print_broken_up_decimal respects our choice above - } - - normalization.multiply = (floored_exp10 < 0 && abs_exp10_covered_by_powers_table); - bool should_skip_normalization = (fall_back_to_decimal_only_mode || floored_exp10 == 0); - struct double_components decimal_part_components = - should_skip_normalization ? get_components(negative ? -abs_number : abs_number, precision) - : get_normalized_components(negative, precision, abs_number, - normalization, floored_exp10); - - // Account for roll-over, e.g. rounding from 9.99 to 100.0 - which effects - // the exponent and may require additional tweaking of the parts - if (fall_back_to_decimal_only_mode) { - if ((flags & FLAGS_ADAPT_EXP) && floored_exp10 >= -1 && - decimal_part_components.integral == powers_of_10[floored_exp10 + 1]) { - floored_exp10++; // Not strictly necessary, since floored_exp10 is no longer really used - precision--; - // ... and it should already be the case that decimal_part_components.fractional == 0 - } - // TODO: What about rollover strictly within the fractional part? - } else { - if (decimal_part_components.integral >= 10) { - floored_exp10++; - decimal_part_components.integral = 1; - decimal_part_components.fractional = 0; - } - } - - // the floored_exp10 format is "E%+03d" and largest possible floored_exp10 value for a 64-bit - // double is "307" (for 2^1023), so we set aside 4-5 characters overall - printf_size_t exp10_part_width = fall_back_to_decimal_only_mode ? 0U - : (PRINTF_ABS(floored_exp10) < 100) ? 4U - : 5U; - - printf_size_t decimal_part_width = - ((flags & FLAGS_LEFT) && exp10_part_width) - ? - // We're padding on the right, so the width constraint is the exponent part's - // problem, not the decimal part's, so we'll use as many characters as we need: - 0U - : - // We're padding on the left; so the width constraint is the decimal part's - // problem. Well, can both the decimal part and the exponent part fit within our overall - // width? - ((width > exp10_part_width) - ? - // Yes, so we limit our decimal part's width. - // (Note this is trivially valid even if we've fallen back to "%f" mode) - width - exp10_part_width - : - // No; we just give up on any restriction on the decimal part and use as many - // characters as we need - 0U); - - const printf_size_t printed_exponential_start_pos = output->pos; - print_broken_up_decimal(decimal_part_components, output, precision, decimal_part_width, flags, - buf, len); - - if (!fall_back_to_decimal_only_mode) { - putchar_via_gadget(output, (flags & FLAGS_UPPERCASE) ? 'E' : 'e'); - print_integer(output, ABS_FOR_PRINTING(floored_exp10), floored_exp10 < 0, 10, 0, - exp10_part_width - 1, FLAGS_ZEROPAD | FLAGS_PLUS); - if (flags & FLAGS_LEFT) { - // We need to right-pad with spaces to meet the width requirement - while (output->pos - printed_exponential_start_pos < width) { - putchar_via_gadget(output, ' '); - } - } - } -} -#endif // PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - -static void print_floating_point(output_gadget_t *output, double value, printf_size_t precision, - printf_size_t width, printf_flags_t flags, bool prefer_exponential) -{ - char buf[PRINTF_DECIMAL_BUFFER_SIZE]; - printf_size_t len = 0U; - - // test for special values - if (value != value) { - out_rev_(output, "nan", 3, width, flags); - return; - } - if (value < -DBL_MAX) { - out_rev_(output, "fni-", 4, width, flags); - return; - } - if (value > DBL_MAX) { - out_rev_(output, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, - width, flags); - return; - } - - if (!prefer_exponential && - ((value > PRINTF_FLOAT_NOTATION_THRESHOLD) || (value < -PRINTF_FLOAT_NOTATION_THRESHOLD))) { - // The required behavior of standard printf is to print _every_ integral-part digit -- which - // could mean printing hundreds of characters, overflowing any fixed internal buffer and - // necessitating a more complicated implementation. -#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - print_exponential_number(output, value, precision, width, flags, buf, len); -#endif - return; - } - - // set default precision, if not set explicitly - if (!(flags & FLAGS_PRECISION)) { - precision = PRINTF_DEFAULT_FLOAT_PRECISION; - } - - // limit precision so that our integer holding the fractional part does not overflow - while ((len < PRINTF_DECIMAL_BUFFER_SIZE) && (precision > PRINTF_MAX_SUPPORTED_PRECISION)) { - buf[len++] = '0'; // This respects the precision in terms of result length only - precision--; - } - -#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - if (prefer_exponential) - print_exponential_number(output, value, precision, width, flags, buf, len); - else -#endif - print_decimal_number(output, value, precision, width, flags, buf, len); -} - -#endif // (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS) - -// Advances the format pointer past the flags, and returns the parsed flags -// due to the characters passed -static printf_flags_t parse_flags(const char **format) -{ - printf_flags_t flags = 0U; - do { - switch (**format) { - case '0': - flags |= FLAGS_ZEROPAD; - (*format)++; - break; - case '-': - flags |= FLAGS_LEFT; - (*format)++; - break; - case '+': - flags |= FLAGS_PLUS; - (*format)++; - break; - case ' ': - flags |= FLAGS_SPACE; - (*format)++; - break; - case '#': - flags |= FLAGS_HASH; - (*format)++; - break; - default: - return flags; - } - } while (true); -} - -static inline void format_string_loop(output_gadget_t *output, const char *format, va_list args) -{ -#if PRINTF_CHECK_FOR_NUL_IN_FORMAT_SPECIFIER -#define ADVANCE_IN_FORMAT_STRING(cptr_) \ - do { \ - (cptr_)++; \ - if (!*(cptr_)) \ - return; \ - } while (0) -#else -#define ADVANCE_IN_FORMAT_STRING(cptr_) (cptr_)++ -#endif - - while (*format) { - if (*format != '%') { - // A regular content character - putchar_via_gadget(output, *format); - format++; - continue; - } - // We're parsing a format specifier: %[flags][width][.precision][length] - ADVANCE_IN_FORMAT_STRING(format); - - printf_flags_t flags = parse_flags(&format); - - // evaluate width field - printf_size_t width = 0U; - if (is_digit_(*format)) { - width = (printf_size_t)atou_(&format); - } else if (*format == '*') { - const int w = va_arg(args, int); - if (w < 0) { - flags |= FLAGS_LEFT; // reverse padding - width = (printf_size_t)-w; - } else { - width = (printf_size_t)w; - } - ADVANCE_IN_FORMAT_STRING(format); - } - - // evaluate precision field - printf_size_t precision = 0U; - if (*format == '.') { - flags |= FLAGS_PRECISION; - ADVANCE_IN_FORMAT_STRING(format); - if (is_digit_(*format)) { - precision = (printf_size_t)atou_(&format); - } else if (*format == '*') { - const int precision_ = va_arg(args, int); - precision = precision_ > 0 ? (printf_size_t)precision_ : 0U; - ADVANCE_IN_FORMAT_STRING(format); - } - } - - // evaluate length field - switch (*format) { -#ifdef PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS - case 'I': { - ADVANCE_IN_FORMAT_STRING(format); - // Greedily parse for size in bits: 8, 16, 32 or 64 - switch (*format) { - case '8': - flags |= FLAGS_INT8; - ADVANCE_IN_FORMAT_STRING(format); - break; - case '1': - ADVANCE_IN_FORMAT_STRING(format); - if (*format == '6') { - format++; - flags |= FLAGS_INT16; - } - break; - case '3': - ADVANCE_IN_FORMAT_STRING(format); - if (*format == '2') { - ADVANCE_IN_FORMAT_STRING(format); - flags |= FLAGS_INT32; - } - break; - case '6': - ADVANCE_IN_FORMAT_STRING(format); - if (*format == '4') { - ADVANCE_IN_FORMAT_STRING(format); - flags |= FLAGS_INT64; - } - break; - default: - break; - } - break; - } -#endif - case 'l': - flags |= FLAGS_LONG; - ADVANCE_IN_FORMAT_STRING(format); - if (*format == 'l') { - flags |= FLAGS_LONG_LONG; - ADVANCE_IN_FORMAT_STRING(format); - } - break; - case 'h': - flags |= FLAGS_SHORT; - ADVANCE_IN_FORMAT_STRING(format); - if (*format == 'h') { - flags |= FLAGS_CHAR; - ADVANCE_IN_FORMAT_STRING(format); - } - break; - case 't': - flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - ADVANCE_IN_FORMAT_STRING(format); - break; - case 'j': - flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - ADVANCE_IN_FORMAT_STRING(format); - break; - case 'z': - flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - ADVANCE_IN_FORMAT_STRING(format); - break; - default: - break; - } - - // evaluate specifier - switch (*format) { - case 'd': - case 'i': - case 'u': - case 'x': - case 'X': - case 'o': - case 'b': { - - if (*format == 'd' || *format == 'i') { - flags |= FLAGS_SIGNED; - } - - numeric_base_t base; - if (*format == 'x' || *format == 'X') { - base = BASE_HEX; - } else if (*format == 'o') { - base = BASE_OCTAL; - } else if (*format == 'b') { - base = BASE_BINARY; - } else { - base = BASE_DECIMAL; - flags &= ~FLAGS_HASH; // decimal integers have no alternative presentation - } - - if (*format == 'X') { - flags |= FLAGS_UPPERCASE; - } - - format++; - // ignore '0' flag when precision is given - if (flags & FLAGS_PRECISION) { - flags &= ~FLAGS_ZEROPAD; - } - - if (flags & FLAGS_SIGNED) { - // A signed specifier: d, i or possibly I + bit size if enabled - - if (flags & FLAGS_LONG_LONG) { -#if PRINTF_SUPPORT_LONG_LONG - const long long value = va_arg(args, long long); - print_integer(output, ABS_FOR_PRINTING(value), value < 0, base, precision, - width, flags); -#endif - } else if (flags & FLAGS_LONG) { - const long value = va_arg(args, long); - print_integer(output, ABS_FOR_PRINTING(value), value < 0, base, precision, - width, flags); - } else { - // We never try to interpret the argument as something potentially-smaller than - // int, due to integer promotion rules: Even if the user passed a short int, - // short unsigned etc. - these will come in after promotion, as int's (or - // unsigned for the case of short unsigned when it has the same size as int) - const int value = (flags & FLAGS_CHAR) ? (signed char)va_arg(args, int) - : (flags & FLAGS_SHORT) ? (short int)va_arg(args, int) - : va_arg(args, int); - print_integer(output, ABS_FOR_PRINTING(value), value < 0, base, precision, - width, flags); - } - } else { - // An unsigned specifier: u, x, X, o, b - - flags &= ~(FLAGS_PLUS | FLAGS_SPACE); - - if (flags & FLAGS_LONG_LONG) { -#if PRINTF_SUPPORT_LONG_LONG - print_integer(output, (printf_unsigned_value_t)va_arg(args, unsigned long long), - false, base, precision, width, flags); -#endif - } else if (flags & FLAGS_LONG) { - print_integer(output, (printf_unsigned_value_t)va_arg(args, unsigned long), - false, base, precision, width, flags); - } else { - const unsigned int value = - (flags & FLAGS_CHAR) ? (unsigned char)va_arg(args, unsigned int) - : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(args, unsigned int) - : va_arg(args, unsigned int); - print_integer(output, (printf_unsigned_value_t)value, false, base, precision, - width, flags); - } - } - break; - } -#if PRINTF_SUPPORT_DECIMAL_SPECIFIERS - case 'f': - case 'F': - if (*format == 'F') - flags |= FLAGS_UPPERCASE; - print_floating_point(output, va_arg(args, double), precision, width, flags, - PRINTF_PREFER_DECIMAL); - format++; - break; -#endif -#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - case 'e': - case 'E': - case 'g': - case 'G': - if ((*format == 'g') || (*format == 'G')) - flags |= FLAGS_ADAPT_EXP; - if ((*format == 'E') || (*format == 'G')) - flags |= FLAGS_UPPERCASE; - print_floating_point(output, va_arg(args, double), precision, width, flags, - PRINTF_PREFER_EXPONENTIAL); - format++; - break; -#endif // PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - case 'c': { - printf_size_t l = 1U; - // pre padding - if (!(flags & FLAGS_LEFT)) { - while (l++ < width) { - putchar_via_gadget(output, ' '); - } - } - // char output - putchar_via_gadget(output, (char)va_arg(args, int)); - // post padding - if (flags & FLAGS_LEFT) { - while (l++ < width) { - putchar_via_gadget(output, ' '); - } - } - format++; - break; - } - - case 's': { - const char *p = va_arg(args, char *); - if (p == NULL) { - out_rev_(output, ")llun(", 6, width, flags); - } else { - printf_size_t l = - strnlen_s_(p, precision ? precision : PRINTF_MAX_POSSIBLE_BUFFER_SIZE); - // pre padding - if (flags & FLAGS_PRECISION) { - l = (l < precision ? l : precision); - } - if (!(flags & FLAGS_LEFT)) { - while (l++ < width) { - putchar_via_gadget(output, ' '); - } - } - // string output - while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision)) { - putchar_via_gadget(output, *(p++)); - --precision; - } - // post padding - if (flags & FLAGS_LEFT) { - while (l++ < width) { - putchar_via_gadget(output, ' '); - } - } - } - format++; - break; - } - - case 'p': { - width = sizeof(void *) * 2U + 2; // 2 hex chars per byte + the "0x" prefix - flags |= FLAGS_ZEROPAD | FLAGS_POINTER; - uintptr_t value = (uintptr_t)va_arg(args, void *); - (value == (uintptr_t)NULL) ? out_rev_(output, ")lin(", 5, width, flags) - : print_integer(output, (printf_unsigned_value_t)value, - false, BASE_HEX, precision, width, flags); - format++; - break; - } - - case '%': - putchar_via_gadget(output, '%'); - format++; - break; - - // Many people prefer to disable support for %n, as it lets the caller - // engineer a write to an arbitrary location, of a value the caller - // effectively controls - which could be a security concern in some cases. -#if PRINTF_SUPPORT_WRITEBACK_SPECIFIER - case 'n': { - if (flags & FLAGS_CHAR) - *(va_arg(args, char *)) = (char)output->pos; - else if (flags & FLAGS_SHORT) - *(va_arg(args, short *)) = (short)output->pos; - else if (flags & FLAGS_LONG) - *(va_arg(args, long *)) = (long)output->pos; -#if PRINTF_SUPPORT_LONG_LONG - else if (flags & FLAGS_LONG_LONG) - *(va_arg(args, long long *)) = (long long int)output->pos; -#endif // PRINTF_SUPPORT_LONG_LONG - else - *(va_arg(args, int *)) = (int)output->pos; - format++; - break; - } -#endif // PRINTF_SUPPORT_WRITEBACK_SPECIFIER - - default: - putchar_via_gadget(output, *format); - format++; - break; - } - } -} - -// internal vsnprintf - used for implementing _all library functions -static int vsnprintf_impl(output_gadget_t *output, const char *format, va_list args) -{ - // Note: The library only calls vsnprintf_impl() with output->pos being 0. However, it is - // possible to call this function with a non-zero pos value for some "remedial printing". - format_string_loop(output, format, args); - - // termination - append_termination_with_gadget(output); - - // return written chars without terminating \0 - return (int)output->pos; -} - -/////////////////////////////////////////////////////////////////////////////// - -// int vprintf_(const char *format, va_list arg) -// { -// output_gadget_t gadget = extern_putchar_gadget(); -// return vsnprintf_impl(&gadget, format, arg); -// } - -int vsnprintf_(char *s, size_t n, const char *format, va_list arg) -{ - output_gadget_t gadget = buffer_gadget(s, n); - return vsnprintf_impl(&gadget, format, arg); -} - -int vsprintf_(char *s, const char *format, va_list arg) -{ - return vsnprintf_(s, PRINTF_MAX_POSSIBLE_BUFFER_SIZE, format, arg); -} - -int vfctprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, - va_list arg) -{ - output_gadget_t gadget = function_gadget(out, extra_arg); - return vsnprintf_impl(&gadget, format, arg); -} - -// int printf_(const char *format, ...) -// { -// va_list args; -// va_start(args, format); -// const int ret = vprintf_(format, args); -// va_end(args); -// return ret; -// } - -int sprintf_(char *s, const char *format, ...) -{ - va_list args; - va_start(args, format); - const int ret = vsprintf_(s, format, args); - va_end(args); - return ret; -} - -int snprintf_(char *s, size_t n, const char *format, ...) -{ - va_list args; - va_start(args, format); - const int ret = vsnprintf_(s, n, format, args); - va_end(args); - return ret; -} - -int fctprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, ...) -{ - va_list args; - va_start(args, format); - const int ret = vfctprintf(out, extra_arg, format, args); - va_end(args); - return ret; -} diff --git a/ulib/ruxlibc/c/printf.h b/ulib/ruxlibc/c/printf.h deleted file mode 100644 index 45a0750b0..000000000 --- a/ulib/ruxlibc/c/printf.h +++ /dev/null @@ -1,224 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -/** - * @author (c) Eyal Rozenberg - * 2021-2022, Haifa, Palestine/Israel - * @author (c) Marco Paland (info@paland.com) - * 2014-2019, PALANDesign Hannover, Germany - * - * @note Others have made smaller contributions to this file: see the - * contributors page at https://github.com/eyalroz/printf/graphs/contributors - * or ask one of the authors. - * - * @brief Small stand-alone implementation of the printf family of functions - * (`(v)printf`, `(v)s(n)printf` etc., geared towards use on embedded systems with - * a very limited resources. - * - * @note the implementations are thread-safe; re-entrant; use no functions from - * the standard library; and do not dynamically allocate any memory. - * - * @license The MIT License (MIT) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef PRINTF_H_ -#define PRINTF_H_ - -#ifdef __cplusplus -#include -#include -extern "C" { -#else -#include -#include -#endif - -#ifdef __GNUC__ -#if ((__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ > 4) -#define ATTR_PRINTF(one_based_format_index, first_arg) \ - __attribute__((format(gnu_printf, (one_based_format_index), (first_arg)))) -#else -#define ATTR_PRINTF(one_based_format_index, first_arg) \ - __attribute__((format(printf, (one_based_format_index), (first_arg)))) -#endif -#define ATTR_VPRINTF(one_based_format_index) ATTR_PRINTF((one_based_format_index), 0) -#else -#define ATTR_PRINTF(one_based_format_index, first_arg) -#define ATTR_VPRINTF(one_based_format_index) -#endif - -#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES -#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 0 -#endif - -#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD -#define printf_ printf -#define sprintf_ sprintf -#define vsprintf_ vsprintf -#define snprintf_ snprintf -#define vsnprintf_ vsnprintf -#define vprintf_ vprintf -#endif - -// If you want to include this implementation file directly rather than -// link against, this will let you control the functions' visibility, -// e.g. make them static so as not to clash with other objects also -// using them. -#ifndef PRINTF_VISIBILITY -#define PRINTF_VISIBILITY -#endif - -/** - * Prints/send a single character to some opaque output entity - * - * @note This function is not implemented by the library, only declared; you must provide an - * implementation if you wish to use the @ref printf / @ref vprintf function (and possibly - * for linking against the library, if your toolchain does not support discarding unused functions) - * - * @note The output could be as simple as a wrapper for the `write()` system call on a Unix-like - * system, or even libc's @ref putchar , for replicating actual functionality of libc's @ref printf - * function; but on an embedded system it may involve interaction with a special output device, - * like a UART, etc. - * - * @note in libc's @ref putchar, the parameter type is an int; this was intended to support the - * representation of either a proper character or EOF in a variable - but this is really not - * meaningful to pass into @ref putchar and is discouraged today. See further discussion in: - * @link https://stackoverflow.com/q/17452847/1593077 - * - * @param c the single character to print - */ -// PRINTF_VISIBILITY -// void putchar_(char c); - -/** - * An implementation of the C standard's printf/vprintf - * - * @note you must implement a @ref putchar_ function for using this function - it invokes @ref - * putchar_ rather than directly performing any I/O (which insulates it from any dependence on the - * operating system and external libraries). - * - * @param format A string specifying the format of the output, with %-marked specifiers of how to - * interpret additional arguments. - * @param arg Additional arguments to the function, one for each %-specifier in @p format string - * @return The number of characters written into @p s, not counting the terminating null character - */ -///@{ -// PRINTF_VISIBILITY -// int printf_(const char* format, ...) ATTR_PRINTF(1, 2); -// PRINTF_VISIBILITY -// int vprintf_(const char* format, va_list arg) ATTR_VPRINTF(1); -///@} - -/** - * An implementation of the C standard's sprintf/vsprintf - * - * @note For security considerations (the potential for exceeding the buffer bounds), please - * consider using the size-constrained variant, @ref snprintf / @ref vsnprintf , instead. - * - * @param s An array in which to store the formatted string. It must be large enough to fit the - * formatted output! - * @param format A string specifying the format of the output, with %-marked specifiers of how to - * interpret additional arguments. - * @param arg Additional arguments to the function, one for each specifier in @p format - * @return The number of characters written into @p s, not counting the terminating null character - */ -///@{ -PRINTF_VISIBILITY -int sprintf_(char *s, const char *format, ...) ATTR_PRINTF(2, 3); -PRINTF_VISIBILITY -int vsprintf_(char *s, const char *format, va_list arg) ATTR_VPRINTF(2); -///@} - -/** - * An implementation of the C standard's snprintf/vsnprintf - * - * @param s An array in which to store the formatted string. It must be large enough to fit either - * the entire formatted output, or at least @p n characters. Alternatively, it can be NULL, in which - * case nothing will be printed, and only the number of characters which _could_ have been printed - * is tallied and returned. - * @param n The maximum number of characters to write to the array, including a terminating null - * character - * @param format A string specifying the format of the output, with %-marked specifiers of how to - * interpret additional arguments. - * @param arg Additional arguments to the function, one for each specifier in @p format - * @return The number of characters that COULD have been written into @p s, not counting the - * terminating null character. A value equal or larger than @p n indicates truncation. Only when the - * returned value is non-negative and less than @p n, the null-terminated string has been fully and - * successfully printed. - */ -///@{ -PRINTF_VISIBILITY -int snprintf_(char *s, size_t count, const char *format, ...) ATTR_PRINTF(3, 4); -PRINTF_VISIBILITY -int vsnprintf_(char *s, size_t count, const char *format, va_list arg) ATTR_VPRINTF(3); -///@} - -/** - * printf/vprintf with user-specified output function - * - * An alternative to @ref printf_, in which the output function is specified dynamically - * (rather than @ref putchar_ being used) - * - * @param out An output function which takes one character and a type-erased additional parameters - * @param extra_arg The type-erased argument to pass to the output function @p out with each call - * @param format A string specifying the format of the output, with %-marked specifiers of how to - * interpret additional arguments. - * @param arg Additional arguments to the function, one for each specifier in @p format - * @return The number of characters for which the output f unction was invoked, not counting the - * terminating null character - * - */ -PRINTF_VISIBILITY -int fctprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, ...) - ATTR_PRINTF(3, 4); -PRINTF_VISIBILITY -int vfctprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, - va_list arg) ATTR_VPRINTF(3); - -#ifdef __cplusplus -} // extern "C" -#endif - -#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD -#undef printf_ -#undef sprintf_ -#undef vsprintf_ -#undef snprintf_ -#undef vsnprintf_ -#undef vprintf_ -#else -#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT -#define printf printf_ -#define sprintf sprintf_ -#define vsprintf vsprintf_ -#define snprintf snprintf_ -#define vsnprintf vsnprintf_ -#define vprintf vprintf_ -#endif -#endif - -#endif // PRINTF_H_ diff --git a/ulib/ruxlibc/c/printf_config.h b/ulib/ruxlibc/c/printf_config.h deleted file mode 100644 index 2086f9276..000000000 --- a/ulib/ruxlibc/c/printf_config.h +++ /dev/null @@ -1,23 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifndef PRINTF_CONFIG_H -#define PRINTF_CONFIG_H - -#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 1 - -#ifndef RUX_CONFIG_FP_SIMD - -#define PRINTF_SUPPORT_DECIMAL_SPECIFIERS 0 - -#define PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS 0 - -#endif // RUX_CONFIG_FP_SIMD - -#endif // PRINTF_CONFIG_H diff --git a/ulib/ruxlibc/c/pthread.c b/ulib/ruxlibc/c/pthread.c deleted file mode 100644 index 363f02d99..000000000 --- a/ulib/ruxlibc/c/pthread.c +++ /dev/null @@ -1,80 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_MULTITASK - -#include -#include -#include -#include -#include - -int pthread_setcancelstate(int new, int *old) -{ - unimplemented(); - return 0; -} - -int pthread_setcanceltype(int new, int *old) -{ - unimplemented(); - return 0; -} - -// TODO -void pthread_testcancel(void) -{ - unimplemented(); - return; -} - -// TODO -int pthread_cancel(pthread_t t) -{ - unimplemented(); - return 0; -} - -// TODO -int pthread_setname_np(pthread_t thread, const char *name) -{ - unimplemented(); - return 0; -} - -#define DEFAULT_STACK_SIZE 131072 -#define DEFAULT_GUARD_SIZE 8192 - -// TODO -int pthread_attr_init(pthread_attr_t *a) -{ - *a = (pthread_attr_t){0}; - // __acquire_ptc(); - a->_a_stacksize = DEFAULT_STACK_SIZE; - a->_a_guardsize = DEFAULT_GUARD_SIZE; - // __release_ptc(); - return 0; -} - -int pthread_attr_getstacksize(const pthread_attr_t *restrict a, size_t *restrict size) -{ - *size = a->_a_stacksize; - return 0; -} - -int pthread_attr_setstacksize(pthread_attr_t *a, size_t size) -{ - if (size - PTHREAD_STACK_MIN > SIZE_MAX / 4) - return EINVAL; - a->_a_stackaddr = 0; - a->_a_stacksize = size; - return 0; -} - -#endif // RUX_CONFIG_MULTITASK diff --git a/ulib/ruxlibc/c/pwd.c b/ulib/ruxlibc/c/pwd.c deleted file mode 100644 index 0b0df0e1a..000000000 --- a/ulib/ruxlibc/c/pwd.c +++ /dev/null @@ -1,52 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include -#include - -static struct passwd pw__ = { - .pw_name = RUX_DEFAULT_USER, - .pw_passwd = RUX_DEFAULT_PASS, - .pw_uid = RUX_DEFAULT_UID, - .pw_gid = RUX_DEFAULT_GID, - .pw_gecos = RUX_DEFAULT_USER, - .pw_dir = "/", - .pw_shell = "", -}; - -int getpwnam_r(const char *name, struct passwd *pw, char *buf, size_t size, struct passwd **res) -{ - unimplemented(); - return 0; -} - -int getpwuid_r(uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res) -{ - unimplemented(); - return 0; -} - -struct passwd *getpwnam(const char *name) -{ - struct passwd *pwd; - - if (name && !strcmp(name, pw__.pw_name)) - pwd = &pw__; - else { - pwd = NULL; - errno = ENOENT; - } - - return pwd; -} diff --git a/ulib/ruxlibc/c/resource.c b/ulib/ruxlibc/c/resource.c deleted file mode 100644 index a50b640e2..000000000 --- a/ulib/ruxlibc/c/resource.c +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -// TODO -int getrusage(int __who, struct rusage *__usage) -{ - unimplemented(); - return 0; -} - -int setpriority(int which, id_t who, int prio) -{ - unimplemented(); - return 0; -} - diff --git a/ulib/ruxlibc/c/sched.c b/ulib/ruxlibc/c/sched.c deleted file mode 100644 index ad24fa270..000000000 --- a/ulib/ruxlibc/c/sched.c +++ /dev/null @@ -1,24 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int sched_setaffinity(pid_t __pid, size_t __cpusetsize, const cpu_set_t *__cpuset) -{ - unimplemented(); - return 0; -} - -int sched_yield(void) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/select.c b/ulib/ruxlibc/c/select.c deleted file mode 100644 index 555b21739..000000000 --- a/ulib/ruxlibc/c/select.c +++ /dev/null @@ -1,26 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_SELECT - -#include -#include -#include -#include -#include - -int pselect(int n, fd_set *restrict rfds, fd_set *restrict wfds, fd_set *restrict efds, - const struct timespec *restrict ts, const sigset_t *restrict mask) -{ - struct timeval tv = {ts->tv_sec, ts->tv_nsec / 1000}; - select(n, rfds, wfds, efds, &tv); - return 0; -} - -#endif // RUX_CONFIG_SELECT diff --git a/ulib/ruxlibc/c/semaphore.c b/ulib/ruxlibc/c/semaphore.c deleted file mode 100644 index 970ea656a..000000000 --- a/ulib/ruxlibc/c/semaphore.c +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int sem_destroy(sem_t *sem) -{ - unimplemented(); - return 0; -} - -// TODO -int sem_init(sem_t *sem, int pshared, unsigned int value) -{ - unimplemented(); - return 0; -} - -// TODO -int sem_post(sem_t *sem) -{ - unimplemented(); - return 0; -} - -// TODO -int sem_wait(sem_t *sem) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/sendfile.c b/ulib/ruxlibc/c/sendfile.c deleted file mode 100644 index 1a7f1f2ed..000000000 --- a/ulib/ruxlibc/c/sendfile.c +++ /dev/null @@ -1,16 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count){ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/signal.c b/ulib/ruxlibc/c/signal.c deleted file mode 100644 index 6c94a5da5..000000000 --- a/ulib/ruxlibc/c/signal.c +++ /dev/null @@ -1,101 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include - -extern int sigaction_inner(int, const struct sigaction *, struct sigaction *); - -void (*signal(int signum, void (*handler)(int)))(int) -{ - struct sigaction old; - struct sigaction act = { - .sa_handler = handler, .sa_flags = SA_RESTART, /* BSD signal semantics */ - }; - - if (sigaction_inner(signum, &act, &old) < 0) - return SIG_ERR; - - return (old.sa_flags & SA_SIGINFO) ? NULL : old.sa_handler; -} - -int sigaction(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact) -{ - return sigaction_inner(sig, act, oact); -} - -// TODO -int kill(pid_t __pid, int __sig) -{ - unimplemented(); - return 0; -} - -int sigemptyset(sigset_t *set) -{ - set->__bits[0] = 0; - if (sizeof(long) == 4 || _NSIG > 65) - set->__bits[1] = 0; - if (sizeof(long) == 4 && _NSIG > 65) { - set->__bits[2] = 0; - set->__bits[3] = 0; - } - return 0; -} - -// TODO -int raise(int __sig) -{ - unimplemented(); - return 0; -} - -int sigaddset(sigset_t *set, int sig) -{ - unsigned s = sig - 1; - if (s >= _NSIG - 1 || sig - 32U < 3) { - errno = EINVAL; - return -1; - } - set->__bits[s / 8 / sizeof *set->__bits] |= 1UL << (s & (8 * sizeof *set->__bits - 1)); - return 0; -} - -// TODO -int pthread_sigmask(int __how, const sigset_t *restrict __newmask, sigset_t *restrict __oldmask) -{ - unimplemented(); - return 0; -} - -// TODO -int sigprocmask(int how, const sigset_t *__restrict set, sigset_t *__restrict oldset) -{ - unimplemented(); - return 0; -} - -// TODO -int sigsuspend(const sigset_t *mask) -{ - unimplemented(); - return 0; -} - -#ifdef RUX_CONFIG_MULTITASK -// TODO -int pthread_kill(pthread_t t, int sig) -{ - unimplemented(); - return 0; -} -#endif diff --git a/ulib/ruxlibc/c/socket.c b/ulib/ruxlibc/c/socket.c deleted file mode 100644 index 9b60da7d3..000000000 --- a/ulib/ruxlibc/c/socket.c +++ /dev/null @@ -1,94 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_NET - -#include -#include -#include -#include -#include -#include - -int accept4(int fd, struct sockaddr *restrict addr, socklen_t *restrict len, int flg) -{ - if (!flg) - return accept(fd, addr, len); - if (flg & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) { - errno = EINVAL; - return -1; - } - int ret = accept(fd, addr, len); - if (ret < 0) - return ret; - if (flg & SOCK_CLOEXEC) - fcntl(ret, F_SETFD, FD_CLOEXEC); - if (flg & SOCK_NONBLOCK) - fcntl(ret, F_SETFL, O_NONBLOCK); - return ret; -} - -int getsockopt(int fd, int level, int optname, void *restrict optval, socklen_t *restrict optlen) -{ - unimplemented(); - return -1; -} - -int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) -{ - unimplemented("fd: %d, level: %d, optname: %d, optval: %d, optlen: %d", fd, level, optname, - *(int *)optval, optlen); - return 0; -} - -// TODO: remove this function in future work -ssize_t ax_sendmsg(int fd, const struct msghdr *msg, int flags); - -ssize_t sendmsg(int fd, const struct msghdr *msg, int flags) -{ -#if LONG_MAX > INT_MAX - struct msghdr h; - /* Kernels before 2.6.38 set SCM_MAX_FD to 255, allocate enough - * space to support an SCM_RIGHTS ancillary message with 255 fds. - * Kernels since 2.6.38 set SCM_MAX_FD to 253. */ - struct cmsghdr chbuf[CMSG_SPACE(255*sizeof(int))/sizeof(struct cmsghdr)+1], *c; - if (msg) { - h = *msg; - h.__pad1 = h.__pad2 = 0; - msg = &h; - if (h.msg_controllen) { - if (h.msg_controllen > sizeof chbuf) { - errno = ENOMEM; - return -1; - } - memcpy(chbuf, h.msg_control, h.msg_controllen); - h.msg_control = chbuf; - for (c=CMSG_FIRSTHDR(&h); c; c=CMSG_NXTHDR(&h,c)) - c->__pad1 = 0; - } - } -#endif - return ax_sendmsg(fd, msg, flags); -} - -// TODO -ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) -{ - unimplemented(); - return 0; -} - -// TODO -int socketpair(int domain, int type, int protocol, int sv[2]) -{ - unimplemented(); - return 0; -} - -#endif // RUX_CONFIG_NET diff --git a/ulib/ruxlibc/c/stat.c b/ulib/ruxlibc/c/stat.c deleted file mode 100644 index 9b93f816b..000000000 --- a/ulib/ruxlibc/c/stat.c +++ /dev/null @@ -1,40 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -// TODO: -int fchmod(int fd, mode_t mode) -{ - unimplemented(); - return 0; -} - -// TODO -int chmod(const char *path, mode_t mode) -{ - unimplemented(); - return 0; -} - -// TODO -mode_t umask(mode_t mask) -{ - unimplemented("mask: %d", mask); - return 0; -} - -// TODO -int fstatat(int fd, const char *restrict path, struct stat *restrict st, int flag) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/statfs.c b/ulib/ruxlibc/c/statfs.c deleted file mode 100644 index 84d773818..000000000 --- a/ulib/ruxlibc/c/statfs.c +++ /dev/null @@ -1,18 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int statfs(const char *path, struct statfs *buf) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/stdio.c b/ulib/ruxlibc/c/stdio.c deleted file mode 100644 index 5122b9b85..000000000 --- a/ulib/ruxlibc/c/stdio.c +++ /dev/null @@ -1,469 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include "printf.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// LOCK used by `puts()` -#ifdef RUX_CONFIG_MULTITASK -#include -static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; -#endif - -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define MIN(a, b) ((a) < (b) ? (a) : (b)) - -FILE __stdin_FILE = {.fd = 0, .buffer_len = 0}; - -FILE __stdout_FILE = {.fd = 1, .buffer_len = 0}; - -FILE __stderr_FILE = {.fd = 2, .buffer_len = 0}; - -FILE *const stdin = &__stdin_FILE; -FILE *const stdout = &__stdout_FILE; -FILE *const stderr = &__stderr_FILE; - -// Returns: number of chars written, negative for failure -// Warn: buffer_len[f] will not be changed -static int __write_buffer(FILE *f) -{ - int r = 0; - if (f->buffer_len == 0) - return 0; - r = write(f->fd, f->buf, f->buffer_len); - return r; -} - -// Clear buffer_len[f] -static void __clear_buffer(FILE *f) -{ - f->buffer_len = 0; -} - -static int __fflush(FILE *f) -{ - int r = __write_buffer(f); - __clear_buffer(f); - return r >= 0 ? 0 : r; -} - -static int out(FILE *f, const char *s, size_t l) -{ - int ret = 0; - for (size_t i = 0; i < l; i++) { - char c = s[i]; - f->buf[f->buffer_len++] = c; - if (f->buffer_len == FILE_BUF_SIZE || c == '\n') { - int r = __write_buffer(f); - __clear_buffer(f); - if (r < 0) - return r; - if (r < f->buffer_len) - return ret + r; - ret += r; - } - } - return ret; -} - -int getchar(void) -{ - unimplemented(); - return 0; -} - -int fflush(FILE *f) -{ - return __fflush(f); -} - -static inline int do_putc(int c, FILE *f) -{ - char byte = c; - return out(f, &byte, 1); -} - -int fputc(int c, FILE *f) -{ - return do_putc(c, f); -} - -int putc(int c, FILE *f) -{ - return do_putc(c, f); -} - -int putchar(int c) -{ - return do_putc(c, stdout); -} - -int puts(const char *s) -{ -#ifdef RUX_CONFIG_MULTITASK - pthread_mutex_lock(&lock); -#endif - - int r = write(1, (const void *)s, strlen(s)); - char brk[1] = {'\n'}; - write(1, (const void *)brk, 1); - -#ifdef RUX_CONFIG_MULTITASK - pthread_mutex_unlock(&lock); -#endif - - return r; -} - -void perror(const char *msg) -{ - FILE *f = stderr; - char *errstr = strerror(errno); - - if (msg && *msg) { - out(f, msg, strlen(msg)); - out(f, ": ", 2); - } - out(f, errstr, strlen(errstr)); - out(f, "\n", 1); -} - -static void __out_wrapper(char c, void *arg) -{ - out(arg, &c, 1); -} - -int printf(const char *restrict fmt, ...) -{ - int ret; - va_list ap; - va_start(ap, fmt); - ret = vfprintf(stdout, fmt, ap); - va_end(ap); - return ret; -} - -int fprintf(FILE *restrict f, const char *restrict fmt, ...) -{ - int ret; - va_list ap; - va_start(ap, fmt); - ret = vfprintf(f, fmt, ap); - va_end(ap); - return ret; -} - -int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap) -{ - return vfctprintf(__out_wrapper, f, fmt, ap); -} - -// TODO -int sscanf(const char *restrict __s, const char *restrict __format, ...) -{ - unimplemented(); - return 0; -} - -#ifdef RUX_CONFIG_FS - -int __fmodeflags(const char *mode) -{ - int flags; - if (strchr(mode, '+')) - flags = O_RDWR; - else if (*mode == 'r') - flags = O_RDONLY; - else - flags = O_WRONLY; - if (strchr(mode, 'x')) - flags |= O_EXCL; - if (strchr(mode, 'e')) - flags |= O_CLOEXEC; - if (*mode != 'r') - flags |= O_CREAT; - if (*mode == 'w') - flags |= O_TRUNC; - if (*mode == 'a') - flags |= O_APPEND; - return flags; -} - -FILE *fopen(const char *filename, const char *mode) -{ - FILE *f; - int flags; - - if (!strchr("rwa", *mode)) { - errno = EINVAL; - return 0; - } - - f = (FILE *)malloc(sizeof(FILE)); - - flags = __fmodeflags(mode); - // TODO: currently mode is unused in ax_open - int fd = open(filename, flags, 0666); - if (fd < 0) - return NULL; - f->fd = fd; - - return f; -} - -char *fgets(char *restrict s, int n, FILE *restrict f) -{ - if (n == 0) - return NULL; - if (n == 1) { - *s = '\0'; - return s; - } - - int cnt = 0; - while (cnt < n - 1) { - char c; - if (read(f->fd, (void *)&c, 1) > 0) { - if (c != '\n') - s[cnt++] = c; - else{ - s[cnt++] = c; - break; - } - - } else - break; - } - if(cnt==0){ - return NULL; - } - s[cnt] = '\0'; - return s; -} - -size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f) -{ - size_t total = size * nmemb; - size_t read_len = 0; - size_t len = 0; - do { - len = read(f->fd, destv + read_len, total - read_len); - if (len < 0) - break; - read_len += len; - } while (len > 0); - return read_len == size * nmemb ? nmemb : read_len / size; -} - -size_t fwrite(const void *restrict src, size_t size, size_t nmemb, FILE *restrict f) -{ - size_t total = size * nmemb; - size_t write_len = 0; - size_t len = 0; - do { - len = write(f->fd, src + write_len, total - write_len); - if (len < 0) - break; - write_len += len; - } while (len > 0); - return write_len == size * nmemb ? nmemb : write_len / size; -} - -int fputs(const char *restrict s, FILE *restrict f) -{ - size_t l = strlen(s); - return (fwrite(s, 1, l, f) == l) - 1; -} - -int fclose(FILE *f) -{ - return close(f->fd); -} - -int fileno(FILE *f) -{ - return f->fd; -} - -int feof(FILE *f) -{ - unimplemented(); - return 0; -} - -// TODO -int fseek(FILE *__stream, long __off, int __whence) -{ - unimplemented(); - return 0; -} - -// TODO -off_t ftello(FILE *__stream) -{ - unimplemented(); - return 0; -} - -// TODO -char *tmpnam(char *buf) -{ - unimplemented(); - return 0; -} - -// TODO -void clearerr(FILE *f) -{ - unimplemented(); -} - -// TODO -int ferror(FILE *f) -{ - unimplemented(); - return 0; -} - - -FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *restrict f) -{ - int fl = __fmodeflags(mode); - FILE *f2; - - fflush(f); - - if (!filename) { - if (fl&O_CLOEXEC) - fcntl(f->fd, F_SETFD, FD_CLOEXEC); - fl &= ~(O_CREAT|O_EXCL|O_CLOEXEC); - if(fcntl(f->fd, F_SETFL, fl) < 0) - goto fail; - } else { - f2 = fopen(filename, mode); - if (!f2) goto fail; - if (f2->fd == f->fd) f2->fd = -1; /* avoid closing in fclose */ - else if (dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) goto fail2; - fclose(f2); - } - return f; - -fail2: - fclose(f2); -fail: - fclose(f); - return NULL; -} - -// TODO -int fscanf(FILE *restrict f, const char *restrict fmt, ...) -{ - unimplemented(); - return 0; -} - -// TODO -long ftell(FILE *f) -{ - unimplemented(); - return 0; -} - -int getc(FILE *f) -{ - unimplemented(); - return 0; -} - -int remove(const char *path) -{ - if(unlink(path) < 0) { - return rmdir(path); - } - return 0; -} - -// TODO -int setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size) -{ - unimplemented(); - return 0; -} - -// TODO -FILE *tmpfile(void) -{ - unimplemented(); - return NULL; -} - -int ungetc(int c, FILE *f) -{ - unimplemented(); - return 0; -} - -ssize_t getdelim(char **restrict s, size_t *restrict n, int delim, FILE *restrict f) -{ - unimplemented(); - return 0; -} - -ssize_t getline(char **restrict s, size_t *restrict n, FILE *restrict f) -{ - return getdelim(s, n, '\n', f); -} - -int __uflow(FILE *f) -{ - unimplemented(); - return 0; -} - -int getc_unlocked(FILE *f) -{ - unimplemented(); - return 0; -} - -FILE *fdopen(int fd, const char *mode) -{ - FILE *f; - if (!strchr("rwa", *mode)) { - errno = EINVAL; - return 0; - } - - if (!(f=malloc(sizeof *f))) return 0; - f->buffer_len = 0; - - /* Apply close-on-exec flag */ - if (strchr(mode, 'e')) fcntl(fd, F_SETFD, FD_CLOEXEC); - - /* Set append mode on fd if opened for append */ - if (*mode == 'a') { - int flags = fcntl(fd, F_GETFL); - if (!(flags & O_APPEND)) - fcntl(fd, F_SETFL, flags | O_APPEND); - } - f->fd = fd; - return f; -} - -#endif // RUX_CONFIG_FS diff --git a/ulib/ruxlibc/c/stdlib.c b/ulib/ruxlibc/c/stdlib.c deleted file mode 100644 index ea14d5772..000000000 --- a/ulib/ruxlibc/c/stdlib.c +++ /dev/null @@ -1,691 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -char *program_invocation_short_name = "dummy"; -char *program_invocation_name = "dummy"; - -#define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var)) - -void srandom(unsigned int s) -{ - srand(s); -} - -#ifdef RUX_CONFIG_ALLOC - -void *calloc(size_t m, size_t n) -{ - void *mem = malloc(m * n); - - return memset(mem, 0, n * m); -} - -void *realloc(void *memblock, size_t size) -{ - if (!memblock) - return malloc(size); - - size_t o_size = *(size_t *)(memblock - 8); - - void *mem = malloc(size); - - for (int i = 0; i < (o_size < size ? o_size : size); i++) - ((char *)mem)[i] = ((char *)memblock)[i]; - - free(memblock); - return mem; -} - -#endif // RUX_CONFIG_ALLOC - -long long llabs(long long a) -{ - return a > 0 ? a : -a; -} - -int abs(int a) -{ - return a > 0 ? a : -a; -} - -long long atoll(const char *s) -{ - long long n = 0; - int neg = 0; - while (isspace(*s)) s++; - switch (*s) { - case '-': - neg = 1; - case '+': - s++; - } - /* Compute n as a negative number to avoid overflow on LLONG_MIN */ - while (isdigit(*s)) n = 10 * n - (*s++ - '0'); - return neg ? n : -n; -} - -long strtol(const char *restrict nptr, char **restrict endptr, int base) -{ - const char *s; - unsigned long acc; - unsigned char c; - unsigned long qbase, cutoff; - int neg, any, cutlim; - - s = nptr; - if (base < 0 || base == 1 || base > 36) { - errno = EINVAL; - any = 0; - acc = 0; - goto exit; - } - - do { - c = *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - qbase = (unsigned int)base; - cutoff = neg ? (unsigned long)LONG_MAX - (unsigned long)(LONG_MIN + LONG_MAX) : LONG_MAX; - cutlim = cutoff % qbase; - cutoff /= qbase; - for (acc = 0, any = 0;; c = *s++) { - if (!isascii(c)) - break; - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) - any = -1; - else { - any = 1; - acc *= qbase; - acc += c; - } - } - - if (any < 0) { - acc = neg ? LONG_MIN : LONG_MAX; - errno = ERANGE; - } else if (neg) - acc = -acc; - -exit: - if (endptr != 0) - *endptr = __DECONST(char *, any ? s - 1 : nptr); - return acc; -} - -unsigned long strtoul(const char *nptr, char **endptr, int base) -{ - const char *s = nptr; - unsigned long acc; - unsigned char c; - unsigned long cutoff; - int neg = 0, any, cutlim; - - if (base < 0 || base == 1 || base > 36) { - errno = EINVAL; - any = 0; - acc = 0; - goto exit; - } - - do { - c = *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else if (c == '+') - c = *s++; - if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - cutoff = (unsigned long)ULONG_MAX / (unsigned long)base; - cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; - - for (acc = 0, any = 0;; c = *s++) { - if (!isascii(c)) - break; - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) - any = -1; - else { - any = 1; - acc *= base; - acc += c; - } - } - if (any < 0) { - acc = ULONG_MAX; - errno = ERANGE; - } else if (neg) - acc = -acc; -exit: - if (endptr != 0) - *endptr = __DECONST(char *, any ? s - 1 : nptr); - return acc; -} - -long long strtoll(const char *nptr, char **endptr, int base) -{ - const char *s; - unsigned long long acc; - unsigned char c; - unsigned long long qbase, cutoff; - int neg, any, cutlim; - - s = nptr; - if (base < 0 || base == 1 || base > 36) { - errno = EINVAL; - any = 0; - acc = 0; - goto exit; - } - - do { - c = *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - qbase = (unsigned int)base; - cutoff = neg ? (unsigned long long)LLONG_MAX - (unsigned long long)(LLONG_MIN + LLONG_MAX) - : LLONG_MAX; - cutlim = cutoff % qbase; - cutoff /= qbase; - for (acc = 0, any = 0;; c = *s++) { - if (!isascii(c)) - break; - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) - any = -1; - else { - any = 1; - acc *= qbase; - acc += c; - } - } - - if (any < 0) { - errno = ERANGE; - acc = neg ? LLONG_MIN : LLONG_MAX; - } else if (neg) - acc = -acc; - -exit: - if (endptr != 0) - *endptr = __DECONST(char *, any ? s - 1 : nptr); - return acc; -} - -unsigned long long strtoull(const char *nptr, char **endptr, int base) -{ - const char *s = nptr; - unsigned long long acc; - unsigned char c; - unsigned long long qbase, cutoff; - int neg, any, cutlim; - - if (base < 0 || base == 1 || base > 36) { - errno = EINVAL; - any = 0; - acc = 0; - goto exit; - } - - do { - c = *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - qbase = (unsigned int)base; - cutoff = (unsigned long long)ULLONG_MAX / qbase; - cutlim = (unsigned long long)ULLONG_MAX % qbase; - for (acc = 0, any = 0;; c = *s++) { - if (!isascii(c)) - break; - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) - any = -1; - else { - any = 1; - acc *= qbase; - acc += c; - } - } - if (any < 0) { - errno = ERANGE; - acc = ULLONG_MAX; - } else if (neg) - acc = -acc; - -exit: - if (endptr != 0) - *endptr = __DECONST(char *, any ? s - 1 : nptr); - return acc; -} - -#ifdef RUX_CONFIG_FP_SIMD - -// TODO: precision may not be enough -long double strtold(const char *restrict s, char **restrict p) -{ - return (long double)strtod(s, p); -} - -#endif // RUX_CONFIG_FP_SIMD - -typedef int (*cmpfun_)(const void *, const void *); - -static int wrapper_cmp(const void *v1, const void *v2, void *cmp) -{ - return ((cmpfun_)cmp)(v1, v2); -} - -typedef int (*cmpfun)(const void *, const void *, void *); - -static inline int a_ctz_32(uint32_t x) -{ - static const char debruijn32[32] = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, - 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, - 7, 12, 21, 17, 9, 6, 16, 5, 15, 14}; - return debruijn32[(x & -x) * 0x076be629 >> 27]; -} - -static inline int a_ctz_64(uint64_t x) -{ - static const char debruijn64[64] = { - 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, 62, 5, 39, 46, 44, 42, - 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, - 23, 58, 17, 10, 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12}; - if (sizeof(long) < 8) { - uint32_t y = x; - if (!y) { - y = x >> 32; - return 32 + a_ctz_32(y); - } - return a_ctz_32(y); - } - return debruijn64[(x & -x) * 0x022fdd63cc95386dull >> 58]; -} - -static inline int a_ctz_l(unsigned long x) -{ - return (sizeof(long) < 8) ? a_ctz_32(x) : a_ctz_64(x); -} - -#define ntz(x) a_ctz_l((x)) - -static inline int pntz(size_t p[2]) -{ - int r = ntz(p[0] - 1); - if (r != 0 || (r = 8 * sizeof(size_t) + ntz(p[1])) != 8 * sizeof(size_t)) { - return r; - } - return 0; -} - -static void cycle(size_t width, unsigned char *ar[], int n) -{ - unsigned char tmp[256]; - size_t l; - int i; - - if (n < 2) { - return; - } - - ar[n] = tmp; - while (width) { - l = sizeof(tmp) < width ? sizeof(tmp) : width; - memcpy(ar[n], ar[0], l); - for (i = 0; i < n; i++) { - memcpy(ar[i], ar[i + 1], l); - ar[i] += l; - } - width -= l; - } -} - -/* shl() and shr() need n > 0 */ -static inline void shl(size_t p[2], int n) -{ - if (n >= 8 * sizeof(size_t)) { - n -= 8 * sizeof(size_t); - p[1] = p[0]; - p[0] = 0; - } - p[1] <<= n; - p[1] |= p[0] >> (sizeof(size_t) * 8 - n); - p[0] <<= n; -} - -static inline void shr(size_t p[2], int n) -{ - if (n >= 8 * sizeof(size_t)) { - n -= 8 * sizeof(size_t); - p[0] = p[1]; - p[1] = 0; - } - p[0] >>= n; - p[0] |= p[1] << (sizeof(size_t) * 8 - n); - p[1] >>= n; -} - -static void sift(unsigned char *head, size_t width, cmpfun cmp, void *arg, int pshift, size_t lp[]) -{ - unsigned char *rt, *lf; - unsigned char *ar[14 * sizeof(size_t) + 1]; - int i = 1; - - ar[0] = head; - while (pshift > 1) { - rt = head - width; - lf = head - width - lp[pshift - 2]; - - if (cmp(ar[0], lf, arg) >= 0 && cmp(ar[0], rt, arg) >= 0) { - break; - } - if (cmp(lf, rt, arg) >= 0) { - ar[i++] = lf; - head = lf; - pshift -= 1; - } else { - ar[i++] = rt; - head = rt; - pshift -= 2; - } - } - cycle(width, ar, i); -} - -static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, size_t pp[2], - int pshift, int trusty, size_t lp[]) -{ - unsigned char *stepson, *rt, *lf; - size_t p[2]; - unsigned char *ar[14 * sizeof(size_t) + 1]; - int i = 1; - int trail; - - p[0] = pp[0]; - p[1] = pp[1]; - - ar[0] = head; - while (p[0] != 1 || p[1] != 0) { - stepson = head - lp[pshift]; - if (cmp(stepson, ar[0], arg) <= 0) { - break; - } - if (!trusty && pshift > 1) { - rt = head - width; - lf = head - width - lp[pshift - 2]; - if (cmp(rt, stepson, arg) >= 0 || cmp(lf, stepson, arg) >= 0) { - break; - } - } - - ar[i++] = stepson; - head = stepson; - trail = pntz(p); - shr(p, trail); - pshift += trail; - trusty = 0; - } - if (!trusty) { - cycle(width, ar, i); - sift(head, width, cmp, arg, pshift, lp); - } -} - -void __qsort_r(void *base, size_t nel, size_t width, cmpfun cmp, void *arg) -{ - size_t lp[12 * sizeof(size_t)]; - size_t i, size = width * nel; - unsigned char *head, *high; - size_t p[2] = {1, 0}; - int pshift = 1; - int trail; - - if (!size) - return; - - head = base; - high = head + size - width; - - /* Precompute Leonardo numbers, scaled by element width */ - for (lp[0] = lp[1] = width, i = 2; (lp[i] = lp[i - 2] + lp[i - 1] + width) < size; i++) - ; - - while (head < high) { - if ((p[0] & 3) == 3) { - sift(head, width, cmp, arg, pshift, lp); - shr(p, 2); - pshift += 2; - } else { - if (lp[pshift - 1] >= high - head) { - trinkle(head, width, cmp, arg, p, pshift, 0, lp); - } else { - sift(head, width, cmp, arg, pshift, lp); - } - - if (pshift == 1) { - shl(p, 1); - pshift = 0; - } else { - shl(p, pshift - 1); - pshift = 1; - } - } - - p[0] |= 1; - head += width; - } - - trinkle(head, width, cmp, arg, p, pshift, 0, lp); - - while (pshift != 1 || p[0] != 1 || p[1] != 0) { - if (pshift <= 1) { - trail = pntz(p); - shr(p, trail); - pshift += trail; - } else { - shl(p, 2); - pshift -= 2; - p[0] ^= 7; - shr(p, 1); - trinkle(head - lp[pshift] - width, width, cmp, arg, p, pshift + 1, 1, lp); - shl(p, 1); - p[0] |= 1; - trinkle(head - width, width, cmp, arg, p, pshift, 1, lp); - } - head -= width; - } -} - -void qsort(void *base, size_t nel, size_t width, cmpfun_ cmp) -{ - __qsort_r(base, nel, width, wrapper_cmp, (void *)cmp); -} - -// TODO -int mkstemp(char *__template) -{ - unimplemented(); - return 0; -} - -// TODO -int mkostemp(char *__template, int __flags) -{ - unimplemented(); - return 0; -} - -// TODO -int system(const char *cmd) -{ - unimplemented(); - return 0; -} - -// TODO -char *realpath(const char *restrict path, char *restrict resolved_path) -{ - unimplemented(); - return 0; -} - -struct chunk { - size_t psize, csize; - struct chunk *next, *prev; -}; - -void __bin_chunk(struct chunk *) -{ - unimplemented(); - return; -} - -void *aligned_alloc(size_t align, size_t len) -{ - unsigned char *mem, *new; - - if ((align & -align) != align) { - errno = EINVAL; - return 0; - } - - if (len > SIZE_MAX - align) { - errno = ENOMEM; - return 0; - } - - if (align <= SIZE_ALIGN) - return malloc(len); - - if (!(mem = malloc(len + align - 1))) - return 0; - - new = (void *)((uintptr_t)mem + align - 1 & -align); - if (new == mem) - return mem; - - struct chunk *c = MEM_TO_CHUNK(mem); - struct chunk *n = MEM_TO_CHUNK(new); - - if (IS_MMAPPED(c)) { - n->psize = c->psize + (new - mem); - n->csize = c->csize - (new - mem); - return new; - } - - struct chunk *t = NEXT_CHUNK(c); - - n->psize = c->csize = C_INUSE | (new - mem); - n->csize = t->psize -= new - mem; - - __bin_chunk(c); - return new; -} - -// TODO -int posix_memalign(void **res, size_t align, size_t len) -{ - if (align < sizeof(void *)) - return EINVAL; - void *mem = aligned_alloc(align, len); - if (!mem) - return errno; - *res = mem; - return 0; -} diff --git a/ulib/ruxlibc/c/string.c b/ulib/ruxlibc/c/string.c deleted file mode 100644 index d712cc90c..000000000 --- a/ulib/ruxlibc/c/string.c +++ /dev/null @@ -1,473 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include -#include - -int atoi(const char *s) -{ - int n = 0, neg = 0; - while (isspace(*s)) s++; - switch (*s) { - case '-': - neg = 1; - case '+': - s++; - } - /* Compute n as a negative number to avoid overflow on INT_MIN */ - while (isdigit(*s)) n = 10 * n - (*s++ - '0'); - return neg ? n : -n; -} - -void *memchr(const void *src, int c, size_t n) -{ - const unsigned char *s = src; - c = (unsigned char)c; - for (; n && *s != c; s++, n--) - ; - return n ? (void *)s : 0; -} - -void *memset(void *dest, int c, size_t n) -{ - unsigned char *s = dest; - size_t k; - - /* Fill head and tail with minimal branching. Each - * conditional ensures that all the subsequently used - * offsets are well-defined and in the dest region. */ - - if (!n) - return dest; - s[0] = c; - s[n - 1] = c; - if (n <= 2) - return dest; - s[1] = c; - s[2] = c; - s[n - 2] = c; - s[n - 3] = c; - if (n <= 6) - return dest; - s[3] = c; - s[n - 4] = c; - if (n <= 8) - return dest; - - /* Advance pointer to align it at a 4-byte boundary, - * and truncate n to a multiple of 4. The previous code - * already took care of any head/tail that get cut off - * by the alignment. */ - - k = -(uintptr_t)s & 3; - s += k; - n -= k; - n &= -4; - - /* Pure C fallback with no aliasing violations. */ - for (; n; n--, s++) *s = c; - - return dest; -} - -char *strcpy(char *restrict d, const char *restrict s) -{ - for (; (*d = *s); s++, d++) - ; - return d; -} - -char *strncpy(char *restrict d, const char *restrict s, size_t n) -{ - for (; n && (*d = *s); n--, s++, d++) - ; - return d; -} - -char *strcat(char *restrict d, const char *restrict s) -{ - strcpy(d + strlen(d), s); - return d; -} - -char *strncat(char *restrict d, const char *restrict s, size_t n) -{ - char *a = d; - d += strlen(d); - while (n && *s) n--, *d++ = *s++; - *d++ = 0; - return a; -} - -int strcmp(const char *l, const char *r) -{ - for (; *l == *r && *l; l++, r++) - ; - return *(unsigned char *)l - *(unsigned char *)r; -} - -int strncmp(const char *_l, const char *_r, size_t n) -{ - const unsigned char *l = (void *)_l, *r = (void *)_r; - if (!n--) - return 0; - for (; *l && *r && n && *l == *r; l++, r++, n--) - ; - return *l - *r; -} - -int strcoll(const char *l, const char *r) -{ - return strcmp(l, r); -} - -#define BITOP(a, b, op) \ - ((a)[(size_t)(b) / (8 * sizeof *(a))] op(size_t) 1 << ((size_t)(b) % (8 * sizeof *(a)))) -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define MIN(a, b) ((a) < (b) ? (a) : (b)) - -size_t strcspn(const char *s1, const char *s2) -{ - const char *a = s1; - size_t byteset[32 / sizeof(size_t)]; - - if (!s2[0] || !s2[1]) { - for (; *s1 != *s2; s1++) return s1 - a; - } - memset(byteset, 0, sizeof byteset); - - for (; *s2 != '\0'; s2++) BITOP(byteset, *(unsigned char *)s2, |=); - for (; *s1 && !(BITOP(byteset, *(unsigned char *)s1, &)); s1++) - ; - - return s1 - a; -} - -size_t strspn(const char *s, const char *c) -{ - const char *a = s; - size_t byteset[32 / sizeof(size_t)] = {0}; - - if (!c[0]) - return 0; - if (!c[1]) { - for (; *s == *c; s++) - ; - return s - a; - } - - for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++) - ; - for (; *s && BITOP(byteset, *(unsigned char *)s, &); s++) - ; - return s - a; -} - -char *strpbrk(const char *s, const char *b) -{ - s += strcspn(s, b); - return *s ? (char *)s : 0; -} - -char *strchrnul(const char *s, int c) -{ - c = (unsigned char)c; - if (!c) - return (char *)s + strlen(s); - - for (; *s && *(unsigned char *)s != c; s++) - ; - return (char *)s; -} - -char *strchr(const char *s, int c) -{ - while (*s != c && *s != '\0') s++; - - if (*s == c) { - return (char *)s; - } else { - return NULL; - } -} - -char *strrchr(const char *s, int c) -{ - char *isCharFind = NULL; - if (s != NULL) { - do { - if (*s == (char)c) { - isCharFind = (char *)s; - } - } while (*s++); - } - return isCharFind; -} - -int strerror_r(int err, char *buf, size_t buflen) -{ - char *msg = strerror(err); - size_t l = strlen(msg); - if (l >= buflen) { - if (buflen) { - memcpy(buf, msg, buflen - 1); - buf[buflen - 1] = 0; - } - return ERANGE; - } - memcpy(buf, msg, l + 1); - return 0; -} - -void *memcpy(void *restrict dest, const void *restrict src, size_t n) -{ - unsigned char *d = dest; - const unsigned char *s = src; - for (; n; n--) *d++ = *s++; - return dest; -} - -void *memmove(void *dest, const void *src, size_t n) -{ - char *d = dest; - const char *s = src; - - if (d == s) - return d; - if ((uintptr_t)s - (uintptr_t)d - n <= -2 * n) - return memcpy(d, s, n); - - if (d < s) { - for (; n; n--) *d++ = *s++; - } else { - while (n) n--, d[n] = s[n]; - } - - return dest; -} - -int memcmp(const void *vl, const void *vr, size_t n) -{ - const unsigned char *l = vl, *r = vr; - for (; n && *l == *r; n--, l++, r++) - ; - return n ? *l - *r : 0; -} - -int strcasecmp(const char *_l, const char *_r) -{ - const unsigned char *l = (void *)_l, *r = (void *)_r; - for (; *l && *r && (*l == *r || tolower(*l) == tolower(*r)); l++, r++) - ; - return tolower(*l) - tolower(*r); -} - -int strncasecmp(const char *_l, const char *_r, size_t n) -{ - const unsigned char *l = (void *)_l, *r = (void *)_r; - if (!n--) - return 0; - for (; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r)); l++, r++, n--) - ; - return tolower(*l) - tolower(*r); -} - -// `strstr` helper function -static char *twobyte_strstr(const unsigned char *h, const unsigned char *n) -{ - uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1]; - for (h++; *h && hw != nw; hw = hw << 8 | *++h) - ; - return *h ? (char *)h - 1 : 0; -} - -// `strstr` helper function -static char *threebyte_strstr(const unsigned char *h, const unsigned char *n) -{ - uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8; - uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8; - for (h += 2; *h && hw != nw; hw = (hw | *++h) << 8) - ; - return *h ? (char *)h - 2 : 0; -} - -// `strstr` helper function -static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) -{ - uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3]; - uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3]; - for (h += 3; *h && hw != nw; hw = hw << 8 | *++h) - ; - return *h ? (char *)h - 3 : 0; -} - -// `strstr` helper function -static char *twoway_strstr(const unsigned char *h, const unsigned char *n) -{ - const unsigned char *z; - size_t l, ip, jp, k, p, ms, p0, mem, mem0; - size_t byteset[32 / sizeof(size_t)] = {0}; - size_t shift[256]; - - /* Computing length of needle and fill shift table */ - for (l = 0; n[l] && h[l]; l++) BITOP(byteset, n[l], |=), shift[n[l]] = l + 1; - if (n[l]) - return 0; /* hit the end of h */ - - /* Compute maximal suffix */ - ip = -1; - jp = 0; - k = p = 1; - while (jp + k < l) { - if (n[ip + k] == n[jp + k]) { - if (k == p) { - jp += p; - k = 1; - } else - k++; - } else if (n[ip + k] > n[jp + k]) { - jp += k; - k = 1; - p = jp - ip; - } else { - ip = jp++; - k = p = 1; - } - } - ms = ip; - p0 = p; - - /* And with the opposite comparison */ - ip = -1; - jp = 0; - k = p = 1; - while (jp + k < l) { - if (n[ip + k] == n[jp + k]) { - if (k == p) { - jp += p; - k = 1; - } else - k++; - } else if (n[ip + k] < n[jp + k]) { - jp += k; - k = 1; - p = jp - ip; - } else { - ip = jp++; - k = p = 1; - } - } - if (ip + 1 > ms + 1) - ms = ip; - else - p = p0; - - /* Periodic needle? */ - if (memcmp(n, n + p, ms + 1)) { - mem0 = 0; - p = MAX(ms, l - ms - 1) + 1; - } else - mem0 = l - p; - mem = 0; - - /* Initialize incremental end-of-haystack pointer */ - z = h; - - /* Search loop */ - for (;;) { - /* Update incremental end-of-haystack pointer */ - if (z - h < l) { - /* Fast estimate for MAX(l,63) */ - size_t grow = l | 63; - const unsigned char *z2 = memchr(z, 0, grow); - if (z2) { - z = z2; - if (z - h < l) - return 0; - } else - z += grow; - } - - /* Check last byte first; advance by shift on mismatch */ - if (BITOP(byteset, h[l - 1], &)) { - k = l - shift[h[l - 1]]; - if (k) { - if (k < mem) - k = mem; - h += k; - mem = 0; - continue; - } - } else { - h += l; - mem = 0; - continue; - } - - /* Compare right half */ - for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++) - ; - if (n[k]) { - h += k - ms; - mem = 0; - continue; - } - /* Compare left half */ - for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--) - ; - if (k <= mem) - return (char *)h; - h += p; - mem = mem0; - } -} - -char *strstr(const char *h, const char *n) -{ - /* Return immediately on empty needle */ - if (!n[0]) - return (char *)h; - - /* Use faster algorithms for short needles */ - h = strchr(h, *n); - if (!h || !n[1]) - return (char *)h; - if (!h[1]) - return 0; - if (!n[2]) - return twobyte_strstr((void *)h, (void *)n); - if (!h[2]) - return 0; - if (!n[3]) - return threebyte_strstr((void *)h, (void *)n); - if (!h[3]) - return 0; - if (!n[4]) - return fourbyte_strstr((void *)h, (void *)n); - - return twoway_strstr((void *)h, (void *)n); -} - -#ifdef RUX_CONFIG_ALLOC - -#include -char *strdup(const char *s) -{ - size_t l = strlen(s); - char *d = malloc(l + 1); - if (!d) - return NULL; - return memcpy(d, s, l + 1); -} - -#endif // RUX_CONFIG_ALLOC diff --git a/ulib/ruxlibc/c/syslog.c b/ulib/ruxlibc/c/syslog.c deleted file mode 100644 index d47afadeb..000000000 --- a/ulib/ruxlibc/c/syslog.c +++ /dev/null @@ -1,25 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -void syslog(int __pri, const char *__fmt, ...) -{ - unimplemented(); - return; -} - -// TODO -void openlog(const char *__ident, int __option, int __facility) -{ - unimplemented(); - return; -} diff --git a/ulib/ruxlibc/c/time.c b/ulib/ruxlibc/c/time.c deleted file mode 100644 index b3e05a13f..000000000 --- a/ulib/ruxlibc/c/time.c +++ /dev/null @@ -1,216 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include -#include - -long timezone = 0; -const char __utc[] = "UTC"; - -const int SEC_PER_MIN = 60; -const int SEC_PER_HOUR = 3600; -const int MIN_PER_HOUR = 60; -const int HOUR_PER_DAY = 24; - -/* 2000-03-01 (mod 400 year, immediately after feb29 */ -#define LEAPOCH (946684800LL + 86400 * (31 + 29)) -#define DAYS_PER_400Y (365 * 400 + 97) -#define DAYS_PER_100Y (365 * 100 + 24) -#define DAYS_PER_4Y (365 * 4 + 1) - -int __secs_to_tm(long long t, struct tm *tm) -{ - long long days, secs, years; - int remdays, remsecs, remyears; - int qc_cycles, c_cycles, q_cycles; - int months; - int wday, yday, leap; - static const char days_in_month[] = {31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29}; - - /* Reject time_t values whose year would overflow int */ - if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL) - return -1; - - secs = t - LEAPOCH; - days = secs / 86400; - remsecs = secs % 86400; - if (remsecs < 0) { - remsecs += 86400; - days--; - } - - wday = (3 + days) % 7; - if (wday < 0) - wday += 7; - - qc_cycles = days / DAYS_PER_400Y; - remdays = days % DAYS_PER_400Y; - if (remdays < 0) { - remdays += DAYS_PER_400Y; - qc_cycles--; - } - - c_cycles = remdays / DAYS_PER_100Y; - if (c_cycles == 4) - c_cycles--; - remdays -= c_cycles * DAYS_PER_100Y; - - q_cycles = remdays / DAYS_PER_4Y; - if (q_cycles == 25) - q_cycles--; - remdays -= q_cycles * DAYS_PER_4Y; - - remyears = remdays / 365; - if (remyears == 4) - remyears--; - remdays -= remyears * 365; - - leap = !remyears && (q_cycles || !c_cycles); - yday = remdays + 31 + 28 + leap; - if (yday >= 365 + leap) - yday -= 365 + leap; - - years = remyears + 4 * q_cycles + 100 * c_cycles + 400LL * qc_cycles; - - for (months = 0; days_in_month[months] <= remdays; months++) remdays -= days_in_month[months]; - - if (months >= 10) { - months -= 12; - years++; - } - - if (years + 100 > INT_MAX || years + 100 < INT_MIN) - return -1; - - tm->tm_year = years + 100; - tm->tm_mon = months + 2; - tm->tm_mday = remdays + 1; - tm->tm_wday = wday; - tm->tm_yday = yday; - - tm->tm_hour = remsecs / 3600; - tm->tm_min = remsecs / 60 % 60; - tm->tm_sec = remsecs % 60; - - return 0; -} - -struct tm *gmtime_r(const time_t *restrict t, struct tm *restrict tm) -{ - if (__secs_to_tm(*t, tm) < 0) { - errno = EOVERFLOW; - return 0; - } - tm->tm_isdst = 0; - tm->__tm_gmtoff = 0; - tm->__tm_zone = __utc; - return tm; -} - -struct tm *gmtime(const time_t *timer) -{ - static struct tm tm; - return gmtime_r(timer, &tm); -} - -struct tm *localtime_r(const time_t *restrict t, struct tm *restrict tm) -{ - if (*t < INT_MIN * 31622400LL || *t > INT_MAX * 31622400LL) { - errno = EOVERFLOW; - return 0; - } - - if (__secs_to_tm(*t, tm) < 0) { - errno = EOVERFLOW; - return 0; - } - - tm->tm_isdst = 0; - tm->__tm_gmtoff = 0; - tm->__tm_zone = __utc; - - return tm; -} - -struct tm *localtime(const time_t *timep) -{ - static struct tm tm; - return localtime_r(timep, &tm); -} - -time_t time(time_t *t) -{ - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - time_t ret = ts.tv_sec; - if (t) - *t = ret; - return ret; -} - -int gettimeofday(struct timeval *tv, struct timezone *tz) -{ - struct timespec ts; - if (!tv) - return 0; - clock_gettime(CLOCK_REALTIME, &ts); - tv->tv_sec = ts.tv_sec; - tv->tv_usec = (int)ts.tv_nsec / 1000; - return 0; -} - -int settimeofday(const struct timeval *tv, const struct timezone *_tz) -{ - if (!tv) - return 0; - if (tv->tv_usec >= 1000000ULL) - return -EINVAL; - return clock_settime(CLOCK_REALTIME, - &((struct timespec){.tv_sec = tv->tv_sec, .tv_nsec = tv->tv_usec * 1000})); -} - -// TODO: -int utimes(const char *filename, const struct timeval times[2]) -{ - unimplemented(); - return 0; -} - -// TODO -void tzset() -{ - unimplemented(); - return; -} - -// TODO -char *ctime_r(const time_t *t, char *buf) -{ - unimplemented(); - return NULL; -} - -// TODO -clock_t clock(void) -{ - unimplemented(); - return 0; -} - -#ifdef RUX_CONFIG_FP_SIMD -double difftime(time_t t1, time_t t0) -{ - return t1 - t0; -} -#endif diff --git a/ulib/ruxlibc/c/uio.c b/ulib/ruxlibc/c/uio.c deleted file mode 100644 index a1e597b3c..000000000 --- a/ulib/ruxlibc/c/uio.c +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -ssize_t readv(int fd, const struct iovec *iov, int iovcnt) -{ - unimplemented(); - return 0; -} - -// TODO -ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset) -{ - unimplemented(); - return 0; -} - -// TODO -ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/unistd.c b/ulib/ruxlibc/c/unistd.c deleted file mode 100644 index a559144c9..000000000 --- a/ulib/ruxlibc/c/unistd.c +++ /dev/null @@ -1,236 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -// TODO: -pid_t getppid(void) -{ - unimplemented(); - return 0; -} - -// TODO: -uid_t geteuid(void) -{ - unimplemented(); - return 0; -} - -// TODO -uid_t getuid(void) -{ - unimplemented(); - return 0; -} - -// TODO -int setuid(uid_t __uid) -{ - unimplemented(); - return 0; -} - -// TODO -pid_t setsid(void) -{ - unimplemented(); - return 0; -} - -// TODO -int setgid(gid_t gid) -{ - unimplemented(); - return 0; -} - -// TODO -int isatty(int fd) -{ - unimplemented(); - return 0; -} - -// TODO -int getpagesize(void) -{ - unimplemented(); - return 0; -} - -// TODO -ssize_t pread(int fd, void *buf, size_t count, off_t offset) -{ - unimplemented(); - return 0; -} - -// TODO -ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset) -{ - unimplemented(); - return 0; -} - -// TODO -int gethostname(char *name, size_t len) -{ - size_t i; - struct utsname uts; - if (uname(&uts)) - return -1; - if (len > sizeof uts.nodename) - len = sizeof uts.nodename; - for (i = 0; i < len && (name[i] = uts.nodename[i]); i++) - ; - if (i && i == len) - name[i - 1] = 0; - return 0; -} - -// TODO -int chown(const char *path, uid_t owner, gid_t group) -{ - unimplemented(); - return 0; -} - -unsigned int sleep(unsigned int seconds) -{ - struct timespec ts; - - ts.tv_sec = seconds; - ts.tv_nsec = 0; - if (nanosleep(&ts, &ts)) - return ts.tv_sec; - - return 0; -} - -int usleep(unsigned useconds) -{ - struct timespec tv = {.tv_sec = useconds / 1000000, .tv_nsec = (useconds % 1000000) * 1000}; - return nanosleep(&tv, &tv); -} - -#ifdef RUX_CONFIG_FS - -// TODO: -int access(const char *pathname, int mode) -{ - unimplemented(); - return 0; -} - -// TODO: -ssize_t readlink(const char *path, char *buf, size_t bufsiz) -{ - unimplemented(); - return 0; -} - -// TODO: -int fsync(int fd) -{ - unimplemented(); - return 0; -} - -// TODO -int fdatasync(int __fildes) -{ - unimplemented(); - return 0; -} - -// TODO: -int fchown(int fd, uid_t owner, gid_t group) -{ - unimplemented("owner: %x group: %x", owner, group); - return 0; -} - -// TODO: -int ftruncate(int fd, off_t length) -{ - unimplemented(); - return 0; -} - -// TODO -int chdir(const char *__path) -{ - unimplemented(); - return 0; -} - -// TODO -int truncate(const char *path, off_t length) -{ - unimplemented(); - return 0; -} - -#endif // RUX_CONFIG_FS - -#ifdef RUX_CONFIG_PIPE - -int pipe2(int fd[2], int flag) -{ - if (!flag) - return pipe(fd); - if (flag & ~(O_CLOEXEC | O_NONBLOCK)) - return -EINVAL; - - int res = pipe(fd); - if (res != 0) - return res; - - if (flag & O_CLOEXEC) { - fcntl(fd[0], F_SETFD, FD_CLOEXEC); - fcntl(fd[1], F_SETFD, FD_CLOEXEC); - } - if (flag & O_NONBLOCK) { - fcntl(fd[0], F_SETFL, O_NONBLOCK); - fcntl(fd[1], F_SETFL, O_NONBLOCK); - } - - return 0; -} - -#endif // RUX_CONFIG_PIPE - -// TODO -_Noreturn void _exit(int status) -{ - exit(status); -} - -// TODO -int execve(const char *__path, char *const *__argv, char *const *__envp) -{ - unimplemented(); - return 0; -} - -// TODO -pid_t fork(void) -{ - unimplemented(); - return -1; -} diff --git a/ulib/ruxlibc/c/utsname.c b/ulib/ruxlibc/c/utsname.c deleted file mode 100644 index 7aeecd9e6..000000000 --- a/ulib/ruxlibc/c/utsname.c +++ /dev/null @@ -1,18 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int uname(struct utsname *a) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/wait.c b/ulib/ruxlibc/c/wait.c deleted file mode 100644 index 90add3e59..000000000 --- a/ulib/ruxlibc/c/wait.c +++ /dev/null @@ -1,26 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -// TODO -pid_t waitpid(pid_t pid, int *status, int options) -{ - unimplemented(); - return 0; -} - -// TODO -pid_t wait3(int *status, int _options, struct rusage *usage) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/ctypes.h b/ulib/ruxlibc/ctypes.h deleted file mode 100644 index f1c1cc8b1..000000000 --- a/ulib/ruxlibc/ctypes.h +++ /dev/null @@ -1,11 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include diff --git a/ulib/ruxlibc/src/env.rs b/ulib/ruxlibc/src/env.rs deleted file mode 100644 index eb0ca4668..000000000 --- a/ulib/ruxlibc/src/env.rs +++ /dev/null @@ -1,140 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ -use core::ffi::{c_char, c_int, c_void}; -use ruxos_posix_api::{environ, environ_iter, RUX_ENVIRON}; - -use crate::malloc::{free, malloc}; -use crate::string::strlen; -unsafe fn find_env(search: *const c_char) -> Option<(usize, *mut c_char)> { - for (i, mut item) in environ_iter().enumerate() { - let mut search = search; - loop { - let end_of_query = *search == 0 || *search == b'=' as c_char; - assert_ne!(*item, 0, "environ has an item without value"); - if *item == b'=' as c_char || end_of_query { - if *item == b'=' as c_char && end_of_query { - // Both keys env here - return Some((i, item.add(1))); - } else { - break; - } - } - - if *item != *search { - break; - } - - item = item.add(1); - search = search.add(1); - } - } - None -} - -unsafe fn put_new_env(insert: *mut c_char) { - // XXX: Another problem is that `environ` can be set to any pointer, which means there is a - // chance of a memory leak. But we can check if it was the same as before, like musl does. - if environ == RUX_ENVIRON.as_mut_ptr() { - *RUX_ENVIRON.last_mut().unwrap() = insert; - RUX_ENVIRON.push(core::ptr::null_mut()); - // Likely a no-op but is needed due to Stacked Borrows. - environ = RUX_ENVIRON.as_mut_ptr(); - } else { - RUX_ENVIRON.clear(); - RUX_ENVIRON.extend(environ_iter()); - RUX_ENVIRON.push(insert); - RUX_ENVIRON.push(core::ptr::null_mut()); - environ = RUX_ENVIRON.as_mut_ptr(); - } -} - -unsafe fn copy_kv( - existing: *mut c_char, - key: *const c_char, - value: *const c_char, - key_len: usize, - value_len: usize, -) { - core::ptr::copy_nonoverlapping(key, existing, key_len); - core::ptr::write(existing.add(key_len), b'=' as c_char); - core::ptr::copy_nonoverlapping(value, existing.add(key_len + 1), value_len); - core::ptr::write(existing.add(key_len + 1 + value_len), 0); -} - -/// set an environ variable -#[no_mangle] -pub unsafe extern "C" fn setenv( - key: *const c_char, - value: *const c_char, - overwrite: c_int, -) -> c_int { - let key_len = strlen(key); - let value_len = strlen(value); - if let Some((i, existing)) = find_env(key) { - if overwrite == 0 { - return 0; - } - - let existing_len = strlen(existing); - if existing_len >= value_len { - // Reuse existing element's allocation - core::ptr::copy_nonoverlapping(value, existing, value_len); - core::ptr::write(existing.add(value_len), 0); - } else { - // Reuse environ slot, but allocate a new pointer. - let ptr = malloc(key_len + 1 + value_len + 1) as *mut c_char; - copy_kv(ptr, key, value, key_len, value_len); - environ.add(i).write(ptr); - } - } else { - // Expand environ and allocate a new pointer. - let ptr = malloc(key_len + 1 + value_len + 1) as *mut c_char; - copy_kv(ptr, key, value, key_len, value_len); - put_new_env(ptr); - } - 0 -} - -/// unset an environ variable -#[no_mangle] -pub unsafe extern "C" fn unsetenv(key: *const c_char) -> c_int { - if let Some((i, _)) = find_env(key) { - if environ == RUX_ENVIRON.as_mut_ptr() { - // No need to worry about updating the pointer, this does not - // reallocate in any way. And the final null is already shifted back. - let rm = RUX_ENVIRON.remove(i); - free(rm as *mut c_void); - // My UB paranoia. - environ = RUX_ENVIRON.as_mut_ptr(); - } else { - let len = RUX_ENVIRON.len(); - for _ in 0..len { - let rm = RUX_ENVIRON.pop().unwrap(); - free(rm as *mut c_void); - } - RUX_ENVIRON.extend( - environ_iter() - .enumerate() - .filter(|&(j, _)| j != i) - .map(|(_, v)| v), - ); - RUX_ENVIRON.push(core::ptr::null_mut()); - environ = RUX_ENVIRON.as_mut_ptr(); - } - } - 0 -} - -/// get the corresponding environ variable -#[no_mangle] -pub unsafe extern "C" fn getenv(name: *const c_char) -> *mut c_char { - find_env(name) - .map(|val| val.1) - .unwrap_or(core::ptr::null_mut()) -} diff --git a/ulib/ruxlibc/src/errno.rs b/ulib/ruxlibc/src/errno.rs deleted file mode 100644 index 6bfd13677..000000000 --- a/ulib/ruxlibc/src/errno.rs +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use axerrno::LinuxError; -use core::ffi::{c_char, c_int}; - -/// The global errno variable. -#[cfg_attr(feature = "tls", thread_local)] -#[no_mangle] -#[allow(non_upper_case_globals)] -pub static mut errno: c_int = 0; - -pub fn set_errno(code: i32) { - unsafe { - errno = code; - } -} - -/// Returns a pointer to the global errno variable. -#[no_mangle] -pub unsafe extern "C" fn __errno_location() -> *mut c_int { - &mut errno -} - -/// Returns a pointer to the string representation of the given error code. -#[no_mangle] -pub unsafe extern "C" fn strerror(e: c_int) -> *mut c_char { - #[allow(non_upper_case_globals)] - static mut strerror_buf: [u8; 256] = [0; 256]; // TODO: thread safe - - let err_str = if e == 0 { - "Success" - } else { - LinuxError::try_from(e) - .map(|e| e.as_str()) - .unwrap_or("Unknown error") - }; - unsafe { - strerror_buf[..err_str.len()].copy_from_slice(err_str.as_bytes()); - strerror_buf.as_mut_ptr() as *mut c_char - } -} diff --git a/ulib/ruxlibc/src/fd_ops.rs b/ulib/ruxlibc/src/fd_ops.rs deleted file mode 100644 index 21518e5e6..000000000 --- a/ulib/ruxlibc/src/fd_ops.rs +++ /dev/null @@ -1,63 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::{ctypes, utils::e}; -use axerrno::LinuxError; -use core::ffi::c_int; -use ruxos_posix_api::{sys_close, sys_dup, sys_dup2, sys_fcntl}; - -/// Close a file by `fd`. -#[no_mangle] -pub unsafe extern "C" fn close(fd: c_int) -> c_int { - e(sys_close(fd)) -} - -/// Duplicate a file descriptor. -#[no_mangle] -pub unsafe extern "C" fn dup(old_fd: c_int) -> c_int { - e(sys_dup(old_fd)) -} - -/// Duplicate a file descriptor, use file descriptor specified in `new_fd`. -#[no_mangle] -pub unsafe extern "C" fn dup2(old_fd: c_int, new_fd: c_int) -> c_int { - e(sys_dup2(old_fd, new_fd)) -} - -/// Duplicate a file descriptor, the caller can force the close-on-exec flag to -/// be set for the new file descriptor by specifying `O_CLOEXEC` in flags. -/// -/// If oldfd equals newfd, then `dup3()` fails with the error `EINVAL`. -#[no_mangle] -pub unsafe extern "C" fn dup3(old_fd: c_int, new_fd: c_int, flags: c_int) -> c_int { - if old_fd == new_fd { - return e((LinuxError::EINVAL as c_int).wrapping_neg()); - } - let r = e(sys_dup2(old_fd, new_fd)); - if r < 0 { - r - } else { - if flags as u32 & ctypes::O_CLOEXEC != 0 { - e(sys_fcntl( - new_fd, - ctypes::F_SETFD as c_int, - ctypes::FD_CLOEXEC as usize, - )); - } - new_fd - } -} - -/// Manipulate file descriptor. -/// -/// TODO: `SET/GET` command is ignored -#[no_mangle] -pub unsafe extern "C" fn ax_fcntl(fd: c_int, cmd: c_int, arg: usize) -> c_int { - e(sys_fcntl(fd, cmd, arg)) -} diff --git a/ulib/ruxlibc/src/fs.rs b/ulib/ruxlibc/src/fs.rs deleted file mode 100644 index a396e4ae4..000000000 --- a/ulib/ruxlibc/src/fs.rs +++ /dev/null @@ -1,109 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::{c_char, c_int}; - -use ruxos_posix_api::{ - sys_fstat, sys_getcwd, sys_lseek, sys_lstat, sys_mkdir, sys_open, sys_rename, sys_rmdir, - sys_stat, sys_unlink, -}; - -use crate::{ctypes, utils::e}; - -/// Open a file by `filename` and insert it into the file descriptor table. -/// -/// Return its index in the file table (`fd`). Return `EMFILE` if it already -/// has the maximum number of files open. -#[no_mangle] -pub unsafe extern "C" fn ax_open( - filename: *const c_char, - flags: c_int, - mode: ctypes::mode_t, -) -> c_int { - e(sys_open(filename, flags, mode)) -} - -/// Set the position of the file indicated by `fd`. -/// -/// Return its position after seek. -#[no_mangle] -pub unsafe extern "C" fn lseek(fd: c_int, offset: ctypes::off_t, whence: c_int) -> ctypes::off_t { - e(sys_lseek(fd, offset, whence) as _) as _ -} - -/// Get the file metadata by `path` and write into `buf`. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn stat(path: *const c_char, buf: *mut ctypes::stat) -> c_int { - e(sys_stat(path, buf as _)) -} - -/// Get file metadata by `fd` and write into `buf`. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn fstat(fd: c_int, buf: *mut ctypes::stat) -> c_int { - e(sys_fstat(fd, buf as *mut core::ffi::c_void)) -} - -/// Get the metadata of the symbolic link and write into `buf`. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn lstat(path: *const c_char, buf: *mut ctypes::stat) -> c_int { - e(sys_lstat(path, buf) as _) -} - -/// Get the path of the current directory. -#[no_mangle] -pub unsafe extern "C" fn getcwd(buf: *mut c_char, size: usize) -> *mut c_char { - if buf.is_null() && size != 0 { - crate::errno::set_errno(axerrno::LinuxError::EINVAL as _); - return core::ptr::null_mut() as *mut c_char; - } - let e = sys_getcwd(buf, size); - if e < 0 { - return core::ptr::null_mut() as *mut c_char; - } - if e == 0 || buf.read() != '/' as _ { - crate::errno::set_errno(axerrno::LinuxError::ENOENT as _); - return core::ptr::null_mut() as *mut c_char; - } - buf -} - -/// Rename `old` to `new` -/// If new exists, it is first removed. -/// -/// Return 0 if the operation succeeds, otherwise return -1. -#[no_mangle] -pub unsafe extern "C" fn rename(old: *const c_char, new: *const c_char) -> c_int { - e(sys_rename(old, new)) -} - -/// Remove a directory, which must be empty -/// -/// Return 0 if the operation succeeds, otherwise return -1. -#[no_mangle] -pub unsafe extern "C" fn rmdir(pathname: *const c_char) -> c_int { - e(sys_rmdir(pathname)) -} - -/// Removes a file from the filesystem. -#[no_mangle] -pub unsafe extern "C" fn unlink(pathname: *const c_char) -> c_int { - e(sys_unlink(pathname)) -} - -/// Creates a new directory -#[no_mangle] -pub unsafe extern "C" fn mkdir(pathname: *const c_char, mode: ctypes::mode_t) -> c_int { - e(sys_mkdir(pathname, mode)) -} diff --git a/ulib/ruxlibc/src/io.rs b/ulib/ruxlibc/src/io.rs deleted file mode 100644 index 2c7450195..000000000 --- a/ulib/ruxlibc/src/io.rs +++ /dev/null @@ -1,54 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::{c_int, c_void}; -#[cfg(feature = "fd")] -use ruxos_posix_api::sys_ioctl; - -#[cfg(not(test))] -use ruxos_posix_api::sys_write; -use ruxos_posix_api::{sys_read, sys_writev}; - -use crate::{ctypes, utils::e}; - -/// Read data from the file indicated by `fd`. -/// -/// Return the read size if success. -#[no_mangle] -pub unsafe extern "C" fn read(fd: c_int, buf: *mut c_void, count: usize) -> ctypes::ssize_t { - e(sys_read(fd, buf, count) as _) as _ -} - -/// Write data to the file indicated by `fd`. -/// -/// Return the written size if success. -#[no_mangle] -#[cfg(not(test))] -pub unsafe extern "C" fn write(fd: c_int, buf: *const c_void, count: usize) -> ctypes::ssize_t { - e(sys_write(fd, buf, count) as _) as _ -} - -/// Write a vector. -#[no_mangle] -pub unsafe extern "C" fn writev( - fd: c_int, - iov: *const ctypes::iovec, - iocnt: c_int, -) -> ctypes::ssize_t { - e(sys_writev(fd, iov, iocnt) as _) as _ -} - -/// Manipulate file descriptor. -/// -/// TODO: `SET/GET` command is ignored -#[cfg(feature = "fd")] -#[no_mangle] -pub unsafe extern "C" fn rux_ioctl(fd: c_int, req: c_int, arg: usize) -> c_int { - e(sys_ioctl(fd, req.try_into().unwrap(), arg)) -} diff --git a/ulib/ruxlibc/src/io_mpx.rs b/ulib/ruxlibc/src/io_mpx.rs deleted file mode 100644 index 15cb297b2..000000000 --- a/ulib/ruxlibc/src/io_mpx.rs +++ /dev/null @@ -1,76 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::{ctypes, utils::e}; - -use core::ffi::c_int; - -#[cfg(feature = "poll")] -use ruxos_posix_api::sys_poll; -#[cfg(feature = "select")] -use ruxos_posix_api::sys_select; -#[cfg(feature = "epoll")] -use ruxos_posix_api::{sys_epoll_create, sys_epoll_ctl, sys_epoll_wait}; - -/// Creates a new epoll instance. -/// -/// It returns a file descriptor referring to the new epoll instance. -#[cfg(feature = "epoll")] -#[no_mangle] -pub unsafe extern "C" fn epoll_create(size: c_int) -> c_int { - e(sys_epoll_create(size)) -} - -/// Control interface for an epoll file descriptor -#[cfg(feature = "epoll")] -#[no_mangle] -pub unsafe extern "C" fn epoll_ctl( - epfd: c_int, - op: c_int, - fd: c_int, - event: *mut ctypes::epoll_event, -) -> c_int { - e(sys_epoll_ctl(epfd, op, fd, event)) -} - -/// Waits for events on the epoll instance referred to by the file descriptor epfd. -#[cfg(feature = "epoll")] -#[no_mangle] -pub unsafe extern "C" fn epoll_wait( - epfd: c_int, - events: *mut ctypes::epoll_event, - maxevents: c_int, - timeout: c_int, -) -> c_int { - e(sys_epoll_wait(epfd, events, maxevents, timeout)) -} - -/// Monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation -#[cfg(feature = "select")] -#[no_mangle] -pub unsafe extern "C" fn select( - nfds: c_int, - readfds: *mut ctypes::fd_set, - writefds: *mut ctypes::fd_set, - exceptfds: *mut ctypes::fd_set, - timeout: *mut ctypes::timeval, -) -> c_int { - e(sys_select(nfds, readfds, writefds, exceptfds, timeout)) -} - -/// Monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation -#[cfg(feature = "poll")] -#[no_mangle] -pub unsafe extern "C" fn poll( - fds: *mut ctypes::pollfd, - nfds: ctypes::nfds_t, - timeout: c_int, -) -> c_int { - e(sys_poll(fds, nfds, timeout)) -} diff --git a/ulib/ruxlibc/src/lib.rs b/ulib/ruxlibc/src/lib.rs deleted file mode 100644 index 160566cff..000000000 --- a/ulib/ruxlibc/src/lib.rs +++ /dev/null @@ -1,152 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -//! [Ruxos] user program library for C apps. -//! -//! ## Cargo Features -//! -//! - CPU -//! - `smp`: Enable SMP (symmetric multiprocessing) support. -//! - `fp_simd`: Enable floating point and SIMD support. -//! - Interrupts: -//! - `irq`: Enable interrupt handling support. -//! - Memory -//! - `alloc`: Enable dynamic memory allocation. -//! - `tls`: Enable thread-local storage. -//! - Task management -//! - `multitask`: Enable multi-threading support. -//! - Upperlayer stacks -//! - `fs`: Enable file system support. -//! - `net`: Enable networking support. -//! - `signal`: Enable signal support. -//! - Lib C functions -//! - `fd`: Enable file descriptor table. -//! - `pipe`: Enable pipe support. -//! - `select`: Enable synchronous I/O multiplexing ([select]) support. -//! - `epoll`: Enable event polling ([epoll]) support. -//! -//! [Ruxos]: https://github.com/syswonder/ruxos -//! [select]: https://man7.org/linux/man-pages/man2/select.2.html -//! [epoll]: https://man7.org/linux/man-pages/man7/epoll.7.html - -#![cfg_attr(all(not(test), not(doc)), no_std)] -#![feature(doc_cfg)] -#![feature(doc_auto_cfg)] -#![feature(naked_functions)] -#![feature(thread_local)] -#![allow(clippy::missing_safety_doc)] - -#[cfg(feature = "alloc")] -extern crate alloc; -#[cfg(feature = "alloc")] -mod env; -#[path = "."] -mod ctypes { - #[rustfmt::skip] - #[path = "libctypes_gen.rs"] - #[allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals, clippy::upper_case_acronyms)] - mod libctypes; - - pub use libctypes::*; - pub use ruxos_posix_api::ctypes::*; -} - -#[macro_use] -mod utils; - -#[cfg(feature = "fd")] -mod fd_ops; -#[cfg(feature = "fs")] -mod fs; -#[cfg(any(feature = "select", feature = "poll", feature = "epoll"))] -mod io_mpx; -#[cfg(feature = "alloc")] -mod malloc; -#[cfg(feature = "alloc")] -mod mmap; -#[cfg(feature = "net")] -mod net; -#[cfg(feature = "pipe")] -mod pipe; -#[cfg(feature = "multitask")] -mod pthread; -#[cfg(feature = "alloc")] -mod strftime; -#[cfg(feature = "fp_simd")] -mod strtod; - -mod errno; -mod io; -mod mktime; -mod rand; -mod resource; -mod setjmp; -mod signal; -mod string; -mod sys; -mod time; -mod unistd; - -#[cfg(not(test))] -pub use self::io::write; -pub use self::io::{read, writev}; - -pub use self::errno::strerror; -pub use self::mktime::mktime; -pub use self::rand::{getrandom, rand, random, srand}; -pub use self::resource::{getrlimit, setrlimit}; -pub use self::setjmp::{longjmp, setjmp}; -pub use self::string::{strlen, strnlen}; -pub use self::sys::sysconf; -pub use self::time::{clock_gettime, nanosleep}; -pub use self::unistd::{abort, exit, getpid}; - -#[cfg(feature = "alloc")] -pub use self::env::{getenv, setenv, unsetenv}; -#[cfg(feature = "fd")] -pub use self::fd_ops::{ax_fcntl, close, dup, dup2, dup3}; -#[cfg(feature = "fs")] -pub use self::fs::{ax_open, fstat, getcwd, lseek, lstat, mkdir, rename, rmdir, stat, unlink}; -#[cfg(feature = "fd")] -pub use self::io::rux_ioctl; -#[cfg(feature = "poll")] -pub use self::io_mpx::poll; -#[cfg(feature = "select")] -pub use self::io_mpx::select; -#[cfg(feature = "epoll")] -pub use self::io_mpx::{epoll_create, epoll_ctl, epoll_wait}; -#[cfg(feature = "alloc")] -pub use self::malloc::{free, malloc}; -#[cfg(feature = "alloc")] -pub use self::mmap::{mmap, munmap}; -#[cfg(feature = "net")] -pub use self::net::{ - accept, ax_sendmsg, bind, connect, freeaddrinfo, getaddrinfo, getpeername, getsockname, listen, - recv, recvfrom, send, sendto, shutdown, socket, -}; -#[cfg(feature = "pipe")] -pub use self::pipe::pipe; -#[cfg(feature = "multitask")] -pub use self::pthread::{ - pthread_cond_broadcast, pthread_cond_init, pthread_cond_signal, pthread_cond_wait, -}; -#[cfg(feature = "multitask")] -pub use self::pthread::{pthread_create, pthread_exit, pthread_join, pthread_self}; -#[cfg(feature = "multitask")] -pub use self::pthread::{ - pthread_mutex_init, pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock, -}; -#[cfg(feature = "alloc")] -pub use self::strftime::strftime; -#[cfg(feature = "fp_simd")] -pub use self::strtod::{strtod, strtof}; -#[cfg(feature = "signal")] -pub use self::time::{getitimer, setitimer}; -#[cfg(feature = "signal")] -pub use self::unistd::{alarm, ualarm}; diff --git a/ulib/ruxlibc/src/malloc.rs b/ulib/ruxlibc/src/malloc.rs deleted file mode 100644 index 4b2cb25c5..000000000 --- a/ulib/ruxlibc/src/malloc.rs +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -//! Provides the corresponding malloc(size_t) and free(size_t) when using the C user program. -//! -//! The normal malloc(size_t) and free(size_t) are provided by the library malloc.h, and -//! sys_brk is used internally to apply for memory from the kernel. But in a unikernel like -//! `Ruxos`, we noticed that the heap of the Rust user program is shared with the kernel. In -//! order to maintain consistency, C user programs also choose to share the kernel heap, -//! skipping the sys_brk step. - -use alloc::alloc::{alloc, dealloc}; -use core::alloc::Layout; -use core::ffi::c_void; - -use crate::ctypes; - -struct MemoryControlBlock { - size: usize, -} - -const CTRL_BLK_SIZE: usize = core::mem::size_of::(); - -/// Allocate memory and return the memory address. -/// -/// Returns 0 on failure (the current implementation does not trigger an exception) -#[no_mangle] -pub unsafe extern "C" fn malloc(size: ctypes::size_t) -> *mut c_void { - // Allocate `(actual length) + 8`. The lowest 8 Bytes are stored in the actual allocated space size. - // This is because free(uintptr_t) has only one parameter representing the address, - // So we need to save in advance to know the size of the memory space that needs to be released - let layout = Layout::from_size_align(size + CTRL_BLK_SIZE, 8).unwrap(); - unsafe { - let ptr = alloc(layout).cast::(); - assert!(!ptr.is_null(), "malloc failed"); - ptr.write(MemoryControlBlock { size }); - ptr.add(1).cast() - } -} - -/// Deallocate memory. -/// -/// (WARNING) If the address to be released does not match the allocated address, an error should -/// occur, but it will NOT be checked out. This is due to the global allocator `Buddy_system` -/// (currently used) does not check the validity of address to be released. -#[no_mangle] -pub unsafe extern "C" fn free(ptr: *mut c_void) { - if ptr.is_null() { - return; - } - let ptr = ptr.cast::(); - assert!(ptr as usize > CTRL_BLK_SIZE, "free a null pointer"); - unsafe { - let ptr = ptr.sub(1); - let size = ptr.read().size; - let layout = Layout::from_size_align(size + CTRL_BLK_SIZE, 8).unwrap(); - dealloc(ptr.cast(), layout) - } -} diff --git a/ulib/ruxlibc/src/mktime.rs b/ulib/ruxlibc/src/mktime.rs deleted file mode 100644 index fadc1cfb5..000000000 --- a/ulib/ruxlibc/src/mktime.rs +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; - -use crate::ctypes; - -const MONTH_DAYS: [[c_int; 12]; 2] = [ - // Non-leap years: - [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], - // Leap years: - [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], -]; - -#[inline(always)] -fn leap_year(year: c_int) -> bool { - year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) -} - -/// Convert broken-down time into time since the Epoch. -#[no_mangle] -pub unsafe extern "C" fn mktime(t: *mut ctypes::tm) -> ctypes::time_t { - let mut year = (*t).tm_year + 1900; - let mut month = (*t).tm_mon; - let mut day = (*t).tm_mday as i64 - 1; - - let leap = if leap_year(year) { 1 } else { 0 }; - - if year < 1970 { - day = MONTH_DAYS[if leap_year(year) { 1 } else { 0 }][(*t).tm_mon as usize] as i64 - day; - - while year < 1969 { - year += 1; - day += if leap_year(year) { 366 } else { 365 }; - } - - while month < 11 { - month += 1; - day += MONTH_DAYS[leap][month as usize] as i64; - } - - (-(day * (60 * 60 * 24) - - (((*t).tm_hour as i64) * (60 * 60) + ((*t).tm_min as i64) * 60 + (*t).tm_sec as i64))) - as ctypes::time_t - } else { - while year > 1970 { - year -= 1; - day += if leap_year(year) { 366 } else { 365 }; - } - - while month > 0 { - month -= 1; - day += MONTH_DAYS[leap][month as usize] as i64; - } - - (day * (60 * 60 * 24) - + ((*t).tm_hour as i64) * (60 * 60) - + ((*t).tm_min as i64) * 60 - + (*t).tm_sec as i64) as ctypes::time_t - } -} diff --git a/ulib/ruxlibc/src/mmap.rs b/ulib/ruxlibc/src/mmap.rs deleted file mode 100644 index 4573e2cfc..000000000 --- a/ulib/ruxlibc/src/mmap.rs +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::ctypes; -use core::ffi::{c_int, c_void}; - -use ruxos_posix_api::{sys_madvise, sys_mmap, sys_mprotect, sys_mremap, sys_msync, sys_munmap}; - -/// Map a file or device into virtual memory. -#[no_mangle] -pub unsafe extern "C" fn mmap( - addr: *mut c_void, - len: ctypes::size_t, - prot: c_int, - flags: c_int, - fid: c_int, - offset: ctypes::off_t, -) -> *mut c_void { - sys_mmap(addr, len, prot, flags, fid, offset) -} - -/// Unmap a range address of memory. -#[no_mangle] -pub unsafe extern "C" fn munmap(addr: *mut c_void, len: ctypes::size_t) -> c_int { - sys_munmap(addr, len) -} - -/// Sync pages mapped in memory to file. -#[no_mangle] -pub unsafe extern "C" fn msync(addr: *mut c_void, len: ctypes::size_t, flags: c_int) -> c_int { - sys_msync(addr, len, flags) -} - -/// Remap the address for already mapped memory. -#[no_mangle] -pub unsafe extern "C" fn mremap( - old_addr: *mut c_void, - old_size: ctypes::size_t, - new_size: ctypes::size_t, - flags: c_int, - new_addr: *mut c_void, -) -> *mut c_void { - sys_mremap(old_addr, old_size, new_size, flags, new_addr) -} - -/// Change the accessiblity for already mapped memory. -#[no_mangle] -pub unsafe extern "C" fn mprotect(addr: *mut c_void, len: ctypes::size_t, flags: c_int) -> c_int { - sys_mprotect(addr, len, flags) -} - -/// Advise the operating system about the expected behavior of a specific region of memory. -/// -/// Note: Unimplement yet. -#[no_mangle] -pub unsafe extern "C" fn madvise(addr: *mut c_void, len: ctypes::size_t, advice: c_int) -> c_int { - sys_madvise(addr, len, advice) -} diff --git a/ulib/ruxlibc/src/net.rs b/ulib/ruxlibc/src/net.rs deleted file mode 100644 index 6ae7d237c..000000000 --- a/ulib/ruxlibc/src/net.rs +++ /dev/null @@ -1,198 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::{c_char, c_int, c_void}; -use ruxos_posix_api as api; - -use crate::{ctypes, utils::e}; - -/// Create an socket for communication. -/// -/// Return the socket file descriptor. -#[no_mangle] -pub unsafe extern "C" fn socket(domain: c_int, socktype: c_int, protocol: c_int) -> c_int { - e(api::sys_socket(domain, socktype, protocol)) -} - -/// Bind a address to a socket. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn bind( - socket_fd: c_int, - socket_addr: *const ctypes::sockaddr, - addrlen: ctypes::socklen_t, -) -> c_int { - e(api::sys_bind(socket_fd, socket_addr, addrlen)) -} - -/// Connects the socket to the address specified. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn connect( - socket_fd: c_int, - socket_addr: *const ctypes::sockaddr, - addrlen: ctypes::socklen_t, -) -> c_int { - e(api::sys_connect(socket_fd, socket_addr, addrlen)) -} - -/// Send a message on a socket to the address specified. -/// -/// Return the number of bytes sent if success. -#[no_mangle] -pub unsafe extern "C" fn sendto( - socket_fd: c_int, - buf_ptr: *const c_void, - len: ctypes::size_t, - flag: c_int, // currently not used - socket_addr: *const ctypes::sockaddr, - addrlen: ctypes::socklen_t, -) -> ctypes::ssize_t { - if socket_addr.is_null() && addrlen == 0 { - return e(api::sys_send(socket_fd, buf_ptr, len, flag) as _) as _; - } - e(api::sys_sendto(socket_fd, buf_ptr, len, flag, socket_addr, addrlen) as _) as _ -} - -/// Send a message on a socket to the address connected. -/// -/// Return the number of bytes sent if success. -#[no_mangle] -pub unsafe extern "C" fn send( - socket_fd: c_int, - buf_ptr: *const c_void, - len: ctypes::size_t, - flag: c_int, // currently not used -) -> ctypes::ssize_t { - e(api::sys_send(socket_fd, buf_ptr, len, flag) as _) as _ -} - -/// Receive a message on a socket and get its source address. -/// -/// Return the number of bytes received if success. -#[no_mangle] -pub unsafe extern "C" fn recvfrom( - socket_fd: c_int, - buf_ptr: *mut c_void, - len: ctypes::size_t, - flag: c_int, // currently not used - socket_addr: *mut ctypes::sockaddr, - addrlen: *mut ctypes::socklen_t, -) -> ctypes::ssize_t { - if socket_addr.is_null() { - return e(api::sys_recv(socket_fd, buf_ptr, len, flag) as _) as _; - } - e(api::sys_recvfrom(socket_fd, buf_ptr, len, flag, socket_addr, addrlen) as _) as _ -} - -/// Receive a message on a socket. -/// -/// Return the number of bytes received if success. -#[no_mangle] -pub unsafe extern "C" fn recv( - socket_fd: c_int, - buf_ptr: *mut c_void, - len: ctypes::size_t, - flag: c_int, // currently not used -) -> ctypes::ssize_t { - e(api::sys_recv(socket_fd, buf_ptr, len, flag) as _) as _ -} - -/// Listen for connections on a socket -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn listen( - socket_fd: c_int, - backlog: c_int, // currently not used -) -> c_int { - e(api::sys_listen(socket_fd, backlog)) -} - -/// Accept for connections on a socket -/// -/// Return file descriptor for the accepted socket if success. -#[no_mangle] -pub unsafe extern "C" fn accept( - socket_fd: c_int, - socket_addr: *mut ctypes::sockaddr, - socket_len: *mut ctypes::socklen_t, -) -> c_int { - e(api::sys_accept(socket_fd, socket_addr, socket_len)) -} - -/// Shut down a full-duplex connection. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn shutdown( - socket_fd: c_int, - flag: c_int, // currently not used -) -> c_int { - e(api::sys_shutdown(socket_fd, flag)) -} - -/// Query addresses for a domain name. -/// -/// Return address number if success. -#[no_mangle] -pub unsafe extern "C" fn getaddrinfo( - nodename: *const c_char, - servname: *const c_char, - hints: *const ctypes::addrinfo, - res: *mut *mut ctypes::addrinfo, -) -> c_int { - let ret = e(api::sys_getaddrinfo(nodename, servname, hints, res)); - match ret { - r if r < 0 => ctypes::EAI_FAIL, - 0 => ctypes::EAI_NONAME, - _ => 0, - } -} - -/// Free queried `addrinfo` struct -#[no_mangle] -pub unsafe extern "C" fn freeaddrinfo(res: *mut ctypes::addrinfo) { - api::sys_freeaddrinfo(res); -} - -/// Get current address to which the socket sockfd is bound. -#[no_mangle] -pub unsafe extern "C" fn getsockname( - sock_fd: c_int, - addr: *mut ctypes::sockaddr, - addrlen: *mut ctypes::socklen_t, -) -> c_int { - e(api::sys_getsockname(sock_fd, addr, addrlen)) -} - -/// Get peer address to which the socket sockfd is connected. -#[no_mangle] -pub unsafe extern "C" fn getpeername( - sock_fd: c_int, - addr: *mut ctypes::sockaddr, - addrlen: *mut ctypes::socklen_t, -) -> c_int { - e(api::sys_getpeername(sock_fd, addr, addrlen)) -} - -/// Send a message on a socket to the address connected. -/// The message is pointed to by the elements of the array msg.msg_iov. -/// -/// Return the number of bytes sent if success. -#[no_mangle] -pub unsafe extern "C" fn ax_sendmsg( - socket_fd: c_int, - msg: *const ctypes::msghdr, - flags: c_int, -) -> ctypes::ssize_t { - e(api::sys_sendmsg(socket_fd, msg, flags) as _) as _ -} diff --git a/ulib/ruxlibc/src/pipe.rs b/ulib/ruxlibc/src/pipe.rs deleted file mode 100644 index d4d853ca6..000000000 --- a/ulib/ruxlibc/src/pipe.rs +++ /dev/null @@ -1,23 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; - -use ruxos_posix_api::sys_pipe; - -use crate::utils::e; - -/// Create a pipe -/// -/// Return 0 if succeed -#[no_mangle] -pub unsafe extern "C" fn pipe(fd: *mut c_int) -> c_int { - let fds = unsafe { core::slice::from_raw_parts_mut(fd, 2) }; - e(sys_pipe(fds)) -} diff --git a/ulib/ruxlibc/src/pthread.rs b/ulib/ruxlibc/src/pthread.rs deleted file mode 100644 index 8df293acc..000000000 --- a/ulib/ruxlibc/src/pthread.rs +++ /dev/null @@ -1,157 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::{ctypes, utils::e}; -use core::ffi::{c_int, c_void}; -use ruxos_posix_api as api; - -/// Returns the `pthread` struct of current thread. -#[no_mangle] -pub unsafe extern "C" fn pthread_self() -> ctypes::pthread_t { - api::sys_pthread_self() -} - -/// Create a new thread with the given entry point and argument. -/// -/// If successful, it stores the pointer to the newly created `struct __pthread` -/// in `res` and returns 0. -#[no_mangle] -pub unsafe extern "C" fn pthread_create( - res: *mut ctypes::pthread_t, - attr: *const ctypes::pthread_attr_t, - start_routine: extern "C" fn(arg: *mut c_void) -> *mut c_void, - arg: *mut c_void, -) -> c_int { - e(api::sys_pthread_create(res, attr, start_routine, arg)) -} - -/// Exits the current thread. The value `retval` will be returned to the joiner. -#[no_mangle] -pub unsafe extern "C" fn pthread_exit(retval: *mut c_void) -> ! { - api::sys_pthread_exit(retval) -} - -/// Waits for the given thread to exit, and stores the return value in `retval`. -#[no_mangle] -pub unsafe extern "C" fn pthread_join( - thread: ctypes::pthread_t, - retval: *mut *mut c_void, -) -> c_int { - e(api::sys_pthread_join(thread, retval)) -} - -/// Initialize a mutex. -#[no_mangle] -pub unsafe extern "C" fn pthread_mutex_init( - mutex: *mut ctypes::pthread_mutex_t, - attr: *const ctypes::pthread_mutexattr_t, -) -> c_int { - e(api::sys_pthread_mutex_init(mutex, attr)) -} - -/// Destroy a mutex. -#[no_mangle] -pub unsafe extern "C" fn pthread_mutex_destroy(mutex: *mut ctypes::pthread_mutex_t) -> c_int { - e(api::sys_pthread_mutex_destroy(mutex)) -} - -/// Lock the given mutex. -#[no_mangle] -pub unsafe extern "C" fn pthread_mutex_lock(mutex: *mut ctypes::pthread_mutex_t) -> c_int { - e(api::sys_pthread_mutex_lock(mutex)) -} - -/// Lock the given mutex. If the mutex is already locked, it returns immediatly with the error -/// code EBUSY. -#[no_mangle] -pub unsafe extern "C" fn pthread_mutex_trylock(mutex: *mut ctypes::pthread_mutex_t) -> c_int { - e(api::sys_pthread_mutex_trylock(mutex)) -} - -/// Unlock the given mutex. -#[no_mangle] -pub unsafe extern "C" fn pthread_mutex_unlock(mutex: *mut ctypes::pthread_mutex_t) -> c_int { - e(api::sys_pthread_mutex_unlock(mutex)) -} - -/// Initialize a condition variable -#[no_mangle] -pub unsafe extern "C" fn pthread_cond_init( - condvar: *mut ctypes::pthread_cond_t, - attr: *mut ctypes::pthread_condattr_t, -) -> c_int { - e(api::sys_pthread_cond_init(condvar, attr)) -} - -/// Destroy a condition variable -#[no_mangle] -pub unsafe extern "C" fn pthread_cond_destroy(condvar: *mut ctypes::pthread_cond_t) -> c_int { - e(api::sys_pthread_cond_destroy(condvar)) -} - -#[no_mangle] -/// Wait for the condition variable to be signaled or timeout -pub unsafe extern "C" fn pthread_cond_timedwait( - condvar: *mut ctypes::pthread_cond_t, - mutex: *mut ctypes::pthread_mutex_t, - abstime: *const ctypes::timespec, -) -> c_int { - e(api::sys_pthread_cond_timedwait(condvar, mutex, abstime)) -} - -/// Wait for the condition variable to be signaled -#[no_mangle] -pub unsafe extern "C" fn pthread_cond_wait( - condvar: *mut ctypes::pthread_cond_t, - mutex: *mut ctypes::pthread_mutex_t, -) -> c_int { - e(api::sys_pthread_cond_wait(condvar, mutex)) -} - -/// Restarts one of the threads that are waiting on the condition variable. -#[no_mangle] -pub unsafe extern "C" fn pthread_cond_signal(condvar: *mut ctypes::pthread_cond_t) -> c_int { - e(api::sys_pthread_cond_signal(condvar)) -} - -/// Restarts all the threads that are waiting on the condition variable. -#[no_mangle] -pub unsafe extern "C" fn pthread_cond_broadcast(condvar: *mut ctypes::pthread_cond_t) -> c_int { - e(api::sys_pthread_cond_broadcast(condvar)) -} - -/// Initialize a thread-specific data key -#[no_mangle] -pub unsafe extern "C" fn pthread_key_create( - key: *mut ctypes::pthread_key_t, - dtor: Option, -) -> c_int { - e(api::sys_pthread_key_create(key, dtor)) -} - -/// Destroy a thread-specific data key -#[no_mangle] -pub unsafe extern "C" fn pthread_key_delete(key: ctypes::pthread_key_t) -> c_int { - e(api::sys_pthread_key_delete(key)) -} - -/// Get the value of a thread-specific data key -#[no_mangle] -pub unsafe extern "C" fn pthread_getspecific(key: ctypes::pthread_key_t) -> *mut c_void { - api::sys_pthread_getspecific(key) -} - -/// Set the value of a thread-specific data key -#[no_mangle] -pub unsafe extern "C" fn pthread_setspecific( - key: ctypes::pthread_key_t, - value: *const c_void, -) -> c_int { - e(api::sys_pthread_setspecific(key, value)) -} diff --git a/ulib/ruxlibc/src/rand.rs b/ulib/ruxlibc/src/rand.rs deleted file mode 100644 index 07eefdd60..000000000 --- a/ulib/ruxlibc/src/rand.rs +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::ctypes::size_t; -use core::ffi::{c_int, c_long, c_uint, c_void}; - -use ruxos_posix_api::{sys_getrandom, sys_rand, sys_random, sys_srand}; - -use crate::utils::e; - -/// srand -#[no_mangle] -pub unsafe extern "C" fn srand(seed: c_uint) { - sys_srand(seed); -} - -/// rand -#[no_mangle] -pub unsafe extern "C" fn rand() -> c_int { - e(sys_rand() as c_int) -} - -/// random -#[no_mangle] -pub unsafe extern "C" fn random() -> c_long { - e(sys_random().try_into().unwrap()) as _ -} - -/// Get random -#[no_mangle] -pub unsafe extern "C" fn getrandom(buf: *mut c_void, buflen: size_t, flags: c_int) -> size_t { - e(sys_getrandom(buf, buflen, flags).try_into().unwrap()) as _ -} diff --git a/ulib/ruxlibc/src/resource.rs b/ulib/ruxlibc/src/resource.rs deleted file mode 100644 index 2f55ea5e9..000000000 --- a/ulib/ruxlibc/src/resource.rs +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; - -use ruxos_posix_api::{sys_getrlimit, sys_setrlimit}; - -use crate::utils::e; - -/// Get resource limitations -#[no_mangle] -pub unsafe extern "C" fn getrlimit(resource: c_int, rlimits: *mut crate::ctypes::rlimit) -> c_int { - e(sys_getrlimit(resource, rlimits)) -} - -/// Set resource limitations -#[no_mangle] -pub unsafe extern "C" fn setrlimit( - resource: c_int, - rlimits: *const crate::ctypes::rlimit, -) -> c_int { - e(sys_setrlimit(resource, rlimits)) -} diff --git a/ulib/ruxlibc/src/setjmp.rs b/ulib/ruxlibc/src/setjmp.rs deleted file mode 100644 index bf310415c..000000000 --- a/ulib/ruxlibc/src/setjmp.rs +++ /dev/null @@ -1,247 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; - -use crate::ctypes; - -/// `setjmp` implementation -#[naked] -#[no_mangle] -pub unsafe extern "C" fn setjmp(_buf: *mut ctypes::__jmp_buf_tag) { - #[cfg(all(target_arch = "aarch64", feature = "fp_simd"))] - core::arch::asm!( - " - stp x19, x20, [x0,#0] - stp x21, x22, [x0,#16] - stp x23, x24, [x0,#32] - stp x25, x26, [x0,#48] - stp x27, x28, [x0,#64] - stp x29, x30, [x0,#80] - mov x2, sp - str x2, [x0,#104] - stp d8, d9, [x0,#112] - stp d10, d11, [x0,#128] - stp d12, d13, [x0,#144] - stp d14, d15, [x0,#160] - mov x0, #0 - ret", - options(noreturn), - ); - #[cfg(all(target_arch = "aarch64", not(feature = "fp_simd")))] - core::arch::asm!( - " - stp x19, x20, [x0,#0] - stp x21, x22, [x0,#16] - stp x23, x24, [x0,#32] - stp x25, x26, [x0,#48] - stp x27, x28, [x0,#64] - stp x29, x30, [x0,#80] - mov x2, sp - str x2, [x0,#104] - mov x0, #0 - ret", - options(noreturn), - ); - #[cfg(target_arch = "x86_64")] - core::arch::asm!( - "mov [rdi], rbx - mov [rdi + 8], rbp - mov [rdi + 16], r12 - mov [rdi + 24], r13 - mov [rdi + 32], r14 - mov [rdi + 40], r15 - lea rdx, [rsp + 8] - mov [rdi + 48], rdx - mov rdx, [rsp] - mov [rdi + 56], rdx - xor rax, rax - ret", - options(noreturn), - ); - #[cfg(all(target_arch = "riscv64", feature = "fp_simd"))] - core::arch::asm!( - "sd s0, 0(a0) - sd s1, 8(a0) - sd s2, 16(a0) - sd s3, 24(a0) - sd s4, 32(a0) - sd s5, 40(a0) - sd s6, 48(a0) - sd s7, 56(a0) - sd s8, 64(a0) - sd s9, 72(a0) - sd s10, 80(a0) - sd s11, 88(a0) - sd sp, 96(a0) - sd ra, 104(a0) - - fsd fs0, 112(a0) - fsd fs1, 120(a0) - fsd fs2, 128(a0) - fsd fs3, 136(a0) - fsd fs4, 144(a0) - fsd fs5, 152(a0) - fsd fs6, 160(a0) - fsd fs7, 168(a0) - fsd fs8, 176(a0) - fsd fs9, 184(a0) - fsd fs10, 192(a0) - fsd fs11, 200(a0) - - li a0, 0 - ret", - options(noreturn), - ); - #[cfg(all(target_arch = "riscv64", not(feature = "fp_simd")))] - core::arch::asm!( - "sd s0, 0(a0) - sd s1, 8(a0) - sd s2, 16(a0) - sd s3, 24(a0) - sd s4, 32(a0) - sd s5, 40(a0) - sd s6, 48(a0) - sd s7, 56(a0) - sd s8, 64(a0) - sd s9, 72(a0) - sd s10, 80(a0) - sd s11, 88(a0) - sd sp, 96(a0) - sd ra, 104(a0) - - li a0, 0 - ret", - options(noreturn), - ); - #[cfg(not(any( - target_arch = "aarch64", - target_arch = "x86_64", - target_arch = "riscv64" - )))] - core::arch::asm!("ret", options(noreturn)) -} - -/// `longjmp` implementation -#[naked] -#[no_mangle] -pub unsafe extern "C" fn longjmp(_buf: *mut ctypes::__jmp_buf_tag, _val: c_int) -> ! { - #[cfg(all(target_arch = "aarch64", feature = "fp_simd"))] - core::arch::asm!( - "ldp x19, x20, [x0,#0] - ldp x21, x22, [x0,#16] - ldp x23, x24, [x0,#32] - ldp x25, x26, [x0,#48] - ldp x27, x28, [x0,#64] - ldp x29, x30, [x0,#80] - ldr x2, [x0,#104] - mov sp, x2 - ldp d8 , d9, [x0,#112] - ldp d10, d11, [x0,#128] - ldp d12, d13, [x0,#144] - ldp d14, d15, [x0,#160] - - cmp w1, 0 - csinc w0, w1, wzr, ne - br x30", - options(noreturn), - ); - #[cfg(all(target_arch = "aarch64", not(feature = "fp_simd")))] - core::arch::asm!( - "ldp x19, x20, [x0,#0] - ldp x21, x22, [x0,#16] - ldp x23, x24, [x0,#32] - ldp x25, x26, [x0,#48] - ldp x27, x28, [x0,#64] - ldp x29, x30, [x0,#80] - ldr x2, [x0,#104] - mov sp, x2 - - cmp w1, 0 - csinc w0, w1, wzr, ne - br x30", - options(noreturn), - ); - #[cfg(target_arch = "x86_64")] - core::arch::asm!( - "mov rax,rsi - test rax,rax - jnz 1f - inc rax - 1: - mov rbx, [rdi] - mov rbp, [rdi + 8] - mov r12, [rdi + 16] - mov r13, [rdi + 24] - mov r14, [rdi + 32] - mov r15, [rdi + 40] - mov rdx, [rdi + 48] - mov rsp, rdx - mov rdx, [rdi + 56] - jmp rdx", - options(noreturn), - ); - #[cfg(all(target_arch = "riscv64", feature = "fp_simd"))] - core::arch::asm!( - "ld s0, 0(a0) - ld s1, 8(a0) - ld s2, 16(a0) - ld s3, 24(a0) - ld s4, 32(a0) - ld s5, 40(a0) - ld s6, 48(a0) - ld s7, 56(a0) - ld s8, 64(a0) - ld s9, 72(a0) - ld s10, 80(a0) - ld s11, 88(a0) - ld sp, 96(a0) - ld ra, 104(a0) - - fld fs0, 112(a0) - fld fs1, 120(a0) - fld fs2, 128(a0) - fld fs3, 136(a0) - fld fs4, 144(a0) - fld fs5, 152(a0) - fld fs6, 160(a0) - fld fs7, 168(a0) - fld fs8, 176(a0) - fld fs9, 184(a0) - fld fs10, 192(a0) - fld fs11, 200(a0) - - seqz a0, a1 - add a0, a0, a1 - ret", - options(noreturn), - ); - #[cfg(all(target_arch = "riscv64", not(feature = "fp_simd")))] - core::arch::asm!( - "ld s0, 0(a0) - ld s1, 8(a0) - ld s2, 16(a0) - ld s3, 24(a0) - ld s4, 32(a0) - ld s5, 40(a0) - ld s6, 48(a0) - ld s7, 56(a0) - ld s8, 64(a0) - ld s9, 72(a0) - ld s10, 80(a0) - ld s11, 88(a0) - ld sp, 96(a0) - ld ra, 104(a0) - - seqz a0, a1 - add a0, a0, a1 - ret", - options(noreturn), - ); -} diff --git a/ulib/ruxlibc/src/signal.rs b/ulib/ruxlibc/src/signal.rs deleted file mode 100644 index 4b3aec0bc..000000000 --- a/ulib/ruxlibc/src/signal.rs +++ /dev/null @@ -1,79 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; - -#[cfg(feature = "signal")] -use crate::ctypes::k_sigaction; -use crate::ctypes::{sigaction, EINVAL, SIGKILL, SIGSTOP}; -#[cfg(feature = "signal")] -use ruxos_posix_api::sys_sigaction; - -#[cfg(feature = "signal")] -unsafe extern "C" fn ignore_handler(_: c_int) {} - -#[no_mangle] -pub unsafe extern "C" fn sigaction_inner( - signum: c_int, - _act: *const sigaction, - oldact: *mut sigaction, -) -> c_int { - if signum >= 32 || signum == SIGKILL as _ || signum == SIGSTOP as _ { - return -(EINVAL as c_int); - } - #[cfg(feature = "signal")] - { - let k_act = { - if _act.is_null() { - None - } else { - let mut sh = (*_act).__sa_handler.sa_handler; - if let Some(h) = sh { - if h as usize == crate::ctypes::SIGIGN as usize { - sh = Some(ignore_handler as unsafe extern "C" fn(c_int)); - } - } - k_sigaction { - handler: sh, - flags: (*_act).sa_flags as _, - restorer: (*_act).sa_restorer, - mask: Default::default(), - } - } - }; - let mut k_oldact = k_sigaction::default(); - sys_sigaction( - signum as u8, - Some(&k_act), - if oldact.is_null() { - None - } else { - Some(&mut k_oldact) - }, - ); - if !oldact.is_null() { - (*oldact).__sa_handler.sa_handler = k_oldact.handler; - (*oldact).sa_flags = k_oldact.flags as _; - (*oldact).sa_restorer = k_oldact.restorer; - // Not support mask - // (*oldact).sa_mask = k_oldact.mask; - } - } - #[cfg(not(feature = "signal"))] - { - if !oldact.is_null() { - // set to 0 - (*oldact).__sa_handler.sa_handler = None; - (*oldact).sa_flags = 0; - (*oldact).sa_restorer = None; - (*oldact).sa_mask = Default::default(); - } - } - 0 -} diff --git a/ulib/ruxlibc/src/strftime.rs b/ulib/ruxlibc/src/strftime.rs deleted file mode 100644 index c839b5edf..000000000 --- a/ulib/ruxlibc/src/strftime.rs +++ /dev/null @@ -1,263 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use alloc::string::String; -use core::{ffi::c_char, fmt}; - -use axio::Write; - -use crate::ctypes; - -pub trait WriteByte: fmt::Write { - fn write_u8(&mut self, byte: u8) -> fmt::Result; -} - -struct StringWriter(pub *mut u8, pub usize); - -impl Write for StringWriter { - fn write(&mut self, buf: &[u8]) -> axerrno::AxResult { - if self.1 > 1 { - let copy_size = buf.len().min(self.1 - 1); - unsafe { - core::ptr::copy_nonoverlapping(buf.as_ptr(), self.0, copy_size); - self.1 -= copy_size; - - self.0 = self.0.add(copy_size); - *self.0 = 0; - } - } - Ok(buf.len()) - } - fn flush(&mut self) -> axerrno::AxResult { - Ok(()) - } -} - -impl fmt::Write for StringWriter { - fn write_str(&mut self, s: &str) -> fmt::Result { - // can't fail - self.write(s.as_bytes()).unwrap(); - Ok(()) - } -} - -impl WriteByte for StringWriter { - fn write_u8(&mut self, byte: u8) -> fmt::Result { - // can't fail - self.write(&[byte]).unwrap(); - Ok(()) - } -} - -struct CountingWriter { - pub inner: T, - pub written: usize, -} - -impl CountingWriter { - pub fn new(writer: T) -> Self { - Self { - inner: writer, - written: 0, - } - } -} - -impl fmt::Write for CountingWriter { - fn write_str(&mut self, s: &str) -> fmt::Result { - self.written += s.len(); - self.inner.write_str(s) - } -} - -impl WriteByte for CountingWriter { - fn write_u8(&mut self, byte: u8) -> fmt::Result { - self.written += 1; - self.inner.write_u8(byte) - } -} - -impl Write for CountingWriter { - fn write(&mut self, buf: &[u8]) -> axerrno::AxResult { - let res = self.inner.write(buf); - if let Ok(written) = res { - self.written += written; - } - res - } - - fn write_all(&mut self, buf: &[u8]) -> axerrno::AxResult { - match self.inner.write_all(buf) { - Ok(()) => (), - Err(err) => return Err(err), - } - self.written += buf.len(); - Ok(()) - } - - fn flush(&mut self) -> axerrno::AxResult { - self.inner.flush() - } -} - -unsafe fn strftime_inner( - w: W, - format: *const c_char, - t: *const ctypes::tm, -) -> ctypes::size_t { - pub unsafe fn inner_strftime( - w: &mut W, - mut format: *const c_char, - t: *const ctypes::tm, - ) -> bool { - macro_rules! w { - (byte $b:expr) => {{ - if w.write_u8($b).is_err() { - return false; - } - }}; - (char $chr:expr) => {{ - if w.write_char($chr).is_err() { - return false; - } - }}; - (recurse $fmt:expr) => {{ - let mut fmt = String::with_capacity($fmt.len() + 1); - fmt.push_str($fmt); - fmt.push('\0'); - - if !inner_strftime(w, fmt.as_ptr() as *mut c_char, t) { - return false; - } - }}; - ($str:expr) => {{ - if w.write_str($str).is_err() { - return false; - } - }}; - ($fmt:expr, $($args:expr),+) => {{ - // Would use write!() if I could get the length written - if write!(w, $fmt, $($args),+).is_err() { - return false; - } - }}; - } - const WDAYS: [&str; 7] = [ - "Sunday", - "Monday", - "Tuesday", - "Wednesday", - "Thursday", - "Friday", - "Saturday", - ]; - const MONTHS: [&str; 12] = [ - "January", - "Febuary", - "March", - "April", - "May", - "June", - "July", - "August", - "September", - "October", - "November", - "December", - ]; - - while *format != 0 { - if *format as u8 != b'%' { - w!(byte * format as u8); - format = format.offset(1); - continue; - } - - format = format.offset(1); - - if *format as u8 == b'E' || *format as u8 == b'O' { - // Ignore because these do nothing without locale - format = format.offset(1); - } - - match *format as u8 { - b'%' => w!(byte b'%'), - b'n' => w!(byte b'\n'), - b't' => w!(byte b'\t'), - b'a' => w!(&WDAYS[(*t).tm_wday as usize][..3]), - b'A' => w!(WDAYS[(*t).tm_wday as usize]), - b'b' | b'h' => w!(&MONTHS[(*t).tm_mon as usize][..3]), - b'B' => w!(MONTHS[(*t).tm_mon as usize]), - b'C' => { - let mut year = (*t).tm_year / 100; - // Round up - if (*t).tm_year % 100 != 0 { - year += 1; - } - w!("{:02}", year + 19); - } - b'd' => w!("{:02}", (*t).tm_mday), - b'D' => w!(recurse "%m/%d/%y"), - b'e' => w!("{:2}", (*t).tm_mday), - b'F' => w!(recurse "%Y-%m-%d"), - b'H' => w!("{:02}", (*t).tm_hour), - b'I' => w!("{:02}", ((*t).tm_hour + 12 - 1) % 12 + 1), - b'j' => w!("{:03}", (*t).tm_yday), - b'k' => w!("{:2}", (*t).tm_hour), - b'l' => w!("{:2}", ((*t).tm_hour + 12 - 1) % 12 + 1), - b'm' => w!("{:02}", (*t).tm_mon + 1), - b'M' => w!("{:02}", (*t).tm_min), - b'p' => w!(if (*t).tm_hour < 12 { "AM" } else { "PM" }), - b'P' => w!(if (*t).tm_hour < 12 { "am" } else { "pm" }), - b'r' => w!(recurse "%I:%M:%S %p"), - b'R' => w!(recurse "%H:%M"), - // Nothing is modified in mktime, but the C standard of course requires a mutable pointer ._. - b's' => w!("{}", super::mktime(t as *mut ctypes::tm)), - b'S' => w!("{:02}", (*t).tm_sec), - b'T' => w!(recurse "%H:%M:%S"), - b'u' => w!("{}", ((*t).tm_wday + 7 - 1) % 7 + 1), - b'U' => w!("{}", ((*t).tm_yday + 7 - (*t).tm_wday) / 7), - b'w' => w!("{}", (*t).tm_wday), - b'W' => w!("{}", ((*t).tm_yday + 7 - ((*t).tm_wday + 6) % 7) / 7), - b'y' => w!("{:02}", (*t).tm_year % 100), - b'Y' => w!("{}", (*t).tm_year + 1900), - b'z' => w!("+0000"), // TODO - b'Z' => w!("UTC"), // TODO - b'+' => w!(recurse "%a %b %d %T %Z %Y"), - _ => return false, - } - - format = format.offset(1); - } - true - } - - let mut w: CountingWriter = CountingWriter::new(w); - if !inner_strftime(&mut w, format, t) { - return 0; - } - - w.written -} - -/// Convert date and time to a string. -#[no_mangle] -pub unsafe extern "C" fn strftime( - buf: *mut c_char, - size: ctypes::size_t, - format: *const c_char, - timeptr: *const ctypes::tm, -) -> ctypes::size_t { - let ret = strftime_inner(StringWriter(buf as *mut u8, size), format, timeptr); - if ret < size { - ret - } else { - 0 - } -} diff --git a/ulib/ruxlibc/src/string.rs b/ulib/ruxlibc/src/string.rs deleted file mode 100644 index 5cbbc7740..000000000 --- a/ulib/ruxlibc/src/string.rs +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::ctypes; -use core::ffi::c_char; -/// calculate the length of a string, excluding the terminating null byte -#[no_mangle] -pub unsafe extern "C" fn strlen(s: *const c_char) -> ctypes::size_t { - strnlen(s, ctypes::size_t::MAX) -} - -/// calculate the length of a string like strlen, but at most maxlen. -#[no_mangle] -pub unsafe extern "C" fn strnlen(s: *const c_char, size: ctypes::size_t) -> ctypes::size_t { - let mut i = 0; - while i < size { - if *s.add(i) == 0 { - break; - } - i += 1; - } - i -} diff --git a/ulib/ruxlibc/src/strtod.rs b/ulib/ruxlibc/src/strtod.rs deleted file mode 100644 index 2e1c3db41..000000000 --- a/ulib/ruxlibc/src/strtod.rs +++ /dev/null @@ -1,140 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::{c_char, c_double, c_float, c_int}; - -macro_rules! strto_float_impl { - ($type:ident, $s:expr, $endptr:expr) => {{ - let mut s = $s; - let endptr = $endptr; - - // TODO: Handle named floats: NaN, Inf... - - while isspace(*s as c_int) { - s = s.offset(1); - } - - let mut result: $type = 0.0; - let mut radix = 10; - - let result_sign = match *s as u8 { - b'-' => { - s = s.offset(1); - -1.0 - } - b'+' => { - s = s.offset(1); - 1.0 - } - _ => 1.0, - }; - - if *s as u8 == b'0' && *s.offset(1) as u8 == b'x' { - s = s.offset(2); - radix = 16; - } - - while let Some(digit) = (*s as u8 as char).to_digit(radix) { - result *= radix as $type; - result += digit as $type; - s = s.offset(1); - } - - if *s as u8 == b'.' { - s = s.offset(1); - - let mut i = 1.0; - while let Some(digit) = (*s as u8 as char).to_digit(radix) { - i *= radix as $type; - result += digit as $type / i; - s = s.offset(1); - } - } - - let s_before_exponent = s; - - let exponent = match (*s as u8, radix) { - (b'e' | b'E', 10) | (b'p' | b'P', 16) => { - s = s.offset(1); - - let is_exponent_positive = match *s as u8 { - b'-' => { - s = s.offset(1); - false - } - b'+' => { - s = s.offset(1); - true - } - _ => true, - }; - - // Exponent digits are always in base 10. - if (*s as u8 as char).is_digit(10) { - let mut exponent_value = 0; - - while let Some(digit) = (*s as u8 as char).to_digit(10) { - exponent_value *= 10; - exponent_value += digit; - s = s.offset(1); - } - - let exponent_base = match radix { - 10 => 10u128, - 16 => 2u128, - _ => unreachable!(), - }; - - if is_exponent_positive { - Some(exponent_base.pow(exponent_value) as $type) - } else { - Some(1.0 / (exponent_base.pow(exponent_value) as $type)) - } - } else { - // Exponent had no valid digits after 'e'/'p' and '+'/'-', rollback - s = s_before_exponent; - None - } - } - _ => None, - }; - - // Return pointer should be *mut - if !endptr.is_null() { - *endptr = s as *mut _; - } - - if let Some(exponent) = exponent { - result_sign * result * exponent - } else { - result_sign * result - } - }}; -} - -fn isspace(c: c_int) -> bool { - c == c_int::from(b' ') - || c == c_int::from(b'\t') - || c == c_int::from(b'\n') - || c == c_int::from(b'\r') - || c == 0x0b - || c == 0x0c -} - -/// Convert a string to a double-precision number. -#[no_mangle] -pub unsafe extern "C" fn strtod(s: *const c_char, endptr: *mut *mut c_char) -> c_double { - strto_float_impl!(c_double, s, endptr) -} - -/// Convert a string to a float number. -#[no_mangle] -pub unsafe extern "C" fn strtof(s: *const c_char, endptr: *mut *mut c_char) -> c_float { - strto_float_impl!(c_float, s, endptr) -} diff --git a/ulib/ruxlibc/src/sys.rs b/ulib/ruxlibc/src/sys.rs deleted file mode 100644 index 1ec89e423..000000000 --- a/ulib/ruxlibc/src/sys.rs +++ /dev/null @@ -1,49 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::ctypes; -use core::ffi::{c_int, c_long}; -use ruxos_posix_api::{config, sys_getrlimit}; - -/// Return system configuration infomation -/// -/// Notice: currently only support what unikraft covers -#[no_mangle] -pub unsafe extern "C" fn sysconf(name: c_int) -> c_long { - match name as u32 { - // Maximum process number - ctypes::_SC_CHILD_MAX => { - let mut rl: ctypes::rlimit = core::mem::zeroed(); - sys_getrlimit(ctypes::RLIMIT_NPROC.try_into().unwrap(), &mut rl); - rl.rlim_max as c_long - } - // Page size - ctypes::_SC_PAGE_SIZE => config::PAGE_SIZE_4K as c_long, - // Total physical pages - ctypes::_SC_PHYS_PAGES => (config::PHYS_MEMORY_SIZE / config::PAGE_SIZE_4K) as c_long, - // Number of processors in use - ctypes::_SC_NPROCESSORS_ONLN => config::SMP as c_long, - // Avaliable physical pages - ctypes::_SC_AVPHYS_PAGES => { - let mut info: ctypes::sysinfo = core::mem::zeroed(); - ruxos_posix_api::sys_sysinfo(&mut info); - (info.freeram / config::PAGE_SIZE_4K as u64) as c_long - } - // Maximum number of files per process - #[cfg(feature = "fd")] - ctypes::_SC_OPEN_MAX => { - let mut rl: ctypes::rlimit = core::mem::zeroed(); - sys_getrlimit(ctypes::RLIMIT_NOFILE.try_into().unwrap(), &mut rl); - rl.rlim_max as c_long - } - // Maximum number of keys per thread - ctypes::_SC_THREAD_KEYS_MAX => config::PTHREAD_KEY_MAX as c_long, - _ => 0, - } -} diff --git a/ulib/ruxlibc/src/time.rs b/ulib/ruxlibc/src/time.rs deleted file mode 100644 index ff26e5ace..000000000 --- a/ulib/ruxlibc/src/time.rs +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; -use ruxos_posix_api::{sys_clock_gettime, sys_clock_nanosleep, sys_clock_settime, sys_nanosleep}; -#[cfg(feature = "signal")] -use ruxos_posix_api::{sys_getitimer, sys_setitimer}; - -use crate::{ctypes, utils::e}; - -/// Get clock time since booting -#[no_mangle] -pub unsafe extern "C" fn clock_gettime(clk: ctypes::clockid_t, ts: *mut ctypes::timespec) -> c_int { - e(sys_clock_gettime(clk, ts)) -} - -/// Set clock time since booting -#[no_mangle] -pub unsafe extern "C" fn clock_settime(clk: ctypes::clockid_t, ts: *mut ctypes::timespec) -> c_int { - e(sys_clock_settime(clk, ts)) -} - -/// Sleep until some nanoseconds -/// -/// TODO: should be woken by signals, and set errno -#[no_mangle] -pub unsafe extern "C" fn clock_nanosleep( - which_clock: ctypes::clockid_t, - flags: c_int, - req: *const ctypes::timespec, - rem: *mut ctypes::timespec, -) -> c_int { - e(sys_clock_nanosleep(which_clock, flags, req, rem)) -} - -/// Sleep some nanoseconds -/// -/// TODO: should be woken by signals, and set errno -#[no_mangle] -pub unsafe extern "C" fn nanosleep( - req: *const ctypes::timespec, - rem: *mut ctypes::timespec, -) -> c_int { - e(sys_nanosleep(req, rem)) -} - -/// Set timer to send signal after some time -#[no_mangle] -pub unsafe extern "C" fn setitimer( - _which: c_int, - _new: *const ctypes::itimerval, - _old: *mut ctypes::itimerval, -) -> c_int { - #[cfg(feature = "signal")] - { - if !_old.is_null() { - let res = e(sys_getitimer(_which, _old)); - if res != 0 { - return res; - } - } - e(sys_setitimer(_which, _new)) - } - #[cfg(not(feature = "signal"))] - { - e(0) - } -} - -/// Set timer to send signal after some time -#[no_mangle] -pub unsafe extern "C" fn getitimer(_which: c_int, _curr_value: *mut ctypes::itimerval) -> c_int { - #[cfg(feature = "signal")] - { - e(sys_getitimer(_which, _curr_value)) - } - #[cfg(not(feature = "signal"))] - { - e(0) - } -} diff --git a/ulib/ruxlibc/src/unistd.rs b/ulib/ruxlibc/src/unistd.rs deleted file mode 100644 index 51d53625f..000000000 --- a/ulib/ruxlibc/src/unistd.rs +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; -use ruxos_posix_api::{sys_exit, sys_getpid, sys_gettid}; -#[cfg(feature = "signal")] -use { - crate::getitimer, - crate::{ctypes, utils::e}, - core::ffi::c_uint, - ruxos_posix_api::sys_setitimer, -}; - -/// Get current thread ID. -#[no_mangle] -pub unsafe extern "C" fn getpid() -> c_int { - sys_getpid() -} - -/// Get current thread ID. -#[no_mangle] -pub unsafe extern "C" fn gettid() -> c_int { - sys_gettid() -} - -/// Abort the current process. -#[no_mangle] -pub unsafe extern "C" fn abort() -> ! { - panic!() -} - -/// Exits the current thread. -#[no_mangle] -pub unsafe extern "C" fn exit(exit_code: c_int) -> ! { - sys_exit(exit_code) -} - -/// Set an alarm clock for delivery of a signal -#[cfg(feature = "signal")] -#[no_mangle] -pub unsafe extern "C" fn alarm(seconds: c_uint) -> c_uint { - let it = ctypes::itimerval { - it_interval: ctypes::timeval { - tv_sec: 0, - tv_usec: 0, - }, - it_value: ctypes::timeval { - tv_sec: seconds as i64, - tv_usec: 0, - }, - }; - let mut old = ctypes::itimerval::default(); - if getitimer(ctypes::ITIMER_REAL as c_int, &mut old) < 0 { - e(sys_setitimer(ctypes::ITIMER_REAL as c_int, &it)) as c_uint - } else { - old.it_value.tv_sec as c_uint - } -} - -/// Schedule signal after given number of microseconds -#[cfg(feature = "signal")] -#[no_mangle] -pub unsafe extern "C" fn ualarm(useconds: c_uint, interval: c_uint) -> c_uint { - let it = ctypes::itimerval { - it_interval: ctypes::timeval { - tv_sec: 0, - tv_usec: interval as i64, - }, - it_value: ctypes::timeval { - tv_sec: 0, - tv_usec: useconds as i64, - }, - }; - let mut old = ctypes::itimerval::default(); - if getitimer(ctypes::ITIMER_REAL as i32, &mut old) < 0 { - e(sys_setitimer(ctypes::ITIMER_REAL as i32, &it)); - 0 - } else { - core::time::Duration::from(old.it_value).as_micros() as c_uint - } -} diff --git a/ulib/ruxmusl/src/aarch64/mod.rs b/ulib/ruxmusl/src/aarch64/mod.rs index 51fc08f11..b8553a6ef 100644 --- a/ulib/ruxmusl/src/aarch64/mod.rs +++ b/ulib/ruxmusl/src/aarch64/mod.rs @@ -1,3 +1,5 @@ +#![allow(unused_imports)] + pub mod syscall_id; use core::ffi::{c_char, c_int}; @@ -201,6 +203,7 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize { SyscallId::EXIT => { ruxos_posix_api::sys_pthread_exit(args[0] as *mut core::ffi::c_void) as _ } + #[cfg(feature = "multitask")] SyscallId::EXIT_GROUP => ruxos_posix_api::sys_exit_group(args[0] as c_int), #[cfg(feature = "multitask")] SyscallId::SET_TID_ADDRESS => ruxos_posix_api::sys_set_tid_address(args[0]) as _, @@ -415,6 +418,7 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize { args[1] as ctypes::size_t, args[2] as c_int, ) as _, + #[cfg(feature = "multitask")] SyscallId::WAIT4 => ruxos_posix_api::sys_wait4( args[0] as ctypes::pid_t, args[1] as *mut c_int, diff --git a/ulib/ruxmusl/src/aarch64/syscall_id.rs b/ulib/ruxmusl/src/aarch64/syscall_id.rs index b9b9b18dc..237cf88c2 100644 --- a/ulib/ruxmusl/src/aarch64/syscall_id.rs +++ b/ulib/ruxmusl/src/aarch64/syscall_id.rs @@ -73,6 +73,7 @@ pub enum SyscallId { FDATASYNC = 83, CAP_GET = 90, EXIT = 93, + #[cfg(feature = "multitask")] EXIT_GROUP = 94, #[cfg(feature = "multitask")] SET_TID_ADDRESS = 96, @@ -153,6 +154,7 @@ pub enum SyscallId { MSYNC = 227, #[cfg(feature = "alloc")] MADVISE = 233, + #[cfg(feature = "multitask")] WAIT4 = 260, PRLIMIT64 = 261, GETRANDOM = 278, diff --git a/ulib/ruxmusl/src/trap.rs b/ulib/ruxmusl/src/trap.rs index 17fb9aecd..c0cf757f4 100644 --- a/ulib/ruxmusl/src/trap.rs +++ b/ulib/ruxmusl/src/trap.rs @@ -21,6 +21,9 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl { #[cfg(feature = "musl")] fn handle_syscall(syscall_id: usize, args: [usize; 6]) -> isize { let id = SyscallId::try_from(syscall_id).unwrap_or(SyscallId::INVALID); + if id == SyscallId::INVALID { + info!("Invalid syscall id: {}", syscall_id); + } crate::syscall(id, args) } } From c6c37ecbe633cdee96b0d328c2eac6506a6c4979 Mon Sep 17 00:00:00 2001 From: ZhengWu Date: Wed, 15 Jan 2025 07:32:43 +0000 Subject: [PATCH 15/15] fix a mistask for swaped-oout memory --- api/ruxos_posix_api/src/imp/mmap/utils.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/ruxos_posix_api/src/imp/mmap/utils.rs b/api/ruxos_posix_api/src/imp/mmap/utils.rs index 89ee4a9a2..a34200fc2 100644 --- a/api/ruxos_posix_api/src/imp/mmap/utils.rs +++ b/api/ruxos_posix_api/src/imp/mmap/utils.rs @@ -229,10 +229,12 @@ pub(crate) fn release_pages_mapped(start: usize, end: usize) { #[cfg(feature = "fs")] pub(crate) fn release_pages_swaped(start: usize, end: usize) { let mut swap_map = SWAPED_MAP.lock(); + let mut off_pool = BITMAP_FREE.lock(); let mut removing_vaddr = Vec::new(); - for (&vaddr, _) in swap_map.range(start..end) { + for (&vaddr, &ref swap_info) in swap_map.range(start..end) { removing_vaddr.push(vaddr); + off_pool.push(swap_info.offset); } for vaddr in removing_vaddr { swap_map.remove(&vaddr);