Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions so3/arch/arm64/include/asm/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ static inline int spin_trylock(spinlock_t *lock)
{
uint32_t tmp;

__asm__ __volatile__(" ldaxr %w0, [%1]\n"
" tbnz %w0, #0, 1f\n"
/* The spinlock must aligned in aarch64 */
BUG_ON((((uint64_t)&lock->lock) & 0x7) != 0);
__asm__ __volatile__(" ldaxr %x0, [%1]\n"
" cbnz %x0, 1f\n"
" stxr %w0, %2, [%1]\n"
"1:"
: "=&r"(tmp)
: "r"(&lock->lock), "r"(1)
: "cc");
: "r"(&lock->lock), "r"(1ULL)
: "cc", "memory");

if (tmp == 0) {
smp_mb();
Expand All @@ -66,10 +68,10 @@ static inline void spin_unlock(spinlock_t *lock)
{
smp_mb();

__asm__ __volatile__(" stlr wzr, [%0]\n"
__asm__ __volatile__(" stlr xzr, [%0]\n"
" sev"
:
: "r"(&lock->lock)
: "cc");
: "cc", "memory");
}
#endif /* ASM_SPINLOCK_H */
8 changes: 4 additions & 4 deletions so3/include/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
#include <asm/atomic.h>

struct mutex {
/* 1: unlocked, 0: locked, negative: locked, possible waiters */
atomic_t count;
tcb_t *owner;
spinlock_t wait_lock;
/* 1: unlocked, 0: locked, negative: locked, possible waiters */
atomic_t count;

/* Allow to manage recursive locking */
uint32_t recursive_count;
Expand All @@ -58,7 +58,7 @@ void mutex_unlock(struct mutex *lock);
void mutex_init(struct mutex *lock);

int do_mutex_init(void);
int do_mutex_lock(mutex_t *lock);
int do_mutex_unlock(mutex_t *lock);
int do_mutex_lock(unsigned long number);
int do_mutex_unlock(unsigned long number);

#endif /* MUTEX_H */
4 changes: 2 additions & 2 deletions so3/include/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#define PROC_STACK_SIZE (PROC_THREAD_MAX * THREAD_STACK_SIZE)

#define FD_MAX 64
#define N_MUTEX 10
#define N_MUTEX 5

typedef enum {
PROC_STATE_NEW,
Expand Down Expand Up @@ -126,7 +126,7 @@ struct pcb {
enum __ptrace_request ptrace_pending_req;

/* Mutex lock to be used in conjunction with the user space (very temporary) */
mutex_t lock[N_MUTEX];
mutex_t *lock;
};
typedef struct pcb pcb_t;

Expand Down
13 changes: 12 additions & 1 deletion so3/include/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,24 @@

#include <asm/processor.h>

#ifdef CONFIG_ARCH_ARM32
typedef struct {
volatile uint32_t lock;
} spinlock_t;

#elif CONFIG_ARCH_ARM64

/*
* On Aarch64, the field has to be 64-bit aligned apparently.
*/
typedef struct {
__attribute__((aligned(8))) volatile uint32_t lock;
volatile uint64_t lock;
} spinlock_t;

#else
#error "Invalid ARCH config"
#endif

#include <asm/spinlock.h>

#define DEFINE_SPINLOCK(l) spinlock_t l = { 0 };
Expand Down
19 changes: 13 additions & 6 deletions so3/kernel/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*
*/

#include <errno.h>
#include <mutex.h>
#include <schedule.h>
#include <string.h>
Expand Down Expand Up @@ -149,17 +150,23 @@ void mutex_unlock(struct mutex *lock)
/*
* The following syscall implementation are a first attempt, mainly used for debugging kernel mutexes.
*/
int do_mutex_lock(mutex_t *lock)
int do_mutex_lock(unsigned long number)
{
mutex_lock(lock);

if (number >= N_MUTEX) {
set_errno(EINVAL);
return -1;
}
mutex_lock(&current()->pcb->lock[number]);
return 0;
}

int do_mutex_unlock(mutex_t *lock)
int do_mutex_unlock(unsigned long number)
{
mutex_unlock(lock);

if (number >= N_MUTEX) {
set_errno(EINVAL);
return -1;
}
mutex_unlock(&current()->pcb->lock[number]);
return 0;
}

Expand Down
13 changes: 11 additions & 2 deletions so3/kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,19 @@ pcb_t *new_process(void)
/* Reset the ptrace request indicator */
pcb->ptrace_pending_req = PTRACE_NO_REQUEST;

/* The spinlock inside the mutex must aligned in aarch64 */
pcb->lock = memalign(N_MUTEX * sizeof(mutex_t), 8);

if (!pcb->lock) {
printk("%s: failed to allocate memory for process mutex\n",
__func__);
kernel_panic();
}

/* Initialize the mutex belonging to this process */
for (i = 0; i < N_MUTEX; i++)
for (i = 0; i < N_MUTEX; i++) {
mutex_init(&pcb->lock[i]);

}
/* Init the list of pages */
INIT_LIST_HEAD(&pcb->page_list);

Expand Down
Loading