74 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
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
Mahavir Jain
68c6ae154c [wait-queue] fix wake all to remove threads from tail
While waking up all threads from wait-queue, order should
start from tail to maintain correct scheduling sequence.

Signed-off-by: Mahavir Jain <mahavirpj@gmail.com>
2025-09-29 23:03:59 -07:00
Travis Geiselbrecht
bab69228bd [lib][libc] clear errno in the thread TLS
When creating new thread, the TLS entries are copied from the parent
thread, but zero out errno so it defaults to 0.
2025-07-28 23:54:07 -07:00
SP-Melokc
a74f9ebbd8 Update thread.c
Changed kmain to lk_main
2025-02-25 01:08:59 -08:00
Travis Geiselbrecht
71a413dab4 [arch][arm-m] move the debugger structure out of kernel/thread
The arm-m specific debugger structure really should live in arm-m code,
so move it there to clean things up a bit.
2024-02-26 00:58:04 -08:00
Aaron Odell
b8e102ecb7 [kernel][thread] Track per-thread scheduling statistics
Track per-thread runtime, last scheduled time, and count of times
scheduled. Query via existing threadstats CLI.
2023-06-01 12:40:55 -07:00
Travis Geiselbrecht
49644a2c39 [platform][rosco-m68k] Add port to the Rosco M68k board
Port to the really neat 68010 based board at https://rosco-m68k.com/

Port Features:
-10Mhz 68010
-1MB ram
-Dual UART + timer implemented as a 68c681 chip
-timer running at 1Khz, UART A for console
-interrupt driven RX support

Some amount of extending of the 68k exceptinon code was needed to
support the autovectored irqs that the 68681 uart uses. Added build
system support for 68010.
2022-04-12 00:25:12 -07:00
Travis Geiselbrecht
a895bcece5 [kernel][warnings] fix a few -Wmissing-declarations warnings in the kernel 2021-10-21 23:14:27 -07:00
Travis Geiselbrecht
d836b07279 [console] add a panic time alternate version of the thread list
This works if the thread lock is held at panic time to keep it from
recursing.
2021-10-07 00:00:22 -07:00
Travis Geiselbrecht
89f9805277 [lib][console] move the state of the console into an object
This will allow in the future multiple instances of it to be active at
at a time. Place the current console in a new TLS slot per thread so
threads created as a side effect of console commands can properly run
commands.
2021-05-29 00:52:47 -07:00
Travis Geiselbrecht
ed7e3571c2 [kernel][thread] gcc 10.2 warning fix
In a previous patch, on architectures where SMP is disabled,
thread_pinned_cpu() will always return a negative number. GCC 10.2
is somewhat more aggressive about negative shift values so it wasn't
detecting that the second path of an if statement is never taken.

Solution is to cache the pinned cpu value and use it for both the
test and the operation.
2020-10-09 01:25:03 -07:00
vannapurve
961ef679cf [kernel][thread] Wake up only needed cores
This change ensures that during thread scheduling
only needed cores are woken up as per the thread's
CPU affinity rather than all cores. This would avoid
un-necessary wakup for other cores from lower power
states without having to execute anything.

Signed-off-by: vannapurve <vannapurve@google.com>
2020-10-09 01:10:35 -07:00
Travis Geiselbrecht
df32504748 [kernel] test run sorting some includes
No functional change.
2019-07-13 17:21:00 -07:00
Travis Geiselbrecht
85a50ea3f4 [kernel][license] try out using a smaller version of the license header
Replace the body of the MIT license with a reference to the LICENSE file
and a URL with the MIT license. Replaces 20 something lines with 3.

No functional change.
2019-06-19 23:28:14 -07:00
Travis Geiselbrecht
3699e45942 [include][lib] move most of the dangling top level include/lib/.h files into their lib
Only remaining one is console.h which is a little funny and will need to
be dealt with separately.
2019-06-19 22:44:15 -07:00
Travis Geiselbrecht
d8fa82cb91 [formatting] run everything through codestyle
Almost nothing changes here except moving braces to the same line as the
function declaration. Everything else is largely whitespace changes and
a few dangling files with tab indents.

