Skip to content

Commit de22ce6

Browse files
author
Ian Seyler
committed
Network cleanup
- Start on virtio-net rework - Disable r8169 for now - Enable per interface send lock - Remove obsolete system variables
1 parent c8e8fca commit de22ce6

File tree

6 files changed

+124
-53
lines changed

6 files changed

+124
-53
lines changed

src/drivers.asm

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
%include "drivers/net/i8254x.asm"
3030
%include "drivers/net/i8257x.asm"
3131
%include "drivers/net/i8259x.asm"
32-
%include "drivers/net/r8169.asm"
32+
; %include "drivers/net/r8169.asm"
3333
%include "drivers/net/virtio-net.asm"
3434

3535
; Video
@@ -129,13 +129,13 @@ dw 0x1572 ; X710 (SFP+)
129129
dw 0x0000
130130

131131
; Realtek 816x/811x Gigabit Ethernet
132-
dw 0x8169 ; Driver ID
133-
dw 0x10EC ; Vendor ID
134-
dw 0x8161 ; 8111/8168/8411 PCI Express
135-
dw 0x8167 ; 8110SC/8169SC
136-
dw 0x8168 ; 8111/8168/8211/8411 PCI Express
137-
dw 0x8169 ; 8169
138-
dw 0x0000
132+
;dw 0x8169 ; Driver ID
133+
;dw 0x10EC ; Vendor ID
134+
;dw 0x8161 ; 8111/8168/8411 PCI Express
135+
;dw 0x8167 ; 8110SC/8169SC
136+
;dw 0x8168 ; 8111/8168/8211/8411 PCI Express
137+
;dw 0x8169 ; 8169
138+
;dw 0x0000
139139

140140
; End of list
141141
dw 0x0000

src/drivers/net/r8169.asm

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@ net_r8169_init:
1515
push rcx
1616
push rax
1717

18+
mov rdi, net_table
19+
xor eax, eax
20+
mov al, [os_net_icount]
21+
shl eax, 7 ; Quick multiply by 128
22+
add rdi, rax
23+
24+
mov ax, 0x8169 ; Driver tag for r8169
25+
stosw
26+
add rdi, 14
27+
1828
; Get the Base I/O Address of the device
1929
mov dl, 0x04 ; BAR0
2030
call os_bus_read
2131
and eax, 0xFFFFFFFC ; EAX now holds the Base IO Address (clear the low 2 bits)
22-
mov word [os_NetIOAddress], ax
32+
stosq ; Save the base
33+
push rdx
2334

2435
; Set PCI Status/Command values
2536
mov dl, 0x01 ; Read Status/Command
@@ -30,28 +41,51 @@ net_r8169_init:
3041
call os_bus_write ; Write updated Status/Command
3142

3243
; Get the MAC address
33-
mov dx, word [os_NetIOAddress]
44+
pop rdx
45+
sub rdi, 16
3446
in al, dx
35-
mov [os_NetMAC], al
47+
stosb
3648
inc dx
3749
in al, dx
38-
mov [os_NetMAC+1], al
50+
stosb
3951
inc dx
4052
in al, dx
41-
mov [os_NetMAC+2], al
53+
stosb
4254
inc dx
4355
in al, dx
44-
mov [os_NetMAC+3], al
56+
stosb
4557
inc dx
4658
in al, dx
47-
mov [os_NetMAC+4], al
59+
stosb
4860
inc dx
4961
in al, dx
50-
mov [os_NetMAC+5], al
62+
stosb
63+
64+
; Set base addresses for TX and RX descriptors
65+
xor ecx, ecx
66+
mov cl, byte [os_net_icount]
67+
shl ecx, 15
68+
69+
add rdi, 0x22
70+
mov rax, os_tx_desc
71+
add rax, rcx
72+
stosq
73+
mov rax, os_rx_desc
74+
add rax, rcx
75+
stosq
5176

5277
; Reset the device
78+
xor edx, edx
79+
mov dl, [os_net_icount]
5380
call net_r8169_reset
5481

82+
; Store call addresses
83+
sub rdi, 0x20
84+
mov rax, net_r8169_transmit
85+
stosq
86+
mov rax, net_r8169_poll
87+
stosq
88+
5589
pop rax
5690
pop rcx
5791
pop rdx
@@ -61,7 +95,7 @@ net_r8169_init:
6195

