Commit graph

622 commits

Author SHA1 Message Date
Sean Cross
dc097e32d8 kernel: fix atomic operations
Atomic operations were previously broken. RISC-V breaks atomic
operations into multiple instructions, and there is some state kept
between those instructions. This state is not visible, and is internal
to the processor.

We were ignoring this state, which resulted in inconsistencies
throughout the system. The following sequence was possible:

1. A thread loads a value, and obtains the `reservation set`
2. A context switch happens on the following instruction
3. A different thread loads the value, modifies it, and continues
4. A third thread (or a different process) loads a value and obtains the
   `reservation set`
5. The original thread runs, sees the `reservation set` is held, and
   updates the value, clobbering what the second thread did in #3

A workaround for this is to perform a dummy `sc` as part of the context
switch. This invalidates any atomic operations that may be pending.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-02-02 23:10:12 +08:00
bunnie
8b7876360f bump kernel package version 2023-02-01 22:45:37 +08:00
Sean Cross
043365ac82 kernel: re-add set_last_thread
git somehow dropped this function during the merge, which would have
made things very sad.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-02-01 20:45:43 +08:00
Sean Cross
c7f5ad25b1 kernel: syscall: fix restoring thread id
The previous patch didn't properly restore thread IDs when switching
contexts -- there were spots where we missed things, and spots where we
were double-borrowing.

Fix these corner cases.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-02-01 20:43:45 +08:00
bunnie
680ddd1c58
Revert "Give servers their full quantum" 2023-02-01 14:08:16 +08:00
bunnie
119296ed5b
Merge pull request #314 from betrusted-io/messages-late-return
Give servers their full quantum
2023-02-01 13:46:01 +08:00
Sean Cross
63e6deae53 kernel: save current thread at context switch
Note the current thread in the original process when its quantum
expires. This will allow us to clean up after ourselves when a context
switch occurs, and will help to prevent thread starvation.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-02-01 10:46:25 +08:00
Sean Cross
4ec43363f5 kernel: don't always context-switch on message return
Currently, returning a Message (e.g. a Scalar1, Scalar2, or
MemoryMessage) always results in a context switch from the server to the
client. This is what makes these syscalls cheaper.

However, this results in process starvation. For example, if three
processes send a message to a server, then the server will only be able
to respond to one of those messages at a time. This works well with a
lightly-loaded system, but quickly falls apart when many threads are
running.

Keep track of which process and thread were originally switched to as
part of the `SwitchTo` call. If the Server is responding to a message
and owns the current quantum, don't immediately switch to the Client
thread.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-02-01 10:19:07 +08:00
bunnie
cc97f157e4 bump kernel rev for publication 2023-01-31 16:10:53 +08:00
Sean Cross
1303e6416d kernel: fixup current_thread==0 on resume
For some reason, the current thread becomes 0 during suspend or resume.
This causes an error during boot.

Patch this up while we figure out what's causing it.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-31 16:01:35 +08:00
Sean Cross
abb1455fe6 fix previous commit
Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-31 15:51:31 +08:00
bunnie
d5b5a83445
Merge pull request #311 from betrusted-io/unify-kernel-scalar-return
Unify kernel scalar return
2023-01-31 15:48:59 +08:00
bunnie
9256e7cc09 bump versions to absorb xous-names fix 2023-01-31 15:07:32 +08:00
Sean Cross
778e217de8 kernel: unify scalar return calls
Unify scalar return calls so that we only have one return path rather
than three.

We can almost unify memory returning as well, but it's not quite there.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-31 14:46:38 +08:00
Sean Cross
fc8d78c2ec kernel: fix hosted mode
The process scheduler changed in subtle ways taht completely broke
hosted mode.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-31 11:24:09 +08:00
bunnie
84368c3f3a bump versions for publication 2023-01-31 02:18:12 +08:00
Sean Cross
8875d9b4b4 kernel: services: fix corner cases on thread selection
There were a few corner cases when it came to thread selection. Namely,
the same thread had a higher chance of repeatedly being executed.

Clean up these corner cases, and as a result get libstd tests to pass.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:01 +08:00
Sean Cross
af3f89d245 kernel: syscall: add notes for handing mem to user
Add notes on how to properly hand memory to userspace. This does not yet
work, but is committed here as a work-in-progress.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:01 +08: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
Sean Cross
a75d030d77 kernel: main: simplify process search
The process search now uses iterators properly. It's still not ideal
because it needs to investigate every process, but it's better than it
was.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:01 +08:00
Sean Cross
7f7142d948 kernel: riscv: print processes from tid #0
Start numbering threads from index 0. This matches up with what the rest
of the system uses.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:01 +08:00
Sean Cross
8c82736524 kernel: syscall: short-circuit return_scalar irq
When in an IRQ, short-circuit the return logic. This should make
interrupt returns slightly faster.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:01 +08:00
Sean Cross
502223ec9b kernel: syscall: rename context to tid
The word `context` is an older word. We now use "thread" or "tid".

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:01 +08:00
Sean Cross
2cb44cdfc2 kernel: services: optimize find_next_thread()
Use a multiply-based algorithm to count the number of leading zeroes.
This results in a function that's just 20 instructions long.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:01 +08:00
Sean Cross
a75bc4f54c kernel: minor formatting cleanups
These files have had rustfmt run on them.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:00 +08:00
Sean Cross
516876a30f kernel: use common function for finding next thread
THe act of finding a runnable thread is so common, yet the kernel
contains three ways of doing this.

