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

Last change on this file since 666 was 666, checked in by alain, 3 years ago

Cosmetic.

File size: 10.7 KB
Line 
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
33/////////////////////////////////////////
34error_t remote_buf_init( xptr_t   buf_xp,
35                         uint32_t size )
36{
37    kmem_req_t     req;
38    uint8_t      * data;
39
40    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
41    cxy_t          buf_cxy = GET_CXY( buf_xp );
42
43    // allocate the data buffer
44    if( size == 0 )
45    {
46        data = NULL;
47    }
48    else if( size >= CONFIG_PPM_PAGE_SIZE )
49    {
50        req.type  = KMEM_PPM;
51        req.order = bits_log2( size >> CONFIG_PPM_PAGE_SHIFT );
52        req.flags = AF_NONE;
53        data = kmem_remote_alloc( buf_cxy , &req );
54
55        if( data == NULL )  return -1;
56    }
57    else
58    {
59        req.type  = KMEM_KCM;
60        req.order = bits_log2( size );
61        req.flags = AF_NONE;
62        data = kmem_remote_alloc( buf_cxy , &req );
63
64        if( data == NULL )  return -1;
65    }
66
67    // initialize buffer descriptor
68    hal_remote_s32( XPTR( buf_cxy , &buf_ptr->size ) , size );
69    hal_remote_s32( XPTR( buf_cxy , &buf_ptr->ptw  ) , 0 );
70    hal_remote_s32( XPTR( buf_cxy , &buf_ptr->ptr  ) , 0 );
71    hal_remote_s32( XPTR( buf_cxy , &buf_ptr->sts  ) , 0 );
72    hal_remote_spt( XPTR( buf_cxy , &buf_ptr->data ) , data );
73
74    return 0;
75
76}  // end remote_buf_init()
77
78/////////////////////////////////////////
79void remote_buf_destroy( xptr_t  buf_xp )
80{
81    kmem_req_t   req;
82
83    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
84    cxy_t          buf_cxy = GET_CXY( buf_xp );
85
86    uint32_t size = hal_remote_l32( XPTR( buf_cxy , &buf_ptr->size ));
87
88    // release memory allocated to data buffer if required
89    if( size == 0 )
90    {
91        return;
92    }
93    else if( size >= CONFIG_PPM_PAGE_SIZE )
94    {
95        req.type  = KMEM_PPM;
96        req.ptr   = hal_remote_lpt( XPTR( buf_cxy , &buf_ptr->data ) );
97        kmem_remote_free( buf_cxy , &req );
98    }
99    else
100    {
101        req.type  = KMEM_KCM;
102        req.ptr   = hal_remote_lpt( XPTR( buf_cxy , &buf_ptr->data ) );
103        kmem_remote_free( buf_cxy , &req );
104    }
105}
106
107/////////////////////////////////////////
108void remote_buf_reset( xptr_t    buf_xp )
109{
110    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
111    cxy_t          buf_cxy = GET_CXY( buf_xp );
112
113    hal_remote_s32( XPTR( buf_cxy , &buf_ptr->ptw ) , 0 );
114    hal_remote_s32( XPTR( buf_cxy , &buf_ptr->ptr ) , 0 );
115    hal_remote_s32( XPTR( buf_cxy , &buf_ptr->sts ) , 0 );
116}
117
118/////////////////////////////////////////////////
119error_t remote_buf_get_to_user( xptr_t    buf_xp,
120                                uint8_t * u_buf, 
121                                uint32_t  nbytes )
122{
123    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
124    cxy_t          buf_cxy = GET_CXY( buf_xp );
125
126    // build relevant extended pointers
127    xptr_t sts_xp  = XPTR( buf_cxy , &buf_ptr->sts );
128    xptr_t ptr_xp  = XPTR( buf_cxy , &buf_ptr->ptr );
129    xptr_t size_xp = XPTR( buf_cxy , &buf_ptr->size );
130    xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
131
132    // get relevant infos from remote buffer descriptor
133    uint32_t  sts  = hal_remote_l32( sts_xp );
134    uint32_t  ptr  = hal_remote_l32( ptr_xp );
135    uint32_t  size = hal_remote_l32( size_xp );
136    uint8_t * data = hal_remote_lpt( data_xp );
137
138    // check enough bytes in buffer
139    if( nbytes > sts ) return -1;
140
141    // move nbytes
142    if( (ptr + nbytes) <= size)  // no wrap around => one move
143    {
144        hal_copy_to_uspace( u_buf,
145                            XPTR( buf_cxy , data + ptr ),
146                            nbytes );
147    }
148    else                         // wrap around => two moves
149    {
150        uint32_t bytes_1 = size - ptr;
151        uint32_t bytes_2 = nbytes - bytes_1;
152     
153        hal_copy_to_uspace( u_buf,
154                            XPTR( buf_cxy , data + ptr ),
155                            bytes_1 );
156
157        hal_copy_to_uspace( u_buf + bytes_1,
158                            XPTR( buf_cxy , data ),
159                            bytes_2 );
160    }
161
162    // update ptr in buffer descriptor
163    hal_remote_s32( ptr_xp , (ptr + nbytes) % size );
164
165    // atomically update sts
166    hal_remote_atomic_add( sts_xp , -nbytes );
167
168    return 0;
169
170}  // end remote_buf_get_to_user()
171
172///////////////////////////////////////////////////
173error_t remote_buf_get_to_kernel( xptr_t    buf_xp,
174                                  uint8_t * k_buf,
175                                  uint32_t  nbytes )
176{
177    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
178    cxy_t          buf_cxy = GET_CXY( buf_xp );
179
180    // build relevant extended pointers
181    xptr_t sts_xp  = XPTR( buf_cxy , &buf_ptr->sts );
182    xptr_t ptr_xp  = XPTR( buf_cxy , &buf_ptr->ptr );
183    xptr_t size_xp = XPTR( buf_cxy , &buf_ptr->size );
184    xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
185
186    // get relevant infos from remote buffer descriptor
187    uint32_t  sts  = hal_remote_l32( sts_xp );
188    uint32_t  ptr  = hal_remote_l32( ptr_xp );
189    uint32_t  size = hal_remote_l32( size_xp );
190    uint8_t * data = hal_remote_lpt( data_xp );
191
192    // check enough bytes in buffer
193    if( nbytes > sts ) return -1;
194
195    // move nbytes
196    if( (ptr + nbytes) <= size)  // no wrap around => one move
197    {
198        hal_remote_memcpy( XPTR( local_cxy , k_buf ),
199                           XPTR( buf_cxy   , data + ptr ),
200                           nbytes );
201    }
202    else                         // wrap around => two moves
203    {
204        uint32_t bytes_1 = size - ptr;
205        uint32_t bytes_2 = nbytes - bytes_1;
206     
207        hal_remote_memcpy( XPTR( local_cxy , k_buf ),
208                           XPTR( buf_cxy   , data + ptr ),
209                           bytes_1 );
210
211        hal_remote_memcpy( XPTR( local_cxy , k_buf + bytes_1 ),
212                           XPTR( buf_cxy   , data ),
213                           bytes_2 );
214    }
215
216    // update ptr in buffer descriptor
217    hal_remote_s32( ptr_xp , (ptr + nbytes) % size );
218
219    // atomically update sts
220    hal_remote_atomic_add( sts_xp , -nbytes );
221
222    return 0;
223
224}  // end remote_buf_get_to_user()
225
226///////////////////////////////////////////////////
227error_t remote_buf_put_from_user( xptr_t    buf_xp,
228                                  uint8_t * u_buf,
229                                  uint32_t  nbytes )
230{
231    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
232    cxy_t          buf_cxy = GET_CXY( buf_xp );
233
234    // build relevant extended pointers
235    xptr_t sts_xp  = XPTR( buf_cxy , &buf_ptr->sts );
236    xptr_t ptw_xp  = XPTR( buf_cxy , &buf_ptr->ptw );
237    xptr_t size_xp = XPTR( buf_cxy , &buf_ptr->size );
238    xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
239
240    // get relevant infos from remote buffer descriptor
241    uint32_t  sts  = hal_remote_l32( sts_xp );
242    uint32_t  ptw  = hal_remote_l32( ptw_xp );
243    uint32_t  size = hal_remote_l32( size_xp );
244    uint8_t * data = hal_remote_lpt( data_xp );
245
246    // check enough space in buffer
247    if( nbytes > (size - sts) ) return -1;
248
249    // move nbytes
250    if( (ptw + nbytes) <= size)  // no wrap around => one move
251    {
252        hal_copy_from_uspace( XPTR( buf_cxy , data + ptw ),
253                              u_buf,
254                              nbytes );
255    }
256    else                         // wrap around => two moves
257    {
258        uint32_t bytes_1 = size - ptw;
259        uint32_t bytes_2 = nbytes - bytes_1;
260     
261        hal_copy_from_uspace( XPTR( buf_cxy , data + ptw ),
262                              u_buf,
263                              bytes_1 );
264
265        hal_copy_from_uspace( XPTR( buf_cxy , data ),
266                              u_buf + bytes_1,
267                              bytes_2 );
268    }
269
270    // update ptw in buffer descriptor
271    hal_remote_s32( ptw_xp , (ptw + nbytes) % size );
272
273    // atomically update sts
274    hal_remote_atomic_add( sts_xp , nbytes );
275
276    return 0;
277
278}  // end remote_buf_put_from_user()
279
280/////////////////////////////////////////////////////
281error_t remote_buf_put_from_kernel( xptr_t    buf_xp,
282                                    uint8_t * k_buf, 
283                                    uint32_t  nbytes )
284{
285    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
286    cxy_t          buf_cxy = GET_CXY( buf_xp );
287
288    // build relevant extended pointers
289    xptr_t sts_xp  = XPTR( buf_cxy , &buf_ptr->sts );
290    xptr_t ptw_xp  = XPTR( buf_cxy , &buf_ptr->ptw );
291    xptr_t size_xp = XPTR( buf_cxy , &buf_ptr->size );
292    xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data );
293
294    // get relevant infos from remote buffer descriptor
295    uint32_t  sts  = hal_remote_l32( sts_xp );
296    uint32_t  ptw  = hal_remote_l32( ptw_xp );
297    uint32_t  size = hal_remote_l32( size_xp );
298    uint8_t * data = hal_remote_lpt( data_xp );
299
300    // check enough space in buffer
301    if( nbytes > (size - sts) ) return -1;
302
303    // move nbytes
304    if( (ptw + nbytes) <= size)  // no wrap around => one move
305    {
306        hal_remote_memcpy( XPTR( buf_cxy   , data + ptw ),
307                           XPTR( local_cxy , k_buf ),
308                           nbytes );
309    }
310    else                         // wrap around => two moves
311    {
312        uint32_t bytes_1 = size - ptw;
313        uint32_t bytes_2 = nbytes - bytes_1;
314     
315        hal_remote_memcpy( XPTR( buf_cxy   , data + ptw ),
316                           XPTR( local_cxy , k_buf ),
317                           bytes_1 );
318
319        hal_remote_memcpy( XPTR( buf_cxy   , data ),
320                           XPTR( local_cxy , k_buf + bytes_1 ),
321                           bytes_2 );
322    }
323
324    // update ptw in buffer descriptor
325    hal_remote_s32( ptw_xp , (ptw + nbytes) % size );
326
327    // atomically update sts
328    hal_remote_atomic_add( sts_xp , nbytes );
329
330    return 0;
331
332}  // end remote_buf_put_from_kernel()
333
334////////////////////////////////////////////
335uint32_t remote_buf_status( xptr_t  buf_xp )
336{
337    remote_buf_t * buf_ptr = GET_PTR( buf_xp );
338    cxy_t          buf_cxy = GET_CXY( buf_xp );
339
340    return hal_remote_l32( XPTR( buf_cxy , &buf_ptr->sts ) );
341   
342}  // end remote_buf_status()
343
344
Note: See TracBrowser for help on using the repository browser.