6296

6397
; -----------------------------------------------------------------------------
64-
; os_net_rtl8136_reset - Reset a Realtek 8169 NIC
98+
; os_net_r8169_reset - Reset a Realtek 8169 NIC
6599
; IN: Nothing
66100
; OUT: Nothing, all registers preserved
67101
net_r8169_reset:

src/drivers/net/virtio-net.asm

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,31 @@
1010
; Initialize a Virtio NIC
1111
; IN: RDX = Packed Bus address (as per syscalls/bus.asm)
1212
net_virtio_init:
13+
push rdi
1314
push rsi
1415
push rdx
1516
push rcx
1617
push rbx
1718
push rax
1819

20+
mov rdi, net_table
21+
xor eax, eax
22+
mov al, [os_net_icount]
23+
shl eax, 7 ; Quick multiply by 128
24+
add rdi, rax
25+
26+
mov ax, 0x1AF4 ; Driver tag for virtio-net
27+
stosw
28+
push rdi ; Used in msi-x init
29+
add rdi, 14
30+
1931
; Get the Base Memory Address of the device
2032
mov al, 4 ; Read BAR4
2133
call os_bus_read_bar
22-
mov [os_NetIOBaseMem], rax ; Save it as the base
23-
mov [os_NetIOLength], rcx ; Save the length
34+
stosq ; Save the base
35+
push rax ; Save the base for gathering the MAC later
36+
mov rax, rcx
37+
stosq ; Save the length
2438

2539
; Set PCI Status/Command values
2640
mov dl, 0x01 ; Read Status/Command
@@ -159,36 +173,51 @@ virtio_net_init_cap_next_offset:
159173
virtio_net_init_cap_end:
160174

161175
; Get the MAC address
162-
mov rsi, [os_NetIOBaseMem]
176+
pop rsi ; Pushed as RAX
177+
pop rdi
178+
add rdi, 6 ; nt_MAC
163179
add rsi, [virtio_net_device_offset]
164-
lodsb
165-
mov [os_NetMAC], al
166-
lodsb
167-
mov [os_NetMAC+1], al
168-
lodsb
169-
mov [os_NetMAC+2], al
170-
lodsb
171-
mov [os_NetMAC+3], al
172-
lodsb
173-
mov [os_NetMAC+4], al
174-
lodsb
175-
mov [os_NetMAC+5], al
180+
mov ecx, 6
181+
rep movsb
182+
183+
; Set base addresses for TX and RX descriptors
184+
xor ecx, ecx
185+
mov cl, byte [os_net_icount]
186+
shl ecx, 15
187+
add rdi, 0x22
188+
mov rax, os_tx_desc
189+
add rax, rcx
190+
stosq
191+
mov rax, os_rx_desc
192+
add rax, rcx
193+
stosq
176194

195+
; Reset the device
196+
xor edx, edx
197+
mov dl, [os_net_icount]
177198
call net_virtio_reset
178199

200+
; Store call addresses
201+
sub rdi, 0x20
202+
mov rax, net_virtio_transmit
203+
stosq
204+
mov rax, net_virtio_poll
205+
stosq
206+
179207
virtio_net_init_error:
180208
pop rax
181209
pop rbx
182210
pop rcx
183211
pop rdx
184212
pop rsi
213+
pop rdi
185214
ret
186215
; -----------------------------------------------------------------------------
187216

188217

189218
; -----------------------------------------------------------------------------
190219
; net_virtio_reset - Reset a Virtio NIC
191-
; IN: Nothing
220+
; IN: RDX = Interface ID
192221
; OUT: Nothing, all registers preserved
193222
net_virtio_reset:
194223
push rdi
@@ -198,7 +227,15 @@ net_virtio_reset:
198227

199228
; Device Initialization (section 3.1)
200229

201-
mov rsi, [os_NetIOBaseMem]
230+
; Gather Base Address from net_table
231+
mov rsi, net_table
232+
xor eax, eax
233+
mov al, [os_net_icount]
234+
shl eax, 7 ; Quick multiply by 128
235+
add rsi, rax
236+
add rsi, 16
237+
mov rsi, [rsi]
238+
mov rdi, rsi
202239

