MentOS  0.8.0
The Mentoring Operating System
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Classes | Macros | Typedefs | Functions | Variables
timer.c File Reference

Timer implementation. More...

Classes

struct  sleep_data
 Contains the entry of a wait queue and timespec which keeps trakc of the remaining time. More...
 

Macros

#define __DEBUG_HEADER__   "[TIMER ]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 
#define PIT_DATAREG0   0x40u
 Channel 0 data port (read/write).
 
#define PIT_DATAREG1   0x41u
 Channel 1 data port (read/write).
 
#define PIT_DATAREG2   0x42u
 Channel 2 data port (read/write).
 
#define PIT_COMREG   0x43u
 Mode/Command register (write only, a read is ignored).
 
#define PIT_DIVISOR   1193182
 Frequency divider value (1.193182 MHz).
 
#define PIT_CONFIGURATION   0x34u
 Command used to configure the PIT. More...
 
#define PIT_MASK   0xFFu
 Mask used to set the divisor.
 

Typedefs

typedef struct sleep_data sleep_data_t
 Contains the entry of a wait queue and timespec which keeps trakc of the remaining time.
 

Functions

void timer_phase (const uint32_t hz)
 Allows to set the timer phase to the given frequency. More...
 
void timer_handler (pt_regs *reg)
 Handles the timer. More...
 
void timer_install (void)
 Sets up the system clock by installing the timer handler into IRQ0.
 
uint64_t timer_get_seconds (void)
 Returns the number of seconds since the system started its execution. More...
 
unsigned long timer_get_ticks (void)
 Returns the number of ticks since the system started its execution. More...
 
static void __print_vector (list_head *vector)
 Prints the addresses of all the timers inside a vector. More...
 
static void __print_vector_base (tvec_base_t *base)
 Prints the addresses of all the timers inside a vector base. More...
 
static void __tvec_base_init (tvec_base_t *base)
 Initializes the tvec_base. More...
 
static list_head * __timer_get_target_vector (tvec_base_t *base, struct timer_list *timer)
 Select correct timer vector and position inside of it for the input timer index contains the position inside of the tv_index timer vector. More...
 
static void __timer_cascate_vector (tvec_base_t *base, list_head *current_vector)
 Move all timers from tv up one level. More...
 
static void __timer_cascate_base (tvec_base_t *base)
 Move all timers from tv up one level. More...
 
static struct timer_list__timer_list_alloc (void)
 Allocates the memory for timer. More...
 
static void __timer_list_dealloc (struct timer_list *timer)
 Frees the memory of a timer. More...
 
void init_timer (struct timer_list *timer)
 Initializes a new timer struct. More...
 
void add_timer (struct timer_list *timer)
 Add a new timer to the current CPU. More...
 
void remove_timer (struct timer_list *timer)
 Removes a timer from the current CPU. More...
 
static sleep_data_t__sleep_data_alloc (void)
 Allocates the memory for sleep_data. More...
 
static void __sleep_data_dealloc (sleep_data_t *sleep_data)
 Frees the memory of a sleep_data. More...
 
static void __values_to_itimerval (time_t interval, time_t value, struct itimerval *timer)
 Transforms interval and value to the timer. More...
 
static void __itimerval_to_values (time_t *interval, time_t *value, const struct itimerval *timer)
 Transforms an interval timer to values. More...
 
static void __update_task_itimerval (int which, const struct itimerval *timer)
 Updates the timer for the given task. More...
 
void dynamic_timers_install (void)
 Initialize dynamic timer system.
 
void run_timer_softirq (void)
 Updates the timer data structures.
 
static void debug_timeout (unsigned long data)
 Standard debugging function callback. More...
 
static void sleep_timeout (unsigned long data)
 Callback for when a sleep timer expires. More...
 
static void alarm_timeout (unsigned long task_ptr)
 Function executed when the real_timer of a process expires, sends SIGALRM to process. More...
 
static void real_timer_timeout (unsigned long task_ptr)
 Function executed when the real_timer of a process expires, sends SIGALRM to process. More...
 
int sys_nanosleep (const struct timespec *req, struct timespec *rem)
 Suspends the execution of the calling thread. More...
 
unsigned sys_alarm (int seconds)
 Send signal to calling thread after desired seconds. More...
 
int sys_getitimer (int which, struct itimerval *curr_value)
 Fills the structure pointed to by curr_value with the current setting for the timer specified by which. More...
 
int sys_setitimer (int which, const struct itimerval *new_value, struct itimerval *old_value)
 Arms or disarms the timer specified by which, by setting the timer to the value specified by new_value. More...
 
void update_process_profiling_timer (task_struct *proc)
 Update the profiling timer and generate SIGPROF if it has expired. More...
 

Variables

static __volatile__ unsigned long timer_ticks = 0
 The number of ticks since the system started its execution.
 
static tvec_base_t cpu_base = { 0 }
 Contains timer for each CPU (for now only one)
 
static wait_queue_head_t sleep_queue
 Contains all process waiting for a sleep.
 

Detailed Description

Timer implementation.

Macro Definition Documentation

◆ PIT_CONFIGURATION

#define PIT_CONFIGURATION   0x34u

Command used to configure the PIT.

Bits Usage 6 and 7 [Select channel] 0 0 = Channel 0 0 1 = Channel 1 1 0 = Channel 2 1 1 = Read-back command (8254 only) 4 and 5 [Access mode] 0 0 = Latch count value command 0 1 = Access mode: lobyte only 1 0 = Access mode: hibyte only 1 1 = Access mode: lobyte/hibyte 1 to 3 [Operating mode] 0 0 0 = Mode 0 (interrupt on terminal count) 0 0 1 = Mode 1 (hardware re-triggerable one-shot) 0 1 0 = Mode 2 (rate generator) 0 1 1 = Mode 3 (square wave generator) 1 0 0 = Mode 4 (software triggered strobe) 1 0 1 = Mode 5 (hardware triggered strobe) 1 1 0 = Mode 2 (rate generator, same as 010b) 1 1 1 = Mode 3 (square wave generator, same as 011b) 0 [BCD/Binary mode] 0 = 16-bit binary 1 = four-digit BCD

Examples: 0x36 = 00|11|011|0 0x34 = 00|11|010|0

Function Documentation

◆ __itimerval_to_values()

static void __itimerval_to_values ( time_t interval,
time_t value,
const struct itimerval timer 
)
inlinestatic

Transforms an interval timer to values.

Parameters
intervalthe output variable where to store the interval for periodic timer.
valuethe output variable where to store the time until next expiration.
timerthe timer used to initialize the values.

◆ __print_vector()

static void __print_vector ( list_head *  vector)
inlinestatic

Prints the addresses of all the timers inside a vector.

Parameters
vectorthe vector for which we print the details.

◆ __print_vector_base()

static void __print_vector_base ( tvec_base_t base)
inlinestatic

Prints the addresses of all the timers inside a vector base.

Parameters
basethe base for which we print the details.

◆ __sleep_data_alloc()

static sleep_data_t* __sleep_data_alloc ( void  )
inlinestatic

Allocates the memory for sleep_data.

Returns
a pointer to the allocated sleep_data.

◆ __sleep_data_dealloc()

static void __sleep_data_dealloc ( sleep_data_t sleep_data)
inlinestatic

Frees the memory of a sleep_data.

Parameters
sleep_datapointer to the sleep_data.

◆ __timer_cascate_base()

static void __timer_cascate_base ( tvec_base_t base)
inlinestatic

Move all timers from tv up one level.

Parameters
basethe base for which we move the timers.

Cascading means moving all dynamic timers in the base->tvn list into the proper lists of base->tvr; then, it returns a positive value, unless all base->tvn lists are now empty. If so, cascade() is invoked once more to replenish base->tvn with the timers included in a list of base->tvn, and so on.

◆ __timer_cascate_vector()

static void __timer_cascate_vector ( tvec_base_t base,
list_head *  current_vector 
)
inlinestatic

Move all timers from tv up one level.

Parameters
basethe base that contains the vector we want to cascate.
current_vectorthe vector we want to cascate.

◆ __timer_get_target_vector()

static list_head* __timer_get_target_vector ( tvec_base_t base,
struct timer_list timer 
)
inlinestatic

Select correct timer vector and position inside of it for the input timer index contains the position inside of the tv_index timer vector.

Parameters
basethe vector base we use to search the vector.
timerthe timer we use to determine the target vector.
Returns
a pointer to the target vector.

◆ __timer_list_alloc()

static struct timer_list* __timer_list_alloc ( void  )
inlinestatic

Allocates the memory for timer.

Returns
a pointer to the allocated timer.

◆ __timer_list_dealloc()

static void __timer_list_dealloc ( struct timer_list timer)
inlinestatic

Frees the memory of a timer.

Parameters
timerpointer to the timer.

◆ __tvec_base_init()

static void __tvec_base_init ( tvec_base_t base)
inlinestatic

Initializes the tvec_base.

