libc_linux_riscv64.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // Copyright 2020 The Libc Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package libc // import "modernc.org/libc"
  5. import (
  6. "os"
  7. "strings"
  8. "unicode"
  9. "unsafe"
  10. "golang.org/x/sys/unix"
  11. "modernc.org/libc/errno"
  12. "modernc.org/libc/fcntl"
  13. "modernc.org/libc/signal"
  14. "modernc.org/libc/sys/types"
  15. "modernc.org/libc/utime"
  16. "modernc.org/libc/wctype"
  17. )
  18. // int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
  19. func Xsigaction(t *TLS, signum int32, act, oldact uintptr) int32 {
  20. // musl/arch/x86_64/ksigaction.h
  21. //
  22. // struct k_sigaction {
  23. // void (*handler)(int);
  24. // unsigned long flags;
  25. // void (*restorer)(void);
  26. // unsigned mask[2];
  27. // };
  28. type k_sigaction struct {
  29. handler uintptr
  30. flags ulong
  31. restorer uintptr
  32. mask [2]uint32
  33. }
  34. var kact, koldact uintptr
  35. if act != 0 {
  36. sz := int(unsafe.Sizeof(k_sigaction{}))
  37. kact = t.Alloc(sz)
  38. defer t.Free(sz)
  39. *(*k_sigaction)(unsafe.Pointer(kact)) = k_sigaction{
  40. handler: (*signal.Sigaction)(unsafe.Pointer(act)).F__sigaction_handler.Fsa_handler,
  41. flags: ulong((*signal.Sigaction)(unsafe.Pointer(act)).Fsa_flags),
  42. restorer: (*signal.Sigaction)(unsafe.Pointer(act)).Fsa_restorer,
  43. }
  44. Xmemcpy(t, kact+unsafe.Offsetof(k_sigaction{}.mask), act+unsafe.Offsetof(signal.Sigaction{}.Fsa_mask), types.Size_t(unsafe.Sizeof(k_sigaction{}.mask)))
  45. }
  46. if oldact != 0 {
  47. panic(todo(""))
  48. }
  49. if _, _, err := unix.Syscall6(unix.SYS_RT_SIGACTION, uintptr(signum), kact, koldact, unsafe.Sizeof(k_sigaction{}.mask), 0, 0); err != 0 {
  50. t.setErrno(err)
  51. return -1
  52. }
  53. if oldact != 0 {
  54. panic(todo(""))
  55. }
  56. return 0
  57. }
  58. // int fcntl(int fd, int cmd, ... /* arg */ );
  59. func Xfcntl64(t *TLS, fd, cmd int32, args uintptr) int32 {
  60. var arg uintptr
  61. if args != 0 {
  62. arg = *(*uintptr)(unsafe.Pointer(args))
  63. }
  64. if cmd == fcntl.F_SETFL {
  65. arg |= unix.O_LARGEFILE
  66. }
  67. n, _, err := unix.Syscall(unix.SYS_FCNTL, uintptr(fd), uintptr(cmd), arg)
  68. if err != 0 {
  69. // if dmesgs {
  70. // dmesg("%v: fd %v cmd %v", origin(1), fcntlCmdStr(fd), cmd)
  71. // }
  72. t.setErrno(err)
  73. return -1
  74. }
  75. // if dmesgs {
  76. // dmesg("%v: %d %s %#x: %d", origin(1), fd, fcntlCmdStr(cmd), arg, n)
  77. // }
  78. return int32(n)
  79. }
  80. // int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags);
  81. func Xfstatat(t *TLS, dirfd int32, pathname, statbuf uintptr, flags int32) int32 {
  82. // From golang.org/x/sys/unix/zsyscall_linux_riscv64.go
  83. if _, _, err := unix.Syscall6(unix.SYS_FSTATAT, uintptr(dirfd), pathname, statbuf, uintptr(flags), 0, 0); err != 0 {
  84. t.setErrno(err)
  85. return -1
  86. }
  87. return 0
  88. }
  89. // int lstat(const char *pathname, struct stat *statbuf);
  90. func Xlstat64(t *TLS, pathname, statbuf uintptr) int32 {
  91. // From golang.org/x/sys/unix/syscall_linux_riscv64.go
  92. return Xfstatat(t, unix.AT_FDCWD, pathname, statbuf, unix.AT_SYMLINK_NOFOLLOW)
  93. }
  94. // int stat(const char *pathname, struct stat *statbuf);
  95. func Xstat64(t *TLS, pathname, statbuf uintptr) int32 {
  96. // From golang.org/x/sys/unix/syscall_linux_riscv64.go
  97. return Xfstatat(t, unix.AT_FDCWD, pathname, statbuf, 0)
  98. }
  99. // int fstat(int fd, struct stat *statbuf);
  100. func Xfstat64(t *TLS, fd int32, statbuf uintptr) int32 {
  101. if _, _, err := unix.Syscall(unix.SYS_FSTAT, uintptr(fd), statbuf, 0); err != 0 {
  102. // if dmesgs {
  103. // dmesg("%v: fd %d: %v", origin(1), fd, err)
  104. // }
  105. t.setErrno(err)
  106. return -1
  107. }
  108. // if dmesgs {
  109. // dmesg("%v: %d size %#x: ok\n%+v", origin(1), fd, (*stat.Stat)(unsafe.Pointer(statbuf)).Fst_size, (*stat.Stat)(unsafe.Pointer(statbuf)))
  110. // }
  111. return 0
  112. }
  113. func Xmmap(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
  114. return Xmmap64(t, addr, length, prot, flags, fd, offset)
  115. }
  116. // void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
  117. func Xmmap64(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
  118. data, _, err := unix.Syscall6(unix.SYS_MMAP, addr, uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset))
  119. if err != 0 {
  120. // if dmesgs {
  121. // dmesg("%v: %v", origin(1), err)
  122. // }
  123. t.setErrno(err)
  124. return ^uintptr(0) // (void*)-1
  125. }
  126. // if dmesgs {
  127. // dmesg("%v: %#x", origin(1), data)
  128. // }
  129. return data
  130. }
  131. // void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, ... /* void *new_address */);
  132. func Xmremap(t *TLS, old_address uintptr, old_size, new_size types.Size_t, flags int32, args uintptr) uintptr {
  133. var arg uintptr
  134. if args != 0 {
  135. arg = *(*uintptr)(unsafe.Pointer(args))
  136. }
  137. data, _, err := unix.Syscall6(unix.SYS_MREMAP, old_address, uintptr(old_size), uintptr(new_size), uintptr(flags), arg, 0)
  138. if err != 0 {
  139. // if dmesgs {
  140. // dmesg("%v: %v", origin(1), err)
  141. // }
  142. t.setErrno(err)
  143. return ^uintptr(0) // (void*)-1
  144. }
  145. // if dmesgs {
  146. // dmesg("%v: %#x", origin(1), data)
  147. // }
  148. return data
  149. }
  150. // int ftruncate(int fd, off_t length);
  151. func Xftruncate64(t *TLS, fd int32, length types.Off_t) int32 {
  152. if _, _, err := unix.Syscall(unix.SYS_FTRUNCATE, uintptr(fd), uintptr(length), 0); err != 0 {
  153. // if dmesgs {
  154. // dmesg("%v: fd %d: %v", origin(1), fd, err)
  155. // }
  156. t.setErrno(err)
  157. return -1
  158. }
  159. // if dmesgs {
  160. // dmesg("%v: %d %#x: ok", origin(1), fd, length)
  161. // }
  162. return 0
  163. }
  164. // off64_t lseek64(int fd, off64_t offset, int whence);
  165. func Xlseek64(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
  166. n, _, err := unix.Syscall(unix.SYS_LSEEK, uintptr(fd), uintptr(offset), uintptr(whence))
  167. if err != 0 {
  168. // if dmesgs {
  169. // dmesg("%v: fd %v, off %#x, whence %v: %v", origin(1), fd, offset, whenceStr(whence), err)
  170. // }
  171. t.setErrno(err)
  172. return -1
  173. }
  174. // if dmesgs {
  175. // dmesg("%v: fd %v, off %#x, whence %v: %#x", origin(1), fd, offset, whenceStr(whence), n)
  176. // }
  177. return types.Off_t(n)
  178. }
  179. // From man utime executed on linux/riscv64:
  180. //
  181. // The utimbuf structure is:
  182. //
  183. // struct utimbuf {
  184. // time_t actime; /* access time */
  185. // time_t modtime; /* modification time */
  186. // };
  187. type utimbuf struct {
  188. actime utime.Time_t
  189. modtime utime.Time_t
  190. }
  191. // int utime(const char *filename, const struct utimbuf *times);
  192. func Xutime(t *TLS, filename, times uintptr) int32 {
  193. if times == 0 {
  194. return Xutimes(t, filename, 0)
  195. }
  196. n := int(unsafe.Sizeof([2]types.Timeval{}))
  197. p := t.Alloc(n)
  198. defer t.Free(n)
  199. *(*[2]types.Timeval)(unsafe.Pointer(p)) = [2]types.Timeval{
  200. {Ftv_sec: (*utimbuf)(unsafe.Pointer(times)).actime},
  201. {Ftv_sec: (*utimbuf)(unsafe.Pointer(times)).modtime},
  202. }
  203. return Xutimes(t, filename, p)
  204. }
  205. // unsigned int alarm(unsigned int seconds);
  206. func Xalarm(t *TLS, seconds uint32) uint32 {
  207. panic(todo(""))
  208. // No alarm syscall on linux/riscv64. And cannot implement with setitimer as in musl,
  209. // because of missing defination to constant ITIMER_REAL in types_linux_riscv64.go.
  210. }
  211. // time_t time(time_t *tloc);
  212. func Xtime(t *TLS, tloc uintptr) types.Time_t {
  213. // From golang.org/x/sys/unix/syscall_linux_riscv64.go
  214. var tv types.Timeval
  215. if err := Xgettimeofday(t, uintptr(unsafe.Pointer(&tv)), 0); err != 0 {
  216. t.setErrno(err)
  217. return -1
  218. }
  219. if tloc != 0 {
  220. *(*types.Time_t)(unsafe.Pointer(tloc)) = tv.Ftv_sec
  221. }
  222. return tv.Ftv_sec
  223. }
  224. // int getrlimit(int resource, struct rlimit *rlim);
  225. func Xgetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
  226. if _, _, err := unix.Syscall(unix.SYS_GETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
  227. t.setErrno(err)
  228. return -1
  229. }
  230. return 0
  231. }
  232. // int mkdir(const char *path, mode_t mode);
  233. func Xmkdir(t *TLS, path uintptr, mode types.Mode_t) int32 {
  234. // From golang.org/x/sys/unix/syscall_linux.go
  235. return Xmkdirat(t, unix.AT_FDCWD, path, mode)
  236. }
  237. // int symlink(const char *target, const char *linkpath);
  238. func Xsymlink(t *TLS, target, linkpath uintptr) int32 {
  239. // From golang.org/x/sys/unix/syscall_linux.go
  240. return Xsymlinkat(t, target, unix.AT_FDCWD, linkpath)
  241. }
  242. // int chmod(const char *pathname, mode_t mode)
  243. func Xchmod(t *TLS, pathname uintptr, mode types.Mode_t) int32 {
  244. // From golang.org/x/sys/unix/syscall_linux.go
  245. return Xfchmodat(t, unix.AT_FDCWD, pathname, mode, 0)
  246. }
  247. // int utimes(const char *filename, const struct timeval times[2]);
  248. func Xutimes(t *TLS, filename, times uintptr) int32 {
  249. return Xutimensat(t, unix.AT_FDCWD, filename, times, 0)
  250. }
  251. // int unlink(const char *pathname);
  252. func Xunlink(t *TLS, pathname uintptr) int32 {
  253. // From golang.org/x/sys/unix/syscall_linux.go
  254. return Xunlinkat(t, unix.AT_FDCWD, pathname, 0)
  255. }
  256. // int access(const char *pathname, int mode);
  257. func Xaccess(t *TLS, pathname uintptr, mode int32) int32 {
  258. // From golang.org/x/sys/unix/syscall_linux.go
  259. return Xfaccessat(t, unix.AT_FDCWD, pathname, mode, 0)
  260. }
  261. // int rmdir(const char *pathname);
  262. func Xrmdir(t *TLS, pathname uintptr) int32 {
  263. // From golang.org/x/sys/unix/syscall_linux.go
  264. return Xunlinkat(t, unix.AT_FDCWD, pathname, unix.AT_REMOVEDIR)
  265. }
  266. // int rename(const char *oldpath, const char *newpath);
  267. func Xrename(t *TLS, oldpath, newpath uintptr) int32 {
  268. // From golang.org/x/sys/unix/syscall_linux.go
  269. return Xrenameat(t, unix.AT_FDCWD, oldpath, unix.AT_FDCWD, newpath)
  270. }
  271. // int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
  272. func Xrenameat(t *TLS, olddirfd int32, oldpath uintptr, newdirfd int32, newpath uintptr) int32 {
  273. // From golang.org/x/sys/unix/syscall_linux_riscv64.go
  274. return Xrenameat2(t, olddirfd, oldpath, newdirfd, newpath, 0)
  275. }
  276. // int mknod(const char *pathname, mode_t mode, dev_t dev);
  277. func Xmknod(t *TLS, pathname uintptr, mode types.Mode_t, dev types.Dev_t) int32 {
  278. // From golang.org/x/sys/unix/syscall_linux.go
  279. return Xmknodat(t, unix.AT_FDCWD, pathname, mode, dev)
  280. }
  281. // int chown(const char *pathname, uid_t owner, gid_t group);
  282. func Xchown(t *TLS, pathname uintptr, owner types.Uid_t, group types.Gid_t) int32 {
  283. // From golang.org/x/sys/unix/syscall_linux.go
  284. return Xfchownat(t, unix.AT_FDCWD, pathname, owner, group, 0)
  285. }
  286. // int link(const char *oldpath, const char *newpath);
  287. func Xlink(t *TLS, oldpath, newpath uintptr) int32 {
  288. // From golang.org/x/sys/unix/syscall_linux.go
  289. return Xlinkat(t, unix.AT_FDCWD, oldpath, unix.AT_FDCWD, newpath, 0)
  290. }
  291. // int pipe(int pipefd[2]);
  292. func Xpipe(t *TLS, pipefd uintptr) int32 {
  293. // From golang.org/x/sys/unix/syscall_linux.go
  294. return Xpipe2(t, pipefd, 0)
  295. }
  296. // int dup2(int oldfd, int newfd);
  297. func Xdup2(t *TLS, oldfd, newfd int32) int32 {
  298. // From golang.org/x/sys/unix/syscall_linux.go
  299. return Xdup3(t, oldfd, newfd, 0)
  300. }
  301. // ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);
  302. func Xreadlink(t *TLS, path, buf uintptr, bufsize types.Size_t) types.Ssize_t {
  303. // From golang.org/x/sys/unix/syscall_linux.go
  304. return Xreadlinkat(t, unix.AT_FDCWD, path, buf, bufsize)
  305. }
  306. // FILE *fopen64(const char *pathname, const char *mode);
  307. func Xfopen64(t *TLS, pathname, mode uintptr) uintptr {
  308. m := strings.ReplaceAll(GoString(mode), "b", "")
  309. var flags int
  310. switch m {
  311. case "r":
  312. flags = os.O_RDONLY
  313. case "r+":
  314. flags = os.O_RDWR
  315. case "w":
  316. flags = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
  317. case "w+":
  318. flags = os.O_RDWR | os.O_CREATE | os.O_TRUNC
  319. case "a":
  320. flags = os.O_WRONLY | os.O_CREATE | os.O_APPEND
  321. case "a+":
  322. flags = os.O_RDWR | os.O_CREATE | os.O_APPEND
  323. default:
  324. panic(m)
  325. }
  326. //TODO- flags |= fcntl.O_LARGEFILE
  327. // From golang.org/x/sys/unix/syscall_linux.go
  328. fd := Xopenat(t, unix.AT_FDCWD, pathname, int32(flags|unix.O_LARGEFILE), 0666)
  329. if fd == -1 {
  330. return 0
  331. }
  332. if p := newFile(t, fd); p != 0 {
  333. return p
  334. }
  335. Xclose(t, fd)
  336. t.setErrno(errno.ENOMEM)
  337. return 0
  338. }
  339. // int iswspace(wint_t wc);
  340. func Xiswspace(t *TLS, wc wctype.Wint_t) int32 {
  341. return Bool32(unicode.IsSpace(rune(wc)))
  342. }
  343. // int iswalnum(wint_t wc);
  344. func Xiswalnum(t *TLS, wc wctype.Wint_t) int32 {
  345. return Bool32(unicode.IsLetter(rune(wc)) || unicode.IsNumber(rune(wc)))
  346. }
  347. func __syscall1(t *TLS, trap, p1 long) long {
  348. return __syscall(unix.Syscall(uintptr(trap), uintptr(p1), 0, 0))
  349. }
  350. func __syscall3(t *TLS, trap, p1, p2, p3 long) long {
  351. return __syscall(unix.Syscall(uintptr(trap), uintptr(p1), uintptr(p2), uintptr(p3)))
  352. }
  353. func __syscall4(t *TLS, trap, p1, p2, p3, p4 long) long {
  354. return __syscall(unix.Syscall6(uintptr(trap), uintptr(p1), uintptr(p2), uintptr(p3), uintptr(p4), 0, 0))
  355. }