Page 1 of 1

Memory space BAR problem!

Posted: Wed Oct 03, 2012 2:19 pm
by kosmisk
Hello guys! I am trying to map the memory space BAR 0xf200100 i got from PCI to 0x144000 virtually
I tried 0x144000 with rtl8139 and it doesnt work, need help.
I have much comments, so that might help you too :-)

Code: Select all

section .text
init_paging:
   ;page table
   mov   ecx, 1024		;need to map 1024 pages to frames - 1024*1024*4 is 4194304 bytes!
   mov   edi, 0x9d000      ;address of the page table 4 kb right after pde
   mov   edx, 0b00000000000000000000000000000011             ;set attributes, 00000000 00000000 00000000 00000011 <- user1/kernel0, read0/write1 and present1, 
														
.loop1:
   mov   [edi], edx            ; attribut
   add   edi, 4              ;go to next memory location, 4 bytes next because the descriptor is 32 bits
   add   edx, 4096 			;go to next frame adress
   cmp ecx, 700				
   jz .mmio
   jmp .next
.mmio:
   mov edx,   0b11110010000000000001000000000001		;0xf200100 - physical MMIO adress, BAR @ 324*4096 - 0x144000, but dont works
.next:
   
   
   dec   ecx
   jnz   .loop1        ;looping tills we reach 0x9e000

   ;Page Directories
   mov   ecx, 1024			
   mov   edi, 0x9C000      ;address of the page directory 4 kb before pte
   mov   edx, 0             
.loop2:
   mov   [edi], edx           ;attribut
   add   edi, 4             ;go to next memory location
   dec   ecx
   jnz   .loop2 				;looping tills we reach 0x9d000
   
   mov ecx, 0
.loop3:
   mov ebx, 0x3

   mov dword [edx+ecx], 0x9d000
   add dword [edx+ecx], ebx
   add ebx, 0x4
   add ecx, 0x4
   cmp ecx, 1024 				;megabyte counter
   jne .loop3
   
   mov   cr3, edx
   mov   eax, cr0				;enable paging! 
   or    eax, 0x80000000
   mov   cr0, eax       
						
	
ret	

Maybe i have done much wrong, am i mapping it the wrong way?

Re: Memory space BAR problem!

Posted: Thu Oct 04, 2012 12:59 am
by Combuster
BAR 0xf200100 i got from PCI to 0x144000 virtually
You do know that pages go in increments of 4KB and that the mapping you mentioned is by consequence impossible?

Re: Memory space BAR problem!

Posted: Thu Oct 04, 2012 1:17 am
by bluemoon
kosmisk wrote:I tried 0x144000 with rtl8139 and it doesnt work, need help.
What do you expect and how it does not match your expectation?
It's difficult to guess what you wanted to do by stare at the code - you tried to map some memory, but no code related to RTL8139 operations is shown.

Re: Memory space BAR problem!

Posted: Thu Oct 04, 2012 3:02 am
by kosmisk
The IO BAR works fine but i am trying to use the MMIO BAR instead...
So i want to replace all IO BAR's with the MMIO adress.

But, the virtual address of MMIO is wrong because i cant get any interupts.
and that must be a consequence of my bad mapping method..

Code: Select all

section .bss
buffer: resb 8208
section .text

install_rtl8139:

	mov dx, word [pciio]	;pciio holds the I/O BAR @ 0xc100
	add dx, 0x52
	mov al, 0x0
	out dx, al
		
	mov dx, word [pciio]
	add dx, 0x40
	mov eax, 0x03000700
	out dx, eax
	
	mov dx, word [pciio]
	add dx, 0x44
	mov eax, 0xf
	out dx, eax
	
	mov dx, word [pciio]
	add dx, 0x37
	mov al, 0x10
	out dx, al
	
	mov dx, word [pciio]
	add dx, 0x37
	in eax, dx
	test eax, 0x10
	jnz .cont
.cont:
	mov dx, word [pciio]
	add dx, 0x30
	mov eax, dword [buffer]
	out dx, eax				;dword
	
	mov dx, word [pciio]
	add dx, 0x37
	mov al, (1<<3)
	out dx, al
		
	mov dx, word [pciio]
	add dx, 0x3c
	mov ax, 0xffff
	out dx, ax				;word
	
	mov dx, word [pciio]
	add dx, 0x37
	mov al, 0x0c
	out dx, al
		
ret


rtl_net_handle:
	
cli	
	pusha

    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
                                         
	;mov eax,0x144000			;commented because it dont work ! trying to use MMIO 
	;lea ebx, [eax + 0x3e]
	;mov word [ebx], 0xffff
	
	mov dx, word [pciio]
	add dx, 0x3e			;status register
	mov ax, 0xffff
	out dx, ax		;reset the interupts!


	mov esi, irqtest
	call putstring

	mov al, 0x20
    mov dx, 0xA0
	out dx, al    ;clear the irq so we can get more interupts 


	mov al, 0x20	
	mov dx, 0x20
	out dx, al    ;clear the irq so we can get more interupts           
           
	popa
	sti	                                            
iret                                              
align 4


irqtest: db "Interupt test!", 0



Combuster wrote:
BAR 0xf200100 i got from PCI to 0x144000 virtually
You do know that pages go in increments of 4KB and that the mapping you mentioned is by consequence impossible?


Yes i know, but dividing 0x144000 with page size gives no remainder
so it is aligned and should work?

Re: Memory space BAR problem!

Posted: Thu Oct 04, 2012 3:20 am
by Combuster
Yes i know, but dividing 0x144000 with page size gives no remainder
Image

Have you tried dividing the other number by 4K?

Re: Memory space BAR problem!

Posted: Thu Oct 04, 2012 10:29 pm
by linguofreak
kosmisk wrote:
Combuster wrote:
BAR 0xf200100 i got from PCI to 0x144000 virtually
You do know that pages go in increments of 4KB and that the mapping you mentioned is by consequence impossible?


Yes i know, but dividing 0x144000 with page size gives no remainder
so it is aligned and should work?
Dividing 0xf200100 by the page size *does*, however, give a remainder.

Your virtual address has to have the *same* remainder with the page size as the BAR, which does not necessarily mean no remainder.

Re: Memory space BAR problem!

Posted: Sun Oct 07, 2012 2:00 pm
by kosmisk
I solved the problem because before
my mind was like: 1024*1024*4096 = 2^20*4096, so i thought i needed to use PAE.
I mean the frame adress is only 20 bit and cant have 0xf2001000 i thoughted, (yes, i said wrong number before).

so i solved the problem like this:

Code: Select all

cmp ecx, 700				
   jz .mmio
   jmp .next
.mmio:
				  
	  mov edx, 0b11110010000000000001000000000000 ;0xf2001000 physical adress
	  or edx, 1
.calc:	
 	;  MMIO adress @ 325*4096 - 0x145000
   mov   [edi], edx            ; attribut
   add   edi, 4  
   add edx, 4096
   
   push edx
   xor edx, edx
	mov eax, ecx
		mov ebx, 1024
	div ebx
	mov ecx, edx
   pop edx  
   
   dec ecx
   cmp ecx, 0
   jz .calc
.next:	
and then in the irq handler

Code: Select all

	mov eax,0x145000
	lea ebx, [eax + 0x3e]
	mov word [ebx], 0xffff
	
Fixed the problem, so now i can use the Memory space BAR :D