102 Commits

Author SHA1 Message Date
Travis Geiselbrecht
e4d65228b5 [mp] restructure the sequence of how cpus are brought up
- Move a bit of the shared logic of secondary bootstrapping into a new
  function, lk_secondary_cpu_entry_early() which sets the current cpu
  pointer before calling the first half of the secondary LK_INIT
  routines.
- Create the per cpu idle threads on the main cpu instead of the
  secondary as they come up.
- Tweak all of the SMP capable architectures to use this new path.
- Move the top level mp routines into a separate file top/mp.c
- A bit more correctly ifdef out more SMP code.
2025-10-12 19:47:33 -07:00
Travis Geiselbrecht
5016118509 [fpu] have the arch layers set WITH_NO_FPU based on the cpu
This will generally turn off more FPU codegen, even if its using
software fallback unless the project/target/platform selects a cpu that
has FPU support. This also turns off a few blocks of test code and the
upcoming floating point printf if it's not present on the arch.

This may break projects that were compiling for say cortex-m0 but
expected FPU code to be present. If so it should be pretty easy to
override it, but not going to add that yet unless it's necessary.
2025-10-08 23:50:53 -07:00
Travis Geiselbrecht
e0b5008641 [riscv][toolchain] fix clang test of -misa switch which is not present
The test inside riscv/rules.mk was assuming gcc and that the CC variable
isn't passed in from the user. This is not a very clean solution and
acts like a bandaid over the problem. Added some todos for a potential
solution.
2025-10-05 13:57:04 -07:00
Travis Geiselbrecht
ad6ef65a5a [arch][ops.h] force all of the fast routines to be inline
Also make sure each arch_ops.h always includes arch/ops.h at the top, to
make sure the declaration always appears in front of the definition.
2025-10-01 20:56:07 -07:00
Travis Geiselbrecht
e739abc490 [kernel] tweak a few thread apis to to take a const pointer
A bit of reformatting on some ARM code while was touching it.
2025-10-01 20:56:06 -07:00
Lei Wen
145e9a0d27 [arch] fix link script not include global array init
Using wild match init_array* to include global array init ctor

Before:

 10 .ctors             00000040 ffff0000001e6b80 00000000401e6b80 DATA
 11 .dtors             00000000 ffff0000001e6bc0 00000000401e6bc0 DATA
 12 .got               00000060 ffff0000001e6bc0 00000000401e6bc0 DATA
 13 .init_array.1      00000470 ffff0000001e6c20 00000000401e6c20
 14 .fini_array.1      00000470 ffff0000001e7090 00000000401e7090
 15 .dummy_post_data   00000000 ffff0000001e7500 00000000401e7500 DATA
 16 .bss               00009460 ffff0000001e8000 00000000401e8000 BSS

After:

 10 .ctors             000004b0 ffff0000001e6b80 00000000401e6b80 DATA
 11 .dtors             00000470 ffff0000001e7030 00000000401e7030
 12 .got               00000060 ffff0000001e74a0 00000000401e74a0 DATA
 13 .dummy_post_data   00000000 ffff0000001e7500 00000000401e7500 DATA
 14 .bss               00009460 ffff0000001e8000 00000000401e8000 BSS

