Rev 413 | Rev 1962 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 413 | Rev 432 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * Copyright (C) 2005 Sergey Bondari |
3 | * Copyright (C) 2005 Sergey Bondari |
4 | * All rights reserved. |
4 | * All rights reserved. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
8 | * are met: |
9 | * |
9 | * |
10 | * - Redistributions of source code must retain the above copyright |
10 | * - Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * - Redistributions in binary form must reproduce the above copyright |
12 | * - Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
14 | * documentation and/or other materials provided with the distribution. |
15 | * - The name of the author may not be used to endorse or promote products |
15 | * - The name of the author may not be used to endorse or promote products |
16 | * derived from this software without specific prior written permission. |
16 | * derived from this software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | #ifndef __ia32_ASM_H__ |
30 | #ifndef __ia32_ASM_H__ |
31 | #define __ia32_ASM_H__ |
31 | #define __ia32_ASM_H__ |
32 | 32 | ||
33 | #include <arch/types.h> |
33 | #include <arch/types.h> |
34 | #include <config.h> |
34 | #include <config.h> |
35 | 35 | ||
36 | extern __u32 interrupt_handler_size; |
36 | extern __u32 interrupt_handler_size; |
37 | 37 | ||
38 | extern void paging_on(void); |
38 | extern void paging_on(void); |
39 | 39 | ||
40 | extern void interrupt_handlers(void); |
40 | extern void interrupt_handlers(void); |
41 | 41 | ||
42 | extern void enable_l_apic_in_msr(void); |
42 | extern void enable_l_apic_in_msr(void); |
43 | 43 | ||
44 | 44 | ||
45 | void asm_delay_loop(__u32 t); |
45 | void asm_delay_loop(__u32 t); |
46 | void asm_fake_loop(__u32 t); |
46 | void asm_fake_loop(__u32 t); |
47 | 47 | ||
48 | 48 | ||
49 | /** Halt CPU |
49 | /** Halt CPU |
50 | * |
50 | * |
51 | * Halt the current CPU until interrupt event. |
51 | * Halt the current CPU until interrupt event. |
52 | */ |
52 | */ |
53 | static inline void cpu_halt(void) { __asm__("hlt\n"); }; |
53 | static inline void cpu_halt(void) { __asm__("hlt\n"); }; |
54 | static inline void cpu_sleep(void) { __asm__("hlt\n"); }; |
54 | static inline void cpu_sleep(void) { __asm__("hlt\n"); }; |
55 | 55 | ||
56 | /** Read CR2 |
56 | /** Read CR2 |
57 | * |
57 | * |
58 | * Return value in CR2 |
58 | * Return value in CR2 |
59 | * |
59 | * |
60 | * @return Value read. |
60 | * @return Value read. |
61 | */ |
61 | */ |
62 | static inline __u32 read_cr2(void) { __u32 v; __asm__ volatile ("movl %%cr2,%0\n" : "=r" (v)); return v; } |
62 | static inline __u32 read_cr2(void) { __u32 v; __asm__ volatile ("movl %%cr2,%0\n" : "=r" (v)); return v; } |
63 | 63 | ||
64 | /** Write CR3 |
64 | /** Write CR3 |
65 | * |
65 | * |
66 | * Write value to CR3. |
66 | * Write value to CR3. |
67 | * |
67 | * |
68 | * @param v Value to be written. |
68 | * @param v Value to be written. |
69 | */ |
69 | */ |
70 | static inline void write_cr3(__u32 v) { __asm__ volatile ("movl %0,%%cr3\n" : : "r" (v)); } |
70 | static inline void write_cr3(__u32 v) { __asm__ volatile ("movl %0,%%cr3\n" : : "r" (v)); } |
71 | 71 | ||
72 | /** Read CR3 |
72 | /** Read CR3 |
73 | * |
73 | * |
74 | * Return value in CR3 |
74 | * Return value in CR3 |
75 | * |
75 | * |
76 | * @return Value read. |
76 | * @return Value read. |
77 | */ |
77 | */ |
78 | static inline __u32 read_cr3(void) { __u32 v; __asm__ volatile ("movl %%cr3,%0\n" : "=r" (v)); return v; } |
78 | static inline __u32 read_cr3(void) { __u32 v; __asm__ volatile ("movl %%cr3,%0\n" : "=r" (v)); return v; } |
79 | 79 | ||
80 | /** Byte to port |
80 | /** Byte to port |
81 | * |
81 | * |
82 | * Output byte to port |
82 | * Output byte to port |
83 | * |
83 | * |
84 | * @param port Port to write to |
84 | * @param port Port to write to |
85 | * @param val Value to write |
85 | * @param val Value to write |
86 | */ |
86 | */ |
87 | static inline void outb(__u16 port, __u8 val) { __asm__ volatile ("outb %b0, %w1\n" : : "a" (val), "d" (port) ); } |
87 | static inline void outb(__u16 port, __u8 val) { __asm__ volatile ("outb %b0, %w1\n" : : "a" (val), "d" (port) ); } |
88 | 88 | ||
89 | /** Word to port |
89 | /** Word to port |
90 | * |
90 | * |
91 | * Output word to port |
91 | * Output word to port |
92 | * |
92 | * |
93 | * @param port Port to write to |
93 | * @param port Port to write to |
94 | * @param val Value to write |
94 | * @param val Value to write |
95 | */ |
95 | */ |
96 | static inline void outw(__u16 port, __u16 val) { __asm__ volatile ("outw %w0, %w1\n" : : "a" (val), "d" (port) ); } |
96 | static inline void outw(__u16 port, __u16 val) { __asm__ volatile ("outw %w0, %w1\n" : : "a" (val), "d" (port) ); } |
97 | 97 | ||
98 | /** Double word to port |
98 | /** Double word to port |
99 | * |
99 | * |
100 | * Output double word to port |
100 | * Output double word to port |
101 | * |
101 | * |
102 | * @param port Port to write to |
102 | * @param port Port to write to |
103 | * @param val Value to write |
103 | * @param val Value to write |
104 | */ |
104 | */ |
105 | static inline void outl(__u16 port, __u32 val) { __asm__ volatile ("outl %l0, %w1\n" : : "a" (val), "d" (port) ); } |
105 | static inline void outl(__u16 port, __u32 val) { __asm__ volatile ("outl %l0, %w1\n" : : "a" (val), "d" (port) ); } |
106 | 106 | ||
107 | /** Byte from port |
107 | /** Byte from port |
108 | * |
108 | * |
109 | * Get byte from port |
109 | * Get byte from port |
110 | * |
110 | * |
111 | * @param port Port to read from |
111 | * @param port Port to read from |
112 | * @return Value read |
112 | * @return Value read |
113 | */ |
113 | */ |
114 | static inline __u8 inb(__u16 port) { __u8 val; __asm__ volatile ("inb %w1, %b0 \n" : "=a" (val) : "d" (port) ); return val; } |
114 | static inline __u8 inb(__u16 port) { __u8 val; __asm__ volatile ("inb %w1, %b0 \n" : "=a" (val) : "d" (port) ); return val; } |
115 | 115 | ||
116 | /** Word from port |
116 | /** Word from port |
117 | * |
117 | * |
118 | * Get word from port |
118 | * Get word from port |
119 | * |
119 | * |
120 | * @param port Port to read from |
120 | * @param port Port to read from |
121 | * @return Value read |
121 | * @return Value read |
122 | */ |
122 | */ |
123 | static inline __u16 inw(__u16 port) { __u16 val; __asm__ volatile ("inw %w1, %w0 \n" : "=a" (val) : "d" (port) ); return val; } |
123 | static inline __u16 inw(__u16 port) { __u16 val; __asm__ volatile ("inw %w1, %w0 \n" : "=a" (val) : "d" (port) ); return val; } |
124 | 124 | ||
125 | /** Double word from port |
125 | /** Double word from port |
126 | * |
126 | * |
127 | * Get double word from port |
127 | * Get double word from port |
128 | * |
128 | * |
129 | * @param port Port to read from |
129 | * @param port Port to read from |
130 | * @return Value read |
130 | * @return Value read |
131 | */ |
131 | */ |
132 | static inline __u32 inl(__u16 port) { __u32 val; __asm__ volatile ("inl %w1, %l0 \n" : "=a" (val) : "d" (port) ); return val; } |
132 | static inline __u32 inl(__u16 port) { __u32 val; __asm__ volatile ("inl %w1, %l0 \n" : "=a" (val) : "d" (port) ); return val; } |
133 | 133 | ||
134 | /** Enable interrupts. |
134 | /** Enable interrupts. |
135 | * |
135 | * |
136 | * Enable interrupts and return previous |
136 | * Enable interrupts and return previous |
137 | * value of EFLAGS. |
137 | * value of EFLAGS. |
138 | * |
138 | * |
139 | * @return Old interrupt priority level. |
139 | * @return Old interrupt priority level. |
140 | */ |
140 | */ |
141 | static inline ipl_t interrupts_enable(void) { |
141 | static inline ipl_t interrupts_enable(void) |
- | 142 | { |
|
142 | ipl_t v; |
143 | ipl_t v; |
143 | __asm__ volatile ( |
144 | __asm__ volatile ( |
144 | "pushf\n\t" |
145 | "pushf\n\t" |
145 | "popl %0\n\t" |
146 | "popl %0\n\t" |
146 | "sti\n" |
147 | "sti\n" |
147 | : "=r" (v) |
148 | : "=r" (v) |
148 | ); |
149 | ); |
149 | return v; |
150 | return v; |
150 | } |
151 | } |
151 | 152 | ||
152 | /** Disable interrupts. |
153 | /** Disable interrupts. |
153 | * |
154 | * |
154 | * Disable interrupts and return previous |
155 | * Disable interrupts and return previous |
155 | * value of EFLAGS. |
156 | * value of EFLAGS. |
156 | * |
157 | * |
157 | * @return Old interrupt priority level. |
158 | * @return Old interrupt priority level. |
158 | */ |
159 | */ |
159 | static inline ipl_t interrupts_disable(void) { |
160 | static inline ipl_t interrupts_disable(void) |
- | 161 | { |
|
160 | ipl_t v; |
162 | ipl_t v; |
161 | __asm__ volatile ( |
163 | __asm__ volatile ( |
162 | "pushf\n\t" |
164 | "pushf\n\t" |
163 | "popl %0\n\t" |
165 | "popl %0\n\t" |
164 | "cli\n" |
166 | "cli\n" |
165 | : "=r" (v) |
167 | : "=r" (v) |
166 | ); |
168 | ); |
167 | return v; |
169 | return v; |
168 | } |
170 | } |
169 | 171 | ||
170 | /** Restore interrupt priority level. |
172 | /** Restore interrupt priority level. |
171 | * |
173 | * |
172 | * Restore EFLAGS. |
174 | * Restore EFLAGS. |
173 | * |
175 | * |
174 | * @param ipl Saved interrupt priority level. |
176 | * @param ipl Saved interrupt priority level. |
175 | */ |
177 | */ |
176 | static inline void interrupts_restore(ipl_t ipl) { |
178 | static inline void interrupts_restore(ipl_t ipl) |
- | 179 | { |
|
177 | __asm__ volatile ( |
180 | __asm__ volatile ( |
178 | "pushl %0\n\t" |
181 | "pushl %0\n\t" |
179 | "popf\n" |
182 | "popf\n" |
180 | : : "r" (ipl) |
183 | : : "r" (ipl) |
181 | ); |
184 | ); |
182 | } |
185 | } |
183 | 186 | ||
184 | /** Return interrupt priority level. |
187 | /** Return interrupt priority level. |
185 | * |
188 | * |
186 | * @return EFLAFS. |
189 | * @return EFLAFS. |
187 | */ |
190 | */ |
188 | static inline ipl_t interrupts_read(void) { |
191 | static inline ipl_t interrupts_read(void) |
- | 192 | { |
|
189 | ipl_t v; |
193 | ipl_t v; |
190 | __asm__ volatile ( |
194 | __asm__ volatile ( |
191 | "pushf\n\t" |
195 | "pushf\n\t" |
192 | "popl %0\n" |
196 | "popl %0\n" |
193 | : "=r" (v) |
197 | : "=r" (v) |
194 | ); |
198 | ); |
195 | return v; |
199 | return v; |
196 | } |
200 | } |
197 | 201 | ||
198 | /** Return base address of current stack |
202 | /** Return base address of current stack |
199 | * |
203 | * |
200 | * Return the base address of the current stack. |
204 | * Return the base address of the current stack. |
201 | * The stack is assumed to be STACK_SIZE bytes long. |
205 | * The stack is assumed to be STACK_SIZE bytes long. |
202 | * The stack must start on page boundary. |
206 | * The stack must start on page boundary. |
203 | */ |
207 | */ |
204 | static inline __address get_stack_base(void) |
208 | static inline __address get_stack_base(void) |
205 | { |
209 | { |
206 | __address v; |
210 | __address v; |
207 | 211 | ||
208 | __asm__ volatile ("andl %%esp, %0\n" : "=r" (v) : "0" (~(STACK_SIZE-1))); |
212 | __asm__ volatile ("andl %%esp, %0\n" : "=r" (v) : "0" (~(STACK_SIZE-1))); |
209 | 213 | ||
210 | return v; |
214 | return v; |
211 | } |
215 | } |
212 | 216 | ||
213 | static inline __u64 rdtsc(void) |
217 | static inline __u64 rdtsc(void) |
214 | { |
218 | { |
215 | __u64 v; |
219 | __u64 v; |
216 | 220 | ||
217 | __asm__ volatile("rdtsc\n" : "=A" (v)); |
221 | __asm__ volatile("rdtsc\n" : "=A" (v)); |
218 | 222 | ||
219 | return v; |
223 | return v; |
220 | } |
224 | } |
221 | 225 | ||
222 | #endif |
226 | #endif |
223 | 227 |