"""Bounded two-PE original atomic/ordered observer; shared CLI owns invocation. This module collects original QEMU frames and RAM, not an Arm outcome model. The single-PE observer and its default assembly remain independent. """ from collections import Counter import json import os from pathlib import Path import struct import subprocess from differential import ROOT, State import simd_original as original from simd_fixtures import atomic_memory PE_COUNT = 2 MAX_PAIRS = 8 MAX_TRIALS = 512 MAX_PROGRAM_WORDS = 16 PAIR_INPUT_BYTES = PE_COUNT * original.INPUT_STRIDE PAIR_RECORD_BYTES = PE_COUNT * original.RECORD_BYTES + original.MEMORY_RAM_BYTES PE_HEADER_BYTES = original.HEADER_BYTES // PE_COUNT MPIDR_OFFSET = original.ATOMIC_FEATURE_HEADER_OFFSET + 8 PSCI_RESULT_OFFSET = MPIDR_OFFSET + 8 SETUP_ERROR_OFFSET = PSCI_RESULT_OFFSET + 8 FINAL_GENERATION_OFFSET = SETUP_ERROR_OFFSET + 8 VBAR_OFFSET = FINAL_GENERATION_OFFSET + 8 CPACR_OFFSET = VBAR_OFFSET + 8 SETUP_SP_OFFSET = CPACR_OFFSET + 8 CONTROL_ADDRESS = 0x40073000 CONTROL_LINE_BYTES = 64 CONTROL_NAMES = ('ready', 'generation', 'armed', 'go', 'done0', 'done1', 'input', 'output', 'pair', 'error') CONTROL = {name: CONTROL_ADDRESS + index * CONTROL_LINE_BYTES for index, name in enumerate(CONTROL_NAMES)} CONTROL_BYTES = len(CONTROL_NAMES) * CONTROL_LINE_BYTES SETUP_STACKS = (0x40078000, 0x4007a000) PSCI_CPU_ON_64 = 0xc4000003 SECONDARY_AFFINITY = 1 MPIDR_AFFINITY_MASK = 0xff00ffffff SETUP_BAD_EL = 1 SETUP_CPU_ON_FAILED = 2 FOCUS = 'atomic_memory_smp' PAIR_FOCUS = 'atomic_pair_memory_smp' DEPENDENCIES = ('dev/simd_atomic_original.py', 'dev/simd_original.py', 'dev/differential.py', 'dev/simd_fixtures/atomic_memory.py', 'dev/a64_checklist.py') def default_pairs(trials=256): """The seven reviewed ordering pairs plus the exact unaligned X64 witness.""" if type(trials) is not int or not 1 <= trials <= MAX_TRIALS: raise ValueError('Original SMP default trial count exceeds fixed bounds') cases = atomic_memory.concurrency_cases() selected = [case for case in cases if case['kind'] == 4] witness = [case for case in cases if case['id'] == 'cross8_ldar_swap_8_offset1'] if len(selected) != 7 or len(witness) != 1: raise AssertionError('Original SMP default reviewed pair inventory changed') return [{**case, 'trials': trials, 'initial': [bytes(atomic_memory.relocate_state(initial, original.MEMORY_RAM_ADDRESS)) for initial in case['initial']]} for case in [*selected, *witness]] def pair_default_pairs(trials=256): """Reviewed CASP pair histories; full outcome validation belongs to the runner.""" from simd_fixtures import atomic_pair_memory if type(trials) is not int or not 1 <= trials <= MAX_TRIALS: raise ValueError('Original CASP SMP trial count exceeds fixed bounds') cases = atomic_pair_memory.original_concurrency_cases() if len(cases) != MAX_PAIRS: raise AssertionError('Original CASP SMP reviewed pair inventory changed') return [{**case, 'trials': trials, 'initial': [bytes(atomic_memory.relocate_state(initial, original.MEMORY_RAM_ADDRESS)) for initial in case['initial']]} for case in cases] def _state_bytes(value): result = bytes.fromhex(value) if isinstance(value, str) else bytes(value) if len(result) != original.STATE_BYTES: raise ValueError('SMP observer requires complete 784-byte initial States') return result def input_files(pairs, focus=FOCUS): """Seal exact absolute-address inputs; no guest pointer rewriting occurs.""" if focus not in (FOCUS, PAIR_FOCUS): raise ValueError('Original SMP focus is not a reviewed atomic family') pair_family = None if focus == PAIR_FOCUS: from simd_fixtures import atomic_pair_memory as pair_family if not 1 <= len(pairs) <= MAX_PAIRS: raise ValueError('Original SMP observer requires one to eight program pairs') plan, programs, inputs, memory = [], [], bytearray(), bytearray() identifiers = set() for pair_index, pair in enumerate(pairs): identifier = pair['id'] if (not isinstance(identifier, str) or not identifier or identifier in identifiers or pair.get('threads', PE_COUNT) != PE_COUNT or pair.get('alias', False)): raise ValueError('Original SMP pair identity/thread/alias policy differs') identifiers.add(identifier) trials = pair.get('trials', pair.get('iterations')) if type(trials) is not int or not 1 <= trials <= MAX_TRIALS: raise ValueError('Original SMP trials exceed the bounded pair limit') pair_programs = pair['programs'] states = [_state_bytes(value) for value in pair['initial']] ram = bytes(pair['memory']) if len(states) != PE_COUNT or len(pair_programs) != PE_COUNT or len(ram) != original.MEMORY_RAM_BYTES: raise ValueError('Original SMP pair needs two programs/States and 8192 RAM bytes') if focus == PAIR_FOCUS and (any(len(program) != 1 for program in pair_programs) or not any(op['kind'] == pair_family.FOCUS for program in pair_programs for op in program)): raise ValueError('Original CASP SMP requires single-operation pair histories containing CASP') for pe, program in enumerate(pair_programs): if not 1 <= len(program) <= MAX_PROGRAM_WORDS: raise ValueError('Original SMP program length exceeds fixed bounds') for op in program: kinds = (atomic_memory.FOCUS, 'atomic_litmus_transfer') if focus == PAIR_FOCUS: kinds += (pair_family.FOCUS,) if op['kind'] not in kinds: raise ValueError('Original SMP accepts only reviewed atomic/ordered litmus words') if type(op['word']) is not int or not 0 <= op['word'] < 1 << 32: raise ValueError('Original SMP subject word is not u32') if op.get('written_vectors', []) or any(type(reg) is not int or not 0 <= reg < 31 for reg in op.get('written_gprs', [])): raise ValueError('Original SMP destination declarations differ') if op['kind'] == 'atomic_litmus_transfer': if op != atomic_memory.ordinary_transfer(op['loading'], op['rt'], op['rn']): raise ValueError('Original SMP ordinary-transfer word/metadata differs') if op.get('operation') != 'dmb': state = State.from_buffer_copy(states[pe]) address = state.sp if op['rn'] == 31 else state.x[op['rn']] is_pair = pair_family is not None and op['kind'] == pair_family.FOCUS width = op['total_bytes'] if is_pair else op['element_bytes'] if not original.MEMORY_RAM_ADDRESS <= address <= original.MEMORY_RAM_ADDRESS + len(ram) - width: raise ValueError('Original SMP inputs must contain absolute owned-RAM bases') if address % atomic_memory.SCA_BYTES + width > atomic_memory.SCA_BYTES: raise ValueError('Original SMP success input crosses the declared SCA granule') if is_pair and op['rn'] == 31 and address % atomic_memory.SCA_BYTES: raise ValueError('Original CASP SMP success input requires aligned SP') programs.append({'operations': program}) for _ in range(trials): for pe, state in enumerate(states): inputs += struct.pack(' original.MARKER_ADDRESS or original.INPUT_ADDRESS + len(inputs) > original.OUTPUT_ADDRESS - original.HEADER_BYTES or original.OUTPUT_ADDRESS + total * PAIR_RECORD_BYTES > original.QEMU_RAM_END or not original.MARKER_ADDRESS + 8 <= CONTROL_ADDRESS or CONTROL_ADDRESS + CONTROL_BYTES > min(SETUP_STACKS) or max(SETUP_STACKS) >= original.TEXT_ADDRESS): raise ValueError('Original SMP input/template/control/output ranges overlap or exceed RAM') source = b''.join(b''.join(struct.pack('> 26 != 0x15 or observed['esr'] & 65535 != original.SVC_IMMEDIATE or observed['elr'] != subject['expected_elr'] or observed['current_el'] != 4 or observed['cpacr'] != original.FP_ACCESS_ENABLED or observed['marker'] != original.COMPLETED_MARKER): raise AssertionError('Original SMP frame controls/SVC/source completion differ') final = bytearray(initial) final[:256] = frame[:256] struct.pack_into('