1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-23 12:22:08 +00:00

Implement critical section (Lamport's Baker s algorithm)...

thanks @fincs for sample code
This commit is contained in:
TuxSH 2018-03-02 23:32:50 +01:00
parent 301b166684
commit bf63a077b1
4 changed files with 116 additions and 28 deletions

View file

@ -1,26 +0,0 @@
#ifndef EXOSPHERE_LOCK_H
#define EXOSPHERE_LOCK_H
#include <stdatomic.h>
#include <stdbool.h>
/* 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

View file

@ -6,7 +6,7 @@
#include "configitem.h" #include "configitem.h"
#include "cpu_context.h" #include "cpu_context.h"
#include "lock.h" #include "synchronization.h"
#include "masterkey.h" #include "masterkey.h"
#include "mc.h" #include "mc.h"
#include "memory_map.h" #include "memory_map.h"

View file

@ -0,0 +1,115 @@
#ifndef EXOSPHERE_SYNCHRONIZATION_H
#define EXOSPHERE_SYNCHRONIZATION_H
#include <stdatomic.h>
#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

View file

@ -16,7 +16,6 @@
#define PACKED __attribute__((packed)) #define PACKED __attribute__((packed))
#define ALINLINE __attribute__((always_inline)) #define ALINLINE __attribute__((always_inline))
#define FAR_REACHING __attribute__((target("cmodel=large"), noinline))
/* Custom stuff below */ /* Custom stuff below */