Commit Graph

203 Commits

Author SHA1 Message Date
Matthew Leach
d8bcc015de arch: arm64: detect kernel stack overflow condition
Currently, when a kernel stack overflow occures, the exception handler
blindly attempts to write the current context to the stack. If the SP
isn't valid this causes another fault, and so on - locking up the
system.

This commit re-arranges the stack layout, performs SP validation before
usage and switches to an emergency stack when SP isn't valid. This
allows the handler to run and panic gracefully.

Fixes: #98
2026-01-03 14:33:51 -08:00
Matthew Leach
57e0aa364c process: task_group: implement pg priority
Currently, each task implements it's own priority value. In Linux, each
thread group (process) has a default process which all tasks in that
group inherit. Tasks can, however, override the default process
priority.

Implement that logic here which also fixes the current compilation error
on master.
2026-01-02 18:16:11 -08:00
Ashwin Naren
477215e641 optimize lookup 2026-01-02 17:01:02 -08:00
Ashwin Naren
8677d7a404 support readlink 2026-01-02 17:01:02 -08:00
Ashwin Naren
40e092faa8 switch qemu-runner back 2026-01-02 17:01:02 -08:00
Ashwin Naren
9d8379c311 fix readdir 2026-01-02 17:01:02 -08:00
Ashwin Naren
d14d2ff355 add note about lookup being suboptimal 2026-01-02 17:01:02 -08:00
Ashwin Naren
dcd74e4e73 optimize read_at 2026-01-02 17:01:02 -08:00
Ashwin Naren
b5138c30f9 working ext4 2026-01-02 17:01:02 -08:00
Matthew Leach
b8bcbfe91b Merge pull request #116 from hexagonal-sun/implement-ownedtask
process: split `Task` into `Task` (shared) and `OwnedTask` (local)
2026-01-02 17:37:43 +00:00
Ashwin Naren
2383bf99bb support stat in procfs 2026-01-01 16:44:55 -08:00
Matthew Leach
37138e1ee7 sched, messenger: get SMP working with sched changes
Add various fixes to get an SMP version of `usertest` working with SMP.
2026-01-01 22:56:52 +00:00
Matthew Leach
41dec05046 sched: implement fast-path return
Previously, the scheduler unconditionally performed a full runqueue
search:(`find_next_runnable_desc`) on every invocation, including every
timer tick and syscall return. This resulted in unnecessary overhead.

This change introduces a "lazy preemption" model:

1. Fast-Path Optimization: `do_schedule` now checks if the current task
   is valid, is not the Idle task, and still has virtual budget remaining.
   If these conditions are met and `force_resched` is not set, the
   scheduler returns immediately without locking the runqueue.

2. Preemption & Idle Handling:
   - `insert_into_runq` now sets `force_resched` if the new task has an
     earlier deadline than the current task, or if the current task is
     Idle.
   - The Idle task is explicitly excluded from the fast-path to ensure
     immediate context switching when new work arrives.
2026-01-01 22:54:43 +00:00
Ashwin Naren
fca835b573 fix scheduling bug 2026-01-01 22:54:43 +00:00
Matthew Leach
35efecad76 process: split Task into Task (shared) and OwnedTask (local)
This commit refactors the core process representation to decouple
"Identity/Resources" from "Execution/Scheduling". Previously, a
monolithic `Task` struct wrapped in `Arc<SpinLock<>>` caused lock
contention during hot scheduling paths and conflated shared state with
CPU-local state.

The `Task` struct has been split into:

1. `Task` (Shared): Holds process-wide resources (VM, FileTable,
Credentials). Managed via `Arc` and internal fine-grained locking.

2. `OwnedTask` (Private): Holds execution state (Context, v_runtime,
signal mask). Strictly owned by a specific CPU (via the Scheduler) and
accessed lock-free.

Key changes:

* Scheduler:
  chedState` now owns tasks via `Box<OwnedTask>`.
  - Transitions between `run_queue` and `running_task` involve strictly
    moving ownership of the Box, ensuring pointer stability.
  - The EEVDF comparison logic now explicitly handles comparisons
    between the queued candidates and the currently running task (which is
    not in the queue).

* Current Task Access:
  - `current()` now returns a `CurrentTaskGuard` which:
    1. Disables preemption (preventing context switches while holding
       the reference).
    2. Performs a runtime borrow check (panic on double-mutable borrow).
    3. Dereferences a cached Per-CPU raw pointer for O(1) access.
2026-01-01 22:54:43 +00:00
ootinnyoo
e8b0eda15c fix issue with AT_EMPTY_PATH and simplify symlink handling 2025-12-30 23:23:01 -08:00
Ashwin Naren
5c86ed2f37 add CI testing 2025-12-29 22:52:59 -08:00
Ashwin Naren
edc9eb3fca add CI testing 2025-12-29 22:52:59 -08:00
Ashwin Naren
02f51a23dd turn SMP into a feature 2025-12-29 22:52:59 -08:00
ootinnyoo
fe4f3103dc implement linux-like process capabilities 2025-12-29 13:53:02 -08:00
Ashwin Naren
379b7ffab8 Refactor vruntime to v_runtime 2025-12-29 12:39:58 -08:00
Matthew Leach
992fe21844 Merge pull request #113 from hexagonal-sun/make-task-list-arc-task
process: `TASK_LIST`: point to `Task` struct
2025-12-29 20:38:56 +00:00
Ashwin Naren
c816054d36 Support cwd symlink in procfs (#101)
Support cwd symlink in procfs
2025-12-29 20:35:31 +00:00
Matthew Leach
9e80a6ae8a process: TASK_LIST: point to Task struct
Make the global `TASK_LIST` struct be a collection of `Task`s, rather
than `task.state` struct members. This allows other cores to access to
any shared task state easily.
2025-12-29 20:28:00 +00:00
Ashwin Naren
74bc44a317 update dependencies 2025-12-29 12:03:32 -08:00
Ashwin Naren
3284b04197 fix build script 2025-12-29 11:54:43 -08:00
Ashwin Naren
9fc6ea6662 fix dockerfile 2025-12-29 11:54:43 -08:00
Ashwin Naren
ddd5b0d461 test usertests on CI 2025-12-29 11:54:43 -08:00
Ashwin Naren
937adb12d0 dockerfile 2025-12-29 11:54:43 -08:00
Matthew Leach
02586457f1 Merge pull request #91 from arihant2math/multicore-sched
Schedule onto multiple cores in round-robin fashion
2025-12-29 16:20:53 +00:00
Matthew Leach
9f0bf1f689 process: mod: last_cpu: use CpuId
Use the new `CpuId` type for the `last_cpu` field in `Task`.
2025-12-29 16:19:49 +00:00
Matthew Leach
bbde9f04aa sched: remove unused function sched_yield 2025-12-28 23:51:51 -08:00
Matthew Leach
108b580e83 sched: fix formatting
Fix formatting in the sched module to keep CI happy.
2025-12-28 23:51:51 -08:00
Matthew Leach
e2e7cdaeec timer: use a per-cpu wakeup queue
Currently, a global wakeup queue is used for all CPUs on the system.
This leads to inefficient behavior regarding preemption. When the
scheduler requests a preemption event, it is inserted into a global list
alongside events from all other CPUs.

When processing IRQs, there is no guarantee which CPU will handle the
timer interrupt. If the current CPU processes a preemption event
intended for a different CPU, it must signal the target CPU via an IPI.
This causes a severe bottleneck, as one CPU may end up distributing
preemption events for the entire system.

Fix this by implementing a per-cpu wakeup queue. Preemption events are
now strictly scheduled for the current CPU, ensuring they are handled
locally by the core that scheduled them. This significantly simplifies
the preemption logic and eliminates the need for IPIs to signal
preemption events.
2025-12-28 23:51:51 -08:00
Ashwin Naren
4fedf19e51 Optimize current-cpu case in task insertion 2025-12-28 23:51:51 -08:00
Ashwin Naren
0f1a486abb Force preemption of idle task 2025-12-28 23:51:51 -08:00
Ashwin Naren
018c1d9450 fix preemption not being scheduled 2025-12-28 23:51:51 -08:00
Ashwin Naren
b818047a8a fix deadlock in interrupt handler 2025-12-28 23:51:51 -08:00
Ashwin Naren
b0d214d3de update debug statements 2025-12-28 23:51:51 -08:00
Ashwin Naren
45135317df fix deadline guard to minimize overhead 2025-12-28 23:51:51 -08:00
Ashwin Naren
0227e0dc9a Preempt IPI 2025-12-28 23:51:51 -08:00
Matthew Leach
116a1adbd0 clone: add all tasks to process task list
This prevents a bug where `sys_exit` calls `exit_group` for the thread's
process, even when there are still active threads.
2025-12-28 23:51:51 -08:00
Matthew Leach
0f566f37e7 sched: remove incorrect assertion check
When switching tasks, we may well be swithing away from a task which is
going to `Sleep`. Therefore the check

```rust
 debug_assert_eq!(*prev_task.state.lock_save_irq(), TaskState::Runnable);
```

Is incorrect.
2025-12-28 23:51:51 -08:00
Ashwin Naren
ca8555283b use messengers
# Conflicts:
#	libkernel/src/sync/per_cpu.rs
2025-12-28 23:51:51 -08:00
Ashwin Naren
0a3d0851ee include last run CPU 2025-12-28 23:51:51 -08:00
Ashwin Naren
39d0bba0c6 EEVDF improvements
Use EEVDF concepts like virtual deadline correctly and actually calculate the necessary deadline and use it to schedule.

Also dynamically preempts based on the deadline.
2025-12-28 23:51:51 -08:00
Ashwin Naren
5e553096dd try fix scheduling on non-main cores 2025-12-28 23:49:33 -08:00
Ashwin Naren
b75f29804f schedule in round-robin fashion 2025-12-28 23:49:33 -08:00
Matthew Leach
7aecc6fecd interrupts: refactor interrupt manager and reduce locking
There is no need to store a seperate inner struct with the interrupt
manager, refactor that.

Also, reduce the amount of locking when servicing an interrupt,
currently we keep the whole interrupt manager locked when servicing an
interrupt. This should be kept unlocked while the ISR is called.
2025-12-28 23:48:43 -08:00
Matthew Leach
3ffb3b2a80 memory: fault: handle colliding page faults
In an SMP environment, two threads sharing an address space may trigger
a page fault on the same address simultaneously. Previously, the loser
of this race would receive an `AlreadyMapped` error from the page table
mapper, causing the kernel to treat a valid execution flow as an error.

This patch modifies `handle_demand_fault` to gracefully handle these
spurious faults by:

1. Accepting `AlreadyMapped` as a successful resolution. If another CPU
has already mapped the page while we were waiting for the lock
(or performing I/O):, we consider the fault handled.

2. Fixing a memory leak in the race path. We now only `leak()` the
allocated `ClaimedPage` (surrendering ownership to the page tables) if
the mapping actually succeeds. If we lose the race, the `ClaimedPage` is
allowed to go out of scope, causing the `Drop` impl to return the unused
physical frame to the allocator.

3. Applying this logic to both the anonymous mapping path and the
deferred file-backed path.
2025-12-28 23:42:19 -08:00