Skip to content
Snippets Groups Projects
Commit 58314e6a authored by bjorn3's avatar bjorn3 Committed by Jeremy Soller
Browse files

Share a whole lot more code between x86 and x86_64

parent 78597508
Branches master
No related tags found
No related merge requests found
#[derive(Clone, Copy, Debug)]
#[repr(u8)]
pub enum IpiKind {
Wakeup = 0x40,
Tlb = 0x41,
Switch = 0x42,
Pit = 0x43,
}
#[derive(Clone, Copy, Debug)]
#[repr(u8)]
pub enum IpiTarget {
Current = 1,
All = 2,
Other = 3,
}
#[cfg(not(feature = "multi_core"))]
#[inline(always)]
pub fn ipi(_kind: IpiKind, _target: IpiTarget) {}
#[cfg(feature = "multi_core")]
#[inline(always)]
pub fn ipi(kind: IpiKind, target: IpiTarget) {
use crate::device::local_apic::LOCAL_APIC;
let icr = (target as u64) << 18 | 1 << 14 | (kind as u64);
unsafe { LOCAL_APIC.set_icr(icr) };
}
......@@ -9,9 +9,6 @@ pub mod consts;
/// CPUID wrapper
pub mod cpuid;
/// Debugging support
pub mod debug;
/// Global descriptor table
pub mod gdt;
......@@ -22,15 +19,9 @@ pub mod interrupt;
/// Interrupt descriptor table
pub mod idt;
/// Inter-processor interrupts
pub mod ipi;
/// Paging
pub mod paging;
/// Page table isolation
pub mod pti;
pub mod rmm;
/// Initialization and start function
......
use core::fmt;
#[cfg(feature = "qemu_debug")]
use spin::Mutex;
use spin::MutexGuard;
#[cfg(any(feature = "lpss_debug", feature = "serial_debug"))]
use crate::devices::uart_16550::SerialPort;
use crate::log::{Log, LOG};
#[cfg(feature = "lpss_debug")]
use crate::syscall::io::Mmio;
#[cfg(any(feature = "qemu_debug", feature = "serial_debug"))]
use crate::syscall::io::Pio;
#[cfg(feature = "qemu_debug")]
use syscall::io::Io;
#[cfg(feature = "serial_debug")]
use super::device::serial::COM1;
#[cfg(feature = "lpss_debug")]
use super::device::serial::LPSS;
#[cfg(feature = "system76_ec_debug")]
use super::device::system76_ec::{System76Ec, SYSTEM76_EC};
#[cfg(feature = "graphical_debug")]
use crate::devices::graphical_debug::{DebugDisplay, DEBUG_DISPLAY};
#[cfg(feature = "qemu_debug")]
pub static QEMU: Mutex<Pio<u8>> = Mutex::new(Pio::<u8>::new(0x402));
pub struct Writer<'a> {
log: MutexGuard<'a, Option<Log>>,
#[cfg(feature = "graphical_debug")]
display: MutexGuard<'a, Option<DebugDisplay>>,
#[cfg(feature = "lpss_debug")]
lpss: MutexGuard<'a, Option<&'static mut SerialPort<Mmio<u32>>>>,
#[cfg(feature = "qemu_debug")]
qemu: MutexGuard<'a, Pio<u8>>,
#[cfg(feature = "serial_debug")]
serial: MutexGuard<'a, SerialPort<Pio<u8>>>,
#[cfg(feature = "system76_ec_debug")]
system76_ec: MutexGuard<'a, Option<System76Ec>>,
}
impl<'a> Writer<'a> {
pub fn new() -> Writer<'a> {
Writer {
log: LOG.lock(),
#[cfg(feature = "graphical_debug")]
display: DEBUG_DISPLAY.lock(),
#[cfg(feature = "lpss_debug")]
lpss: LPSS.lock(),
#[cfg(feature = "qemu_debug")]
qemu: QEMU.lock(),
#[cfg(feature = "serial_debug")]
serial: COM1.lock(),
#[cfg(feature = "system76_ec_debug")]
system76_ec: SYSTEM76_EC.lock(),
}
}
pub fn write(&mut self, buf: &[u8]) {
{
if let Some(ref mut log) = *self.log {
log.write(buf);
}
}
#[cfg(feature = "graphical_debug")]
{
if let Some(ref mut display) = *self.display {
let _ = display.write(buf);
}
}
#[cfg(feature = "lpss_debug")]
{
if let Some(ref mut lpss) = *self.lpss {
lpss.write(buf);
}
}
#[cfg(feature = "qemu_debug")]
{
for &b in buf {
self.qemu.write(b);
}
}
#[cfg(feature = "serial_debug")]
{
self.serial.write(buf);
}
#[cfg(feature = "system76_ec_debug")]
{
if let Some(ref mut system76_ec) = *self.system76_ec {
system76_ec.print_slice(buf);
}
}
}
}
impl<'a> fmt::Write for Writer<'a> {
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
self.write(s.as_bytes());
Ok(())
}
}
......@@ -15,9 +15,6 @@ pub mod consts;
/// CPUID wrapper
pub mod cpuid;
/// Debugging support
pub mod debug;
/// Global descriptor table
pub mod gdt;
......@@ -28,18 +25,12 @@ pub mod interrupt;
/// Interrupt descriptor table
pub mod idt;
/// Inter-processor interrupts
pub mod ipi;
/// Miscellaneous processor features
pub mod misc;
/// Paging
pub mod paging;
/// Page table isolation
pub mod pti;
pub mod rmm;
/// Initialization and start function
......
#[cfg(feature = "pti")]
use core::ptr;
#[cfg(feature = "pti")]
use crate::memory::Frame;
#[cfg(feature = "pti")]
use crate::paging::entry::EntryFlags;
#[cfg(feature = "pti")]
use crate::paging::ActivePageTable;
#[cfg(feature = "pti")]
#[thread_local]
pub static mut PTI_CPU_STACK: [u8; 256] = [0; 256];
#[cfg(feature = "pti")]
#[thread_local]
pub static mut PTI_CONTEXT_STACK: usize = 0;
#[cfg(feature = "pti")]
#[inline(always)]
unsafe fn switch_stack(old: usize, new: usize) {
let old_rsp: usize;
asm!("", out("rsp") old_rsp);
let offset_rsp = old - old_rsp;
let new_rsp = new - offset_rsp;
ptr::copy_nonoverlapping(old_rsp as *const u8, new_rsp as *mut u8, offset_rsp);
asm!("", out("rsp") new_rsp);
}
#[cfg(feature = "pti")]
#[inline(always)]
pub unsafe fn map() {
// {
// let mut active_table = unsafe { ActivePageTable::new() };
//
// // Map kernel heap
// let address = active_table.p4()[::KERNEL_HEAP_PML4].address();
// let frame = Frame::containing_address(address);
// let mut flags = active_table.p4()[::KERNEL_HEAP_PML4].flags();
// flags.remove(EntryFlags::PRESENT);
// active_table.p4_mut()[::KERNEL_HEAP_PML4].set(frame, flags);
//
// // Reload page tables
// active_table.flush_all();
// }
// Switch to per-context stack
switch_stack(
PTI_CPU_STACK.as_ptr() as usize + PTI_CPU_STACK.len(),
PTI_CONTEXT_STACK,
);
}
#[cfg(feature = "pti")]
#[inline(always)]
pub unsafe extern "C" fn unmap() {
// Switch to per-CPU stack
switch_stack(
PTI_CONTEXT_STACK,
PTI_CPU_STACK.as_ptr() as usize + PTI_CPU_STACK.len(),
);
// {
// let mut active_table = unsafe { ActivePageTable::new() };
//
// // Unmap kernel heap
// let address = active_table.p4()[::KERNEL_HEAP_PML4].address();
// let frame = Frame::containing_address(address);
// let mut flags = active_table.p4()[::KERNEL_HEAP_PML4].flags();
// flags.insert(EntryFlags::PRESENT);
// active_table.p4_mut()[::KERNEL_HEAP_PML4].set(frame, flags);
//
// // Reload page tables
// active_table.flush_all();
// }
}
#[cfg(not(feature = "pti"))]
#[inline(always)]
pub unsafe fn map() {}
#[cfg(not(feature = "pti"))]
#[inline(always)]
pub unsafe extern "C" fn unmap() {}
File moved
File moved
/// Debugging support
pub mod debug;
/// Devices
pub mod device;
/// Inter-processor interrupts
pub mod ipi;
/// Page table isolation
pub mod pti;
/// Stop function
pub mod stop;
......
File moved
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment