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

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

This version is a major evolution: The physical memory allocators,
defined in the kmem.c, ppm.c, and kcm.c files have been modified
to support remote accesses. The RPCs that were previously user
to allocate physical memory in a remote cluster have been removed.
This has been done to cure a dead-lock in case of concurrent page-faults.

This version 2.2 has been tested on a (4 clusters / 2 cores per cluster)
TSAR architecture, for both the "sort" and the "fft" applications.

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