KThread

KThread

class KThread extends KSynchronizationObject, KTimeableInterruptEvent, KSendableInterruptEvent and KSchedulableInterruptEvent;

Size : 0xB0 bytes

OffsetTypeDescription
0x0u32Pointer to vtable
0x4u32Reference count
0x8u32Count of KThreads that sync with this object - number of nodes in the linked list below
0xCKLinkedListNode*Pointer to first KLinkedListNode in node list of KThreads that sync with this object
0x10KLinkedListNode*Pointer to last KLinkedListNode in node list of KThreads that sync with this object
0x14KTimeableInterruptEventUsed to suspend threads (*this)
0x24KSendableInterruptEventInterrupt event (*this) sent to terminate a thread, except in the case just below
0x2CKSchedulableInterruptEventInterrupt event (*this) scheduled by the IRQ handler when the thread should terminate (*this)
0x34u8Scheduling mask
0x35u8Set to 1 when a thread is woken up from a svcSendSyncRequest call due to the ServerSession endpoint closing down
0x36s8Indicates that the thread shall terminate
0x37u8Indicates there was an error translating the parameters in the command buffer during an IPC call
0x38KDebugThread*Pointer to KDebugThread object used with the current KThread
0x3Cu32Base thread priority
0x40u32Pointer to object the KThread is waiting on- can be a timer, event, session, etc.
0x44ResultStatus for object above
0x48KObjectMutex*Locking kernel mutex
0x4Cu32Arbitration address
0x50KLinkedListNode*Pointer to first KLinkedListNode in node list of objects this thread is waiting on
0x54KLinkedListNode*Pointer to last KLinkedListNode in node list of objects this thread is
0x5CKMutexLinkedList *Set in some very specific circumstances
0x60u32Count of KMutex objects this thread is using
0x64KLinkedListNode*Pointer to first KLinkedListNode in node list of KMutex objects this thread is using
0x68KLinkedListNode*Pointer to last KLinkedListNode in node list of KMutex objects this thread is using
0x6Cs32Dynamic thread priority
0x70s32Processor that created the thread (in the sense of “first ran”) ; processor the thread is running in
0x74KPreemptionTimer *Points to KResourceLimit+0x60, which among other things holds the amount of CPU time available in ticks, or NULL
0x7Cu8Thread is alive
0x7Du8Thread has been terminated
0x7Eu8Thread affinity mask - set differently depending on whether the thread is created via svc call or from inside the kernel
0x80KProcess*Process the thread belongs to (virtual address)
0x84u32Thread id
0x88u32*Ptr to svc mode register storage for KThread inside the thread context.
0x8Cu32*End-address of the page for this thread context allocated in the 0xFF4XX000 region. Thus, if the beginning of this mapped page is 0xFF401000, this ptr would be 0xFF402000. Thread context page - used for thread svc stack, preserving svc mode registers and VFP exception register for thread.
0x90s32Ideal processor (processorid value from svcCreateThread)
0x94void*Ptr to Thread Local Storage
0x98void*Ptr to Thread Local Storage in FCRAM via kernel vmem
0xA0KThreadLinkedListNodePrevious and next scheduled threads
0xA8KThreadLinkedList *Pointer to linked list of scheduled threads that has stolen it, or 0 if in normal list
0xACs32Priority to restore after sleep if suspended, otherwise -1

With the following declarations: struct KThreadLinkedList { KThread *first, *last; }; and struct KThreadLinkedListNode { KThread *prev, *next; };.

Thread Scheduling Mask #

The thread scheduling mask is made of a low nibble (enum) and a high nibble (bitfield).

Low nibble:

ValueDescription
0The thread is not scheduled
1The thread is scheduled
2The thread is being terminated

High nibble:

MaskDescription
0Nothing special
0x8 (bit 3)The thread has been debug-locked

A thread is scheduled *if and only if* its (full) scheduling mask is exactly 1. This allows debug-(un)locking of threads to be done transparently.

Thread Affinity Mask #

If the thread is created via a call to SVC 8(CreateThread), which uses thread creation type 3, and the CPU ID is -2(meaning use the core specified in the exheader), the mask is just the affinity mask from the thread’s owner KProcess. If the CPU ID is -1(meaning any core), the affinity mask is set to 0xF on the New3DS and 0x3 on the Old3DS. Otherwise, the affinity mask is (1 << cpu_id) | 1.

If the thread is created via a call by the kernel, which uses thread creation types 0-2, the checks for -1 and -2 are skipped and the mask is always (1 << cpu_id) | 1.

Category:Kernel synchronization objects Category:Kernel interrupt events