Commit graph

890 commits

Author SHA1 Message Date
bunnie
44890d72ac add test and debug deps/features 2024-05-18 00:22:23 +08:00
bunnie
d2d982b9f2
Merge branch 'main' into swap-opt 2024-05-16 14:56:02 +05:30
bunnie
e94e447e93 handle IRQ swaps & lighter-fingered about flag sets
previously was setting all pages as USER even if they shouldn't
be. Trust the flags are set correctly, just mark the valid bit.

Also, detect swaps in an IRQ handler and manage accordingly.
In particular, don't re-disable IRQs (as it overwrites the
saved IRQ mask), don't re-enable IRQs on exit, and don't
modify process Run states.
2024-05-16 03:55:41 +08:00
bunnie
19128a83a3 add ability to "touch" an alloc so that the LRU count updates
also add some debug access functions
2024-05-16 03:54:51 +08:00
bunnie
3112f41d96 fix bug in debug 2024-05-16 03:54:35 +08:00
bunnie
c06612e43e lock down some key pages
- no swapping of kernel pages
- mark L1 PT's properly in the memory allocation table
  (was accidentally marking them with the VA of the first address
   allocated)
2024-05-16 03:53:07 +08:00
bunnie
82da94b3da add code to try to handle the case of swap in IRQ
the cases are delineated but still getting a fault on return
from the handler.

also optimize out a memory space swap that was incurred due to
inappropriate abstraction boundaries.
2024-05-15 04:26:08 +08:00
bunnie
5f127b4548 create swap resume functions; differentiate irq context calls
- invoking the swapper context is different in an IRQ because
we don't want to modify the runnability states of the original
process

- didn't want to make a custom version of swap resume but
with the IRQ issue it made sense. In the process of doing this,
we can also optimize out a memory space transition, so it's
probably worth it even if we're not entirely going down the
right path to fix the bug of interest.
2024-05-15 04:24:19 +08:00
bunnie
900d829bf4 a lot of bug fixes in the core kernel swapper
probably should have done this in smaller commit chunks but it
was hard to say if this was going in the right direction until now.

at the moment, system can boot and evict quite a bit of memory before
hitting a panic. The current state is that we panic on returning
from an interrupt handler that hit a swapped page.

This is a pretty decent point to check point everything. hopefully
this next arc will be a bit cleaner.
2024-05-15 02:44:11 +08:00
bunnie
0fbedcebbf add the skeleton for the kernel OOM handler 2024-05-15 02:43:06 +08:00
bunnie
2b05626b69 add context switching debug option 2024-05-15 02:42:29 +08:00
bunnie
1b0fc181a5 ensure process state is correct on errors; remove excess mmu flushes 2024-05-15 02:41:15 +08:00
bunnie
b86ee4b036 move irq-renable as late as possible; add helper function to access irq state 2024-05-15 02:40:31 +08:00
bunnie
27d7fb414c add debug on evictpage 2024-05-15 02:39:58 +08:00
bunnie
ad9ef3dedc cleanup comments and fix ordering of events 2024-05-13 21:05:46 +08:00
bunnie
8824372101 make debug a little more compact for the renode swap target 2024-05-13 21:02:32 +08:00
bunnie
327205edb5 release page relies on a physical address, not a virtual address. 2024-05-13 21:00:17 +08:00
bunnie
235806025c clarify api names; add evict target pages; more error checking on eviction 2024-05-13 19:20:59 +08:00
bunnie
0847f99433 swapper abi fixes
- evict error checking - reject requests to swap kernel or swapper
- add target amount of pages to evict on oom handler
2024-05-13 19:19:50 +08:00
bunnie
0fca04f14e swapper needs write privs to the lent page
encryption is in-place to save on memory
2024-05-13 19:18:49 +08:00
bunnie
74f75e1ef5 re-org swap code for readability, add oom_doom routine
some thought needs to go into when and how we call oom_doom
but let's test more core functionality of the swapper before we
worry about that. I can think of several places where we could
use that call, the main tricky bit is that oom_doom diverges
and so wherever we patch it has to be able to resume. If we put
it into e.g. somewhere in the main memory alloc loop, the trouble
is we would have to track all of the multiple places that we
could have arrived there...ideally we can find a single spot
to patch this, with a clean entry and exit path.
2024-05-13 17:24:35 +08:00
bunnie
a07ab0e121 update comments and add license headers 2024-05-13 14:21:53 +08:00
bunnie
dcef549099 move SwapAlloc to the swap module 2024-05-13 14:15:38 +08:00
bunnie
9b89bde46a refactor alloc API
instead of sending scalar messages on every page alloc/free, which
is a lot of traffic, introduce an API where the swapper can request
a snapshot of the alloc status by mapping the kernel's alloc
table as a read-only structure into the swapper's memory space,
and then invoking the swapper in its interrupt context to read that out.

