WIP: Test code to detect VirtIO devices #373

Draft
YVishere wants to merge 10 commits from YVishere/kolibrios:detect_virtio into main
Contributor

Compares Vendor IDs listed in qemu's website with IDs extracted from PCIList to determine whether the driver is VirtIO or not

Compares Vendor IDs listed in qemu's website with IDs extracted from PCIList to determine whether the driver is VirtIO or not
YVishere requested review from dunkaist 2026-03-16 20:42:15 +00:00
dunkaist requested changes 2026-03-16 22:10:41 +00:00
@@ -0,0 +50,4 @@
push ebx
invoke GetPCIList
test eax, eax
Owner

It is safe to remove this check: GetPCIList returns a pointer to a statically allocated object which is never zero.

It is safe to remove this check: GetPCIList returns a pointer to a statically allocated object which is never zero.
YVishere marked this conversation as resolved
@@ -0,0 +57,4 @@
.next_dev:
mov eax, [eax+PCIDEV.fd]
test eax, eax
Owner

I'm not sure this can be zero too

I'm not sure this can be zero too
YVishere marked this conversation as resolved
@@ -0,0 +64,4 @@
jz .finish
mov edx, [eax+PCIDEV.vendor_device_id]
test edx, edx
Owner

Can a PCI device have both vendor_id and device_id zeroed?

Can a PCI device have both vendor_id and device_id zeroed?
YVishere marked this conversation as resolved
@@ -0,0 +77,4 @@
jmp @F
.isVirt:
mov esi, msgVirtIO
Owner

You can move this to the line 69 and remove the jmp one line above

You can move this to the line 69 and remove the jmp one line above
YVishere marked this conversation as resolved
@@ -0,0 +80,4 @@
mov esi, msgVirtIO
@@:
DEBUGF 1, "I: %s %x\n", esi, edx
Owner

Please, log vendor_id and device_id separately like 1234:5678

Please, log vendor_id and device_id separately like 1234:5678
YVishere marked this conversation as resolved
@@ -0,0 +95,4 @@
ret
endp
proc service_proc stdcall, ioctl:dword
Owner

Is this proc needed?

Is this proc needed?
Author
Contributor

I added this when I couldn't get my driver to work. I thought maybe having this would solve the issue but I just verified it works without this and the invoke RegService, my_service, service_proc line

I added this when I couldn't get my driver to work. I thought maybe having this would solve the issue but I just verified it works without this and the `invoke RegService, my_service, service_proc` line
YVishere marked this conversation as resolved
YVishere requested review from dunkaist 2026-03-16 23:43:47 +00:00
Owner

@YVishere, detection of VirtIO devices seems to work, good job! Since you plan to work on specific VirtIO devices, could you, please, add detection of those now? I.e. not just vendor id's but also device id's. Let's say, for block devices, gpus, network devices.

@YVishere, detection of VirtIO devices seems to work, good job! Since you plan to work on specific VirtIO devices, could you, please, add detection of those now? I.e. not just vendor id's but also device id's. Let's say, for block devices, gpus, network devices.
dunkaist marked the pull request as work in progress 2026-03-18 02:56:17 +00:00
YVishere added 1 commit 2026-03-18 04:40:45 +00:00
Added some documentation on register use
Build system / Build (pull_request) Successful in 17m52s
Build system / Check kernel codestyle (pull_request) Successful in 48s
3c8782552d
YVishere force-pushed detect_virtio from ab87edf417 to 3c8782552d 2026-03-18 04:40:45 +00:00 Compare
dunkaist requested changes 2026-03-19 18:09:01 +00:00
@@ -0,0 +11,4 @@
; Returns pointer to the device id stored in data
; clears and returns value in esi
if used PCI_find_dev
Owner

Is this 'if' needed?

Is this 'if' needed?
Author
Contributor

I assumed it helps in memory optimization like an #ifdef in c. I noticed in another .inc file I was looking at so I thought to add it as a good practice.

I assumed it helps in memory optimization like an #ifdef in c. I noticed in another .inc file I was looking at so I thought to add it as a good practice.
Owner

If a file can be included into several other files within one project, then such a logic can take place, indeed. For example, macros.inc includes struct.inc, some other file includes struct.inc. It might make sense to put some guard into struct.inc to be included just once. In your case device_table.inc is always included only one time by virt_io_det.asm. You can build the driver with this 'if' and without it and see no difference.

