Changeset 671 for trunk/kernel/libk/remote_buf.c
- Timestamp:
- Nov 19, 2020, 11:47:00 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/libk/remote_buf.c
r666 r671 31 31 #include <remote_buf.h> 32 32 33 ///////////////////////////////////////////// 34 remote_buf_t * remote_buf_alloc( cxy_t cxy ) 35 { 36 kmem_req_t req; 37 38 req.type = KMEM_KCM; 39 req.order = bits_log2( sizeof(remote_buf_t) ); 40 req.flags = AF_ZERO; 41 return kmem_remote_alloc( cxy , &req ); 42 } 43 33 44 ///////////////////////////////////////// 34 45 error_t remote_buf_init( xptr_t buf_xp, 35 uint32_t size ) 36 { 46 uint32_t order ) 47 { 48 49 assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); 50 assert( __FUNCTION__ , (order < 32) , "order cannot be larger than 31" ); 51 37 52 kmem_req_t req; 38 53 uint8_t * data; … … 42 57 43 58 // allocate the data buffer 44 if( size == 0 ) 45 { 46 data = NULL; 47 } 48 else if( size >= CONFIG_PPM_PAGE_SIZE ) 59 if( order >= CONFIG_PPM_PAGE_SHIFT ) // use KMEM_PPM 49 60 { 50 61 req.type = KMEM_PPM; 51 req.order = bits_log2( size >> CONFIG_PPM_PAGE_SHIFT );62 req.order = order - CONFIG_PPM_PAGE_SHIFT; 52 63 req.flags = AF_NONE; 53 64 data = kmem_remote_alloc( buf_cxy , &req ); … … 55 66 if( data == NULL ) return -1; 56 67 } 57 else 68 else // use KMEM_KCM 58 69 { 59 70 req.type = KMEM_KCM; 60 req.order = bits_log2( size );71 req.order = order; 61 72 req.flags = AF_NONE; 62 73 data = kmem_remote_alloc( buf_cxy , &req ); … … 66 77 67 78 // 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 );79 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->order ) , order ); 80 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->wid ) , 0 ); 81 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->rid ) , 0 ); 82 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->sts ) , 0 ); 83 hal_remote_spt( XPTR( buf_cxy , &buf_ptr->data ) , data ); 73 84 74 85 return 0; 75 86 76 87 } // end remote_buf_init() 88 89 ////////////////////////////////////////////// 90 void remote_buf_release_data( xptr_t buf_xp ) 91 { 92 kmem_req_t req; 93 94 assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); 95 96 remote_buf_t * buf_ptr = GET_PTR( buf_xp ); 97 cxy_t buf_cxy = GET_CXY( buf_xp ); 98 99 // gets data buffer local pointer and order 100 uint32_t order = hal_remote_l32( XPTR( buf_cxy , &buf_ptr->order )); 101 char * data_ptr = hal_remote_lpt( XPTR( buf_cxy , &buf_ptr->data )); 102 103 // release memory allocated for data buffer if required 104 if( data_ptr != NULL ) 105 { 106 if( order >= CONFIG_PPM_PAGE_SHIFT ) // use KMEM_PPM 107 { 108 req.type = KMEM_PPM; 109 req.ptr = data_ptr; 110 kmem_remote_free( buf_cxy , &req ); 111 } 112 else // use KMEM_KCM 113 { 114 req.type = KMEM_KCM; 115 req.ptr = data_ptr; 116 kmem_remote_free( buf_cxy , &req ); 117 } 118 } 119 } // end remote_buf_release_data() 77 120 78 121 ///////////////////////////////////////// 79 122 void remote_buf_destroy( xptr_t buf_xp ) 80 123 { 124 125 assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); 126 81 127 kmem_req_t req; 82 128 … … 84 130 cxy_t buf_cxy = GET_CXY( buf_xp ); 85 131 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 } 132 // release data buffer 133 remote_buf_release_data( buf_xp ); 134 135 // release remote_buf descriptor 136 req.type = KMEM_KCM; 137 req.ptr = buf_ptr; 138 kmem_remote_free( buf_cxy , &req ); 139 140 } // end remote_buf_destroy() 106 141 107 142 ///////////////////////////////////////// 108 143 void remote_buf_reset( xptr_t buf_xp ) 109 144 { 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 ); 145 146 assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); 147 148 remote_buf_t * buf_ptr = GET_PTR( buf_xp ); 149 cxy_t buf_cxy = GET_CXY( buf_xp ); 150 151 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->wid ) , 0 ); 152 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->rid ) , 0 ); 115 153 hal_remote_s32( XPTR( buf_cxy , &buf_ptr->sts ) , 0 ); 116 154 } … … 121 159 uint32_t nbytes ) 122 160 { 161 162 assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); 163 123 164 remote_buf_t * buf_ptr = GET_PTR( buf_xp ); 124 165 cxy_t buf_cxy = GET_CXY( buf_xp ); 125 166 126 167 // 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 );168 xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts ); 169 xptr_t rid_xp = XPTR( buf_cxy , &buf_ptr->rid ); 170 xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order ); 171 xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data ); 131 172 132 173 // get relevant infos from remote buffer descriptor 133 174 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 );175 uint32_t rid = hal_remote_l32( rid_xp ); 176 uint32_t order = hal_remote_l32( order_xp ); 136 177 uint8_t * data = hal_remote_lpt( data_xp ); 178 179 uint32_t size = 1 << order; 180 uint32_t mask = size - 1; 137 181 138 182 // check enough bytes in buffer … … 140 184 141 185 // move nbytes 142 if( ( ptr+ nbytes) <= size) // no wrap around => one move186 if( (rid + nbytes) <= size) // no wrap around => one move 143 187 { 144 188 hal_copy_to_uspace( u_buf, 145 XPTR( buf_cxy , data + ptr),189 XPTR( buf_cxy , data + rid ), 146 190 nbytes ); 147 191 } 148 192 else // wrap around => two moves 149 193 { 150 uint32_t bytes_1 = size - ptr;194 uint32_t bytes_1 = size - rid; 151 195 uint32_t bytes_2 = nbytes - bytes_1; 152 196 153 197 hal_copy_to_uspace( u_buf, 154 XPTR( buf_cxy , data + ptr),198 XPTR( buf_cxy , data + rid ), 155 199 bytes_1 ); 156 200 … … 160 204 } 161 205 162 // update ptrin buffer descriptor163 hal_remote_s32( ptr_xp , (ptr + nbytes) % size);206 // update rid in buffer descriptor 207 hal_remote_s32( rid_xp , (rid + nbytes) & mask ); 164 208 165 209 // atomically update sts … … 175 219 uint32_t nbytes ) 176 220 { 221 222 assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); 223 177 224 remote_buf_t * buf_ptr = GET_PTR( buf_xp ); 178 225 cxy_t buf_cxy = GET_CXY( buf_xp ); 179 226 180 227 // 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 );228 xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts ); 229 xptr_t rid_xp = XPTR( buf_cxy , &buf_ptr->rid ); 230 xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order ); 231 xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data ); 185 232 186 233 // get relevant infos from remote buffer descriptor 187 234 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 );235 uint32_t rid = hal_remote_l32( rid_xp ); 236 uint32_t order = hal_remote_l32( order_xp ); 190 237 uint8_t * data = hal_remote_lpt( data_xp ); 238 239 uint32_t size = 1 << order; 240 uint32_t mask = size - 1; 191 241 192 242 // check enough bytes in buffer … … 194 244 195 245 // move nbytes 196 if( ( ptr+ nbytes) <= size) // no wrap around => one move246 if( (rid + nbytes) <= size) // no wrap around => one move 197 247 { 198 248 hal_remote_memcpy( XPTR( local_cxy , k_buf ), 199 XPTR( buf_cxy , data + ptr),249 XPTR( buf_cxy , data + rid ), 200 250 nbytes ); 201 251 } 202 252 else // wrap around => two moves 203 253 { 204 uint32_t bytes_1 = size - ptr;254 uint32_t bytes_1 = size - rid; 205 255 uint32_t bytes_2 = nbytes - bytes_1; 206 256 207 257 hal_remote_memcpy( XPTR( local_cxy , k_buf ), 208 XPTR( buf_cxy , data + ptr),258 XPTR( buf_cxy , data + rid ), 209 259 bytes_1 ); 210 260 … … 214 264 } 215 265 216 // update ptrin buffer descriptor217 hal_remote_s32( ptr_xp , (ptr + nbytes) % size);266 // update rid in buffer descriptor 267 hal_remote_s32( rid_xp , (rid + nbytes) & mask ); 218 268 219 269 // atomically update sts … … 222 272 return 0; 223 273 224 } // end remote_buf_get_to_ user()274 } // end remote_buf_get_to_kernel() 225 275 226 276 /////////////////////////////////////////////////// … … 229 279 uint32_t nbytes ) 230 280 { 281 282 assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); 283 231 284 remote_buf_t * buf_ptr = GET_PTR( buf_xp ); 232 285 cxy_t buf_cxy = GET_CXY( buf_xp ); 233 286 234 287 // 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 );288 xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts ); 289 xptr_t wid_xp = XPTR( buf_cxy , &buf_ptr->wid ); 290 xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order ); 291 xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data ); 239 292 240 293 // get relevant infos from remote buffer descriptor 241 294 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 );295 uint32_t wid = hal_remote_l32( wid_xp ); 296 uint32_t order = hal_remote_l32( order_xp ); 244 297 uint8_t * data = hal_remote_lpt( data_xp ); 298 299 uint32_t size = 1 << order; 300 uint32_t mask = size - 1; 245 301 246 302 // check enough space in buffer … … 248 304 249 305 // move nbytes 250 if( ( ptw+ nbytes) <= size) // no wrap around => one move251 { 252 hal_copy_from_uspace( XPTR( buf_cxy , data + ptw),306 if( (wid + nbytes) <= size) // no wrap around => one move 307 { 308 hal_copy_from_uspace( XPTR( buf_cxy , data + wid ), 253 309 u_buf, 254 310 nbytes ); … … 256 312 else // wrap around => two moves 257 313 { 258 uint32_t bytes_1 = size - ptw;314 uint32_t bytes_1 = size - wid; 259 315 uint32_t bytes_2 = nbytes - bytes_1; 260 316 261 hal_copy_from_uspace( XPTR( buf_cxy , data + ptw),317 hal_copy_from_uspace( XPTR( buf_cxy , data + wid ), 262 318 u_buf, 263 319 bytes_1 ); … … 268 324 } 269 325 270 // update ptwin buffer descriptor271 hal_remote_s32( ptw_xp , (ptw + nbytes) % size);326 // update wid in buffer descriptor 327 hal_remote_s32( wid_xp , (wid + nbytes) & mask ); 272 328 273 329 // atomically update sts … … 283 339 uint32_t nbytes ) 284 340 { 341 342 assert( __FUNCTION__ , (buf_xp != XPTR_NULL) , "buf_xp cannot be NULL" ); 343 285 344 remote_buf_t * buf_ptr = GET_PTR( buf_xp ); 286 345 cxy_t buf_cxy = GET_CXY( buf_xp ); 287 346 288 347 // 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 );348 xptr_t sts_xp = XPTR( buf_cxy , &buf_ptr->sts ); 349 xptr_t wid_xp = XPTR( buf_cxy , &buf_ptr->wid ); 350 xptr_t order_xp = XPTR( buf_cxy , &buf_ptr->order ); 351 xptr_t data_xp = XPTR( buf_cxy , &buf_ptr->data ); 293 352 294 353 // get relevant infos from remote buffer descriptor 295 354 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 );355 uint32_t wid = hal_remote_l32( wid_xp ); 356 uint32_t order = hal_remote_l32( order_xp ); 298 357 uint8_t * data = hal_remote_lpt( data_xp ); 358 359 uint32_t size = 1 << order; 360 uint32_t mask = size - 1; 299 361 300 362 // check enough space in buffer … … 302 364 303 365 // move nbytes 304 if( ( ptw+ nbytes) <= size) // no wrap around => one move305 { 306 hal_remote_memcpy( XPTR( buf_cxy , data + ptw),366 if( (wid + nbytes) <= size) // no wrap around => one move 367 { 368 hal_remote_memcpy( XPTR( buf_cxy , data + wid ), 307 369 XPTR( local_cxy , k_buf ), 308 370 nbytes ); … … 310 372 else // wrap around => two moves 311 373 { 312 uint32_t bytes_1 = size - ptw;374 uint32_t bytes_1 = size - wid; 313 375 uint32_t bytes_2 = nbytes - bytes_1; 314 376 315 hal_remote_memcpy( XPTR( buf_cxy , data + ptw),377 hal_remote_memcpy( XPTR( buf_cxy , data + wid ), 316 378 XPTR( local_cxy , k_buf ), 317 379 bytes_1 ); … … 322 384 } 323 385 324 // update ptwin buffer descriptor325 hal_remote_s32( ptw_xp , (ptw + nbytes) % size);386 // update wid in buffer descriptor 387 hal_remote_s32( wid_xp , (wid + nbytes) & mask ); 326 388 327 389 // atomically update sts
Note: See TracChangeset
for help on using the changeset viewer.