SimpleKernel 1.17.0
Loading...
Searching...
No Matches
expected.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstdint>
8#include <expected>
9
11enum class ErrorCode : uint64_t {
12 kSuccess = 0,
13 // ELF 相关错误 (0x100 - 0x1FF)
14 kElfInvalidAddress = 0x100,
15 kElfInvalidMagic = 0x101,
17 kElfInvalidClass = 0x103,
18 kElfSymtabNotFound = 0x104,
19 kElfStrtabNotFound = 0x105,
20 // FDT 相关错误 (0x200 - 0x2FF)
21 kFdtInvalidAddress = 0x200,
22 kFdtInvalidHeader = 0x201,
23 kFdtNodeNotFound = 0x202,
25 kFdtParseFailed = 0x204,
27 // SpinLock 相关错误 (0x300 - 0x3FF)
29 kSpinLockNotOwned = 0x301,
30 // Mutex 相关错误 (0x380 - 0x3FF)
31 kMutexNoTaskContext = 0x380,
32 kMutexRecursiveLock = 0x381,
33 kMutexNotOwned = 0x382,
34 kMutexNotLocked = 0x383,
35 // VirtualMemory 相关错误 (0x400 - 0x4FF)
36 kVmAllocationFailed = 0x400,
37 kVmMapFailed = 0x401,
38 kVmUnmapFailed = 0x402,
39 kVmInvalidPageTable = 0x403,
40 kVmPageNotMapped = 0x404,
41 // IPI 相关错误 (0x500 - 0x5FF)
43 kIpiSendFailed = 0x501,
44 // APIC 相关错误 (0x600 - 0x6FF)
45 kApicInitFailed = 0x600,
46 kApicInvalidIrq = 0x601,
48 kApicCodeCopyFailed = 0x603,
51 kApicIpiTimeout = 0x606,
52 // Task 相关错误 (0x700 - 0x7FF)
53 kTaskNoCurrentTask = 0x700,
59 kTaskNoChildFound = 0x706,
60 kTaskInvalidPid = 0x707,
61 // Device 相关错误 (0x800 - 0x8FF)
62 kDeviceNotFound = 0x800,
63 kDeviceAlreadyOpen = 0x801,
64 kDeviceNotOpen = 0x802,
65 kDeviceReadFailed = 0x803,
66 kDeviceWriteFailed = 0x804,
67 kDeviceIoctlFailed = 0x805,
68 kDeviceMmapFailed = 0x806,
69 kDeviceNotSupported = 0x807,
70 kDeviceBusy = 0x808,
75 kDeviceFlushFailed = 0x80D,
77 kDeviceError = 0x80E,
79 kIoError = 0x80F,
81 kNotSupported = 0x810,
83 kTimeout = 0x811,
84 // VirtIO 传输层错误 (0x820 - 0x82F)
86 kInvalidMagic = 0x820,
88 kInvalidVersion = 0x821,
90 kInvalidDeviceId = 0x822,
95 // VirtIO 虚拟队列错误 (0x830 - 0x83F)
97 kQueueNotAvailable = 0x830,
99 kQueueAlreadyUsed = 0x831,
101 kQueueTooLarge = 0x832,
103 kNoFreeDescriptors = 0x833,
105 kInvalidDescriptor = 0x834,
107 kNoUsedBuffers = 0x835,
108 // 文件系统相关错误 (0xA00 - 0xAFF)
109 kFsFileNotFound = 0xA00,
110 kFsPermissionDenied = 0xA01,
111 kFsNotADirectory = 0xA02,
112 kFsIsADirectory = 0xA03,
113 kFsFileExists = 0xA04,
114 kFsNoSpace = 0xA05,
115 kFsMountFailed = 0xA06,
116 kFsUnmountFailed = 0xA07,
117 kFsInvalidPath = 0xA08,
118 kFsFdTableFull = 0xA09,
119 kFsInvalidFd = 0xA0A,
120 kFsNotMounted = 0xA0B,
121 kFsReadOnly = 0xA0C,
122 kFsCorrupted = 0xA0D,
123 kFsAlreadyMounted = 0xA0E,
124 kFsNotEmpty = 0xA0F,
125 // BlockDevice 相关错误 (0xB00 - 0xBFF)
126 kBlkDeviceNotFound = 0xB00,
127 kBlkReadFailed = 0xB01,
128 kBlkWriteFailed = 0xB02,
129 kBlkSectorOutOfRange = 0xB03,
130 // IrqChip 相关错误 (0x900 - 0x9FF)
131 kIrqChipInvalidIrq = 0x900,
132 kIrqChipIrqNotEnabled = 0x901,
134 kIrqChipIpiTimeout = 0x903,
135 // 通用错误 (0xF00 - 0xFFF)
136 kInvalidArgument = 0xF00,
137 kOutOfMemory = 0xF01,
138};
139
141constexpr auto GetErrorMessage(ErrorCode code) -> const char* {
142 switch (code) {
144 return "Success";
146 return "Invalid ELF address";
148 return "Invalid ELF magic number";
150 return "32-bit ELF not supported";
152 return "Invalid ELF class";
154 return ".symtab section not found";
156 return ".strtab section not found";
158 return "Invalid FDT address";
160 return "Invalid FDT header";
162 return "FDT node not found";
164 return "FDT property not found";
166 return "FDT parse failed";
168 return "Invalid FDT property size";
170 return "Recursive spinlock detected";
172 return "Spinlock not owned by current core";
174 return "Mutex operation outside task context";
176 return "Recursive mutex lock detected";
178 return "Mutex not owned by current task";
180 return "Mutex not locked";
182 return "Virtual memory allocation failed";
184 return "Virtual memory mapping failed";
186 return "Virtual memory unmapping failed";
188 return "Invalid page table";
190 return "Page not mapped";
192 return "IPI target CPU mask out of range";
194 return "IPI send failed";
196 return "APIC initialization failed";
198 return "Invalid IRQ number";
200 return "Invalid APIC parameter";
202 return "AP code copy verification failed";
204 return "Address not aligned to required boundary";
206 return "Address out of valid range";
208 return "IPI delivery timeout";
210 return "No current task";
212 return "PID allocation failed";
214 return "Task allocation failed";
216 return "Invalid clone flags";
218 return "Page table clone failed";
220 return "Kernel stack allocation failed";
222 return "No child process found";
224 return "Invalid PID";
226 return "Device not found";
228 return "Device already open";
230 return "Device not open";
232 return "Device read failed";
234 return "Device write failed";
236 return "Device ioctl failed";
238 return "Device mmap failed";
240 return "Operation not supported by device";
242 return "Device busy";
244 return "Device permission denied";
246 return "Invalid device offset";
248 return "Block access not aligned to block size";
250 return "Block number out of device range";
252 return "Device flush failed";
254 return "Device reported an error";
256 return "I/O operation failed";
258 return "Operation not supported";
260 return "Operation timed out";
261 // VirtIO 传输层错误 (0x820 - 0x82F)
263 return "Invalid MMIO magic value";
265 return "Unsupported virtio version";
267 return "Invalid device ID (device does not exist)";
269 return "Transport layer not initialized";
271 return "Feature negotiation failed";
272 // VirtIO 虚拟队列错误 (0x830 - 0x83F)
274 return "Queue not available (queue_num_max == 0)";
276 return "Queue already used";
278 return "Requested queue size exceeds maximum";
280 return "No free descriptors available";
282 return "Invalid descriptor index";
284 return "No used buffers to reclaim";
286 return "File not found";
288 return "Filesystem permission denied";
290 return "Not a directory";
292 return "Is a directory";
294 return "File already exists";
296 return "No space left on device";
298 return "Mount failed";
300 return "Unmount failed";
302 return "Invalid path";
304 return "File descriptor table full";
306 return "Invalid file descriptor";
308 return "Filesystem not mounted";
310 return "Read-only filesystem";
312 return "Filesystem corrupted";
314 return "Filesystem already mounted";
316 return "Directory not empty";
318 return "Block device not found";
320 return "Block read failed";
322 return "Block write failed";
324 return "Sector out of range";
326 return "IRQ number out of controller range";
328 return "IRQ not enabled";
330 return "Failed to set IRQ CPU affinity";
332 return "IPI delivery timeout";
334 return "Invalid argument";
336 return "Out of memory";
337 default:
338 return "Unknown error";
339 }
340}
341
343struct Error {
345
346 explicit constexpr Error(ErrorCode c) : code(c) {}
347
350 Error() = default;
351 Error(const Error&) = default;
352 Error(Error&&) = default;
353 auto operator=(const Error&) -> Error& = default;
354 auto operator=(Error&&) -> Error& = default;
355 ~Error() = default;
357
358 [[nodiscard]] constexpr auto message() const -> const char* {
359 return GetErrorMessage(code);
360 }
361};
362
364template <typename T>
365using Expected = std::expected<T, Error>;
ErrorCode
内核错误码
Definition expected.hpp:11
@ kDeviceMmapFailed
@ kDeviceInvalidOffset
@ kElfStrtabNotFound
@ kElfSymtabNotFound
@ kTaskPageTableCloneFailed
@ kTaskInvalidCloneFlags
@ kVmInvalidPageTable
@ kIrqChipAffinityFailed
@ kApicAddressOutOfRange
@ kMutexNoTaskContext
@ kFsPermissionDenied
@ kVmPageNotMapped
@ kBlkDeviceNotFound
@ kFdtPropertyNotFound
@ kQueueAlreadyUsed
队列已被使用
@ kElfUnsupported32Bit
@ kFsUnmountFailed
@ kDeviceBlockUnaligned
@ kVmAllocationFailed
@ kIpiTargetOutOfRange
@ kInvalidVersion
无效的版本号
@ kBlkSectorOutOfRange
@ kTransportNotInitialized
传输层未正确初始化
@ kIrqChipInvalidIrq
@ kQueueTooLarge
请求的队列大小超过设备支持的最大值
@ kElfInvalidMagic
@ kApicCodeCopyFailed
@ kDevicePermissionDenied
@ kTimeout
操作超时
@ kFeatureNegotiationFailed
特性协商失败
@ kInvalidArgument
@ kDeviceNotSupported
@ kTaskNoCurrentTask
@ kDeviceReadFailed
@ kNoFreeDescriptors
没有空闲描述符
@ kDeviceWriteFailed
@ kDeviceAlreadyOpen
@ kIrqChipIrqNotEnabled
@ kIoError
IO 操作错误
@ kDeviceError
通用设备报告错误
@ kFdtInvalidPropertySize
@ kApicInvalidParameter
@ kInvalidDescriptor
无效的描述符索引
@ kQueueNotAvailable
队列不可用(queue_num_max == 0)
@ kTaskNoChildFound
@ kFsAlreadyMounted
@ kTaskAllocationFailed
@ kFdtNodeNotFound
@ kDeviceBlockOutOfRange
@ kElfInvalidAddress
@ kDeviceFlushFailed
@ kElfInvalidClass
@ kNoUsedBuffers
没有已使用的缓冲区可回收
@ kFdtInvalidAddress
@ kDeviceIoctlFailed
@ kInvalidMagic
无效的 MMIO 魔数
@ kNotSupported
不支持的操作(通用,非设备特定)
@ kApicAddressNotAligned
@ kMutexRecursiveLock
@ kIrqChipIpiTimeout
@ kFdtInvalidHeader
@ kSpinLockRecursiveLock
@ kSpinLockNotOwned
@ kTaskPidAllocationFailed
@ kInvalidDeviceId
无效的设备 ID(设备不存在)
@ kTaskKernelStackAllocationFailed
@ kFsNotADirectory
constexpr auto GetErrorMessage(ErrorCode code) -> const char *
获取错误码对应的错误信息
Definition expected.hpp:141
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
错误类型,用于 std::expected
Definition expected.hpp:343
ErrorCode code
Definition expected.hpp:344
constexpr Error(ErrorCode c)
Definition expected.hpp:346
Error(const Error &)=default
constexpr auto message() const -> const char *
Definition expected.hpp:358
Error(Error &&)=default
auto operator=(const Error &) -> Error &=default
~Error()=default
auto operator=(Error &&) -> Error &=default
Error()=default