Signed-off-by: Lei Wen <leiwen@asrmicro.com>
2025-09-29 22:40:41 -07:00
Travis Geiselbrecht
5a17519e54 [arch][riscv] fix rounding issue with PAGE_SIZE on riscv64
Inadvertently added this bug where PAGE_SIZE type is 32bit which causes
truncating when using it to round a kernel address.
2025-09-23 23:05:15 -07:00
Travis Geiselbrecht
cc9c3a053c [arch][mmu] clean up page size definitions in each arch's defines.h
No real functional change except how the smaller ARCH_DEFAULT_PAGE_SIZE
is now computed and set in defines.h instead of rules.mk for arch/arm to
be consistent with the other arch that has a large/small build (riscv).
2025-08-31 19:16:58 -07:00
Alex Richardson
d673d211eb riscv: add missing stdint.h include
While this is currently pulled in transitively, uint32_t is defined there
and we should explicitly include the appropriate header.
2025-01-07 22:28:07 -08:00
Alex Richardson
22ae6b6cea riscv: ensure arguments to __ASM_STR() are expanded first
Right now this does not cause any issues, but I had a local change where
missing expansion resulted in compilation errors when reading CSRs.
2025-01-07 22:28:07 -08:00
Alex Richardson
9241c78667 riscv: Add missing parens around macro argument
This triggered a out of range count overflow warning when
building for RV32 (calling the macro with `ticks >> 32`)
since the truncation happened before the shift.
2025-01-07 22:28:07 -08:00
Travis Geiselbrecht
6f32a0f377 [arch][riscv] use newly discovered pseudo-instructions for load/stores
I hadn't noticed this before, but you can directly reference a global
variable in a load/store in assembly, which combines a lla + ld/sd into
a 2 instruction pair instead of 3 due to the 12 bit offset provided in
the load/store.
2024-11-27 21:53:29 -08:00
Travis Geiselbrecht
2ca679aeca [arch][riscv][asm] use the call pseudoinstruction instead of jal
This fixes a problem if the text segment gets larger than ~1MB where the
raw jal instruction cannot reach. Using 'call' or 'tail' allows the
assembler to emit a 2 instruction sequence that the linker later
relaxes if it can.
2024-11-14 19:33:46 -08:00
Travis Geiselbrecht
69b8bccd76 [riscv] switch stimecmp/stimecmph registers to using the raw integer format
Though using the named nmemonics is a generally better idea it has the
unforunate property of not working on older compilers. In this case,
these new registers are for the Sstic extension, which is new enough
that even reasonably recent compilers as GCC 12.1 doesn't understand it.

Fixes issue #410
2024-06-16 22:37:10 -07:00
Travis Geiselbrecht
14bd7728a6 [arch][riscv][feature] add a few more feature bits
These may be useful in the future.
2024-06-02 15:31:30 -07:00
Travis Geiselbrecht
c4effaeef0 [arch][riscv] add SSTC extension support
Pretty simple extension, just directly set the supervisor timer compare
register (new) instead of calling through to SBI to set it for you.
2024-06-02 15:29:53 -07:00
Travis Geiselbrecht
b9c3603c59 [arch][riscv] fix typo matching against the zifencei feature 2024-06-02 14:51:53 -07:00
Travis Geiselbrecht
566b25d1ec [arch][riscv] read the riscv feature string out of device tree
Also added initial implementation of a way to query run time features of
the cpu.
2024-06-01 17:21:01 -07:00
Travis Geiselbrecht
86267ca23c [include][reg.h] define new mmio_read/write accessors
To work properly with some hypervisors on various architectures (ARM,
ARM64, x86), add global routines to allow access to MMIO registers via
architecturally defined accessors.

Add accessors for ARM, ARM64, and x86-32/64. Have the other arches
default to just using whatever the compiler emits.

Will need to generally move things off the legacy REG*() accessors
since they're really not safe going forward with what compilers emit.
2024-05-13 00:39:29 -07:00
Travis Geiselbrecht
356e9adc01 [make] remove an undefine, unsupported on older gnu makes
It wasn't really that important anyway, was just a general nicety in the
riscv rules.mk
2024-05-10 16:15:54 -07:00
Travis Geiselbrecht
284bf108bc [arch][riscv] add new extensions for SBI 2.0
No actual features enabled, just detection.
2024-04-23 23:27:02 -07:00
Travis Geiselbrecht
6ed6f36fa0 [warnings] remove some redundant declarations
Discovered with -Wredundant-decls
2024-04-19 00:07:49 -07:00
Travis Geiselbrecht
03eb343e52 [arch][riscv] add a way for platforms to set optional riscv ISA extensions
A pretty simple mechanism, a list of extensions added to
RISCV_EXTENSION_LIST make variable is expanded to an underscore
delimited string appended to the end of -march=

Pretty simple but it should work for now.
2024-04-07 23:10:31 -07:00
Travis Geiselbrecht
00b06a8302 [arch][riscv] change secondary cpu bootstrap api
Instead of setting a counter of the number of secondaries to start, have
platform or target code pass in a list of harts to start instead. This
allows for there to be discontinuties in the layout of the cpu harts, or
in the case of some sifive based hardware, hart 0 is otherwise offline.
2024-04-07 22:32:49 -07:00
Travis Geiselbrecht
f7121c7b7e [arch][riscv] general riscv spinlock cleanup
-Hard set the spinlock type to uint32_t to be clearer
-Switch the free/held value to a simple 0 or 1

