/*************************************************************** __CPU_CONTEXT.H This file contains CPU CONTEXT operation functions & data declarations. PART OF : CPU - library . USAGE : Internal only . NOTE : Include "libcpu.h" before this FILE . Copyright (C) 2026 by Andrew V.Kosteltsev. All Rights Reserved. ***************************************************************/ #ifndef __CPU_CONTEXT_H #define __CPU_CONTEXT_H #ifdef __cplusplus extern "C" { #endif #define CPU_GUEST_FD_MAX 256 typedef struct __cpu_fd_entry { int host_fd; unsigned int owned; unsigned int flags; } __cpu_fd_entry; typedef struct __cpu_context cpu_context; struct __cpu_context { __mpu_uint64_t _pc; __mpu_uint64_t _sp; __mpu_uint64_t _fp; __mpu_uint32_t _psw; cpu_stop_reason _stop_reason; __mpu_int32_t _exit_status; __mpu_UNIT_t *_program; __mpu_uint64_t _program_size; __mpu_uint64_t _program_capacity; __mpu_UNIT_t *_data; __mpu_uint64_t _data_size; __mpu_uint64_t _data_capacity; __mpu_UNIT_t *_stack; __mpu_uint64_t _stack_size; __mpu_uint64_t _stack_capacity; __mpu_UNIT_t *_control; __mpu_uint64_t _control_size; __mpu_uint64_t _control_capacity; __mpu_UNIT_t *_input; __mpu_uint64_t _input_size; __mpu_uint64_t _input_capacity; __mpu_UNIT_t *_output; __mpu_uint64_t _output_size; __mpu_uint64_t _output_capacity; __mpu_UNIT_t *_trace; __mpu_uint64_t _trace_size; __mpu_uint64_t _trace_capacity; __mpu_UNIT_t *_heap; __mpu_uint64_t _heap_brk; __mpu_uint64_t _heap_size; __mpu_uint64_t _heap_capacity; __mpu_uint64_t _heap_limit; int _fd_initialized; __cpu_fd_entry _fd[CPU_GUEST_FD_MAX]; int _cwd_initialized; int _cwd_fd; __mpu_UNIT_t _regs[CPU_REGISTERS_MEM_SIZE_IN_UNITS]; }; extern __thread struct __cpu_context *pcctx; #define __PC (pcctx->_pc) #define __SP (pcctx->_sp) #define __FP (pcctx->_fp) #define __REGS ((__mpu_UNIT_t *)&pcctx->_regs[0]) #define __PROGRAM (pcctx->_program) #define __PSIZE (pcctx->_program_size) #define __PCAP (pcctx->_program_capacity) #define __DATA (pcctx->_data) #define __DSIZE (pcctx->_data_size) #define __DCAP (pcctx->_data_capacity) #define __STACK (pcctx->_stack) #define __SSIZE (pcctx->_stack_size) #define __SCAP (pcctx->_stack_capacity) #define __CONTROL (pcctx->_control) #define __CSIZE (pcctx->_control_size) #define __CCAP (pcctx->_control_capacity) #define __INPUT (pcctx->_input) #define __ISIZE (pcctx->_input_size) #define __ICAP (pcctx->_input_capacity) #define __OUTPUT (pcctx->_output) #define __OSIZE (pcctx->_output_size) #define __OCAP (pcctx->_output_capacity) #define __TRACE (pcctx->_trace) #define __TSIZE (pcctx->_trace_size) #define __TCAP (pcctx->_trace_capacity) #define __HEAP (pcctx->_heap) #define __HBRK (pcctx->_heap_brk) #define __HSIZE (pcctx->_heap_size) #define __HCAP (pcctx->_heap_capacity) #define __HLIMIT (pcctx->_heap_limit) #define __STOP_REASON (pcctx->_stop_reason) #define __EXIT_STATUS (pcctx->_exit_status) #define __CPU_PSW (pcctx->_psw) #define __SET_CFLAG(mask) (pcctx->_psw |= (mask)) #define __CLEAR_CFLAG(mask) (pcctx->_psw &= ~(mask)) #define __INVERT_CFLAG(mask) (pcctx->_psw ^= (mask)) #define __CFLAG(mask,shift) ((pcctx->_psw & (mask)) >> (shift)) extern __mpu_uint64_t __cpu_heap_brk( __mpu_uint64_t new_brk ); #ifdef __cplusplus } #endif #endif