675 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
e47183725d [arch][arm64] move secondary cpu entry point to separate function
- Make the secondary entry point be logically separate function, though
  declared in the same file.
- Add a trick where the kernel base + 4 is the secondary entry point.
  Not really useful except makes it easy to compute the offset
  elsewhere.
- Changed the entry point to arm64_reset and move _start to the linker
  script, which is what most other arches do.
- While was in the linker script, make sure the text segment is aligned
  on MAXPAGESIZE, though doesn't make any real difference currently.
- Generally clean up the assembly in start.S with newer macros from
  Fuchsia, and avoid using ldr X, =value as much as possible.
- Fix and make sure arm64 can build and run with WITH_SMP set to false.
  Add a new no-smp project to test this.

Note this will likely break systems where all of the cpus enter the
kernel simultaneously, which we can fix if that becomes an issue.
Secondary code now completely assumes the cpu number is passed in x0.
This can be emulated with platform specific trampoline code if it needs
to that then just directs into the the secondary entry point, instead of
trying to make the arch code have to deal with all cases.
2025-10-12 19:47:33 -07:00
Travis Geiselbrecht
91128ad729 [arch][arm64] clean up how secondary cpus are initialized and tracked
- Add a percpu structure for each cpu, akin to x86-64 and riscv. Pointed
  to by x18, which is now reserved for this in the kernel. Tweaked
  exception and context switch routines to leave x18 alone.
