Merge ch9

This commit is contained in:
yufeng 2023-02-06 19:51:24 +08:00
commit 55d9cfa1cd
57 changed files with 1909 additions and 678 deletions

View file

@ -17,7 +17,6 @@ volatile = "0.3"
virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers", rev = "4ee80e5" }
lose-net-stack = { git = "https://github.com/yfblock/lose-net-stack", rev = "3f467dd" }
easy-fs = { path = "../easy-fs" }
virtio-input-decoder = "0.1.4"
embedded-graphics = "0.7.1"
tinybmp = "0.3.1"

View file

@ -12,7 +12,7 @@ pub type CharDeviceImpl = crate::drivers::chardev::NS16550a<VIRT_UART>;
pub const VIRT_PLIC: usize = 0xC00_0000;
pub const VIRT_UART: usize = 0x1000_0000;
#[allow(unused)]
pub const VIRTGPU_XRES: u32 = 1280;
#[allow(unused)]
pub const VIRTGPU_YRES: u32 = 800;

View file

@ -131,7 +131,7 @@ pub struct NS16550a<const BASE_ADDR: usize> {
impl<const BASE_ADDR: usize> NS16550a<BASE_ADDR> {
pub fn new() -> Self {
let mut inner = NS16550aInner {
let inner = NS16550aInner {
ns16550a: NS16550aRaw::new(BASE_ADDR),
read_buffer: VecDeque::new(),
};
@ -141,14 +141,20 @@ impl<const BASE_ADDR: usize> NS16550a<BASE_ADDR> {
condvar: Condvar::new(),
}
}
pub fn read_buffer_is_empty(&self) -> bool {
self.inner
.exclusive_session(|inner| inner.read_buffer.is_empty())
}
}
impl<const BASE_ADDR: usize> CharDevice for NS16550a<BASE_ADDR> {
fn init(&self){
fn init(&self) {
let mut inner = self.inner.exclusive_access();
inner.ns16550a.init();
drop(inner);
}
}
fn read(&self) -> u8 {
loop {
let mut inner = self.inner.exclusive_access();

View file

@ -1,20 +1,28 @@
use crate::drivers::bus::virtio::VirtioHal;
use crate::{
gui::{move_rect, reset},
sync::UPIntrFreeCell,
};
use crate::sync::{Condvar, UPIntrFreeCell};
use crate::task::schedule;
use alloc::collections::VecDeque;
use alloc::sync::Arc;
use core::any::Any;
use virtio_drivers::{VirtIOHeader, VirtIOInput};
use virtio_input_decoder::{Decoder, Key, KeyType};
const VIRTIO5: usize = 0x10005000;
const VIRTIO6: usize = 0x10006000;
struct VirtIOInputWrapper(UPIntrFreeCell<VirtIOInput<'static, VirtioHal>>);
struct VirtIOInputInner {
virtio_input: VirtIOInput<'static, VirtioHal>,
events: VecDeque<u64>,
}
struct VirtIOInputWrapper {
inner: UPIntrFreeCell<VirtIOInputInner>,
condvar: Condvar,
}
pub trait InputDevice: Send + Sync + Any {
fn read_event(&self) -> u64;
fn handle_irq(&self);
fn is_empty(&self) -> bool;
}
lazy_static::lazy_static!(
@ -24,52 +32,52 @@ lazy_static::lazy_static!(
impl VirtIOInputWrapper {
pub fn new(addr: usize) -> Self {
Self(unsafe {
UPIntrFreeCell::new(
VirtIOInput::<VirtioHal>::new(&mut *(addr as *mut VirtIOHeader)).unwrap(),
)
})
let inner = VirtIOInputInner {
virtio_input: unsafe {
VirtIOInput::<VirtioHal>::new(&mut *(addr as *mut VirtIOHeader)).unwrap()
},
events: VecDeque::new(),
};
Self {
inner: unsafe { UPIntrFreeCell::new(inner) },
condvar: Condvar::new(),
}
}
}
impl InputDevice for VirtIOInputWrapper {
fn handle_irq(&self) {
let mut input = self.0.exclusive_access();
input.ack_interrupt();
while let Some(event) = input.pop_pending_event() {
let dtype = match Decoder::decode(
event.event_type as usize,
event.code as usize,
event.value as usize,
) {
Ok(dtype) => dtype,
Err(_) => break,
};
match dtype {
virtio_input_decoder::DecodeType::Key(key, r#type) => {
if r#type == KeyType::Press {
match key {
Key::C | Key::MouseLeft => {
reset();
}
Key::W => {
move_rect(0, -10);
}
Key::S => {
move_rect(0, 10);
}
Key::A => {
move_rect(-10, 0);
}
Key::D => {
move_rect(10, 0);
}
_ => {}
}
}
}
_ => {}
fn is_empty(&self) -> bool {
self.inner.exclusive_access().events.is_empty()
}
fn read_event(&self) -> u64 {
loop {
let mut inner = self.inner.exclusive_access();
if let Some(event) = inner.events.pop_front() {
return event;
} else {
let task_cx_ptr = self.condvar.wait_no_sched();
drop(inner);
schedule(task_cx_ptr);
}
}
}
fn handle_irq(&self) {
let mut count = 0;
let mut result = 0;
self.inner.exclusive_session(|inner| {
inner.virtio_input.ack_interrupt();
while let Some(event) = inner.virtio_input.pop_pending_event() {
count += 1;
result = (event.event_type as u64) << 48
| (event.code as u64) << 32
| (event.value) as u64;
inner.events.push_back(result);
}
});
if count > 0 {
self.condvar.signal();
};
}
}

View file

@ -170,4 +170,4 @@ impl File for Pipe {
}
}
}
}
}

View file

@ -1,63 +0,0 @@
use alloc::sync::Arc;
use embedded_graphics::{
draw_target::DrawTarget,
pixelcolor::Rgb888,
prelude::{OriginDimensions, Point, RgbColor, Size},
};
use crate::board::VIRTGPU_XRES;
use crate::drivers::{GpuDevice, GPU_DEVICE};
#[derive(Clone)]
pub struct Graphics {
pub size: Size,
pub point: Point,
pub drv: Arc<dyn GpuDevice>,
}
impl Graphics {
pub fn new(size: Size, point: Point) -> Self {
Self {
size,
point,
drv: GPU_DEVICE.clone(),
}
}
pub fn reset(&self) {
let fb = self.drv.get_framebuffer();
fb.fill(0u8);
}
}
impl OriginDimensions for Graphics {
fn size(&self) -> Size {
self.size
}
}
impl DrawTarget for Graphics {
type Color = Rgb888;
type Error = core::convert::Infallible;
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
{
let fb = self.drv.get_framebuffer();
pixels.into_iter().for_each(|px| {
let idx = ((self.point.y + px.0.y) * VIRTGPU_XRES as i32 + self.point.x + px.0.x)
as usize
* 4;
if idx + 2 >= fb.len() {
return;
}
fb[idx] = px.1.b();
fb[idx + 1] = px.1.g();
fb[idx + 2] = px.1.r();
});
self.drv.flush();
Ok(())
}
}

View file

@ -1,5 +0,0 @@
mod graphic;
mod paint;
use graphic::Graphics;
pub use paint::{init_paint, move_rect, reset};

View file

@ -1,62 +0,0 @@
use super::Graphics;
use crate::sync::UPIntrFreeCell;
use embedded_graphics::pixelcolor::Rgb888;
use embedded_graphics::prelude::{Drawable, Point, RgbColor, Size};
use embedded_graphics::primitives::Primitive;
use embedded_graphics::primitives::{PrimitiveStyle, Rectangle};
use lazy_static::*;
const INIT_X: i32 = 640;
const INIT_Y: i32 = 400;
const RECT_SIZE: u32 = 40;
pub struct DrawingBoard {
graphics: Graphics,
latest_pos: Point,
}
impl DrawingBoard {
pub fn new() -> Self {
Self {
graphics: Graphics::new(Size::new(1280, 800), Point::new(0, 0)),
latest_pos: Point::new(INIT_X, INIT_Y),
}
}
fn paint(&mut self) {
Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE))
.into_styled(PrimitiveStyle::with_stroke(Rgb888::WHITE, 1))
.draw(&mut self.graphics)
.ok();
}
pub fn move_rect(&mut self, dx: i32, dy: i32) {
self.latest_pos.x += dx;
self.latest_pos.y += dy;
self.paint();
}
pub fn reset(&mut self) {
self.latest_pos = Point::new(INIT_X, INIT_Y);
self.graphics.reset();
}
}
lazy_static! {
pub static ref DRAWING_BOARD: UPIntrFreeCell<DrawingBoard> = unsafe { UPIntrFreeCell::new(DrawingBoard::new()) };
}
pub fn init_paint() {
DRAWING_BOARD.exclusive_session(|ripple| {
ripple.paint();
});
}
pub fn move_rect(dx: i32, dy: i32) {
DRAWING_BOARD.exclusive_session(|ripple| {
ripple.move_rect(dx, dy);
});
}
pub fn reset() {
DRAWING_BOARD.exclusive_session(|ripple| {
ripple.reset();
});
}

View file

@ -5,8 +5,8 @@
#![feature(naked_functions)]
#![feature(fn_align)]
//use crate::drivers::{GPU_DEVICE, KEYBOARD_DEVICE, MOUSE_DEVICE, INPUT_CONDVAR};
use crate::drivers::{GPU_DEVICE, KEYBOARD_DEVICE, MOUSE_DEVICE};
extern crate alloc;
#[macro_use]
@ -20,7 +20,6 @@ mod console;
mod config;
mod drivers;
mod fs;
mod gui;
mod lang_items;
mod mm;
mod sbi;
@ -256,8 +255,6 @@ pub fn rust_main() -> ! {
//clear_bss();
mm::init();
UART.init();
println!("KERN: begin");
//loop{};
println!("KERN: init gpu");
let _gpu = GPU_DEVICE.clone();
println!("KERN: init keyboard");
@ -270,7 +267,6 @@ pub fn rust_main() -> ! {
//timer::set_next_trigger();
board::device_init();
fs::list_apps();
//gui::init_paint();
task::add_initproc();
*DEV_NON_BLOCKING_ACCESS.exclusive_access() = true;
task::run_tasks();

View file

@ -71,7 +71,10 @@ impl MemorySet {
self.areas.remove(idx);
}
}
fn push(&mut self, mut map_area: MapArea, data: Option<&[u8]>) {
/// Add a new MapArea into this MemorySet.
/// Assuming that there are no conflicts in the virtual address
/// space.
pub fn push(&mut self, mut map_area: MapArea, data: Option<&[u8]>) {
map_area.map(&mut self.page_table);
if let Some(data) = data {
map_area.copy_data(&mut self.page_table, data);
@ -286,6 +289,11 @@ impl MapArea {
ppn = frame.ppn;
self.data_frames.insert(vpn, frame);
}
MapType::Linear(pn_offset) => {
// check for sv39
assert!(vpn.0 < (1usize << 27));
ppn = PhysPageNum((vpn.0 as isize + pn_offset) as usize);
}
}
let pte_flags = PTEFlags::from_bits(self.map_perm.bits).unwrap();
page_table.map(vpn, ppn, pte_flags);
@ -334,6 +342,8 @@ impl MapArea {
pub enum MapType {
Identical,
Framed,
/// offset of page num
Linear(isize),
}
bitflags! {

View file

@ -4,11 +4,11 @@ mod heap_allocator;
mod memory_set;
mod page_table;
use address::VPNRange;
pub use address::VPNRange;
pub use address::{PhysAddr, PhysPageNum, StepByOne, VirtAddr, VirtPageNum};
pub use frame_allocator::{frame_alloc, frame_alloc_more, frame_dealloc, FrameTracker};
pub use memory_set::remap_test;
pub use memory_set::{kernel_token, MapPermission, MemorySet, KERNEL_SPACE};
pub use memory_set::{kernel_token, MapArea, MapPermission, MapType, MemorySet, KERNEL_SPACE};
use page_table::PTEFlags;
pub use page_table::{
translated_byte_buffer, translated_ref, translated_refmut, translated_str, PageTable,

View file

@ -1,6 +1,6 @@
use crate::sync::{Mutex, UPIntrFreeCell};
use crate::task::{
add_task, block_current_and_run_next, block_current_task, current_task, TaskContext,
wakeup_task, block_current_and_run_next, block_current_task, current_task, TaskContext,
TaskControlBlock,
};
use alloc::{collections::VecDeque, sync::Arc};
@ -27,7 +27,7 @@ impl Condvar {
pub fn signal(&self) {
let mut inner = self.inner.exclusive_access();
if let Some(task) = inner.wait_queue.pop_front() {
add_task(task);
wakeup_task(task);
}
}

View file

@ -1,6 +1,6 @@
use super::UPIntrFreeCell;
use crate::task::TaskControlBlock;
use crate::task::{add_task, current_task};
use crate::task::{wakeup_task, current_task};
use crate::task::{block_current_and_run_next, suspend_current_and_run_next};
use alloc::{collections::VecDeque, sync::Arc};
@ -80,7 +80,7 @@ impl Mutex for MutexBlocking {
let mut mutex_inner = self.inner.exclusive_access();
assert!(mutex_inner.locked);
if let Some(waking_task) = mutex_inner.wait_queue.pop_front() {
add_task(waking_task);
wakeup_task(waking_task);
} else {
mutex_inner.locked = false;
}

View file

@ -1,5 +1,5 @@
use crate::sync::UPIntrFreeCell;
use crate::task::{add_task, block_current_and_run_next, current_task, TaskControlBlock};
use crate::task::{wakeup_task, block_current_and_run_next, current_task, TaskControlBlock};
use alloc::{collections::VecDeque, sync::Arc};
pub struct Semaphore {
@ -28,7 +28,7 @@ impl Semaphore {
inner.count += 1;
if inner.count <= 0 {
if let Some(task) = inner.wait_queue.pop_front() {
add_task(task);
wakeup_task(task);
}
}
}

34
os/src/syscall/gui.rs Normal file
View file

@ -0,0 +1,34 @@
use crate::drivers::GPU_DEVICE;
use crate::mm::{MapArea, MapPermission, MapType, PhysAddr, VirtAddr};
use crate::task::current_process;
const FB_VADDR: usize = 0x10000000;
pub fn sys_framebuffer() -> isize {
let fb = GPU_DEVICE.get_framebuffer();
let len = fb.len();
// println!("[kernel] FrameBuffer: addr 0x{:X}, len {}", fb.as_ptr() as usize , len);
let fb_start_pa = PhysAddr::from(fb.as_ptr() as usize);
assert!(fb_start_pa.aligned());
let fb_start_ppn = fb_start_pa.floor();
let fb_start_vpn = VirtAddr::from(FB_VADDR).floor();
let pn_offset = fb_start_ppn.0 as isize - fb_start_vpn.0 as isize;
let current_process = current_process();
let mut inner = current_process.inner_exclusive_access();
inner.memory_set.push(
MapArea::new(
(FB_VADDR as usize).into(),
(FB_VADDR + len as usize).into(),
MapType::Linear(pn_offset),
MapPermission::R | MapPermission::W | MapPermission::U,
),
None,
);
FB_VADDR as isize
}
pub fn sys_framebuffer_flush() -> isize {
GPU_DEVICE.flush();
0
}

28
os/src/syscall/input.rs Normal file
View file

@ -0,0 +1,28 @@
//use crate::drivers::{KEYBOARD_DEVICE,MOUSE_DEVICE,INPUT_CONDVAR,read_input_event};
use crate::drivers::{KEYBOARD_DEVICE, MOUSE_DEVICE};
pub fn sys_event_get() -> isize {
let kb = KEYBOARD_DEVICE.clone();
let mouse = MOUSE_DEVICE.clone();
//let input=INPUT_CONDVAR.clone();
//read_input_event() as isize
if !kb.is_empty() {
kb.read_event() as isize
} else if !mouse.is_empty() {
mouse.read_event() as isize
} else {
0
}
}
use crate::drivers::chardev::UART;
/// check UART's read-buffer is empty or not
pub fn sys_key_pressed() -> isize {
let res = !UART.read_buffer_is_empty();
if res {
1
} else {
0
}
}

View file

@ -26,14 +26,22 @@ const SYSCALL_SEMAPHORE_DOWN: usize = 1022;
const SYSCALL_CONDVAR_CREATE: usize = 1030;
const SYSCALL_CONDVAR_SIGNAL: usize = 1031;
const SYSCALL_CONDVAR_WAIT: usize = 1032;
const SYSCALL_FRAMEBUFFER: usize = 2000;
const SYSCALL_FRAMEBUFFER_FLUSH: usize = 2001;
const SYSCALL_EVENT_GET: usize = 3000;
const SYSCALL_KEY_PRESSED: usize = 3001;
mod fs;
mod gui;
mod input;
mod process;
mod sync;
mod thread;
mod net;
use fs::*;
use gui::*;
use input::*;
use process::*;
use sync::*;
use thread::*;
@ -66,9 +74,13 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
SYSCALL_SEMAPHORE_CREATE => sys_semaphore_create(args[0]),
SYSCALL_SEMAPHORE_UP => sys_semaphore_up(args[0]),
SYSCALL_SEMAPHORE_DOWN => sys_semaphore_down(args[0]),
SYSCALL_CONDVAR_CREATE => sys_condvar_create(args[0]),
SYSCALL_CONDVAR_CREATE => sys_condvar_create(),
SYSCALL_CONDVAR_SIGNAL => sys_condvar_signal(args[0]),
SYSCALL_CONDVAR_WAIT => sys_condvar_wait(args[0], args[1]),
SYSCALL_FRAMEBUFFER => sys_framebuffer(),
SYSCALL_FRAMEBUFFER_FLUSH => sys_framebuffer_flush(),
SYSCALL_EVENT_GET => sys_event_get(),
SYSCALL_KEY_PRESSED => sys_key_pressed(),
_ => panic!("Unsupported syscall_id: {}", syscall_id),
}
}

View file

@ -93,7 +93,7 @@ pub fn sys_semaphore_down(sem_id: usize) -> isize {
0
}
pub fn sys_condvar_create(_arg: usize) -> isize {
pub fn sys_condvar_create() -> isize {
let process = current_process();
let mut process_inner = process.inner_exclusive_access();
let id = if let Some(id) = process_inner

View file

@ -1,4 +1,4 @@
use super::{ProcessControlBlock, TaskControlBlock};
use super::{ProcessControlBlock, TaskControlBlock, TaskStatus};
use crate::sync::UPIntrFreeCell;
use alloc::collections::{BTreeMap, VecDeque};
use alloc::sync::Arc;
@ -34,6 +34,13 @@ pub fn add_task(task: Arc<TaskControlBlock>) {
TASK_MANAGER.exclusive_access().add(task);
}
pub fn wakeup_task(task: Arc<TaskControlBlock>) {
let mut task_inner = task.inner_exclusive_access();
task_inner.task_status = TaskStatus::Ready;
drop(task_inner);
add_task(task);
}
pub fn fetch_task() -> Option<Arc<TaskControlBlock>> {
TASK_MANAGER.exclusive_access().fetch()
}

View file

@ -18,7 +18,7 @@ use switch::__switch;
pub use context::TaskContext;
pub use id::{kstack_alloc, pid_alloc, KernelStack, PidHandle, IDLE_PID};
pub use manager::{add_task, pid2process, remove_from_pid2process};
pub use manager::{add_task, wakeup_task, pid2process, remove_from_pid2process};
pub use processor::{
current_kstack_top, current_process, current_task, current_trap_cx, current_trap_cx_user_va,
current_user_token, run_tasks, schedule, take_current_task,
@ -48,7 +48,7 @@ pub fn suspend_current_and_run_next() {
pub fn block_current_task() -> *mut TaskContext {
let task = take_current_task().unwrap();
let mut task_inner = task.inner_exclusive_access();
task_inner.task_status = TaskStatus::Blocking;
task_inner.task_status = TaskStatus::Blocked;
&mut task_inner.task_cx as *mut TaskContext
}

View file

@ -76,5 +76,5 @@ impl TaskControlBlock {
pub enum TaskStatus {
Ready,
Running,
Blocking,
Blocked,
}

View file

@ -3,7 +3,7 @@ use core::cmp::Ordering;
use crate::config::CLOCK_FREQ;
use crate::sbi::set_timer;
use crate::sync::UPIntrFreeCell;
use crate::task::{add_task, TaskControlBlock};
use crate::task::{wakeup_task, TaskControlBlock};
use alloc::collections::BinaryHeap;
use alloc::sync::Arc;
use lazy_static::*;
@ -64,7 +64,7 @@ pub fn check_timer() {
TIMERS.exclusive_session(|timers| {
while let Some(timer) = timers.peek() {
if timer.expire_ms <= current_ms {
add_task(Arc::clone(&timer.task));
wakeup_task(Arc::clone(&timer.task));
timers.pop();
} else {
break;