source: trunk/kernel/kern/kernel_init.c @ 662

Last change on this file since 662 was 662, checked in by alain, 4 years ago

Introduce the ksocket.h & ksocket.c files in kernel/kern.

File size: 65.5 KB
Line 
1/*
2 * kernel_init.c - kernel parallel initialization
3 *
4 * Authors :  Mohamed Lamine Karaoui (2015)
5 *            Alain Greiner  (2016,2017,2018,2019,2020)
6 *
7 * Copyright (c) Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <kernel_config.h>
26#include <errno.h>
27#include <hal_kernel_types.h>
28#include <hal_special.h>
29#include <hal_context.h>
30#include <hal_irqmask.h>
31#include <hal_macros.h>
32#include <hal_ppm.h>
33#include <barrier.h>
34#include <xbarrier.h>
35#include <remote_fifo.h>
36#include <core.h>
37#include <list.h>
38#include <xlist.h>
39#include <xhtab.h>
40#include <thread.h>
41#include <scheduler.h>
42#include <ksocket.h>
43#include <kmem.h>
44#include <cluster.h>
45#include <string.h>
46#include <memcpy.h>
47#include <ppm.h>
48#include <page.h>
49#include <chdev.h>
50#include <boot_info.h>
51#include <dqdt.h>
52#include <dev_mmc.h>
53#include <dev_dma.h>
54#include <dev_iob.h>
55#include <dev_ioc.h>
56#include <dev_txt.h>
57#include <dev_pic.h>
58#include <printk.h>
59#include <vfs.h>
60#include <devfs.h>
61#include <mapper.h>
62
63///////////////////////////////////////////////////////////////////////////////////////////
64// All the following global variables are replicated in all clusters.
65// They are initialised by the kernel_init() function.
66//
67// WARNING : The section names have been defined to control the base addresses of the
68// boot_info structure and the idle thread descriptors, through the kernel.ld script:
69// - the boot_info structure is built by the bootloader, and used by kernel_init.
70//   it must be the first object in the kdata segment.
71// - the array of idle threads descriptors must be placed on the first page boundary after
72//   the boot_info structure in the kdata segment.
73///////////////////////////////////////////////////////////////////////////////////////////
74
75// This variable defines the local boot_info structure
76__attribute__((section(".kinfo")))
77boot_info_t          boot_info;
78
79// This variable defines the "idle" threads descriptors array
80__attribute__((section(".kidle")))
81char                 idle_threads[CONFIG_THREAD_DESC_SIZE *
82                                   CONFIG_MAX_LOCAL_CORES]   CONFIG_PPM_PAGE_ALIGNED;
83
84// This variable defines the local cluster manager
85__attribute__((section(".kdata")))
86cluster_t            cluster_manager                         CONFIG_CACHE_LINE_ALIGNED;
87
88// This variable defines the TXT_TX[0] chdev
89__attribute__((section(".kdata")))
90chdev_t              txt0_tx_chdev                           CONFIG_CACHE_LINE_ALIGNED;
91
92// This variable defines the TXT_RX[0] chdev
93__attribute__((section(".kdata")))
94chdev_t              txt0_rx_chdev                           CONFIG_CACHE_LINE_ALIGNED;
95
96// This variables define the kernel process0 descriptor
97__attribute__((section(".kdata")))
98process_t            process_zero                            CONFIG_CACHE_LINE_ALIGNED;
99
100// This variable defines a set of extended pointers on the distributed chdevs
101__attribute__((section(".kdata")))
102chdev_directory_t    chdev_dir                               CONFIG_CACHE_LINE_ALIGNED;
103
104// This variable contains the input IRQ indexes for the IOPIC controller
105__attribute__((section(".kdata")))
106iopic_input_t        iopic_input                             CONFIG_CACHE_LINE_ALIGNED;
107
108// This variable contains the input IRQ indexes for the LAPIC controller
109__attribute__((section(".kdata")))
110lapic_input_t        lapic_input                             CONFIG_CACHE_LINE_ALIGNED;
111
112// This variable defines the local cluster identifier
113__attribute__((section(".kdata")))
114cxy_t                local_cxy                               CONFIG_CACHE_LINE_ALIGNED;
115
116// This variable is used for core[0] cores synchronisation in kernel_init()
117__attribute__((section(".kdata")))
118xbarrier_t           global_barrier                          CONFIG_CACHE_LINE_ALIGNED;
119
120// This variable is used for local cores synchronisation in kernel_init()
121__attribute__((section(".kdata")))
122barrier_t            local_barrier                           CONFIG_CACHE_LINE_ALIGNED;
123
124// This variable defines the array of supported File System contexts
125__attribute__((section(".kdata")))
126vfs_ctx_t            fs_context[FS_TYPES_NR]                 CONFIG_CACHE_LINE_ALIGNED;
127
128// This array is used for debug, and describes the kernel locks usage,
129// It must be kept consistent with the defines in kernel_config.h file.
130__attribute__((section(".kdata")))
131char * lock_type_str[] =
132{
133    "unused_0",              //  0   must be unused to help debug
134
135    "CLUSTER_KCM",           //  1
136    "SCHED_STATE",           //  2
137    "VMM_STACK",             //  3
138    "VMM_MMAP",              //  4
139    "KCM_STATE",             //  5
140    "KHM_STATE",             //  6
141    "HTAB_STATE",            //  7
142
143    "VFS_CTX",               //  8
144    "PPM_FREE",              //  9
145    "THREAD_JOIN",           // 10
146    "XHTAB_STATE",           // 11
147    "CHDEV_QUEUE",           // 12
148    "CHDEV_TXT0",            // 13
149    "CHDEV_TXTLIST",         // 14
150    "PAGE_STATE",            // 15
151    "MUTEX_STATE",           // 16
152    "CONDVAR_STATE",         // 17
153    "SEM_STATE",             // 18
154    "PROCESS_CWD",           // 19
155    "BARRIER_STATE",         // 20
156    "LISTEN_SOCKET",         // 21
157
158    "CLUSTER_PREFTBL",       // 22
159
160    "SOCKET_STATE",          // 23
161    "PPM_DIRTY",             // 24
162    "CLUSTER_LOCALS",        // 25
163    "CLUSTER_COPIES",        // 26
164    "PROCESS_CHILDREN",      // 27
165    "PROCESS_USERSYNC",      // 28
166    "PROCESS_FDARRAY",       // 29
167    "PROCESS_DIR",           // 30
168    "VMM_VSL",               // 31
169
170    "PROCESS_THTBL",         // 32
171
172    "MAPPER_STATE",          // 33
173    "VFS_SIZE",              // 34
174    "VFS_FILE",              // 35
175    "VFS_MAIN",              // 36
176    "FATFS_FAT",             // 37
177    "FBF_WINDOWS",           // 38
178};       
179
180// debug variables to analyse the sys_read()  syscalls timing
181
182#if DEBUG_SYS_READ
183uint32_t   enter_sys_read;
184uint32_t   exit_sys_read;
185
186uint32_t   enter_devfs_read;
187uint32_t   exit_devfs_read;
188
189uint32_t   enter_txt_read;
190uint32_t   exit_txt_read;
191
192uint32_t   enter_chdev_cmd_read;
193uint32_t   exit_chdev_cmd_read;
194
195uint32_t   enter_chdev_server_read;
196uint32_t   exit_chdev_server_read;
197
198uint32_t   enter_tty_cmd_read;
199uint32_t   exit_tty_cmd_read;
200
201uint32_t   enter_tty_isr_read;
202uint32_t   exit_tty_isr_read;
203#endif
204
205// debug variables  to analyse the sys_write() syscall timing
206
207#if DEBUG_SYS_WRITE   
208uint32_t   enter_sys_write;
209uint32_t   exit_sys_write;
210
211uint32_t   enter_devfs_write;
212uint32_t   exit_devfs_write;
213
214uint32_t   enter_txt_write;
215uint32_t   exit_txt_write;
216
217uint32_t   enter_chdev_cmd_write;
218uint32_t   exit_chdev_cmd_write;
219
220uint32_t   enter_chdev_server_write;
221uint32_t   exit_chdev_server_write;
222
223uint32_t   enter_tty_cmd_write;
224uint32_t   exit_tty_cmd_write;
225
226uint32_t   enter_tty_isr_write;
227uint32_t   exit_tty_isr_write;
228#endif
229
230// intrumentation variables : cumulated costs per syscall type in cluster
231
232#if CONFIG_INSTRUMENTATION_SYSCALLS
233__attribute__((section(".kdata")))
234uint32_t   syscalls_cumul_cost[SYSCALLS_NR];
235
236__attribute__((section(".kdata")))
237uint32_t   syscalls_occurences[SYSCALLS_NR];
238#endif
239
240///////////////////////////////////////////////////////////////////////////////////////////
241// This function displays the ALMOS_MKH banner.
242///////////////////////////////////////////////////////////////////////////////////////////
243static void print_banner( uint32_t nclusters , uint32_t ncores )
244{
245    printk("\n"
246           "                    _        __    __     _____     ______         __    __    _   __   _     _   \n"
247           "          /\\       | |      |  \\  /  |   / ___ \\   / _____|       |  \\  /  |  | | / /  | |   | |  \n"
248           "         /  \\      | |      |   \\/   |  | /   \\ | | /             |   \\/   |  | |/ /   | |   | |  \n"
249           "        / /\\ \\     | |      | |\\  /| |  | |   | | | |_____   ___  | |\\  /| |  |   /    | |___| |  \n"
250           "       / /__\\ \\    | |      | | \\/ | |  | |   | | \\_____  \\ |___| | | \\/ | |  |   \\    |  ___  |  \n"
251           "      / ______ \\   | |      | |    | |  | |   | |       | |       | |    | |  | |\\ \\   | |   | |  \n"
252           "     / /      \\ \\  | |____  | |    | |  | \\___/ |  _____/ |       | |    | |  | | \\ \\  | |   | |  \n"
253           "    /_/        \\_\\ |______| |_|    |_|   \\_____/  |______/        |_|    |_|  |_|  \\_\\ |_|   |_|  \n"
254           "\n\n\t\t Advanced Locality Management Operating System / Multi Kernel Hybrid\n"
255           "\n\n\t\t %s / %d cluster(s) / %d core(s) per cluster\n\n",
256           CONFIG_VERSION , nclusters , ncores );
257}
258
259
260///////////////////////////////////////////////////////////////////////////////////////////
261// This function initializes the TXT_TX[0] and TXT_RX[0] chdev descriptors, implementing
262// the "kernel terminal", shared by all kernel instances for debug messages.
263// These chdev are implemented as global variables (replicated in all clusters),
264// because this terminal is used before the kmem allocator initialisation, but only
265// the chdevs in cluster 0 are registered in the "chdev_dir" directory.
266// As this TXT0 chdev supports only the TXT_SYNC_WRITE command, we don't create
267// a server thread, we don't allocate a WTI, and we don't initialize the waiting queue.
268// Note: The TXT_RX[0] chdev is created, but is not used by ALMOS-MKH (september 2018).
269///////////////////////////////////////////////////////////////////////////////////////////
270// @ info    : pointer on the local boot-info structure.
271///////////////////////////////////////////////////////////////////////////////////////////
272static void __attribute__ ((noinline)) txt0_device_init( boot_info_t * info )
273{
274    boot_device_t * dev_tbl;         // pointer on array of devices in boot_info
275    uint32_t        dev_nr;          // actual number of devices in this cluster
276    xptr_t          base;            // remote pointer on segment base
277    uint32_t        func;            // device functional index
278    uint32_t        impl;            // device implementation index
279    uint32_t        i;               // device index in dev_tbl
280    uint32_t        x;               // X cluster coordinate
281    uint32_t        y;               // Y cluster coordinate
282
283    // get number of peripherals and base of devices array from boot_info
284    dev_nr      = info->ext_dev_nr;
285    dev_tbl     = info->ext_dev;
286
287    // loop on external peripherals to find TXT device
288    for( i = 0 ; i < dev_nr ; i++ )
289    {
290        base        = dev_tbl[i].base;
291        func        = FUNC_FROM_TYPE( dev_tbl[i].type );
292        impl        = IMPL_FROM_TYPE( dev_tbl[i].type );
293
294        if (func == DEV_FUNC_TXT )
295        {
296            // initialize TXT_TX[0] chdev
297            txt0_tx_chdev.func    = func;
298            txt0_tx_chdev.impl    = impl;
299            txt0_tx_chdev.channel = 0;
300            txt0_tx_chdev.base    = base;
301            txt0_tx_chdev.is_rx   = false;
302            remote_busylock_init( XPTR( local_cxy , &txt0_tx_chdev.wait_lock ),
303                                  LOCK_CHDEV_TXT0 );
304           
305            // initialize TXT_RX[0] chdev
306            txt0_rx_chdev.func    = func;
307            txt0_rx_chdev.impl    = impl;
308            txt0_rx_chdev.channel = 0;
309            txt0_rx_chdev.base    = base;
310            txt0_rx_chdev.is_rx   = true;
311            remote_busylock_init( XPTR( local_cxy , &txt0_rx_chdev.wait_lock ),
312                                  LOCK_CHDEV_TXT0 );
313           
314            // make TXT specific initialisations
315            dev_txt_init( &txt0_tx_chdev );                 
316            dev_txt_init( &txt0_rx_chdev );
317
318            // register TXT_TX[0] & TXT_RX[0] in chdev_dir[x][y]
319            // for all valid clusters             
320            for( x = 0 ; x < info->x_size ; x++ )
321            {
322                for( y = 0 ; y < info->y_size ; y++ )
323                {
324                    cxy_t cxy = HAL_CXY_FROM_XY( x , y );
325
326                    if( cluster_is_active( cxy ) )
327                    {
328                        hal_remote_s64( XPTR( cxy , &chdev_dir.txt_tx[0] ) ,
329                                        XPTR( local_cxy , &txt0_tx_chdev ) );
330                        hal_remote_s64( XPTR( cxy , &chdev_dir.txt_rx[0] ) ,
331                                        XPTR( local_cxy , &txt0_rx_chdev ) );
332                    }
333                }
334            }
335
336            hal_fence();
337        }
338        } // end loop on devices
339}  // end txt0_device_init()
340
341///////////////////////////////////////////////////////////////////////////////////////////
342// This function allocates memory and initializes the chdev descriptors for the internal
343// peripherals contained in the local cluster, other than the LAPIC, as specified by
344// the boot_info, including the linking with the driver for the specified implementation.
345// The relevant entries in all copies of the devices directory are initialised.
346///////////////////////////////////////////////////////////////////////////////////////////
347// @ info    : pointer on the local boot-info structure.
348///////////////////////////////////////////////////////////////////////////////////////////
349static void __attribute__ ((noinline)) internal_devices_init( boot_info_t * info )
350{
351    boot_device_t * dev_tbl;         // pointer on array of internaldevices in boot_info
352        uint32_t        dev_nr;          // actual number of devices in this cluster
353        xptr_t          base;            // remote pointer on segment base
354    uint32_t        func;            // device functionnal index
355    uint32_t        impl;            // device implementation index
356        uint32_t        i;               // device index in dev_tbl
357        uint32_t        x;               // X cluster coordinate
358        uint32_t        y;               // Y cluster coordinate
359        uint32_t        channels;        // number of channels
360        uint32_t        channel;         // channel index
361        chdev_t       * chdev_ptr;       // local pointer on created chdev
362
363    // get number of internal peripherals and base from boot_info
364        dev_nr  = info->int_dev_nr;
365    dev_tbl = info->int_dev;
366
367    // loop on internal peripherals
368        for( i = 0 ; i < dev_nr ; i++ )
369        {
370        base        = dev_tbl[i].base;
371        channels    = dev_tbl[i].channels;
372        func        = FUNC_FROM_TYPE( dev_tbl[i].type );
373        impl        = IMPL_FROM_TYPE( dev_tbl[i].type );
374 
375        //////////////////////////
376        if( func == DEV_FUNC_MMC ) 
377        {
378
379            // check channels
380            if( channels != 1 )
381            {
382                printk("\n[PANIC] in %s : MMC device must be single channel\n",
383                __FUNCTION__ );
384                hal_core_sleep();
385            }
386
387            // create chdev in local cluster
388            chdev_ptr = chdev_create( func,
389                                      impl,
390                                      0,          // channel
391                                      false,      // direction
392                                      base );
393
394            // check memory
395            if( chdev_ptr == NULL )
396            {
397                printk("\n[PANIC] in %s : cannot create MMC chdev\n",
398                __FUNCTION__ );
399                hal_core_sleep();
400            }
401           
402            // make MMC specific initialisation
403            dev_mmc_init( chdev_ptr );
404
405            // set the MMC field in all chdev_dir[x][y] structures
406            for( x = 0 ; x < info->x_size ; x++ )
407            {
408                for( y = 0 ; y < info->y_size ; y++ )
409                {
410                    cxy_t cxy = HAL_CXY_FROM_XY( x , y );
411
412                    if( cluster_is_active( cxy ) )
413                    {
414                        hal_remote_s64( XPTR( cxy , &chdev_dir.mmc[local_cxy] ), 
415                                        XPTR( local_cxy , chdev_ptr ) );
416                    }
417                }
418            }
419
420#if( DEBUG_KERNEL_INIT & 0x1 )
421if( hal_time_stamp() > DEBUG_KERNEL_INIT )
422printk("\n[%s] : created MMC in cluster %x / chdev = %x\n",
423__FUNCTION__ , local_cxy , chdev_ptr );
424#endif
425        }
426        ///////////////////////////////
427        else if( func == DEV_FUNC_DMA )
428        {
429            // create one chdev per channel in local cluster
430            for( channel = 0 ; channel < channels ; channel++ )
431            {   
432                // create chdev[channel] in local cluster
433                chdev_ptr = chdev_create( func,
434                                          impl,
435                                          channel,
436                                          false,     // direction
437                                          base );
438
439                // check memory
440                if( chdev_ptr == NULL )
441                {
442                    printk("\n[PANIC] in %s : cannot create DMA chdev\n",
443                    __FUNCTION__ );
444                    hal_core_sleep();
445                }
446           
447                // make DMA specific initialisation
448                dev_dma_init( chdev_ptr );     
449
450                // initialize only the DMA[channel] field in the local chdev_dir[x][y]
451                // structure because the DMA device is not remotely accessible.
452                chdev_dir.dma[channel] = XPTR( local_cxy , chdev_ptr );
453
454#if( DEBUG_KERNEL_INIT & 0x1 )
455if( hal_time_stamp() > DEBUG_KERNEL_INIT )
456printk("\n[%s] : created DMA[%d] in cluster %x / chdev = %x\n",
457__FUNCTION__ , channel , local_cxy , chdev_ptr );
458#endif
459            }
460        }
461    }
462}  // end internal_devices_init()
463
464///////////////////////////////////////////////////////////////////////////////////////////
465// This function allocates memory and initializes the chdev descriptors for the 
466// external (shared) peripherals other than the IOPIC, as specified by the boot_info.
467// This includes the dynamic linking with the driver for the specified implementation.
468// These chdev descriptors are distributed on all clusters, using a modulo on a global
469// index, identically computed in all clusters.
470// This function is executed in all clusters by the core[0] core, that computes a global index
471// for all external chdevs. Each core[0] core creates only the chdevs that must be placed in
472// the local cluster, because the global index matches the local index.
473// The relevant entries in all copies of the devices directory are initialised.
474///////////////////////////////////////////////////////////////////////////////////////////
475// @ info    : pointer on the local boot-info structure.
476///////////////////////////////////////////////////////////////////////////////////////////
477static void external_devices_init( boot_info_t * info )
478{
479    boot_device_t * dev_tbl;         // pointer on array of external devices in boot_info
480        uint32_t        dev_nr;          // actual number of external devices
481        xptr_t          base;            // remote pointer on segment base
482    uint32_t        func;            // device functionnal index
483    uint32_t        impl;            // device implementation index
484        uint32_t        i;               // device index in dev_tbl
485        uint32_t        x;               // X cluster coordinate
486        uint32_t        y;               // Y cluster coordinate
487        uint32_t        channels;        // number of channels
488        uint32_t        channel;         // channel index
489        uint32_t        directions;      // number of directions (1 or 2)
490        uint32_t        rx;              // direction index (0 or 1)
491    chdev_t       * chdev;           // local pointer on one channel_device descriptor
492    uint32_t        ext_chdev_gid;   // global index of external chdev
493
494    // get number of peripherals and base of devices array from boot_info
495    dev_nr      = info->ext_dev_nr;
496    dev_tbl     = info->ext_dev;
497
498    // initializes global index (PIC is already placed in cluster 0
499    ext_chdev_gid = 1;
500
501    // loop on external peripherals
502    for( i = 0 ; i < dev_nr ; i++ )
503    {
504        base     = dev_tbl[i].base;
505        channels = dev_tbl[i].channels;
506        func     = FUNC_FROM_TYPE( dev_tbl[i].type );
507        impl     = IMPL_FROM_TYPE( dev_tbl[i].type );
508
509        // There is one chdev per direction for NIC and for TXT
510        if((func == DEV_FUNC_NIC) || (func == DEV_FUNC_TXT)) directions = 2;
511        else                                                 directions = 1;
512
513        // do nothing for ROM, that does not require a device descriptor.
514        if( func == DEV_FUNC_ROM ) continue;
515
516        // do nothing for PIC, that is already initialized
517        if( func == DEV_FUNC_PIC ) continue;
518
519        // check PIC device initialized
520        if( chdev_dir.pic == XPTR_NULL )
521        {
522            printk("\n[PANIC] in %s : PIC device must be initialized first\n",
523            __FUNCTION__ );
524            hal_core_sleep();
525        }
526
527        // check external device functionnal type
528        if( (func != DEV_FUNC_IOB) && (func != DEV_FUNC_IOC) && (func != DEV_FUNC_TXT) &&
529            (func != DEV_FUNC_NIC) && (func != DEV_FUNC_FBF) )
530        {
531            printk("\n[PANIC] in %s : undefined peripheral type\n",
532            __FUNCTION__ );
533            hal_core_sleep();
534        }
535
536        // loops on channels
537        for( channel = 0 ; channel < channels ; channel++ )
538        {
539            // loop on directions
540            for( rx = 0 ; rx < directions ; rx++ )
541            {
542                // skip TXT0 that has already been initialized
543                if( (func == DEV_FUNC_TXT) && (channel == 0) ) continue;
544
545                // all kernel instances compute the target cluster for all chdevs,
546                // computing the global index ext_chdev_gid[func,channel,direction]
547                cxy_t target_cxy;
548                while( 1 )
549                {
550                    uint32_t offset     = ext_chdev_gid % ( info->x_size * info->y_size );
551                    uint32_t x          = offset / info->y_size;
552                    uint32_t y          = offset % info->y_size;
553
554                    target_cxy = HAL_CXY_FROM_XY( x , y );
555
556                    // exit loop if target cluster is active
557                    if( cluster_is_active( target_cxy ) ) break;
558               
559                    // increment global index otherwise
560                    ext_chdev_gid++;
561                }
562
563                // allocate and initialize a local chdev
564                // when local cluster matches target cluster
565                if( target_cxy == local_cxy )
566                {
567
568#if( DEBUG_KERNEL_INIT & 0x3 )
569if( hal_time_stamp() > DEBUG_KERNEL_INIT )
570printk("\n[%s] : found chdev %s / channel = %d / rx = %d / cluster %x\n",
571__FUNCTION__ , chdev_func_str( func ), channel , rx , local_cxy );
572#endif
573                    chdev = chdev_create( func,
574                                          impl,
575                                          channel,
576                                          rx,          // direction
577                                          base );
578
579                    if( chdev == NULL )
580                    {
581                        printk("\n[PANIC] in %s : cannot allocate chdev\n",
582                        __FUNCTION__ );
583                        hal_core_sleep();
584                    }
585
586                    // make device type specific initialisation
587                    if     ( func == DEV_FUNC_IOB ) dev_iob_init( chdev );
588                    else if( func == DEV_FUNC_IOC ) dev_ioc_init( chdev );
589                    else if( func == DEV_FUNC_TXT ) dev_txt_init( chdev );
590                    else if( func == DEV_FUNC_NIC ) dev_nic_init( chdev );
591                    else if( func == DEV_FUNC_FBF ) dev_fbf_init( chdev );
592
593                    // all external (shared) devices are remotely accessible
594                    // initialize the replicated chdev_dir[x][y] structures
595                    // defining the extended pointers on chdev descriptors
596                    xptr_t * entry = NULL;
597
598                    if(func==DEV_FUNC_IOB             ) entry  = &chdev_dir.iob;
599                    if(func==DEV_FUNC_IOC             ) entry  = &chdev_dir.ioc[channel];
600                    if(func==DEV_FUNC_FBF             ) entry  = &chdev_dir.fbf[channel];
601                    if((func==DEV_FUNC_TXT) && (rx==0)) entry  = &chdev_dir.txt_tx[channel];
602                    if((func==DEV_FUNC_TXT) && (rx==1)) entry  = &chdev_dir.txt_rx[channel];
603                    if((func==DEV_FUNC_NIC) && (rx==0)) entry  = &chdev_dir.nic_tx[channel];
604                    if((func==DEV_FUNC_NIC) && (rx==1)) entry  = &chdev_dir.nic_rx[channel];
605
606                    for( x = 0 ; x < info->x_size ; x++ )
607                    {
608                        for( y = 0 ; y < info->y_size ; y++ )
609                        {
610                            cxy_t cxy = HAL_CXY_FROM_XY( x , y );
611
612                            if( cluster_is_active( cxy ) && ( entry != NULL ) )
613                            {
614                                hal_remote_s64( XPTR( cxy , entry ),
615                                                XPTR( local_cxy , chdev ) );
616                            }
617                        }
618                    }
619
620#if( DEBUG_KERNEL_INIT & 0x3 )
621if( hal_time_stamp() > DEBUG_KERNEL_INIT )
622printk("\n[%s] : created chdev %s / channel = %d / rx = %d / cluster %x / chdev = %x\n",
623__FUNCTION__ , chdev_func_str( func ), channel , rx , local_cxy , chdev );
624#endif
625                }  // end if match
626
627                // increment chdev global index (matching or not)
628                ext_chdev_gid++;
629
630            } // end loop on directions
631        }  // end loop on channels
632        } // end loop on devices
633}  // end external_devices_init()
634
635///////////////////////////////////////////////////////////////////////////////////////////
636// This function is called by core[0] in cluster 0 to allocate memory and initialize the PIC
637// device, namely the informations attached to the external IOPIC controller, that
638// must be replicated in all clusters (struct iopic_input).
639// This initialisation must be done before other devices initialisation because the IRQ
640// routing infrastructure is required for both internal and external devices init.
641///////////////////////////////////////////////////////////////////////////////////////////
642// @ info    : pointer on the local boot-info structure.
643///////////////////////////////////////////////////////////////////////////////////////////
644static void __attribute__ ((noinline)) iopic_init( boot_info_t * info )
645{
646    boot_device_t * dev_tbl;         // pointer on boot_info external devices array
647        uint32_t        dev_nr;          // actual number of external devices
648        xptr_t          base;            // remote pointer on segment base
649    uint32_t        func;            // device functionnal index
650    uint32_t        impl;            // device implementation index
651        uint32_t        i;               // device index in dev_tbl
652    uint32_t        x;               // cluster X coordinate
653    uint32_t        y;               // cluster Y coordinate
654    bool_t          found;           // IOPIC found
655        chdev_t       * chdev;           // pointer on PIC chdev descriptor
656
657    // get number of external peripherals and base of array from boot_info
658        dev_nr      = info->ext_dev_nr;
659    dev_tbl     = info->ext_dev;
660
661    // avoid GCC warning
662    base        = XPTR_NULL;
663    impl        = 0;
664
665    // loop on external peripherals to get the IOPIC 
666        for( i = 0 , found = false ; i < dev_nr ; i++ )
667        {
668        func = FUNC_FROM_TYPE( dev_tbl[i].type );
669
670        if( func == DEV_FUNC_PIC )
671        {
672            base     = dev_tbl[i].base;
673            impl     = IMPL_FROM_TYPE( dev_tbl[i].type );
674            found    = true;
675            break;
676        }
677    }
678
679    // check PIC existence
680    if( found == false )
681    {
682        printk("\n[PANIC] in %s : PIC device not found\n",
683        __FUNCTION__ );
684        hal_core_sleep();
685    }
686
687    // allocate and initialize the PIC chdev in cluster 0
688    chdev = chdev_create( DEV_FUNC_PIC,
689                          impl,
690                          0,      // channel
691                          0,      // direction,
692                          base );
693
694    // check memory
695    if( chdev == NULL )
696    {
697        printk("\n[PANIC] in %s : no memory for PIC chdev\n",
698        __FUNCTION__ );
699        hal_core_sleep();
700    }
701
702    // make PIC device type specific initialisation
703    dev_pic_init( chdev );
704
705    // register, in all clusters, the extended pointer
706    // on PIC chdev in "chdev_dir" array
707    xptr_t * entry = &chdev_dir.pic;   
708               
709    for( x = 0 ; x < info->x_size ; x++ )
710    {
711        for( y = 0 ; y < info->y_size ; y++ )
712        {
713            cxy_t cxy = HAL_CXY_FROM_XY( x , y );
714
715            if( cluster_is_active( cxy ) )
716            {
717                hal_remote_s64( XPTR( cxy , entry ) , 
718                                XPTR( local_cxy , chdev ) );
719            }
720        }
721    }
722
723    // initialize, in all clusters, the "iopic_input" structure
724    // defining how external IRQs are connected to IOPIC
725
726    // register default value for unused inputs
727    for( x = 0 ; x < info->x_size ; x++ )
728    {
729        for( y = 0 ; y < info->y_size ; y++ )
730        {
731            cxy_t cxy = HAL_CXY_FROM_XY( x , y );
732
733            if( cluster_is_active( cxy ) )
734            {
735                hal_remote_memset( XPTR( cxy , &iopic_input ), 
736                                   0xFF , sizeof(iopic_input_t) );
737            }
738        }
739    }
740
741    // register input IRQ index for valid inputs
742    uint32_t   id;             // input IRQ index
743    uint8_t    valid;          // input IRQ is connected
744    uint32_t   type;           // source device type
745    uint8_t    channel;        // source device channel
746    uint8_t    is_rx;          // source device direction
747    uint32_t * ptr = NULL;     // local pointer on one field in iopic_input stucture
748
749    for( id = 0 ; id < CONFIG_MAX_EXTERNAL_IRQS ; id++ )
750    {
751        valid   = dev_tbl[i].irq[id].valid;
752        type    = dev_tbl[i].irq[id].dev_type;
753        channel = dev_tbl[i].irq[id].channel;
754        is_rx   = dev_tbl[i].irq[id].is_rx;
755        func    = FUNC_FROM_TYPE( type );
756
757        // get pointer on relevant field in iopic_input
758        if( valid )
759        {
760            if     ( func == DEV_FUNC_IOC )                 ptr = &iopic_input.ioc[channel]; 
761            else if((func == DEV_FUNC_TXT) && (is_rx == 0)) ptr = &iopic_input.txt_tx[channel];
762            else if((func == DEV_FUNC_TXT) && (is_rx != 0)) ptr = &iopic_input.txt_rx[channel];
763            else if((func == DEV_FUNC_NIC) && (is_rx == 0)) ptr = &iopic_input.nic_tx[channel];
764            else if((func == DEV_FUNC_NIC) && (is_rx != 0)) ptr = &iopic_input.nic_rx[channel];
765            else if( func == DEV_FUNC_IOB )                 ptr = &iopic_input.iob;
766            else
767            {
768                printk("\n[PANIC] in %s : illegal source device for IOPIC input\n",
769                __FUNCTION__ );
770                hal_core_sleep();
771            }
772
773            // set one entry in all "iopic_input" structures
774            for( x = 0 ; x < info->x_size ; x++ )
775            {
776                for( y = 0 ; y < info->y_size ; y++ )
777                {
778                    cxy_t cxy = HAL_CXY_FROM_XY( x , y );
779
780                    if( cluster_is_active( cxy ) )
781                    {
782                        hal_remote_s64( XPTR( cxy , ptr ) , id ); 
783                    }
784                }
785            }
786        }
787    } 
788
789#if( DEBUG_KERNEL_INIT & 0x1 )
790if( hal_time_stamp() > DEBUG_KERNEL_INIT )
791{
792    printk("\n[%s] created PIC chdev in cluster %x at cycle %d\n",
793    __FUNCTION__ , local_cxy , (uint32_t)hal_time_stamp() );
794    dev_pic_inputs_display();
795}
796#endif
797   
798}  // end iopic_init()
799
800///////////////////////////////////////////////////////////////////////////////////////////
801// This function is called by all core[0]s in all cluster to complete the PIC device
802// initialisation, namely the informations attached to the LAPIC controller.
803// This initialisation must be done after the IOPIC initialisation, but before other
804// devices initialisation because the IRQ routing infrastructure is required for both
805// internal and external devices initialisation.
806///////////////////////////////////////////////////////////////////////////////////////////
807// @ info    : pointer on the local boot-info structure.
808///////////////////////////////////////////////////////////////////////////////////////////
809static void __attribute__ ((noinline)) lapic_init( boot_info_t * info )
810{
811    boot_device_t * dev_tbl;      // pointer on boot_info internal devices array
812    uint32_t        dev_nr;       // number of internal devices
813    uint32_t        i;            // device index in dev_tbl
814        xptr_t          base;         // remote pointer on segment base
815    uint32_t        func;         // device functionnal type in boot_info
816    bool_t          found;        // LAPIC found
817
818    // get number of internal peripherals and base
819        dev_nr      = info->int_dev_nr;
820    dev_tbl     = info->int_dev;
821
822    // loop on internal peripherals to get the lapic device
823        for( i = 0 , found = false ; i < dev_nr ; i++ )
824        {
825        func = FUNC_FROM_TYPE( dev_tbl[i].type );
826
827        if( func == DEV_FUNC_ICU )
828        {
829            base     = dev_tbl[i].base;
830            found    = true;
831            break;
832        }
833    }
834
835    // if the LAPIC controller is not defined in the boot_info,
836    // we simply don't initialize the PIC extensions in the kernel,
837    // making the assumption that the LAPIC related informations
838    // are hidden in the hardware specific PIC driver.
839    if( found )
840    {
841        // initialise the PIC extensions for
842        // the core descriptor and core manager extensions
843        dev_pic_extend_init( (uint32_t *)GET_PTR( base ) );
844
845        // initialize the "lapic_input" structure
846        // defining how internal IRQs are connected to LAPIC
847        uint32_t        id;
848        uint8_t         valid;
849        uint8_t         channel;
850        uint32_t        func;
851
852        for( id = 0 ; id < CONFIG_MAX_INTERNAL_IRQS ; id++ )
853        {
854            valid    = dev_tbl[i].irq[id].valid;
855            func     = FUNC_FROM_TYPE( dev_tbl[i].irq[id].dev_type );
856            channel  = dev_tbl[i].irq[id].channel;
857
858            if( valid ) // only valid local IRQs are registered
859            {
860                if     ( func == DEV_FUNC_MMC ) lapic_input.mmc = id;
861                else if( func == DEV_FUNC_DMA ) lapic_input.dma[channel] = id;
862                else
863                {
864                    printk("\n[PANIC] in %s : illegal source device for LAPIC input\n",
865                    __FUNCTION__ );
866                    hal_core_sleep();
867                }
868            }
869        }
870    }
871}  // end lapic_init()
872
873///////////////////////////////////////////////////////////////////////////////////////////
874// This static function returns the identifiers of the calling core.
875///////////////////////////////////////////////////////////////////////////////////////////
876// @ info    : pointer on boot_info structure.
877// @ lid     : [out] core local index in cluster.
878// @ cxy     : [out] cluster identifier.
879// @ lid     : [out] core global identifier (hardware).
880// @ return 0 if success / return -1 if not found.
881///////////////////////////////////////////////////////////////////////////////////////////
882static error_t __attribute__ ((noinline)) get_core_identifiers( boot_info_t * info,
883                                                                lid_t       * lid,
884                                                                cxy_t       * cxy,
885                                                                gid_t       * gid )
886{
887    uint32_t   i;
888    gid_t      global_id;
889
890    // get global identifier from hardware register
891    global_id = hal_get_gid();
892
893    // makes an associative search in boot_info to get (cxy,lid) from global_id
894    for( i = 0 ; i < info->cores_nr ; i++ )
895    {
896        if( global_id == info->core[i].gid )
897        {
898            *lid = info->core[i].lid;
899            *cxy = info->core[i].cxy;
900            *gid = global_id;
901            return 0;
902        }
903    }
904    return -1;
905}
906
907
908
909
910
911///////////////////////////////////////////////////////////////////////////////////////////
912// This function is the entry point for the kernel initialisation.
913// It is executed by all cores in all clusters, but only core[cxy][0] initializes
914// the shared resources such as the cluster manager, or the local peripherals.
915// To comply with the multi-kernels paradigm, it accesses only local cluster memory, using
916// only information contained in the local boot_info_t structure, set by the bootloader.
917// Only core[0] in cluster 0 print the log messages.
918///////////////////////////////////////////////////////////////////////////////////////////
919// @ info    : pointer on the local boot-info structure.
920///////////////////////////////////////////////////////////////////////////////////////////
921void kernel_init( boot_info_t * info )
922{
923    lid_t        core_lid = -1;             // running core local index
924    cxy_t        core_cxy = -1;             // running core cluster identifier
925    gid_t        core_gid;                  // running core hardware identifier
926    cluster_t  * cluster;                   // pointer on local cluster manager
927    core_t     * core;                      // pointer on running core descriptor
928    thread_t   * thread;                    // pointer on idle thread descriptor
929
930    xptr_t       vfs_root_inode_xp;         // extended pointer on VFS root inode
931    xptr_t       devfs_dev_inode_xp;        // extended pointer on DEVFS dev inode   
932    xptr_t       devfs_external_inode_xp;   // extended pointer on DEVFS external inode       
933
934    error_t      error;
935    reg_t        status;                    // running core status register
936
937    /////////////////////////////////////////////////////////////////////////////////
938    // STEP 1 : Each core get its core identifier from boot_info, and makes
939    //          a partial initialisation of its private idle thread descriptor.
940    //          core[0] initializes the "local_cxy" global variable.
941    //          core[0] in cluster[0] initializes the TXT0 chdev for log messages.
942    /////////////////////////////////////////////////////////////////////////////////
943
944    error = get_core_identifiers( info,
945                                  &core_lid,
946                                  &core_cxy,
947                                  &core_gid );
948
949    // core[0] initialize cluster identifier
950    if( core_lid == 0 ) local_cxy = info->cxy;
951
952    // each core gets a pointer on its private idle thread descriptor
953    thread = (thread_t *)( idle_threads + (core_lid * CONFIG_THREAD_DESC_SIZE) );
954
955    // each core registers this thread pointer in hardware register
956    hal_set_current_thread( thread );
957
958    // each core register core descriptor pointer in idle thread descriptor
959    thread->core = &LOCAL_CLUSTER->core_tbl[core_lid];
960
961    // each core initializes the idle thread locks counters
962    thread->busylocks = 0;
963
964#if DEBUG_BUSYLOCK
965    // each core initialise the idle thread list of busylocks
966    xlist_root_init( XPTR( local_cxy , &thread->busylocks_root ) );
967#endif
968
969    // core[0] initializes cluster info
970    if( core_lid == 0 ) cluster_info_init( info );
971
972    // core[0] in cluster[0] initialises TXT0 chdev descriptor
973    if( (core_lid == 0) && (core_cxy == 0) ) txt0_device_init( info );
974
975    // all cores check identifiers
976    if( error )
977    {
978        printk("\n[PANIC] in %s : illegal core : gid %x / cxy %x / lid %d",
979        __FUNCTION__, core_lid, core_cxy, core_lid );
980        hal_core_sleep();
981    }
982
983    /////////////////////////////////////////////////////////////////////////////////
984    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
985                                        (info->x_size * info->y_size) );
986    barrier_wait( &local_barrier , info->cores_nr );
987    /////////////////////////////////////////////////////////////////////////////////
988
989#if DEBUG_KERNEL_INIT
990if( (core_lid ==  0) & (local_cxy == 0) ) 
991printk("\n[%s] exit barrier 1 : TXT0 initialized / cycle %d\n",
992__FUNCTION__, (uint32_t)hal_get_cycles() );
993#endif
994
995    /////////////////////////////////////////////////////////////////////////////////
996    // STEP 2 : core[0] initializes the cluster manager,
997    //          including the physical memory allocators.
998    /////////////////////////////////////////////////////////////////////////////////
999
1000    // core[0] initialises DQDT (only core[0][0] build the quad-tree)
1001    if( core_lid == 0 ) dqdt_init();
1002   
1003    // core[0] initialize other cluster manager complex structures
1004    if( core_lid == 0 )
1005    {
1006        error = cluster_manager_init( info );
1007
1008        if( error )
1009        {
1010             printk("\n[PANIC] in %s : cannot initialize cluster manager in cluster %x\n",
1011             __FUNCTION__, local_cxy );
1012             hal_core_sleep();
1013        }
1014    }
1015
1016    /////////////////////////////////////////////////////////////////////////////////
1017    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1018                                        (info->x_size * info->y_size) );
1019    barrier_wait( &local_barrier , info->cores_nr );
1020    /////////////////////////////////////////////////////////////////////////////////
1021
1022#if DEBUG_KERNEL_INIT
1023if( (core_lid ==  0) & (local_cxy == 0) ) 
1024printk("\n[%s] exit barrier 2 : cluster manager initialized / cycle %d\n",
1025__FUNCTION__, (uint32_t)hal_get_cycles() );
1026#endif
1027
1028    /////////////////////////////////////////////////////////////////////////////////
1029    // STEP 3 : all cores initialize the idle thread descriptor.
1030    //          core[0] initializes the process_zero descriptor,
1031    //          including the kernel VMM (both GPT and VSL)
1032    /////////////////////////////////////////////////////////////////////////////////
1033
1034    // all cores get pointer on local cluster manager & core descriptor
1035    cluster = &cluster_manager;
1036    core    = &cluster->core_tbl[core_lid];
1037
1038    // all cores update the register(s) defining the kernel
1039    // entry points for interrupts, exceptions and syscalls,
1040    // this must be done before VFS initialisation, because
1041    // kernel_init() uses RPCs requiring IPIs...
1042    hal_set_kentry();
1043
1044    // all cores initialize the idle thread descriptor
1045    thread_idle_init( thread,
1046                      THREAD_IDLE,
1047                      &thread_idle_func,
1048                      NULL,
1049                      core_lid );
1050
1051    // core[0] initializes the process_zero descriptor,
1052    if( core_lid == 0 ) process_zero_create( &process_zero , info );
1053
1054    /////////////////////////////////////////////////////////////////////////////////
1055    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1056                                        (info->x_size * info->y_size) );
1057    barrier_wait( &local_barrier , info->cores_nr );
1058    /////////////////////////////////////////////////////////////////////////////////
1059
1060#if DEBUG_KERNEL_INIT
1061if( (core_lid ==  0) & (local_cxy == 0) ) 
1062printk("\n[%s] exit barrier 3 : kernel processs initialized / cycle %d\n",
1063__FUNCTION__, (uint32_t)hal_get_cycles() );
1064#endif
1065
1066    /////////////////////////////////////////////////////////////////////////////////
1067    // STEP 4 : all cores initialize their private MMU
1068    //          core[0] in cluster 0 initializes the IOPIC device.
1069    /////////////////////////////////////////////////////////////////////////////////
1070
1071    // all cores initialise their MMU
1072    hal_mmu_init( &process_zero.vmm.gpt );
1073
1074    // core[0] in cluster[0] initializes the PIC chdev,
1075    if( (core_lid == 0) && (local_cxy == 0) ) iopic_init( info );
1076   
1077    ////////////////////////////////////////////////////////////////////////////////
1078    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1079                                        (info->x_size * info->y_size) );
1080    barrier_wait( &local_barrier , info->cores_nr );
1081    ////////////////////////////////////////////////////////////////////////////////
1082
1083#if DEBUG_KERNEL_INIT
1084if( (core_lid ==  0) & (local_cxy == 0) ) 
1085printk("\n[%s] exit barrier 4 : MMU and IOPIC initialized / cycle %d\n",
1086__FUNCTION__, (uint32_t)hal_get_cycles() );
1087#endif
1088
1089    ////////////////////////////////////////////////////////////////////////////////
1090    // STEP 5 : core[0] initialize the distibuted LAPIC descriptor.
1091    //          core[0] initialize the internal chdev descriptors
1092    //          core[0] initialize the local external chdev descriptors
1093    ////////////////////////////////////////////////////////////////////////////////
1094
1095    // all core[0]s initialize their local LAPIC extension,
1096    if( core_lid == 0 ) lapic_init( info );
1097
1098    // core[0] scan the internal (private) peripherals,
1099    // and allocates memory for the corresponding chdev descriptors.
1100    if( core_lid == 0 ) internal_devices_init( info );
1101       
1102
1103    // All core[0]s contribute to initialise external peripheral chdev descriptors.
1104    // Each core[0][cxy] scan the set of external (shared) peripherals (but the TXT0),
1105    // and allocates memory for the chdev descriptors that must be placed
1106    // on the (cxy) cluster according to the global index value.
1107
1108    if( core_lid == 0 ) external_devices_init( info );
1109
1110    /////////////////////////////////////////////////////////////////////////////////
1111    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1112                                        (info->x_size * info->y_size) );
1113    barrier_wait( &local_barrier , info->cores_nr );
1114    /////////////////////////////////////////////////////////////////////////////////
1115
1116#if DEBUG_KERNEL_INIT
1117if( (core_lid ==  0) & (local_cxy == 0) ) 
1118printk("\n[%s] exit barrier 5 : chdevs initialised / cycle %d\n",
1119__FUNCTION__, (uint32_t)hal_get_cycles() );
1120#endif
1121
1122#if CONFIG_INSTRUMENTATION_CHDEVS
1123if( (core_lid ==  0) & (local_cxy == 0) ) 
1124chdev_dir_display();
1125#endif
1126   
1127    /////////////////////////////////////////////////////////////////////////////////
1128    // STEP 6 : All cores enable IPI (Inter Procesor Interrupt),
1129    //          All cores unblock the idle thread, and register it in scheduler.
1130    //          The core[0] in cluster defined by the CONFIG_VFS_ROOT_CXY parameter,
1131    //          access the IOC device to initialize the VFS for the FS identified
1132    //          by the CONFIG_VFS_ROOT_IS_*** parameter. It does the following
1133    //          actions in the VFS_ROOT cluster :
1134    //          1. allocate and initialize the selected FS context,
1135    //          2. create and initializes the VFS root inodes,
1136    //          3. initialize the VFS context for FATFS (in fs_context[] array),
1137    //          4. create the <.> and <..> dentries in VFS root directory,
1138    //          5. register the VFS root inode in process_zero descriptor,
1139    //          6. allocate the DEVFS context,
1140    //          7. initialize the VFS context for DEVFS (in fs_context[] array),
1141    //          8. create the <dev> and <external> inodes,
1142    //          9. initialize the DEVFS context.
1143    /////////////////////////////////////////////////////////////////////////////////
1144
1145    // All cores enable IPI
1146    dev_pic_enable_ipi();
1147    hal_enable_irq( &status );
1148
1149    // all cores unblock the idle thread, and register it in scheduler
1150    thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
1151    core->scheduler.idle = thread;
1152
1153    // core[O] in VFS_ROOT cluster creates the VFS root
1154    if( (core_lid ==  0) && (local_cxy == CONFIG_VFS_ROOT_CXY ) ) 
1155    {
1156        // Only FATFS is supported yet,
1157        // TODO other File System can be introduced below
1158        if( CONFIG_VFS_ROOT_IS_FATFS )
1159        {
1160            // 1. allocate memory and initialize FATFS context in VFS_ROOT cluster
1161            xptr_t  fatfs_ctx_xp = fatfs_ctx_alloc( CONFIG_VFS_ROOT_CXY );
1162
1163            if( fatfs_ctx_xp == XPTR_NULL )
1164            {
1165                printk("\n[PANIC] in %s : cannot allocate FATFS context in cluster %x\n",
1166                __FUNCTION__ , CONFIG_VFS_ROOT_CXY );
1167                hal_core_sleep();
1168            }
1169
1170            // initialise FATFS context in VFS_ROOT cluster from IOC device (boot_record)
1171            error = fatfs_ctx_init( fatfs_ctx_xp );
1172
1173            if( error )
1174            {
1175                printk("\n[PANIC] in %s : cannot initialize FATFS context in cluster %x\n",
1176                __FUNCTION__ , CONFIG_VFS_ROOT_CXY );
1177                hal_core_sleep();
1178            }
1179
1180#if( DEBUG_KERNEL_INIT & 1 )
1181printk("\n[%s] initialized FATFS context in cluster %x\n",
1182__FUNCTION__, CONFIG_VFS_ROOT_CXY );
1183#endif
1184   
1185            // get various informations from FATFS context
1186            fatfs_ctx_t * fatfs_ctx_ptr = GET_PTR( fatfs_ctx_xp );
1187
1188            uint32_t root_dir_cluster    = hal_remote_l32( XPTR( CONFIG_VFS_ROOT_CXY,
1189                                           &fatfs_ctx_ptr->root_dir_cluster ) );
1190
1191            uint32_t bytes_per_sector    = hal_remote_l32( XPTR( CONFIG_VFS_ROOT_CXY,
1192                                           &fatfs_ctx_ptr->bytes_per_sector ) );
1193 
1194            uint32_t sectors_per_cluster = hal_remote_l32( XPTR( CONFIG_VFS_ROOT_CXY,
1195                                           &fatfs_ctx_ptr->sectors_per_cluster ) );
1196 
1197            uint32_t cluster_size        = bytes_per_sector * sectors_per_cluster;
1198 
1199            uint32_t fat_sectors_count   = hal_remote_l32( XPTR( CONFIG_VFS_ROOT_CXY,
1200                                           &fatfs_ctx_ptr->fat_sectors_count ) ) << 7;
1201
1202            uint32_t total_clusters      = fat_sectors_count << 7;
1203 
1204            // 2. create VFS root inode in VFS_ROOT cluster
1205            // TODO define attr, rights, uid, gid
1206            error = vfs_inode_create( CONFIG_VFS_ROOT_CXY,          // target cluster
1207                                      FS_TYPE_FATFS,                // fs_type
1208                                      0,                            // attr
1209                                      0,                            // rights
1210                                      0,                            // uid
1211                                      0,                            // gid
1212                                      &vfs_root_inode_xp );         // return
1213            if( error )
1214            {
1215                printk("\n[PANIC] in %s : cannot create VFS root inode in cluster %x\n",
1216                __FUNCTION__ , CONFIG_VFS_ROOT_CXY );
1217                hal_core_sleep();
1218            }
1219
1220#if( DEBUG_KERNEL_INIT & 1 )
1221vfs_inode_t * root_inode = GET_PTR( vfs_root_inode_xp );
1222printk("\n[%s] created </> root inode %x in cluster %x / ctx %x\n",
1223__FUNCTION__, root_inode, CONFIG_VFS_ROOT_CXY, root_inode->ctx );
1224#endif
1225   
1226            // update FATFS root inode "type" and "extend" fields 
1227            vfs_inode_t * vfs_root_inode_ptr = GET_PTR( vfs_root_inode_xp );
1228
1229            hal_remote_s32( XPTR( CONFIG_VFS_ROOT_CXY , &vfs_root_inode_ptr->type ),
1230                            INODE_TYPE_DIR );
1231
1232            hal_remote_spt( XPTR( CONFIG_VFS_ROOT_CXY , &vfs_root_inode_ptr->extend ), 
1233                            (void*)(intptr_t)root_dir_cluster );
1234
1235            // 3. initialize the VFS context for FATFS in VFS_ROOT cluster
1236            vfs_ctx_init( CONFIG_VFS_ROOT_CXY,                      // target cluster
1237                          FS_TYPE_FATFS,                            // fs type
1238                              total_clusters,                           // number of clusters
1239                              cluster_size,                             // bytes
1240                              vfs_root_inode_xp,                        // VFS root
1241                          fatfs_ctx_ptr );                          // extend
1242
1243#if( DEBUG_KERNEL_INIT & 1 )
1244vfs_ctx_t * vfs_for_fatfs_ctx =  &fs_context[FS_TYPE_FATFS];
1245printk("\n[%s] initialized VFS_for_FATFS context in cluster %x / ctx %x / fs_type %d\n",
1246__FUNCTION__, CONFIG_VFS_ROOT_CXY, vfs_for_fatfs_ctx, vfs_for_fatfs_ctx->type );
1247#endif
1248        }
1249        else
1250        {
1251            printk("\n[PANIC] in %s : unsupported VFS type in cluster %x\n",
1252            __FUNCTION__ , CONFIG_VFS_ROOT_CXY );
1253            hal_core_sleep();
1254        }
1255
1256        // 4. create the <.> and <..> dentries in VFS root directory
1257        // the VFS root parent inode is the VFS root inode itself
1258        vfs_add_special_dentries( vfs_root_inode_xp,
1259                                  vfs_root_inode_xp );
1260
1261        // 5. register VFS root inode in target cluster process_zero descriptor
1262        hal_remote_s64( XPTR( CONFIG_VFS_ROOT_CXY , &process_zero.vfs_root_xp ),
1263                        vfs_root_inode_xp );
1264        hal_remote_s64( XPTR( CONFIG_VFS_ROOT_CXY , &process_zero.cwd_xp ),
1265                        vfs_root_inode_xp );
1266
1267        // 6. allocate memory for DEVFS context in VFS_ROOT cluster
1268        xptr_t devfs_ctx_xp = devfs_ctx_alloc( CONFIG_VFS_ROOT_CXY );
1269
1270        if( devfs_ctx_xp == XPTR_NULL )
1271        {
1272            printk("\n[PANIC] in %s : cannot create DEVFS context in cluster %x\n",
1273            __FUNCTION__ , CONFIG_VFS_ROOT_CXY );
1274            hal_core_sleep();
1275        }
1276
1277        // 7. initialize the VFS context for DEVFS in VFS_ROOT cluster
1278        vfs_ctx_init( CONFIG_VFS_ROOT_CXY,                          // target cluster
1279                      FS_TYPE_DEVFS,                                // fs type
1280                          0,                                            // total_clusters: unused
1281                          0,                                            // cluster_size: unused
1282                          vfs_root_inode_xp,                            // VFS root
1283                      GET_PTR( devfs_ctx_xp ) );                    // extend
1284
1285#if( DEBUG_KERNEL_INIT & 1 )
1286vfs_ctx_t * vfs_for_devfs_ctx =  &fs_context[FS_TYPE_DEVFS];
1287printk("\n[%s] initialized VFS_for_DEVFS context in cluster %x / ctx %x / fs_type %d\n",
1288__FUNCTION__, CONFIG_VFS_ROOT_CXY, vfs_for_devfs_ctx, vfs_for_devfs_ctx->type );
1289#endif
1290
1291        // 8. create "dev" and "external" inodes (directories)
1292        devfs_global_init( vfs_root_inode_xp,
1293                           &devfs_dev_inode_xp,
1294                           &devfs_external_inode_xp );
1295
1296        // 9. initializes DEVFS context in VFS_ROOT cluster
1297        devfs_ctx_init( devfs_ctx_xp,
1298                        devfs_dev_inode_xp,
1299                        devfs_external_inode_xp );
1300    }
1301
1302    /////////////////////////////////////////////////////////////////////////////////
1303    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1304                                        (info->x_size * info->y_size) );
1305    barrier_wait( &local_barrier , info->cores_nr );
1306    /////////////////////////////////////////////////////////////////////////////////
1307
1308#if DEBUG_KERNEL_INIT
1309if( (core_lid ==  0) & (local_cxy == CONFIG_VFS_ROOT_CXY) ) 
1310printk("\n[%s] exit barrier 6 : VFS root inode (%x) created in cluster (%x) / cycle %d\n",
1311__FUNCTION__, GET_CXY(vfs_root_inode_xp), 
1312GET_PTR(vfs_root_inode_xp), (uint32_t)hal_get_cycles() );
1313#endif
1314
1315    /////////////////////////////////////////////////////////////////////////////////
1316    // STEP 7 : In all clusters other than the VFS_ROOT cluster, the core[0] makes
1317    //          the following local actions to complete the VFS initialisation :
1318    //          1. allocate a local context for the selected FS extension,
1319    //          2. copy FS context from VFS_ROOT cluster to local cluster,
1320    //          3. copy VFS_for_FATFS context from VFS_ROOT cluster to local cluster,
1321    //          4. allocate a local context for the DEVFS extension,
1322    //          5. copy DEVFS context from VFS_ROOT cluster to local cluster,
1323    //          6. update the local "root_inode_xp" field in process_zero.
1324    /////////////////////////////////////////////////////////////////////////////////
1325
1326    if( (core_lid ==  0) && (local_cxy != CONFIG_VFS_ROOT_CXY) ) 
1327    {
1328        // only FATFS is supported yet
1329        // TODO other File System can be introduced below
1330        if( CONFIG_VFS_ROOT_IS_FATFS )
1331        {
1332            // 1. allocate a local FATFS context extension
1333            xptr_t local_fatfs_ctx_xp = fatfs_ctx_alloc( local_cxy );
1334
1335            if( local_fatfs_ctx_xp == XPTR_NULL )
1336            {
1337                printk("\n[PANIC] in %s : cannot create FATFS context in cluster %x\n",
1338                __FUNCTION__ , local_cxy );
1339                hal_core_sleep();
1340            }
1341
1342            // get local pointer on VFS_for_FATFS context (same in all clusters)
1343            vfs_ctx_t * vfs_fat_ctx_ptr = &fs_context[FS_TYPE_FATFS];
1344
1345            // build extended pointer on VFS_for_FATFS "extend" field in VFS_ROOT cluster
1346            xptr_t fatfs_extend_xp = XPTR( CONFIG_VFS_ROOT_CXY , &vfs_fat_ctx_ptr->extend );
1347
1348            // get local pointer on FATFS context in VFS_ROOT cluster
1349            fatfs_ctx_t * remote_fatfs_ctx_ptr = hal_remote_lpt( fatfs_extend_xp );
1350
1351            // build extended pointer on FATFS context in VFS_ROOT cluster
1352            xptr_t remote_fatfs_ctx_xp = XPTR( CONFIG_VFS_ROOT_CXY , remote_fatfs_ctx_ptr );
1353
1354            // 2. copy FATFS context from VFS_ROOT cluster to local cluster
1355            hal_remote_memcpy( local_fatfs_ctx_xp,
1356                               remote_fatfs_ctx_xp,
1357                               sizeof(fatfs_ctx_t) );
1358
1359            // build extended pointer on remote VFS_for_FATFS context
1360            xptr_t remote_vfs_ctx_xp = XPTR( CONFIG_VFS_ROOT_CXY , vfs_fat_ctx_ptr );
1361
1362            // build extended pointer on local VFS_for_FATFS context
1363            xptr_t local_vfs_ctx_xp = XPTR( local_cxy , vfs_fat_ctx_ptr );
1364 
1365            // 3. copy VFS_for_FATFS context from VFS_ROOT cluster to local cluster
1366            hal_remote_memcpy( local_vfs_ctx_xp,
1367                               remote_vfs_ctx_xp,
1368                               sizeof(vfs_ctx_t) );
1369
1370            // update "extend" field in local VFS_for_FATFS context
1371            vfs_fat_ctx_ptr->extend = GET_PTR( local_fatfs_ctx_xp );
1372
1373// check local FATFS and VFS context copies
1374assert( (((fatfs_ctx_t *)vfs_fat_ctx_ptr->extend)->sectors_per_cluster == 8),
1375"illegal FATFS context in cluster %x\n", local_cxy );
1376
1377        }
1378        else
1379        {
1380            printk("\n[PANIC] in %s : unsupported VFS type in cluster %x\n",
1381            __FUNCTION__ , local_cxy );
1382            hal_core_sleep();
1383        }
1384
1385        // 4. allocate a local DEVFS context extension,
1386        xptr_t local_devfs_ctx_xp = devfs_ctx_alloc( local_cxy );
1387
1388        // get local pointer on VFS_for_DEVFS context (same in all clusters)
1389        vfs_ctx_t * vfs_dev_ctx_ptr = &fs_context[FS_TYPE_DEVFS];
1390
1391        // build extended pointer on VFS_for_DEVFS extend field in VFS_ROOT cluster
1392        xptr_t remote_extend_xp = XPTR( CONFIG_VFS_ROOT_CXY , &vfs_dev_ctx_ptr->extend );
1393
1394        // get local pointer on DEVFS context in VFS_ROOT cluster
1395        devfs_ctx_t * remote_devfs_ctx_ptr = hal_remote_lpt( remote_extend_xp );
1396
1397        // build extended pointer on FATFS context in VFS_ROOT cluster
1398        xptr_t remote_devfs_ctx_xp = XPTR( CONFIG_VFS_ROOT_CXY , remote_devfs_ctx_ptr );
1399
1400        // 5. copy DEVFS context from VFS_ROOT cluster to local cluster
1401        hal_remote_memcpy( local_devfs_ctx_xp,
1402                           remote_devfs_ctx_xp,
1403                           sizeof(devfs_ctx_t) );
1404
1405        // update "extend" field in local VFS_for_DEVFS context
1406        vfs_dev_ctx_ptr->extend = GET_PTR( local_devfs_ctx_xp );
1407
1408        // get extended pointer on VFS root inode from VFS_ROOT cluster
1409        vfs_root_inode_xp = hal_remote_l64( XPTR( CONFIG_VFS_ROOT_CXY,
1410                                                  &process_zero.vfs_root_xp ) );
1411
1412        // 6. update local process_zero descriptor
1413        process_zero.vfs_root_xp = vfs_root_inode_xp;
1414        process_zero.cwd_xp      = vfs_root_inode_xp;
1415    }
1416
1417    /////////////////////////////////////////////////////////////////////////////////
1418    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1419                                        (info->x_size * info->y_size) );
1420    barrier_wait( &local_barrier , info->cores_nr );
1421    /////////////////////////////////////////////////////////////////////////////////
1422
1423#if DEBUG_KERNEL_INIT
1424if( (core_lid ==  0) & (local_cxy == 0) ) 
1425printk("\n[%s] exit barrier 7 : VFS & DEVFS contexts replicated in all clusters / cycle %d\n",
1426__FUNCTION__ , (uint32_t)hal_get_cycles() );
1427#endif
1428
1429    /////////////////////////////////////////////////////////////////////////////////
1430    // STEP 8 : In all clusters in parallel, core[0] completes DEVFS initialization.
1431    //          Each core[0] creates the local DEVFS "internal" directory,
1432    //          and creates the pseudo-files for chdevs placed in local cluster.
1433    /////////////////////////////////////////////////////////////////////////////////
1434
1435    if( core_lid == 0 )
1436    {
1437        // get local pointer on local DEVFS context
1438        devfs_ctx_t * ctx = fs_context[FS_TYPE_DEVFS].extend;
1439
1440        // populate DEVFS in all clusters
1441        devfs_local_init( ctx->dev_inode_xp,
1442                          ctx->external_inode_xp );
1443    }
1444
1445    /////////////////////////////////////////////////////////////////////////////////
1446    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1447                                        (info->x_size * info->y_size) );
1448    barrier_wait( &local_barrier , info->cores_nr );
1449    /////////////////////////////////////////////////////////////////////////////////
1450
1451#if DEBUG_KERNEL_INIT
1452if( (core_lid ==  0) & (local_cxy == 0) ) 
1453printk("\n[%s] exit barrier 8 : DEVFS initialized in all clusters / cycle %d\n",
1454__FUNCTION__, (uint32_t)hal_get_cycles() );
1455#endif
1456
1457#if( DEBUG_KERNEL_INIT & 1 )
1458if( (core_lid ==  0) & (local_cxy == 0) ) 
1459vfs_display( vfs_root_inode_xp );
1460#endif
1461
1462    /////////////////////////////////////////////////////////////////////////////////
1463    // STEP 9 : core[0] in cluster 0 creates the first user process (process_init).
1464    //          This include the process VMM (GPT and VSL) creation.
1465    //          Finally, it prints the ALMOS-MKH banner.
1466    /////////////////////////////////////////////////////////////////////////////////
1467
1468    if( (core_lid == 0) && (local_cxy == 0) ) 
1469    {
1470       process_init_create();
1471    }
1472
1473#if DEBUG_KERNEL_INIT
1474if( (core_lid ==  0) & (local_cxy == 0) ) 
1475printk("\n[%s] exit barrier 9 : process_init created in cluster 0 / cycle %d\n",
1476__FUNCTION__, (uint32_t)hal_get_cycles() );
1477#endif
1478
1479    if( (core_lid == 0) && (local_cxy == 0) ) 
1480    {
1481        print_banner( (info->x_size * info->y_size) , info->cores_nr );
1482    }
1483
1484#if CONFIG_INSTRUMENTATION_FOOTPRINT
1485if( (core_lid ==  0) & (local_cxy == 0) ) 
1486printk("\n\n***** memory fooprint for main kernel objects\n\n"
1487                   " - thread descriptor  : %d bytes\n"
1488                   " - process descriptor : %d bytes\n"
1489                   " - cluster manager    : %d bytes\n"
1490                   " - chdev descriptor   : %d bytes\n"
1491                   " - core descriptor    : %d bytes\n"
1492                   " - scheduler          : %d bytes\n"
1493                   " - socket             : %d bytes\n"
1494                   " - rpc fifo           : %d bytes\n"
1495                   " - page descriptor    : %d bytes\n"
1496                   " - mapper descriptor  : %d bytes\n"
1497                   " - vseg descriptor    : %d bytes\n"
1498                   " - ppm manager        : %d bytes\n"
1499                   " - kcm manager        : %d bytes\n"
1500                   " - khm manager        : %d bytes\n"
1501                   " - vmm manager        : %d bytes\n"
1502                   " - vfs inode          : %d bytes\n"
1503                   " - vfs dentry         : %d bytes\n"
1504                   " - vfs file           : %d bytes\n"
1505                   " - vfs context        : %d bytes\n"
1506                   " - xhtab root         : %d bytes\n"
1507                   " - list item          : %d bytes\n"
1508                   " - xlist item         : %d bytes\n"
1509                   " - busylock           : %d bytes\n"
1510                   " - remote busylock    : %d bytes\n"
1511                   " - queuelock          : %d bytes\n"
1512                   " - remote queuelock   : %d bytes\n"
1513                   " - rwlock             : %d bytes\n"
1514                   " - remote rwlock      : %d bytes\n",
1515                   sizeof( thread_t           ),
1516                   sizeof( process_t          ),
1517                   sizeof( cluster_t          ),
1518                   sizeof( chdev_t            ),
1519                   sizeof( core_t             ),
1520                   sizeof( scheduler_t        ),
1521                   sizeof( socket_t           ),
1522                   sizeof( remote_fifo_t      ),
1523                   sizeof( page_t             ),
1524                   sizeof( mapper_t           ),
1525                   sizeof( vseg_t             ),
1526                   sizeof( ppm_t              ),
1527                   sizeof( kcm_t              ),
1528                   sizeof( khm_t              ),
1529                   sizeof( vmm_t              ),
1530                   sizeof( vfs_inode_t        ),
1531                   sizeof( vfs_dentry_t       ),
1532                   sizeof( vfs_file_t         ),
1533                   sizeof( vfs_ctx_t          ),
1534                   sizeof( xhtab_t            ),
1535                   sizeof( list_entry_t       ),
1536                   sizeof( xlist_entry_t      ),
1537                   sizeof( busylock_t         ),
1538                   sizeof( remote_busylock_t  ),
1539                   sizeof( queuelock_t        ),
1540                   sizeof( remote_queuelock_t ),
1541                   sizeof( rwlock_t           ),
1542                   sizeof( remote_rwlock_t    ));
1543#endif
1544
1545    // each core activates its private TICK IRQ
1546    dev_pic_enable_timer( CONFIG_SCHED_TICK_MS_PERIOD );
1547
1548    /////////////////////////////////////////////////////////////////////////////////
1549    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ),
1550                                        (info->x_size * info->y_size) );
1551    barrier_wait( &local_barrier , info->cores_nr );
1552    /////////////////////////////////////////////////////////////////////////////////
1553
1554#if DEBUG_KERNEL_INIT
1555thread_t * this = CURRENT_THREAD;
1556printk("\n[%s] : thread[%x,%x] on core[%x,%d] jumps to thread_idle_func() / cycle %d\n",
1557__FUNCTION__ , this->process->pid, this->trdid,
1558local_cxy, core_lid, (uint32_t)hal_get_cycles() );
1559#endif
1560
1561    // each core jump to thread_idle_func
1562    thread_idle_func();
1563
1564}  // end kernel_init()
1565
Note: See TracBrowser for help on using the repository browser.