|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Institute of Parallel And Distributed Systems (IPADS), |
| 3 | + * Shanghai Jiao Tong University (SJTU) OS-Lab-2020 (i.e., ChCore) is licensed |
| 4 | + * under the Mulan PSL v1. You can use this software according to the terms and |
| 5 | + * conditions of the Mulan PSL v1. You may obtain a copy of Mulan PSL v1 at: |
| 6 | + * http://license.coscl.org.cn/MulanPSL |
| 7 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY |
| 8 | + * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
| 9 | + * NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the |
| 10 | + * Mulan PSL v1 for more details. |
| 11 | + */ |
| 12 | + |
| 13 | +#include <common/smp.h> |
| 14 | +#include <common/sync.h> |
| 15 | +#include <common/errno.h> |
| 16 | +#include <common/kprint.h> |
| 17 | +#include <common/lock.h> |
| 18 | +#include <common/macro.h> |
| 19 | +#include <common/types.h> |
| 20 | + |
| 21 | +struct lock big_kernel_lock; |
| 22 | + |
| 23 | +int lock_init(struct lock *lock) |
| 24 | +{ |
| 25 | + BUG_ON(!lock); |
| 26 | + /* Initialize ticket lock */ |
| 27 | + lock->owner = 0; |
| 28 | + lock->next = 0; |
| 29 | + return 0; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Lock the ticket lock |
| 34 | + * This function will block until the lock is held |
| 35 | + */ |
| 36 | +void lock(struct lock *lock) |
| 37 | +{ |
| 38 | + u32 lockval = 0, newval = 0, ret = 0; |
| 39 | + |
| 40 | + BUG_ON(!lock); |
| 41 | + |
| 42 | + /** |
| 43 | + * The following asm code means: |
| 44 | + * |
| 45 | + * lock->next = fetch_and_add(1); |
| 46 | + * while(lock->next != lock->owner); |
| 47 | + */ |
| 48 | + asm volatile (" prfm pstl1strm, %3\n" |
| 49 | + "1: ldaxr %w0, %3\n" |
| 50 | + " add %w1, %w0, #0x1\n" |
| 51 | + " stxr %w2, %w1, %3\n" |
| 52 | + " cbnz %w2, 1b\n" |
| 53 | + "2: ldar %w2, %4\n" |
| 54 | + " cmp %w0, %w2\n" |
| 55 | + " b.ne 2b\n":"=&r" (lockval), "=&r"(newval), |
| 56 | + "=&r"(ret), "+Q"(lock->next) |
| 57 | + :"Q"(lock->owner) |
| 58 | + :"memory"); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Try to lock the ticket lock |
| 63 | + * Return 0 if succeed, -1 otherwise |
| 64 | + */ |
| 65 | +int try_lock(struct lock *lock) |
| 66 | +{ |
| 67 | + |
| 68 | + u32 lockval = 0, newval = 0, ret = 0, ownerval = 0; |
| 69 | + |
| 70 | + BUG_ON(!lock); |
| 71 | + asm volatile (" prfm pstl1strm, %4\n" " ldaxr %w0, %4\n" " ldar %w3, %5\n" " add %w1, %w0, #0x1\n" " cmp %w0, %w3\n" " b.ne 1f\n" " stxr %w2, %w1, %4\n" " cbz %w2, 2f\n" "1: mov %w2, #0xffffffffffffffff\n" /* fail */ |
| 72 | + " b 3f\n" "2: mov %w2, #0x0\n" /* success */ |
| 73 | + " dmb ish\n" /* barrier */ |
| 74 | + "3:\n":"=&r" (lockval), "=&r"(newval), "=&r"(ret), |
| 75 | + "=&r"(ownerval), "+Q"(lock->next) |
| 76 | + :"Q"(lock->owner) |
| 77 | + :"memory"); |
| 78 | + return ret; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Unlock the ticket lock |
| 83 | + */ |
| 84 | +void unlock(struct lock *lock) |
| 85 | +{ |
| 86 | + BUG_ON(!lock); |
| 87 | + asm volatile ("dmb ish"); |
| 88 | + |
| 89 | + /** |
| 90 | + * Unlock the ticket lock here |
| 91 | + * Your code should be no more than 5 lines |
| 92 | + */ |
| 93 | + lock->owner++; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Check whether the ticket lock is locked |
| 98 | + * Return 1 if locked, 0 otherwise |
| 99 | + * Your code should be no more than 5 lines |
| 100 | + */ |
| 101 | +int is_locked(struct lock *lock) |
| 102 | +{ |
| 103 | + return lock->owner != lock->next; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Initialization of the big kernel lock |
| 108 | + */ |
| 109 | +void kernel_lock_init(void) |
| 110 | +{ |
| 111 | + lock_init(&big_kernel_lock); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Acquire the big kernel lock |
| 116 | + */ |
| 117 | +void lock_kernel(void) |
| 118 | +{ |
| 119 | + lock(&big_kernel_lock); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Release the big kernel lock |
| 124 | + */ |
| 125 | +void unlock_kernel(void) |
| 126 | +{ |
| 127 | + unlock(&big_kernel_lock); |
| 128 | +} |
0 commit comments