| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- /* Tiny userland shim for ring-3 commands: syscall wrappers + string output.
- * Freestanding (no libc); include "syscall.h" for the syscall numbers. */
- #ifndef UCMD_LIB_H
- #define UCMD_LIB_H
- #include "syscall.h"
- static inline int u_syscall1(int num, int a1)
- {
- int ret;
- __asm__ volatile ("int $0x80"
- : "=a"(ret)
- : "a"(num), "b"(a1)
- : "memory");
- return ret;
- }
- static inline int u_syscall0(int num)
- {
- int ret;
- __asm__ volatile ("int $0x80"
- : "=a"(ret)
- : "a"(num)
- : "memory");
- return ret;
- }
- static inline int u_syscall2(int num, int a1, int a2)
- {
- int ret;
- __asm__ volatile ("int $0x80"
- : "=a"(ret)
- : "a"(num), "b"(a1), "c"(a2)
- : "memory");
- return ret;
- }
- static inline int u_syscall3(int num, int a1, int a2, int a3)
- {
- int ret;
- __asm__ volatile ("int $0x80"
- : "=a"(ret)
- : "a"(num), "b"(a1), "c"(a2), "d"(a3)
- : "memory");
- return ret;
- }
- static inline void u_putc(char c)
- {
- char b[2];
- b[0] = c;
- b[1] = '\0';
- u_syscall1(SYS_PUTSTR, (int)b);
- }
- static inline void u_puts(const char *s)
- {
- u_syscall1(SYS_PUTSTR, (int)s);
- }
- static inline void u_putdec(unsigned int n)
- {
- char buf[12];
- char *p = buf + sizeof(buf) - 1;
- *p = '\0';
- if (n == 0) {
- u_putc('0');
- return;
- }
- while (n) {
- *--p = (char)('0' + (n % 10));
- n /= 10;
- }
- u_puts(p);
- }
- static inline void u_puthex(unsigned int n)
- {
- static const char hex[] = "0123456789ABCDEF";
- char buf[11];
- int i;
- for (i = 0; i < 8; i++)
- buf[i] = hex[(n >> (28 - i * 4)) & 0xF];
- buf[8] = '\0';
- u_puts(buf);
- }
- /* "a.b.c.d" (strict, 4 parts, 0-255) -> 0, else -1. */
- static inline int u_parse_ip(const char *s, unsigned char ip[4])
- {
- unsigned int i, val = 0, nparts = 0;
- int have_digit = 0;
- for (i = 0; ; i++) {
- char c = s[i];
- if (c >= '0' && c <= '9') {
- val = val * 10 + (unsigned int)(c - '0');
- if (val > 255)
- return -1;
- have_digit = 1;
- } else if (c == '.' || c == '\0') {
- if (!have_digit || nparts >= 4)
- return -1;
- ip[nparts++] = (unsigned char)val;
- val = 0;
- have_digit = 0;
- if (c == '\0')
- break;
- } else {
- return -1;
- }
- }
- return (nparts == 4) ? 0 : -1;
- }
- static inline void u_put_ip(const unsigned char ip[4])
- {
- unsigned int i;
- for (i = 0; i < 4; i++) {
- if (i)
- u_putc('.');
- u_putdec(ip[i]);
- }
- }
- static inline void u_put_hex2(unsigned int v)
- {
- static const char hex[] = "0123456789ABCDEF";
- u_putc(hex[(v >> 4) & 0xF]);
- u_putc(hex[v & 0xF]);
- }
- static inline void u_put_mac(const unsigned char mac[6])
- {
- unsigned int i;
- for (i = 0; i < 6; i++) {
- if (i)
- u_putc(':');
- u_put_hex2(mac[i]);
- }
- }
- static inline int u_getcwd(char *buf, unsigned int max)
- {
- return u_syscall2(SYS_GETCWD, (int)buf, (int)max);
- }
- static inline unsigned int u_ticks_ms(void)
- {
- return (unsigned int)u_syscall0(SYS_GET_TICKS_MS);
- }
- /* Cursor position of this process's terminal screen (linear cell index);
- * used by `top` to repaint its frame in place. */
- static inline unsigned int u_get_cursor(void)
- {
- return (unsigned int)u_syscall0(SYS_GET_CURSOR);
- }
- static inline void u_set_cursor(unsigned int pos)
- {
- u_syscall1(SYS_SET_CURSOR, (int)pos);
- }
- static inline void u_sleep_ms(unsigned int ms)
- {
- u_syscall1(SYS_SLEEP_MS, (int)ms);
- }
- static inline void u_exit(int code)
- {
- u_syscall1(SYS_EXIT, code);
- for (;;)
- ;
- }
- static inline int u_rtc_hms(struct sys_time *t)
- {
- return u_syscall1(SYS_RTC_HMS, (int)t);
- }
- static inline int u_gfx_begin(struct sys_gfx_info *info)
- {
- return u_syscall1(SYS_GFX_BEGIN, (int)info);
- }
- static inline int u_gfx_present(void)
- {
- return u_syscall0(SYS_GFX_PRESENT);
- }
- static inline int u_gfx_end(void)
- {
- return u_syscall0(SYS_GFX_END);
- }
- static inline int u_key_poll(void)
- {
- return u_syscall0(SYS_KEY_POLL);
- }
- #endif
|