isync
Instruction Synchronize - 4C 00 01 2C
isync

Instruction Syntax

Mnemonic Format Flags
isync - -

Instruction Encoding

0
1
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
1
0
1
0

Field Bits Description
Primary Opcode 0-5 010011 (0x13)
Reserved 6-10 00000
Reserved 11-15 00000
Reserved 16-20 00000
XO 21-30 0000010010 (150)
Reserved 31 0

Operation

Wait for all previous instructions to complete
Discard any prefetched instructions following isync
Refetch and execute instructions following isync

The instruction synchronize instruction provides an ordering function for instruction execution. It ensures that all instructions preceding the isync instruction complete before any instructions following the isync instruction are executed. Additionally, the isync instruction causes the processor to discard any prefetched instructions and refetch instructions following the isync.

Note: This instruction is essential after cache invalidation operations (like icbi), context changes, or any operation that might affect instruction fetch. It ensures that the processor sees any changes to the instruction stream. The instruction acts as both an execution barrier and an instruction fetch barrier.

Affected Registers

None - This instruction does not affect any registers.

For more information on instruction synchronization see Section 2.1.2, "Instruction Synchronization," in the PowerPC Microprocessor Family: The Programming Environments manual.

Examples

Self-Modifying Code Synchronization

# Complete pattern for self-modifying code
lis r3, target_instruction@ha
addi r3, r3, target_instruction@l

# Modify instruction in memory
lis r4, new_instruction@ha
addi r4, r4, new_instruction@l
lwz r5, 0(r4)           # Load new instruction word
stw r5, 0(r3)           # Store modified instruction

# Ensure cache coherency and instruction synchronization
dcbst 0, r3             # Flush data cache to memory
sync                    # Ensure store completes
icbi 0, r3              # Invalidate instruction cache
isync                   # Synchronize instruction stream

# Now safe to execute modified code
target_instruction:
nop                     # This instruction was modified

Context Switch Synchronization

# Operating system context switch with proper synchronization
# Save current context
stmw r14, save_area(r1) # Save registers

# Load new context
lwz r3, new_msr(r0)     # Load new MSR value
mtmsr r3                # Change processor state
isync                   # Synchronize after MSR change

# Load new context registers
lmw r14, new_context(r0) # Load new register values
lwz r3, new_pc(r0)      # Load new program counter
mtlr r3                 # Set up return address
isync                   # Synchronize before context switch

# Return to new context
blr                     # Jump to new context

Cache Management Sequence

# Comprehensive cache management for dynamic code
lis r3, dynamic_code@ha
addi r3, r3, dynamic_code@l
lwz r4, code_size(r0)
li r5, 0

# Generate new code
generate_code_loop:
    # ... code generation logic ...
    addi r5, r5, 4      # Next instruction
    cmpw r5, r4         # Check if done
    blt generate_code_loop

# Complete cache management sequence
li r5, 0
cache_mgmt_loop:
    add r6, r3, r5      # Current address
    dcbst 0, r6         # Flush data cache
    icbi 0, r6          # Invalidate instruction cache
    addi r5, r5, 32     # Next cache line
    cmpw r5, r4         # Check if done
    blt cache_mgmt_loop

sync                    # Memory barrier
isync                   # Instruction synchronization

# Now execute generated code
mtctr r3
bcctr 20, 0             # Execute generated code

Exception Handler Installation

# Install new exception handler with proper synchronization
lis r3, exception_vector@ha
addi r3, r3, exception_vector@l
lis r4, new_handler@ha
addi r4, r4, new_handler@l

# Create branch instruction to new handler
sub r5, r4, r3          # Calculate displacement
srwi r5, r5, 2          # Convert to word displacement
rlwinm r5, r5, 2, 6, 29 # Position in instruction
oris r5, r5, 0x4800     # Create branch instruction

# Install new exception handler
stw r5, 0(r3)           # Install branch to handler
dcbst 0, r3             # Flush to memory
sync                    # Ensure store completes
icbi 0, r3              # Invalidate instruction cache
isync                   # Synchronize instruction stream

# Exception handler is now active

MMU Configuration Change

# Change MMU configuration with proper synchronization
# Disable interrupts during MMU change
mfmsr r3                # Get current MSR
rlwinm r4, r3, 0, 17, 15 # Clear interrupt enable bits
mtmsr r4                # Disable interrupts
isync                   # Synchronize MSR change

# Update page table entries
lis r5, page_table@ha
addi r5, r5, page_table@l
lwz r6, new_mapping(r0) # Load new page mapping
stw r6, 0(r5)           # Update page table entry

# Flush TLB and synchronize
tlbie r7                # Invalidate TLB entry
sync                    # Ensure TLB invalidation completes
isync                   # Synchronize instruction stream

# Re-enable interrupts
mtmsr r3                # Restore original MSR
isync                   # Synchronize MSR change

Performance Counter Reconfiguration

# Reconfigure performance monitoring unit
mfspr r3, PMC1          # Read current performance counter
lis r4, new_event@ha
addi r4, r4, new_event@l
lwz r5, 0(r4)           # Load new event configuration

# Stop performance monitoring
li r6, 0
mtspr MMCR0, r6         # Disable performance monitoring
isync                   # Synchronize SPR change

# Configure new event
mtspr PMC1, r5          # Set new performance counter event
isync                   # Synchronize SPR change

