Switch page table for user process

master
WangRunji 7 years ago
parent 369f3183a3
commit 40b02c33cb

@ -18,12 +18,13 @@ pub fn init(mut page_map: impl FnMut(usize)) -> acpi::AcpiResult {
page_map(0x7fe1000); // RSDT page_map(0x7fe1000); // RSDT
let acpi = acpi::init().expect("Failed to init ACPI"); let acpi = acpi::init().expect("Failed to init ACPI");
assert_eq!(acpi.lapic_addr as usize, 0xfee00000);
debug!("{:?}", acpi); debug!("{:?}", acpi);
if cfg!(feature = "use_apic") { if cfg!(feature = "use_apic") {
pic::disable(); pic::disable();
page_map(acpi.lapic_addr as usize); // LAPIC page_map(0xfee00000); // LAPIC
page_map(0xFEC00000); // IOAPIC page_map(0xFEC00000); // IOAPIC
apic::init(acpi.lapic_addr, acpi.ioapic_id); apic::init(acpi.lapic_addr, acpi.ioapic_id);

@ -1,5 +1,6 @@
use memory::Frame; use memory::Frame;
#[derive(Copy, Clone)]
pub struct Entry(u64); pub struct Entry(u64);
impl Entry { impl Entry {

@ -76,8 +76,7 @@ impl Mapper {
let mut p1 = p2.next_table_create(page.p2_index()); let mut p1 = p2.next_table_create(page.p2_index());
assert!(p1[page.p1_index()].is_unused()); assert!(p1[page.p1_index()].is_unused());
// TODO: Remove USER_ACCESSIBLE p1[page.p1_index()].set(frame, flags | EntryFlags::PRESENT);
p1[page.p1_index()].set(frame, flags | EntryFlags::PRESENT | EntryFlags::USER_ACCESSIBLE);
} }
pub fn map(&mut self, page: Page, flags: EntryFlags) pub fn map(&mut self, page: Page, flags: EntryFlags)

@ -155,6 +155,7 @@ impl ActivePageTable {
} }
} }
#[derive(Debug)]
pub struct InactivePageTable { pub struct InactivePageTable {
p4_frame: Frame, p4_frame: Frame,
} }

@ -83,17 +83,18 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) -> ! {
unsafe{ arch::interrupt::enable(); } unsafe{ arch::interrupt::enable(); }
unsafe{ // 直接进入用户态暂不可用:内核代码用户不可访问
use arch::syscall; // unsafe{
// 在用户模式下触发时钟中断会导致GPF // use arch::syscall;
// (可能是由于没有合理分离栈) // // 在用户模式下触发时钟中断会导致GPF
no_interrupt!({ // // (可能是由于没有合理分离栈)
syscall::switch_to_user(); // no_interrupt!({
println!("Now in user mode"); // syscall::switch_to_user();
syscall::switch_to_kernel(); // println!("Now in user mode");
println!("Now in kernel mode"); // syscall::switch_to_kernel();
}); // println!("Now in kernel mode");
} // });
// }
loop{ loop{
println!("init ..."); println!("init ...");

@ -211,7 +211,18 @@ impl MemoryController {
} }
pub fn make_page_table(&mut self, set: &mut MemorySet) -> InactivePageTable { pub fn make_page_table(&mut self, set: &mut MemorySet) -> InactivePageTable {
let mut page_table = InactivePageTable::new(alloc_frame(), &mut self.active_table); let mut page_table = InactivePageTable::new(alloc_frame(), &mut self.active_table);
self.active_table.with(&mut page_table, |pt| set.map(pt));
use consts::{KERNEL_HEAP_PML4, KERNEL_PML4};
let e510 = self.active_table.p4()[KERNEL_PML4].clone();
let e509 = self.active_table.p4()[KERNEL_HEAP_PML4].clone();
self.active_table.with(&mut page_table, |pt: &mut Mapper| {
set.map(pt);
pt.p4_mut()[KERNEL_PML4] = e510;
pt.p4_mut()[KERNEL_HEAP_PML4] = e509;
pt.identity_map(Frame::of_addr(0xfee00000), EntryFlags::WRITABLE); // LAPIC
});
page_table page_table
} }
} }

