Commit graph

118 commits

Author SHA1 Message Date
bunnie
c23bed360f implement extraction of kernel analytics
this is gated behind the feature "debug-proc" because this could
leak attacker-useful information, and also it adds a primitive that
causes the kernel to write the debug information into any page
that the userspace already has access to. There are some checks
to ensure that the page is actually in RAM (so you can't use this
to trigger weird peripheral states, etc.) so hypothetically this
should just cause corruption in the userspace process if it is
abused, but still - more attack surface, more problems.

however, the feature is useful enough and the risk is small enough
that I think I'm going to leave it enabled for the foreseeable future.
It's definitely helpful to be able to see how much free memory
you have, and other resource allocation trackers like connection
limits and interrupt handler allocations.
2026-01-08 02:56:33 +08:00
bunnie
ecbac27e76 fix rustc-1.89 warnings 2025-08-19 19:45:29 +08:00
bunnie
23c2b77ace fix hosted build 2025-05-18 09:24:22 +08:00
bunnie
788efb3056 cleanup dirty-bit attempt to track pages 2025-05-18 04:23:20 +08:00
bunnie
75073aab99 add functions for marking pages as dirty in the RPT
this is what ultimately allows us to decide whether or not a
page can be flushed from memory safely
2025-05-16 19:09:53 +08:00
bunnie
b8c30c32ac fix bug where pages were not marked as belonging to USER
it's assumed the kernel would never want to access the flash
directly, so no mode is implemented to support this possibility.
2025-05-15 11:52:41 +08:00
bunnie
f5315dc014 incorporate partial support for virtual-only alloc swap into kernel
also has migrations for constant locations
2025-05-11 12:46:56 +08:00
bunnie
4f21bf3c3e raw pointer dance for memory allocations and extra allocations
this is probably the most significant source of consternation but
also this code path has been fairly well-trodden.

That being said it might be worth considering if there is any
way to use some of the new Rust const based reasoning logic
to see if we can't tighten this up someday, but today is not
that day.
2024-12-02 19:10:39 +08:00
bunnie
6961bb3e54 fix a bug in debug-print when swap is not specified
a field doesn't exist that is accesed by a debug-print resource
in the case of swap being inactivated
2024-11-30 02:21:20 +08:00
bunnie
a854ad53ea fix broken code in deep debug option 2024-11-07 16:10:07 +08:00
bunnie
89f92c28a2
Merge branch 'main' into nto-work 2024-08-19 02:30:35 +08:00
bunnie
6eab1c48bb patches to clean up warnings in rust-1.80 2024-08-19 02:24:24 +08:00
bunnie
3ae4fde572 fix comment 2024-06-21 18:20:10 +08:00
bunnie
3e8cfab6b0 fix hosted mode 2024-06-20 01:56:36 +08:00
bunnie
32893b9b4c fix build option 2024-06-19 22:04:42 +08:00
bunnie
c5aee47861 update comment to reflect current policy on OOM
I think it's more Xous-y to punt to userland in the case that
no consecutive memory is found, rather than trying to implement
an evictor in the kernel itself.

For now, the blunt hammer we use is to simply request some
blocks be freed and we retry; this works well enough to find
blocks of a few pages in sequence. If we needed larger blocks,
we may want to consider modifying the swapper userland algorithm
to have some API for finding consecutive runs of non-wired
memory to swap out.
2024-06-19 21:37:00 +08:00
bunnie
6ba57774cf add "device RAM" allocation mode to kernel
"Device RAM" is a block of RAM whose physical address is not
important, but the block must be consecutively allocated in
physical address space.

This is used to allocate blocks of memory that are intended
for hardware DMA targets and other peripheral shared-memory
applications.

