Commit Graph

190 Commits

Author SHA1 Message Date
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
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
Travis Geiselbrecht
2d567e437d [kernel][vm] remove default KERNEL_ASPACE_BASE definition
Require the platform or arch to provide it. The default was pretty
obsolete, assuming a 32bit arch with a split 50/50 mapping. May as well
make it a forced arch thing so that it's known to be correct.

Had to define it for OR1K, it was already defined for all the other
(known) architectures.
2025-07-09 23:55:13 -07:00
Travis Geiselbrecht
22fee46d6d [kernel][includes] clean up and add more commentary to the kernel API
-In a few places, rearrange some of the fields of some structures to
pack more nicely in 64bit environments.
-Convert C style comments to C++ style

No functional difference.
2025-07-09 22:09:32 -07:00
Travis Geiselbrecht
9d66069183 Merge from x86-smp: implement SMP for both 32 and 64bit x86
A large pile of changes to the PC platform and x86 architecture that
facilitate SMP support. Tested in both 64 and 32bit on qemu and real
hardware all the way back through i486.
2025-04-10 22:17:02 -07:00
Arve Hjønnevåg
4ff60704a5 kernel: mutex: Don't allow mutex_acquire with interrupts disabled
Calling mutex_acquire with interrupts disabled is usually unsafe. Add a
debug assert to catch this. Only check this after threading is enabled
on the primary CPU though, as the thread that runs the initial init
hooks run with interrupts disabled and in this case no other thread can
own the mutex anyway. Also allow 0 timeout since
vmm_get_address_description uses this to allow exception handlers to
dump additional information when the vmm_lock is available.

Bug: 304850822
Change-Id: I22c91c48b7853c0b8e0eb8da61cdfc9cd1650ae8
2025-03-29 16:18:55 -07:00
SP-Melokc
a74f9ebbd8 Update thread.c
Changed kmain to lk_main
2025-02-25 01:08:59 -08:00
Travis Geiselbrecht
0ac7a2d7cb [kernel][mp] use proper types when computing idle and active cpus
Also return a proper bool instead of an int. No functional change.
2025-01-11 16:59:42 -08:00
Noah Woo
bea3787d6e [kernel][port] add a missing THREAD_UNLOCK call in port_group_remove()
port_group_remove() used to return without unlocking a thread if no
matching read port is found, which is not correct. This patch adds a
call to THREAD_UNLOCK before it returns ERR_BAD_HANDLE.
2025-01-03 20:52:08 -08:00
Travis Geiselbrecht
902e2fcb8a WIP set up per cpu structures for x86-64
only on the boot cpu for now
2024-12-06 21:11:51 -08:00
Kelvin Zhang
0d8a1ec055 [app][uefi] Make allocate_pool return identity mapped memory 2024-11-06 10:12:17 -08:00
Kelvin Zhang
347db09959 [app][uefi] Place linux kernel at identity mapped virtual memory addresses 2024-11-06 10:12:17 -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
bcfad25587 [arch][x86][mmu] update how mmu code accesses physical pages
Move the mmu_initial_mapping from platform into arch/x86. For this
architecture the default mappings are basically hard coded in arch/x86
anyway, so move ownership of this data there, closer to where it's
actually initialized.
Update the 32 and 64bit paging code to properly use the paddr_to_kvaddr
and vice versa routines.
Update to allocate page tables directly from the pmm instead of the heap
(on 32bit code).
2022-11-02 23:48:49 -07:00
Travis Geiselbrecht
2367686854 [make] add a way for a module to opt into additional warnings
Move -Wmissing-declarations and -Wmissing-prototypes into this bucket.
Opt in most of the core top level modules in the system. More to follow.
2022-10-23 23:16:48 -07:00
Chintan Pandya
550256d6bc kernel: vm: Fix incorrect argument passing in 'vm map' test
vm map takes arg 3 as virtual address. And same needs
to be passed to retrieve aspace. Fix this by passing
right arg.

Test: Ran 'vm map' test
        ] vm map 0xc0000000 0xffff000ff0000000 1 0x0
        arch_mmu_map returns 0