Previously, was writing the current cpu number into the spinlock, which
was only useful for debugging purposes. However, since the atomic
operation was an amoswap instead of a proper CAS, it would end up
overwriting the old cpu number with the new cpu number when it tried to
take it. It would still work as a spinlock, since the value was !0, but
it was falsely tracking which cpu actually held it.

To keep it simple, just switch to 0/1 and stick with the amoswap
instruction, which is much more efficient than a LR/SC pair to implement
CAS on riscv.

Internally still use an unsigned long for the old value, since the
amoswap instruction overwrites the entire Rd register, and thus keeps
the codegen efficient since it wont try to sign extend it for any
comparisons afterwards.

Thanks @loqoman (darwin.s.clark@gmail.com) for catching this one.
2023-12-28 17:02:07 -08:00
Alex Richardson
496e2f4b8c [riscv][clang] Use a CSR name instead of a numeric expression
Clang's assembler rejects expressions containing e.g. (1u << N) in the
assembler. Instead using numeric expressions for per-privilege level
CSRs, we can prepend `m` or `s`. This also lets the compiler assign the
CSR encoding instead of having to hardcode it in the source code.
2023-06-08 07:08:49 -07:00
Travis Geiselbrecht
c66ad44efa WIP [target][visionfive2] Add initial support for a VisionFive 2 RISC-V board
Still TODO:
-Set the timer rate properly
-Fix nonzero based hart secondary cpu boot
-Try to parse the device tree for some information
2023-03-06 01:06:27 -08:00
Travis Geiselbrecht
81d6014493 [arch][riscv] Add more defines and optimize fpu context switch
Add a few more #defines for the status register
Tweak the FPU context switch to not rewrite the entire status register,
      which may not be a generally safe thing to do.
2023-02-18 16:39:45 -08:00
Travis Geiselbrecht
3dd8a32e92 [arch][riscv] a few small tweaks
Updated comment regarding clobbers on the SBI call.
Tweak a boot time print of SBI version.
Add a commented out line to the do-qemuriscv to assist in dumping future
device trees.
2023-02-18 14:42:59 -08:00
Travis Geiselbrecht
7017a8fff9 [arch][mmu] add another routine to query if the arch supports user aspaces
Trim the arch mmu unit tests accordingly.

Should probably switch this to a #define, but it's possible some of
these queries could be dynamically detected (XN for example). May
revisit at some point.
2022-10-23 22:40:00 -07:00
Travis Geiselbrecht
d5451cc8e6 [arch][tests] add a way to query some arch mmu features
Allow asking the arch layer if it supports NX pages or NS pages.
Have the arch mmu test code test accordingly.
Also tweak the tests to pass on arm32 mmu, which does not precisely
match the return semantics of the rest of the mmu routines on map/unmap.
2022-10-21 00:00:49 -07:00
Travis Geiselbrecht
cd96c43006 [make] define and undefine some make variables
Based on building with --warn-undefined-variables, find a few places in
the build system where undefined variables were used incorrectly, or
never set due to unused code.
2022-07-22 23:45:42 -07:00
Travis Geiselbrecht
8372f46055 [arch][riscv64] fix build with older toolchains (gcc 7.5.0)
When building fpu variant, use -march=rv64imafdc instead of rc64gc
since some older compilers and/or mainline do not understand the
equivalence when selecting libgcc.

This is only an issue on rv64 due to the need to pick out the medany
variant of libgcc.
2022-07-22 23:08:44 -07:00
Travis Geiselbrecht
b0d8aeed18 [arch][riscv] add fpu context switch support
Currently only implemented for double precision floating point.

Caveat: currently unable to only compile some code with or without
float. The linker is extremely picky about mixing float and no-float
objects, so stick with all on or off for now.

It's not as much of a problem currently because the toolchain is not
using any riscv vector instructions to assist normal code, so it's
generally only emitting fpu instructions for floating point code.
2022-07-17 23:27:42 -07:00
Travis Geiselbrecht
6462cbf51c [arch][fpu] add ability to specify per file or module if code needs fpu
Have the arch define additional compiler flags to explicit support or
not support a floating point unit.

Add ability for modules to per file or for the whole module mark code
as needing floating point support.

Add default flags for arm64, riscv, and x86 toolchains.

Needed because gcc 12 is getting much more aggressive about using vector
instructions for non float code, so getting away with avoiding it was
no longer working.

