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