source: mainline/kernel/arch/ia32/include/asm.h@ a0fc4be

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a0fc4be was 1c99eae, checked in by Martin Decky <martin@…>, 14 years ago

cstyle
(no change in functionality)

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