📄 linux_0
字号:
74 if (new_value)
75 *bitmap_base++ |= mask;
76 else
77 *bitmap_base++ &= ~mask;
78 }
79 }
80
81 /* Check for set bits in BITMAP starting at BASE, going to EXTENT. */
82 asmlinkage int check_bitmap(unsigned long *bitmap, short base, short extent)
83 {
84 int mask;
85 unsigned long *bitmap_base = bitmap + (base >> 5);
86 unsigned short low_index = base & 0x1f;
87 int length = low_index + extent;
88
89 if (low_index != 0) {
90 mask = (~0 << low_index);
91 if (length < 32)
92 mask &= ~(~0 << length);
93 if (*bitmap_base++ & mask)
94 return 1;
95 length -= 32;
96 }
97 while (length >= 32) {
98 if (*bitmap_base++ != 0)
99 return 1;
100 length -= 32;
101 }
102
103 if (length > 0) {
104 mask = ~(~0 << length);
105 if (*bitmap_base++ & mask)
106 return 1;
107 }
108 return 0;
109 }
110
111 /*
112 * this changes the io permissions bitmap in the current task.
113 */
114 asmlinkage int sys_ioperm(unsigned long from, unsigned long num, int turn_on)
115 {
116 if (from + num <= from)
117 return -EINVAL;
118 if (from + num > IO_BITMAP_SIZE*32)
119 return -EINVAL;
120 if (!suser())
121 return -EPERM;
122
123 #ifdef IODEBUG
124 printk("io: from=%d num=%d %s\n", from, num, (turn_on ? "on" : "off"));
125 #endif
126 set_bitmap((unsigned long *)current->tss.io_bitmap, from, num, !turn_on);
127 return 0;
128 }
129
130 unsigned int *stack;
131
132 /*
133 * sys_iopl has to be used when you want to access the IO ports
134 * beyond the 0x3ff range: to get the full 65536 ports bitmapped
135 * you'd need 8kB of bitmaps/process, which is a bit excessive.
136 *
137 * Here we just change the eflags value on the stack: we allow
138 * only the super-user to do it. This depends on the stack-layout
139 * on system-call entry - see also fork() and the signal handling
140 * code.
141 */
142 asmlinkage int sys_iopl(long ebx,long ecx,long edx,
143 long esi, long edi, long ebp, long eax, long ds,
144 long es, long fs, long gs, long orig_eax,
145 long eip,long cs,long eflags,long esp,long ss)
146 {
147 unsigned int level = ebx;
148
149 if (level > 3)
150 return -EINVAL;
151 if (!suser())
152 return -EPERM;
153 *(&eflags) = (eflags & 0xffffcfff) | (level << 12);
154 return 0;
155 }
156
157
158 void snarf_region(unsigned int from, unsigned int num)
159 {
160 if (from > IO_BITMAP_SIZE*32)
161 return;
162 if (from + num > IO_BITMAP_SIZE*32)
163 num = IO_BITMAP_SIZE*32 - from;
164 set_bitmap(ioport_registrar, from, num, 1);
165 return;
166 }
167
168 int check_region(unsigned int from, unsigned int num)
169 {
170 if (from > IO_BITMAP_SIZE*32)
171 return 0;
172 if (from + num > IO_BITMAP_SIZE*32)
173 num = IO_BITMAP_SIZE*32 - from;
174 return check_bitmap(ioport_registrar, from, num);
175 }
176
177 /* Called from init/main.c to reserve IO ports. */
178 void reserve_setup(char *str, int *ints)
179 {
180 int i;
181
182 for (i = 1; i < ints[0]; i += 2)
183 snarf_region(ints[i], ints[i+1]);
184 }
185
-------------------------------
1 /*
2 * linux/kernel/irq.c
3 *
4 * Copyright (C) 1992 Linus Torvalds
5 *
6 * This file contains the code used by various IRQ handling routines:
7 * asking for different IRQ's should be done through these routines
8 * instead of just grabbing them. Thus setups with different IRQ numbers
9 * shouldn't result in any weird surprises, and installing new handlers
10 * should be easier.
11 */
12
13 /*
14 * IRQ's are in fact implemented a bit like signal handlers for the kernel.
15 * The same sigaction struct is used, and with similar semantics (ie there
16 * is a SA_INTERRUPT flag etc). Naturally it's not a 1:1 relation, but there
17 * are similarities.
18 *
19 * sa_handler(int irq_NR) is the default function called.
20 * sa_mask is 0 if nothing uses this IRQ
21 * sa_flags contains various info: SA_INTERRUPT etc
22 * sa_restorer is the unused
23 */
24
25 #include <linux/ptrace.h>
26 #include <linux/errno.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/signal.h>
29 #include <linux/sched.h>
30 #include <linux/interrupt.h>
31
32 #include <asm/system.h>
33 #include <asm/io.h>
34 #include <asm/irq.h>
35
36 #define CR0_NE 32
37
38 static unsigned char cache_21 = 0xff;
39 static unsigned char cache_A1 = 0xff;
40
41 unsigned long intr_count = 0;
42 unsigned long bh_active = 0;
43 unsigned long bh_mask = 0xFFFFFFFF;
44 struct bh_struct bh_base[32];
45
46 void disable_irq(unsigned int irq_nr)
47 {
48 unsigned long flags;
49 unsigned char mask;
50
51 mask = 1 << (irq_nr & 7);
52 save_flags(flags);
53 if (irq_nr < 8) {
54 cli();
55 cache_21 |= mask;
56 outb(cache_21,0x21);
57 restore_flags(flags);
58 return;
59 }
60 cli();
61 cache_A1 |= mask;
62 outb(cache_A1,0xA1);
63 restore_flags(flags);
64 }
65
66 void enable_irq(unsigned int irq_nr)
67 {
68 unsigned long flags;
69 unsigned char mask;
70
71 mask = ~(1 << (irq_nr & 7));
72 save_flags(flags);
73 if (irq_nr < 8) {
74 cli();
75 cache_21 &= mask;
76 outb(cache_21,0x21);
77 restore_flags(flags);
78 return;
79 }
80 cli();
81 cache_A1 &= mask;
82 outb(cache_A1,0xA1);
83 restore_flags(flags);
84 }
85
86 /*
87 * do_bottom_half() runs at normal kernel priority: all interrupts
88 * enabled. do_bottom_half() is atomic with respect to itself: a
89 * bottom_half handler need not be re-entrant.
90 */
91 asmlinkage void do_bottom_half(void)
92 {
93 unsigned long active;
94 unsigned long mask, left;
95 struct bh_struct *bh;
96
97 bh = bh_base;
98 active = bh_active & bh_mask;
99 for (mask = 1, left = ~0 ; left & active ; bh++,mask += mask,left += left) {
100 if (mask & active) {
101 void (*fn)(void *);
102 bh_active &= ~mask;
103 fn = bh->routine;
104 if (!fn)
105 goto bad_bh;
106 fn(bh->data);
107 }
108 }
109 return;
110 bad_bh:
111 printk ("irq.c:bad bottom half entry\n");
112 }
113
114 /*
115 * This builds up the IRQ handler stubs using some ugly macros in irq.h
116 *
117 * These macros create the low-level assembly IRQ routines that do all
118 * the operations that are needed to keep the AT interrupt-controller
119 * happy. They are also written to be fast - and to disable interrupts
120 * as little as humanly possible.
121 *
122 * NOTE! These macros expand to three different handlers for each line: one
123 * complete handler that does all the fancy stuff (including signal handling),
124 * and one fast handler that is meant for simple IRQ's that want to be
125 * atomic. The specific handler is chosen depending on the SA_INTERRUPT
126 * flag when installing a handler. Finally, one "bad interrupt" handler, that
127 * is used when no handler is present.
128 */
129 BUILD_IRQ(FIRST,0,0x01)
130 BUILD_IRQ(FIRST,1,0x02)
131 BUILD_IRQ(FIRST,2,0x04)
132 BUILD_IRQ(FIRST,3,0x08)
133 BUILD_IRQ(FIRST,4,0x10)
134 BUILD_IRQ(FIRST,5,0x20)
135 BUILD_IRQ(FIRST,6,0x40)
136 BUILD_IRQ(FIRST,7,0x80)
137 BUILD_IRQ(SECOND,8,0x01)
138 BUILD_IRQ(SECOND,9,0x02)
139 BUILD_IRQ(SECOND,10,0x04)
140 BUILD_IRQ(SECOND,11,0x08)
141 BUILD_IRQ(SECOND,12,0x10)
142 BUILD_IRQ(SECOND,13,0x20)
143 BUILD_IRQ(SECOND,14,0x40)
144 BUILD_IRQ(SECOND,15,0x80)
145
146 /*
147 * Pointers to the low-level handlers: first the general ones, then the
148 * fast ones, then the bad ones.
149 */
150 static void (*interrupt[16])(void) = {
151 IRQ0_interrupt, IRQ1_interrupt, IRQ2_interrupt, IRQ3_interrupt,
152 IRQ4_interrupt, IRQ5_interrupt, IRQ6_interrupt, IRQ7_interrupt,
153 IRQ8_interrupt, IRQ9_interrupt, IRQ10_interrupt, IRQ11_interrupt,
154 IRQ12_interrupt, IRQ13_interrupt, IRQ14_interrupt, IRQ15_interrupt
155 };
156
157 static void (*fast_interrupt[16])(void) = {
158 fast_IRQ0_interrupt, fast_IRQ1_interrupt,
159 fast_IRQ2_interrupt, fast_IRQ3_interrupt,
160 fast_IRQ4_interrupt, fast_IRQ5_interrupt,
161 fast_IRQ6_interrupt, fast_IRQ7_interrupt,
162 fast_IRQ8_interrupt, fast_IRQ9_interrupt,
163 fast_IRQ10_interrupt, fast_IRQ11_interrupt,
164 fast_IRQ12_interrupt, fast_IRQ13_interrupt,
165 fast_IRQ14_interrupt, fast_IRQ15_interrupt
166 };
167
168 static void (*bad_interrupt[16])(void) = {
169 bad_IRQ0_interrupt, bad_IRQ1_interrupt,
170 bad_IRQ2_interrupt, bad_IRQ3_interrupt,
171 bad_IRQ4_interrupt, bad_IRQ5_interrupt,
172 bad_IRQ6_interrupt, bad_IRQ7_interrupt,
173 bad_IRQ8_interrupt, bad_IRQ9_interrupt,
174 bad_IRQ10_interrupt, bad_IRQ11_interrupt,
175 bad_IRQ12_interrupt, bad_IRQ13_interrupt,
176 bad_IRQ14_interrupt, bad_IRQ15_interrupt
177 };
178
179 /*
180 * Initial irq handlers.
181 */
182 static struct sigaction irq_sigaction[16] = {
183 { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
184 { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
185 { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
186 { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
187 { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
188 { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
189 { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL },
190 { NULL, 0, 0, NULL }, { NULL, 0, 0, NULL }
191 };
192
193 /*
194 * do_IRQ handles IRQ's that have been installed without the
195 * SA_INTERRUPT flag: it uses the full signal-handling return
196 * and runs with other interrupts enabled. All relatively slow
197 * IRQ's should use this format: notably the keyboard/timer
198 * routines.
199 */
200 asmlinkage void do_IRQ(int irq, struct pt_regs * regs)
201 {
202 struct sigaction * sa = irq + irq_sigaction;
203
204 kstat.interrupts[irq]++;
205 sa->sa_handler((int) regs);
206 }
207
208 /*
209 * do_fast_IRQ handles IRQ's that don't need the fancy interrupt return
210 * stuff - the handler is also running with interrupts disabled unless
211 * it explicitly enables them later.
212 */
213 asmlinkage void do_fast_IRQ(int irq)
214 {
215 struct sigaction * sa = irq + irq_sigaction;
216
217 kstat.interrupts[irq]++;
218 sa->sa_handler(irq);
219 }
220
221 int irqaction(unsigned int irq, struct sigaction * new_sa)
222 {
223 struct sigaction * sa;
224 unsigned long flags;
225
226 if (irq > 15)
227 return -EINVAL;
228 sa = irq + irq_sigaction;
229 if (sa->sa_mask)
230 return -EBUSY;
231 if (!new_sa->sa_handler)
232 return -EINVAL;
233 save_flags(flags);
234 cli();
235 *sa = *new_sa;
236 sa->sa_mask = 1;
237 if (sa->sa_flags & SA_INTERRUPT)
238 set_intr_gate(0x20+irq,fast_interrupt[irq]);
239 else
240 set_intr_gate(0x20+irq,interrupt[irq]);
241 if (irq < 8) {
242 cache_21 &= ~(1<<irq);
243 outb(cache_21,0x21);
244 } else {
245 cache_21 &= ~(1<<2);
246 cache_A1 &= ~(1<<(irq-8));
247 outb(cache_21,0x21);
248 outb(cache_A1,0xA1);
249 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -