MentOS  0.8.0
The Mentoring Operating System
timer.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include "kernel.h"
9 #include "stdint.h"
10 #include "sys/list_head.h"
11 #include "klib/spinlock.h"
12 #include "process/process.h"
13 #include "time.h"
14 
18 #define ENABLE_REAL_TIMER_SYSTEM
19 
22 //#define ENABLE_REAL_TIMER_SYSTEM_DUMP
23 
25 #define ITIMER_REAL 0
27 #define ITIMER_VIRTUAL 1
30 #define ITIMER_PROF 2
31 
33 #define TICKS_PER_SECOND 1193
34 
42 void timer_handler(pt_regs *f);
43 
45 void timer_install(void);
46 
50 
53 unsigned long timer_get_ticks(void);
54 
57 void timer_phase(const uint32_t hz);
58 
59 // ===============================================================================
60 // Per-CPU timer vectors
61 
62 #ifdef ENABLE_REAL_TIMER_SYSTEM
63 
65 #define TVN_COUNT 4
67 #define TVN_BITS 6
69 #define TVR_BITS 8
71 #define TVN_SIZE (1 << TVN_BITS)
73 #define TVR_SIZE (1 << TVR_BITS)
75 #define TVN_MASK (TVN_SIZE - 1)
77 #define TVR_MASK (TVR_SIZE - 1)
79 #define TIMER_TICKS_BITS(tv) (TVR_BITS + TVN_BITS * (tv))
81 #define TIMER_TICKS(tv) (1 << TIMER_TICKS_BITS(tv))
82 
83 #endif
84 
86 typedef struct tvec_base_s {
91 #ifdef ENABLE_REAL_TIMER_SYSTEM
93  unsigned long timer_ticks;
94 
96  list_head tvr[TVR_SIZE];
102  list_head tvn[TVN_COUNT][TVN_SIZE];
103 
104 #else
106  struct list_head list;
107 #endif
108 
110 
113 struct timer_list {
117  struct list_head entry;
119  unsigned long expires;
121  void (*function)(unsigned long);
123  unsigned long data;
126 };
127 
129 void dynamic_timers_install(void);
130 
133 void init_timer(struct timer_list *timer);
134 
136 void run_timer_softirq(void);
137 
140 void add_timer(struct timer_list *timer);
141 
144 void remove_timer(struct timer_list *timer);
145 
157 int sys_nanosleep(const struct timespec *req, struct timespec *rem);
158 
164 unsigned sys_alarm(int seconds);
165 
170 int sys_getitimer(int which, struct itimerval *curr_value);
171 
182 int sys_setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);
183 
Kernel generic data structure and functions.
Process data structures and functions.
atomic_t spinlock_t
Spinlock structure.
Definition: spinlock.h:16
Standard integer data-types.
unsigned int uint64_t
Define the unsigned 64-bit integer.
Definition: stdint.h:12
unsigned int uint32_t
Define the unsigned 32-bit integer.
Definition: stdint.h:18
Rappresents a time interval.
Definition: time.h:53
Interrupt stack frame.
Definition: kernel.h:24
this is our task object. Every process in the system has this, and it holds a lot of information....
Definition: process.h:82
Represents the request to execute a function in the future, also known as timer.
Definition: timer.h:113
tvec_base_t * base
Pointer to the structure containing all the other related timers.
Definition: timer.h:125
spinlock_t lock
Protects the access to the timer.
Definition: timer.h:115
struct list_head entry
Lists of timers are mantained using the list_head.
Definition: timer.h:117
unsigned long data
Custom data to be passed to the timer function.
Definition: timer.h:123
unsigned long expires
Ticks value when the timer has to expire.
Definition: timer.h:119
Specify intervals of time with nanosecond precision.
Definition: time.h:59
Contains all the timers of a single CPU.
Definition: timer.h:86
spinlock_t lock
Lock for the timer data structure.
Definition: timer.h:88
list_head tvn[TVN_COUNT][TVN_SIZE]
Definition: timer.h:102
struct timer_list * running_timer
Points to the dynamic timer that is currently handled by the CPU.
Definition: timer.h:90
list_head tvr[TVR_SIZE]
Lists of root timers that will expires in the next 255 ticks.
Definition: timer.h:96
unsigned long timer_ticks
The earliest expiration time of the dynamic timers yet to be checked.
Definition: timer.h:93
Time-related functions.
#define TVN_COUNT
Number of normal timer vectors.
Definition: timer.h:65
int sys_nanosleep(const struct timespec *req, struct timespec *rem)
Suspends the execution of the calling thread.
Definition: timer.c:603
void init_timer(struct timer_list *timer)
Initializes a new timer struct.
Definition: timer.c:314
void timer_install(void)
Sets up the system clock by installing the timer handler into IRQ0.
Definition: timer.c:111
#define TVN_SIZE
Number of headers in a normal timer vector.
Definition: timer.h:71
uint64_t timer_get_seconds(void)
Returns the number of seconds since the system started its execution.
Definition: timer.c:123
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_valu...
Definition: timer.c:680
#define TVR_SIZE
Number of headers in a root timer vector.
Definition: timer.h:73
void dynamic_timers_install(void)
Initialize dynamic timer system.
Definition: timer.c:451
void remove_timer(struct timer_list *timer)
Removes a timer from the current CPU.
Definition: timer.c:340
unsigned long timer_get_ticks(void)
Returns the number of ticks since the system started its execution.
Definition: timer.c:128
unsigned sys_alarm(int seconds)
Send signal to calling thread after desired seconds.
Definition: timer.c:626
void run_timer_softirq(void)
Updates the timer data structures.
Definition: timer.c:461
void add_timer(struct timer_list *timer)
Add a new timer to the current CPU.
Definition: timer.c:325
void timer_handler(pt_regs *f)
Handles the timer.
Definition: timer.c:93
void timer_phase(const uint32_t hz)
Allows to set the timer phase to the given frequency.
Definition: timer.c:81
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 whic...
Definition: timer.c:662
struct tvec_base_s tvec_base_t
Contains all the timers of a single CPU.
void update_process_profiling_timer(task_struct *proc)
Update the profiling timer and generate SIGPROF if it has expired.
Definition: timer.c:733