basic-lib.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. typedef __UINT32_TYPE__ uint32_t;
  2. typedef __UINT16_TYPE__ uint16_t;
  3. typedef __UINT8_TYPE__ uint8_t;
  4. typedef uint32_t pid_t;
  5. typedef uint32_t size_t;
  6. typedef size_t ino_t;
  7. #define GNU_ATTRIBUTE(attr) __attribute__((attr))
  8. #define NORETURN GNU_ATTRIBUTE(noreturn)
  9. #define O_RDONLY (0)
  10. #define O_DIRECTORY (0x4)
  11. // dirent file types
  12. #define DT_UNKNOWN 0
  13. #define DT_FIFO 1
  14. #define DT_CHR 2
  15. #define DT_DIR 4
  16. #define DT_BLK 6
  17. #define DT_REG 8
  18. #define DT_LNK 10
  19. #define DT_SOCK 12
  20. #define DT_WHT 14
  21. #define DT_MAX (S_DT_MASK + 1) /* 16 */
  22. static inline uint32_t syscall(uint32_t num, uint32_t arg1, uint32_t arg2, uint32_t arg3)
  23. {
  24. asm volatile(
  25. "movl %1, %%edi\n"
  26. "movl %2, %%esi\n"
  27. "movl %3, %%edx\n"
  28. "movl %4, %%eax\n"
  29. "int $0x80\n"
  30. "movl %%eax, %0"
  31. : "=g"(num)
  32. : "g"(arg1), "g"(arg2), "g"(arg3), "g"(num)
  33. : "eax", "edx", "edi", "esi");
  34. return num;
  35. }
  36. static inline void NORETURN syscall_noreturn(uint32_t num, uint32_t arg1, uint32_t arg2, uint32_t arg3)
  37. {
  38. asm volatile(
  39. "movl %1, %%edi\n"
  40. "movl %2, %%esi\n"
  41. "movl %3, %%edx\n"
  42. "movl %4, %%eax\n"
  43. "int $0x80\n"
  44. "movl %%eax, %0"
  45. : "=g"(num)
  46. : "g"(arg1), "g"(arg2), "g"(arg3), "g"(num)
  47. : "eax", "edx", "edi", "esi");
  48. // crash
  49. syscall_noreturn(0x05, 0, 0, 0);
  50. }
  51. static inline int fork(void)
  52. {
  53. return syscall(0x00, 0, 0, 0);
  54. }
  55. static inline uint32_t write(int fd, const char* buf, size_t count)
  56. {
  57. return syscall(0x01, fd, (uint32_t)buf, count);
  58. }
  59. static inline uint32_t read(int fd, char* buf, size_t count)
  60. {
  61. return syscall(0x07, fd, (uint32_t)buf, count);
  62. }
  63. static inline void sleep(void)
  64. {
  65. syscall(0x02, 0, 0, 0);
  66. }
  67. static inline void NORETURN exec(const char* bin, const char** argv)
  68. {
  69. syscall_noreturn(0x04, (uint32_t)bin, (uint32_t)argv, 0);
  70. }
  71. static inline void NORETURN exit(int exit_code)
  72. {
  73. syscall_noreturn(0x05, exit_code, 0, 0);
  74. }
  75. static inline uint32_t wait(int* return_value)
  76. {
  77. return syscall(0x06, (uint32_t)return_value, 0, 0);
  78. }
  79. struct __attribute__((__packed__)) user_dirent {
  80. ino_t d_ino; // inode number
  81. uint32_t d_off; // ignored
  82. uint16_t d_reclen; // length of this struct user_dirent
  83. char d_name[1]; // file name with a padding zero
  84. // uint8_t d_type; // file type, with offset of (d_reclen - 1)
  85. };
  86. static inline size_t getdents(int fd, struct user_dirent* buf, size_t cnt)
  87. {
  88. return syscall(0x08, fd, (uint32_t)buf, cnt);
  89. }
  90. static inline int open(const char* path, uint32_t flags)
  91. {
  92. return syscall(0x09, (uint32_t)path, flags, 0);
  93. }