From 7ee30af4460f4a00a89f073a5a11b78649b16118 Mon Sep 17 00:00:00 2001 From: "Sergey Semyonov (Serge)" Date: Fri, 16 Jun 2017 07:53:37 +0000 Subject: [PATCH] kernel: read/write operations on pipe git-svn-id: svn://kolibrios.org@6927 a494cfbc-eb01-0410-851d-a64ba20cac60 --- kernel/trunk/posix/pipe.inc | 79 ++++++++++++++++++++++++++++++------ kernel/trunk/posix/posix.inc | 33 +++++++++++++-- 2 files changed, 96 insertions(+), 16 deletions(-) diff --git a/kernel/trunk/posix/pipe.inc b/kernel/trunk/posix/pipe.inc index 097085ce7e..37f094d2df 100644 --- a/kernel/trunk/posix/pipe.inc +++ b/kernel/trunk/posix/pipe.inc @@ -140,6 +140,8 @@ purge .intpipe align 4 pipe_read: mov edi, edx + +.again: lea ecx, [ebp+PIPE.pipe_lock] call mutex_lock @@ -161,16 +163,15 @@ pipe_read: mov [esp+SYSCALL_STACK._eax], ecx sub [ebp+PIPE.count], ecx cld - rep stosb + rep movsb and esi, 0xFFF - add [ebp+PIPE.read_end], esi + mov [ebp+PIPE.read_end], esi lea ecx, [ebp+PIPE.pipe_lock] call mutex_unlock ret .wait: - sub esp, sizeof.MUTEX_WAITER mov ebx, [TASK_BASE] mov [esp+MUTEX_WAITER.task], ebx @@ -178,23 +179,15 @@ pipe_read: list_add_tail esp, edx ;esp= new waiter, edx= list head -.again: lea ecx, [ebp+PIPE.pipe_lock] call mutex_unlock mov [ebx+TASKDATA.state], 1 call change_task - lea ecx, [ebp+PIPE.pipe_lock] - call mutex_lock - - mov ecx, [ebp+PIPE.count] - test ecx, ecx - jz .again - list_del esp add esp, sizeof.MUTEX_WAITER - jmp .check_count + jmp .again .eof: mov [esp+SYSCALL_STACK._eax], eax @@ -202,9 +195,69 @@ pipe_read: call mutex_unlock ret +; edx buf +; esi count +; ebp pipe + +align 4 +pipe_write: + mov edi, edx + +.again: + lea ecx, [ebp+PIPE.pipe_lock] + call mutex_lock + + xor eax, eax + cmp eax, [ebp+PIPE.readers] + je .epipe + + mov ecx, [ebp+PIPE.count] + sub ecx, 4096 + jz .wait + +.check_count: + cmp ecx, esi + jb .write + mov ecx, esi +.write: + mov esi, [ebp+PIPE.buffer] + add esi, [ebp+PIPE.write_end] + mov [esp+SYSCALL_STACK._eax], ecx + add [ebp+PIPE.count], ecx + cld + rep movsb + and esi, 0xFFF + mov [ebp+PIPE.write_end], esi + + lea ecx, [ebp+PIPE.pipe_lock] + call mutex_unlock + ret + +.wait: + sub esp, sizeof.MUTEX_WAITER + mov ebx, [TASK_BASE] + mov [esp+MUTEX_WAITER.task], ebx + lea edx, [ebp+PIPE.wlist] + + list_add_tail esp, edx ;esp= new waiter, edx= list head + + lea ecx, [ebp+PIPE.pipe_lock] + call mutex_unlock + + mov [ebx+TASKDATA.state], 1 + call change_task + + list_del esp + add esp, sizeof.MUTEX_WAITER + jmp .again + +.epipe: + mov [esp+SYSCALL_STACK._eax], -EPIPE + lea ecx, [ebp+PIPE.pipe_lock] + call mutex_unlock + ret align 4 pipe_close: -pipe_write: mov [esp+SYSCALL_STACK._eax], -EBADF ret diff --git a/kernel/trunk/posix/posix.inc b/kernel/trunk/posix/posix.inc index 05d905b1f7..0dcd835cc0 100644 --- a/kernel/trunk/posix/posix.inc +++ b/kernel/trunk/posix/posix.inc @@ -13,7 +13,7 @@ EFAULT equ 14 ;EINVAL equ 22 11 defined in stack.inc ENFILE equ 23 EMFILE equ 24 - +EPIPE equ 32 FILEOP_CLOSE equ 0 FILEOP_READ equ 1 @@ -49,8 +49,8 @@ sys_posix_call: dd sys_futex ;7 reserved dd sys_posix.fail ;8 sys_open dd sys_posix.fail ;9 sys_close - dd sys_posix.fail ;10 sys_read - dd sys_posix.fail ;11 sys_write + dd sys_read ;10 read() + dd sys_write ;11 write() dd sys_posix.fail ;12 sys_dup3 dd sys_pipe2 ;13 .end: @@ -95,3 +95,30 @@ sys_read: mov [esp+SYSCALL_STACK._eax], -EBADF ret +;ssize_t write(int fd, const void *buf, size_t count); +; ecx fd +; edx buf +; esi count + +align 4 +sys_write: + cmp ecx, STDERR_FILENO + jbe .fail + cmp ecx, (PROC.pdt_0 - PROC.htab)/4 + jae .fail + + mov edi, [current_process] + mov ebp, [edi+PROC.htab+ecx*4] + + cmp [ebp+FILED.magic], 'PIPE' + jne .fail + cmp [ebp+FILED.handle], ecx + jne .fail + + mov ebp, [ebp+FILED.file] + mov eax, [ebp] + jmp dword [eax+FILEOP_WRITE*4] +.fail: + mov [esp+SYSCALL_STACK._eax], -EBADF + ret +