203240
; 3.1.1 - Step 1 - Reset the device (section 2.4)
204241
mov al, 0x00
@@ -211,7 +248,7 @@ virtio_net_init_reset_wait:
211248
; 3.1.1 - Step 2 - Tell the device we see it
212249
mov al, VIRTIO_STATUS_ACKNOWLEDGE
213250
mov [rsi+VIRTIO_DEVICE_STATUS], al
214-
251+
215252
; 3.1.1 - Step 3 - Tell the device we support it
216253
mov al, VIRTIO_STATUS_ACKNOWLEDGE | VIRTIO_STATUS_DRIVER
217254
mov [rsi+VIRTIO_DEVICE_STATUS], al
@@ -244,7 +281,7 @@ virtio_net_init_reset_wait:
244281
; 3.1.1 - Step 5
245282
mov al, VIRTIO_STATUS_ACKNOWLEDGE | VIRTIO_STATUS_DRIVER | VIRTIO_STATUS_FEATURES_OK
246283
mov [rsi+VIRTIO_DEVICE_STATUS], al
247-
284+
248285
; 3.1.1 - Step 6 - Re-read device status to make sure FEATURES_OK is still set
249286
mov al, [rsi+VIRTIO_DEVICE_STATUS]
250287
bt ax, 3 ; VIRTIO_STATUS_FEATURES_OK
@@ -260,7 +297,7 @@ virtio_net_init_reset_wait:
260297
mov ax, 0x0000
261298
mov [rsi+VIRTIO_CONFIG_MSIX_VECTOR], ax
262299

263-
; Set up Queue 0
300+
; Set up Queue 0 (Receive)
264301
xor eax, eax
265302
mov [rsi+VIRTIO_QUEUE_SELECT], ax
266303
mov ax, [rsi+VIRTIO_QUEUE_SIZE] ; Return the size of the queue
@@ -286,7 +323,7 @@ virtio_net_init_reset_wait:
286323
mov ax, 1
287324
mov [rsi+VIRTIO_QUEUE_ENABLE], ax
288325

289-
; Set up Queue 1
326+
; Set up Queue 1 (Transmit)
290327
mov eax, 1
291328
mov [rsi+VIRTIO_QUEUE_SELECT], ax
292329
mov ax, [rsi+VIRTIO_QUEUE_SIZE] ; Return the size of the queue
@@ -360,6 +397,7 @@ virtio_net_init_pop:
360397
; -----------------------------------------------------------------------------
361398
; net_virtio_transmit - Transmit a packet via a Virtio NIC
362399
; IN: RSI = Location of packet
400+
; RDX = Interface ID
363401
; RCX = Length of packet
364402
; OUT: Nothing
365403
net_virtio_transmit:
@@ -394,7 +432,7 @@ net_virtio_transmit:
394432
stosw ; 16-bit ring
395433

396434
; Notify the queue
397-
mov rdi, [os_NetIOBaseMem]
435+
mov rdi, [rdx+nt_base] ; Transmit Descriptor Base Address
398436
add rdi, [virtio_net_notify_offset]
399437
add rdi, 4
400438
xor eax, eax
@@ -422,6 +460,7 @@ net_virtio_transmit_wait:
422460
; -----------------------------------------------------------------------------
423461
; net_virtio_poll - Polls the Virtio NIC for a received packet
424462
; IN: RDI = Location to store packet
463+
; RDX = Interface ID
425464
; OUT: RCX = Length of packet
426465
net_virtio_poll:
427466
push rdi

src/init/net.asm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ init_net_probe_found:
5555
je init_net_probe_found_i8257x
5656
cmp bx, 0x8259
5757
je init_net_probe_found_i8259x
58-
cmp bx, 0x8169
59-
je init_net_probe_found_r8169
58+
; cmp bx, 0x8169
59+
; je init_net_probe_found_r8169
6060
jmp init_net_probe_not_found
6161

6262
init_net_probe_found_virtio:
@@ -75,9 +75,9 @@ init_net_probe_found_i8259x:
7575
call net_i8259x_init
7676
jmp init_net_probe_found_finish
7777

