Skip to content

Commit 7640bec

Browse files
LemonBoyandrewrk
authored andcommitted
Fix pipe syscall for MIPS
1 parent 7481a4a commit 7640bec

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

lib/std/os/linux.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32, flags: u32) usize {
328328
}
329329

330330
pub fn pipe(fd: *[2]i32) usize {
331-
if (@hasDecl(@This(), "SYS_pipe")) {
331+
if (builtin.arch == .mipsel) {
332+
return syscall_pipe(fd);
333+
} else if (@hasDecl(@This(), "SYS_pipe")) {
332334
return syscall1(SYS_pipe, @ptrToInt(fd));
333335
} else {
334336
return syscall2(SYS_pipe2, @ptrToInt(fd), 0);

lib/std/os/linux/mipsel.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ pub fn syscall0(number: usize) usize {
1212
);
1313
}
1414

15+
pub fn syscall_pipe(fd: *[2]i32) usize {
16+
return asm volatile (
17+
\\ .set noat
18+
\\ .set noreorder
19+
\\ syscall
20+
\\ blez $7, 1f
21+
\\ nop
22+
\\ b 2f
23+
\\ subu $2, $0, $2
24+
\\ 1:
25+
\\ sw $2, 0($4)
26+
\\ sw $3, 4($4)
27+
\\ 2:
28+
: [ret] "={$2}" (-> usize)
29+
: [number] "{$2}" (usize(SYS_pipe))
30+
: "memory", "cc", "$7"
31+
);
32+
}
33+
1534
pub fn syscall1(number: usize, arg1: usize) usize {
1635
return asm volatile (
1736
\\ syscall

lib/std/os/test.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,16 @@ test "gethostname" {
219219
const hostname = try os.gethostname(&buf);
220220
expect(hostname.len != 0);
221221
}
222+
223+
test "pipe" {
224+
if (os.windows.is_the_target)
225+
return error.SkipZigTest;
226+
227+
var fds = try os.pipe();
228+
try os.write(fds[1], "hello");
229+
var buf: [16]u8 = undefined;
230+
expect((try os.read(fds[0], buf[0..])) == 5);
231+
testing.expectEqualSlices(u8, buf[0..5], "hello");
232+
os.close(fds[1]);
233+
os.close(fds[0]);
234+
}

0 commit comments

Comments
 (0)