Skip to content

fix: Resolve build errors and add multi-core synchronization#9

Open
muditbhargava66 wants to merge 3 commits intoasynts:trunkfrom
muditbhargava66:fix/build-errors-and-improvements
Open

fix: Resolve build errors and add multi-core synchronization#9
muditbhargava66 wants to merge 3 commits intoasynts:trunkfrom
muditbhargava66:fix/build-errors-and-improvements

Conversation

@muditbhargava66
Copy link
Copy Markdown

Description

This PR resolves build errors reported in #8 and adds significant improvements
to the PicoOS codebase.
Fixes #8

Changes

Build System Fixes

  • Added Tools/compat/elf.h for macOS ELF definitions
  • Replaced Linux-specific memfd_create/copy_file_range with ANSI C
  • Removed unsupported -fropi/-frwpi flags
  • Fixed missing header includes

New Synchronization Primitives

  • SoftwareSpinLock with deadlock detection
  • SoftwareMutex for blocking synchronization
  • WaitingThreadQueue for thread management
  • Power-efficient wfe/sev implementation

Bug Fixes

  • HardwareSpinLock lock acquisition logic
  • Memory leaks in file handles
  • /dev/tty stat information

Documentation

  • Restructured Docs/ with comprehensive guides
  • Added Mermaid diagrams
  • System call and shell command reference

Testing

  • Build passes on macOS (ARM64)
  • Unit tests pass (18/18)
  • Kernel binary generated successfully

Screenshots

N/A (no UI changes)

Fixes asynts#8

## Build Fixes
- Added Tools/compat/elf.h for macOS ELF definitions
- Replaced Linux-specific memfd_create/copy_file_range with ANSI C
- Removed unsupported -fropi/-frwpi flags and -lbsd dependency
- Fixed missing MaskedInterruptGuard.hpp includes
- Ensured 8-byte stack alignment to prevent hardfaults

## New Features
- SoftwareSpinLock with deadlock detection and wfe/sev
- SoftwareMutex for blocking synchronization
- WaitingThreadQueue for thread management
- Power-efficient multi-core sync primitives

## Bug Fixes
- Fixed HardwareSpinLock infinite loop on lock acquisition
- Fixed memory leaks in VirtualFileHandle
- Fixed stat /dev/tty returning invalid device info
- Fixed ConsoleFileHandle invalid pointer issues

## Documentation
- Restructured Docs/ with architecture guides
- Added Mermaid diagrams for visualization
- Created syscall reference and shell command docs

## Configuration
- Added VSCode IntelliSense and Cortex-Debug support
- Updated CMakeLists.txt with version info
- Reformatted CHANGELOG.md following Keep a Changelog
- **Multi-Core**: Implemented SMP scheduler for RP2040 using `HardwareSpinLock`.
- **Std Lib Enhancements**:
  - Upgraded `SortedSet` to Red-Black Tree (O(log n)).
  - Refactored `Vector` and `Array` for safety (`at()`) and standard iteration.
  - Improved `Singleton` thread-safety documentation and CRTP support.
- **Userland**: Implemented free-list `malloc/free` with coalescing.
- **Fixes**: Resolved `stat /dev/tty` device ID bug.
- **Docs**: Updated CHANGELOG and TODO.
This commit introduces major architectural improvements to the PicoOS kernel:

- **Memory Management**:
  - Implemented dynamic kernel heap growth in `GlobalMemoryAllocator`.
  - Refactored `PageAllocator` with Buddy System handling and detailed documentation.
  - Added `add_memory` support to `Std::MemoryAllocator`.
  - Added `ScopedLock` (RAII) for thread-safe allocation detailed in `Kernel/Synchronization/ScopedLock.hpp`.

- **Security**:
  - Implemented Stack Smashing Protection (SSP) with hardware-randomized canaries using RP2040 ROSC (`StackProtector.cpp`).
  - Enhanced HardFault handlers to differentiate User/Kernel faults and dump register state.

- **System**:
  - Updated `linker.ld` to enforce 8-byte alignment for .stack and .heap sections.
  - Removed unstable emulation scripts.

- **Documentation**:
  - Added `Docs/architecture/memory.md` detailing memory model and security.
  - Updated `README.md`, `Docs/index.md`, and `Docs/architecture/system_overview.md`.
  - Updated `TODO.md` and `CHANGELOG.md`.

Fixes build system alignment issues and completes several TODO items.
@muditbhargava66 muditbhargava66 marked this pull request as draft December 13, 2025 21:56
@muditbhargava66
Copy link
Copy Markdown
Author

Update 2 – 2025-12-14

This update continues the build-fix work and focuses on internal cleanup of the kernel memory path, fault diagnostics, and basic safety checks. The intent here is correctness and debuggability on RP2040, not feature completeness.


Kernel Core & Memory

  • Refactored GlobalMemoryAllocator to support logical heap growth by requesting additional fixed-size regions from PageAllocator when the current heap is exhausted (see Kernel/GlobalMemoryAllocator.cpp, Kernel/PageAllocator.cpp). This is backed by statically available RP2040 SRAM (no virtual memory or runtime expansion).
  • Cleaned up PageAllocator buddy-system initialization with explicit comments describing the RP2040 SRAM layout and allocation assumptions.
  • Improved StackWrapper parameter naming and documentation for clarity.
  • Added ScopedLock RAII helper (Kernel/Synchronization/ScopedLock.hpp) to make lock usage explicit and less error-prone. Current usage assumes non-preemptive sections; IRQ and multicore behavior will be tightened in follow-up work.
  • Enhanced HardFault handling to dump register state and execution context to improve post-mortem debugging. Fault classification is informational and not based on hardware privilege separation.

Safety / Defensive Checks

  • Added a best-effort stack canary initialization using RP2040 ROSC entropy (Kernel/StackProtector.cpp). This is intended for accidental corruption detection, not as a cryptographic security boundary.
  • Enforced 8-byte alignment for .stack and .heap sections in the linker script (Kernel/linker.ld, verified with readelf).
  • Added integer overflow protection to reallocarray() using __builtin_mul_overflow (Kernel/GlobalMemoryAllocator.cpp).

Documentation

  • Added Docs/architecture/memory.md – allocator design, memory layout assumptions, and current limitations.
  • Added Docs/Emulation_Status.md – rationale for deferring QEMU/Renode support.
  • Updated README, architecture docs, TODO, and CHANGELOG to reflect the current kernel state.

Build & Status

  • Removed unstable QEMU/Renode scripts for now; emulation will be reintroduced once the memory model stabilizes.
  • Build passes locally with ninja and produces a kernel binary.

Status: Marked as Draft while hardware testing and allocator stress testing are finalized.

@muditbhargava66 muditbhargava66 marked this pull request as ready for review December 13, 2025 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build error with current trunk branch

1 participant