mod.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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, errno::ENOSYS, 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. return -ENOSYS;
  101. }
  102. fn close(fildes: c_int) -> c_int {
  103. e(unsafe { syscall!(SYS_CLOSE, fildes) }) as c_int
  104. }
  105. fn dup(fildes: c_int) -> c_int {
  106. e(unsafe { syscall!(SYS_DUP, fildes) }) as c_int
  107. }
  108. fn dup2(fildes: c_int, fildes2: c_int) -> c_int {
  109. e(unsafe { syscall!(SYS_DUP2, fildes, fildes2) }) as c_int
  110. }
  111. unsafe fn execve(path: &CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int {
  112. e(syscall!(SYS_EXECVE, path.as_ptr(), argv, envp)) as c_int
  113. }
  114. fn exit(status: c_int) -> ! {
  115. unsafe {
  116. syscall!(SYS_EXIT, status);
  117. }
  118. loop {}
  119. }
  120. fn fchdir(fildes: c_int) -> c_int {
  121. // e(unsafe { syscall!(FCHDIR, fildes) }) as c_int
  122. unimplemented!()
  123. }
  124. fn fchmod(fildes: c_int, mode: mode_t) -> c_int {
  125. // e(unsafe { syscall!(FCHMOD, fildes, mode) }) as c_int
  126. return 0;
  127. }
  128. fn fchown(fildes: c_int, owner: uid_t, group: gid_t) -> c_int {
  129. // e(unsafe { syscall!(FCHOWN, fildes, owner, group) }) as c_int
  130. return 0;
  131. }
  132. fn flock(fd: c_int, operation: c_int) -> c_int {
  133. // e(unsafe { syscall!(FLOCK, fd, operation) }) as c_int
  134. return 0;
  135. }
  136. fn fstat(fildes: c_int, buf: *mut stat) -> c_int {
  137. // let empty = b"\0";
  138. // let empty_ptr = empty.as_ptr() as *const c_char;
  139. // e(unsafe { syscall!(NEWFSTATAT, fildes, empty_ptr, buf, AT_EMPTY_PATH) }) as c_int
  140. // unimplemented!()
  141. e(unsafe { syscall!(SYS_FSTAT, fildes, buf) }) as c_int
  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!(SYS_GETTIMEOFDAY, tp, tzp) }) as c_int
  262. }
  263. fn getuid() -> uid_t {
  264. // e(unsafe { syscall!(GETUID) }) as uid_t
  265. return 0;
  266. }
  267. fn lchown(path: &CStr, owner: uid_t, group: gid_t) -> c_int {
  268. // e(unsafe { syscall!(LCHOWN, path.as_ptr(), owner, group) }) as c_int
  269. return 0;
  270. }
  271. fn link(path1: &CStr, path2: &CStr) -> c_int {
  272. // e(unsafe {
  273. // syscall!(
  274. // LINKAT,
  275. // AT_FDCWD,
  276. // path1.as_ptr(),
  277. // AT_FDCWD,
  278. // path2.as_ptr(),
  279. // 0
  280. // )
  281. // }) as c_int
  282. unimplemented!()
  283. }
  284. fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t {
  285. e(unsafe { syscall!(SYS_LSEEK, fildes, offset, whence) }) as off_t
  286. }
  287. fn mkdir(path: &CStr, mode: mode_t) -> c_int {
  288. // e(unsafe { syscall!(MKDIRAT, AT_FDCWD, path.as_ptr(), mode) }) as c_int
  289. e(unsafe { syscall!(SYS_MKDIR, path.as_ptr(), mode) }) as c_int
  290. }
  291. fn mkfifo(path: &CStr, mode: mode_t) -> c_int {
  292. // e(unsafe { syscall!(MKNODAT, AT_FDCWD, path.as_ptr(), mode | S_IFIFO, 0) }) as c_int
  293. unimplemented!()
  294. }
  295. unsafe fn mlock(addr: *const c_void, len: usize) -> c_int {
  296. // e(syscall!(MLOCK, addr, len)) as c_int
  297. unimplemented!()
  298. }
  299. fn mlockall(flags: c_int) -> c_int {
  300. // e(unsafe { syscall!(MLOCKALL, flags) }) as c_int
  301. unimplemented!()
  302. }
  303. unsafe fn mmap(
  304. addr: *mut c_void,
  305. len: usize,
  306. prot: c_int,
  307. flags: c_int,
  308. fildes: c_int,
  309. off: off_t,
  310. ) -> *mut c_void {
  311. // e(syscall!(MMAP, addr, len, prot, flags, fildes, off)) as *mut c_void
  312. // Due to DragonOS has not implement mmap yet,
  313. // set the return value here as 0xFFFF_FFFF_FFFF_FFFF to match dlmalloc's requirements.
  314. // See https://opengrok.ringotek.cn/xref/dragonos-relibc/src/c/dlmalloc.c?r=031194b9#5439
  315. return !(0usize) as *mut c_void;
  316. // unimplemented!()
  317. }
  318. unsafe fn mprotect(addr: *mut c_void, len: usize, prot: c_int) -> c_int {
  319. // e(syscall!(MPROTECT, addr, len, prot)) as c_int
  320. unimplemented!()
  321. }
  322. unsafe fn msync(addr: *mut c_void, len: usize, flags: c_int) -> c_int {
  323. // e(syscall!(MSYNC, addr, len, flags)) as c_int
  324. unimplemented!()
  325. }
  326. unsafe fn munlock(addr: *const c_void, len: usize) -> c_int {
  327. // e(syscall!(MUNLOCK, addr, len)) as c_int
  328. unimplemented!()
  329. }
  330. fn munlockall() -> c_int {
  331. // e(unsafe { syscall!(MUNLOCKALL) }) as c_int
  332. unimplemented!()
  333. }
  334. unsafe fn munmap(addr: *mut c_void, len: usize) -> c_int {
  335. // e(syscall!(MUNMAP, addr, len)) as c_int
  336. unimplemented!()
  337. }
  338. fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
  339. e(unsafe { syscall!(SYS_NANOSLEEP, rqtp, rmtp) }) as c_int
  340. }
  341. fn open(path: &CStr, oflag: c_int, mode: mode_t) -> c_int {
  342. e(unsafe { syscall!(SYS_OPEN, path.as_ptr(), oflag, mode) }) as c_int
  343. }
  344. fn pipe2(fildes: &mut [c_int], flags: c_int) -> c_int {
  345. // e(unsafe { syscall!(PIPE2, fildes.as_mut_ptr(), flags) }) as c_int
  346. unimplemented!()
  347. }
  348. #[cfg(target_arch = "x86_64")]
  349. unsafe fn pte_clone(stack: *mut usize) -> pid_t {
  350. unimplemented!()
  351. // let flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD;
  352. // let pid;
  353. // asm!("
  354. // # Call clone syscall
  355. // syscall
  356. // # Check if child or parent
  357. // test rax, rax
  358. // jnz 1f
  359. // # Load registers
  360. // pop rax
  361. // pop rdi
  362. // pop rsi
  363. // pop rdx
  364. // pop rcx
  365. // pop r8
  366. // pop r9
  367. // # Call entry point
  368. // call rax
  369. // # Exit
  370. // mov rax, 60
  371. // xor rdi, rdi
  372. // syscall
  373. // # Invalid instruction on failure to exit
  374. // ud2
  375. // # Return PID if parent
  376. // 1:
  377. // ",
  378. // inout("rax") SYS_CLONE => pid,
  379. // inout("rdi") flags => _,
  380. // inout("rsi") stack => _,
  381. // inout("rdx") 0 => _,
  382. // inout("r10") 0 => _,
  383. // inout("r8") 0 => _,
  384. // //TODO: out("rbx") _,
  385. // out("rcx") _,
  386. // out("r9") _,
  387. // out("r11") _,
  388. // out("r12") _,
  389. // out("r13") _,
  390. // out("r14") _,
  391. // out("r15") _,
  392. // );
  393. // e(pid) as pid_t
  394. }
  395. fn read(fildes: c_int, buf: &mut [u8]) -> ssize_t {
  396. e(unsafe { syscall!(SYS_READ, fildes, buf.as_mut_ptr(), buf.len()) }) as ssize_t
  397. }
  398. fn readlink(pathname: &CStr, out: &mut [u8]) -> ssize_t {
  399. unimplemented!()
  400. // e(unsafe {
  401. // syscall!(
  402. // READLINKAT,
  403. // AT_FDCWD,
  404. // pathname.as_ptr(),
  405. // out.as_mut_ptr(),
  406. // out.len()
  407. // )
  408. // }) as ssize_t
  409. }
  410. fn rename(old: &CStr, new: &CStr) -> c_int {
  411. // e(unsafe { syscall!(RENAMEAT, AT_FDCWD, old.as_ptr(), AT_FDCWD, new.as_ptr()) }) as c_int
  412. unimplemented!()
  413. }
  414. fn rmdir(path: &CStr) -> c_int {
  415. e(unsafe { syscall!(SYS_UNLINK_AT, 0, path.as_ptr(), AT_REMOVEDIR) }) as c_int
  416. }
  417. fn sched_yield() -> c_int {
  418. // e(unsafe { syscall!(SCHED_YIELD) }) as c_int
  419. unimplemented!()
  420. }
  421. fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
  422. // e(unsafe { syscall!(SETPGID, pid, pgid) }) as c_int
  423. unimplemented!()
  424. }
  425. fn setregid(rgid: gid_t, egid: gid_t) -> c_int {
  426. // e(unsafe { syscall!(SETREGID, rgid, egid) }) as c_int
  427. unimplemented!()
  428. }
  429. fn setreuid(ruid: uid_t, euid: uid_t) -> c_int {
  430. // e(unsafe { syscall!(SETREUID, ruid, euid) }) as c_int
  431. unimplemented!()
  432. }
  433. fn symlink(path1: &CStr, path2: &CStr) -> c_int {
  434. // e(unsafe { syscall!(SYMLINKAT, path1.as_ptr(), AT_FDCWD, path2.as_ptr()) }) as c_int
  435. unimplemented!()
  436. }
  437. fn umask(mask: mode_t) -> mode_t {
  438. // unsafe { syscall!(UMASK, mask) as mode_t }
  439. unimplemented!()
  440. }
  441. fn uname(utsname: *mut utsname) -> c_int {
  442. // e(unsafe { syscall!(UNAME, utsname, 0) }) as c_int
  443. unimplemented!()
  444. }
  445. fn unlink(path: &CStr) -> c_int {
  446. e(unsafe { syscall!(SYS_UNLINK_AT, AT_FDCWD, path.as_ptr(), 0) }) as c_int
  447. }
  448. fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t {
  449. e(unsafe { syscall!(SYS_WAIT4, pid, stat_loc, options, 0) }) as pid_t
  450. }
  451. fn write(fildes: c_int, buf: &[u8]) -> ssize_t {
  452. e(unsafe { syscall!(SYS_WRITE, fildes, buf.as_ptr(), buf.len()) }) as ssize_t
  453. }
  454. fn verify() -> bool {
  455. // GETPID on Linux is 39, which does not exist on Redox
  456. // e(unsafe { dsc::syscall5(dsc::nr::GETPID, !0, !0, !0, !0, !0) }) != !0
  457. // unimplemented!()
  458. return true;
  459. }
  460. }