source: soft/giet_vm/giet_kernel/kernel_init.c @ 473

Last change on this file since 473 was 467, checked in by alain, 10 years ago

Adding a new step in the kernel_init() procedure:
Processor[0][0][0] initialises the distributed kernel heap
allocators, and the distributed lock protecting TTY0.

  • Property svn:executable set to *
File size: 13.3 KB
RevLine 
[258]1///////////////////////////////////////////////////////////////////////////////////
2// File     : kernel_init.c
3// Date     : 26/05/2012
4// Authors  : alain greiner & mohamed karaoui
5// Copyright (c) UPMC-LIP6
6////////////////////////////////////////////////////////////////////////////////////
[440]7// This kernel_init.c file is part of the GIET-VM nano-kernel.
[258]8////////////////////////////////////////////////////////////////////////////////////
9
10#include <giet_config.h>
[440]11#include <hard_config.h>
[258]12#include <utils.h>
[459]13#include <tty0.h>
[467]14#include <kernel_malloc.h>
[258]15#include <fat32.h>
16#include <xcu_driver.h>
[459]17#include <ioc_driver.h>
[258]18#include <ctx_handler.h>
19#include <irq_handler.h>
20#include <mapping_info.h>
21#include <mips32_registers.h>
22
[322]23#if !defined(X_SIZE)
24# error: You must define X_SIZE in the hard_config.h file
25#endif
26
27#if !defined(Y_SIZE)
28# error: You must define Y_SIZE in the hard_config.h file
29#endif
30
31#if !defined(Y_WIDTH)
32# error: You must define Y_WIDTH in the hard_config.h file
33#endif
34
35#if !defined(Y_WIDTH)
36# error: You must define Y_WIDTH in the hard_config.h file
37#endif
38
39#if !defined(NB_PROCS_MAX)
40# error: You must define NB_PROCS_MAX in the hard_config.h file
41#endif
42
43#if !defined(NB_TOTAL_PROCS)
44# error: You must define NB_TOTAL_PROCS in the hard_config.h file
45#endif
46
47#if !defined(USE_XCU)
48# error: You must define USE_XCU in the hard_config.h file
49#endif
50
51#if !defined(IDLE_TASK_INDEX)
[391]52# error: You must define IDLE_TASK_INDEX in the ctx_handler.h file
[322]53#endif
54
55#if !defined(GIET_TICK_VALUE)
56# error: You must define GIET_TICK_VALUE in the giet_config.h file
57#endif
58
59#if !defined(GIET_NB_VSPACE_MAX)
60# error: You must define GIET_NB_VSPACE_MAX in the giet_config.h file
61#endif
62
[467]63#if !defined(NB_TTY_CHANNELS)
64# error: You must define NB_TTY_CHANNELS in the hard_config.h file
65#endif
66
67#if (NB_TTY_CHANNELS < 1)
68# error: NB_TTY_CHANNELS cannot be smaller than 1
69#endif
70
71
72
[258]73///////////////////////////////////////////////////////////////////////////////////
[467]74// Ditributed kernel heap descriptors array (for dynamic memory allocation)
75///////////////////////////////////////////////////////////////////////////////////
76
77kernel_heap_t  kernel_heap[X_SIZE][Y_SIZE];
78
79///////////////////////////////////////////////////////////////////////////////////
[410]80// FAT internal representation for kernel code
81///////////////////////////////////////////////////////////////////////////////////
82
[467]83fat32_fs_t     fat      __attribute__((aligned(512)));
[410]84
85///////////////////////////////////////////////////////////////////////////////////
[258]86// array of pointers on the page tables (virtual addresses)
87///////////////////////////////////////////////////////////////////////////////////
88
[345]89volatile unsigned int _ptabs_vaddr[GIET_NB_VSPACE_MAX];    // virtual addresses
90volatile unsigned int _ptabs_ptprs[GIET_NB_VSPACE_MAX];    // physical addresses >> 13
[258]91
92///////////////////////////////////////////////////////////////////////////////////
[467]93// Array of pointers on the schedulers (physical addresses)
[258]94///////////////////////////////////////////////////////////////////////////////////
95
[467]96volatile static_scheduler_t*    _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX]; 
[258]97
98////////////////////////////////////////////////////////////////////////////////////
[391]99// Synchonisation barrier before jumping to user code
[258]100////////////////////////////////////////////////////////////////////////////////////
101
[428]102volatile unsigned int kernel_init_barrier = 0;
[294]103
[467]104////////////////////////////////////////////////////////////////////////////////////
105// Global variables for TTY/kernel communications
106////////////////////////////////////////////////////////////////////////////////////
107
108sbt_lock_t     _tty_tx_lock[NB_TTY_CHANNELS]  __attribute__((aligned(64)));
109
110unsigned int   _tty_rx_buf[NB_TTY_CHANNELS];
111unsigned int   _tty_rx_full[NB_TTY_CHANNELS]; 
112
113
114
115
116
[310]117///////////////////////////////////////////////////////////////////////////////////
[467]118// This kernel_init() function completes the kernel initialisation in 7 steps:
119// All processors execute this code, but this is done sequencially.
120// - step 0 : Initialise fat, heap descriptors, and tty locks
121// - step 1 : Initialise scheduler pointers array
122// - step 2 : Initialise PTAB pointers arrays
123// - step 3 : Initialise private XCU masks
124// - step 4 :
125// - step 5 :
126// - step 6 :
127///////////////////////////////////////////////////////////////////////////////////
[310]128__attribute__((section (".kinit"))) void kernel_init() 
[258]129{
[428]130    // gpid : hardware processor index (fixed format: X_WIDTH|Y_WIDTH|P_WIDTH)
[467]131    // p    : local processor id in a cluster ( p < NB_PROCS_MAX)
132    // cpid : continuous processor index = (((x * Y_SIZE + y) * NB_PROCS_MAX) + p
[428]133
134    unsigned int gpid       = _get_procid();
135    unsigned int cluster_xy = gpid >> P_WIDTH;
136    unsigned int x          = cluster_xy >> Y_WIDTH & ((1<<X_WIDTH)-1);
[294]137    unsigned int y          = cluster_xy & ((1<<Y_WIDTH)-1);
[440]138    unsigned int p          = gpid & ((1<<P_WIDTH)-1);
139    unsigned int cpid       = ((( x * Y_SIZE) + y) * NB_PROCS_MAX) + p;
[258]140
[428]141    // This initialisation is done sequencially by each processor
142    while( cpid != kernel_init_barrier ) asm volatile ( "nop" );
143
[467]144    // Step 0 : P[0,0,0] initialises the kernel FAT
145    //          and the distributed kernel_heap descriptors array.
146    if ( gpid == 0 )
147    {
148        _heap_init();
149       
150#if GIET_DEBUG_INIT
151_nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] completes kernel HEAP init\n", x, y, p );
152#endif
153        unsigned int channel;
154        for ( channel = 0 ; channel < NB_TTY_CHANNELS ; channel++ )
155        {
156            _sbt_lock_init( &_tty_tx_lock[channel] );
157        }
[459]158
[467]159#if GIET_DEBUG_INIT
160_nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] completes TTY locks init\n", x, y, p );
161#endif
162
163        _fat_init( IOC_BOOT_MODE ); 
164
165#if GIET_DEBUG_INIT
166_nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] completes kernel FAT init\n", x, y, p );
167#endif
168
169    }
170
[440]171    // Step 1 : each processor get its scheduler virtual address from CP0_SCHED register
172    //          and contributes to _schedulers[] array initialisation
[258]173
174    static_scheduler_t* psched     = (static_scheduler_t*)_get_sched();
175    unsigned int        tasks      = psched->tasks;
176
[440]177    _schedulers[x][y][p] = psched;
[258]178
179#if GIET_DEBUG_INIT
[467]180_nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] initialises SCHED array\n"
181               " - scheduler vbase = %x\n"
182               " - tasks           = %d\n",
183               x, y, p, (unsigned int)psched, tasks );
[258]184#endif
185
[294]186    // step 2 : each processor that is allocated at least one task loops
187    //          on all allocated tasks:
188    //          - contributes to _ptabs_vaddr[] & _ptabs_ptprs[] initialisation.
189    //          - set CTX_RA slot  with the kernel _ctx_eret() virtual address.
190    //          - set CTX_EPC slot that must contain the task entry point,
191    //            and contain only at this point the virtual address of the memory
[440]192    //            location containing this entry point.
[258]193
194    unsigned int ltid;
195
196    for (ltid = 0; ltid < tasks; ltid++) 
197    {
[440]198        unsigned int vsid = _get_task_slot( x, y, p, ltid , CTX_VSID_ID ); 
199        unsigned int ptab = _get_task_slot( x, y, p, ltid , CTX_PTAB_ID ); 
200        unsigned int ptpr = _get_task_slot( x, y, p, ltid , CTX_PTPR_ID ); 
[258]201
[294]202        // initialize PTABS arrays
[258]203        _ptabs_vaddr[vsid] = ptab;
204        _ptabs_ptprs[vsid] = ptpr;
205
[294]206#if GIET_DEBUG_INIT
[467]207_nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] initialises PTABS arrays\n"
[294]208        " - ptabs_vaddr[%d] = %x / ptpr_paddr[%d] = %l\n",
[440]209        x, y, p, 
[294]210        vsid, ptab, vsid, ((unsigned long long)ptpr)<<13 );
211#endif
212
213        // set the ptpr to use the task page table
214        asm volatile( "mtc2    %0,   $0   \n"
215                      : : "r" (ptpr) );
216
217        // compute ctx_ra
[258]218        unsigned int ctx_ra = (unsigned int)(&_ctx_eret);
[440]219        _set_task_slot( x, y, p, ltid, CTX_RA_ID, ctx_ra );
[258]220
[294]221        // compute ctx_epc
[440]222        unsigned int* ptr = (unsigned int*)_get_task_slot( x, y, p, ltid, CTX_EPC_ID );
223        _set_task_slot( x, y, p, ltid, CTX_EPC_ID, *ptr );
[258]224
225#if GIET_DEBUG_INIT
[467]226_nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] updates context for task %d\n"
227               " - ctx_epc   = %x\n"
228               " - ctx_ra    = %x\n",
229               x, y, p, ltid,
230               _get_task_slot( x, y, p, ltid, CTX_EPC_ID ),
231               _get_task_slot( x, y, p, ltid, CTX_RA_ID ) );
[258]232#endif
233
[294]234    }  // end for tasks
[258]235
[449]236    // step 3 : compute and set XCU masks
[258]237
[263]238    unsigned int isr_switch_index = 0xFFFFFFFF;
[258]239    unsigned int hwi_mask = 0;
240    unsigned int pti_mask = 0;
[294]241    unsigned int wti_mask = 0;
242    unsigned int irq_id;            // IN_IRQ index
243    unsigned int entry;             // interrupt vector entry
[258]244
245    for (irq_id = 0; irq_id < 32; irq_id++) 
246    {
[294]247        entry = psched->hwi_vector[irq_id];
248        if ( entry & 0x80000000 ) hwi_mask = hwi_mask | (1<<irq_id);
249        if ( (entry & 0x0000FFFF) == ISR_TICK ) isr_switch_index = irq_id;
[258]250
[294]251        entry = psched->pti_vector[irq_id];
252        if ( entry & 0x80000000 ) pti_mask = pti_mask | (1<<irq_id);
253        if ( (entry & 0x0000FFFF) == ISR_TICK ) isr_switch_index = irq_id;
254
255        entry = psched->wti_vector[irq_id];
256        if ( entry & 0x80000000 ) wti_mask = wti_mask | (1<<irq_id);
257        if ( (entry & 0x0000FFFF) == ISR_TICK ) isr_switch_index = irq_id;
[258]258    }
259
260#if GIET_DEBUG_INIT
[467]261_nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] sets XCU masks\n"
262               " - XCU HWI_MASK = %x\n"
263               " - XCU WTI_MASK = %x\n"
264               " - XCU PTI_MASK = %x\n",
265               x, y, p, hwi_mask, wti_mask, pti_mask );
[258]266#endif
267
[440]268    unsigned int channel = p * IRQ_PER_PROCESSOR; 
[258]269
[294]270    _xcu_set_mask( cluster_xy, channel, hwi_mask, IRQ_TYPE_HWI ); 
271    _xcu_set_mask( cluster_xy, channel, wti_mask, IRQ_TYPE_WTI );
272    _xcu_set_mask( cluster_xy, channel, pti_mask, IRQ_TYPE_PTI );
[258]273
[449]274    // step 4 : start TICK timer if at least one task
[258]275    if (tasks > 0) 
276    {
[294]277        // one ISR_TICK must be defined for each proc
[263]278        if (isr_switch_index == 0xFFFFFFFF) 
[258]279        {
[467]280            _nolock_printf("\n[GIET ERROR] ISR_TICK not found for processor[%d,%d,%d]\n",
281                           x, y, p );
[258]282            _exit();
283        }
284
[294]285        // start system timer
286        _xcu_timer_start( cluster_xy, isr_switch_index, GIET_TICK_VALUE ); 
[258]287
[294]288    }
289
[258]290#if GIET_DEBUG_INIT
[467]291_nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] starts TICK timer\n",
292               x, y, p );
[258]293#endif
294
[449]295    // step 5 : each processor updates the idle_task context:
[391]296    //          (CTX_SP, CTX_RA, CTX_EPC).
297    //          The 4 Kbytes idle stack is implemented in the scheduler.
[258]298    //          The PTPR register, the CTX_PTPR and CTX_PTAB slots
299    //          have been initialised in boot code.
300
[391]301    unsigned int pstack = ((unsigned int)psched) + 0x2000;
[258]302
[440]303    _set_task_slot( x, y, p, IDLE_TASK_INDEX, CTX_SP_ID,  pstack);
304    _set_task_slot( x, y, p, IDLE_TASK_INDEX, CTX_RA_ID,  (unsigned int) &_ctx_eret);
305    _set_task_slot( x, y, p, IDLE_TASK_INDEX, CTX_EPC_ID, (unsigned int) &_idle_task);
[258]306
307#if GIET_DEBUG_INIT
[467]308_nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] initializes IDLE task\n"
309               " - stack_base = %x\n"
310               " - stack_size = 0x1000\n",
311               x, y, p, pstack - 0x1000 );
[258]312#endif
313
[449]314    // step 6 : when all processors reach the synchronisation barrier,
[294]315    //          each processor set registers SP, SR, PTPR, EPC,
[258]316    //          with the values corresponding to the first allocated task,
[294]317    //          or to the idle_task if there is no task allocated,
318    //          and jump to user code
[258]319
320    if (tasks == 0) 
321    {
322        ltid = IDLE_TASK_INDEX;
323
[467]324        _nolock_printf("\n[GIET WARNING] No task allocated to processor[%d,%d,%d]\n",
325                       x, y, p );
[258]326    }
[294]327    else
328    {
329        ltid = 0;
330    }
[258]331
[440]332    unsigned int sp_value   = _get_task_slot( x, y, p, ltid, CTX_SP_ID);
333    unsigned int sr_value   = _get_task_slot( x, y, p, ltid, CTX_SR_ID);
334    unsigned int ptpr_value = _get_task_slot( x, y, p, ltid, CTX_PTPR_ID);
335    unsigned int epc_value  = _get_task_slot( x, y, p, ltid, CTX_EPC_ID);
[258]336
[330]337#if GIET_DEBUG_INIT
[467]338_nolock_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] reach barrier at cycle %d\n",
339               x, y, p, _get_proctime() );
[330]340#endif
[258]341
[310]342    // increment barrier counter
[428]343    kernel_init_barrier++;
[310]344
345    // busy waiting until all processors synchronized
[428]346    while ( kernel_init_barrier != NB_TOTAL_PROCS );
[310]347
[391]348#if GIET_DEBUG_INIT
[467]349_printf("\n[DEBUG KERNEL_INIT] P[%d,%d,%d] initializes registers at cycle %d\n"
[391]350        " - sp   = %x\n"
351        " - sr   = %x\n"
352        " - ptpr = %x\n"
353        " - epc  = %x\n",
[440]354        x, y, p, _get_proctime(),
[391]355        sp_value, sr_value, ptpr_value, epc_value );
356#endif
357
[294]358    // set registers and jump to user code
359    asm volatile ( "move  $29,  %0                  \n"   /* SP <= ctx[CTX_SP_ID] */
360                   "mtc0  %1,   $12                 \n"   /* SR <= ctx[CTX_SR_ID] */
361                   "mtc2  %2,   $0                  \n"   /* PTPR <= ctx[CTX_PTPR] */
362                   "mtc0  %3,   $14                 \n"   /* EPC <= ctx[CTX_EPC]  */
363                   "eret                            \n"   /* jump to user code  */
364                   "nop                             \n"
365                   : 
366                   : "r"(sp_value), "r"(sr_value), "r"(ptpr_value), "r"(epc_value)
[345]367                   : "$29", "memory" );
[294]368
[310]369} // end kernel_init()
[258]370
371
372// Local Variables:
373// tab-width: 4
374// c-basic-offset: 4
375// c-file-offsets:((innamespace . 0)(inline-open . 0))
376// indent-tabs-mode: nil
377// End:
378// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
379
Note: See TracBrowser for help on using the repository browser.