syscall.c 14 KB

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