Explorar o código

Move curl to ring 3; expose the TCP client; fix blocking net syscalls

- sys_net.c gains the TCP client as syscalls (TCP_CONNECT/PEER_MSS/SEND/
  RECV/CLOSE, with tcp.c holding the single connection) plus
  NET_NEXT_HOP/NET_ARP/NET_CONFIGURED, so ucmd_curl.c is the old curl.c
  with its HTTP logic intact (URL parse, request, header and chunked
  decoding) and only the network parts replaced by syscalls.  curl.c is
  gone.
- The net syscalls that wait inside the stack (ARP, DNS, DHCP, TCP) now
  re-enable interrupts for the duration: the syscall stub enters with
  them off and the deadline loops spin on get_ticks_ms(), so a frozen
  PIT would never satisfy a deadline and the machine would wedge (QEMU
  replies instantly, which is why the fast paths hid it).  The netdev
  ownership already serializes net users, so being preempted mid-wait is
  safe.

Verified on QEMU (slirp + ne2k_pci, the host running python3 -m
http.server): `curl http://10.0.2.2:8000/hello.txt` prints the status
line, the headers, the exact body and "26 bytes received, status 200";
a missing path gives 404; https is refused; `host example.com` resolves
through the real resolver.  The timeout paths were checked separately:
`ping 10.255.255.1` runs its four ICMP timeouts (4 x 1 s inside the net
syscalls) and returns, and a dead DNS server yields "dns: no reply"
instead of hanging.

AGENTS.md: the command lists, the TCP calls, the blocking note and the
updated known issues (only display/lsusb are left; the ELF page-padding
observation is issue 9).
Johannes Zellner hai 1 mes
pai
achega
0ac3383f09
Modificáronse 8 ficheiros con 338 adicións e 215 borrados
  1. 19 9
      AGENTS.md
  2. 3 6
      Makefile
  3. 109 2
      sys_net.c
  4. 9 0
      sys_net.h
  5. 20 0
      syscall.c
  6. 11 0
      syscall.h
  7. 4 2
      terminal.c
  8. 163 196
      ucmd_curl.c

+ 19 - 9
AGENTS.md

@@ -187,7 +187,7 @@ below the run area), so adding code no longer moves any address.
   `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`,
   `mouseinfo`, `storage`, `netinfo`, `netip`, `netdns`, `host`, `dhcp`,
   `ping`, `pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`,
-  `netloop`, `netreset`.  They talk to the kernel through `int 0x80`
+  `netloop`, `netreset`, `curl`.  They talk to the kernel through `int 0x80`
   (`syscall.h`: putstr/ticks/sleep/exit, the whole-file service in
   `sys_fs.c`, the state snapshots in `sys_info.c` and the network service in
   `sys_net.c`) and inherit the terminal's screen, so their output lands in
@@ -203,6 +203,13 @@ below the run area), so adding code no longer moves any address.
   process at a time (`netdev_acquire`): the background kexec watcher pauses
   while a command owns it, and the reaper force-releases it if that process
   dies (`sys_net_reap`, driven by `netdev_owner()`/`netdev_release_all()`).
+  The TCP client is exposed as `TCP_CONNECT/PEER_MSS/SEND/RECV/CLOSE` (one
+  connection at a time, `tcp.c` keeps the state) plus `NET_NEXT_HOP`/`NET_ARP`
+  for the route to the peer.  Calls that wait inside the stack (ARP, DNS,
+  DHCP, TCP) re-enable interrupts for the duration: the syscall stub enters
+  with them off, and the deadlines spin on `get_ticks_ms()` -- a frozen tick
+  counter would never satisfy them.  The netdev ownership already serializes
+  net users, so being preempted mid-wait is safe.
 
 ## Shell commands
 
@@ -239,9 +246,9 @@ filesystem commands operate inside a mount, e.g. `cat /floppy1/HELLO.TXT`).
 
 `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`,
 `mouseinfo`, `storage`, `netinfo`, `netip`, `netdns`, `host`, `dhcp`, `ping`,
