ucmd_lib.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /* Tiny userland shim for ring-3 commands: syscall wrappers + string output.
  2. * Freestanding (no libc); include "syscall.h" for the syscall numbers. */
  3. #ifndef UCMD_LIB_H
  4. #define UCMD_LIB_H
  5. #include "syscall.h"
  6. #include "cursor_abi.h"
  7. #include "wm_abi.h"
  8. #ifdef UCMD_HOST
  9. /* The Linux host harness implements the same little syscall ABI in
  10. * host/syscall.c; no ring transition is involved. */
  11. int u_syscall0(int num);
  12. int u_syscall1(int num, int a1);
  13. int u_syscall2(int num, int a1, int a2);
  14. int u_syscall3(int num, int a1, int a2, int a3);
  15. #else
  16. static inline int u_syscall1(int num, int a1)
  17. {
  18. int ret;
  19. __asm__ volatile ("int $0x80"
  20. : "=a"(ret)
  21. : "a"(num), "b"(a1)
  22. : "memory");
  23. return ret;
  24. }
  25. static inline int u_syscall0(int num)
  26. {
  27. int ret;
  28. __asm__ volatile ("int $0x80"
  29. : "=a"(ret)
  30. : "a"(num)
  31. : "memory");
  32. return ret;
  33. }
  34. static inline int u_syscall2(int num, int a1, int a2)
  35. {
  36. int ret;
  37. __asm__ volatile ("int $0x80"
  38. : "=a"(ret)
  39. : "a"(num), "b"(a1), "c"(a2)
  40. : "memory");
  41. return ret;
  42. }
  43. static inline int u_syscall3(int num, int a1, int a2, int a3)
  44. {
  45. int ret;
  46. __asm__ volatile ("int $0x80"
  47. : "=a"(ret)
  48. : "a"(num), "b"(a1), "c"(a2), "d"(a3)
  49. : "memory");
  50. return ret;
  51. }
  52. #endif /* !UCMD_HOST */
  53. static inline void u_putc(char c)
  54. {
  55. char b[2];
  56. b[0] = c;
  57. b[1] = '\0';
  58. u_syscall1(SYS_PUTSTR, (int)b);
  59. }
  60. static inline void u_puts(const char *s)
  61. {
  62. u_syscall1(SYS_PUTSTR, (int)s);
  63. }
  64. static inline void u_putdec(unsigned int n)
  65. {
  66. char buf[12];
  67. char *p = buf + sizeof(buf) - 1;
  68. *p = '\0';
  69. if (n == 0) {
  70. u_putc('0');
  71. return;
  72. }
  73. while (n) {
  74. *--p = (char)('0' + (n % 10));
  75. n /= 10;
  76. }
  77. u_puts(p);
  78. }
  79. static inline void u_puthex(unsigned int n)
  80. {
  81. static const char hex[] = "0123456789ABCDEF";
  82. char buf[11];
  83. int i;
  84. for (i = 0; i < 8; i++)
  85. buf[i] = hex[(n >> (28 - i * 4)) & 0xF];
  86. buf[8] = '\0';
  87. u_puts(buf);
  88. }
  89. /* "a.b.c.d" (strict, 4 parts, 0-255) -> 0, else -1. */
  90. static inline int u_parse_ip(const char *s, unsigned char ip[4])
  91. {
  92. unsigned int i, val = 0, nparts = 0;
  93. int have_digit = 0;
  94. for (i = 0; ; i++) {
  95. char c = s[i];
  96. if (c >= '0' && c <= '9') {
  97. val = val * 10 + (unsigned int)(c - '0');
  98. if (val > 255)
  99. return -1;
  100. have_digit = 1;
  101. } else if (c == '.' || c == '\0') {
  102. if (!have_digit || nparts >= 4)
  103. return -1;
  104. ip[nparts++] = (unsigned char)val;
  105. val = 0;
  106. have_digit = 0;
  107. if (c == '\0')
  108. break;
  109. } else {
  110. return -1;
  111. }
  112. }
  113. return (nparts == 4) ? 0 : -1;
  114. }
  115. static inline void u_put_ip(const unsigned char ip[4])
  116. {
  117. unsigned int i;
  118. for (i = 0; i < 4; i++) {
  119. if (i)
  120. u_putc('.');
  121. u_putdec(ip[i]);
  122. }
  123. }
  124. static inline void u_put_hex2(unsigned int v)
  125. {
  126. static const char hex[] = "0123456789ABCDEF";
  127. u_putc(hex[(v >> 4) & 0xF]);
  128. u_putc(hex[v & 0xF]);
  129. }
  130. static inline void u_put_mac(const unsigned char mac[6])
  131. {
  132. unsigned int i;
  133. for (i = 0; i < 6; i++) {
  134. if (i)
  135. u_putc(':');
  136. u_put_hex2(mac[i]);
  137. }
  138. }
  139. static inline int u_getcwd(char *buf, unsigned int max)
  140. {
  141. return u_syscall2(SYS_GETCWD, (int)buf, (int)max);
  142. }
  143. static inline unsigned int u_ticks_ms(void)
  144. {
  145. return (unsigned int)u_syscall0(SYS_GET_TICKS_MS);
  146. }
  147. /* Boot storage-scan log (the `bootscan` window). STAT fills the ring
  148. * counters; READ copies one SYS_BOOTLOG_CHUNK chunk at absolute offset. */
  149. static inline int u_bootlog_stat(struct sys_bootlog *out)
  150. {
  151. return u_syscall2(SYS_BOOTLOG, SYS_BOOTLOG_STAT, (int)out);
  152. }
  153. static inline int u_bootlog_read(unsigned int off, char *buf)
  154. {
  155. return u_syscall3(SYS_BOOTLOG, SYS_BOOTLOG_READ, (int)off, (int)buf);
  156. }
  157. /* Cursor position of this process's terminal screen (linear cell index);
  158. * used by `top` to repaint its frame in place. */
  159. static inline void u_sleep_ms(unsigned int ms)
  160. {
  161. u_syscall1(SYS_SLEEP_MS, (int)ms);
  162. }
  163. static inline void u_exit(int code)
  164. {
  165. u_syscall1(SYS_EXIT, code);
  166. for (;;)
  167. ;
  168. }
  169. static inline int u_rtc_hms(struct sys_time *t)
  170. {
  171. return u_syscall1(SYS_RTC_HMS, (int)t);
  172. }
  173. static inline int u_rtc_datetime(char *buf, unsigned int max)
  174. {
  175. return u_syscall2(SYS_RTC_DATETIME, (int)buf, (int)max);
  176. }
  177. /* Full RTC date/time (year 1970-2069) and its setter. The setter validates
  178. * the fields and returns 0 on success. */
  179. static inline int u_rtc_get(struct sys_datetime *t)
  180. {
  181. return u_syscall1(SYS_RTC_GET, (int)t);
  182. }
  183. static inline int u_rtc_set(const struct sys_datetime *t)
  184. {
  185. return u_syscall1(SYS_RTC_SET, (int)t);
  186. }
  187. /* Fetch the UTC time from an SNTP server (empty name = pool.ntp.org) and
  188. * fill `dt`. Returns SYS_NTP_*; the kernel owns the NIC for the duration. */
  189. static inline int u_ntp_sync(const char *server, unsigned int timeout_ms,
  190. struct sys_datetime *dt)
  191. {
  192. return u_syscall3(SYS_NTP_SYNC, (int)server, (int)timeout_ms, (int)dt);
  193. }
  194. /* Window-manager service (only the kernel-spawned /ramfs/bin/wm may use it). */
  195. static inline int u_wm_attach(struct wm_screen_info *out)
  196. {
  197. return u_syscall1(SYS_WM_ATTACH, (int)out);
  198. }
  199. static inline int u_wm_info(unsigned int index, struct wm_window_info *out)
  200. {
  201. return u_syscall2(SYS_WM_INFO, (int)index, (int)out);
  202. }
  203. static inline int u_wm_map(unsigned int index, struct wm_map_info *out)
  204. {
  205. return u_syscall2(SYS_WM_MAP, (int)index, (int)out);
  206. }
  207. static inline int u_wm_ack(unsigned int index, unsigned int seq)
  208. {
  209. return u_syscall2(SYS_WM_ACK, (int)index, (int)seq);
  210. }
  211. static inline int u_wm_focus(unsigned int index)
  212. {
  213. return u_syscall1(SYS_WM_FOCUS, (int)index);
  214. }
  215. static inline int u_wm_set_geom(unsigned int index, int x, int y)
  216. {
  217. struct wm_geom g;
  218. g.x = x;
  219. g.y = y;
  220. return u_syscall2(SYS_WM_SET_GEOM, (int)index, (int)&g);
  221. }
  222. static inline int u_wm_create_term(void)
  223. {
  224. return u_syscall0(SYS_WM_CREATE_TERM);
  225. }
  226. static inline int u_wm_close(unsigned int index)
  227. {
  228. return u_syscall1(SYS_WM_CLOSE, (int)index);
  229. }
  230. static inline int u_wm_poll(struct wm_status *out)
  231. {
  232. return u_syscall1(SYS_WM_POLL, (int)out);
  233. }
  234. static inline int u_wm_begin(void)
  235. {
  236. return u_syscall0(SYS_WM_BEGIN);
  237. }
  238. static inline int u_wm_present(struct wm_present_info *in)
  239. {
  240. return u_syscall1(SYS_WM_PRESENT, (int)in);
  241. }
  242. static inline int u_wm_sync(void)
  243. {
  244. return u_syscall0(SYS_WM_SYNC);
  245. }
  246. /* Generic canvas window (gfx commands own a window instead of replacing a
  247. * terminal's content). */
  248. static inline int u_win_create_flags(unsigned int w, unsigned int h,
  249. const char *title, unsigned int flags,
  250. struct win_info *out)
  251. {
  252. struct win_create in;
  253. unsigned int i = 0;
  254. in.w = w;
  255. in.h = h;
  256. in.flags = flags;
  257. while (title && title[i] && i < sizeof(in.title) - 1) {
  258. in.title[i] = title[i];
  259. i++;
  260. }
  261. in.title[i] = '\0';
  262. return u_syscall2(SYS_WIN_CREATE, (int)&in, (int)out);
  263. }
  264. static inline int u_win_create(unsigned int w, unsigned int h, const char *title,
  265. struct win_info *out)
  266. {
  267. return u_win_create_flags(w, h, title, 0, out);
  268. }
  269. /* A chromeless panel anchored by the WM under the top-bar status icons. */
  270. static inline int u_win_create_popover(unsigned int w, unsigned int h,
  271. const char *title,
  272. struct win_info *out)
  273. {
  274. return u_win_create_flags(w, h, title, WIN_F_POPOVER, out);
  275. }
  276. static inline int u_win_present(void)
  277. {
  278. return u_syscall1(SYS_WIN_PRESENT, 0);
  279. }
  280. /* Publish only the content-relative rectangle that changed. */
  281. static inline int u_win_present_rect(int x, int y, unsigned int w,
  282. unsigned int h)
  283. {
  284. struct wm_win_present in;
  285. in.x = (unsigned int)x;
  286. in.y = (unsigned int)y;
  287. in.w = w;
  288. in.h = h;
  289. return u_syscall1(SYS_WIN_PRESENT, (int)&in);
  290. }
  291. static inline int u_win_close(void)
  292. {
  293. return u_syscall0(SYS_WIN_CLOSE);
  294. }
  295. static inline int u_win_info(struct win_info *out)
  296. {
  297. return u_syscall1(SYS_WIN_INFO, (int)out);
  298. }
  299. static inline int u_win_ack_resize(void)
  300. {
  301. return u_syscall0(SYS_WIN_ACK_RESIZE);
  302. }
  303. /* Ask the kernel to grow/shrink this canvas window (a popover expanding a
  304. * section); ngui_begin() acknowledges the new canvas on the next frame. */
  305. static inline int u_win_resize_self(unsigned int w, unsigned int h)
  306. {
  307. return u_syscall2(SYS_WIN_RESIZE_SELF, (int)w, (int)h);
  308. }
  309. /* Cursor shown while the pointer is over this window's content
  310. * (enum cursor_shape in cursor_abi.h). */
  311. static inline int u_win_set_cursor(int shape)
  312. {
  313. return u_syscall1(SYS_WIN_SET_CURSOR, shape);
  314. }
  315. /* WM only: cursor for the WM's own chrome and drags; CURSOR_NO_OVERRIDE
  316. * hands the pointer back to the windows. */
  317. static inline int u_wm_set_cursor(int shape)
  318. {
  319. return u_syscall1(SYS_WM_SET_CURSOR, shape);
  320. }
  321. /* WM only: take the pointer while dragging/resizing, so canvas clients do
  322. * not see the held button; release it when the drag ends. */
  323. static inline int u_wm_mouse_capture(int on)
  324. {
  325. return u_syscall1(SYS_WM_MOUSE_CAPTURE, on);
  326. }
  327. /* Window-manager preferences (struct wm_prefs in wm_abi.h) and the request
  328. * that focuses/starts the Settings app at a section. */
  329. static inline int u_wm_get_prefs(struct wm_prefs *out)
  330. {
  331. return u_syscall2(SYS_WM_PREFS, SYS_WM_PREFS_GET, (int)out);
  332. }
  333. static inline int u_wm_set_prefs(const struct wm_prefs *in)
  334. {
  335. return u_syscall2(SYS_WM_PREFS, SYS_WM_PREFS_SET, (int)in);
  336. }
  337. static inline int u_open_settings(int section)
  338. {
  339. return u_syscall1(SYS_WM_OPEN_SETTINGS, section);
  340. }
  341. static inline int u_screen_size(unsigned int *w, unsigned int *h)
  342. {
  343. struct sys_screen s;
  344. if (u_syscall1(SYS_SCREEN_INFO, (int)&s) != 0)
  345. return -1;
  346. *w = s.width;
  347. *h = s.height;
  348. return 0;
  349. }
  350. static inline int u_win_pointer(struct win_pointer *out)
  351. {
  352. return u_syscall1(SYS_WIN_POINTER, (int)out);
  353. }
  354. static inline int u_win_event(struct win_event *out)
  355. {
  356. return u_syscall1(SYS_WIN_EVENT, (int)out);
  357. }
  358. static inline int u_win_scroll(struct win_scroll *out)
  359. {
  360. return u_syscall1(SYS_WIN_SCROLL, (int)out);
  361. }
  362. static inline int u_wm_resize(unsigned int index, unsigned int cw,
  363. unsigned int ch, int dx, int dy)
  364. {
  365. struct wm_size s;
  366. s.w = cw;
  367. s.h = ch;
  368. s.dx = dx;
  369. s.dy = dy;
  370. return u_syscall2(SYS_WM_RESIZE, (int)index, (int)&s);
  371. }
  372. static inline int u_win_key_wait(unsigned int timeout_ms)
  373. {
  374. return u_syscall1(SYS_WIN_KEY_WAIT, (int)timeout_ms);
  375. }
  376. static inline int u_tty_create(void)
  377. {
  378. return u_syscall0(SYS_TTY_CREATE);
  379. }
  380. static inline int u_tty_alloc(void)
  381. {
  382. return u_syscall0(SYS_TTY_ALLOC);
  383. }
  384. static inline int u_tty_read(int id, char *buf, unsigned int max)
  385. {
  386. return u_syscall3(SYS_TTY_READ, id, (int)buf, (int)max);
  387. }
  388. static inline int u_tty_write(int id, const char *buf, unsigned int len)
  389. {
  390. return u_syscall3(SYS_TTY_WRITE, id, (int)buf, (int)len);
  391. }
  392. static inline int u_tty_close(int id)
  393. {
  394. return u_syscall1(SYS_TTY_CLOSE, id);
  395. }
  396. static inline int u_spawn(const char *path, const char *arg, int tty,
  397. const char *cwd)
  398. {
  399. struct sys_spawn s;
  400. s.path = path;
  401. s.arg = arg;
  402. s.cwd = cwd;
  403. s.tty = (unsigned int)tty;
  404. s.flags = 0;
  405. return u_syscall1(SYS_SPAWN, (int)&s);
  406. }
  407. /* Give the child to the caller's parent (the terminal/WM session) instead of
  408. * the caller, so it survives a launcher/file-manager exit but not the
  409. * session. */
  410. static inline int u_spawn_detached(const char *path, const char *arg, int tty,
  411. const char *cwd)
  412. {
  413. struct sys_spawn s;
  414. s.path = path;
  415. s.arg = arg;
  416. s.cwd = cwd;
  417. s.tty = (unsigned int)tty;
  418. s.flags = SYS_SPAWN_DETACH;
  419. return u_syscall1(SYS_SPAWN, (int)&s);
  420. }
  421. static inline int u_is_init(void)
  422. {
  423. return u_syscall0(SYS_IS_INIT);
  424. }
  425. static inline int u_spawn_kcmd(const char *name, const char *arg, int tty)
  426. {
  427. return u_syscall3(SYS_SPAWN_KCMD, (int)name, (int)arg, tty);
  428. }
  429. static inline int u_getpid(void)
  430. {
  431. return u_syscall0(SYS_GETPID);
  432. }
  433. static inline int u_alive(unsigned int pid)
  434. {
  435. return u_syscall1(SYS_ALIVE, (int)pid);
  436. }
  437. static inline int u_kill(unsigned int pid)
  438. {
  439. return u_syscall1(SYS_KILL, (int)pid);
  440. }
  441. static inline int u_chdir(const char *path)
  442. {
  443. return u_syscall1(SYS_CHDIR, (int)path);
  444. }
  445. static inline int u_key_poll(void)
  446. {
  447. return u_syscall0(SYS_KEY_POLL);
  448. }
  449. static inline int u_schedinfo(struct sys_schedinfo *out)
  450. {
  451. return u_syscall1(SYS_SCHEDINFO, (int)out);
  452. }
  453. static inline int u_procinfo(unsigned int index, struct sys_procinfo *out)
  454. {
  455. return u_syscall2(SYS_PROCINFO, (int)index, (int)out);
  456. }
  457. static inline int u_cpu_temp(struct sys_cputemp *out)
  458. {
  459. return u_syscall1(SYS_CPU_TEMP, (int)out);
  460. }
  461. static inline int u_sensors(struct sys_sensors *out)
  462. {
  463. return u_syscall1(SYS_SENSORS, (int)out);
  464. }
  465. static inline int u_tpec_sensors(struct sys_tpec_sensors *out)
  466. {
  467. return u_syscall1(SYS_TPEC_SENSORS, (int)out);
  468. }
  469. static inline int u_cros_sensors(struct sys_cros_sensors *out)
  470. {
  471. return u_syscall1(SYS_CROS_EC_SENSORS, (int)out);
  472. }
  473. static inline int u_power_info(struct sys_powerinfo *out, unsigned int flags)
  474. {
  475. return u_syscall2(SYS_POWER_INFO, (int)out, (int)flags);
  476. }
  477. /* Panel brightness in percent (-1 when the machine has no PWM backlight). */
  478. static inline int u_backlight_get(void)
  479. {
  480. return u_syscall2(SYS_BACKLIGHT, SYS_BACKLIGHT_GET, 0);
  481. }
  482. static inline int u_backlight_set(int percent)
  483. {
  484. return u_syscall2(SYS_BACKLIGHT, SYS_BACKLIGHT_SET, percent);
  485. }
  486. /* CPU power mode: -1 when the CPU has no P-state control. */
  487. static inline int u_cpufreq_get(void)
  488. {
  489. return u_syscall1(SYS_CPUFREQ, SYS_CPUFREQ_GET);
  490. }
  491. static inline int u_cpufreq_set(int mode)
  492. {
  493. return u_syscall1(SYS_CPUFREQ, mode);
  494. }
  495. /* Path metadata (size, directory flag, read-only mount). */
  496. static inline int u_stat(const char *path, struct sys_stat *out)
  497. {
  498. return u_syscall2(SYS_FS_STAT, (int)path, (int)out);
  499. }
  500. static inline int u_read_file(const char *path, void *buf, unsigned int max)
  501. {
  502. return u_syscall3(SYS_READ_FILE, (int)path, (int)buf, (int)max);
  503. }
  504. static inline int u_write_file(const char *path, const void *buf,
  505. unsigned int len)
  506. {
  507. return u_syscall3(SYS_WRITE_FILE, (int)path, (int)buf, (int)len);
  508. }
  509. /* TCP server (the caller owns the NIC). */
  510. static inline int u_tcp_listen(unsigned int port)
  511. {
  512. return u_syscall1(SYS_TCP_LISTEN, (int)port);
  513. }
  514. static inline int u_tcp_accept(void)
  515. {
  516. return u_syscall0(SYS_TCP_ACCEPT);
  517. }
  518. static inline int u_tcp_srv_recv(void *buf, unsigned int max,
  519. unsigned int timeout_ms)
  520. {
  521. return u_syscall3(SYS_TCP_SRV_RECV, (int)buf, (int)max,
  522. (int)timeout_ms);
  523. }
  524. static inline int u_tcp_srv_send(const void *buf, unsigned int len)
  525. {
  526. return u_syscall2(SYS_TCP_SRV_SEND, (int)buf, (int)len);
  527. }
  528. static inline void u_tcp_srv_close(void)
  529. {
  530. u_syscall0(SYS_TCP_SRV_CLOSE);
  531. }
  532. static inline int u_tcp_peer(unsigned char ip[4], unsigned int *port)
  533. {
  534. return u_syscall2(SYS_TCP_PEER, (int)ip, (int)port);
  535. }
  536. /* Raw FAT 8.3 field -> display name ("FOO TXT" -> "FOO.TXT"). */
  537. static inline void u_fat_name(const char *raw, char *out, unsigned int cap)
  538. {
  539. unsigned int n = 0, i, ext = 8;
  540. while (ext < 11 && raw[ext] == ' ')
  541. ext++;
  542. for (i = 0; i < 8 && raw[i] && raw[i] != ' ' && n + 1 < cap; i++)
  543. out[n++] = raw[i];
  544. if (ext < 11 && raw[ext]) {
  545. if (n + 1 < cap)
  546. out[n++] = '.';
  547. for (i = ext; i < 11 && raw[i] && raw[i] != ' ' &&
  548. n + 1 < cap; i++)
  549. out[n++] = raw[i];
  550. }
  551. out[n] = '\0';
  552. }
  553. /* Network state and device selection (the kernel owns the stack/NIC). */
  554. static inline int u_net_info(struct sys_netinfo *out)
  555. {
  556. return u_syscall1(SYS_NET_INFO, (int)out);
  557. }
  558. static inline int u_net_devlist(struct sys_netdev *out, unsigned int max)
  559. {
  560. return u_syscall2(SYS_NET_DEVLIST, (int)out, (int)max);
  561. }
  562. static inline int u_net_select(const char *name)
  563. {
  564. return u_syscall1(SYS_NET_SELECT, (int)name);
  565. }
  566. static inline int u_dhcp_run(unsigned int timeout_ms)
  567. {
  568. return u_syscall1(SYS_DHCP_RUN, (int)timeout_ms);
  569. }
  570. static inline int u_wifi(int op, int a2, int a3)
  571. {
  572. return u_syscall3(SYS_WIFI, op, a2, a3);
  573. }
  574. #endif