Skip to contentSkip to conversation

SPARC iommu mapping

Qemu-devel Joerg Platte 3 participants 7 messages Apr 4, 2006 – Apr 6, 2006

lore
Share & tools
lore
#0 lore raw

Hi!

I still have problems when using a disk image for the SPARC system emulator. Reading and writing now works better than in previous versions, but sometimes data written to image is simply corrupt. To find this problem I wrote a small program writing data to the beginning of a disk image. Afterwards I compared the written data in the image with the expected values. Most of the data was correct, but sometimes a few pages mostly contains only zeros.

To find this problem, I enabled debugging in the esp.c file and printed the mapped address (after iommu mapping). In some cases the mapped address is zero:

Normal write:

ESP: DMA Direction: r, addr 0x008fd000 0000000b
ESP: DMA Direction: r, addr 0x0bea1000 0000e000
ESP: DMA address 0bea2000
ESP: DMA address 0bea4000
ESP: DMA address 0bea5000
ESP: DMA address 0bea6000
ESP: DMA address 0bea7000
ESP: DMA address 0bea8000
ESP: DMA address 0bea9000
ESP: DMA address 0beaa000
ESP: DMA address 0beab000
ESP: DMA address 0beac000
ESP: DMA address 0bead000
ESP: DMA address 0beaf000
ESP: DMA address 0beb0000

Faulty write:

ESP: DMA Direction: r
ESP: DMA Direction: r, addr 0x008fd000 0000000b
ESP: DMA Direction: r, addr 0x0beb1000 0000e000
ESP: DMA address 0beb2000
ESP: DMA address 0beb3000
ESP: DMA address 0beb4000
ESP: DMA address 0beb5000
ESP: DMA address 0beb6000
ESP: DMA address 0beb7000
ESP: DMA address 0beb8000
ESP: DMA address 00000000

I used this code in handle_ti to produce this output:

	    dmaptr = iommu_translate(s->espdmaregs[1] + i);
	    if ((olddma&0xfffff000) != (dmaptr&0xfffff000)) {
		    DPRINTF("DMA address %08x\n", dmaptr);
		    olddma=dmaptr;
	    }

Unfortunately, I'm no hardware or SPARC expert and it took a whole day to track down this problem. So I'm not able to find the reason for this behaviour. What may cause the wrong mapping? Is this some kind of page fault, which must be handled by the kernel? Or just a bug?

regards,
Jörg

#1 lore raw

Am Dienstag, 4. April 2006 21:27 schrieb Joerg Platte:
Hi!

Quoted 6 lines

To find this problem, I enabled debugging in the esp.c file and printed the mapped address (after iommu mapping). In some cases the mapped address is zero:

ESP: DMA address 0beb8000
ESP: DMA address 00000000

I didn't find a manual of the SBUS IOMMU. But if I understand the contents of newer implementations of an IOMMU, each entry in this MMU table has a valid bit. And therefore, a 0 entry is invalid. But the current implementation of qemu's IOMMU does not check for a valid bit. Where can I find more information on this topic? And is a besser IOMMU currently in development?

regards,
Jörg

#2 lore raw
Quoted 6 lines
Quoted 1 line

To find this problem, I enabled debugging in the esp.c file and printed

the

Quoted 1 line

mapped address (after iommu mapping). In some cases the mapped address

is

Quoted 1 line

zero:

Quoted 8 lines
Quoted 2 lines

ESP: DMA address 0beb8000
ESP: DMA address 00000000

I didn't find a manual of the SBUS IOMMU. But if I understand the contents of newer implementations of an IOMMU, each entry in this MMU table has a valid bit. And therefore, a 0 entry is invalid. But the current implementation of qemu's IOMMU does not check for a valid bit. Where can I find more information on this topic? And is a besser IOMMU currently in development?

I don't have any better documentation either, I just coded against what Proll and Linux expect. But the theory of operation is simple. Much like how the MMU translates CPU's virtual addresses to physical addresses for memory, IOMMU translates device virtual memory accesses to physical addresses. The VA to PA entries are found in a simple page table.

In the case of not finding a valid translation entry, IOMMU can't fault the device like normal MMU can easily fault the CPU. I don't know what should happen then, probably put the address to AFAR register and raise some interrupt, while the device (for example Ethernet controller) waiting for the data suffers in limbo. I think it would be strange for an OS to rely on this, so I guess it's a bug somewhere else. My guess for the valid bit is that it's used in a real IOMMU to select the entries that will be loaded to its internal translation buffer.

The DMA controllers for both ESP and Lance are within the same page. This means that in Qemu, DMA controller register accesses for either of them go to just one of these. It just happens to work, but maybe this causes the problem. You could try to confirm this by enabling also DEBUG_LANCE and see if there is troublesome activity in the Lance direction near the bad accesses.