-`pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`, `netloop`
-and `netreset` run as ring-3 processes (embedded `ucmd_*.c`); the rest are
-kernel commands spawned as processes, or shell builtins (`cd`, `clear`,
+`pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`, `netloop`,
+`netreset` and `curl` run as ring-3 processes (embedded `ucmd_*.c`); the rest
+are kernel commands spawned as processes, or shell builtins (`cd`, `clear`,
 `help`).
 
 ## Serial hot-reload (kexec) workflow
@@ -582,8 +589,11 @@ Caveats:
 7. Killing/reaping a ring-3 process frees its kernel stack and page directory
    but not the user pages mapped into it (a few pages leak per command run;
    needs a page-table walk/unmap on reap).
-8. `curl` (needs a TCP socket API), `display` (needs a GPU snapshot) and
-   `lsusb` (a 45-byte wrapper around the USB tree printer) are the last
-   commands still living in the kernel, plus the privileged ones (`peek`,
-   `poke`, `dbg`, `serial`, `kexec`, `reboot`, `poweroff`, `format`,
-   `run`/`prog`/`space`, `ps`/`kill`).
+8. `display` (needs a GPU snapshot) and `lsusb` (a 45-byte wrapper around
+   the USB tree printer) are the last non-privileged commands still living in
+   the kernel, plus the privileged ones (`peek`, `poke`, `dbg`, `serial`,
+   `kexec`, `reboot`, `poweroff`, `format`, `run`/`prog`/`space`, `ps`/`kill`).
+9. Every ring-3 command is a full ELF whose LOAD segment starts at file
+   offset 0x1000 (the linker's page alignment), so ~4 KB of each ~8 KB blob is
+   padding: 25 commands carry ~100 KB of it.  Linking them with
+   `-z max-page-size=0x100` would remove most of that.

+ 3 - 6
Makefile

@@ -158,9 +158,6 @@ dns.o: dns.c dns.h io.h timer.h netdev.h net.h
 tcp.o: tcp.c tcp.h io.h timer.h netdev.h net.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-curl.o: curl.c io.h timer.h netdev.h net.h dns.h tcp.h
-	$(CC) $(CFLAGS) -o $@ $<
-
 netdev.o: netdev.c netdev.h ne2000.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -251,7 +248,7 @@ sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.o hello_embed.o prog_embed.o space_embed.o ucmd_cpuinfo_embed.o ucmd_ls_embed.o ucmd_cat_embed.o ucmd_save_embed.o ucmd_rm_embed.o ucmd_mkdir_embed.o ucmd_rmdir_embed.o ucmd_meminfo_embed.o ucmd_mouseinfo_embed.o ucmd_storage_embed.o ucmd_netinfo_embed.o ucmd_netip_embed.o ucmd_netdns_embed.o ucmd_host_embed.o ucmd_dhcp_embed.o ucmd_netsend_embed.o ucmd_netrecv_embed.o ucmd_ping_embed.o ucmd_pong_embed.o ucmd_netlog_embed.o ucmd_netconsole_embed.o ucmd_netregs_embed.o ucmd_netloop_embed.o ucmd_netreset_embed.o
+kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o hello_embed.o prog_embed.o space_embed.o ucmd_cpuinfo_embed.o ucmd_ls_embed.o ucmd_cat_embed.o ucmd_save_embed.o ucmd_rm_embed.o ucmd_mkdir_embed.o ucmd_rmdir_embed.o ucmd_meminfo_embed.o ucmd_mouseinfo_embed.o ucmd_storage_embed.o ucmd_netinfo_embed.o ucmd_netip_embed.o ucmd_netdns_embed.o ucmd_host_embed.o ucmd_dhcp_embed.o ucmd_netsend_embed.o ucmd_netrecv_embed.o ucmd_ping_embed.o ucmd_pong_embed.o ucmd_netlog_embed.o ucmd_netconsole_embed.o ucmd_netregs_embed.o ucmd_netloop_embed.o ucmd_netreset_embed.o ucmd_curl_embed.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -340,7 +337,7 @@ UCMDS = ucmd_cpuinfo ucmd_ls ucmd_cat ucmd_save ucmd_rm ucmd_mkdir ucmd_rmdir \
         ucmd_meminfo ucmd_mouseinfo ucmd_storage \
         ucmd_netinfo ucmd_netip ucmd_netdns ucmd_host ucmd_dhcp \
         ucmd_netsend ucmd_netrecv ucmd_ping ucmd_pong ucmd_netlog \
-        ucmd_netconsole ucmd_netregs ucmd_netloop ucmd_netreset
+        ucmd_netconsole ucmd_netregs ucmd_netloop ucmd_netreset ucmd_curl
 
 # Keep the linked ELFs around (make would treat them as disposable
 # intermediates between the pattern rules).
@@ -526,4 +523,4 @@ net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o $(addprefix $(UCMDS),.o) $(addprefix $(UCMDS),.elf) $(addsuffix _embed.o,$(UCMDS)) bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o $(addprefix $(UCMDS),.o) $(addprefix $(UCMDS),.elf) $(addsuffix _embed.o,$(UCMDS)) bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor

+ 109 - 2
sys_net.c

@@ -16,6 +16,27 @@
 #include "netdev.h"
 #include "netlog.h"
 #include "process.h"
+#include "tcp.h"
+#include "timer.h"
+
+/* One TCP connection at a time (the stack is single-connection, and the
+ * receiver is owned by the process using it). */
+static struct tcp_conn tcp;
+
+/* Long waits inside the stack (ARP/DNS/DHCP/TCP) spin on get_ticks_ms(), so
+ * they need interrupts: the syscall stub entered with them off, and a frozen
+ * tick counter would never satisfy a deadline.  The netdev ownership already
+ * serializes net users, so running with IRQs on (and being preempted) is
+ * safe. */
+static void net_enter(void)
+{
+	__asm__ volatile ("sti");
+}
+
+static void net_exit(void)
+{
+	__asm__ volatile ("cli");
+}
 
 static unsigned int caller_pid(void)
 {
@@ -167,7 +188,12 @@ int sys_net_reset(void)
 
 int sys_dhcp_run(unsigned int timeout_ms)
 {
-	return dhcp_run(timeout_ms);
+	int r;
+
+	net_enter();
+	r = dhcp_run(timeout_ms);
+	net_exit();
+	return r;
 }
 
 int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
@@ -177,9 +203,90 @@ int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
 
 int sys_dns_resolve(const char *name, unsigned char ip[4])
 {
+	int r;
+
 	if (!owns_nic())
 		return -1;
-	return dns_resolve(name, ip);
+	net_enter();
+	r = dns_resolve(name, ip);
+	net_exit();
+	return r;
+}
+
+/* ---- TCP client ---- */
+
+int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
+		    unsigned int port)
+{
+	int r;
+
+	if (!owns_nic())
+		return -3;
+	net_enter();
+	r = tcp_connect(mac, ip, port, &tcp);
+	net_exit();
+	return r;
+}
+
+int sys_tcp_peer_mss(void)
+{
+	return (int)tcp.peer_mss;
+}
+
+int sys_tcp_send(const void *buf, unsigned int len)
+{
+	int r;
+
+	if (!owns_nic())
+		return -3;
+	net_enter();
+	r = tcp_send(&tcp, buf, len);
+	net_exit();
+	return r;
+}
+
+int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
+{
+	int r;
+
+	if (!owns_nic())
+		return -2;
+	net_enter();
+	r = tcp_recv(&tcp, buf, max, get_ticks_ms() + timeout_ms);
+	net_exit();
+	return r;
+}
+
+void sys_tcp_close(void)
+{
+	if (!owns_nic())
+		return;
+	net_enter();
+	tcp_close(&tcp);
+	net_exit();
+}
+
+int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4])
+{
+	net_next_hop(dst, hop);
+	return 0;
+}
+
+int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
+{
+	int r;
+
+	if (!owns_nic())
+		return -2;
+	net_enter();
+	r = net_arp_resolve(ip, mac);
+	net_exit();
+	return r;
+}
+
+int sys_net_configured(void)
+{
+	return net_is_configured();
 }
 
 int sys_netlog(const char *text)

