Skip to content

Conversation

@Galfurian
Copy link
Member

Summary

  • Fixes a long-standing Release-only boot failure by correcting inline assembly constraints for port I/O so the dx register is always used.
  • Restores Release stability while keeping Debug behavior unchanged.

Root Cause

The I/O port helpers used the "dN" constraint while the assembly always referenced dx. In Release builds, the compiler could select an immediate instead of dx, leading to I/O on the wrong ports and device initialization failures.

Changes

  • Force port I/O constraints to use d (dx) consistently.
  • Clean up related logging in PS/2 and allocator paths where needed.

Impact

  • Release builds now boot reliably.
  • Eliminates a years-old optimizer-dependent failure.

Testing

  • Booted in Debug and Release.
  • Verified consistent device initialization and system startup.

Initial executable segments should be mapped as present, not COW.
COW is for fork() where there's an existing backing frame to copy.
Initial loads have no previous frame, causing immediate page faults.
Initial process stack should be mapped as present, not COW.
COW is for fork() with shared backing. Initial stack has no previous frame.
Don't call scheduler_run() when interrupted from kernel mode.
Kernel mode interrupts don't save complete pt_regs frame.
- Add __ps2_delay() for busy-wait delays with memory barriers
- Add __ps2_blind_read_buffer() for clearing output buffer before writes
- Add __ps2_flush_output_buffer() for flushing data with timeout
- Refactor ps2_write_data/command to use __ps2_blind_read_buffer
- Refactor ps2_initialize to use new helper functions throughout
- Eliminates repeated delay loops and buffer flush patterns
- Compiler was eliminating CMOS register reads in Release builds
- Replaced read_register() calls with direct inline assembly in rtc_read_datetime()
- Added NMI disable, I/O wait cycles, and memory barriers
- Improved diagnostics: UIP timeout, all-zero/0xFF reads, mirrored index detection
- Added proper I/O wait helper using port 0x80
- RTC now works correctly in both Debug and Release modes
Replaced 7 duplicate inline assembly blocks with calls to __rtc_read_cmos_direct().
Improves code readability while maintaining unoptimizable inline assembly behavior.
- Fixed volatile semantics in paging test loops to prevent optimization
- Added proper memory barriers and volatile declarations
- Ensured tests work correctly in both Debug and Release builds
…frame

- Replaced ISR_NOERR/ISR_ERR macros with single unified ISR macro
- Added intelligent privilege level detection (kernel vs user mode)
- Normalize stack frame by conditionally pushing SS/UESP in kernel mode
- Ensures pt_regs struct alignment matches actual stack layout
- Fixes crashes when accessing SS register from kernel exceptions
…rnel exceptions

- Removed overly complex stack frame normalization logic
- Simplified isr_common to standard register save/restore
- Made PRINT_REGS conditional: only access SS/UESP for user mode exceptions
- Prevents crashes when exception handlers try to print register state
- Exception frame is now simpler and less error-prone
Changed port I/O constraints from 'dN' to 'd' to force dx register usage.
The 'dN' constraint allowed compiler to use immediate values in Release mode,
but assembly instructions always used %%dx, causing I/O on wrong ports.

This fixed years-long issue where OS only booted in Debug mode.
Reverted temporary changes used for debugging:
- Removed forced Release compilation for userspace programs and libc
- Restored normal CMAKE_BUILD_TYPE optimization flags
- Changed log levels back from DEBUG to NOTICE

The critical I/O port fix remains in place.
@Galfurian Galfurian self-assigned this Feb 9, 2026
@Galfurian Galfurian added bug Something isn't working enhancement New feature or request labels Feb 9, 2026
@Galfurian Galfurian merged commit 449263f into develop Feb 9, 2026
8 checks passed
@Galfurian Galfurian deleted the feature/debug-double-fault branch February 9, 2026 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant