source: trunk/kernel/libk/remote_buf.c@ 690

Last change on this file since 690 was 683, checked in by alain, 6 years ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

File size: 13.0 KB
RevLine 
[657]1/*
2 * remote_buf.c Remotely accessible, circular buffer implementation.
3 *
4 * Authors : Alain Greiner (2016,2017,2018,2019,2020)
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 <hal_kernel_types.h>
25#include <hal_irqmask.h>
26#include <hal_remote.h>
27#include <hal_uspace.h>
28#include <bits.h>
29#include <memcpy.h>
30#include <kmem.h>
31#include <remote_buf.h>
32
[671]33/////////////////////////////////////////////
34remote_buf_t * remote_buf_alloc( cxy_t cxy )
35{
[683]36 return kmem_remote_alloc( cxy,
37 bits_log2(sizeof(remote_buf_t)),
38 AF_ZERO );
[671]39}
40
[666]41/////////////////////////////////////////
42error_t remote_buf_init( xptr_t buf_xp,
[671]43 uint32_t order )
[657]44{
[671]45
46assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
47assert( __FUNCTION__ , (order < 32) , "order cannot be larger than 31" );
48
[657]49 uint8_t * data;
50
51 remote_buf_t * buf_ptr = GET_PTR( buf_xp );
52 cxy_t buf_cxy = GET_CXY( buf_xp );
53
54 // allocate the data buffer
[683]55 data = kmem_remote_alloc( buf_cxy , order , AF_NONE );
[666]56
[683]57 if( data == NULL ) return -1;
[666]58
[657]59 // initialize buffer descriptor
[671]60 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->order ) , order );
61 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->wid ) , 0 );
62 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->rid ) , 0 );
63 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->sts ) , 0 );
64 hal_remote_spt( XPTR( buf_cxy , &buf_ptr->data ) , data );
[657]65
66 return 0;
67
[666]68} // end remote_buf_init()
[657]69
[671]70//////////////////////////////////////////////
71void remote_buf_release_data( xptr_t buf_xp )
72{
73
74assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
75
76 remote_buf_t * buf_ptr = GET_PTR( buf_xp );
77 cxy_t buf_cxy = GET_CXY( buf_xp );
78
79 // gets data buffer local pointer and order
80 uint32_t order = hal_remote_l32( XPTR( buf_cxy , &buf_ptr->order ));
81 char * data_ptr = hal_remote_lpt( XPTR( buf_cxy , &buf_ptr->data ));
82
83 // release memory allocated for data buffer if required
[683]84 if( data_ptr != NULL ) kmem_remote_free( buf_cxy , data_ptr , order );
85
[671]86} // end remote_buf_release_data()
87
[657]88/////////////////////////////////////////
89void remote_buf_destroy( xptr_t buf_xp )
90{
[671]91
92assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
93
[657]94 remote_buf_t * buf_ptr = GET_PTR( buf_xp );
95 cxy_t buf_cxy = GET_CXY( buf_xp );
96
[671]97 // release data buffer
98 remote_buf_release_data( buf_xp );
[666]99
[671]100 // release remote_buf descriptor
[683]101 kmem_remote_free( buf_cxy , buf_ptr , bits_log2(sizeof(remote_buf_t)) );
[657]102
[671]103} // end remote_buf_destroy()
104
[657]105/////////////////////////////////////////
106void remote_buf_reset( xptr_t buf_xp )
107{
[671]108
109assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
110
[657]111 remote_buf_t * buf_ptr = GET_PTR( buf_xp );
112 cxy_t buf_cxy = GET_CXY( buf_xp );
113
[671]114 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->wid ) , 0 );
115 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->rid ) , 0 );
[657]116 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->sts ) , 0 );
117}
118
119/////////////////////////////////////////////////
120error_t remote_buf_get_to_user( xptr_t buf_xp,
121 uint8_t * u_buf,
122 uint32_t nbytes )
123{
[671]124
125assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
126
[657]127 remote_buf_t * buf_ptr = GET_PTR( buf_xp );
128 cxy_t buf_cxy = GET_CXY( buf_xp );
129
130 // build relevant extended pointers
[671]131 xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts );
132 xptr_t rid_xp = XPTR( buf_cxy , &buf_ptr->rid );
133 xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order );
134 xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
[657]135
136 // get relevant infos from remote buffer descriptor
137 uint32_t sts = hal_remote_l32( sts_xp );
[671]138 uint32_t rid = hal_remote_l32( rid_xp );
139 uint32_t order = hal_remote_l32( order_xp );
[657]140 uint8_t * data = hal_remote_lpt( data_xp );
141
[671]142 uint32_t size = 1 << order;
143 uint32_t mask = size - 1;
144
[657]145 // check enough bytes in buffer
146 if( nbytes > sts ) return -1;
147
148 // move nbytes
[671]149 if( (rid + nbytes) <= size) // no wrap around => one move
[657]150 {
151 hal_copy_to_uspace( u_buf,
[671]152 XPTR( buf_cxy , data + rid ),
[657]153 nbytes );
154 }
155 else // wrap around => two moves
156 {
[671]157 uint32_t bytes_1 = size - rid;
[657]158 uint32_t bytes_2 = nbytes - bytes_1;
159
160 hal_copy_to_uspace( u_buf,
[671]161 XPTR( buf_cxy , data + rid ),
[657]162 bytes_1 );
163
164 hal_copy_to_uspace( u_buf + bytes_1,
165 XPTR( buf_cxy , data ),
166 bytes_2 );
167 }
168
[671]169 // update rid in buffer descriptor
170 hal_remote_s32( rid_xp , (rid + nbytes) & mask );
[657]171
172 // atomically update sts
173 hal_remote_atomic_add( sts_xp , -nbytes );
174
175 return 0;
176
177} // end remote_buf_get_to_user()
178
179///////////////////////////////////////////////////
180error_t remote_buf_get_to_kernel( xptr_t buf_xp,
181 uint8_t * k_buf,
182 uint32_t nbytes )
183{
[671]184
185assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
186
[657]187 remote_buf_t * buf_ptr = GET_PTR( buf_xp );
188 cxy_t buf_cxy = GET_CXY( buf_xp );
189
190 // build relevant extended pointers
[671]191 xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts );
192 xptr_t rid_xp = XPTR( buf_cxy , &buf_ptr->rid );
193 xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order );
194 xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
[657]195
196 // get relevant infos from remote buffer descriptor
197 uint32_t sts = hal_remote_l32( sts_xp );
[671]198 uint32_t rid = hal_remote_l32( rid_xp );
199 uint32_t order = hal_remote_l32( order_xp );
[657]200 uint8_t * data = hal_remote_lpt( data_xp );
201
[671]202 uint32_t size = 1 << order;
203 uint32_t mask = size - 1;
204
[657]205 // check enough bytes in buffer
206 if( nbytes > sts ) return -1;
207
208 // move nbytes
[671]209 if( (rid + nbytes) <= size) // no wrap around => one move
[657]210 {
211 hal_remote_memcpy( XPTR( local_cxy , k_buf ),
[671]212 XPTR( buf_cxy , data + rid ),
[657]213 nbytes );
214 }
215 else // wrap around => two moves
216 {
[671]217 uint32_t bytes_1 = size - rid;
[657]218 uint32_t bytes_2 = nbytes - bytes_1;
219
220 hal_remote_memcpy( XPTR( local_cxy , k_buf ),
[671]221 XPTR( buf_cxy , data + rid ),
[657]222 bytes_1 );
223
224 hal_remote_memcpy( XPTR( local_cxy , k_buf + bytes_1 ),
225 XPTR( buf_cxy , data ),
226 bytes_2 );
227 }
228
[671]229 // update rid in buffer descriptor
230 hal_remote_s32( rid_xp , (rid + nbytes) & mask );
[657]231
232 // atomically update sts
233 hal_remote_atomic_add( sts_xp , -nbytes );
234
235 return 0;
236
[671]237} // end remote_buf_get_to_kernel()
[657]238
239///////////////////////////////////////////////////
240error_t remote_buf_put_from_user( xptr_t buf_xp,
241 uint8_t * u_buf,
242 uint32_t nbytes )
243{
[671]244
245assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
246
[657]247 remote_buf_t * buf_ptr = GET_PTR( buf_xp );
248 cxy_t buf_cxy = GET_CXY( buf_xp );
249
250 // build relevant extended pointers
[671]251 xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts );
252 xptr_t wid_xp = XPTR( buf_cxy , &buf_ptr->wid );
253 xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order );
254 xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
[657]255
256 // get relevant infos from remote buffer descriptor
257 uint32_t sts = hal_remote_l32( sts_xp );
[671]258 uint32_t wid = hal_remote_l32( wid_xp );
259 uint32_t order = hal_remote_l32( order_xp );
[657]260 uint8_t * data = hal_remote_lpt( data_xp );
261
[671]262 uint32_t size = 1 << order;
263 uint32_t mask = size - 1;
264
[657]265 // check enough space in buffer
266 if( nbytes > (size - sts) ) return -1;
267
268 // move nbytes
[671]269 if( (wid + nbytes) <= size) // no wrap around => one move
[657]270 {
[671]271 hal_copy_from_uspace( XPTR( buf_cxy , data + wid ),
[657]272 u_buf,
273 nbytes );
274 }
275 else // wrap around => two moves
276 {
[671]277 uint32_t bytes_1 = size - wid;
[657]278 uint32_t bytes_2 = nbytes - bytes_1;
279
[671]280 hal_copy_from_uspace( XPTR( buf_cxy , data + wid ),
[657]281 u_buf,
282 bytes_1 );
283
284 hal_copy_from_uspace( XPTR( buf_cxy , data ),
285 u_buf + bytes_1,
286 bytes_2 );
287 }
288
[671]289 // update wid in buffer descriptor
290 hal_remote_s32( wid_xp , (wid + nbytes) & mask );
[657]291
292 // atomically update sts
293 hal_remote_atomic_add( sts_xp , nbytes );
294
295 return 0;
296
297} // end remote_buf_put_from_user()
298
299/////////////////////////////////////////////////////
300error_t remote_buf_put_from_kernel( xptr_t buf_xp,
301 uint8_t * k_buf,
302 uint32_t nbytes )
303{
[671]304
305assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" );
306
[657]307 remote_buf_t * buf_ptr = GET_PTR( buf_xp );
308 cxy_t buf_cxy = GET_CXY( buf_xp );
309
310 // build relevant extended pointers
[671]311 xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts );
312 xptr_t wid_xp = XPTR( buf_cxy , &buf_ptr->wid );
313 xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order );
314 xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
[657]315
316 // get relevant infos from remote buffer descriptor
317 uint32_t sts = hal_remote_l32( sts_xp );
[671]318 uint32_t wid = hal_remote_l32( wid_xp );
319 uint32_t order = hal_remote_l32( order_xp );
[657]320 uint8_t * data = hal_remote_lpt( data_xp );
321
[671]322 uint32_t size = 1 << order;
323 uint32_t mask = size - 1;
324
[657]325 // check enough space in buffer
326 if( nbytes > (size - sts) ) return -1;
327
328 // move nbytes
[671]329 if( (wid + nbytes) <= size) // no wrap around => one move
[657]330 {
[671]331 hal_remote_memcpy( XPTR( buf_cxy , data + wid ),
[657]332 XPTR( local_cxy , k_buf ),
333 nbytes );
334 }
335 else // wrap around => two moves
336 {
[671]337 uint32_t bytes_1 = size - wid;
[657]338 uint32_t bytes_2 = nbytes - bytes_1;
339
[671]340 hal_remote_memcpy( XPTR( buf_cxy , data + wid ),
[657]341 XPTR( local_cxy , k_buf ),
342 bytes_1 );
343
344 hal_remote_memcpy( XPTR( buf_cxy , data ),
345 XPTR( local_cxy , k_buf + bytes_1 ),
346 bytes_2 );
347 }
348
[671]349 // update wid in buffer descriptor
350 hal_remote_s32( wid_xp , (wid + nbytes) & mask );
[657]351
352 // atomically update sts
353 hal_remote_atomic_add( sts_xp , nbytes );
354
355 return 0;
356
357} // end remote_buf_put_from_kernel()
358
359////////////////////////////////////////////
360uint32_t remote_buf_status( xptr_t buf_xp )
361{
362 remote_buf_t * buf_ptr = GET_PTR( buf_xp );
363 cxy_t buf_cxy = GET_CXY( buf_xp );
364
365 return hal_remote_l32( XPTR( buf_cxy , &buf_ptr->sts ) );
366
367} // end remote_buf_status()
368
[683]369///////////////////////////////////////////////
370void remote_buf_display( const char * func_str,
371 xptr_t buf_xp,
372 uint32_t nbytes,
373 uint32_t offset )
374{
375 if( nbytes > 256 )
376 {
377 printk("\n[WARNING] in %s : no more than 256 bytes\n", __FUNCTION__ );
378 nbytes = 256;
379 }
[657]380
[683]381 uint8_t string[128]; // for header
382 uint8_t local_data[256]; // local data buffer
383
384 cxy_t cxy = GET_CXY( buf_xp );
385 remote_buf_t * ptr = GET_PTR( buf_xp );
386
387 uint32_t order = hal_remote_l32( XPTR( cxy , &ptr->order ));
388 uint32_t rid = hal_remote_l32( XPTR( cxy , &ptr->rid ));
389 uint32_t wid = hal_remote_l32( XPTR( cxy , &ptr->wid ));
390 uint32_t sts = hal_remote_l32( XPTR( cxy , &ptr->sts ));
391 uint8_t * data = hal_remote_lpt( XPTR( cxy , &ptr->data ));
392
393 // make a local copy of data buffer
394 hal_remote_memcpy( XPTR( local_cxy , local_data ),
395 XPTR( cxy , data + offset ),
396 nbytes );
397
398 // build header
399 snprintk( (char*)string , 128 ,
400 "in %s remote buffer [%x,%x] : size %d / rid %d / wid %d / sts %d ",
401 func_str , cxy , ptr , 1<<order , rid , wid , sts );
402
403 // display buffer on TXT0
404 putb( (char*)string , local_data , nbytes );
405
406} // end remote_buf_display()
Note: See TracBrowser for help on using the repository browser.