2020-05-09 12:04:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-09 12:04:14 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-08 11:34:33 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2020-05-16 10:00:04 +00:00
|
|
|
#if defined(KERNEL)
|
|
|
|
# include <Kernel/StdLib.h>
|
2020-03-08 11:34:33 +00:00
|
|
|
#else
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE void fast_u32_copy(u32* dest, const u32* src, size_t count)
|
2020-03-08 11:34:33 +00:00
|
|
|
{
|
2022-01-25 21:41:26 +00:00
|
|
|
#if ARCH(I386) || ARCH(X86_64)
|
2020-03-08 11:34:33 +00:00
|
|
|
asm volatile(
|
|
|
|
"rep movsl\n"
|
2020-07-27 13:54:39 +00:00
|
|
|
: "+S"(src), "+D"(dest), "+c"(count)::"memory");
|
2021-05-31 15:25:40 +00:00
|
|
|
#else
|
|
|
|
__builtin_memcpy(dest, src, count * 4);
|
|
|
|
#endif
|
2020-03-08 11:34:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
ALWAYS_INLINE void fast_u32_fill(u32* dest, u32 value, size_t count)
|
2020-03-08 11:34:33 +00:00
|
|
|
{
|
2022-01-25 21:41:26 +00:00
|
|
|
#if ARCH(I386) || ARCH(X86_64)
|
2020-03-08 11:34:33 +00:00
|
|
|
asm volatile(
|
|
|
|
"rep stosl\n"
|
|
|
|
: "=D"(dest), "=c"(count)
|
|
|
|
: "D"(dest), "c"(count), "a"(value)
|
|
|
|
: "memory");
|
2021-05-31 15:25:40 +00:00
|
|
|
#else
|
|
|
|
for (auto* p = dest; p < (dest + count); ++p) {
|
|
|
|
*p = value;
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-08 11:34:33 +00:00
|
|
|
}
|
2021-09-12 21:26:59 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
inline void secure_zero(void* ptr, size_t size)
|
|
|
|
{
|
|
|
|
__builtin_memset(ptr, 0, size);
|
|
|
|
// The memory barrier is here to avoid the compiler optimizing
|
|
|
|
// away the memset when we rely on it for wiping secrets.
|
|
|
|
asm volatile("" ::
|
|
|
|
: "memory");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::secure_zero;
|