The Pedigree Project 0.1
HostedPlatform.h
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted.
6 */
7
8#ifndef PEDIGREE_KERNEL_PROCESSOR_HOSTED_PLATFORM_H
9#define PEDIGREE_KERNEL_PROCESSOR_HOSTED_PLATFORM_H
10#include <config.h>
11#include <cstddef>
12#include <cstdint>
13#include <ucontext.h>
14#include <unistd.h>
15
16namespace HostedPlatform {
17inline size_t pageSize() {
18 const long result = sysconf(_SC_PAGESIZE);
19 return result > 0 ? static_cast<size_t>(result) : 0;
20}
21
22template <class Context>
23inline uintptr_t instructionPointer(const Context* context) {
24#if defined(__APPLE__) && defined(__aarch64__)
25 return context->uc_mcontext->__ss.__pc;
26#elif defined(__APPLE__) && defined(__x86_64__)
27 return context->uc_mcontext->__ss.__rip;
28#elif defined(__linux__) && defined(__x86_64__)
29 return context->uc_mcontext.gregs[REG_RIP];
30#else
31#error Unsupported hosted ucontext platform
32#endif
33}
34
35template <class Context>
36inline uintptr_t stackPointer(const Context* context) {
37#if defined(__APPLE__) && defined(__aarch64__)
38 return context->uc_mcontext->__ss.__sp;
39#elif defined(__APPLE__) && defined(__x86_64__)
40 return context->uc_mcontext->__ss.__rsp;
41#elif defined(__linux__) && defined(__x86_64__)
42 return context->uc_mcontext.gregs[REG_RSP];
43#else
44#error Unsupported hosted ucontext platform
45#endif
46}
47
48template <class Context>
49inline uintptr_t basePointer(const Context* context) {
50#if defined(__APPLE__) && defined(__aarch64__)
51 return context->uc_mcontext->__ss.__fp;
52#elif defined(__APPLE__) && defined(__x86_64__)
53 return context->uc_mcontext->__ss.__rbp;
54#elif defined(__linux__) && defined(__x86_64__)
55 return context->uc_mcontext.gregs[REG_RBP];
56#else
57#error Unsupported hosted ucontext platform
58#endif
59}
60} // namespace HostedPlatform
61
62#endif