Files
RedBear-OS/drivers/graphics/fbcond/src/scheme.rs
T
Red Bear OS 9bbdc2cafa Merge upstream/main (59cf8189) into submodule/base
Sync the base fork with 13 upstream commits (xhci event-processing
dedup + IRQ race fix, randd permission simplification, nvmed TimeSpec
fix, /dev/ptmx, fpath legacy-path cleanup, dynamically-linked init
fix, and more). Brings process_one_event into the tree, satisfying the
verify-fork-functions gate for base.

Conflicts resolved (3 of 8 overlapping files; 5 auto-merged):

- drivers/usb/xhcid/src/xhci/irq_reactor.rs: took upstream's
  process_one_event/EventProcessResult refactor (b2ed85ea) including
  the a01d3ce6 race fix (process events between NoEvent and
  unmasking), and re-applied the Red Bear SPURIOUS_REBOOT quirk on the
  NoEvent warning (downgrade to debug when quirked).

- randd/src/main.rs: took upstream's permission-handling
  simplification (e26db606); re-applied the RB fcntl improvement
  (F_GETFL/F_GETFD/F_SETFL/F_SETFD handling, Linux random_fops xref)
  and the is_cpu_feature_detected early-return cleanup. Dropped
  test_scheme_perms (deleted by upstream's simplification).

- Makefile: kept upstream's new /dev symlink block (dev/null, ptmx,
  random, urandom, zero, tty, stdin/stdout/stderr).

Verified: cargo check clean; 57/57 tests pass (xhcid 35, usbhubd 14,
xhci trb 8); verify-fork-functions.sh --no-fetch base reports all
upstream functions present.
2026-07-19 05:47:27 +09:00

221 lines
6.3 KiB
Rust

