[关闭]
@zifehng 2017-04-24T02:21:46.000000Z 字数 2470 阅读 1770

Linux内核错误码与错误指针

linux EFAULT IS_ERR


kernel version: linux-4.9.13

内核错误码

在内核中定义了一些列错误码,以指示不同的出错情况:
include/uapi/asm-generic/errno-base.h

  1. #ifndef _ASM_GENERIC_ERRNO_BASE_H
  2. #define _ASM_GENERIC_ERRNO_BASE_H
  3. #define EPERM 1 /* Operation not permitted */
  4. #define ENOENT 2 /* No such file or directory */
  5. #define ESRCH 3 /* No such process */
  6. #define EINTR 4 /* Interrupted system call */
  7. #define EIO 5 /* I/O error */
  8. #define ENXIO 6 /* No such device or address */
  9. #define E2BIG 7 /* Argument list too long */
  10. #define ENOEXEC 8 /* Exec format error */
  11. #define EBADF 9 /* Bad file number */
  12. #define ECHILD 10 /* No child processes */
  13. #define EAGAIN 11 /* Try again */
  14. #define ENOMEM 12 /* Out of memory */
  15. #define EACCES 13 /* Permission denied */
  16. #define EFAULT 14 /* Bad address */
  17. #define ENOTBLK 15 /* Block device required */
  18. #define EBUSY 16 /* Device or resource busy */
  19. #define EEXIST 17 /* File exists */
  20. #define EXDEV 18 /* Cross-device link */
  21. #define ENODEV 19 /* No such device */
  22. #define ENOTDIR 20 /* Not a directory */
  23. #define EISDIR 21 /* Is a directory */
  24. #define EINVAL 22 /* Invalid argument */
  25. #define ENFILE 23 /* File table overflow */
  26. #define EMFILE 24 /* Too many open files */
  27. #define ENOTTY 25 /* Not a typewriter */
  28. #define ETXTBSY 26 /* Text file busy */
  29. #define EFBIG 27 /* File too large */
  30. #define ENOSPC 28 /* No space left on device */
  31. #define ESPIPE 29 /* Illegal seek */
  32. #define EROFS 30 /* Read-only file system */
  33. #define EMLINK 31 /* Too many links */
  34. #define EPIPE 32 /* Broken pipe */
  35. #define EDOM 33 /* Math argument out of domain of func */
  36. #define ERANGE 34 /* Math result not representable */
  37. #endif

内核错误指针

通常如果一个函数返回值是指针类型,在调用出错的情况下会返回NULL指针,但Linux内核对指针返回值作了更精妙的处理,使得出错情况能通过返回的指针体现出来。在内核中,有以下三种指针:

其中错误指针定义为指向内核空间保留区域(addr:0xffff000~0xffffffff, size: 4K)的指针,每一个地址都指示了一种出错情况。错误指针与错误码通过如下函数进行转换:
include/linux/err.h

  1. ......
  2. #define MAX_ERRNO 4095
  3. #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
  4. /* 将错误码转为错误指针 */
  5. static inline void * __must_check ERR_PTR(long error)
  6. {
  7. return (void *) error;
  8. }
  9. /* 将错误指针转为错误码 */
  10. static inline long __must_check PTR_ERR(__force const void *ptr)
  11. {
  12. return (long) ptr;
  13. }
  14. /* 判断指针是否为错误指针 */
  15. static inline bool __must_check IS_ERR(__force const void *ptr)
  16. {
  17. return IS_ERR_VALUE((unsigned long)ptr);
  18. }
  19. static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
  20. {
  21. return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
  22. }
  23. ......

内联函数IS_ERR(),用来判断指针是否错误,将其一一展开:

  1. IS_ERR(ptr)
  2. |
  3. IS_ERR_VALUE(ptr)
  4. |
  5. unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
  6. |
  7. unlikely((unsigned long)(void *)(x) >= (unsigned long)-4095)

表达式((unsigned long)-4095)的值就是0xfffff000,如果指针指向内核空间保留区域(0xfffff000~0xffffffff),IS_ERR()将返回flase,该指针被定义为错误指针。
内联函数PTR_ERR()与ERR_PTR(),提供错误指针与错误码相互转换的功能,涉及到类型强制转换,错误指针指向地址范围为0xfffff000~0xffffffff,因此转成错误码范围为-4096~-1。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注