ucmd_lib.h 10 KB

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