Use a unified function to select the next available thread.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:00 +08:00
Sean Cross
69b5e924f3 kernel: properly set current_thread on init
Previously the `current_thread` was set to 0, which is invalid. Set the
`current_thread` so that it defaults to the initial thread ID.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:00 +08:00
Sean Cross
fcf74c4d9b kernel: limit memory regions to 4 MB
This memory region is much smaller than the previous value of 256 MB,
but it will limit the amount of memory used by superpages in
long-running processes.

Previously, we would march through memory as messages were passed back
and forth and a process' memory space would quickly be eaten up by 1024
pages.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:00 +08:00
Sean Cross
5dae8f7662 kernel: don't yield when JoinThread returns immediately
When waiting on a thread, it may be the case that the thread we're
joining has exited already. In this case, the correct approach is to
resume execution rather than switching to the parent process.

This fixes an issue where `SWITCHTO_CALLER` is None when joining a
short-lived thread.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:00 +08:00
Sean Cross
b18e7d7e99 kernel: immediately switch to new thread on CreateThread
When a new thread is created, immediately switch to the new thread.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:00 +08:00
Sean Cross
d25629a3ed kernel: return DoubleFree on invalid Return calls
When attempting to return an invalid item, return `DoubleFree`. This is
because such cases are most likely due to instances where a message was
responded to twice.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-30 22:15:00 +08:00
bunnie
232a4b6c6c bump xous version 2023-01-18 00:02:46 +08:00
bunnie
b275660a71 add an "endcap" to the kernel region
This works around https://github.com/SpinalHDL/VexRiscv/issues/296
2023-01-17 23:07:05 +08:00
bunnie
3417046e7b cleanup memset command a bit more
- we don't need a general memset call -- just a zero page call
- use native rust semantics to embody the call. some alignment checking is
done on the arguments on the way in and out that seem worth it
- the unrolled loop doesn't pay off because we're at the limit of
write bandwidth on the CPU at this point. we would need write-back
caches to gain more performance through unrolling.
2023-01-17 23:05:20 +08:00
bunnie
7be4a88a9b fix scheduler bug (credit: @xobs)
@xobs found a scheduler bug that would trigger a runnable process
search every time a scalar message returned. This fixes the problem,
causing the quantum to go back to the caller as intended.

Improves performance on the syscall by about 40%.
2023-01-17 02:11:48 +08:00
bunnie
aa48476937 Unroll loop for initializing memory in kernel
This also changes the initialization method from using `u8`
to `u32`.

The loop is unrolled to one iteration per cache line.
2023-01-17 02:10:46 +08:00
bunnie
bb5d7b3adf bump versions to absorb hosted mode fix 2023-01-05 14:07:24 +08:00
bunnie
64fa83cec4
Merge pull request #290 from betrusted-io/reply-and-receive-next-syscall
Fix ReplyAndReceiveNext for hosted mode
2023-01-05 13:56:24 +08:00
Sean Cross
3593d715c1 kernel: fix memorymessages with ReplyAndReceiveNext
Fix a segfault that occurred when using ReplyAndReceiveNext on hosted
mode. This now properly passes memory messages back to the client.

Signed-off-by: Sean Cross <sean@xobs.io>
2023-01-05 11:56:15 +08:00
bunnie
36398810dd bump version numbers to absorb new syscalls 2022-12-30 15:24:36 +08:00
Sean Cross
3218bde2b4 kernel: implement reply_and_receive_next
Implement `ReplyAndReceiveNext` as a syscall. This will reply to a
message, pull the next message out of the queue, and then re-queue the
server to run. This should make servers much more responsive.

This patch has been tested on both hosted and native modes.

Signed-off-by: Sean Cross <sean@xobs.io>
2022-12-30 11:07:04 +08:00
Sean Cross
ab0c7d3b32 kernel: server: check buf is some when returning mem
Signed-off-by: Sean Cross <sean@xobs.io>
2022-12-29 21:03:39 +08:00
Sean Cross
46f340324d kernel: rename Message -> MessageEnvelope
This syncs with the latest changes to `xous-rs`.

Signed-off-by: Sean Cross <sean@xobs.io>
2022-12-29 21:02:57 +08:00
Sean Cross
911ddb60fc kernel: debug: fix klog on baremetal
klog() refers to an outdated `.uart` struct, which has since been
renamed to `.serial`.

Signed-off-by: Sean Cross <sean@xobs.io>
2022-12-29 21:01:27 +08:00
bunnie
5370ce396d bump all revs due to SoC artifact update 2022-12-12 16:07:20 +08:00
bunnie
1d1b3a35b7 commit version bump 2022-11-26 10:00:44 +08:00
bunnie
aa9977cc58 bump all the things because we changed utralib again 2022-11-17 22:13:40 +08:00
bunnie
dabbfd6c35 bump all the things to absorb the SoC change 2022-11-17 19:39:18 +08:00
bunnie
30583bdf07 bump versions with xous change 2022-11-06 03:48:12 +08:00
Sean Cross
da5647539f kernel: syscall: add support for return_scalar5
Add kernel support for returning 5 scalars.

Signed-off-by: Sean Cross <sean@xobs.io>
2022-11-05 18:39:54 +08:00