SafeMem.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Atomic.h>
  7. #include <AK/Optional.h>
  8. #include <AK/Types.h>
  9. #pragma once
  10. namespace Kernel {
  11. struct RegisterState;
  12. [[nodiscard]] bool safe_memcpy(void* dest_ptr, const void* src_ptr, size_t n, void*& fault_at) __attribute__((used));
  13. [[nodiscard]] ssize_t safe_strnlen(const char* str, size_t max_n, void*& fault_at) __attribute__((used));
  14. [[nodiscard]] bool safe_memset(void* dest_ptr, int c, size_t n, void*& fault_at) __attribute__((used));
  15. [[nodiscard]] Optional<u32> safe_atomic_fetch_add_relaxed(volatile u32* var, u32 val) __attribute__((used));
  16. [[nodiscard]] Optional<u32> safe_atomic_exchange_relaxed(volatile u32* var, u32 val) __attribute__((used));
  17. [[nodiscard]] Optional<u32> safe_atomic_load_relaxed(volatile u32* var) __attribute__((used));
  18. [[nodiscard]] bool safe_atomic_store_relaxed(volatile u32* var, u32 val) __attribute__((used));
  19. [[nodiscard]] Optional<bool> safe_atomic_compare_exchange_relaxed(volatile u32* var, u32& expected, u32 val) __attribute__((used));
  20. [[nodiscard]] ALWAYS_INLINE Optional<u32> safe_atomic_fetch_and_relaxed(volatile u32* var, u32 val)
  21. {
  22. auto expected_value = safe_atomic_load_relaxed(var);
  23. if (!expected_value.has_value())
  24. return {}; // fault
  25. u32& expected = expected_value.value();
  26. for (;;) {
  27. auto result = safe_atomic_compare_exchange_relaxed(var, expected, expected & val);
  28. if (!result.has_value())
  29. return {}; // fault
  30. if (result.value())
  31. return expected; // exchanged
  32. // This is only so that we don't saturate the bus...
  33. AK::atomic_thread_fence(AK::MemoryOrder::memory_order_acquire);
  34. }
  35. }
  36. [[nodiscard]] ALWAYS_INLINE Optional<u32> safe_atomic_fetch_and_not_relaxed(volatile u32* var, u32 val)
  37. {
  38. auto expected_value = safe_atomic_load_relaxed(var);
  39. if (!expected_value.has_value())
  40. return {}; // fault
  41. u32& expected = expected_value.value();
  42. for (;;) {
  43. auto result = safe_atomic_compare_exchange_relaxed(var, expected, expected & ~val);
  44. if (!result.has_value())
  45. return {}; // fault
  46. if (result.value())
  47. return expected; // exchanged
  48. // This is only so that we don't saturate the bus...
  49. AK::atomic_thread_fence(AK::MemoryOrder::memory_order_acquire);
  50. }
  51. }
  52. [[nodiscard]] ALWAYS_INLINE Optional<u32> safe_atomic_fetch_or_relaxed(volatile u32* var, u32 val)
  53. {
  54. auto expected_value = safe_atomic_load_relaxed(var);
  55. if (!expected_value.has_value())
  56. return {}; // fault
  57. u32& expected = expected_value.value();
  58. for (;;) {
  59. auto result = safe_atomic_compare_exchange_relaxed(var, expected, expected | val);
  60. if (!result.has_value())
  61. return {}; // fault
  62. if (result.value())
  63. return expected; // exchanged
  64. // This is only so that we don't saturate the bus...
  65. AK::atomic_thread_fence(AK::MemoryOrder::memory_order_acquire);
  66. }
  67. }
  68. [[nodiscard]] ALWAYS_INLINE Optional<u32> safe_atomic_fetch_xor_relaxed(volatile u32* var, u32 val)
  69. {
  70. auto expected_value = safe_atomic_load_relaxed(var);
  71. if (!expected_value.has_value())
  72. return {}; // fault
  73. u32& expected = expected_value.value();
  74. for (;;) {
  75. auto result = safe_atomic_compare_exchange_relaxed(var, expected, expected ^ val);
  76. if (!result.has_value())
  77. return {}; // fault
  78. if (result.value())
  79. return expected; // exchanged
  80. // This is only so that we don't saturate the bus...
  81. AK::atomic_thread_fence(AK::MemoryOrder::memory_order_acquire);
  82. }
  83. }
  84. bool handle_safe_access_fault(RegisterState& regs, FlatPtr fault_address);
  85. }