Can you provide a test case so that I could try it as well?

_________________________________________________________________
Is your PC infected? Get a FREE online computer virus scan from McAfee� Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963

#3 lore raw

Am Mittwoch, 5. April 2006 19:36 schrieb Blue Swirl:
Hi!

Quoted 2 lines

I don't have any better documentation either, I just coded against what Proll and Linux expect. But the theory of operation is simple. Much like

OK, I thought I was just too stupid to find good documentation :-)

Quoted 3 lines

how the MMU translates CPU's virtual addresses to physical addresses for memory, IOMMU translates device virtual memory accesses to physical addresses. The VA to PA entries are found in a simple page table.

That's what I read from qemu's iommu functions.

Quoted 8 lines

In the case of not finding a valid translation entry, IOMMU can't fault the device like normal MMU can easily fault the CPU. I don't know what should happen then, probably put the address to AFAR register and raise some interrupt, while the device (for example Ethernet controller) waiting for the data suffers in limbo. I think it would be strange for an OS to rely on this, so I guess it's a bug somewhere else. My guess for the valid bit is that it's used in a real IOMMU to select the entries that will be loaded to its internal translation buffer.

Maybe.

Quoted 6 lines

The DMA controllers for both ESP and Lance are within the same page. This means that in Qemu, DMA controller register accesses for either of them go to just one of these. It just happens to work, but maybe this causes the problem. You could try to confirm this by enabling also DEBUG_LANCE and see if there is troublesome activity in the Lance direction near the bad accesses.

Hmmm, I don't use the network. Just disk access. But I'll check this.

Quoted 1 line

Can you provide a test case so that I could try it as well?

I'm using the two attached programs. writetest is used inside qemu to directly write to /dev/sda (with "writetest /dev/sda"). readtest can then be used outside qemu to check the written data ("readtest imagefile").

Additionally, I logged the translated virtual address as mentioned in my first mail. The kernel is 2.6.13.

regards,
Jörg

#include <stdio.h>

int main(int argc, char**argv) {
FILE *fp;
unsigned char buffer[4];
unsigned int i,j;
unsigned int doprint=1;
unsigned int d=0;

  if ((fp=fopen(argv[1], "r"))) {
    for (i=0; i<10000000; i+=4) {
      if (fread(&buffer, 4, 1, fp)!=1) {
        printf("error writing at byte %d\n", i);
        break;
      }
      d=0;
      for(j=0;j<4; j++) {
        if (buffer[3-j]!=((i>>(j*8))&0xff)) {
          if (doprint) {
            printf("data differs at address %08x\n", i);
          }
          d=1;
          break;
        }
      }
      if (d) {
        doprint=0;
      } else {
        doprint=1;
      }
    }
    fclose(fp);

}
}

#include <stdio.h>

int main(int argc, char**argv) {
FILE *fp;
unsigned int i;

  if ((fp=fopen(argv[1], "w"))) {
    for (i=0; i<10000000; i+=4) {
      if (fwrite(&i, 4, 1, fp)!=1) {
        printf("error writing at byte %d\n", i);
        break;
      }
    }
    fclose(fp);

}
}

#4 lore raw
In reply to Blue Swirl

Am Mittwoch, 5. April 2006 19:36 schrieb Blue Swirl:
Hi!

Quoted 2 lines

I don't have any better documentation either, I just coded against what Proll and Linux expect. But the theory of operation is simple. Much like

OK, I thought I was just too stupid to find good documentation :-)

Quoted 3 lines

how the MMU translates CPU's virtual addresses to physical addresses for memory, IOMMU translates device virtual memory accesses to physical addresses. The VA to PA entries are found in a simple page table.

That's what I read from qemu's iommu functions.

Quoted 8 lines

In the case of not finding a valid translation entry, IOMMU can't fault the device like normal MMU can easily fault the CPU. I don't know what should happen then, probably put the address to AFAR register and raise some interrupt, while the device (for example Ethernet controller) waiting for the data suffers in limbo. I think it would be strange for an OS to rely on this, so I guess it's a bug somewhere else. My guess for the valid bit is that it's used in a real IOMMU to select the entries that will be loaded to its internal translation buffer.

Maybe.

Quoted 6 lines

The DMA controllers for both ESP and Lance are within the same page. This means that in Qemu, DMA controller register accesses for either of them go to just one of these. It just happens to work, but maybe this causes the problem. You could try to confirm this by enabling also DEBUG_LANCE and see if there is troublesome activity in the Lance direction near the bad accesses.

Hmmm, I don't use the network. Just disk access. But I'll check this.

Quoted 1 line

Can you provide a test case so that I could try it as well?

