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
RevLine 
[1]1/*
2 * kernel_init.c - kernel parallel initialization
[127]3 *
[23]4 * Authors :  Mohamed Lamine Karaoui (2015)
[623]5 *            Alain Greiner  (2016,2017,2018,2019)
[1]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
[14]25#include <kernel_config.h>
[1]26#include <errno.h>
[457]27#include <hal_kernel_types.h>
[1]28#include <hal_special.h>
29#include <hal_context.h>
[279]30#include <hal_irqmask.h>
[564]31#include <hal_macros.h>
[296]32#include <hal_ppm.h>
[14]33#include <barrier.h>
[564]34#include <xbarrier.h>
[407]35#include <remote_fifo.h>
[1]36#include <core.h>
37#include <list.h>
[68]38#include <xlist.h>
[204]39#include <xhtab.h>
[1]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>
[5]48#include <chdev.h>
[1]49#include <boot_info.h>
50#include <dqdt.h>
51#include <dev_mmc.h>
[5]52#include <dev_dma.h>
53#include <dev_iob.h>
[1]54#include <dev_ioc.h>
[5]55#include <dev_txt.h>
[1]56#include <dev_pic.h>
57#include <printk.h>
58#include <vfs.h>
[23]59#include <devfs.h>
[68]60#include <mapper.h>
[1]61
62///////////////////////////////////////////////////////////////////////////////////////////
[279]63// All the following global variables are replicated in all clusters.
[1]64// They are initialised by the kernel_init() function.
[14]65//
[127]66// WARNING : The section names have been defined to control the base addresses of the
[14]67// boot_info structure and the idle thread descriptors, through the kernel.ld script:
[127]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.
[14]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.
[1]72///////////////////////////////////////////////////////////////////////////////////////////
73
[5]74// This variable defines the local boot_info structure
75__attribute__((section(".kinfo")))
[14]76boot_info_t          boot_info;
[5]77
[14]78// This variable defines the "idle" threads descriptors array
79__attribute__((section(".kidle")))
[381]80char                 idle_threads[CONFIG_THREAD_DESC_SIZE *
[14]81                                   CONFIG_MAX_LOCAL_CORES]   CONFIG_PPM_PAGE_ALIGNED;
82
[127]83// This variable defines the local cluster manager
[5]84__attribute__((section(".kdata")))
[19]85cluster_t            cluster_manager                         CONFIG_CACHE_LINE_ALIGNED;
[1]86
[564]87// This variable defines the TXT_TX[0] chdev
[188]88__attribute__((section(".kdata")))
[564]89chdev_t              txt0_tx_chdev                           CONFIG_CACHE_LINE_ALIGNED;
[188]90
[564]91// This variable defines the TXT_RX[0] chdev
[539]92__attribute__((section(".kdata")))
[564]93chdev_t              txt0_rx_chdev                           CONFIG_CACHE_LINE_ALIGNED;
[539]94
[14]95// This variables define the kernel process0 descriptor
[5]96__attribute__((section(".kdata")))
[19]97process_t            process_zero                            CONFIG_CACHE_LINE_ALIGNED;
[1]98
[624]99// This variable defines a set of extended pointers on the distributed chdevs
[5]100__attribute__((section(".kdata")))
[14]101chdev_directory_t    chdev_dir                               CONFIG_CACHE_LINE_ALIGNED;
[1]102
[188]103// This variable contains the input IRQ indexes for the IOPIC controller
[5]104__attribute__((section(".kdata")))
[246]105iopic_input_t        iopic_input                             CONFIG_CACHE_LINE_ALIGNED;
[1]106
[188]107// This variable contains the input IRQ indexes for the LAPIC controller
[5]108__attribute__((section(".kdata")))
[188]109lapic_input_t        lapic_input                             CONFIG_CACHE_LINE_ALIGNED;
[1]110
[14]111// This variable defines the local cluster identifier
[5]112__attribute__((section(".kdata")))
[14]113cxy_t                local_cxy                               CONFIG_CACHE_LINE_ALIGNED;
[5]114
[623]115// This variable is used for core[0] cores synchronisation in kernel_init()
[5]116__attribute__((section(".kdata")))
[564]117xbarrier_t           global_barrier                          CONFIG_CACHE_LINE_ALIGNED;
[1]118
[127]119// This variable is used for local cores synchronisation in kernel_init()
[14]120__attribute__((section(".kdata")))
121barrier_t            local_barrier                           CONFIG_CACHE_LINE_ALIGNED;
122
[127]123// This variable defines the array of supported File System contexts
[50]124__attribute__((section(".kdata")))
125vfs_ctx_t            fs_context[FS_TYPES_NR]                 CONFIG_CACHE_LINE_ALIGNED;
126
[564]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.
[624]129__attribute__((section(".kdata")))
[564]130char * lock_type_str[] =
131{
132    "unused_0",              //  0
[408]133
[564]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
[610]145    "XHTAB_STATE",           // 11
[564]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
[619]153    "PROCESS_CWD",           // 19
154    "BARRIER_STATE",         // 20
[564]155
156    "CLUSTER_PREFTBL",       // 21
[601]157
[564]158    "PPM_DIRTY",             // 22
159    "CLUSTER_LOCALS",        // 23
160    "CLUSTER_COPIES",        // 24
161    "PROCESS_CHILDREN",      // 25
162    "PROCESS_USERSYNC",      // 26
163    "PROCESS_FDARRAY",       // 27
[628]164    "PROCESS_DIR",           // 28
165    "unused_29",             // 29
[564]166
[611]167    "PROCESS_THTBL",         // 30
[564]168
[611]169    "MAPPER_STATE",          // 31
170    "VFS_SIZE",              // 32
171    "VFS_FILE",              // 33
172    "VMM_VSL",               // 34
173    "VMM_GPT",               // 35
174    "VFS_MAIN",              // 36
[628]175    "FATFS_FAT",             // 37
[564]176};       
177
[601]178// debug variables to analyse the sys_read() and sys_write() syscalls timing
[564]179
[438]180#if DEBUG_SYS_READ
[407]181uint32_t   enter_sys_read;
182uint32_t   exit_sys_read;
183
[435]184uint32_t   enter_devfs_read;
185uint32_t   exit_devfs_read;
[407]186
187uint32_t   enter_txt_read;
188uint32_t   exit_txt_read;
189
[435]190uint32_t   enter_chdev_cmd_read;
191uint32_t   exit_chdev_cmd_read;
[407]192
[435]193uint32_t   enter_chdev_server_read;
194uint32_t   exit_chdev_server_read;
[407]195
[435]196uint32_t   enter_tty_cmd_read;
197uint32_t   exit_tty_cmd_read;
[407]198
[435]199uint32_t   enter_tty_isr_read;
200uint32_t   exit_tty_isr_read;
[407]201#endif
202
[435]203// these debug variables are used to analyse the sys_write() syscall timing
204
[438]205#if DEBUG_SYS_WRITE   
[435]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
[564]228// intrumentation variables : cumulated costs per syscall type in cluster
[624]229
230#if CONFIG_INSTRUMENTATION_SYSCALLS
231__attribute__((section(".kdata")))
[564]232uint32_t   syscalls_cumul_cost[SYSCALLS_NR];
233
[624]234__attribute__((section(".kdata")))
[564]235uint32_t   syscalls_occurences[SYSCALLS_NR];
[624]236#endif
[564]237
[1]238///////////////////////////////////////////////////////////////////////////////////////////
[5]239// This function displays the ALMOS_MKH banner.
[1]240///////////////////////////////////////////////////////////////////////////////////////////
[5]241static void print_banner( uint32_t nclusters , uint32_t ncores )
[127]242{
[5]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"
[457]253           "\n\n\t\t %s / %d cluster(s) / %d core(s) per cluster\n\n",
254           CONFIG_ALMOS_VERSION , nclusters , ncores );
[5]255}
[1]256
257
[5]258///////////////////////////////////////////////////////////////////////////////////////////
[564]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.
[127]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.
[564]266// Note: The TXT_RX[0] chdev is created, but is not used by ALMOS-MKH (september 2018).
[5]267///////////////////////////////////////////////////////////////////////////////////////////
268// @ info    : pointer on the local boot-info structure.
269///////////////////////////////////////////////////////////////////////////////////////////
[564]270static void __attribute__ ((noinline)) txt0_device_init( boot_info_t * info )
[5]271{
272    boot_device_t * dev_tbl;         // pointer on array of devices in boot_info
[127]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
[5]276    uint32_t        impl;            // device implementation index
[127]277    uint32_t        i;               // device index in dev_tbl
278    uint32_t        x;               // X cluster coordinate
279    uint32_t        y;               // Y cluster coordinate
[188]280    uint32_t        channels;        // number of channels
[1]281
[5]282    // get number of peripherals and base of devices array from boot_info
[127]283    dev_nr      = info->ext_dev_nr;
[5]284    dev_tbl     = info->ext_dev;
[1]285
[14]286    // loop on external peripherals to find TXT device
[127]287    for( i = 0 ; i < dev_nr ; i++ )
288    {
[5]289        base        = dev_tbl[i].base;
[188]290        func        = FUNC_FROM_TYPE( dev_tbl[i].type );
291        impl        = IMPL_FROM_TYPE( dev_tbl[i].type );
292        channels    = dev_tbl[i].channels;
[5]293
[127]294        if (func == DEV_FUNC_TXT )
[5]295        {
[564]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 );
[188]304           
[564]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 );
[14]317
[564]318            // register TXT_TX[0] & TXT_RX[0] in chdev_dir[x][y]
319            // for all valid clusters             
[5]320            for( x = 0 ; x < info->x_size ; x++ )
321            {
[564]322                for( y = 0 ; y < info->y_size ; y++ )
[5]323                {
[564]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 ) );
[559]332                    }
[5]333                }
334            }
[564]335
336            hal_fence();
[5]337        }
[188]338        } // end loop on devices
339}  // end txt0_device_init()
[5]340
[1]341///////////////////////////////////////////////////////////////////////////////////////////
[188]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.
[1]346///////////////////////////////////////////////////////////////////////////////////////////
347// @ info    : pointer on the local boot-info structure.
348///////////////////////////////////////////////////////////////////////////////////////////
[564]349static void __attribute__ ((noinline)) internal_devices_init( boot_info_t * info )
[1]350{
[188]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
[1]362
[188]363    // get number of internal peripherals and base from boot_info
364        dev_nr  = info->int_dev_nr;
365    dev_tbl = info->int_dev;
[1]366
[188]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 );
[204]374 
[188]375        //////////////////////////
376        if( func == DEV_FUNC_MMC ) 
[5]377        {
[1]378
[564]379            // check channels
380            if( channels != 1 )
[580]381            {
382                printk("\n[PANIC] in %s : MMC device must be single channel\n",
383                __FUNCTION__ );
384                hal_core_sleep();
385            }
[564]386
[188]387            // create chdev in local cluster
388            chdev_ptr = chdev_create( func,
389                                      impl,
390                                      0,          // channel
391                                      false,      // direction
392                                      base );
[14]393
[564]394            // check memory
395            if( chdev_ptr == NULL )
[580]396            {
397                printk("\n[PANIC] in %s : cannot create MMC chdev\n",
398                __FUNCTION__ );
399                hal_core_sleep();
400            }
[188]401           
402            // make MMC specific initialisation
403            dev_mmc_init( chdev_ptr );
[1]404
[188]405            // set the MMC field in all chdev_dir[x][y] structures
406            for( x = 0 ; x < info->x_size ; x++ )
[1]407            {
[564]408                for( y = 0 ; y < info->y_size ; y++ )
[188]409                {
[564]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] ), 
[559]415                                        XPTR( local_cxy , chdev_ptr ) );
416                    }
[188]417                }
[1]418            }
[188]419
[438]420#if( DEBUG_KERNEL_INIT & 0x1 )
421if( hal_time_stamp() > DEBUG_KERNEL_INIT )
[601]422printk("\n[%s] : created MMC in cluster %x / chdev = %x\n",
[407]423__FUNCTION__ , local_cxy , chdev_ptr );
[389]424#endif
[14]425        }
[188]426        ///////////////////////////////
427        else if( func == DEV_FUNC_DMA )
[127]428        {
[188]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 );
[5]438
[564]439                // check memory
440                if( chdev_ptr == NULL )
[580]441                {
442                    printk("\n[PANIC] in %s : cannot create DMA chdev\n",
443                    __FUNCTION__ );
444                    hal_core_sleep();
445                }
[564]446           
[188]447                // make DMA specific initialisation
448                dev_dma_init( chdev_ptr );     
[127]449
[188]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 );
[5]453
[438]454#if( DEBUG_KERNEL_INIT & 0x1 )
455if( hal_time_stamp() > DEBUG_KERNEL_INIT )
[601]456printk("\n[%s] : created DMA[%d] in cluster %x / chdev = %x\n",
[389]457__FUNCTION__ , channel , local_cxy , chdev_ptr );
458#endif
[188]459            }
[14]460        }
[127]461    }
[5]462}  // end internal_devices_init()
463
464///////////////////////////////////////////////////////////////////////////////////////////
[188]465// This function allocates memory and initializes the chdev descriptors for the 
[408]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.
[188]468// These chdev descriptors are distributed on all clusters, using a modulo on a global
[408]469// index, identically computed in all clusters.
[623]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
[408]472// the local cluster, because the global index matches the local index.
[188]473// The relevant entries in all copies of the devices directory are initialised.
[5]474///////////////////////////////////////////////////////////////////////////////////////////
475// @ info    : pointer on the local boot-info structure.
476///////////////////////////////////////////////////////////////////////////////////////////
477static void external_devices_init( boot_info_t * info )
478{
[188]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
[5]482    uint32_t        func;            // device functionnal index
483    uint32_t        impl;            // device implementation index
[188]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)
[127]491    chdev_t       * chdev;           // local pointer on one channel_device descriptor
[188]492    uint32_t        ext_chdev_gid;   // global index of external chdev
[5]493
494    // get number of peripherals and base of devices array from boot_info
[127]495    dev_nr      = info->ext_dev_nr;
[5]496    dev_tbl     = info->ext_dev;
497
[188]498    // initializes global index (PIC is already placed in cluster 0
499    ext_chdev_gid = 1;
500
[5]501    // loop on external peripherals
[127]502    for( i = 0 ; i < dev_nr ; i++ )
503    {
[188]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 );
[5]508
[407]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;
[5]512
[407]513        // do nothing for ROM, that does not require a device descriptor.
[5]514        if( func == DEV_FUNC_ROM ) continue;
515
[188]516        // do nothing for PIC, that is already initialized
517        if( func == DEV_FUNC_PIC ) continue;
[5]518
[188]519        // check PIC device initialized
[564]520        if( chdev_dir.pic == XPTR_NULL )
[580]521        {
522            printk("\n[PANIC] in %s : PIC device must be initialized first\n",
523            __FUNCTION__ );
524            hal_core_sleep();
525        }
[188]526
527        // check external device functionnal type
[564]528        if( (func != DEV_FUNC_IOB) && (func != DEV_FUNC_IOC) && (func != DEV_FUNC_TXT) &&
529            (func != DEV_FUNC_NIC) && (func != DEV_FUNC_FBF) )
[580]530        {
531            printk("\n[PANIC] in %s : undefined peripheral type\n",
532            __FUNCTION__ );
533            hal_core_sleep();
534        }
[188]535
[127]536        // loops on channels
[428]537        for( channel = 0 ; channel < channels ; channel++ )
[127]538        {
[5]539            // loop on directions
[188]540            for( rx = 0 ; rx < directions ; rx++ )
[1]541            {
[564]542                // skip TXT0 that has already been initialized
543                if( (func == DEV_FUNC_TXT) && (channel == 0) ) continue;
[428]544
[564]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 )
[536]549                {
[564]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;
[536]553
[564]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++;
[550]561                }
562
[5]563                // allocate and initialize a local chdev
[407]564                // when local cluster matches target cluster
[5]565                if( target_cxy == local_cxy )
[1]566                {
[5]567                    chdev = chdev_create( func,
568                                          impl,
569                                          channel,
[188]570                                          rx,          // direction
[5]571                                          base );
572
[564]573                    if( chdev == NULL )
[580]574                    {
575                        printk("\n[PANIC] in %s : cannot allocate chdev\n",
576                        __FUNCTION__ );
577                        hal_core_sleep();
578                    }
[5]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 );
[188]585                    else if( func == DEV_FUNC_FBF ) dev_fbf_init( chdev );
[5]586
[127]587                    // all external (shared) devices are remotely accessible
[5]588                    // initialize the replicated chdev_dir[x][y] structures
[127]589                    // defining the extended pointers on chdev descriptors
590                    xptr_t * entry;
591
[188]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];
[407]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];
[188]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];
[127]599
[1]600                    for( x = 0 ; x < info->x_size ; x++ )
601                    {
[564]602                        for( y = 0 ; y < info->y_size ; y++ )
[1]603                        {
[564]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 ),
[559]609                                                XPTR( local_cxy , chdev ) );
610                            }
[5]611                        }
[1]612                    }
613
[438]614#if( DEBUG_KERNEL_INIT & 0x1 )
615if( hal_time_stamp() > DEBUG_KERNEL_INIT )
[601]616printk("\n[%s] : create chdev %s / channel = %d / rx = %d / cluster %x / chdev = %x\n",
[407]617__FUNCTION__ , chdev_func_str( func ), channel , rx , local_cxy , chdev );
[389]618#endif
[5]619                }  // end if match
620
[19]621                // increment chdev global index (matching or not)
[188]622                ext_chdev_gid++;
[5]623
624            } // end loop on directions
625        }  // end loop on channels
[188]626        } // end loop on devices
627}  // end external_devices_init()
[5]628
[188]629///////////////////////////////////////////////////////////////////////////////////////////
[623]630// This function is called by core[0] in cluster 0 to allocate memory and initialize the PIC
[407]631// device, namely the informations attached to the external IOPIC controller, that
632// must be replicated in all clusters (struct iopic_input).
[188]633// This initialisation must be done before other devices initialisation because the IRQ
[407]634// routing infrastructure is required for both internal and external devices init.
[188]635///////////////////////////////////////////////////////////////////////////////////////////
636// @ info    : pointer on the local boot-info structure.
637///////////////////////////////////////////////////////////////////////////////////////////
[564]638static void __attribute__ ((noinline)) iopic_init( boot_info_t * info )
[188]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
[564]655    // avoid GCC warning
656    base        = XPTR_NULL;
657    impl        = 0;
658
[188]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
[127]664        if( func == DEV_FUNC_PIC )
[1]665        {
[188]666            base     = dev_tbl[i].base;
667            impl     = IMPL_FROM_TYPE( dev_tbl[i].type );
668            found    = true;
669            break;
670        }
671    }
[5]672
[564]673    // check PIC existence
674    if( found == false )
[580]675    {
676        printk("\n[PANIC] in %s : PIC device not found\n",
677        __FUNCTION__ );
678        hal_core_sleep();
679    }
[1]680
[407]681    // allocate and initialize the PIC chdev in cluster 0
682    chdev = chdev_create( DEV_FUNC_PIC,
[188]683                          impl,
684                          0,      // channel
685                          0,      // direction,
686                          base );
[5]687
[564]688    // check memory
689    if( chdev == NULL )
[580]690    {
691        printk("\n[PANIC] in %s : no memory for PIC chdev\n",
692        __FUNCTION__ );
693        hal_core_sleep();
694    }
[5]695
[188]696    // make PIC device type specific initialisation
697    dev_pic_init( chdev );
[1]698
[407]699    // register, in all clusters, the extended pointer
700    // on PIC chdev in "chdev_dir" array
[188]701    xptr_t * entry = &chdev_dir.pic;   
702               
703    for( x = 0 ; x < info->x_size ; x++ )
704    {
[564]705        for( y = 0 ; y < info->y_size ; y++ )
[188]706        {
[564]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 ) , 
[559]712                                XPTR( local_cxy , chdev ) );
713            }
[188]714        }
715    }
[1]716
[407]717    // initialize, in all clusters, the "iopic_input" structure
[188]718    // defining how external IRQs are connected to IOPIC
719
[407]720    // register default value for unused inputs
721    for( x = 0 ; x < info->x_size ; x++ )
722    {
[564]723        for( y = 0 ; y < info->y_size ; y++ )
[407]724        {
[564]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) );
[559]731            }
[407]732        }
733    }
734
735    // register input IRQ index for valid inputs
[577]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
[407]742
[188]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;
[407]749        func    = FUNC_FROM_TYPE( type );
[188]750
[407]751        // get pointer on relevant field in iopic_input
752        if( valid )
[188]753        {
[407]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];
[492]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;
[580]760            else
761            {
762                printk("\n[PANIC] in %s : illegal source device for IOPIC input\n",
763                __FUNCTION__ );
764                hal_core_sleep();
765            }
[188]766
[407]767            // set one entry in all "iopic_input" structures
768            for( x = 0 ; x < info->x_size ; x++ )
769            {
[564]770                for( y = 0 ; y < info->y_size ; y++ )
[407]771                {
[564]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 ); 
[559]777                    }
[407]778                }
779            }
[188]780        }
781    } 
782
[438]783#if( DEBUG_KERNEL_INIT & 0x1 )
[601]784if( hal_time_stamp() > DEBUG_KERNEL_INIT )
[407]785{
[601]786    printk("\n[%s] created PIC chdev in cluster %x at cycle %d\n",
[407]787    __FUNCTION__ , local_cxy , (uint32_t)hal_time_stamp() );
788    dev_pic_inputs_display();
789}
[389]790#endif
[188]791   
792}  // end iopic_init()
793
[1]794///////////////////////////////////////////////////////////////////////////////////////////
[623]795// This function is called by all core[0]s in all cluster to complete the PIC device
[188]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///////////////////////////////////////////////////////////////////////////////////////////
[564]803static void __attribute__ ((noinline)) lapic_init( boot_info_t * info )
[188]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;
[580]856                else
857                {
858                    printk("\n[PANIC] in %s : illegal source device for LAPIC input\n",
859                    __FUNCTION__ );
860                    hal_core_sleep();
861                }
[188]862            }
863        }
864    }
865}  // end lapic_init()
866
867///////////////////////////////////////////////////////////////////////////////////////////
[14]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///////////////////////////////////////////////////////////////////////////////////////////
[564]876static error_t __attribute__ ((noinline)) get_core_identifiers( boot_info_t * info,
877                                                                lid_t       * lid,
878                                                                cxy_t       * cxy,
879                                                                gid_t       * gid )
[14]880{
[127]881    uint32_t   i;
[14]882    gid_t      global_id;
[19]883
[14]884    // get global identifier from hardware register
[127]885    global_id = hal_get_gid();
[14]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;
[19]899}
[14]900
[626]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
[14]927///////////////////////////////////////////////////////////////////////////////////////////
[1]928// This function is the entry point for the kernel initialisation.
[623]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.
[19]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.
[623]933// Only core[0] in cluster 0 print the log messages.
[1]934///////////////////////////////////////////////////////////////////////////////////////////
935// @ info    : pointer on the local boot-info structure.
936///////////////////////////////////////////////////////////////////////////////////////////
937void kernel_init( boot_info_t * info )
938{
[204]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
[1]951    error_t      error;
[285]952    reg_t        status;                    // running core status register
[1]953
[188]954    /////////////////////////////////////////////////////////////////////////////////
[623]955    // STEP 1 : Each core get its core identifier from boot_info, and makes
[188]956    //          a partial initialisation of its private idle thread descriptor.
[623]957    //          core[0] initializes the "local_cxy" global variable.
958    //          core[0] in cluster[0] initializes the TXT0 chdev for log messages.
[188]959    /////////////////////////////////////////////////////////////////////////////////
960
[23]961    error = get_core_identifiers( info,
[14]962                                  &core_lid,
963                                  &core_cxy,
964                                  &core_gid );
[1]965
[623]966    // core[0] initialize cluster identifier
[14]967    if( core_lid == 0 ) local_cxy = info->cxy;
[1]968
[127]969    // each core gets a pointer on its private idle thread descriptor
970    thread = (thread_t *)( idle_threads + (core_lid * CONFIG_THREAD_DESC_SIZE) );
[68]971
[127]972    // each core registers this thread pointer in hardware register
[68]973    hal_set_current_thread( thread );
[71]974
[407]975    // each core register core descriptor pointer in idle thread descriptor
976    thread->core = &LOCAL_CLUSTER->core_tbl[core_lid];
977
[564]978    // each core initializes the idle thread locks counters
979    thread->busylocks = 0;
[124]980
[564]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
[14]985
[623]986    // core[0] initializes cluster info
[564]987    if( core_lid == 0 ) cluster_info_init( info );
988
[623]989    // core[0] in cluster[0] initialises TXT0 chdev descriptor
[564]990    if( (core_lid == 0) && (core_cxy == 0) ) txt0_device_init( info );
991
[623]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
[14]1000    /////////////////////////////////////////////////////////////////////////////////
[564]1001    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1002                                        (info->x_size * info->y_size) );
[14]1003    barrier_wait( &local_barrier , info->cores_nr );
[437]1004    /////////////////////////////////////////////////////////////////////////////////
[14]1005
[438]1006#if DEBUG_KERNEL_INIT
[583]1007if( (core_lid ==  0) & (local_cxy == 0) ) 
[624]1008printk("\n[%s] exit barrier 1 : TXT0 initialized / cycle %d\n",
[610]1009__FUNCTION__, (uint32_t)hal_get_cycles() );
[437]1010#endif
[14]1011
[623]1012    /////////////////////////////////////////////////////////////////////////////////
1013    // STEP 2 : core[0] initializes the cluter manager,
1014    //          including the physical memory allocator.
1015    /////////////////////////////////////////////////////////////////////////////////
[188]1016
[623]1017    // core[0] initialises DQDT (only core[0] in cluster 0 build the quad-tree)
[582]1018    if( core_lid == 0 ) dqdt_init();
1019   
[623]1020    // core[0] initialize other cluster manager complex structures
[14]1021    if( core_lid == 0 )
[1]1022    {
[564]1023        error = cluster_manager_init( info );
[1]1024
[14]1025        if( error )
[580]1026        {
1027             printk("\n[PANIC] in %s : cannot initialize cluster manager in cluster %x\n",
1028             __FUNCTION__, local_cxy );
1029             hal_core_sleep();
1030        }
[14]1031    }
[5]1032
[14]1033    /////////////////////////////////////////////////////////////////////////////////
[564]1034    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1035                                        (info->x_size * info->y_size) );
[14]1036    barrier_wait( &local_barrier , info->cores_nr );
1037    /////////////////////////////////////////////////////////////////////////////////
[1]1038
[438]1039#if DEBUG_KERNEL_INIT
1040if( (core_lid ==  0) & (local_cxy == 0) ) 
[624]1041printk("\n[%s] exit barrier 2 : cluster manager initialized / cycle %d\n",
[610]1042__FUNCTION__, (uint32_t)hal_get_cycles() );
[437]1043#endif
[1]1044
[188]1045    /////////////////////////////////////////////////////////////////////////////////
[624]1046    // STEP 3 : all cores initialize the idle thread descriptor.
1047    //          core[0] initializes the process_zero descriptor,
[623]1048    //          including the kernel VMM (both GPT and VSL)
[188]1049    /////////////////////////////////////////////////////////////////////////////////
1050
1051    // all cores get pointer on local cluster manager & core descriptor
[14]1052    cluster = &cluster_manager;
[127]1053    core    = &cluster->core_tbl[core_lid];
[1]1054
[624]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
[623]1068    // core[0] initializes the process_zero descriptor,
1069    if( core_lid == 0 ) process_zero_create( &process_zero , info );
[5]1070
[623]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) ) 
[624]1079printk("\n[%s] exit barrier 3 : kernel processs initialized / cycle %d\n",
[623]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,
[188]1092    if( (core_lid == 0) && (local_cxy == 0) ) iopic_init( info );
1093   
1094    ////////////////////////////////////////////////////////////////////////////////
[564]1095    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1096                                        (info->x_size * info->y_size) );
[188]1097    barrier_wait( &local_barrier , info->cores_nr );
1098    ////////////////////////////////////////////////////////////////////////////////
[127]1099
[438]1100#if DEBUG_KERNEL_INIT
1101if( (core_lid ==  0) & (local_cxy == 0) ) 
[624]1102printk("\n[%s] exit barrier 4 : MMU and IOPIC initialized / cycle %d\n",
[610]1103__FUNCTION__, (uint32_t)hal_get_cycles() );
[437]1104#endif
[1]1105
[188]1106    ////////////////////////////////////////////////////////////////////////////////
[623]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
[188]1110    ////////////////////////////////////////////////////////////////////////////////
[5]1111
[623]1112    // all core[0]s initialize their local LAPIC extension,
[279]1113    if( core_lid == 0 ) lapic_init( info );
1114
[623]1115    // core[0] scan the internal (private) peripherals,
[188]1116    // and allocates memory for the corresponding chdev descriptors.
1117    if( core_lid == 0 ) internal_devices_init( info );
1118       
[1]1119
[623]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),
[14]1122    // and allocates memory for the chdev descriptors that must be placed
[127]1123    // on the (cxy) cluster according to the global index value.
[188]1124
[14]1125    if( core_lid == 0 ) external_devices_init( info );
[1]1126
[14]1127    /////////////////////////////////////////////////////////////////////////////////
[564]1128    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1129                                        (info->x_size * info->y_size) );
[14]1130    barrier_wait( &local_barrier , info->cores_nr );
1131    /////////////////////////////////////////////////////////////////////////////////
[5]1132
[438]1133#if DEBUG_KERNEL_INIT
1134if( (core_lid ==  0) & (local_cxy == 0) ) 
[624]1135printk("\n[%s] exit barrier 5 : chdevs initialised / cycle %d\n",
[610]1136__FUNCTION__, (uint32_t)hal_get_cycles() );
[437]1137#endif
[1]1138
[438]1139#if( DEBUG_KERNEL_INIT & 1 )
[443]1140if( (core_lid ==  0) & (local_cxy == 0) ) 
[437]1141chdev_dir_display();
1142#endif
1143   
[188]1144    /////////////////////////////////////////////////////////////////////////////////
[624]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.
[188]1148    //          It access the boot device to initialize the file system context.
1149    /////////////////////////////////////////////////////////////////////////////////
1150
[564]1151    // All cores enable IPI
[279]1152    dev_pic_enable_ipi();
1153    hal_enable_irq( &status );
1154
[624]1155    // all cores unblock the idle thread, and register it in scheduler
[296]1156    thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
[103]1157    core->scheduler.idle = thread;
[1]1158
[438]1159#if( DEBUG_KERNEL_INIT & 1 )
[407]1160sched_display( core_lid );
[389]1161#endif
[14]1162
[623]1163    // core[O] in cluster[0] creates the VFS root
[188]1164    if( (core_lid ==  0) && (local_cxy == 0 ) ) 
[14]1165    {
[188]1166        vfs_root_inode_xp = XPTR_NULL;
[23]1167
[614]1168        // Only FATFS is supported yet,
1169        // other File System can be introduced here
[23]1170        if( CONFIG_VFS_ROOT_IS_FATFS )
1171        {
[626]1172            // 1. allocate memory for FATFS context in cluster 0
[188]1173            fatfs_ctx_t * fatfs_ctx = fatfs_ctx_alloc();
1174
[564]1175            if( fatfs_ctx == NULL )
[580]1176            {
1177                printk("\n[PANIC] in %s : cannot create FATFS context in cluster 0\n",
1178                __FUNCTION__ );
1179                hal_core_sleep();
1180            }
[188]1181
1182            // 2. access boot device to initialize FATFS context
1183            fatfs_ctx_init( fatfs_ctx );
[626]1184
[188]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 
[601]1191            // 4. create VFS root inode in cluster 0
[610]1192            error = vfs_inode_create( FS_TYPE_FATFS,                       // fs_type
[188]1193                                      0,                                   // attr
1194                                      0,                                   // rights
1195                                      0,                                   // uid
1196                                      0,                                   // gid
1197                                      &vfs_root_inode_xp );                // return
[564]1198            if( error )
[580]1199            {
1200                printk("\n[PANIC] in %s : cannot create VFS root inode in cluster 0\n",
1201                __FUNCTION__ );
1202                hal_core_sleep();
1203            }
[188]1204
[623]1205            // 5. update FATFS root inode "type" and "extend" fields 
[601]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 );
[624]1208            hal_remote_s32( XPTR( vfs_root_cxy , &vfs_root_ptr->type ), INODE_TYPE_DIR );
[601]1209            hal_remote_spt( XPTR( vfs_root_cxy , &vfs_root_ptr->extend ), 
1210                            (void*)(intptr_t)root_dir_cluster );
[188]1211
[601]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
[23]1219        }
1220        else
1221        {
[564]1222            printk("\n[PANIC] in %s : unsupported VFS type in cluster 0\n",
1223            __FUNCTION__ );
[580]1224            hal_core_sleep();
[23]1225        }
1226
[614]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
[389]1232        // register VFS root inode in process_zero descriptor of cluster 0
[188]1233        process_zero.vfs_root_xp = vfs_root_inode_xp;
[610]1234        process_zero.cwd_xp      = vfs_root_inode_xp;
[188]1235    }
1236
1237    /////////////////////////////////////////////////////////////////////////////////
[564]1238    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1239                                        (info->x_size * info->y_size) );
[188]1240    barrier_wait( &local_barrier , info->cores_nr );
1241    /////////////////////////////////////////////////////////////////////////////////
1242
[438]1243#if DEBUG_KERNEL_INIT
1244if( (core_lid ==  0) & (local_cxy == 0) ) 
[624]1245printk("\n[%s] exit barrier 6 : VFS root (%x,%x) in cluster 0 / cycle %d\n",
[610]1246__FUNCTION__, GET_CXY(process_zero.vfs_root_xp),
1247GET_PTR(process_zero.vfs_root_xp), (uint32_t)hal_get_cycles() );
[437]1248#endif
[188]1249
1250    /////////////////////////////////////////////////////////////////////////////////
[623]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.
[188]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 )
[23]1262        {
[389]1263            // 1. allocate memory for local FATFS context
1264            fatfs_ctx_t * local_fatfs_ctx = fatfs_ctx_alloc();
[188]1265
[564]1266            // check memory
1267            if( local_fatfs_ctx == NULL )
[580]1268            {
1269                printk("\n[PANIC] in %s : cannot create FATFS context in cluster %x\n",
1270                __FUNCTION__ , local_cxy );
1271                hal_core_sleep();
1272            }
[188]1273
[389]1274            // 2. get local pointer on VFS context for FATFS
[188]1275            vfs_ctx_t   * vfs_ctx = &fs_context[FS_TYPE_FATFS];
1276
[389]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
[188]1285            hal_remote_memcpy( XPTR( local_cxy , vfs_ctx ), 
[389]1286                               XPTR( 0 ,         vfs_ctx ), sizeof(vfs_ctx_t) );
[188]1287
[389]1288            // 6. update extend field in local copy of VFS context
1289            vfs_ctx->extend = local_fatfs_ctx;
[188]1290
[564]1291            if( ((fatfs_ctx_t *)vfs_ctx->extend)->sectors_per_cluster != 8 )
[580]1292            {
1293                printk("\n[PANIC] in %s : illegal FATFS context in cluster %x\n",
1294                __FUNCTION__ , local_cxy );
1295                hal_core_sleep();
1296            }
[23]1297        }
1298
[188]1299        // get extended pointer on VFS root inode from cluster 0
[564]1300        vfs_root_inode_xp = hal_remote_l64( XPTR( 0 , &process_zero.vfs_root_xp ) );
[101]1301
[188]1302        // update local process_zero descriptor
1303        process_zero.vfs_root_xp = vfs_root_inode_xp;
[610]1304        process_zero.cwd_xp      = vfs_root_inode_xp;
[14]1305    }
1306
[188]1307    /////////////////////////////////////////////////////////////////////////////////
[564]1308    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1309                                        (info->x_size * info->y_size) );
[188]1310    barrier_wait( &local_barrier , info->cores_nr );
[204]1311    /////////////////////////////////////////////////////////////////////////////////
[101]1312
[438]1313#if DEBUG_KERNEL_INIT
[564]1314if( (core_lid ==  0) & (local_cxy == 1) ) 
[624]1315printk("\n[%s] exit barrier 7 : VFS root (%x,%x) in cluster 1 / cycle %d\n",
[610]1316__FUNCTION__, GET_CXY(process_zero.vfs_root_xp),
1317GET_PTR(process_zero.vfs_root_xp), (uint32_t)hal_get_cycles() );
[437]1318#endif
[188]1319
1320    /////////////////////////////////////////////////////////////////////////////////
[623]1321    // STEP 8 : core[0] in cluster 0 makes the global DEVFS initialisation:
[564]1322    //          It initializes the DEVFS context, and creates the DEVFS
1323    //          "dev" and "external" inodes in cluster 0.
[188]1324    /////////////////////////////////////////////////////////////////////////////////
1325
[564]1326    if( (core_lid ==  0) && (local_cxy == 0) ) 
[1]1327    {
[564]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 )
[580]1332        {
1333            printk("\n[PANIC] in %s : cannot create DEVFS context in cluster 0\n",
1334            __FUNCTION__ , local_cxy );
1335            hal_core_sleep();
1336        }
[564]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)
[188]1347        devfs_global_init( process_zero.vfs_root_xp,
[204]1348                           &devfs_dev_inode_xp,
[188]1349                           &devfs_external_inode_xp );
1350
[564]1351        // 4. initializes DEVFS context extension
1352        devfs_ctx_init( devfs_ctx,
1353                        devfs_dev_inode_xp,
1354                        devfs_external_inode_xp );
[188]1355    }   
1356
1357    /////////////////////////////////////////////////////////////////////////////////
[564]1358    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1359                                        (info->x_size * info->y_size) );
[188]1360    barrier_wait( &local_barrier , info->cores_nr );
[204]1361    /////////////////////////////////////////////////////////////////////////////////
[188]1362
[438]1363#if DEBUG_KERNEL_INIT
1364if( (core_lid ==  0) & (local_cxy == 0) ) 
[624]1365printk("\n[%s] exit barrier 8 : DEVFS root initialized in cluster 0 / cycle %d\n",
[610]1366__FUNCTION__, (uint32_t)hal_get_cycles() );
[437]1367#endif
[188]1368
1369    /////////////////////////////////////////////////////////////////////////////////
[624]1370    // STEP 9 : In all clusters in parallel, core[0] completes DEVFS initialization.
[623]1371    //          Each core[0] get the "dev" and "external" extended pointers from
[624]1372    //          values stored in cluster(0), creates the DEVFS "internal" directory,
1373    //          and creates the pseudo-files for all chdevs in local cluster.
[188]1374    /////////////////////////////////////////////////////////////////////////////////
1375
1376    if( core_lid == 0 )
1377    {
[564]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 );
[188]1380
[457]1381        // get pointer on DEVFS context in cluster 0
[188]1382        devfs_ctx_t * devfs_ctx = hal_remote_lpt( extend_xp );
1383       
[564]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 ) );
[188]1386
[204]1387        // populate DEVFS in all clusters
1388        devfs_local_init( devfs_dev_inode_xp,
1389                          devfs_external_inode_xp,
1390                          &devfs_internal_inode_xp );
[188]1391    }
1392
1393    /////////////////////////////////////////////////////////////////////////////////
[564]1394    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1395                                        (info->x_size * info->y_size) );
[188]1396    barrier_wait( &local_barrier , info->cores_nr );
[204]1397    /////////////////////////////////////////////////////////////////////////////////
[188]1398
[438]1399#if DEBUG_KERNEL_INIT
1400if( (core_lid ==  0) & (local_cxy == 0) ) 
[624]1401printk("\n[%s] exit barrier 9 : DEVFS initialized in cluster 0 / cycle %d\n",
[610]1402__FUNCTION__, (uint32_t)hal_get_cycles() );
[437]1403#endif
[188]1404
[623]1405#if( DEBUG_KERNEL_INIT & 1 )
1406if( (core_lid ==  0) & (local_cxy == 0) ) 
1407vfs_display( vfs_root_inode_xp );
1408#endif
1409
[188]1410    /////////////////////////////////////////////////////////////////////////////////
[623]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.
[188]1414    /////////////////////////////////////////////////////////////////////////////////
1415
[457]1416    if( (core_lid == 0) && (local_cxy == 0) ) 
[188]1417    {
[428]1418       process_init_create();
[188]1419    }
[101]1420
[624]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
[443]1427#if (DEBUG_KERNEL_INIT & 1)
[564]1428if( (core_lid ==  0) & (local_cxy == 0) ) 
[443]1429sched_display( 0 );
1430#endif
1431
[564]1432    if( (core_lid == 0) && (local_cxy == 0) ) 
[188]1433    {
[5]1434        print_banner( (info->x_size * info->y_size) , info->cores_nr );
[623]1435    }
[68]1436
[438]1437#if( DEBUG_KERNEL_INIT & 1 )
[623]1438if( (core_lid ==  0) & (local_cxy == 0) ) 
[437]1439printk("\n\n***** memory fooprint for main kernel objects\n\n"
[68]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"
[564]1456                   " - busylock           : %d bytes\n"
1457                   " - remote busylock    : %d bytes\n"
1458                   " - queuelock          : %d bytes\n"
1459                   " - remote queuelock   : %d bytes\n"
[68]1460                   " - rwlock             : %d bytes\n"
1461                   " - remote rwlock      : %d bytes\n",
[564]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    ));
[406]1484#endif
1485
[398]1486    // each core activates its private TICK IRQ
1487    dev_pic_enable_timer( CONFIG_SCHED_TICK_MS_PERIOD );
[14]1488
[610]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
[623]1495#if( DEBUG_KERNEL_INIT & 1 )
[610]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() );
[440]1500#endif
1501
[407]1502    // each core jump to thread_idle_func
[50]1503    thread_idle_func();
[14]1504
[610]1505}  // end kernel_init()
1506
Note: See TracBrowser for help on using the repository browser.