ucmd_lib.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Tiny userland shim for ring-3 commands: syscall wrappers + string output.
  2. * Freestanding (no libc); include "syscall.h" for the syscall numbers. */
  3. #ifndef UCMD_LIB_H
  4. #define UCMD_LIB_H
  5. #include "syscall.h"
  6. static inline int u_syscall1(int num, int a1)
  7. {
  8. int ret;
  9. __asm__ volatile ("int $0x80"
  10. : "=a"(ret)
  11. : "a"(num), "b"(a1)
  12. : "memory");
  13. return ret;
  14. }
  15. static inline int u_syscall0(int num)
  16. {
  17. int ret;
  18. __asm__ volatile ("int $0x80"
  19. : "=a"(ret)
  20. : "a"(num)
  21. : "memory");
  22. return ret;
  23. }
  24. static inline int u_syscall2(int num, int a1, int a2)
  25. {
  26. int ret;
  27. __asm__ volatile ("int $0x80"
  28. : "=a"(ret)
  29. : "a"(num), "b"(a1), "c"(a2)
  30. : "memory");
  31. return ret;
  32. }
  33. static inline int u_syscall3(int num, int a1, int a2, int a3)
  34. {
  35. int ret;
  36. __asm__ volatile ("int $0x80"
  37. : "=a"(ret)
  38. : "a"(num), "b"(a1), "c"(a2), "d"(a3)
  39. : "memory");
  40. return ret;
  41. }
  42. static inline void u_putc(char c)
  43. {
  44. char b[2];
  45. b[0] = c;
  46. b[1] = '\0';
  47. u_syscall1(SYS_PUTSTR, (int)b);
  48. }
  49. static inline void u_puts(const char *s)
  50. {
  51. u_syscall1(SYS_PUTSTR, (int)s);
  52. }
  53. static inline void u_putdec(unsigned int n)
  54. {
  55. char buf[12];
  56. char *p = buf + sizeof(buf) - 1;
  57. *p = '\0';
  58. if (n == 0) {
  59. u_putc('0');
  60. return;
  61. }
  62. while (n) {
  63. *--p = (char)('0' + (n % 10));
  64. n /= 10;
  65. }
  66. u_puts(p);
  67. }
  68. static inline void u_puthex(unsigned int n)
  69. {
  70. static const char hex[] = "0123456789ABCDEF";
  71. char buf[11];
  72. int i;
  73. for (i = 0; i < 8; i++)
  74. buf[i] = hex[(n >> (28 - i * 4)) & 0xF];
  75. buf[8] = '\0';
  76. u_puts(buf);
  77. }
  78. /* "a.b.c.d" (strict, 4 parts, 0-255) -> 0, else -1. */
  79. static inline int u_parse_ip(const char *s, unsigned char ip[4])
  80. {
  81. unsigned int i, val = 0, nparts = 0;
  82. int have_digit = 0;
  83. for (i = 0; ; i++) {
  84. char c = s[i];
  85. if (c >= '0' && c <= '9') {
  86. val = val * 10 + (unsigned int)(c - '0');
  87. if (val > 255)
  88. return -1;
  89. have_digit = 1;
  90. } else if (c == '.' || c == '\0') {
  91. if (!have_digit || nparts >= 4)
  92. return -1;
  93. ip[nparts++] = (unsigned char)val;
  94. val = 0;
  95. have_digit = 0;
  96. if (c == '\0')
  97. break;
  98. } else {
  99. return -1;
  100. }
  101. }
  102. return (nparts == 4) ? 0 : -1;
  103. }
  104. static inline void u_put_ip(const unsigned char ip[4])
  105. {
  106. unsigned int i;
  107. for (i = 0; i < 4; i++) {
  108. if (i)
  109. u_putc('.');
  110. u_putdec(ip[i]);
  111. }
  112. }
  113. static inline void u_put_hex2(unsigned int v)
  114. {
  115. static const char hex[] = "0123456789ABCDEF";
  116. u_putc(hex[(v >> 4) & 0xF]);
  117. u_putc(hex[v & 0xF]);
  118. }
  119. static inline void u_put_mac(const unsigned char mac[6])
  120. {
  121. unsigned int i;
  122. for (i = 0; i < 6; i++) {
  123. if (i)
  124. u_putc(':');
  125. u_put_hex2(mac[i]);
  126. }
  127. }
  128. static inline int u_getcwd(char *buf, unsigned int max)
  129. {
  130. return u_syscall2(SYS_GETCWD, (int)buf, (int)max);
  131. }
  132. static inline unsigned int u_ticks_ms(void)
  133. {
  134. return (unsigned int)u_syscall0(SYS_GET_TICKS_MS);
  135. }
  136. /* Cursor position of this process's terminal screen (linear cell index);
  137. * used by `top` to repaint its frame in place. */
  138. static inline unsigned int u_get_cursor(void)
  139. {
  140. return (unsigned int)u_syscall0(SYS_GET_CURSOR);
  141. }
  142. static inline void u_set_cursor(unsigned int pos)
  143. {
  144. u_syscall1(SYS_SET_CURSOR, (int)pos);
  145. }
  146. static inline void u_sleep_ms(unsigned int ms)
  147. {
  148. u_syscall1(SYS_SLEEP_MS, (int)ms);
  149. }
  150. static inline void u_exit(int code)
  151. {
  152. u_syscall1(SYS_EXIT, code);
  153. for (;;)
  154. ;
  155. }
  156. #endif