Skip to content

Commit 23142f3

Browse files
authored
Merge pull request #121 from ReturnInfinity/virtio-net-update
Virtio net update
2 parents 2878406 + df3202e commit 23142f3

File tree

2 files changed

+44
-69
lines changed

2 files changed

+44
-69
lines changed

src/drivers/net/virtio-net.asm

Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -261,24 +261,17 @@ virtio_net_init_reset_wait:
261261
xor eax, eax
262262
mov [rsi+VIRTIO_DEVICE_FEATURE_SELECT], eax
263263
mov eax, [rsi+VIRTIO_DEVICE_FEATURE]
264-
; btc eax, VIRTIO_NET_F_MQ ; Disable Multiqueue support for this driver
265-
; bts eax, VIRTIO_NET_F_MAC
266-
; bts eax, VIRTIO_NET_F_STATUS
267-
mov eax, 0x00010020 ; STATUS, MAC
268-
push rax
269264
xor eax, eax
270265
mov [rsi+VIRTIO_DRIVER_FEATURE_SELECT], eax
271-
pop rax
266+
mov eax, 0x00010020 ; Feature bits 31:0 - STATUS (16), MAC (5)
272267
mov [rsi+VIRTIO_DRIVER_FEATURE], eax
273268
; Process the next 32-bits of Feature bits
274269
mov eax, 1
275270
mov [rsi+VIRTIO_DEVICE_FEATURE_SELECT], eax
276271
mov eax, [rsi+VIRTIO_DEVICE_FEATURE]
277-
and eax, 1
278-
push rax
279272
mov eax, 1
280273
mov [rsi+VIRTIO_DRIVER_FEATURE_SELECT], eax
281-
pop rax
274+
mov eax, 1 ; Feature bits 63:32 - LEGACY (32)
282275
mov [rsi+VIRTIO_DRIVER_FEATURE], eax
283276

284277
; 3.1.1 - Step 5
@@ -349,57 +342,46 @@ virtio_net_init_reset_wait:
349342
mov ax, 1
350343
mov [rsi+VIRTIO_QUEUE_ENABLE], ax
351344

352-
; Populate the Next entries in the description rings
353-
; ; FIXME - Don't expect exactly 256 entries
354-
; mov eax, 1
355-
; mov rdi, [r8+nt_rx_desc]
356-
; add rdi, 14
357-
;virtio_net_init_pop_rx:
358-
; mov [rdi], al
359-
; add rdi, 16
360-
; add al, 1
361-
; cmp al, 0
362-
; jne virtio_net_init_pop_rx
363-
345+
; Populate TX Descriptor Table Entries
364346
mov eax, 1
365347
mov rdi, [r8+nt_tx_desc]
366348
add rdi, 14
367-
virtio_net_init_pop_tx:
349+
virtio_net_init_pop_tx_d:
368350
mov [rdi], al
369351
add rdi, 16
370352
add al, 1
371353
cmp al, 0
372-
jne virtio_net_init_pop_tx
354+
jne virtio_net_init_pop_tx_d
373355

374-
; Populate RX Descriptor Table
356+
; Populate RX Descriptor Table Entries
375357
xor ecx, ecx
376358
mov rdi, [r8+nt_rx_desc]
377-
virtio_net_init_pop_rx_1:
359+
virtio_net_init_pop_rx_d:
378360
mov rax, os_PacketBuffers ; 64-bit Address
379361
stosq
380-
mov eax, 1500 ; 32-bit Length
362+
mov eax, 1536 ; 32-bit Length
381363
stosd
382364
mov ax, VIRTQ_DESC_F_WRITE
383365
stosw ; 16-bit Flags
384366
inc cl
385367
mov ax, 0
386368
stosw ; 16-bit Next
387369
cmp cl, 0
388-
jne virtio_net_init_pop_rx_1
370+
jne virtio_net_init_pop_rx_d
389371

