Index: sys/riscv/include/riscvreg.h =================================================================== --- sys/riscv/include/riscvreg.h +++ sys/riscv/include/riscvreg.h @@ -156,6 +156,7 @@ #define SATP_MODE_SV48 (9ULL << SATP_MODE_S) #define XLEN 8 +#define REG_SIZE 8 #define INSN_SIZE 4 #define INSN_C_SIZE 2 Index: sys/riscv/riscv/copyinout.S =================================================================== --- sys/riscv/riscv/copyinout.S +++ sys/riscv/riscv/copyinout.S @@ -129,21 +129,62 @@ * a0 (src) - Source address * a1 (dest) - Destination address * a2 (len) - Size of the copy + * + * This function will perform word-sized loads and stores when possible. + * src and dest need not be word-aligned addresses, so long as they are + * aligned with one another. Misalignments at the beginning or end are + * handled with a simple byte-by-byte copy. + * + * For cases where src and dest are not aligned, or when len < REG_SIZE, + * we resort to a byte-by-byte copy for the entire thing. */ ENTRY_LOCAL(copycommon) - la a6, copyio_fault /* Get the handler address */ - SET_FAULT_HANDLER(a6, a7) /* Set the handler */ + la a6, copyio_fault /* Get the handler address */ + SET_FAULT_HANDLER(a6, a7) /* Set the handler */ ENTER_USER_ACCESS(a7) -1: lb a4, 0(a0) /* Load from src */ + li t2, REG_SIZE + blt a2, t2, 3f /* Byte-copy if len < REG_SIZE */ + + /* + * Compare lower bits of src and dest. If they are + * aligned with each other, we can do word copy. + */ + andi t0, a0, (REG_SIZE-1) /* Low bits of src */ + andi t1, a1, (REG_SIZE-1) /* Low bits of dest */ + bne t0, t1, 3f /* Misaligned. Go to byte copy */ + beqz t0, 2f /* Already word-aligned, skip ahead */ + + /* Byte copy until the first word-aligned address */ +1: lb a4, 0(a0) /* Load byte from src */ addi a0, a0, 1 - sb a4, 0(a1) /* Store in dest */ + sb a4, 0(a1) /* Store byte in dest */ addi a1, a1, 1 - addi a2, a2, -1 /* len-- */ - bnez a2, 1b + addi a2, a2, -1 /* len-- */ + andi t0, a0, (REG_SIZE-1) + bnez t0, 1b + + /* Copy words */ +2: ld a4, 0(a0) /* Load word from src */ + addi a0, a0, REG_SIZE + sd a4, 0(a1) /* Store word in dest */ + addi a1, a1, REG_SIZE + addi a2, a2, -REG_SIZE /* len -= REG_SIZE */ + bgeu a2, t2, 2b /* Again if len >= REG_SIZE */ + + /* Check if we're finished */ + beqz a2, 4f + + /* Copy all remaining bytes */ +3: lb a4, 0(a0) /* Load byte from src */ + addi a0, a0, 1 + sb a4, 0(a1) /* Store byte in dest */ + addi a1, a1, 1 + addi a2, a2, -1 /* len-- */ + bnez a2, 3b - EXIT_USER_ACCESS(a7) - SET_FAULT_HANDLER(x0, a7) /* Clear the handler */ +4: EXIT_USER_ACCESS(a7) + SET_FAULT_HANDLER(x0, a7) /* Clear the handler */ li a0, 0 ret