The structure is unmapped on return from the context.

additionally, a running tally of free space is now maintained
and this can be queried with a fast syscall.

Thus, the normal workflow is the swapper would have a timer that
periodically queries the kernel's free space level, and when it
falls below a critical amount, it will invoke the expensive snapshot.

this can also be extended so that the kernel can call into
the swapper's interrupt context with a "help, OOM imminent"
message down the road.
2024-05-13 13:59:17 +08:00
bunnie
84d42460eb correspending kernel changes for run time page tracker split
memory alloc table split into RAM region and everything else ("extra")
2024-05-12 20:23:29 +08:00
bunnie
a3e55834e9 checking in the corresponding kernel changes to the RPT type alias 2024-05-12 16:28:14 +08:00
bunnie
90108f266e allow this trait to be unused
it's not used in all configs but it's not a bad thing to have
it around.
2024-05-10 00:38:41 +08:00
bunnie
1f12a97b64 cleanup debug 2024-05-08 07:22:43 +08:00
bunnie
1112ef5df6 remove unused function (forgot this arg was already available) 2024-05-08 07:22:21 +08:00
bunnie
a58a8dc438 cleanup some debug 2024-05-08 07:21:06 +08:00
bunnie
ee0b1a042c optimize kernel-side alloc advisories; cleanup some APIs 2024-05-08 07:20:33 +08:00
bunnie
4f471544bd add nesting depth = 2 for swapper ops
I think that structurally we might never exceed a nesting depth
of 2, but there are some specific cases where an alloc can call
an alloc in the kernel (e.g., having to create a new superpage
in the midst of a routine alloc), therefore, we have to handle
some nesting.

There's also chance that maybe nesting could be due to race
conditions or self-circular behavior in the swapper, but ideally
we avoid that entirely because we can't handle non-structural,
unbounded nesting of re-entry to the swapper routine.
2024-05-08 00:57:10 +08:00
bunnie
43c452bf97 add a call to get the free memory status from the kernel
this will be helpful for debugging our page tracker
2024-05-07 04:12:46 +08:00
bunnie
9e66c1bee2 commit the version bump 2024-05-07 01:03:05 +08:00
bunnie
b1cc676345 absorb ABI changes 2024-05-07 00:56:35 +08:00
bunnie
77cfc34f59 make panic outputs in hex
everything else is in hex, why should these be in decimal...
2024-05-06 18:47:11 +08:00
bunnie
f635917658 fix typo 2024-05-06 02:06:15 +08:00
bunnie
4db82e01d8 retrieve tid as well as pid when invoking swapper 2024-05-04 02:01:56 +08:00
bunnie
feefb57791 cleanup some debug output 2024-05-04 02:01:38 +08:00
bunnie
09b2b29074 remember tid for swap-read; do PID dance for resume abstraction
We want to use the finish_callback_and_resume() function because
it's well-tested and sets up everything just right, but it expects
that we're coming from another process. Unfortunately, in the
middle of all of this, we need to map a page back into the
destination process' memory space, so we end up starting from
the swapper's memory space, changing into the destination process'
memory space, then mapping the page, then changing back into
the swapper's memory space, then using the finish_callback_and_resume()
call.