Signed-off-by: Chintan Pandya <chintanpandya@google.com>
2022-10-23 21:40:18 -07:00
Travis Geiselbrecht
5377fcbd12 [kernel][vm] add error handling with failed arch mmu mappings
In the 3 places in the upper VMM where it calls arch_mmu_map, try to
gracefully handle errors by unwinding the region creation.
2022-10-23 21:02:22 -07:00
Travis Geiselbrecht
c997cfbc48 [kernel][mutex] Add a default timeout to the C++ wrapper mutex 2022-04-22 20:27:23 -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
thomas
a7d2752b28 [kernel][vm] try the next arena if current is out of free pages 2022-01-26 12:16:53 -08:00
Travis Geiselbrecht
be6169820d [kernel][vm] Move the VM postheap init hook back one step
This is a cheesy solution to adding another top level hook, which really
should be done.
2021-12-27 22:24:31 -08:00
Michael Bishop
4e54f0fedc [timer] make periodic timers more accurate 2021-11-12 19:31:27 -08:00
Travis Geiselbrecht
dbd1b6d903 [kernel][C++] add some simple C++ wrapper and RAII holders for various locks
Just a wrapper around mutex and spinlock for now.
2021-11-10 22:57:07 -08: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
476b750102 [kernel][ports] double the size of the port message on 64bit hosts
It seems the initial port packet size was sized to hold two words for a
32bit machine. In the 64bit case double the size from 8 bytes to 16.
2021-06-27 00:30:58 -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
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
d28e8dc3e0 [kernel][mutex] tweak a mutex routine to take a const 2021-04-02 19:28:58 -07:00
Travis Geiselbrecht
7102838b49 [arch] have each arch define ARCH_HAS_MMU
This lets some code decide whether or not there's any mmu
present to use. Also kernel VM will complain if it isn't set
as an extra safety.
2021-03-30 02:48:59 -07:00
Travis Geiselbrecht
7033559d38 [inc][c++] add some additional __BEGIN_CDECL/__END_CDECLS to various global headers
Just a few that were missed, and picked up with some additional C++ code.
2021-01-20 01:02:48 -08: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
4edb93adde [lib][console] rename some console command types to be prefixed with console_
Some of the structures, notably 'cmd', in the lib console stuff are a
little too generically named and have collided with some other code
so prefix the names a bit more cleanly with console_

The change is largely mechanical, and folks with out of tree code can
easily switch by renaming:
cmd -> console_cmd
cmd_args -> console_cmd_args
cmd_block -> console_cmd_block
console_cmd -> console_cmd_func

Apologies if this breaks you but it should be pretty easy to fix.
2020-07-25 15:59:58 -07:00
Travis Geiselbrecht
f371fa246b [arch] move the atomic ops into a separate header
Now you need to include arch/atomic.h to get to the atomic routines.
This simplifies a recusion issue in the way arch/ops.h included
arch_ops. Also just generally makes things cleaner.
2020-05-16 15:05:34 -07:00
Travis Geiselbrecht
c57b661c93 [kernel][thread] change the way get_current_thread is inlined
Previously, was relying on a regular definition with the arch_ops.h code
overriding it with a static inline. This has been annoying for some
years since it forces the declarations to be in order. Change it to
simple declare an inline wrapper around an arch_ routine that does
whatever it needs to do.
2020-05-16 14:29:21 -07:00
Travis Geiselbrecht
7cc7d79e74 [pmm] add a few more convenience routines
Add a routine to just allocate a single page and a fast page for
allocating a single kvaddr page.
2020-05-10 16:51:58 -07:00
Travis Geiselbrecht
bff17f7e99 [platform][riscv] add code to read the size of memory from the FDT
Tweak the novm allocator to let us more easily add a variable sized
arena at boot.

Also added code to trap secondary cpus and reenable the use of WFI
instruction.
2019-11-02 18:13:02 -07:00
Travis Geiselbrecht
fdc08a8446 [arch][riscv] port to riscv64
Very little needed to port except to conditionalize some assembly in the
context switch and exception code. Mostly needed to move build system
stuff around and add a new project.
2019-11-02 17:21:13 -07:00
Travis Geiselbrecht
df32504748 [kernel] test run sorting some includes
No functional change.
2019-07-13 17:21:00 -07:00
Travis Geiselbrecht
6cb02526b7 [include][console] split lib/console.h into two
TL;DR most uses of lib/console.h -> lk/console_cmd.h

Move the part that lets a piece of code somewhere in the system to
define a console command from the actual lib/console api to start an
instance of the console. Move in almost every place the user of the
console command definition to the new header, lk/console_cmd.h which is
always in the include path.

Also remove most uses of testing for WITH_LIB_CONSOLE since you can
almost always just safely define it and then let the linker remove it.
2019-07-13 16:56:33 -07:00
Travis Geiselbrecht
3aecdda231 [includes] replace header guards with #pragma once 2019-07-13 15:46:16 -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
4f2d4841d8 [kernel][includes] move the kernel includes into the kernel directory
No functional change, just moved some include files.
2019-06-19 19:46:11 -07:00
Travis Geiselbrecht
1b7a28efb8 [include][lk] fixup lk/ include path move 2019-06-19 19:46:11 -07:00
leliang.zhang
dfc1159abb [novm] fix the bug which cause memory leak in novm 2019-06-19 18:38:05 -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