Enumerate PCI Buses

Programming, for all ages and all languages.
Multit4sker
Posts: 5
Joined: Fri Jul 31, 2015 5:49 am

Enumerate PCI Buses

Post by Multit4sker »

Hi All,

In the context of a school project on computers architecture, I have to make a PCI Bus Enumerator.
The code will be divided into two layers :
- An enumeration layer that handle enumeration of the differents buses and devices in each bus. It then must use a data structure to store all these informations (Tree maybe?) and other informations about the device (vendor id, device id, functions). It should generate a tree representing all the devices and buses.
This layer will then be ported to the kernel land (fictive of course).

- A display layer that take the data structure and display it (using dashes, colors, spaces etc... It must be fashion).

To make this program, I can use assembly languages or C language. I can't use any API. I must talk directly to the hardware and/or use BIOS routines.
I have to deliver a binary that runs on Linux or Win32.

Having only assembly concepts (Hello world :/), I don't see at all how can I start this project. I read some articles on OsDev and I more or less understood the operation (use two loops to browse the buses and devices and check for every device/bus if it exists and if it have functions). But I don't know how to code this.

It's why I ask for your help. Naturally, I don't want any pre-made code, I'm very motivated to learn.

Thanks.

(Sorry for my english, I'm french).
Octocontrabass
Member
Member
Posts: 5513
Joined: Mon Mar 25, 2013 7:01 pm

Re: Enumerate PCI Buses

Post by Octocontrabass »

Multit4sker wrote:I can't use any API. I must talk directly to the hardware and/or use BIOS routines.
I have to deliver a binary that runs on Linux or Win32.
It sounds like you have to fight the operating system for access to the hardware...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Enumerate PCI Buses

Post by Combuster »

PCI for the hardware and ioperm for dealing with linux. Obviously it's nonsense to have to deliver Linux/Windows apps and not be allowed to use any APIs at all, so I assume that restriction is only for the PCI-related part.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Enumerate PCI Buses

Post by SpyderTL »

Octocontrabass wrote:
Multit4sker wrote:I can't use any API. I must talk directly to the hardware and/or use BIOS routines.
I have to deliver a binary that runs on Linux or Win32.
It sounds like you have to fight the operating system for access to the hardware...
It sounds like his professor doesn't understand how computers work. :)

I'd double check the requirements and make sure you aren't missing something.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: Enumerate PCI Buses

Post by BrightLight »

Multit4sker wrote:I can't use any API. I must talk directly to the hardware and/or use BIOS routines.
I have to deliver a binary that runs on Linux or Win32.
It's not going to be easy, that is if you figure out a way to do it. Linux and Win32 prevent hardware access to programs. And I'm pretty sure they don't let you call the BIOS, either.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
Peterbjornx
Member
Member
Posts: 116
Joined: Thu May 06, 2010 4:34 am
Libera.chat IRC: peterbjornx
Location: Leiden, The Netherlands
Contact:

Re: Enumerate PCI Buses

Post by Peterbjornx »

Is it required that the computer keeps operating normally afterwards?, you could use kexec() to load a stub kernel containing your pci code and a simple kprintf or on windows, load a driver that clears the interrupt flag and proceeds to load that same kernel
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: Enumerate PCI Buses

Post by Kevin »

omarrx024 wrote:Linux and Win32 prevent hardware access to programs.
Combuster already mentioned ioperm(). This will generally only work as root, but that's it.

It might be helpful to turn off hardware interrupts while you're accessing the PCI registers in order to avoid any conflicts with the kernel accessing them. After iopl(), this should be possible.
Developer of tyndur - community OS of Lowlevel (German)
Multit4sker
Posts: 5
Joined: Fri Jul 31, 2015 5:49 am

Re: Enumerate PCI Buses

Post by Multit4sker »

Hi guys, I'm really sorry, I didn't see I had so much answers.
Obviously, I can't use API only on the PCI related part.
According to my professor, this program will be executed on a virtual machine.
Here is the homework if you can understand it better than me (Maybe I haven't explained it well) : https://www.dropbox.com/s/4e7ufkpshcsoc ... r.pdf?dl=0
Again, I don't tell you to make this for me, I want to do it myself (If I success, I will go out stronger :p ).

Thanks !
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Enumerate PCI Buses

Post by SpyderTL »

Nope, you explained it perfectly.

The PDF actually says that you must a) write a PCI enumerator in ASM or C for a kernel that does not exist yet, and b) you must deliver an EXE that will run in Linux or Windows.