I think the impact might not be *that* bad, mainly it's a TLB
miss and the PTW isn't super bad, and this comes after a disastrously
expensive decrypt-from-RAM operation -- so probably not worth it
to angonize over optimizing this bit at this point, and stick
with well-tested primitives.
2024-05-04 01:54:23 +08:00
bunnie
dc0244ca35 cleanup init stuff now that args are mapped into memory 2024-05-02 22:08:48 +08:00
bunnie
61128cb25f remove swap init call, as it is no longer needed 2024-05-02 22:08:08 +08:00
bunnie
39a427e1b0 improve debug output 2024-05-01 14:27:31 +08:00
bunnie
5f425ebc56 expand flags scope and fix swap test 2024-05-01 14:27:06 +08:00
bunnie
88a9854e54 add swap flags to debug output 2024-04-30 18:35:54 +08:00
bunnie
863f17652d broaded scope of conditional compilation 2024-04-28 02:57:04 +08:00
bunnie
ecf2396d0c put debug prints behind debug flags 2024-04-28 02:41:29 +08:00
bunnie
73c834d067 start debugging of swap in kernel
switching contexts to bringing up hardware.
2024-04-22 10:58:37 +08:00
bunnie
a732eed679 make kernel aware of the IniS type
this should now introduce a crash in the kernel when an IniS
process is in the image set; previously we didn't try to
schedule and run them.
2024-04-06 07:47:18 -07:00
bunnie
cc75520bc2 fix missing commands in non-swap path 2024-04-01 18:11:35 +08:00
bunnie
d9a27ea3d7 remove unnecessary print 2024-04-01 18:11:22 +08:00
bunnie
7d38d1761e add missing config flags 2024-04-01 15:58:16 +08:00
bunnie
b3f695dbc2 fix missing config flag 2024-04-01 15:57:50 +08:00
bunnie
d6b33159a5 stage version pin bump 2024-04-01 15:46:26 +08:00
bunnie
95a10e93ca cleanup warning when swap is not used / hosted mode 2024-04-01 15:38:51 +08:00
bunnie
a37b2c06e1
Merge pull request #512 from betrusted-io/swap
WIP: Encrypted Swap
2024-04-01 00:23:43 -07:00
bunnie
376eff8790 cleanup warning that has crept up in hosted mode 2024-04-01 14:28:02 +08:00
bunnie
ec439a38d4 handle both cases in the irq return handler
differentiate between the case that we got here via
a page fault, or a syscall, and handle appropriately
2024-04-01 14:08:15 +08:00
bunnie
2f23225b9d update API for advise alloc 2024-04-01 14:07:43 +08:00
bunnie
fa23a6f829 remove swap-specific patches now that we're patched at the syscall entry point 2024-04-01 14:07:14 +08:00
bunnie
5645a645e5 add handler for the syscall return case and unmap loop
There's two ways we can enter the swapper:

1. via an interrupt due to a page fault
2. via a syscall that generated a memory event (e.g. unmap)

This patch handles case (2) better and clarifies variable names
around that to make the code read better.

