ucmd_lib.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. /* Cursor position of this process's terminal screen (linear cell index);
  148. * used by `top` to repaint its frame in place. */
  149. static inline void u_sleep_ms(unsigned int ms)
  150. {
  151. u_syscall1(SYS_SLEEP_MS, (int)ms);
  152. }
  153. static inline void u_exit(int code)
  154. {
  155. u_syscall1(SYS_EXIT, code);
  156. for (;;)
  157. ;
  158. }
  159. static inline int u_rtc_hms(struct sys_time *t)
  160. {
  161. return u_syscall1(SYS_RTC_HMS, (int)t);
  162. }
  163. static inline int u_rtc_datetime(char *buf, unsigned int max)
  164. {
  165. return u_syscall2(SYS_RTC_DATETIME, (int)buf, (int)max);
  166. }
  167. /* Full RTC date/time (year 1970-2069) and its setter. The setter validates
  168. * the fields and returns 0 on success. */
  169. static inline int u_rtc_get(struct sys_datetime *t)
  170. {
  171. return u_syscall1(SYS_RTC_GET, (int)t);
  172. }
  173. static inline int u_rtc_set(const struct sys_datetime *t)
  174. {
  175. return u_syscall1(SYS_RTC_SET, (int)t);
  176. }
  177. /* Fetch the UTC time from an SNTP server (empty name = pool.ntp.org) and
  178. * fill `dt`. Returns SYS_NTP_*; the kernel owns the NIC for the duration. */
  179. static inline int u_ntp_sync(const char *server, unsigned int timeout_ms,
  180. struct sys_datetime *dt)
  181. {
  182. return u_syscall3(SYS_NTP_SYNC, (int)server, (int)timeout_ms, (int)dt);
  183. }
  184. /* Window-manager service (only the kernel-spawned /ramfs/bin/wm may use it). */
  185. static inline int u_wm_attach(struct wm_screen_info *out)
  186. {
  187. return u_syscall1(SYS_WM_ATTACH, (int)out);
  188. }
  189. static inline int u_wm_info(unsigned int index, struct wm_window_info *out)
  190. {
  191. return u_syscall2(SYS_WM_INFO, (int)index, (int)out);
  192. }
  193. static inline int u_wm_map(unsigned int index, struct wm_map_info *out)
  194. {
  195. return u_syscall2(SYS_WM_MAP, (int)index, (int)out);
  196. }
  197. static inline int u_wm_ack(unsigned int index, unsigned int seq)
  198. {
  199. return u_syscall2(SYS_WM_ACK, (int)index, (int)seq);
  200. }
  201. static inline int u_wm_focus(unsigned int index)
  202. {
  203. return u_syscall1(SYS_WM_FOCUS, (int)index);
  204. }
  205. static inline int u_wm_set_geom(unsigned int index, int x, int y)
  206. {
  207. struct wm_geom g;
  208. g.x = x;
  209. g.y = y;
  210. return u_syscall2(SYS_WM_SET_GEOM, (int)index, (int)&g);
  211. }
  212. static inline int u_wm_create_term(void)
  213. {
  214. return u_syscall0(SYS_WM_CREATE_TERM);
  215. }
  216. static inline int u_wm_close(unsigned int index)
  217. {
  218. return u_syscall1(SYS_WM_CLOSE, (int)index);
  219. }
  220. static inline int u_wm_poll(struct wm_status *out)
  221. {
  222. return u_syscall1(SYS_WM_POLL, (int)out);
  223. }
  224. static inline int u_wm_begin(void)
  225. {
  226. return u_syscall0(SYS_WM_BEGIN);
  227. }
  228. static inline int u_wm_present(struct wm_present_info *in)
  229. {
  230. return u_syscall1(SYS_WM_PRESENT, (int)in);
  231. }
  232. static inline int u_wm_sync(void)
  233. {
  234. return u_syscall0(SYS_WM_SYNC);
  235. }
  236. /* Generic canvas window (gfx commands own a window instead of replacing a
  237. * terminal's content). */
  238. static inline int u_win_create_flags(unsigned int w, unsigned int h,
  239. const char *title, unsigned int flags,
  240. struct win_info *out)
  241. {
  242. struct win_create in;
  243. unsigned int i = 0;
  244. in.w = w;
  245. in.h = h;
  246. in.flags = flags;
  247. while (title && title[i] && i < sizeof(in.title) - 1) {
  248. in.title[i] = title[i];
  249. i++;
  250. }
  251. in.title[i] = '\0';
  252. return u_syscall2(SYS_WIN_CREATE, (int)&in, (int)out);
  253. }
  254. static inline int u_win_create(unsigned int w, unsigned int h, const char *title,
  255. struct win_info *out)
  256. {
  257. return u_win_create_flags(w, h, title, 0, out);
  258. }
  259. /* A chromeless panel anchored by the WM under the top-bar status icons. */
  260. static inline int u_win_create_popover(unsigned int w, unsigned int h,
  261. const char *title,
  262. struct win_info *out)
  263. {
  264. return u_win_create_flags(w, h, title, WIN_F_POPOVER, out);
  265. }
  266. static inline int u_win_present(void)
  267. {
  268. return u_syscall1(SYS_WIN_PRESENT, 0);
  269. }
  270. /* Publish only the content-relative rectangle that changed. */
  271. static inline int u_win_present_rect(int x, int y, unsigned int w,
  272. unsigned int h)
  273. {
  274. struct wm_win_present in;
  275. in.x = (unsigned int)x;
  276. in.y = (unsigned int)y;
  277. in.w = w;
  278. in.h = h;
  279. return u_syscall1(SYS_WIN_PRESENT, (int)&in);
  280. }
  281. static inline int u_win_close(void)
  282. {
  283. return u_syscall0(SYS_WIN_CLOSE);
  284. }
  285. static inline int u_win_info(struct win_info *out)
  286. {
  287. return u_syscall1(SYS_WIN_INFO, (int)out);
  288. }
  289. static inline int u_win_ack_resize(void)
  290. {
  291. return u_syscall0(SYS_WIN_ACK_RESIZE);
  292. }
  293. /* Ask the kernel to grow/shrink this canvas window (a popover expanding a
  294. * section); ngui_begin() acknowledges the new canvas on the next frame. */
  295. static inline int u_win_resize_self(unsigned int w, unsigned int h)
  296. {
  297. return u_syscall2(SYS_WIN_RESIZE_SELF, (int)w, (int)h);
  298. }
  299. /* Cursor shown while the pointer is over this window's content
  300. * (enum cursor_shape in cursor_abi.h). */
  301. static inline int u_win_set_cursor(int shape)
  302. {
  303. return u_syscall1(SYS_WIN_SET_CURSOR, shape);
  304. }
  305. /* WM only: cursor for the WM's own chrome and drags; CURSOR_NO_OVERRIDE
  306. * hands the pointer back to the windows. */
  307. static inline int u_wm_set_cursor(int shape)
  308. {
  309. return u_syscall1(SYS_WM_SET_CURSOR, shape);
  310. }
  311. /* WM only: take the pointer while dragging/resizing, so canvas clients do
  312. * not see the held button; release it when the drag ends. */
  313. static inline int u_wm_mouse_capture(int on)
  314. {
  315. return u_syscall1(SYS_WM_MOUSE_CAPTURE, on);
  316. }
  317. static inline int u_screen_size(unsigned int *w, unsigned int *h)
  318. {
  319. struct sys_screen s;
  320. if (u_syscall1(SYS_SCREEN_INFO, (int)&s) != 0)
  321. return -1;
  322. *w = s.width;
  323. *h = s.height;
  324. return 0;
  325. }
  326. static inline int u_win_pointer(struct win_pointer *out)
  327. {
  328. return u_syscall1(SYS_WIN_POINTER, (int)out);
  329. }
  330. static inline int u_win_event(struct win_event *out)
  331. {
  332. return u_syscall1(SYS_WIN_EVENT, (int)out);
  333. }
  334. static inline int u_win_scroll(struct win_scroll *out)
  335. {
  336. return u_syscall1(SYS_WIN_SCROLL, (int)out);
  337. }
  338. static inline int u_wm_resize(unsigned int index, unsigned int cw,
  339. unsigned int ch)
  340. {
  341. struct wm_size s;
  342. s.w = cw;
  343. s.h = ch;
  344. return u_syscall2(SYS_WM_RESIZE, (int)index, (int)&s);
  345. }
  346. static inline int u_win_key_wait(unsigned int timeout_ms)
  347. {
  348. return u_syscall1(SYS_WIN_KEY_WAIT, (int)timeout_ms);
  349. }
  350. static inline int u_tty_create(void)
  351. {
  352. return u_syscall0(SYS_TTY_CREATE);
  353. }
  354. static inline int u_tty_read(int id, char *buf, unsigned int max)
  355. {
  356. return u_syscall3(SYS_TTY_READ, id, (int)buf, (int)max);
  357. }
  358. static inline int u_spawn(const char *path, const char *arg, int tty,
  359. const char *cwd)
  360. {
  361. struct sys_spawn s;
  362. s.path = path;
  363. s.arg = arg;
  364. s.cwd = cwd;
  365. s.tty = (unsigned int)tty;
  366. return u_syscall1(SYS_SPAWN, (int)&s);
  367. }
  368. static inline int u_spawn_kcmd(const char *name, const char *arg, int tty)
  369. {
  370. return u_syscall3(SYS_SPAWN_KCMD, (int)name, (int)arg, tty);
  371. }
  372. static inline int u_getpid(void)
  373. {
  374. return u_syscall0(SYS_GETPID);
  375. }
  376. static inline int u_alive(unsigned int pid)
  377. {
  378. return u_syscall1(SYS_ALIVE, (int)pid);
  379. }
  380. static inline int u_kill(unsigned int pid)
  381. {
  382. return u_syscall1(SYS_KILL, (int)pid);
  383. }
  384. static inline int u_chdir(const char *path)
  385. {
  386. return u_syscall1(SYS_CHDIR, (int)path);
  387. }
  388. static inline int u_key_poll(void)
  389. {
  390. return u_syscall0(SYS_KEY_POLL);
  391. }
  392. static inline int u_schedinfo(struct sys_schedinfo *out)
  393. {
  394. return u_syscall1(SYS_SCHEDINFO, (int)out);
  395. }
  396. static inline int u_cpu_temp(struct sys_cputemp *out)
  397. {
  398. return u_syscall1(SYS_CPU_TEMP, (int)out);
  399. }
  400. static inline int u_sensors(struct sys_sensors *out)
  401. {
  402. return u_syscall1(SYS_SENSORS, (int)out);
  403. }
  404. static inline int u_tpec_sensors(struct sys_tpec_sensors *out)
  405. {
  406. return u_syscall1(SYS_TPEC_SENSORS, (int)out);
  407. }
  408. static inline int u_cros_sensors(struct sys_cros_sensors *out)
  409. {
  410. return u_syscall1(SYS_CROS_EC_SENSORS, (int)out);
  411. }
  412. static inline int u_power_info(struct sys_powerinfo *out, unsigned int flags)
  413. {
  414. return u_syscall2(SYS_POWER_INFO, (int)out, (int)flags);
  415. }
  416. /* Panel brightness in percent (-1 when the machine has no PWM backlight). */
  417. static inline int u_backlight_get(void)
  418. {
  419. return u_syscall2(SYS_BACKLIGHT, SYS_BACKLIGHT_GET, 0);
  420. }
  421. static inline int u_backlight_set(int percent)
  422. {
  423. return u_syscall2(SYS_BACKLIGHT, SYS_BACKLIGHT_SET, percent);
  424. }
  425. /* CPU power mode: -1 when the CPU has no P-state control. */
  426. static inline int u_cpufreq_get(void)
  427. {
  428. return u_syscall1(SYS_CPUFREQ, SYS_CPUFREQ_GET);
  429. }
  430. static inline int u_cpufreq_set(int mode)
  431. {
  432. return u_syscall1(SYS_CPUFREQ, mode);
  433. }
  434. /* Path metadata (size, directory flag, read-only mount). */
  435. static inline int u_stat(const char *path, struct sys_stat *out)
  436. {
  437. return u_syscall2(SYS_FS_STAT, (int)path, (int)out);
  438. }
  439. /* TCP server (the caller owns the NIC). */
  440. static inline int u_tcp_listen(unsigned int port)
  441. {
  442. return u_syscall1(SYS_TCP_LISTEN, (int)port);
  443. }
  444. static inline int u_tcp_accept(void)
  445. {
  446. return u_syscall0(SYS_TCP_ACCEPT);
  447. }
  448. static inline int u_tcp_srv_recv(void *buf, unsigned int max,
  449. unsigned int timeout_ms)
  450. {
  451. return u_syscall3(SYS_TCP_SRV_RECV, (int)buf, (int)max,
  452. (int)timeout_ms);
  453. }
  454. static inline int u_tcp_srv_send(const void *buf, unsigned int len)
  455. {
  456. return u_syscall2(SYS_TCP_SRV_SEND, (int)buf, (int)len);
  457. }
  458. static inline void u_tcp_srv_close(void)
  459. {
  460. u_syscall0(SYS_TCP_SRV_CLOSE);
  461. }
  462. static inline int u_tcp_peer(unsigned char ip[4], unsigned int *port)
  463. {
  464. return u_syscall2(SYS_TCP_PEER, (int)ip, (int)port);
  465. }
  466. /* Raw FAT 8.3 field -> display name ("FOO TXT" -> "FOO.TXT"). */
  467. static inline void u_fat_name(const char *raw, char *out, unsigned int cap)
  468. {
  469. unsigned int n = 0, i, ext = 8;
  470. while (ext < 11 && raw[ext] == ' ')
  471. ext++;
  472. for (i = 0; i < 8 && raw[i] && raw[i] != ' ' && n + 1 < cap; i++)
  473. out[n++] = raw[i];
  474. if (ext < 11 && raw[ext]) {
  475. if (n + 1 < cap)
  476. out[n++] = '.';
  477. for (i = ext; i < 11 && raw[i] && raw[i] != ' ' &&
  478. n + 1 < cap; i++)
  479. out[n++] = raw[i];
  480. }
  481. out[n] = '\0';
  482. }
  483. /* Network state and device selection (the kernel owns the stack/NIC). */
  484. static inline int u_net_info(struct sys_netinfo *out)
  485. {
  486. return u_syscall1(SYS_NET_INFO, (int)out);
  487. }
  488. static inline int u_net_devlist(struct sys_netdev *out, unsigned int max)
  489. {
  490. return u_syscall2(SYS_NET_DEVLIST, (int)out, (int)max);
  491. }
  492. static inline int u_net_select(const char *name)
  493. {
  494. return u_syscall1(SYS_NET_SELECT, (int)name);
  495. }
  496. static inline int u_dhcp_run(unsigned int timeout_ms)
  497. {
  498. return u_syscall1(SYS_DHCP_RUN, (int)timeout_ms);
  499. }
  500. static inline int u_wifi(int op, int a2, int a3)
  501. {
  502. return u_syscall3(SYS_WIFI, op, a2, a3);
  503. }
  504. #endif