2023-07-04 18:19:17 +02:00
|
|
|
#include "orbis/KernelContext.hpp"
|
|
|
|
|
#include "orbis/thread/Process.hpp"
|
2023-07-20 17:18:12 +02:00
|
|
|
#include "orbis/thread/ProcessOps.hpp"
|
2023-07-16 17:31:12 +02:00
|
|
|
#include "orbis/utils/Logs.hpp"
|
2023-07-04 18:19:17 +02:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <sys/unistd.h>
|
|
|
|
|
|
|
|
|
|
namespace orbis {
|
2023-10-31 12:22:22 +01:00
|
|
|
thread_local Thread *g_currentThread;
|
|
|
|
|
|
2023-07-04 18:19:17 +02:00
|
|
|
KernelContext &g_context = *[]() -> KernelContext * {
|
|
|
|
|
// Allocate global shared kernel memory
|
|
|
|
|
// TODO: randomize for hardening and reduce size
|
|
|
|
|
auto ptr = mmap(reinterpret_cast<void *>(0x200'0000'0000), 0x1'0000'0000,
|
2023-10-31 12:22:22 +01:00
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
|
MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
|
2023-10-31 19:28:40 +01:00
|
|
|
if (ptr == MAP_FAILED)
|
2023-07-04 18:19:17 +02:00
|
|
|
std::abort();
|
|
|
|
|
|
|
|
|
|
return new (ptr) KernelContext;
|
|
|
|
|
}();
|
|
|
|
|
|
2023-07-05 10:38:31 +02:00
|
|
|
KernelContext::KernelContext() {
|
2023-07-06 21:10:15 +02:00
|
|
|
// Initialize recursive heap mutex
|
|
|
|
|
pthread_mutexattr_t mtx_attr;
|
|
|
|
|
pthread_mutexattr_init(&mtx_attr);
|
|
|
|
|
pthread_mutexattr_settype(&mtx_attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
|
pthread_mutexattr_setpshared(&mtx_attr, PTHREAD_PROCESS_SHARED);
|
|
|
|
|
pthread_mutex_init(&m_heap_mtx, &mtx_attr);
|
|
|
|
|
pthread_mutexattr_destroy(&mtx_attr);
|
|
|
|
|
|
2023-11-10 21:57:20 +01:00
|
|
|
// std::printf("orbis::KernelContext initialized, addr=%p\n", this);
|
|
|
|
|
// std::printf("TSC frequency: %lu\n", getTscFreq());
|
2023-07-05 10:38:31 +02:00
|
|
|
}
|
2023-07-04 18:19:17 +02:00
|
|
|
KernelContext::~KernelContext() {}
|
|
|
|
|
|
|
|
|
|
Process *KernelContext::createProcess(pid_t pid) {
|
|
|
|
|
auto newProcess = knew<utils::LinkedNode<Process>>();
|
|
|
|
|
newProcess->object.context = this;
|
|
|
|
|
newProcess->object.pid = pid;
|
|
|
|
|
newProcess->object.state = ProcessState::NEW;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard lock(m_proc_mtx);
|
|
|
|
|
if (m_processes != nullptr) {
|
|
|
|
|
m_processes->insertPrev(*newProcess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_processes = newProcess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &newProcess->object;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KernelContext::deleteProcess(Process *proc) {
|
|
|
|
|
auto procNode = reinterpret_cast<utils::LinkedNode<Process> *>(proc);
|
|
|
|
|
auto pid = proc->pid;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard lock(m_proc_mtx);
|
|
|
|
|
auto next = procNode->erase();
|
|
|
|
|
|
|
|
|
|
if (procNode == m_processes) {
|
|
|
|
|
m_processes = next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kdelete(procNode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Process *KernelContext::findProcessById(pid_t pid) const {
|
|
|
|
|
std::lock_guard lock(m_proc_mtx);
|
|
|
|
|
for (auto proc = m_processes; proc != nullptr; proc = proc->next) {
|
|
|
|
|
if (proc->object.pid == pid) {
|
|
|
|
|
return &proc->object;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-15 09:15:32 +02:00
|
|
|
long KernelContext::getTscFreq() {
|
|
|
|
|
auto cal_tsc = []() -> long {
|
|
|
|
|
const long timer_freq = 1'000'000'000;
|
|
|
|
|
|
|
|
|
|
// Calibrate TSC
|
|
|
|
|
constexpr int samples = 40;
|
|
|
|
|
long rdtsc_data[samples];
|
|
|
|
|
long timer_data[samples];
|
|
|
|
|
long error_data[samples];
|
|
|
|
|
|
2023-07-20 17:18:12 +02:00
|
|
|
struct ::timespec ts0;
|
2023-07-15 09:15:32 +02:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts0);
|
|
|
|
|
long sec_base = ts0.tv_sec;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < samples; i++) {
|
|
|
|
|
usleep(200);
|
|
|
|
|
error_data[i] = (__builtin_ia32_lfence(), __builtin_ia32_rdtsc());
|
2023-07-20 17:18:12 +02:00
|
|
|
struct ::timespec ts;
|
2023-07-15 09:15:32 +02:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
|
rdtsc_data[i] = (__builtin_ia32_lfence(), __builtin_ia32_rdtsc());
|
|
|
|
|
timer_data[i] = ts.tv_nsec + (ts.tv_sec - sec_base) * 1'000'000'000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compute average TSC
|
|
|
|
|
long acc = 0;
|
|
|
|
|
for (int i = 0; i < samples - 1; i++) {
|
|
|
|
|
acc += (rdtsc_data[i + 1] - rdtsc_data[i]) * timer_freq /
|
|
|
|
|
(timer_data[i + 1] - timer_data[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rounding
|
|
|
|
|
acc /= (samples - 1);
|
|
|
|
|
constexpr long grain = 1'000'000;
|
|
|
|
|
return grain * (acc / grain + long{(acc % grain) > (grain / 2)});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
long freq = m_tsc_freq.load();
|
|
|
|
|
if (freq)
|
|
|
|
|
return freq;
|
|
|
|
|
m_tsc_freq.compare_exchange_strong(freq, cal_tsc());
|
|
|
|
|
return m_tsc_freq.load();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-04 18:19:17 +02:00
|
|
|
void *KernelContext::kalloc(std::size_t size, std::size_t align) {
|
2023-07-06 21:32:35 +02:00
|
|
|
size = (size + (__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1)) &
|
|
|
|
|
~(__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1);
|
|
|
|
|
if (!size)
|
|
|
|
|
std::abort();
|
|
|
|
|
|
2023-07-06 21:10:15 +02:00
|
|
|
pthread_mutex_lock(&m_heap_mtx);
|
|
|
|
|
if (!m_heap_is_freeing) {
|
|
|
|
|
// Try to reuse previously freed block
|
|
|
|
|
for (auto [it, end] = m_free_heap.equal_range(size); it != end; it++) {
|
|
|
|
|
auto result = it->second;
|
|
|
|
|
if (!(reinterpret_cast<std::uintptr_t>(result) & (align - 1))) {
|
2023-07-10 04:19:21 +02:00
|
|
|
auto node = m_free_heap.extract(it);
|
|
|
|
|
node.key() = 0;
|
|
|
|
|
node.mapped() = nullptr;
|
|
|
|
|
m_used_node.insert(m_used_node.begin(), std::move(node));
|
2023-07-06 21:10:15 +02:00
|
|
|
pthread_mutex_unlock(&m_heap_mtx);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
align = std::max<std::size_t>(align, __STDCPP_DEFAULT_NEW_ALIGNMENT__);
|
|
|
|
|
auto heap = reinterpret_cast<std::uintptr_t>(m_heap_next);
|
2023-07-04 18:19:17 +02:00
|
|
|
heap = (heap + (align - 1)) & ~(align - 1);
|
|
|
|
|
auto result = reinterpret_cast<void *>(heap);
|
2023-07-06 21:10:15 +02:00
|
|
|
m_heap_next = reinterpret_cast<void *>(heap + size);
|
2023-07-13 15:05:37 +02:00
|
|
|
// Check overflow
|
|
|
|
|
if (heap + size < heap)
|
|
|
|
|
std::abort();
|
|
|
|
|
if (heap + size > (uintptr_t)&g_context + 0x1'0000'0000)
|
|
|
|
|
std::abort();
|
2023-07-06 21:10:15 +02:00
|
|
|
pthread_mutex_unlock(&m_heap_mtx);
|
2023-07-04 18:19:17 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KernelContext::kfree(void *ptr, std::size_t size) {
|
2023-07-06 21:32:35 +02:00
|
|
|
size = (size + (__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1)) &
|
|
|
|
|
~(__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1);
|
2023-07-04 18:19:17 +02:00
|
|
|
if (!size)
|
|
|
|
|
std::abort();
|
2023-10-31 19:28:40 +01:00
|
|
|
if ((uintptr_t)ptr == 0x2000001a2b0) {
|
|
|
|
|
std::fprintf(stderr, "free %p-%p (%zu)\n", ptr, (char *)ptr + size, size);
|
|
|
|
|
}
|
2023-07-16 13:53:35 +02:00
|
|
|
std::memset(ptr, 0xcc, size);
|
2023-07-06 21:32:35 +02:00
|
|
|
|
|
|
|
|
pthread_mutex_lock(&m_heap_mtx);
|
2023-07-06 21:10:15 +02:00
|
|
|
if (m_heap_is_freeing)
|
|
|
|
|
std::abort();
|
|
|
|
|
m_heap_is_freeing = true;
|
|
|
|
|
if (!m_used_node.empty()) {
|
|
|
|
|
auto node = m_used_node.extract(m_used_node.begin());
|
|
|
|
|
node.key() = size;
|
|
|
|
|
node.mapped() = ptr;
|
|
|
|
|
m_free_heap.insert(std::move(node));
|
|
|
|
|
} else {
|
|
|
|
|
m_free_heap.emplace(size, ptr);
|
|
|
|
|
}
|
|
|
|
|
m_heap_is_freeing = false;
|
|
|
|
|
pthread_mutex_unlock(&m_heap_mtx);
|
2023-07-04 18:19:17 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-22 11:03:46 +02:00
|
|
|
std::tuple<UmtxChain &, UmtxKey, std::unique_lock<shared_mutex>>
|
|
|
|
|
KernelContext::getUmtxChainIndexed(int i, Thread *t, uint32_t flags,
|
|
|
|
|
void *ptr) {
|
|
|
|
|
auto pid = t->tproc->pid;
|
2023-11-13 19:36:25 +01:00
|
|
|
auto p = reinterpret_cast<std::uintptr_t>(ptr);
|
2023-07-22 11:03:46 +02:00
|
|
|
if (flags & 1) {
|
|
|
|
|
pid = 0; // Process shared (TODO)
|
2023-11-13 19:36:25 +01:00
|
|
|
ORBIS_LOG_WARNING("Using process-shared umtx", t->tid, ptr, (p % 0x4000));
|
2023-07-22 11:03:46 +02:00
|
|
|
}
|
|
|
|
|
auto n = p + pid;
|
|
|
|
|
if (flags & 1)
|
|
|
|
|
n %= 0x4000;
|
|
|
|
|
n = ((n * c_golden_ratio_prime) >> c_umtx_shifts) % c_umtx_chains;
|
|
|
|
|
std::unique_lock lock(m_umtx_chains[i][n].mtx);
|
|
|
|
|
return {m_umtx_chains[i][n], UmtxKey{p, pid}, std::move(lock)};
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-04 18:19:17 +02:00
|
|
|
inline namespace utils {
|
2023-07-06 21:32:35 +02:00
|
|
|
void kfree(void *ptr, std::size_t size) { return g_context.kfree(ptr, size); }
|
2023-07-04 18:19:17 +02:00
|
|
|
void *kalloc(std::size_t size, std::size_t align) {
|
2023-07-06 21:10:15 +02:00
|
|
|
return g_context.kalloc(size, align);
|
2023-07-04 18:19:17 +02:00
|
|
|
}
|
|
|
|
|
} // namespace utils
|
2023-07-16 17:31:12 +02:00
|
|
|
|
|
|
|
|
inline namespace logs {
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<kstring>::format(std::string &out, const void *arg) {
|
|
|
|
|
out += get_object(arg);
|
|
|
|
|
}
|
|
|
|
|
} // namespace logs
|
2023-07-20 17:18:12 +02:00
|
|
|
|
|
|
|
|
void Thread::where() { tproc->ops->where(this); }
|
2023-07-04 18:19:17 +02:00
|
|
|
} // namespace orbis
|