use std::collections::BTreeMap;
use std::os::fd::AsRawFd;
use event::{EventQueue, UserData};
use redox_scheme::scheme::SchemeSync;
use redox_scheme::{CallerCtx, OpenResult};
use scheme_utils::{FpathWriter, HandleMap};
use syscall::schemev2::NewFdFlags;
use syscall::{Error, EventFlags, Result, EACCES, EAGAIN, EBADF, ENOENT};
use crate::display::Display;
use crate::text::TextScreen;
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Debug)]
pub struct VtIndex(usize);
impl VtIndex {
pub const SCHEMA_SENTINEL: VtIndex = VtIndex(usize::MAX);
/// Event-queue sentinel for readable input on the kernel debug (serial)
/// scheme, used to feed serial-line input into the console.
pub const SERIAL_SENTINEL: VtIndex = VtIndex(usize::MAX - 1);
}
impl UserData for VtIndex {
fn into_user_data(self) -> usize {
self.0
}
fn from_user_data(user_data: usize) -> Self {
VtIndex(user_data)
}
}
pub struct FdHandle {
pub vt_i: VtIndex,
pub flags: usize,
pub events: EventFlags,
pub notified_read: bool,
}
pub enum Handle {
Vt(FdHandle),
SchemeRoot,
/// A read-only handle over a VT's scrollback buffer, opened via
/// `/scheme/fbcon/<vt>/scrollback`.
Scrollback(VtIndex),
}
pub struct FbconScheme {
pub vts: BTreeMap<VtIndex, TextScreen>,
pub handles: HandleMap<Handle>,
}
impl FbconScheme {
pub fn new(vt_ids: &[usize], event_queue: &mut EventQueue<VtIndex>) -> FbconScheme {
let mut vts = BTreeMap::new();
for &vt_i in vt_ids {
let display = Display::open_new_vt().expect("Failed to open display for vt");
event_queue
.subscribe(
display.input_handle.event_handle().as_raw_fd() as usize,
VtIndex(vt_i),
event::EventFlags::READ,
)
.expect("Failed to subscribe to input events for vt");
vts.insert(VtIndex(vt_i), TextScreen::new(display));
}
FbconScheme {
vts,
handles: HandleMap::new(),
}
}
fn get_vt_handle_mut(&mut self, id: usize) -> Result<&mut FdHandle> {
match self.handles.get_mut(id)? {
Handle::Vt(handle) => Ok(handle),
Handle::SchemeRoot | Handle::Scrollback(_) => Err(Error::new(EBADF)),
}
}
}
impl SchemeSync for FbconScheme {
fn scheme_root(&mut self) -> Result<usize> {
Ok(self.handles.insert(Handle::SchemeRoot))
}
fn openat(
&mut self,
dirfd: usize,
path_str: &str,
flags: usize,
fcntl_flags: u32,
_ctx: &CallerCtx,
) -> Result<OpenResult> {
if !matches!(self.handles.get(dirfd)?, Handle::SchemeRoot) {
return Err(Error::new(EACCES));
}
// `/scheme/fbcon/<vt>/scrollback` opens a read-only view over the VT's
// scrollback ring buffer instead of the live terminal.
let (vt_str, is_scrollback) = match path_str.strip_suffix("/scrollback") {
Some(stripped) => (stripped, true),
None => (path_str, false),
};
let vt_i = VtIndex(vt_str.parse::<usize>().map_err(|_| Error::new(ENOENT))?);
if self.vts.contains_key(&vt_i) {
if is_scrollback {
let id = self.handles.insert(Handle::Scrollback(vt_i));
return Ok(OpenResult::ThisScheme {
number: id,
flags: NewFdFlags::empty(),
});
}
let id = self.handles.insert(Handle::Vt(FdHandle {
vt_i,
flags: flags | fcntl_flags as usize,
events: EventFlags::empty(),
notified_read: false,
}));
Ok(OpenResult::ThisScheme {
number: id,
flags: NewFdFlags::empty(),
})
} else {
Err(Error::new(ENOENT))
}
}
fn fevent(
&mut self,
id: usize,
flags: syscall::EventFlags,
_ctx: &CallerCtx,
) -> Result<syscall::EventFlags> {
let handle = self.get_vt_handle_mut(id)?;
handle.notified_read = false;
handle.events = flags;
Ok(syscall::EventFlags::empty())
}
fn fpath(&mut self, id: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result<usize> {
FpathWriter::with(buf, "fbcon", |w| {
let handle = self.get_vt_handle_mut(id)?;
write!(w, "{}", handle.vt_i.0).unwrap();
Ok(())
})
}
fn fsync(&mut self, id: usize, _ctx: &CallerCtx) -> Result<()> {
let _handle = self.get_vt_handle_mut(id)?;
Ok(())
}
fn fcntl(&mut self, id: usize, _cmd: usize, _arg: usize, _ctx: &CallerCtx) -> Result<usize> {
self.handles.get(id)?;
Ok(0)
}
fn read(
&mut self,
id: usize,
buf: &mut [u8],
_offset: u64,
_fcntl_flags: u32,
_ctx: &CallerCtx,
) -> Result<usize> {
let (vt_i, is_scrollback) = match self.handles.get(id)? {
Handle::Vt(handle) => (handle.vt_i, false),
Handle::Scrollback(vt_i) => (*vt_i, true),
Handle::SchemeRoot => return Err(Error::new(EBADF)),
};
if is_scrollback {
return if let Some(screen) = self.vts.get(&vt_i) {
let data = screen.read_scrollback();
let to_copy = data.len().min(buf.len());
buf[..to_copy].copy_from_slice(&data[..to_copy]);
Ok(to_copy)
} else {
Err(Error::new(EBADF))
};
}
if let Some(screen) = self.vts.get_mut(&vt_i) {
if !screen.can_read() {
Err(Error::new(EAGAIN))
} else {
screen.read(buf)
}
} else {
Err(Error::new(EBADF))
}
}
fn write(
&mut self,
id: usize,
buf: &[u8],
_offset: u64,
_fcntl_flags: u32,
_ctx: &CallerCtx,
) -> Result<usize> {
let vt_i = self.get_vt_handle_mut(id)?.vt_i;
if let Some(console) = self.vts.get_mut(&vt_i) {
console.write(buf)
} else {
Err(Error::new(EBADF))
}
}
fn on_close(&mut self, id: usize) {
self.handles.remove(id);
}
}