Simplify code and resolve lints

This commit is contained in:
Ruihan Li 2025-11-28 10:49:08 +08:00 committed by Tate, Hongliang Tian
parent 4f71f4bbe6
commit bd67ea489e
3 changed files with 26 additions and 48 deletions

View File

@ -4,14 +4,14 @@ use super::SyscallReturn;
use crate::{prelude::*, process::credentials::capabilities::CapSet};
// Linux reboot magic constants.
const LINUX_REBOOT_MAGIC1: i32 = 0xfee1dead_u32 as i32;
const LINUX_REBOOT_MAGIC2: i32 = 0x28121969;
const LINUX_REBOOT_MAGIC2A: i32 = 0x05121996;
const LINUX_REBOOT_MAGIC2B: i32 = 0x16041998;
const LINUX_REBOOT_MAGIC2C: i32 = 0x20112000;
const LINUX_REBOOT_MAGIC1: u32 = 0xfee1dead;
const LINUX_REBOOT_MAGIC2: u32 = 0x28121969;
const LINUX_REBOOT_MAGIC2A: u32 = 0x05121996;
const LINUX_REBOOT_MAGIC2B: u32 = 0x16041998;
const LINUX_REBOOT_MAGIC2C: u32 = 0x20112000;
/// Linux reboot commands.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromInt)]
#[repr(u32)]
enum RebootCmd {
Restart = 0x01234567,
@ -20,42 +20,28 @@ enum RebootCmd {
// TODO: Add more reboot sub-commands.
}
impl TryFrom<i32> for RebootCmd {
type Error = Error;
fn try_from(value: i32) -> Result<Self> {
match value {
0x01234567 => Ok(Self::Restart),
0xcdef0123 => Ok(Self::Halt),
0x4321fedc => Ok(Self::PowerOff),
_ => return_errno_with_message!(Errno::EINVAL, "invalid reboot command"),
}
}
}
pub fn sys_reboot(
magic1: i32,
magic2: i32,
op: i32,
magic1: u32,
magic2: u32,
op: u32,
_arg: Vaddr,
ctx: &Context,
) -> Result<SyscallReturn> {
debug!(
"Reboot syscall invoked with magic1: {:#x}, magic2: {:#x}, op: {:#x}",
"[sys_reboot]: magic1 = {:#x}, magic2 = {:#x}, op = {:#x}",
magic1, magic2, op
);
// Verify magic numbers
if magic1 != LINUX_REBOOT_MAGIC1 {
return_errno_with_message!(Errno::EINVAL, "invalid magic1");
return_errno_with_message!(Errno::EINVAL, "the reboot magic is invalid");
}
if magic2 != LINUX_REBOOT_MAGIC2
&& magic2 != LINUX_REBOOT_MAGIC2A
&& magic2 != LINUX_REBOOT_MAGIC2B
&& magic2 != LINUX_REBOOT_MAGIC2C
{
return_errno_with_message!(Errno::EINVAL, "invalid magic2");
return_errno_with_message!(Errno::EINVAL, "the reboot magic is invalid");
}
if !ctx
@ -64,7 +50,7 @@ pub fn sys_reboot(
.effective_capset()
.contains(CapSet::SYS_BOOT)
{
return_errno_with_message!(Errno::EPERM, "insufficient capabilities for reboot");
return_errno_with_message!(Errno::EPERM, "reboot without SYS_BOOT is not allowed");
}
let cmd = RebootCmd::try_from(op)?;
@ -72,17 +58,11 @@ pub fn sys_reboot(
match cmd {
RebootCmd::Restart => {
// TODO: Implement restart functionality.
return_errno_with_message!(Errno::ENOSYS, "restart not implemented");
return_errno_with_message!(Errno::ENOSYS, "restart is not implemented yet");
}
RebootCmd::Halt | RebootCmd::PowerOff => {
poweroff();
// TODO: Perform any necessary cleanup before powering off.
ostd::arch::cpu::poweroff::poweroff();
}
}
}
fn poweroff() -> ! {
use ostd::arch::cpu::poweroff::poweroff;
// TODO: Perform any necessary cleanup before powering off.
poweroff()
}

View File

@ -104,7 +104,7 @@ pub(in crate::arch) fn query_xsave_area_size() -> Option<u32> {
/// Queries if the system is running in QEMU.
///
/// This function uses CPUID to detect QEMU hypervisor signature.
/// This function uses the CPUID instruction to detect the QEMU hypervisor signature.
pub(in crate::arch) fn query_is_running_in_qemu() -> bool {
let Some(result) = cpuid(Leaf::HypervisorBase as u32, 0) else {
return false;
@ -115,7 +115,7 @@ pub(in crate::arch) fn query_is_running_in_qemu() -> bool {
signature[4..8].copy_from_slice(&result.ecx.to_ne_bytes());
signature[8..12].copy_from_slice(&result.edx.to_ne_bytes());
// Check for QEMU hypervisor signature: "TCGTCGTCGTCG" or "KVMKVMKVM"
// Reference: https://wiki.osdev.org/QEMU_fw_cfg
signature == *b"TCGTCGTCGTCG" || signature.starts_with(b"KVMKVMKVM")
// Check for the QEMU hypervisor signature: "TCGTCGTCGTCG" or "KVMKVMKVM\0\0\0".
// Reference: <https://wiki.osdev.org/QEMU_fw_cfg#Detecting_QEMU>
matches!(&signature, b"TCGTCGTCGTCG" | b"KVMKVMKVM\0\0\0")
}

View File

@ -2,21 +2,14 @@
//! Panic support.
#![cfg_attr(target_arch = "loongarch64", expect(unused_imports))]
use core::ffi::c_void;
use crate::{
arch::qemu::{exit_qemu, QemuExitCode},
early_print, early_println,
sync::SpinLock,
early_println,
};
extern crate cfg_if;
extern crate gimli;
use gimli::Register;
/// The default panic handler for OSTD based kernels.
///
/// The user can override it by defining their own panic handler with the macro
@ -56,11 +49,16 @@ pub use unwinding::panic::{begin_panic, catch_unwind};
/// The printing procedure is protected by a spin lock to prevent interleaving.
#[cfg(not(target_arch = "loongarch64"))]
pub fn print_stack_trace() {
use core::ffi::c_void;
use gimli::Register;
use unwinding::abi::{
UnwindContext, UnwindReasonCode, _Unwind_Backtrace, _Unwind_FindEnclosingFunction,
_Unwind_GetGR, _Unwind_GetIP,
};
use crate::{early_print, sync::SpinLock};
/// We acquire a global lock to prevent the frames in the stack trace from
/// interleaving. The spin lock is used merely for its simplicity.
static BACKTRACE_PRINT_LOCK: SpinLock<()> = SpinLock::new(());