kernel: read/write operations on pipe

git-svn-id: svn://kolibrios.org@6927 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Sergey Semyonov (Serge) 2017-06-16 07:53:37 +00:00
parent af61cc97e8
commit 7ee30af446
2 changed files with 96 additions and 16 deletions

View File

@ -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

View File

@ -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