Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ default = ["arch-x86_64"]
spec = []
arch-x86_64 = []
arch-aarch64 = []
# Hosted QEMU keeps kernel diagnostics off COM1 so an NT serial driver owns the UART.
qemu-debugcon = []
# Mixed-Criticality Systems is the only scheduler model. The
# `mcs` cargo feature is retained as a no-op so existing
# `--features mcs` invocations don't break; nothing is gated on
Expand All @@ -22,6 +24,9 @@ mcs = []
# Compatibility alias. CPU count is discovered at boot; ownership, IPI and
# FPU safeguards must remain active even when callers omit this feature.
smp = []
# Print every user page fault for debugging. Normal builds retain a count and
# report faults that cannot be delivered without serializing every page-in.
trace-user-page-faults = []
# Hot-path IPC bypassing the slowpath. Phase 10c.
fastpath = []
# Lazy FPU state-switch. Phase 10d.
Expand Down
12 changes: 8 additions & 4 deletions DEBUGGING_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,14 @@ function) is at VA `0x78e77d30`, and a fault reported by the kernel as

Workflow to root-cause a userspace fault:

1. The kernel prints `[user #PF: tcb=N cr2=... err=... rip=VA]` on the serial log
(`/tmp/gdb-serial.log`). Note `rip` (where it faulted) and `cr2` (the bad
address). `err` bits: `1`=present, `2`=write, `4`=user — e.g. `err=7` is a
user-mode write to a present (read-only) page.
1. For per-fault details, build the kernel with the
`trace-user-page-faults` feature (add it to the arguments for
`scripts/build_kernel.sh`). The serial log (`/tmp/gdb-serial.log`) then
prints `[user #PF: tcb=N cr2=... err=... rip=VA]` for every user fault.
Normal builds print a count every 4096 faults and retain full details for
faults that cannot reach a handler. Note `rip` (where it faulted) and `cr2`
(the bad address). `err` bits: `1`=present, `2`=write, `4`=user — e.g.
`err=7` is a user-mode write to a present (read-only) page.
2. Break at `rip`, `disassemble` around it to see the faulting instruction and
which register is bad.
3. Follow that register back: `disassemble` earlier, or break at the `call` that
Expand Down
13 changes: 13 additions & 0 deletions codegen/object-api-rust-micro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,16 @@
<description>Exact two-word request without extra caps: expected child PML4 and retained provider PML4 cptrs in the invoker's CSpace. Returns one word: 1 only when all roots have current canonical ASID assignments, the target's actual root equals the expected physical root and differs from the provider physical root; otherwise 0. Wrong capability types and malformed requests reject. No physical identities are disclosed. This serialized observation does not pin resources or authorize subsequent effects.</description>
</method>
</interface>
<interface name="seL4_X86_VSpace" manual_name="x86 virtual address space">
<method id="X86VSpaceQueryPage" name="QueryPage" manual_name="Query Page" manual_label="x86_vspace_querypage">
<brief>Report whether a virtual address has a present leaf mapping in this live VSpace.</brief>
<param dir="in" name="vaddr" type="seL4_Word" description="Virtual address in this VSpace."/>
<param dir="out" name="present" type="seL4_Word" description="One if the page is present, zero otherwise."/>
</method>
</interface>
<interface name="seL4_TCB_YieldTo" manual_name="TCB Yield To">
<method id="TCBYieldTo" name="YieldTo" manual_name="Yield To" manual_label="tcb_yieldto">
<brief>Give one dispatch opportunity to this exact authorized runnable TCB.</brief>
<description>Exact zero-word request without extra caps. The invoked TCB capability names the sole eligible target. Reject self-target, stale, blocked, unschedulable, different-affinity or different-domain targets before effects. A successful request gives the target one dispatch opportunity without changing any TCB priority, IPC binding, Reply ownership or scheduling context. Normal priority scheduling resumes immediately afterward.</description>
</method>
</interface>
9 changes: 9 additions & 0 deletions crates/sel4-rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub const LBL_TCB_RELEASE_EXECUTION_HOLD: u64 = 72;
pub const LBL_TCB_QUERY_REPLY_BINDING: u64 = 73;
pub mod reply_binding;
pub const LBL_TCB_QUERY_VSPACE_BINDING: u64 = 74;
pub const LBL_TCB_YIELD_TO: u64 = 76;
pub const LBL_X86_VSPACE_QUERY_PAGE: u64 = 75;
pub mod vspace_binding;
pub mod legacy_context;
pub mod execution_hold;
Expand Down Expand Up @@ -324,6 +326,13 @@ pub fn tcb_resume(target: u64) -> u64 {
unsafe { syscall5(SYS_SEND, target, msg_info, 0, 0, 0) }
}

