syscall.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* Syscall dispatcher: services int 0x80 requests from ring-3. */
  2. #include "syscall.h"
  3. #include "backlight.h"
  4. #include "io.h"
  5. #include "paging.h"
  6. #include "power.h"
  7. #include "process.h"
  8. #include "rtc.h"
  9. #include "serial.h"
  10. #include "sys_fs.h"
  11. #include "sys_info.h"
  12. #include "sys_mem.h"
  13. #include "sys_net.h"
  14. #include "sys_wm.h"
  15. #include "terminal.h"
  16. #include "timer.h"
  17. #include "tty.h"
  18. #include "vfs.h"
  19. #include "vga.h"
  20. #include "wifi.h"
  21. static int copy_user_string(char *dst, unsigned int max, const char *src)
  22. {
  23. unsigned int i;
  24. if (!src || !max ||
  25. !paging_user_range_ok(current_proc()->cr3, (unsigned int)src,
  26. max, 0))
  27. return -1;
  28. for (i = 0; i + 1 < max && src[i]; i++)
  29. dst[i] = src[i];
  30. dst[i] = '\0';
  31. return 0;
  32. }
  33. static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
  34. {
  35. switch (num) {
  36. case SYS_PUTSTR:
  37. io_putstr((const char *)a1);
  38. return 0;
  39. case SYS_GET_TICKS_MS:
  40. return (int)get_ticks_ms();
  41. case SYS_SLEEP_MS:
  42. process_sleep_ms((unsigned int)a1);
  43. return 0;
  44. case SYS_EXIT:
  45. process_exit(); /* never returns */
  46. return 0;
  47. case SYS_READ_FILE:
  48. return sys_read_file((const char *)a1, (void *)a2, (unsigned int)a3);
  49. case SYS_WRITE_FILE:
  50. return sys_write_file((const char *)a1, (const void *)a2,
  51. (unsigned int)a3);
  52. case SYS_LIST_DIR:
  53. return sys_list_dir((const char *)a1, (unsigned int)a2,
  54. (struct sys_dirent *)a3);
  55. case SYS_UNLINK:
  56. return sys_unlink((const char *)a1);
  57. case SYS_MKDIR:
  58. return sys_mkdir((const char *)a1);
  59. case SYS_RMDIR:
  60. return sys_rmdir((const char *)a1);
  61. case SYS_LIST_MOUNTS:
  62. return sys_list_mounts((unsigned int)a1, (struct sys_mount *)a2);
  63. case SYS_FS_STAT:
  64. return sys_stat_path((const char *)a1, (struct sys_stat *)a2);
  65. case SYS_MEMINFO:
  66. return sys_meminfo((struct sys_meminfo *)a1);
  67. case SYS_E820:
  68. return sys_e820((unsigned int)a1, (struct sys_e820 *)a2);
  69. case SYS_MOUSEINFO:
  70. return sys_mouseinfo((struct sys_mouseinfo *)a1);
  71. case SYS_STORAGE_INFO:
  72. return sys_storage_info((struct sys_storage_info *)a1);
  73. case SYS_BLKDEV:
  74. return sys_blkdev((unsigned int)a1, (struct sys_blkdev *)a2);
  75. case SYS_BLK_READ:
  76. return sys_blk_read((unsigned int)a1, (unsigned int)a2, (void *)a3);
  77. case SYS_MOUNT_CTL:
  78. return sys_mount_ctl(a1, a2, a3);
  79. case SYS_NET_INFO:
  80. return sys_net_info((struct sys_netinfo *)a1);
  81. case SYS_NET_PRINT_INFO:
  82. return sys_net_print_info();
  83. case SYS_NET_ACQUIRE:
  84. return sys_net_acquire();
  85. case SYS_NET_RELEASE:
  86. return sys_net_release();
  87. case SYS_NET_MAC:
  88. return sys_net_mac((unsigned char *)a1);
  89. case SYS_NET_GET_IP:
  90. sys_net_get_ip((unsigned char *)a1);
  91. return 0;
  92. case SYS_NET_SET_IP:
  93. sys_net_set_ip((const unsigned char *)a1);
  94. return 0;
  95. case SYS_NET_GET_MASK:
  96. sys_net_get_mask((unsigned char *)a1);
  97. return 0;
  98. case SYS_NET_GET_GW:
  99. sys_net_get_gw((unsigned char *)a1);
  100. return 0;
  101. case SYS_NET_GET_DNS:
  102. sys_net_get_dns((unsigned char *)a1);
  103. return 0;
  104. case SYS_NET_SET_DNS:
  105. sys_net_set_dns((const unsigned char *)a1);
  106. return 0;
  107. case SYS_NET_TX:
  108. return sys_net_tx((const unsigned char *)a1, (unsigned int)a2);
  109. case SYS_NET_RX:
  110. return sys_net_rx((unsigned char *)a1, (unsigned int)a2);
  111. case SYS_NET_DECODE:
  112. sys_net_decode((const unsigned char *)a1, (unsigned int)a2);
  113. return 0;
  114. case SYS_NET_REPLY:
  115. return sys_net_reply((const unsigned char *)a1, (unsigned int)a2,
  116. (unsigned char *)a3);
  117. case SYS_NET_LOOPBACK:
  118. return sys_net_loopback(a1);
  119. case SYS_NET_REGS:
  120. return sys_net_regs();
  121. case SYS_NET_RESET:
  122. return sys_net_reset();
  123. case SYS_NET_SPEED:
  124. return sys_net_speed((unsigned int)a1);
  125. case SYS_NET_MAC_SET:
  126. return sys_net_mac_set((const unsigned char *)a1);
  127. case SYS_DHCP_RUN:
  128. return sys_dhcp_run((unsigned int)a1);
  129. case SYS_DHCP_INFO:
  130. return sys_dhcp_info((unsigned char *)a1, (unsigned int *)a2);
  131. case SYS_DNS_RESOLVE:
  132. return sys_dns_resolve((const char *)a1, (unsigned char *)a2);
  133. case SYS_NETLOG:
  134. return sys_netlog((const char *)a1);
  135. case SYS_NETLOG_CONF:
  136. return sys_netlog_conf(a1, (const unsigned char *)a2);
  137. case SYS_TCP_CONNECT:
  138. return sys_tcp_connect((const unsigned char *)a1,
  139. (const unsigned char *)a2,
  140. (unsigned int)a3);
  141. case SYS_TCP_PEER_MSS:
  142. return sys_tcp_peer_mss();
  143. case SYS_TCP_SEND:
  144. return sys_tcp_send((const void *)a1, (unsigned int)a2);
  145. case SYS_TCP_RECV:
  146. return sys_tcp_recv((void *)a1, (unsigned int)a2, (unsigned int)a3);
  147. case SYS_TCP_CLOSE:
  148. sys_tcp_close();
  149. return 0;
  150. case SYS_TCP_LISTEN:
  151. return sys_tcp_listen((unsigned int)a1);
  152. case SYS_TCP_ACCEPT:
  153. return sys_tcp_accept();
  154. case SYS_TCP_SRV_RECV:
  155. return sys_tcp_srv_recv((void *)a1, (unsigned int)a2,
  156. (unsigned int)a3);
  157. case SYS_TCP_SRV_SEND:
  158. return sys_tcp_srv_send((const void *)a1, (unsigned int)a2);
  159. case SYS_TCP_SRV_CLOSE:
  160. sys_tcp_srv_close();
  161. return 0;
  162. case SYS_TCP_PEER:
  163. return sys_tcp_peer((unsigned char *)a1, (unsigned int *)a2);
  164. case SYS_NET_NEXT_HOP:
  165. return sys_net_next_hop((const unsigned char *)a1,
  166. (unsigned char *)a2);
  167. case SYS_NET_ARP:
  168. return sys_net_arp((const unsigned char *)a1, (unsigned char *)a2);
  169. case SYS_NET_CONFIGURED:
  170. return sys_net_configured();
  171. case SYS_GETCWD: {
  172. const char *cwd = current_proc()->cwd;
  173. char *out = (char *)a1;
  174. unsigned int i = 0, max = (unsigned int)a2;
  175. if (!cwd || max == 0)
  176. return -1;
  177. for (i = 0; cwd[i] && i + 1 < max; i++)
  178. out[i] = cwd[i];
  179. out[i] = '\0';
  180. return 0;
  181. }
  182. case SYS_PROCINFO:
  183. return process_procinfo((unsigned int)a1,
  184. (struct sys_procinfo *)a2);
  185. case SYS_KEY_POLL:
  186. return sys_wm_key_poll();
  187. case SYS_RTC_HMS: {
  188. struct sys_time *out = (struct sys_time *)a1;
  189. unsigned int h, m, s;
  190. if (!out)
  191. return -1;
  192. rtc_read_hms(&h, &m, &s);
  193. out->hour = h;
  194. out->minute = m;
  195. out->second = s;
  196. return 0;
  197. }
  198. case SYS_RTC_DATETIME: {
  199. char buf[20];
  200. char *out = (char *)a1;
  201. unsigned int i;
  202. if ((unsigned int)a2 < sizeof(buf) ||
  203. !paging_user_range_ok(current_proc()->cr3,
  204. (unsigned int)out, sizeof(buf), 1))
  205. return -1;
  206. rtc_read_datetime(buf);
  207. for (i = 0; i < sizeof(buf); i++)
  208. out[i] = buf[i];
  209. return 0;
  210. }
  211. case SYS_RTC_GET: {
  212. struct sys_datetime dt;
  213. struct sys_datetime *out = (struct sys_datetime *)a1;
  214. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)out,
  215. sizeof(dt), 1))
  216. return -1;
  217. rtc_read_datetime_full(&dt);
  218. *out = dt;
  219. return 0;
  220. }
  221. case SYS_RTC_SET: {
  222. struct sys_datetime dt;
  223. const unsigned char *in = (const unsigned char *)a1;
  224. unsigned char *d = (unsigned char *)&dt;
  225. unsigned int i;
  226. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)in,
  227. sizeof(dt), 0))
  228. return -1;
  229. for (i = 0; i < sizeof(dt); i++)
  230. d[i] = in[i];
  231. return rtc_set_datetime(&dt);
  232. }
  233. case SYS_NTP_SYNC: {
  234. struct sys_datetime dt;
  235. struct sys_datetime *out = (struct sys_datetime *)a3;
  236. char server[64];
  237. int r;
  238. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)out,
  239. sizeof(dt), 1))
  240. return -1;
  241. if (copy_user_string(server, sizeof(server), (const char *)a1) != 0)
  242. return -1;
  243. r = sys_ntp_sync(server, (unsigned int)a2, &dt);
  244. if (r == 0)
  245. *out = dt;
  246. return r;
  247. }
  248. case SYS_WM_ATTACH:
  249. return sys_wm_attach((struct wm_screen_info *)a1);
  250. case SYS_WM_INFO:
  251. return sys_wm_info((unsigned int)a1,
  252. (struct wm_window_info *)a2);
  253. case SYS_WM_MAP:
  254. return sys_wm_map((unsigned int)a1,
  255. (struct wm_map_info *)a2);
  256. case SYS_WM_ACK:
  257. return sys_wm_ack((unsigned int)a1, (unsigned int)a2);
  258. case SYS_WM_FOCUS:
  259. return sys_wm_focus((unsigned int)a1);
  260. case SYS_WM_SET_GEOM:
  261. return sys_wm_set_geom((unsigned int)a1,
  262. (const struct wm_geom *)a2);
  263. case SYS_WM_CREATE_TERM:
  264. return sys_wm_create_term();
  265. case SYS_WM_CLOSE:
  266. return sys_wm_close((unsigned int)a1);
  267. case SYS_WM_POLL:
  268. return sys_wm_poll((struct wm_status *)a1);
  269. case SYS_WM_BEGIN:
  270. return sys_wm_begin();
  271. case SYS_WM_PRESENT:
  272. return sys_wm_present((const struct wm_present_info *)a1);
  273. case SYS_WM_SYNC:
  274. return sys_wm_sync();
  275. case SYS_WIN_CREATE:
  276. return sys_win_create((const struct win_create *)a1,
  277. (struct win_info *)a2);
  278. case SYS_WIN_PRESENT:
  279. return sys_win_present((const struct wm_win_present *)a1);
  280. case SYS_WIN_CLOSE:
  281. return sys_win_close();
  282. case SYS_WM_RESIZE:
  283. return sys_wm_resize((unsigned int)a1,
  284. (const struct wm_size *)a2);
  285. case SYS_WIN_INFO:
  286. return sys_win_info((struct win_info *)a1);
  287. case SYS_WIN_ACK_RESIZE:
  288. return sys_win_ack_resize();
  289. case SYS_WIN_POINTER:
  290. return sys_win_pointer((struct win_pointer *)a1);
  291. case SYS_WIN_EVENT:
  292. return sys_win_event((struct win_event *)a1);
  293. case SYS_WIN_SCROLL:
  294. return sys_win_scroll((struct win_scroll *)a1);
  295. case SYS_MEM_ALLOC:
  296. return sys_mem_alloc((unsigned int)a1);
  297. case SYS_MEM_GROW:
  298. return sys_mem_grow((unsigned int)a1);
  299. case SYS_MEM_FREE:
  300. return sys_mem_free();
  301. case SYS_CPU_TEMP:
  302. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)a1,
  303. sizeof(struct sys_cputemp), 1))
  304. return -1;
  305. return sys_cputemp((struct sys_cputemp *)a1);
  306. case SYS_SENSORS:
  307. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)a1,
  308. sizeof(struct sys_sensors), 1))
  309. return -1;
  310. return sys_sensors((struct sys_sensors *)a1);
  311. case SYS_TPEC_SENSORS:
  312. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)a1,
  313. sizeof(struct sys_tpec_sensors), 1))
  314. return -1;
  315. return sys_tpec_sensors((struct sys_tpec_sensors *)a1);
  316. case SYS_CROS_EC_SENSORS:
  317. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)a1,
  318. sizeof(struct sys_cros_sensors), 1))
  319. return -1;
  320. return sys_cros_sensors((struct sys_cros_sensors *)a1);
  321. case SYS_POWER_INFO:
  322. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)a1,
  323. sizeof(struct sys_powerinfo), 1))
  324. return -1;
  325. return sys_powerinfo((struct sys_powerinfo *)a1,
  326. (unsigned int)a2);
  327. case SYS_BACKLIGHT:
  328. if (a1 == SYS_BACKLIGHT_GET)
  329. return backlight_percent();
  330. if (a1 == SYS_BACKLIGHT_SET) {
  331. backlight_set_percent(a2);
  332. return backlight_percent();
  333. }
  334. if (a1 == SYS_BACKLIGHT_STEP) {
  335. backlight_step(a2);
  336. return backlight_percent();
  337. }
  338. return -1;
  339. case SYS_CPUFREQ:
  340. if (a1 == SYS_CPUFREQ_GET)
  341. return power_cpufreq_mode();
  342. return power_cpufreq_set_mode(a1);
  343. case SYS_WIN_KEY_WAIT:
  344. return sys_win_key_wait((unsigned int)a1);
  345. case SYS_WIN_RESIZE_SELF:
  346. return sys_win_resize_self((unsigned int)a1, (unsigned int)a2);
  347. case SYS_WIN_SET_CURSOR:
  348. return sys_win_set_cursor((int)a1);
  349. case SYS_WM_SET_CURSOR:
  350. return sys_wm_set_cursor((int)a1);
  351. case SYS_WM_MOUSE_CAPTURE:
  352. return sys_wm_mouse_capture((int)a1);
  353. case SYS_SCREEN_INFO: {
  354. struct sys_screen *out = (struct sys_screen *)a1;
  355. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)a1,
  356. sizeof(*out), 1))
  357. return -1;
  358. out->width = vga_fb_width();
  359. out->height = vga_fb_height();
  360. return 0;
  361. }
  362. case SYS_SCHEDINFO:
  363. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)a1,
  364. sizeof(struct sys_schedinfo), 1))
  365. return -1;
  366. return sched_snapshot((struct sys_schedinfo *)a1);
  367. case SYS_TTY_CREATE:
  368. return tty_create();
  369. case SYS_TTY_READ: {
  370. int id = a1;
  371. char *buf = (char *)a2;
  372. unsigned int max = (unsigned int)a3;
  373. if (!tty_valid(id) || !buf || !max ||
  374. !paging_user_range_ok(current_proc()->cr3,
  375. (unsigned int)buf, max, 1))
  376. return -1;
  377. return tty_read(id, buf, max);
  378. }
  379. case SYS_SPAWN: {
  380. struct sys_spawn in;
  381. struct vfs_file f;
  382. struct vfs_mount *m = 0;
  383. char path[VFS_PATH_MAX], arg[VFS_PATH_MAX], cwd[VFS_PATH_MAX];
  384. const char *name;
  385. int pid;
  386. int lr;
  387. if (!paging_user_range_ok(current_proc()->cr3,
  388. (unsigned int)a1, sizeof(in), 0))
  389. return -1;
  390. {
  391. const char *p = (const char *)a1;
  392. unsigned int i;
  393. unsigned char *d = (unsigned char *)&in;
  394. for (i = 0; i < sizeof(in); i++)
  395. d[i] = (unsigned char)p[i];
  396. }
  397. if (in.tty && !tty_valid((int)in.tty))
  398. return -1;
  399. if (copy_user_string(path, sizeof(path), in.path) != 0 ||
  400. copy_user_string(arg, sizeof(arg), in.arg) != 0 ||
  401. copy_user_string(cwd, sizeof(cwd), in.cwd) != 0)
  402. return -1;
  403. /* A FAT load reads the floppy through IRQ 6, so open the
  404. * syscall's interrupts-off section for the duration (the same
  405. * window SYS_READ_FILE uses). */
  406. sys_fs_enter_path(path);
  407. lr = vfs_load_file(path, &f, &m);
  408. sys_fs_exit();
  409. if (lr != 0)
  410. return -1;
  411. name = path;
  412. {
  413. const char *q = path;
  414. while (*q) {
  415. if (*q == '/')
  416. name = q + 1;
  417. q++;
  418. }
  419. }
  420. pid = process_create_user_tty(name, f.data, f.size, arg, cwd,
  421. in.tty);
  422. vfs_release_file(m, &f);
  423. return pid;
  424. }
  425. case SYS_SPAWN_KCMD: {
  426. char name[16], arg[VFS_PATH_MAX];
  427. if (copy_user_string(name, sizeof(name), (const char *)a1) != 0 ||
  428. copy_user_string(arg, sizeof(arg), (const char *)a2) != 0)
  429. return -1;
  430. if (a3 && !tty_valid(a3))
  431. return -1;
  432. return term_kcmd_spawn(name, arg, (unsigned int)a3);
  433. }
  434. case SYS_GETPID:
  435. return (int)current_proc()->pid;
  436. case SYS_WIFI:
  437. return sys_wifi(a1, a2, a3);
  438. case SYS_NET_DEVLIST: {
  439. struct sys_netdev list[8];
  440. unsigned int max = (unsigned int)a2, i;
  441. int n;
  442. if (max > 8)
  443. max = 8;
  444. if (!max || !paging_user_range_ok(current_proc()->cr3,
  445. (unsigned int)a1,
  446. max * sizeof(list[0]), 1))
  447. return -1;
  448. n = sys_net_devlist(list, max);
  449. if (n < 0)
  450. return -1;
  451. for (i = 0; i < (unsigned int)n; i++)
  452. ((struct sys_netdev *)a1)[i] = list[i];
  453. return n;
  454. }
  455. case SYS_NET_SELECT: {
  456. char name[16];
  457. if (copy_user_string(name, sizeof(name),
  458. (const char *)a1) != 0)
  459. return -1;
  460. return sys_net_select(name);
  461. }
  462. case SYS_ALIVE:
  463. return process_is_alive((unsigned int)a1);
  464. case SYS_KILL:
  465. return process_kill((unsigned int)a1);
  466. case SYS_CHDIR: {
  467. char arg[VFS_PATH_MAX], abs[VFS_PATH_MAX];
  468. unsigned int size;
  469. int is_dir;
  470. if (copy_user_string(arg, sizeof(arg), (const char *)a1) != 0)
  471. return -1;
  472. if (vfs_join_path(current_proc()->cwd, arg[0] ? arg : ".",
  473. abs, sizeof(abs)) != 0)
  474. return -1;
  475. if (vfs_stat(abs, &size, &is_dir) != 0 || !is_dir)
  476. return -1;
  477. {
  478. unsigned int i;
  479. for (i = 0; abs[i] && i + 1 < sizeof(current_proc()->cwd); i++)
  480. current_proc()->cwd[i] = abs[i];
  481. current_proc()->cwd[i] = '\0';
  482. }
  483. return 0;
  484. }
  485. default:
  486. return -1;
  487. }
  488. }
  489. /* All ring-3 syscalls run under the big kernel lock. Blocking syscalls drop
  490. * it through sched_yield(); the lock is taken back before the dispatcher
  491. * resumes, so the rest of the kernel sees one syscall at a time. */
  492. int syscall_dispatch(int num, int a1, int a2, int a3)
  493. {
  494. struct process *p = current_proc();
  495. int r;
  496. /* A process killed while it was in ring 3 may reach the gate before its
  497. * CPU notices the DEAD state. Do not let it run one more syscall on
  498. * resources that may already be reused. */
  499. if (p && p->state == PROC_DEAD)
  500. process_exit();
  501. bkl_lock();
  502. r = syscall_dispatch_locked(num, a1, a2, a3);
  503. bkl_unlock();
  504. return r;
  505. }