Note that this also marks the allocated block as "wired", so
it can't be swapped out.
2024-06-19 21:34:42 +08:00
bunnie
ec9a2be9cc reduce warnings when debug is turned off 2024-05-28 20:27:13 +08:00
bunnie
bed9252614 suppress warning when debug is turned off 2024-05-28 16:54:18 +08:00
bunnie
62eb233db1 make swapper api generic for re-entrant kernel calls (alloc element) 2024-05-28 02:44:36 +08:00
bunnie
fa777b6198 cleanup the original alloc_page now that all are oomable 2024-05-26 23:00:20 +08:00
bunnie
081f2009e8 make alloc_page_oomable re-entrant
This version of the OOM handler uses a syscall in a syscall
(previously forbidden) to make the OOM case deep in an alloc
situation oomable, but also have multiple exit paths.

this saves us from having to recode oodles of exit paths which
is complicated and error-prone.

I also think we should trim back some of the OOM handling paths
because it is bloating the swapper, but let's commit the current
working state before we trim stuff.
2024-05-25 00:01:22 +08:00
bunnie
cf6c084696 quite down debug in kernel 2024-05-21 02:56:08 +08:00
bunnie
353f4d5f1b fix config flag 2024-05-21 01:46:02 +08:00
bunnie
853f9d3cd3 add hooks to hard-oom handler in the alloc_page_oomable routine
also add some features for debugginng.
2024-05-21 01:07:13 +08:00
bunnie
8d318d3ed9 add epoch rollover core handler
This is put in the `mem.rs` file because it has to have access
to its private allocation table.

The renorm strategy used does an in-place reduction of epoch
values down to the minimum set that can uniquely represent every
value within the array. The approach only uses stack allocated
temporary storage, and the amount of stack required is chosen
to fit within the one-page goal of the kernel's overhead.

This does mean that the algorithm doesn't run as fast as
it possibly could, in fact, it has to do multiple passes
on the allocation table due to the limited storage for normalized
values.

However, the allocation table itself is probably going to be
limited to hundreds to low-thousands of values, and the renorm
routine happens once in a blue moon, so I think this is
acceptable, and preferable to the YOLO punt of setting all
epoch stamps to 0 and re-discovering the LRU ordering, or
allocating a temporary storage structure equivalent to that
of the allocation table to make the thing run optimally fast.
2024-05-18 00:24:52 +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
0fbedcebbf add the skeleton for the kernel OOM handler 2024-05-15 02:43:06 +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
1112ef5df6 remove unused function (forgot this arg was already available) 2024-05-08 07:22:21 +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
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
fa23a6f829 remove swap-specific patches now that we're patched at the syscall entry point 2024-04-01 14:07:14 +08:00
bunnie
03388b1413 add tid reporting 2024-03-31 17:23:54 +08:00
bunnie
168e3454f6 handle free advisories 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
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
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
bunnie
f9fc3d0d4b ok, format the kernel now that all in-line assembly has been checked 2024-01-14 19:27:14 +08:00
bunnie
710c45969f Revert "format the kernel"
This reverts commit 1f69e6aa39.

this has in-line assembly, let's fix that before we format
2024-01-14 19:24:33 +08:00
bunnie
1f69e6aa39 format the kernel 2024-01-14 19:11:19 +08:00
Sean Cross
e44d009b84 kernel: mem: add function for verifying mappings
Add a function to ensure mappings are unique across the whole system.
This takes a very long time and can crash if the watchdog timer isn't
paused.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-29 18:42:16 +08:00
Sean Cross
006a50d206 kernel: mem: fix memory zeroing
Fix memory zeroing when requesting a physical pointer. Previously, only
the first 25% of a page was zeroed due to a bug when casting between
*mut u8 and *mut usize.

This now uses `write_volatile` to ensure the memory clear isn't
optimized out, and uses `usize` pointers everywhere.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-25 23:30:26 +08:00
Sean Cross
d798962151 kernel: format some files
Format some files prior to patching them.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-12-25 23:28:52 +08:00
eupn
434e865a45
kernel: add sama5d2 uart workaround 2023-03-14 18:36:41 +04:00
Sean Cross
1d6c1e34de kernel: mem: increase default mem size
The default mem size got shrunk down to 4 MB earlier, but there is a
lingering issue that prevents specifying virtual addresses. As a result,
pddb and others don't work.

Temporarily revert this change.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:01 +08:00