2017-06-16 00:36:39 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2022-02-12 18:27:41 +01:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2022. All rights reserved. ;;
|
2017-06-16 00:36:39 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License. ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-11-03 17:03:39 +01:00
|
|
|
$Revision$
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2017-12-20 02:07:10 +01:00
|
|
|
F_READ = 0x0001 ; file opened for reading
|
|
|
|
F_WRITE = 0x0002 ; file opened for writing
|
2017-06-17 00:47:00 +02:00
|
|
|
|
2017-12-20 02:07:10 +01:00
|
|
|
O_CLOEXEC = 0x40000
|
|
|
|
PIPE_BUFFER_SIZE = 4096
|
2017-06-16 00:36:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
iglobal
|
|
|
|
align 4
|
|
|
|
pipe_file_ops:
|
|
|
|
dd pipe_close ;0
|
|
|
|
dd pipe_read ;1
|
|
|
|
dd pipe_write ;2
|
|
|
|
endg
|
|
|
|
|
|
|
|
;int pipe2(int pipefd[2], int flags);
|
|
|
|
;ecx pipefd
|
|
|
|
;edx flags
|
|
|
|
|
|
|
|
align 4
|
|
|
|
sys_pipe2:
|
|
|
|
.pipeflags equ esp+16
|
|
|
|
.pipefd equ esp+12
|
|
|
|
.fdread equ esp+8
|
|
|
|
.fdwrite equ esp+4
|
|
|
|
.intpipe equ esp
|
|
|
|
|
|
|
|
push ebp
|
|
|
|
test ecx, ecx
|
|
|
|
mov ebp, -EFAULT
|
|
|
|
js .fail
|
|
|
|
|
|
|
|
test edx, not O_CLOEXEC
|
|
|
|
mov ebp, -EINVAL
|
|
|
|
jnz .fail
|
|
|
|
|
|
|
|
push edx
|
2017-06-16 13:19:43 +02:00
|
|
|
push ecx
|
2017-06-16 00:36:39 +02:00
|
|
|
sub esp, (5-2)*4
|
|
|
|
|
|
|
|
mov ecx, sizeof.FILED
|
|
|
|
call create_object
|
|
|
|
mov [.fdread], eax
|
|
|
|
test eax, eax
|
|
|
|
mov ebp, -EMFILE
|
|
|
|
jz .err_0
|
|
|
|
|
|
|
|
mov ecx, sizeof.FILED
|
|
|
|
call create_object
|
|
|
|
mov [.fdwrite], eax
|
|
|
|
test eax, eax
|
|
|
|
jz .err_1
|
|
|
|
|
|
|
|
mov eax, sizeof.PIPE
|
|
|
|
call malloc
|
|
|
|
test eax, eax
|
|
|
|
mov ebp, -ENFILE
|
|
|
|
jz .err_2
|
|
|
|
|
|
|
|
mov ebp, eax
|
|
|
|
|
|
|
|
stdcall create_ring_buffer, PIPE_BUFFER_SIZE, PG_SWR
|
|
|
|
test eax, eax
|
|
|
|
jz .err_3
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [ebp + PIPE.pipe_ops], pipe_file_ops
|
|
|
|
mov [ebp + PIPE.buffer], eax
|
2017-06-16 00:36:39 +02:00
|
|
|
|
|
|
|
xor eax, eax
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [ebp + PIPE.count], eax
|
|
|
|
mov [ebp + PIPE.read_end], eax
|
|
|
|
mov [ebp + PIPE.write_end], eax
|
2017-06-16 00:36:39 +02:00
|
|
|
|
|
|
|
inc eax
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [ebp + PIPE.readers], eax
|
|
|
|
mov [ebp + PIPE.writers], eax
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.pipe_lock]
|
2017-06-16 00:36:39 +02:00
|
|
|
call mutex_init
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.rlist]
|
2017-06-16 00:36:39 +02:00
|
|
|
list_init ecx
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.wlist]
|
2017-06-16 00:36:39 +02:00
|
|
|
list_init ecx
|
|
|
|
|
|
|
|
mov eax, [.fdread]
|
|
|
|
mov edx, [.fdwrite]
|
|
|
|
mov ecx, [.pipefd]
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [eax + FILED.magic], 'PIPE'
|
|
|
|
mov [eax + FILED.destroy], 0
|
|
|
|
mov [eax + FILED.mode], F_READ
|
|
|
|
mov [eax + FILED.file], ebp
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [edx + FILED.magic], 'PIPE'
|
|
|
|
mov [edx + FILED.destroy], 0
|
|
|
|
mov [edx + FILED.mode], F_WRITE
|
|
|
|
mov [edx + FILED.file], ebp
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [eax + FILED.handle]
|
|
|
|
mov edx, [edx + FILED.handle]
|
2017-06-16 13:19:43 +02:00
|
|
|
|
2017-06-16 00:36:39 +02:00
|
|
|
mov [ecx], eax
|
|
|
|
mov [ecx+4], edx
|
|
|
|
add esp, 5*4
|
|
|
|
pop ebp
|
|
|
|
xor eax, eax
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [esp + SYSCALL_STACK._eax], eax
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
.err_3:
|
|
|
|
mov eax, ebp
|
|
|
|
call free
|
|
|
|
mov ebp, -ENFILE
|
|
|
|
.err_2:
|
|
|
|
mov ecx, [.fdwrite]
|
|
|
|
call destroy_object
|
|
|
|
.err_1:
|
|
|
|
mov ecx, [.fdread]
|
|
|
|
call destroy_object
|
|
|
|
.err_0:
|
|
|
|
add esp, 5*4
|
|
|
|
.fail:
|
2017-06-17 00:47:00 +02:00
|
|
|
mov eax, ebp
|
2017-06-16 00:36:39 +02:00
|
|
|
pop ebp
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [esp + SYSCALL_STACK._eax], eax
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
purge .pipeflags
|
|
|
|
purge .filefd
|
|
|
|
purge .fdread
|
|
|
|
purge .fdwrite
|
|
|
|
purge .intpipe
|
|
|
|
|
|
|
|
|
2017-06-17 00:47:00 +02:00
|
|
|
; edx dst_buf
|
|
|
|
; esi read count
|
2017-06-16 00:36:39 +02:00
|
|
|
; ebp pipe
|
|
|
|
|
|
|
|
align 4
|
|
|
|
pipe_read:
|
2017-06-17 00:47:00 +02:00
|
|
|
|
2017-06-16 00:36:39 +02:00
|
|
|
mov edi, edx
|
2017-06-16 09:53:37 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.pipe_lock]
|
2017-06-16 00:36:39 +02:00
|
|
|
call mutex_lock
|
2017-06-17 00:47:00 +02:00
|
|
|
.again:
|
2017-06-16 00:36:39 +02:00
|
|
|
xor eax, eax
|
2022-02-12 18:27:41 +01:00
|
|
|
cmp eax, [ebp + PIPE.writers]
|
2017-06-16 00:36:39 +02:00
|
|
|
je .eof
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ecx, [ebp + PIPE.count]
|
2017-06-16 00:36:39 +02:00
|
|
|
test ecx, ecx
|
|
|
|
jz .wait
|
|
|
|
|
|
|
|
.check_count:
|
|
|
|
cmp ecx, esi
|
|
|
|
jb .read
|
|
|
|
mov ecx, esi
|
|
|
|
.read:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov esi, [ebp + PIPE.buffer]
|
|
|
|
add esi, [ebp + PIPE.read_end]
|
|
|
|
mov [esp + SYSCALL_STACK._eax], ecx
|
|
|
|
sub [ebp + PIPE.count], ecx
|
2017-06-16 00:36:39 +02:00
|
|
|
cld
|
2017-06-16 09:53:37 +02:00
|
|
|
rep movsb
|
2017-06-16 00:36:39 +02:00
|
|
|
and esi, 0xFFF
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [ebp + PIPE.read_end], esi
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.wlist]
|
|
|
|
cmp ecx, [ebp + PIPE.wlist.next]
|
2017-06-17 00:47:00 +02:00
|
|
|
je @F
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ecx, [ecx + MUTEX_WAITER.task]
|
2022-02-09 18:07:51 +01:00
|
|
|
mov [ecx + APPDATA.state], TSTATE_RUNNING ;activate writer task
|
2017-06-17 00:47:00 +02:00
|
|
|
@@:
|
2022-02-12 18:27:41 +01:00
|
|
|
cmp [ebp + PIPE.count], 0
|
2017-06-17 00:47:00 +02:00
|
|
|
je @F
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea eax, [ebp + PIPE.rlist]
|
|
|
|
cmp eax, [ebp + PIPE.rlist.next]
|
2017-06-17 00:47:00 +02:00
|
|
|
je @F
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [eax + MUTEX_WAITER.task]
|
2022-02-09 18:07:51 +01:00
|
|
|
mov [eax + APPDATA.state], TSTATE_RUNNING ;activate reader task
|
2017-06-17 00:47:00 +02:00
|
|
|
@@:
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.pipe_lock]
|
2017-06-16 00:36:39 +02:00
|
|
|
call mutex_unlock
|
|
|
|
ret
|
|
|
|
|
|
|
|
.wait:
|
2017-06-17 00:47:00 +02:00
|
|
|
pushfd
|
|
|
|
cli
|
|
|
|
|
2017-06-16 00:36:39 +02:00
|
|
|
sub esp, sizeof.MUTEX_WAITER
|
2022-02-09 18:07:51 +01:00
|
|
|
mov ebx, [current_slot]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [esp + MUTEX_WAITER.task], ebx
|
|
|
|
lea edx, [ebp + PIPE.rlist]
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2017-06-17 00:47:00 +02:00
|
|
|
list_add_tail esp, edx ;esp= new waiter, edx= list head
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.pipe_lock]
|
2017-06-16 00:36:39 +02:00
|
|
|
call mutex_unlock
|
|
|
|
|
2022-02-09 18:07:51 +01:00
|
|
|
mov [ebx + APPDATA.state], TSTATE_RUN_SUSPENDED
|
2017-06-16 00:36:39 +02:00
|
|
|
call change_task
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.pipe_lock]
|
2017-06-17 00:47:00 +02:00
|
|
|
call mutex_lock
|
|
|
|
|
2017-06-16 09:53:37 +02:00
|
|
|
list_del esp
|
|
|
|
add esp, sizeof.MUTEX_WAITER
|
2017-06-17 00:47:00 +02:00
|
|
|
popfd
|
2017-06-16 09:53:37 +02:00
|
|
|
jmp .again
|
|
|
|
|
|
|
|
.eof:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [esp + SYSCALL_STACK._eax], eax
|
|
|
|
lea ecx, [ebp + PIPE.pipe_lock]
|
2017-06-16 09:53:37 +02:00
|
|
|
call mutex_unlock
|
|
|
|
ret
|
|
|
|
|
2017-06-17 00:47:00 +02:00
|
|
|
; edx src_buf
|
|
|
|
; esi write count
|
2017-06-16 09:53:37 +02:00
|
|
|
; ebp pipe
|
|
|
|
|
|
|
|
align 4
|
|
|
|
pipe_write:
|
|
|
|
|
2017-06-17 00:47:00 +02:00
|
|
|
.written equ esp
|
|
|
|
|
|
|
|
push 0 ;written
|
|
|
|
mov ebx, esi ;ebx = write count
|
|
|
|
mov esi, edx ;esi = src
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.pipe_lock]
|
2017-06-16 00:36:39 +02:00
|
|
|
call mutex_lock
|
2017-06-17 00:47:00 +02:00
|
|
|
.again:
|
2017-06-16 09:53:37 +02:00
|
|
|
xor eax, eax
|
2022-02-12 18:27:41 +01:00
|
|
|
cmp eax, [ebp + PIPE.readers]
|
2017-06-16 09:53:37 +02:00
|
|
|
je .epipe
|
|
|
|
|
2017-06-17 00:47:00 +02:00
|
|
|
mov ecx, 4096
|
2022-02-12 18:27:41 +01:00
|
|
|
sub ecx, [ebp + PIPE.count]
|
2017-06-17 00:47:00 +02:00
|
|
|
jz .wait ;wait if buffer full
|
2017-06-16 09:53:37 +02:00
|
|
|
|
|
|
|
.check_count:
|
2017-06-17 00:47:00 +02:00
|
|
|
cmp ecx, ebx
|
2017-06-16 09:53:37 +02:00
|
|
|
jb .write
|
2017-06-17 00:47:00 +02:00
|
|
|
mov ecx, ebx
|
2017-06-16 09:53:37 +02:00
|
|
|
.write:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov edi, [ebp + PIPE.buffer]
|
|
|
|
add edi, [ebp + PIPE.write_end]
|
2017-06-17 00:47:00 +02:00
|
|
|
add [.written], ecx
|
|
|
|
sub ebx, ecx
|
2022-02-12 18:27:41 +01:00
|
|
|
add [ebp + PIPE.count], ecx
|
2017-06-17 00:47:00 +02:00
|
|
|
|
2017-06-16 09:53:37 +02:00
|
|
|
cld
|
|
|
|
rep movsb
|
2017-06-16 13:19:43 +02:00
|
|
|
and edi, 0xFFF
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [ebp + PIPE.write_end], edi
|
2017-06-16 09:53:37 +02:00
|
|
|
|
2017-06-17 00:47:00 +02:00
|
|
|
pushfd
|
|
|
|
cli
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea eax, [ebp + PIPE.rlist]
|
|
|
|
cmp eax, [ebp + PIPE.rlist.next]
|
2017-06-17 00:47:00 +02:00
|
|
|
je @F
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [eax + MUTEX_WAITER.task]
|
2022-02-09 18:07:51 +01:00
|
|
|
mov [eax + APPDATA.state], TSTATE_RUNNING ;activate reader task
|
2017-06-17 00:47:00 +02:00
|
|
|
@@:
|
2022-02-12 18:27:41 +01:00
|
|
|
cmp [ebp + PIPE.count], 4096
|
2017-06-17 00:47:00 +02:00
|
|
|
je @F
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.wlist]
|
|
|
|
cmp ecx, [ebp + PIPE.wlist.next]
|
2017-06-17 00:47:00 +02:00
|
|
|
je @F
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ecx, [eax + MUTEX_WAITER.task]
|
2022-02-09 18:07:51 +01:00
|
|
|
mov [ecx + APPDATA.state], TSTATE_RUNNING ;activate writer task
|
2017-06-17 00:47:00 +02:00
|
|
|
@@:
|
|
|
|
popfd
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.pipe_lock]
|
2017-06-16 09:53:37 +02:00
|
|
|
call mutex_unlock
|
2017-06-17 00:47:00 +02:00
|
|
|
|
|
|
|
test ebx, ebx
|
|
|
|
jnz .again
|
|
|
|
|
|
|
|
pop eax ; written
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [esp + SYSCALL_STACK._eax], eax
|
2017-06-16 09:53:37 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.wait:
|
2017-06-17 00:47:00 +02:00
|
|
|
pushfd
|
|
|
|
cli
|
|
|
|
|
2017-06-16 09:53:37 +02:00
|
|
|
sub esp, sizeof.MUTEX_WAITER
|
2022-02-09 18:07:51 +01:00
|
|
|
mov ecx, [current_slot]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [esp + MUTEX_WAITER.task], ecx
|
2017-06-16 09:53:37 +02:00
|
|
|
lea edx, [ebp+PIPE.wlist]
|
|
|
|
|
2017-06-17 00:47:00 +02:00
|
|
|
list_add_tail esp, edx ;esp= new waiter, edx= list head
|
2017-06-16 09:53:37 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.pipe_lock]
|
2017-06-16 09:53:37 +02:00
|
|
|
call mutex_unlock
|
|
|
|
|
2022-02-09 18:07:51 +01:00
|
|
|
mov [ecx + APPDATA.state], TSTATE_RUN_SUSPENDED
|
2017-06-16 09:53:37 +02:00
|
|
|
call change_task
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.pipe_lock]
|
2017-06-17 00:47:00 +02:00
|
|
|
call mutex_lock
|
|
|
|
|
2017-06-16 00:36:39 +02:00
|
|
|
list_del esp
|
|
|
|
add esp, sizeof.MUTEX_WAITER
|
2017-06-17 00:47:00 +02:00
|
|
|
popfd
|
2017-06-16 09:53:37 +02:00
|
|
|
jmp .again
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2017-06-16 09:53:37 +02:00
|
|
|
.epipe:
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ecx, [ebp + PIPE.pipe_lock]
|
2017-06-16 00:36:39 +02:00
|
|
|
call mutex_unlock
|
2017-06-17 00:47:00 +02:00
|
|
|
|
|
|
|
add esp, 4
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [esp + SYSCALL_STACK._eax], -EPIPE
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
pipe_close:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [esp + SYSCALL_STACK._eax], -EBADF
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|