/// Give the exact TCB named by `target` one dispatch opportunity. Returns the invocation
/// error label; a nonzero label means that no handoff was scheduled.
#[inline(always)]
pub fn tcb_yield_to(target: u64) -> u64 {
unsafe { syscall5_call(target, LBL_TCB_YIELD_TO << 12, 0, 0, 0) }
}

/// `TCB::SetPriority(target, prio)`.
#[inline(always)]
pub fn tcb_set_priority(target: u64, prio: u64) -> u64 {
Expand Down
10 changes: 10 additions & 0 deletions scripts/build_kernel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,13 @@ KERNEL_BIN="target/mykernel-x86/release/mykernel-rust"
echo "kernel built: $KERNEL_BIN"

./scripts/make_image.sh

# The runner must bind stdout to the transport compiled into this image. Publish
# the marker only after the complete image succeeds so a failed build cannot
# change the transport used for the previous bootable disk.
if [[ ",${FEATURES}," == *",qemu-debugcon,"* ]]; then
printf 'debugcon\n' > .tmp/kernel-debug-output.tmp
else
printf 'uart\n' > .tmp/kernel-debug-output.tmp
fi
mv .tmp/kernel-debug-output.tmp .tmp/kernel-debug-output
32 changes: 24 additions & 8 deletions scripts/run_specs.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# run_specs.sh — boot the disk image in QEMU with serial-on-stdio and exit
# run_specs.sh — boot the disk image in QEMU and exit
# via the isa-debug-exit device once the spec runner finishes.
#
# Pass `--debug` to dump interrupts and CPU resets (useful for triple-fault
Expand Down Expand Up @@ -40,13 +40,13 @@ if [ ! -f "$IMAGE" ]; then
echo "error: $IMAGE missing — run scripts/build_kernel.sh first" >&2
exit 1
fi
QEMU_MEMORY="${QEMU_MEMORY:-2048M}"
QEMU_MEMORY="${QEMU_MEMORY:-3072M}"
QEMU_CPUS="${QEMU_CPUS:-4}"

DEBUG_FLAGS=()
# GRAPHICS=1 (or the `--display`/`--graphics` flag) boots with a real QEMU
# display WINDOW instead of the headless `-nographic` gate, so you can SEE the
# Simpleboot GOP framebuffer that win32k paints (the ReactOS desktop background).
# Simpleboot GOP framebuffer that win32k paints.
# Default stays headless so the CI/spec gate is unchanged.
GRAPHICS="${GRAPHICS:-0}"
while [ $# -gt 0 ]; do
Expand Down Expand Up @@ -81,12 +81,25 @@ if [ "$EXPLICIT_ACCEL" = 0 ]; then
fi
fi

# Display vs headless. Graphics mode selects a real display backend, keeps std
# VGA so OVMF's GOP framebuffer is shown, and keeps serial on stdio. Both lanes
# retain isa-debug-exit: the guest's explicit final verdict must terminate QEMU
# Display vs headless. Graphics mode selects a real display backend and keeps std
# VGA so OVMF's GOP framebuffer is shown. The kernel debug transport determines
# whether COM1 goes to stdio or a guest log file. Both lanes retain
# isa-debug-exit: the guest's explicit final verdict must terminate QEMU
# instead of falling through to the post-verdict spin loop indefinitely.
DISPLAY_FLAGS=()
EXIT_DEVICE=(-device isa-debug-exit,iobase=0x501,iosize=0x2)
SERIAL_FLAGS=(-serial stdio)
DEBUGCON_FLAGS=()
if [ -f .tmp/kernel-debug-output ]; then
case "$(cat .tmp/kernel-debug-output)" in
debugcon)
SERIAL_FLAGS=(-serial file:.tmp/guest-com1.log)
DEBUGCON_FLAGS=(-chardev stdio,id=ntdebugcon,mux=off -device isa-debugcon,iobase=0xe9,chardev=ntdebugcon)
;;
uart) ;;
*) echo "error: unknown kernel debug transport marker" >&2; exit 1 ;;
esac
fi
case "$GRAPHICS" in
1|true|yes|on)
DISP="${QEMU_DISPLAY:-}"
Expand Down Expand Up @@ -117,6 +130,8 @@ esac
# need. Under q35 the boot disk lands on AHCI/SATA and OVMF won't
# auto-boot it from a bare `-drive`, so we attach it explicitly with
# `bootindex=0`. intremap=off: we only do DMA remapping, not IRQ remap.
# The kernel's VT-d context and IO page tables require four levels. Explicitly
# request 48-bit IOVA support: older QEMU defaults to 39-bit/three-level VT-d.
qemu_status=0
qemu-system-x86_64 \
-machine q35 \
Expand All @@ -127,10 +142,11 @@ qemu-system-x86_64 \
-device ide-hd,drive=bootdisk,bus=ahci0.0,bootindex=0 \
-netdev user,id=ntnet0 \
-device e1000,netdev=ntnet0 \
-device intel-iommu,intremap=off \
-device intel-iommu,intremap=off,aw-bits=48 \
-m "$QEMU_MEMORY" \
-smp "$QEMU_CPUS" \
-serial stdio \
"${SERIAL_FLAGS[@]}" \
"${DEBUGCON_FLAGS[@]}" \
-no-reboot \
${DISPLAY_FLAGS[@]+"${DISPLAY_FLAGS[@]}"} \
${EXIT_DEVICE[@]+"${EXIT_DEVICE[@]}"} \
Expand Down
61 changes: 61 additions & 0 deletions scripts/test_run_specs_vtd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python3
"""Check the QEMU VT-d topology against the kernel's four-level IOPT ABI."""

