--- Cannot embed stylesheet: [Errno 2] No such file or directory: '../../../aros.css' --->
void * KrnAddExceptionHandler(
uint8_t num,
exhandler_t * handler,
void * handlerData,
void * handlerData2 );
Register a CPU exception handler.
num - CPU-specific exception number.
handler - Exception callback with the signature:
int ExceptionHandler(void *ctx, void *handlerData,
void *handlerData2);
handlerData - First user-defined callback argument.
handlerData2 - Second user-defined callback argument.
Opaque handler handle, or NULL if registration fails.
The ctx callback argument is an opaque CPU context handle. Return a nonzero value from the callback to handle the exception and continue execution. If every handler returns zero, the exception is passed to the task's tc_TrapCode handler. This interface is preliminary and should be treated as private.
void * KrnAddIRQHandler(
uint32_t irq,
irqhandler_t * handler,
void * handlerData,
void * handlerData2 );
Register a hardware IRQ handler.
irq - Hardware-specific interrupt number.
handler - IRQ callback with the signature:
void IRQHandler(void *handlerData, void *handlerData2);
handlerData - First argument passed to the callback.
handlerData2 - Second argument passed to the callback.
Opaque handler handle, or NULL if registration fails.
void * KrnAllocCPUMask();
Allocate a zero-initialized CPU affinity mask.
Pointer to the allocated mask, or NULL if allocation fails (or the platform has no CPU masks, as on uniprocessor builds).
Masks are a fixed size for the lifetime of the boot, large enough for every CPU the platform can number, so a mask stays valid as CPUs come online. A NULL mask is not valid: no CPU is ever selected by it. The TASKAFFINITY_ANY and TASKAFFINITY_ALL_BUT_SELF values are sentinels rather than mask storage.
ULONG KrnAllocIRQ(
ULONG irq_type,
ULONG count );
Allocate a consecutive range of interrupt identifiers.
irq_type - Platform-defined interrupt type to allocate. count - Number of consecutive interrupt identifiers.
First allocated interrupt identifier, or (ULONG)-1 on failure.
void * KrnAllocPages(
void * addr,
uintptr_t length,
uint32_t flags );
Allocate physical memory pages
addr - Starting address of region which must be included in the
allocated region or NULL for the system to choose the
starting address. Normally you will supply NULL here.
length - Length of the memory region to allocate
flags - Flags describing type of needed memory. These are the same
flags as passed to exec.library/AllocMem().
Real starting address of the allocated region.
Since this allocator is page-based, length will always be round up to system's memory page size. The same applies to starting address (if specified), it will be rounded down to page boundary. This function works only on systems with MMU support. Without MMU it will always return NULL.
ULONG KrnBacktraceFromFrame(
APTR frame_in,
APTR * out_pcs,
ULONG max_depth );
Walks the current call stack using frame-pointer chaining and stores the return addresses into the provided array. The function starts from the frame pointer value supplied in frame_in and follows the saved link fields until the maximum depth is reached or the chain becomes invalid. This is a low-level helper used to capture a stack backtrace for diagnostic and crash reporting purposes.
frame_in - Frame pointer of the starting stack frame. Typically this
should be the current function's frame pointer.
out_pcs - Pointer to an array of addresses which will receive the
collected return addresses.
max_depth - Maximum number of entries to store in the output array.
Returns the number of valid entries written into the output array.
APTR pcs[64];
ULONG depth;
depth = KrnBacktraceFromFrame((APTR)__builtin_frame_address(0), pcs, 64);
KrnPrintBacktrace("[Kernel] ", pcs, depth);
This function relies on standard frame-link conventions and requires code to be compiled with frame pointers enabled (for GCC or Clang, use -fno-omit-frame-pointer). The function performs basic safety checks on the frame chain and stops if the next frame pointer is invalid, unaligned, or lower than the previous one. It is safe to call from an exception or trap context.
The function may produce incomplete results if compiler optimizations omit frame pointers, or if stack corruption has occurred prior to the call.
int KrnBug(
const char * format,
va_list args );
Output a formatted string to low-level debug output stream. The function supports the same set of formatting specifiers as standard C printf() function.
format - A format string args - A list of arguments
Number of successfully printed characters
void KrnCause();
Run software interrupt processing sequence
None
None
This entry point directly calls interrupt processing routine in supervisor mode. It neither performs any checks of caller status nor obeys interrupt enable state. This function is safe to call only from within user mode. This function is considered internal, and not meant to be called by user's software.
void KrnClearCPUMask(
void * mask );
Clear all CPU selections in an affinity mask.
mask - CPU affinity mask allocated by KrnAllocCPUMask().
None.
NULL and the TASKAFFINITY_* sentinels are ignored.
void KrnCli();
Instantly disable interrupts.
None
None
This is low level function, it does not have nesting count and state tracking mechanism. It operates directly on the CPU. Normal applications should consider using exec.library/Disable(). This function isn't implemented on all platforms.
BOOL KrnCPUInMask(
uint32_t id,
void * mask );
Determine whether a CPU is selected in an affinity mask.
id - CPU number to test. mask - CPU affinity mask allocated by KrnAllocCPUMask().
TRUE if the CPU is selected; FALSE otherwise. TRUE for any CPU with TASKAFFINITY_ANY, and for any CPU but the caller's with TASKAFFINITY_ALL_BUT_SELF.
A NULL mask is not valid and selects no CPU, so FALSE is returned for it on SMP platforms. This generic uniprocessor version, where KrnAllocCPUMask() returns NULL and affinity is never in force, returns TRUE.
void * KrnCreateContext();
Allocate and initialize CPU context storage area.
None.
A pointer to a CPU context storage area.
CPU context storage is considered private and accessible only from within supevisor mode.
void KrnDeleteContext(
void * context );
Free CPU context storage area
context - a pointer to a CPU context storage previously allocated using
KrnCreateContext()
None.
void KrnDispatch();
Run the next available task
None
None
This entry point directly calls task dispatch routine in supervisor mode. It neither performs any checks of caller status nor obeys interrupt enable state. After calling this function, caller's task will be replaced by another one, and caller's state will be lost. This function is safe to call only from within user mode. This function is considered internal, and not meant to be called by user's software.
void KrnDisplayAlert(
uint32_t code,
const char * text );
Inform the user about critical system failure.
code - Corresponding alert code. text - A NULL-terminated text to print out. First three lines are assumed to be a header. Some implementations may print them centered inside a frame.
None. This function is not guaranteed to return.
This function exists for system internal purposes. Please do not call it from within regular applications! In 99% of cases this function will halt or reboot the machine. Certain structures in RAM, as well as video hardware state, will be irreversibly destroyed. 'code' parameter is passed for convenience. Based on it, the system can make a decision to log the alert in debug output and continue, instead of displaying a message and halting. This function is currently experimental. Its definition may change.
void KrnExitInterrupt(
APTR ctx );
Leave an interrupt using an architecture-defined saved context.
ctx - Saved interrupt context to restore.
The architecture-specific implementation owns context restoration. The generic fallback performs no operation.
void KrnFmtAlertInfo(
STRPTR * TemplatePtr,
IPTR * ParamPtr );
Supply architecture-specific details for an alert message.
TemplatePtr - Pointer to storage for an alert format string. ParamPtr - Pointer to storage for its format parameters.
The generic implementation leaves both outputs unchanged. The all-pc implementation may replace the template and two parameter entries when an interrupt context is available. ParamPtr must provide space for at least two IPTR values.
int KrnFormatStr(
void * putch,
const char * format,
va_list args );
Format a string using C printf() convention, using 'putch' as character output function.
putch - A pointer to the output function format - A format string args - A list of arguments A character output function needs to be declared as: int myPutCh(int char, void *KernelBase) It is expected to return 1 on success and 0 on failure.
Number of successfully printed characters
void KrnFreeCPUMask(
void * mask );
Release storage used by a CPU affinity mask.
mask - CPU affinity mask returned by KrnAllocCPUMask().
NULL.
NULL and the TASKAFFINITY_* sentinels are ignored.
void KrnFreeIRQ(
ULONG start,
ULONG count );
Release a range of interrupt identifiers allocated by KrnAllocIRQ().
start - First interrupt identifier in the allocated range. count - Number of consecutive identifiers to release.
None.
Release support depends on the target.
void KrnFreePages(
void * addr,
uintptr_t length );
Release a region allocated by KrnAllocPages().
addr - Starting address returned by KrnAllocPages(). length - Length of the region in bytes.
None.
This function works only on systems with MMU support.
struct TagItem * KrnGetBootInfo();
Return the kernel boot-information tag list.
Pointer to the boot-information TagItem list (BootMsg).
intptr_t KrnGetCPUAttr(
uint32_t id,
uint32_t cpu );
Get the value of a per-CPU attribute, for the given CPU.
Currently defined attributes are:
KATTR_CPULoad [.G] (ULONG) - Load of the CPU, 0 - 0xffffffff.
KATTR_CPUFrequencyKHz [.G] (IPTR) - Current core clock, in kHz.
KATTR_CPUHardwareID [.G] (IPTR) - The hardware id behind the
CPU number (RISC-V hart id,
x86 local APIC id, ARM
MPIDR affinity fields).
id - ID of the attribute to get cpu - CPU number, 0 .. KrnGetCPUCount() - 1
Value of the attribute, or -1 when the attribute (or the CPU) is not known.
This supersedes querying KrnGetSystemAttr() with the KATTR_CPULoad + n attribute range, which cannot scale beyond the range's size.
unsigned int KrnGetCPUCount();
Return the number of processors running AROS
None
Number of running CPUs in this system, at least 1. It grows while secondary CPUs are brought up, then stays fixed; CPUs the hardware describes but that are not running are not counted.
void KrnGetCPUMask(
uint32_t id,
void * mask );
Add a CPU to an affinity mask.
id - CPU number to select. mask - CPU affinity mask allocated by KrnAllocCPUMask().
None.
The CPU is added to whatever the mask already selects, so a set of CPUs is built with repeated calls. NULL, the TASKAFFINITY_* sentinels and out-of-range CPU numbers are ignored.
uint32_t KrnGetCPUNumber();
Return number of the caller CPU
None
Number of the CPU on which the function is called: a logical number from 0 to KrnGetCPUCount() - 1, the boot CPU being 0. CPU masks and KrnGetCPUAttr() are indexed by it; the hardware id behind it is KrnGetCPUAttr(KATTR_CPUHardwareID, n).
void KrnGetIRQA(
ULONG irq,
struct TagItem * attribs );
void KrnGetIRQ(
ULONG irq,
TAG tag, ... );
Query an IRQ using the passed in tags.
KRN_SchedType KrnGetScheduler();
Return the kernel scheduling policy.
SCHED_RR (round-robin scheduling).
intptr_t KrnGetSystemAttr(
uint32_t id );
Get value of internal system attributes.
Currently defined attributes are:
KATTR_Architecture [.G] (char *) - Name of architecture the kernel built for.
KATTR_ClockSource [.G] APTR - Used Time Source Resource, or -1 if not set.
KATTR_SystemLoad [.G]
KATTR_CPULoad [.G] - Base of the legacy per-CPU load
attribute range. Add the CPU index;
KATTR_CPULoad_END is exclusive.
KATTR_AffinityMask [.G] IPTR - Bitmap of the running CPUs, by
CPU number (KrnGetCPUNumber()).
KATTR_PeripheralBase [.G] IPTR - (ARM only) peripheral IO Base address.
id - ID of the attribute to get
Value of the attribute
void KrnInitMemory(
struct MemHeader * mh );
Initialize kernel memory management on a given memory region
mh - Address of a filled in structure describing the region.
None.
int KrnIsSuper();
Determine if the caller is running in supervisor mode
None
Nonzero for supervisor mode, zero for user mode
Callers should only test the return value against zero. Nonzero values may actually be different, since they may carry some private implementation-dependent information (like CPU privilege level, for example).
int KrnMapGlobal(
void * virt,
void * phys,
uint32_t length,
KRN_MapAttr flags );
Map a physical memory region into the virtual address space.
virt - Starting virtual address of the mapping. phys - Starting physical address of the region. length - Length of the mapping in bytes. flags - Mapping attributes specified by KRN_MapAttr.
Non-zero on success, 0 on failure, on every port.
The range is rounded out to whole pages, and virt and phys must agree below the page size (4 KiB on most ports). Mapping over an existing mapping replaces it. On x86_64 the local TLB is reloaded before the call returns and the other CPUs are asked to reload theirs. That request is asynchronous when the caller is inside Disable(), or when it was already in supervisor state, so on that path the other CPUs' TLBs are not updated before the call returns - the shootdown completes later. On x86_64 page tables created by the call, including the 4 KiB table made when part of a 2 MiB mapping is replaced, are never freed: the cost is bounded at one table per 2 MiB region ever split. On x86_64 a call that fails partway leaves the part of the range it had already processed mapped. On architectures without an MMU, or where this is not implemented, the call does nothing and returns 0 (failure).
int KrnMayGetChar();
Read a single character from low-level debug input stream
None
An ASCII code of the character or -1 if there's no character available
This function never waits. If there is no character available on the stream it just returns with -1
ULONG KrnModifyIRQA(
ULONG irq,
struct TagItem * attribs );
ULONG KrnModifyIRQ(
ULONG irq,
TAG tag, ... );
Configure platform-specific attributes of an interrupt source.
irq - Interrupt identifier to configure. attribs - Tag list of platform-specific interrupt attributes.
void KrnModifyIRQHandler(
void * handle,
void * handlerData,
void * handlerData2 );
Update the arguments passed to a registered IRQ handler.
handle - Handle returned by KrnAddIRQHandler(). handlerData - First argument passed to the callback. handlerData2 - Second argument passed to the callback.
None.
int KrnObtainInput();
Take over low-level debug input hardware and initialize the input
None
Nonzero for success, zero for failure (for example there's no input channel)
void KrnPrintBacktrace(
const STRPTR prefix,
APTR * pcs,
ULONG depth );
Prints a formatted stack backtrace to the kernel debug output. Each entry in the provided address list is optionally resolved to a module and symbol name using the currently registered symbol resolver (if any). The output includes one line per program counter, showing the address and, when available, the function or symbol name with offset.
prefix - Optional string prefix printed before the "Backtrace" header.
If NULL, "[Kernel] " is used.
pcs - Pointer to an array of program counter values representing the
captured call stack.
depth - Number of valid entries in the pcs array.
None
APTR pcs[64];
ULONG depth = KrnBacktraceFromFrame((APTR)__builtin_frame_address(0), pcs, 64);
KrnPrintBacktrace("[Crash] ", pcs, depth);
The function relies on an optional symbol resolver that can be registered with KrnRegisterSymResolver(). If no resolver is present, only raw addresses are printed. This function is safe to call from exception and trap context.
None known.
void KrnPutChar(
char c );
Output a single character to low-level debug output stream
c - A character to output
None
void KrnRegisterClockSource(
APTR CSBase,
struct TagItem * tstags );
void KrnRegisterClockSourceTags(
APTR CSBase,
TAG tag, ... );
Select a clock source for the kernel when none is registered.
CSBase - ClockSource resource base to register. tstags - Tag list; unused by this implementation.
A NULL CSBase or an existing clock source leaves the selection unchanged. Registration also requires AllocCSUnit() to succeed.
LONG KrnRegisterSymResolver(
KrnSymResolver_t resolver,
APTR priv );
Registers a global kernel symbol resolver callback used for decoding instruction addresses into symbolic names. The resolver is used by the kernel when printing diagnostic backtraces or exception dumps. Only one resolver can be active at any time. The function fails if a resolver is already registered.
resolver - Pointer to the resolver callback function. This function will
be called by the kernel with an instruction address and must
fill a KrnSymResolve structure with module and symbol
information.
priv - Optional user data pointer passed unmodified to the resolver
each time it is invoked.
Returns 0 on success, or a negative value on error:
-1 — resolver argument was NULL
-2 — a resolver is already registered
static LONG MyResolver(APTR addr, struct KrnSymResolve *out, APTR priv)
{
return DecodeLocationA(addr, priv);
}
if (KrnRegisterSymResolver(MyResolver, DebugBase) == 0)
KrnBug("Symbol resolver registered.\n");
This API allows an external component (typically debug.library) to provide symbol resolution for kernel stack traces. Once registered, the resolver will be used automatically by KrnPrintBacktrace() and the kernel crash handler. The function is safe to call from user or supervisor context.
Only one resolver can be registered globally. There is currently no queuing or stacking mechanism.
void KrnReleaseInput();
Release low-level debug input hardware and hand it back to the operating system
None
None
void KrnRemExceptionHandler(
void * handle );
Remove a registered CPU exception handler.
handle - Handle returned by KrnAddExceptionHandler().
None.
void KrnRemIRQHandler(
void * handle );
Remove a registered hardware IRQ handler.
handle - Handle returned by KrnAddIRQHandler().
None.
void KrnSchedule();
Run task scheduling sequence
None
None
This entry point directly calls task scheduling routine in supervisor mode. It neither performs any checks of caller status nor obeys interrupt enable state. This function is safe to call only from within user mode. This function is considered internal, and not meant to be called by user's software.
void KrnScheduleCPU(
void * cpu_mask );
Run task scheduling sequence on all CPUs given in the cpu_mask
Pointer to CPU Affinity mask.
None
This entry point directly calls task scheduling routine in supervisor mode. It neither performs any checks of caller status nor obeys interrupt enable state. This function is safe to call only from within user mode. This function is considered internal, and not meant to be called by user's software.
void KrnSetProtection(
void * address,
uint32_t length,
KRN_MapAttr flags );
Set the access permissions of a memory region.
address - Start of the region.
length - Length of the region in bytes.
flags - Requested access permissions, expressed as KRN_MapAttr
attributes.
None.
void KrnSetScheduler(
KRN_SchedType sched );
Currently leaves the kernel scheduling policy unchanged.
sched - Requested scheduling policy (currently ignored).
void KrnSpinInit(
spinlock_t * lock );
Initialize a spinlock to its unlocked state.
lock - Spinlock to initialize.
Initialization is architecture-specific; the generic fallback does not modify the lock.
int KrnSpinIsLocked(
spinlock_t * lock );
Report whether a spinlock is locked.
lock - Pointer to the spinlock to inspect.
Nonzero if the spinlock is locked; zero if it is unlocked.
spinlock_t * KrnSpinLock(
spinlock_t * lock,
struct Hook * failhook,
ULONG mode );
Acquire a spinlock in read or write mode, waiting if it is held on supported architectures.
lock - Spinlock to acquire. failhook - Optional contention callback on supported targets. mode - SPINLOCK_MODE_READ or SPINLOCK_MODE_WRITE.
The supplied spinlock pointer.
The all-pc implementation invokes failhook while waiting for a contended lock. The ARM and RISC-V implementations do not invoke it. The generic fallback returns lock without acquiring it.
spinlock_t * KrnSpinTryLock(
spinlock_t * lock,
ULONG mode );
Attempt to acquire a spinlock in read or write mode without waiting for a current lock holder to release it.
lock - Spinlock to acquire. mode - SPINLOCK_MODE_READ or SPINLOCK_MODE_WRITE.
Lock pointer if acquired; NULL if the requested mode cannot be obtained on an implementation with spinlock support.
The generic fallback returns lock without acquiring it. The implementation and availability are architecture-specific.
void KrnSpinUnLock(
spinlock_t * lock );
Release one read or write acquisition of a spinlock.
lock - Spinlock to release.
Unlocking is architecture-specific; the generic fallback does not modify the lock.
ULONG KrnStatMemoryA(
ULONG flags,
struct TagItem * query );
ULONG KrnStatMemory(
ULONG flags,
TAG tag, ... );
Get various statistics about memory usage
query - An array of TagItems containing query specification. Each
TagItem consists of tag ID and a pointer to a value of
specified type which will contain the result of the query.
Available tag IDs are:
KMS_Free (IPTR) - Get amount of free memory in bytes
KMS_Total (IPTR) - Get total amount of memory in bytes
KMS_LargestAlloc (IPTR) - Get size of the largest allocated chunk in bytes
KMS_SmallestAlloc (IPTR) - Get size of the smallest allocated chunk in bytes
KMS_LargestFree (IPTR) - Get size of the largest free chunk in bytes
KMS_SmallestFree (IPTR) - Get size of the smallest free chunk in bytes
KMS_NumAlloc (IPTR) - Get number of allocated chunks
KMS_NumFree (IPTR) - Get number of free chunks
KMS_PageSize (ULONG) - Get memory page size
flags - Flags which specify physical properties of the memory to query.
These are the same flags as passed to exec.library/AllocMem().
TRUE if the function worked, FALSE if MMU is not up and running on the system. If the function returns FALSE, values will stay uninitialized.
For all unknown tag IDs result values will be set to 0.
void KrnSti();
Instantly enable interrupts.
None
None
This is low level function, it does not have nesting count and state tracking mechanism. It operates directly on the CPU. Normal applications should consider using exec.library/Enable(). This function isn't implemented on all platforms.
void KrnSwitch();
Save context of caller's task and dispatch the next available task
None
None
This entry point directly calls task switch routine in supervisor mode. It neither performs any checks of caller status nor obeys interrupt enable state. After calling this function, caller's task will be replaced by another one, and it's caller's responsibility to do anything to prevent task loss. This function is safe to call only from within user mode. This function is considered internal, and not meant to be called by user's software.
UQUAD KrnTimeStamp();
Read the architecture-specific timestamp counter.
Architecture-specific timestamp value.
The timestamp unit and frequency depend on the architecture. The generic fallback returns zero. This is not wall-clock time.
int KrnUnmapGlobal(
void * virt,
uint32_t length );
Unmap a region of virtual address space.
virt - Starting virtual address of the region. length - Length of the region in bytes.
Non-zero on success, 0 on failure, on every port.
The range is rounded out to whole pages; accessing it afterwards faults. Parts of the range that are not mapped are skipped. On x86_64 the local TLB is reloaded before the call returns and the other CPUs are asked to reload theirs. That request is asynchronous when the caller is inside Disable(), or when it was already in supervisor state, so on that path the other CPUs' TLBs are not updated before the call returns - the shootdown completes later. A 2 MiB mapping that is only partly unmapped is split into 4 KiB pages first; the page table this creates is not freed. A call that fails partway leaves the part of the range it had already processed unmapped. On architectures without an MMU, or where this is not implemented, the call does nothing and returns 0 (failure).
LONG KrnUnregisterSymResolver(
KrnSymResolver_t resolver );
Unregisters the previously installed kernel symbol resolver callback. The resolver function is used by the kernel to translate instruction addresses into module and symbol information when printing diagnostic or crash reports. Only one resolver can be active at a time.
resolver - The resolver callback previously registered with
KrnRegisterSymResolver(). If this value does not match
the currently active resolver, no action is taken.
0 if resolver matches the stored callback; -1 otherwise.
if (old_resolver && KrnUnregisterSymResolver(old_resolver) == 0)
KrnBug("Symbol resolver removed.\n");
The resolver is typically implemented by debug.library and used by the kernel to produce symbolic backtraces when handling traps or exceptions. The function performs no locking and is safe to call from within the resolver itself, or from kernel exception context.
None known.
void * KrnVirtualToPhysical(
void * virtual );
Translate a virtual address to a physical address.
virtual - Virtual address to translate.
Translated address, or the original address when the target's translation rules leave it unchanged.