Skip to content

Commit 40281f6

Browse files
author
Ian Seyler
committed
Fix msix_init off by 4 error
1 parent 28976ea commit 40281f6

File tree

4 files changed

+47
-65
lines changed

4 files changed

+47
-65
lines changed

src/drivers/bus/xhci.asm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ xhci_reset_build_scratchpad:
347347
; ├──────────────────────────────┴───────┤
348348
; | Ring Segment Base Address Hi |
349349
; ├──────────────────┬───────────────────┤
350-
; | RsvdZ | Ring Segment Size |
350+
; | RsvdZ | Ring Segment Size |
351351
; ├──────────────────┴───────────────────┤
352352
; | RsvdZ |
353353
; └──────────────────────────────────────┘
@@ -1862,7 +1862,7 @@ xHCI_CC_STALL_ERROR equ 6
18621862
xHCI_CC_RESOURCE_ERROR equ 7
18631863
xHCI_CC_BANDWIDTH_ERROR equ 8
18641864
xHCI_CC_NO_SLOTS_ERROR equ 9
1865-
xHCI_CC_INVALID_STREAM_TYPE_ERROR equ 10
1865+
xHCI_CC_INVALID_STREAM_TYPE_ERROR equ 10
18661866
xHCI_CC_SLOT_NOT_ENABLED_ERROR equ 11
18671867
xHCI_CC_EP_NOT_ENABLED_ERROR equ 12
18681868
xHCI_CC_SHORT_PACKET equ 13

src/drivers/msi.asm

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
; Initialize MSI-X for a device
1111
; IN: RDX = Packed Bus address (as per syscalls/bus.asm)
1212
; AL = Start Vector
13+
; OUT: Carry flag
14+
; -----------------------------------------------------------------------------
15+
; Message Control - Enable (15), Function Mask (14), Table Size (10:0)
16+
;
17+
; Example MSI-X Entry (From QEMU xHCI Controller)
18+
; 000FA011 <- Cap ID 0x11 (MSI-X), next ptr 0xA0, message control 0x000F - Table size is bits 10:0 so 0x0F
19+
; 00003000 <- BIR (2:0) is 0x0 so BAR0, Table Offset (31:3) - 8-byte aligned so clear low 3 bits - 0x3000 in this case
20+
; 00003800 <- Pending Bit BIR (2:0) and Pending Bit Offset (31:3) - 0x3800 in this case
21+
;
22+
; Example MSI-X Entry (From QEMU Virtio-Net)
23+
; 00038411 <- Cap ID 0x11 (MSI-X), next ptr 0x84, message control 0x0003 - Table size is bits 10:0 so 3 (n-1 so table size is actually 4)
24+
; 00000001 <- BIR (2:0) is 0x1 so BAR1, Table Offset (31:3) - 8-byte aligned so clear low 3 bits - 0x0 in this case
25+
; 00000801 <- Pending Bit BIR (2:0) is 0x1 so BAR1 and Pending Bit Offset (31:3) is 0x800
1326
msix_init:
1427
push r8
1528
push rdx
@@ -22,17 +35,12 @@ msix_init:
2235
; Check for MSI-X in PCI Capabilities
2336
mov cl, 0x11 ; PCI Capability ID for MSI-X
2437
call os_bus_cap_check
25-
jc msix_init_error
38+
jc msix_init_error ; os_bus_cap_check sets carry flag is the cap isn't found
39+
40+
push rdx ; Save packed bus address
2641

27-
; Enable MSI-X
28-
msix_init_enable:
29-
push rdx
3042
; Enable MSI-X, Mask it, Get Table Size
31-
; Example MSI-X Entry (From QEMU xHCI Controller)
32-
; 000FA011 <- Cap ID 0x11 (MSI-X), next ptr 0xA0, message control 0x000F - Table size is bits 10:0 so 0x0F
33-
; 00003000 <- BIR (2:0) is 0x0 so BAR0, Table Offset (31:3) - 8-byte aligned so clear low 3 bits - 0x3000 in this case
34-
; 00003800 <- Pending Bit BIR (2:0) and Pending Bit Offset (31:3) - 0x3800 in this case
35-
; Message Control - Enable (15), Function Mask (14), Table Size (10:0)
43+
msix_init_enable:
3644
call os_bus_read
3745
mov ecx, eax ; Save for Table Size
3846
bts eax, 31 ; Enable MSI-X
@@ -51,7 +59,6 @@ msix_init_enable:
5159
mov dl, al
5260
call os_bus_read ; Read the BAR address
5361
add rax, rbx ; Add offset to base
54-
sub rax, 0x04
5562
mov rdi, rax
5663
pop rdx
5764
; Configure MSI-X Table
@@ -73,7 +80,7 @@ msix_init_create_entry:
7380
jne msix_init_create_entry
7481

7582
; Unmask MSI-X via bus
76-
pop rdx
83+
pop rdx ; Restore packed bus address
7784
call os_bus_read
7885
btr eax, 30 ; Clear Function Mask
7986
call os_bus_write

src/drivers/net/virtio-net.asm

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ net_virtio_init:
1717
push rbx
1818
push rax
1919

