Add early_sleep
All checks were successful
Build / build (push) Successful in 3m0s

This commit is contained in:
Mathieu Strypsteen 2024-10-30 18:15:18 +01:00
parent e427d58351
commit f58d15a197
4 changed files with 49 additions and 6 deletions

View file

@ -1,8 +1,11 @@
use core::{
ptr::null_mut,
sync::atomic::{AtomicPtr, AtomicU64, Ordering},
sync::atomic::{AtomicBool, AtomicPtr, AtomicU64, Ordering},
};
use kernel_common::instructions::pause;
use log::warn;
use crate::{
cpu::paging::map_physical,
sys::{
@ -20,11 +23,38 @@ const REGISTER_TIMER0_CONFIG: usize = 0x20;
const REGISTER_TIMER0_COMPARATOR: usize = 0x21;
const CONFIG_ENABLE: u64 = 1;
const CONFIG_LEGACY_REPLACEMENT: u64 = 2;
const TIMER_CONFIG_ENABLE: u64 = 4;
static ADDRESS: AtomicPtr<u64> = AtomicPtr::new(null_mut());
static PERIOD: AtomicU64 = AtomicU64::new(0);
static EARLY_SLEEP: AtomicBool = AtomicBool::new(false);
fn handler() {}
fn handler() {
if EARLY_SLEEP.load(Ordering::Relaxed) {
let address = ADDRESS.load(Ordering::Relaxed);
unsafe {
let current_config = address.add(REGISTER_TIMER0_CONFIG).read_volatile();
address.add(REGISTER_TIMER0_CONFIG).write_volatile(current_config & !TIMER_CONFIG_ENABLE);
}
EARLY_SLEEP.store(false, Ordering::Relaxed);
return;
}
warn!("Spurious HPET interrupt");
}
pub fn early_sleep(ms: usize) {
EARLY_SLEEP.store(true, Ordering::Relaxed);
unsafe {
let address = ADDRESS.load(Ordering::Relaxed);
let period = PERIOD.load(Ordering::Relaxed);
let current_counter = address.add(REGISTER_COUNTER).read_volatile();
address.add(REGISTER_TIMER0_COMPARATOR).write_volatile(current_counter + u64::pow(10, 12) / period * ms as u64);
let current_config = address.add(REGISTER_TIMER0_CONFIG).read_volatile();
address.add(REGISTER_TIMER0_CONFIG).write_volatile(current_config | TIMER_CONFIG_ENABLE);
}
while EARLY_SLEEP.load(Ordering::Relaxed) {
pause();
}
}
pub fn setup_hpet() {
let signature = b"HPET" as *const u8;
let address;
@ -42,7 +72,8 @@ pub fn setup_hpet() {
}
PERIOD.store(period, Ordering::Relaxed);
unsafe {
address.add(REGISTER_CONFIG).write_volatile(CONFIG_ENABLE | CONFIG_LEGACY_REPLACEMENT);
let current_config = address.add(REGISTER_CONFIG).read_volatile();
address.add(REGISTER_CONFIG).write_volatile(current_config | CONFIG_ENABLE | CONFIG_LEGACY_REPLACEMENT);
}
register_isa_irq_handler(0, handler);
}

View file

@ -1,4 +1,6 @@
use core::{arch::asm, mem::size_of};
use core::mem::size_of;
use kernel_common::instructions::sti;
use crate::sys::acpica_osl::AE_OK;
@ -67,6 +69,6 @@ pub fn parse_madt() {
subtable_address_i += subtable_header.Length as u64;
}
unsafe {
asm!("sti");
sti();
}
}

@ -1 +1 @@
Subproject commit 1d9c129746c737b68e642f9f29a894e2700fbf46
Subproject commit 993a25704ac18784b44c1af75cb54abec5141981

View file

@ -1,10 +1,20 @@
use core::arch::asm;
pub fn pause() {
unsafe {
asm!("pause");
}
}
pub fn cli() {
unsafe {
asm!("cli");
}
}
pub unsafe fn sti() {
unsafe {
asm!("sti");
}
}
pub fn hlt() {
unsafe {
asm!("hlt");