Compare commits

...
3 Commits
Author SHA1 Message Date
Bananymous 7348511ed3 ports/llvm: Add support for pthread_setname_np and cpu count
Also package the llvm into the format mesa expects. This was kinda
tedious to always do manually
2026-07-07 10:30:07 +03:00
Bananymous 20392c6cc1 LibC: Add malloc.h with some GNU extension
This adds `reallocarray`, `malloc_usable_size`, `mallinfo`, `mallinfo2`
2026-07-07 10:30:07 +03:00
Bananymous 0843b8989d Kernel: Fix timer deadline issues in scheduler
Don't wake up threads when getting the next timer deadline.
If we are idling this can: remove thread from block list, get the
deadline for the next blocked thread, return to idle thread. This will
end up sleeping until the next wake up condition which in the worst
case would be rebalance, or indefinitely when not running SMP.

Make sure we always set the next timer deadline. I was not doing that if
the timer interrupt did not lead to a yield
2026-07-07 10:30:07 +03:00
6 changed files with 197 additions and 14 deletions
+4 -2
View File
@@ -326,8 +326,6 @@ namespace Kernel
if (!interrupt_controller.is_using_apic())
return;
wake_up_sleeping_threads();
uint64_t deadline_ns = m_next_reschedule_ns;
if (!m_block_queue.empty())
deadline_ns = BAN::Math::min(deadline_ns, m_block_queue.front()->wake_time_ns);
@@ -368,8 +366,12 @@ namespace Kernel
wake_up_sleeping_threads();
// NOTE: yield will update the timer deadline, but if we do not yield make sure
// we set the next deadline or we won't get another timer interrupt
if (is_idle() || SystemTimer::get().ns_since_boot() >= m_next_reschedule_ns)
Processor::yield();
else
update_wake_up_deadline();
}
void Scheduler::unblock_thread(SchedulerQueue::Node* node)
+13 -2
View File
@@ -29,6 +29,17 @@ build() {
install() {
# This port only contains llvm libraries used optionally by
# mesa port. There is no need to install and fill the disk :D
:
# mesa port. There is no need to install to sysroot :D
local pkg_dir="../llvm-banan_os-$VERSION-$BANAN_ARCH"
rm -rf "$pkg_dir"
mkdir -p "$pkg_dir/include" || exit 1
cp -r 'llvm/include/llvm' "$pkg_dir/include/" || exit 1
cp -r 'llvm/include/llvm-c' "$pkg_dir/include/" || exit 1
cp -r 'build/include/llvm' "$pkg_dir/include/" || exit 1
mkdir -p "$pkg_dir/lib" || exit 1
cp 'build/lib/'*.a "$pkg_dir/lib/" || exit 1
}
@@ -1,6 +1,6 @@
diff -ruN llvm-22.1.8/llvm/include/llvm/ADT/bit.h llvm-22.1.8-banan_os/llvm/include/llvm/ADT/bit.h
--- llvm-22.1.8/llvm/include/llvm/ADT/bit.h 2026-06-16 01:33:53.000000000 +0300
+++ llvm-22.1.8-banan_os/llvm/include/llvm/ADT/bit.h 2026-06-26 20:05:01.569826765 +0300
+++ llvm-22.1.8-banan_os/llvm/include/llvm/ADT/bit.h 2026-07-07 10:03:36.848199127 +0300
@@ -30,7 +30,8 @@
#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \
@@ -13,7 +13,7 @@ diff -ruN llvm-22.1.8/llvm/include/llvm/ADT/bit.h llvm-22.1.8-banan_os/llvm/incl
#include <sys/machine.h>
diff -ruN llvm-22.1.8/llvm/include/llvm/Support/ExitCodes.h llvm-22.1.8-banan_os/llvm/include/llvm/Support/ExitCodes.h
--- llvm-22.1.8/llvm/include/llvm/Support/ExitCodes.h 2026-06-16 01:33:53.000000000 +0300
+++ llvm-22.1.8-banan_os/llvm/include/llvm/Support/ExitCodes.h 2026-06-26 20:05:11.472186824 +0300
+++ llvm-22.1.8-banan_os/llvm/include/llvm/Support/ExitCodes.h 2026-07-07 10:03:36.965344890 +0300
@@ -20,7 +20,7 @@
#if HAVE_SYSEXITS_H
@@ -25,7 +25,7 @@ diff -ruN llvm-22.1.8/llvm/include/llvm/Support/ExitCodes.h llvm-22.1.8-banan_os
// Define the macro with its usual value from BSD systems, which is chosen to
diff -ruN llvm-22.1.8/llvm/lib/Support/Unix/Path.inc llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Path.inc
--- llvm-22.1.8/llvm/lib/Support/Unix/Path.inc 2026-06-16 01:33:53.000000000 +0300
+++ llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Path.inc 2026-06-26 20:22:57.780776278 +0300
+++ llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Path.inc 2026-07-07 10:03:36.987545022 +0300
@@ -111,7 +111,7 @@
#endif
@@ -68,7 +68,7 @@ diff -ruN llvm-22.1.8/llvm/lib/Support/Unix/Path.inc llvm-22.1.8-banan_os/llvm/l
// target
diff -ruN llvm-22.1.8/llvm/lib/Support/Unix/Process.inc llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Process.inc
--- llvm-22.1.8/llvm/lib/Support/Unix/Process.inc 2026-06-16 01:33:53.000000000 +0300
+++ llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Process.inc 2026-06-26 20:19:55.915545542 +0300
+++ llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Process.inc 2026-07-07 10:03:37.017664239 +0300
@@ -37,6 +37,9 @@
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
@@ -81,7 +81,7 @@ diff -ruN llvm-22.1.8/llvm/lib/Support/Unix/Process.inc llvm-22.1.8-banan_os/llv
//=== WARNING: Implementation here must contain only generic UNIX code that
diff -ruN llvm-22.1.8/llvm/lib/Support/Unix/Program.inc llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Program.inc
--- llvm-22.1.8/llvm/lib/Support/Unix/Program.inc 2026-06-16 01:33:53.000000000 +0300
+++ llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Program.inc 2026-06-26 20:05:22.461478923 +0300
+++ llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Program.inc 2026-07-07 10:03:37.031901635 +0300
@@ -334,7 +334,7 @@
namespace llvm {
namespace sys {
@@ -106,8 +106,19 @@ diff -ruN llvm-22.1.8/llvm/lib/Support/Unix/Program.inc llvm-22.1.8-banan_os/llv
ProcessInfo llvm::sys::Wait(const ProcessInfo &PI,
diff -ruN llvm-22.1.8/llvm/lib/Support/Unix/Threading.inc llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Threading.inc
--- llvm-22.1.8/llvm/lib/Support/Unix/Threading.inc 2026-06-16 01:33:53.000000000 +0300
+++ llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Threading.inc 2026-06-26 20:10:24.025022264 +0300
@@ -121,7 +121,7 @@
+++ llvm-22.1.8-banan_os/llvm/lib/Support/Unix/Threading.inc 2026-07-07 10:03:37.256828558 +0300
@@ -70,6 +70,10 @@
#include <OS.h> // For B_OS_NAME_LENGTH
#endif
+#if defined(__banan_os__)
+#include <unistd.h> // For sysconf()
+#endif
+
namespace llvm {
pthread_t
llvm_execute_on_thread_impl(void *(*ThreadFunc)(void *), void *Arg,
@@ -121,7 +125,7 @@
pthread_t llvm_thread_get_id_impl(pthread_t Thread) { return Thread; }
@@ -116,3 +127,30 @@ diff -ruN llvm-22.1.8/llvm/lib/Support/Unix/Threading.inc llvm-22.1.8-banan_os/l
} // namespace llvm
@@ -148,6 +152,8 @@
return uint64_t(syscall(__NR_gettid));
#elif defined(_AIX)
return uint64_t(thread_self());
+#elif defined(__banan_os__)
+ return uint64_t(pthread_self()->id);
#else
return uint64_t(pthread_self());
#endif
@@ -171,6 +177,8 @@
return 24;
#elif defined(__CYGWIN__)
return 16;
+#elif defined(__banan_os__)
+ return sizeof(uthread::name);
#else
return 0;
#endif
@@ -393,7 +401,7 @@
}
return CPU_COUNT(&Enabled);
}
-#elif (defined(__linux__) && defined(__s390x__)) || defined(_AIX)
+#elif (defined(__linux__) && defined(__s390x__)) || defined(_AIX) || defined(__banan_os__)
static int computeHostNumPhysicalCores() {
return sysconf(_SC_NPROCESSORS_ONLN);
}
+54
View File
@@ -0,0 +1,54 @@
#ifndef _MALLOC_H
#define _MALLOC_H 1
#include <sys/cdefs.h>
__BEGIN_DECLS
#define __need_size_t
#include <stddef.h>
struct mallinfo
{
int arena;
int ordblks;
int smblks;
int hblks;
int hblkhd;
int usmblks;
int fsmblks;
int uordblks;
int fordblks;
int keepcost;
};
struct mallinfo2
{
size_t arena;
size_t ordblks;
size_t smblks;
size_t hblks;
size_t hblkhd;
size_t usmblks;
size_t fsmblks;
size_t uordblks;
size_t fordblks;
size_t keepcost;
};
void* malloc(size_t total_size);
void free(void* ptr);
void* realloc(void* ptr, size_t size);
void* calloc(size_t nmemb, size_t size);
void* reallocarray(void* ptr, size_t nmemb, size_t size);
void* aligned_alloc(size_t alignment, size_t size);
int posix_memalign(void** memptr, size_t alignment, size_t size);
size_t malloc_usable_size(void* ptr);
struct mallinfo mallinfo(void);
struct mallinfo2 mallinfo2(void);
__END_DECLS
#endif
@@ -89,6 +89,7 @@ int rand(void);
int rand_r(unsigned* seed);
long random(void);
void* realloc(void* ptr, size_t size);
void* reallocarray(void* ptr, size_t nmemb, size_t size);
char* realpath(const char* __restrict file_name, char* __restrict resolved_name);
unsigned short* seed48(unsigned short seed16v[3]);
int setenv(const char* envname, const char* envval, int overwrite);
+80 -3
View File
@@ -1,7 +1,9 @@
#include <BAN/Atomic.h>
#include <BAN/Math.h>
#include <assert.h>
#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
@@ -220,6 +222,9 @@ static size_t s_allocator_capacity { 0 };
static BitmapAllocator* s_allocators { nullptr };
static pthread_mutex_t s_allocator_lock = PTHREAD_MUTEX_INITIALIZER;
static BAN::Atomic<size_t> s_mmap_count { 0 };
static BAN::Atomic<size_t> s_mmap_bytes { 0 };
void* malloc(size_t total_size)
{
if (total_size >= s_mmap_threshold)
@@ -230,6 +235,9 @@ void* malloc(size_t total_size)
if (address == MAP_FAILED)
return nullptr;
s_mmap_count++;
s_mmap_bytes += mmap_size;
auto& header = static_cast<MmapAllocationHeader*>(address)[0];
header = {
.mmap_size = mmap_size,
@@ -294,6 +302,8 @@ void free(void* ptr)
pthread_mutex_unlock(&s_allocator_lock);
const auto& header = static_cast<MmapAllocationHeader*>(ptr)[-1];
s_mmap_count--;
s_mmap_bytes += header.mmap_size;
munmap(static_cast<uint8_t*>(ptr) - header.offset, header.mmap_size);
}
@@ -311,7 +321,7 @@ static size_t allocation_size(void* ptr)
pthread_mutex_unlock(&s_allocator_lock);
const auto& header = static_cast<MmapAllocationHeader*>(ptr)[-1];
return header.mmap_size - sizeof(MmapAllocationHeader);
return header.mmap_size - header.offset;
}
void* realloc(void* ptr, size_t size)
@@ -346,13 +356,14 @@ void* realloc(void* ptr, size_t size)
void* calloc(size_t nmemb, size_t size)
{
const size_t total = nmemb * size;
if (size != 0 && total / size != nmemb)
if (BAN::Math::will_multiplication_overflow(nmemb, size))
{
errno = ENOMEM;
return nullptr;
}
const size_t total = nmemb * size;
void* ptr = malloc(total);
if (ptr == nullptr)
return nullptr;
@@ -361,6 +372,17 @@ void* calloc(size_t nmemb, size_t size)
return ptr;
}
void* reallocarray(void* ptr, size_t nmemb, size_t size)
{
if (BAN::Math::will_multiplication_overflow(nmemb, size))
{
errno = ENOMEM;
return nullptr;
}
return realloc(ptr, nmemb * size);
}
void* aligned_alloc(size_t alignment, size_t size)
{
if (!BAN::Math::is_power_of_two(alignment))
@@ -380,6 +402,9 @@ void* aligned_alloc(size_t alignment, size_t size)
if (address == MAP_FAILED)
return nullptr;
s_mmap_count++;
s_mmap_bytes += mmap_size;
uintptr_t data = reinterpret_cast<uintptr_t>(address) + sizeof(MmapAllocationHeader);
if (auto rem = data % alignment)
data += alignment - rem;
@@ -404,3 +429,55 @@ int posix_memalign(void** memptr, size_t alignment, size_t size)
return (*memptr = aligned_alloc(alignment, size)) ? 0 : -1;
}
size_t malloc_usable_size(void* ptr)
{
if (ptr == nullptr)
return 0;
return allocation_size(ptr);
}
struct mallinfo mallinfo(void)
{
constexpr auto saturate = [](size_t value) -> int {
if (value > static_cast<size_t>(BAN::numeric_limits<int>::max()))
return BAN::numeric_limits<int>::max();
return value;
};
const auto info = mallinfo2();
return {
.arena = saturate(info.arena),
.ordblks = saturate(info.ordblks),
.smblks = saturate(info.smblks),
.hblks = saturate(info.hblks),
.hblkhd = saturate(info.hblkhd),
.usmblks = saturate(info.usmblks),
.fsmblks = saturate(info.fsmblks),
.uordblks = saturate(info.uordblks),
.fordblks = saturate(info.fordblks),
.keepcost = saturate(info.keepcost),
};
}
struct mallinfo2 mallinfo2(void)
{
struct mallinfo2 info {};
pthread_mutex_lock(&s_allocator_lock);
info.arena = s_allocator_count * s_allocator_size;
for (size_t i = 0; i < s_allocator_count; i++)
{
const size_t total_mem = s_allocators[i].total_chunks * s_allocator_chunk_size;
const size_t free_mem = s_allocators[i].free_chunks * s_allocator_chunk_size;
info.fordblks += free_mem;
info.uordblks += total_mem - free_mem;
}
pthread_mutex_unlock(&s_allocator_lock);
info.hblks = s_mmap_count.load();
info.hblkhd = s_mmap_bytes.load();
info.uordblks += info.hblkhd;
return info;
}