PCI BAR rewrite and access

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
alphatruly
Posts: 9
Joined: Tue Aug 17, 2010 7:34 pm

PCI BAR rewrite and access

Post by alphatruly »

Hi all,i am new here.
And I got a problem with the PCI BAR.
I want to access the onboard HDA controller which DeviceID=27D8,VendorID=8086.
I have already accessed the configuration space successfully,and then I readed the only one nonzero BAR out,it shows me the address is:D0440004 .I know it map into the memory space , but I only have 2GB RAM.then I change the value by writing 00300000h into the BAR.After that I read the BAR once again , it shows me 00300004 and the command register return value with 00100106H , It means that the memory space map has enabled , right ?.
Before that , I wrote a program to load 2GB descriptor and selector into FS and GS register ,thus i can access 2G RAM in real mode (I use MOV EAX,AZBAR MOV EBX,GS:[EAX] AZBAR = 00300000H), but when i access the address 00300000H , it return the wrong value with all 0's , I doubt that whether the new value which i wrote into BAR did't map or not.
Am i missed some step or do some incorrent ? what should i do next?

and , as you see ,i am so sorry with my poor english , i comes from China.
Thank you for your help!
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: PCI BAR rewrite and access

Post by Combuster »

the BIOS sets up the BARS for you. This is because the chipset has to know where to send a specific memory access, which may or may not be the particular bus you expect it to go (and the device in question may thus not see it). Also, a BAR corresponds to something that's usually not system memory, and therefore you shouldn't place it in a location where system memory is.

Bottom line, never change BARs unless you have a chipset driver.
"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 ]
alphatruly
Posts: 9
Joined: Tue Aug 17, 2010 7:34 pm

Re: PCI BAR rewrite and access

Post by alphatruly »

Thanks to combuster very much.

It seems that I have to prepare for protect mode paging........
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: PCI BAR rewrite and access

Post by jal »

alphatruly wrote:Thanks to combuster very much.
It seems that I have to prepare for protect mode paging.
While that's always a good thing to prepare for, you do not need to do so to access the memory, as it is device memory, not main memory, and hence does not interfere with the 2GB in you machine.


JAL
alphatruly
Posts: 9
Joined: Tue Aug 17, 2010 7:34 pm

Re: PCI BAR rewrite and access

Post by alphatruly »

jal wrote: While that's always a good thing to prepare for, you do not need to do so to access the memory, as it is device memory, not main memory, and hence does not interfere with the 2GB in you machine.
Thank you all the same!

Now I have a bit confused , what's the device memory ? Is it map into the main memory ?
what should I do if I don't enable the paging ?(infact , I am not good at this really)
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: PCI BAR rewrite and access

Post by thepowersgang »

Some CPU architectures (such as the x86) have two address spaces.
In the x86, there are two - main memory (RAM) and IO Memory.

Main memory is accessed using the standard arithmatic/manipulation instructions (add,sub,mov,....).

IO memory is accessed using the [tt]in[/tt] and [tt]out[/tt] instructions. IO memory is a completely separate address space to the main memory that is only used for device IO.

PCI indicates if a BAR refers to an IO address by setting the lowest bit in the address, if it's set the BAR refers to the alternate address space (IO space), otherwise it's in the main memory.

In both cases, the presence of memory mapped hardware does not detract from available RAM (unless you have more ram than the size of your physical address space)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
alphatruly
Posts: 9
Joined: Tue Aug 17, 2010 7:34 pm

Re: PCI BAR rewrite and access

Post by alphatruly »

thepowersgang wrote:the presence of memory mapped hardware does not detract from available RAM (unless you have more ram than the size of your physical address space)
I think I understand now.
So,it's said,even the address which returns from the BAR has overrun my available RAM , But it still mapped into somewhere actually ? Right ?

But , How I can get access to the device , and JAL said that it doesn't need to paging ,
give me some suggestion in detial please .
I am already gone crazy from this .
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: PCI BAR rewrite and access

Post by Combuster »

alphatruly wrote:So,it's said,even the address which returns from the BAR has overrun my available RAM
The BAR is still correct. If you have too much RAM, parts of it will get disabled as devices are more important. At the same time, the BIOS will tell you there's less memory than physically installed - the amount it can fit in.
But , How I can get access to the device
The BAR is the address, plus a few bits to provide details which you can mask out. You do know how to write code to access a specific address, right?
"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 ]
alphatruly
Posts: 9
Joined: Tue Aug 17, 2010 7:34 pm

Re: PCI BAR rewrite and access

Post by alphatruly »

To Combuster:

