Xbox 360 Kernel
The Xbox 360 runs a modified version of the Windows NT kernel:- Base: Windows NT kernel (similar to Windows Server 2003)
- Modifications: Custom Xbox-specific functionality added
- Architecture: Monolithic kernel with loadable modules
- Modules: xboxkrnl.exe (kernel) and xam.xex (Xbox Application Manager)
High-Level Emulation (HLE)
Xenia uses HLE for kernel emulation: Low-Level Emulation (LLE):- Emulates actual kernel machine code instruction-by-instruction
- Accurate but extremely slow
- Requires complete reverse engineering of kernel internals
- Implements kernel APIs as native functions
- Much faster than LLE
- Focuses on API behavior rather than implementation
- Requires understanding what each API does, not how it works
System Call Mechanism
When a game calls a kernel function:Module Loading and Import Resolution
- Game XEX is loaded - The executable format is parsed
- Imports are found - Loader identifies all kernel imports (functions the game needs)
- Syscall insertion - Loader places
sc(syscall) instruction at import location - Export lookup - Kernel export is found and linked to the import
- JIT code generation - JIT generates code to call Xenia’s export implementation
docs/kernel.md.
Syscall Execution
- Guest code executes
sc- System call instruction - JIT intercepts - Recognizes syscall and generates host call
- Context transition - Switch from guest PowerPC context to host context
- Export execution - Native C++ kernel function runs
- Return value - Result placed in r3 register
- Resume guest - Return to translated PowerPC code
Kernel Export Functions
Kernel exports can be defined in two ways:Legacy SHIM_CALL Convention
src/xenia/kernel/xboxkrnl/xboxkrnl_audio.cc.
The SHIM_CALL convention:
- Provides direct access to PowerPC context
- Parameters are in r3-r10 (
SHIM_GET_ARG_32) - Additional params on stack
- Return value in r3 (
SHIM_SET_RETURN_32) - Used in older code, being phased out
Modern Template Convention
- Uses templates to automate parameter extraction
- Type-safe parameter handling
- Automatic return value setting
- Cleaner, more maintainable code
- Preferred for new implementations
dword_result_t and lpdword_t are defined in src/xenia/kernel/util/shim_utils.h.
Kernel Modules
Xenia emulates two kernel modules:xboxkrnl.exe - Xbox Kernel
Location:src/xenia/kernel/xboxkrnl/
A modified NT kernel with Xbox-specific functionality:
API Categories (from docs/kernel.md):
- Memory:
NtAllocateVirtualMemory,MmAllocatePhysicalMemoryEx - Threading:
ExCreateThread,KeSetAffinityThread,NtWaitForSingleObject - Synchronization:
NtCreateEvent,NtCreateMutex,NtCreateSemaphore - I/O:
NtCreateFile,NtReadFile,NtWriteFile - Modules:
XexLoadImage,XexGetProcedureAddress - Debugging:
DbgPrint,KeBugCheck - Audio: Low-level XAudio APIs
- Video: Display and video output APIs
xboxkrnl_memory.cc- Memory managementxboxkrnl_threading.cc- Thread and process managementxboxkrnl_io.cc- File I/O and devicesxboxkrnl_rtl.cc- Runtime library functions- And more…
xam.xex - Xbox Application Manager
Location:src/xenia/kernel/xam/
Xbox-specific functionality not in standard NT kernel:
API Categories:
- User Management: Profiles, avatars, friends
- Achievements: Achievement unlocking and tracking
- Content: DLC, save games, marketplace
- UI: Dashboards, dialogs, keyboard input
- Networking: Xbox Live services
- Matchmaking: Multiplayer session management
- Storage: Save device management
src/xenia/kernel/xam/xam_state.h.
XAM handles the Xbox-specific user experience that makes Xbox 360 distinct from Windows.
Kernel State
TheKernelState class (src/xenia/kernel/kernel_state.h) is the core of kernel emulation:
src/xenia/kernel/kernel_state.h:150+.
Kernel Guest Globals
The kernel maintains guest-visible global data (src/xenia/kernel/kernel_state.h:114):
Object Management
The kernel manages objects (threads, events, files, etc.) through handles:Object Types
All kernel objects derive fromXObject (src/xenia/kernel/xobject.h):
XThread- Thread of executionXEvent- Event object (signaled/unsignaled)XMutant- Mutex (called “mutant” in NT kernel)XSemaphore- SemaphoreXTimer- Timer objectXFile- File handleXModule- Loaded executable module
Object Table
Handles are managed by the object table (src/xenia/kernel/util/object_table.h):
Threading Model
The Xbox 360 has 6 hardware threads (3 cores × 2 threads/core).Thread Creation
Games create threads withExCreateThread or NtCreateThread:
- Allocate
XThreadobject - Create host thread (pthread or Windows thread)
- Set up guest stack and context
- Add to thread list in process
- Return handle to caller
Thread Scheduling
The host OS schedules threads:- No emulation of Xbox 360 scheduler
- Relies on host to balance threads across cores
- Thread affinity can be set to pin to specific cores
- Priority levels are mapped to host priority levels
Thread Synchronization
Synchronization primitives are implemented in kernel: Events (XEvent):
- Manual or auto-reset
- Signaled/unsignaled state
- Wait functions block until signaled
XMutant):
- Recursive locking support
- Ownership tracking
- Priority inheritance
XSemaphore):
- Count-based synchronization
- Maximum count limit
- Lightweight spinlocks for short critical regions
- Implemented as tight loops checking atomic flags
Process Model
The kernel maintains three processes (fromsrc/xenia/kernel/kernel_state.h:128):
Idle Process (X_PROCTYPE_IDLE)
- Runs in interrupt contexts
- Initial kernel startup context
- Minimal functionality
Title Process (X_PROCTYPE_TITLE)
- The game itself
- Most game threads run in this process
- Has access to full system resources
System Process (X_PROCTYPE_SYSTEM)
- System background tasks
- Threads can be created with special flags
- Purpose not fully understood
File System
The kernel provides file I/O through the Virtual File System (VFS):Device Mounting
Games access files through drive letters and symbolic links:File Operations
src/xenia/kernel/xboxkrnl/xboxkrnl_io.cc.
Debugging Support
The kernel provides debugging facilities:Debug Print
Assertions
Performance and Limitations
HLE Benefits
- Much faster than emulating kernel code
- Easier to implement and maintain
- Can use host OS facilities directly
- Focuses effort on game compatibility
HLE Challenges
- Requires reverse engineering each API
- Behavior must match original exactly
- Edge cases and undocumented behavior are difficult
- Some games may depend on kernel internals
Incomplete APIs
Not all kernel functions are implemented:- Some are stubs that log warnings
- Rarely-used APIs may not be needed
- Implementation is driven by game requirements
References
- Kernel export implementations in
src/xenia/kernel/xboxkrnl/ - XAM implementations in
src/xenia/kernel/xam/ - Object management in
src/xenia/kernel/util/object_table.h - Kernel state in
src/xenia/kernel/kernel_state.h
