Skip to content

Commit

Permalink
revise codes
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayol committed May 11, 2024
1 parent 46d971e commit cb0d48a
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 72 deletions.
1 change: 1 addition & 0 deletions api/ruxos_posix_api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ typedef struct {{
"PROT_.+",
"MS_.+",
"MREMAP_.+",
"GRND_.*",
];

#[derive(Debug)]
Expand Down
2 changes: 2 additions & 0 deletions api/ruxos_posix_api/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@
#include <sys/uio.h>
#include <unistd.h>
#include <dirent.h>

#include <sys/random.h>
9 changes: 4 additions & 5 deletions api/ruxos_posix_api/src/imp/getrandom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,10 @@ pub unsafe extern "C" fn sys_getrandom(buf: *mut c_void, buflen: size_t, flags:
if buf.is_null() {
return Err(LinuxError::EFAULT);
}
const GRND_NONBLOCK: c_int = 1;
const GRND_RANDOM: c_int = 2;
match flags {
GRND_NONBLOCK => {}
GRND_RANDOM => {}

match flags as _ {
crate::ctypes::GRND_NONBLOCK => {}
crate::ctypes::GRND_RANDOM => {}
_ => return Err(LinuxError::EINVAL),
}
// fill the buffer 8 bytes at a time first, then fill the remaining bytes
Expand Down
46 changes: 1 addition & 45 deletions api/ruxos_posix_api/src/imp/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use crate::{imp::fd_ops::get_file_like, sys_getpgid};
use axerrno::LinuxError;
use core::ffi::{c_int, c_uchar, c_uint};
use core::ffi::c_int;

/// IOCTL oprations
pub const TCGETS: usize = 0x5401;
Expand All @@ -27,23 +27,6 @@ pub struct ConsoleWinSize {
pub ws_ypixel: u16,
}

const NCCS: usize = 19;
const ICANON: usize = 2;
const ECHO: usize = 8;

#[derive(Debug)]
#[repr(C)]
struct Termios {
c_iflag: c_uint, /* input mode flags */
c_oflag: c_uint, /* output mode flags */
c_cflag: c_uint, /* control mode flags */
c_lflag: c_uint, /* local mode flags */
c_line: c_uchar, /* line discipline */
c_cc: [c_uchar; NCCS], /* control characters */
c_ispeed: c_uint, /* input speed */
c_ospeed: c_uint, /* output speed */
}

/// ioctl implementation,
/// currently only support fd = 1
pub fn sys_ioctl(fd: c_int, request: usize, data: usize) -> c_int {
Expand All @@ -65,33 +48,6 @@ pub fn sys_ioctl(fd: c_int, request: usize, data: usize) -> c_int {
}
TCGETS => {
debug!("sys_ioctl: tty TCGETS");
let data = data as *const u8 as *mut Termios;
unsafe {
const ICRNL: usize = 0x100;
(*data).c_iflag = ICRNL as _; // ICRNL | IXON

const ISIG: usize = 1;
const ECHOE: usize = 0x10;
const ECHOK: usize = 0x20;
const ECHOCTL: usize = 0x200;
const ECHOKE: usize = 0x800;
const IEXTEN: usize = 0x8000;
(*data).c_lflag =
(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN) as _;

const OPOST: usize = 1;
const ONLCR: usize = 8;
(*data).c_oflag = (OPOST | ONLCR) as _;

// .c_cflag = B38400 | CS8 | CREAD | HUPCL,

// .c_cc = INIT_C_CC,
(*data).c_cc[2] = 0o177;

(*data).c_ispeed = 38400;
(*data).c_ospeed = 38400;
}

Ok(0)
}
TIOCSPGRP => {
Expand Down
24 changes: 24 additions & 0 deletions apps/c/busybox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# busybox

## Quick Start

1. Compile `busybox` or get its ELF binary (using Musl), then copy to `rootfs/bin`.

2. Copy the Musl dyanmic linker to `rootfs/lib`.

3. modify `axbuild.mk`, like:

```makefile
app-objs=main.o

ARGS = /bin/busybox,ls
ENVS =
V9P_PATH=${APP}/rootfs
```

4. Run

```sh
# in the RuxOS main directory.
make run ARCH=aarch64 A=apps/c/busybox V9P=y MUSL=y
```
3 changes: 1 addition & 2 deletions apps/c/busybox/axbuild.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ app-objs=main.o

ARGS = /bin/busybox,sh
ENVS =
V9P_PATH=${APP}/rootfs
# make run ARCH=aarch64 A=apps/c/busybox V9P=y MUSL=y LOG=debug
V9P_PATH=${APP}/rootfs
Binary file removed apps/c/busybox/busybox
Binary file not shown.
9 changes: 9 additions & 0 deletions crates/tty/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/* 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.
*/

//! functions for tty buffer.
//! Drivers should fill the buffer by functions below.
//! then the data will be passed to line discipline for processing.
Expand Down
9 changes: 9 additions & 0 deletions crates/tty/src/constant.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/* 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.
*/

pub const LF: u8 = b'\n';
pub const CR: u8 = b'\r';

Expand Down
34 changes: 21 additions & 13 deletions crates/tty/src/driver.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
/* 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.
*/

//! the first thing a driver should do is registering itself by `register_driver()`,
//! which will allocate an index for this driver.
//!
//! then, driver should register every device it has by `register_device()`,
//! which will allocate an index for this device.
use crate::tty::TtyStruct;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::{vec, vec::Vec};
Expand All @@ -31,7 +41,7 @@ pub struct TtyDriver {

/// driver's devices.
/// TODO: maybe use rwlock for dynamicly adding devices is better.
ttys: SpinNoIrq<Vec<Arc<TtyStruct>>>,
ttys: SpinNoIrq<BTreeMap<usize, Arc<TtyStruct>>>,

/// index of driver.
index: usize,
Expand All @@ -44,15 +54,18 @@ impl TtyDriver {
pub fn new(ops: TtyDriverOps, name: &str) -> Self {
Self {
ops,
ttys: SpinNoIrq::new(vec![]),
ttys: SpinNoIrq::new(BTreeMap::new()),
index: 0,
name: String::from(name),
}
}

/// add a device, return its index, -1 means failure.
fn add_one_device(&self, tty: Arc<TtyStruct>) -> isize {
let index = self.ttys.lock().len();
let mut index = 0;
if let Some(k) = self.ttys.lock().last_key_value() {
index = *k.0;
}

// set index of device
tty.set_index(index);
Expand All @@ -63,7 +76,7 @@ impl TtyDriver {
tty.set_name(&name);

// save this device
self.ttys.lock().push(tty);
self.ttys.lock().insert(index, tty.clone());

// return device's index
index as _
Expand All @@ -80,16 +93,15 @@ impl TtyDriver {
/// get all devices' name
pub fn get_all_device_names(&self) -> Vec<String> {
let mut ret = vec![];
for dev in self.ttys.lock().iter() {
let name = dev.name();
ret.push(name);
for (_, tty) in self.ttys.lock().iter() {
ret.push(tty.name());
}
ret
}

/// get device
pub fn get_device_by_name(&self, name: &str) -> Option<Arc<TtyStruct>> {
for tty in self.ttys.lock().iter() {
for (_, tty) in self.ttys.lock().iter() {
if tty.name() == name {
return Some(tty.clone());
}
Expand All @@ -99,11 +111,7 @@ impl TtyDriver {

/// get device
pub fn get_device_by_index(&self, index: usize) -> Option<Arc<TtyStruct>> {
let lock = self.ttys.lock();
if let Some(dev) = lock.get(index) {
return Some(dev.clone());
}
None
self.ttys.lock().get(&index).cloned()
}
}

Expand Down
9 changes: 9 additions & 0 deletions crates/tty/src/ldisc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/* 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.
*/

//! TTY line discipline process all incoming and outgoing chars from/to a tty device.
//! the currently implemented line discipline is N_TTY.
//! line disciplines are registered when a device is registered.
Expand Down
9 changes: 9 additions & 0 deletions crates/tty/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/* 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.
*/

//! Init
//!
//! firstly, a driver registers itself to get its index.
Expand Down
9 changes: 9 additions & 0 deletions crates/tty/src/tty.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/* 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::sync::atomic::AtomicUsize;

use alloc::{string::String, sync::Arc, vec, vec::Vec};
Expand Down
5 changes: 2 additions & 3 deletions modules/ruxhal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,11 @@ pub fn get_all_device_names() -> alloc::vec::Vec<alloc::string::String> {
}
}

#[allow(unused)]
/// write a tty device specified by its name.
pub fn tty_write(buf: &[u8], dev_name: &str) -> usize {
pub fn tty_write(buf: &[u8], _dev_name: &str) -> usize {
#[cfg(feature = "tty")]
{
tty::tty_write(buf, dev_name)
tty::tty_write(buf, _dev_name)
}
#[cfg(not(feature = "tty"))]
{
Expand Down
5 changes: 1 addition & 4 deletions modules/ruxhal/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ pub(crate) fn default_free_regions() -> impl Iterator<Item = MemRegion> {
core::iter::once(MemRegion {
paddr: start,
size: end.as_usize() - start.as_usize(),
flags: MemRegionFlags::FREE
| MemRegionFlags::READ
| MemRegionFlags::WRITE
| MemRegionFlags::EXECUTE,
flags: MemRegionFlags::FREE | MemRegionFlags::READ | MemRegionFlags::WRITE,
name: "free memory",
})
}
Expand Down

0 comments on commit cb0d48a

Please sign in to comment.