See scripts/codestyle
2019-06-19 21:02:24 -07:00
Travis Geiselbrecht
1b7a28efb8 [include][lk] fixup lk/ include path move 2019-06-19 19:46:11 -07:00
Travis Geiselbrecht
f98cef7992 [warnings] fix warnings across the entire code base
Fix or squelch all warnings in all code covered by buildall.
2018-12-16 17:33:22 -08:00
Travis Geiselbrecht
8cf28bbdcf [arch][riscv] Initial port to a riscv32 sifive target
Currently targets qemu's sifive_e machine, which is a split flash/ram
machine, much like the Sifive HiFive1. Untested as of yet on a real
HiFive1.

Basic support including interrupts and architectural timers in place.
2018-12-16 16:44:23 -08:00
circl0
6782ea9af6 [kernel] fix get top thread error if NUM_PRIORITIES < 32 2016-09-30 10:39:58 -07:00
Erik Gilling
c01adc6e0d [kernel] Add debug tracking of stack memory usage.
On thread creation, initializes the stack to a known value.  When
thread state is dumped, this value is uses to intuit max stack
usage.
2016-06-03 12:25:01 -07:00
Travis Geiselbrecht
2a51111387 [kernel] add more controlled way to switch vmm address spaces 2016-02-23 21:07:22 -08:00
Travis Geiselbrecht
9a5cd5c529 [kernel][vm] add support for multiple user address spaces
Generically add support for more than one user address space,
attach to a thread, and context switch as thread swaps happen.

Add support for ARM and ARM64, and stub out support for x86 platforms.
2016-02-16 15:24:25 -08:00
Travis Geiselbrecht
d569c090ea [vim] remove vim expandtab comments on most of the files 2016-02-14 12:32:07 -08:00
Travis Geiselbrecht
2eb32a4369 [style] mass reformat all the non external code to 4 space indents
Ran everything through scripts/codestyle.space, which uses astyle
to generally follow K&R style.

Biggest non whitespace change is pulling brackets down on function
declarations, which I'm pretty ambivalent about, but astyle insists
on taking a stance
2016-02-14 12:24:01 -08:00
Brian Swetland
1de5270d6e [kernel][smp] reduce impact of SMP code on UP builds
Hopefully to nothing...
- remove current/pinned cpu tracking in thread struct
- macroize access to current/pinned cpu tracking
- empty-inline/remove vestigial mp_* bits in UP builds
2016-01-17 18:46:51 -08:00
Travis Geiselbrecht
ec69e5f881 [kernel] properly set the target debug led when inside non-idle thread context 2015-12-14 15:20:25 -08:00
Travis Geiselbrecht
4ab14424bd [cppcheck] clean up a few suggestions from cppcheck 2015-11-24 15:08:38 -08:00
Travis Geiselbrecht
7c1b8a0b66 [kernel] add a #define to set to enable tracing 2015-11-06 15:51:29 -08:00
Travis Geiselbrecht
814ca4e8a5 [kernel] add code to check some overrun padding placed on the bottom of each threads' stack
-Should impact context switch performance a bit
-Only enabled at LK_DEBUGLEVEL > 1
2015-11-04 18:35:20 -08:00
Travis Geiselbrecht
2d1e01cdd4 [kernel] remove THREAD_CHECKS which was always just the equivalent of DEBUG_ASSERT 2015-11-04 18:35:20 -08:00
Travis Geiselbrecht
1baadec12a [kernel] thread_create_etc() needs to actually save the custom stack 2015-09-21 14:19:42 -07:00
Brian Swetland
e21e81f24d [arch][arm-m] debugger info
Use the reserved cortex-m vectors 8 and 9 for a magic cookie and
pointer to a debugger info structure.  This structure contains
pointers to the threadlist, the current thread pointer, and
offsets to important members of the thread structure.  With
this information, an attached debugger can walk the threadlist
and obtain state, name, registers, etc for each thread.

Use global define WITH_DEBUGGER_INFO to enable this.
2015-07-09 17:39:25 -07:00
Brian Swetland
56d269dc68 [kernel] don't print tls entries on platforms with no tls slots
Avoids wasting a line on this in the thread list.
2015-06-28 13:56:24 -07:00
Arve Hjønnevåg
e07de0831a [kernel][thread] Fixup conflict resolution of "Allow pinning threads to a specific cpu"
Change-Id: I8b2cab8b60e5a25ab5270db55bbe6b59eecf88c6
2015-05-13 20:21:07 -07:00
Arve Hjønnevåg
be2ba93d69 [kernel][thread] Fix startup problem
Change-Id: I4b0f70a5951d641a6dbb477c6f48600aaef8e219
2015-05-13 20:20:39 -07:00
Arve Hjønnevåg
071878c104 [kernel][thread] Hack thread_is_realtime to ignore thread that don't have a high priority
Allow setting the realtime flag on low priority threads to disable the
tick without also loosing ipi interrupts.