# Restart performance monitoring
lis r7, mmcr0_enable@ha
addi r7, r7, mmcr0_enable@l
lwz r8, 0(r7)           # Load enable configuration
mtspr MMCR0, r8         # Enable performance monitoring
isync                   # Synchronize SPR change

Branch Prediction Reset

# Reset branch prediction after code modification
lis r3, modified_branch@ha
addi r3, r3, modified_branch@l

# Modify branch target
lis r4, new_target@ha
addi r4, r4, new_target@l
sub r5, r4, r3          # Calculate new displacement
srwi r5, r5, 2          # Word displacement
rlwinm r5, r5, 2, 6, 29 # Position displacement
oris r5, r5, 0x4000     # Create conditional branch
ori r5, r5, 0x0002      # Add condition bits
stw r5, 0(r3)           # Update branch instruction

# Clear instruction cache and branch prediction
dcbst 0, r3             # Flush data cache
sync                    # Ensure store completes
icbi 0, r3              # Invalidate instruction cache
isync                   # Clear branch prediction and synchronize

# Branch prediction is now reset for this address

Secure Boot Verification

# Secure boot code verification and execution
lis r3, boot_code@ha
addi r3, r3, boot_code@l
lwz r4, code_size(r0)

# Verify code integrity
bl verify_signature     # Verify digital signature
cmpwi r3, 0             # Check verification result
bne boot_failure        # Branch if verification failed

# Code is verified, ensure clean execution environment
sync                    # Ensure all pending operations complete
icbi 0, r3              # Invalidate instruction cache
isync                   # Synchronize instruction stream

# Clear any speculative state
li r5, 0
mtctr r5                # Clear count register
mtlr r5                 # Clear link register
isync                   # Synchronize register changes

# Execute verified boot code
lis r3, boot_code@ha
addi r3, r3, boot_code@l
mtctr r3
bcctr 20, 0             # Execute verified code

JIT Compiler Code Installation

# Install JIT compiled code with proper synchronization
lis r3, jit_cache@ha
addi r3, r3, jit_cache@l
lis r4, compiled_method@ha
addi r4, r4, compiled_method@l
lwz r5, method_size(r0)

# Copy compiled method to cache
li r6, 0
copy_method_loop:
    lwzx r7, r4, r6     # Load compiled instruction
    stwx r7, r3, r6     # Store to JIT cache
    addi r6, r6, 4      # Next instruction
    cmpw r6, r5         # Check if done
    blt copy_method_loop

# Synchronize compiled code installation
li r6, 0
sync_method_loop:
    add r7, r3, r6      # Current cache address
    dcbst 0, r7         # Flush data cache
    icbi 0, r7          # Invalidate instruction cache
    addi r6, r6, 32     # Next cache line
    cmpw r6, r5         # Check if done
    blt sync_method_loop

sync                    # Memory barrier
isync                   # Instruction synchronization

# Update method pointer to JIT compiled version
lis r8, method_pointer@ha
addi r8, r8, method_pointer@l
stw r3, 0(r8)          # Point to JIT compiled code
dcbst 0, r8            # Ensure pointer update is visible
sync                    # Memory barrier
isync                   # Instruction synchronization

Debug Trap Installation and Removal

# Install debug trap with synchronization
lis r3, debug_point@ha
addi r3, r3, debug_point@l

# Save original instruction
lwz r4, 0(r3)           # Load original instruction
stw r4, saved_instr(r0) # Save for later restoration

# Install debug trap
lis r5, 0x7FE0          # Debug trap instruction
ori r5, r5, 0x0008      # Complete trap instruction
stw r5, 0(r3)           # Install trap

# Synchronize trap installation
dcbst 0, r3             # Flush data cache
sync                    # Ensure store completes
icbi 0, r3              # Invalidate instruction cache
isync                   # Synchronize instruction stream

# ... debug session occurs ...

# Remove debug trap
lwz r4, saved_instr(r0) # Load original instruction
stw r4, 0(r3)           # Restore original instruction

# Synchronize trap removal
dcbst 0, r3             # Flush data cache
sync                    # Ensure store completes
icbi 0, r3              # Invalidate instruction cache
isync                   # Synchronize instruction stream

Multiprocessor Synchronization Point

# Create synchronization point for multiple processors
lis r3, sync_counter@ha
addi r3, r3, sync_counter@l
lwz r4, processor_count(r0)

# Atomic increment of sync counter
sync_loop:
    lwarx r5, 0, r3     # Load with reservation
    addi r5, r5, 1      # Increment counter
    stwcx. r5, 0, r3    # Store conditional
    bne sync_loop       # Retry if reservation lost

# Check if all processors have reached sync point
cmpw r5, r4             # Compare with total processor count
bne wait_for_others     # Wait if not all processors ready

# All processors ready, proceed with synchronized operation
sync                    # Memory barrier
isync                   # Instruction synchronization

# Perform synchronized operation
bl synchronized_operation

# Reset sync counter for next synchronization point
li r6, 0
stw r6, 0(r3)           # Reset counter
sync                    # Ensure reset is visible
isync                   # Synchronize

wait_for_others:
# Spin wait for other processors
spin_wait:
    lwz r7, 0(r3)       # Load current counter
    cmpw r7, r4         # Check if all ready
    bne spin_wait       # Continue waiting
    
    sync                # Memory barrier
    isync               # Instruction synchronization
    b synchronized_operation

Related Instructions

sync, icbi, dcbst, eieio, dcbf

Back to Index