390-
; Populate RX avail
372+
; Populate RX Available Ring Entries
391373
mov rdi, [r8+nt_rx_desc]
392374
add rdi, 0x1000
393375
xor eax, eax
394376
stosw ; 16-bit flags
395-
mov ax, 1
377+
mov ax, 128
396378
stosw ; 16-bit index
397379
xor eax, eax
398-
virtio_net_init_pop_rx_2:
380+
virtio_net_init_pop_rx_a:
399381
stosw ; 16-bit ring
400382
inc al
401383
cmp al, 0
402-
jne virtio_net_init_pop_rx_2
384+
jne virtio_net_init_pop_rx_a
403385

404386
; Set nettxavailindex
405387
mov ax, 1
@@ -463,7 +445,7 @@ net_virtio_transmit:
463445

464446
; Create first entry in the Descriptor Table
465447
mov rdi, r8
466-
mov rax, netheader ; Address of the 12-byte netheader
448+
mov rax, virtio_net_hdr ; Address of the 12-byte virtio_net_hdr
467449
stosq ; 64-bit address
468450
mov eax, 12
469451
stosd ; 32-bit length
@@ -542,11 +524,9 @@ net_virtio_poll:
542524
; Get size of packet that was received
543525
mov ax, [rdx+0x78] ; Last known index
544526
shl eax, 3 ; Quick multiply by 8
545-
add eax, 8
527+
add eax, 4 ; Add offset to entries
546528
add rdi, rax ; RDI points to the Used Ring Entry
547-
mov ax, [rdi] ; Load the received packet size
548-
mov cx, ax ; Save the packet size to CX for later
549-
sub cx, 12 ; Subtract the virtio header
529+
mov rcx, [rdi] ; Load the 32-bit Index and 32-bit Length
550530

551531
; Populate RX Available Ring
552532
mov rdi, r8
@@ -556,23 +536,18 @@ net_virtio_poll:
556536
and ax, 0x00FF ; Wrap back to 0 if greater than 255
557537
mov [rdi], ax ; 16-bit Index
558538

559-
; Clear old Used Ring Entry
560-
mov rdi, r8
561-
add rdi, 0x2004 ; Start of Used Ring Entries
562-
mov ax, [rdx+0x78]
563-
shl rax, 3 ; Quick multiply by 8
564-
add rdi, rax
565-
xor eax, eax
566-
stosq
567-
568539
; Set RDI to address of packet
569540
xor eax, eax
570-
mov ax, [rdx+0x78] ; Gather last RX
571-
shl rax, 4 ; Quick multiply by 16
572-
mov rdi, r8
573-
add rdi, rax ; Add offset into Descriptor Table
574-
mov rdi, [rdi] ; Load address
575-
add rdi, 12 ; Skip past the header
541+
mov eax, ecx ; Lower 32 bits of RCX contains the Used Ring Entry Address
542+
shl rax, 4 ; Quick multiply by 16 as each Descriptor Table Entry is 16 bytes
543+
mov rdi, r8 ; Set RDI to the address of the Descriptor Table
544+
add rdi, rax ; Add entry offset into Descriptor Table
545+
mov rdi, [rdi] ; Load memory address
546+
add rdi, 12 ; Skip past the virtio-net header
547+
548+
; Set RCX to just the packet length
549+
shr rcx, 32 ; Shift upper 32 bits to the lower 32
550+
sub cx, 12 ; Subtract the virtio-net header
576551

577552
; Increment internal counters
578553
mov ax, [rdx+0x78] ; lastrx
@@ -596,10 +571,14 @@ virtio_net_isr_offset: dq 0
596571
virtio_net_device_offset: dq 0
597572

598573
align 16
599-
netheader:
600-
dd 0x00000000
601-
dd 0x00000000
602-
dd 0x00000000
574+
virtio_net_hdr:
575+
flags: db 0x00
576+
gso_type: db 0x00
577+
hdr_len: dw 0x0000
578+
gso_size: dw 0x0000
579+
csum_start: dw 0x0000
580+
csum_offset: dw 0x0000
581+
num_buffers: dw 0x0000
603582

