ucmd_lib.h 12 KB

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