+ 9 - 0
sys_net.h

@@ -31,5 +31,14 @@ int sys_dhcp_info(unsigned char server[4], unsigned int *lease);
 int sys_dns_resolve(const char *name, unsigned char ip[4]);
 int sys_netlog(const char *text);
 int sys_netlog_conf(int enabled, const unsigned char *ip);
+int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
+		    unsigned int port);
+int sys_tcp_peer_mss(void);
+int sys_tcp_send(const void *buf, unsigned int len);
+int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms);
+void sys_tcp_close(void);
+int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4]);
+int sys_net_arp(const unsigned char ip[4], unsigned char mac[6]);
+int sys_net_configured(void);
 
 #endif

+ 20 - 0
syscall.c

@@ -103,6 +103,26 @@ int syscall_dispatch(int num, int a1, int a2, int a3)
 		return sys_netlog((const char *)a1);
 	case SYS_NETLOG_CONF:
 		return sys_netlog_conf(a1, (const unsigned char *)a2);
+	case SYS_TCP_CONNECT:
+		return sys_tcp_connect((const unsigned char *)a1,
+				       (const unsigned char *)a2,
+				       (unsigned int)a3);
+	case SYS_TCP_PEER_MSS:
+		return sys_tcp_peer_mss();
+	case SYS_TCP_SEND:
+		return sys_tcp_send((const void *)a1, (unsigned int)a2);
+	case SYS_TCP_RECV:
+		return sys_tcp_recv((void *)a1, (unsigned int)a2, (unsigned int)a3);
+	case SYS_TCP_CLOSE:
+		sys_tcp_close();
+		return 0;
+	case SYS_NET_NEXT_HOP:
+		return sys_net_next_hop((const unsigned char *)a1,
+					(unsigned char *)a2);
+	case SYS_NET_ARP:
+		return sys_net_arp((const unsigned char *)a1, (unsigned char *)a2);
+	case SYS_NET_CONFIGURED:
+		return sys_net_configured();
 	default:
 		return -1;
 	}