import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest


RUN_SPECS = Path(__file__).with_name("run_specs.sh")


class RunSpecsVtdTest(unittest.TestCase):
def test_launcher_requests_four_level_vtd(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
scripts = root / "scripts"
scripts.mkdir()
shutil.copy2(RUN_SPECS, scripts / "run_specs.sh")
(root / ".tmp").mkdir()
(root / ".tmp" / "disk.img").touch()
firmware = root / "firmware.fd"
firmware.touch()

binaries = root / "bin"
binaries.mkdir()
qemu = binaries / "qemu-system-x86_64"
qemu.write_text(
"#!/bin/sh\n"
'printf "%s\\n" "$@" > "$QEMU_ARGS_FILE"\n'
"exit 1\n"
)
qemu.chmod(0o755)
arguments = root / "qemu-args.txt"
environment = dict(os.environ)
environment.update(
PATH=f"{binaries}{os.pathsep}{environment.get('PATH', '')}",
OVMF=str(firmware),
QEMU_ARGS_FILE=str(arguments),
GRAPHICS="0",
)
result = subprocess.run(
["bash", str(scripts / "run_specs.sh")],
cwd=root,
env=environment,
capture_output=True,
text=True,
timeout=10,
)
self.assertEqual(result.returncode, 0, result.stderr)
args = arguments.read_text().splitlines()
self.assertIn(
("-device", "intel-iommu,intremap=off,aw-bits=48"),
zip(args, args[1:]),
)


if __name__ == "__main__":
unittest.main()
8 changes: 5 additions & 3 deletions src/arch/aarch64/syscall_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ impl DebugSink for SerialSink {
}
}

fn may_consume_direct_handoff(syscall: Syscall, reply: u64) -> bool {
fn may_consume_direct_handoff(syscall: Syscall, reply: u64, msginfo: u64) -> bool {
matches!(
syscall,
Syscall::SysReplyRecv | Syscall::SysNBSendRecv | Syscall::SysNBSendWait
) || (matches!(syscall, Syscall::SysCall) && reply == crate::invocation::REPLY_HANDOFF_MAGIC)
) || (matches!(syscall, Syscall::SysCall)
&& (reply == crate::invocation::REPLY_HANDOFF_MAGIC
|| msginfo == (crate::syscalls::InvocationLabel::TCBYieldTo as u64) << 12))
}

