Skip to content

Commit

Permalink
Correct LR/SC implementation
Browse files Browse the repository at this point in the history
LR/SC needs to ensure the address is valid and the value does
not change. In the original implementation, it only checks
if the address is valid.
  • Loading branch information
ranvd committed Jun 24, 2024
1 parent cfdb547 commit c370e33
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,10 @@ static void mmu_load(vm_t *vm,
if (vm->error)
return;

if (unlikely(reserved))
if (unlikely(reserved)) {
vm->lr_reservation = addr | 1;
vm->lr_val = *value;
}
}

static bool mmu_store(vm_t *vm,
Expand All @@ -328,8 +330,11 @@ static bool mmu_store(vm_t *vm,
return false;

if (unlikely(cond)) {
if (vm->lr_reservation != (addr | 1))
uint32_t cas_value;
vm->mem_load(vm, addr, width, &cas_value);
if ((vm->lr_reservation != (addr | 1)) || vm->lr_val != cas_value)
return false;

vm->lr_reservation = 0;
} else {
if (unlikely(vm->lr_reservation & 1) &&
Expand Down
1 change: 1 addition & 0 deletions riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct __vm_internal {

/* LR reservation virtual address. last bit is 1 if valid */
uint32_t lr_reservation;
uint32_t lr_val;

/* Assumed to contain an aligned address at all times */
uint32_t pc;
Expand Down

0 comments on commit c370e33

Please sign in to comment.