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