simple gui app can run now!

This commit is contained in:
Yu Chen 2023-01-07 16:11:41 +08:00
parent 944f114cf8
commit beaef1f304
11 changed files with 119 additions and 3 deletions

View file

@ -27,6 +27,10 @@ impl Graphics {
let fb = self.drv.get_framebuffer(); let fb = self.drv.get_framebuffer();
fb.fill(0u8); fb.fill(0u8);
} }
pub fn get_framebuffer(&self)-> &mut [u8] {
self.drv.get_framebuffer()
}
} }
impl OriginDimensions for Graphics { impl OriginDimensions for Graphics {

View file

@ -260,3 +260,4 @@ where
} }
} }
pub type VPNRange = SimpleRange<VirtPageNum>; pub type VPNRange = SimpleRange<VirtPageNum>;
pub type PPNRange = SimpleRange<PhysPageNum>;

View file

@ -18,6 +18,9 @@ impl FrameTracker {
} }
Self { ppn } Self { ppn }
} }
pub fn new_nowrite(ppn: PhysPageNum) -> Self {
Self { ppn }
}
} }
impl Debug for FrameTracker { impl Debug for FrameTracker {

View file

@ -1,7 +1,7 @@
use super::{frame_alloc, FrameTracker}; use super::{frame_alloc, FrameTracker};
use super::{PTEFlags, PageTable, PageTableEntry}; use super::{PTEFlags, PageTable, PageTableEntry};
use super::{PhysAddr, PhysPageNum, VirtAddr, VirtPageNum}; use super::{PhysAddr, PhysPageNum, VirtAddr, VirtPageNum};
use super::{StepByOne, VPNRange}; use super::{StepByOne, VPNRange, PPNRange};
use crate::config::{MEMORY_END, MMIO, PAGE_SIZE, TRAMPOLINE}; use crate::config::{MEMORY_END, MMIO, PAGE_SIZE, TRAMPOLINE};
use crate::sync::UPIntrFreeCell; use crate::sync::UPIntrFreeCell;
use alloc::collections::BTreeMap; use alloc::collections::BTreeMap;
@ -78,6 +78,10 @@ impl MemorySet {
} }
self.areas.push(map_area); self.areas.push(map_area);
} }
pub fn push_noalloc(&mut self, mut map_area: MapArea, ppn_range: PPNRange) {
map_area.map_noalloc(&mut self.page_table, ppn_range);
self.areas.push(map_area);
}
/// Mention that trampoline is not collected by areas. /// Mention that trampoline is not collected by areas.
fn map_trampoline(&mut self) { fn map_trampoline(&mut self) {
self.page_table.map( self.page_table.map(
@ -301,6 +305,14 @@ impl MapArea {
self.map_one(page_table, vpn); self.map_one(page_table, vpn);
} }
} }
pub fn map_noalloc(&mut self, page_table: &mut PageTable,ppn_range:PPNRange) {
for (vpn,ppn) in core::iter::zip(self.vpn_range,ppn_range) {
self.data_frames.insert(vpn, FrameTracker::new_nowrite(ppn));
let pte_flags = PTEFlags::from_bits(self.map_perm.bits).unwrap();
page_table.map(vpn, ppn, pte_flags);
}
}
pub fn unmap(&mut self, page_table: &mut PageTable) { pub fn unmap(&mut self, page_table: &mut PageTable) {
for vpn in self.vpn_range { for vpn in self.vpn_range {
self.unmap_one(page_table, vpn); self.unmap_one(page_table, vpn);

View file

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

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

@ -0,0 +1,37 @@
use crate::mm::{MapArea, MapPermission, MapType, PPNRange, PhysAddr};
use crate::task::current_process;
//use crate::gui::*;
use crate::drivers::GPU_DEVICE;
const FB_VADDR: usize = 0x10000000;
pub fn sys_framebuffer() -> isize {
let gpu = GPU_DEVICE.clone();
let fb = gpu.get_framebuffer();
let len = fb.len();
println!("[kernel] FrameBuffer: addr 0x{:X}, len {}", fb.as_ptr() as usize , len);
let fb_ppn = PhysAddr::from(fb.as_ptr() as usize).floor();
let fb_end_ppn = PhysAddr::from(fb.as_ptr() as usize + len).ceil();
let current_process = current_process();
let mut inner = current_process.inner_exclusive_access();
let mem_set = &mut inner.memory_set;
mem_set.push_noalloc(
MapArea::new(
(FB_VADDR as usize).into(),
(FB_VADDR + len as usize).into(),
MapType::Framed,
MapPermission::R | MapPermission::W | MapPermission::U,
),
PPNRange::new(fb_ppn, fb_end_ppn),
);
FB_VADDR as isize
}
pub fn sys_framebuffer_flush() -> isize {
let gpu = GPU_DEVICE.clone();
gpu.flush();
0
}

View file

@ -25,16 +25,20 @@ const SYSCALL_SEMAPHORE_DOWN: usize = 1022;
const SYSCALL_CONDVAR_CREATE: usize = 1030; const SYSCALL_CONDVAR_CREATE: usize = 1030;
const SYSCALL_CONDVAR_SIGNAL: usize = 1031; const SYSCALL_CONDVAR_SIGNAL: usize = 1031;
const SYSCALL_CONDVAR_WAIT: usize = 1032; const SYSCALL_CONDVAR_WAIT: usize = 1032;
const SYSCALL_FRAMEBUFFER: usize = 2000;
const SYSCALL_FRAMEBUFFER_FLUSH: usize = 2001;
mod fs; mod fs;
mod process; mod process;
mod sync; mod sync;
mod thread; mod thread;
mod gui;
use fs::*; use fs::*;
use process::*; use process::*;
use sync::*; use sync::*;
use thread::*; use thread::*;
use gui::*;
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize { pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
match syscall_id { match syscall_id {
@ -65,6 +69,8 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
SYSCALL_CONDVAR_CREATE => sys_condvar_create(args[0]), SYSCALL_CONDVAR_CREATE => sys_condvar_create(args[0]),
SYSCALL_CONDVAR_SIGNAL => sys_condvar_signal(args[0]), SYSCALL_CONDVAR_SIGNAL => sys_condvar_signal(args[0]),
SYSCALL_CONDVAR_WAIT => sys_condvar_wait(args[0], args[1]), SYSCALL_CONDVAR_WAIT => sys_condvar_wait(args[0], args[1]),
SYSCALL_FRAMEBUFFER => sys_framebuffer(),
SYSCALL_FRAMEBUFFER_FLUSH => sys_framebuffer_flush(),
_ => panic!("Unsupported syscall_id: {}", syscall_id), _ => panic!("Unsupported syscall_id: {}", syscall_id),
} }
} }

View file

@ -10,6 +10,7 @@ edition = "2018"
buddy_system_allocator = "0.6" buddy_system_allocator = "0.6"
bitflags = "1.2.1" bitflags = "1.2.1"
riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] }
embedded-graphics = "0.7.1"
[profile.release] [profile.release]
debug = true debug = true

View file

@ -0,0 +1,35 @@
#![no_std]
#![no_main]
use user_lib::{framebuffer, framebuffer_flush};
#[macro_use]
extern crate user_lib;
// 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 embedded_graphics::{
// draw_target::DrawTarget,
// prelude::{OriginDimensions},
// };
pub const VIRTGPU_XRES: usize = 1280;
pub const VIRTGPU_YRES: usize = 800;
#[no_mangle]
pub fn main() -> i32 {
let fb_ptr =framebuffer() as *mut u8;
println!("Hello world from user mode program! 0x{:X} , len {}", fb_ptr as usize, VIRTGPU_XRES*VIRTGPU_YRES*4);
let fb= unsafe {core::slice::from_raw_parts_mut(fb_ptr as *mut u8, VIRTGPU_XRES*VIRTGPU_YRES*4 as usize)};
for y in 0..800 {
for x in 0..1280 {
let idx = (y * 1280 + x) * 4;
fb[idx] = x as u8;
fb[idx + 1] = y as u8;
fb[idx + 2] = (x + y) as u8;
}
}
framebuffer_flush();
0
}

View file

@ -198,6 +198,12 @@ pub fn condvar_signal(condvar_id: usize) {
pub fn condvar_wait(condvar_id: usize, mutex_id: usize) { pub fn condvar_wait(condvar_id: usize, mutex_id: usize) {
sys_condvar_wait(condvar_id, mutex_id); sys_condvar_wait(condvar_id, mutex_id);
} }
pub fn framebuffer() -> isize {
sys_framebuffer()
}
pub fn framebuffer_flush() -> isize {
sys_framebuffer_flush()
}
#[macro_export] #[macro_export]
macro_rules! vstore { macro_rules! vstore {

View file

@ -25,6 +25,8 @@ const SYSCALL_SEMAPHORE_DOWN: usize = 1022;
const SYSCALL_CONDVAR_CREATE: usize = 1030; const SYSCALL_CONDVAR_CREATE: usize = 1030;
const SYSCALL_CONDVAR_SIGNAL: usize = 1031; const SYSCALL_CONDVAR_SIGNAL: usize = 1031;
const SYSCALL_CONDVAR_WAIT: usize = 1032; const SYSCALL_CONDVAR_WAIT: usize = 1032;
const SYSCALL_FRAMEBUFFER: usize = 2000;
const SYSCALL_FRAMEBUFFER_FLUSH: usize = 2001;
fn syscall(id: usize, args: [usize; 3]) -> isize { fn syscall(id: usize, args: [usize; 3]) -> isize {
let mut ret: isize; let mut ret: isize;
@ -154,3 +156,12 @@ pub fn sys_condvar_signal(condvar_id: usize) -> isize {
pub fn sys_condvar_wait(condvar_id: usize, mutex_id: usize) -> isize { pub fn sys_condvar_wait(condvar_id: usize, mutex_id: usize) -> isize {
syscall(SYSCALL_CONDVAR_WAIT, [condvar_id, mutex_id, 0]) syscall(SYSCALL_CONDVAR_WAIT, [condvar_id, mutex_id, 0])
} }
pub fn sys_framebuffer() -> isize {
syscall(SYSCALL_FRAMEBUFFER, [0, 0, 0])
}
pub fn sys_framebuffer_flush() -> isize {
syscall(SYSCALL_FRAMEBUFFER_FLUSH, [0, 0, 0])
}