only check local benchmark dir if it exists
[minix.git] / kernel / system / do_irqctl.c
blob36c0a0e36aa2a555959b8be718ec3c11cdd6754a
1 /* The kernel call implemented in this file:
2 * m_type: SYS_IRQCTL
4 * The parameters for this kernel call are:
5 * m5_s1: IRQ_REQUEST (control operation to perform)
6 * m5_s2: IRQ_VECTOR (irq line that must be controlled)
7 * m5_i1: IRQ_POLICY (irq policy allows reenabling interrupts)
8 * m5_l3: IRQ_HOOK_ID (provides index to be returned on interrupt)
9 * ,, ,, (returns index of irq hook assigned at kernel)
12 #include "../system.h"
14 #include <minix/endpoint.h>
16 #if USE_IRQCTL
18 FORWARD _PROTOTYPE(int generic_handler, (irq_hook_t *hook));
20 /*===========================================================================*
21 * do_irqctl *
22 *===========================================================================*/
23 PUBLIC int do_irqctl(struct proc * caller, message * m_ptr)
25 /* Dismember the request message. */
26 int irq_vec;
27 int irq_hook_id;
28 int notify_id;
29 int r = OK;
30 int i;
31 irq_hook_t *hook_ptr;
32 struct priv *privp;
34 /* Hook identifiers start at 1 and end at NR_IRQ_HOOKS. */
35 irq_hook_id = (unsigned) m_ptr->IRQ_HOOK_ID - 1;
36 irq_vec = (unsigned) m_ptr->IRQ_VECTOR;
38 /* See what is requested and take needed actions. */
39 switch(m_ptr->IRQ_REQUEST) {
41 /* Enable or disable IRQs. This is straightforward. */
42 case IRQ_ENABLE:
43 case IRQ_DISABLE:
44 if (irq_hook_id >= NR_IRQ_HOOKS || irq_hook_id < 0 ||
45 irq_hooks[irq_hook_id].proc_nr_e == NONE) return(EINVAL);
46 if (irq_hooks[irq_hook_id].proc_nr_e != m_ptr->m_source) return(EPERM);
47 if (m_ptr->IRQ_REQUEST == IRQ_ENABLE) {
48 enable_irq(&irq_hooks[irq_hook_id]);
50 else
51 disable_irq(&irq_hooks[irq_hook_id]);
52 break;
54 /* Control IRQ policies. Set a policy and needed details in the IRQ table.
55 * This policy is used by a generic function to handle hardware interrupts.
57 case IRQ_SETPOLICY:
59 /* Check if IRQ line is acceptable. */
60 if (irq_vec < 0 || irq_vec >= NR_IRQ_VECTORS) return(EINVAL);
62 privp= priv(caller);
63 if (!privp)
65 kprintf("do_irqctl: no priv structure!\n");
66 return EPERM;
68 if (privp->s_flags & CHECK_IRQ)
70 for (i= 0; i<privp->s_nr_irq; i++)
72 if (irq_vec == privp->s_irq_tab[i])
73 break;
75 if (i >= privp->s_nr_irq)
77 kprintf(
78 "do_irqctl: IRQ check failed for proc %d, IRQ %d\n",
79 m_ptr->m_source, irq_vec);
80 return EPERM;
84 /* Find a free IRQ hook for this mapping. */
85 hook_ptr = NULL;
86 for (irq_hook_id=0; irq_hook_id<NR_IRQ_HOOKS; irq_hook_id++) {
87 if (irq_hooks[irq_hook_id].proc_nr_e == NONE) {
88 hook_ptr = &irq_hooks[irq_hook_id]; /* free hook */
89 break;
92 if (hook_ptr == NULL) return(ENOSPC);
94 /* When setting a policy, the caller must provide an identifier that
95 * is returned on the notification message if a interrupt occurs.
97 notify_id = (unsigned) m_ptr->IRQ_HOOK_ID;
98 if (notify_id > CHAR_BIT * sizeof(irq_id_t) - 1) return(EINVAL);
100 /* Install the handler. */
101 hook_ptr->proc_nr_e = m_ptr->m_source; /* process to notify */
102 hook_ptr->notify_id = notify_id; /* identifier to pass */
103 hook_ptr->policy = m_ptr->IRQ_POLICY; /* policy for interrupts */
104 put_irq_handler(hook_ptr, irq_vec, generic_handler);
106 /* Return index of the IRQ hook in use. */
107 m_ptr->IRQ_HOOK_ID = irq_hook_id + 1;
108 break;
110 case IRQ_RMPOLICY:
111 if (irq_hook_id < 0 || irq_hook_id >= NR_IRQ_HOOKS ||
112 irq_hooks[irq_hook_id].proc_nr_e == NONE) {
113 return(EINVAL);
114 } else if (m_ptr->m_source != irq_hooks[irq_hook_id].proc_nr_e) {
115 return(EPERM);
117 /* Remove the handler and return. */
118 rm_irq_handler(&irq_hooks[irq_hook_id]);
119 break;
121 default:
122 r = EINVAL; /* invalid IRQ_REQUEST */
124 return(r);
127 /*===========================================================================*
128 * generic_handler *
129 *===========================================================================*/
130 PRIVATE int generic_handler(hook)
131 irq_hook_t *hook;
133 /* This function handles hardware interrupt in a simple and generic way. All
134 * interrupts are transformed into messages to a driver. The IRQ line will be
135 * reenabled if the policy says so.
137 int proc_nr;
139 vmassert(intr_disabled());
141 /* As a side-effect, the interrupt handler gathers random information by
142 * timestamping the interrupt events. This is used for /dev/random.
144 get_randomness(&krandom, hook->irq);
146 /* Check if the handler is still alive.
147 * If it's dead, this should never happen, as processes that die
148 * automatically get their interrupt hooks unhooked.
150 if(!isokendpt(hook->proc_nr_e, &proc_nr))
151 minix_panic("invalid interrupt handler", hook->proc_nr_e);
153 /* Add a bit for this interrupt to the process' pending interrupts. When
154 * sending the notification message, this bit map will be magically set
155 * as an argument.
157 priv(proc_addr(proc_nr))->s_int_pending |= (1 << hook->notify_id);
159 /* Build notification message and return. */
160 vmassert(intr_disabled());
161 mini_notify(proc_addr(HARDWARE), hook->proc_nr_e);
162 return(hook->policy & IRQ_REENABLE);
165 #endif /* USE_IRQCTL */