source: trunk/kernel/kern/rpc.c @ 25

Last change on this file since 25 was 23, checked in by alain, 7 years ago

Introduce syscalls.

File size: 55.5 KB
Line 
1/*
2 * rpc.c - RPC related operations implementation.
3 *
4 * Author    Alain Greiner (2016,2017)
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_types.h>
26#include <hal_atomic.h>
27#include <hal_remote.h>
28#include <hal_irqmask.h>
29#include <hal_special.h>
30#include <printk.h>
31#include <remote_sem.h>
32#include <core.h>
33#include <mapper.h>
34#include <chdev.h>
35#include <bits.h>
36#include <thread.h>
37#include <cluster.h>
38#include <process.h>
39#include <vfs.h>
40#include <fatfs.h>
41#include <signal.h>
42#include <dev_icu.h>
43#include <rpc.h>
44
45/////////////////////////////////////////////////////////////////////////////////////////
46//      array of function pointers  (must be consistent with enum in rpc.h)
47/////////////////////////////////////////////////////////////////////////////////////////
48
49rpc_server_t * rpc_server[RPC_MAX_INDEX] =
50{
51    &rpc_pmem_get_pages_server,         // 0
52    &rpc_process_pid_alloc_server,      // 1
53    &rpc_process_exec_server,           // 2
54    &rpc_process_kill_server,           // 3
55    &rpc_thread_user_create_server,     // 4
56    &rpc_thread_kernel_create_server,   // 5
57    &rpc_signal_rise_server,            // 6                       
58    &rpc_undefined,                     // 7
59    &rpc_undefined,                     // 8
60    &rpc_undefined,                     // 9
61
62    &rpc_vfs_inode_create_server,       // 10 
63    &rpc_vfs_inode_destroy_server,      // 11 
64    &rpc_vfs_dentry_create_server,      // 12 
65    &rpc_vfs_dentry_destroy_server,     // 13 
66    &rpc_vfs_file_create_server,        // 14
67    &rpc_vfs_file_destroy_server,       // 15
68    &rpc_fatfs_get_cluster_server,      // 16
69    &rpc_undefined,                     // 17
70    &rpc_undefined,                     // 18
71    &rpc_undefined,                     // 19
72
73    &rpc_vmm_get_ref_vseg_server,       // 20
74    &rpc_vmm_get_pte_server,            // 21
75    &rpc_kcm_alloc_server,              // 22
76    &rpc_kcm_free_server,               // 23
77    &rpc_mapper_move_server,            // 24
78    &rpc_undefined,                     // 25
79    &rpc_undefined,                     // 26
80    &rpc_undefined,                     // 27
81    &rpc_undefined,                     // 28
82    &rpc_undefined,                     // 29
83};
84
85//////////////////////////////////////////////
86void __attribute__((noinline)) rpc_undefined()
87{
88    printk("\n[PANIC] ‰s called in cluster %x\n", __FUNCTION__ , local_cxy );
89    hal_core_sleep();
90}
91
92/////////////////////////////////////////////////////////////////////////////////////////
93// [0]           Marshaling functions attached to RPC_PMEM_GET_PAGES
94/////////////////////////////////////////////////////////////////////////////////////////
95
96///////////////////////////////////////////////
97void rpc_pmem_get_pages_client( cxy_t      cxy,
98                                uint32_t   order,      // in
99                                error_t  * error,      // out
100                                uint32_t * ppn )       // out
101{
102    // any RPC must be remote
103    if( cxy == local_cxy ) 
104    {
105        printk("PANIC in %s : target is not remote\n", __FUNCTION__ );
106        hal_core_sleep();
107    }
108
109    // initialise RPC descriptor header
110    rpc_desc_t  rpc;
111    rpc.index    = RPC_PMEM_GET_PAGES;
112    rpc.response = 1;
113
114    // set input arguments in RPC descriptor
115    rpc.args[0] = (uint64_t)order;
116
117    // register RPC request in remote RPC fifo (blocking function)
118    rpc_send_sync( cxy , &rpc );
119
120    // get output arguments RPC descriptor
121    *error  = (error_t)rpc.args[0];     
122    *ppn    = (uint32_t)rpc.args[1];
123}
124
125///////////////////////////////////////////
126void rpc_pmem_get_pages_server( xptr_t xp )
127{
128    uint32_t order;  // input
129    error_t  error;  // output
130    uint32_t ppn;    // output
131
132    // get client cluster identifier and pointer on RPC descriptor
133    cxy_t        cxy  = (cxy_t)GET_CXY( xp );
134    rpc_desc_t * desc = (rpc_desc_t *)GET_PTR( xp );
135
136    // get input arguments from client RPC descriptor
137    order = hal_remote_lw( XPTR( cxy , &desc->args[0] ) );
138   
139    // call local pmem allocator
140    page_t * page = ppm_alloc_pages( order ); 
141    error = ( page == NULL ) ? ENOMEM : 0;
142    ppn   = ppm_page2ppn( page );
143
144    // set output arguments into client RPC descriptor
145    hal_remote_sw( XPTR( cxy , &desc->args[0] ) , error );
146    hal_remote_sw( XPTR( cxy , &desc->args[1] ) , ppn );
147}
148
149/////////////////////////////////////////////////////////////////////////////////////////
150// [1]           Marshaling functions attached to RPC_PROCESS_PID_ALLOC
151/////////////////////////////////////////////////////////////////////////////////////////
152
153//////////////////////////////////////////////////
154void rpc_process_pid_alloc_client( cxy_t       cxy, 
155                                   process_t * process,  // in
156                                   error_t   * error,    // out
157                                   pid_t     * pid )     // out
158{
159    // RPC must be remote
160    if( cxy == local_cxy ) 
161    {
162        printk("PANIC in %s : target is not remote\n", __FUNCTION__ );
163        hal_core_sleep();
164    }
165
166    // initialise RPC descriptor header
167    rpc_desc_t  rpc;
168    rpc.index    = RPC_PROCESS_PID_ALLOC;
169    rpc.response = 1;
170
171    // set input arguments in RPC descriptor
172    rpc.args[0] = (uint64_t)(intptr_t)process;
173
174    // register RPC request in remote RPC fifo (blocking function)
175    rpc_send_sync( cxy , &rpc );
176
177    // get output arguments RPC descriptor
178    *pid    = (pid_t)rpc.args[1];
179    *error  = (error_t)rpc.args[2];     
180}
181
182//////////////////////////////////////////////
183void rpc_process_pid_alloc_server( xptr_t xp )
184{
185    process_t * process;   // input  : client process descriptor
186    error_t     error;     // output : error status
187    pid_t       pid;       // output : process identifier
188
189    // get client cluster identifier and pointer on RPC descriptor
190    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
191    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
192
193    // get input argument from client RPC descriptor
194    process = (process_t*)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
195   
196    // call local pid allocator
197    xptr_t xp_process = XPTR( client_cxy , process );
198    error = cluster_pid_alloc( xp_process , &pid ); 
199
200    // set output arguments into client RPC descriptor
201    hal_remote_sw( XPTR( client_cxy , &desc->args[0] ) , (uint64_t)error );
202    hal_remote_sw( XPTR( client_cxy , &desc->args[1] ) , (uint64_t)pid );
203}
204
205
206/////////////////////////////////////////////////////////////////////////////////////////
207// [2]           Marshaling functions attached to RPC_PROCESS_EXEC
208/////////////////////////////////////////////////////////////////////////////////////////
209
210////////////////////////////////////////////////
211void rpc_process_exec_client( cxy_t         cxy,
212                              exec_info_t * info,     // in
213                              error_t     * error )   // out
214{
215    // RPC must be remote
216    if( cxy == local_cxy ) 
217    {
218        printk("PANIC in %s : target is not remote\n", __FUNCTION__ );
219        hal_core_sleep();
220    }
221
222    // initialise RPC descriptor header
223    rpc_desc_t  rpc;
224    rpc.index    = RPC_PROCESS_EXEC;
225    rpc.response = 1;
226
227    // set input arguments in RPC descriptor 
228    rpc.args[0] = (uint64_t)(intptr_t)info;
229
230    // register RPC request in remote RPC fifo (blocking function)
231    rpc_send_sync( cxy , &rpc );
232
233    // get output arguments from RPC descriptor
234    *error  = (error_t)rpc.args[1];     
235}
236
237/////////////////////////////////////////
238void rpc_process_exec_server( xptr_t xp )
239{
240    exec_info_t * ptr;       // local pointer on remote exec_info structure
241    exec_info_t   info;      // local copy of exec_info structure
242    error_t       error;     // local error error status
243
244    // get client cluster identifier and pointer on RPC descriptor
245    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
246    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
247
248    // get pointer on exec_info structure in client cluster from RPC descriptor
249    ptr = (exec_info_t*)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
250
251    // copy exec_info structure from client buffer to server buffer
252    hal_remote_memcpy( XPTR( client_cxy , ptr ),
253                       XPTR( local_cxy , &info ),
254                       sizeof(exec_info_t) );
255
256    // call local kernel function
257    error = process_make_exec( &info ); 
258
259    // set output argument into client RPC descriptor
260    hal_remote_swd( XPTR( client_cxy , &desc->args[1] ) , (uint64_t)error );
261}
262
263
264/////////////////////////////////////////////////////////////////////////////////////////
265// [3]           Marshaling functions attached to RPC_PROCESS_KILL
266/////////////////////////////////////////////////////////////////////////////////////////
267
268///////////////////////////////////////////////////
269void rpc_process_kill_client( process_t * process ) 
270{
271    // only reference cluster can send this RPC
272    if( GET_CXY( process->ref_xp ) != local_cxy )
273    {
274        printk("PANIC in %s : caller is not the reference process\n", __FUNCTION__ );
275        hal_core_sleep();
276    }
277
278    // get local process index in reference cluster
279    lpid_t lpid = LPID_FROM_PID( process->pid );
280
281    // get local process manager pointer
282    pmgr_t * pmgr = &LOCAL_CLUSTER->pmgr;
283
284    // get number of copies
285    uint32_t copies = pmgr->copies_nr[lpid];
286
287    // initialise RPC descriptor
288    rpc_desc_t  rpc;
289    rpc.index    = RPC_PROCESS_KILL;
290    rpc.response = copies;
291    rpc.args[0]  = (uint64_t)process->pid;
292
293    // loop on list of copies to send RPC
294    xptr_t  iter;
295    XLIST_FOREACH( XPTR( local_cxy , &pmgr->copies_root[lpid] ) , iter )
296    {
297        // get cluster_identifier for current copy
298        cxy_t  target_cxy = GET_CXY( iter );
299
300        // register RPC request in remote RPC fifo ... but the reference
301        if( target_cxy != local_cxy ) rpc_send_sync( target_cxy , &rpc );
302    }
303} 
304
305/////////////////////////////////////////
306void rpc_process_kill_server( xptr_t xp )
307{
308    pid_t       pid;
309    process_t * process; 
310
311    // get client cluster identifier and pointer on RPC descriptor
312    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
313    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
314
315    // get pid argument from RPC descriptor
316    pid = (pid_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
317
318    // get process pointer to call local kernel function
319    process = cluster_get_local_process_from_pid( pid );
320
321    if( process == NULL )  // process not found => do nothing
322    {
323        printk("\n[WARNING] in %s : process %x not found in cluster %x\n",
324               __FUNCTION__ , pid , local_cxy );
325    }
326    else                   // destroy process
327    {
328        process_kill( process ); 
329    }
330} 
331
332
333/////////////////////////////////////////////////////////////////////////////////////////
334// [4]           Marshaling functions attached to RPC_THREAD_USER_CREATE               
335/////////////////////////////////////////////////////////////////////////////////////////
336
337/////////////////////////////////////////////////////////
338void rpc_thread_user_create_client( cxy_t            cxy, 
339                                    pid_t            pid,         // in
340                                    void           * start_func,  // in
341                                    void           * start_arg,   // in
342                                    pthread_attr_t * attr,        // in
343                                    xptr_t         * thread_xp,   // out
344                                    error_t        * error )      // out
345{
346    // RPC must be remote
347    if( cxy == local_cxy ) 
348    {
349        printk("\n[PANIC] in %s : target is not remote\n", __FUNCTION__ );
350        hal_core_sleep();
351    }
352
353    // initialise RPC descriptor header
354    rpc_desc_t  rpc;
355    rpc.index    = RPC_THREAD_USER_CREATE;
356    rpc.response = 1;
357
358    // set input arguments in RPC descriptor
359    rpc.args[0] = (uint64_t)pid;
360    rpc.args[1] = (uint64_t)(intptr_t)start_func;
361    rpc.args[2] = (uint64_t)(intptr_t)start_arg;
362    rpc.args[3] = (uint64_t)(intptr_t)attr;
363
364    // register RPC request in remote RPC fifo
365    rpc_send_sync( cxy , &rpc );
366
367    // get output arguments from RPC descriptor
368    *thread_xp = (xptr_t)rpc.args[4];
369    *error     = (error_t)rpc.args[5];
370}
371
372///////////////////////////////////////////////
373void rpc_thread_user_create_server( xptr_t xp )
374{
375    pthread_attr_t * attr_ptr;   // pointer on attributes structure in client cluster
376    pthread_attr_t   attr_copy;  // attributes structure  copy in server cluster
377    thread_t       * thread_ptr; // local pointer on thread descriptor
378    xptr_t           thread_xp;  // extended pointer on thread descriptor
379
380    pid_t            pid;        // process identifier
381    void           * start_func;
382    void           * start_arg;
383    error_t          error;
384
385    // get client cluster identifier and pointer on RPC descriptor
386    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
387    rpc_desc_t * desc = (rpc_desc_t *)GET_PTR( xp );
388
389    // get pointer on attributes structure in client cluster from RPC descriptor
390
391    // get input arguments from RPC descriptor
392    pid        = (pid_t)                     hal_remote_lwd(XPTR(client_cxy , &desc->args[0]));
393    start_func = (void *)(intptr_t)          hal_remote_lwd(XPTR(client_cxy , &desc->args[1]));
394    start_arg  = (void *)(intptr_t)          hal_remote_lwd(XPTR(client_cxy , &desc->args[2]));
395    attr_ptr   = (pthread_attr_t *)(intptr_t)hal_remote_lwd(XPTR(client_cxy , &desc->args[3]));
396
397    // makes a local copy of attributes structure
398    hal_remote_memcpy( XPTR( local_cxy , &attr_copy ),
399                       XPTR( client_cxy , attr_ptr ), 
400                       sizeof(pthread_attr_t) );
401   
402    assert( (attr_copy.cxy == local_cxy) , __FUNCTION__ , "bad target cluster\n" );
403
404    // call kernel function
405    error = thread_user_create( pid,
406                                start_func,
407                                start_arg,
408                                &attr_copy,
409                                &thread_ptr );
410
411    // set output arguments
412    thread_xp = XPTR( local_cxy , thread_ptr );
413    hal_remote_swd( XPTR( client_cxy , &desc->args[1] ) , (uint64_t)error );
414    hal_remote_swd( XPTR( client_cxy , &desc->args[2] ) , (uint64_t)thread_xp );
415}
416
417/////////////////////////////////////////////////////////////////////////////////////////
418// [5]           Marshaling functions attached to RPC_THREAD_KERNEL_CREATE
419/////////////////////////////////////////////////////////////////////////////////////////
420
421////////////////////////////////////////////////////
422void rpc_thread_kernel_create_client( cxy_t     cxy,
423                                      uint32_t  type,        // in
424                                      void    * func,        // in
425                                      void    * args,        // in
426                                      xptr_t  * thread_xp,   // out
427                                      error_t * error )      // out
428{
429    // RPC must be remote
430    if( cxy == local_cxy ) 
431    {
432        printk("PANIC in %s : target is not remote\n", __FUNCTION__ );
433        hal_core_sleep();
434    }
435
436    // initialise RPC descriptor header
437    rpc_desc_t  rpc;
438    rpc.index    = RPC_THREAD_KERNEL_CREATE;
439    rpc.response = 1;
440
441    // set input arguments in RPC descriptor
442    rpc.args[0] = (uint64_t)type;
443    rpc.args[1] = (uint64_t)(intptr_t)func;
444    rpc.args[2] = (uint64_t)(intptr_t)args;
445   
446    // register RPC request in remote RPC fifo
447    rpc_send_sync( cxy , &rpc );
448
449    // get output arguments from RPC descriptor
450    *thread_xp = (xptr_t)rpc.args[3];
451    *error     = (error_t)rpc.args[4];
452}
453
454/////////////////////////////////////////////////
455void rpc_thread_kernel_create_server( xptr_t xp )
456{
457    thread_t       * thread_ptr;  // local pointer on thread descriptor
458    xptr_t           thread_xp;   // extended pointer on thread descriptor
459    lid_t            core_lid;    // core local index
460    error_t          error;   
461
462    // get client cluster identifier and pointer on RPC descriptor
463    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
464    rpc_desc_t * desc = (rpc_desc_t *)GET_PTR( xp );
465
466    // get attributes from RPC descriptor
467    uint32_t  type = (uint32_t)       hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
468    void    * func = (void*)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[1] ) );
469    void    * args = (void*)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[2] ) );
470
471    // select one core
472    core_lid = cluster_select_local_core();
473
474    // call local kernel function
475    error = thread_kernel_create( &thread_ptr , type , func , args , core_lid );
476
477    // set output arguments
478    thread_xp = XPTR( local_cxy , thread_ptr );
479    hal_remote_swd( XPTR( client_cxy , &desc->args[1] ) , (uint64_t)error );
480    hal_remote_swd( XPTR( client_cxy , &desc->args[2] ) , (uint64_t)thread_xp );
481}
482
483/////////////////////////////////////////////////////////////////////////////////////////
484// [6]           Marshaling functions attached to RPC_SIGNAL_RISE
485/////////////////////////////////////////////////////////////////////////////////////////
486
487/////////////////////////////////////////////
488void rpc_signal_rise_client( cxy_t       cxy,
489                             process_t * process,    // in
490                             uint32_t    sig_id )    // in
491{
492    // RPC must be remote
493    if( cxy == local_cxy ) 
494    {
495        printk("PANIC in %s : target is not remote\n", __FUNCTION__ );
496        hal_core_sleep();
497    }
498
499    // initialise RPC descriptor header
500    rpc_desc_t  rpc;
501    rpc.index    = RPC_SIGNAL_RISE;
502    rpc.response = 1;
503
504    // set input arguments in RPC descriptor
505    rpc.args[0] = (uint64_t)(intptr_t)process;
506    rpc.args[1] = (uint64_t)sig_id;
507   
508    // register RPC request in remote RPC fifo
509    rpc_send_sync( cxy , &rpc );
510}
511
512////////////////////////////////////////                             
513void rpc_signal_rise_server( xptr_t xp )
514{
515    process_t  * process;  // local pointer on process descriptor
516    uint32_t     sig_id;   // signal index
517
518    // get client cluster identifier and pointer on RPC descriptor
519    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
520    rpc_desc_t * desc = (rpc_desc_t *)GET_PTR( xp );
521
522    // get attributes from RPC descriptor
523    process = (process_t *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
524    sig_id  = (uint32_t)             hal_remote_lwd( XPTR( client_cxy , &desc->args[1] ) );
525
526    // call local kernel function
527    signal_rise( process , sig_id );
528}
529
530/////////////////////////////////////////////////////////////////////////////////////////
531// [10]          Marshaling functions attached to RPC_VFS_INODE_CREATE
532/////////////////////////////////////////////////////////////////////////////////////////
533
534/////////////////////////////////////////////////////
535void rpc_vfs_inode_create_client( cxy_t          cxy,     
536                                  xptr_t         dentry_xp,  // in
537                                  uint32_t       fs_type,    // in
538                                  uint32_t       inode_type, // in
539                                  uint32_t       attr,       // in
540                                  uint32_t       rights,     // in
541                                  uint32_t       uid,        // in
542                                  uint32_t       gid,        // in
543                                  xptr_t       * inode_xp,   // out
544                                  error_t      * error )     // out
545{
546    // RPC must be remote
547    if( cxy == local_cxy ) 
548    {
549        printk("PANIC in %s : target cluster is not remote\n", __FUNCTION__ );
550        hal_core_sleep();
551    }
552
553    // initialise RPC descriptor header
554    rpc_desc_t  rpc;
555    rpc.index    = RPC_VFS_INODE_CREATE;
556    rpc.response = 1;
557
558    // set input arguments in RPC descriptor
559    rpc.args[0] = (uint64_t)dentry_xp;
560    rpc.args[1] = (uint64_t)fs_type;
561    rpc.args[2] = (uint64_t)inode_type;
562    rpc.args[3] = (uint64_t)attr;
563    rpc.args[4] = (uint64_t)rights;
564    rpc.args[5] = (uint64_t)uid;
565    rpc.args[6] = (uint64_t)gid;
566
567    // register RPC request in remote RPC fifo (blocking function)
568    rpc_send_sync( cxy , &rpc );
569
570    // get output values from RPC descriptor
571    *inode_xp = (xptr_t)rpc.args[7];
572    *error    = (error_t)rpc.args[8];
573}
574
575/////////////////////////////////////////////
576void rpc_vfs_inode_create_server( xptr_t xp )
577{
578    xptr_t           dentry_xp;
579    uint32_t         fs_type;
580    uint32_t         inode_type;
581    uint32_t         attr;
582    uint32_t         rights;
583    uint32_t         uid;
584    uint32_t         gid;
585    xptr_t           inode_xp;
586    error_t          error;
587
588    // get client cluster identifier and pointer on RPC descriptor
589    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
590    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
591
592    // get input arguments from client rpc descriptor
593    dentry_xp  = (xptr_t)  hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
594    fs_type    = (uint32_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[1] ) );
595    inode_type = (uint32_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[2] ) );
596    attr       = (uint32_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[3] ) );
597    rights     = (uint32_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[4] ) );
598    uid        = (uid_t)   hal_remote_lwd( XPTR( client_cxy , &desc->args[5] ) );
599    gid        = (gid_t)   hal_remote_lwd( XPTR( client_cxy , &desc->args[6] ) );
600
601    // call local kernel function
602    error = vfs_inode_create( dentry_xp,
603                              fs_type,
604                              inode_type,
605                              attr,
606                              rights,
607                              uid,
608                              gid,
609                              &inode_xp );
610
611    // set output arguments
612    hal_remote_swd( XPTR( client_cxy , &desc->args[7] ) , (uint64_t)inode_xp );
613    hal_remote_swd( XPTR( client_cxy , &desc->args[8] ) , (uint64_t)error );
614}
615
616/////////////////////////////////////////////////////////////////////////////////////////
617// [11]          Marshaling functions attached to RPC_VFS_INODE_DESTROY
618/////////////////////////////////////////////////////////////////////////////////////////
619
620/////////////////////////////////////////////////////////////
621void rpc_vfs_inode_destroy_client( cxy_t                cxy,
622                                   struct vfs_inode_s * inode )
623{
624    // RPC must be remote
625    if( cxy == local_cxy ) 
626    {
627        printk("PANIC in %s : target cluster is not remote\n", __FUNCTION__ );
628        hal_core_sleep();
629    }
630
631    // initialise RPC descriptor header
632    rpc_desc_t  rpc;
633    rpc.index    = RPC_VFS_INODE_DESTROY;
634    rpc.response = 1;
635
636    // set input arguments in RPC descriptor
637    rpc.args[0] = (uint64_t)(intptr_t)inode;
638   
639    // register RPC request in remote RPC fifo (blocking function)
640    rpc_send_sync( cxy , &rpc );
641}
642
643//////////////////////////////////////////////
644void rpc_vfs_inode_destroy_server( xptr_t xp )
645{
646    vfs_inode_t * inode;
647
648    // get client cluster identifier and pointer on RPC descriptor
649    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
650    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
651
652    // get arguments "inode" from client RPC descriptor
653    inode = (vfs_inode_t *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
654                       
655    // call local kernel function
656    vfs_inode_destroy( inode );
657}
658
659/////////////////////////////////////////////////////////////////////////////////////////
660// [12]          Marshaling functions attached to RPC_VFS_DENTRY_CREATE
661/////////////////////////////////////////////////////////////////////////////////////////
662
663//////////////////////////////////////////////////////////////
664void rpc_vfs_dentry_create_client( cxy_t                  cxy,
665                                   uint32_t               type,         // in
666                                   char                 * name,         // in
667                                   struct vfs_inode_s   * parent,       // in
668                                   xptr_t               * dentry_xp,    // out
669                                   error_t              * error )       // out
670{
671    // RPC must be remote
672    if( cxy == local_cxy ) 
673    {
674        printk("PANIC in %s : target cluster is not remote\n", __FUNCTION__ );
675        hal_core_sleep();
676    }
677
678    // initialise RPC descriptor header
679    rpc_desc_t  rpc;
680    rpc.index    = RPC_VFS_DENTRY_CREATE;
681    rpc.response = 1;
682
683    // set input arguments in RPC descriptor
684    rpc.args[0] = (uint64_t)type;
685    rpc.args[1] = (uint64_t)(intptr_t)name;
686    rpc.args[2] = (uint64_t)strlen( name );
687    rpc.args[3] = (uint64_t)(intptr_t)parent;
688
689    // register RPC request in remote RPC fifo (blocking function)
690    rpc_send_sync( cxy , &rpc );
691
692    // get output values from RPC descriptor
693    *dentry_xp = (xptr_t)rpc.args[4];
694    *error     = (error_t)rpc.args[5];
695}
696
697//////////////////////////////////////////////
698void rpc_vfs_dentry_create_server( xptr_t xp )
699{
700    uint32_t      type;
701    char        * name;
702    vfs_inode_t * parent;
703    xptr_t        dentry_xp;
704    char          name_copy[CONFIG_VFS_MAX_NAME_LENGTH];
705    uint32_t      length;
706    error_t       error;
707
708    // get client cluster identifier and pointer on RPC descriptor
709    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
710    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
711
712    // get argument "name" & "length" from client RPC descriptor and makes a local copy
713    name   = (char *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[1] ) );
714    length = (uint32_t)        hal_remote_lwd( XPTR( client_cxy , &desc->args[2] ) );
715    hal_remote_memcpy( XPTR( local_cxy , name_copy ),
716                       XPTR( client_cxy , name ),
717                                           length + 1 );               // +1 for the NUL char
718
719    // get arguments "type" and "parent" from client RPC descriptor
720    type   = (uint32_t)               hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
721    parent = (vfs_inode_t *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[3] ) );
722                       
723    // call local kernel function
724    error = vfs_dentry_create( type,
725                               name_copy,
726                               parent,
727                               &dentry_xp );
728 
729    // set output arguments
730    hal_remote_swd( XPTR( client_cxy , &desc->args[4] ) , (uint64_t)dentry_xp );
731    hal_remote_swd( XPTR( client_cxy , &desc->args[5] ) , (uint64_t)error );
732}
733
734/////////////////////////////////////////////////////////////////////////////////////////
735// [13]          Marshaling functions attached to RPC_VFS_DENTRY_DESTROY
736/////////////////////////////////////////////////////////////////////////////////////////
737
738///////////////////////////////////////////////////////
739void rpc_vfs_dentry_destroy_client( cxy_t          cxy,
740                                    vfs_dentry_t * dentry )
741{
742    // RPC must be remote
743    if( cxy == local_cxy ) 
744    {
745        printk("PANIC in %s : target cluster is not remote\n", __FUNCTION__ );
746        hal_core_sleep();
747    }
748
749    // initialise RPC descriptor header
750    rpc_desc_t  rpc;
751    rpc.index    = RPC_VFS_DENTRY_DESTROY;
752    rpc.response = 1;
753
754    // set input arguments in RPC descriptor
755    rpc.args[0] = (uint64_t)(intptr_t)dentry;
756   
757    // register RPC request in remote RPC fifo (blocking function)
758    rpc_send_sync( cxy , &rpc );
759}
760
761///////////////////////////////////////////////
762void rpc_vfs_dentry_destroy_server( xptr_t xp )
763{
764    vfs_dentry_t * dentry;
765
766    // get client cluster identifier and pointer on RPC descriptor
767    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
768    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
769
770    // get arguments "dentry" from client RPC descriptor
771    dentry = (vfs_dentry_t *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
772                       
773    // call local kernel function
774    vfs_dentry_destroy( dentry );
775}
776
777
778/////////////////////////////////////////////////////////////////////////////////////////
779// [14]          Marshaling functions attached to RPC_VFS_FILE_CREATE
780/////////////////////////////////////////////////////////////////////////////////////////
781
782//////////////////////////////////////////////////////////////
783void rpc_vfs_file_create_client( cxy_t                  cxy,
784                                 struct vfs_inode_s   * inode,       // in
785                                 uint32_t               file_attr,   // in
786                                 xptr_t               * file_xp,     // out
787                                 error_t              * error )      // out
788{
789    // RPC must be remote
790    if( cxy == local_cxy ) 
791    {
792        printk("PANIC in %s : target cluster is not remote\n", __FUNCTION__ );
793        hal_core_sleep();
794    }
795
796    // initialise RPC descriptor header
797    rpc_desc_t  rpc;
798    rpc.index    = RPC_VFS_FILE_CREATE;
799    rpc.response = 1;
800
801    // set input arguments in RPC descriptor
802    rpc.args[0] = (uint64_t)(intptr_t)inode;
803    rpc.args[1] = (uint64_t)file_attr;
804
805    // register RPC request in remote RPC fifo (blocking function)
806    rpc_send_sync( cxy , &rpc );
807
808    // get output values from RPC descriptor
809    *file_xp = (xptr_t)rpc.args[2];
810    *error   = (error_t)rpc.args[3];
811}
812
813////////////////////////////////////////////
814void rpc_vfs_file_create_server( xptr_t xp )
815{
816    uint32_t      file_attr;
817    vfs_inode_t * inode;
818    xptr_t        file_xp;
819    error_t       error;
820
821    // get client cluster identifier and pointer on RPC descriptor
822    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
823    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
824
825    // get arguments "file_attr" and "inode" from client RPC descriptor
826    inode     = (vfs_inode_t *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
827    file_attr = (uint32_t)               hal_remote_lwd( XPTR( client_cxy , &desc->args[1] ) );
828                       
829    // call local kernel function
830    error = vfs_file_create( inode,
831                             file_attr,
832                             &file_xp );
833 
834    // set output arguments
835    hal_remote_swd( XPTR( client_cxy , &desc->args[2] ) , (uint64_t)file_xp );
836    hal_remote_swd( XPTR( client_cxy , &desc->args[3] ) , (uint64_t)error );
837}
838
839/////////////////////////////////////////////////////////////////////////////////////////
840// [15]          Marshaling functions attached to RPC_VFS_FILE_DESTROY
841/////////////////////////////////////////////////////////////////////////////////////////
842
843///////////////////////////////////////////////////
844void rpc_vfs_file_destroy_client( cxy_t        cxy,
845                                  vfs_file_t * file )
846{
847    // RPC must be remote
848    if( cxy == local_cxy ) 
849    {
850        printk("PANIC in %s : target cluster is not remote\n", __FUNCTION__ );
851        hal_core_sleep();
852    }
853
854    // initialise RPC descriptor header
855    rpc_desc_t  rpc;
856    rpc.index    = RPC_VFS_FILE_DESTROY;
857    rpc.response = 1;
858
859    // set input arguments in RPC descriptor
860    rpc.args[0] = (uint64_t)(intptr_t)file;
861   
862    // register RPC request in remote RPC fifo (blocking function)
863    rpc_send_sync( cxy , &rpc );
864}
865
866/////////////////////////////////////////////
867void rpc_vfs_file_destroy_server( xptr_t xp )
868{
869    vfs_file_t * file;
870
871    // get client cluster identifier and pointer on RPC descriptor
872    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
873    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
874
875    // get arguments "dentry" from client RPC descriptor
876    file = (vfs_file_t *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
877                       
878    // call local kernel function
879    vfs_file_destroy( file );
880}
881
882/////////////////////////////////////////////////////////////////////////////////////////
883// [16]          Marshaling functions attached to RPC_FATFS_GET_CLUSTER
884/////////////////////////////////////////////////////////////////////////////////////////
885
886//////////////////////////////////////////////////
887void rpc_fatfs_get_cluster_client( cxy_t      cxy,
888                                   mapper_t * mapper,    // in
889                                   uint32_t   first,     // in
890                                   uint32_t   page,      // in
891                                   uint32_t * cluster,   // out
892                                   error_t  * error )    // out
893{
894    // RPC must be remote
895    if( cxy == local_cxy ) 
896    {
897        printk("PANIC in %s : target is not remote\n", __FUNCTION__ );
898        hal_core_sleep();
899    }
900
901    // initialise RPC descriptor header
902    rpc_desc_t  rpc;
903    rpc.index    = RPC_FATFS_GET_CLUSTER;
904    rpc.response = 1;
905
906    // set input arguments in RPC descriptor
907    rpc.args[0] = (uint64_t)(intptr_t)mapper;
908    rpc.args[1] = (uint64_t)first;
909    rpc.args[2] = (uint64_t)page;
910
911    // register RPC request in remote RPC fifo
912    rpc_send_sync( cxy , &rpc );
913
914    // get output argument from rpc descriptor
915    *cluster = (uint32_t)rpc.args[3];
916    *error   = (error_t)rpc.args[4];
917}
918
919//////////////////////////////////////////////
920void rpc_fatfs_get_cluster_server( xptr_t xp )
921{
922    mapper_t    * mapper;
923    uint32_t      first;
924    uint32_t      page;
925    uint32_t      cluster;
926    error_t       error;
927
928    // get client cluster identifier and pointer on RPC descriptor
929    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
930    rpc_desc_t * desc = (rpc_desc_t *)GET_PTR( xp );
931
932    // get input arguments
933    mapper = (mapper_t *)(intptr_t)hal_remote_lpt( XPTR( client_cxy , &desc->args[0] ) );
934    first  = (uint32_t)            hal_remote_lw ( XPTR( client_cxy , &desc->args[1] ) );
935    page   = (uint32_t)            hal_remote_lw ( XPTR( client_cxy , &desc->args[2] ) );
936
937    // call the kernel function
938    error = fatfs_get_cluster( mapper , first , page , &cluster );
939
940    // set output argument
941    hal_remote_swd( XPTR( client_cxy , &desc->args[3] ) , (uint64_t)cluster );
942    hal_remote_swd( XPTR( client_cxy , &desc->args[4] ) , (uint64_t)error );
943}
944
945/////////////////////////////////////////////////////////////////////////////////////////
946// [20]          Marshaling functions attached to RPC_VMM_GET_REF_VSEG
947/////////////////////////////////////////////////////////////////////////////////////////
948
949//////////////////////////////////////////////////
950void rpc_vmm_get_ref_vseg_client( cxy_t       cxy,     
951                                  process_t * process,     // in 
952                                  intptr_t    vaddr,       // in 
953                                  xptr_t    * vseg_xp )    // out
954{
955    // RPC must be remote
956    if( cxy == local_cxy ) 
957    {
958        printk("PANIC in %s : target is not remote\n", __FUNCTION__ );
959        hal_core_sleep();
960    }
961
962    // initialise RPC descriptor header
963    rpc_desc_t  rpc;
964    rpc.index    = RPC_VMM_GET_REF_VSEG;
965    rpc.response = 1;
966
967    // set input arguments in RPC descriptor
968    rpc.args[0] = (uint64_t)(intptr_t)process;
969    rpc.args[1] = (uint64_t)vaddr;
970
971    // register RPC request in remote RPC fifo (blocking function)
972    rpc_send_sync( cxy , &rpc );
973
974    // get output argument from rpc descriptor
975    *vseg_xp = rpc.args[2];
976}
977
978/////////////////////////////////////////////
979void rpc_vmm_get_ref_vseg_server( xptr_t xp )
980{
981    process_t   * process;
982    intptr_t      vaddr;
983    vseg_t      * vseg_ptr;
984    xptr_t        vseg_xp;
985
986    // get client cluster identifier and pointer on RPC descriptor
987    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
988    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
989
990    // get input argument from client RPC descriptor
991    process = (process_t *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
992    vaddr   = (intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[1] ) );
993   
994    // call local kernel function
995    vseg_ptr = vmm_get_vseg( process , vaddr );
996
997    // set output argument to client RPC descriptor
998    if( vseg_ptr == NULL ) vseg_xp = XPTR_NULL;
999    else                   vseg_xp = XPTR( local_cxy , vseg_ptr );
1000    hal_remote_swd( XPTR( client_cxy , &desc->args[2] ) , (uint64_t)vseg_xp );
1001}
1002
1003
1004/////////////////////////////////////////////////////////////////////////////////////////
1005// [21]          Marshaling functions attached to RPC_VMM_GET_PTE
1006/////////////////////////////////////////////////////////////////////////////////////////
1007
1008////////////////////////////////////////////
1009void rpc_vmm_get_pte_client( cxy_t       cxy,   
1010                             process_t * process,  // in
1011                             vpn_t       vpn,      // in
1012                             uint32_t  * attr,     // out
1013                             ppn_t     * ppn,      // out
1014                             error_t   * error )   // out
1015{
1016    // RPC must be remote
1017    if( cxy == local_cxy ) 
1018    {
1019        printk("PANIC in %s : target is not remote\n", __FUNCTION__ );
1020        hal_core_sleep();
1021    }
1022
1023    // initialise RPC descriptor header
1024    rpc_desc_t  rpc;
1025    rpc.index    = RPC_VMM_GET_PTE;
1026    rpc.response = 1;
1027
1028    // set input arguments in RPC descriptor
1029    rpc.args[0] = (uint64_t)(intptr_t)process;
1030    rpc.args[1] = (uint64_t)vpn;
1031
1032    // register RPC request in remote RPC fifo (blocking function)
1033    rpc_send_sync( cxy , &rpc );
1034
1035    // get output argument from rpc descriptor
1036    *attr  = (uint32_t)rpc.args[2];
1037    *ppn   = (ppn_t)rpc.args[3];
1038    *error = (error_t)rpc.args[4];
1039}
1040
1041////////////////////////////////////////
1042void rpc_vmm_get_pte_server( xptr_t xp )
1043{
1044    process_t   * process;
1045    vpn_t         vpn;
1046    uint32_t      attr;
1047    ppn_t         ppn;
1048    error_t       error;
1049
1050    // get client cluster identifier and pointer on RPC descriptor
1051    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
1052    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
1053
1054    // get input argument "process" & "vpn" from client RPC descriptor
1055    process = (process_t *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
1056    vpn     = (vpn_t)                hal_remote_lwd( XPTR( client_cxy , &desc->args[1] ) );
1057   
1058    // call local kernel function
1059    error = vmm_get_pte( process , vpn , &attr , &ppn ); 
1060
1061    // set output argument "attr" & "ppn" to client RPC descriptor
1062    hal_remote_swd( XPTR( client_cxy , &desc->args[2] ) , (uint64_t)attr );
1063    hal_remote_swd( XPTR( client_cxy , &desc->args[3] ) , (uint64_t)ppn );
1064    hal_remote_swd( XPTR( client_cxy , &desc->args[4] ) , (uint64_t)error );
1065}
1066
1067/////////////////////////////////////////////////////////////////////////////////////////
1068// [22]          Marshaling functions attached to RPC_KCM_ALLOC
1069/////////////////////////////////////////////////////////////////////////////////////////
1070
1071//////////////////////////////////////////
1072void rpc_kcm_alloc_client( cxy_t      cxy,
1073                           uint32_t   kmem_type,   // in
1074                           xptr_t *   buf_xp )     // out
1075{
1076    // RPC must be remote
1077    if( cxy == local_cxy ) 
1078    {
1079        printk("PANIC in %s : target is not remote\n", __FUNCTION__ );
1080        hal_core_sleep();
1081    }
1082
1083    // initialise RPC descriptor header
1084    rpc_desc_t  rpc;
1085    rpc.index    = RPC_THREAD_USER_CREATE;
1086    rpc.response = 1;
1087
1088    // set input arguments in RPC descriptor
1089    rpc.args[0] = (uint64_t)kmem_type;
1090
1091    // register RPC request in remote RPC fifo
1092    rpc_send_sync( cxy , &rpc );
1093
1094    // get output arguments from RPC descriptor
1095    *buf_xp = (xptr_t)rpc.args[1];
1096}
1097
1098//////////////////////////////////////
1099void rpc_kcm_alloc_server( xptr_t xp )
1100{
1101    // get client cluster identifier and pointer on RPC descriptor
1102    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
1103    rpc_desc_t * desc = (rpc_desc_t *)GET_PTR( xp );
1104
1105    // get input argument "kmem_type" from client RPC descriptor
1106    uint32_t kmem_type = (uint32_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
1107
1108    // allocates memory for kcm
1109    kmem_req_t  req;
1110    req.type  = kmem_type;
1111    req.flags = AF_ZERO;
1112    void * buf_ptr = kmem_alloc( &req );
1113
1114    // set output argument
1115    xptr_t buf_xp = XPTR( local_cxy , buf_ptr );
1116    hal_remote_swd( XPTR( client_cxy , &desc->args[1] ) , (uint64_t)buf_xp );
1117}   
1118
1119/////////////////////////////////////////////////////////////////////////////////////////
1120// [23]          Marshaling functions attached to RPC_KCM_FREE
1121/////////////////////////////////////////////////////////////////////////////////////////
1122
1123/////////////////////////////////////////
1124void rpc_kcm_free_client( cxy_t      cxy,
1125                          void     * buf,          // in
1126                          uint32_t   kmem_type )   // in
1127{
1128    // RPC must be remote
1129    if( cxy == local_cxy ) 
1130    {
1131        printk("PANIC in %s : target is not remote\n", __FUNCTION__ );
1132        hal_core_sleep();
1133    }
1134
1135    // initialise RPC descriptor header
1136    rpc_desc_t  rpc;
1137    rpc.index    = RPC_THREAD_USER_CREATE;
1138    rpc.response = 1;
1139
1140    // set input arguments in RPC descriptor
1141    rpc.args[0] = (uint64_t)(intptr_t)buf;
1142    rpc.args[1] = (uint64_t)kmem_type;
1143
1144    // register RPC request in remote RPC fifo
1145    rpc_send_sync( cxy , &rpc );
1146}
1147
1148/////////////////////////////////////
1149void rpc_kcm_free_server( xptr_t xp )
1150{
1151    // get client cluster identifier and pointer on RPC descriptor
1152    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
1153    rpc_desc_t * desc = (rpc_desc_t *)GET_PTR( xp );
1154
1155    // get input arguments "buf" and "kmem_type" from client RPC descriptor
1156    void     * buf = (void *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
1157    uint32_t   kmem_type = (uint32_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[1] ) );
1158
1159    // releases memory
1160    kmem_req_t  req;
1161    req.type = kmem_type;
1162    req.ptr  = buf;
1163    kmem_free( &req );
1164}   
1165
1166/////////////////////////////////////////////////////////////////////////////////////////
1167// [24]          Marshaling functions attached to RPC_MAPPER_MOVE
1168/////////////////////////////////////////////////////////////////////////////////////////
1169
1170///////////////////////////////////////////
1171void rpc_mapper_move_client( cxy_t      cxy,
1172                             mapper_t * mapper,        // in
1173                             uint32_t   to_buffer,     // in
1174                             uint32_t   file_offset,   // in
1175                             void     * buffer,        // in
1176                             uint32_t   size,          // in
1177                             error_t  * error )        // out
1178{
1179    // RPC must be remote
1180    if( cxy == local_cxy ) 
1181    {
1182        printk("PANIC in %s : target cluster is not remote\n", __FUNCTION__ );
1183        hal_core_sleep();
1184    }
1185
1186    // initialise RPC descriptor header
1187    rpc_desc_t  rpc;
1188    rpc.index    = RPC_MAPPER_MOVE;
1189    rpc.response = 1;
1190
1191    // set input arguments in RPC descriptor
1192    rpc.args[0] = (uint64_t)(intptr_t)mapper;
1193    rpc.args[1] = (uint64_t)to_buffer;
1194    rpc.args[2] = (uint64_t)file_offset;
1195    rpc.args[3] = (uint64_t)(intptr_t)buffer;
1196    rpc.args[4] = (uint64_t)size;
1197
1198    // register RPC request in remote RPC fifo (blocking function)
1199    rpc_send_sync( cxy , &rpc );
1200
1201    // get output values from RPC descriptor
1202    *error     = (error_t)rpc.args[5];
1203}
1204
1205////////////////////////////////////////
1206void rpc_mapper_move_server( xptr_t xp )
1207{
1208    mapper_t * mapper;
1209    uint32_t   to_buffer;
1210    uint32_t   file_offset;
1211    void     * buffer;
1212    uint32_t   size;
1213    error_t    error;
1214
1215    // get client cluster identifier and pointer on RPC descriptor
1216    cxy_t        client_cxy  = (cxy_t)GET_CXY( xp );
1217    rpc_desc_t * desc        = (rpc_desc_t *)GET_PTR( xp );
1218
1219    // get arguments from client RPC descriptor
1220    mapper      = (mapper_t *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[0] ) );
1221    to_buffer   =                       hal_remote_lwd( XPTR( client_cxy , &desc->args[1] ) );
1222    file_offset =                       hal_remote_lwd( XPTR( client_cxy , &desc->args[2] ) );
1223    buffer      = (void     *)(intptr_t)hal_remote_lwd( XPTR( client_cxy , &desc->args[3] ) );
1224    size        =                       hal_remote_lwd( XPTR( client_cxy , &desc->args[4] ) );
1225
1226    // call local kernel function
1227    error = mapper_move( mapper,
1228                         to_buffer,
1229                         file_offset,
1230                         buffer,
1231                         size );
1232
1233    // set output argument to client RPC descriptor
1234    hal_remote_swd( XPTR( client_cxy , &desc->args[5] ) , (uint64_t)error );
1235}
1236
1237/***************************************************************************************/
1238/************ Generic functions supporting RPCs : client side **************************/
1239/***************************************************************************************/
1240
1241////////////////////////////////////////////
1242void rpc_send_sync( cxy_t        server_cxy, 
1243                    rpc_desc_t * rpc )
1244{
1245        thread_t * this = CURRENT_THREAD;
1246    uint32_t   cores;
1247    error_t    error;
1248    bool_t     first;
1249    reg_t      sr_save;
1250
1251printk("\n@@@ coucou 0\n");
1252
1253    // get client CPU and cluster coordinates
1254    cxy_t      client_cxy = local_cxy;   
1255    lid_t      client_lid = CURRENT_CORE->lid;
1256
1257    // allocate and initialise an extended pointer on the RPC descriptor
1258        xptr_t   xp = XPTR( client_cxy , rpc );
1259
1260    // get local pointer on rpc_fifo in remote cluster with the
1261    // assumption that addresses are identical in all clusters
1262    rpc_fifo_t * rf = &LOCAL_CLUSTER->rpc_fifo;
1263
1264        // try to post an item in remote fifo
1265    // deschedule and retry if remote fifo full
1266    do
1267    { 
1268        error = remote_fifo_put_item( XPTR( server_cxy , &rf->fifo ),
1269                                      (void *)&xp,
1270                                      sizeof(xptr_t), 
1271                                      &first );
1272
1273            if ( error ) 
1274        {
1275            printk("\n[WARNING] %s : core %d in cluster %x cannot post RPC to cluster %x\n",
1276                   __FUNCTION__ , client_lid , client_cxy , server_cxy );
1277            if( thread_can_yield() ) sched_yield();
1278        }
1279    }
1280    while( error );
1281 
1282printk("\n@@@ coucou 1\n");
1283
1284    rpc_dmsg("\n[INFO] %s on core %d in cluster %x sent RPC %p to cluster %x\n", 
1285              __FUNCTION__ , client_lid , client_cxy , rpc , server_cxy );
1286       
1287    // send IPI if this is the first RPC in remote FIFO
1288    // and no CPU is in kernel mode in server cluster.
1289    // the selected CPU in server has the same lid as the client CPU.
1290        if( first )
1291        {
1292        // get number of cores in kernel mode in server cluster
1293        cores = hal_remote_lw( XPTR( server_cxy , &LOCAL_CLUSTER->cores_in_kernel ) );
1294
1295                if( cores == 0 ) // no core in kernel mode in server
1296                {
1297                    dev_icu_send_ipi( server_cxy , client_lid );
1298
1299                    rpc_dmsg("\n[INFO] %s : core %d in cluster %x send IPI to core %d in cluster %x\n",
1300                      __FUNCTION__, client_lid , client_cxy , client_lid , server_cxy );
1301        }
1302        }
1303
1304printk("\n@@@ coucou 2\n");
1305
1306        // activate preemption to allow incoming RPC and avoid deadlock
1307        if( this->type == THREAD_RPC ) hal_enable_irq( &sr_save );
1308
1309    // the sending thread poll the response slot until RPC completed
1310        while( 1 )
1311    {
1312        if( rpc->response == 0 ) break;
1313    }
1314
1315printk("\n@@@ coucou 3\n");
1316
1317    // restore preemption
1318        if( this->type == THREAD_RPC ) hal_restore_irq( sr_save );
1319
1320}  // end rpc_send_sync()
1321
1322
1323
1324/***************************************************************************************/
1325/************ Generic functions supporting RPCs : server side **************************/
1326/***************************************************************************************/
1327
1328///////////////////////////////////////////
1329void rpc_fifo_init( rpc_fifo_t * rpc_fifo )
1330{
1331        rpc_fifo->count       = 0;
1332        rpc_fifo->owner       = 0;
1333        local_fifo_init( &rpc_fifo->fifo );
1334}
1335
1336////////////////////////////////////////////////
1337error_t rpc_execute_all( rpc_fifo_t * rpc_fifo )
1338{
1339        xptr_t         xp;             // extended pointer on RPC descriptor
1340        uint32_t       count;          // handled RPC request counter
1341        thread_t     * this;           // pointer on this RPC thread
1342    core_t       * core;           // pointer on core running this thread
1343    rpc_desc_t   * desc;           // pointer on RPC descriptor
1344    uint32_t       index;          // RPC index
1345    uint32_t       expected;       // number of expected responses
1346    cxy_t          client_cxy;     // client cluster identifier
1347        error_t        error;
1348     
1349        this = CURRENT_THREAD; 
1350    core = this->core;   
1351
1352
1353    // handle up to CONFIG_RPC_PENDING_MAX requests before exit
1354        count = 0;
1355        do
1356    {
1357            error = local_fifo_get_item( &rpc_fifo->fifo, 
1358                                     &xp,
1359                                     sizeof(xptr_t) );
1360
1361                if ( error == 0 )  // One RPC request successfully extracted from RPC_FIFO
1362        {
1363            rpc_dmsg("\n[INFO] %s : RPC_THREAD %x on core %x in cluster %x handles RPC %d\n"
1364                                     __FUNCTION__ , this->trdid , core->lid , local_cxy , count );
1365
1366            // get client cluster identifier and pointer on RPC descriptor
1367            client_cxy = (cxy_t)GET_CXY( xp );
1368            desc       = (rpc_desc_t *)GET_PTR( xp );
1369
1370            // get rpc index and expected responses from RPC descriptor
1371                index     = hal_remote_lw( XPTR( client_cxy , &desc->index ) );
1372                expected  = hal_remote_lw( XPTR( client_cxy , &desc->response ) );
1373
1374            // call the relevant server function
1375            rpc_server[index]( xp );
1376
1377            // increment handled RPC counter
1378                count++;
1379
1380            // notify RPC completion as required
1381            if( expected == 1 ) hal_remote_sw( XPTR(client_cxy,&desc->response) , 0 );
1382            if( expected >  1 ) hal_remote_atomic_add( XPTR(client_cxy,&desc->response) , -1 );
1383                }
1384       
1385                // exit loop in three cases:
1386        // - fifo is empty
1387        // - look has been released (because descheduling)
1388        // - max number of RPCs has been reached
1389                if( error || 
1390            (rpc_fifo->owner != this->trdid) || 
1391            (count > CONFIG_RPC_PENDING_MAX) ) break;
1392        }
1393    while( 1 )
1394
1395        rpc_dmsg("\n[INFO] %s running on core %d in cluster %x exit\n"
1396              __FUNCTION__ , CURRENT_CORE->lid , local_cxy );
1397               
1398    // update RPC_FIFO global counter
1399        rpc_fifo->count += count;
1400
1401        return 0;
1402}  // end rpc_execute_all()
1403
1404////////////////////////////////////////////////////
1405error_t rpc_activate_thread( rpc_fifo_t * rpc_fifo )
1406{
1407        core_t      * core;
1408        thread_t    * thread;
1409        thread_t    * this;
1410    scheduler_t * sched;
1411        error_t       error;
1412    bool_t        found;
1413    reg_t         sr_save;
1414
1415        this   = CURRENT_THREAD;
1416    core   = this->core;
1417    sched  = &core->scheduler;
1418    found  = false;
1419
1420    // calling thread must be the RPC_FIFO owner
1421    if( this->trdid != rpc_fifo->owner )
1422    {
1423        printk("\n[PANIC] in %s : calling thread is not RPC_FIFO owner\n", __FUNCTION__ );
1424        hal_core_sleep();
1425    }
1426
1427    // makes the calling thread not preemptable
1428    // during activation / creation of the RPC thread
1429        hal_disable_irq( &sr_save );
1430
1431    // search a free RPC thread (must be in THREAD_BLOCKED_IDLE state)   
1432    list_entry_t * iter;
1433    LIST_FOREACH( &sched->k_root , iter )
1434    {
1435        thread = LIST_ELEMENT( iter , thread_t , sched_list );
1436        if( (thread->type == THREAD_RPC) && (thread->blocked ==  THREAD_BLOCKED_IDLE ) ) 
1437        {
1438            found = true;
1439            break;
1440        }
1441    }
1442
1443    if( found )                    // activate this idle RPC thread     
1444    {
1445        thread->blocked = 0;
1446    }
1447    else                           // create a new RPC thread
1448    {
1449        error = thread_kernel_create( &thread,
1450                                      THREAD_RPC, 
1451                                                  &rpc_thread_func, 
1452                                      NULL,
1453                                                  core->lid );
1454            if( error ) 
1455        {
1456                hal_restore_irq( sr_save );
1457            printk("\n[ERROR] in %s : no memory for new RPC thread in cluster %x\n",
1458                   __FUNCTION__ , local_cxy );
1459            return ENOMEM;
1460        }
1461
1462        rpc_dmsg("\n[INFO] %s creates RPC thread %x on core %x in cluster %x at cycle %d\n", 
1463                          __FUNCTION__ , thread , core->gid , local_cxy , hal_time_stamp() );
1464
1465        // update core descriptor counter 
1466            core->rpc_threads++;
1467    }
1468
1469    // update owner in rpc_fifo
1470    rpc_fifo->owner = thread->trdid;
1471
1472    rpc_dmsg ("\n[INFO] %s activates RPC thread %x on core %x in cluster %x at cycle %d\n",
1473                      __FUNCTION__ , thread , core->gid , local_cxy , hal_time_stamp() );
1474
1475    // current thread deschedules / RPC thread start execution
1476        sched_switch_to( thread );
1477
1478    // restore IRQs for the calling thread
1479        hal_restore_irq( sr_save );
1480
1481    // return success
1482        return 0;
1483
1484}  // end rpc_activate_thread()
1485
1486//////////////////
1487bool_t rpc_check()
1488{
1489        thread_t   * this     = CURRENT_THREAD;
1490        rpc_fifo_t * rpc_fifo = &LOCAL_CLUSTER->rpc_fifo;
1491    error_t      error;
1492
1493    // calling thread does nothing if light lock already taken or FIFO empty 
1494        if( (rpc_fifo->owner != 0) || (local_fifo_is_empty( &rpc_fifo->fifo )) )
1495    {
1496        return false;
1497    }
1498
1499        // calling thread tries to take the light lock,
1500    // and activates an RPC thread if success
1501    if( hal_atomic_test_set( &rpc_fifo->owner , this->trdid ) )
1502        {
1503        error = rpc_activate_thread( rpc_fifo );
1504
1505        if( error )    // cannot activate an RPC_THREAD
1506        {
1507            rpc_fifo->owner = 0;
1508
1509            printk("\n[ERROR] in %s : no memory to create a RPC thread for core %d"
1510                   " in cluster %x => do nothing\n",
1511                   __FUNCTION__ , CURRENT_CORE->lid , local_cxy );
1512        }
1513
1514        return true;
1515    }
1516    else  // light lock taken by another thread
1517    {
1518        return false;
1519    }
1520} // end rpc_check()
1521
1522
1523//////////////////////
1524void rpc_thread_func()
1525{
1526    // makes the calling thread not preemptable
1527        hal_disable_irq( NULL );
1528 
1529        thread_t   * this     = CURRENT_THREAD;
1530        rpc_fifo_t * rpc_fifo = &LOCAL_CLUSTER->rpc_fifo;
1531
1532    rpc_dmsg("\n[INFO] RPC thread %x created on core %d in cluster %x at cycle %d\n",
1533             this->trdid , this->core->lid , local_cxy , hal_time_stamp() );
1534
1535    // this infinite loop is not preemptable
1536    // the RPC thread deschedule when the RPC_FIFO is empty
1537        while(1)
1538        {
1539        // check fifo ownership (ownership should be given by rpc_activate()
1540        if( this->trdid != rpc_fifo->owner )
1541        {
1542            printk("\n[PANIC] in %s : RPC_THREAD %x not owner of RPC_FIFO in cluster %x\n",
1543                   __FUNCTION__ , this->trdid , local_cxy );
1544            hal_core_sleep();
1545        }
1546 
1547        // executes pending RPC(s)
1548        rpc_execute_all( rpc_fifo );
1549
1550        // release rpc_fifo ownership (can be lost during RPC execution)
1551        if( rpc_fifo->owner == this->trdid ) rpc_fifo->owner = 0;
1552
1553
1554        // suicide if too much RPC threads for this core
1555                if( this->core->rpc_threads > CONFIG_RPC_THREADS_MAX ) 
1556                {
1557            rpc_dmsg("\n[INFO] RPC thread %x suicide on core %d in cluster %x at cycle %d\n", 
1558                             this->trdid , this->core->lid , local_cxy , hal_time_stamp() );
1559
1560            // update core descriptor counter
1561                        this->core->rpc_threads--;
1562
1563            // suicide
1564                        thread_exit();
1565                }
1566
1567        // block and deschedule
1568        rpc_dmsg("\n[INFO] RPC thread %x deschedule on core %d in cluster %x at cycle %d\n", 
1569                              this->trdid , this->core->lid , local_cxy , hal_time_stamp() ); 
1570
1571                thread_block( this , THREAD_BLOCKED_IDLE );
1572        sched_yield();
1573
1574                rpc_dmsg("\n[INFO] RPC thread %x wake up on core %d in cluster %x at cycle %d\n", 
1575                              this->trdid , this->core->lid , local_cxy , hal_time_stamp() );
1576        }
1577} // end rpc_thread_func()
1578
Note: See TracBrowser for help on using the repository browser.