First , I am sorry for my poor english and could you explan this ? I not sure I get it........
plus a few bits to provide details which you can mask out. You do know how to write code to access a specific address, right?
eh..........
I know how to access the specific address , like this :
address equ D0440000H
mov eax,address
mov ebx,[eax] ;right ?
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: PCI BAR rewrite and access

Post by jal »

alphatruly wrote:First , I am sorry for my poor english and could you explan this ? I not sure I get it.
Look, if your English is so bad that you don't understand the advise people give you, especially when it is as simple as "the BAR is the address", you should not frequent this place.


JAL
alphatruly
Posts: 9
Joined: Tue Aug 17, 2010 7:34 pm

Re: PCI BAR rewrite and access

Post by alphatruly »

To JAL

yes , I know , but this :

'the BAR is the address'

I have read the PCI Loca Bus Specification , Isn't the BAR = Base Address Register ?

It's a register which save the base address ? Right ? Tell me .

And I think , PCI maped the device memory into the main memory from the base address , So When I access to the device , I can use code like BAR+offset to access it .

But you see , the BAR give me D0440000H , my main memory range is 0 - 7FFFFFFFH

So I rewrite the BAR with my define value : 00300000H , Because I don't think it will conllision to other device in the real mode especially above FFFFFH , And even the BAR changed , but the return value was wrong after I accessed the new address.

Then , Combuster told me that I'd better not to change the BAR .
So , I have to consider whether enable protect mode paging or not , and from you reply , you told me that as it isn't necessary to do this .

I just want to know
1: why the access by changed BAR value was failed , and if the BAR could be changed , show me some key steps which I missed

2: if the BAR should not be changed , tell me how you can access the memory maped registers of pci device with a BAR's value that overrun your physical address space.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: PCI BAR rewrite and access

Post by Candy »

The values you get from 0xE820 are where in your physical address space there's memory. So you should map all BARs from devices to *NOT* be in that range. Treat memory (from E820) like a device, basically.

Your physical address space in 32 bits is *always* a full 32 bits of address space, so it always runs from 0x0 to 0xFFFFFFFF. No exceptions to that.

Because somebody at IBM designed a 20-bit 8086 with devices at 0xA0000 to 0xFFFFF, they messed with the memory to tell it "don't use those adresses even though there is memory for it". The rest just gets you the memory. And, since memory is on an internal bus, no memory addresses (that are used by actual memory) ever make it to the outside PCI bus.

Changing BARs however also involves you changing the pass-through mappings for the PCI bridges between your cpu and that device. Just don't change any bars and you'll be fine.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: PCI BAR rewrite and access

Post by jal »

alphatruly wrote:It's a register which save the base address ? Right ? Tell me .
Yes, correct.
And I think , PCI maped the device memory into the main memory from the base address , So When I access to the device , I can use code like BAR+offset to access it .
Correct again.
But you see , the BAR give me D0440000H , my main memory range is 0 - 7FFFFFFFH
So? Your main memory leads to the little black chips on your memory module. The BAR leads you to the little black chips on your device. Your PC has the capability of addressing 2^32 bytes of memory. Whether or not there is "main memory" (i.e. RAM), ROM or a device at a given address, that's something you needn't be bother with.
So , I have to consider whether enable protect mode paging or not , and from you reply , you told me that as it isn't necessary to do this .
It indeed is not. Because paging maps physical addresses (like the BAR) to virtual ones. Your device doesn't know about virtual addresses.
2: if the BAR should not be changed , tell me how you can access the memory maped registers of pci device with a BAR's value that overrun your physical address space.
It does not overrun your physical address space. The BAR is 32 bit, and has thus the same limit as a physical address on a 32 bit CPU. Look, you are extremely confused about the difference between addressability of memory and the actual RAM, if it's so hard for you, I'd really give up trying to do anything low-level like creating an OS.


JAL
alphatruly
Posts: 9
Joined: Tue Aug 17, 2010 7:34 pm

Re: PCI BAR rewrite and access

Post by alphatruly »

To JAL

OK , OK .

This is the last :

I SHOULD USE THESE:

BAR EQU D0440000H
MOV EAX,GS:[BAR] ;access the maped device register "directly"!!!!!

If I still wrong............

OK , GOD , Kick my @$$......................
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: PCI BAR rewrite and access

Post by jal »

alphatruly wrote:I SHOULD USE THESE:

BAR EQU D0440000H
MOV EAX,GS:[BAR] ;access the maped device register "directly"!!!!!
You are basically right, although of course BAR is not a constant but a value retrieved from reading a register (so it's most likely stored in a memory location by your code), and I'm not sure what GS does there...


JAL
Post Reply