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

Last change on this file since 628 was 628, checked in by alain, 5 years ago

Introduce teh page_min / page_max mechanism in the fatfs_release_inode()
function, to avoid to scan all pages in FAT mapper.

File size: 62.0 KB
Line 
1/*
2 * kernel_init.c - kernel parallel initialization
3 *
4 * Authors :  Mohamed Lamine Karaoui (2015)
5 *            Alain Greiner  (2016,2017,2018,2019)
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 <kmem.h>
43#include <cluster.h>
44#include <string.h>
45#include <memcpy.h>
46#include <ppm.h>
47#include <page.h>
48#include <chdev.h>
49#include <boot_info.h>
50#include <dqdt.h>
51#include <dev_mmc.h>
52#include <dev_dma.h>
53#include <dev_iob.h>
54#include <dev_ioc.h>
55#include <dev_txt.h>
56#include <dev_pic.h>
57#include <printk.h>
58#include <vfs.h>
59#include <devfs.h>
60#include <mapper.h>
61
62///////////////////////////////////////////////////////////////////////////////////////////
63// All the following global variables are replicated in all clusters.
64// They are initialised by the kernel_init() function.
65//
66// WARNING : The section names have been defined to control the base addresses of the
67// boot_info structure and the idle thread descriptors, through the kernel.ld script:
68// - the boot_info structure is built by the bootloader, and used by kernel_init.
69//   it must be the first object in the kdata segment.
70// - the array of idle threads descriptors must be placed on the first page boundary after
71//   the boot_info structure in the kdata segment.
72///////////////////////////////////////////////////////////////////////////////////////////
73
74// This variable defines the local boot_info structure
75__attribute__((section(".kinfo")))
76boot_info_t          boot_info;
77
78// This variable defines the "idle" threads descriptors array
79__attribute__((section(".kidle")))
80char                 idle_threads[CONFIG_THREAD_DESC_SIZE *
81                                   CONFIG_MAX_LOCAL_CORES]   CONFIG_PPM_PAGE_ALIGNED;
82
83// This variable defines the local cluster manager
84__attribute__((section(".kdata")))
85cluster_t            cluster_manager                         CONFIG_CACHE_LINE_ALIGNED;
86
87// This variable defines the TXT_TX[0] chdev
88__attribute__((section(".kdata")))
89chdev_t              txt0_tx_chdev                           CONFIG_CACHE_LINE_ALIGNED;
90
91// This variable defines the TXT_RX[0] chdev
92__attribute__((section(".kdata")))
93chdev_t              txt0_rx_chdev                           CONFIG_CACHE_LINE_ALIGNED;
94
95// This variables define the kernel process0 descriptor
96__attribute__((section(".kdata")))
97process_t            process_zero                            CONFIG_CACHE_LINE_ALIGNED;
98
99// This variable defines a set of extended pointers on the distributed chdevs
100__attribute__((section(".kdata")))
101chdev_directory_t    chdev_dir                               CONFIG_CACHE_LINE_ALIGNED;
102
103// This variable contains the input IRQ indexes for the IOPIC controller
104__attribute__((section(".kdata")))
105iopic_input_t        iopic_input                             CONFIG_CACHE_LINE_ALIGNED;
106
107// This variable contains the input IRQ indexes for the LAPIC controller
108__attribute__((section(".kdata")))
109lapic_input_t        lapic_input                             CONFIG_CACHE_LINE_ALIGNED;
110
111// This variable defines the local cluster identifier
112__attribute__((section(".kdata")))
113cxy_t                local_cxy                               CONFIG_CACHE_LINE_ALIGNED;
114
115// This variable is used for core[0] cores synchronisation in kernel_init()
116__attribute__((section(".kdata")))
117xbarrier_t           global_barrier                          CONFIG_CACHE_LINE_ALIGNED;
118
119// This variable is used for local cores synchronisation in kernel_init()
120__attribute__((section(".kdata")))
121barrier_t            local_barrier                           CONFIG_CACHE_LINE_ALIGNED;
122
123// This variable defines the array of supported File System contexts
124__attribute__((section(".kdata")))
125vfs_ctx_t            fs_context[FS_TYPES_NR]                 CONFIG_CACHE_LINE_ALIGNED;
126
127// This array is used for debug, and describes the kernel locks usage,
128// It must be kept consistent with the defines in kernel_config.h file.
129__attribute__((section(".kdata")))
130char * lock_type_str[] =
131{
132    "unused_0",              //  0
133
134    "CLUSTER_KCM",           //  1
135    "PPM_FREE",              //  2
136    "SCHED_STATE",           //  3
137    "VMM_STACK",             //  4
138    "VMM_MMAP",              //  5
139    "VFS_CTX",               //  6
140    "KCM_STATE",             //  7
141    "KHM_STATE",             //  8
142    "HTAB_STATE",            //  9
143
144    "THREAD_JOIN",           // 10
145    "XHTAB_STATE",           // 11
146    "CHDEV_QUEUE",           // 12
147    "CHDEV_TXT0",            // 13
148    "CHDEV_TXTLIST",         // 14
149    "PAGE_STATE",            // 15
150    "MUTEX_STATE",           // 16
151    "CONDVAR_STATE",         // 17
152    "SEM_STATE",             // 18
153    "PROCESS_CWD",           // 19
154    "BARRIER_STATE",         // 20
155
156    "CLUSTER_PREFTBL",       // 21
157
158    "PPM_DIRTY",             // 22
159    "CLUSTER_LOCALS",        // 23
160    "CLUSTER_COPIES",        // 24
161    "PROCESS_CHILDREN",      // 25
162    "PROCESS_USERSYNC",      // 26
163    "PROCESS_FDARRAY",       // 27
164    "PROCESS_DIR",           // 28
165    "unused_29",             // 29
166
167    "PROCESS_THTBL",         // 30
168
169    "MAPPER_STATE",          // 31
170    "VFS_SIZE",              // 32
171    "VFS_FILE",              // 33
172    "VMM_VSL",               // 34
173    "VMM_GPT",               // 35
174    "VFS_MAIN",              // 36
175    "FATFS_FAT",             // 37
176};       
177
178// debug variables to analyse the sys_read() and sys_write() syscalls timing
179
180#if DEBUG_SYS_READ
181uint32_t   enter_sys_read;
182uint32_t   exit_sys_read;
183
184uint32_t   enter_devfs_read;
185uint32_t   exit_devfs_read;
186
187uint32_t   enter_txt_read;
188uint32_t   exit_txt_read;
189
190uint32_t   enter_chdev_cmd_read;
191uint32_t   exit_chdev_cmd_read;
192
193uint32_t   enter_chdev_server_read;
194uint32_t   exit_chdev_server_read;
195
196uint32_t   enter_tty_cmd_read;
197uint32_t   exit_tty_cmd_read;
198
199uint32_t   enter_tty_isr_read;
200uint32_t   exit_tty_isr_read;
201#endif
202
203// these debug variables are used to analyse the sys_write() syscall timing
204
205#if DEBUG_SYS_WRITE   
206uint32_t   enter_sys_write;
207uint32_t   exit_sys_write;
208
209uint32_t   enter_devfs_write;
210uint32_t   exit_devfs_write;
211
212uint32_t   enter_txt_write;
213uint32_t   exit_txt_write;
214
215uint32_t   enter_chdev_cmd_write;
216uint32_t   exit_chdev_cmd_write;
217
218uint32_t   enter_chdev_server_write;
219uint32_t   exit_chdev_server_write;
220
221uint32_t   enter_tty_cmd_write;
222uint32_t   exit_tty_cmd_write;
223
224uint32_t   enter_tty_isr_write;
225uint32_t   exit_tty_isr_write;
226#endif
227
228// intrumentation variables : cumulated costs per syscall type in cluster
229
230#if CONFIG_INSTRUMENTATION_SYSCALLS
231__attribute__((section(".kdata")))
232uint32_t   syscalls_cumul_cost[SYSCALLS_NR];
233
234__attribute__((section(".kdata")))
235uint32_t   syscalls_occurences[SYSCALLS_NR];
236#endif
237
238///////////////////////////////////////////////////////////////////////////////////////////
239// This function displays the ALMOS_MKH banner.
240///////////////////////////////////////////////////////////////////////////////////////////
241static void print_banner( uint32_t nclusters , uint32_t ncores )
242{
243    printk("\n"
244           "                    _        __    __     _____     ______         __    __    _   __   _     _   \n"
245           "          /\\       | |      |  \\  /  |   / ___ \\   / _____|       |  \\  /  |  | | / /  | |   | |  \n"
246           "         /  \\      | |      |   \\/   |  | /   \\ | | /             |   \\/   |  | |/ /   | |   | |  \n"
247           "        / /\\ \\     | |      | |\\  /| |  | |   | | | |_____   ___  | |\\  /| |  |   /    | |___| |  \n"
248           "       / /__\\ \\    | |      | | \\/ | |  | |   | | \\_____  \\ |___| | | \\/ | |  |   \\    |  ___  |  \n"
249           "      / ______ \\   | |      | |    | |  | |   | |       | |       | |    | |  | |\\ \\   | |   | |  \n"
250           "     / /      \\ \\  | |____  | |    | |  | \\___/ |  _____/ |       | |    | |  | | \\ \\  | |   | |  \n"
251           "    /_/        \\_\\ |______| |_|    |_|   \\_____/  |______/        |_|    |_|  |_|  \\_\\ |_|   |_|  \n"
252           "\n\n\t\t Advanced Locality Management Operating System / Multi Kernel Hybrid\n"
253           "\n\n\t\t %s / %d cluster(s) / %d core(s) per cluster\n\n",
254           CONFIG_ALMOS_VERSION , nclusters , ncores );
255}
256
257
258///////////////////////////////////////////////////////////////////////////////////////////
259// This function initializes the TXT_TX[0] and TXT_RX[0] chdev descriptors, implementing
260// the "kernel terminal", shared by all kernel instances for debug messages.
261// These chdev are implemented as global variables (replicated in all clusters),
262// because this terminal is used before the kmem allocator initialisation, but only
263// the chdevs in cluster 0 are registered in the "chdev_dir" directory.
264// As this TXT0 chdev supports only the TXT_SYNC_WRITE command, we don't create
265// a server thread, we don't allocate a WTI, and we don't initialize the waiting queue.
266// Note: The TXT_RX[0] chdev is created, but is not used by ALMOS-MKH (september 2018).
267///////////////////////////////////////////////////////////////////////////////////////////
268// @ info    : pointer on the local boot-info structure.
269///////////////////////////////////////////////////////////////////////////////////////////
270static void __attribute__ ((noinline)) txt0_device_init( boot_info_t * info )
271{
272    boot_device_t * dev_tbl;         // pointer on array of devices in boot_info
273    uint32_t        dev_nr;          // actual number of devices in this cluster
274    xptr_t          base;            // remote pointer on segment base
275    uint32_t        func;            // device functional index
276    uint32_t        impl;            // device implementation index
277    uint32_t        i;               // device index in dev_tbl
278    uint32_t        x;               // X cluster coordinate
279    uint32_t        y;               // Y cluster coordinate
280    uint32_t        channels;        // number of channels
281
282    // get number of peripherals and base of devices array from boot_info
283    dev_nr      = info->ext_dev_nr;
284    dev_tbl     = info->ext_dev;
285
286    // loop on external peripherals to find TXT device
287    for( i = 0 ; i < dev_nr ; i++ )
288    {
289        base        = dev_tbl[i].base;
290        func        = FUNC_FROM_TYPE( dev_tbl[i].type );
291        impl        = IMPL_FROM_TYPE( dev_tbl[i].type );
292        channels    = dev_tbl[i].channels;
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                    chdev = chdev_create( func,
568                                          impl,
569                                          channel,
570                                          rx,          // direction
571                                          base );
572
573                    if( chdev == NULL )
574                    {
575                        printk("\n[PANIC] in %s : cannot allocate chdev\n",
576                        __FUNCTION__ );
577                        hal_core_sleep();
578                    }
579
580                    // make device type specific initialisation
581                    if     ( func == DEV_FUNC_IOB ) dev_iob_init( chdev );
582                    else if( func == DEV_FUNC_IOC ) dev_ioc_init( chdev );
583                    else if( func == DEV_FUNC_TXT ) dev_txt_init( chdev );
584                    else if( func == DEV_FUNC_NIC ) dev_nic_init( chdev );
585                    else if( func == DEV_FUNC_FBF ) dev_fbf_init( chdev );
586
587                    // all external (shared) devices are remotely accessible
588                    // initialize the replicated chdev_dir[x][y] structures
589                    // defining the extended pointers on chdev descriptors
590                    xptr_t * entry;
591
592                    if(func==DEV_FUNC_IOB             ) entry  = &chdev_dir.iob;
593                    if(func==DEV_FUNC_IOC             ) entry  = &chdev_dir.ioc[channel];
594                    if(func==DEV_FUNC_FBF             ) entry  = &chdev_dir.fbf[channel];
595                    if((func==DEV_FUNC_TXT) && (rx==0)) entry  = &chdev_dir.txt_tx[channel];
596                    if((func==DEV_FUNC_TXT) && (rx==1)) entry  = &chdev_dir.txt_rx[channel];
597                    if((func==DEV_FUNC_NIC) && (rx==0)) entry  = &chdev_dir.nic_tx[channel];
598                    if((func==DEV_FUNC_NIC) && (rx==1)) entry  = &chdev_dir.nic_rx[channel];
599
600                    for( x = 0 ; x < info->x_size ; x++ )
601                    {
602                        for( y = 0 ; y < info->y_size ; y++ )
603                        {
604                            cxy_t cxy = HAL_CXY_FROM_XY( x , y );
605
606                            if( cluster_is_active( cxy ) )
607                            {
608                                hal_remote_s64( XPTR( cxy , entry ),
609                                                XPTR( local_cxy , chdev ) );
610                            }
611                        }
612                    }
613
614#if( DEBUG_KERNEL_INIT & 0x1 )
615if( hal_time_stamp() > DEBUG_KERNEL_INIT )
616printk("\n[%s] : create chdev %s / channel = %d / rx = %d / cluster %x / chdev = %x\n",
617__FUNCTION__ , chdev_func_str( func ), channel , rx , local_cxy , chdev );
618#endif
619                }  // end if match
620
621                // increment chdev global index (matching or not)
622                ext_chdev_gid++;
623
624            } // end loop on directions
625        }  // end loop on channels
626        } // end loop on devices
627}  // end external_devices_init()
628
629///////////////////////////////////////////////////////////////////////////////////////////
630// This function is called by core[0] in cluster 0 to allocate memory and initialize the PIC
631// device, namely the informations attached to the external IOPIC controller, that
632// must be replicated in all clusters (struct iopic_input).
633// This initialisation must be done before other devices initialisation because the IRQ
634// routing infrastructure is required for both internal and external devices init.
635///////////////////////////////////////////////////////////////////////////////////////////
636// @ info    : pointer on the local boot-info structure.
637///////////////////////////////////////////////////////////////////////////////////////////
638static void __attribute__ ((noinline)) iopic_init( boot_info_t * info )
639{
640    boot_device_t * dev_tbl;         // pointer on boot_info external devices array
641        uint32_t        dev_nr;          // actual number of external devices
642        xptr_t          base;            // remote pointer on segment base
643    uint32_t        func;            // device functionnal index
644    uint32_t        impl;            // device implementation index
645        uint32_t        i;               // device index in dev_tbl
646    uint32_t        x;               // cluster X coordinate
647    uint32_t        y;               // cluster Y coordinate
648    bool_t          found;           // IOPIC found
649        chdev_t       * chdev;           // pointer on PIC chdev descriptor
650
651    // get number of external peripherals and base of array from boot_info
652        dev_nr      = info->ext_dev_nr;
653    dev_tbl     = info->ext_dev;
654
655    // avoid GCC warning
656    base        = XPTR_NULL;
657    impl        = 0;
658
659    // loop on external peripherals to get the IOPIC 
660        for( i = 0 , found = false ; i < dev_nr ; i++ )
661        {
662        func = FUNC_FROM_TYPE( dev_tbl[i].type );
663
664        if( func == DEV_FUNC_PIC )
665        {
666            base     = dev_tbl[i].base;
667            impl     = IMPL_FROM_TYPE( dev_tbl[i].type );
668            found    = true;
669            break;
670        }
671    }
672
673    // check PIC existence
674    if( found == false )
675    {
676        printk("\n[PANIC] in %s : PIC device not found\n",
677        __FUNCTION__ );
678        hal_core_sleep();
679    }
680
681    // allocate and initialize the PIC chdev in cluster 0
682    chdev = chdev_create( DEV_FUNC_PIC,
683                          impl,
684                          0,      // channel
685                          0,      // direction,
686                          base );
687
688    // check memory
689    if( chdev == NULL )
690    {
691        printk("\n[PANIC] in %s : no memory for PIC chdev\n",
692        __FUNCTION__ );
693        hal_core_sleep();
694    }
695
696    // make PIC device type specific initialisation
697    dev_pic_init( chdev );
698
699    // register, in all clusters, the extended pointer
700    // on PIC chdev in "chdev_dir" array
701    xptr_t * entry = &chdev_dir.pic;   
702               
703    for( x = 0 ; x < info->x_size ; x++ )
704    {
705        for( y = 0 ; y < info->y_size ; y++ )
706        {
707            cxy_t cxy = HAL_CXY_FROM_XY( x , y );
708
709            if( cluster_is_active( cxy ) )
710            {
711                hal_remote_s64( XPTR( cxy , entry ) , 
712                                XPTR( local_cxy , chdev ) );
713            }
714        }
715    }
716
717    // initialize, in all clusters, the "iopic_input" structure
718    // defining how external IRQs are connected to IOPIC
719
720    // register default value for unused inputs
721    for( x = 0 ; x < info->x_size ; x++ )
722    {
723        for( y = 0 ; y < info->y_size ; y++ )
724        {
725            cxy_t cxy = HAL_CXY_FROM_XY( x , y );
726
727            if( cluster_is_active( cxy ) )
728            {
729                hal_remote_memset( XPTR( cxy , &iopic_input ), 
730                                   0xFF , sizeof(iopic_input_t) );
731            }
732        }
733    }
734
735    // register input IRQ index for valid inputs
736    uint32_t   id;             // input IRQ index
737    uint8_t    valid;          // input IRQ is connected
738    uint32_t   type;           // source device type
739    uint8_t    channel;        // source device channel
740    uint8_t    is_rx;          // source device direction
741    uint32_t * ptr = NULL;     // local pointer on one field in iopic_input stucture
742
743    for( id = 0 ; id < CONFIG_MAX_EXTERNAL_IRQS ; id++ )
744    {
745        valid   = dev_tbl[i].irq[id].valid;
746        type    = dev_tbl[i].irq[id].dev_type;
747        channel = dev_tbl[i].irq[id].channel;
748        is_rx   = dev_tbl[i].irq[id].is_rx;
749        func    = FUNC_FROM_TYPE( type );
750
751        // get pointer on relevant field in iopic_input
752        if( valid )
753        {
754            if     ( func == DEV_FUNC_IOC )                 ptr = &iopic_input.ioc[channel]; 
755            else if((func == DEV_FUNC_TXT) && (is_rx == 0)) ptr = &iopic_input.txt_tx[channel];
756            else if((func == DEV_FUNC_TXT) && (is_rx != 0)) ptr = &iopic_input.txt_rx[channel];
757            else if((func == DEV_FUNC_NIC) && (is_rx == 0)) ptr = &iopic_input.nic_tx[channel];
758            else if((func == DEV_FUNC_NIC) && (is_rx != 0)) ptr = &iopic_input.nic_rx[channel];
759            else if( func == DEV_FUNC_IOB )                 ptr = &iopic_input.iob;
760            else
761            {
762                printk("\n[PANIC] in %s : illegal source device for IOPIC input\n",
763                __FUNCTION__ );
764                hal_core_sleep();
765            }
766
767            // set one entry in all "iopic_input" structures
768            for( x = 0 ; x < info->x_size ; x++ )
769            {
770                for( y = 0 ; y < info->y_size ; y++ )
771                {
772                    cxy_t cxy = HAL_CXY_FROM_XY( x , y );
773
774                    if( cluster_is_active( cxy ) )
775                    {
776                        hal_remote_s64( XPTR( cxy , ptr ) , id ); 
777                    }
778                }
779            }
780        }
781    } 
782
783#if( DEBUG_KERNEL_INIT & 0x1 )
784if( hal_time_stamp() > DEBUG_KERNEL_INIT )
785{
786    printk("\n[%s] created PIC chdev in cluster %x at cycle %d\n",
787    __FUNCTION__ , local_cxy , (uint32_t)hal_time_stamp() );
788    dev_pic_inputs_display();
789}
790#endif
791   
792}  // end iopic_init()
793
794///////////////////////////////////////////////////////////////////////////////////////////
795// This function is called by all core[0]s in all cluster to complete the PIC device
796// initialisation, namely the informations attached to the LAPIC controller.
797// This initialisation must be done after the IOPIC initialisation, but before other
798// devices initialisation because the IRQ routing infrastructure is required for both
799// internal and external devices initialisation.
800///////////////////////////////////////////////////////////////////////////////////////////
801// @ info    : pointer on the local boot-info structure.
802///////////////////////////////////////////////////////////////////////////////////////////
803static void __attribute__ ((noinline)) lapic_init( boot_info_t * info )
804{
805    boot_device_t * dev_tbl;      // pointer on boot_info internal devices array
806    uint32_t        dev_nr;       // number of internal devices
807    uint32_t        i;            // device index in dev_tbl
808        xptr_t          base;         // remote pointer on segment base
809    uint32_t        func;         // device functionnal type in boot_info
810    bool_t          found;        // LAPIC found
811
812    // get number of internal peripherals and base
813        dev_nr      = info->int_dev_nr;
814    dev_tbl     = info->int_dev;
815
816    // loop on internal peripherals to get the lapic device
817        for( i = 0 , found = false ; i < dev_nr ; i++ )
818        {
819        func = FUNC_FROM_TYPE( dev_tbl[i].type );
820
821        if( func == DEV_FUNC_ICU )
822        {
823            base     = dev_tbl[i].base;
824            found    = true;
825            break;
826        }
827    }
828
829    // if the LAPIC controller is not defined in the boot_info,
830    // we simply don't initialize the PIC extensions in the kernel,
831    // making the assumption that the LAPIC related informations
832    // are hidden in the hardware specific PIC driver.
833    if( found )
834    {
835        // initialise the PIC extensions for
836        // the core descriptor and core manager extensions
837        dev_pic_extend_init( (uint32_t *)GET_PTR( base ) );
838
839        // initialize the "lapic_input" structure
840        // defining how internal IRQs are connected to LAPIC
841        uint32_t        id;
842        uint8_t         valid;
843        uint8_t         channel;
844        uint32_t        func;
845
846        for( id = 0 ; id < CONFIG_MAX_INTERNAL_IRQS ; id++ )
847        {
848            valid    = dev_tbl[i].irq[id].valid;
849            func     = FUNC_FROM_TYPE( dev_tbl[i].irq[id].dev_type );
850            channel  = dev_tbl[i].irq[id].channel;
851
852            if( valid ) // only valid local IRQs are registered
853            {
854                if     ( func == DEV_FUNC_MMC ) lapic_input.mmc = id;
855                else if( func == DEV_FUNC_DMA ) lapic_input.dma[channel] = id;
856                else
857                {
858                    printk("\n[PANIC] in %s : illegal source device for LAPIC input\n",
859                    __FUNCTION__ );
860                    hal_core_sleep();
861                }
862            }
863        }
864    }
865}  // end lapic_init()
866
867///////////////////////////////////////////////////////////////////////////////////////////
868// This static function returns the identifiers of the calling core.
869///////////////////////////////////////////////////////////////////////////////////////////
870// @ info    : pointer on boot_info structure.
871// @ lid     : [out] core local index in cluster.
872// @ cxy     : [out] cluster identifier.
873// @ lid     : [out] core global identifier (hardware).
874// @ return 0 if success / return EINVAL if not found.
875///////////////////////////////////////////////////////////////////////////////////////////
876static error_t __attribute__ ((noinline)) get_core_identifiers( boot_info_t * info,
877                                                                lid_t       * lid,
878                                                                cxy_t       * cxy,
879                                                                gid_t       * gid )
880{
881    uint32_t   i;
882    gid_t      global_id;
883
884    // get global identifier from hardware register
885    global_id = hal_get_gid();
886
887    // makes an associative search in boot_info to get (cxy,lid) from global_id
888    for( i = 0 ; i < info->cores_nr ; i++ )
889    {
890        if( global_id == info->core[i].gid )
891        {
892            *lid = info->core[i].lid;
893            *cxy = info->core[i].cxy;
894            *gid = global_id;
895            return 0;
896        }
897    }
898    return EINVAL;
899}
900
901
902
903
904
905/////////////////////////////////
906// kleenex debug function
907/////////////////////////////////
908void display_fat( uint32_t step )
909{
910    fatfs_ctx_t * fatfs_ctx = fs_context[FS_TYPE_FATFS].extend;
911    if( fatfs_ctx != NULL ) 
912    {
913        printk("\n[%s] step %d at cycle %d\n", __FUNCTION__, step, (uint32_t)hal_get_cycles() );
914        xptr_t     mapper_xp = fatfs_ctx->fat_mapper_xp;
915        mapper_display_page( mapper_xp , 0 , 128 );
916    }
917    else
918    {
919        printk("\n[%s] step %d : fatfs context not initialized\n", __FUNCTION__, step );
920    }
921}
922
923
924
925
926
927///////////////////////////////////////////////////////////////////////////////////////////
928// This function is the entry point for the kernel initialisation.
929// It is executed by all cores in all clusters, but only core[0] initializes
930// the shared resources such as the cluster manager, or the local peripherals.
931// To comply with the multi-kernels paradigm, it accesses only local cluster memory, using
932// only information contained in the local boot_info_t structure, set by the bootloader.
933// Only core[0] in cluster 0 print the log messages.
934///////////////////////////////////////////////////////////////////////////////////////////
935// @ info    : pointer on the local boot-info structure.
936///////////////////////////////////////////////////////////////////////////////////////////
937void kernel_init( boot_info_t * info )
938{
939    lid_t        core_lid = -1;             // running core local index
940    cxy_t        core_cxy = -1;             // running core cluster identifier
941    gid_t        core_gid;                  // running core hardware identifier
942    cluster_t  * cluster;                   // pointer on local cluster manager
943    core_t     * core;                      // pointer on running core descriptor
944    thread_t   * thread;                    // pointer on idle thread descriptor
945
946    xptr_t       vfs_root_inode_xp;         // extended pointer on VFS root inode
947    xptr_t       devfs_dev_inode_xp;        // extended pointer on DEVFS dev inode   
948    xptr_t       devfs_external_inode_xp;   // extended pointer on DEVFS external inode       
949    xptr_t       devfs_internal_inode_xp;   // extended pointer on DEVFS internal inode       
950
951    error_t      error;
952    reg_t        status;                    // running core status register
953
954    /////////////////////////////////////////////////////////////////////////////////
955    // STEP 1 : Each core get its core identifier from boot_info, and makes
956    //          a partial initialisation of its private idle thread descriptor.
957    //          core[0] initializes the "local_cxy" global variable.
958    //          core[0] in cluster[0] initializes the TXT0 chdev for log messages.
959    /////////////////////////////////////////////////////////////////////////////////
960
961    error = get_core_identifiers( info,
962                                  &core_lid,
963                                  &core_cxy,
964                                  &core_gid );
965
966    // core[0] initialize cluster identifier
967    if( core_lid == 0 ) local_cxy = info->cxy;
968
969    // each core gets a pointer on its private idle thread descriptor
970    thread = (thread_t *)( idle_threads + (core_lid * CONFIG_THREAD_DESC_SIZE) );
971
972    // each core registers this thread pointer in hardware register
973    hal_set_current_thread( thread );
974
975    // each core register core descriptor pointer in idle thread descriptor
976    thread->core = &LOCAL_CLUSTER->core_tbl[core_lid];
977
978    // each core initializes the idle thread locks counters
979    thread->busylocks = 0;
980
981#if DEBUG_BUSYLOCK
982    // each core initialise the idle thread list of busylocks
983    xlist_root_init( XPTR( local_cxy , &thread->busylocks_root ) );
984#endif
985
986    // core[0] initializes cluster info
987    if( core_lid == 0 ) cluster_info_init( info );
988
989    // core[0] in cluster[0] initialises TXT0 chdev descriptor
990    if( (core_lid == 0) && (core_cxy == 0) ) txt0_device_init( info );
991
992    // all cores check identifiers
993    if( error )
994    {
995        printk("\n[PANIC] in %s : illegal core : gid %x / cxy %x / lid %d",
996        __FUNCTION__, core_lid, core_cxy, core_lid );
997        hal_core_sleep();
998    }
999
1000    /////////////////////////////////////////////////////////////////////////////////
1001    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1002                                        (info->x_size * info->y_size) );
1003    barrier_wait( &local_barrier , info->cores_nr );
1004    /////////////////////////////////////////////////////////////////////////////////
1005
1006#if DEBUG_KERNEL_INIT
1007if( (core_lid ==  0) & (local_cxy == 0) ) 
1008printk("\n[%s] exit barrier 1 : TXT0 initialized / cycle %d\n",
1009__FUNCTION__, (uint32_t)hal_get_cycles() );
1010#endif
1011
1012    /////////////////////////////////////////////////////////////////////////////////
1013    // STEP 2 : core[0] initializes the cluter manager,
1014    //          including the physical memory allocator.
1015    /////////////////////////////////////////////////////////////////////////////////
1016
1017    // core[0] initialises DQDT (only core[0] in cluster 0 build the quad-tree)
1018    if( core_lid == 0 ) dqdt_init();
1019   
1020    // core[0] initialize other cluster manager complex structures
1021    if( core_lid == 0 )
1022    {
1023        error = cluster_manager_init( info );
1024
1025        if( error )
1026        {
1027             printk("\n[PANIC] in %s : cannot initialize cluster manager in cluster %x\n",
1028             __FUNCTION__, local_cxy );
1029             hal_core_sleep();
1030        }
1031    }
1032
1033    /////////////////////////////////////////////////////////////////////////////////
1034    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1035                                        (info->x_size * info->y_size) );
1036    barrier_wait( &local_barrier , info->cores_nr );
1037    /////////////////////////////////////////////////////////////////////////////////
1038
1039#if DEBUG_KERNEL_INIT
1040if( (core_lid ==  0) & (local_cxy == 0) ) 
1041printk("\n[%s] exit barrier 2 : cluster manager initialized / cycle %d\n",
1042__FUNCTION__, (uint32_t)hal_get_cycles() );
1043#endif
1044
1045    /////////////////////////////////////////////////////////////////////////////////
1046    // STEP 3 : all cores initialize the idle thread descriptor.
1047    //          core[0] initializes the process_zero descriptor,
1048    //          including the kernel VMM (both GPT and VSL)
1049    /////////////////////////////////////////////////////////////////////////////////
1050
1051    // all cores get pointer on local cluster manager & core descriptor
1052    cluster = &cluster_manager;
1053    core    = &cluster->core_tbl[core_lid];
1054
1055    // all cores update the register(s) defining the kernel
1056    // entry points for interrupts, exceptions and syscalls,
1057    // this must be done before VFS initialisation, because
1058    // kernel_init() uses RPCs requiring IPIs...
1059    hal_set_kentry();
1060
1061    // all cores initialize the idle thread descriptor
1062    thread_idle_init( thread,
1063                      THREAD_IDLE,
1064                      &thread_idle_func,
1065                      NULL,
1066                      core_lid );
1067
1068    // core[0] initializes the process_zero descriptor,
1069    if( core_lid == 0 ) process_zero_create( &process_zero , info );
1070
1071    /////////////////////////////////////////////////////////////////////////////////
1072    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1073                                        (info->x_size * info->y_size) );
1074    barrier_wait( &local_barrier , info->cores_nr );
1075    /////////////////////////////////////////////////////////////////////////////////
1076
1077#if DEBUG_KERNEL_INIT
1078if( (core_lid ==  0) & (local_cxy == 0) ) 
1079printk("\n[%s] exit barrier 3 : kernel processs initialized / cycle %d\n",
1080__FUNCTION__, (uint32_t)hal_get_cycles() );
1081#endif
1082
1083    /////////////////////////////////////////////////////////////////////////////////
1084    // STEP 4 : all cores initialize their private MMU
1085    //          core[0] in cluster 0 initializes the IOPIC device.
1086    /////////////////////////////////////////////////////////////////////////////////
1087
1088    // all cores initialise their MMU
1089    hal_mmu_init( &process_zero.vmm.gpt );
1090
1091    // core[0] in cluster[0] initializes the PIC chdev,
1092    if( (core_lid == 0) && (local_cxy == 0) ) iopic_init( info );
1093   
1094    ////////////////////////////////////////////////////////////////////////////////
1095    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1096                                        (info->x_size * info->y_size) );
1097    barrier_wait( &local_barrier , info->cores_nr );
1098    ////////////////////////////////////////////////////////////////////////////////
1099
1100#if DEBUG_KERNEL_INIT
1101if( (core_lid ==  0) & (local_cxy == 0) ) 
1102printk("\n[%s] exit barrier 4 : MMU and IOPIC initialized / cycle %d\n",
1103__FUNCTION__, (uint32_t)hal_get_cycles() );
1104#endif
1105
1106    ////////////////////////////////////////////////////////////////////////////////
1107    // STEP 5 : core[0] initializes the distibuted LAPIC descriptor.
1108    //          core[0] initializes the internal chdev descriptors
1109    //          core[0] initialize the local external chdev descriptors
1110    ////////////////////////////////////////////////////////////////////////////////
1111
1112    // all core[0]s initialize their local LAPIC extension,
1113    if( core_lid == 0 ) lapic_init( info );
1114
1115    // core[0] scan the internal (private) peripherals,
1116    // and allocates memory for the corresponding chdev descriptors.
1117    if( core_lid == 0 ) internal_devices_init( info );
1118       
1119
1120    // All core[0]s contribute to initialise external peripheral chdev descriptors.
1121    // Each core[0][cxy] scan the set of external (shared) peripherals (but the TXT0),
1122    // and allocates memory for the chdev descriptors that must be placed
1123    // on the (cxy) cluster according to the global index value.
1124
1125    if( core_lid == 0 ) external_devices_init( info );
1126
1127    /////////////////////////////////////////////////////////////////////////////////
1128    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1129                                        (info->x_size * info->y_size) );
1130    barrier_wait( &local_barrier , info->cores_nr );
1131    /////////////////////////////////////////////////////////////////////////////////
1132
1133#if DEBUG_KERNEL_INIT
1134if( (core_lid ==  0) & (local_cxy == 0) ) 
1135printk("\n[%s] exit barrier 5 : chdevs initialised / cycle %d\n",
1136__FUNCTION__, (uint32_t)hal_get_cycles() );
1137#endif
1138
1139#if( DEBUG_KERNEL_INIT & 1 )
1140if( (core_lid ==  0) & (local_cxy == 0) ) 
1141chdev_dir_display();
1142#endif
1143   
1144    /////////////////////////////////////////////////////////////////////////////////
1145    // STEP 6 : all cores enable IPI (Inter Procesor Interrupt),
1146    //          all cores unblock the idle thread, and register it in scheduler.
1147    //          core[0] in cluster[0] creates the VFS root inode.
1148    //          It access the boot device to initialize the file system context.
1149    /////////////////////////////////////////////////////////////////////////////////
1150
1151    // All cores enable IPI
1152    dev_pic_enable_ipi();
1153    hal_enable_irq( &status );
1154
1155    // all cores unblock the idle thread, and register it in scheduler
1156    thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
1157    core->scheduler.idle = thread;
1158
1159#if( DEBUG_KERNEL_INIT & 1 )
1160sched_display( core_lid );
1161#endif
1162
1163    // core[O] in cluster[0] creates the VFS root
1164    if( (core_lid ==  0) && (local_cxy == 0 ) ) 
1165    {
1166        vfs_root_inode_xp = XPTR_NULL;
1167
1168        // Only FATFS is supported yet,
1169        // other File System can be introduced here
1170        if( CONFIG_VFS_ROOT_IS_FATFS )
1171        {
1172            // 1. allocate memory for FATFS context in cluster 0
1173            fatfs_ctx_t * fatfs_ctx = fatfs_ctx_alloc();
1174
1175            if( fatfs_ctx == NULL )
1176            {
1177                printk("\n[PANIC] in %s : cannot create FATFS context in cluster 0\n",
1178                __FUNCTION__ );
1179                hal_core_sleep();
1180            }
1181
1182            // 2. access boot device to initialize FATFS context
1183            fatfs_ctx_init( fatfs_ctx );
1184
1185            // 3. get various informations from FATFS context
1186            uint32_t root_dir_cluster = fatfs_ctx->root_dir_cluster;
1187            uint32_t cluster_size     = fatfs_ctx->bytes_per_sector * 
1188                                        fatfs_ctx->sectors_per_cluster;
1189            uint32_t total_clusters   = fatfs_ctx->fat_sectors_count << 7;
1190 
1191            // 4. create VFS root inode in cluster 0
1192            error = vfs_inode_create( FS_TYPE_FATFS,                       // fs_type
1193                                      0,                                   // attr
1194                                      0,                                   // rights
1195                                      0,                                   // uid
1196                                      0,                                   // gid
1197                                      &vfs_root_inode_xp );                // return
1198            if( error )
1199            {
1200                printk("\n[PANIC] in %s : cannot create VFS root inode in cluster 0\n",
1201                __FUNCTION__ );
1202                hal_core_sleep();
1203            }
1204
1205            // 5. update FATFS root inode "type" and "extend" fields 
1206            cxy_t         vfs_root_cxy = GET_CXY( vfs_root_inode_xp );
1207            vfs_inode_t * vfs_root_ptr = GET_PTR( vfs_root_inode_xp );
1208            hal_remote_s32( XPTR( vfs_root_cxy , &vfs_root_ptr->type ), INODE_TYPE_DIR );
1209            hal_remote_spt( XPTR( vfs_root_cxy , &vfs_root_ptr->extend ), 
1210                            (void*)(intptr_t)root_dir_cluster );
1211
1212            // 6. initialize the generic VFS context for FATFS
1213            vfs_ctx_init( FS_TYPE_FATFS,                               // fs type
1214                          0,                                           // attributes: unused
1215                              total_clusters,                              // number of clusters
1216                              cluster_size,                                // bytes
1217                              vfs_root_inode_xp,                           // VFS root
1218                          fatfs_ctx );                                 // extend
1219        }
1220        else
1221        {
1222            printk("\n[PANIC] in %s : unsupported VFS type in cluster 0\n",
1223            __FUNCTION__ );
1224            hal_core_sleep();
1225        }
1226
1227        // create the <.> and <..> dentries in VFS root directory
1228        // the VFS root parent inode is the VFS root inode itself
1229        vfs_add_special_dentries( vfs_root_inode_xp,
1230                                  vfs_root_inode_xp );
1231
1232        // register VFS root inode in process_zero descriptor of cluster 0
1233        process_zero.vfs_root_xp = vfs_root_inode_xp;
1234        process_zero.cwd_xp      = vfs_root_inode_xp;
1235    }
1236
1237    /////////////////////////////////////////////////////////////////////////////////
1238    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1239                                        (info->x_size * info->y_size) );
1240    barrier_wait( &local_barrier , info->cores_nr );
1241    /////////////////////////////////////////////////////////////////////////////////
1242
1243#if DEBUG_KERNEL_INIT
1244if( (core_lid ==  0) & (local_cxy == 0) ) 
1245printk("\n[%s] exit barrier 6 : VFS root (%x,%x) in cluster 0 / cycle %d\n",
1246__FUNCTION__, GET_CXY(process_zero.vfs_root_xp),
1247GET_PTR(process_zero.vfs_root_xp), (uint32_t)hal_get_cycles() );
1248#endif
1249
1250    /////////////////////////////////////////////////////////////////////////////////
1251    // STEP 7 : In all other clusters than cluster[0], the core[0] allocates memory
1252    //          for the selected FS context, and initialise the local FS context and
1253    //          the local VFS context from values stored in cluster 0.
1254    //          They get the VFS root inode extended pointer from cluster 0.
1255    /////////////////////////////////////////////////////////////////////////////////
1256
1257    if( (core_lid ==  0) && (local_cxy != 0) ) 
1258    {
1259        // File System must be FATFS in this implementation,
1260        // but other File System can be introduced here
1261        if( CONFIG_VFS_ROOT_IS_FATFS )
1262        {
1263            // 1. allocate memory for local FATFS context
1264            fatfs_ctx_t * local_fatfs_ctx = fatfs_ctx_alloc();
1265
1266            // check memory
1267            if( local_fatfs_ctx == NULL )
1268            {
1269                printk("\n[PANIC] in %s : cannot create FATFS context in cluster %x\n",
1270                __FUNCTION__ , local_cxy );
1271                hal_core_sleep();
1272            }
1273
1274            // 2. get local pointer on VFS context for FATFS
1275            vfs_ctx_t   * vfs_ctx = &fs_context[FS_TYPE_FATFS];
1276
1277            // 3. get local pointer on FATFS context in cluster 0
1278            fatfs_ctx_t * remote_fatfs_ctx = hal_remote_lpt( XPTR( 0 , &vfs_ctx->extend ) );
1279
1280            // 4. copy FATFS context from cluster 0 to local cluster
1281            hal_remote_memcpy( XPTR( local_cxy , local_fatfs_ctx ), 
1282                               XPTR( 0 ,         remote_fatfs_ctx ), sizeof(fatfs_ctx_t) );
1283
1284            // 5. copy VFS context from cluster 0 to local cluster
1285            hal_remote_memcpy( XPTR( local_cxy , vfs_ctx ), 
1286                               XPTR( 0 ,         vfs_ctx ), sizeof(vfs_ctx_t) );
1287
1288            // 6. update extend field in local copy of VFS context
1289            vfs_ctx->extend = local_fatfs_ctx;
1290
1291            if( ((fatfs_ctx_t *)vfs_ctx->extend)->sectors_per_cluster != 8 )
1292            {
1293                printk("\n[PANIC] in %s : illegal FATFS context in cluster %x\n",
1294                __FUNCTION__ , local_cxy );
1295                hal_core_sleep();
1296            }
1297        }
1298
1299        // get extended pointer on VFS root inode from cluster 0
1300        vfs_root_inode_xp = hal_remote_l64( XPTR( 0 , &process_zero.vfs_root_xp ) );
1301
1302        // update local process_zero descriptor
1303        process_zero.vfs_root_xp = vfs_root_inode_xp;
1304        process_zero.cwd_xp      = vfs_root_inode_xp;
1305    }
1306
1307    /////////////////////////////////////////////////////////////////////////////////
1308    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1309                                        (info->x_size * info->y_size) );
1310    barrier_wait( &local_barrier , info->cores_nr );
1311    /////////////////////////////////////////////////////////////////////////////////
1312
1313#if DEBUG_KERNEL_INIT
1314if( (core_lid ==  0) & (local_cxy == 1) ) 
1315printk("\n[%s] exit barrier 7 : VFS root (%x,%x) in cluster 1 / cycle %d\n",
1316__FUNCTION__, GET_CXY(process_zero.vfs_root_xp),
1317GET_PTR(process_zero.vfs_root_xp), (uint32_t)hal_get_cycles() );
1318#endif
1319
1320    /////////////////////////////////////////////////////////////////////////////////
1321    // STEP 8 : core[0] in cluster 0 makes the global DEVFS initialisation:
1322    //          It initializes the DEVFS context, and creates the DEVFS
1323    //          "dev" and "external" inodes in cluster 0.
1324    /////////////////////////////////////////////////////////////////////////////////
1325
1326    if( (core_lid ==  0) && (local_cxy == 0) ) 
1327    {
1328        // 1. allocate memory for DEVFS context extension in cluster 0
1329        devfs_ctx_t * devfs_ctx = devfs_ctx_alloc();
1330
1331        if( devfs_ctx == NULL )
1332        {
1333            printk("\n[PANIC] in %s : cannot create DEVFS context in cluster 0\n",
1334            __FUNCTION__ , local_cxy );
1335            hal_core_sleep();
1336        }
1337
1338        // 2. initialize the DEVFS entry in the vfs_context[] array
1339        vfs_ctx_init( FS_TYPE_DEVFS,                                // fs type
1340                      0,                                            // attributes: unused
1341                          0,                                            // total_clusters: unused
1342                          0,                                            // cluster_size: unused
1343                          vfs_root_inode_xp,                            // VFS root
1344                      devfs_ctx );                                  // extend
1345
1346        // 3. create "dev" and "external" inodes (directories)
1347        devfs_global_init( process_zero.vfs_root_xp,
1348                           &devfs_dev_inode_xp,
1349                           &devfs_external_inode_xp );
1350
1351        // 4. initializes DEVFS context extension
1352        devfs_ctx_init( devfs_ctx,
1353                        devfs_dev_inode_xp,
1354                        devfs_external_inode_xp );
1355    }   
1356
1357    /////////////////////////////////////////////////////////////////////////////////
1358    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1359                                        (info->x_size * info->y_size) );
1360    barrier_wait( &local_barrier , info->cores_nr );
1361    /////////////////////////////////////////////////////////////////////////////////
1362
1363#if DEBUG_KERNEL_INIT
1364if( (core_lid ==  0) & (local_cxy == 0) ) 
1365printk("\n[%s] exit barrier 8 : DEVFS root initialized in cluster 0 / cycle %d\n",
1366__FUNCTION__, (uint32_t)hal_get_cycles() );
1367#endif
1368
1369    /////////////////////////////////////////////////////////////////////////////////
1370    // STEP 9 : In all clusters in parallel, core[0] completes DEVFS initialization.
1371    //          Each core[0] get the "dev" and "external" extended pointers from
1372    //          values stored in cluster(0), creates the DEVFS "internal" directory,
1373    //          and creates the pseudo-files for all chdevs in local cluster.
1374    /////////////////////////////////////////////////////////////////////////////////
1375
1376    if( core_lid == 0 )
1377    {
1378        // get extended pointer on "extend" field of VFS context for DEVFS in cluster 0
1379        xptr_t  extend_xp = XPTR( 0 , &fs_context[FS_TYPE_DEVFS].extend );
1380
1381        // get pointer on DEVFS context in cluster 0
1382        devfs_ctx_t * devfs_ctx = hal_remote_lpt( extend_xp );
1383       
1384        devfs_dev_inode_xp      = hal_remote_l64( XPTR( 0 , &devfs_ctx->dev_inode_xp ) );
1385        devfs_external_inode_xp = hal_remote_l64( XPTR( 0 , &devfs_ctx->external_inode_xp ) );
1386
1387        // populate DEVFS in all clusters
1388        devfs_local_init( devfs_dev_inode_xp,
1389                          devfs_external_inode_xp,
1390                          &devfs_internal_inode_xp );
1391    }
1392
1393    /////////////////////////////////////////////////////////////////////////////////
1394    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1395                                        (info->x_size * info->y_size) );
1396    barrier_wait( &local_barrier , info->cores_nr );
1397    /////////////////////////////////////////////////////////////////////////////////
1398
1399#if DEBUG_KERNEL_INIT
1400if( (core_lid ==  0) & (local_cxy == 0) ) 
1401printk("\n[%s] exit barrier 9 : DEVFS initialized in cluster 0 / cycle %d\n",
1402__FUNCTION__, (uint32_t)hal_get_cycles() );
1403#endif
1404
1405#if( DEBUG_KERNEL_INIT & 1 )
1406if( (core_lid ==  0) & (local_cxy == 0) ) 
1407vfs_display( vfs_root_inode_xp );
1408#endif
1409
1410    /////////////////////////////////////////////////////////////////////////////////
1411    // STEP 10 : core[0] in cluster 0 creates the first user process (process_init).
1412    //           This include the first user process VMM (GPT and VSL) creation.
1413    //           Finally, it prints the ALMOS-MKH banner.
1414    /////////////////////////////////////////////////////////////////////////////////
1415
1416    if( (core_lid == 0) && (local_cxy == 0) ) 
1417    {
1418       process_init_create();
1419    }
1420
1421#if DEBUG_KERNEL_INIT
1422if( (core_lid ==  0) & (local_cxy == 0) ) 
1423printk("\n[%s] exit barrier 10 : process_init created in cluster 0 / cycle %d\n",
1424__FUNCTION__, (uint32_t)hal_get_cycles() );
1425#endif
1426
1427#if (DEBUG_KERNEL_INIT & 1)
1428if( (core_lid ==  0) & (local_cxy == 0) ) 
1429sched_display( 0 );
1430#endif
1431
1432    if( (core_lid == 0) && (local_cxy == 0) ) 
1433    {
1434        print_banner( (info->x_size * info->y_size) , info->cores_nr );
1435    }
1436
1437#if( DEBUG_KERNEL_INIT & 1 )
1438if( (core_lid ==  0) & (local_cxy == 0) ) 
1439printk("\n\n***** memory fooprint for main kernel objects\n\n"
1440                   " - thread descriptor  : %d bytes\n"
1441                   " - process descriptor : %d bytes\n"
1442                   " - cluster manager    : %d bytes\n"
1443                   " - chdev descriptor   : %d bytes\n"
1444                   " - core descriptor    : %d bytes\n"
1445                   " - scheduler          : %d bytes\n"
1446                   " - rpc fifo           : %d bytes\n"
1447                   " - page descriptor    : %d bytes\n"
1448                   " - mapper root        : %d bytes\n"
1449                   " - ppm manager        : %d bytes\n"
1450                   " - kcm manager        : %d bytes\n"
1451                   " - khm manager        : %d bytes\n"
1452                   " - vmm manager        : %d bytes\n"
1453                   " - gpt root           : %d bytes\n"
1454                   " - list item          : %d bytes\n"
1455                   " - xlist item         : %d bytes\n"
1456                   " - busylock           : %d bytes\n"
1457                   " - remote busylock    : %d bytes\n"
1458                   " - queuelock          : %d bytes\n"
1459                   " - remote queuelock   : %d bytes\n"
1460                   " - rwlock             : %d bytes\n"
1461                   " - remote rwlock      : %d bytes\n",
1462                   sizeof( thread_t           ),
1463                   sizeof( process_t          ),
1464                   sizeof( cluster_t          ),
1465                   sizeof( chdev_t            ),
1466                   sizeof( core_t             ),
1467                   sizeof( scheduler_t        ),
1468                   sizeof( remote_fifo_t      ),
1469                   sizeof( page_t             ),
1470                   sizeof( mapper_t           ),
1471                   sizeof( ppm_t              ),
1472                   sizeof( kcm_t              ),
1473                   sizeof( khm_t              ),
1474                   sizeof( vmm_t              ),
1475                   sizeof( gpt_t              ),
1476                   sizeof( list_entry_t       ),
1477                   sizeof( xlist_entry_t      ),
1478                   sizeof( busylock_t         ),
1479                   sizeof( remote_busylock_t  ),
1480                   sizeof( queuelock_t        ),
1481                   sizeof( remote_queuelock_t ),
1482                   sizeof( rwlock_t           ),
1483                   sizeof( remote_rwlock_t    ));
1484#endif
1485
1486    // each core activates its private TICK IRQ
1487    dev_pic_enable_timer( CONFIG_SCHED_TICK_MS_PERIOD );
1488
1489    /////////////////////////////////////////////////////////////////////////////////
1490    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ),
1491                                        (info->x_size * info->y_size) );
1492    barrier_wait( &local_barrier , info->cores_nr );
1493    /////////////////////////////////////////////////////////////////////////////////
1494
1495#if( DEBUG_KERNEL_INIT & 1 )
1496thread_t * this = CURRENT_THREAD;
1497printk("\n[%s] : thread[%x,%x] on core[%x,%d] jumps to thread_idle_func() / cycle %d\n",
1498__FUNCTION__ , this->process->pid, this->trdid,
1499local_cxy, core_lid, (uint32_t)hal_get_cycles() );
1500#endif
1501
1502    // each core jump to thread_idle_func
1503    thread_idle_func();
1504
1505}  // end kernel_init()
1506
Note: See TracBrowser for help on using the repository browser.