From 4100884587424e81d2d1dbed1dbf5a7e9291df6e Mon Sep 17 00:00:00 2001 From: LoGin Date: Wed, 17 Apr 2024 10:03:22 +0800 Subject: [PATCH] Feat(tty): add dummy console (#735) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使得riscv能暂时完成stdio_init(将来需要实现riscv的串口console) --- kernel/src/driver/tty/termios.rs | 11 ++ kernel/src/driver/tty/tty_port.rs | 4 + kernel/src/driver/tty/virtual_terminal/mod.rs | 34 +++-- .../tty/virtual_terminal/virtual_console.rs | 3 +- kernel/src/driver/video/console/dummycon.rs | 127 ++++++++++++++++++ kernel/src/driver/video/console/mod.rs | 1 + .../fbdev/base/fbcon/framebuffer_console.rs | 8 +- kernel/src/driver/video/mod.rs | 1 + kernel/src/init/initial_kthread.rs | 1 + 9 files changed, 172 insertions(+), 18 deletions(-) create mode 100644 kernel/src/driver/video/console/dummycon.rs create mode 100644 kernel/src/driver/video/console/mod.rs diff --git a/kernel/src/driver/tty/termios.rs b/kernel/src/driver/tty/termios.rs index 8ba0c44d6..e56d0ac71 100644 --- a/kernel/src/driver/tty/termios.rs +++ b/kernel/src/driver/tty/termios.rs @@ -14,6 +14,17 @@ pub struct WindowSize { pub ypixel: u16, } +impl WindowSize { + pub fn new(row: u16, col: u16, xpixel: u16, ypixel: u16) -> Self { + Self { + row, + col, + xpixel, + ypixel, + } + } +} + #[derive(Debug, Clone, Copy)] pub struct Termios { pub input_mode: InputMode, diff --git a/kernel/src/driver/tty/tty_port.rs b/kernel/src/driver/tty/tty_port.rs index b5cc7acad..1a9097a10 100644 --- a/kernel/src/driver/tty/tty_port.rs +++ b/kernel/src/driver/tty/tty_port.rs @@ -60,6 +60,10 @@ impl TtyPortData { pub fn internal_tty(&self) -> Option> { self.internal_tty.upgrade() } + + pub fn tty(&self) -> Option> { + self.tty.upgrade() + } } #[allow(dead_code)] diff --git a/kernel/src/driver/tty/virtual_terminal/mod.rs b/kernel/src/driver/tty/virtual_terminal/mod.rs index 5a77aef05..f36bf7d4b 100644 --- a/kernel/src/driver/tty/virtual_terminal/mod.rs +++ b/kernel/src/driver/tty/virtual_terminal/mod.rs @@ -1,4 +1,4 @@ -use core::sync::atomic::Ordering; +use core::{fmt::Formatter, sync::atomic::Ordering}; use alloc::{ string::{String, ToString}, @@ -8,12 +8,9 @@ use alloc::{ use system_error::SystemError; use crate::{ - driver::{ - base::device::{ - device_number::{DeviceNumber, Major}, - device_register, IdTable, - }, - video::fbdev::base::fbcon::framebuffer_console::BlittingFbConsole, + driver::base::device::{ + device_number::{DeviceNumber, Major}, + device_register, IdTable, }, filesystem::devfs::devfs_register, libs::spinlock::SpinLock, @@ -95,16 +92,29 @@ impl Color { } } -#[derive(Debug)] pub struct TtyConsoleDriverInner { - console: Arc, + console: Arc, +} + +impl core::fmt::Debug for TtyConsoleDriverInner { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + write!(f, "TtyConsoleDriverInner") + } } impl TtyConsoleDriverInner { pub fn new() -> Result { - Ok(Self { - console: Arc::new(BlittingFbConsole::new()?), - }) + let console = { + #[cfg(not(target_arch = "riscv64"))] + { + Arc::new(crate::driver::video::fbdev::base::fbcon::framebuffer_console::BlittingFbConsole::new()?) + } + + #[cfg(target_arch = "riscv64")] + crate::driver::video::console::dummycon::dummy_console() + }; + + Ok(Self { console }) } fn do_write(&self, tty: &TtyCoreData, buf: &[u8], mut nr: usize) -> Result { diff --git a/kernel/src/driver/tty/virtual_terminal/virtual_console.rs b/kernel/src/driver/tty/virtual_terminal/virtual_console.rs index f63af433c..3fc94b323 100644 --- a/kernel/src/driver/tty/virtual_terminal/virtual_console.rs +++ b/kernel/src/driver/tty/virtual_terminal/virtual_console.rs @@ -236,9 +236,8 @@ impl VirtualConsoleData { self.underline_color = 3; // cyan self.half_color = 0x08; // grey - self.reset(clear); - self.screen_buf.resize(self.cols * self.rows, 0); + self.reset(clear); } pub fn should_update(&self) -> bool { diff --git a/kernel/src/driver/video/console/dummycon.rs b/kernel/src/driver/video/console/dummycon.rs new file mode 100644 index 000000000..abc78020b --- /dev/null +++ b/kernel/src/driver/video/console/dummycon.rs @@ -0,0 +1,127 @@ +use alloc::sync::Arc; +use system_error::SystemError; + +use crate::driver::tty::{ + console::ConsoleSwitch, + termios::WindowSize, + tty_driver::TtyOperation, + virtual_terminal::virtual_console::{CursorOperation, ScrollDir, VirtualConsoleData}, +}; + +lazy_static! { + pub static ref DUMMY_CONSOLE: Arc = Arc::new(DummyConsole::new()); +} + +#[inline] +#[allow(dead_code)] +pub fn dummy_console() -> Arc { + DUMMY_CONSOLE.clone() +} + +pub struct DummyConsole; + +impl DummyConsole { + pub const COLUNMS: usize = 80; + pub const ROWS: usize = 25; + pub const fn new() -> Self { + DummyConsole + } +} + +impl ConsoleSwitch for DummyConsole { + fn con_getxy( + &self, + _vc_data: &VirtualConsoleData, + _pos: usize, + ) -> Result<(usize, usize, usize), SystemError> { + Ok((0, 0, 0)) + } + + fn con_build_attr( + &self, + _vc_data: &VirtualConsoleData, + _color: u8, + _intensity: crate::driver::tty::virtual_terminal::virtual_console::VirtualConsoleIntensity, + _blink: bool, + _underline: bool, + _reverse: bool, + _italic: bool, + ) -> Result { + Ok(0) + } + fn con_init(&self, vc_data: &mut VirtualConsoleData, init: bool) -> Result<(), SystemError> { + vc_data.color_mode = true; + + if init { + vc_data.cols = Self::COLUNMS; + vc_data.rows = Self::ROWS; + } else { + let tty = vc_data.port().port_data().tty().unwrap(); + tty.resize( + tty.clone(), + WindowSize::new(Self::ROWS as u16, Self::COLUNMS as u16, 0, 0), + )?; + } + + Ok(()) + } + + fn con_deinit(&self) -> Result<(), SystemError> { + Ok(()) + } + + fn con_clear( + &self, + _vc_data: &mut VirtualConsoleData, + _sy: usize, + _sx: usize, + _height: usize, + _width: usize, + ) -> Result<(), SystemError> { + Ok(()) + } + + fn con_putc( + &self, + _vc_data: &VirtualConsoleData, + _ch: u16, + _ypos: u32, + _xpos: u32, + ) -> Result<(), SystemError> { + Ok(()) + } + + fn con_putcs( + &self, + _vc_data: &VirtualConsoleData, + _buf: &[u16], + _count: usize, + _ypos: u32, + _xpos: u32, + ) -> Result<(), SystemError> { + Ok(()) + } + + fn con_cursor(&self, _vc_data: &VirtualConsoleData, _op: CursorOperation) { + // Do nothing + } + + fn con_set_palette( + &self, + _vc_data: &VirtualConsoleData, + _color_table: &[u8], + ) -> Result<(), SystemError> { + Ok(()) + } + + fn con_scroll( + &self, + _vc_data: &mut VirtualConsoleData, + _top: usize, + _bottom: usize, + _dir: ScrollDir, + _nr: usize, + ) -> bool { + false + } +} diff --git a/kernel/src/driver/video/console/mod.rs b/kernel/src/driver/video/console/mod.rs new file mode 100644 index 000000000..bbf88de4e --- /dev/null +++ b/kernel/src/driver/video/console/mod.rs @@ -0,0 +1 @@ +pub mod dummycon; diff --git a/kernel/src/driver/video/fbdev/base/fbcon/framebuffer_console.rs b/kernel/src/driver/video/fbdev/base/fbcon/framebuffer_console.rs index 9ffa4d106..d0a3040e6 100644 --- a/kernel/src/driver/video/fbdev/base/fbcon/framebuffer_console.rs +++ b/kernel/src/driver/video/fbdev/base/fbcon/framebuffer_console.rs @@ -40,11 +40,11 @@ impl BlittingFbConsole { }) } - pub fn fb(&self) -> Arc { + fn fb(&self) -> Arc { self.fb.lock().clone().unwrap() } - pub fn get_color(&self, vc_data: &VirtualConsoleData, c: u16, is_fg: bool) -> u32 { + fn get_color(&self, vc_data: &VirtualConsoleData, c: u16, is_fg: bool) -> u32 { let fb_info = self.fb(); let mut color = 0; @@ -104,7 +104,7 @@ impl BlittingFbConsole { } /// ## 计算单色调的函数 - pub fn mono_color(&self) -> u32 { + fn mono_color(&self) -> u32 { let fb_info = self.fb(); let mut max_len = fb_info .current_fb_var() @@ -117,7 +117,7 @@ impl BlittingFbConsole { return (!(0xfff << max_len)) & 0xff; } - pub fn bit_put_string( + fn bit_put_string( &self, vc_data: &VirtualConsoleData, buf: &[u16], diff --git a/kernel/src/driver/video/mod.rs b/kernel/src/driver/video/mod.rs index 12f765168..6a8c406d9 100644 --- a/kernel/src/driver/video/mod.rs +++ b/kernel/src/driver/video/mod.rs @@ -19,6 +19,7 @@ use crate::{ use alloc::{boxed::Box, sync::Arc}; use system_error::SystemError; +pub mod console; pub mod fbdev; static mut __MAMAGER: Option = None; diff --git a/kernel/src/init/initial_kthread.rs b/kernel/src/init/initial_kthread.rs index 5c14c4392..7f3ddc34c 100644 --- a/kernel/src/init/initial_kthread.rs +++ b/kernel/src/init/initial_kthread.rs @@ -34,6 +34,7 @@ fn kernel_init() -> Result<(), SystemError> { // 由于目前加锁,速度过慢,所以先不开启双缓冲 // scm_enable_double_buffer().expect("Failed to enable double buffer"); + #[cfg(target_arch = "x86_64")] ahci_init().expect("Failed to initialize AHCI"); mount_root_fs().expect("Failed to mount root fs");