Skip to content

Commit 6c51c7d

Browse files
committed
feat: add pipe syscall
1 parent 7bc0f2f commit 6c51c7d

18 files changed

Lines changed: 563 additions & 58 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ tg-kernel-context = { path = "tg-kernel-context", version = "0.1.0-preview.1" }
5454
tg-kernel-alloc = { path = "tg-kernel-alloc", version = "0.1.0-preview.2" }
5555
tg-kernel-vm = { path = "tg-kernel-vm", version = "0.1.0-preview.2" }
5656
tg-task-manage = { path = "tg-task-manage", version = "0.1.0-preview.1" }
57-
tg-easy-fs = { path = "tg-easy-fs", version = "0.1.0-preview.1" }
57+
tg-easy-fs = { path = "tg-easy-fs", version = "0.1.0-preview.2" }
5858
tg-signal-defs = { path = "tg-signal-defs", version = "0.1.0-preview.1" }
5959
tg-signal = { path = "tg-signal", version = "0.1.0-preview.1" }
6060
tg-signal-impl = { path = "tg-signal-impl", version = "0.1.0-preview.1" }

ch6/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ mod impls {
324324
});
325325
count as _
326326
} else if let Some(file) = &current.fd_table[fd] {
327-
let mut file = file.lock();
327+
let file = file.lock();
328328
if file.writable() {
329329
let mut v: Vec<&'static mut [u8]> = Vec::new();
330330
unsafe { v.push(core::slice::from_raw_parts_mut(ptr.as_ptr(), count)) };
@@ -356,7 +356,7 @@ mod impls {
356356
}
357357
count as _
358358
} else if let Some(file) = &current.fd_table[fd] {
359-
let mut file = file.lock();
359+
let file = file.lock();
360360
if file.readable() {
361361
let mut v: Vec<&'static mut [u8]> = Vec::new();
362362
unsafe { v.push(core::slice::from_raw_parts_mut(ptr.as_ptr(), count)) };

ch7/src/main.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ mod impls {
237237
use core::{alloc::Layout, ptr::NonNull};
238238
use spin::Mutex;
239239
use tg_console::log;
240-
use tg_easy_fs::UserBuffer;
241-
use tg_easy_fs::{FSManager, OpenFlags};
240+
use tg_easy_fs::{make_pipe, FSManager, OpenFlags, UserBuffer};
242241
use tg_kernel_vm::{
243242
page_table::{MmuMeta, Pte, Sv39, VAddr, VmFlags, PPN, VPN},
244243
PageManager,
@@ -338,7 +337,7 @@ mod impls {
338337
});
339338
count as _
340339
} else if let Some(file) = &current.fd_table[fd] {
341-
let mut file = file.lock();
340+
let file = file.lock();
342341
if file.writable() {
343342
let mut v: Vec<&'static mut [u8]> = Vec::new();
344343
unsafe { v.push(core::slice::from_raw_parts_mut(ptr.as_ptr(), count)) };
@@ -370,7 +369,7 @@ mod impls {
370369
}
371370
count as _
372371
} else if let Some(file) = &current.fd_table[fd] {
373-
let mut file = file.lock();
372+
let file = file.lock();
374373
if file.readable() {
375374
let mut v: Vec<&'static mut [u8]> = Vec::new();
376375
unsafe { v.push(core::slice::from_raw_parts_mut(ptr.as_ptr(), count)) };
@@ -410,7 +409,7 @@ mod impls {
410409
FS.open(string.as_str(), OpenFlags::from_bits(flags as u32).unwrap())
411410
{
412411
let new_fd = current.fd_table.len();
413-
current.fd_table.push(Some(Mutex::new(fd.as_ref().clone())));
412+
current.fd_table.push(Some(Mutex::new(fd)));
414413
new_fd as isize
415414
} else {
416415
-1
@@ -430,6 +429,34 @@ mod impls {
430429
current.fd_table[fd].take();
431430
0
432431
}
432+
433+
fn pipe(&self, _caller: Caller, pipe: usize) -> isize {
434+
let current = PROCESSOR.get_mut().current().unwrap();
435+
let (read_end, write_end) = make_pipe();
436+
let read_fd = current.fd_table.len();
437+
current.fd_table.push(Some(Mutex::new(read_end)));
438+
let write_fd = current.fd_table.len();
439+
current.fd_table.push(Some(Mutex::new(write_end)));
440+
// 将 read_fd 写入 pipe[0]
441+
if let Some(mut ptr) = current
442+
.address_space
443+
.translate::<usize>(VAddr::new(pipe), WRITEABLE)
444+
{
445+
unsafe { *ptr.as_mut() = read_fd };
446+
} else {
447+
return -1;
448+
}
449+
// 将 write_fd 写入 pipe[1]
450+
if let Some(mut ptr) = current
451+
.address_space
452+
.translate::<usize>(VAddr::new(pipe + core::mem::size_of::<usize>()), WRITEABLE)
453+
{
454+
unsafe { *ptr.as_mut() = write_fd };
455+
} else {
456+
return -1;
457+
}
458+
0
459+
}
433460
}
434461

435462
impl Process for SyscallContext {

ch7/src/process.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{map_portal, Sv39Manager};
2-
use alloc::{alloc::alloc_zeroed, boxed::Box, vec::Vec};
2+
use alloc::{alloc::alloc_zeroed, boxed::Box, sync::Arc, vec::Vec};
33
use core::{alloc::Layout, str::FromStr};
44
use spin::Mutex;
55
use tg_easy_fs::FileHandle;
@@ -25,7 +25,7 @@ pub struct Process {
2525
pub address_space: AddressSpace<Sv39, Sv39Manager>,
2626

2727
/// 文件描述符表
28-
pub fd_table: Vec<Option<Mutex<FileHandle>>>,
28+
pub fd_table: Vec<Option<Mutex<Arc<FileHandle>>>>,
2929

3030
/// 信号模块
3131
pub signal: Box<dyn Signal>,
@@ -58,7 +58,7 @@ impl Process {
5858
let satp = (8 << 60) | address_space.root_ppn().val();
5959
let foreign_ctx = ForeignContext { context, satp };
6060
// 复制父进程文件符描述表
61-
let mut new_fd_table: Vec<Option<Mutex<FileHandle>>> = Vec::new();
61+
let mut new_fd_table: Vec<Option<Mutex<Arc<FileHandle>>>> = Vec::new();
6262
for fd in self.fd_table.iter_mut() {
6363
if let Some(file) = fd {
6464
new_fd_table.push(Some(Mutex::new(file.get_mut().clone())));
@@ -153,11 +153,11 @@ impl Process {
153153
address_space,
154154
fd_table: vec![
155155
// Stdin
156-
Some(Mutex::new(FileHandle::empty(true, false))),
156+
Some(Mutex::new(Arc::new(FileHandle::empty(true, false)))),
157157
// Stdout
158-
Some(Mutex::new(FileHandle::empty(false, true))),
158+
Some(Mutex::new(Arc::new(FileHandle::empty(false, true)))),
159159
// Stderr
160-
Some(Mutex::new(FileHandle::empty(false, true))),
160+
Some(Mutex::new(Arc::new(FileHandle::empty(false, true)))),
161161
],
162162
signal: Box::new(SignalImpl::new()),
163163
heap_bottom,

ch8/src/main.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,7 @@ mod impls {
252252
use core::{alloc::Layout, ptr::NonNull};
253253
use spin::Mutex;
254254
use tg_console::log;
255-
use tg_easy_fs::UserBuffer;
256-
use tg_easy_fs::{FSManager, OpenFlags};
255+
use tg_easy_fs::{make_pipe, FSManager, OpenFlags, UserBuffer};
257256
use tg_kernel_vm::{
258257
page_table::{MmuMeta, Pte, Sv39, VAddr, VmFlags, VmMeta, PPN, VPN},
259258
PageManager,
@@ -354,7 +353,7 @@ mod impls {
354353
});
355354
count as _
356355
} else if let Some(file) = &current.fd_table[fd] {
357-
let mut file = file.lock();
356+
let file = file.lock();
358357
if file.writable() {
359358
let mut v: Vec<&'static mut [u8]> = Vec::new();
360359
unsafe { v.push(core::slice::from_raw_parts_mut(ptr.as_ptr(), count)) };
@@ -386,7 +385,7 @@ mod impls {
386385
}
387386
count as _
388387
} else if let Some(file) = &current.fd_table[fd] {
389-
let mut file = file.lock();
388+
let file = file.lock();
390389
if file.readable() {
391390
let mut v: Vec<&'static mut [u8]> = Vec::new();
392391
unsafe { v.push(core::slice::from_raw_parts_mut(ptr.as_ptr(), count)) };
@@ -426,7 +425,7 @@ mod impls {
426425
FS.open(string.as_str(), OpenFlags::from_bits(flags as u32).unwrap())
427426
{
428427
let new_fd = current.fd_table.len();
429-
current.fd_table.push(Some(Mutex::new(fd.as_ref().clone())));
428+
current.fd_table.push(Some(Mutex::new(fd)));
430429
new_fd as isize
431430
} else {
432431
-1
@@ -446,6 +445,34 @@ mod impls {
446445
current.fd_table[fd].take();
447446
0
448447
}
448+
449+
fn pipe(&self, _caller: Caller, pipe: usize) -> isize {
450+
let current = PROCESSOR.get_mut().get_current_proc().unwrap();
451+
let (read_end, write_end) = make_pipe();
452+
let read_fd = current.fd_table.len();
453+
current.fd_table.push(Some(Mutex::new(read_end)));
454+
let write_fd = current.fd_table.len();
455+
current.fd_table.push(Some(Mutex::new(write_end)));
456+
// 将 read_fd 写入 pipe[0]
457+
if let Some(mut ptr) = current
458+
.address_space
459+
.translate::<usize>(VAddr::new(pipe), WRITEABLE)
460+
{
461+
unsafe { *ptr.as_mut() = read_fd };
462+
} else {
463+
return -1;
464+
}
465+
// 将 write_fd 写入 pipe[1]
466+
if let Some(mut ptr) = current
467+
.address_space
468+
.translate::<usize>(VAddr::new(pipe + core::mem::size_of::<usize>()), WRITEABLE)
469+
{
470+
unsafe { *ptr.as_mut() = write_fd };
471+
} else {
472+
return -1;
473+
}
474+
0
475+
}
449476
}
450477

451478
impl Process for SyscallContext {

ch8/src/process.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::{map_portal, processor::ProcessorInner, Sv39Manager, PROCESSOR};
2-
use alloc::sync::Arc;
3-
use alloc::{alloc::alloc_zeroed, boxed::Box, vec::Vec};
2+
use alloc::{alloc::alloc_zeroed, boxed::Box, sync::Arc, vec::Vec};
43
use core::{alloc::Layout, str::FromStr};
54
use spin::Mutex;
65
use tg_easy_fs::FileHandle;
@@ -42,7 +41,7 @@ pub struct Process {
4241
/// 可变
4342
pub address_space: AddressSpace<Sv39, Sv39Manager>,
4443
/// 文件描述符表
45-
pub fd_table: Vec<Option<Mutex<FileHandle>>>,
44+
pub fd_table: Vec<Option<Mutex<Arc<FileHandle>>>>,
4645
/// 信号模块
4746
pub signal: Box<dyn Signal>,
4847
/// 分配的锁以及信号量
@@ -85,7 +84,7 @@ impl Process {
8584
let satp = (8 << 60) | address_space.root_ppn().val();
8685
let thread = Thread::new(satp, context);
8786
// 复制父进程文件符描述表
88-
let mut new_fd_table: Vec<Option<Mutex<FileHandle>>> = Vec::new();
87+
let mut new_fd_table: Vec<Option<Mutex<Arc<FileHandle>>>> = Vec::new();
8988
for fd in self.fd_table.iter_mut() {
9089
if let Some(file) = fd {
9190
new_fd_table.push(Some(Mutex::new(file.get_mut().clone())));
@@ -175,11 +174,11 @@ impl Process {
175174
address_space,
176175
fd_table: vec![
177176
// Stdin
178-
Some(Mutex::new(FileHandle::empty(true, false))),
177+
Some(Mutex::new(Arc::new(FileHandle::empty(true, false)))),
179178
// Stdout
180-
Some(Mutex::new(FileHandle::empty(false, true))),
179+
Some(Mutex::new(Arc::new(FileHandle::empty(false, true)))),
181180
// Stderr
182-
Some(Mutex::new(FileHandle::empty(false, true))),
181+
Some(Mutex::new(Arc::new(FileHandle::empty(false, true)))),
183182
],
184183
signal: Box::new(SignalImpl::new()),
185184
semaphore_list: Vec::new(),

tg-easy-fs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "tg-easy-fs"
33
description = "A simple filesystem implementation for rCore tutorial OS."
4-
version = "0.1.0-preview.1"
4+
version = "0.1.0-preview.2"
55
edition = "2021"
66
authors = ["Yifan Wu <shinbokuow@163.com>"]
77
repository = "https://github.com/rcore-os/rCore-Tutorial-in-single-workspace"

tg-easy-fs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This crate provides a lightweight filesystem (EasyFS) implementation designed fo
1212
- **Inode-based structure**: Unix-like inode system for file metadata management
1313
- **Block caching**: Efficient block cache layer for improved I/O performance
1414
- **Bitmap allocation**: Bitmap-based block and inode allocation
15+
- **Pipe support**: IPC pipe implementation with ring buffer
1516
- **no_std compatible**: Designed for bare-metal kernel environments
1617

1718
## Usage
@@ -31,6 +32,7 @@ use tg_easy_fs::{BlockDevice, EasyFileSystem, Inode};
3132
- `EasyFileSystem` - Main filesystem structure
3233
- `Inode` - Virtual filesystem node interface
3334
- `BlockCache` - Block caching layer
35+
- `Pipe` / `PipeRingBuffer` - IPC pipe support
3436

3537
## License
3638

0 commit comments

Comments
 (0)