fn completed_receive(
Expand Down Expand Up @@ -205,7 +207,7 @@ pub fn dispatch(frame: *mut UserContext) {
invoker,
syscall,
result.is_ok(),
may_consume_direct_handoff(syscall, context.x[6]),
may_consume_direct_handoff(syscall, context.x[6], args.a1),
);
}

Expand Down
1 change: 1 addition & 0 deletions src/arch/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod paging;
pub mod pic;
pub mod pit;
pub mod syscall_entry;
mod star_selectors;
pub mod usermode;
pub mod vspace;

Expand Down
44 changes: 31 additions & 13 deletions src/arch/x86_64/acpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ pub fn iter_madt_entries<F: FnMut(MadtEntry)>(

pub const DMAR_SIGNATURE: &[u8; 4] = b"DMAR";

#[path = "dmar_scope.rs"]
mod dmar_scope;
pub use dmar_scope::DmarUnit;

/// Walk the loader-provided RSDT/XSDT for one checksummed table signature.
pub fn find_table(sdt_addr: u64, wanted: &[u8; 4]) -> Result<u64, AcpiError> {
let hdr = validate_sdt(sdt_addr)?;
Expand Down Expand Up @@ -320,30 +324,44 @@ pub fn find_pc_rtc(sdt_addr: u64) -> Result<PcRtcInfo, AcpiError> {
})
}

/// Parse the DMAR remapping structures for the first DRHD (DMA
/// Remapping Hardware unit Definition, type 0) and return its IOMMU
/// register base physical address. Layout after the 36-byte SDT header:
/// host_addr_width(u8), flags(u8), reserved[10]; then remapping
/// structures each `type(u16) length(u16) ...`; DRHD has the 64-bit
/// register base at struct-offset +8.
pub fn dmar_first_drhd_base(dmar_addr: u64) -> Option<u64> {
/// This VT-d backend programs one register unit and one root table. Admit
/// exactly one DRHD for segment zero; a scoped DRHD must authorize each
/// requester individually. Multi-unit routing needs an explicit extension.
pub fn dmar_single_drhd(dmar_addr: u64) -> Option<DmarUnit> {
let total_len =
unsafe { read_unaligned(&raw const (*(dmar_addr as *const SdtHeader)).length) } as usize;
let mut off = core::mem::size_of::<SdtHeader>() + 12; // +host_addr_width/flags/resv
let mut off = core::mem::size_of::<SdtHeader>() + 12;
let mut selected = None;
while off + 4 <= total_len {
let ty: u16 = unsafe { read_unaligned((dmar_addr + off as u64) as *const u16) };
let len: u16 = unsafe { read_unaligned((dmar_addr + (off + 2) as u64) as *const u16) };
if len < 4 || off + len as usize > total_len {
break;
return None;
}
if ty == 0 {
// DRHD — register base at +8.
let base: u64 = unsafe { read_unaligned((dmar_addr + (off + 8) as u64) as *const u64) };
return Some(base);
// DRHD fixed part: type/length/flags/reserved/segment/base.
if len < 16 || selected.is_some() {
return None;
}
let flags = unsafe { read_unaligned((dmar_addr + (off + 4) as u64) as *const u8) };
let segment = unsafe { read_unaligned((dmar_addr + (off + 6) as u64) as *const u16) };
let base = unsafe { read_unaligned((dmar_addr + (off + 8) as u64) as *const u64) };
if segment != 0 || base == 0 {
return None;
}
selected = Some(DmarUnit {
base,
include_all: flags & 1 != 0,
scopes_paddr: dmar_addr + off as u64 + 16,
scopes_len: len as usize - 16,
});
}
off += len as usize;
}
None
if off != total_len {
return None;
}
selected.filter(|unit| unit.scopes_valid())
}

// ---------------------------------------------------------------------------
Expand Down
Loading
Loading