michaeljclark / rv8

RISC-V simulator for x86-64
https://michaeljclark.github.io/
MIT License
682 stars 96 forks source link

Fix or replace TLSF allocator to support 16-byte alignment for SSE4.2/AVX #25

Closed michaeljclark closed 8 years ago

michaeljclark commented 8 years ago

The meta emulator uses the TLSF memory allocator: https://github.com/mattconte/tlsf

It does this due to the non default link location to allow loading RISC-V executables into the lower part in the non privileged common address space proxy model (not --privileged which currently implements a soft-mmu).

MACOS_LDFLAGS = -Wl,-pagezero_size,0x1000 -Wl,-no_pie -image_base 0x40000000 LINUX_LDFLAGS = -Wl,--section-start=.text=0x40000000 -static

The platform allocator is not guaranteed to adjust its allocation area for the new text segment location so we implement a custom allocator that is the same on all platforms.

The issue is Clang generates code like this on x86_64, which requires allocations to be 16-byte aligned:

0000000040068f1e    movaps  %xmm0, 0x70(%rbx)
0000000040068f22    movaps  %xmm0, 0x60(%rbx)
0000000040068f26    movaps  %xmm0, 0x50(%rbx)
0000000040068f2a    movaps  %xmm0, 0x40(%rbx)

There are some pretty deep assumptions made in the TLSF allocator around 4 and 8 byte alignment so the allocator may need to be changed for a more flexible and thread-safe cross platform allocator.

michaeljclark commented 8 years ago

The TLSF library has been changed for Doug Lea's dlmalloc

https://github.com/michaeljclark/riscv-meta/commit/409730fb78310c85697aa4edc3667f10c27d9167