source: soft/giet_vm/sys/ctx_handler.c @ 160

Last change on this file since 160 was 160, checked in by karaoui, 12 years ago

giet-vm new version

File size: 4.5 KB
Line 
1///////////////////////////////////////////////////////////////////////////////////
2// File     : ctx_handler.c
3// Date     : 01/04/2012
4// Authors  : alain greiner & joel porquet
5// Copyright (c) UPMC-LIP6
6////////////////////////////////////////////////////////////////////////////////////
7// The ctx_handler.h and ctx_handler.c files are part of the GIET nano-kernel.
8// This code is used to support context switch when several tasks are executing
9// in time multiplexing on a single processor.
10// The tasks must be statically allocated to a processor in the boot phase, and
11// there is one private scheduler per processor:  NB_CLUSTERS * NB_PROCS
12// Each sheduler contains up to NB_TASKS_MAX contexts.
13////////////////////////////////////////////////////////////////////////////////////
14// A task context is an array of 64 words = 256 bytes.
15// It contains copies of processor registers, when the task is not running,
16// and some general informations associated to the task.
17// - It contains GPR[i], generally stored in slot (i). $0, *26 & $27 are not saved.
18// - It contains HI & LO registers.
19// - It contains CP0 registers: EPC, SR, CR.
20// - It contains CP2 registers : PTPR and MODE.
21// - It contains the TTY index for the terminal allocated to the task.
22// ctx[0] <- SR   ctx[8] <- $8    ctx[16]<- $16   ctx[24]<- $24   ctx[32]<- EPC
23// ctx[1] <- $1   ctx[9] <- $9    ctx[17]<- $17   ctx[25]<- $25   ctx[33]<- CR
24// ctx[2] <- $2   ctx[10]<- $10   ctx[18]<- $18   ctx[26]<- LO    ctx[34]<- TTY
25// ctx[3] <- $3   ctx[11]<- $11   ctx[19]<- $19   ctx[27]<- HI    ctx[35]<- PTPR
26// ctx[4] <- $4   ctx[12]<- $12   ctx[20]<- $20   ctx[28]<- $28   ctx[36]<- MODE
27// ctx[5] <- $5   ctx[13]<- $13   ctx[21]<- $21   ctx[29]<- $29   ctx[37]<- reserved
28// ctx[6] <- $6   ctx[14]<- $14   ctx[22]<- $22   ctx[30]<- $30   ctx[38]<- reserved
29// ctx[7] <- $7   ctx[15]<- $15   ctx[23]<- $23   ctx[31]<- $31   ctx[39]<- reserved
30/////////////////////////////////////////////////////////////////////////////////////
31
32#include <giet_config.h>
33#include <drivers.h>
34#include <common.h>
35#include <ctx_handler.h>
36#include <mapping_info.h>
37#include <sys_handler.h>
38
39extern void _task_switch(unsigned int *, unsigned int *);
40
41/////////////////////////////////////////////////////////////////////////////////
42//      Global variables
43/////////////////////////////////////////////////////////////////////////////////
44
45static_scheduler_t _scheduler[NB_CLUSTERS * NB_PROCS];
46
47/////////////////////////////////////////////////////////////////////////////////
48//      _ctx_switch()
49// This function performs a context switch between the running task
50// and  another task, using a round-robin sheduling policy.
51// It use the global variable scheduler[] : array indexed by the procid,
52// that contains NB_CLUSTERS * NB_PROCS entries.
53// The return address contained in $31 is saved in the _current task context
54// (in the ctx[31] slot), and the function actually returns to the address
55// contained in the ctx[31] slot of the new task context. To perform the
56// actual switch, it calls the _task_switch function written in assembly language.
57/////////////////////////////////////////////////////////////////////////////////
58void _ctx_switch()
59{
60    unsigned char curr_task_id;
61    unsigned char next_task_id;
62
63    unsigned int *curr_context;
64    unsigned int *next_context;
65
66    unsigned int tasks;
67    unsigned int proc_id;
68
69    proc_id = _procid();
70    tasks   = _scheduler[proc_id].tasks;
71
72    // return if only one task  */
73    if ( tasks <= 1) return;
74 
75    // compute the task context base address for the current task
76    curr_task_id = _scheduler[proc_id].current;
77    curr_context = &(_scheduler[proc_id].context[curr_task_id][0]);
78   
79    // select the next task using a round-robin scheduling policy
80    next_task_id = (curr_task_id + 1) % tasks;
81   
82    // compute the task context base address for the next task
83    next_context = &(_scheduler[proc_id].context[next_task_id][0]);
84
85    //  update the scheduler state, and makes the task switch
86    _scheduler[proc_id].current = next_task_id;
87    _task_switch( curr_context, next_context );
88
89#if GIET_DEBUG_SWITCH
90unsigned int time = _proctime();
91_tty_puts( "\n[GIET] Context switch for processor ");
92_tty_putw( proc_id );
93_tty_puts( " at cycle ");
94_tty_putw( time );
95_tty_puts("\n");
96_tty_puts( " - tasks        = ");
97_tty_putw( tasks );
98_tty_puts("\n");
99_tty_puts( " - curr_task_id = ");
100_tty_putw( curr_task_id );
101_tty_puts("\n");
102_tty_puts( " - next_task_id = ");
103_tty_putw( next_task_id );
104_tty_puts("\n");
105#endif
106
107} // end _ctx_switch
108
Note: See TracBrowser for help on using the repository browser.