1 | /*
|
---|
2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
3 | * Copyright (c) 2005 Sergey Bondari
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
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
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
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
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
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
|
---|
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
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup ia32
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef KERN_ia32_ASM_H_
|
---|
37 | #define KERN_ia32_ASM_H_
|
---|
38 |
|
---|
39 | #include <arch/pm.h>
|
---|
40 | #include <arch/cpu.h>
|
---|
41 | #include <typedefs.h>
|
---|
42 | #include <config.h>
|
---|
43 | #include <trace.h>
|
---|
44 |
|
---|
45 | /** Halt CPU
|
---|
46 | *
|
---|
47 | * Halt the current CPU.
|
---|
48 | *
|
---|
49 | */
|
---|
50 | NO_TRACE static inline __attribute__((noreturn)) void cpu_halt(void)
|
---|
51 | {
|
---|
52 | while (true) {
|
---|
53 | asm volatile (
|
---|
54 | "hlt\n"
|
---|
55 | );
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | NO_TRACE static inline void cpu_sleep(void)
|
---|
60 | {
|
---|
61 | asm volatile (
|
---|
62 | "hlt\n"
|
---|
63 | );
|
---|
64 | }
|
---|
65 |
|
---|
66 | #define GEN_READ_REG(reg) NO_TRACE static inline sysarg_t read_ ##reg (void) \
|
---|
67 | { \
|
---|
68 | sysarg_t res; \
|
---|
69 | asm volatile ( \
|
---|
70 | "movl %%" #reg ", %[res]" \
|
---|
71 | : [res] "=r" (res) \
|
---|
72 | ); \
|
---|
73 | return res; \
|
---|
74 | }
|
---|
75 |
|
---|
76 | #define GEN_WRITE_REG(reg) NO_TRACE static inline void write_ ##reg (sysarg_t regn) \
|
---|
77 | { \
|
---|
78 | asm volatile ( \
|
---|
79 | "movl %[regn], %%" #reg \
|
---|
80 | :: [regn] "r" (regn) \
|
---|
81 | ); \
|
---|
82 | }
|
---|
83 |
|
---|
84 | GEN_READ_REG(cr0)
|
---|
85 | GEN_READ_REG(cr2)
|
---|
86 | GEN_READ_REG(cr3)
|
---|
87 | GEN_WRITE_REG(cr3)
|
---|
88 |
|
---|
89 | GEN_READ_REG(dr0)
|
---|
90 | GEN_READ_REG(dr1)
|
---|
91 | GEN_READ_REG(dr2)
|
---|
92 | GEN_READ_REG(dr3)
|
---|
93 | GEN_READ_REG(dr6)
|
---|
94 | GEN_READ_REG(dr7)
|
---|
95 |
|
---|
96 | GEN_WRITE_REG(dr0)
|
---|
97 | GEN_WRITE_REG(dr1)
|
---|
98 | GEN_WRITE_REG(dr2)
|
---|
99 | GEN_WRITE_REG(dr3)
|
---|
100 | GEN_WRITE_REG(dr6)
|
---|
101 | GEN_WRITE_REG(dr7)
|
---|
102 |
|
---|
103 | /** Byte to port
|
---|
104 | *
|
---|
105 | * Output byte to port
|
---|
106 | *
|
---|
107 | * @param port Port to write to
|
---|
108 | * @param val Value to write
|
---|
109 | *
|
---|
110 | */
|
---|
111 | NO_TRACE static inline void pio_write_8(ioport8_t *port, uint8_t val)
|
---|
112 | {
|
---|
113 | asm volatile (
|
---|
114 | "outb %b[val], %w[port]\n"
|
---|
115 | :: [val] "a" (val),
|
---|
116 | [port] "d" (port)
|
---|
117 | );
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Word to port
|
---|
121 | *
|
---|
122 | * Output word to port
|
---|
123 | *
|
---|
124 | * @param port Port to write to
|
---|
125 | * @param val Value to write
|
---|
126 | *
|
---|
127 | */
|
---|
128 | NO_TRACE static inline void pio_write_16(ioport16_t *port, uint16_t val)
|
---|
129 | {
|
---|
130 | asm volatile (
|
---|
131 | "outw %w[val], %w[port]\n"
|
---|
132 | :: [val] "a" (val),
|
---|
133 | [port] "d" (port)
|
---|
134 | );
|
---|
135 | }
|
---|
136 |
|
---|
137 | /** Double word to port
|
---|
138 | *
|
---|
139 | * Output double word to port
|
---|
140 | *
|
---|
141 | * @param port Port to write to
|
---|
142 | * @param val Value to write
|
---|
143 | *
|
---|
144 | */
|
---|
145 | NO_TRACE static inline void pio_write_32(ioport32_t *port, uint32_t val)
|
---|
146 | {
|
---|
147 | asm volatile (
|
---|
148 | "outl %[val], %w[port]\n"
|
---|
149 | :: [val] "a" (val),
|
---|
150 | [port] "d" (port)
|
---|
151 | );
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Byte from port
|
---|
155 | *
|
---|
156 | * Get byte from port
|
---|
157 | *
|
---|
158 | * @param port Port to read from
|
---|
159 | * @return Value read
|
---|
160 | *
|
---|
161 | */
|
---|
162 | NO_TRACE static inline uint8_t pio_read_8(ioport8_t *port)
|
---|
163 | {
|
---|
164 | uint8_t val;
|
---|
165 |
|
---|
166 | asm volatile (
|
---|
167 | "inb %w[port], %b[val]\n"
|
---|
168 | : [val] "=a" (val)
|
---|
169 | : [port] "d" (port)
|
---|
170 | );
|
---|
171 |
|
---|
172 | return val;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /** Word from port
|
---|
176 | *
|
---|
177 | * Get word from port
|
---|
178 | *
|
---|
179 | * @param port Port to read from
|
---|
180 | * @return Value read
|
---|
181 | *
|
---|
182 | */
|
---|
183 | NO_TRACE static inline uint16_t pio_read_16(ioport16_t *port)
|
---|
184 | {
|
---|
185 | uint16_t val;
|
---|
186 |
|
---|
187 | asm volatile (
|
---|
188 | "inw %w[port], %w[val]\n"
|
---|
189 | : [val] "=a" (val)
|
---|
190 | : [port] "d" (port)
|
---|
191 | );
|
---|
192 |
|
---|
193 | return val;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** Double word from port
|
---|
197 | *
|
---|
198 | * Get double word from port
|
---|
199 | *
|
---|
200 | * @param port Port to read from
|
---|
201 | * @return Value read
|
---|
202 | *
|
---|
203 | */
|
---|
204 | NO_TRACE static inline uint32_t pio_read_32(ioport32_t *port)
|
---|
205 | {
|
---|
206 | uint32_t val;
|
---|
207 |
|
---|
208 | asm volatile (
|
---|
209 | "inl %w[port], %[val]\n"
|
---|
210 | : [val] "=a" (val)
|
---|
211 | : [port] "d" (port)
|
---|
212 | );
|
---|
213 |
|
---|
214 | return val;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** Enable interrupts.
|
---|
218 | *
|
---|
219 | * Enable interrupts and return previous
|
---|
220 | * value of EFLAGS.
|
---|
221 | *
|
---|
222 | * @return Old interrupt priority level.
|
---|
223 | *
|
---|
224 | */
|
---|
225 | NO_TRACE static inline ipl_t interrupts_enable(void)
|
---|
226 | {
|
---|
227 | ipl_t v;
|
---|
228 |
|
---|
229 | asm volatile (
|
---|
230 | "pushf\n"
|
---|
231 | "popl %[v]\n"
|
---|
232 | "sti\n"
|
---|
233 | : [v] "=r" (v)
|
---|
234 | );
|
---|
235 |
|
---|
236 | return v;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /** Disable interrupts.
|
---|
240 | *
|
---|
241 | * Disable interrupts and return previous
|
---|
242 | * value of EFLAGS.
|
---|
243 | *
|
---|
244 | * @return Old interrupt priority level.
|
---|
245 | *
|
---|
246 | */
|
---|
247 | NO_TRACE static inline ipl_t interrupts_disable(void)
|
---|
248 | {
|
---|
249 | ipl_t v;
|
---|
250 |
|
---|
251 | asm volatile (
|
---|
252 | "pushf\n"
|
---|
253 | "popl %[v]\n"
|
---|
254 | "cli\n"
|
---|
255 | : [v] "=r" (v)
|
---|
256 | );
|
---|
257 |
|
---|
258 | return v;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /** Restore interrupt priority level.
|
---|
262 | *
|
---|
263 | * Restore EFLAGS.
|
---|
264 | *
|
---|
265 | * @param ipl Saved interrupt priority level.
|
---|
266 | *
|
---|
267 | */
|
---|
268 | NO_TRACE static inline void interrupts_restore(ipl_t ipl)
|
---|
269 | {
|
---|
270 | asm volatile (
|
---|
271 | "pushl %[ipl]\n"
|
---|
272 | "popf\n"
|
---|
273 | :: [ipl] "r" (ipl)
|
---|
274 | );
|
---|
275 | }
|
---|
276 |
|
---|
277 | /** Return interrupt priority level.
|
---|
278 | *
|
---|
279 | * @return EFLAFS.
|
---|
280 | *
|
---|
281 | */
|
---|
282 | NO_TRACE static inline ipl_t interrupts_read(void)
|
---|
283 | {
|
---|
284 | ipl_t v;
|
---|
285 |
|
---|
286 | asm volatile (
|
---|
287 | "pushf\n"
|
---|
288 | "popl %[v]\n"
|
---|
289 | : [v] "=r" (v)
|
---|
290 | );
|
---|
291 |
|
---|
292 | return v;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /** Check interrupts state.
|
---|
296 | *
|
---|
297 | * @return True if interrupts are disabled.
|
---|
298 | *
|
---|
299 | */
|
---|
300 | NO_TRACE static inline bool interrupts_disabled(void)
|
---|
301 | {
|
---|
302 | ipl_t v;
|
---|
303 |
|
---|
304 | asm volatile (
|
---|
305 | "pushf\n"
|
---|
306 | "popl %[v]\n"
|
---|
307 | : [v] "=r" (v)
|
---|
308 | );
|
---|
309 |
|
---|
310 | return ((v & EFLAGS_IF) == 0);
|
---|
311 | }
|
---|
312 |
|
---|
313 | /** Write to MSR */
|
---|
314 | NO_TRACE static inline void write_msr(uint32_t msr, uint64_t value)
|
---|
315 | {
|
---|
316 | asm volatile (
|
---|
317 | "wrmsr"
|
---|
318 | :: "c" (msr),
|
---|
319 | "a" ((uint32_t) (value)),
|
---|
320 | "d" ((uint32_t) (value >> 32))
|
---|
321 | );
|
---|
322 | }
|
---|
323 |
|
---|
324 | NO_TRACE static inline uint64_t read_msr(uint32_t msr)
|
---|
325 | {
|
---|
326 | uint32_t ax, dx;
|
---|
327 |
|
---|
328 | asm volatile (
|
---|
329 | "rdmsr"
|
---|
330 | : "=a" (ax),
|
---|
331 | "=d" (dx)
|
---|
332 | : "c" (msr)
|
---|
333 | );
|
---|
334 |
|
---|
335 | return ((uint64_t) dx << 32) | ax;
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | /** Return base address of current stack
|
---|
340 | *
|
---|
341 | * Return the base address of the current stack.
|
---|
342 | * The stack is assumed to be STACK_SIZE bytes long.
|
---|
343 | * The stack must start on page boundary.
|
---|
344 | *
|
---|
345 | */
|
---|
346 | NO_TRACE static inline uintptr_t get_stack_base(void)
|
---|
347 | {
|
---|
348 | uintptr_t v;
|
---|
349 |
|
---|
350 | asm volatile (
|
---|
351 | "andl %%esp, %[v]\n"
|
---|
352 | : [v] "=r" (v)
|
---|
353 | : "0" (~(STACK_SIZE - 1))
|
---|
354 | );
|
---|
355 |
|
---|
356 | return v;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /** Invalidate TLB Entry.
|
---|
360 | *
|
---|
361 | * @param addr Address on a page whose TLB entry is to be invalidated.
|
---|
362 | *
|
---|
363 | */
|
---|
364 | NO_TRACE static inline void invlpg(uintptr_t addr)
|
---|
365 | {
|
---|
366 | asm volatile (
|
---|
367 | "invlpg %[addr]\n"
|
---|
368 | :: [addr] "m" (*(sysarg_t *) addr)
|
---|
369 | );
|
---|
370 | }
|
---|
371 |
|
---|
372 | /** Load GDTR register from memory.
|
---|
373 | *
|
---|
374 | * @param gdtr_reg Address of memory from where to load GDTR.
|
---|
375 | *
|
---|
376 | */
|
---|
377 | NO_TRACE static inline void gdtr_load(ptr_16_32_t *gdtr_reg)
|
---|
378 | {
|
---|
379 | asm volatile (
|
---|
380 | "lgdtl %[gdtr_reg]\n"
|
---|
381 | :: [gdtr_reg] "m" (*gdtr_reg)
|
---|
382 | );
|
---|
383 | }
|
---|
384 |
|
---|
385 | /** Store GDTR register to memory.
|
---|
386 | *
|
---|
387 | * @param gdtr_reg Address of memory to where to load GDTR.
|
---|
388 | *
|
---|
389 | */
|
---|
390 | NO_TRACE static inline void gdtr_store(ptr_16_32_t *gdtr_reg)
|
---|
391 | {
|
---|
392 | asm volatile (
|
---|
393 | "sgdtl %[gdtr_reg]\n"
|
---|
394 | : [gdtr_reg] "=m" (*gdtr_reg)
|
---|
395 | );
|
---|
396 | }
|
---|
397 |
|
---|
398 | /** Load IDTR register from memory.
|
---|
399 | *
|
---|
400 | * @param idtr_reg Address of memory from where to load IDTR.
|
---|
401 | *
|
---|
402 | */
|
---|
403 | NO_TRACE static inline void idtr_load(ptr_16_32_t *idtr_reg)
|
---|
404 | {
|
---|
405 | asm volatile (
|
---|
406 | "lidtl %[idtr_reg]\n"
|
---|
407 | :: [idtr_reg] "m" (*idtr_reg)
|
---|
408 | );
|
---|
409 | }
|
---|
410 |
|
---|
411 | /** Load TR from descriptor table.
|
---|
412 | *
|
---|
413 | * @param sel Selector specifying descriptor of TSS segment.
|
---|
414 | *
|
---|
415 | */
|
---|
416 | NO_TRACE static inline void tr_load(uint16_t sel)
|
---|
417 | {
|
---|
418 | asm volatile (
|
---|
419 | "ltr %[sel]"
|
---|
420 | :: [sel] "r" (sel)
|
---|
421 | );
|
---|
422 | }
|
---|
423 |
|
---|
424 | extern void paging_on(void);
|
---|
425 | extern void enable_l_apic_in_msr(void);
|
---|
426 |
|
---|
427 | extern void asm_delay_loop(uint32_t);
|
---|
428 | extern void asm_fake_loop(uint32_t);
|
---|
429 |
|
---|
430 | extern uintptr_t int_syscall;
|
---|
431 |
|
---|
432 | extern uintptr_t int_0;
|
---|
433 | extern uintptr_t int_1;
|
---|
434 | extern uintptr_t int_2;
|
---|
435 | extern uintptr_t int_3;
|
---|
436 | extern uintptr_t int_4;
|
---|
437 | extern uintptr_t int_5;
|
---|
438 | extern uintptr_t int_6;
|
---|
439 | extern uintptr_t int_7;
|
---|
440 | extern uintptr_t int_8;
|
---|
441 | extern uintptr_t int_9;
|
---|
442 | extern uintptr_t int_10;
|
---|
443 | extern uintptr_t int_11;
|
---|
444 | extern uintptr_t int_12;
|
---|
445 | extern uintptr_t int_13;
|
---|
446 | extern uintptr_t int_14;
|
---|
447 | extern uintptr_t int_15;
|
---|
448 | extern uintptr_t int_16;
|
---|
449 | extern uintptr_t int_17;
|
---|
450 | extern uintptr_t int_18;
|
---|
451 | extern uintptr_t int_19;
|
---|
452 | extern uintptr_t int_20;
|
---|
453 | extern uintptr_t int_21;
|
---|
454 | extern uintptr_t int_22;
|
---|
455 | extern uintptr_t int_23;
|
---|
456 | extern uintptr_t int_24;
|
---|
457 | extern uintptr_t int_25;
|
---|
458 | extern uintptr_t int_26;
|
---|
459 | extern uintptr_t int_27;
|
---|
460 | extern uintptr_t int_28;
|
---|
461 | extern uintptr_t int_29;
|
---|
462 | extern uintptr_t int_30;
|
---|
463 | extern uintptr_t int_31;
|
---|
464 | extern uintptr_t int_32;
|
---|
465 | extern uintptr_t int_33;
|
---|
466 | extern uintptr_t int_34;
|
---|
467 | extern uintptr_t int_35;
|
---|
468 | extern uintptr_t int_36;
|
---|
469 | extern uintptr_t int_37;
|
---|
470 | extern uintptr_t int_38;
|
---|
471 | extern uintptr_t int_39;
|
---|
472 | extern uintptr_t int_40;
|
---|
473 | extern uintptr_t int_41;
|
---|
474 | extern uintptr_t int_42;
|
---|
475 | extern uintptr_t int_43;
|
---|
476 | extern uintptr_t int_44;
|
---|
477 | extern uintptr_t int_45;
|
---|
478 | extern uintptr_t int_46;
|
---|
479 | extern uintptr_t int_47;
|
---|
480 | extern uintptr_t int_48;
|
---|
481 | extern uintptr_t int_49;
|
---|
482 | extern uintptr_t int_50;
|
---|
483 | extern uintptr_t int_51;
|
---|
484 | extern uintptr_t int_52;
|
---|
485 | extern uintptr_t int_53;
|
---|
486 | extern uintptr_t int_54;
|
---|
487 | extern uintptr_t int_55;
|
---|
488 | extern uintptr_t int_56;
|
---|
489 | extern uintptr_t int_57;
|
---|
490 | extern uintptr_t int_58;
|
---|
491 | extern uintptr_t int_59;
|
---|
492 | extern uintptr_t int_60;
|
---|
493 | extern uintptr_t int_61;
|
---|
494 | extern uintptr_t int_62;
|
---|
495 | extern uintptr_t int_63;
|
---|
496 |
|
---|
497 | #endif
|
---|
498 |
|
---|
499 | /** @}
|
---|
500 | */
|
---|