summaryrefslogtreecommitdiffhomepage
path: root/Kernel/ArchKit/ArchKit.hpp
blob: 639a3f047667455204e9f666bf06d2860764af6d (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
/* -------------------------------------------

	Copyright Zeta Electronics Corporation

------------------------------------------- */

#pragma once

#include <NewKit/Array.hpp>
#include <NewKit/Defines.hpp>
#include <NewKit/Function.hpp>

#ifdef __NEWOS_AMD64__
#include <HALKit/AMD64/HalPageAlloc.hpp>
#include <HALKit/AMD64/Hypervisor.hpp>
#include <HALKit/AMD64/Processor.hpp>
#elif defined(__NEWOS_PPC__)
#include <HALKit/POWER/Processor.hpp>
#elif defined(__NEWOS_ARM64__)
#include <HALKit/ARM64/Processor.hxx>
#else
#error !!! unknown architecture !!!
#endif

namespace NewOS
{
	constexpr static inline SSizeT rt_hash_seed(const char* seed, int mul)
	{
		SSizeT 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.
	inline void ke_dma_write(UInt32 base, UInt32 reg, UInt32 value) noexcept
	{
		*(volatile UInt32*)((UInt64)base + reg) = value;
	}

	/// @brief read from mapped memory register.
	/// @param base base address
	/// @param reg the register.
	/// @return the value inside the register.
	inline UInt32 ke_dma_read(UInt32 base, UInt32 reg) noexcept
	{
		return *(volatile UInt32*)((UInt64)base + reg);
	}

	/// @brief Print a region of memory.
	/// @param start
	/// @param length
	inline void ke_print_raw_memory(const void* start, Size length)
	{
		const UInt8* ptr = (const UInt8*)start;
		for (Size i = 0; i < length; i++)
		{
			if (i % 16 == 0)
			{
				kcout << hex_number((UIntPtr)ptr + i);
			}
			else
			{
				kcout << hex_number(ptr[i]);
			}

			kcout << " ";
		}

		kcout << "\r";
	}
} // namespace NewOS

#define kKernelMaxSystemCalls (256)

typedef NewOS::Void (*rt_syscall_proc)(NewOS::VoidPtr);

struct RTSyscallInfoHdr final
{
	NewOS::Int64	fHash;
	NewOS::Bool		fHooked;
	rt_syscall_proc fProc;
};

inline NewOS::Array<RTSyscallInfoHdr,
					kKernelMaxSystemCalls>
	kSyscalls;

inline NewOS::Array<RTSyscallInfoHdr,
					kKernelMaxSystemCalls>
	kKerncalls;

EXTERN_C NewOS::HAL::StackFramePtr rt_get_current_context();
EXTERN_C NewOS::Void rt_do_context_switch(NewOS::HAL::StackFramePtr stackFrame);

#include <FirmwareKit/Handover.hxx>