Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6552fa8059 | |||
| 0ff48fd520 |
@@ -4,6 +4,8 @@ const EXT_HSM: u64 = 0x48534D;
|
||||
const EXT_TIME: u64 = 0x54494D45;
|
||||
const EXT_DBCN: u64 = 0x4442434E;
|
||||
const EXT_SPI: u64 = 0x735049;
|
||||
const EXT_SYSTEM_SHUTDOWN: u64 = 0x53525354;
|
||||
const EXT_SYSTEM_SHUTDOWN_LEGACY: u64 = 0x08;
|
||||
|
||||
primitive_enum! {
|
||||
pub enum Status: i64 {
|
||||
@@ -108,3 +110,9 @@ pub fn sbi_debug_console_write_byte(byte: u8) {
|
||||
pub fn sbi_set_timer(next_event: u64) {
|
||||
unsafe { sbi_do_call(EXT_TIME, 0x00, next_event, 0, 0, 0, 0, 0) }.ok();
|
||||
}
|
||||
|
||||
pub fn sbi_system_shutdown() -> ! {
|
||||
unsafe { sbi_do_call(EXT_SYSTEM_SHUTDOWN, 0x00, 0, 0, 0, 0, 0, 0) }.ok();
|
||||
unsafe { sbi_do_call(EXT_SYSTEM_SHUTDOWN_LEGACY, 0x00, 0, 0, 0, 0, 0, 0) }.ok();
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#![feature(allocator_api)]
|
||||
#![feature(allocator_api, never_type)]
|
||||
#![no_std]
|
||||
|
||||
use acpi::AcpiTables;
|
||||
use acpi_system::{AcpiInterruptMethod, AcpiSystem};
|
||||
use acpi_system::{AcpiInterruptMethod, AcpiSleepState, AcpiSystem};
|
||||
use alloc::boxed::Box;
|
||||
use libk::error::Error;
|
||||
use libk_util::{sync::IrqSafeSpinlock, OneTimeInit};
|
||||
@@ -45,6 +45,17 @@ pub fn get_pci_route(
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub fn power_off() -> Result<!, Error> {
|
||||
let system = ACPI_SYSTEM.get();
|
||||
unsafe {
|
||||
system.lock().enter_sleep_state(AcpiSleepState::S5).ok();
|
||||
|
||||
loop {
|
||||
core::arch::asm!("cli; hlt");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes ACPI management
|
||||
pub fn switch_to_acpi(tables: &'static AcpiTables<AcpiHandlerImpl>) -> Result<(), Error> {
|
||||
// NOTE mostly broken for real HW
|
||||
@@ -67,12 +78,6 @@ pub fn switch_to_acpi(tables: &'static AcpiTables<AcpiHandlerImpl>) -> Result<()
|
||||
// // 6. Do something with the devices
|
||||
// // 7. Actually enter the S5 state
|
||||
|
||||
// unsafe {
|
||||
// PLATFORM
|
||||
// .send_ipi(IpiDeliveryTarget::OtherCpus, IpiMessage::Shutdown)
|
||||
// .unwrap();
|
||||
// }
|
||||
|
||||
// SHUTDOWN_FENCE.signal();
|
||||
// SHUTDOWN_FENCE.wait_all(CPU_COUNT.load(Ordering::Acquire));
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use alloc::sync::Arc;
|
||||
use device_api::{
|
||||
device::{Device, DeviceInitContext},
|
||||
interrupt::{FullIrq, InterruptHandler, IrqVector},
|
||||
interrupt::{InterruptHandler, IrqHandle, IrqVector},
|
||||
};
|
||||
use device_tree::driver::{device_tree_driver, Node, ProbeContext};
|
||||
use libk::{
|
||||
debug::DebugSink,
|
||||
device::{external_interrupt_controller, manager::DEVICE_REGISTRY},
|
||||
device::manager::DEVICE_REGISTRY,
|
||||
vfs::{Terminal, TerminalInput, TerminalOutput},
|
||||
};
|
||||
use libk_mm::{address::PhysicalAddress, device::DeviceMemoryIo};
|
||||
@@ -68,7 +68,7 @@ struct Pl011Inner {
|
||||
pub struct Pl011 {
|
||||
inner: OneTimeInit<Arc<Terminal<Pl011Inner>>>,
|
||||
base: PhysicalAddress,
|
||||
irq: FullIrq,
|
||||
irq: IrqHandle,
|
||||
}
|
||||
|
||||
impl Io {
|
||||
@@ -164,14 +164,12 @@ impl Device for Pl011 {
|
||||
}
|
||||
|
||||
unsafe fn init_irq(self: Arc<Self>) -> Result<(), Error> {
|
||||
let intc = external_interrupt_controller()?;
|
||||
|
||||
intc.register_irq(self.irq.irq, self.irq.options, self.clone())?;
|
||||
self.irq.register(self.clone())?;
|
||||
{
|
||||
let io = self.inner.get().output().io.lock();
|
||||
io.regs.IMSC.modify(IMSC::RXIM::SET);
|
||||
}
|
||||
intc.enable_irq(self.irq.irq)?;
|
||||
self.irq.enable()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use device_api::{
|
||||
GpioController, GpioInterruptEvent, GpioInterruptMode, GpioPinLevel, PinHandle,
|
||||
SinglePinDirection,
|
||||
},
|
||||
interrupt::{FullIrq, InterruptHandler, IrqVector},
|
||||
interrupt::{InterruptHandler, IrqHandle, IrqVector},
|
||||
};
|
||||
use device_tree::{
|
||||
driver::{
|
||||
@@ -17,7 +17,7 @@ use device_tree::{
|
||||
},
|
||||
DeviceTreePropertyRead, TProp,
|
||||
};
|
||||
use libk::{device::external_interrupt_controller, event::signal_gpio_event};
|
||||
use libk::event::signal_gpio_event;
|
||||
use libk_mm::device::DeviceMemoryIo;
|
||||
use libk_util::{bit::BitField, sync::IrqSafeSpinlock};
|
||||
use tock_registers::{
|
||||
@@ -50,7 +50,7 @@ register_structs! {
|
||||
struct Pl061 {
|
||||
#[allow(unused)]
|
||||
regs: IrqSafeSpinlock<DeviceMemoryIo<'static, Regs>>,
|
||||
irq: FullIrq,
|
||||
irq: IrqHandle,
|
||||
clocks: Vec<ClockHandle>,
|
||||
resets: Vec<ResetHandle>,
|
||||
gpio_events: [AtomicU64; 8],
|
||||
@@ -69,9 +69,8 @@ impl Device for Pl061 {
|
||||
}
|
||||
|
||||
unsafe fn init_irq(self: Arc<Self>) -> Result<(), Error> {
|
||||
let intc = external_interrupt_controller()?;
|
||||
intc.register_irq(self.irq.irq, self.irq.options, self.clone())?;
|
||||
intc.enable_irq(self.irq.irq)?;
|
||||
self.irq.register(self.clone())?;
|
||||
self.irq.enable()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,12 @@ use alloc::sync::Arc;
|
||||
use device_api::{
|
||||
clock::ClockHandle,
|
||||
device::{Device, DeviceInitContext},
|
||||
interrupt::{FullIrq, InterruptHandler, IrqVector},
|
||||
interrupt::{InterruptHandler, IrqHandle, IrqVector},
|
||||
};
|
||||
use device_tree::driver::{device_tree_driver, Node, ProbeContext};
|
||||
use libk::{
|
||||
debug::DebugSink,
|
||||
device::{external_interrupt_controller, manager::DEVICE_REGISTRY},
|
||||
device::manager::DEVICE_REGISTRY,
|
||||
vfs::{Terminal, TerminalInput, TerminalOutput},
|
||||
};
|
||||
use libk_mm::{address::PhysicalAddress, device::DeviceMemoryIo};
|
||||
@@ -61,7 +61,7 @@ struct Inner {
|
||||
/// Broadcom 283x mini-UART driver
|
||||
pub struct Bcm2835AuxUart {
|
||||
base: PhysicalAddress,
|
||||
irq: FullIrq,
|
||||
irq: IrqHandle,
|
||||
clock: ClockHandle,
|
||||
inner: OneTimeInit<Arc<Terminal<Inner>>>,
|
||||
}
|
||||
@@ -171,9 +171,8 @@ impl Device for Bcm2835AuxUart {
|
||||
}
|
||||
|
||||
unsafe fn init_irq(self: Arc<Self>) -> Result<(), Error> {
|
||||
let intc = external_interrupt_controller()?;
|
||||
intc.register_irq(self.irq.irq, self.irq.options, self.clone())?;
|
||||
intc.enable_irq(self.irq.irq)?;
|
||||
self.irq.register(self.clone())?;
|
||||
self.irq.enable()?;
|
||||
|
||||
let inner = self.inner.get().output();
|
||||
let regs = inner.regs.lock();
|
||||
|
||||
@@ -2,7 +2,7 @@ use alloc::sync::Arc;
|
||||
use device_api::{
|
||||
device::{Device, DeviceInitContext},
|
||||
gpio::{GpioController, GpioInterruptEvent, PinHandle},
|
||||
interrupt::{FullIrq, InterruptHandler, IrqVector},
|
||||
interrupt::{InterruptHandler, IrqHandle, IrqVector},
|
||||
};
|
||||
use device_tree::{
|
||||
driver::{
|
||||
@@ -11,7 +11,6 @@ use device_tree::{
|
||||
},
|
||||
DeviceTreePropertyRead, TProp,
|
||||
};
|
||||
use libk::device::external_interrupt_controller;
|
||||
use libk_mm::device::DeviceMemoryIo;
|
||||
use libk_util::sync::IrqSafeSpinlock;
|
||||
use tock_registers::{
|
||||
@@ -69,7 +68,7 @@ register_structs! {
|
||||
struct Bcm2711Gpio {
|
||||
regs: IrqSafeSpinlock<DeviceMemoryIo<'static, Regs>>,
|
||||
#[allow(unused)]
|
||||
irqs: [FullIrq; 2],
|
||||
irqs: [IrqHandle; 2],
|
||||
}
|
||||
|
||||
impl Regs {
|
||||
@@ -142,10 +141,9 @@ impl Device for Bcm2711Gpio {
|
||||
regs.configure_pin_interrupts(pin, false, false, false, false);
|
||||
}
|
||||
|
||||
let intc = external_interrupt_controller()?;
|
||||
for irq in self.irqs.iter() {
|
||||
intc.register_irq(irq.irq, irq.options, self.clone())?;
|
||||
intc.enable_irq(irq.irq)?;
|
||||
irq.register(self.clone())?;
|
||||
irq.enable()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -2,8 +2,8 @@ use alloc::{sync::Arc, vec::Vec};
|
||||
use device_api::{
|
||||
device::{Device, DeviceInitContext},
|
||||
interrupt::{
|
||||
ExternalInterruptController, FixedInterruptTable, FullIrq, InterruptHandler,
|
||||
InterruptTable, Irq, IrqOptions, IrqVector,
|
||||
ExternalInterruptController, FixedInterruptTable, InterruptHandler, InterruptTable, Irq,
|
||||
IrqHandle, IrqOptions, IrqVector,
|
||||
},
|
||||
};
|
||||
use device_tree::{
|
||||
@@ -261,9 +261,9 @@ impl Device for Plic {
|
||||
}
|
||||
|
||||
impl DeviceTreeInterruptController for Plic {
|
||||
fn map_interrupt(&self, property: &TProp, offset: usize) -> Option<FullIrq> {
|
||||
fn map_interrupt(&self, property: &TProp, offset: usize) -> Option<IrqHandle> {
|
||||
let num = property.read_cell(offset, 1)?;
|
||||
Some(FullIrq {
|
||||
Some(IrqHandle {
|
||||
irq: Irq::External(num as _),
|
||||
options: IrqOptions::default(),
|
||||
})
|
||||
|
||||
@@ -8,14 +8,13 @@ use device_api::{
|
||||
clock::{ClockHandle, ResetHandle},
|
||||
device::{Device, DeviceInitContext},
|
||||
dma::DmaAllocator,
|
||||
interrupt::{FullIrq, InterruptHandler, IrqVector},
|
||||
interrupt::{InterruptHandler, IrqHandle, IrqVector},
|
||||
};
|
||||
use device_tree::driver::{
|
||||
device_tree_driver, util::read_mac_address, InitSequence, Node, ProbeContext,
|
||||
};
|
||||
use futures_util::task::AtomicWaker;
|
||||
use libk::{
|
||||
device::external_interrupt_controller,
|
||||
dma::DmaBuffer,
|
||||
error::Error,
|
||||
task::runtime::{self, psleep, pwait},
|
||||
@@ -68,7 +67,7 @@ struct Stmmac {
|
||||
rst_stmmaceth: ResetHandle,
|
||||
rst_ahb: ResetHandle,
|
||||
|
||||
irq: FullIrq,
|
||||
irq: IrqHandle,
|
||||
softirq_events: BitmapEvent<AtomicWaker>,
|
||||
|
||||
inner: OneTimeInit<Inner>,
|
||||
@@ -184,9 +183,7 @@ impl Device for Stmmac {
|
||||
}
|
||||
let dma = self.dma.init(cx.dma_allocator.clone());
|
||||
|
||||
let intc = external_interrupt_controller()?;
|
||||
|
||||
intc.register_irq(self.irq.irq, self.irq.options, self.clone())?;
|
||||
self.irq.register(self.clone())?;
|
||||
|
||||
let tx_ring_capacity = 32;
|
||||
let rx_ring_capacity = 32;
|
||||
@@ -350,7 +347,7 @@ impl Device for Stmmac {
|
||||
ygg_driver_net_core::register_interface(NetworkInterfaceType::Ethernet, self.clone());
|
||||
self.iface_id.init(iface.id());
|
||||
|
||||
intc.enable_irq(self.irq.irq)?;
|
||||
self.irq.enable()?;
|
||||
|
||||
let p = self.clone();
|
||||
runtime::spawn(async move { p.softirq().await })?;
|
||||
|
||||
@@ -3,10 +3,11 @@ use yggdrasil_abi::error::Error;
|
||||
|
||||
use crate::device::Device;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct FullIrq {
|
||||
#[derive(Clone)]
|
||||
pub struct IrqHandle {
|
||||
pub irq: Irq,
|
||||
pub options: IrqOptions,
|
||||
pub intc: Arc<dyn ExternalInterruptController>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -154,6 +155,16 @@ pub struct FixedInterruptTable<const N: usize> {
|
||||
rows: [Option<Arc<dyn InterruptHandler>>; N],
|
||||
}
|
||||
|
||||
impl IrqHandle {
|
||||
pub fn register(&self, handler: Arc<dyn InterruptHandler>) -> Result<(), Error> {
|
||||
self.intc.register_irq(self.irq, self.options, handler)
|
||||
}
|
||||
|
||||
pub fn enable(&self) -> Result<(), Error> {
|
||||
self.intc.enable_irq(self.irq)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> FixedInterruptTable<N> {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
|
||||
@@ -3,7 +3,7 @@ use alloc::sync::Arc;
|
||||
use device_api::{
|
||||
clock::{ClockHandle, ResetHandle},
|
||||
gpio::PinHandle,
|
||||
interrupt::FullIrq,
|
||||
interrupt::IrqHandle,
|
||||
};
|
||||
use fdt_rs::spec::Phandle;
|
||||
|
||||
@@ -83,14 +83,14 @@ pub fn map_interrupt_at(
|
||||
interrupt_controller: &Arc<Node>,
|
||||
property: &TProp,
|
||||
offset: usize,
|
||||
) -> Option<FullIrq> {
|
||||
) -> Option<IrqHandle> {
|
||||
let interrupt_controller = interrupt_controller.interrupt_controller.try_get()?;
|
||||
interrupt_controller.map_interrupt(property, offset)
|
||||
interrupt_controller.clone().map_interrupt(property, offset)
|
||||
}
|
||||
|
||||
/// Same as [map_interrupt_at], but uses a phandle to address the `interrupt-controller` node
|
||||
/// and a scaled `index`.
|
||||
pub fn map_interrupt(phandle: Phandle, property: &TProp, index: usize) -> Option<FullIrq> {
|
||||
pub fn map_interrupt(phandle: Phandle, property: &TProp, index: usize) -> Option<IrqHandle> {
|
||||
let interrupt_controller = lookup_phandle(phandle, true)?;
|
||||
let interrupt_cells = interrupt_controller.self_interrupt_cells()?;
|
||||
map_interrupt_at(&interrupt_controller, property, index * interrupt_cells)
|
||||
|
||||
@@ -7,7 +7,7 @@ use device_api::{
|
||||
clock::{ClockHandle, ResetHandle},
|
||||
device::Device,
|
||||
gpio::PinHandle,
|
||||
interrupt::{ExternalInterruptController, FullIrq},
|
||||
interrupt::{ExternalInterruptController, IrqHandle},
|
||||
};
|
||||
use libk::error::Error;
|
||||
|
||||
@@ -25,7 +25,7 @@ pub trait Driver: Sync {
|
||||
pub trait DeviceTreeInterruptController {
|
||||
/// Reads interrupt information from `property` at given `offset` and maps it to
|
||||
/// the interrupt used by the controller
|
||||
fn map_interrupt(&self, property: &TProp, offset: usize) -> Option<FullIrq>;
|
||||
fn map_interrupt(self: Arc<Self>, property: &TProp, offset: usize) -> Option<IrqHandle>;
|
||||
|
||||
/// Returns the [ExternalInterruptController] implementor of this node
|
||||
fn as_interrupt_controller(self: Arc<Self>) -> Arc<dyn ExternalInterruptController>;
|
||||
|
||||
@@ -10,7 +10,7 @@ use device_api::{
|
||||
clock::{ClockController, ClockHandle, ResetHandle},
|
||||
device::{Device, DeviceInitContext},
|
||||
gpio::PinHandle,
|
||||
interrupt::{ExternalInterruptController, FullIrq, MessageInterruptController},
|
||||
interrupt::{ExternalInterruptController, IrqHandle, MessageInterruptController},
|
||||
};
|
||||
use fdt_rs::spec::Phandle;
|
||||
use libk::dma::DummyDmaAllocator;
|
||||
@@ -410,7 +410,7 @@ impl Node {
|
||||
|
||||
/// Reads interrupt information from `interrupts[index]` property, mapped by the node's
|
||||
/// `interrupt-parent`.
|
||||
pub fn interrupt(&self, index: usize) -> Option<FullIrq> {
|
||||
pub fn interrupt(&self, index: usize) -> Option<IrqHandle> {
|
||||
let interrupts = self.property("interrupts")?;
|
||||
let phandle = self.interrupt_parent?;
|
||||
map_interrupt(phandle, &interrupts, index)
|
||||
@@ -418,7 +418,7 @@ impl Node {
|
||||
|
||||
/// Same as [Node::interrupt], but allows specifying other property to read the information
|
||||
/// from.
|
||||
pub fn interrupt_from(&self, property: &TProp, index: usize) -> Option<FullIrq> {
|
||||
pub fn interrupt_from(&self, property: &TProp, index: usize) -> Option<IrqHandle> {
|
||||
let phandle = self.interrupt_parent?;
|
||||
map_interrupt(phandle, property, index)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
use alloc::sync::Arc;
|
||||
use device_api::{
|
||||
gpio::{GpioPinConfig, GpioPinLevel, InputPinBias, OutputPinBias},
|
||||
interrupt::{FullIrq, MessageInterruptController},
|
||||
interrupt::{IrqHandle, MessageInterruptController},
|
||||
};
|
||||
use fdt_rs::prelude::PropReader;
|
||||
use yggdrasil_abi::net::MacAddress;
|
||||
@@ -44,7 +44,6 @@ pub struct GenericPinctrlConfig {
|
||||
}
|
||||
|
||||
/// Represents an entry in a PCIe-controller's `interrupt-map` field
|
||||
#[derive(Debug)]
|
||||
pub struct PcieInterruptEntry {
|
||||
/// PCI address bus
|
||||
pub bus: u8,
|
||||
@@ -57,7 +56,7 @@ pub struct PcieInterruptEntry {
|
||||
/// Destination interrupt controller
|
||||
pub interrupt_controller: Arc<Node>,
|
||||
/// Destination IRQ options
|
||||
pub irq: FullIrq,
|
||||
pub irq: IrqHandle,
|
||||
}
|
||||
|
||||
/// Represents a single PCI address (Requester ID for a MSI(-x))
|
||||
|
||||
@@ -350,7 +350,6 @@ impl Thread {
|
||||
if debug.tracer.is_some() {
|
||||
let timestamp = monotonic_time();
|
||||
debug.store_state(frame);
|
||||
// log::info!("TRACE {payload:?}");
|
||||
self.events.trace.write(TraceEvent {
|
||||
suspend,
|
||||
timestamp,
|
||||
@@ -514,11 +513,7 @@ impl Thread {
|
||||
|
||||
/// Returns `true` if the thread is a tracee of given process
|
||||
pub fn is_tracee_of(&self, pid: ProcessId) -> bool {
|
||||
{
|
||||
let tracer = self.debug.lock().tracer;
|
||||
// log::info!("{:?}'s tracer: {:?}", self.id, tracer);
|
||||
tracer == Some(pid)
|
||||
}
|
||||
self.debug.lock().tracer == Some(pid)
|
||||
}
|
||||
|
||||
pub fn attach_trace(&self, tracer: ProcessId) -> Result<(), Error> {
|
||||
@@ -582,13 +577,11 @@ impl Thread {
|
||||
DebugControl::Detach => todo!(),
|
||||
DebugControl::Resume => {
|
||||
let single_step = debug::Resume::load_request(input)?;
|
||||
// log::info!("Resume single_step={single_step}");
|
||||
self.resume(single_step);
|
||||
debug::Resume::store_response(&(), buffer)
|
||||
}
|
||||
DebugControl::SetTraceFlags => {
|
||||
let flags = debug::SetTraceFlags::load_request(buffer)?;
|
||||
// log::info!("SetTraceFlags {flags:?}");
|
||||
self.set_trace_flags(flags);
|
||||
debug::SetTraceFlags::store_response(&(), buffer)
|
||||
}
|
||||
|
||||
@@ -310,6 +310,7 @@ fn el0_sync_inner(frame: &mut ExceptionFrame) {
|
||||
}
|
||||
|
||||
fn irq_common() {
|
||||
// TODO some concept of "root" interrupt controller
|
||||
external_interrupt_controller()
|
||||
.unwrap()
|
||||
.handle_pending_irqs();
|
||||
|
||||
@@ -8,9 +8,9 @@ use alloc::sync::Arc;
|
||||
use device_api::{
|
||||
device::{Device, DeviceInitContext},
|
||||
interrupt::{
|
||||
ExternalInterruptController, FixedInterruptTable, FullIrq, InterruptHandler,
|
||||
InterruptTable, IpiDeliveryTarget, IpiMessage, Irq, IrqLevel, IrqOptions, IrqTrigger,
|
||||
IrqVector, LocalInterruptController,
|
||||
ExternalInterruptController, FixedInterruptTable, InterruptHandler, InterruptTable,
|
||||
IpiDeliveryTarget, IpiMessage, Irq, IrqHandle, IrqLevel, IrqOptions, IrqTrigger, IrqVector,
|
||||
LocalInterruptController,
|
||||
},
|
||||
};
|
||||
use device_tree::{
|
||||
@@ -141,8 +141,9 @@ impl ExternalInterruptController for Gic {
|
||||
gicc.clear_irq(irq_number);
|
||||
|
||||
if irq_number == IPI_VECTOR as usize {
|
||||
// TODO pop entry
|
||||
crate::panic::panic_secondary();
|
||||
let ipi = Cpu::local().get_ipi();
|
||||
AArch64::handle_ipi(ipi);
|
||||
return;
|
||||
}
|
||||
|
||||
if irq_number < GIC_PPI_START as usize {
|
||||
@@ -203,7 +204,7 @@ impl LocalInterruptController for Gic {
|
||||
}
|
||||
|
||||
impl DeviceTreeInterruptController for Gic {
|
||||
fn map_interrupt(&self, property: &TProp, offset: usize) -> Option<FullIrq> {
|
||||
fn map_interrupt(self: Arc<Self>, property: &TProp, offset: usize) -> Option<IrqHandle> {
|
||||
// IRQ_TYPE_NONE - 0
|
||||
// IRQ_TYPE_EDGE_RISING - 1
|
||||
// IRQ_TYPE_EDGE_FALLING - 2
|
||||
@@ -229,9 +230,10 @@ impl DeviceTreeInterruptController for Gic {
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
Some(FullIrq {
|
||||
Some(IrqHandle {
|
||||
irq,
|
||||
options: IrqOptions { trigger, level },
|
||||
intc: self.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -5,14 +5,15 @@ use core::sync::atomic::{self, Ordering};
|
||||
use aarch64_cpu::registers::{CNTP_CTL_EL0, CNTP_TVAL_EL0};
|
||||
use alloc::sync::Arc;
|
||||
use device_api::{
|
||||
interrupt::{Irq, LocalInterruptController},
|
||||
interrupt::{IpiDeliveryTarget, IpiMessage, Irq, LocalInterruptController},
|
||||
ResetDevice,
|
||||
};
|
||||
use device_tree::{
|
||||
driver::{unflatten_device_tree, InitSequence},
|
||||
DeviceTree, DeviceTreeNodeExt,
|
||||
};
|
||||
use kernel_arch_aarch64::{mem, ArchitectureImpl, PerCpuData};
|
||||
use kernel_arch::Architecture;
|
||||
use kernel_arch_aarch64::{mem, ArchitectureImpl, PerCpuData, CPU_COUNT};
|
||||
use libk::{arch::Cpu, config, debug, device::external_interrupt_controller, error::Error};
|
||||
use libk_mm::{
|
||||
address::PhysicalAddress,
|
||||
@@ -20,7 +21,7 @@ use libk_mm::{
|
||||
pointer::PhysicalRef,
|
||||
table::EntryLevelExt,
|
||||
};
|
||||
use libk_util::OneTimeInit;
|
||||
use libk_util::{sync::SpinFence, OneTimeInit};
|
||||
use tock_registers::interfaces::Writeable;
|
||||
use ygg_driver_pci::PciBusManager;
|
||||
|
||||
@@ -28,6 +29,7 @@ use crate::{
|
||||
arch::{aarch64::gic::Gic, Platform},
|
||||
device::{power::arm_psci::Psci, MACHINE_NAME},
|
||||
fs::{Initrd, INITRD_DATA},
|
||||
panic,
|
||||
util::call_init_array,
|
||||
};
|
||||
|
||||
@@ -68,6 +70,8 @@ impl<const SIZE: usize> BootStack<SIZE> {
|
||||
}
|
||||
}
|
||||
|
||||
static SHUTDOWN_FENCE: SpinFence = SpinFence::new();
|
||||
|
||||
impl Platform for AArch64 {
|
||||
unsafe fn start_application_processors(&self) {
|
||||
if let Some(compatible) = self.machine_compatible.try_get() {
|
||||
@@ -96,6 +100,18 @@ impl Platform for AArch64 {
|
||||
psci.reset()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn power_off(&self) -> Result<!, Error> {
|
||||
Self::halt_aps()?;
|
||||
|
||||
if let Some(psci) = self.psci.try_get() {
|
||||
log::info!("Powering off");
|
||||
psci.power_off()
|
||||
} else {
|
||||
log::warn!("No power off method, halting");
|
||||
ArchitectureImpl::halt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AArch64 {
|
||||
@@ -103,6 +119,41 @@ impl AArch64 {
|
||||
GIC.init(gic);
|
||||
}
|
||||
|
||||
fn handle_ipi(ipi: Option<IpiMessage>) {
|
||||
let Some(ipi) = ipi else {
|
||||
log::warn!("Spurious IPI received, assuming panic");
|
||||
panic::panic_secondary();
|
||||
};
|
||||
|
||||
match ipi {
|
||||
IpiMessage::Panic => panic::panic_secondary(),
|
||||
IpiMessage::Shutdown => Self::secondary_halt_handler(),
|
||||
}
|
||||
}
|
||||
|
||||
fn secondary_halt_handler() {
|
||||
log::info!("CPU halted");
|
||||
SHUTDOWN_FENCE.signal();
|
||||
ArchitectureImpl::halt();
|
||||
}
|
||||
|
||||
unsafe fn halt_aps() -> Result<(), Error> {
|
||||
let ap_count = CPU_COUNT.load(Ordering::Acquire) - 1;
|
||||
|
||||
if ap_count > 0 {
|
||||
if let Some(gic) = GIC.try_get() {
|
||||
gic.send_ipi(IpiDeliveryTarget::OtherCpus, IpiMessage::Shutdown)?;
|
||||
SHUTDOWN_FENCE.wait_all(ap_count);
|
||||
Ok(())
|
||||
} else {
|
||||
log::warn!("No way to handle secondary CPUs");
|
||||
Err(Error::NotImplemented)
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
unsafe fn init_memory_management(
|
||||
&'static self,
|
||||
|
||||
@@ -7,16 +7,16 @@ use abi::{error::Error, time::NANOSECONDS_IN_SECOND};
|
||||
use alloc::sync::Arc;
|
||||
use device_api::{
|
||||
device::{Device, DeviceInitContext},
|
||||
interrupt::{FullIrq, InterruptHandler, IrqVector},
|
||||
interrupt::{InterruptHandler, IrqHandle, IrqVector},
|
||||
};
|
||||
use device_tree::driver::{device_tree_driver, Node, ProbeContext};
|
||||
use kernel_arch::task::Scheduler;
|
||||
use libk::{arch::Cpu, device::external_interrupt_controller, task::runtime, time};
|
||||
use libk::{arch::Cpu, task::runtime, time};
|
||||
use tock_registers::interfaces::{ReadWriteable, Readable, Writeable};
|
||||
|
||||
/// ARM Generic Timer driver
|
||||
pub struct ArmTimer {
|
||||
irq: FullIrq,
|
||||
irq: IrqHandle,
|
||||
}
|
||||
|
||||
/// ARM timer tick interval (in some time units?)
|
||||
@@ -63,14 +63,12 @@ impl Device for ArmTimer {
|
||||
unsafe fn init_irq(self: Arc<Self>) -> Result<(), Error> {
|
||||
log::info!("ARM Generic Timer frequency={}Hz", CNTFRQ_EL0.get());
|
||||
|
||||
let intc = external_interrupt_controller()?;
|
||||
|
||||
intc.register_irq(self.irq.irq, self.irq.options, self.clone())?;
|
||||
self.irq.register(self.clone())?;
|
||||
|
||||
CNTP_CTL_EL0.modify(CNTP_CTL_EL0::IMASK::CLEAR);
|
||||
CNTP_TVAL_EL0.set(TICK_INTERVAL);
|
||||
|
||||
intc.enable_irq(self.irq.irq)?;
|
||||
self.irq.enable()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -82,7 +80,7 @@ impl ArmTimer {
|
||||
/// # Safety
|
||||
///
|
||||
/// The caller must ensure the function has not been called before.
|
||||
pub const unsafe fn new(irq: FullIrq) -> Self {
|
||||
pub const unsafe fn new(irq: IrqHandle) -> Self {
|
||||
Self { irq }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Provides architecture/platform-specific implementation details
|
||||
|
||||
use abi::error::Error;
|
||||
use kernel_arch::{Architecture, ArchitectureImpl};
|
||||
|
||||
#[cfg(any(target_arch = "aarch64", rust_analyzer))]
|
||||
@@ -51,6 +52,19 @@ pub trait Platform {
|
||||
}
|
||||
}
|
||||
|
||||
/// Performs full system powerdown.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The caller must ensure it is actually safe to power down, i.e. no critical processes will be
|
||||
/// aborted and no data will be lost.
|
||||
unsafe fn power_off(&self) -> Result<!, Error> {
|
||||
ArchitectureImpl::set_interrupt_mask(true);
|
||||
loop {
|
||||
ArchitectureImpl::wait_for_interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
// /// Adds a reset device to the system
|
||||
// fn register_reset_device(&self, reset: Arc<dyn ResetDevice>) -> Result<(), Error> {
|
||||
// Err(Error::NotImplemented)
|
||||
|
||||
@@ -11,7 +11,7 @@ use kernel_arch::{Architecture, ArchitectureImpl};
|
||||
use kernel_arch_riscv64::{
|
||||
mem,
|
||||
registers::{SIE, SSTATUS},
|
||||
PerCpuData,
|
||||
sbi, PerCpuData,
|
||||
};
|
||||
use libk::{arch::Cpu, config};
|
||||
use libk_mm::{
|
||||
@@ -51,6 +51,10 @@ impl Platform for Riscv64 {
|
||||
ArchitectureImpl::halt();
|
||||
}
|
||||
|
||||
unsafe fn power_off(&self) -> Result<!, Error> {
|
||||
sbi::sbi_system_shutdown()
|
||||
}
|
||||
|
||||
unsafe fn start_application_processors(&self) {
|
||||
// TODO asymmetric systems with different hart types are not yet supported.
|
||||
// e.g., in JH7110 there're two different types of cores
|
||||
|
||||
@@ -11,6 +11,8 @@ use libk::{
|
||||
};
|
||||
use static_assertions::{const_assert, const_assert_eq};
|
||||
|
||||
use crate::arch::x86_64::X86_64;
|
||||
|
||||
use super::exception;
|
||||
|
||||
pub mod ioapic;
|
||||
@@ -115,8 +117,8 @@ unsafe extern "C" fn dummy_irq_handler() {
|
||||
}
|
||||
|
||||
unsafe extern "C" fn ipi_handler() {
|
||||
let cpu = Cpu::local();
|
||||
todo!("Processor {} received an IPI", cpu.id());
|
||||
let ipi = Cpu::local().get_ipi();
|
||||
X86_64::handle_ipi(ipi);
|
||||
}
|
||||
|
||||
global_asm!(
|
||||
|
||||
@@ -4,12 +4,12 @@ use core::{arch::global_asm, mem::size_of};
|
||||
use abi::{bitflags, primitive_enum, process::Signal};
|
||||
use kernel_arch_x86::registers::{CR2, CR3};
|
||||
use kernel_arch_x86_64::context::ExceptionFrame;
|
||||
use libk::task::thread::Thread;
|
||||
use libk::{arch::Cpu, task::thread::Thread};
|
||||
use libk_mm::PageFaultKind;
|
||||
use libk_util::sync::spin_rwlock::IrqSafeRwLock;
|
||||
use tock_registers::interfaces::Readable;
|
||||
|
||||
use crate::arch::x86_64::apic;
|
||||
use crate::arch::x86_64::{apic, X86_64};
|
||||
|
||||
primitive_enum! {
|
||||
enum ExceptionKind: u64 {
|
||||
@@ -231,7 +231,13 @@ extern "C" fn __x86_64_exception_handler(frame: *mut ExceptionFrame) {
|
||||
}
|
||||
|
||||
extern "C" fn __x86_64_nmi_handler() -> ! {
|
||||
crate::panic::panic_secondary();
|
||||
let ipi = Cpu::local().get_ipi();
|
||||
if let Some(ipi) = ipi {
|
||||
X86_64::handle_ipi(Some(ipi));
|
||||
unreachable!();
|
||||
} else {
|
||||
panic!("Spurious/unknown NMI received");
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes the interrupt descriptor table for the given CPU.
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
//! x86-64 architecture implementation
|
||||
|
||||
use core::ptr::null_mut;
|
||||
use core::{ptr::null_mut, sync::atomic::Ordering};
|
||||
|
||||
use abi::error::Error;
|
||||
use acpi::{mcfg::Mcfg, AcpiTables, HpetInfo, InterruptModel};
|
||||
use alloc::sync::Arc;
|
||||
use device_api::device::Device;
|
||||
use device_api::{
|
||||
device::Device,
|
||||
interrupt::{IpiDeliveryTarget, IpiMessage},
|
||||
};
|
||||
use kernel_arch::{Architecture, ArchitectureImpl};
|
||||
use kernel_arch_x86::{
|
||||
cpuid::{self, CpuFeatures, EcxFeatures, EdxFeatures, ExtEdxFeatures},
|
||||
gdt,
|
||||
};
|
||||
use kernel_arch_x86_64::{mem, LocalApicInterface, PerCpuData};
|
||||
use kernel_arch_x86_64::{mem, LocalApicInterface, PerCpuData, CPU_COUNT};
|
||||
use libk::{
|
||||
arch::Cpu,
|
||||
config, debug,
|
||||
@@ -27,7 +31,10 @@ use libk_mm::{
|
||||
pointer::PhysicalRef,
|
||||
table::EntryLevel,
|
||||
};
|
||||
use libk_util::OneTimeInit;
|
||||
use libk_util::{
|
||||
sync::{IrqGuard, SpinFence},
|
||||
OneTimeInit,
|
||||
};
|
||||
use yboot_proto::{
|
||||
v1::{self, AvailableMemoryRegion},
|
||||
LoadProtocolV1,
|
||||
@@ -45,6 +52,7 @@ use crate::{
|
||||
Platform,
|
||||
},
|
||||
device::display::linear_fb::LinearFramebuffer,
|
||||
panic,
|
||||
util::call_init_array,
|
||||
};
|
||||
|
||||
@@ -72,7 +80,9 @@ pub static PLATFORM: X86_64 = X86_64 {
|
||||
|
||||
fbconsole: OneTimeInit::new(),
|
||||
};
|
||||
//
|
||||
|
||||
static SHUTDOWN_FENCE: SpinFence = SpinFence::new();
|
||||
|
||||
impl Platform for X86_64 {
|
||||
unsafe fn start_application_processors(&self) {
|
||||
if let Some(acpi) = self.acpi.try_get() {
|
||||
@@ -87,9 +97,42 @@ impl Platform for X86_64 {
|
||||
smp::start_ap_cores(&pinfo);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn power_off(&self) -> Result<!, Error> {
|
||||
let _guard = IrqGuard::acquire();
|
||||
|
||||
let ap_count = CPU_COUNT.load(Ordering::Acquire) - 1;
|
||||
if ap_count > 0 {
|
||||
self.send_ipi(IpiDeliveryTarget::OtherCpus, IpiMessage::Shutdown)?;
|
||||
SHUTDOWN_FENCE.wait_all(ap_count);
|
||||
}
|
||||
|
||||
ygg_driver_acpi::power_off()
|
||||
}
|
||||
}
|
||||
|
||||
impl X86_64 {
|
||||
fn handle_ipi(ipi: Option<IpiMessage>) {
|
||||
let Some(ipi) = ipi else {
|
||||
log::warn!("Spurious IPI received, assuming panic");
|
||||
panic::panic_secondary();
|
||||
};
|
||||
|
||||
match ipi {
|
||||
IpiMessage::Shutdown => {
|
||||
log::info!("Core halted");
|
||||
SHUTDOWN_FENCE.signal();
|
||||
ArchitectureImpl::halt()
|
||||
}
|
||||
IpiMessage::Panic => panic::panic_secondary(),
|
||||
}
|
||||
}
|
||||
|
||||
fn send_ipi(&self, target: IpiDeliveryTarget, message: IpiMessage) -> Result<(), Error> {
|
||||
let cpu = Cpu::local();
|
||||
cpu.local_apic.send_ipi(target, message)
|
||||
}
|
||||
|
||||
fn set_boot_data(&self, data: BootData) {
|
||||
match data {
|
||||
BootData::YBoot(data) => {
|
||||
@@ -196,6 +239,12 @@ impl X86_64 {
|
||||
call_init_array();
|
||||
|
||||
x86::init_platform_devices(early);
|
||||
|
||||
extern "C" {
|
||||
static __kernel_start: u8;
|
||||
}
|
||||
|
||||
log::info!("Load base: {:p}", &raw const __kernel_start);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -61,6 +61,7 @@ impl ResetDevice for Psci {
|
||||
|
||||
impl Psci {
|
||||
const SYSTEM_RESET: u32 = 0x84000009;
|
||||
const SYSTEM_OFF: u32 = 0x84000008;
|
||||
|
||||
#[inline]
|
||||
unsafe fn call(&self, mut x0: u64, x1: u64, x2: u64, x3: u64) -> u64 {
|
||||
@@ -74,6 +75,12 @@ impl Psci {
|
||||
}
|
||||
x0
|
||||
}
|
||||
|
||||
/// Shut down the system
|
||||
pub unsafe fn power_off(&self) -> Result<!, Error> {
|
||||
self.call(Self::SYSTEM_OFF as _, 0, 0, 0);
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
device_tree_driver! {
|
||||
|
||||
@@ -3,7 +3,7 @@ use abi::{error::Error, io::TerminalOptions};
|
||||
use alloc::sync::Arc;
|
||||
use device_api::{
|
||||
device::{Device, DeviceInitContext},
|
||||
interrupt::{FullIrq, InterruptHandler, IrqVector},
|
||||
interrupt::{InterruptHandler, IrqHandle, IrqVector},
|
||||
};
|
||||
use device_tree::driver::{device_tree_driver, Node, ProbeContext};
|
||||
use libk::{
|
||||
@@ -79,7 +79,7 @@ struct Inner {
|
||||
pub struct Ns16550a {
|
||||
inner: OneTimeInit<Arc<Terminal<Inner>>>,
|
||||
base: PhysicalAddress,
|
||||
irq: FullIrq,
|
||||
irq: IrqHandle,
|
||||
}
|
||||
|
||||
impl Io {
|
||||
@@ -135,9 +135,8 @@ impl Device for Ns16550a {
|
||||
}
|
||||
|
||||
unsafe fn init_irq(self: Arc<Self>) -> Result<(), Error> {
|
||||
let intc = external_interrupt_controller()?;
|
||||
intc.register_irq(self.irq.irq, self.irq.options, self.clone())?;
|
||||
intc.enable_irq(self.irq.irq)?;
|
||||
self.irq.register(self.clone())?;
|
||||
self.irq.enable()?;
|
||||
let io = self.inner.get().output().io.lock();
|
||||
io.regs.IER.modify(IER::RDR::SET);
|
||||
Ok(())
|
||||
|
||||
@@ -4,7 +4,7 @@ use alloc::sync::Arc;
|
||||
use device_api::{
|
||||
clock::{ClockHandle, Hertz, ResetHandle},
|
||||
device::{Device, DeviceInitContext},
|
||||
interrupt::{FullIrq, InterruptHandler, IrqVector},
|
||||
interrupt::{InterruptHandler, IrqHandle, IrqVector},
|
||||
};
|
||||
use device_tree::driver::{device_tree_driver, Node, ProbeContext};
|
||||
use libk::{
|
||||
@@ -110,7 +110,7 @@ struct Inner {
|
||||
/// Synopsys DesignWare 8250 UART
|
||||
pub struct DwUart {
|
||||
base: PhysicalAddress,
|
||||
irq: FullIrq,
|
||||
irq: IrqHandle,
|
||||
clk_baud: ClockHandle,
|
||||
#[allow(unused)]
|
||||
clk_apb: Option<ClockHandle>,
|
||||
@@ -241,9 +241,8 @@ impl Device for DwUart {
|
||||
}
|
||||
|
||||
unsafe fn init_irq(self: Arc<Self>) -> Result<(), Error> {
|
||||
let intc = external_interrupt_controller()?;
|
||||
intc.register_irq(self.irq.irq, Default::default(), self.clone())?;
|
||||
intc.enable_irq(self.irq.irq)?;
|
||||
self.irq.register(self.clone())?;
|
||||
self.irq.enable()?;
|
||||
|
||||
let output = self.inner.get().output();
|
||||
let io = output.io.lock();
|
||||
|
||||
@@ -16,7 +16,7 @@ use abi::{
|
||||
option::OptionValue,
|
||||
path::Path,
|
||||
process::{ExecveOptions, ProcessId},
|
||||
system::{self, SystemInfoVariant},
|
||||
system::{self, SystemControlVariant, SystemInfoVariant},
|
||||
time::{ClockType, SystemTime},
|
||||
};
|
||||
use libk::{
|
||||
@@ -26,7 +26,10 @@ use libk::{
|
||||
};
|
||||
use libk_mm::phys;
|
||||
|
||||
use crate::fs;
|
||||
use crate::{
|
||||
arch::{Platform, PLATFORM},
|
||||
fs,
|
||||
};
|
||||
|
||||
use super::run_with_io;
|
||||
|
||||
@@ -86,6 +89,16 @@ pub(crate) fn set_clock(ty: ClockType, value: &SystemTime) -> Result<(), Error>
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn system_control(option: u32, value: &mut [u8], size: usize) -> Result<usize, Error> {
|
||||
let _ = (value, size);
|
||||
let option = SystemControlVariant::try_from(option)?;
|
||||
match option {
|
||||
SystemControlVariant::PowerOff => {
|
||||
unsafe { PLATFORM.power_off() }?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_system_info(option: u32, buffer: &mut [u8]) -> Result<usize, Error> {
|
||||
let option = SystemInfoVariant::try_from(option)?;
|
||||
match option {
|
||||
|
||||
@@ -80,6 +80,7 @@ syscall unmount(opts: &UnmountOptions) -> Result<()>;
|
||||
syscall load_module(path: &str) -> Result<()>;
|
||||
|
||||
syscall filesystem_control(fd: Option<RawFd>, option: u32, value: &mut [u8], len: usize) -> Result<usize>;
|
||||
syscall system_control(option: u32, value: &mut [u8], len: usize) -> Result<usize>;
|
||||
|
||||
syscall get_system_info(option: u32, value: &mut [u8]) -> Result<usize>;
|
||||
|
||||
|
||||
@@ -21,6 +21,14 @@ option_group!(
|
||||
}
|
||||
);
|
||||
|
||||
request_group!(
|
||||
#[doc = "Common system controls"]
|
||||
pub enum SystemControlVariant<'de> {
|
||||
#[doc = "Power down the system"]
|
||||
0x1000: PowerOff((), ()),
|
||||
}
|
||||
);
|
||||
|
||||
abi_serde::impl_struct_serde!(
|
||||
SystemMemoryStats: [total_usable_pages, allocated_pages, free_pages, page_size]
|
||||
);
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
//! System-related parameters
|
||||
|
||||
pub use abi::system::*;
|
||||
use abi::{error::Error, option::OptionValue};
|
||||
use abi::{
|
||||
error::Error,
|
||||
option::{OptionValue, RequestValue},
|
||||
};
|
||||
|
||||
/// Helper macro for [get_system_info]
|
||||
pub macro get_system_info($variant_ty:ty) {{
|
||||
@@ -14,3 +17,13 @@ pub fn get_system_info<'de, T: OptionValue<'de>>(buffer: &'de mut [u8]) -> Resul
|
||||
let len = unsafe { crate::sys::get_system_info(T::VARIANT.into(), buffer) }?;
|
||||
T::load(&buffer[..len])
|
||||
}
|
||||
|
||||
/// Performs an operation on the system, like power management, reset etc
|
||||
pub fn system_control<'de, T: RequestValue<'de>>(
|
||||
buffer: &'de mut [u8],
|
||||
request: &T::Request,
|
||||
) -> Result<T::Response, Error> {
|
||||
let len = T::store_request(request, buffer)?;
|
||||
let len = unsafe { crate::sys::system_control(T::VARIANT.into(), buffer, len) }?;
|
||||
T::load_response(&buffer[..len])
|
||||
}
|
||||
|
||||
Generated
-2
@@ -2970,8 +2970,6 @@ name = "strace"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"cross",
|
||||
"libc",
|
||||
"runtime",
|
||||
]
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
pub(crate) mod sys;
|
||||
|
||||
pub mod debug;
|
||||
pub mod fs;
|
||||
pub mod io;
|
||||
pub mod mem;
|
||||
|
||||
@@ -1,64 +1,6 @@
|
||||
use std::io;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TraceEvent {
|
||||
SyscallEntry(u64, [u64; 6]),
|
||||
SyscallExit(i64),
|
||||
Exited,
|
||||
}
|
||||
|
||||
pub trait ChildTrace {
|
||||
fn next_event(&mut self) -> io::Result<TraceEvent>;
|
||||
fn resume(&mut self) -> io::Result<()>;
|
||||
fn kill(&mut self) -> io::Result<()>;
|
||||
|
||||
unsafe fn peek_u8(&mut self, address: usize) -> io::Result<u8> {
|
||||
let aligned = address & !7;
|
||||
let shift = (address & 7) << 3;
|
||||
let word = self.peek_u64(aligned)?;
|
||||
Ok((word >> shift) as u8)
|
||||
}
|
||||
unsafe fn peek_u32(&mut self, address: usize) -> io::Result<u32> {
|
||||
eprintln!("PEEK U32 @ {address:#x}");
|
||||
assert_eq!(address & 3, 0);
|
||||
let aligned = address & !7;
|
||||
let shift = (address & 7) << 3;
|
||||
let word = self.peek_u64(aligned)?;
|
||||
Ok((word >> shift) as u32)
|
||||
}
|
||||
unsafe fn peek_u64(&mut self, address: usize) -> io::Result<u64>;
|
||||
|
||||
unsafe fn peek_bytes(&mut self, address: usize, buffer: &mut [u8]) -> io::Result<()> {
|
||||
for i in 0..buffer.len() {
|
||||
buffer[i] = self.peek_u8(address + i)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
unsafe fn peek_cstr(&mut self, address: usize, buffer: &mut [u8]) -> io::Result<usize> {
|
||||
let mut len = 0;
|
||||
while len < buffer.len() - 1 {
|
||||
let byte = self.peek_u8(address + len)?;
|
||||
buffer[len] = byte;
|
||||
if byte == 0 {
|
||||
break;
|
||||
}
|
||||
len += 1;
|
||||
}
|
||||
Ok(len)
|
||||
}
|
||||
|
||||
unsafe fn peek_usize(&mut self, address: usize) -> io::Result<usize> {
|
||||
// FIXME I only support 64-bit archs
|
||||
Ok(self.peek_u64(address)? as _)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CommandSpawnExt {
|
||||
type ChildTrace: ChildTrace;
|
||||
|
||||
fn create_session(&mut self) -> io::Result<&mut Self>;
|
||||
fn create_process_group(&mut self) -> io::Result<&mut Self>;
|
||||
// TODO options for tracing across subchildren, etc
|
||||
fn spawn_with_trace(&mut self) -> io::Result<Self::ChildTrace>;
|
||||
}
|
||||
|
||||
@@ -10,12 +10,10 @@ pub mod time;
|
||||
pub mod timer;
|
||||
|
||||
use std::{
|
||||
ffi::{c_int, c_void},
|
||||
ffi::c_int,
|
||||
io,
|
||||
mem::MaybeUninit,
|
||||
os::{fd::RawFd, unix::process::CommandExt},
|
||||
process::Command,
|
||||
ptr::null_mut,
|
||||
sync::Mutex,
|
||||
};
|
||||
|
||||
@@ -28,126 +26,7 @@ pub use socket::{BorrowedAddressImpl, LocalPacketSocketImpl, OwnedAddressImpl};
|
||||
pub use term::RawStdinImpl;
|
||||
pub use timer::TimerFdImpl;
|
||||
|
||||
use crate::process::{ChildTrace, CommandSpawnExt, TraceEvent};
|
||||
|
||||
pub struct ChildTraceImpl {
|
||||
child: libc::pid_t,
|
||||
}
|
||||
|
||||
impl ChildTrace for ChildTraceImpl {
|
||||
fn next_event(&mut self) -> io::Result<TraceEvent> {
|
||||
let ev = loop {
|
||||
unsafe {
|
||||
let mut status = 0;
|
||||
if libc::waitpid(self.child, &mut status, 0) < 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
if libc::WIFSTOPPED(status) {
|
||||
let stopsig = libc::WSTOPSIG(status);
|
||||
if stopsig == libc::SIGTRAP | 0x80 {
|
||||
let mut syscall_info = MaybeUninit::<libc::ptrace_syscall_info>::zeroed();
|
||||
if libc::ptrace(
|
||||
libc::PTRACE_GET_SYSCALL_INFO,
|
||||
self.child,
|
||||
size_of::<libc::ptrace_syscall_info>(),
|
||||
syscall_info.as_mut_ptr(),
|
||||
) < 0
|
||||
{
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
|
||||
let syscall_info = syscall_info.assume_init();
|
||||
match syscall_info.op {
|
||||
libc::PTRACE_SYSCALL_INFO_EXIT => {
|
||||
break TraceEvent::SyscallExit(syscall_info.u.exit.sval);
|
||||
}
|
||||
libc::PTRACE_SYSCALL_INFO_ENTRY => {
|
||||
break TraceEvent::SyscallEntry(
|
||||
syscall_info.u.entry.nr,
|
||||
syscall_info.u.entry.args,
|
||||
);
|
||||
}
|
||||
_ => self.resume()?,
|
||||
}
|
||||
} else if stopsig == libc::SIGTRAP {
|
||||
let event = (status >> 16) & 0xffff;
|
||||
eprintln!("event = {event}");
|
||||
match event {
|
||||
libc::PTRACE_EVENT_EXIT => {
|
||||
// Ignore
|
||||
eprintln!("PTRACE_EVENT_EXIT");
|
||||
self.resume()?;
|
||||
}
|
||||
libc::PTRACE_EVENT_VFORK => {
|
||||
eprintln!("PTRACE_EVENT_VFORK");
|
||||
self.resume()?;
|
||||
}
|
||||
libc::PTRACE_EVENT_EXEC => {
|
||||
// Ignore
|
||||
eprintln!("PTRACE_EVENT_EXEC");
|
||||
self.resume()?;
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
} else {
|
||||
todo!("signum == {stopsig}");
|
||||
}
|
||||
} else if libc::WIFEXITED(status) {
|
||||
break TraceEvent::Exited;
|
||||
} else {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(ev)
|
||||
}
|
||||
|
||||
fn kill(&mut self) -> io::Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn resume(&mut self) -> io::Result<()> {
|
||||
if unsafe {
|
||||
libc::ptrace(
|
||||
libc::PTRACE_SYSCALL,
|
||||
self.child,
|
||||
null_mut::<c_void>(),
|
||||
null_mut::<c_void>(),
|
||||
)
|
||||
} == 0
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
Err(io::Error::last_os_error())
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn peek_u64(&mut self, address: usize) -> io::Result<u64> {
|
||||
libc::__errno_location().write_volatile(0);
|
||||
let word = libc::ptrace(
|
||||
libc::PTRACE_PEEKDATA,
|
||||
self.child,
|
||||
address,
|
||||
null_mut::<c_void>(),
|
||||
);
|
||||
if libc::__errno_location().read_volatile() != 0 {
|
||||
Err(io::Error::last_os_error())
|
||||
} else {
|
||||
Ok(word as _)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ChildTraceImpl {
|
||||
fn drop(&mut self) {
|
||||
let mut ignore = 0;
|
||||
unsafe {
|
||||
libc::ptrace(libc::PTRACE_DETACH, self.child);
|
||||
libc::waitpid(self.child, &mut ignore, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::process::CommandSpawnExt;
|
||||
|
||||
fn dummy_sigint() {}
|
||||
|
||||
@@ -182,8 +61,6 @@ pub fn clone_fd(fd: RawFd) -> io::Result<RawFd> {
|
||||
}
|
||||
|
||||
impl CommandSpawnExt for Command {
|
||||
type ChildTrace = ChildTraceImpl;
|
||||
|
||||
fn create_process_group(&mut self) -> io::Result<&mut Self> {
|
||||
Ok(self.process_group(0))
|
||||
}
|
||||
@@ -192,54 +69,4 @@ impl CommandSpawnExt for Command {
|
||||
// TODO
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn spawn_with_trace(&mut self) -> io::Result<Self::ChildTrace> {
|
||||
unsafe {
|
||||
match libc::fork() {
|
||||
0 => {
|
||||
// Child
|
||||
// if libc::ptrace(libc::PTRACE_TRACEME, libc::getpid()) != 0 {
|
||||
// panic!("PTRACE_TRACEME: {}", io::Error::last_os_error());
|
||||
// }
|
||||
libc::raise(libc::SIGSTOP);
|
||||
panic!("exec error: {}", self.exec());
|
||||
}
|
||||
code if code < 0 => {
|
||||
// Error
|
||||
Err(io::Error::last_os_error())
|
||||
}
|
||||
pid => {
|
||||
let options = libc::PTRACE_O_TRACESYSGOOD | libc::PTRACE_O_TRACEEXEC;
|
||||
let mut ignore = 0;
|
||||
// Parent
|
||||
if libc::ptrace(
|
||||
libc::PTRACE_SEIZE,
|
||||
pid,
|
||||
null_mut::<c_void>(),
|
||||
null_mut::<c_void>(),
|
||||
) != 0
|
||||
{
|
||||
// TODO waitpid child? kill child?
|
||||
eprintln!("PTRACE_SEIZE error");
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
if libc::waitpid(pid, &mut ignore, 0) < 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
if libc::ptrace(libc::PTRACE_SETOPTIONS, pid, null_mut::<c_void>(), options)
|
||||
!= 0
|
||||
{
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
libc::ptrace(
|
||||
libc::PTRACE_CONT,
|
||||
pid,
|
||||
null_mut::<c_void>(),
|
||||
null_mut::<c_void>(),
|
||||
);
|
||||
Ok(ChildTraceImpl { child: pid })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,20 +11,8 @@ pub mod timer;
|
||||
|
||||
use std::{
|
||||
io,
|
||||
mem::MaybeUninit,
|
||||
os::{
|
||||
fd::RawFd,
|
||||
yggdrasil::process::{ChildExt, CommandExt},
|
||||
},
|
||||
process::{Child, Command},
|
||||
};
|
||||
|
||||
use runtime::{
|
||||
abi::{
|
||||
arch::SavedFrame,
|
||||
debug::{self, TraceFlags},
|
||||
},
|
||||
rt::{debug::debug_control, process::ThreadEvent},
|
||||
os::{fd::RawFd, yggdrasil::process::CommandExt},
|
||||
process::Command,
|
||||
};
|
||||
|
||||
pub use mem::{FileMappingImpl, SharedMemoryImpl};
|
||||
@@ -36,131 +24,7 @@ pub use socket::{BorrowedAddressImpl, LocalPacketSocketImpl, OwnedAddressImpl};
|
||||
pub use term::RawStdinImpl;
|
||||
pub use timer::TimerFdImpl;
|
||||
|
||||
use crate::process::{ChildTrace, CommandSpawnExt, TraceEvent};
|
||||
|
||||
pub struct ChildTraceImpl {
|
||||
#[allow(unused)]
|
||||
child: Child,
|
||||
buffer: [u8; 512],
|
||||
tid: u32,
|
||||
}
|
||||
|
||||
impl ChildTraceImpl {
|
||||
fn attach(child: Child) -> io::Result<Self> {
|
||||
let mut buffer = [0; 512];
|
||||
let tid = child.main_thread_id()?;
|
||||
|
||||
debug_control::<debug::SetTraceFlags>(
|
||||
tid,
|
||||
&mut buffer,
|
||||
&(TraceFlags::SYSCALL_ENTRY | TraceFlags::SYSCALL_EXIT),
|
||||
)
|
||||
.map_err(io::Error::from)
|
||||
.unwrap();
|
||||
|
||||
Ok(Self { child, tid, buffer })
|
||||
}
|
||||
|
||||
fn read_regs(&mut self) -> io::Result<SavedFrame> {
|
||||
debug_control::<debug::GetRegisters>(self.tid, &mut self.buffer, &())
|
||||
.map_err(io::Error::from)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(rust_analyzer, target_arch = "aarch64"))]
|
||||
impl ChildTraceImpl {
|
||||
fn read_syscall_entry(&mut self) -> io::Result<(u64, [u64; 6])> {
|
||||
let frame = self.read_regs()?;
|
||||
Ok((
|
||||
frame.gp_regs[8] as _,
|
||||
[
|
||||
frame.gp_regs[0] as _,
|
||||
frame.gp_regs[1] as _,
|
||||
frame.gp_regs[2] as _,
|
||||
frame.gp_regs[3] as _,
|
||||
frame.gp_regs[4] as _,
|
||||
frame.gp_regs[5] as _,
|
||||
],
|
||||
))
|
||||
}
|
||||
|
||||
fn read_syscall_exit(&mut self) -> io::Result<u64> {
|
||||
let frame = self.read_regs()?;
|
||||
Ok(frame.gp_regs[0] as _)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(rust_analyzer, target_arch = "x86_64"))]
|
||||
impl ChildTraceImpl {
|
||||
fn read_syscall_entry(&mut self) -> io::Result<(u64, [u64; 6])> {
|
||||
let frame = self.read_regs()?;
|
||||
Ok((
|
||||
frame.rax as _,
|
||||
[
|
||||
frame.rdi as _,
|
||||
frame.rsi as _,
|
||||
frame.rdx as _,
|
||||
frame.r10 as _,
|
||||
frame.r8 as _,
|
||||
frame.r9 as _,
|
||||
],
|
||||
))
|
||||
}
|
||||
|
||||
fn read_syscall_exit(&mut self) -> io::Result<u64> {
|
||||
let frame = self.read_regs()?;
|
||||
Ok(frame.rax as _)
|
||||
}
|
||||
}
|
||||
|
||||
impl ChildTrace for ChildTraceImpl {
|
||||
unsafe fn peek_u64(&mut self, address: usize) -> io::Result<u64> {
|
||||
let word = debug_control::<debug::ReadMemory>(self.tid, &mut self.buffer, &address)?;
|
||||
Ok(word as _)
|
||||
}
|
||||
|
||||
fn kill(&mut self) -> io::Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn resume(&mut self) -> io::Result<()> {
|
||||
debug_control::<debug::Resume>(self.tid, &mut self.buffer, &false).map_err(io::Error::from)
|
||||
}
|
||||
|
||||
fn next_event(&mut self) -> io::Result<TraceEvent> {
|
||||
loop {
|
||||
let mut event = MaybeUninit::uninit();
|
||||
unsafe { runtime::rt::sys::wait_thread(self.tid, &mut event) }
|
||||
.map_err(io::Error::from)?;
|
||||
let event = unsafe { event.assume_init() };
|
||||
|
||||
match event {
|
||||
ThreadEvent::Trace(trace) => {
|
||||
let event = match trace.payload {
|
||||
debug::TraceEventPayload::SyscallEntry(_seq) => {
|
||||
let (nr, args) = self.read_syscall_entry()?;
|
||||
TraceEvent::SyscallEntry(nr, args)
|
||||
}
|
||||
debug::TraceEventPayload::SyscallExit(_seq) => {
|
||||
let status = self.read_syscall_exit()?;
|
||||
TraceEvent::SyscallExit(status as _)
|
||||
}
|
||||
debug::TraceEventPayload::SingleStep => {
|
||||
self.resume()?;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
break Ok(event);
|
||||
}
|
||||
ThreadEvent::Exited => {
|
||||
// TODO status
|
||||
break Ok(TraceEvent::Exited);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::process::CommandSpawnExt;
|
||||
|
||||
pub fn set_sigint_handler(_handler: fn()) {}
|
||||
|
||||
@@ -181,13 +45,6 @@ pub fn clone_fd(fd: RawFd) -> io::Result<RawFd> {
|
||||
}
|
||||
|
||||
impl CommandSpawnExt for Command {
|
||||
type ChildTrace = ChildTraceImpl;
|
||||
|
||||
fn spawn_with_trace(&mut self) -> io::Result<Self::ChildTrace> {
|
||||
unsafe { self.attach_tracing() };
|
||||
self.spawn().and_then(ChildTraceImpl::attach)
|
||||
}
|
||||
|
||||
fn create_process_group(&mut self) -> io::Result<&mut Self> {
|
||||
let group = unsafe { runtime::rt::sys::create_process_group() };
|
||||
Ok(self.process_group(group))
|
||||
|
||||
@@ -33,6 +33,9 @@ yggdrasil-abi.workspace = true
|
||||
yggdrasil-rt.workspace = true
|
||||
runtime.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
runtime.workspace = true
|
||||
|
||||
[lib]
|
||||
path = "src/lib.rs"
|
||||
|
||||
@@ -41,6 +44,14 @@ path = "src/lib.rs"
|
||||
name = "mount"
|
||||
path = "src/mount.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "poweroff"
|
||||
path = "src/poweroff.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "reboot"
|
||||
path = "src/reboot.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "chroot"
|
||||
path = "src/chroot.rs"
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#![feature(rustc_private)]
|
||||
|
||||
use std::{io, process::ExitCode};
|
||||
|
||||
use runtime::{abi::system, rt::system::system_control};
|
||||
|
||||
fn main() -> ExitCode {
|
||||
if let Err(error) = system_control::<system::PowerOff>(&mut [], &()) {
|
||||
let error = io::Error::from(error);
|
||||
eprintln!("{error}");
|
||||
ExitCode::FAILURE
|
||||
} else {
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
fn main() {}
|
||||
@@ -1,3 +1,68 @@
|
||||
#![feature(yggdrasil_os, if_let_guard)]
|
||||
|
||||
fn main() {}
|
||||
// use std::{fmt::{LowerHex, Display}, io, path::PathBuf, process::Command};
|
||||
//
|
||||
// use clap::Parser;
|
||||
// use debugger::{Debugger, SymbolResolver};
|
||||
// use imp::TargetImpl;
|
||||
// use yggdrasil_abi::arch::SavedFrame;
|
||||
//
|
||||
// #[cfg(any(target_arch = "x86_64", target_arch = "x86", rust_analyzer))]
|
||||
// #[path = "x86.rs"]
|
||||
// pub mod imp;
|
||||
// #[cfg(any(target_arch = "aarch64", rust_analyzer))]
|
||||
// #[path = "aarch64.rs"]
|
||||
// pub mod imp;
|
||||
//
|
||||
// pub mod comm;
|
||||
// pub mod state;
|
||||
//
|
||||
// pub mod debugger;
|
||||
//
|
||||
// #[derive(thiserror::Error, Debug)]
|
||||
// pub enum Error {
|
||||
// #[error("I/O error: {0}")]
|
||||
// IoError(#[from] io::Error),
|
||||
// #[error("Terminal error: {0}")]
|
||||
// TermError(#[from] libterm::Error),
|
||||
// #[error("Debug control error: {0:?}")]
|
||||
// DebugError(yggdrasil_rt::Error),
|
||||
// #[error("Invalid address: {0:?}")]
|
||||
// InvalidAddress(String)
|
||||
// }
|
||||
//
|
||||
// pub trait Target {
|
||||
// type Instruction;
|
||||
// type InstructionFormatter: InstructionFormatter<Self::Instruction>;
|
||||
// type Register: LowerHex;
|
||||
//
|
||||
// fn disassemble(
|
||||
// window: &[u8],
|
||||
// ip: usize,
|
||||
// limit: usize,
|
||||
// ) -> Result<Vec<(usize, Self::Instruction)>, Error>;
|
||||
// fn new_instruction_formatter(resolver: SymbolResolver) -> Self::InstructionFormatter;
|
||||
//
|
||||
// fn register_list(frame: &SavedFrame, out: &mut Vec<(String, Self::Register)>);
|
||||
// fn flags_register_as_display(frame: &SavedFrame) -> impl Display;
|
||||
// fn real_ip(frame: &SavedFrame) -> usize;
|
||||
// }
|
||||
//
|
||||
// pub trait InstructionFormatter<I> {
|
||||
// fn format_instruction(&mut self, insn: &I, out: &mut String);
|
||||
// }
|
||||
//
|
||||
// #[derive(Parser)]
|
||||
// struct Args {
|
||||
// program: PathBuf,
|
||||
// }
|
||||
//
|
||||
// fn main() {
|
||||
// let args = Args::parse();
|
||||
//
|
||||
// let command = Command::new(&args.program);
|
||||
// let debug: Debugger<TargetImpl> = Debugger::from_command(&args.program, command).unwrap();
|
||||
//
|
||||
// debug.run().unwrap();
|
||||
// }
|
||||
|
||||
@@ -4,19 +4,9 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
cross.workspace = true
|
||||
runtime.workspace = true
|
||||
|
||||
clap.workspace = true
|
||||
|
||||
[target.'cfg(target_os = "yggdrasil")'.dependencies]
|
||||
runtime.workspace = true
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
libc = "*"
|
||||
|
||||
[dev-dependencies]
|
||||
runtime.workspace = true
|
||||
libc = "*"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
@@ -1,175 +0,0 @@
|
||||
#[cfg(any(rust_analyzer, target_arch = "x86_64"))]
|
||||
#[path = "x86_64.rs"]
|
||||
pub mod imp;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
pub use imp::*;
|
||||
use libc::{c_int, c_uint};
|
||||
|
||||
macro_rules! impl_ioctls {
|
||||
($f:expr, $v:expr, $ty:ty, [$($ioctl:ident),* $(,)?]) => {{
|
||||
#[allow(unreachable_patterns)]
|
||||
let v = match $v as $ty {
|
||||
$(libc::$ioctl => stringify!($ioctl),)*
|
||||
_ => return write!($f, "{:#x}", $v),
|
||||
};
|
||||
write!($f, "{v}")
|
||||
}};
|
||||
}
|
||||
|
||||
fn print_ioctl<W: fmt::Write>(f: &mut W, v: c_uint) -> fmt::Result {
|
||||
impl_ioctls!(
|
||||
f,
|
||||
v,
|
||||
u64,
|
||||
[
|
||||
TCGETS,
|
||||
TCSETS,
|
||||
TCSETSW,
|
||||
TCSETSF,
|
||||
TCGETA,
|
||||
TCSETA,
|
||||
TCSETAW,
|
||||
TCSETAF,
|
||||
TCSBRK,
|
||||
TCXONC,
|
||||
TCFLSH,
|
||||
TIOCEXCL,
|
||||
TIOCNXCL,
|
||||
TIOCSCTTY,
|
||||
TIOCGPGRP,
|
||||
TIOCSPGRP,
|
||||
TIOCOUTQ,
|
||||
TIOCSTI,
|
||||
TIOCGWINSZ,
|
||||
TIOCSWINSZ,
|
||||
TIOCMGET,
|
||||
TIOCMBIS,
|
||||
TIOCMBIC,
|
||||
TIOCMSET,
|
||||
TIOCGSOFTCAR,
|
||||
TIOCSSOFTCAR,
|
||||
FIONREAD,
|
||||
TIOCLINUX,
|
||||
TIOCCONS,
|
||||
TIOCGSERIAL,
|
||||
TIOCSSERIAL,
|
||||
TIOCPKT,
|
||||
FIONBIO,
|
||||
TIOCNOTTY,
|
||||
TIOCSETD,
|
||||
TIOCGETD,
|
||||
TCSBRKP,
|
||||
TIOCSBRK,
|
||||
TIOCCBRK,
|
||||
TIOCGSID,
|
||||
TCGETS2,
|
||||
TCSETS2,
|
||||
TCSETSW2,
|
||||
TCSETSF2,
|
||||
TIOCGRS485,
|
||||
TIOCSRS485,
|
||||
TIOCGPTN,
|
||||
TIOCSPTLCK,
|
||||
TIOCGDEV,
|
||||
TCGETX,
|
||||
TCSETX,
|
||||
TCSETXF,
|
||||
TCSETXW,
|
||||
TIOCSIG,
|
||||
TIOCVHANGUP,
|
||||
TIOCGPKT,
|
||||
TIOCGPTLCK,
|
||||
TIOCGEXCL,
|
||||
TIOCGPTPEER,
|
||||
FIONCLEX,
|
||||
FIOCLEX,
|
||||
FIOASYNC,
|
||||
TIOCSERCONFIG,
|
||||
TIOCSERGWILD,
|
||||
TIOCSERSWILD,
|
||||
TIOCGLCKTRMIOS,
|
||||
TIOCSLCKTRMIOS,
|
||||
TIOCSERGSTRUCT,
|
||||
TIOCSERGETLSR,
|
||||
TIOCSERGETMULTI,
|
||||
TIOCSERSETMULTI,
|
||||
TIOCMIWAIT,
|
||||
TIOCGICOUNT,
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
fn print_prctl<W: fmt::Write>(f: &mut W, c: c_int) -> fmt::Result {
|
||||
impl_ioctls!(
|
||||
f,
|
||||
c,
|
||||
c_int,
|
||||
[
|
||||
PR_SET_PDEATHSIG,
|
||||
PR_GET_PDEATHSIG,
|
||||
PR_GET_DUMPABLE,
|
||||
PR_SET_DUMPABLE,
|
||||
PR_GET_UNALIGN,
|
||||
PR_SET_UNALIGN,
|
||||
PR_GET_KEEPCAPS,
|
||||
PR_SET_KEEPCAPS,
|
||||
PR_GET_FPEMU,
|
||||
PR_SET_FPEMU,
|
||||
PR_GET_FPEXC,
|
||||
PR_SET_FPEXC,
|
||||
PR_GET_TIMING,
|
||||
PR_SET_TIMING,
|
||||
PR_SET_NAME,
|
||||
PR_GET_NAME,
|
||||
PR_GET_ENDIAN,
|
||||
PR_SET_ENDIAN,
|
||||
PR_GET_SECCOMP,
|
||||
PR_SET_SECCOMP,
|
||||
PR_CAPBSET_READ,
|
||||
PR_CAPBSET_DROP,
|
||||
PR_GET_TSC,
|
||||
PR_SET_TSC,
|
||||
PR_GET_SECUREBITS,
|
||||
PR_SET_SECUREBITS,
|
||||
PR_SET_TIMERSLACK,
|
||||
PR_GET_TIMERSLACK,
|
||||
PR_TASK_PERF_EVENTS_DISABLE,
|
||||
PR_TASK_PERF_EVENTS_ENABLE,
|
||||
PR_MCE_KILL,
|
||||
PR_SET_MM,
|
||||
PR_SET_PTRACER,
|
||||
PR_SET_CHILD_SUBREAPER,
|
||||
PR_GET_CHILD_SUBREAPER,
|
||||
PR_SET_NO_NEW_PRIVS,
|
||||
PR_GET_NO_NEW_PRIVS,
|
||||
PR_GET_TID_ADDRESS,
|
||||
PR_SET_THP_DISABLE,
|
||||
PR_GET_THP_DISABLE,
|
||||
PR_MPX_ENABLE_MANAGEMENT,
|
||||
PR_MPX_DISABLE_MANAGEMENT,
|
||||
PR_SET_FP_MODE,
|
||||
PR_GET_FP_MODE,
|
||||
PR_CAP_AMBIENT,
|
||||
PR_GET_SPECULATION_CTRL,
|
||||
PR_SET_SPECULATION_CTRL,
|
||||
PR_SCHED_CORE,
|
||||
PR_SET_MDWE,
|
||||
PR_GET_MDWE,
|
||||
PR_SET_VMA,
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
fn print_access_mode<W: fmt::Write>(f: &mut W, mode: c_int) -> fmt::Result {
|
||||
let s = match mode {
|
||||
libc::F_OK => "F_OK",
|
||||
libc::W_OK => "W_OK",
|
||||
libc::R_OK => "R_OK",
|
||||
libc::X_OK => "X_OK",
|
||||
_ => return write!(f, "{mode:#x}"),
|
||||
};
|
||||
|
||||
write!(f, "{s}")
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
use std::{ffi::c_void, fmt};
|
||||
|
||||
use cross::process::ChildTrace;
|
||||
use libc::{c_char, c_int, c_long, c_uint, c_ulong};
|
||||
|
||||
use crate::definition::{
|
||||
imp::{print_access_mode, print_ioctl, print_prctl},
|
||||
print_cstring, PrintSyscallArgument, Syscall,
|
||||
};
|
||||
|
||||
pub enum SyscallArgument {
|
||||
Fd(c_int),
|
||||
MaybeFd(c_int),
|
||||
Bytes(*const c_char),
|
||||
BytesMut(*const c_char),
|
||||
Size(usize),
|
||||
Filename(*const c_char),
|
||||
OpenOptions(c_int),
|
||||
FileMode(c_int),
|
||||
Ptr(*const c_void),
|
||||
Long(c_long),
|
||||
ULong(c_ulong),
|
||||
Int(c_int),
|
||||
UInt(c_uint),
|
||||
Whence(c_int),
|
||||
Off(libc::off_t),
|
||||
LOff(libc::loff_t),
|
||||
IoctlCmd(c_uint),
|
||||
PrctlCmd(c_int),
|
||||
AccessMode(c_int),
|
||||
AtFd(c_int),
|
||||
}
|
||||
|
||||
impl PrintSyscallArgument for SyscallArgument {
|
||||
fn print_syscall_argument<W: fmt::Write, T: ChildTrace>(&self, f: &mut W, trace: &mut T) {
|
||||
match self {
|
||||
Self::Fd(libc::STDIN_FILENO) => write!(f, "STDIN_FILENO"),
|
||||
Self::Fd(libc::STDOUT_FILENO) => write!(f, "STDOUT_FILENO"),
|
||||
Self::Fd(libc::STDERR_FILENO) => write!(f, "STDERR_FILENO"),
|
||||
Self::Fd(fd) => write!(f, "{fd}"),
|
||||
Self::MaybeFd(-1) => write!(f, "<none>"),
|
||||
Self::MaybeFd(fd) => write!(f, "{fd}"),
|
||||
Self::Bytes(bytes) => write!(f, "{bytes:p}"),
|
||||
Self::BytesMut(bytes) => write!(f, "{bytes:p}"),
|
||||
Self::Size(size) if *size <= 999 => write!(f, "{size}"),
|
||||
Self::Size(size) => write!(f, "{size:#x}"),
|
||||
Self::Filename(filename) => print_cstring(f, trace, filename.addr()),
|
||||
Self::OpenOptions(opts) => write!(f, "{opts:#x}"),
|
||||
Self::FileMode(mode) => write!(f, "0{mode:o}"),
|
||||
Self::Ptr(ptr) => write!(f, "{ptr:p}"),
|
||||
Self::Long(val) => write!(f, "{val}"),
|
||||
Self::ULong(val) => write!(f, "{val}"),
|
||||
Self::Int(val) => write!(f, "{val}"),
|
||||
Self::UInt(val) => write!(f, "{val}"),
|
||||
Self::Whence(libc::SEEK_SET) => write!(f, "SEEK_SET"),
|
||||
Self::Whence(libc::SEEK_CUR) => write!(f, "SEEK_CUR"),
|
||||
Self::Whence(libc::SEEK_END) => write!(f, "SEEK_END"),
|
||||
Self::Whence(w) => write!(f, "{w}"),
|
||||
Self::Off(off) => write!(f, "{off}"),
|
||||
Self::LOff(off) => write!(f, "{off}"),
|
||||
&Self::IoctlCmd(v) => print_ioctl(f, v),
|
||||
&Self::PrctlCmd(v) => print_prctl(f, v),
|
||||
&Self::AccessMode(m) => print_access_mode(f, m),
|
||||
Self::AtFd(libc::AT_FDCWD) => write!(f, "AT_FDCWD"),
|
||||
Self::AtFd(fd) => write!(f, "{fd}"),
|
||||
}
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_syscall_args {
|
||||
($input:expr, [$($collect:expr),*], $i:expr, [$arg:ident, $($tail:ident),+]) => {
|
||||
impl_syscall_args!($input, [$($collect,)* (SyscallArgument::$arg($input[$i] as _))], $i + 1, [$($tail),+])
|
||||
};
|
||||
($input:expr, [$($collect:expr),*], $i:expr, [$arg:ident]) => {
|
||||
vec![$($collect,)* (SyscallArgument::$arg($input[$i] as _))]
|
||||
};
|
||||
($input:expr, [], $i:expr, []) => {
|
||||
vec![]
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_syscall_parse {
|
||||
($(
|
||||
$nr:literal => $name:ident ( $($arg:ident),* )
|
||||
),+ $(,)?) => {
|
||||
pub fn parse_syscall(nr: u64, args: &[u64]) -> Option<Syscall> {
|
||||
Some(match nr {
|
||||
$($nr => Syscall(
|
||||
stringify!($name),
|
||||
impl_syscall_args!(args, [], 0, [$($arg),*])
|
||||
),)+
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_syscall_parse!(
|
||||
0 => read(Fd, BytesMut, Size),
|
||||
1 => write(Fd, Bytes, Size),
|
||||
2 => open(Filename, OpenOptions, FileMode),
|
||||
3 => close(Fd),
|
||||
4 => stat(Filename, BytesMut),
|
||||
5 => fstat(Fd, BytesMut),
|
||||
6 => lstat(Filename, BytesMut),
|
||||
7 => poll(Ptr, ULong, Long),
|
||||
8 => lseek(Fd, Off, Whence),
|
||||
9 => mmap(Ptr, ULong, ULong, ULong, MaybeFd, ULong),
|
||||
10 => mprotect(Ptr, Size, ULong),
|
||||
11 => munmap(Ptr, Size),
|
||||
12 => brk(Ptr),
|
||||
// TODO rt_...
|
||||
16 => ioctl(Fd, IoctlCmd, ULong),
|
||||
17 => pread64(Fd, BytesMut, Size, LOff),
|
||||
21 => access(Filename, AccessMode),
|
||||
157 => prctl(PrctlCmd, ULong, ULong, ULong),
|
||||
218 => set_tid_address(Ptr),
|
||||
334 => rseq(Ptr, UInt, Int, UInt),
|
||||
257 => openat(AtFd, Filename, OpenOptions, FileMode),
|
||||
262 => newfstatat(AtFd, Filename, Ptr, Int),
|
||||
24 => sched_yield(),
|
||||
);
|
||||
@@ -1,67 +0,0 @@
|
||||
use std::fmt;
|
||||
|
||||
use cross::process::ChildTrace;
|
||||
|
||||
#[cfg(any(rust_analyzer, target_os = "linux"))]
|
||||
#[path = "linux/mod.rs"]
|
||||
pub mod imp;
|
||||
#[cfg(any(rust_analyzer, target_os = "yggdrasil"))]
|
||||
#[path = "yggdrasil.rs"]
|
||||
pub mod imp;
|
||||
|
||||
pub use imp::*;
|
||||
|
||||
pub trait PrintSyscallArgument {
|
||||
fn print_syscall_argument<W: fmt::Write, T: ChildTrace>(&self, f: &mut W, trace: &mut T);
|
||||
}
|
||||
|
||||
pub struct Syscall(pub &'static str, pub Vec<SyscallArgument>);
|
||||
|
||||
impl Syscall {
|
||||
pub fn print<W: fmt::Write, T: ChildTrace>(&self, f: &mut W, trace: &mut T) {
|
||||
write!(f, "{}(", self.0).ok();
|
||||
for (i, arg) in self.1.iter().enumerate() {
|
||||
if i != 0 {
|
||||
write!(f, ", ").ok();
|
||||
}
|
||||
arg.print_syscall_argument(f, trace);
|
||||
}
|
||||
write!(f, ")").ok();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_string<W: fmt::Write, T: ChildTrace>(
|
||||
f: &mut W,
|
||||
trace: &mut T,
|
||||
address: usize,
|
||||
len: usize,
|
||||
) -> fmt::Result {
|
||||
let mut buffer = [0; 64];
|
||||
let len = len.min(buffer.len());
|
||||
if let Ok(()) = unsafe { trace.peek_bytes(address, &mut buffer[..len]) } {
|
||||
if let Ok(s) = std::str::from_utf8(&buffer[..len]) {
|
||||
write!(f, "{s:?}")
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
} else {
|
||||
write!(f, "<error str {address:#x}>")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_cstring<W: fmt::Write, T: ChildTrace>(
|
||||
f: &mut W,
|
||||
trace: &mut T,
|
||||
address: usize,
|
||||
) -> fmt::Result {
|
||||
let mut buffer = [0; 64];
|
||||
if let Ok(len) = unsafe { trace.peek_cstr(address, &mut buffer) } {
|
||||
if let Ok(s) = std::str::from_utf8(&buffer[..len]) {
|
||||
write!(f, "{s:?}")
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
} else {
|
||||
write!(f, "<error str {address:#x}>")
|
||||
}
|
||||
}
|
||||
@@ -1,335 +0,0 @@
|
||||
use std::fmt;
|
||||
|
||||
use cross::process::ChildTrace;
|
||||
use runtime::{abi::SyscallFunction, abi_lib::SyscallRegister, rt::io::RawFd};
|
||||
|
||||
use crate::definition::{print_string, PrintSyscallArgument, Syscall};
|
||||
|
||||
pub enum SyscallArgument {
|
||||
Bytes(u64, u64),
|
||||
BytesMut(u64, u64),
|
||||
MaybePtr(u64),
|
||||
Fd(u64),
|
||||
OptionFd(u64),
|
||||
Ptr(u64),
|
||||
Size(u64),
|
||||
U64(u64),
|
||||
I64(i64),
|
||||
U32(u32),
|
||||
I32(i32),
|
||||
PtrSystemTime(u64),
|
||||
ClockType(u64),
|
||||
Filename(u64, u64),
|
||||
}
|
||||
|
||||
pub enum ArgType {
|
||||
Bytes,
|
||||
BytesMut,
|
||||
MaybePtr,
|
||||
Fd,
|
||||
OptionFd,
|
||||
Ptr,
|
||||
Size,
|
||||
U64,
|
||||
I64,
|
||||
U32,
|
||||
I32,
|
||||
PtrSystemTime,
|
||||
ClockType,
|
||||
Filename,
|
||||
}
|
||||
|
||||
struct SyscallDefinition(SyscallFunction, &'static str, &'static [ArgType]);
|
||||
|
||||
impl PrintSyscallArgument for SyscallArgument {
|
||||
fn print_syscall_argument<W: fmt::Write, T: ChildTrace>(&self, f: &mut W, trace: &mut T) {
|
||||
match self {
|
||||
&Self::MaybePtr(0) => write!(f, "None"),
|
||||
Self::Fd(fd) => {
|
||||
let fd = unsafe { RawFd::from_raw(*fd as _) };
|
||||
match fd {
|
||||
RawFd::STDIN => write!(f, "STDIN"),
|
||||
RawFd::STDOUT => write!(f, "STDOUT"),
|
||||
RawFd::STDERR => write!(f, "STDERR"),
|
||||
_ => write!(f, "{fd:?}"),
|
||||
}
|
||||
}
|
||||
Self::OptionFd(fd) => match Option::<RawFd>::from_syscall_register(*fd as _) {
|
||||
Some(fd) => write!(f, "Some({fd:?})"),
|
||||
None => write!(f, "None"),
|
||||
},
|
||||
Self::MaybePtr(addr) => write!(f, "Some({addr:#x})"),
|
||||
Self::Ptr(ptr) => write!(f, "{ptr:#x}"),
|
||||
Self::U64(val) => write!(f, "{val}"),
|
||||
Self::I64(val) => write!(f, "{val}"),
|
||||
Self::U32(val) => write!(f, "{val}"),
|
||||
Self::I32(val) => write!(f, "{val}"),
|
||||
Self::Size(val) => write!(f, "{val}"),
|
||||
Self::Bytes(addr, len) => write!(f, "&[{addr:#x}; {len}]"),
|
||||
Self::BytesMut(addr, len) => write!(f, "&mut [{addr:#x}; {len}]"),
|
||||
Self::PtrSystemTime(ptr) => write!(f, "{ptr:#x}"),
|
||||
Self::ClockType(1) => write!(f, "RealTime"),
|
||||
Self::ClockType(2) => write!(f, "Monotonic"),
|
||||
Self::ClockType(v) => write!(f, "{v}"),
|
||||
Self::Filename(ptr, len) => print_string(f, trace, *ptr as _, *len as _),
|
||||
}
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
|
||||
const DEFS: &[SyscallDefinition] = &const {
|
||||
use ArgType::*;
|
||||
[
|
||||
SyscallDefinition(SyscallFunction::GetRandom, "get_random", &[BytesMut]),
|
||||
SyscallDefinition(
|
||||
SyscallFunction::GetClock,
|
||||
"get_clock",
|
||||
&[ClockType, PtrSystemTime],
|
||||
),
|
||||
SyscallDefinition(
|
||||
SyscallFunction::SetClock,
|
||||
"set_clock",
|
||||
&[ClockType, PtrSystemTime],
|
||||
),
|
||||
SyscallDefinition(SyscallFunction::Mount, "mount", &[Ptr]), // TODO
|
||||
SyscallDefinition(SyscallFunction::Unmount, "unmount", &[Ptr]), // TODO
|
||||
SyscallDefinition(SyscallFunction::LoadModule, "load_module", &[Filename]),
|
||||
SyscallDefinition(
|
||||
SyscallFunction::FilesystemControl,
|
||||
"filesystem_control",
|
||||
&[OptionFd, U32, Size],
|
||||
),
|
||||
SyscallDefinition(
|
||||
SyscallFunction::GetSystemInfo,
|
||||
"get_system_info",
|
||||
&[U32, BytesMut],
|
||||
), // TODO
|
||||
// Memory management
|
||||
SyscallDefinition(
|
||||
SyscallFunction::MapMemory,
|
||||
"map_memory",
|
||||
&[MaybePtr, Size, U32, Ptr],
|
||||
),
|
||||
SyscallDefinition(SyscallFunction::UnmapMemory, "unmap_memory", &[Ptr, Size]),
|
||||
// Process/thread management
|
||||
SyscallDefinition(
|
||||
SyscallFunction::CreateProcessGroup,
|
||||
"create_process_group",
|
||||
&[],
|
||||
),
|
||||
SyscallDefinition(
|
||||
SyscallFunction::GetProcessGroupId,
|
||||
"get_process_group_id",
|
||||
&[],
|
||||
),
|
||||
SyscallDefinition(SyscallFunction::ExitProcess, "exit_process", &[U64]), // TODO
|
||||
SyscallDefinition(SyscallFunction::SpawnProcess, "spawn_process", &[Ptr]), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::WaitProcess,
|
||||
"wait_process",
|
||||
&[Ptr, Ptr, U32],
|
||||
), // TODO
|
||||
SyscallDefinition(SyscallFunction::GetPid, "get_pid", &[]),
|
||||
SyscallDefinition(SyscallFunction::GetTid, "get_tid", &[]),
|
||||
SyscallDefinition(SyscallFunction::SpawnThread, "spawn_thread", &[Ptr]), // TODO
|
||||
SyscallDefinition(SyscallFunction::ExitThread, "exit_thread", &[]),
|
||||
SyscallDefinition(SyscallFunction::WaitThread, "wait_thread", &[U32, Ptr]), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::GetThreadOption,
|
||||
"get_thread_option",
|
||||
&[U32, BytesMut],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::SetThreadOption,
|
||||
"set_thread_option",
|
||||
&[U32, Bytes],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::GetProcessOption,
|
||||
"get_process_option",
|
||||
&[U32, U32, BytesMut],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::SetProcessOption,
|
||||
"set_process_option",
|
||||
&[U32, U32, Bytes],
|
||||
), // TODO
|
||||
SyscallDefinition(SyscallFunction::Nanosleep, "nanosleep", &[Ptr, Ptr]), // TODO
|
||||
SyscallDefinition(SyscallFunction::ExitSignal, "exit_signal", &[Ptr]), // TODO
|
||||
SyscallDefinition(SyscallFunction::SendSignal, "send_signal", &[U32, U32]), // TODO
|
||||
SyscallDefinition(SyscallFunction::Mutex, "mutex", &[Ptr, Ptr]), // TODO
|
||||
SyscallDefinition(SyscallFunction::StartSession, "start_session", &[]),
|
||||
// I/O
|
||||
SyscallDefinition(
|
||||
SyscallFunction::Open,
|
||||
"open",
|
||||
&[OptionFd, Filename, U32, U32],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::CheckAccess,
|
||||
"check_access",
|
||||
&[OptionFd, Filename, U32],
|
||||
), // TODO
|
||||
SyscallDefinition(SyscallFunction::Close, "close", &[Fd]),
|
||||
SyscallDefinition(SyscallFunction::Write, "write", &[Fd, Bytes]),
|
||||
SyscallDefinition(SyscallFunction::Read, "read", &[Fd, BytesMut]),
|
||||
SyscallDefinition(SyscallFunction::Seek, "seek", &[Fd, U32, Ptr]), // TODO
|
||||
SyscallDefinition(SyscallFunction::Truncate, "truncate", &[Fd, U64]), // TODO
|
||||
SyscallDefinition(SyscallFunction::Fsync, "fsync", &[Fd, U32]), // TODO
|
||||
SyscallDefinition(SyscallFunction::ReadAt, "read_at", &[Fd, U64, BytesMut]),
|
||||
SyscallDefinition(SyscallFunction::WriteAt, "write_at", &[Fd, U64, Bytes]),
|
||||
SyscallDefinition(
|
||||
SyscallFunction::GetFileOption,
|
||||
"get_file_option",
|
||||
&[Fd, U32, BytesMut],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::SetFileOption,
|
||||
"set_file_option",
|
||||
&[Fd, U32, Bytes],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::OpenDirectory,
|
||||
"open_directory",
|
||||
&[OptionFd, Filename],
|
||||
),
|
||||
SyscallDefinition(
|
||||
SyscallFunction::ReadDirectoryEntries,
|
||||
"read_directory_entries",
|
||||
&[Fd, Ptr, Size],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::CreateDirectory,
|
||||
"create_directory",
|
||||
&[OptionFd, Filename, U32],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::ReadLink,
|
||||
"read_link",
|
||||
&[OptionFd, Filename, BytesMut],
|
||||
),
|
||||
SyscallDefinition(
|
||||
SyscallFunction::Remove,
|
||||
"remove",
|
||||
&[OptionFd, Filename, U32],
|
||||
), // TODO
|
||||
SyscallDefinition(SyscallFunction::Rename, "rename", &[Ptr]), // TODO
|
||||
SyscallDefinition(SyscallFunction::CloneFd, "clone_fd", &[Fd, OptionFd]),
|
||||
SyscallDefinition(
|
||||
SyscallFunction::UpdateMetadata,
|
||||
"update_metadata",
|
||||
&[OptionFd, Filename, Ptr],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::GetMetadata,
|
||||
"get_metadata",
|
||||
&[OptionFd, Filename, Ptr, U32],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::DeviceRequest,
|
||||
"device_request",
|
||||
&[Fd, U32, BytesMut, Size],
|
||||
), // TODO
|
||||
SyscallDefinition(SyscallFunction::CreateTimer, "create_timer", &[U32]), // TODO
|
||||
SyscallDefinition(SyscallFunction::CreatePid, "create_pid", &[Ptr, U32]), // TODO
|
||||
SyscallDefinition(SyscallFunction::CreatePty, "create_pty", &[Ptr, Ptr, Ptr]), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::CreateSharedMemory,
|
||||
"create_shared_memory",
|
||||
&[Size],
|
||||
),
|
||||
SyscallDefinition(
|
||||
SyscallFunction::CreatePollChannel,
|
||||
"create_poll_channel",
|
||||
&[],
|
||||
),
|
||||
SyscallDefinition(SyscallFunction::CreatePipe, "create_pipe", &[Ptr]), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::PollChannelWait,
|
||||
"poll_channel_wait",
|
||||
&[Fd, Ptr, U32, Ptr],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::PollChannelControl,
|
||||
"poll_channel_control",
|
||||
&[Fd, U32, Fd],
|
||||
), // TODO
|
||||
// Network
|
||||
SyscallDefinition(SyscallFunction::CreateSocket, "create_socket", &[U32]), // TODO
|
||||
SyscallDefinition(SyscallFunction::Bind, "bind", &[Fd, Bytes]), // TODO
|
||||
SyscallDefinition(SyscallFunction::Listen, "listen", &[Fd]),
|
||||
SyscallDefinition(SyscallFunction::Connect, "connect", &[Fd, Bytes]), // TODO
|
||||
SyscallDefinition(SyscallFunction::Accept, "accept", &[Fd, MaybePtr]), // TODO
|
||||
SyscallDefinition(SyscallFunction::Shutdown, "shutdown", &[Fd, U32]), // TODO
|
||||
SyscallDefinition(SyscallFunction::SendTo, "send_to", &[Fd, Bytes, MaybePtr]), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::ReceiveFrom,
|
||||
"receive_from",
|
||||
&[Fd, BytesMut, MaybePtr],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::GetSocketOption,
|
||||
"get_socket_option",
|
||||
&[Fd, U32, BytesMut],
|
||||
), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::SetSocketOption,
|
||||
"set_socket_option",
|
||||
&[Fd, U32, Bytes],
|
||||
), // TODO
|
||||
SyscallDefinition(SyscallFunction::SendMessage, "send_message", &[Fd, Ptr]), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::ReceiveMessage,
|
||||
"receive_message",
|
||||
&[Fd, Ptr],
|
||||
), // TODO
|
||||
// C compat
|
||||
SyscallDefinition(SyscallFunction::Fork, "fork", &[]),
|
||||
SyscallDefinition(SyscallFunction::Execve, "execve", &[Ptr]), // TODO
|
||||
// Debugging
|
||||
SyscallDefinition(SyscallFunction::DebugTrace, "debug_trace", &[U32, Filename]), // TODO
|
||||
SyscallDefinition(
|
||||
SyscallFunction::DebugControl,
|
||||
"debug_control",
|
||||
&[U32, U32, BytesMut, Size],
|
||||
), // TODO
|
||||
]
|
||||
};
|
||||
|
||||
impl SyscallDefinition {
|
||||
fn parse(&self, args: &[u64]) -> Syscall {
|
||||
let mut res = vec![];
|
||||
let mut pos = 0;
|
||||
for arg_def in self.2.iter() {
|
||||
use SyscallArgument::*;
|
||||
|
||||
let (a, l) = match arg_def {
|
||||
ArgType::MaybePtr => (MaybePtr(args[pos]), 1),
|
||||
ArgType::Bytes => (Bytes(args[pos], args[pos + 1]), 2),
|
||||
ArgType::BytesMut => (BytesMut(args[pos], args[pos + 1]), 2),
|
||||
ArgType::Fd => (Fd(args[pos]), 1),
|
||||
ArgType::OptionFd => (OptionFd(args[pos]), 1),
|
||||
ArgType::Ptr => (Ptr(args[pos]), 1),
|
||||
ArgType::Size => (Size(args[pos]), 1),
|
||||
ArgType::U64 => (U64(args[pos]), 1),
|
||||
ArgType::I64 => (I64(args[pos] as _), 1),
|
||||
ArgType::U32 => (U32(args[pos] as _), 1),
|
||||
ArgType::I32 => (I32(args[pos] as _), 1),
|
||||
ArgType::PtrSystemTime => (PtrSystemTime(args[pos]), 1),
|
||||
ArgType::ClockType => (ClockType(args[pos]), 1),
|
||||
ArgType::Filename => (Filename(args[pos], args[pos + 1]), 2),
|
||||
};
|
||||
|
||||
pos += l;
|
||||
res.push(a);
|
||||
}
|
||||
|
||||
Syscall(self.1, res)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_syscall(nr: u64, args: &[u64]) -> Option<Syscall> {
|
||||
let func = SyscallFunction::try_from(nr as usize).ok()?;
|
||||
let def = DEFS.iter().find(|e| e.0 == func)?;
|
||||
Some(def.parse(args))
|
||||
}
|
||||
@@ -1,68 +1,36 @@
|
||||
#![feature(rustc_private)]
|
||||
#![feature(rustc_private, yggdrasil_os, let_chains)]
|
||||
|
||||
use std::{
|
||||
io,
|
||||
process::{Command, ExitCode},
|
||||
sync::atomic::{AtomicBool, Ordering},
|
||||
};
|
||||
|
||||
use clap::Parser;
|
||||
use cross::{
|
||||
process::{ChildTrace, CommandSpawnExt, TraceEvent},
|
||||
signal,
|
||||
};
|
||||
use tracer::CommandTracer;
|
||||
|
||||
use crate::definition::parse_syscall;
|
||||
|
||||
pub mod definition;
|
||||
pub mod format;
|
||||
pub mod tracer;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
struct Args {
|
||||
program: String,
|
||||
command: String,
|
||||
args: Vec<String>,
|
||||
}
|
||||
|
||||
fn run(args: &Args) -> io::Result<()> {
|
||||
let mut command = Command::new(&args.command);
|
||||
command.args(&args.args);
|
||||
let tracer = CommandTracer::spawn(command)?;
|
||||
tracer.run(format::format_syscall)
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let args = Args::parse();
|
||||
|
||||
static ABORT: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
signal::set_sigint_handler(|| {
|
||||
ABORT.store(true, Ordering::Release);
|
||||
eprintln!("SIGINT");
|
||||
});
|
||||
let mut child = Command::new(args.program)
|
||||
.args(args.args)
|
||||
.spawn_with_trace()
|
||||
.unwrap();
|
||||
|
||||
let mut last_syscall: Option<(u64, [u64; 6])> = None;
|
||||
|
||||
while !ABORT.load(Ordering::Relaxed) {
|
||||
let event = child.next_event().unwrap();
|
||||
match event {
|
||||
TraceEvent::Exited => {
|
||||
eprintln!("Exit");
|
||||
break;
|
||||
}
|
||||
TraceEvent::SyscallExit(ret) => match last_syscall {
|
||||
Some((nr, args)) => {
|
||||
let call = parse_syscall(nr, &args);
|
||||
if let Some(call) = call {
|
||||
let mut s = String::new();
|
||||
call.print(&mut s, &mut child);
|
||||
eprintln!("{s} -> {ret}");
|
||||
} else {
|
||||
eprintln!("syscall {nr} {args:?} -> {ret}");
|
||||
}
|
||||
}
|
||||
None => {
|
||||
eprintln!("??? -> {ret}");
|
||||
}
|
||||
},
|
||||
TraceEvent::SyscallEntry(nr, args) => {
|
||||
last_syscall = Some((nr, args));
|
||||
}
|
||||
match run(&args) {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(error) => {
|
||||
eprintln!("{}: {error}", args.command);
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
child.resume().ok();
|
||||
}
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
|
||||
@@ -29,35 +29,37 @@ const PROGRAMS: &[(&str, &str)] = &[
|
||||
// shell
|
||||
("shell", "bin/sh"),
|
||||
// sysutils
|
||||
("echo", "bin/echo"),
|
||||
("mount", "sbin/mount"),
|
||||
("chroot", "sbin/chroot"),
|
||||
("login", "sbin/login"),
|
||||
("strace", "bin/strace"),
|
||||
("ls", "bin/ls"),
|
||||
("mv", "bin/mv"),
|
||||
("ln", "bin/ln"),
|
||||
("mkdir", "bin/mkdir"),
|
||||
("touch", "bin/touch"),
|
||||
("env", "bin/env"),
|
||||
("rm", "bin/rm"),
|
||||
("cat", "bin/cat"),
|
||||
("hexd", "bin/hexd"),
|
||||
("dd", "bin/dd"),
|
||||
("random", "bin/random"),
|
||||
("view", "bin/view"),
|
||||
("grep", "bin/grep"),
|
||||
("chmod", "bin/chmod"),
|
||||
("sha256sum", "bin/sha256sum"),
|
||||
("sysmon", "bin/sysmon"),
|
||||
("chroot", "sbin/chroot"),
|
||||
("date", "bin/date"),
|
||||
("sync", "bin/sync"),
|
||||
("sleep", "bin/sleep"),
|
||||
("dd", "bin/dd"),
|
||||
("echo", "bin/echo"),
|
||||
("env", "bin/env"),
|
||||
("grep", "bin/grep"),
|
||||
("hexd", "bin/hexd"),
|
||||
("ln", "bin/ln"),
|
||||
("login", "sbin/login"),
|
||||
("ls", "bin/ls"),
|
||||
("lspci", "bin/lspci"),
|
||||
("ps", "bin/ps"),
|
||||
("top", "bin/top"),
|
||||
("tst", "bin/tst"),
|
||||
("md2txt", "bin/md2txt"),
|
||||
("mkdir", "bin/mkdir"),
|
||||
("mount", "sbin/mount"),
|
||||
("mv", "bin/mv"),
|
||||
("poweroff", "sbin/poweroff"),
|
||||
("ps", "bin/ps"),
|
||||
("random", "bin/random"),
|
||||
("reboot", "sbin/reboot"),
|
||||
("rm", "bin/rm"),
|
||||
("sha256sum", "bin/sha256sum"),
|
||||
("sleep", "bin/sleep"),
|
||||
("strace", "bin/strace"),
|
||||
("sync", "bin/sync"),
|
||||
("sysmon", "bin/sysmon"),
|
||||
("top", "bin/top"),
|
||||
("touch", "bin/touch"),
|
||||
("tst", "bin/tst"),
|
||||
("view", "bin/view"),
|
||||
// netutils
|
||||
("netconf", "sbin/netconf"),
|
||||
("dhcp-client", "sbin/dhcp-client"),
|
||||
|
||||
Reference in New Issue
Block a user