mod.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. use core::{arch::asm, ptr};
  2. use core_io::Write;
  3. use super::{errno, types::*, Pal};
  4. use crate::{
  5. c_str::CStr,
  6. header::{dirent::dirent, signal::SIGCHLD, sys_stat::S_IFIFO},
  7. };
  8. // use header::sys_resource::rusage;
  9. use crate::header::{
  10. sys_resource::rlimit,
  11. sys_stat::stat,
  12. sys_statvfs::statvfs,
  13. sys_time::{timeval, timezone},
  14. };
  15. // use header::sys_times::tms;
  16. use crate::header::{sys_utsname::utsname, time::timespec};
  17. mod epoll;
  18. mod ptrace;
  19. mod signal;
  20. mod socket;
  21. const AT_FDCWD: c_int = -100;
  22. const AT_EMPTY_PATH: c_int = 0x1000;
  23. const AT_REMOVEDIR: c_int = 0x200;
  24. const SYS_CLONE: usize = 56;
  25. const CLONE_VM: usize = 0x0100;
  26. const CLONE_FS: usize = 0x0200;
  27. const CLONE_FILES: usize = 0x0400;
  28. const CLONE_SIGHAND: usize = 0x0800;
  29. const CLONE_THREAD: usize = 0x00010000;
  30. #[repr(C)]
  31. #[derive(Default)]
  32. struct linux_statfs {
  33. f_type: c_long, /* type of file system (see below) */
  34. f_bsize: c_long, /* optimal transfer block size */
  35. f_blocks: fsblkcnt_t, /* total data blocks in file system */
  36. f_bfree: fsblkcnt_t, /* free blocks in fs */
  37. f_bavail: fsblkcnt_t, /* free blocks available to unprivileged user */
  38. f_files: fsfilcnt_t, /* total file nodes in file system */
  39. f_ffree: fsfilcnt_t, /* free file nodes in fs */
  40. f_fsid: c_long, /* file system id */
  41. f_namelen: c_long, /* maximum length of filenames */
  42. f_frsize: c_long, /* fragment size (since Linux 2.6) */
  43. f_flags: c_long,
  44. f_spare: [c_long; 4],
  45. }
  46. pub fn e(sys: usize) -> usize {
  47. if (sys as isize) < 0 && (sys as isize) >= -256 {
  48. unsafe {
  49. errno = -(sys as isize) as c_int;
  50. }
  51. !0
  52. } else {
  53. sys
  54. }
  55. }
  56. pub struct Sys;
  57. impl Sys {
  58. // fn getrusage(who: c_int, r_usage: *mut rusage) -> c_int {
  59. // e(unsafe { syscall!(GETRUSAGE, who, r_usage) }) as c_int
  60. // }
  61. pub unsafe fn ioctl(fd: c_int, request: c_ulong, out: *mut c_void) -> c_int {
  62. // TODO: Somehow support varargs to syscall??
  63. // e(syscall!(IOCTL, fd, request, out)) as c_int
  64. unimplemented!()
  65. }
  66. // fn times(out: *mut tms) -> clock_t {
  67. // unsafe { syscall!(TIMES, out) as clock_t }
  68. // }
  69. }
  70. impl Pal for Sys {
  71. fn access(path: &CStr, mode: c_int) -> c_int {
  72. // e(unsafe { syscall!(ACCESS, path.as_ptr(), mode) }) as c_int
  73. unimplemented!()
  74. }
  75. fn brk(addr: *mut c_void) -> *mut c_void {
  76. unsafe { syscall!(SYS_BRK, addr) as *mut c_void }
  77. }
  78. fn chdir(path: &CStr) -> c_int {
  79. e(unsafe { syscall!(SYS_CHDIR, path.as_ptr()) }) as c_int
  80. }
  81. fn chmod(path: &CStr, mode: mode_t) -> c_int {
  82. // e(unsafe { syscall!(FCHMODAT, AT_FDCWD, path.as_ptr(), mode, 0) }) as c_int
  83. return 0;
  84. }
  85. fn chown(path: &CStr, owner: uid_t, group: gid_t) -> c_int {
  86. // e(unsafe {
  87. // syscall!(
  88. // FCHOWNAT,
  89. // AT_FDCWD,
  90. // path.as_ptr(),
  91. // owner as u32,
  92. // group as u32
  93. // )
  94. // }) as c_int
  95. return 0;
  96. }
  97. fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int {
  98. // e(unsafe { syscall!(CLOCK_GETTIME, clk_id, tp) }) as c_int
  99. unimplemented!()
  100. }
  101. fn close(fildes: c_int) -> c_int {
  102. e(unsafe { syscall!(SYS_CLOSE, fildes) }) as c_int
  103. }
  104. fn dup(fildes: c_int) -> c_int {
  105. // e(unsafe { syscall!(DUP, fildes) }) as c_int
  106. unimplemented!()
  107. }
  108. fn dup2(fildes: c_int, fildes2: c_int) -> c_int {
  109. // e(unsafe { syscall!(DUP3, fildes, fildes2, 0) }) as c_int
  110. unimplemented!()
  111. }
  112. unsafe fn execve(path: &CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int {
  113. e(syscall!(SYS_EXECVE, path.as_ptr(), argv, envp)) as c_int
  114. }
  115. fn exit(status: c_int) -> ! {
  116. unsafe {
  117. syscall!(SYS_EXIT, status);
  118. }
  119. loop {}
  120. }
  121. fn fchdir(fildes: c_int) -> c_int {
  122. // e(unsafe { syscall!(FCHDIR, fildes) }) as c_int
  123. unimplemented!()
  124. }
  125. fn fchmod(fildes: c_int, mode: mode_t) -> c_int {
  126. // e(unsafe { syscall!(FCHMOD, fildes, mode) }) as c_int
  127. return 0;
  128. }
  129. fn fchown(fildes: c_int, owner: uid_t, group: gid_t) -> c_int {
  130. // e(unsafe { syscall!(FCHOWN, fildes, owner, group) }) as c_int
  131. return 0;
  132. }
  133. fn flock(fd: c_int, operation: c_int) -> c_int {
  134. // e(unsafe { syscall!(FLOCK, fd, operation) }) as c_int
  135. return 0;
  136. }
  137. fn fstat(fildes: c_int, buf: *mut stat) -> c_int {
  138. // let empty = b"\0";
  139. // let empty_ptr = empty.as_ptr() as *const c_char;
  140. // e(unsafe { syscall!(NEWFSTATAT, fildes, empty_ptr, buf, AT_EMPTY_PATH) }) as c_int
  141. unimplemented!()
  142. }
  143. fn fstatvfs(fildes: c_int, buf: *mut statvfs) -> c_int {
  144. // let mut kbuf = linux_statfs::default();
  145. // let kbuf_ptr = &mut kbuf as *mut linux_statfs;
  146. // let res = e(unsafe { syscall!(FSTATFS, fildes, kbuf_ptr) }) as c_int;
  147. // if res == 0 {
  148. // unsafe {
  149. // if !buf.is_null() {
  150. // (*buf).f_bsize = kbuf.f_bsize as c_ulong;
  151. // (*buf).f_frsize = if kbuf.f_frsize != 0 {
  152. // kbuf.f_frsize
  153. // } else {
  154. // kbuf.f_bsize
  155. // } as c_ulong;
  156. // (*buf).f_blocks = kbuf.f_blocks;
  157. // (*buf).f_bfree = kbuf.f_bfree;
  158. // (*buf).f_bavail = kbuf.f_bavail;
  159. // (*buf).f_files = kbuf.f_files;
  160. // (*buf).f_ffree = kbuf.f_ffree;
  161. // (*buf).f_favail = kbuf.f_ffree;
  162. // (*buf).f_fsid = kbuf.f_fsid as c_ulong;
  163. // (*buf).f_flag = kbuf.f_flags as c_ulong;
  164. // (*buf).f_namemax = kbuf.f_namelen as c_ulong;
  165. // }
  166. // }
  167. // }
  168. // res
  169. unimplemented!()
  170. }
  171. fn fcntl(fildes: c_int, cmd: c_int, arg: c_int) -> c_int {
  172. // e(unsafe { syscall!(FCNTL, fildes, cmd, arg) }) as c_int
  173. unimplemented!()
  174. }
  175. fn fork() -> pid_t {
  176. // e(unsafe { syscall!(CLONE, SIGCHLD, 0, 0, 0, 0) }) as pid_t
  177. e(unsafe { syscall!(SYS_FORK) }) as pid_t
  178. }
  179. fn fpath(fildes: c_int, out: &mut [u8]) -> ssize_t {
  180. // let mut proc_path = b"/proc/self/fd/".to_vec();
  181. // write!(proc_path, "{}", fildes).unwrap();
  182. // proc_path.push(0);
  183. // Self::readlink(CStr::from_bytes_with_nul(&proc_path).unwrap(), out)
  184. unimplemented!()
  185. }
  186. fn fsync(fildes: c_int) -> c_int {
  187. // e(unsafe { syscall!(FSYNC, fildes) }) as c_int
  188. return 0;
  189. }
  190. fn ftruncate(fildes: c_int, length: off_t) -> c_int {
  191. // e(unsafe { syscall!(FTRUNCATE, fildes, length) }) as c_int
  192. unimplemented!()
  193. }
  194. fn futex(addr: *mut c_int, op: c_int, val: c_int, val2: usize) -> c_int {
  195. // unsafe { syscall!(FUTEX, addr, op, val, val2, 0, 0) as c_int }
  196. unimplemented!()
  197. }
  198. fn futimens(fd: c_int, times: *const timespec) -> c_int {
  199. // e(unsafe { syscall!(UTIMENSAT, fd, ptr::null::<c_char>(), times, 0) }) as c_int
  200. unimplemented!()
  201. }
  202. fn utimens(path: &CStr, times: *const timespec) -> c_int {
  203. // e(unsafe { syscall!(UTIMENSAT, AT_FDCWD, path.as_ptr(), times, 0) }) as c_int
  204. unimplemented!()
  205. }
  206. fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char {
  207. // if e(unsafe { syscall!(GETCWD, buf, size) }) == !0 {
  208. // ptr::null_mut()
  209. // } else {
  210. // buf
  211. // }
  212. unimplemented!()
  213. }
  214. fn getdents(fd: c_int, dirents: *mut dirent, bytes: usize) -> c_int {
  215. unsafe { syscall!(SYS_GET_DENTS, fd, dirents, bytes) as c_int }
  216. }
  217. fn getegid() -> gid_t {
  218. // e(unsafe { syscall!(GETEGID) }) as gid_t
  219. return 0;
  220. }
  221. fn geteuid() -> uid_t {
  222. // e(unsafe { syscall!(GETEUID) }) as uid_t
  223. return 0;
  224. }
  225. fn getgid() -> gid_t {
  226. // e(unsafe { syscall!(GETGID) }) as gid_t
  227. return 0;
  228. }
  229. fn getpagesize() -> usize {
  230. // 4096
  231. 1024 * 1024 * 2
  232. }
  233. fn getpgid(pid: pid_t) -> pid_t {
  234. // e(unsafe { syscall!(GETPGID, pid) }) as pid_t
  235. return 0;
  236. }
  237. fn getpid() -> pid_t {
  238. e(unsafe { syscall!(SYS_GETPID) }) as pid_t
  239. }
  240. fn getppid() -> pid_t {
  241. // e(unsafe { syscall!(GETPPID) }) as pid_t
  242. return 0;
  243. }
  244. fn getrandom(buf: &mut [u8], flags: c_uint) -> ssize_t {
  245. // e(unsafe { syscall!(GETRANDOM, buf.as_mut_ptr(), buf.len(), flags) }) as ssize_t
  246. unimplemented!()
  247. }
  248. unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> c_int {
  249. // e(syscall!(GETRLIMIT, resource, rlim)) as c_int
  250. unimplemented!()
  251. }
  252. fn getsid(pid: pid_t) -> pid_t {
  253. // e(unsafe { syscall!(GETSID, pid) }) as pid_t
  254. return 0;
  255. }
  256. fn gettid() -> pid_t {
  257. // e(unsafe { syscall!(GETTID) }) as pid_t
  258. return Self::getpid();
  259. }
  260. fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> c_int {
  261. // e(unsafe { syscall!(GETTIMEOFDAY, tp, tzp) }) as c_int
  262. unimplemented!()
  263. }
  264. fn getuid() -> uid_t {
  265. // e(unsafe { syscall!(GETUID) }) as uid_t
  266. return 0;
  267. }
  268. fn lchown(path: &CStr, owner: uid_t, group: gid_t) -> c_int {
  269. // e(unsafe { syscall!(LCHOWN, path.as_ptr(), owner, group) }) as c_int
  270. return 0;
  271. }
  272. fn link(path1: &CStr, path2: &CStr) -> c_int {
  273. // e(unsafe {
  274. // syscall!(
  275. // LINKAT,
  276. // AT_FDCWD,
  277. // path1.as_ptr(),
  278. // AT_FDCWD,
  279. // path2.as_ptr(),
  280. // 0
  281. // )
  282. // }) as c_int
  283. unimplemented!()
  284. }
  285. fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t {
  286. e(unsafe { syscall!(SYS_LSEEK, fildes, offset, whence) }) as off_t
  287. }
  288. fn mkdir(path: &CStr, mode: mode_t) -> c_int {
  289. // e(unsafe { syscall!(MKDIRAT, AT_FDCWD, path.as_ptr(), mode) }) as c_int
  290. e(unsafe { syscall!(SYS_MKDIR, path.as_ptr(), mode) }) as c_int
  291. }
  292. fn mkfifo(path: &CStr, mode: mode_t) -> c_int {
  293. // e(unsafe { syscall!(MKNODAT, AT_FDCWD, path.as_ptr(), mode | S_IFIFO, 0) }) as c_int
  294. unimplemented!()
  295. }
  296. unsafe fn mlock(addr: *const c_void, len: usize) -> c_int {
  297. // e(syscall!(MLOCK, addr, len)) as c_int
  298. unimplemented!()
  299. }
  300. fn mlockall(flags: c_int) -> c_int {
  301. // e(unsafe { syscall!(MLOCKALL, flags) }) as c_int
  302. unimplemented!()
  303. }
  304. unsafe fn mmap(
  305. addr: *mut c_void,
  306. len: usize,
  307. prot: c_int,
  308. flags: c_int,
  309. fildes: c_int,
  310. off: off_t,
  311. ) -> *mut c_void {
  312. // e(syscall!(MMAP, addr, len, prot, flags, fildes, off)) as *mut c_void
  313. unimplemented!()
  314. }
  315. unsafe fn mprotect(addr: *mut c_void, len: usize, prot: c_int) -> c_int {
  316. // e(syscall!(MPROTECT, addr, len, prot)) as c_int
  317. unimplemented!()
  318. }
  319. unsafe fn msync(addr: *mut c_void, len: usize, flags: c_int) -> c_int {
  320. // e(syscall!(MSYNC, addr, len, flags)) as c_int
  321. unimplemented!()
  322. }
  323. unsafe fn munlock(addr: *const c_void, len: usize) -> c_int {
  324. // e(syscall!(MUNLOCK, addr, len)) as c_int
  325. unimplemented!()
  326. }
  327. fn munlockall() -> c_int {
  328. // e(unsafe { syscall!(MUNLOCKALL) }) as c_int
  329. unimplemented!()
  330. }
  331. unsafe fn munmap(addr: *mut c_void, len: usize) -> c_int {
  332. // e(syscall!(MUNMAP, addr, len)) as c_int
  333. unimplemented!()
  334. }
  335. fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
  336. e(unsafe { syscall!(SYS_NANOSLEEP, rqtp, rmtp) }) as c_int
  337. }
  338. fn open(path: &CStr, oflag: c_int, mode: mode_t) -> c_int {
  339. e(unsafe { syscall!(SYS_OPEN, path.as_ptr(), oflag, mode) }) as c_int
  340. }
  341. fn pipe2(fildes: &mut [c_int], flags: c_int) -> c_int {
  342. // e(unsafe { syscall!(PIPE2, fildes.as_mut_ptr(), flags) }) as c_int
  343. unimplemented!()
  344. }
  345. #[cfg(target_arch = "x86_64")]
  346. unsafe fn pte_clone(stack: *mut usize) -> pid_t {
  347. unimplemented!()
  348. // let flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD;
  349. // let pid;
  350. // asm!("
  351. // # Call clone syscall
  352. // syscall
  353. // # Check if child or parent
  354. // test rax, rax
  355. // jnz 1f
  356. // # Load registers
  357. // pop rax
  358. // pop rdi
  359. // pop rsi
  360. // pop rdx
  361. // pop rcx
  362. // pop r8
  363. // pop r9
  364. // # Call entry point
  365. // call rax
  366. // # Exit
  367. // mov rax, 60
  368. // xor rdi, rdi
  369. // syscall
  370. // # Invalid instruction on failure to exit
  371. // ud2
  372. // # Return PID if parent
  373. // 1:
  374. // ",
  375. // inout("rax") SYS_CLONE => pid,
  376. // inout("rdi") flags => _,
  377. // inout("rsi") stack => _,
  378. // inout("rdx") 0 => _,
  379. // inout("r10") 0 => _,
  380. // inout("r8") 0 => _,
  381. // //TODO: out("rbx") _,
  382. // out("rcx") _,
  383. // out("r9") _,
  384. // out("r11") _,
  385. // out("r12") _,
  386. // out("r13") _,
  387. // out("r14") _,
  388. // out("r15") _,
  389. // );
  390. // e(pid) as pid_t
  391. }
  392. fn read(fildes: c_int, buf: &mut [u8]) -> ssize_t {
  393. e(unsafe { syscall!(SYS_READ, fildes, buf.as_mut_ptr(), buf.len()) }) as ssize_t
  394. }
  395. fn readlink(pathname: &CStr, out: &mut [u8]) -> ssize_t {
  396. unimplemented!()
  397. // e(unsafe {
  398. // syscall!(
  399. // READLINKAT,
  400. // AT_FDCWD,
  401. // pathname.as_ptr(),
  402. // out.as_mut_ptr(),
  403. // out.len()
  404. // )
  405. // }) as ssize_t
  406. }
  407. fn rename(old: &CStr, new: &CStr) -> c_int {
  408. // e(unsafe { syscall!(RENAMEAT, AT_FDCWD, old.as_ptr(), AT_FDCWD, new.as_ptr()) }) as c_int
  409. unimplemented!()
  410. }
  411. fn rmdir(path: &CStr) -> c_int {
  412. e(unsafe { syscall!(SYS_UNLINK_AT, 0, path.as_ptr(), AT_REMOVEDIR) }) as c_int
  413. }
  414. fn sched_yield() -> c_int {
  415. // e(unsafe { syscall!(SCHED_YIELD) }) as c_int
  416. unimplemented!()
  417. }
  418. fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
  419. // e(unsafe { syscall!(SETPGID, pid, pgid) }) as c_int
  420. unimplemented!()
  421. }
  422. fn setregid(rgid: gid_t, egid: gid_t) -> c_int {
  423. // e(unsafe { syscall!(SETREGID, rgid, egid) }) as c_int
  424. unimplemented!()
  425. }
  426. fn setreuid(ruid: uid_t, euid: uid_t) -> c_int {
  427. // e(unsafe { syscall!(SETREUID, ruid, euid) }) as c_int
  428. unimplemented!()
  429. }
  430. fn symlink(path1: &CStr, path2: &CStr) -> c_int {
  431. // e(unsafe { syscall!(SYMLINKAT, path1.as_ptr(), AT_FDCWD, path2.as_ptr()) }) as c_int
  432. unimplemented!()
  433. }
  434. fn umask(mask: mode_t) -> mode_t {
  435. // unsafe { syscall!(UMASK, mask) as mode_t }
  436. unimplemented!()
  437. }
  438. fn uname(utsname: *mut utsname) -> c_int {
  439. // e(unsafe { syscall!(UNAME, utsname, 0) }) as c_int
  440. unimplemented!()
  441. }
  442. fn unlink(path: &CStr) -> c_int {
  443. e(unsafe { syscall!(SYS_UNLINK_AT, AT_FDCWD, path.as_ptr(), 0) }) as c_int
  444. }
  445. fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t {
  446. e(unsafe { syscall!(SYS_WAIT4, pid, stat_loc, options, 0) }) as pid_t
  447. }
  448. fn write(fildes: c_int, buf: &[u8]) -> ssize_t {
  449. e(unsafe { syscall!(SYS_WRITE, fildes, buf.as_ptr(), buf.len()) }) as ssize_t
  450. }
  451. fn verify() -> bool {
  452. // GETPID on Linux is 39, which does not exist on Redox
  453. // e(unsafe { dsc::syscall5(dsc::nr::GETPID, !0, !0, !0, !0, !0) }) != !0
  454. unimplemented!()
  455. }
  456. }