kernel/abi/linux/riscv64/
errno.rs

1//! Linux errno constants for RISC-V 64-bit ABI
2//!
3//! This module defines the standard Linux error numbers used by system calls.
4//! These values match the Linux kernel's errno definitions for RISC-V architecture.
5
6/// Success (no error)
7pub const SUCCESS: usize = 0;
8
9/// Operation not permitted
10pub const EPERM: usize = 1;
11
12/// No such file or directory  
13pub const ENOENT: usize = 2;
14
15/// No such process
16pub const ESRCH: usize = 3;
17
18/// Interrupted system call
19pub const EINTR: usize = 4;
20
21/// I/O error
22pub const EIO: usize = 5;
23
24/// No such device or address
25pub const ENXIO: usize = 6;
26
27/// Argument list too long
28pub const E2BIG: usize = 7;
29
30/// Exec format error
31pub const ENOEXEC: usize = 8;
32
33/// Bad file number
34pub const EBADF: usize = 9;
35
36/// No child processes
37pub const ECHILD: usize = 10;
38
39/// Try again
40pub const EAGAIN: usize = 11;
41
42/// Out of memory
43pub const ENOMEM: usize = 12;
44
45/// Permission denied
46pub const EACCES: usize = 13;
47
48/// Bad address
49pub const EFAULT: usize = 14;
50
51/// Block device required
52pub const ENOTBLK: usize = 15;
53
54/// Device or resource busy
55pub const EBUSY: usize = 16;
56
57/// File exists
58pub const EEXIST: usize = 17;
59
60/// Cross-device link
61pub const EXDEV: usize = 18;
62
63/// No such device
64pub const ENODEV: usize = 19;
65
66/// Not a directory
67pub const ENOTDIR: usize = 20;
68
69/// Is a directory
70pub const EISDIR: usize = 21;
71
72/// Invalid argument
73pub const EINVAL: usize = 22;
74
75/// File table overflow
76pub const ENFILE: usize = 23;
77
78/// Too many open files
79pub const EMFILE: usize = 24;
80
81/// Not a typewriter
82pub const ENOTTY: usize = 25;
83
84/// Text file busy
85pub const ETXTBSY: usize = 26;
86
87/// File too large
88pub const EFBIG: usize = 27;
89
90/// No space left on device
91pub const ENOSPC: usize = 28;
92
93/// Illegal seek
94pub const ESPIPE: usize = 29;
95
96/// Read-only file system
97pub const EROFS: usize = 30;
98
99/// Too many links
100pub const EMLINK: usize = 31;
101
102/// Broken pipe
103pub const EPIPE: usize = 32;
104
105/// Math argument out of domain of func
106pub const EDOM: usize = 33;
107
108/// Math result not representable
109pub const ERANGE: usize = 34;
110
111/// Resource deadlock would occur
112pub const EDEADLK: usize = 35;
113
114/// File name too long
115pub const ENAMETOOLONG: usize = 36;
116
117/// No record locks available
118pub const ENOLCK: usize = 37;
119
120/// Function not implemented
121pub const ENOSYS: usize = 38;
122
123/// Directory not empty
124pub const ENOTEMPTY: usize = 39;
125
126/// Too many symbolic links encountered
127pub const ELOOP: usize = 40;
128
129/// Operation would block (same as EAGAIN)
130pub const EWOULDBLOCK: usize = EAGAIN;
131
132/// No message of desired type
133pub const ENOMSG: usize = 42;
134
135/// Identifier removed
136pub const EIDRM: usize = 43;
137
138/// Channel number out of range
139pub const ECHRNG: usize = 44;
140
141/// Level 2 not synchronized
142pub const EL2NSYNC: usize = 45;
143
144/// Level 3 halted
145pub const EL3HLT: usize = 46;
146
147/// Level 3 reset
148pub const EL3RST: usize = 47;
149
150/// Link number out of range
151pub const ELNRNG: usize = 48;
152
153/// Protocol driver not attached
154pub const EUNATCH: usize = 49;
155
156/// No CSI structure available
157pub const ENOCSI: usize = 50;
158
159/// Level 2 halted
160pub const EL2HLT: usize = 51;
161
162/// Invalid exchange
163pub const EBADE: usize = 52;
164
165/// Invalid request descriptor
166pub const EBADR: usize = 53;
167
168/// Exchange full
169pub const EXFULL: usize = 54;
170
171/// No anode
172pub const ENOANO: usize = 55;
173
174/// Invalid request code
175pub const EBADRQC: usize = 56;
176
177/// Invalid slot
178pub const EBADSLT: usize = 57;
179
180/// Resource deadlock would occur (same as EDEADLK)
181pub const EDEADLOCK: usize = EDEADLK;
182
183/// Bad font file format
184pub const EBFONT: usize = 59;
185
186/// Device not a stream
187pub const ENOSTR: usize = 60;
188
189/// No data available
190pub const ENODATA: usize = 61;
191
192/// Timer expired
193pub const ETIME: usize = 62;
194
195/// Out of streams resources
196pub const ENOSR: usize = 63;
197
198/// Machine is not on the network
199pub const ENONET: usize = 64;
200
201/// Package not installed
202pub const ENOPKG: usize = 65;
203
204/// Object is remote
205pub const EREMOTE: usize = 66;
206
207/// Link has been severed
208pub const ENOLINK: usize = 67;
209
210/// Advertise error
211pub const EADV: usize = 68;
212
213/// Srmount error
214pub const ESRMNT: usize = 69;
215
216/// Communication error on send
217pub const ECOMM: usize = 70;
218
219/// Protocol error
220pub const EPROTO: usize = 71;
221
222/// Multihop attempted
223pub const EMULTIHOP: usize = 72;
224
225/// RFS specific error
226pub const EDOTDOT: usize = 73;
227
228/// Not a data message
229pub const EBADMSG: usize = 74;
230
231/// Value too large for defined data type
232pub const EOVERFLOW: usize = 75;
233
234/// Name not unique on network
235pub const ENOTUNIQ: usize = 76;
236
237/// File descriptor in bad state
238pub const EBADFD: usize = 77;
239
240/// Remote address changed
241pub const EREMCHG: usize = 78;
242
243/// Can not access a needed shared library
244pub const ELIBACC: usize = 79;
245
246/// Accessing a corrupted shared library
247pub const ELIBBAD: usize = 80;
248
249/// .lib section in a.out corrupted
250pub const ELIBSCN: usize = 81;
251
252/// Attempting to link in too many shared libraries
253pub const ELIBMAX: usize = 82;
254
255/// Cannot exec a shared library directly
256pub const ELIBEXEC: usize = 83;
257
258/// Illegal byte sequence
259pub const EILSEQ: usize = 84;
260
261/// Interrupted system call should be restarted
262pub const ERESTART: usize = 85;
263
264/// Streams pipe error
265pub const ESTRPIPE: usize = 86;
266
267/// Too many users
268pub const EUSERS: usize = 87;
269
270/// Socket operation on non-socket
271pub const ENOTSOCK: usize = 88;
272
273/// Destination address required
274pub const EDESTADDRREQ: usize = 89;
275
276/// Message too long
277pub const EMSGSIZE: usize = 90;
278
279/// Protocol wrong type for socket
280pub const EPROTOTYPE: usize = 91;
281
282/// Protocol not available
283pub const ENOPROTOOPT: usize = 92;
284
285/// Protocol not supported
286pub const EPROTONOSUPPORT: usize = 93;
287
288/// Socket type not supported
289pub const ESOCKTNOSUPPORT: usize = 94;
290
291/// Operation not supported on transport endpoint
292pub const EOPNOTSUPP: usize = 95;
293
294/// Protocol family not supported
295pub const EPFNOSUPPORT: usize = 96;
296
297/// Address family not supported by protocol
298pub const EAFNOSUPPORT: usize = 97;
299
300/// Address already in use
301pub const EADDRINUSE: usize = 98;
302
303/// Cannot assign requested address
304pub const EADDRNOTAVAIL: usize = 99;
305
306/// Network is down
307pub const ENETDOWN: usize = 100;
308
309/// Network is unreachable
310pub const ENETUNREACH: usize = 101;
311
312/// Network dropped connection because of reset
313pub const ENETRESET: usize = 102;
314
315/// Software caused connection abort
316pub const ECONNABORTED: usize = 103;
317
318/// Connection reset by peer
319pub const ECONNRESET: usize = 104;
320
321/// No buffer space available
322pub const ENOBUFS: usize = 105;
323
324/// Transport endpoint is already connected
325pub const EISCONN: usize = 106;
326
327/// Transport endpoint is not connected
328pub const ENOTCONN: usize = 107;
329
330/// Cannot send after transport endpoint shutdown
331pub const ESHUTDOWN: usize = 108;
332
333/// Too many references: cannot splice
334pub const ETOOMANYREFS: usize = 109;
335
336/// Connection timed out
337pub const ETIMEDOUT: usize = 110;
338
339/// Connection refused
340pub const ECONNREFUSED: usize = 111;
341
342/// Host is down
343pub const EHOSTDOWN: usize = 112;
344
345/// No route to host
346pub const EHOSTUNREACH: usize = 113;
347
348/// Operation already in progress
349pub const EALREADY: usize = 114;
350
351/// Operation now in progress
352pub const EINPROGRESS: usize = 115;
353
354/// Stale file handle
355pub const ESTALE: usize = 116;
356
357/// Structure needs cleaning
358pub const EUCLEAN: usize = 117;
359
360/// Not a XENIX named type file
361pub const ENOTNAM: usize = 118;
362
363/// No XENIX semaphores available
364pub const ENAVAIL: usize = 119;
365
366/// Is a named type file
367pub const EISNAM: usize = 120;
368
369/// Remote I/O error
370pub const EREMOTEIO: usize = 121;
371
372/// Quota exceeded
373pub const EDQUOT: usize = 122;
374
375/// No medium found
376pub const ENOMEDIUM: usize = 123;
377
378/// Wrong medium type
379pub const EMEDIUMTYPE: usize = 124;
380
381/// Operation Canceled
382pub const ECANCELED: usize = 125;
383
384/// Required key not available
385pub const ENOKEY: usize = 126;
386
387/// Key has expired
388pub const EKEYEXPIRED: usize = 127;
389
390/// Key has been revoked
391pub const EKEYREVOKED: usize = 128;
392
393/// Key was rejected by service
394pub const EKEYREJECTED: usize = 129;
395
396/// Owner died
397pub const EOWNERDEAD: usize = 130;
398
399/// State not recoverable
400pub const ENOTRECOVERABLE: usize = 131;
401
402/// Operation not possible due to RF-kill
403pub const ERFKILL: usize = 132;
404
405/// Memory page has hardware error
406pub const EHWPOISON: usize = 133;
407
408/// Helper function to convert FileSystemErrorKind to Linux errno
409pub fn from_fs_error(error: &crate::fs::FileSystemError) -> usize {
410    use crate::fs::FileSystemErrorKind;
411
412    match error.kind {
413        FileSystemErrorKind::NotFound => ENOENT,
414        FileSystemErrorKind::PermissionDenied => EACCES,
415        FileSystemErrorKind::FileExists => EEXIST,
416        FileSystemErrorKind::AlreadyExists => EEXIST,
417        FileSystemErrorKind::NotADirectory => ENOTDIR,
418        FileSystemErrorKind::IsADirectory => EISDIR,
419        FileSystemErrorKind::NotAFile => EISDIR,
420        FileSystemErrorKind::DirectoryNotEmpty => ENOTEMPTY,
421        FileSystemErrorKind::InvalidPath => EINVAL,
422        FileSystemErrorKind::InvalidOperation => EPERM,
423        FileSystemErrorKind::CrossDevice => EXDEV,
424        FileSystemErrorKind::NoSpace => ENOSPC,
425        FileSystemErrorKind::ReadOnly => EROFS,
426        FileSystemErrorKind::IoError => EIO,
427        FileSystemErrorKind::DeviceError => EIO,
428        FileSystemErrorKind::InvalidData => EINVAL,
429        FileSystemErrorKind::NotSupported => ENOSYS,
430        FileSystemErrorKind::BrokenFileSystem => EIO,
431        FileSystemErrorKind::Busy => EBUSY,
432    }
433}
434
435/// Convert any error to Linux errno, defaulting to EIO for unknown errors
436pub fn from_error<E>(_error: E) -> usize {
437    EIO // Generic I/O error for unknown error types
438}
439
440/// Convert errno to negative value as required by Linux system calls
441/// Linux system calls return negative errno values on error
442pub fn to_result(errno_val: usize) -> usize {
443    if errno_val == SUCCESS {
444        SUCCESS
445    } else {
446        // Convert positive errno to negative value
447        // Using two's complement: -errno = !errno + 1
448        // But in usize, we use wrapping_neg() which is equivalent to (usize::MAX - errno + 1)
449        errno_val.wrapping_neg()
450    }
451}