libpayload: Support unaligned pointers for memset

The optimization of the memset() function introduced by commit
dbadb1dd63 (libpayload: Reorder default
memcpy, speed up memset and memcmp) is provoking an issue on x86
platform when compiling without the CONFIG_GPL option.

GCC is making use of the movdqa instruction to copy words.  This
instruction can raise a "General Protection Fault Exception" when it
is called on a non-aligned address argument.

Change-Id: I73382a76a4399d8e78244867f2ebb1dca176a6bf
Signed-off-by: Jeremy Compostella <jeremy.compostella@intel.com>
Reviewed-on: https://review.coreboot.org/20524
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
This commit is contained in:
Jeremy Compostella 2017-07-10 17:21:51 -07:00 committed by Nico Huber
parent 3d38448619
commit 54db255529
1 changed files with 5 additions and 0 deletions

View File

@ -38,6 +38,11 @@ static void *default_memset(void *s, int c, size_t n)
size_t i; size_t i;
void *ret = s; void *ret = s;
unsigned long w = c & 0xff; unsigned long w = c & 0xff;
u8 *p = s;
s = (void *)ALIGN_UP((uintptr_t)s, sizeof(unsigned long));
while (p != (u8 *)s && n--)
*p++ = c;
for (i = 1; i < sizeof(unsigned long); i <<= 1) for (i = 1; i < sizeof(unsigned long); i <<= 1)
w = (w << (i * 8)) | w; w = (w << (i * 8)) | w;