604583
; VIRTIO_DEVICEFEATURES bits
605584
VIRTIO_NET_F_CSUM equ 0 ; Device handles packets with partial checksum
@@ -635,19 +614,6 @@ VIRTIO_NET_F_RSC_EXT equ 61 ; Device can process duplicated ACKs and report num
635614
VIRTIO_NET_F_STANDBY equ 62 ; Device may act as a standby for a primary device with the same MAC address.
636615
VIRTIO_NET_F_SPEED_DUPLEX equ 63 ; Device reports speed and duplex
637616

638-
; VIRTIO_STATUS
639-
VIRTIO_STATUS_FAILED equ 0x80 ; Indicates that something went wrong in the guest, and it has given up on the device
640-
VIRTIO_STATUS_DEVICE_NEEDS_RESET equ 0x40 ; Indicates that the device has experienced an error from which it can’t recover
641-
VIRTIO_STATUS_FEATURES_OK equ 0x08 ; Indicates that the driver has acknowledged all the features it understands, and feature negotiation is complete
642-
VIRTIO_STATUS_DRIVER_OK equ 0x04 ; Indicates that the driver is set up and ready to drive the device
643-
VIRTIO_STATUS_DRIVER equ 0x02 ; Indicates that the guest OS knows how to drive the device
644-
VIRTIO_STATUS_ACKNOWLEDGE equ 0x01 ; Indicates that the guest OS has found the device and recognized it as a valid virtio device.
645-
646-
; VIRTQUEUE Flags
647-
VIRTQ_DESC_F_NEXT equ 1
648-
VIRTQ_DESC_F_WRITE equ 2
649-
VIRTQ_DESC_F_INDIRECT equ 4
650-
651617
; VIRTQUEUES
652618
VIRTIO_NET_QUEUE_RX equ 0 ; The first of the Receive Queues
653619
VIRTIO_NET_QUEUE_TX equ 1 ; The first of the Transmit Queues

src/drivers/virtio.asm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,20 @@ VIRTIO_STATUS_DRIVER_OK equ 0x04 ; Indicates that the driver is set up and rea
6262
VIRTIO_STATUS_DRIVER equ 0x02 ; Indicates that the guest OS knows how to drive the device
6363
VIRTIO_STATUS_ACKNOWLEDGE equ 0x01 ; Indicates that the guest OS has found the device and recognized it as a valid virtio device.
6464

65-
; VIRTQUEUE Flags
65+
; VIRTQUEUE Descriptor Flags
6666
VIRTQ_DESC_F_NEXT equ 1
6767
VIRTQ_DESC_F_WRITE equ 2
6868
VIRTQ_DESC_F_INDIRECT equ 4
6969

70+
; VIRTQUEUE Used Flags
71+
VIRTQ_USED_F_NO_NOTIFY equ 1
7072

73+
; VIRTQUEUE Available Flags
74+
VIRTQ_AVAIL_F_NO_INTERRUPT equ 1
75+
76+
; VIRTIO Feature Flags
77+
VIRTIO_F_NOTIFY_ON_EMPTY equ 24
78+
VIRTIO_F_ANY_LAYOUT equ 27
7179
VIRTIO_F_INDIRECT_DESC equ 28
7280
VIRTIO_F_EVENT_IDX equ 29
7381
VIRTIO_F_VERSION_1 equ 32
@@ -79,6 +87,7 @@ VIRTIO_F_SR_IOV equ 37
7987
VIRTIO_F_NOTIFICATION_DATA equ 38
8088
VIRTIO_F_NOTIF_CONFIG_DATA equ 39
8189
VIRTIO_F_RING_RESET equ 40
90+
VIRTIO_F_ADMIN_VQ equ 41
8291

8392
VIRTIO_MSI_NO_VECTOR equ 0xFFFF
8493

0 commit comments

Comments
 (0)