- Remove the cpu-trapping spinlock logic that is unused in mainline,
  probably. (Can add a new version of it back if it's necessary).
- Switch fdtwalk helper to using the newer, cleaner way of initializing
  secondaries using the PSCI CPU_ON argument that should be pretty
  standard on modern implementations. (Possibly an issue with old
  firmware).
- Remove the notion of computing the cpu ID from the Affinity levels,
  which doesn't really work properly on modern ARM CPUs which more or
  less abandoned the logical meaning of AFFn.
2025-10-12 19:47:33 -07:00
Travis Geiselbrecht
1684855b9a [dev][psci] clean up the psci driver a bit, add arg to cpu_on
- General cleanup of the driver a bit
- Aadd a boot time message that prints the version
- Add the argument field to CPU_ON
- Pass the cpu number through from fdtwalk library
2025-10-12 19:47:33 -07:00
Travis Geiselbrecht
9c67917dd7 [fpu] Implement two versions of all of the printf routines: with and without fpu support
The default printf and family will now not implement FPU support, a
second copy of the routines will be generated with the _float suffix.

ie, printf() has no %f support, but printf_float() does.

This is to avoid the default printf from emitting any floating point
instructions when used within core kernel code which has been an off and
on problem for years, especially on architectures that are eager to use
fpu/vector instructions for regular non-fpu code.

If FPU is not implemented on the arch, the *_float routines will alias
to the integer only one.

Perhaps a much more proper solution is to invert this and require every
caller of printf that cannot tolerate fpu codegen (which in mainline is
most of it) use a _nofloat implementation, but this would touch
pratically all printfs in mainline.

This solution acknowledges that for the most part most of the code in
mainline is in-kernel support code, and doesn't need floating point,
except for perhaps some app/* code, which already can opt in.

This solution also can potentially bloat the size of the binary by
having two complete implementations, though I think in practice the
architectures where the extra few KB of code will matter generally dont
have FPU support, or aren't using it. In the latter case the
link-time-gc should remove unused _float routines.
2025-10-08 23:51:06 -07:00
Travis Geiselbrecht
664bb17afa [ubsan] fix some bugs and warnings discovered by ubsan
- X86 cpuid feature list dump was using the wrong array and walking off
  the end of one.
- GICv2 code had a left shift by up to 31 of an integer. Needs to be
  unsigned.
- PLIC same as GIC code.
- fdtwalker code should be using a bytewise accessor based helper
  function for reading large integers out of an unaliged FDT.
- PCI BIOS32 search code could do a 32bit unaligned read of a string,
  switch to using memcmp.
2025-10-05 15:35:31 -07:00
Travis Geiselbrecht
ac6468c916 [ubsan] fix some warnings on 32bit arches
Mostly from the use of ssize_t which does not play well with printf and
32bit arches on LK for underlying type reasons that haven't really ever
been sorted out properly.
2025-10-05 14:43:13 -07:00
Travis Geiselbrecht
48d331f144 [clang] fix a few warnings
Mostly dealing with comparing a pointer variable that is declared
nonnull, which I find to be a dubious warning but is pretty safe in this
case since the compiler will be more aggressive about the nullness of
the arguments.
2025-10-05 13:56:55 -07:00
Travis Geiselbrecht
976cd70f4f [unittests] add RUN_UNITTESTS_AT_BOOT build option
This will cause the system to automatically run all of the unit tests
after a short pause on boot. Will be used by an automatic test script.

At the moment there aren't a lot of unit tests in the list, but this
should greatly increase the utility of them since they'll be
automatically run.
2025-10-01 23:54:54 -07:00
Travis Geiselbrecht
5720b2a32f [fs][fat][tests] be a bit more forgiving if the test device isn't present
Have the unit tests for the FAT driver fail a bit more gracefully if the
test device is not present, to make it somewhat easier to live with this
driver present with systems a way to mount a test volume.
2025-10-01 23:35:48 -07:00
Travis Geiselbrecht
04b88750b3 [debug] remove lib/debug and move to the top/ module
Most of the functions for this was declared in a top level lk/ include
space, so go ahead and move it there.

A few exceptions:
- Moved spin() over to platform/time.h and platform/time.c since the
function more logically belongs to platform/time.h.  Any users of
spin() will need to update their headers to include platform/time.h
instead.

- Renamed spin_cycles() to arm_cm_spin_cycles() and moved over into
arm/cm.h since it is currently defined in arch/arm-m and only used for
targets that implicitly are for arm-m.
2025-10-01 20:56:06 -07:00
Kelvin Zhang
a1be045514 [lib][uefi] Migrate to boot memory protocol
Image loading protocol is deprecated, migrate
2025-09-18 22:13:33 -07:00
Kelvin Zhang
848fbfefe7 [lib][uefi] Mark bio functions as WEAK
OEMs may want to override certain bio functions, hence mark
`open_block_device` and `open_async_block_device`  as WEAK. This allows
OEMs to customize any of the bio functions
2025-09-18 22:13:33 -07:00
Kelvin Zhang
72c92b5d81 [lib][uefi] Update protocol headers from upstream
Copied from https://cs.android.com/android/kernel/superproject/+/common-android-mainline:bootable/libbootloader/gbl/libefi_types/defs/protocols/
2025-09-17 10:27:49 -07:00
Yi-Yo Chiang
fb9c37cbd6 [lib][uefi] Implement EFI_ERASE_BLOCK protocol 2025-09-05 13:49:28 -07:00
Yi-Yo Chiang
91a76a9a03 [lib][uefi] Fix -Wc++20-compat compiler warning 2025-09-04 23:27:02 -07:00
Yi-Yo Chiang
b1e26e90cd [lib][uefi] Update UEFI protocol definitions
GBL HEAD: 878653395d
2025-09-04 09:59:22 -07:00
Travis Geiselbrecht
2377c3d440 [make] fix three misuses of TOBOOL
Forgot to expand the variable in the call to the TOBOOL function which
would cause the result to always be true. In this case always resulted
in the test code for these three modules to be included.
2025-09-03 15:14:18 -07:00
Travis Geiselbrecht
fff0f2a740 [arch][arm64] add support for 16k pages 2025-08-31 21:47:32 -07:00
Yi-Yo Chiang
e38bf65034 [lib][uefi] Default implementation of BootService.SetWatchdogTimer()
The platform_watchdog_ methods are optional features that the target
platform can choose to implement.
If they are implemented, then BootService.SetWatchdogTimer() would call
them to setup the hardware watchdog.
If they are not implemented (for example presubmit targets), then
BootService.SetWatchdogTimer() would test the WEAK symbol against a NULL
pointer and error out.

To use BootService.SetWatchdogTimer(), platforms can choose to either
implement platform_watchdog_init and platform_watchdog_set_enabled, or
override the entire BootService.SetWatchdogTimer() method.
2025-08-22 09:21:56 -07:00
Yi-Yo Chiang
7b26b7cf3a [lib][uefi] Stub implementation of BootService.RaiseTpl .RestoreTpl 2025-08-22 09:21:56 -07:00
Kelvin Zhang
2be24ed6e6 [lib][uefi] Fix os loading protocol setup
get_buffer was actually unused, fix
2025-08-15 01:02:55 -04:00
Ying-Chun Liu (PaulLiu)
17dc5cd0aa lib: uefi: runtime_service_provider.cpp: implement Get/SetVariable
GBL debug target will use GetVariable to get a variable called
gbl_debug. If the variable exists, it waits for the gdb to be started.

Signed-off-by: Ying-Chun Liu (PaulLiu) <paulliu@debian.org>
2025-08-14 12:25:36 -04:00
Ying-Chun Liu (PaulLiu)
45d7da5640 lib: uefi: add volatile UEFI variable support
GBL needs to read an UEFI variable called "gbl_debug".
We add 2 commands. "uefi_set_var" to set an UEFI variable. And
"uefi_list_var" to list the UEFI variables.

For GBL, we can use "uefi_set_var gbl_debug 1" to enable its
debug mode.

Signed-off-by: Ying-Chun Liu (PaulLiu) <paulliu@debian.org>
2025-08-14 12:25:36 -04:00
Ying-Chun Liu (PaulLiu)
de3e831eae lib: uefi: add charset converting helper functions.
The UEFI variable names are in UTF-16. We add several helper
functions to convert US-ASCII to UTF-16.

Signed-off-by: Ying-Chun Liu (PaulLiu) <paulliu@debian.org>
2025-08-14 12:25:36 -04:00
Kelvin Zhang
af1f19a2cc [lib][uefi] Add interrupt based async IO support
Add a new API to bio layer(read_async) where function will
return immediately, and a callback function will be called from
interrupt context, when block driver completes the IO request.
2025-08-13 16:13:38 -04:00
Ying-Chun Liu (PaulLiu)
cfeef533d1 lib: uefi: debug_support.cpp: fix code-style of teardown_debug_support()
This commit refactors teardown_debug_support() to make the code
looks simpler.

Signed-off-by: Ying-Chun Liu (PaulLiu) <paulliu@debian.org>
2025-08-11 14:19:55 -04:00
Kelvin Zhang
22b808b6ec [lib][uefi] Fix LTRACEF logging
LTRACEF already outputs function name, no need to do it again
2025-08-08 16:13:30 -04:00
Kelvin Zhang
0f15dc23bc [lib][uefi] Move several memory APIs to uefi_platform.cpp
This allows OEMs to customize exactly how their memories are allocated.
For examples, OEMs might want to allocate certain parts of the UEFI
from high address kernel space, for ease of access in LK world.
Since UEFI spec requires everything to be identity mapped, the default
implementation of memory allocation is identity mapped.
2025-08-08 03:06:05 -04:00
Ying-Chun Liu (PaulLiu)
c5612330cc lib: uefi: add EfiDebugImageInfo for debug
This commit adds the functionality of generate EfiDebugImageInfo
while loading the image.

This feature is described in UEFI Spec 2.10. Section 18.4.3.
The implementation ensures support for hardware-assisted debugging and
provides a standardized mechanism for debuggers to discover the load
address of an EFI application.

Signed-off-by: Ying-Chun Liu (PaulLiu) <paulliu@debian.org>
2025-08-06 03:19:47 -04:00
Ying-Chun Liu (PaulLiu)
1ffb54dace lib: uefi: add EfiDebugImageInfoTable for debug
EfiDebugImageInfoTable is used to store EfiLoadedImage for
debug purpose. This commit adds the table to the EfiConfigurationTable.

This feature is described in UEFI Spec version 2.10. Section 18.4.
The implementation ensures support for hardware-assisted debugging and
provides a standardized mechanism for debuggers to discover and interact
with system-level debug resources.

Signed-off-by: Ying-Chun Liu (PaulLiu) <paulliu@debian.org>
2025-08-06 03:19:47 -04:00
Ying-Chun Liu (PaulLiu)
ea327d7242 lib: uefi: add EfiSystemTablePointer for debug
Add EfiSystemTablePointer structure for remote debugger to locate
the address of EfiSystemTable.

This feature is described in UEFI SPEC version 2.10. Section 18.4.2.
The implementation ensures support for hardware-assisted debugging and
provides a standardized mechanism for debuggers to discover the EFI
system table.

Signed-off-by: Ying-Chun Liu (PaulLiu) <paulliu@debian.org>
2025-08-06 03:19:47 -04:00
Ying-Chun Liu (PaulLiu)
777297f156 lib: uefi: uefi.cpp: set vendor string of SystemTable
Setting the vendor string makes us easier for testing the
SystemTablePointer. When we discover SystemTable we can check
if the vendor string is set with proper value.

Signed-off-by: Ying-Chun Liu (PaulLiu) <paulliu@debian.org>
2025-08-06 03:19:47 -04:00
Ying-Chun Liu (PaulLiu)
7c23aac677 lib: uefi: boot_service_provider.cpp: implement Stall call
We implement a Stall call for easier testing with Rust UEFI application.

Signed-off-by: Ying-Chun Liu (PaulLiu) <paulliu@debian.org>
Link: https://rust-osdev.github.io/uefi-rs/tutorial/app.html
2025-08-06 03:19:47 -04:00
Kelvin Zhang
07f5cdf370 [lib][uefi] Make UEFI API signal_event usable in interrupt context
This is to prepare events API for interrupt based async block I/O,
in which case UEFI events will be signaled from block driver callback(in
in interrupt context).
2025-08-05 14:21:57 -04:00
Kelvin Zhang
9bbfeabb9d [lib][uefi] Free memory used by UEFI app after app returns 2025-08-05 14:21:57 -04:00
Yi-Yo Chiang
6ae7086222 [lib][uefi] Add GBL protocol definitions
GBL HEAD: aosp/I28a1926447e54401d9678c40a98957d558125ced
2025-08-05 03:51:19 -04:00
Yi-Yo Chiang
3460cd6c2d [lib][uefi] Implement EFI_TIMESTAMP_PROTOCOL
Reads the ARM64-specific counter registers.
This only works on ARM64 platform obviously, which is the only platform
we support for UEFI at the moment.
2025-07-30 00:11:49 -04:00
Kelvin Zhang
f356261b67 [lib][uefi] Allow identity mapped pages to be free'ed
Since UEFI requires app to be run in physical address space,
we allocate memory that are identity mapped. Previous identity
mapping works as follows:

1. call vmm_alloc_contigious to obtain a VA1(virtual address) mapping to
   PA1(physical address)
2. Use arch_mmu_query to obtain PA1 from VA1
3. arch_mmu_unmap VA1 <-> PA1
4. arch_mmu_map PA1 <-> PA1

Now we can return PA1 as a valid virtual address pointer, which is
mapped to physical memory. However, this has problems. LK allocates a
vmm_region_t struct for each call to vmm_alloc_* . This struct can
never be free'ed, as the associated virtual address VA1 is forever
lost. We can't even free PA1, as PA1 is not a valid argument to
vmm_free(PA1 does not have associated vmm_region_t struct).

The new approach uses pmm_alloc and vmm_reserve_space, allowing
vmm_free_region to be called.
2025-07-23 10:46:42 -07:00
Kelvin Zhang
f7d8f58cdc [lib][uefi] Support async blockio 2025-07-23 10:46:42 -07:00
Kelvin Zhang
76ce54625a [lib][uefi] Support 5 arguments for switch stack call
Some functions have >4 arguments, bump support to 5 arguments
2025-07-16 14:00:00 -07:00
Kelvin Zhang
e8cea74e9b [lib][uefi] Implement events API in UEFI
LK already has events APIs. To support events protocols in UEFI spec,
I created a wrapper object for LK events. This wrapper object keeps
additional data that are necessary to support UEFI events spec:

* Notification function, or callbacks
* Argument for the notification function
* Type of this UEFI event (which specifies when the callback should be
  called)

The events API is essential for supporting async I/O in UEFI. Intended
use case looks like:

* UEFI app creates an event object, attatch callback to this event
* UEFI app calls LK for asynchronous read/write on a block device, with
  the newly created events object
* LK executes IO operation in background, after IO finishes, signal the
  specified event object
* UEFI's callback gets executed
2025-07-16 14:00:00 -07:00
Kelvin Zhang
2ff6ab3920 [lib][uefi] Implement os loading buffer protocol 2025-07-16 14:00:00 -07:00
Kelvin Zhang
953b519d69 [lib][uefi] Update os configuration protocol
Two breaking changes:

* fixup_kernel_commandline was removed by upstream
* GblEfiDeviceTreeMetadata's reserve field is removed, instead a `type`
  variable is inserted earlier in the struct.

Both changes come from upstream GBL, update LK accordingly
2025-07-16 13:59:59 -07:00
Kelvin Zhang
62409f55f6 [lib][uefi] Add error checking to certain functions 2025-07-16 13:59:59 -07:00
Matt Schulte
e51cf68c3c [lib][libc] Make errno thread safe using TLS APIs
Before this change, errno was "completely un-threadsafe" as the comment
states.

This changes errno to be threadsafe by making errno a thread local
variable.
2025-07-10 15:37:43 -07:00
Chieh-Min Wang
816be268be [console] support background command execution
Launch a thread to execute commands when the last argument is ampersand

Signed-off-by: Chieh-Min Wang <cmwang@google.com>
2025-07-09 23:12:51 -07:00
Travis Geiselbrecht
73d29b0860 [lib][libc] don't compile the floating point tests if there's no FPU 2025-06-10 23:57:05 -07:00
Travis Geiselbrecht
2865ab6813 [lib][libc][tests] switch floating point test vector to a constructor
This should avoid a problem with older compilers that do not support non
trivial designated initializers.
2025-06-10 23:37:51 -07:00
Travis Geiselbrecht
03bc415b2b [lib][libc] a few little style tweaks in the printf code
No functional change.
2025-06-10 23:10:45 -07:00