Compare commits

..
Author SHA1 Message Date
Leency 36854f1382 kernel/net: allocate socket rings from non-contiguous pages
Check kernel codestyle / Check kernel codestyle (pull_request) Successful in 34s
Test PR / Build (en_US) (pull_request) Successful in 2m38s
Test PR / Build (es_ES) (pull_request) Successful in 2m30s
Test PR / Build (ru_RU) (pull_request) Successful in 2m26s
2026-09-30 13:49:15 +03:00
Leency f990a48b98 kernel/net: check that STREAM_SOCKET fits in SOCKET_STRUCT_SIZE 2026-09-30 13:49:14 +03:00
Leency fb2906bc1d kernel: release address space when kernel_alloc fails 2026-09-30 13:49:13 +03:00
Leency 9c58dc1f04 kernel: flush the mirror half in create_ring_buffer 2026-09-30 13:49:11 +03:00
5 changed files with 80 additions and 28 deletions

No files matched your search

+8 -1
View File
@@ -463,7 +463,7 @@ proc kernel_alloc stdcall, size:dword
and eax, -PAGE_SIZE;
mov [size], eax
and eax, eax
jz .err
jz .err0
mov ebx, eax
shr ebx, 12
mov [pages_count], ebx
@@ -509,6 +509,13 @@ proc kernel_alloc stdcall, size:dword
pop ebx
ret
.err:
mov eax, [lin_addr]
test eax, eax
jz .err0
mov ecx, [pages_count]
call release_pages
stdcall free_kernel_space, [lin_addr]
.err0:
xor eax, eax
pop edi
pop ebx
+59 -1
View File
@@ -1397,7 +1397,9 @@ proc create_ring_buffer stdcall, size:dword, flags:dword
mov [page_tabs + edi], eax
mov [page_tabs + edi + edx], eax
invlpg [ebx]
invlpg [ebx+0x10000]
add ebx, [size]
invlpg [ebx] ; the mirror half
sub ebx, [size]
add eax, 0x1000
add ebx, 0x1000
add edi, 4
@@ -1416,6 +1418,62 @@ proc create_ring_buffer stdcall, size:dword, flags:dword
ret
endp
; Same as create_ring_buffer, but from non-contiguous pages.
; Not for DMA. Free with kernel_free.
align 4
proc create_ring_buffer_sparse stdcall, size:dword, flags:dword
locals
buf_ptr dd ?
endl
mov eax, [size]
test eax, eax
jz .fail
add eax, eax
stdcall alloc_kernel_space, eax
test eax, eax
jz .fail
mov [buf_ptr], eax
push ebx esi edi
mov ebx, eax ; page being mapped
mov esi, [size] ; distance to its mirror
mov edi, esi
shr edi, 12 ; pages left
.page:
call alloc_page
test eax, eax
jz .mm_fail
or eax, [flags]
mov edx, ebx
shr edx, 12
mov [page_tabs + edx*4], eax
invlpg [ebx]
lea edx, [ebx + esi]
shr edx, 12
mov [page_tabs + edx*4], eax
invlpg [ebx + esi]
add ebx, PAGE_SIZE
dec edi
jnz .page
mov eax, [buf_ptr]
pop edi esi ebx
ret
.mm_fail:
mov eax, [buf_ptr]
mov ecx, [size]
shr ecx, 11 ; both halves, in pages
call release_pages
stdcall free_kernel_space, [buf_ptr]
pop edi esi ebx
xor eax, eax
.fail:
ret
endp
align 4
proc print_mem
+5 -3
View File
@@ -198,6 +198,10 @@ ends
SOCKET_STRUCT_SIZE = 4096 ; in bytes
if sizeof.STREAM_SOCKET > SOCKET_STRUCT_SIZE
err "STREAM_SOCKET does not fit in SOCKET_STRUCT_SIZE"
end if
SOCKET_QUEUE_SIZE = 10 ; maximum number of incoming packets queued for 1 socket
; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start
SOCKET_QUEUE_LOCATION = (SOCKET_STRUCT_SIZE - SOCKET_QUEUE_SIZE*sizeof.socket_queue_entry - sizeof.queue)
@@ -780,7 +784,6 @@ socket_close:
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
jne .free
or [eax + SOCKET.state], SS_NOFDREF ; the application is gone
test [eax + SOCKET.state], SS_ISDISCONNECTING
jnz @f
call tcp_disconnect
@@ -1648,7 +1651,7 @@ socket_ring_create:
mov esi, eax
push edx
stdcall create_ring_buffer, SOCKET_BUFFER_SIZE, PG_SWR
stdcall create_ring_buffer_sparse, SOCKET_BUFFER_SIZE, PG_SWR
pop edx
test eax, eax
jz .fail
@@ -2407,7 +2410,6 @@ socket_process_end:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: killing socket %x\n", ebx
mov [ebx + SOCKET.PID], 0
or [ebx + SOCKET.state], SS_NOFDREF
mov eax, ebx
mov ebx, [ebx + SOCKET.NextPtr]
+2 -13
View File
@@ -722,28 +722,17 @@ endl
.no_duplicate:
;--------------------------------------------------
; Handle data that arrives after the application closed the socket
; Handle data that arrives after process terminates
test [ebx + SOCKET.state], SS_NOFDREF
jnz .closed_by_app
cmp [ebx + SOCKET.PID], 0
cmp [ebx + SOCKET.PID], 0 ;;; TODO: use socket flags instead??
jne .not_terminated
.closed_by_app:
cmp [ebx + TCP_SOCKET.t_state], TCPS_CLOSE_WAIT
jbe .not_terminated
test ecx, ecx
jz .not_terminated
; Unlock the socket, tcp_close frees it
pusha
lea ecx, [ebx + SOCKET.mutex]
call mutex_unlock
popa
mov eax, ebx
push edx
call tcp_close
pop edx
inc [TCPS_rcvafterclose]
jmp .respond_seg_reset
.not_terminated:
+6 -10
View File
@@ -192,14 +192,14 @@ tcp_disconnect:
; je TCP_drop
.nolinger:
; Unread data: reset the connection instead of sending FIN (RFC 2525, 2.17)
cmp [eax + STREAM_SOCKET.rcv.size], 0
jne .reset
call socket_is_disconnecting
push eax
add eax, STREAM_SOCKET.rcv
mov ecx, [eax + RING_BUFFER.size]
call socket_ring_free
pop eax
call tcp_usrclosed
test eax, eax
@@ -210,10 +210,6 @@ tcp_disconnect:
@@:
ret
.reset:
mov ebx, ECONNRESET
jmp tcp_drop
;-----------------------------------------------------------------;
; ;