blob: b376b7c88e89e572d60a5af07b1c197041b4865c (
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/* ========================================
Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
======================================== */
#include <KernelKit/KPC.h>
#include <KernelKit/ProcessScheduler.h>
#include <NetworkKit/IPC.h>
namespace Kernel {
/***********************************************************************************/
/// @internal internal use for IPC system only.
/// @brief The internal sanitize function.
/***********************************************************************************/
Bool ipc_int_sanitize_packet(IPC_MSG* pckt) {
auto endian = RTL_ENDIAN(pckt, ((Char*) pckt)[0]);
switch (endian) {
case Endian::kEndianBig: {
if (pckt->IpcEndianess == kIPCLittleEndian) goto ipc_check_failed;
break;
}
case Endian::kEndianLittle: {
if (pckt->IpcEndianess == kIPCBigEndian) goto ipc_check_failed;
break;
}
case Endian::kEndianMixed: {
if (pckt->IpcEndianess == kIPCMixedEndian) goto ipc_check_failed;
break;
}
default:
goto ipc_check_failed;
}
if (pckt->IpcFrom == pckt->IpcTo || pckt->IpcPacketSize > kIPCMsgSize) {
goto ipc_check_failed;
}
return pckt->IpcPacketSize > 1 && pckt->IpcHeaderMagic == kIPCHeaderMagic;
ipc_check_failed:
err_local_get() = kErrorIPC;
return false;
}
/***********************************************************************************/
/// @brief Sanitize packet function
/// @retval true packet is correct.
/// @retval false packet is incorrect and process has crashed.
/***********************************************************************************/
Bool ipc_sanitize_packet(IPC_MSG* pckt) {
if (!pckt || !ipc_int_sanitize_packet(pckt)) {
return false;
}
return true;
}
/***********************************************************************************/
/// @brief Construct packet function
/// @retval true packet is correct.
/// @retval false packet is incorrect and process has crashed.
/***********************************************************************************/
Bool ipc_construct_packet(_Output IPC_MSG** pckt_in) {
// don't act if it's not even valid.
if (!pckt_in) return false;
if (!*pckt_in) *pckt_in = new IPC_MSG();
MUST_PASS(*pckt_in);
if (*pckt_in) {
const auto endianess = RTL_ENDIAN((*pckt_in), ((Char*) (*pckt_in))[0]);
(*pckt_in)->IpcHeaderMagic = kIPCHeaderMagic;
(*pckt_in)->IpcEndianess = static_cast<UInt8>(endianess);
(*pckt_in)->IpcPacketSize = sizeof(IPC_MSG);
(*pckt_in)->IpcTo.UserProcessID = 0;
(*pckt_in)->IpcTo.UserProcessTeam = 0;
(*pckt_in)->IpcFrom.UserProcessID = 0;
(*pckt_in)->IpcFrom.UserProcessTeam = 0;
(*pckt_in)->IpcLock = kIPCLockFree;
return Yes;
}
return No;
}
/***********************************************************************************/
/// @brief Pass message from **src** to **target**
/// @param src Source message.
/// @param target Target message.
/***********************************************************************************/
Bool IPC_MSG::Pass(IPC_MSG* src, IPC_MSG* target) noexcept {
if (src && target && (target != src)) {
if (src->IpcMsgSz > target->IpcMsgSz) return No;
if (target->IpcMsgSz > src->IpcMsgSz) return No;
auto timeout = 0U;
const auto kLimitTimeout = 1000000U;
while ((target->IpcLock % kIPCLockUsed) != 0) {
if (timeout > kLimitTimeout) {
return No;
}
}
++target->IpcLock;
rt_copy_memory_safe(src->IpcData, target->IpcData, src->IpcMsgSz, kIPCMsgSize);
--target->IpcLock;
return Yes;
}
return No;
}
} // namespace Kernel
|