Commit graph

520 commits

Author SHA1 Message Date
eupn
90df83f885
kernel: revamp IRQ handling 2023-04-05 16:35:01 +04:00
eupn
723a730231
kernel: fix warnings 2023-03-14 19:01:11 +04:00
eupn
e2d34d364c
kernel: always print when process is terminated 2023-03-14 18:48:45 +04:00
eupn
1634243f63
kernel: fix compile errors 2023-03-14 18:48:15 +04:00
eupn
7f5656445a
kernel: change some println!s to klog!s
Even when the print-debug feature isn't turned on, println! macro is still able to output some critical non-debug info to the console such as panics, process crashes and violations, etc. Debugging messages are printed with klog! macro that's disabled when debug-print feature is not set.
2023-03-14 18:47:57 +04:00
eupn
434e865a45
kernel: add sama5d2 uart workaround 2023-03-14 18:36:41 +04:00
eupn
0953812dfc
kernel: hide hw specific details in process init 2023-03-14 18:35:12 +04:00
eupn
3451d106f9
kernel: update debug and shell modules 2023-03-14 18:32:52 +04:00
eupn
38a02f9dbb
kernel: split precursor and sama5d2 platform modules 2023-03-14 18:30:07 +04:00
eupn
2ecd91b3c5
kernel: split arm and riscv arch modules 2023-03-14 18:28:49 +04:00
bunnie
73aa6c3b14 cleanup hosted mode warning 2023-02-15 20:35:32 +08:00
bunnie
08ae66a264 fix: #320
this commit adds the facilities to extract eh_frame[_header]
sections and pass it to each process.

A bump is required to `std` to extract the passed parameters.
2023-02-15 19:55:08 +08:00
bunnie
4a2234f539 XIP works with some processes, but not others
it works with e.g. shellchat, but not vault.

still scratching my head on what's gone wrong -- it's
crashing in the pre-boot, which means something is not
mapped right for some case that wasn't caught in the
earlier testing.
2023-02-11 01:42:04 +08:00
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
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
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
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
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
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
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
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
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
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
bunnie
6332bf1c90
Merge branch 'main' into driver-refactor 2022-11-03 01:53:23 +08:00
Jean-Pierre De Jesus DIAZ
d8807e6cea
kernel: Move rand driver to platform module.
Signed-off-by: Jean-Pierre De Jesus DIAZ <me@jeandudey.tech>
2022-11-02 14:36:41 +01:00