I'm not sure how both of these statements can be in the same document. They appear to be mutually exclusive. I would talk to the instructor and verify that this wording is correct, because it makes no sense to me...

Personally, I think that I would ignore the line about delivering an EXE that will run on Windows or Linux. I think that line is a mistake. It has nothing to do with OS development at all.

I did notice that they included a link to the osdev wiki as a reference for you to do research. Pretty cool!
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Enumerate PCI Buses

Post by iansjack »

SpyderTL wrote:The PDF actually says that you must a) write a PCI enumerator in ASM or C for a kernel that does not exist yet, and b) you must deliver an EXE that will run in Linux or Windows.
Actually, the PDF says
You’ll create a userland mock-­‐up that will later be ported to the kernel space.
and deliver
A binary that runs on Linux or Win32.
That sounds perfectly doable to me, with no mutual exclusion involved. The only aspect I am not 100% sure of is whether userland programs can access the PCI memory space. But "lspci" does this very thing, so I presume it is possible.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Enumerate PCI Buses

Post by Combuster »

lspci runs unprivileged, so it will be using device files and system calls rather than direct port access (and the required mutual exclusion probably happens in the kernel).
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Enumerate PCI Buses

Post by iansjack »

Yes. A little research turned up this paper from AMD that shows that lspci uses kernel APIs: http://developer.amd.com/wordpress/medi ... access.pdf . But it also points out that a userland program can access the memory directly (via /dev/mem). It says that it is inadvisable to do so as it might affect system stability. I can't see that reading the configuration space could cause such problems, though writing could be a mistake.

Bottom line - it can be done, and this is presumably what the question intends. Really, the Wiki article on PCI tells you just about all you need to know. (And a bit of research will no doubt tell you how to do it in Windows.)
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Enumerate PCI Buses

Post by onlyonemac »

SpyderTL wrote:The PDF actually says that you must a) write a PCI enumerator in ASM or C for a kernel that does not exist yet, and b) you must deliver an EXE that will run in Linux or Windows.
I assume (read: hope) that they mean a *binary* that will run on Linux or Windows, not an *EXE*. Linux does not use .exe files, and if they're going to the trouble of mentioning Linux I certainly hope that they are not mentioning it just for the few Wine users out there...
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Enumerate PCI Buses

Post by iansjack »

onlyonemac wrote:
SpyderTL wrote:The PDF actually says that you must a) write a PCI enumerator in ASM or C for a kernel that does not exist yet, and b) you must deliver an EXE that will run in Linux or Windows.
I assume (read: hope) that they mean a *binary* that will run on Linux or Windows, not an *EXE*. Linux does not use .exe files, and if they're going to the trouble of mentioning Linux I certainly hope that they are not mentioning it just for the few Wine users out there...
That's probably why the PDF specifies "binary" rather than "EXE".
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Enumerate PCI Buses

Post by onlyonemac »

iansjack wrote:
onlyonemac wrote:
SpyderTL wrote:The PDF actually says that you must a) write a PCI enumerator in ASM or C for a kernel that does not exist yet, and b) you must deliver an EXE that will run in Linux or Windows.
I assume (read: hope) that they mean a *binary* that will run on Linux or Windows, not an *EXE*. Linux does not use .exe files, and if they're going to the trouble of mentioning Linux I certainly hope that they are not mentioning it just for the few Wine users out there...
That's probably why the PDF specifies "binary" rather than "EXE".
I was assuming that when SpyderTL wrote "The PDF actually says..." that he was quoting it exactly as it was written in the PDF.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Post Reply