Parameters
basethe base to initialize.

◆ __update_task_itimerval()

static void __update_task_itimerval ( int  which,
const struct itimerval timer 
)
static

Updates the timer for the given task.

Parameters
whichthe type of operation to execute.
timerthe timer to update.

◆ __values_to_itimerval()

static void __values_to_itimerval ( time_t  interval,
time_t  value,
struct itimerval timer 
)
inlinestatic

Transforms interval and value to the timer.

Parameters
intervalthe interval for periodic timer.
valuethe time until next expiration.
timerwhere the final values should be stored.

◆ add_timer()

void add_timer ( struct timer_list timer)

Add a new timer to the current CPU.

Parameters
timerThe timer to add.

◆ alarm_timeout()

static void alarm_timeout ( unsigned long  task_ptr)
inlinestatic

Function executed when the real_timer of a process expires, sends SIGALRM to process.

Parameters
task_ptrpointer to the process whos associated timer has expired.

◆ debug_timeout()

static void debug_timeout ( unsigned long  data)
inlinestatic

Standard debugging function callback.

Parameters
dataThe data.

◆ init_timer()

void init_timer ( struct timer_list timer)

Initializes a new timer struct.

Parameters
timerThe timer to initialize.

◆ real_timer_timeout()

static void real_timer_timeout ( unsigned long  task_ptr)
inlinestatic

Function executed when the real_timer of a process expires, sends SIGALRM to process.

Parameters
task_ptrpointer to the process whos associated timer has expired.

◆ remove_timer()

void remove_timer ( struct timer_list timer)

Removes a timer from the current CPU.

Parameters
timerThe timer to remove.

◆ sleep_timeout()

static void sleep_timeout ( unsigned long  data)
inlinestatic

Callback for when a sleep timer expires.

Parameters
dataCustom data stored in the timer.

◆ sys_alarm()

unsigned sys_alarm ( int  seconds)

Send signal to calling thread after desired seconds.

Parameters
secondsThe number of seconds in the interval
Returns
the number of seconds remaining until any previously scheduled alarm was due to be delivered, or zero if there was no previously scheduled alarm.

◆ sys_getitimer()

int sys_getitimer ( int  which,
struct itimerval curr_value 
)

Fills the structure pointed to by curr_value with the current setting for the timer specified by which.

Parameters
whichThe time domain (i.e., ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF)
curr_valueThere the values must be stored.
Returns
Zero on success, or a negative value indicating the error.

◆ sys_nanosleep()

int sys_nanosleep ( const struct timespec req,
struct timespec rem 
)

Suspends the execution of the calling thread.

Parameters
reqThe amount of time we want to sleep.
remThe remaining time we did not sleep.
Returns
If the call is interrupted by a signal handler, nanosleep() returns -1, sets errno to EINTR, and writes the remaining time into the structure pointed to by rem unless rem is NULL.

The execution is suspended until either at least the time specified in *req has elapsed, or the delivery of a signal that triggers the invocation of a handler in the calling thread or that terminates the process.

◆ sys_setitimer()

int sys_setitimer ( int  which,
const struct itimerval new_value,
struct itimerval old_value 
)

Arms or disarms the timer specified by which, by setting the timer to the value specified by new_value.

The system provides each process with three interval timers, each decrementing in a distinct time domain. When any timer expires, a signal is sent to the process, and the timer (potentially) restarts.

Parameters
whichThe time domain (i.e., ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF)
new_valueThe new value to set.
old_valueWhere the old value must be stored.
Returns
Zero on success, or a negative value indicating the error.

◆ timer_get_seconds()

uint64_t timer_get_seconds ( void  )

Returns the number of seconds since the system started its execution.

Returns
Value in seconds.

◆ timer_get_ticks()

unsigned long timer_get_ticks ( void  )

Returns the number of ticks since the system started its execution.

Returns
Value in ticks.

◆ timer_handler()

void timer_handler ( pt_regs f)

Handles the timer.

Parameters
fThe interrupt stack frame.

In this case, it's very simple: We increment the 'timer_ticks' variable every time the timer fires. By default, the timer fires 18.222 times per second. Why 18.222Hz? Some engineer at IBM must've been smoking something funky.

◆ timer_phase()

void timer_phase ( const uint32_t  hz)

Allows to set the timer phase to the given frequency.

Parameters
hzThe frequency to set.

◆ update_process_profiling_timer()

void update_process_profiling_timer ( task_struct proc)

Update the profiling timer and generate SIGPROF if it has expired.

Parameters
procThe process for which we must update the profiling.