source: trunk/kernel/syscalls/sys_display.c @ 664

Last change on this file since 664 was 664, checked in by alain, 4 years ago
  • Introduce the sys_socket.c file implementing all socket related syscalls.
  • Improve the non-standard sys_get_config() function.
File size: 15.9 KB
Line 
1/*
2 * sys_display.c - display the current state of a kernel structure on TXT0
3 *
4 * Author    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_uspace.h>
26#include <hal_vmm.h>
27#include <errno.h>
28#include <vmm.h>
29#include <cluster.h>
30#include <thread.h>
31#include <process.h>
32#include <string.h>
33#include <shared_syscalls.h>
34#include <remote_barrier.h>
35#include <vfs.h>
36#include <mapper.h>
37#include <ksocket.h>
38#include <syscalls.h>
39
40/////////////////////////////////////////////////////////////////////////////////
41// This static function returns a printable string for the type of display.
42/////////////////////////////////////////////////////////////////////////////////
43
44#if DEBUG_SYS_DISPLAY
45static char* display_type_str( uint32_t type )
46{
47    if     ( type == DISPLAY_STRING            ) return "STRING"; 
48    else if( type == DISPLAY_VMM               ) return "VMM"; 
49    else if( type == DISPLAY_SCHED             ) return "SCHED"; 
50    else if( type == DISPLAY_CLUSTER_PROCESSES ) return "CLUSTER_PROCESSES"; 
51    else if( type == DISPLAY_VFS               ) return "VFS"; 
52    else if( type == DISPLAY_CHDEV             ) return "CHDEV"; 
53    else if( type == DISPLAY_TXT_PROCESSES     ) return "TXT_PROCESSES"; 
54    else if( type == DISPLAY_DQDT              ) return "DQDT";
55    else if( type == DISPLAY_BUSYLOCKS         ) return "BUSYLOCKS"; 
56    else if( type == DISPLAY_MAPPER            ) return "MAPPER";
57    else if( type == DISPLAY_BARRIER           ) return "BARRIER";
58    else if( type == DISPLAY_FAT               ) return "FAT";
59    else if( type == DISPLAY_SOCKET            ) return "SOCKET";
60    else                                         return "undefined";
61}
62#endif
63
64/////////////////////////////
65int sys_display( reg_t  type,
66                 reg_t  arg0,
67                 reg_t  arg1,
68                 reg_t  arg2 )
69{
70
71    error_t     error;
72    vseg_t    * vseg;
73
74    thread_t  * this    = CURRENT_THREAD;
75    process_t * process = this->process;
76
77#if (DEBUG_SYS_DISPLAY || CONFIG_INSTRUMENTATION_SYSCALLS)
78uint64_t     tm_start = hal_get_cycles();
79#endif
80
81#if DEBUG_SYS_DISPLAY
82tm_start = hal_get_cycles();
83if( DEBUG_SYS_DISPLAY < tm_start )
84printk("\n[%s] thread[%x,%x] enter / type  %s / arg0 %x / arg1 %x / arg2 %x / cycle = %d\n",
85__FUNCTION__, process->pid, this->trdid, display_type_str(type),
86(uint32_t)arg0, (uint32_t)arg1, (uint32_t)arg2, (uint32_t)tm_start );
87#endif
88
89    switch( type )
90    {
91        ////////////////////
92        case DISPLAY_STRING:
93        {
94            char      kbuf[512];
95            uint32_t  length;
96
97            char    * string = (char *)arg0;
98
99            // check string in user space
100            error = vmm_get_vseg( process , (intptr_t)arg0 , &vseg );
101            if( error )
102            {
103
104#if DEBUG_SYSCALLS_ERROR
105printk("\n[ERROR] in %s for STRING : string buffer %x unmapped\n",
106__FUNCTION__ , (intptr_t)arg0 );
107#endif
108                this->errno = EINVAL;
109                return -1;
110            }
111
112            // ckeck string length
113            length = hal_strlen_from_uspace( string );
114            if( length >= 512 )
115            {
116
117#if DEBUG_SYSCALLS_ERROR
118printk("\n[ERROR] in %s STRING : string length %d too large\n",
119__FUNCTION__ , length );
120#endif
121                this->errno = EINVAL;
122                return -1;
123            }
124
125            // copy string to kernel space
126            hal_strcpy_from_uspace( XPTR( local_cxy , kbuf ),
127                                    string,
128                                    512 );
129
130            // print message on TXT0 kernel terminal
131            printk("\n%s / cycle %d\n", kbuf, (uint32_t)hal_get_cycles() );
132
133            break;
134        }
135        /////////////////
136        case DISPLAY_VMM:
137        {
138            cxy_t cxy      = (cxy_t)arg0;
139            pid_t pid      = (pid_t)arg1;
140            bool_t mapping = (arg2 != 0);
141
142            // check cxy argument
143                if( cluster_is_active( cxy ) == false ) 
144            {
145
146#if DEBUG_SYSCALLS_ERROR
147printk("\n[ERROR] in %s VMM : process %x in cluster %x not found\n",
148__FUNCTION__ , pid , cxy );
149#endif
150                this->errno = EINVAL;
151                return -1;
152            }
153
154            // get extended pointer on process PID in cluster CXY
155            xptr_t process_xp = cluster_get_process_from_pid_in_cxy( cxy , pid );
156                if( process_xp == XPTR_NULL )
157            {
158
159#if DEBUG_SYSCALLS_ERROR
160printk("\n[ERROR] in %s VMM : process %x in cluster %x not found\n",
161__FUNCTION__ , pid , cxy );
162#endif
163                this->errno = EINVAL;
164                return -1;
165            }
166
167            // call kernel function
168                hal_vmm_display( process_xp , mapping );
169
170            break;
171        }
172        ///////////////////
173        case DISPLAY_SCHED:
174        {
175            cxy_t cxy = (cxy_t)arg0;
176            lid_t lid = (lid_t)arg1;
177
178            // check cxy argument
179                if( cluster_is_active( cxy ) == false ) 
180            {
181
182#if DEBUG_SYSCALLS_ERROR
183printk("\n[ERROR] in %s SCHED : illegal cxy argument %x\n",
184__FUNCTION__ , cxy );
185#endif
186                this->errno = EINVAL;
187                return -1;
188            }
189
190            // check lid argument
191            if( lid >= LOCAL_CLUSTER->cores_nr )
192            {
193
194#if DEBUG_SYSCALLS_ERROR
195printk("\n[ERROR] in %s SCHED : illegal lid argument %x\n",
196__FUNCTION__ , lid );
197#endif
198                this->errno = EINVAL;
199                return -1;
200            }
201
202            // call kernel function
203            sched_remote_display( cxy , lid );
204
205            break;
206        }
207        ///////////////////////////////
208        case DISPLAY_CLUSTER_PROCESSES:
209        {
210            cxy_t  cxy   = (cxy_t)arg0;
211            bool_t owned = (bool_t)arg1;
212
213            // check cxy argument
214                if( cluster_is_active( cxy ) == false )
215            {
216
217#if DEBUG_SYSCALLS_ERROR
218printk("\n[ERROR] in %s CLUSTER_PROCESSES : illegal cxy argument %x\n",
219__FUNCTION__ , cxy );
220#endif
221                this->errno = EINVAL;
222                return -1;
223            }
224
225            cluster_processes_display( cxy , owned );
226
227            break;
228        }
229        /////////////////
230        case DISPLAY_VFS:
231        {
232            vfs_display( process->vfs_root_xp );
233
234            break;
235        }
236        ///////////////////
237        case DISPLAY_CHDEV:
238        {
239            chdev_dir_display();
240
241            break;
242        }
243        ///////////////////////////
244        case DISPLAY_TXT_PROCESSES:
245        {
246            uint32_t txt_id = (uint32_t)arg0;
247
248            // check argument
249                if( txt_id >= LOCAL_CLUSTER->nb_txt_channels )
250            {
251
252#if DEBUG_SYSCALLS_ERROR
253printk("\n[ERROR] in %s TXT_PROCESSES : illegal txt_id argument %d\n",
254__FUNCTION__ , txt_id );
255#endif
256                this->errno = EINVAL;
257                return -1;
258            }
259
260            process_txt_display( txt_id );
261
262            break;
263        }
264        //////////////////
265        case DISPLAY_DQDT:
266        {
267            dqdt_display();
268
269            break;
270        }
271        ///////////////////////
272        case DISPLAY_BUSYLOCKS:
273        {
274            pid_t   pid   = (pid_t)arg0;
275            trdid_t trdid = (trdid_t)arg1;
276
277            // get extended pointer on target thread
278            xptr_t thread_xp = thread_get_xptr( pid , trdid );
279
280            if( thread_xp == XPTR_NULL )
281            {
282
283#if DEBUG_SYSCALLS_ERROR
284printk("\n[ERROR] in %s BUSYLOCKS : thread[%x,%x] not found\n",
285__FUNCTION__ , pid, trdid );
286#endif
287                this->errno = EINVAL;
288                return -1;
289            }
290
291            thread_display_busylocks( thread_xp , __FUNCTION__ );
292
293            break;
294        }
295        ////////////////////
296        case DISPLAY_MAPPER:
297        {
298            xptr_t        root_inode_xp;
299            xptr_t        inode_xp;
300            cxy_t         inode_cxy;
301            vfs_inode_t * inode_ptr;
302            xptr_t        mapper_xp;
303            mapper_t    * mapper_ptr;
304            xptr_t        page_xp;
305
306            char          kbuf[CONFIG_VFS_MAX_PATH_LENGTH];
307
308            char     * path    = (char *)arg0;
309            uint32_t   page_id = (uint32_t)arg1;
310            uint32_t   nbytes  = (uint32_t)arg2;
311
312            // check pathname length
313            if( hal_strlen_from_uspace( path ) >= CONFIG_VFS_MAX_PATH_LENGTH )
314            {
315
316#if DEBUG_SYSCALLS_ERROR
317printk("\n[ERROR] in %s MAPPER : pathname too long\n",
318 __FUNCTION__ );
319#endif
320                this->errno = EINVAL;
321                return -1;
322            }
323
324            // check nbytes
325            if( nbytes >= 4096 )
326            {
327
328#if DEBUG_SYSCALLS_ERROR
329printk("\n[ERROR] in %s MAPPER : nbytes cannot be larger than 4096\n",
330 __FUNCTION__ );
331#endif
332                this->errno = EINVAL;
333                return -1;
334            }
335           
336            // copy pathname in kernel space
337            hal_strcpy_from_uspace( XPTR( local_cxy , kbuf ),
338                                    path,
339                                    CONFIG_VFS_MAX_PATH_LENGTH );
340
341            // compute root inode for pathname
342            if( kbuf[0] == '/' )                        // absolute path
343            {
344                // use extended pointer on VFS root inode
345                root_inode_xp = process->vfs_root_xp;
346            }
347            else                                        // relative path
348            {
349                // get cluster and local pointer on reference process
350                xptr_t      ref_xp  = process->ref_xp;
351                process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
352                cxy_t       ref_cxy = GET_CXY( ref_xp );
353
354                // get extended pointer on CWD inode
355                root_inode_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->cwd_xp ) );
356            }
357
358            // get extended pointer on target inode
359            error = vfs_lookup( root_inode_xp,
360                                kbuf,
361                                0,
362                                &inode_xp,
363                                NULL );
364            if( error )
365                {
366
367#if DEBUG_SYSCALLS_ERROR
368printk("\n[ERROR] in %s for MAPPER : cannot found inode <%s>\n",
369__FUNCTION__ , kbuf );
370#endif
371                        this->errno = ENFILE;
372                        return -1;
373                }
374   
375            // get target inode cluster and local pointer
376            inode_cxy = GET_CXY( inode_xp );
377            inode_ptr = GET_PTR( inode_xp );
378
379            // get extended pointer on target mapper
380            mapper_ptr = hal_remote_lpt( XPTR( inode_cxy , &inode_ptr->mapper ) );
381            mapper_xp  = XPTR( inode_cxy , mapper_ptr );
382
383            // get extended pointer on target page
384            page_xp = mapper_get_page( mapper_xp , page_id );
385
386            if( page_xp == XPTR_NULL )
387                {
388
389#if DEBUG_SYSCALLS_ERROR
390printk("\n[ERROR] in %s MAPPER : cannot get page %d\n",
391__FUNCTION__ , page_id );
392#endif
393                        this->errno = ENFILE;
394                        return -1;
395                }
396
397            // display mapper
398            mapper_display_page( mapper_xp , page_xp , nbytes );
399
400
401            break;
402        }
403        /////////////////////
404        case DISPLAY_BARRIER:
405        {
406            // get target process PID
407            pid_t pid = (pid_t)arg0;
408
409            // get pointers on owner process
410            xptr_t      process_xp  = cluster_get_reference_process_from_pid( pid );
411            process_t * process_ptr = GET_PTR( process_xp );
412            cxy_t       process_cxy = GET_CXY( process_xp );
413
414            if( process_xp == XPTR_NULL )
415            {
416
417#if DEBUG_SYSCALLS_ERROR
418printk("\n[ERROR] in %s BARRIER : process %x not found\n",
419__FUNCTION__ , pid );
420#endif
421                this->errno = EINVAL;
422                return -1;
423            }
424
425            // get extended pointer on root of list of barriers
426            xptr_t root_xp = XPTR( process_cxy , &process_ptr->barrier_root );
427
428            if( xlist_is_empty( root_xp ) )
429            {
430
431#if DEBUG_SYSCALLS_ERROR
432printk("\n[ERROR] in %s BARRIER : no registered barrier in process %x\n",
433__FUNCTION__ , pid );
434#endif
435                this->errno = EINVAL;
436                return -1;
437            }
438
439            // get extended pointer on first registered generic barrier descriptor
440            xptr_t gen_barrier_xp  = XLIST_FIRST( root_xp , generic_barrier_t , list );
441
442            // display barrier state
443            generic_barrier_display( gen_barrier_xp );
444
445            break;
446        }
447        /////////////////
448        case DISPLAY_FAT:
449        {
450            uint32_t  slots = (uint32_t)arg1;
451
452            if( slots > 1024 )
453                {
454
455#if DEBUG_SYSCALLS_ERROR
456printk("\n[ERROR] in %s FAT : nb_slots larger than 1024\n",
457__FUNCTION__ );
458#endif
459                        this->errno = EINVAL;
460                        return -1;
461                }
462
463            if( slots == 0 )  // display fat context in cluster cxy
464            {
465                uint32_t  cxy = (uint32_t)arg0;
466
467                if( cluster_is_active( cxy ) == false ) 
468                {
469
470#if DEBUG_SYSCALLS_ERROR
471printk("\n[ERROR] in %s FAT : illegal cxy argument %x\n",
472__FUNCTION__ , cxy );
473#endif
474                     this->errno = EINVAL;
475                     return -1;
476                }
477
478                fatfs_display_ctx( cxy );
479            }
480            else                  // display nb_slots in page
481            {                       
482                uint32_t min  = (uint32_t)arg0;
483
484                fatfs_display_fat( min , slots );
485            }
486
487            break;
488        }
489        ////////////////////
490        case DISPLAY_SOCKET:
491        {
492            pid_t   pid   = (pid_t)arg0;
493            trdid_t fdid  = (trdid_t)arg1;
494
495            // get extended pointer on owner process descriptor
496            xptr_t owner_xp = cluster_get_owner_process_from_pid( pid );
497
498            if( owner_xp == XPTR_NULL )
499            {
500
501#if DEBUG_SYSCALLS_ERROR
502printk("\n[ERROR] in %s SOCKET : pid %x not found\n", __FUNCTION__ , pid );
503#endif
504                this->errno = EINVAL;
505                return -1;
506            }
507
508            // get extended pointer on file descriptor
509            xptr_t file_xp = process_fd_get_xptr_from_owner( owner_xp , fdid );
510
511            if( file_xp == XPTR_NULL )
512            {
513
514#if DEBUG_SYSCALLS_ERROR
515printk("\n[ERROR] in %s SOCKET : fdid %d not found\n", __FUNCTION__ , fdid );
516#endif
517                this->errno = EINVAL;
518                return -1;
519            }
520
521            // get local pointer and cluster for file
522            vfs_file_t * file_ptr = GET_PTR( file_xp );
523            cxy_t        file_cxy = GET_CXY( file_xp );
524
525            // get local pointer on socket descriptor
526            socket_t * socket = hal_remote_lpt( XPTR( file_cxy , &file_ptr->socket ) );
527
528            // display socket descriptor on TXT0
529            socket_display( XPTR( file_cxy , socket ), NULL );
530
531            break;
532        }
533        ////////
534        default: 
535        {
536
537#if DEBUG_SYSCALLS_ERROR
538printk("\n[ERROR] in %s : undefined display type %d\n",
539        __FUNCTION__ , type );
540#endif
541            this->errno = EINVAL;
542            return -1;
543        }
544    }  // end switch on type
545
546#if (DEBUG_SYS_DISPLAY || CONFIG_INSTRUMENTATION_SYSCALLS)
547uint64_t     tm_end = hal_get_cycles();
548#endif
549
550#if DEBUG_SYS_DISPLAY
551if( DEBUG_SYS_DISPLAY < tm_end )
552printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
553__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end );
554#endif
555
556#if CONFIG_INSTRUMENTATION_SYSCALLS
557hal_atomic_add( &syscalls_cumul_cost[SYS_DISPLAY] , tm_end - tm_start );
558hal_atomic_add( &syscalls_occurences[SYS_DISPLAY] , 1 );
559#endif
560
561    return 0;
562
563}  // end sys_display()
Note: See TracBrowser for help on using the repository browser.