+ 11 - 0
syscall.h

@@ -57,6 +57,17 @@
 #define SYS_NETLOG        38  /* (const char *text) -> 0/-1 */
 #define SYS_NETLOG_CONF   39  /* (int enabled_or_-1, const ip[4] or NULL) */
 
+/* TCP client (see sys_net.c/tcp.c): one connection at a time; the caller
+ * must own the NIC (SYS_NET_ACQUIRE). */
+#define SYS_TCP_CONNECT   40  /* (mac[6], ip[4], port) -> 0/-1/-2/-3 */
+#define SYS_TCP_PEER_MSS  41  /* () -> the peer's MSS of the current conn */
+#define SYS_TCP_SEND      42  /* (buf, len) -> 0/-1/-2/-3 */
+#define SYS_TCP_RECV      43  /* (buf, max, timeout_ms) -> >0/0/-1/-2 */
+#define SYS_TCP_CLOSE     44  /* () */
+#define SYS_NET_NEXT_HOP  45  /* (dst[4], hop[4]) */
+#define SYS_NET_ARP       46  /* (ip[4], mac[6]) -> 0/-1/-2 (net_arp_resolve) */
+#define SYS_NET_CONFIGURED 47 /* () -> 1 when an IP address is set */
+
 struct sys_netinfo {
 	unsigned int present;
 	unsigned char mac[6];

+ 4 - 2
terminal.c

@@ -26,7 +26,6 @@ extern void program_format_main(const char *arg);
 extern void program_lspci_main(const char *filter);
 extern void program_peek_main(const char *arg);
 extern void program_poke_main(const char *arg);
-extern void program_curl_main(const char *arg);
 
 /* Print the prompt (with the current directory) and set line_start. */
 static void term_print_prompt(struct term *t, int first)
@@ -140,7 +139,7 @@ static const struct kcmd cmds[] = {
 	{ "dbg",        "dbg",                           CK_CUSTOM, 0, 0, 0, 0, cmd_dbg },
 	{ "serial",     "serial test|recv",              CK_CUSTOM, 0, 0, 0, 0, cmd_serial },
 	{ "clear",      "clear",                         CK_CUSTOM, 0, 0, 0, 0, cmd_clear },
-	{ "curl",       "curl <url>",                    CK_SPAWN_ARG, 0, 0, program_curl_main, 0, 0 },
+	{ "curl",       "curl <url>",                    CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
 	{ "netdns",     "netdns [a.b.c.d]",              CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
 	{ "host",       "host <name>",                   CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
 	{ "help",       "help",                          CK_CUSTOM, 0, 0, 0, 0, cmd_help },
@@ -199,6 +198,8 @@ extern unsigned char _binary_ucmd_netloop_elf_start[];
 extern unsigned char _binary_ucmd_netloop_elf_end[];
 extern unsigned char _binary_ucmd_netreset_elf_start[];
 extern unsigned char _binary_ucmd_netreset_elf_end[];
+extern unsigned char _binary_ucmd_curl_elf_start[];
+extern unsigned char _binary_ucmd_curl_elf_end[];
 
 struct ucmd_blob {
 	const char *name;
@@ -231,6 +232,7 @@ static const struct ucmd_blob ucmds[] = {
 	{ "netregs",      _binary_ucmd_netregs_elf_start,              _binary_ucmd_netregs_elf_end },
 	{ "netloop",      _binary_ucmd_netloop_elf_start,              _binary_ucmd_netloop_elf_end },
 	{ "netreset",     _binary_ucmd_netreset_elf_start,              _binary_ucmd_netreset_elf_end },
+	{ "curl",         _binary_ucmd_curl_elf_start,                 _binary_ucmd_curl_elf_end },
 };
 
 

+ 163 - 196
curl.c → ucmd_curl.c

@@ -1,16 +1,12 @@
-/* curl: fetch one http:// URL with a minimal HTTP/1.0 GET and dump the
- * status line, the headers and the body to the terminal.
+/* curl: ring-3 command.  Fetch one http:// URL with a minimal HTTP/1.0 GET and
+ * dump the status line, the headers and the body to the terminal.
  *
  * Deliberately small: no TLS, no redirect following (a Location header is
- * reported), no gzip; a chunked response is decoded.  Uses the DNS resolver
- * (dns.c) and the TCP client (tcp.c); the NIC is owned for the whole run. */
+ * reported), no gzip; a chunked response is decoded.  The TCP client (tcp.c)
+ * and the DNS resolver stay in the kernel; the nic is owned for the run
+ * (SYS_NET_ACQUIRE) and TCP goes through the SYS_TCP_* calls. */
 
-#include "io.h"
-#include "timer.h"
-#include "netdev.h"
-#include "net.h"
-#include "dns.h"
-#include "tcp.h"
+#include "ucmd_lib.h"
 
 #define URL_HOST_MAX 128
 #define URL_PATH_MAX 128
@@ -27,36 +23,6 @@ struct url {
 	unsigned int port;
 };
 
-/* ---- small print helpers ---- */
-
-static void put_dec(unsigned int v)
-{
-	char b[12];
-	int i = 0;
-
-	if (v == 0) {
-		io_putchar('0');
-		return;
-	}
-	while (v) {
-		b[i++] = (char)('0' + (v % 10));
-		v /= 10;
-	}
-	while (i)
-		io_putchar(b[--i]);
-}
-
-static void put_ip(const unsigned char *ip)
-{
-	unsigned int i;
-
-	for (i = 0; i < 4; i++) {
-		if (i)
-			io_putchar('.');
-		put_dec(ip[i]);
-	}
-}
-
 static int starts_with(const char *s, const char *prefix)
 {
 	unsigned int i = 0;
@@ -169,7 +135,6 @@ static unsigned int req_app_dec(char *buf, unsigned int pos, unsigned int v)
 /* ---- HTTP response reader (a refillable line/byte stream) ---- */
 
 struct reader {
-	struct tcp_conn *conn;
 	unsigned char buf[READER_MAX];
 	unsigned int len, pos;
 	unsigned int total_deadline;
@@ -180,16 +145,18 @@ struct reader {
 /* Returns 1 when new data (or EOF) is available, 0 on timeout/reset. */
 static int reader_fill(struct reader *r)
 {
-	unsigned int deadline, idle;
+	unsigned int now, idle, deadline, rel;
 	int n;
 
 	if (r->eof)
 		return 0;
 
-	idle = get_ticks_ms() + IDLE_TIMEOUT_MS;
+	now = u_ticks_ms();
+	idle = now + IDLE_TIMEOUT_MS;
 	deadline = idle < r->total_deadline ? idle : r->total_deadline;
+	rel = (int)(deadline - now) > 0 ? deadline - now : 1;
 
-	n = tcp_recv(r->conn, r->buf, sizeof(r->buf), deadline);
+	n = u_syscall3(SYS_TCP_RECV, (int)r->buf, sizeof(r->buf), (int)rel);
 	if (n > 0) {
 		r->len = (unsigned int)n;
 		r->pos = 0;
@@ -341,7 +308,7 @@ static unsigned int dump_body(struct reader *r, int have_limit,
 			break;
 		if (c == '\r')
 			continue;
-		io_putchar((char)c);
+		u_putc((char)c);
 		n++;
 	}
 	return n;
@@ -375,7 +342,7 @@ static unsigned int dump_chunked(struct reader *r)
 			k++;
 		}
 		if (k == 0) {
-			io_putstr("curl: bad chunk size\n");
+			u_puts("curl: bad chunk size\n");
 			return total;
 		}
 		if (sz == 0) {
@@ -389,176 +356,176 @@ static unsigned int dump_chunked(struct reader *r)
 
 			if (c < 0)
 				return total;
-			io_putchar((char)c);
+			u_putc((char)c);
 			total++;
 		}
 		reader_readline(r, line, sizeof(line));   /* the CRLF */
 	}
 }
 
-/* ---- the command ---- */
-
-void program_curl_main(const char *arg)
+void _start(const char *arg)
 {
-	netdev_acquire();
-	{
-		static struct url u;
-		static char req[REQ_MAX];
-		static struct reader r;
-		const struct netdev *nd = netdev_active();
-		unsigned char ip[4], hop[4], mac[6];
-		struct tcp_conn conn;
-		char line[LINE_MAX_C];
-		char location[128];
-		unsigned int n, status = 0, body = 0, content_len = 0;
-		int have_loc = 0, have_len = 0, chunked = 0, rv;
-
-		if (arg[0] == '\0') {
-			io_putstr("curl: usage curl <http://host[:port]/[path]>\n");
-			goto out;
-		}
-		rv = url_parse(arg, &u);
-		if (rv == -2) {
-			io_putstr("curl: https is not supported (no TLS)\n");
-			goto out;
-		}
-		if (rv != 0) {
-			io_putstr("curl: bad URL\n");
-			goto out;
-		}
-		if (!nd) {
-			io_putstr("curl: no NIC found\n");
-			goto out;
-		}
-		if (!net_is_configured()) {
-			io_putstr("curl: no IP address (run dhcp or netip)\n");
-			goto out;
-		}
+	static struct url u;
+	static char req[REQ_MAX];
+	static struct reader r;
+	struct sys_netinfo ni;
+	unsigned char ip[4], hop[4], mac[6];
+	char line[LINE_MAX_C];
+	char location[128];
+	unsigned int n, status = 0, body = 0, content_len = 0;
+	int have_loc = 0, have_len = 0, chunked = 0, rv;
+
+	if (!arg || !arg[0]) {
+		u_puts("curl: usage curl <http://host[:port]/[path]>\n");
+		u_exit(1);
+	}
 
-		io_putstr("curl: GET http://");
-		io_putstr(u.host);
-		if (u.port != 80) {
-			io_putstr(":");
-			put_dec(u.port);
-		}
-		io_putstr(u.path);
-		io_putchar('\n');
+	rv = url_parse(arg, &u);
+	if (rv == -2) {
+		u_puts("curl: https is not supported (no TLS)\n");
+		u_exit(1);
+	}
+	if (rv != 0) {
+		u_puts("curl: bad URL\n");
+		u_exit(1);
+	}
+	if (u_syscall1(SYS_NET_INFO, (int)&ni) != 1) {
+		u_puts("curl: no NIC found\n");
+		u_exit(1);
+	}
+	if (u_syscall0(SYS_NET_CONFIGURED) != 1) {
+		u_puts("curl: no IP address (run dhcp or netip)\n");
+		u_exit(1);
+	}
 
-		if (net_parse_ip(u.host, ip) != 0) {
-			if (dns_resolve(u.host, ip) != 0)
-				goto out;
-		}
+	u_puts("curl: GET http://");
+	u_puts(u.host);
+	if (u.port != 80) {
+		u_puts(":");
+		u_putdec(u.port);
+	}
+	u_puts(u.path);
+	u_putc('\n');
 
-		net_next_hop(ip, hop);
-		if (net_arp_resolve(hop, mac) != 0) {
-			io_putstr("curl: no ARP reply from ");
-			put_ip(hop);
-			io_putchar('\n');
-			goto out;
-		}
+	if (u_syscall0(SYS_NET_ACQUIRE) != 0) {
+		u_puts("curl: NIC busy\n");
+		u_exit(1);
+	}
 
-		rv = tcp_connect(mac, ip, u.port, &conn);
-		if (rv == -1) {
-			io_putstr("curl: connect timeout\n");
-			goto out;
-		}
-		if (rv == -2) {
-			io_putstr("curl: connection refused\n");
-			goto out;
-		}
-		if (rv != 0) {
-			io_putstr("curl: connect failed\n");
+	if (u_parse_ip(u.host, ip) != 0) {
+		if (u_syscall2(SYS_DNS_RESOLVE, (int)u.host, (int)ip) != 0)
 			goto out;
-		}
-		io_putstr("curl: connected, sending request\n");
-
-		n = req_app(req, 0, "GET ");
-		n = req_app(req, n, u.path);
-		n = req_app(req, n, " HTTP/1.0\r\nHost: ");
-		n = req_app(req, n, u.host);
-		if (u.port != 80) {
-			n = req_app(req, n, ":");
-			n = req_app_dec(req, n, u.port);
-		}
-		n = req_app(req, n, "\r\nUser-Agent: nebos-curl/0.1\r\n"
-				    "Accept: */*\r\nConnection: close\r\n\r\n");
-		if (n > conn.peer_mss) {
-			io_putstr("curl: request too long\n");
-			goto out;
-		}
-		rv = tcp_send(&conn, req, n);
-		if (rv != 0) {
-			io_putstr("curl: request send failed\n");
-			goto out;
-		}
+	}
 
-		r.conn = &conn;
-		r.len = 0;
-		r.pos = 0;
-		r.eof = 0;
-		r.error = 0;
-		r.total_deadline = get_ticks_ms() + TOTAL_TIMEOUT_MS;
+	u_syscall2(SYS_NET_NEXT_HOP, (int)ip, (int)hop);
+	rv = u_syscall2(SYS_NET_ARP, (int)hop, (int)mac);
+	if (rv != 0) {
+		u_puts("curl: no ARP reply from ");
+		u_put_ip(hop);
+		u_putc('\n');
+		goto out;
+	}
 
+	rv = u_syscall3(SYS_TCP_CONNECT, (int)mac, (int)ip, (int)u.port);
+	if (rv == -1) {
+		u_puts("curl: connect timeout\n");
+		goto out;
+	}
+	if (rv == -2) {
+		u_puts("curl: connection refused\n");
+		goto out;
+	}
+	if (rv != 0) {
+		u_puts("curl: connect failed\n");
+		goto out;
+	}
+	u_puts("curl: connected, sending request\n");
+
+	n = req_app(req, 0, "GET ");
+	n = req_app(req, n, u.path);
+	n = req_app(req, n, " HTTP/1.0\r\nHost: ");
+	n = req_app(req, n, u.host);
+	if (u.port != 80) {
+		n = req_app(req, n, ":");
+		n = req_app_dec(req, n, u.port);
+	}
+	n = req_app(req, n, "\r\nUser-Agent: nebos-curl/0.1\r\n"
+			    "Accept: */*\r\nConnection: close\r\n\r\n");
+	if ((int)n > u_syscall0(SYS_TCP_PEER_MSS)) {
+		u_puts("curl: request too long\n");
+		goto out;
+	}
+	rv = u_syscall2(SYS_TCP_SEND, (int)req, n);
+	if (rv != 0) {
+		u_puts("curl: request send failed\n");
+		goto out;
+	}
+
+	r.len = 0;
+	r.pos = 0;
+	r.eof = 0;
+	r.error = 0;
+	r.total_deadline = u_ticks_ms() + TOTAL_TIMEOUT_MS;
+
+	rv = reader_readline(&r, line, sizeof(line));
+	if (rv < 0) {
+		u_puts("curl: no response (");
+		u_puts(r.error == -2 ? "reset" : "timeout");
+		u_puts(")\n");
+		goto out;
+	}
+	status = parse_status(line);
+	u_puts("curl: ");
+	u_puts(line);
+	u_putc('\n');
+
+	for (;;) {
 		rv = reader_readline(&r, line, sizeof(line));
 		if (rv < 0) {
-			io_putstr("curl: no response (");
-			io_putstr(r.error == -2 ? "reset" : "timeout");
-			io_putstr(")\n");
+			u_puts("curl: connection lost in headers\n");
 			goto out;
 		}
-		status = parse_status(line);
-		io_putstr("curl: ");
-		io_putstr(line);
-		io_putchar('\n');
-
-		for (;;) {
-			rv = reader_readline(&r, line, sizeof(line));
-			if (rv < 0) {
-				io_putstr("curl: connection lost in headers\n");
-				goto out;
-			}
-			if (rv == 0)
-				break;              /* end of headers */
-			io_putstr(line);
-			io_putchar('\n');
-			if (hdr_match(line, "content-length"))
-				have_len = parse_dec_after_colon(line, &content_len);
-			else if (hdr_match(line, "transfer-encoding") &&
-				 contains_ci(line, "chunked"))
-				chunked = 1;
-			else if (hdr_match(line, "location")) {
-				copy_after_colon(line, location,
-						 sizeof(location));
-				have_loc = 1;
-			}
-		}
-
-		if (status >= 300 && status < 400 && have_loc) {
-			io_putstr("curl: redirect not followed: ");
-			io_putstr(location);
-			io_putchar('\n');
+		if (rv == 0)
+			break;              /* end of headers */
+		u_puts(line);
+		u_putc('\n');
+		if (hdr_match(line, "content-length"))
+			have_len = parse_dec_after_colon(line, &content_len);
+		else if (hdr_match(line, "transfer-encoding") &&
+			 contains_ci(line, "chunked"))
+			chunked = 1;
+		else if (hdr_match(line, "location")) {
+			copy_after_colon(line, location, sizeof(location));
+			have_loc = 1;
 		}
+	}
 
-		io_putchar('\n');
-		if (chunked)
-			body = dump_chunked(&r);
-		else
-			body = dump_body(&r, have_len, content_len);
-		io_putchar('\n');
-
-		if (r.error == -2)
-			io_putstr("curl: connection reset\n");
-		else if (r.error == -1)
-			io_putstr("curl: timeout while reading body\n");
-
-		io_putstr("curl: ");
-		put_dec(body);
-		io_putstr(" bytes received, status ");
-		put_dec(status);
-		io_putstr("\n");
-
-		tcp_close(&conn);
+	if (status >= 300 && status < 400 && have_loc) {
+		u_puts("curl: redirect not followed: ");
+		u_puts(location);
+		u_putc('\n');
 	}
+
+	u_putc('\n');
+	if (chunked)
+		body = dump_chunked(&r);
+	else
+		body = dump_body(&r, have_len, content_len);
+	u_putc('\n');
+
+	if (r.error == -2)
+		u_puts("curl: connection reset\n");
+	else if (r.error == -1)
+		u_puts("curl: timeout while reading body\n");
+
+	u_puts("curl: ");
+	u_putdec(body);
+	u_puts(" bytes received, status ");
+	u_putdec(status);
+	u_putc('\n');
+
+	u_syscall0(SYS_TCP_CLOSE);
 out:
-	netdev_release();
+	u_syscall0(SYS_NET_RELEASE);
+	u_exit(0);
 }