ucmd_lib.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. static inline unsigned int u_ticks_ms(void)
  79. {
  80. return (unsigned int)u_syscall0(SYS_GET_TICKS_MS);
  81. }
  82. static inline void u_sleep_ms(unsigned int ms)
  83. {
  84. u_syscall1(SYS_SLEEP_MS, (int)ms);
  85. }
  86. static inline void u_exit(int code)
  87. {
  88. u_syscall1(SYS_EXIT, code);
  89. for (;;)
  90. ;
  91. }
  92. #endif