@ -1,5 +1,5 @@
use super::*; use super::*;
use memory::Stack; use memory::{Stack, InactivePageTable};
use xmas_elf::{ElfFile, program::{Flags, ProgramHeader}}; use xmas_elf::{ElfFile, program::{Flags, ProgramHeader}};
use core::slice; use core::slice;
use alloc::rc::Rc; use alloc::rc::Rc;
@ -9,7 +9,8 @@ pub struct Process {
pub(in process) pid: Pid, pub(in process) pid: Pid,
name: &'static str, name: &'static str,
kstack: Stack, kstack: Stack,
// memory_set: Rc<MemorySet>, pub(in process) memory_set: Option<MemorySet>,
pub(in process) page_table: Option<InactivePageTable>,
pub(in process) status: Status, pub(in process) status: Status,
pub(in process) rsp: usize, pub(in process) rsp: usize,
pub(in process) is_user: bool, pub(in process) is_user: bool,
@ -33,6 +34,8 @@ impl Process {
pid: 0, pid: 0,
name, name,
kstack, kstack,
memory_set: None,
page_table: None,
status: Status::Ready, status: Status::Ready,
rsp, rsp,
is_user: false, is_user: false,
@ -46,6 +49,8 @@ impl Process {
pid: 0, pid: 0,
name: "init", name: "init",
kstack: mc.kernel_stack.take().unwrap(), kstack: mc.kernel_stack.take().unwrap(),
memory_set: None,
page_table: None,
status: Status::Running, status: Status::Running,
rsp: 0, // will be set at first schedule rsp: 0, // will be set at first schedule
is_user: false, is_user: false,
@ -56,9 +61,9 @@ impl Process {
let slice = unsafe{ slice::from_raw_parts(begin as *const u8, end - begin) }; let slice = unsafe{ slice::from_raw_parts(begin as *const u8, end - begin) };
let elf = ElfFile::new(slice).expect("failed to read elf"); let elf = ElfFile::new(slice).expect("failed to read elf");
let phys_start = PhysAddr::from_kernel_virtual(begin); let phys_start = PhysAddr::from_kernel_virtual(begin);
let mut set = MemorySet::from((&elf, phys_start)); let mut memory_set = MemorySet::from((&elf, phys_start));
let page_table = mc.make_page_table(&mut set); let page_table = mc.make_page_table(&mut memory_set);
debug!("{:#x?}", set); debug!("{:#x?}", memory_set);
use xmas_elf::header::HeaderPt2; use xmas_elf::header::HeaderPt2;
let entry_addr = match elf.header.pt2 { let entry_addr = match elf.header.pt2 {
@ -74,6 +79,8 @@ impl Process {
pid: 0, pid: 0,
name: "user", name: "user",
kstack, kstack,
memory_set: Some(memory_set),
page_table: Some(page_table),
status: Status::Ready, status: Status::Ready,
rsp, rsp,
is_user: true, is_user: true,
@ -107,7 +114,7 @@ impl<'a> From<(&'a ElfFile<'a>, PhysAddr)> for MemorySet {
impl From<Flags> for EntryFlags { impl From<Flags> for EntryFlags {
fn from(elf_flags: Flags) -> Self { fn from(elf_flags: Flags) -> Self {
let mut flags = EntryFlags::PRESENT; let mut flags = EntryFlags::PRESENT | EntryFlags::USER_ACCESSIBLE;
if elf_flags.is_write() { if elf_flags.is_write() {
flags = flags | EntryFlags::WRITABLE; flags = flags | EntryFlags::WRITABLE;
} }

@ -1,4 +1,5 @@
use alloc::BTreeMap; use alloc::BTreeMap;
use memory::{ActivePageTable, InactivePageTable};
use super::*; use super::*;
#[derive(Debug)] #[derive(Debug)]
@ -61,7 +62,13 @@ impl Processor {
let process = self.procs.get_mut(&pid).unwrap(); let process = self.procs.get_mut(&pid).unwrap();
process.status = Status::Running; process.status = Status::Running;
*rsp = process.rsp; *rsp = process.rsp;
// TODO switch page table
// switch page table
if let Some(page_table) = process.page_table.take() {
let mut active_table = unsafe { ActivePageTable::new() };
let old_table = active_table.switch(page_table);
process.page_table = Some(old_table);
}
} }
self.current_pid = pid; self.current_pid = pid;
debug!("Processor: switch from {} to {}\n rsp: {:#x} -> {:#x}", pid0, pid, rsp0, rsp); debug!("Processor: switch from {} to {}\n rsp: {:#x} -> {:#x}", pid0, pid, rsp0, rsp);

Loading…
Cancel
Save