Skip to content

Commit a5b3985

Browse files
committed
Start working on libc
1 parent 30a85ee commit a5b3985

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+2462
-121
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ virtio-net.pcap
1616
/userland/tmp
1717

1818
**/*.log
19+
**/target/**
20+
**/sysroot/**

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
path = extern/limine
33
url = https://github.com/limine-bootloader/limine.git
44
branch = v2.0-branch
5+
[submodule "userland/libc/core_io"]
6+
path = userland/libc/core_io
7+
url = https://gitlab.redox-os.org/redox-os/core_io.git

Justfile

+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ build init release:
4141
just build {{init}} circinus
4242
popd
4343
44+
just build_kern {{init}} {{release}}
45+
46+
build_kern init release:
47+
#!/usr/bin/env bash
48+
set -e
49+
export INIT_FILE={{init}}
50+
if { [ release != "debug" ] && [ release != "release" ] ;} then \
51+
echo Unknown build mode \"{{release}}\";\
52+
exit 1; \
53+
fi;
54+
4455
cargo -Z build-std=core,alloc -Z build-std-features=compiler-builtins-mem build --target kernel/arch/x64/x64.json {{ if release == "debug" { "" } else { "--release" } }}
4556
cp target/{{target}}/{{release}}/kernel build/kernel.elf
4657
@@ -106,3 +117,6 @@ run_gdb init="init" release="debug": (build init release) (image img)
106117
107118
kvm init="init" release="debug": (build init release) (image img)
108119
sudo qemu-system-{{qemutarget}} -enable-kvm -cpu host {{qemu-args}}
120+
121+
run_file file release="debug": (build_kern file release) (image img)
122+
qemu-system-{{qemutarget}} -cpu Haswell {{qemu-args}}

environment/address.rs

+8-26
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ impl VAddr {
7575
}
7676

7777
pub const fn is_accessible_from_kernel(addr: usize) -> bool {
78-
(addr) >= KERNEL_BASE_ADDR
79-
&& (addr) < KERNEL_BASE_ADDR + KERNEL_STRAIGHT_MAP_PADDR_END
78+
(addr) >= KERNEL_BASE_ADDR && (addr) < KERNEL_BASE_ADDR + KERNEL_STRAIGHT_MAP_PADDR_END
8079
}
8180

8281
pub const fn as_ptr<T>(self) -> *const T {
@@ -140,8 +139,7 @@ impl fmt::Display for VAddr {
140139

141140
extern "C" {
142141
fn copy_from_user(dst: *mut u8, src: *const u8, len: usize);
143-
fn strncpy_from_user(dst: *mut u8, src: *const u8, max_len: usize)
144-
-> usize;
142+
fn strncpy_from_user(dst: *mut u8, src: *const u8, max_len: usize) -> usize;
145143
fn copy_to_user(dst: *mut u8, src: *const u8, len: usize);
146144
fn memset_user(dst: *mut u8, value: u8, len: usize);
147145
}
@@ -177,9 +175,7 @@ impl UserVAddr {
177175
}
178176
}
179177

180-
pub const fn new_nonnull(
181-
addr: usize,
182-
) -> Result<Self, NullUserPointerError> {
178+
pub const fn new_nonnull(addr: usize) -> Result<Self, NullUserPointerError> {
183179
match Self::new(addr) {
184180
Some(uaddr) => Ok(uaddr),
185181
None => Err(NullUserPointerError),
@@ -227,10 +223,7 @@ impl UserVAddr {
227223
pub fn read<T>(self) -> Result<T, AccessError> {
228224
let mut buf: MaybeUninit<T> = MaybeUninit::uninit();
229225
self.read_bytes(unsafe {
230-
slice::from_raw_parts_mut(
231-
buf.as_mut_ptr() as *mut u8,
232-
size_of::<T>(),
233-
)
226+
slice::from_raw_parts_mut(buf.as_mut_ptr() as *mut u8, size_of::<T>())
234227
})?;
235228
Ok(unsafe { buf.assume_init() })
236229
}
@@ -239,11 +232,7 @@ impl UserVAddr {
239232
call_usercopy_hook();
240233
self.access_ok(buf.len())?;
241234
unsafe {
242-
copy_from_user(
243-
buf.as_mut_ptr(),
244-
self.value() as *const u8,
245-
buf.len(),
246-
);
235+
copy_from_user(buf.as_mut_ptr(), self.value() as *const u8, buf.len());
247236
}
248237
Ok(())
249238
}
@@ -255,21 +244,14 @@ impl UserVAddr {
255244
pub fn read_cstr(self, buf: &mut [u8]) -> Result<usize, AccessError> {
256245
call_usercopy_hook();
257246
self.access_ok(buf.len())?;
258-
let read_len = unsafe {
259-
strncpy_from_user(
260-
buf.as_mut_ptr(),
261-
self.value() as *const u8,
262-
buf.len(),
263-
)
264-
};
247+
let read_len =
248+
unsafe { strncpy_from_user(buf.as_mut_ptr(), self.value() as *const u8, buf.len()) };
265249
Ok(read_len)
266250
}
267251

268252
pub fn write<T>(self, buf: &T) -> Result<usize, AccessError> {
269253
let len = size_of::<T>();
270-
self.write_bytes(unsafe {
271-
slice::from_raw_parts(buf as *const T as *const u8, len)
272-
})?;
254+
self.write_bytes(unsafe { slice::from_raw_parts(buf as *const T as *const u8, len) })?;
273255
Ok(len)
274256
}
275257

extensions/api/kernel.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ static OPS: StaticCell<&dyn KernelOps> = StaticCell::new(&NopOps);
2323
struct NopOps;
2424

2525
impl KernelOps for NopOps {
26-
fn attach_irq(
27-
&self,
28-
_irq: u8,
29-
_f: Box<dyn FnMut() + Send + Sync + 'static>,
30-
) {
31-
}
26+
fn attach_irq(&self, _irq: u8, _f: Box<dyn FnMut() + Send + Sync + 'static>) {}
3227

3328
fn register_block_driver(&self, _driver: Box<dyn BlockDriver>) {}
3429

extensions/api/lib.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![no_std]
2+
#![allow(non_camel_case_types)]
23
#![feature(box_syntax)]
34

45
extern crate log;
@@ -13,7 +14,7 @@ use ctypes::c_int;
1314
use environment::spinlock::SpinLock;
1415
use kernel::kernel_ops;
1516
use process::{Pid, ProcessState};
16-
use vfs::mount::Rootfs;
17+
use vfs::{mount::Rootfs, opened_file::OpenedFileTable};
1718

1819
pub use environment::{debug_warn, print, println, warn_if_err, warn_once};
1920
pub use log::{debug, error, info, trace, warn};
@@ -29,9 +30,7 @@ pub mod arch {
2930
}
3031

3132
pub mod mm {
32-
pub use environment::page_allocator::{
33-
alloc_pages, AllocPageFlags, PageAllocError,
34-
};
33+
pub use environment::page_allocator::{alloc_pages, AllocPageFlags, PageAllocError};
3534
}
3635

3736
pub mod sync {
@@ -71,9 +70,11 @@ impl Process {
7170
proc!(kernel_ops()).exit(status)
7271
}
7372

74-
pub fn get_open_file_by_fid(
75-
fd: vfs::Fd,
76-
) -> result::Result<Arc<vfs::opened_file::OpenedFile>> {
73+
pub fn opened_files() -> Arc<SpinLock<OpenedFileTable>> {
74+
proc!(kernel_ops()).opened_files()
75+
}
76+
77+
pub fn get_open_file_by_fid(fd: vfs::Fd) -> result::Result<Arc<vfs::opened_file::OpenedFile>> {
7778
proc!(kernel_ops()).get_open_file_by_fid(fd)
7879
}
7980

@@ -96,20 +97,10 @@ impl Process {
9697

9798
pub unsafe trait AsBuf: Sized {
9899
fn as_buf(&self) -> &[u8] {
99-
unsafe {
100-
core::slice::from_raw_parts(
101-
self as *const _ as _,
102-
size_of::<Self>(),
103-
)
104-
}
100+
unsafe { core::slice::from_raw_parts(self as *const _ as _, size_of::<Self>()) }
105101
}
106102
fn as_buf_mut(&mut self) -> &mut [u8] {
107-
unsafe {
108-
core::slice::from_raw_parts_mut(
109-
self as *mut _ as _,
110-
size_of::<Self>(),
111-
)
112-
}
103+
unsafe { core::slice::from_raw_parts_mut(self as *mut _ as _, size_of::<Self>()) }
113104
}
114105
}
115106

@@ -272,6 +263,7 @@ pub mod posix;
272263
pub mod process;
273264
pub mod result;
274265
pub mod schema;
266+
#[macro_use]
275267
pub mod user_buffer;
276268
pub mod uuid;
277269
pub mod vfs;

extensions/api/posix/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
use crate::{vfs, ctypes::c_int};
2+
13
#[derive(Debug)]
24
#[repr(transparent)]
35
pub struct Timestamp(pub u32);
6+
7+
pub enum CwdOrFd {
8+
/// `AT_FDCWD`
9+
AtCwd,
10+
Fd(vfs::Fd),
11+
}
12+
13+
impl CwdOrFd {
14+
pub fn parse(value: c_int) -> CwdOrFd {
15+
match value {
16+
-100 => CwdOrFd::AtCwd,
17+
_ => CwdOrFd::Fd(vfs::Fd::new(value)),
18+
}
19+
}
20+
}
21+

extensions/api/process.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use environment::spinlock::SpinLock;
33

44
use crate::{
55
ctypes::c_int,
6-
vfs::{mount::Rootfs, opened_file::OpenedFile, Fd},
6+
vfs::{mount::Rootfs, opened_file::{OpenedFile, OpenedFileTable}, Fd},
77
Result,
88
};
99

@@ -34,6 +34,7 @@ impl Pid {
3434
pub trait ProcessOps {
3535
fn rootfs(&self) -> &Arc<SpinLock<Rootfs>>;
3636
fn exit(&self, status: c_int) -> !;
37+
fn opened_files(&self) -> Arc<SpinLock<OpenedFileTable>>;
3738
fn get_open_file_by_fid(&self, fd: Fd) -> Result<Arc<OpenedFile>>;
3839
fn set_state(&self, new_state: ProcessState);
3940
fn has_pending_signals(&self) -> bool;

extensions/api/result.rs

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub enum ErrorKind {
1414
PageFault,
1515
OutOfMemory,
1616

17+
NotSupported,
18+
1719
Invalid,
1820

1921
TooBig,
@@ -28,6 +30,8 @@ pub enum ErrorKind {
2830
NoEntry,
2931
Unsupported,
3032

33+
IsADirectory,
34+
3135
BadFile,
3236

3337
BufferError,

extensions/api/schema/posix.rs

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ pub const S_IFLNK: u32 = 0o120000;
6161

6262
pub const O_ACCMODE: u32 = 0o3;
6363

64+
#[allow(unused)]
65+
pub const O_RDONLY: u32 = 0o0;
66+
pub const O_WRONLY: u32 = 0o1;
67+
pub const O_RDWR: u32 = 0o2;
68+
6469
#[derive(Debug, Copy, Clone)]
6570
#[repr(transparent)]
6671
pub struct FileMode(pub u32);

extensions/api/user_buffer.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use utils::alignment::align_up;
77
use crate::{Error, ErrorKind, Result};
88

99
/// Parses a bitflags field given from the user. Returns `Result<T>`.
10+
#[macro_export]
1011
macro_rules! bitflags_from_user {
1112
($st:tt, $input:expr) => {{
1213
let bits = $input;
@@ -16,7 +17,7 @@ macro_rules! bitflags_from_user {
1617
bits
1718
);
1819

19-
crate::Error::new(crate::result::Errno::ENOSYS)
20+
api::result::Error::new(api::result::ErrorKind::NoSyscall)
2021
})
2122
}};
2223
}

extensions/api/vfs/mod.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use alloc::sync::Arc;
1+
use alloc::{string::String, sync::Arc};
22

33
use crate::{
44
ctypes::c_int,
55
io,
6-
schema::{
7-
posix::{DevId, FileMode, FileSize, INodeNo},
8-
unix::PathBuf,
9-
},
6+
schema::posix::{FileMode, FileSize, INodeNo},
107
user_buffer::{UserBuffer, UserBufferMut},
118
ErrorKind, Result,
129
};
@@ -60,32 +57,33 @@ pub trait Filesystem: Send + Sync {
6057

6158
#[derive(Debug)]
6259
pub struct DirEntry {
63-
pub path: PathBuf,
64-
pub ftype: FileType,
60+
pub node_id: NodeId,
61+
pub file_type: FileType,
62+
pub name: String,
6563
}
6664

6765
pub trait Directory: Send + Sync + core::fmt::Debug {
6866
fn _lookup(&self, name: &str) -> Result<Node>;
6967

70-
fn read_dir(&self, index: usize) -> Option<DirEntry>;
68+
fn read_dir(&self, index: usize) -> Result<Option<DirEntry>>;
7169

7270
fn stat(&self) -> Result<Stat>;
7371
}
7472

75-
pub struct ReadDir<'a> {
76-
pub directory: Arc<&'a dyn Directory>,
77-
pub current: usize,
78-
}
73+
// pub struct ReadDir<'a> {
74+
// pub directory: Arc<&'a dyn Directory>,
75+
// pub current: usize,
76+
// }
7977

80-
impl Iterator for ReadDir<'_> {
81-
type Item = DirEntry;
78+
// impl Iterator for ReadDir<'_> {
79+
// type Item = DirEntry;
8280

83-
fn next(&mut self) -> Option<Self::Item> {
84-
let res = self.directory.read_dir(self.current);
85-
self.current += 1;
86-
res
87-
}
88-
}
81+
// fn next(&mut self) -> Result<Self::Item> {
82+
// let res = self.directory.read_dir(self.current);
83+
// self.current += 1;
84+
// res
85+
// }
86+
// }
8987

9088
pub trait File: Send + Sync + core::fmt::Debug {
9189
fn open(&self, options: &io::OpenOptions) -> Result<Option<Arc<dyn File>>>;
@@ -173,20 +171,23 @@ pub enum FileKind {
173171
RegularFile,
174172
Directory,
175173
CharDevice,
174+
BlockDevice,
176175
}
177176

178177
pub const S_IFMT: u32 = 0o170000;
179178
pub const S_IFCHR: u32 = 0o020000;
180179
pub const S_IFDIR: u32 = 0o040000;
181180
pub const S_IFREG: u32 = 0o100000;
182181
pub const S_IFLNK: u32 = 0o120000;
182+
pub const S_IFBLK: u32 = 0o060000;
183183

184184
impl From<FileKind> for u32 {
185185
fn from(mode: FileKind) -> Self {
186186
match mode {
187187
FileKind::RegularFile => S_IFREG,
188188
FileKind::Directory => S_IFDIR,
189189
FileKind::CharDevice => S_IFCHR,
190+
FileKind::BlockDevice => S_IFBLK,
190191
}
191192
}
192193
}

0 commit comments

Comments
 (0)