shithub: mc

Download patch

ref: 69eda20a8c3c5f04f71c22242864e87f3b34e69b
parent: e432245ed33827b380d4ff9314135783c86fff64
author: Ori Bernstein <[email protected]>
date: Thu Aug 14 09:21:23 EDT 2014

Add support for pipe() syscall.

    Another funky OSX call. Oy, OSX.

--- a/libstd/sys-linux.myr
+++ b/libstd/sys-linux.myr
@@ -5,7 +5,7 @@
 	type pid	= int64
 	type scno	= int64	/* syscall */
 	type fdopt	= int64	/* fd options */
-	type fd		= int64	/* fd */
+	type fd		= int32	/* fd */
 	type mprot	= int64	/* memory protection */
 	type mopt	= int64	/* memory mapping options */
 	type socktype	= int64	/* socket type */
--- a/libstd/sys-osx.myr
+++ b/libstd/sys-osx.myr
@@ -4,7 +4,7 @@
 pkg std =
 	type scno 	= int64	/* syscall */
 	type fdopt	= int64	/* fd options */
-	type fd		= int64	/* fd */
+	type fd		= int32	/* fd */
 	type mprot	= int64	/* memory protection */
 	type mopt	= int64	/* memory mapping options */
 	type socktype	= int64	/* socket type */
@@ -521,7 +521,7 @@
 	const execv	: (cmd : byte[:], args : byte[:][:] -> int64)
 	const execve	: (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
 
-	/* fd manipulation */
+	/* file manipulation */
 	const open	: (path:byte[:], opts:fdopt -> fd)
 	const openmode	: (path:byte[:], opts:fdopt, mode:int64 -> fd)
 	const close	: (fd:fd -> int64)
@@ -536,6 +536,9 @@
 	const ioctl	: (fd:fd, req : int64, args:... -> int64)
 	const getdirentries64	: (fd : fd, buf : byte[:], basep : uint64# -> int64)
 
+	/* fd stuff */
+	const pipe	: (fd : fd[:] -> int64)
+
 	/* networking */
 	const socket	: (dom : sockfam, stype : socktype, proto : sockproto	-> fd)
 	const connect	: (sock	: fd, addr : sockaddr#, len : size -> int)
@@ -543,6 +546,7 @@
 	const listen	: (sock : fd, backlog : int	-> int)
 	const bind	: (sock : fd, addr : sockaddr#, len : size -> int)
 
+
 	/* memory mapping */
 	const munmap	: (addr:byte#, len:size -> int64)
 	const mmap	: (addr:byte#, len:size, prot:mprot, flags:mopt, fd:fd, off:off -> byte#)
@@ -561,6 +565,7 @@
 ;;
 
 extern const __osx_fork : (->int64)
+extern const __osx_pipe : (fd : fd# ->int64)
 extern const cstring : (str : byte[:] -> byte#)
 extern const alloca : (sz : size -> byte#)
 extern const __cenvp : byte##
@@ -637,6 +642,8 @@
 	-> syscall(Sysioctl, fd, req, arg) castto(int64)
 }
 const getdirentries64	= {fd, buf, basep;	-> syscall(Sysgetdirentries64, fd, buf castto(byte#), buf.len castto(size), basep)}
+
+const pipe	= {fd;	-> __osx_pipe(fd castto(fd#))}
 
 /* networking */
 const socket	= {dom, stype, proto;	-> syscall(Syssocket, dom castto(int64), stype, proto) castto(fd) }
--- a/libstd/syscall-osx.s
+++ b/libstd/syscall-osx.s
@@ -41,7 +41,7 @@
 	ret
 
 /*
- * OSX is dumb about fork, and needs an assembly wrapper.
+ * OSX is strange about fork, and needs an assembly wrapper.
  * The fork() syscall, when called directly, returns the pid in both
  * processes, which means that both parent and child think they're
  * the parent.
@@ -71,6 +71,46 @@
 	jz isparent
 	xorq %rax,%rax
 isparent:
+
+	popq %r11 
+	popq %rcx 
+	popq %r9
+	popq %r8
+	popq %r10 
+	popq %rdx 
+	popq %rsi 
+	popq %rdi 
+	popq %rbp
+	ret
+
+/*
+ * OSX is strange about pipe, and needs an assembly wrapper.
+ * The pipe() syscall returns the pipes created in eax:edx, and
+ * needs to copy them to the destination locations manually.
+ */
+.globl _std$__osx_pipe
+_std$__osx_pipe:
+	pushq %rbp
+	pushq %rdi 
+	pushq %rsi 
+	pushq %rdx 
+	pushq %r10 
+	pushq %r8
+	pushq %r9
+	pushq %rcx 
+	pushq %r11 
+
+	movq $0x200002a,%rax
+	syscall
+
+	jae pipesuccess
+	negq %rax
+
+pipesuccess:
+	movq 88 (%rsp),%rdi
+	movl %eax,(%rdi)
+	movl %edx,4(%rdi)
+	xorq %rax,%rax
 
 	popq %r11 
 	popq %rcx