General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.7 KiB

  1. #pragma once
  2. #include <cstdint>
  3. #include <cstddef>
  4. #include <gp/containers/buffer.hpp>
  5. template<int64_t syscall_id, int arg_count>
  6. struct syscall;
  7. template<int64_t syscall_id>
  8. struct syscall<syscall_id, 1> {
  9. int64_t operator()(int64_t p1) const {
  10. int64_t ret;
  11. asm volatile
  12. (
  13. "syscall"
  14. : "=a" (ret)
  15. : "0"(syscall_id), "D"(p1)
  16. : "rcx", "r11", "memory"
  17. );
  18. return ret;
  19. }
  20. };
  21. template<int64_t syscall_id>
  22. struct syscall<syscall_id, 2> {
  23. int64_t operator()(int64_t p1,int64_t p2) const {
  24. int64_t ret;
  25. asm volatile
  26. (
  27. "syscall"
  28. : "=a" (ret)
  29. : "0"(syscall_id), "D"(p1), "S"(p2)
  30. : "rcx", "r11", "memory"
  31. );
  32. return ret;
  33. }
  34. };
  35. template<int64_t syscall_id>
  36. struct syscall<syscall_id, 3> {
  37. int64_t operator()(int64_t p1,int64_t p2,int64_t p3) const {
  38. int64_t ret;
  39. asm volatile
  40. (
  41. "syscall"
  42. : "=a" (ret)
  43. : "0"(syscall_id), "D"(p1), "S"(p2), "d"(p3)
  44. : "rcx", "r11", "memory"
  45. );
  46. return ret;
  47. }
  48. };
  49. template<int64_t syscall_id>
  50. struct syscall<syscall_id, 4> {
  51. int64_t operator()(int64_t p1,int64_t p2,int64_t p3,int64_t p4) const {
  52. int64_t ret;
  53. register long r10 asm("r10") = p4;
  54. asm volatile
  55. (
  56. "syscall"
  57. : "=a" (ret)
  58. : "0"(syscall_id), "D"(p1), "S"(p2), "d"(p3), "r"(r10)
  59. : "rcx", "r11", "memory"
  60. );
  61. return ret;
  62. }
  63. };
  64. template<int64_t syscall_id>
  65. struct syscall<syscall_id, 5> {
  66. int64_t operator()(int64_t p1,int64_t p2,int64_t p3,int64_t p4,int64_t p5) const {
  67. int64_t ret;
  68. register long r10 asm("r10") = p4;
  69. register long r8 asm("r8") = p5;
  70. asm volatile
  71. (
  72. "syscall"
  73. : "=a" (ret)
  74. : "0"(syscall_id), "D"(p1), "S"(p2), "d"(p3), "r"(r10), "r"(r8)
  75. : "rcx", "r11", "memory"
  76. );
  77. return ret;
  78. }
  79. };
  80. template<int64_t syscall_id>
  81. struct syscall<syscall_id, 6> {
  82. int64_t operator()(int64_t p1,int64_t p2,int64_t p3,int64_t p4,int64_t p5,int64_t p6) const {
  83. int64_t ret;
  84. register long r10 asm("r10") = p4;
  85. register long r8 asm("r8") = p5;
  86. register long r9 asm("r9") = p6;
  87. asm volatile
  88. (
  89. "syscall"
  90. : "=a" (ret)
  91. : "0"(syscall_id), "D"(p1), "S"(p2), "d"(p3), "r"(r10), "r"(r8), "r"(r9)
  92. : "rcx", "r11", "memory"
  93. );
  94. return ret;
  95. }
  96. };
  97. constexpr auto _read = syscall<0, 3>{};
  98. constexpr auto _write = syscall<1, 3>{};
  99. constexpr auto _mmap = syscall<9, 6>{};
  100. constexpr auto _munmap = syscall<11, 2>{};
  101. constexpr auto _exit = syscall<60, 1>{};
  102. inline int read(int fd, char* buffer, size_t sz) {
  103. return _read((int64_t)fd, (int64_t)buffer, (int64_t)sz);
  104. }
  105. inline int write(int fd, char* buffer, size_t sz) {
  106. return _write((int64_t)fd, (int64_t)buffer, (int64_t)sz);
  107. }
  108. inline void* mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) {
  109. return (void*)_mmap((int64_t)addr, (int64_t)length, (int64_t)prot, (int64_t)flags, (int64_t)fd, (int64_t)offset);
  110. }
  111. inline int munmap(void *addr, size_t length) {
  112. return _munmap((int64_t)addr, (int64_t)length);
  113. }
  114. inline int read(int fd, gp::buffer<char> buffer) {
  115. return _read((int64_t)fd, (int64_t)buffer.begin().data, (int64_t)buffer.size());
  116. }
  117. inline int write(int fd, gp::buffer<char> buffer) {
  118. return _write((int64_t)fd, (int64_t)buffer.begin().data, (int64_t)buffer.size());
  119. }
  120. extern "C" {
  121. inline __attribute__ ((__noreturn__)) void exit(int status) {
  122. _exit((int64_t)status);
  123. while(true);
  124. }
  125. }