It also has logic to handle divergent looping through a large
unmap request.
2024-04-01 14:05:26 +08:00
bunnie
46525eff04 add separate handling for unmap syscall
The unmap syscall is a little bit tricky, because it can de-allocate
several pages at once, which can trigger an advisory de-allocate
message to the swapper in the middle of the loop. The entire structure
has to be re-written to handle this case, so we patch this right at
the top level syscall point and handle it entirely with custom
logic in the swapper.
2024-04-01 14:03:43 +08:00
bunnie
30bdecc2a0 fix return type of register swap syscall 2024-03-31 17:48:32 +08:00
bunnie
0e756003a1 debugging around the swapper protected context calls 2024-03-31 17:25:47 +08:00
bunnie
504253a033 more tid recording 2024-03-31 17:25:05 +08:00
bunnie
03388b1413 add tid reporting 2024-03-31 17:23:54 +08:00
bunnie
ecbce84385 add some debug & add state visibility
@xobs made the `state` element of a process publicly read-only visible
through a function call. I don't think this is a security issue but
please lmk if that's a problem.
2024-03-31 17:21:54 +08:00
bunnie
1af43fba7b track current tid in swap manager 2024-03-31 17:20:50 +08:00
bunnie
cc49867403 fix warnings from rust 1.77.0 upgrade 2024-03-29 09:45:49 +08:00
bunnie
8b7912fb34 add some targeted debug; handle advisories before swapper registered
for now, we just count how mayn pages we missed an advisory on,
those all effectively are "wired". If we're fast enough on registering,
it turns out we actually miss nothing.
2024-03-23 11:59:31 +08:00
bunnie
233bb63878 add debug-swap targeted debug config 2024-03-23 11:59:31 +08:00
bunnie
803ce1a847 move allocadvice to new home 2024-03-23 11:59:31 +08:00
bunnie
af856124a0 pass kernel dependency to xous-rs 2024-03-23 11:59:31 +08:00
bunnie
025e39913b implement retrieval from swap and allocation advisories 2024-03-23 11:59:31 +08:00
bunnie
4eefbd88bc cleanup warning that crept into the code 2024-03-23 11:59:31 +08:00
bunnie
168e3454f6 handle free advisories 2024-03-23 11:59:31 +08:00
bunnie
28b415cb27 handle swapped pages and allocation advisories 2024-03-23 11:59:31 +08:00
bunnie
14364e09c6 ensure compliance with unsafe conditions on exit_blocking_call() 2024-03-23 11:59:31 +08:00
bunnie
167cd04f06 remove arg page list, replace with set of 8 usize
this makes calls more efficient and avoids the double-shared page
2024-03-23 11:59:31 +08:00
bunnie
d6cfb13e4f upgrade irq path to handle the new API 2024-03-23 11:59:31 +08:00
bunnie
5234127b7b support multiple irq-like contexts with an enum 2024-03-23 11:59:31 +08:00
bunnie
70639214e1 specify the swap return address for swaps 2024-03-23 11:59:31 +08:00
bunnie
500a641848 specify the irq return address for irqs 2024-03-23 11:59:31 +08:00
bunnie
474e8f0822 make return address of callback configurable 2024-03-23 11:59:31 +08:00
bunnie
f2e44d364a implement blocking callback
a roughed-out nonblocking callback version also staged
2024-03-23 11:59:31 +08:00
bunnie
9008452a36 add arguments to register syscall for handler and state 2024-03-23 11:59:31 +08:00
bunnie
6806403da8 make the release page mechanism visible externally for swap config 2024-03-23 11:59:31 +08:00
bunnie
47a16b6ce4 add RETURN_FROM_ISR magic address
this is picked to not conflict with other addresses used by
the ARM implementation. I think it should be OK?
2024-03-23 11:59:31 +08:00
bunnie
26047bf956 add the routine to evict a page 2024-03-23 11:59:31 +08:00
bunnie
6f18a0ed21 add trampoline to return from swapper 2024-03-23 11:59:31 +08:00
bunnie
daf4d9a93e make valid bit mapping symbolic instead of hard coded constant 2024-03-23 11:59:31 +08:00
bunnie
bc91055307 add template for EvictPage
actual function is still a todo
2024-03-23 11:59:31 +08:00
bunnie
b0665c3412 implement RegisterSwapper syscall 2024-03-23 11:59:31 +08:00
bunnie
9fb1acb705 swap module, with some initial functionality for registration 2024-03-23 11:59:31 +08:00
bunnie
4f15af3fee add swap module to the kernel, and initialize it from args 2024-03-23 11:59:31 +08:00
bunnie
a1cffed9cd un-use the P bit in the page table
prep for re-allocating it to swap
2024-03-23 11:59:31 +08:00
Sean Cross
b4d6884cc4 kernel: use addr_of!() and addr_of_mut!()
DUe to https://github.com/rust-lang/rust/issues/114447 direct access to
static mut variables causes a warning which will be turned into an
error in the 2024 edition.

The workaround is to use `addr_of!()` and `addr_of_mut!()` macros and
then dereference that.

Signed-off-by: Sean Cross <sean@xobs.io>
2024-03-22 15:53:11 +08:00
bunnie
717abf4af8 work around dependency conflict on atsma5d27
the dependency is pinned to an old version of utralib that
causes a build conflict...
2024-02-08 00:33:14 +08:00
bunnie
699004e7b0 bump xous pins 2024-02-06 14:49:10 +08:00
Sean Cross
bf61ce5071 kernel: remove dead code
This code is commented-out and dead. It gets adjusted with `cargo fmt`,
so just remove it.

Signed-off-by: Sean Cross <sean@xobs.io>
2024-02-06 10:33:33 +08:00