On x86_64 systems with 4 GiB or more of RAM (e.g. Debian GNU/Hurd amd64 running under QEMU/KVM with >= 4 GiB of memory), boot fails when ext2fs accesses the root disk through piixide0:
ext2fs: part:2:device:wd0: Input/output error On x86_64, GNU Mach may allocate physical pages above 4 GiB, which cannot be accessed by legacy 32-bit DMA controllers such as piixide. The existing write path only used a low-memory bounce buffer for unaligned source buffers, leaving page-aligned writes and reads vulnerable to DMA failures. Fix both paths in block-rump.c: * rumpdisk_device_write(): Always copy the data into a buffer allocated below 4 GiB using vm_allocate_contiguous(..., 0, 0x100000000ULL, 0). If contiguous allocation fails due to fragmentation, fall back to a single-page bounce buffer below 4 GiB. Also handle count == 0 early. * rumpdisk_device_read(): First allocate the destination buffer directly below 4 GiB using vm_allocate_contiguous(). If that fails, fall back to vm_allocate() with a single-page bounce buffer below 4 GiB. Signed-off-by: Sophiel Zhou <[email protected]> --- rumpdisk/block-rump.c | 221 ++++++++++++++++++++++++------------------ 1 file changed, 129 insertions(+), 92 deletions(-) diff --git a/rumpdisk/block-rump.c b/rumpdisk/block-rump.c index 35fb00e9..abb52ad8 100644 --- a/rumpdisk/block-rump.c +++ b/rumpdisk/block-rump.c @@ -412,6 +412,12 @@ rumpdisk_device_write (void *d, mach_port_t reply_port, if ((bd->mode & D_WRITE) == 0) return D_INVALID_OPERATION; + if (count == 0) + { + *bytes_written = 0; + return D_SUCCESS; + } + pthread_rwlock_rdlock (&rumpdisk_rwlock); /* Ensure device is still open */ if (! bd->taken) @@ -420,69 +426,68 @@ rumpdisk_device_write (void *d, mach_port_t reply_port, return D_INVALID_OPERATION; } - if ((vm_offset_t) data % pagesize) - { - /* Not aligned, have to copy to aligned buffer. */ - vm_address_t buf; - rpc_phys_addr_t pap; - kern_return_t ret; - int err; - - /* While at it, make it contiguous */ - ret = vm_allocate_contiguous (master_host, mach_task_self (), &buf, &pap, count, 0, 0x100000000ULL, 0); - if (ret != KERN_SUCCESS) - { - pthread_rwlock_unlock (&rumpdisk_rwlock); - return ENOMEM; - } - - memcpy ((void*) buf, data, count); - - written = rump_sys_pwrite (bd->rump_fd, (const void *)buf, (size_t)count, (off_t)bn * bd->block_size); - err = errno; - - vm_deallocate (mach_task_self (), (vm_address_t) buf, count); - - if (written < 0) - { - vm_deallocate (mach_task_self (), (vm_address_t) data, count); - pthread_rwlock_unlock (&rumpdisk_rwlock); - return rump_errno2host (err); - } - } - else - { - volatile uint8_t dummy_read __attribute__ ((unused)); - int npages = (count + pagesize - 1) / pagesize; - int i; - - /* Fault-in the memory pages by reading a single byte of each */ - for (i = 0; i < npages; i++) - dummy_read = ((volatile uint8_t *)data)[i * pagesize]; - - /* XXX: _bus_dmamap_load_buffer seems not to call rumpcomp_pci_virt_to_mach - * for each page, so separate out pages ourselves :/ */ - written = 0; - while (written < count) - { - size_t todo = count - written; - ssize_t done; - - if (todo > pagesize) - todo = pagesize; - - done = rump_sys_pwrite (bd->rump_fd, (const void *)data + written, todo, (off_t)bn * bd->block_size + written); - - if (done < 0) - { - vm_deallocate (mach_task_self (), (vm_address_t) data, count); - pthread_rwlock_unlock (&rumpdisk_rwlock); - return rump_errno2host (errno); - } + /* Ensure physical addresses are below 4 GiB for 32-bit DMA controllers + such as piixide. */ + { + vm_address_t buf; + rpc_phys_addr_t pap; + kern_return_t ret; + int err = 0; + + ret = vm_allocate_contiguous (master_host, mach_task_self (), &buf, &pap, + count, 0, 0x100000000ULL, 0); + if (ret == KERN_SUCCESS) + { + memcpy ((void *) buf, data, count); + written = rump_sys_pwrite (bd->rump_fd, (const void *) buf, + (size_t) count, (off_t) bn * bd->block_size); + if (written < 0) + err = errno; + vm_deallocate (mach_task_self (), buf, count); + } + else + { + /* Fall back to a single-page bounce buffer if <4G memory is fragmented. */ + ret = vm_allocate_contiguous (master_host, mach_task_self (), &buf, &pap, + pagesize, 0, 0x100000000ULL, 0); + if (ret != KERN_SUCCESS) + { + pthread_rwlock_unlock (&rumpdisk_rwlock); + return ENOMEM; + } + + written = 0; + while (written < count) + { + size_t todo = count - written; + ssize_t done; + + if (todo > pagesize) + todo = pagesize; + + memcpy ((void *) buf, (const void *) data + written, todo); + done = rump_sys_pwrite (bd->rump_fd, (const void *) buf, todo, + (off_t) bn * bd->block_size + written); + if (done < 0) + { + err = errno; + written = -1; + break; + } + + written += done; + } + + vm_deallocate (mach_task_self (), buf, pagesize); + } - written += done; - } - } + if (written < 0) + { + vm_deallocate (mach_task_self (), (vm_address_t) data, count); + pthread_rwlock_unlock (&rumpdisk_rwlock); + return rump_errno2host (err); + } + } vm_deallocate (mach_task_self (), (vm_address_t) data, count); @@ -519,40 +524,72 @@ rumpdisk_device_read (void *d, mach_port_t reply_port, return D_INVALID_OPERATION; } - /* TODO: directly write at *data when it is aligned */ + /* Ensure physical addresses are below 4 GiB for 32-bit DMA controllers + such as piixide. */ *data = 0; - ret = vm_allocate (mach_task_self (), &buf, npages * pagesize, TRUE); - if (ret != KERN_SUCCESS) - { - pthread_rwlock_unlock (&rumpdisk_rwlock); - return ENOMEM; - } - - /* Ensure physical allocation by writing a single byte of each */ - for (i = 0; i < npages; i++) - ((uint8_t *)buf)[i * pagesize] = 0; + { + rpc_phys_addr_t pap; + vm_address_t bounce = 0; - /* XXX: _bus_dmamap_load_buffer seems not to call rumpcomp_pci_virt_to_mach - * for each page, so separate out pages ourselves :/ */ - done = 0; - while (done < count) - { - size_t todo = count - done; - - if (todo > pagesize) - todo = pagesize; + ret = vm_allocate_contiguous (master_host, mach_task_self (), &buf, &pap, + npages * pagesize, 0, 0x100000000ULL, 0); + if (ret != KERN_SUCCESS) + { + /* Fall back to normal allocation + single-page <4G bounce buffer. */ + ret = vm_allocate (mach_task_self (), &buf, npages * pagesize, TRUE); + if (ret != KERN_SUCCESS) + { + pthread_rwlock_unlock (&rumpdisk_rwlock); + return ENOMEM; + } + + ret = vm_allocate_contiguous (master_host, mach_task_self (), + &bounce, &pap, pagesize, 0, + 0x100000000ULL, 0); + if (ret != KERN_SUCCESS) + { + vm_deallocate (mach_task_self (), buf, npages * pagesize); + pthread_rwlock_unlock (&rumpdisk_rwlock); + return ENOMEM; + } + + /* Ensure physical allocation by writing a single byte of each */ + for (i = 0; i < npages; i++) + ((uint8_t *) buf)[i * pagesize] = 0; + } - err = rump_sys_pread (bd->rump_fd, (void *)buf + done, todo, (off_t)bn * bd->block_size + done); - if (err < 0) - { - err = errno; - vm_deallocate (mach_task_self (), buf, npages * pagesize); - pthread_rwlock_unlock (&rumpdisk_rwlock); - return rump_errno2host (err); - } + /* XXX: _bus_dmamap_load_buffer seems not to call rumpcomp_pci_virt_to_mach + * for each page, so separate out pages ourselves :/ */ + done = 0; + while (done < count) + { + size_t todo = count - done; + void *dst = bounce ? (void *) bounce : (void *) buf + done; + + if (todo > pagesize) + todo = pagesize; + + err = rump_sys_pread (bd->rump_fd, dst, todo, + (off_t) bn * bd->block_size + done); + if (err < 0) + { + err = errno; + if (bounce) + vm_deallocate (mach_task_self (), bounce, pagesize); + vm_deallocate (mach_task_self (), buf, npages * pagesize); + pthread_rwlock_unlock (&rumpdisk_rwlock); + return rump_errno2host (err); + } + + if (bounce) + memcpy ((void *) buf + done, dst, err); + + done += err; + } - done += err; - } + if (bounce) + vm_deallocate (mach_task_self (), bounce, pagesize); + } *bytes_read = done; *data = (void*) buf; -- 2.56.0