78-
init_net_probe_found_r8169:
79-
call net_r8169_init
80-
jmp init_net_probe_found_finish
78+
;init_net_probe_found_r8169:
79+
; call net_r8169_init
80+
; jmp init_net_probe_found_finish
8181

8282
init_net_probe_found_finish:
8383
mov byte [os_NetEnabled], 1 ; A supported NIC was found. Signal to the OS that networking is enabled

src/syscalls/net.asm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ b_net_tx_maxcheck:
6161
cmp rcx, 1522 ; Fail if more than 1522 bytes
6262
ja b_net_tx_fail
6363

64-
; TODO - This should be per interface
65-
mov rax, os_NetLock ; Lock the net so only one send can happen at a time
64+
; Lock the network interface so only one send can happen at a time
65+
mov rax, rdx
66+
add rax, nt_lock
6667
call b_smp_lock
6768

6869
; Calculate where in physical memory the data should be read from
@@ -77,8 +78,9 @@ b_net_tx_maxcheck:
7778
inc qword [rdx+nt_tx_packets] ; Increment TXPackets
7879
add qword [rdx+nt_tx_bytes], rcx ; Increment TXBytes
7980

80-
; TODO - This should be per interface
81-
mov rax, os_NetLock
81+
; Unlock the network interface
82+
mov rax, rdx
83+
add rax, nt_lock
8284
call b_smp_unlock
8385

8486
b_net_tx_fail:

src/sysvar.asm

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ os_LocalAPICAddress: equ os_SystemVariables + 0x0000
6666
os_IOAPICAddress: equ os_SystemVariables + 0x0008
6767
os_SysConfEn: equ os_SystemVariables + 0x0010 ; Enabled bits: 0=PS/2 Keyboard, 1=PS/2 Mouse, 2=Serial, 4=HPET, 5=xHCI
6868
os_StackBase: equ os_SystemVariables + 0x0020
69-
os_NetIOBaseMem: equ os_SystemVariables + 0x0040
70-
os_NetMAC: equ os_SystemVariables + 0x0048
7169
os_HPET_Address: equ os_SystemVariables + 0x0050
7270
os_AHCI_Base: equ os_SystemVariables + 0x0058
7371
os_NetworkCallback: equ os_SystemVariables + 0x0060
@@ -78,7 +76,6 @@ os_nvs_io: equ os_SystemVariables + 0x00B0
7876
os_nvs_id: equ os_SystemVariables + 0x00B8
7977
os_screen_lfb: equ os_SystemVariables + 0x00C0
8078
os_virtioblk_base: equ os_SystemVariables + 0x00C8
81-
os_NetIOLength: equ os_SystemVariables + 0x00D0
8279
os_MouseCallback: equ os_SystemVariables + 0x00D8
8380
os_xHCI_Base: equ os_SystemVariables + 0x00E0
8481
os_usb_evtoken: equ os_SystemVariables + 0x00E8
@@ -98,8 +95,6 @@ os_xhci_int0_count: equ os_SystemVariables + 0x011C ; Incremented on xHCI Interr
9895
; DW - Starting at offset 512, increments by 2
9996
os_NumCores: equ os_SystemVariables + 0x0200
10097
os_CoreSpeed: equ os_SystemVariables + 0x0202
101-
os_NetIOAddress: equ os_SystemVariables + 0x0204
102-
os_NetLock: equ os_SystemVariables + 0x0206
10398
os_nvsVar: equ os_SystemVariables + 0x0208 ; Bit 0 for NVMe, 1 for AHCI, 2 for ATA, 3 for Virtio Block
10499
os_screen_x: equ os_SystemVariables + 0x020A
105100
os_screen_y: equ os_SystemVariables + 0x020C
@@ -155,6 +150,7 @@ os_PacketBuffers: equ os_SystemVariables + 0xC000 ; 16KiB
155150

156151
; net_table values (per device - 128 bytes)
157152
nt_ID: equ 0x00 ; 16-bit
153+
nt_lock: equ 0x02 ; 16-bit
158154
nt_MAC: equ 0x08 ; 48-bit
159155
nt_base: equ 0x10 ; 64-bit
160156
nt_len: equ 0x18 ; 64-bit

0 commit comments

Comments
 (0)