blob: 8929f7e8f4523b09af0796842ed342060028de37 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _linux_POSIX_TIMERS_H
2#define _linux_POSIX_TIMERS_H
3
4#include <linux/spinlock.h>
5#include <linux/list.h>
6#include <linux/sched.h>
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00007#include <linux/timex.h>
John Stultz9a7adcf2011-01-11 09:54:33 -08008#include <linux/alarmtimer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Christoph Hellwig31ea70e2017-06-03 21:01:00 +020010struct siginfo;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +000011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012struct cpu_timer_list {
13 struct list_head entry;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +010014 u64 expires, incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 struct task_struct *task;
16 int firing;
17};
18
Richard Cochran81e294c2011-02-01 13:52:32 +000019/*
20 * Bit fields within a clockid:
21 *
22 * The most significant 29 bits hold either a pid or a file descriptor.
23 *
24 * Bit 2 indicates whether a cpu clock refers to a thread or a process.
25 *
26 * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
27 *
28 * A clockid is invalid if bits 2, 1, and 0 are all set.
29 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define CPUCLOCK_PID(clock) ((pid_t) ~((clock) >> 3))
31#define CPUCLOCK_PERTHREAD(clock) \
32 (((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
Richard Cochran0606f422011-02-01 13:52:35 +000033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define CPUCLOCK_PERTHREAD_MASK 4
35#define CPUCLOCK_WHICH(clock) ((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
36#define CPUCLOCK_CLOCK_MASK 3
37#define CPUCLOCK_PROF 0
38#define CPUCLOCK_VIRT 1
39#define CPUCLOCK_SCHED 2
40#define CPUCLOCK_MAX 3
Richard Cochran81e294c2011-02-01 13:52:32 +000041#define CLOCKFD CPUCLOCK_MAX
42#define CLOCKFD_MASK (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
45 ((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
46#define MAKE_THREAD_CPUCLOCK(tid, clock) \
47 MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
48
Richard Cochran0606f422011-02-01 13:52:35 +000049#define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD)
50#define CLOCKID_TO_FD(clk) ((unsigned int) ~((clk) >> 3))
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* POSIX.1b interval timer structure. */
53struct k_itimer {
54 struct list_head list; /* free/ allocate list */
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040055 struct hlist_node t_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 spinlock_t it_lock;
57 clockid_t it_clock; /* which timer type */
58 timer_t it_id; /* timer id */
59 int it_overrun; /* overrun on pending signal */
60 int it_overrun_last; /* overrun on last delivered signal */
Thomas Gleixner2a698972006-01-09 20:52:28 -080061 int it_requeue_pending; /* waiting to requeue this timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define REQUEUE_PENDING 1
63 int it_sigev_notify; /* notify word of sigevent struct */
Oleg Nesterov27af4242008-12-01 14:18:13 -080064 struct signal_struct *it_signal;
65 union {
66 struct pid *it_pid; /* pid of process to send signal to */
67 struct task_struct *it_process; /* for clock_nanosleep */
68 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct sigqueue *sigq; /* signal queue entry. */
70 union {
71 struct {
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -080072 struct hrtimer timer;
73 ktime_t interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 } real;
75 struct cpu_timer_list cpu;
76 struct {
John Stultz9e264762011-08-10 12:09:24 -070077 struct alarm alarmtimer;
78 ktime_t interval;
79 } alarm;
Eric Dumazet8af08872011-05-24 11:12:58 +020080 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 } it;
82};
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084struct k_clock {
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -070085 int (*clock_getres) (const clockid_t which_clock, struct timespec64 *tp);
Richard Cochran1e6d7672011-02-01 13:50:58 +000086 int (*clock_set) (const clockid_t which_clock,
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -070087 const struct timespec64 *tp);
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -070088 int (*clock_get) (const clockid_t which_clock, struct timespec64 *tp);
Richard Cochranf1f1d5e2011-02-01 13:52:26 +000089 int (*clock_adj) (const clockid_t which_clock, struct timex *tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int (*timer_create) (struct k_itimer *timer);
Thomas Gleixner2a698972006-01-09 20:52:28 -080091 int (*nsleep) (const clockid_t which_clock, int flags,
Deepa Dinamaniad196382017-03-26 12:04:18 -070092 struct timespec64 *, struct timespec __user *);
Toyo Abe1711ef32006-09-29 02:00:28 -070093 long (*nsleep_restart) (struct restart_block *restart_block);
Deepa Dinamani5f252b32017-03-26 12:04:17 -070094 int (*timer_set) (struct k_itimer *timr, int flags,
95 struct itimerspec64 *new_setting,
96 struct itimerspec64 *old_setting);
97 int (*timer_del) (struct k_itimer *timr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define TIMER_RETRY 1
Deepa Dinamani5f252b32017-03-26 12:04:17 -070099 void (*timer_get) (struct k_itimer *timr,
100 struct itimerspec64 *cur_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300103extern const struct k_clock clock_posix_cpu;
104extern const struct k_clock clock_posix_dynamic;
105extern const struct k_clock clock_process;
106extern const struct k_clock clock_thread;
107extern const struct k_clock alarm_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* function to call to trigger timer event */
110int posix_timer_event(struct k_itimer *timr, int si_private);
111
Thomas Gleixner2a698972006-01-09 20:52:28 -0800112void posix_cpu_timer_schedule(struct k_itimer *timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Thomas Gleixner2a698972006-01-09 20:52:28 -0800114void run_posix_cpu_timers(struct task_struct *task);
115void posix_cpu_timers_exit(struct task_struct *task);
116void posix_cpu_timers_exit_group(struct task_struct *task);
Thomas Gleixner2a698972006-01-09 20:52:28 -0800117void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100118 u64 *newval, u64 *oldval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Toyo Abe1711ef32006-09-29 02:00:28 -0700120long clock_nanosleep_restart(struct restart_block *restart_block);
121
Jiri Slaby5ab46b32009-08-28 14:05:12 +0200122void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
Frank Mayharf06febc2008-09-12 09:54:39 -0700123
Christoph Hellwig31ea70e2017-06-03 21:01:00 +0200124void do_schedule_next_timer(struct siginfo *info);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#endif