Commit graph

779 commits

Author SHA1 Message Date
bunnie
2119413b7d
Update kernel/src/syscall.rs
Co-authored-by: Sean Cross <sean@xobs.io>
2025-07-29 17:20:50 +08:00
bunnie
d58a0c763f
Update kernel/src/services.rs
Co-authored-by: Sean Cross <sean@xobs.io>
2025-07-29 17:20:35 +08:00
bunnie
a35f20b1c8
Update kernel/src/services.rs
Co-authored-by: Sean Cross <sean@xobs.io>
2025-07-29 17:19:26 +08:00
bunnie
f53502d9a2
Update kernel/src/services.rs
Co-authored-by: Sean Cross <sean@xobs.io>
2025-07-29 17:19:18 +08:00
bunnie
126896bf54 optimize kernel message passing times
Based on this benchmark of elapsed_ms():

```rust
    pub fn elapsed_ms(&self) -> u64 {
        let response = send_message(
            self.conn,
            xous::Message::new_blocking_scalar(api::Opcode::ElapsedMs.to_usize().unwrap(), 0, 0, 0, 0),
        )
        .expect("Ticktimer: failure to send message to Ticktimer");
        if let xous::Result::Scalar2(upper, lower) = response {
            upper as u64 | ((lower as u64) << 32)
        } else {
            panic!("Ticktimer elapsed_ms(): unexpected return value.");
        }
    }
```

We find the following context switches:
    1. send_message -> kernel/src/syscall.rs @ 166 - ss.remember_server_message()
        - *context switch round trip* to enqueue the message into the target thread's space, then switch back.
        - takes 546ns just in the target context, out of 4430ns round-trip
          (measured as leave userspace of caller to enter userspace of callee) (12% of total time)
        - emplaces the message in the target queue
        - generates "sender_idx" which is the index in the message queue to find the message, which is put into Envelope
    2. send_message -> kernel/src/syscall.rs @ 222 - ss.activate_process_thread()
        - activates the thread to run
        - Total penalty is closer to 1us - 22% of total time - once pre/amble/post-amble overhead considered

This set of patches removes the context-switch round trip to emplace messages in the target's thread
space, as well as to set thread results, by passing them as arguments to `activate_process_thread`.

What then happens is that these items are emplaced after the activation privot to the target
process space occurs, saving a set of TLB-flushes and page table walks.

More testing is required, but at least on a reasonable set of cases the system seems to be stable
with these patcehs, and we get a 24% speedup on blocking scalar messages and 8% speedup on
mutable lends in the case that the target is waiting to receive messages.
2025-07-29 01:24:38 +08:00
bunnie
25f7bd6f52 remove some redundant mmu flush commands
The double-flush in activate() has a big impact on performance,
and I'm pretty sure it only needs to happen after the SATP update
not before, at least architecturally. Eliminating the extra
flush improves the fast message passing benchmark time by 9%.
2025-07-26 00:46:15 +08:00
bunnie
9c7ed885f9 add vexii-test entries to the cramium target 2025-07-24 00:29:21 +08:00
bunnie
89d80f6288 make the kernel irq extension generic to verilator target 2025-07-24 00:28:10 +08:00
bunnie
1effeab858 add legacy_int handler to the kernel, handle vexii init case
The "main" product line is affected here in that the UART
init is hosted above the trng init. This should have no impact
on security or boot time, but allows us to more easily debug
TRNG init issues. This is necessary because in the vexii test
version the XADC interface is broken by the slower clocking
and I'm not taking the time to trace down that issue as it
does nothing to further the goal of testing the vex-ii core
in a non-production config.

legacy_int handler allows vex-ii to emulate/behave like the vex-i
in terms of interrupt handling.

eventually this will be replaced with/augmeted by a PLIC.
2025-07-21 17:11:23 +08:00
bunnie
52abe83b74 handle vex-ii a/d flags and error gracefully on page re-allocates
1. Instead of panicing when a page is re-requested, return an
error message to the caller. This allows us to have the UART
pre-allocated by the loader and the kernel to request it prophylactically