I'm using the two attached programs. writetest is used inside qemu to directly write to /dev/sda (with "writetest /dev/sda"). readtest can then be used outside qemu to check the written data ("readtest imagefile").

Additionally, I logged the translated virtual address as mentioned in my first mail. The kernel is 2.6.13.

regards,
Jörg

#include <stdio.h>

int main(int argc, char**argv) {
FILE *fp;
unsigned char buffer[4];
unsigned int i,j;
unsigned int doprint=1;
unsigned int d=0;

  if ((fp=fopen(argv[1], "r"))) {
    for (i=0; i<10000000; i+=4) {
      if (fread(&buffer, 4, 1, fp)!=1) {
        printf("error writing at byte %d\n", i);
        break;
      }
      d=0;
      for(j=0;j<4; j++) {
        if (buffer[3-j]!=((i>>(j*8))&0xff)) {
          if (doprint) {
            printf("data differs at address %08x\n", i);
          }
          d=1;
          break;
        }
      }
      if (d) {
        doprint=0;
      } else {
        doprint=1;
      }
    }
    fclose(fp);

}
}

#include <stdio.h>

int main(int argc, char**argv) {
FILE *fp;
unsigned int i;

  if ((fp=fopen(argv[1], "w"))) {
    for (i=0; i<10000000; i+=4) {
      if (fwrite(&i, 4, 1, fp)!=1) {
        printf("error writing at byte %d\n", i);
        break;
      }
    }
    fclose(fp);

}
}

#5 lore raw
In reply to Blue Swirl

Am Mittwoch, 5. April 2006 19:36 schrieb Blue Swirl:
Hi!

Quoted 6 lines

The DMA controllers for both ESP and Lance are within the same page. This means that in Qemu, DMA controller register accesses for either of them go to just one of these. It just happens to work, but maybe this causes the problem. You could try to confirm this by enabling also DEBUG_LANCE and see if there is troublesome activity in the Lance direction near the bad accesses.

Lance is only used during system startup. Then there is no more debugging output. Looks like ethernet is not causing any problems.

Can you reproduce my problems, or is it caused by the kernel? But the same kernel works without problems on a SS4...

regards,
Jörg

#6 lore raw
In reply to Blue Swirl

Am Mittwoch, 5. April 2006 19:36 schrieb Blue Swirl:
Hi!

Today I did some more tests to determine, what's going wrong. At first, Linux flushes the page entries for the 14 buffers that are to be written to disk:
IOMMU: page flush f001c000
IOMMU: page flush f001d000
IOMMU: page flush f001e000
IOMMU: page flush f001f000
IOMMU: page flush f0020000
IOMMU: page flush f0021000
IOMMU: page flush f0022000
IOMMU: page flush f0023000
IOMMU: page flush f0024000

Until here, the addresses are subsequent. Then the next address is much lower:

IOMMU: page flush f000e000
IOMMU: page flush f000f000
IOMMU: page flush f0010000
IOMMU: page flush f0011000
IOMMU: page flush f0012000

Then, after sending the SCSI command, the DMA transfer starts:
ESP: DMA Transfer Information len 00009000
ESP: DMA Direction: r, addr 0x0bdbb000 0000e000

Here, 0000e000 is the number of bytes to write accoring to the SCSI command. In this case it equals the number of pages mentioned above (14). But Linux stores 00009000 in the SCSI controllers transfer counter, which is less than these 14 pages.

Qemu's SCSI driver implementation now reads 14 pages and ignores the number of bytes written to the transfer register and writes 14 pages to disk:

ESP: DMA address p 0bdbb000 v f001c000
ESP: DMA address p 0bdbc000 v f001d000
ESP: DMA address p 0bdbd000 v f001e000
ESP: DMA address p 0bdbe000 v f001f000
ESP: DMA address p 0bdbf000 v f0020000
ESP: DMA address p 0bdc0000 v f0021000
ESP: DMA address p 0bdc1000 v f0022000
ESP: DMA address p 0bdc2000 v f0023000
ESP: DMA address p 0bdc3000 v f0024000

No problem until here.

ESP: DMA address p 00000000 v f0025000

Here, after 9 pages qemu tries to access page f0025000. But this page has never been written to the page table and therefore the mapped address is 0. The next page is f000e000. And this problem occures after 00009000 bytes (which is the number of bytes written to the transfer register). Hence, the kernel must adjust the DMA controller to point to the next address, because starting from here, there is a "gap" in the virtual address space. Currently I'm trying to understand the Linux kernel to figure out, whats the expexted behaviour of this scsi controller. But it's not that easy, because I have no deep knowledge about the sparc hardware.

What is expected, when the number of bytes from the transfer register have been read? Should the hardware raise an interrupt? And if yes, which flags must be set?

regards,
Jörg