Still not perfect: printf code is being compiled with float, so it's
possible to use floating point instructions inside core kernel or
interrupt handling code if a printf is used.

Possibly will have problems on architectures where mixing float and non
float code at the linker generates issues, but so far seems to be okay.
2022-07-17 16:32:24 -07:00
Travis Geiselbrecht
c580451e83 [arch][riscv] more interrupt cause debugging strings 2022-05-29 17:55:41 -07:00
Travis Geiselbrecht
095aca80f1 [arch][riscv][sbi] add the pmu extension, tweak boot messages, qemu-virt use reset calls 2022-05-29 14:48:04 -07:00
Travis Geiselbrecht
a8e460f847 [arch][riscv] add -misa-spec=2.2 to the compile switches
This works around an issue with newer compilers that default to an
earlier ISA spec that doesn't by default include the zicsr extension by
default.

If the compiler doesn't support the switch, then it's assumed that it
has the extension by default, as older gcc compilers did.
2022-05-22 15:14:34 -07:00
Travis Geiselbrecht
4695035505 [arch][riscv] C++ header guard the core riscv header 2022-05-13 01:06:58 -07:00
Travis Geiselbrecht
ce9f4f6db2 [arch][riscv] using a new trick utilize real instructions to zero fpu state
Previously was hard coding the instructions to work around a limitation
of the assembler that did not allow using fpu instructions when the code
was being compiled without support. Move the zeroing routine into a
separate assembly file and override the architure at the top.
2022-03-12 17:39:00 -08:00
Travis Geiselbrecht
2bb32fe813 [libc][assert] add __PRINTFLIKE to assert_fail_msg()
Fix a warning as a result of this.

fixes issue #302
2021-11-12 22:07:14 -08:00
Travis Geiselbrecht
7285a2d1fd [bus][pci] convert the pci driver's back end to simple C++ objects
It was already rolling a vtable so go ahead and just implement it as
actual c++ objects.
2021-11-12 20:44:47 -08:00
Travis Geiselbrecht
a6ddffd80b [arch][warnings] fix -Wmissing-declarations warnings 2021-10-21 23:08:38 -07:00
Travis Geiselbrecht
035a15325a [arch][riscv] start of FPU support
Add build system support for at least being aware of the FPU on
the architecture, not building code to use it.

At the moment, only sets up the FPU into Initial state prior to
entering user space and then ignores it.
2021-04-13 02:39:34 -07:00
Travis Geiselbrecht
1772e7e890 [arch][riscv] move the iframe definitions into a separate header and add syscall support 2021-04-11 03:38:03 -07:00
Travis Geiselbrecht
38fa93cbbf [arch][riscv] update exception code to handle exceptions from user space
Added two paths in the exception code to deal with coming from user and
kernel space.
Added first stab at a functional arch_enter_uspace()
2021-04-11 02:56:06 -07:00
Travis Geiselbrecht
525d2e9c77 [arch][riscv] switch the percpu pointer to tp reg
For user space support, the sscratch register cannot hold the pointer to
the current cpu, as much as it is convenient.

Change the logic to use tp register (x4) to point to percpu, and
dereference the local thread from it directly.
2021-04-11 01:38:25 -07:00
Travis Geiselbrecht
a3713e8b39 [arch][riscv] add simple support for context switching user space aspaces
Pretty inefficient but probably works well enough.
2021-04-09 02:00:57 -07:00
Travis Geiselbrecht
8b6a43945d [arch][riscv][mmu] permanently allocate top level buffers to help keep user space aspaces in sync
Kind of wasteful, but much simpler than having to manually sync every time something changes
in the kernel aspace. I think riscv machines with mmu can waste 1MB of page tables up front.

Can revisit later if needed.
2021-04-08 23:55:27 -07:00
Travis Geiselbrecht
f463f50e54 [arch][riscv] add trampoline page table and start user space address space support
Up until now the bottom part of ram has been identity mapped, left over
from initial bootstrapping. Set up two top level page tables: one with the
the identity map and one without. Once the kernel starts switch to the second
but keep the former around for bootstrapping secondary cpus.

Start adding support for user address spaces, currently mostly untested.

Still have to solve the problem of keeping the kernel parts of the page tables
in sync. Will probably preallocate all of the ones needed.
2021-04-03 02:40:32 -07:00