2. add A/D flags for R/W pages in vexii-testing. This is a work-around
until we have proper A/D tracking in the page lifecycle mechanism.
2025-07-21 17:08:56 +08:00
bunnie
de8ddbf9c2 handle case where uart is re-allocated by the kernel
this case happens when the loader is instructed to pre-allocate
the kernel UART page based on the early-printk feature.
2025-07-21 17:07:28 +08:00
bunnie
4fb3a3da43 add vexii-test shims for irq subsystem in kernel 2025-07-20 19:03:29 +08:00
bunnie
04c7933c87 handle case of write pages being swapped out upon write release 2025-05-21 18:02:21 +08:00
bunnie
19455d1bc0 add block erase routine 2025-05-21 10:20:04 +08:00
bunnie
23c2b77ace fix hosted build 2025-05-18 09:24:22 +08:00
bunnie
b1a8b6fdae cleanup some debug 2025-05-18 04:33:49 +08:00
bunnie
289028fa48 deprecate sync/dirty and emplace the writepage syscall 2025-05-18 04:25:18 +08:00
bunnie
dfd82c94b8 refactor write page syscall for explicitly managed writing
this is a much simpler, race condition-free version of flash
writing which has the penalty of requiring a copy of the page
to be made by the caller before the write. However the
copy is mostly already paid for because on the initial access
to the page, we had to copy the flash into RAM.
2025-05-18 04:23:53 +08:00
bunnie
788efb3056 cleanup dirty-bit attempt to track pages 2025-05-18 04:23:20 +08:00
bunnie
1a5a0a1388 attempt to fix race conditions is dirty/flush loop in swapper
this ultimately failed, we're going to try a different approach
2025-05-17 16:27:17 +08:00
bunnie
979ce7a589 add missing args to flush syscall 2025-05-17 16:26:51 +08:00
bunnie
ca61545ad4 implement syscall wrappers for marking dirty and sync of flash 2025-05-16 19:13:20 +08:00
bunnie
b86612f614 implement WriteToFlash on the kernel side 2025-05-16 19:11:05 +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
3f0b833b90 add swapped flag to flag translation 2025-05-15 11:51:53 +08:00
bunnie
7af009be78 migrations for moving constants out of xous-rs/kernel and into board/platform
this is a fairly major re-org of where key constants go that
define platform features. things that were in xous-rs that were
platform specific (namely precursor) were moved into a boards file,
and things that are generic to the system (like PAGE_SIZE) are moved
to a public location in xous-rs::arch.
2025-05-11 12:50:02 +08:00
bunnie
63e4319314 add partial support for memory mapped flash (alloc virt without phys backing) 2025-05-11 12:49:23 +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
4f14a67504 formatting changes due to changes in nightly rust
needed to get a clean check on gh
2025-02-01 22:26:40 +08:00
bunnie
c8cb5cda47 ignore dead code in shell.rs
when the debug stuff is commented out, a bunch of unused code
warnings happen in the shell interface
2025-01-01 22:48:52 +08:00
bunnie
03d67de1f6 kernel in -fpga variant uses duart 2025-01-01 22:48:32 +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
b59177176b raw pointer dance for the SWITCHTO_CALLER 2024-12-02 19:06:25 +08:00
bunnie
8876f6e7f5 raw pointer dance for IRQ handlers 2024-12-02 19:05:56 +08:00
bunnie
9662f13d31 raw pointer dance for kernel TRNG 2024-12-02 19:05:24 +08:00
bunnie
64612352d3 raw pointer dance for debug UART 2024-12-02 19:04:37 +08:00
bunnie
95611d9598 This begins a series of commits to cleanup a static_mut warning
So in Rust 1.83, we now get a strongly worded warning telling
us to really, really don't use static muts. They aren't wrong,
except that in some places like the kernel we have few options:
we are conjuring pointers out of thin air, creating memory mappings,
and then doing things with them that are hard for the compiler
to reason about.

I think the newly minted warning was made because the intention
is that we could use the new fancy `const` semantics to take
the place of static mut usage by wrapping things in UnsafeCell,
and I think maaaaybe in some
cases this could apply here, but as a simple example a dive
into the kernel debug UART indicates that to initialize the
shared variable we have to allocate pages in the page table,
which involves calling the memory manager and doing some
decidedly non-const type of things. So one option is we could
just push the debug UART initialization into the loader and
paper over the whole thing by pretending it's static.

Instead of that, we adopt a pattern of casting all our static
muts to a raw pointer, and then back into what we want. The
cast to raw pointer is like an "uber unsafe" marker that says
"no we really mean that this pointer is where it is and we
conjured it out of thin air and we promise that the behavior
is defined and values are representable, etc.".

In general we're fairly judicious about the use of static
mut in the kernel, and when in the kernel there's no thread
pre-emption (although we could take an interrupt or page
fault) so generally we are single-threaded. The trickiest
spots are when we have to take a page fault while handling
a page fault (for example, allocating a new L1 page table
entry would require something like this) but this code path
is fairly carefully vetted.

So instead of just doing a global lint silence, we use an
`&raw` idiom to call out the fact that we're doing something
naughty wherever we do it, which makes the compiler "happy"
(for now) while still making the code ugly enough that it
looks sufficiently scary and you have to apply some
cognitive burden to even reason about what's going on,
which I *think* is the intention of Rust in this case.

Basically you get a get out of jail free card but you can't
simply wear it on a lanyard and pass through the gate -- every
time we need to use it, we have to do a little dance and
wash ourselves in the waters of unsafety and recite the
mantras about defined behavior, representable numbers, thread
safety, etc.
2024-12-02 18:55:18 +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
820acfa2e6 make sure that swapper pages are marked as wired once allocated
also improve debug output, it was a little nonsensical previously
(some bit rot on meaning of fields)
2024-11-07 16:10:27 +08:00
bunnie
a854ad53ea fix broken code in deep debug option 2024-11-07 16:10:07 +08:00
bunnie
b3de92aabc add option to dump kernel pages in panic dumps 2024-11-07 16:09:49 +08:00
bunnie
735c4bfedd add debug options for sussing out IRQ problems in the kernel 2024-11-07 16:05:47 +08:00
bunnie
bb7209508b update formatting on the published crates
not doing a reformat of the entire project to match
nightly formatting changes, but capturing the published
crates so we can maybe save a version bump later on to get
the project clean.
2024-09-23 01:43:02 +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
6ddc8bc79a
Merge branch 'main' into nto-work 2024-06-27 00:41:07 +08:00
bunnie
a9b0ba2666 improve documentation on how the CSPRNG is reseeded from the TRNG. 2024-06-22 21:18: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