mod.rs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. use core::{
  2. ffi::{c_int, c_void},
  3. sync::atomic::{AtomicBool, Ordering},
  4. };
  5. use crate::{
  6. arch::{ipc::signal::SigSet, syscall::nr::*},
  7. filesystem::vfs::syscall::{PosixStatfs, PosixStatx},
  8. ipc::shm::{ShmCtlCmd, ShmFlags, ShmId, ShmKey},
  9. libs::{futex::constant::FutexFlag, rand::GRandFlags},
  10. mm::{page::PAGE_4K_SIZE, syscall::MremapFlags},
  11. net::syscall::MsgHdr,
  12. process::{
  13. fork::KernelCloneArgs,
  14. resource::{RLimit64, RUsage},
  15. ProcessFlags, ProcessManager,
  16. },
  17. sched::{schedule, SchedMode},
  18. syscall::user_access::check_and_clone_cstr,
  19. };
  20. use log::{info, warn};
  21. use num_traits::FromPrimitive;
  22. use system_error::SystemError;
  23. use crate::{
  24. arch::{interrupt::TrapFrame, MMArch},
  25. filesystem::vfs::{
  26. fcntl::{AtFlags, FcntlCommand},
  27. file::FileMode,
  28. syscall::{ModeType, PosixKstat, UtimensFlags},
  29. MAX_PATHLEN,
  30. },
  31. libs::align::page_align_up,
  32. mm::{verify_area, MemoryManagementArch, VirtAddr},
  33. net::syscall::SockAddr,
  34. process::{fork::CloneFlags, syscall::PosixOldUtsName, Pid},
  35. time::{
  36. syscall::{PosixTimeZone, PosixTimeval},
  37. PosixTimeSpec,
  38. },
  39. };
  40. use self::{
  41. misc::SysInfo,
  42. user_access::{UserBufferReader, UserBufferWriter},
  43. };
  44. pub mod misc;
  45. pub mod user_access;
  46. // 与linux不一致的调用,在linux基础上累加
  47. pub const SYS_PUT_STRING: usize = 100000;
  48. pub const SYS_SBRK: usize = 100001;
  49. /// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc
  50. pub const SYS_CLOCK: usize = 100002;
  51. pub const SYS_SCHED: usize = 100003;
  52. #[derive(Debug)]
  53. pub struct Syscall;
  54. impl Syscall {
  55. /// 初始化系统调用
  56. #[inline(never)]
  57. pub fn init() -> Result<(), SystemError> {
  58. static INIT_FLAG: AtomicBool = AtomicBool::new(false);
  59. let prev = INIT_FLAG.swap(true, Ordering::SeqCst);
  60. if prev {
  61. panic!("Cannot initialize syscall more than once!");
  62. }
  63. info!("Initializing syscall...");
  64. let r = crate::arch::syscall::arch_syscall_init();
  65. info!("Syscall init successfully!");
  66. return r;
  67. }
  68. /// 系统调用分发器,用于分发系统调用。
  69. ///
  70. /// 与[handle]不同,这个函数会捕获系统调用处理函数的panic,返回错误码。
  71. #[cfg(feature = "backtrace")]
  72. pub fn catch_handle(
  73. syscall_num: usize,
  74. args: &[usize],
  75. frame: &mut TrapFrame,
  76. ) -> Result<usize, SystemError> {
  77. let res = unwinding::panic::catch_unwind(|| Self::handle(syscall_num, args, frame));
  78. res.unwrap_or_else(|_| loop {
  79. core::hint::spin_loop();
  80. })
  81. }
  82. /// @brief 系统调用分发器,用于分发系统调用。
  83. ///
  84. /// 这个函数内,需要根据系统调用号,调用对应的系统调用处理函数。
  85. /// 并且,对于用户态传入的指针参数,需要在本函数内进行越界检查,防止访问到内核空间。
  86. #[inline(never)]
  87. pub fn handle(
  88. syscall_num: usize,
  89. args: &[usize],
  90. frame: &mut TrapFrame,
  91. ) -> Result<usize, SystemError> {
  92. let r = match syscall_num {
  93. SYS_PUT_STRING => {
  94. Self::put_string(args[0] as *const u8, args[1] as u32, args[2] as u32)
  95. }
  96. #[cfg(target_arch = "x86_64")]
  97. SYS_OPEN => {
  98. let path = args[0] as *const u8;
  99. let flags = args[1] as u32;
  100. let mode = args[2] as u32;
  101. Self::open(path, flags, mode, true)
  102. }
  103. #[cfg(target_arch = "x86_64")]
  104. SYS_RENAME => {
  105. let oldname: *const u8 = args[0] as *const u8;
  106. let newname: *const u8 = args[1] as *const u8;
  107. Self::do_renameat2(
  108. AtFlags::AT_FDCWD.bits(),
  109. oldname,
  110. AtFlags::AT_FDCWD.bits(),
  111. newname,
  112. 0,
  113. )
  114. }
  115. #[cfg(target_arch = "x86_64")]
  116. SYS_RENAMEAT => {
  117. let oldfd = args[0] as i32;
  118. let oldname: *const u8 = args[1] as *const u8;
  119. let newfd = args[2] as i32;
  120. let newname: *const u8 = args[3] as *const u8;
  121. Self::do_renameat2(oldfd, oldname, newfd, newname, 0)
  122. }
  123. SYS_RENAMEAT2 => {
  124. let oldfd = args[0] as i32;
  125. let oldname: *const u8 = args[1] as *const u8;
  126. let newfd = args[2] as i32;
  127. let newname: *const u8 = args[3] as *const u8;
  128. let flags = args[4] as u32;
  129. Self::do_renameat2(oldfd, oldname, newfd, newname, flags)
  130. }
  131. SYS_OPENAT => {
  132. let dirfd = args[0] as i32;
  133. let path = args[1] as *const u8;
  134. let flags = args[2] as u32;
  135. let mode = args[3] as u32;
  136. Self::openat(dirfd, path, flags, mode, true)
  137. }
  138. SYS_CLOSE => {
  139. let fd = args[0];
  140. Self::close(fd)
  141. }
  142. SYS_READ => {
  143. let fd = args[0] as i32;
  144. let buf_vaddr = args[1];
  145. let len = args[2];
  146. let from_user = frame.is_from_user();
  147. let mut user_buffer_writer =
  148. UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
  149. let user_buf = user_buffer_writer.buffer(0)?;
  150. Self::read(fd, user_buf)
  151. }
  152. SYS_WRITE => {
  153. let fd = args[0] as i32;
  154. let buf_vaddr = args[1];
  155. let len = args[2];
  156. let from_user = frame.is_from_user();
  157. let user_buffer_reader =
  158. UserBufferReader::new(buf_vaddr as *const u8, len, from_user)?;
  159. let user_buf = user_buffer_reader.read_from_user(0)?;
  160. Self::write(fd, user_buf)
  161. }
  162. SYS_LSEEK => {
  163. let fd = args[0] as i32;
  164. let offset = args[1] as i64;
  165. let whence = args[2] as u32;
  166. Self::lseek(fd, offset, whence)
  167. }
  168. SYS_PREAD64 => {
  169. let fd = args[0] as i32;
  170. let buf_vaddr = args[1];
  171. let len = args[2];
  172. let offset = args[3];
  173. let mut user_buffer_writer =
  174. UserBufferWriter::new(buf_vaddr as *mut u8, len, frame.is_from_user())?;
  175. let buf = user_buffer_writer.buffer(0)?;
  176. Self::pread(fd, buf, len, offset)
  177. }
  178. SYS_PWRITE64 => {
  179. let fd = args[0] as i32;
  180. let buf_vaddr = args[1];
  181. let len = args[2];
  182. let offset = args[3];
  183. let user_buffer_reader =
  184. UserBufferReader::new(buf_vaddr as *const u8, len, frame.is_from_user())?;
  185. let buf = user_buffer_reader.read_from_user(0)?;
  186. Self::pwrite(fd, buf, len, offset)
  187. }
  188. SYS_IOCTL => {
  189. let fd = args[0];
  190. let cmd = args[1];
  191. let data = args[2];
  192. Self::ioctl(fd, cmd as u32, data)
  193. }
  194. #[cfg(target_arch = "x86_64")]
  195. SYS_FORK => Self::fork(frame),
  196. #[cfg(target_arch = "x86_64")]
  197. SYS_VFORK => Self::vfork(frame),
  198. SYS_BRK => {
  199. let new_brk = VirtAddr::new(args[0]);
  200. Self::brk(new_brk).map(|vaddr| vaddr.data())
  201. }
  202. SYS_SBRK => {
  203. let increment = args[0] as isize;
  204. Self::sbrk(increment).map(|vaddr: VirtAddr| vaddr.data())
  205. }
  206. SYS_REBOOT => {
  207. let magic1 = args[0] as u32;
  208. let magic2 = args[1] as u32;
  209. let cmd = args[2] as u32;
  210. let arg = args[3];
  211. Self::reboot(magic1, magic2, cmd, arg)
  212. }
  213. SYS_CHDIR => {
  214. let r = args[0] as *const u8;
  215. Self::chdir(r)
  216. }
  217. SYS_FCHDIR => {
  218. let fd = args[0] as i32;
  219. Self::fchdir(fd)
  220. }
  221. #[allow(unreachable_patterns)]
  222. SYS_GETDENTS64 | SYS_GETDENTS => {
  223. let fd = args[0] as i32;
  224. let buf_vaddr = args[1];
  225. let len = args[2];
  226. let virt_addr: VirtAddr = VirtAddr::new(buf_vaddr);
  227. // 判断缓冲区是否来自用户态,进行权限校验
  228. let res = if frame.is_from_user() && verify_area(virt_addr, len).is_err() {
  229. // 来自用户态,而buffer在内核态,这样的操作不被允许
  230. Err(SystemError::EPERM)
  231. } else if buf_vaddr == 0 {
  232. Err(SystemError::EFAULT)
  233. } else {
  234. let buf: &mut [u8] = unsafe {
  235. core::slice::from_raw_parts_mut::<'static, u8>(buf_vaddr as *mut u8, len)
  236. };
  237. Self::getdents(fd, buf)
  238. };
  239. res
  240. }
  241. SYS_EXECVE => {
  242. let path_ptr = args[0];
  243. let argv_ptr = args[1];
  244. let env_ptr = args[2];
  245. let virt_path_ptr = VirtAddr::new(path_ptr);
  246. let virt_argv_ptr = VirtAddr::new(argv_ptr);
  247. let virt_env_ptr = VirtAddr::new(env_ptr);
  248. // 权限校验
  249. if frame.is_from_user()
  250. && (verify_area(virt_path_ptr, MAX_PATHLEN).is_err()
  251. || verify_area(virt_argv_ptr, PAGE_4K_SIZE).is_err())
  252. || verify_area(virt_env_ptr, PAGE_4K_SIZE).is_err()
  253. {
  254. Err(SystemError::EFAULT)
  255. } else {
  256. Self::execve(
  257. path_ptr as *const u8,
  258. argv_ptr as *const *const u8,
  259. env_ptr as *const *const u8,
  260. frame,
  261. )
  262. .map(|_| 0)
  263. }
  264. }
  265. SYS_WAIT4 => {
  266. let pid = args[0] as i32;
  267. let wstatus = args[1] as *mut i32;
  268. let options = args[2] as c_int;
  269. let rusage = args[3] as *mut c_void;
  270. // 权限校验
  271. // todo: 引入rusage之后,更正以下权限校验代码中,rusage的大小
  272. Self::wait4(pid.into(), wstatus, options, rusage)
  273. }
  274. SYS_EXIT => {
  275. let exit_code = args[0];
  276. Self::exit(exit_code)
  277. }
  278. #[cfg(target_arch = "x86_64")]
  279. SYS_MKDIR => {
  280. let path = args[0] as *const u8;
  281. let mode = args[1];
  282. Self::mkdir(path, mode)
  283. }
  284. SYS_MKDIRAT => {
  285. let dirfd = args[0] as i32;
  286. let path = args[1] as *const u8;
  287. let mode = args[2];
  288. Self::mkdir_at(dirfd, path, mode)
  289. }
  290. SYS_NANOSLEEP => {
  291. let req = args[0] as *const PosixTimeSpec;
  292. let rem = args[1] as *mut PosixTimeSpec;
  293. let virt_req = VirtAddr::new(req as usize);
  294. let virt_rem = VirtAddr::new(rem as usize);
  295. if frame.is_from_user()
  296. && (verify_area(virt_req, core::mem::size_of::<PosixTimeSpec>()).is_err()
  297. || verify_area(virt_rem, core::mem::size_of::<PosixTimeSpec>()).is_err())
  298. {
  299. Err(SystemError::EFAULT)
  300. } else {
  301. Self::nanosleep(req, rem)
  302. }
  303. }
  304. SYS_CLOCK => Self::clock(),
  305. #[cfg(target_arch = "x86_64")]
  306. SYS_PIPE => {
  307. let pipefd: *mut i32 = args[0] as *mut c_int;
  308. if pipefd.is_null() {
  309. Err(SystemError::EFAULT)
  310. } else {
  311. Self::pipe2(pipefd, FileMode::empty())
  312. }
  313. }
  314. SYS_PIPE2 => {
  315. let pipefd: *mut i32 = args[0] as *mut c_int;
  316. let arg1 = args[1];
  317. if pipefd.is_null() {
  318. Err(SystemError::EFAULT)
  319. } else {
  320. let flags = FileMode::from_bits_truncate(arg1 as u32);
  321. Self::pipe2(pipefd, flags)
  322. }
  323. }
  324. SYS_UNLINKAT => {
  325. let dirfd = args[0] as i32;
  326. let path = args[1] as *const u8;
  327. let flags = args[2] as u32;
  328. Self::unlinkat(dirfd, path, flags)
  329. }
  330. #[cfg(target_arch = "x86_64")]
  331. SYS_SYMLINK => {
  332. let oldname = args[0] as *const u8;
  333. let newname = args[1] as *const u8;
  334. Self::symlink(oldname, newname)
  335. }
  336. SYS_SYMLINKAT => {
  337. let oldname = args[0] as *const u8;
  338. let newdfd = args[1] as i32;
  339. let newname = args[2] as *const u8;
  340. Self::symlinkat(oldname, newdfd, newname)
  341. }
  342. #[cfg(target_arch = "x86_64")]
  343. SYS_RMDIR => {
  344. let path = args[0] as *const u8;
  345. Self::rmdir(path)
  346. }
  347. #[cfg(target_arch = "x86_64")]
  348. SYS_LINK => {
  349. let old = args[0] as *const u8;
  350. let new = args[1] as *const u8;
  351. return Self::link(old, new);
  352. }
  353. SYS_LINKAT => {
  354. let oldfd = args[0] as i32;
  355. let old = args[1] as *const u8;
  356. let newfd = args[2] as i32;
  357. let new = args[3] as *const u8;
  358. let flags = args[4] as i32;
  359. return Self::linkat(oldfd, old, newfd, new, flags);
  360. }
  361. #[cfg(target_arch = "x86_64")]
  362. SYS_UNLINK => {
  363. let path = args[0] as *const u8;
  364. Self::unlink(path)
  365. }
  366. SYS_KILL => {
  367. let pid = Pid::new(args[0]);
  368. let sig = args[1] as c_int;
  369. // debug!("KILL SYSCALL RECEIVED");
  370. Self::kill(pid, sig)
  371. }
  372. SYS_RT_SIGACTION => {
  373. let sig = args[0] as c_int;
  374. let act = args[1];
  375. let old_act = args[2];
  376. Self::sigaction(sig, act, old_act, frame.is_from_user())
  377. }
  378. SYS_GETPID => Self::getpid().map(|pid| pid.into()),
  379. SYS_SCHED => {
  380. warn!("syscall sched");
  381. schedule(SchedMode::SM_NONE);
  382. Ok(0)
  383. }
  384. SYS_DUP => {
  385. let oldfd: i32 = args[0] as c_int;
  386. Self::dup(oldfd)
  387. }
  388. #[cfg(target_arch = "x86_64")]
  389. SYS_DUP2 => {
  390. let oldfd: i32 = args[0] as c_int;
  391. let newfd: i32 = args[1] as c_int;
  392. Self::dup2(oldfd, newfd)
  393. }
  394. SYS_DUP3 => {
  395. let oldfd: i32 = args[0] as c_int;
  396. let newfd: i32 = args[1] as c_int;
  397. let flags: u32 = args[2] as u32;
  398. Self::dup3(oldfd, newfd, flags)
  399. }
  400. SYS_SOCKET => Self::socket(args[0], args[1], args[2]),
  401. SYS_SETSOCKOPT => {
  402. let optval = args[3] as *const u8;
  403. let optlen = args[4];
  404. let virt_optval = VirtAddr::new(optval as usize);
  405. // 验证optval的地址是否合法
  406. if verify_area(virt_optval, optlen).is_err() {
  407. // 地址空间超出了用户空间的范围,不合法
  408. Err(SystemError::EFAULT)
  409. } else {
  410. let data: &[u8] = unsafe { core::slice::from_raw_parts(optval, optlen) };
  411. Self::setsockopt(args[0], args[1], args[2], data)
  412. }
  413. }
  414. SYS_GETSOCKOPT => {
  415. let optval = args[3] as *mut u8;
  416. let optlen = args[4] as *mut usize;
  417. let virt_optval = VirtAddr::new(optval as usize);
  418. let virt_optlen = VirtAddr::new(optlen as usize);
  419. let security_check = || {
  420. // 验证optval的地址是否合法
  421. if verify_area(virt_optval, PAGE_4K_SIZE).is_err() {
  422. // 地址空间超出了用户空间的范围,不合法
  423. return Err(SystemError::EFAULT);
  424. }
  425. // 验证optlen的地址是否合法
  426. if verify_area(virt_optlen, core::mem::size_of::<u32>()).is_err() {
  427. // 地址空间超出了用户空间的范围,不合法
  428. return Err(SystemError::EFAULT);
  429. }
  430. return Ok(());
  431. };
  432. let r = security_check();
  433. if let Err(e) = r {
  434. Err(e)
  435. } else {
  436. Self::getsockopt(args[0], args[1], args[2], optval, optlen as *mut u32)
  437. }
  438. }
  439. SYS_CONNECT => {
  440. let addr = args[1] as *const SockAddr;
  441. let addrlen = args[2];
  442. let virt_addr = VirtAddr::new(addr as usize);
  443. // 验证addr的地址是否合法
  444. if verify_area(virt_addr, addrlen).is_err() {
  445. // 地址空间超出了用户空间的范围,不合法
  446. Err(SystemError::EFAULT)
  447. } else {
  448. Self::connect(args[0], addr, addrlen)
  449. }
  450. }
  451. SYS_BIND => {
  452. let addr = args[1] as *const SockAddr;
  453. let addrlen = args[2];
  454. let virt_addr = VirtAddr::new(addr as usize);
  455. // 验证addr的地址是否合法
  456. if verify_area(virt_addr, addrlen).is_err() {
  457. // 地址空间超出了用户空间的范围,不合法
  458. Err(SystemError::EFAULT)
  459. } else {
  460. Self::bind(args[0], addr, addrlen)
  461. }
  462. }
  463. SYS_SENDTO => {
  464. let buf = args[1] as *const u8;
  465. let len = args[2];
  466. let flags = args[3] as u32;
  467. let addr = args[4] as *const SockAddr;
  468. let addrlen = args[5];
  469. let virt_buf = VirtAddr::new(buf as usize);
  470. let virt_addr = VirtAddr::new(addr as usize);
  471. // 验证buf的地址是否合法
  472. if verify_area(virt_buf, len).is_err() || verify_area(virt_addr, addrlen).is_err() {
  473. // 地址空间超出了用户空间的范围,不合法
  474. Err(SystemError::EFAULT)
  475. } else {
  476. let data: &[u8] = unsafe { core::slice::from_raw_parts(buf, len) };
  477. Self::sendto(args[0], data, flags, addr, addrlen)
  478. }
  479. }
  480. SYS_RECVFROM => {
  481. let buf = args[1] as *mut u8;
  482. let len = args[2];
  483. let flags = args[3] as u32;
  484. let addr = args[4] as *mut SockAddr;
  485. let addrlen = args[5] as *mut usize;
  486. let virt_buf = VirtAddr::new(buf as usize);
  487. let virt_addrlen = VirtAddr::new(addrlen as usize);
  488. let virt_addr = VirtAddr::new(addr as usize);
  489. let security_check = || {
  490. // 验证buf的地址是否合法
  491. if verify_area(virt_buf, len).is_err() {
  492. // 地址空间超出了用户空间的范围,不合法
  493. return Err(SystemError::EFAULT);
  494. }
  495. // 验证addrlen的地址是否合法
  496. if verify_area(virt_addrlen, core::mem::size_of::<u32>()).is_err() {
  497. // 地址空间超出了用户空间的范围,不合法
  498. return Err(SystemError::EFAULT);
  499. }
  500. if verify_area(virt_addr, core::mem::size_of::<SockAddr>()).is_err() {
  501. // 地址空间超出了用户空间的范围,不合法
  502. return Err(SystemError::EFAULT);
  503. }
  504. return Ok(());
  505. };
  506. let r = security_check();
  507. if let Err(e) = r {
  508. Err(e)
  509. } else {
  510. let buf = unsafe { core::slice::from_raw_parts_mut(buf, len) };
  511. Self::recvfrom(args[0], buf, flags, addr, addrlen as *mut u32)
  512. }
  513. }
  514. SYS_RECVMSG => {
  515. let msg = args[1] as *mut MsgHdr;
  516. let flags = args[2] as u32;
  517. let mut user_buffer_writer = UserBufferWriter::new(
  518. msg,
  519. core::mem::size_of::<MsgHdr>(),
  520. frame.is_from_user(),
  521. )?;
  522. let buffer = user_buffer_writer.buffer::<MsgHdr>(0)?;
  523. let msg = &mut buffer[0];
  524. Self::recvmsg(args[0], msg, flags)
  525. }
  526. SYS_LISTEN => Self::listen(args[0], args[1]),
  527. SYS_SHUTDOWN => Self::shutdown(args[0], args[1]),
  528. SYS_ACCEPT => Self::accept(args[0], args[1] as *mut SockAddr, args[2] as *mut u32),
  529. SYS_ACCEPT4 => Self::accept4(
  530. args[0],
  531. args[1] as *mut SockAddr,
  532. args[2] as *mut u32,
  533. args[3] as u32,
  534. ),
  535. SYS_GETSOCKNAME => {
  536. Self::getsockname(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
  537. }
  538. SYS_GETPEERNAME => {
  539. Self::getpeername(args[0], args[1] as *mut SockAddr, args[2] as *mut u32)
  540. }
  541. SYS_GETTIMEOFDAY => {
  542. let timeval = args[0] as *mut PosixTimeval;
  543. let timezone_ptr = args[1] as *mut PosixTimeZone;
  544. Self::gettimeofday(timeval, timezone_ptr)
  545. }
  546. SYS_MMAP => {
  547. let len = page_align_up(args[1]);
  548. let virt_addr = VirtAddr::new(args[0]);
  549. if verify_area(virt_addr, len).is_err() {
  550. Err(SystemError::EFAULT)
  551. } else {
  552. Self::mmap(
  553. VirtAddr::new(args[0]),
  554. len,
  555. args[2],
  556. args[3],
  557. args[4] as i32,
  558. args[5],
  559. )
  560. }
  561. }
  562. SYS_MREMAP => {
  563. let old_vaddr = VirtAddr::new(args[0]);
  564. let old_len = args[1];
  565. let new_len = args[2];
  566. let mremap_flags = MremapFlags::from_bits_truncate(args[3] as u8);
  567. let new_vaddr = VirtAddr::new(args[4]);
  568. Self::mremap(old_vaddr, old_len, new_len, mremap_flags, new_vaddr)
  569. }
  570. SYS_MUNMAP => {
  571. let addr = args[0];
  572. let len = page_align_up(args[1]);
  573. if addr & (MMArch::PAGE_SIZE - 1) != 0 {
  574. // The addr argument is not a multiple of the page size
  575. Err(SystemError::EINVAL)
  576. } else {
  577. Self::munmap(VirtAddr::new(addr), len)
  578. }
  579. }
  580. SYS_MPROTECT => {
  581. let addr = args[0];
  582. let len = page_align_up(args[1]);
  583. if addr & (MMArch::PAGE_SIZE - 1) != 0 {
  584. // The addr argument is not a multiple of the page size
  585. Err(SystemError::EINVAL)
  586. } else {
  587. Self::mprotect(VirtAddr::new(addr), len, args[2])
  588. }
  589. }
  590. SYS_GETCWD => {
  591. let buf = args[0] as *mut u8;
  592. let size = args[1];
  593. let security_check = || {
  594. verify_area(VirtAddr::new(buf as usize), size)?;
  595. return Ok(());
  596. };
  597. let r = security_check();
  598. if let Err(e) = r {
  599. Err(e)
  600. } else {
  601. let buf = unsafe { core::slice::from_raw_parts_mut(buf, size) };
  602. Self::getcwd(buf).map(|ptr| ptr.data())
  603. }
  604. }
  605. SYS_GETPGID => Self::getpgid(Pid::new(args[0])).map(|pid| pid.into()),
  606. SYS_GETPPID => Self::getppid().map(|pid| pid.into()),
  607. SYS_FSTAT => {
  608. let fd = args[0] as i32;
  609. let kstat: *mut PosixKstat = args[1] as *mut PosixKstat;
  610. let vaddr = VirtAddr::new(kstat as usize);
  611. // FIXME 由于c中的verify_area与rust中的verify_area重名,所以在引入时加了前缀区分
  612. // TODO 应该将用了c版本的verify_area都改为rust的verify_area
  613. match verify_area(vaddr, core::mem::size_of::<PosixKstat>()) {
  614. Ok(_) => Self::fstat(fd, kstat),
  615. Err(e) => Err(e),
  616. }
  617. }
  618. SYS_FCNTL => {
  619. let fd = args[0] as i32;
  620. let cmd: Option<FcntlCommand> =
  621. <FcntlCommand as FromPrimitive>::from_u32(args[1] as u32);
  622. let arg = args[2] as i32;
  623. let res = if let Some(cmd) = cmd {
  624. Self::fcntl(fd, cmd, arg)
  625. } else {
  626. Err(SystemError::EINVAL)
  627. };
  628. // debug!("FCNTL: fd: {}, cmd: {:?}, arg: {}, res: {:?}", fd, cmd, arg, res);
  629. res
  630. }
  631. SYS_FTRUNCATE => {
  632. let fd = args[0] as i32;
  633. let len = args[1];
  634. let res = Self::ftruncate(fd, len);
  635. // debug!("FTRUNCATE: fd: {}, len: {}, res: {:?}", fd, len, res);
  636. res
  637. }
  638. #[cfg(target_arch = "x86_64")]
  639. SYS_MKNOD => {
  640. let path = args[0];
  641. let flags = args[1];
  642. let dev_t = args[2];
  643. let flags: ModeType = ModeType::from_bits_truncate(flags as u32);
  644. Self::mknod(
  645. path as *const u8,
  646. flags,
  647. crate::driver::base::device::device_number::DeviceNumber::from(dev_t as u32),
  648. )
  649. }
  650. SYS_CLONE => {
  651. let parent_tid = VirtAddr::new(args[2]);
  652. let child_tid = VirtAddr::new(args[3]);
  653. // 地址校验
  654. verify_area(parent_tid, core::mem::size_of::<i32>())?;
  655. verify_area(child_tid, core::mem::size_of::<i32>())?;
  656. let mut clone_args = KernelCloneArgs::new();
  657. clone_args.flags = CloneFlags::from_bits_truncate(args[0] as u64);
  658. clone_args.stack = args[1];
  659. clone_args.parent_tid = parent_tid;
  660. clone_args.child_tid = child_tid;
  661. clone_args.tls = args[4];
  662. Self::clone(frame, clone_args)
  663. }
  664. SYS_FUTEX => {
  665. let uaddr = VirtAddr::new(args[0]);
  666. let operation = FutexFlag::from_bits(args[1] as u32).ok_or(SystemError::ENOSYS)?;
  667. let val = args[2] as u32;
  668. let utime = args[3];
  669. let uaddr2 = VirtAddr::new(args[4]);
  670. let val3 = args[5] as u32;
  671. let mut timespec = None;
  672. if utime != 0 && operation.contains(FutexFlag::FLAGS_HAS_TIMEOUT) {
  673. let reader = UserBufferReader::new(
  674. utime as *const PosixTimeSpec,
  675. core::mem::size_of::<PosixTimeSpec>(),
  676. true,
  677. )?;
  678. timespec = Some(*reader.read_one_from_user::<PosixTimeSpec>(0)?);
  679. }
  680. Self::do_futex(uaddr, operation, val, timespec, uaddr2, utime as u32, val3)
  681. }
  682. SYS_SET_ROBUST_LIST => {
  683. let head = args[0];
  684. let head_uaddr = VirtAddr::new(head);
  685. let len = args[1];
  686. let ret = Self::set_robust_list(head_uaddr, len);
  687. return ret;
  688. }
  689. SYS_GET_ROBUST_LIST => {
  690. let pid = args[0];
  691. let head = args[1];
  692. let head_uaddr = VirtAddr::new(head);
  693. let len_ptr = args[2];
  694. let len_ptr_uaddr = VirtAddr::new(len_ptr);
  695. let ret = Self::get_robust_list(pid, head_uaddr, len_ptr_uaddr);
  696. return ret;
  697. }
  698. SYS_READV => Self::readv(args[0] as i32, args[1], args[2]),
  699. SYS_WRITEV => Self::writev(args[0] as i32, args[1], args[2]),
  700. SYS_SET_TID_ADDRESS => Self::set_tid_address(args[0]),
  701. #[cfg(target_arch = "x86_64")]
  702. SYS_LSTAT => {
  703. let path = args[0] as *const u8;
  704. let kstat = args[1] as *mut PosixKstat;
  705. Self::lstat(path, kstat)
  706. }
  707. #[cfg(target_arch = "x86_64")]
  708. SYS_STAT => {
  709. let path = args[0] as *const u8;
  710. let kstat = args[1] as *mut PosixKstat;
  711. Self::stat(path, kstat)
  712. }
  713. SYS_STATFS => {
  714. let path = args[0] as *const u8;
  715. let statfs = args[1] as *mut PosixStatfs;
  716. Self::statfs(path, statfs)
  717. }
  718. SYS_FSTATFS => {
  719. let fd = args[0] as i32;
  720. let statfs = args[1] as *mut PosixStatfs;
  721. Self::fstatfs(fd, statfs)
  722. }
  723. SYS_STATX => {
  724. let fd = args[0] as i32;
  725. let path = args[1] as *const u8;
  726. let flags = args[2] as u32;
  727. let mask = args[3] as u32;
  728. let kstat = args[4] as *mut PosixStatx;
  729. Self::do_statx(fd, path, flags, mask, kstat)
  730. }
  731. #[cfg(target_arch = "x86_64")]
  732. SYS_EPOLL_CREATE => Self::epoll_create(args[0] as i32),
  733. SYS_EPOLL_CREATE1 => Self::epoll_create1(args[0]),
  734. SYS_EPOLL_CTL => Self::epoll_ctl(
  735. args[0] as i32,
  736. args[1],
  737. args[2] as i32,
  738. VirtAddr::new(args[3]),
  739. ),
  740. #[cfg(target_arch = "x86_64")]
  741. SYS_EPOLL_WAIT => Self::epoll_wait(
  742. args[0] as i32,
  743. VirtAddr::new(args[1]),
  744. args[2] as i32,
  745. args[3] as i32,
  746. ),
  747. SYS_EPOLL_PWAIT => {
  748. let epfd = args[0] as i32;
  749. let epoll_event = VirtAddr::new(args[1]);
  750. let max_events = args[2] as i32;
  751. let timespec = args[3] as i32;
  752. let sigmask_addr = args[4] as *mut SigSet;
  753. if sigmask_addr.is_null() {
  754. return Self::epoll_wait(epfd, epoll_event, max_events, timespec);
  755. }
  756. let sigmask_reader =
  757. UserBufferReader::new(sigmask_addr, core::mem::size_of::<SigSet>(), true)?;
  758. let mut sigmask = *sigmask_reader.read_one_from_user::<SigSet>(0)?;
  759. Self::epoll_pwait(
  760. args[0] as i32,
  761. VirtAddr::new(args[1]),
  762. args[2] as i32,
  763. args[3] as i32,
  764. &mut sigmask,
  765. )
  766. }
  767. // 目前为了适配musl-libc,以下系统调用先这样写着
  768. SYS_GETRANDOM => {
  769. let flags = GRandFlags::from_bits(args[2] as u8).ok_or(SystemError::EINVAL)?;
  770. Self::get_random(args[0] as *mut u8, args[1], flags)
  771. }
  772. SYS_SOCKETPAIR => {
  773. let mut user_buffer_writer = UserBufferWriter::new(
  774. args[3] as *mut c_int,
  775. core::mem::size_of::<[c_int; 2]>(),
  776. frame.is_from_user(),
  777. )?;
  778. let fds = user_buffer_writer.buffer::<i32>(0)?;
  779. Self::socketpair(args[0], args[1], args[2], fds)
  780. }
  781. #[cfg(target_arch = "x86_64")]
  782. SYS_POLL => {
  783. let fds = args[0];
  784. let nfds = args[1] as u32;
  785. let timeout = args[2] as i32;
  786. Self::poll(fds, nfds, timeout)
  787. }
  788. SYS_PPOLL => {
  789. log::warn!("SYS_PPOLL has not yet been implemented");
  790. Ok(0)
  791. }
  792. SYS_SETPGID => {
  793. warn!("SYS_SETPGID has not yet been implemented");
  794. Ok(0)
  795. }
  796. SYS_RT_SIGPROCMASK => {
  797. let how = args[0] as i32;
  798. let nset = args[1];
  799. let oset = args[2];
  800. let sigsetsize = args[3];
  801. Self::rt_sigprocmask(how, nset, oset, sigsetsize)
  802. }
  803. SYS_TKILL => {
  804. warn!("SYS_TKILL has not yet been implemented");
  805. Ok(0)
  806. }
  807. SYS_SIGALTSTACK => {
  808. warn!("SYS_SIGALTSTACK has not yet been implemented");
  809. Ok(0)
  810. }
  811. SYS_EXIT_GROUP => {
  812. warn!("SYS_EXIT_GROUP has not yet been implemented");
  813. Ok(0)
  814. }
  815. SYS_MADVISE => {
  816. let addr = args[0];
  817. let len = page_align_up(args[1]);
  818. if addr & (MMArch::PAGE_SIZE - 1) != 0 {
  819. Err(SystemError::EINVAL)
  820. } else {
  821. Self::madvise(VirtAddr::new(addr), len, args[2])
  822. }
  823. }
  824. SYS_GETTID => Self::gettid().map(|tid| tid.into()),
  825. SYS_SYSLOG => {
  826. let syslog_action_type = args[0];
  827. let buf_vaddr = args[1];
  828. let len = args[2];
  829. let from_user = frame.is_from_user();
  830. let mut user_buffer_writer =
  831. UserBufferWriter::new(buf_vaddr as *mut u8, len, from_user)?;
  832. let user_buf = user_buffer_writer.buffer(0)?;
  833. Self::do_syslog(syslog_action_type, user_buf, len)
  834. }
  835. SYS_GETUID => Self::getuid(),
  836. SYS_GETGID => Self::getgid(),
  837. SYS_SETUID => Self::setuid(args[0]),
  838. SYS_SETGID => Self::setgid(args[0]),
  839. SYS_GETEUID => Self::geteuid(),
  840. SYS_GETEGID => Self::getegid(),
  841. SYS_SETRESUID => Self::seteuid(args[1]),
  842. SYS_SETRESGID => Self::setegid(args[1]),
  843. SYS_SETFSUID => Self::setfsuid(args[0]),
  844. SYS_SETFSGID => Self::setfsgid(args[0]),
  845. SYS_SETSID => {
  846. warn!("SYS_SETSID has not yet been implemented");
  847. Ok(0)
  848. }
  849. SYS_GETRUSAGE => {
  850. let who = args[0] as c_int;
  851. let rusage = args[1] as *mut RUsage;
  852. Self::get_rusage(who, rusage)
  853. }
  854. #[cfg(target_arch = "x86_64")]
  855. SYS_READLINK => {
  856. let path = args[0] as *const u8;
  857. let buf = args[1] as *mut u8;
  858. let bufsiz = args[2];
  859. Self::readlink(path, buf, bufsiz)
  860. }
  861. SYS_READLINKAT => {
  862. let dirfd = args[0] as i32;
  863. let path = args[1] as *const u8;
  864. let buf = args[2] as *mut u8;
  865. let bufsiz = args[3];
  866. Self::readlink_at(dirfd, path, buf, bufsiz)
  867. }
  868. SYS_PRLIMIT64 => {
  869. let pid = args[0];
  870. let pid = Pid::new(pid);
  871. let resource = args[1];
  872. let new_limit = args[2] as *const RLimit64;
  873. let old_limit = args[3] as *mut RLimit64;
  874. Self::prlimit64(pid, resource, new_limit, old_limit)
  875. }
  876. #[cfg(target_arch = "x86_64")]
  877. SYS_ACCESS => {
  878. let pathname = args[0] as *const u8;
  879. let mode = args[1] as u32;
  880. Self::access(pathname, mode)
  881. }
  882. SYS_FACCESSAT => {
  883. let dirfd = args[0] as i32;
  884. let pathname = args[1] as *const u8;
  885. let mode = args[2] as u32;
  886. Self::faccessat2(dirfd, pathname, mode, 0)
  887. }
  888. SYS_FACCESSAT2 => {
  889. let dirfd = args[0] as i32;
  890. let pathname = args[1] as *const u8;
  891. let mode = args[2] as u32;
  892. let flags = args[3] as u32;
  893. Self::faccessat2(dirfd, pathname, mode, flags)
  894. }
  895. SYS_CLOCK_GETTIME => {
  896. let clockid = args[0] as i32;
  897. let timespec = args[1] as *mut PosixTimeSpec;
  898. Self::clock_gettime(clockid, timespec)
  899. }
  900. SYS_SYSINFO => {
  901. let info = args[0] as *mut SysInfo;
  902. Self::sysinfo(info)
  903. }
  904. SYS_UMASK => {
  905. let mask = args[0] as u32;
  906. Self::umask(mask)
  907. }
  908. SYS_FCHOWN => {
  909. let dirfd = args[0] as i32;
  910. let uid = args[1];
  911. let gid = args[2];
  912. Self::fchown(dirfd, uid, gid)
  913. }
  914. #[cfg(target_arch = "x86_64")]
  915. SYS_CHOWN => {
  916. let pathname = args[0] as *const u8;
  917. let uid = args[1];
  918. let gid = args[2];
  919. Self::chown(pathname, uid, gid)
  920. }
  921. #[cfg(target_arch = "x86_64")]
  922. SYS_LCHOWN => {
  923. let pathname = args[0] as *const u8;
  924. let uid = args[1];
  925. let gid = args[2];
  926. Self::lchown(pathname, uid, gid)
  927. }
  928. SYS_FCHOWNAT => {
  929. let dirfd = args[0] as i32;
  930. let pathname = args[1] as *const u8;
  931. let uid = args[2];
  932. let gid = args[3];
  933. let flag = args[4] as i32;
  934. Self::fchownat(dirfd, pathname, uid, gid, flag)
  935. }
  936. SYS_FSYNC => {
  937. warn!("SYS_FSYNC has not yet been implemented");
  938. Ok(0)
  939. }
  940. SYS_RSEQ => {
  941. warn!("SYS_RSEQ has not yet been implemented");
  942. Ok(0)
  943. }
  944. #[cfg(target_arch = "x86_64")]
  945. SYS_CHMOD => {
  946. let pathname = args[0] as *const u8;
  947. let mode = args[1] as u32;
  948. Self::chmod(pathname, mode)
  949. }
  950. SYS_FCHMOD => {
  951. let fd = args[0] as i32;
  952. let mode = args[1] as u32;
  953. Self::fchmod(fd, mode)
  954. }
  955. SYS_FCHMODAT => {
  956. let dirfd = args[0] as i32;
  957. let pathname = args[1] as *const u8;
  958. let mode = args[2] as u32;
  959. Self::fchmodat(dirfd, pathname, mode)
  960. }
  961. SYS_SCHED_YIELD => Self::do_sched_yield(),
  962. SYS_SCHED_GETAFFINITY => {
  963. let pid = args[0] as i32;
  964. let size = args[1];
  965. let set_vaddr = args[2];
  966. let mut user_buffer_writer =
  967. UserBufferWriter::new(set_vaddr as *mut u8, size, frame.is_from_user())?;
  968. let set: &mut [u8] = user_buffer_writer.buffer(0)?;
  969. Self::getaffinity(pid, set)
  970. }
  971. #[cfg(target_arch = "x86_64")]
  972. SYS_GETRLIMIT => {
  973. let resource = args[0];
  974. let rlimit = args[1] as *mut RLimit64;
  975. Self::prlimit64(
  976. ProcessManager::current_pcb().pid(),
  977. resource,
  978. core::ptr::null::<RLimit64>(),
  979. rlimit,
  980. )
  981. }
  982. SYS_FADVISE64 => {
  983. // todo: 这个系统调用还没有实现
  984. Err(SystemError::ENOSYS)
  985. }
  986. SYS_MOUNT => {
  987. let source = args[0] as *const u8;
  988. let target = args[1] as *const u8;
  989. let filesystemtype = args[2] as *const u8;
  990. let mountflags = args[3];
  991. let data = args[4] as *const u8; // 额外的mount参数,实现自己的mountdata来获取
  992. return Self::mount(source, target, filesystemtype, mountflags, data);
  993. }
  994. SYS_UMOUNT2 => {
  995. let target = args[0] as *const u8;
  996. let flags = args[1] as i32;
  997. Self::umount2(target, flags)?;
  998. return Ok(0);
  999. }
  1000. SYS_NEWFSTATAT => {
  1001. // todo: 这个系统调用还没有实现
  1002. Err(SystemError::ENOSYS)
  1003. }
  1004. // SYS_SCHED_YIELD => Self::sched_yield(),
  1005. SYS_UNAME => {
  1006. let name = args[0] as *mut PosixOldUtsName;
  1007. Self::uname(name)
  1008. }
  1009. SYS_PRCTL => {
  1010. // todo: 这个系统调用还没有实现
  1011. Err(SystemError::EINVAL)
  1012. }
  1013. #[cfg(target_arch = "x86_64")]
  1014. SYS_ALARM => {
  1015. let second = args[0] as u32;
  1016. Self::alarm(second)
  1017. }
  1018. SYS_SHMGET => {
  1019. let key = ShmKey::new(args[0]);
  1020. let size = args[1];
  1021. let shmflg = ShmFlags::from_bits_truncate(args[2] as u32);
  1022. Self::shmget(key, size, shmflg)
  1023. }
  1024. SYS_SHMAT => {
  1025. let id = ShmId::new(args[0]);
  1026. let vaddr = VirtAddr::new(args[1]);
  1027. let shmflg = ShmFlags::from_bits_truncate(args[2] as u32);
  1028. Self::shmat(id, vaddr, shmflg)
  1029. }
  1030. SYS_SHMDT => {
  1031. let vaddr = VirtAddr::new(args[0]);
  1032. Self::shmdt(vaddr)
  1033. }
  1034. SYS_SHMCTL => {
  1035. let id = ShmId::new(args[0]);
  1036. let cmd = ShmCtlCmd::from(args[1]);
  1037. let user_buf = args[2] as *const u8;
  1038. let from_user = frame.is_from_user();
  1039. Self::shmctl(id, cmd, user_buf, from_user)
  1040. }
  1041. SYS_MSYNC => {
  1042. let start = page_align_up(args[0]);
  1043. let len = page_align_up(args[1]);
  1044. let flags = args[2];
  1045. Self::msync(VirtAddr::new(start), len, flags)
  1046. }
  1047. SYS_UTIMENSAT => Self::sys_utimensat(
  1048. args[0] as i32,
  1049. args[1] as *const u8,
  1050. args[2] as *const PosixTimeSpec,
  1051. args[3] as u32,
  1052. ),
  1053. #[cfg(target_arch = "x86_64")]
  1054. SYS_FUTIMESAT => {
  1055. let flags = UtimensFlags::empty();
  1056. Self::sys_utimensat(
  1057. args[0] as i32,
  1058. args[1] as *const u8,
  1059. args[2] as *const PosixTimeSpec,
  1060. flags.bits(),
  1061. )
  1062. }
  1063. #[cfg(target_arch = "x86_64")]
  1064. SYS_UTIMES => Self::sys_utimes(args[0] as *const u8, args[1] as *const PosixTimeval),
  1065. #[cfg(target_arch = "x86_64")]
  1066. SYS_EVENTFD => {
  1067. let initval = args[0] as u32;
  1068. Self::sys_eventfd(initval, 0)
  1069. }
  1070. SYS_EVENTFD2 => {
  1071. let initval = args[0] as u32;
  1072. let flags = args[1] as u32;
  1073. Self::sys_eventfd(initval, flags)
  1074. }
  1075. SYS_UNSHARE => Self::sys_unshare(args[0] as u64),
  1076. SYS_BPF => {
  1077. let cmd = args[0] as u32;
  1078. let attr = args[1] as *mut u8;
  1079. let size = args[2] as u32;
  1080. Self::sys_bpf(cmd, attr, size)
  1081. }
  1082. SYS_PERF_EVENT_OPEN => {
  1083. let attr = args[0] as *const u8;
  1084. let pid = args[1] as i32;
  1085. let cpu = args[2] as i32;
  1086. let group_fd = args[3] as i32;
  1087. let flags = args[4] as u32;
  1088. Self::sys_perf_event_open(attr, pid, cpu, group_fd, flags)
  1089. }
  1090. SYS_SETRLIMIT => Ok(0),
  1091. SYS_RESTART_SYSCALL => Self::restart_syscall(),
  1092. _ => panic!("Unsupported syscall ID: {}", syscall_num),
  1093. };
  1094. if ProcessManager::current_pcb()
  1095. .flags()
  1096. .contains(ProcessFlags::NEED_SCHEDULE)
  1097. {
  1098. schedule(SchedMode::SM_PREEMPT);
  1099. }
  1100. return r;
  1101. }
  1102. pub fn put_string(
  1103. s: *const u8,
  1104. front_color: u32,
  1105. back_color: u32,
  1106. ) -> Result<usize, SystemError> {
  1107. // todo: 删除这个系统调用
  1108. let s = check_and_clone_cstr(s, Some(4096))?
  1109. .into_string()
  1110. .map_err(|_| SystemError::EINVAL)?;
  1111. let fr = (front_color & 0x00ff0000) >> 16;
  1112. let fg = (front_color & 0x0000ff00) >> 8;
  1113. let fb = front_color & 0x000000ff;
  1114. let br = (back_color & 0x00ff0000) >> 16;
  1115. let bg = (back_color & 0x0000ff00) >> 8;
  1116. let bb = back_color & 0x000000ff;
  1117. print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m");
  1118. return Ok(s.len());
  1119. }
  1120. }