/////////////////////////////////////////////////////////////////////////////////// // File : ctx_handler.c // Date : 01/04/2012 // Authors : alain greiner & joel porquet // Copyright (c) UPMC-LIP6 //////////////////////////////////////////////////////////////////////////////////// // The ctx_handler.h and ctx_handler.c files are part of the GIET nano-kernel. // This code is used to support context switch when several tasks are executing // in time multiplexing on a single processor. // The tasks must be statically allocated to a processor in the boot phase, and // there is one private scheduler per processor: NB_CLUSTERS * NB_PROCS // Each sheduler contains up to NB_TASKS_MAX contexts. //////////////////////////////////////////////////////////////////////////////////// // A task context is an array of 64 words = 256 bytes. // It contains copies of processor registers, when the task is not running, // and some general informations associated to the task. // - It contains GPR[i], generally stored in slot (i). $0, *26 & $27 are not saved. // - It contains HI & LO registers. // - It contains CP0 registers: EPC, SR, CR. // - It contains CP2 registers : PTPR and MODE. // - It contains the TTY index for the terminal allocated to the task. // ctx[0] <- SR ctx[8] <- $8 ctx[16]<- $16 ctx[24]<- $24 ctx[32]<- EPC // ctx[1] <- $1 ctx[9] <- $9 ctx[17]<- $17 ctx[25]<- $25 ctx[33]<- CR // ctx[2] <- $2 ctx[10]<- $10 ctx[18]<- $18 ctx[26]<- LO ctx[34]<- TTY // ctx[3] <- $3 ctx[11]<- $11 ctx[19]<- $19 ctx[27]<- HI ctx[35]<- PTPR // ctx[4] <- $4 ctx[12]<- $12 ctx[20]<- $20 ctx[28]<- $28 ctx[36]<- MODE // ctx[5] <- $5 ctx[13]<- $13 ctx[21]<- $21 ctx[29]<- $29 ctx[37]<- reserved // ctx[6] <- $6 ctx[14]<- $14 ctx[22]<- $22 ctx[30]<- $30 ctx[38]<- reserved // ctx[7] <- $7 ctx[15]<- $15 ctx[23]<- $23 ctx[31]<- $31 ctx[39]<- reserved ///////////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include extern void _task_switch(unsigned int *, unsigned int *); ///////////////////////////////////////////////////////////////////////////////// // Global variables ///////////////////////////////////////////////////////////////////////////////// static_scheduler_t _scheduler[NB_CLUSTERS * NB_PROCS]; ///////////////////////////////////////////////////////////////////////////////// // _ctx_switch() // This function performs a context switch between the running task // and another task, using a round-robin sheduling policy. // It use the global variable scheduler[] : array indexed by the procid, // that contains NB_CLUSTERS * NB_PROCS entries. // The return address contained in $31 is saved in the _current task context // (in the ctx[31] slot), and the function actually returns to the address // contained in the ctx[31] slot of the new task context. To perform the // actual switch, it calls the _task_switch function written in assembly language. ///////////////////////////////////////////////////////////////////////////////// void _ctx_switch() { unsigned char curr_task_id; unsigned char next_task_id; unsigned int *curr_context; unsigned int *next_context; unsigned int tasks; unsigned int proc_id; proc_id = _procid(); tasks = _scheduler[proc_id].tasks; // return if only one task */ if ( tasks <= 1) return; // compute the task context base address for the current task curr_task_id = _scheduler[proc_id].current; curr_context = &(_scheduler[proc_id].context[curr_task_id][0]); // select the next task using a round-robin scheduling policy next_task_id = (curr_task_id + 1) % tasks; // compute the task context base address for the next task next_context = &(_scheduler[proc_id].context[next_task_id][0]); // update the scheduler state, and makes the task switch _scheduler[proc_id].current = next_task_id; _task_switch( curr_context, next_context ); #if GIET_DEBUG_SWITCH unsigned int time = _proctime(); _tty_puts( "\n[GIET] Context switch for processor "); _tty_putw( proc_id ); _tty_puts( " at cycle "); _tty_putw( time ); _tty_puts("\n"); _tty_puts( " - tasks = "); _tty_putw( tasks ); _tty_puts("\n"); _tty_puts( " - curr_task_id = "); _tty_putw( curr_task_id ); _tty_puts("\n"); _tty_puts( " - next_task_id = "); _tty_putw( next_task_id ); _tty_puts("\n"); #endif } // end _ctx_switch