← Back to Pull requests

P2.4–P2.9: kernel objects, ring 3, budgeted scheduler, IPC, IRQs and init (Phase 2 completion) #151

Closed
unknown closed this pull request without merging on Oct 11, 2026 18:20 UTC
View on GitHub
unknown unknown commented on Oct 06, 2026 12:05 UTC
Author

Finishes the kernel half of Phase 2. P2.4–P2.9 were foundations only: no code ran in ring 3, IPC was non-blocking, the scheduler was a model that never switched threads, and IRQs reached nothing. Capability transfer also had bug #141. After this PR, init boots from a Limine module and runs in ring 3 with real capabilities, budgets, blocking IPC and IRQ delivery.

Needs maintainer action: ADR-0003 (an additive ABI v1 addendum) is Proposed. Phase 2 sign-off depends on accepting it.

Kernel objects and budgets (P2.4)


  • objects.rs: a single registry holds every object together with its state, behind the global Kernel lock (objects::with, interrupts off).

  • invoke BUDGET_CREATE/DESTROY create and destroy all 9 object types, charging and crediting the budget behind the capability.

  • Child budgets are carved out of their parent with carv-budget.

  • The registry refuses to destroy:

  • - the root budget;
    - a budget that still pays for objects;
    - an address space that a live thread still uses.
  • The kernel heap charges its own carv_budget::Budget (limit HEAP_SIZE) on every allocation and is refused when the charge fails. This closes the CLAUDE.md "known gap".
  • Ring 3 and context switch (P2.5)


  • syscall entry stub switches to a per-thread kernel stack (mm/kstack.rs: 64 KiB slots, guard page, slot recycling).

  • Threads return to user mode with sysretq; first entry uses iretq. EFER.SCE is now set.

  • New mm/address_space.rs: user page tables share the kernel half, enforce W^X, and refuse addresses at or above USER_TOP.

  • A ring-3 fault kills only the faulting thread.
  • Scheduler (P2.6)


  • Preemptive round-robin on the 1 kHz LAPIC timer, with a 10 ms budget period. Threads are throttled until their next refill.

  • The kernel runs with IF=0. The timer preempts only when it interrupts ring 3.

  • Dead threads are parked as zombies and freed on the next scheduling decision.
  • IPC (P2.7, #141)


  • send, recv, call and reply_recv block through the scheduler and use the carv_abi::Message wire layout.

  • Sending a capability requires GRANT on it. The receiver's copy goes into its first empty slots as a child of the sender's capability, using the new cross-CSpace derivation tree (carv_caps::CapSpaces), so revoking the sender's capability removes it.
  • IRQs (P2.8)


  • New I/O APIC driver (arch/x86_64/ioapic.rs) that honours ACPI ISA overrides. GSI n arrives on vector 48 + n.

  • New IrqControl and Irq capabilities. IRQ_BIND routes a line to a Notification and unmasks it.

  • The Irq object is charged to the calling thread's budget, so it is reclaimed together with that budget.
  • init (P2.9)


  • New userland/init crate.

  • cargo xtask build builds it into target/userland/, and every ISO ships it as boot():/boot/init.

  • The kernel loads it into a fresh address space with this starting CSpace:
  • | Slot | Capability |
    |---|---|
    | 0 | root budget |
    | 1 | its own thread |
    | 2 | its own address space |
    | 3 | IRQ control |

  • Smoke (UEFI/BIOS) and perf now wait for init: idle instead of halting.
  • ABI (ADR-0003, additive)


  • Results come back in rax (status), rdx and rsi.

  • Nine invoke methods; object types 8 (Irq) and 9 (IrqControl).

  • Capability-transfer semantics as described above.

  • Root CSpace and stack layout constants.

  • debug_putc is available in all builds until P3.4 (fixes the "debug builds only" line in docs/abi.md).

  • Everything is mirrored in carv-abi: ObjectType, method, map, init, USER_TOP.
  • // invoke(cap, method, a0..a3): create an endpoint charged to the root budget
    invoke(init::ROOT_BUDGET, method::BUDGET_CREATE, ObjectType::Endpoint as u64, init::FIRST_FREE, 0, 0)
    

    Tests


  • New kernel/src/usertest.rs. User programs are written in global_asm! and run in ring 3:
  • | Test | Checks |
    |---|---|
    | ring3_thread_prints_and_destroys_itself | debug_putc from ring 3 |
    | ring3_fault_kills_only_the_faulting_thread | fault isolation |
    | budgets_split_the_cpu_thirty_seventy | 30/70 split within ±5% |
    | ipc_call_reply_ping_pong | 2000 rounds; prints TSC cycles per round trip |
    | ipc_transfers_a_capability_and_revoke_takes_it_back, revoke_removes_capabilities_transferred_to_another_cspace | transfer and revocation |
    | irq_wakes_a_user_space_waiter | PIT IRQ wakes a user waiter |
    | invoke_creates_and_destroys_every_object_type | create/destroy plus charges |
    | init_module_is_a_valid_user_image | the shipped init image |

  • test.rs adds:

  • - kernel stack guard and recycling;
    - address-space W^X and user bounds;
    - heap budget charging;
    - an updated syscall MSR test.

    Docs


  • New ADR-0003, added to the ADR index.

  • docs/abi.md addendum.

  • docs/PLAN.md: P2 rows, §11 table and header status.

  • CHANGELOG.md, README.md (boot sample, xtask build) and CLAUDE.md (build command, budget rule).
  • Review notes


  • The 30/70 test aligns to the period boundary before carving budgets: CPU released by a destroyed child budget returns to the parent only at its next refill.

  • Scheduler::zombies stays Vec<Box<Tcb>> (clippy::vec_box allowed on purpose) so a dead thread's saved context doesn't move while the CPU switches away from it.

  • Ping-pong latency measured under TCG (no KVM) is about 1.5M cycles per round trip and does not reflect real hardware.
  • task: P2.4
    task: P2.5
    task: P2.6
    task: P2.7
    task: P2.8
    task: P2.9

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: garrickdabbs Review duration: 7574m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #151"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%