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 | ((uint32_t)hal_get_cycles() > DEBUG_BUSYLOCK) ) |
---|
91 | { |
---|
92 | xptr_t root_xp = XPTR( local_cxy , &this->busylocks_root ); |
---|
93 | |
---|
94 | // update thread list of busyslocks |
---|
95 | xlist_add_last( root_xp , XPTR( lock_cxy , &lock_ptr->xlist ) ); |
---|
96 | } |
---|
97 | #endif |
---|
98 | |
---|
99 | #if( DEBUG_BUSYLOCK && DEBUG_BUSYLOCK_THREAD_XP ) |
---|
100 | if( (type != LOCK_CHDEV_TXT0) && |
---|
101 | (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) ) |
---|
102 | { |
---|
103 | printk("\n[%s] thread[%x,%x] ACQUIRE lock %s\n", |
---|
104 | __FUNCTION__, this->process->pid, this->trdid, lock_type_str[type] ); |
---|
105 | } |
---|
106 | #endif |
---|
107 | |
---|
108 | } // end remote_busylock_acquire() |
---|
109 | |
---|
110 | /////////////////////////////////////////////// |
---|
111 | void remote_busylock_release( xptr_t lock_xp ) |
---|
112 | { |
---|
113 | thread_t * this = CURRENT_THREAD; |
---|
114 | |
---|
115 | // memory barrier to update the protected object |
---|
116 | hal_fence(); |
---|
117 | |
---|
118 | // get remote lock cluster and local pointer |
---|
119 | cxy_t lock_cxy = GET_CXY( lock_xp ); |
---|
120 | busylock_t * lock_ptr = GET_PTR( lock_xp ); |
---|
121 | |
---|
122 | // update lock state |
---|
123 | hal_remote_atomic_add( XPTR( lock_cxy , &lock_ptr->current ) , 1 ); |
---|
124 | |
---|
125 | // decrement thread locks counter |
---|
126 | this->busylocks--; |
---|
127 | |
---|
128 | // memory barrier to update busylock and thread state |
---|
129 | hal_fence(); |
---|
130 | |
---|
131 | #if DEBUG_BUSYLOCK |
---|
132 | uint32_t type = hal_remote_l32( XPTR( lock_cxy , &lock_ptr->type ) ); |
---|
133 | if( (type != LOCK_CHDEV_TXT0) && |
---|
134 | (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) && |
---|
135 | ((uint32_t)hal_get_cycles() > DEBUG_BUSYLOCK) ) |
---|
136 | { |
---|
137 | // remove lock from thread list of busyslocks |
---|
138 | xlist_unlink( XPTR( lock_cxy , &lock_ptr->xlist ) ); |
---|
139 | } |
---|
140 | #endif |
---|
141 | |
---|
142 | #if (DEBUG_BUSYLOCK && DEBUG_BUSYLOCK_THREAD_XP ) |
---|
143 | if( (type != LOCK_CHDEV_TXT0) && |
---|
144 | (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) ) |
---|
145 | { |
---|
146 | printk("\n[%s] thread[%x,%x] RELEASE lock %s\n", |
---|
147 | __FUNCTION__, this->process->pid, this->trdid, lock_type_str[type] ); |
---|
148 | } |
---|
149 | #endif |
---|
150 | |
---|
151 | // exit critical section |
---|
152 | hal_restore_irq( hal_remote_l32( XPTR( lock_cxy , &lock_ptr->save_sr ) ) ); |
---|
153 | |
---|
154 | } // end remote_busylock_release() |
---|
155 | |
---|
156 | |
---|