blob: 6e31be877827834e3bd584e46cf1b721ee011792 [file]
// Copyright 2026 The Fuchsia Authors
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT
use crate::kernel::percpu::PerCpu;
use crate::kernel::scheduler::Scheduler;
use crate::object::{
EventDispatcher, HandleValue, JobDispatcher, ProcessDispatcher, get_memory_watchdog,
get_root_job_dispatcher, validate_system_resource,
};
use crate::platform_rs::HaltToken;
use crate::platform_rs::halt_helper::platform_graceful_halt_helper;
use crate::platform_rs::power::{PlatformHaltAction, ZirconCrashReason};
use crate::platform_rs::timer::InstantMono;
use crate::user_copy::{UserInPtr, UserOutPtr};
use syscalls_macro::syscall;
use zx_status::Status;
use zx_types::*;
const VALID_SUSPEND_FLAGS: u64 = ZX_SYSTEM_SUSPEND_OPTION_DISCARD
| ZX_SYSTEM_SUSPEND_OPTION_REPORT_ONLY
| ZX_SYSTEM_SUSPEND_OPTION_DEEP;
zr::static_assert!(core::mem::size_of::<zx_wake_source_report_header_t>() == 24);
zr::static_assert!(core::mem::align_of::<zx_wake_source_report_header_t>() == 8);
zr::static_assert!(core::mem::size_of::<zx_wake_source_report_entry_t>() == 72);
zr::static_assert!(core::mem::align_of::<zx_wake_source_report_entry_t>() == 8);
pub const MEMORY_STALL_MAX_WINDOW: zx_duration_mono_t = 10 * 1_000_000_000;
#[syscall]
pub fn sys_system_get_event(
root_job: HandleValue,
kind: u32,
out: &mut HandleValue,
) -> Result<(), Status> {
let rights = if kind == ZX_SYSTEM_EVENT_OUT_OF_MEMORY {
ZX_RIGHT_MANAGE_PROCESS
} else {
// We check for the root job below. We should not need to enforce rights beyond that.
ZX_RIGHT_NONE
};
let job = ProcessDispatcher::with_current(|up| {
up.handle_table().get_dispatcher_with_rights::<JobDispatcher>(up, root_job, rights)
})?;
// Validate that the job is in fact the first usermode job (aka root job).
if &job != get_root_job_dispatcher() {
return Err(Status::ACCESS_DENIED);
}
match kind {
ZX_SYSTEM_EVENT_OUT_OF_MEMORY
| ZX_SYSTEM_EVENT_IMMINENT_OUT_OF_MEMORY
| ZX_SYSTEM_EVENT_MEMORY_PRESSURE_CRITICAL
| ZX_SYSTEM_EVENT_MEMORY_PRESSURE_WARNING
| ZX_SYSTEM_EVENT_MEMORY_PRESSURE_NORMAL => {
let event =
get_memory_watchdog().get_mem_pressure_event(kind).ok_or(Status::INVALID_ARGS)?;
// Do not grant default event rights, as we don't want userspace to, for
// example, be able to signal this event.
*out = ProcessDispatcher::with_current(|up| {
up.make_and_add_handle_from_ref(event, ZX_DEFAULT_SYSTEM_EVENT_LOW_MEMORY_RIGHTS)
})?;
Ok(())
}
_ => Err(Status::INVALID_ARGS),
}
}
#[syscall]
pub fn sys_system_watch_memory_stall(
resource: HandleValue,
kind: u32,
threshold: zx_duration_mono_t,
window: zx_duration_mono_t,
out: &mut HandleValue,
) -> Result<(), Status> {
*out = ProcessDispatcher::with_current(|up| {
up.enforce_basic_policy(ZX_POL_NEW_EVENT)?;
if window > MEMORY_STALL_MAX_WINDOW || window <= 0 || threshold <= 0 || threshold > window {
return Err(Status::INVALID_ARGS);
}
validate_system_resource(resource, ZX_RSRC_SYSTEM_STALL_BASE)?;
let (handle, rights) = EventDispatcher::create_memory_stall(kind, threshold, window)?;
up.make_and_add_handle(handle, rights)
})?;
Ok(())
}
#[syscall]
pub fn sys_system_set_performance_info(
resource: HandleValue,
topic: u32,
info_void: UserInPtr<u8>,
count: usize,
) -> Result<(), Status> {
validate_system_resource(resource, ZX_RSRC_SYSTEM_CPU_BASE)?;
let num_cpus = PerCpu::processor_count();
if count == 0 || count > num_cpus {
return Err(Status::OUT_OF_RANGE);
}
match topic {
ZX_CPU_PERF_SCALE => {
let mut uninit_info =
kalloc::Box::<[zx_cpu_performance_info_t]>::try_new_uninit_slice(count)
.map_err(|_| Status::NO_MEMORY)?;
let performance_info = info_void
.reinterpret::<zx_cpu_performance_info_t>()
.copy_slice_from_user(&mut uninit_info)?;
let mut last_cpu = u32::MAX;
for info in performance_info.iter() {
let cpu = info.logical_cpu_number;
if last_cpu != u32::MAX && cpu <= last_cpu {
return Err(Status::INVALID_ARGS);
}
last_cpu = cpu;
if cpu as usize >= num_cpus
|| (info.performance_scale.integer_part == 0
&& info.performance_scale.fractional_part == 0)
{
return Err(Status::OUT_OF_RANGE);
}
}
// SAFETY: `performance_info.len() == count <= num_cpus`, each
// `logical_cpu_number < num_cpus` was verified above, and no scheduler queue lock is
// held on the syscall entry path.
unsafe { Scheduler::update_processing_rates(performance_info) };
Ok(())
}
ZX_CPU_PERF_LIMIT => {
let mut uninit_info = kalloc::Box::<[zx_cpu_perf_limit_t]>::try_new_uninit_slice(count)
.map_err(|_| Status::NO_MEMORY)?;
let limit_info = info_void
.reinterpret::<zx_cpu_perf_limit_t>()
.copy_slice_from_user(&mut uninit_info)?;
let mut last_cpu = u32::MAX;
for entry in limit_info.iter() {
let cpu = entry.logical_cpu_number;
if last_cpu != u32::MAX && cpu <= last_cpu {
return Err(Status::INVALID_ARGS);
}
last_cpu = cpu;
if cpu as usize >= num_cpus {
return Err(Status::OUT_OF_RANGE);
}
// TODO(eieio): Add support for the other limit types.
if entry.limit_type != ZX_CPU_PERF_LIMIT_TYPE_RATE {
return Err(Status::NOT_SUPPORTED);
}
}
// SAFETY: `limit_info.len() == count <= num_cpus`, each `logical_cpu_number < num_cpus`
// was verified above, and no scheduler queue lock is held on the syscall entry path.
unsafe { Scheduler::update_processing_limits(limit_info) };
Ok(())
}
_ => Err(Status::INVALID_ARGS),
}
}
#[syscall]
pub fn sys_system_get_performance_info(
resource: HandleValue,
topic: u32,
info_count: usize,
info_void: UserOutPtr<u8>,
output_count: UserOutPtr<usize>,
) -> Result<(), Status> {
validate_system_resource(resource, ZX_RSRC_SYSTEM_CPU_BASE)?;
let num_cpus = PerCpu::processor_count();
if info_count != num_cpus {
return Err(Status::OUT_OF_RANGE);
}
if output_count.is_null() {
return Err(Status::INVALID_ARGS);
}
match topic {
ZX_CPU_PERF_SCALE => {
let mut info =
kalloc::Box::<[zx_cpu_performance_info_t]>::try_new_zeroed_slice(info_count)
.map_err(|_| Status::NO_MEMORY)?;
// SAFETY: `info.len() == info_count == num_cpus` and no scheduler queue lock is held
// on the syscall entry path.
unsafe { Scheduler::get_performance_scales(&mut info) };
info_void.reinterpret::<zx_cpu_performance_info_t>().copy_slice_to_user(&info)?;
output_count.write(info_count)?;
Ok(())
}
ZX_CPU_DEFAULT_PERF_SCALE => {
let mut info =
kalloc::Box::<[zx_cpu_performance_info_t]>::try_new_zeroed_slice(info_count)
.map_err(|_| Status::NO_MEMORY)?;
// SAFETY: `info.len() == info_count == num_cpus` and no scheduler queue lock is held
// on the syscall entry path.
unsafe { Scheduler::get_default_performance_scales(&mut info) };
info_void.reinterpret::<zx_cpu_performance_info_t>().copy_slice_to_user(&info)?;
output_count.write(info_count)?;
Ok(())
}
ZX_CPU_PERF_LIMIT => {
let mut info = kalloc::Box::<[zx_cpu_perf_limit_t]>::try_new_zeroed_slice(info_count)
.map_err(|_| Status::NO_MEMORY)?;
// SAFETY: `info.len() == info_count == num_cpus` and no scheduler queue lock is held
// on the syscall entry path.
unsafe { Scheduler::get_processing_limits(&mut info) };
info_void.reinterpret::<zx_cpu_perf_limit_t>().copy_slice_to_user(&info)?;
output_count.write(info_count)?;
Ok(())
}
_ => Err(Status::INVALID_ARGS),
}
}
#[syscall]
pub fn sys_system_powerctl(power_rsrc: HandleValue, cmd: u32) -> Result<(), Status> {
validate_system_resource(power_rsrc, ZX_RSRC_SYSTEM_POWER_BASE)?;
match cmd {
ZX_SYSTEM_POWERCTL_REBOOT => {
platform_graceful_halt_helper(
PlatformHaltAction::Reboot,
ZirconCrashReason::NoCrash,
InstantMono::INFINITE,
);
}
ZX_SYSTEM_POWERCTL_ACK_KERNEL_INITIATED_REBOOT => HaltToken::get().ack_pending_halt(),
ZX_SYSTEM_POWERCTL_REBOOT_BOOTLOADER => {
platform_graceful_halt_helper(
PlatformHaltAction::RebootBootloader,
ZirconCrashReason::NoCrash,
InstantMono::INFINITE,
);
}
ZX_SYSTEM_POWERCTL_REBOOT_RECOVERY => {
platform_graceful_halt_helper(
PlatformHaltAction::RebootRecovery,
ZirconCrashReason::NoCrash,
InstantMono::INFINITE,
);
}
ZX_SYSTEM_POWERCTL_SHUTDOWN => {
platform_graceful_halt_helper(
PlatformHaltAction::Shutdown,
ZirconCrashReason::NoCrash,
InstantMono::INFINITE,
);
}
_ => Err(Status::INVALID_ARGS),
}
}
// TODO(https://fxbug.dev/42182544): Reconcile with HaltToken, zx_system_powerctl, and
// kernel-initiated-oom-reboot.
#[syscall]
pub fn sys_system_suspend_enter(
resource: HandleValue,
resume_deadline: zx_instant_boot_t,
options: u64,
out_header: UserOutPtr<zx_wake_source_report_header_t>,
out_entries: UserOutPtr<zx_wake_source_report_entry_t>,
num_entries: u32,
actual_entries: UserOutPtr<u32>,
) -> Result<(), Status> {
validate_system_resource(resource, ZX_RSRC_SYSTEM_CPU_BASE)?;
// Make sure that any flags passed by the user are defined.
if options & !VALID_SUSPEND_FLAGS != 0 {
return Err(Status::INVALID_ARGS);
}
// DEEP may not be combined with any other option. Requesting deep suspend
// on a system which does not support it behaves as if the option had not
// been specified.
if options & ZX_SYSTEM_SUSPEND_OPTION_DEEP != 0 && options != ZX_SYSTEM_SUSPEND_OPTION_DEEP {
return Err(Status::INVALID_ARGS);
}
// The event parameters need to be "consistent". IOW - if someone wants
// entries, they need to pass a buffer with at least enough room for 1 event,
// and a pointer to a u32 to hold the number of entries returned. If they
// don't, then the pointers should be nullptr, and the num_entries should be
// zero.
let wants_entries = !out_entries.is_null();
if (wants_entries != (num_entries > 0)) || (wants_entries != !actual_entries.is_null()) {
return Err(Status::INVALID_ARGS);
}
// Additionally, if the user passes space for entries, they have to have passed
// room for a report header as well. It is OK to get a report header without
// any entries, but not the other way around.
let wants_report = !out_header.is_null();
if wants_entries && !wants_report {
return Err(Status::INVALID_ARGS);
}
// If the user has asked us to discard wake entries for wake vectors which are
// currently ack'ed, do so now.
if options & ZX_SYSTEM_SUSPEND_OPTION_DISCARD != 0 {
crate::wake_vector::WakeEvent::discard_wake_event_report();
}
// If the user has asked us to only generate a report (but not actually try to
// suspend), make sure they (at least) passed us a buffer to hold a report
// header, then generate the report and get out.
if options & ZX_SYSTEM_SUSPEND_OPTION_REPORT_ONLY != 0 {
if !wants_report {
return Err(Status::INVALID_ARGS);
}
crate::wake_vector::WakeEvent::generate_wake_event_report(
ZX_TIME_INFINITE,
out_header,
out_entries,
num_entries,
actual_entries,
)?;
return Ok(());
}
// Finally, attempt to drop into suspend. Then, if the user asked for a wake
// event report, generate one for them.
// SAFETY: Syscalls execute in normal userspace-backed thread context after
// `LK_INIT_LEVEL_PLATFORM` has completed, with no spinlocks, `TransitionLock`, or active
// `ChainLockTransaction` held, and no concurrent CPU hotplug/unplug operations occur during
// system suspend.
let suspend_start_time = unsafe {
if options & ZX_SYSTEM_SUSPEND_OPTION_DEEP != 0 {
crate::kernel::idle_power_thread::IdlePowerThread::suspend_system(resume_deadline)
} else {
crate::kernel::idle_power_thread::IdlePowerThread::transition_all_active_to_suspend(
resume_deadline,
)
}
};
if wants_report {
crate::wake_vector::WakeEvent::generate_wake_event_report(
suspend_start_time,
out_header,
out_entries,
num_entries,
actual_entries,
)?;
}
Ok(())
}