Change-Id: Ia30e4d20105a65945918fc996c3114c3852963a9
2015-05-13 20:20:39 -07:00
Satya Popuri
19c2ea6dc8 [kernel][thread] Hook uthread context switch
thread_resched() calls into libuthread to switch userspace.

Change-Id: Iad8e5ade175316eb784410c369c6473b68b6f5d6
Signed-off-by: Satya Popuri <popuri@google.com>
2015-03-19 18:01:09 -07:00
Arve Hjønnevåg
8a7acf34f6 [kernel][thread] Add thread_secondary_cpu_init_early
Allows use of bootstrap thread to initialize secondary cpus.

Change-Id: I09a5e8ab446a8f422f72599631269df83865d966
2015-03-12 14:51:49 -07:00
Arve Hjønnevåg
63000f0cb3 [kernel][thread] Add some tracing when DEBUG_THREAD_CONTEXT_SWITCH is set
Change-Id: I65118c52155a6ef55dfdbf2c43107c7439fae38b

Conflicts:
	kernel/thread.c
2015-03-10 17:49:32 -07:00
Arve Hjønnevåg
794d304599 [kernel][thread] Allow pinning threads to a specific cpu
The existing runqueues are scanned on resched until a thread that is
not pinned to another cpu is found.

Per-pu runqueues can be added later to avoid this scan.

Change-Id: Idd7dbddb4b79ef38a0ccd0c42b53f8867020e6cd

Conflicts:
	kernel/thread.c
2015-03-10 17:49:32 -07:00
Arve Hjønnevåg
1d7d5c5e60 [kernel][thread] Move thread to new run queue and call thread_resched in thread_set_priority
The new priority of the thread may mean that a different thread should run.

Change-Id: Ia683613bb825929b506ea885c6ef303d9748c6a0
Signed-off-by: Arve Hjønnevåg <arve@android.com>
2015-03-10 17:49:32 -07:00
Travis Geiselbrecht
ac53b59ca7 [kernel][smp] move idle threads out of the run queue, fix some mp code
-Each cpu's idle thread now doesn't sit in the run queue and is only
selected when no other threads are ready to run. This means there is
now an implicit affinity for the idle threads for each cpu, and reduces
the amount of idle thread thrashing without real affinity (or per cpu
run queues).
-Fix some bitmap logic in the mp_reschedule and mp_mbx_reschedule_irq
code path that probably resulted in not enough reschedules.
-Change a few spots for mp reschedule ipis in general thread path. Add
ipi to thread_resume, remove one from thread_create_etc.
2015-03-10 16:43:55 -07:00
Travis Geiselbrecht
f55d8edd74 [kerne] a few smp tweaks
-fix a couple of mp accessor routines
-make sure the cpu number is read inside a spinlock in timer code
-add a few debug asserts
2015-03-10 16:43:55 -07:00
Travis Geiselbrecht
adc07c0b69 [kernel] fix some smp code in particular build configurations 2015-03-10 16:43:55 -07:00
Travis Geiselbrecht
16d0a96dff [kernel] add preliminary SMP support to the kernel 2015-03-10 16:43:55 -07:00
Travis Geiselbrecht
8fb2c54304 [kernel] remove critical_section, move everything to spinlocks 2015-03-10 16:43:54 -07:00
Travis Geiselbrecht
45e146fc81 [kernel] fix bug in thread_detach that wakes up waiters on the wrong queue
Thanks to a bug report by Arnab Basu, fixed the incorrect waking up of
thread_join waiters on the calling thread, instead of the thread being detached.
2015-03-10 16:34:21 -07:00
Arve Hjønnevåg
0eba11d485 [kernel][thread] Switch tls entries from uint32_t to uintptr_t
Allows storing a pointer in a tls slot on 64 bit systems.

Change-Id: I47f58e27c5625945c6aeca054eb463fd3c89ac45
2015-03-08 19:05:12 -07:00