[563] | 1 | /* |
---|
| 2 | * remote_busylock.c - remote kernel busy-waiting lock implementation. |
---|
| 3 | * |
---|
| 4 | * Authors Alain Greiner (2016,2017,2018) |
---|
| 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include <kernel_config.h> |
---|
| 25 | #include <hal_kernel_types.h> |
---|
| 26 | #include <hal_irqmask.h> |
---|
| 27 | #include <hal_special.h> |
---|
| 28 | #include <hal_remote.h> |
---|
| 29 | #include <thread.h> |
---|
| 30 | #include <remote_busylock.h> |
---|
| 31 | |
---|
| 32 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 33 | // Extern global variables |
---|
| 34 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 35 | |
---|
| 36 | extern char * lock_type_str[]; // allocated in kernel_init.c |
---|
| 37 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | //////////////////////////////////////////// |
---|
| 41 | void remote_busylock_init( xptr_t lock_xp, |
---|
| 42 | uint32_t type ) |
---|
| 43 | { |
---|
| 44 | // get remote lock cluster and local pointer |
---|
| 45 | cxy_t lock_cxy = GET_CXY( lock_xp ); |
---|
| 46 | busylock_t * lock_ptr = GET_PTR( lock_xp ); |
---|
| 47 | |
---|
| 48 | hal_remote_s32( XPTR( lock_cxy , &lock_ptr->ticket ) , 0 ); |
---|
| 49 | hal_remote_s32( XPTR( lock_cxy , &lock_ptr->current ) , 0 ); |
---|
| 50 | hal_remote_s32( XPTR( lock_cxy , &lock_ptr->type ) , type ); |
---|
| 51 | |
---|
| 52 | #if DEBUG_BUSYLOCK |
---|
| 53 | xlist_entry_init( XPTR( lock_cxy , &lock_ptr->xlist ) ); |
---|
| 54 | #endif |
---|
| 55 | |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | //////////////////////////////////////////////// |
---|
| 59 | void remote_busylock_acquire( xptr_t lock_xp ) |
---|
| 60 | { |
---|
| 61 | reg_t save_sr; |
---|
| 62 | thread_t * this = CURRENT_THREAD; |
---|
| 63 | |
---|
| 64 | // get remote lock cluster and local pointer |
---|
| 65 | cxy_t lock_cxy = GET_CXY( lock_xp ); |
---|
| 66 | busylock_t * lock_ptr = GET_PTR( lock_xp ); |
---|
| 67 | |
---|
| 68 | // enter critical section |
---|
| 69 | hal_disable_irq( &save_sr ); |
---|
| 70 | |
---|
| 71 | // get one ticket |
---|
| 72 | uint32_t ticket = hal_remote_atomic_add( XPTR( lock_cxy , &lock_ptr->ticket ) , 1 ); |
---|
| 73 | |
---|
| 74 | // poll current until success |
---|
| 75 | xptr_t current_xp = XPTR( lock_cxy , &lock_ptr->current ); |
---|
| 76 | while( hal_remote_l32( current_xp ) != ticket ) asm volatile ("nop"); |
---|
| 77 | |
---|
| 78 | // increment thread locks counter |
---|
| 79 | this->busylocks++; |
---|
| 80 | |
---|
| 81 | // save SR in lock descriptor |
---|
| 82 | hal_remote_s32( XPTR( lock_cxy , &lock_ptr->save_sr ) , save_sr ); |
---|
| 83 | |
---|
| 84 | // memory barrier to update busylock and thread state |
---|
| 85 | hal_fence(); |
---|
| 86 | |
---|
| 87 | #if DEBUG_BUSYLOCK |
---|
| 88 | uint32_t type = hal_remote_l32( XPTR( lock_cxy , &lock_ptr->type ) ); |
---|
| 89 | if( (type != LOCK_CHDEV_TXT0) && |
---|
| 90 | (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) && |
---|
| 91 | ((uint32_t)hal_get_cycles() > DEBUG_BUSYLOCK) ) |
---|
| 92 | { |
---|
| 93 | xptr_t root_xp = XPTR( local_cxy , &this->busylocks_root ); |
---|
| 94 | |
---|
| 95 | // update thread list of busyslocks |
---|
| 96 | xlist_add_last( root_xp , XPTR( lock_cxy , &lock_ptr->xlist ) ); |
---|
| 97 | |
---|
| 98 | // display busylocks for the selected thread (acquire) |
---|
| 99 | thread_display_busylocks( type , true ); |
---|
| 100 | } |
---|
| 101 | #endif |
---|
| 102 | |
---|
| 103 | } // end remote_busylock_acquire() |
---|
| 104 | |
---|
| 105 | /////////////////////////////////////////////// |
---|
| 106 | void remote_busylock_release( xptr_t lock_xp ) |
---|
| 107 | { |
---|
| 108 | thread_t * this = CURRENT_THREAD; |
---|
| 109 | |
---|
| 110 | // memory barrier to update the protected object |
---|
| 111 | hal_fence(); |
---|
| 112 | |
---|
| 113 | // get remote lock cluster and local pointer |
---|
| 114 | cxy_t lock_cxy = GET_CXY( lock_xp ); |
---|
| 115 | busylock_t * lock_ptr = GET_PTR( lock_xp ); |
---|
| 116 | |
---|
| 117 | // update lock state |
---|
| 118 | hal_remote_atomic_add( XPTR( lock_cxy , &lock_ptr->current ) , 1 ); |
---|
| 119 | |
---|
| 120 | // decrement thread locks counter |
---|
| 121 | this->busylocks--; |
---|
| 122 | |
---|
| 123 | // memory barrier to update busylock and thread state |
---|
| 124 | hal_fence(); |
---|
| 125 | |
---|
| 126 | #if DEBUG_BUSYLOCK |
---|
| 127 | uint32_t type = hal_remote_l32( XPTR( lock_cxy , &lock_ptr->type ) ); |
---|
| 128 | if( (type != LOCK_CHDEV_TXT0) && |
---|
| 129 | (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) && |
---|
| 130 | ((uint32_t)hal_get_cycles() > DEBUG_BUSYLOCK) ) |
---|
| 131 | { |
---|
| 132 | // remove lock from thread list of busyslocks |
---|
| 133 | xlist_unlink( XPTR( lock_cxy , &lock_ptr->xlist ) ); |
---|
| 134 | |
---|
| 135 | // display busylocks for the selected thread (release) |
---|
| 136 | thread_display_busylocks( type , false ); |
---|
| 137 | } |
---|
| 138 | #endif |
---|
| 139 | |
---|
| 140 | // exit critical section |
---|
| 141 | hal_restore_irq( hal_remote_l32( XPTR( lock_cxy , &lock_ptr->save_sr ) ) ); |
---|
| 142 | |
---|
| 143 | } // end remote_busylock_release() |
---|
| 144 | |
---|
| 145 | |
---|