summaryrefslogtreecommitdiffhomepage
path: root/src/kernel/ArchKit/ArchKit.h
blob: 6ec3a05ba89c81664b2844391f92159451bf569c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org)
// Licensed under the Apache License, Version 2.0 (see LICENSE file)
// Official repository: https://github.com/nekernel-org/nekernel

#ifndef ARCHKIT_ARCHKIT_H
#define ARCHKIT_ARCHKIT_H

#include <NeKit/Array.h>
#include <NeKit/Config.h>
#include <NeKit/Function.h>

#include <FirmwareKit/Handover.h>

#ifdef __NE_AMD64__
#include <HALKit/AMD64/Hypervisor.h>
#include <HALKit/AMD64/Paging.h>
#include <HALKit/AMD64/Processor.h>
#elif defined(__NE_POWER64__)
#include <HALKit/POWER/Processor.h>
#elif defined(__NE_ARM64__)
#include <HALKit/ARM64/Processor.h>
#elif defined(__NE_SDK__)
#include <HALKit/ARM64/Processor.h>
#else
#error !!! unknown architecture !!!
#endif

#define kMaxDispatchCallCount (512U)

namespace Kernel {
inline SizeT rt_hash_seed(const Char* seed, UInt32 mul) {
  SizeT hash = 0;

  for (SSizeT idx = 0; seed[idx] != 0; ++idx) {
    hash += seed[idx];
    hash ^= mul;
  }

  return hash;
}

/// @brief write to mapped memory register
/// @param base the base address.
/// @param reg the register.
/// @param value the write to write on it.
template <typename DataKind>
inline Void ke_dma_write(UIntPtr base, DataKind reg, DataKind value) {
  *(volatile DataKind*) (base + reg) = value;
}

/// @brief read from mapped memory register.
/// @param base base address
/// @param reg the register.
/// @return the value inside the register.
template <typename DataKind>
inline UInt32 ke_dma_read(UIntPtr base, DataKind reg) {
  return *(volatile DataKind*) (base + reg);
}

/// @brief Hardware Abstraction Layer
namespace HAL {
  /// @brief Check whether this pointer is a bitmap object.
  /// @param ptr argument to verify.
  /// @param whether successful or not.
  auto mm_is_bitmap(VoidPtr ptr) -> Bool;
}  // namespace HAL
}  // namespace Kernel

using rt_syscall_proc = Kernel::Void (*)(Kernel::VoidPtr);

/// @brief System Call Dispatch.
struct HAL_DISPATCH_ENTRY final {
  Kernel::UInt64  fHash;
  Kernel::Bool    fHooked;
  rt_syscall_proc fProc;

  BOOL IsKernCall() { return NO; }
  BOOL IsSysCall() { return YES; }

  operator bool() { return fHooked; }
};

using rt_kerncall_proc = Kernel::Void (*)(Kernel::SizeT, Kernel::VoidPtr, Kernel::SizeT);

/// @brief Kernel Call Dispatch.
struct HAL_KERNEL_DISPATCH_ENTRY final {
  Kernel::UInt64   fHash;
  Kernel::Bool     fHooked;
  rt_kerncall_proc fProc;

  BOOL IsKernCall() { return YES; }
  BOOL IsSysCall() { return NO; }

  operator bool() { return fHooked; }
};

inline Kernel::Array<HAL_DISPATCH_ENTRY, kMaxDispatchCallCount> kSysCalls;

inline Kernel::Array<HAL_KERNEL_DISPATCH_ENTRY, kMaxDispatchCallCount> kKernCalls;

#ifdef __NE_VIRTUAL_MEMORY_SUPPORT__

inline Kernel::VoidPtr kKernelVM = nullptr;

#endif  // __NE_VIRTUAL_MEMORY_SUPPORT__

inline Kernel::SizeT kBitMapCursor = 0UL;

#endif