From bf63a077b14cec4f0319fa3a611ef1b16c9c9f3d Mon Sep 17 00:00:00 2001 From: TuxSH Date: Fri, 2 Mar 2018 23:32:50 +0100 Subject: [PATCH] Implement critical section (Lamport's Baker s algorithm)... thanks @fincs for sample code --- exosphere/src/lock.h | 26 -------- exosphere/src/smc_api.c | 2 +- exosphere/src/synchronization.h | 115 ++++++++++++++++++++++++++++++++ exosphere/src/utils.h | 1 - 4 files changed, 116 insertions(+), 28 deletions(-) delete mode 100644 exosphere/src/lock.h create mode 100644 exosphere/src/synchronization.h diff --git a/exosphere/src/lock.h b/exosphere/src/lock.h deleted file mode 100644 index e122de503..000000000 --- a/exosphere/src/lock.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef EXOSPHERE_LOCK_H -#define EXOSPHERE_LOCK_H - -#include -#include - -/* Simple atomics driver for Exosphere. */ - -/* Acquire a lock. */ -static inline void lock_acquire(atomic_flag *flag) { - while (atomic_flag_test_and_set_explicit(flag, memory_order_acquire)) { - /* Wait to acquire lock. */ - } -} - -/* Release a lock. */ -static inline void lock_release(atomic_flag *flag) { - atomic_flag_clear_explicit(flag, memory_order_release); -} - -/* Try to acquire a lock. */ -static inline bool lock_try_acquire(atomic_flag *flag) { - return atomic_flag_test_and_set_explicit(flag, memory_order_acquire); -} - -#endif \ No newline at end of file diff --git a/exosphere/src/smc_api.c b/exosphere/src/smc_api.c index 7ba07486a..464a28c58 100644 --- a/exosphere/src/smc_api.c +++ b/exosphere/src/smc_api.c @@ -6,7 +6,7 @@ #include "configitem.h" #include "cpu_context.h" -#include "lock.h" +#include "synchronization.h" #include "masterkey.h" #include "mc.h" #include "memory_map.h" diff --git a/exosphere/src/synchronization.h b/exosphere/src/synchronization.h new file mode 100644 index 000000000..0f16f5680 --- /dev/null +++ b/exosphere/src/synchronization.h @@ -0,0 +1,115 @@ +#ifndef EXOSPHERE_SYNCHRONIZATION_H +#define EXOSPHERE_SYNCHRONIZATION_H + +#include +#include "utils.h" + +/* Simple atomics driver for Exosphere. */ + +typedef struct { + struct { + uint8_t ticket_number : 7; + uint8_t is_entering : 1; + } customers[4]; +} critical_section_t; + +static inline void __dsb_sy(void) { + __asm__ __volatile__ ("dsb sy" ::: "memory"); +} + +static inline void __dmb_sy(void) { + __asm__ __volatile__ ("dmb sy" ::: "memory"); +} + +static inline void __sev(void) { + __asm__ __volatile__ ("sev"); +} + +static inline void __sevl(void) { + __asm__ __volatile__ ("sevl"); +} + +static inline void __wfe(void) { + __asm__ __volatile__ ("wfe"); +} + +/* Acquire a lock. */ +static inline void lock_acquire(atomic_flag *flag) { + while (atomic_flag_test_and_set_explicit(flag, memory_order_acquire)) { + /* Wait to acquire lock. */ + } +} + +/* Release a lock. */ +static inline void lock_release(atomic_flag *flag) { + atomic_flag_clear_explicit(flag, memory_order_release); +} + +/* Try to acquire a lock. */ +static inline bool lock_try_acquire(atomic_flag *flag) { + return atomic_flag_test_and_set_explicit(flag, memory_order_acquire); +} + +/* + Enter a critical section, using the Lamport's bakery algorithm. + https://en.wikipedia.org/wiki/Lamport%27s_bakery_algorithm + + This is invoked on warmboot before the MMU is turned on, therefore + exclusive load/store instructions can't be used. + + Note: Nintendo has tried to implement that algorithm, but it seems that + they didn't understand how it works, and their implementation + is therefore a complete failure: in particular, the "ticket number" is + always the same (core0 will always enter the critical section before all the cores, + and so on...), and thus there can be starvation, etc. + + Nintendo, wtf. +*/ +ALINLINE static inline unsigned int critical_section_enter(volatile critical_section_t *critical_section) { + unsigned int id = get_core_id(); + uint8_t my_ticket_number = 0; + uint8_t tmp; + + critical_section->customers[id].is_entering = 1; + for (unsigned int i = 0; i < 4; i++) { + tmp = critical_section->customers[id].ticket_number; + my_ticket_number = tmp > my_ticket_number ? tmp : my_ticket_number; + } + + critical_section->customers[id].ticket_number = ++my_ticket_number; + critical_section->customers[id].is_entering = 0; + __dsb_sy(); + __sev(); + + for (unsigned int i = 0; i < 4; i++) { + __sevl(); + do { + __wfe(); + } while (critical_section->customers[i].is_entering); + + __sevl(); + do { + __wfe(); + tmp = critical_section->customers[i].ticket_number; + } while (tmp && (tmp < my_ticket_number || (tmp == my_ticket_number && i < id))); + } + + __dmb_sy(); + return id; +} + +/* + Leaves a critical section, using the Lamport's bakery algorithm (see above). + + Note: Nintendo failed even that: they're clearing the entire critical section state + instead of just the counter associated to the current core. + + Nintendo, wtf. +*/ +ALINLINE static inline void critical_section_leave(volatile critical_section_t *critical_section) { + critical_section->customers[get_core_id()].ticket_number = 0; + __dsb_sy(); + __sev(); +} + +#endif diff --git a/exosphere/src/utils.h b/exosphere/src/utils.h index 99dff13a6..0aadc1172 100644 --- a/exosphere/src/utils.h +++ b/exosphere/src/utils.h @@ -16,7 +16,6 @@ #define PACKED __attribute__((packed)) #define ALINLINE __attribute__((always_inline)) -#define FAR_REACHING __attribute__((target("cmodel=large"), noinline)) /* Custom stuff below */