Skip to content

Commit 672d0a9

Browse files
authored
Merge pull request #189 from Freax13/enhancement/smaller-future
reduce size of syscall handler future
2 parents ab4db17 + 9ee3b0c commit 672d0a9

File tree

5 files changed

+58
-18
lines changed

5 files changed

+58
-18
lines changed

tee/kernel/src/fs/fd.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ pub mod timer;
7474
pub mod unix_socket;
7575

7676
pub use self::buf::{
77-
KernelReadBuf, KernelWriteBuf, OffsetBuf, ReadBuf, UserBuf, VectoredUserBuf, WriteBuf,
77+
KernelPageWriteBuf, KernelReadBuf, KernelWriteBuf, OffsetBuf, ReadBuf, UserBuf,
78+
VectoredUserBuf, WriteBuf,
7879
};
7980

8081
pub struct OpenFileDescriptionData<T: ?Sized> {

tee/kernel/src/fs/fd/buf.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use x86_64::VirtAddr;
66

77
use crate::{
88
error::Result,
9+
memory::page::KernelPage,
910
user::{
1011
memory::VirtualMemory,
1112
syscall::{
@@ -372,3 +373,33 @@ where
372373
unsafe { self.buffer.read_volatile(offset + self.offset, bytes) }
373374
}
374375
}
376+
377+
pub struct KernelPageWriteBuf<'a> {
378+
page: &'a KernelPage,
379+
offset: usize,
380+
len: usize,
381+
}
382+
383+
impl<'a> KernelPageWriteBuf<'a> {
384+
pub fn new(page: &'a KernelPage, offset: usize, len: usize) -> Self {
385+
Self { page, offset, len }
386+
}
387+
}
388+
389+
impl WriteBuf for KernelPageWriteBuf<'_> {
390+
fn buffer_len(&self) -> usize {
391+
self.len
392+
}
393+
394+
fn read(&self, offset: usize, bytes: &mut [u8]) -> Result<()> {
395+
self.page.read(self.offset + offset, bytes);
396+
Ok(())
397+
}
398+
399+
unsafe fn read_volatile(&self, offset: usize, bytes: NonNull<[u8]>) -> Result<()> {
400+
unsafe {
401+
self.page.read_volatile(self.offset + offset, bytes);
402+
}
403+
Ok(())
404+
}
405+
}

tee/kernel/src/memory/page/kernel.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,23 @@ impl KernelPage {
169169
}
170170

171171
pub fn read(&self, index: usize, buf: &mut [u8]) {
172-
let ptr = self.index(index..index + buf.len());
173-
unsafe {
174-
core::intrinsics::volatile_copy_nonoverlapping_memory(
175-
buf.as_mut_ptr(),
176-
ptr.as_ptr().cast(),
177-
buf.len(),
178-
);
172+
unsafe { self.read_volatile(index, NonNull::from_mut(buf)) }
173+
}
174+
175+
pub unsafe fn read_volatile(&self, index: usize, buf: NonNull<[u8]>) {
176+
if self.is_zero_page() {
177+
unsafe {
178+
core::intrinsics::volatile_set_memory(buf.as_mut_ptr(), 0, buf.len());
179+
}
180+
} else {
181+
let ptr = self.index(index..index + buf.len());
182+
unsafe {
183+
core::intrinsics::volatile_copy_nonoverlapping_memory(
184+
buf.as_mut_ptr(),
185+
ptr.as_ptr().cast(),
186+
buf.len(),
187+
);
188+
}
179189
}
180190
}
181191

tee/kernel/src/user/syscall.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::{
3030
fs::{
3131
StatFs,
3232
fd::{
33-
Events, FdFlags, FileDescriptorTable, KernelWriteBuf, OffsetBuf, ReadBuf,
33+
Events, FdFlags, FileDescriptorTable, KernelPageWriteBuf, OffsetBuf, ReadBuf,
3434
StrongFileDescriptor, UnixLock, UnixLockOwner, UnixLockType, UserBuf, VectoredUserBuf,
3535
WriteBuf, do_io, do_write_io,
3636
epoll::Epoll,
@@ -1327,23 +1327,16 @@ async fn sendfile(
13271327
let stat = r#in.stat()?;
13281328
let count = cmp::min(count, (stat.size as usize).saturating_sub(offset_value));
13291329

1330-
let mut buffer = [0; 0x1000];
13311330
let mut total_len = 0;
13321331
while total_len < count {
13331332
let page_offset = offset_value / 0x1000;
13341333
let offset_in_page = offset_value % 0x1000;
13351334
let chunk_len = cmp::min(0x1000 - offset_in_page, count - total_len);
13361335

1337-
let buffer = &mut buffer[offset_in_page..];
1338-
let buffer = &mut buffer[..chunk_len];
1339-
13401336
let page = r#in.get_page(page_offset, false)?;
1341-
page.read(offset_in_page, buffer);
1337+
let buf = KernelPageWriteBuf::new(&page, offset_in_page, chunk_len);
13421338

1343-
let res = do_write_io(&**r#in, buffer.len(), || {
1344-
out.write(&KernelWriteBuf::new(buffer))
1345-
})
1346-
.await;
1339+
let res = do_write_io(&**r#in, chunk_len, || out.write(&buf)).await;
13471340
match res {
13481341
Ok(0) => break,
13491342
Ok(n) => {

tee/kernel/src/user/syscall/traits.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ impl SyscallHandlers {
133133
self.future_size = return_size;
134134
}
135135

136+
// Make sure that the future is not too big. There's no technical
137+
// reason why large futures can't work, but we should avoid them if
138+
// possible.
139+
assert!(return_size < 2048, "future is very big");
140+
136141
/// Returns the alignment of the return type of `T::execute`.
137142
const fn return_align<'a, T>(
138143
_: fn(thread: &'a Arc<Thread>, syscall_args: SyscallArgs) -> T,

0 commit comments

Comments
 (0)