Skip to content

Commit

Permalink
Add sound-daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed Sep 29, 2024
1 parent 4a597a0 commit ee08f7d
Show file tree
Hide file tree
Showing 15 changed files with 358 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ exclude = ["userspace", "sysroot/src/rust"]
resolver = "2"

[profile.release]
debug=false
debug=true
1 change: 1 addition & 0 deletions cykusz-rs/src/arch/x86_64/mm/virt/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ impl Table<Level4> {

let (was_alloc_2, l1) = l2.alloc_next_level(page.p2_index(), user);

dbgln!(virt, "map_flags {} {:?}", addr, flags);
if l1.alloc_set_flags(page.p1_index(), Entry::from_kernel_flags(flags)) {
l2.entries[page.p2_index()].inc_entry_count();
}
Expand Down
2 changes: 2 additions & 0 deletions cykusz-rs/src/kernel/net/unix/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ impl INode for Socket {
return Err(FsError::NotSupported);
};

dbgln!(unix, "Writing {} data", buf.len());

Ok(target
.buffer
.append_data_flags(buf, WaitQueueFlags::from(flags))?)
Expand Down
3 changes: 1 addition & 2 deletions cykusz-rs/src/kernel/syscall/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,11 +1140,10 @@ pub fn sys_poll(fds: u64, nfds: u64, timeout: u64) -> SyscallResult {
}
if let Some(handle) = task.get_handle(fd.fd as usize) {
let f = handle.poll(if first { Some(&mut poll_table) } else { None }, fd.events)?;
fd.revents = f;
if !f.is_empty() {
found += 1;

fd.revents = f;

logln4!("found {}: {:?}", fd.fd, fd.revents);
}
} else {
Expand Down
1 change: 1 addition & 0 deletions cykusz-rs/src/kernel/task/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,7 @@ impl VM {
if current_task_ref().locks() > 0 {
logln!("handle_pagefault: locks > 0");
}
dbgln!(vm, "handle_pagefault: {:?} {}", reason, addr);
let mut res = self.data.lock();

let ret = res.handle_pagefault(reason, addr);
Expand Down
33 changes: 23 additions & 10 deletions cykusz-rs/src/kernel/utils/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl BufferQueue {
return Ok(0);
}

logln4!("appending data len: {}", data.len());
dbgln!(buffer, "appending data len: {}", data.len());

let mut buffer = self
.writer_queue
Expand All @@ -174,18 +174,24 @@ impl BufferQueue {
})?
.ok_or(vfs::FsError::WouldBlock)?;

dbgln!(
buffer,
"appending data starting: available size: {}",
buffer.available_size()
);

if !self.has_readers() {
logln!("no readers......");
return Err(FsError::Pipe);
}

let written = buffer.append_data(data);

logln4!("data appended");
dbgln!(buffer, "data appended {}", written);

drop(buffer);

self.reader_queue.notify_one();
self.reader_queue.notify_all();

Ok(written)
}
Expand Down Expand Up @@ -213,15 +219,15 @@ impl BufferQueue {
return Ok(0);
}

logln!("READ DATA WAIT");
dbgln!(buffer, "read data wait");
let mut buffer = self
.reader_queue
.wait_lock_for(wg_flags, &self.buffer, |l| {
self.has_data_locked(l) || self.shutting_down() || offset > 0
})?
.ok_or(vfs::FsError::WouldBlock)?;

logln!("READ DATA STARTING");
dbgln!(buffer, "read data starting");
let read = if transient {
buffer.read_data_transient_from(offset, buf)
} else {
Expand All @@ -230,10 +236,10 @@ impl BufferQueue {

drop(buffer);

logln!("READ DATA DONE {}", read);
dbgln!(buffer, "read data done {}", read);

if !transient && read > 0 {
self.writer_queue.notify_one();
self.writer_queue.notify_all();
}

Ok(read)
Expand Down Expand Up @@ -300,36 +306,43 @@ impl Buffer {
return 0;
}

return if self.r <= self.w {
if self.r <= self.w {
self.data.len() - (self.w - self.r)
} else {
self.r - self.w
};
}
}

pub fn size(&self) -> usize {
return self.data.len() - self.available_size();
}

pub fn append_data(&mut self, data: &[u8]) -> usize {
dbgln!(buffer, "append data {} {} {}", self.r, self.w, data.len());
if self.full {
dbgln!(buffer, "append data buffer full");
return 0;
}

if self.r > self.w {
let cap = self.r - self.w;
let to_copy = core::cmp::min(cap, data.len());
dbgln!(buffer, "append data {}..{}", self.w, self.w + to_copy);
self.data.as_mut_slice()[self.w..self.w + to_copy].copy_from_slice(&data[..to_copy]);
self.w += to_copy;
self.w = (self.w + to_copy) % self.data.len();
self.full = self.r == self.w;
dbgln!(buffer, "append data copied {}", to_copy);
to_copy
} else {
let right = self.data.len() - self.w;
let to_copy = core::cmp::min(right, data.len());
dbgln!(buffer, "append data 2 {}..{}", self.w, self.w + to_copy);
self.data.as_mut_slice()[self.w..self.w + to_copy].copy_from_slice(&data[..to_copy]);
dbgln!(buffer, "append data done");
self.w = (self.w + to_copy) % self.data.len();
self.full = self.r == self.w;
let written = if to_copy < data.len() {
dbgln!(buffer, "append data recurse");
self.append_data(&data[to_copy..])
} else {
0
Expand Down
14 changes: 6 additions & 8 deletions cykusz-rs/src/kernel/utils/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,27 @@ impl<'a, T> Iterator for PageIter<'a, T> {
.align_down(PAGE_SIZE) as *const T
};

self.cur = if next <= last {
if next <= last {
unsafe { Some(&*next) }
} else {
None
};

None
}
};

return Some(ret);
Some(ret)
}
}

pub trait Prefault {
fn prefault(&self);
}

impl<T> Prefault for &T {
impl<T: Copy> Prefault for &T {
fn prefault(&self) {
let t = *self;
let t = **self;

unsafe {

Check failure on line 95 in cykusz-rs/src/kernel/utils/types.rs

View workflow job for this annotation

GitHub Actions / build

unnecessary `unsafe` block
(t as *const T).read_volatile();
core::hint::black_box(t); // prevent optimisations...
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion disk-scripts/install_os.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fi
sudo umount mnt

PROGS="test testcpp hello stack nyancat ttytest fork poweroff stat fbdoom doom1.wad open_sleep"
RUST_PROGS="init shell mount umount unixsocket-server unixsocket-client forktest mprotecttest playwav"
RUST_PROGS="init shell mount umount unixsocket-server unixsocket-client forktest mprotecttest playwav threads sound-daemon"

sudo mount /dev/loop0p2 mnt
sudo chown -R $USER:$USER mnt
Expand Down
8 changes: 8 additions & 0 deletions userspace/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion userspace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
resolver = "2"
members = ["init", "shell", "mount", "umount", "syscall-user", "unixsockets", "testprogs", "playwav"]
members = ["init", "shell", "mount", "umount", "syscall-user", "unixsockets", "testprogs", "playwav", "sound-daemon"]
40 changes: 38 additions & 2 deletions userspace/playwav/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![feature(seek_stream_len)]
#![feature(raw_ref_op)]

use std::io::Seek;
use std::io::{Seek, Write};
use std::os::fd::AsRawFd;
use std::os::unix::net::UnixStream;
use std::process::ExitCode;
use std::ptr::{slice_from_raw_parts, slice_from_raw_parts_mut};
use syscall_defs::{MMapFlags, MMapProt};
Expand Down Expand Up @@ -81,6 +83,40 @@ fn play(song: &str) -> Result<(), ExitCode> {
Ok(())
}

fn send_to_daemon(song: &str) -> Result<(), ExitCode> {
let mut wav = std::fs::File::open(song).map_err(|_e| ExitCode::from(1))?;
let wav_size = wav.stream_len().map_err(|_e| ExitCode::from(2))? as usize;

let wav_map = syscall_user::mmap(
None,
wav_size,
MMapProt::PROT_READ,
MMapFlags::MAP_PRIVATE,
Some(wav.as_raw_fd() as usize),
0,
)
.map_err(|_e| ExitCode::from(1))?;

let buf = unsafe { &*slice_from_raw_parts(wav_map as *const u8, wav_size) };

let mut socket = UnixStream::connect("/sound-daemon.pid").map_err(|e| ExitCode::from(3))?;

//println!("writing {} bytes", buf.len());
let mut written = 0;
while let n = socket
.write(&buf[written..])
.map_err(|_e| ExitCode::from(4))?
{
written += n;
if written == buf.len() {
break;
}
}
println!("write finished");

Ok(())
}

fn main() -> Result<(), ExitCode> {
let mut args = std::env::args();

Expand All @@ -101,7 +137,7 @@ fn main() -> Result<(), ExitCode> {
}

if !daemonize {
return Ok(play(file.as_str())?);
return Ok(send_to_daemon(file.as_str())?);
}

let daemon = syscall_user::fork().expect("fork failed");
Expand Down
16 changes: 16 additions & 0 deletions userspace/sound-daemon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "sound-daemon"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "sound-daemon"
test = false
bench = false
path = "src/sound-daemon/bin/main.rs"

[dependencies.syscall-defs]
path = "../../syscall-defs"

[dependencies.syscall-user]
path = "../syscall-user"
Loading

0 comments on commit ee08f7d

Please sign in to comment.