If a file can be included into several other files within one project, then such a logic can take place, indeed. For example, macros.inc includes struct.inc, some other file includes struct.inc. It might make sense to put some guard into struct.inc to be included just once. In your case device_table.inc is always included only one time by virt_io_det.asm. You can build the driver with this 'if' and without it and see no difference.
Author
Contributor

Oh ok that makes sense

Oh ok that makes sense
YVishere marked this conversation as resolved
@@ -0,0 +21,4 @@
mov esi, ven2_array
cmp [ven_id], VirtVendorID2
jnz @F ;unknown vendor id
Owner

Anonymous labels are convenient when jumping for a few lines for an obvious reason. Here you are jumping quite far over several named labels. Please, make this label named too.

Anonymous labels are convenient when jumping for a few lines for an obvious reason. Here you are jumping quite far over several named labels. Please, make this label named too.
YVishere marked this conversation as resolved
@@ -0,0 +24,4 @@
jnz @F ;unknown vendor id
.pre_loop:
push edx
Owner

You push/pop edx in several places. It will be easier to push just once at the begin of the function and pop at the end

You push/pop edx in several places. It will be easier to push just once at the begin of the function and pop at the end
Author
Contributor

Yeah I came up with this so that there would be fewer memory accesses. Maybe the branches could minimize the benefits though

Yeah I came up with this so that there would be fewer memory accesses. Maybe the branches could minimize the benefits though
YVishere marked this conversation as resolved
@@ -0,0 +26,4 @@
.pre_loop:
push edx
.loop:
Owner

'.loop' is a faceless name, use something more descriptive like '.check_next_device', '.next_dev', or similar

'.loop' is a faceless name, use something more descriptive like '.check_next_device', '.next_dev', or similar
YVishere marked this conversation as resolved
@@ -0,0 +35,4 @@
cmp edx, [dev_id]
jz .found
add esi, VEN_STRIDE
Owner

If you move this addition a few lines above, you can replace these two branches

        jz     .found
        jmp     .loop

with just one
jnz .loop

If you move this addition a few lines above, you can replace these two branches ``` jz .found jmp .loop ``` with just one ` jnz .loop`
YVishere marked this conversation as resolved
@@ -0,0 +13,4 @@
__DEBUG__ equ 1
__DEBUG_LEVEL__ equ 1
API_VERSION equ 0 ;debug
Owner

Not used

Not used
YVishere marked this conversation as resolved
@@ -0,0 +15,4 @@
API_VERSION equ 0 ;debug
STRIDE equ 4 ;size of row in devices table
Owner

Not used

Not used
YVishere marked this conversation as resolved
@@ -0,0 +17,4 @@
STRIDE equ 4 ;size of row in devices table
SRV_GETVERSION equ 0
Owner

Not used

Not used
YVishere marked this conversation as resolved
@@ -0,0 +62,4 @@
shr ecx, 16 ;cx upper half - device id, dx lower half - vendor id
;prepare regs for find dev proc
movzx ecx, cx
Owner

The upper half of ecx is already 0 because of 'shr' above

The upper half of ecx is already 0 because of 'shr' above
YVishere marked this conversation as resolved
@@ -0,0 +76,4 @@
jmp .next_dev
.finish:
xor eax, eax
Owner
Code style: https://board.kolibrios.org/viewtopic.php?t=1950
YVishere marked this conversation as resolved
YVishere added 1 commit 2026-03-19 23:25:13 +00:00
Improved logic flow and code readability based on suggestions
Build system / Check kernel codestyle (pull_request) Successful in 1m21s
Build system / Build (pull_request) Successful in 10m31s
cd73ea0da6
YVishere requested review from dunkaist 2026-03-19 23:25:25 +00:00
YVishere added 1 commit 2026-03-20 02:13:14 +00:00
Conditional compile for array helper proc
Build system / Check kernel codestyle (pull_request) Successful in 56s
Build system / Build (pull_request) Successful in 10m8s
b878e4e8cc
Owner

@YVishere, looks good to me. You can consider your test task done.
Feel free to start writing a real VirtIO driver based on this code, if you wish. Formally you don't have to, of course.

@YVishere, looks good to me. You can consider your test task done. Feel free to start writing a real VirtIO driver based on this code, if you wish. Formally you don't have to, of course.
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 56s
Required
Details
Build system / Build (pull_request) Successful in 10m8s
Required
Details
This pull request is marked as a work in progress.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u http://git.kolibrios.org/YVishere/kolibrios detect_virtio:YVishere-detect_virtio
git checkout YVishere-detect_virtio
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: KolibriOS/kolibrios#373