20+
push rdx ; Save packed bus address
21+
2022
mov rdi, net_table
2123
xor eax, eax
2224
mov al, [os_net_icount]
@@ -25,7 +27,7 @@ net_virtio_init:
2527

2628
mov ax, 0x1AF4 ; Driver tag for virtio-net
2729
stosw
28-
push rdi ; Used in msi-x init
30+
push rdi ; Save offset into net_table
2931
add rdi, 14
3032

3133
; Get the Base Memory Address of the device
@@ -58,60 +60,9 @@ virtio_net_init_cap_next:
5860
call os_bus_read
5961
cmp al, VIRTIO_PCI_CAP_VENDOR_CFG
6062
je virtio_net_init_cap
61-
cmp al, 0x11
62-
je virtio_net_init_msix
6363
shr eax, 8
6464
jmp virtio_net_init_cap_next_offset
6565

66-
virtio_net_init_msix:
67-
push rdx
68-
69-
; Enable MSI-X, Mask it, Get Table Size
70-
call os_bus_read
71-
mov ecx, eax ; Save for Table Size
72-
bts eax, 31 ; Enable MSIX
73-
bts eax, 30 ; Set Function Mask
74-
call os_bus_write
75-
shr ecx, 16 ; Shift Message Control to low 16-bits
76-
and cx, 0x7FF ; Keep bits 10:0
77-
78-
; Read the BIR and Table Offset
79-
push rdx
80-
add dl, 1
81-
call os_bus_read
82-
mov ebx, eax ; EBX for the Table Offset
83-
and ebx, 0xFFFFFFF8 ; Clear bits 2:0
84-
and eax, 0x00000007 ; Keep bits 2:0 for the BIR
85-
add al, 0x04 ; Add offset to start of BARs
86-
mov dl, al
87-
call os_bus_read ; Read the BAR address
88-
add rax, rbx ; Add offset to base
89-
mov rdi, rax
90-
pop rdx
91-
92-
; Configure MSI-X Table
93-
add cx, 1 ; Table Size is 0-indexed
94-
virtio_net_init_msix_entry:
95-
mov rax, [os_LocalAPICAddress] ; 0xFEE for bits 31:20, Dest (19:12), RH (3), DM (2)
96-
stosd ; Store Message Address Low
97-
shr rax, 32 ; Rotate the high bits to EAX
98-
stosd ; Store Message Address High
99-
mov eax, 0x000040AB ; Trigger Mode (15), Level (14), Delivery Mode (10:8), Vector (7:0)
100-
stosd ; Store Message Data
101-
xor eax, eax ; Bits 31:1 are reserved, Masked (0) - 1 for masked
102-
stosd ; Store Vector Control
103-
dec cx
104-
cmp cx, 0
105-
jne virtio_net_init_msix_entry
106-
pop rdx
107-
108-
; Unmask MSI-X
109-
call os_bus_read
110-
btc eax, 30 ; Clear Function Mask
111-
call os_bus_write
112-
113-
jmp virtio_net_init_cap_next_offset
114-
11566
virtio_net_init_cap:
11667
rol eax, 8 ; Move Virtio cfg_type to AL
11768
cmp al, VIRTIO_PCI_CAP_COMMON_CFG
@@ -197,6 +148,27 @@ virtio_net_init_cap_end:
197148
mov dl, [os_net_icount]
198149
call net_virtio_reset
199150

151+
; Enable interrupts
152+
pop rdx ; Restore packed bus address
153+
; mov al, 0x40
154+
; call msix_init
155+
; jc virtio_net_init_no_int
156+
; Create a gate in the IDT
157+
; mov edi, 0x40
158+
; mov rax, net_virtio_int
159+
; call create_gate
160+
; mov edi, 0x41
161+
; mov rax, net_virtio_int
162+
; call create_gate
163+
; mov edi, 0x42
164+
; mov rax, net_virtio_int
165+
; call create_gate
166+
; mov edi, 0x43
167+
; mov rax, net_virtio_int
168+
; call create_gate
169+
170+
virtio_net_init_no_int:
171+
200172
; Store call addresses
201173
sub rdi, 0x28
202174
mov eax, net_virtio_config
@@ -583,6 +555,8 @@ net_virtio_int:
583555
push rax
584556

585557
; Clear pending interrupt (if set)
558+
mov al, 0xab
559+
call os_debug_dump_al
586560

587561
; Acknowledge the interrupt
588562
mov ecx, APIC_EOI

src/sysvar.asm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ os_PacketBuffers: equ os_SystemVariables + 0xC000 ; 16KiB
149149
; net_table values (per device - 128 bytes)
150150
nt_ID: equ 0x00 ; 16-bit Driver ID
151151
nt_lock: equ 0x02 ; 16-bit Lock for b_net_tx
152+
nt_interrupt: equ 0x04 ; 16-bit Interrupts enabled flag
152153
nt_MAC: equ 0x08 ; 48-bit MAC Address
153154
nt_base: equ 0x10 ; 64-bit Base MMIO
154155
nt_config: equ 0x18 ; 64-bit Config function address

0 commit comments

Comments
 (0)