Writing DMA buffers to a memory mapped file

I need to write in embedded Linux (2.6.37) the incoming DMA buffers in the HD partition as raw device / dev / sda1 as quickly as possible. Buffers are aligned as needed and have an equal length of 512 KB. The process can continue for a very long time and fill as much as, for example, 256 GB of data. I need to use memory mapping file technology (O_DIRECT is not applicable), but cannot figure out how to do this. So, in the pseudocode the "normal" entry:

fd=open(/dev/sda1",O_WRONLY);
while(1) {
    p = GetVirtualPointerToNewBuffer();
    if (InputStopped())
        break;
    write(fd, p, BLOCK512KB);
}

Now I will be very grateful for a similar example of pseudo / real code, how to use the memory mapping technique for this entry.

UPDATE2: Thanks to kestasx, the last working test code looks like this:

#define TSIZE   (64*KB)
void* TBuf;
int main(int argc, char **argv) {
    int fdi=open("input.dat", O_RDONLY);
    //int fdo=open("/dev/sdb2", O_RDWR);
    int fdo=open("output.dat", O_RDWR);
    int i, offs=0;
    void* addr;
        i = posix_memalign(&TBuf, TSIZE, TSIZE);
        if ((fdo < 1) || (fdi < 1)) {
            printf("Error in files\n");
            return -1; }
        while(1) {
            addr = mmap((void*)TBuf, TSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fdo, offs);
            if ((unsigned int)addr == 0xFFFFFFFFUL) {
                printf("Error MMAP=%d, %s\n", errno, strerror(errno));
                return -1; }
            i = read(fdi, TBuf, TSIZE);
            if (i != TSIZE) {
                printf("End of data\n");
                return 0; }
            i = munmap(addr, TSIZE);
            offs += TSIZE;
            sleep(1);
        };
}

Update3:  1. DMA, read() mmp(), DMA , , . , : while(1) { read(fdi, TBuf, TSIZE); addr = mmap((void*)TBuf, TSIZE, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED, fdo, offs); munmap(addr, TSIZE); offs += TSIZE; }

(!) - read() BAD ADDRESS TBuf. , , munmap() msync(). . , - unmapping addr TBuf?

2. DMA. , read() - , , DMA .
, , (!). , Linux , ​​ () .
, read() - , .

, - Linux, , , !

!

+4
1

, , mmap() ( , mmap() raw partition/block-device), DMA .

p ( ) , . - ( mmap).

, psudo- :

truncate("data.bin", 256GB);
fd = open( "data.bin", O_RDWR );
p = GetVirtualPointerToNewBuffer();
adr = mmap( p, 1GB, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset_in_file );
startDMA();
waitDMAfinish();
munmap( adr, 1GB );

, , DMA ( ).

, 32- , 1GB ( . ).

, , .

, addr 4 .

, . , , ( DMA, , / ).

UPDATE:

, mmap'ed DMA, ( , : , ).

UPDATE2: :

DMA :

  • CPU DMA (, );
  • DMA , ;
  • DMA - DMA CPU IRQ (), .

, : DMA ( VM CPU). , DMA ( , - , ).

mmap() :

  • mmap() ( , - );
  • I/O ( ) mmaped range pagefault, , atached;
  • mmaped ( , : , , , , , , , , ).
  • () ( ) msync() munmap()

DMA mmaped , , ar ( - , , ).

Update3:

DMA , ( ant, ), , ( , 4 ). , () .

UPDATE4:

, O_WRONLY mmap'ed (. , ). mmap(), . ( POSIX, ).

, , mmap() ( ).

DMA , , , , DMA ( , DMA, ). Linux MAP_POPULATE mmap, MAP_PRIVATE- ( ), , , . , , . .

mmap DMA , , , mmap O_WRONLY (, , , ).

0

Source: https://habr.com/ru/post/1568635/


All Articles